-
Notifications
You must be signed in to change notification settings - Fork 296
/
tests.rs
2442 lines (2164 loc) · 79.2 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use assert_matches::assert_matches;
use ic_base_types::PrincipalId;
use ic_config::flag_status::FlagStatus;
use ic_error_types::{ErrorCode, UserError};
use ic_registry_routing_table::{CanisterIdRange, RoutingTable};
use ic_replicated_state::canister_state::system_state::wasm_chunk_store;
use ic_replicated_state::{ExecutionTask, ReplicatedState};
use ic_state_machine_tests::{IngressState, IngressStatus};
use ic_types::{
CanisterId, ComputeAllocation, Cycles, MemoryAllocation, NumBytes, NumInstructions,
};
use ic_ic00_types::{
CanisterChange, CanisterChangeDetails, CanisterChangeOrigin, CanisterInstallMode,
CanisterInstallModeV2, EmptyBlob, InstallChunkedCodeArgs, InstallCodeArgs, InstallCodeArgsV2,
Method, Payload, UploadChunkArgs, UploadChunkReply,
};
use ic_replicated_state::canister_state::NextExecution;
use ic_test_utilities_execution_environment::{
check_ingress_status, get_reply, ExecutionTest, ExecutionTestBuilder,
};
use ic_test_utilities_metrics::fetch_int_counter;
use ic_types::messages::MessageId;
use ic_types::{ingress::WasmResult, MAX_STABLE_MEMORY_IN_BYTES, MAX_WASM_MEMORY_IN_BYTES};
use ic_types_test_utils::ids::user_test_id;
use ic_types_test_utils::ids::{canister_test_id, subnet_test_id};
use ic_universal_canister::{call_args, wasm, UNIVERSAL_CANISTER_WASM};
use maplit::btreemap;
use std::mem::size_of;
const DTS_INSTALL_WAT: &str = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(import "ic0" "stable_read"
(func $stable_read (param $dst i32) (param $offset i32) (param $size i32))
)
(import "ic0" "stable_write"
(func $stable_write (param $offset i32) (param $src i32) (param $size i32))
)
(import "ic0" "msg_reply" (func $msg_reply))
(import "ic0" "msg_reply_data_append"
(func $msg_reply_data_append (param i32 i32))
)
(func (export "canister_query read")
(call $stable_read (i32.const 0) (i32.const 0) (i32.const 10))
(call $msg_reply_data_append
(i32.const 0) ;; the counter from heap[0]
(i32.const 10)) ;; length
(call $msg_reply)
)
(func $start
(drop (memory.grow (i32.const 1)))
(memory.fill (i32.const 0) (i32.const 12) (i32.const 1000))
(memory.fill (i32.const 0) (i32.const 23) (i32.const 1000))
)
(func (export "canister_init")
(drop (memory.grow (i32.const 1)))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
)
(start $start)
(memory 0 20)
)"#;
#[test]
fn install_code_fails_on_invalid_compute_allocation() {
let mut test = ExecutionTestBuilder::new().build();
let binary = wat::parse_str("(module)").unwrap();
let canister = test.create_canister(Cycles::new(1_000_000_000_000));
let err = test
.install_canister_with_allocation(canister, binary, Some(1_000), None)
.unwrap_err();
assert_eq!(ErrorCode::CanisterContractViolation, err.code());
assert_eq!(
"ComputeAllocation expected to be in the range [0..100], got 1_000",
err.description()
);
}
#[test]
fn install_code_fails_on_invalid_memory_allocation() {
let mut test = ExecutionTestBuilder::new().build();
let binary = wat::parse_str("(module)").unwrap();
let canister = test.create_canister(Cycles::new(1_000_000_000_000));
let err = test
.install_canister_with_allocation(canister, binary, None, Some(u64::MAX))
.unwrap_err();
assert_eq!(ErrorCode::CanisterContractViolation, err.code());
assert_eq!(
format!(
"MemoryAllocation expected to be in the range [0..{}], got 18_446_744_073_709_551_615",
candid::Nat((MAX_STABLE_MEMORY_IN_BYTES + MAX_WASM_MEMORY_IN_BYTES).into())
),
err.description()
);
}
#[test]
fn dts_resume_works_in_install_code() {
const INSTRUCTION_LIMIT: u64 = 3_000_000;
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(INSTRUCTION_LIMIT)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let canister_id = test.create_canister(Cycles::new(1_000_000_000_000_000));
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(DTS_INSTALL_WAT).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
let original_system_state = test.canister_state(canister_id).system_state.clone();
let original_execution_cost = test.canister_execution_cost(canister_id);
let ingress_id = test.dts_install_code(payload);
for _ in 0..4 {
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::ContinueInstallCode
);
assert_eq!(
test.canister_state(canister_id).system_state.balance(),
original_system_state.balance()
- test
.cycles_account_manager()
.execution_cost(NumInstructions::from(INSTRUCTION_LIMIT), test.subnet_size()),
);
test.execute_slice(canister_id);
}
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None
);
assert_eq!(
test.canister_state(canister_id).system_state.balance(),
original_system_state.balance()
- (test.canister_execution_cost(canister_id) - original_execution_cost)
);
let ingress_status = test.ingress_status(&ingress_id);
let result = check_ingress_status(ingress_status).unwrap();
assert_eq!(result, WasmResult::Reply(EmptyBlob.encode()));
}
#[test]
fn dts_abort_works_in_install_code() {
const INSTRUCTION_LIMIT: u64 = 3_000_000;
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(INSTRUCTION_LIMIT)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let canister_id = test.create_canister(Cycles::new(1_000_000_000_000_000));
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(DTS_INSTALL_WAT).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
let original_system_state = test.canister_state(canister_id).system_state.clone();
let original_execution_cost = test.canister_execution_cost(canister_id);
let ingress_id = test.dts_install_code(payload);
for _ in 0..3 {
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::ContinueInstallCode
);
assert_eq!(
test.canister_state(canister_id).system_state.balance(),
original_system_state.balance()
- test
.cycles_account_manager()
.execution_cost(NumInstructions::from(INSTRUCTION_LIMIT), test.subnet_size()),
);
test.execute_slice(canister_id);
}
test.abort_all_paused_executions();
assert_eq!(
fetch_int_counter(test.metrics_registry(), "executions_aborted"),
Some(1)
);
for _ in 0..5 {
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::ContinueInstallCode
);
assert_eq!(
test.canister_state(canister_id).system_state.balance(),
original_system_state.balance()
- test
.cycles_account_manager()
.execution_cost(NumInstructions::from(INSTRUCTION_LIMIT), test.subnet_size()),
);
test.execute_slice(canister_id);
}
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None,
);
assert_eq!(
test.canister_state(canister_id).system_state.balance(),
original_system_state.balance()
- (test.canister_execution_cost(canister_id) - original_execution_cost)
);
let ingress_status = test.ingress_status(&ingress_id);
let result = check_ingress_status(ingress_status).unwrap();
assert_eq!(result, WasmResult::Reply(EmptyBlob.encode()));
}
#[test]
fn install_code_validate_input_compute_allocation() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
test.create_canister_with_allocation(Cycles::new(2_000_000_000_000_000), Some(50), None)
.unwrap();
let canister_id = test
.create_canister_with_allocation(Cycles::new(2_000_000_000_000_000), Some(40), None)
.unwrap();
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(DTS_INSTALL_WAT).unwrap(),
arg: vec![],
compute_allocation: Some(candid::Nat::from(90u64)),
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
let message_id = test.subnet_message_raw(Method::InstallCode, payload.encode());
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None,
);
// Start execution of install code.
test.execute_subnet_message();
let result = check_ingress_status(test.ingress_status(&message_id));
assert_eq!(
result,
Err(UserError::new(ErrorCode::SubnetOversubscribed, "Canister requested a compute allocation of 90% which cannot be satisfied because the Subnet's remaining compute capacity is 49%"))
);
}
#[test]
fn install_code_validate_input_memory_allocation() {
let mib: u64 = 1024 * 1024;
let mut test = ExecutionTestBuilder::new()
.with_subnet_execution_memory(500 * mib as i64)
.with_subnet_memory_reservation(0)
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
test.create_canister_with_allocation(
Cycles::new(20_000_000_000_000_000),
None,
Some(250 * mib),
)
.unwrap();
let canister_id = test
.create_canister_with_allocation(
Cycles::new(20_000_000_000_000_000),
Some(40),
Some(240 * mib),
)
.unwrap();
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: vec![],
arg: vec![],
compute_allocation: None,
memory_allocation: Some(candid::Nat::from(260 * mib)),
query_allocation: None,
sender_canister_version: None,
};
let message_id = test.subnet_message_raw(Method::InstallCode, payload.encode());
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None,
);
// Start execution of install code.
test.execute_subnet_message();
let result = check_ingress_status(test.ingress_status(&message_id));
assert_eq!(
result,
Err(UserError::new(ErrorCode::SubnetOversubscribed, "Canister requested 260.00 MiB of memory but only 250.00 MiB are available in the subnet"))
);
}
#[test]
fn install_code_validate_input_controller() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let controller = user_test_id(1);
test.set_user_id(controller);
let canister_id = test.create_canister(Cycles::new(1_000_000_000_000_000));
let sender = user_test_id(2);
test.set_user_id(sender);
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(DTS_INSTALL_WAT).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
// Install code from a non-controller.
let message_id = test.subnet_message_raw(Method::InstallCode, payload.encode());
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None,
);
test.execute_subnet_message();
let result = check_ingress_status(test.ingress_status(&message_id));
assert_eq!(
result,
Err(UserError::new(
ErrorCode::CanisterInvalidController,
format!("Only the controllers of the canister {} can control it.\nCanister's controllers: {}\nSender's ID: {}", canister_id, controller, sender)
))
);
}
#[test]
fn install_code_validates_execution_state() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let mut payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(r#"(module (memory 0 20))"#).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
// Install code on empty canister.
assert!(test
.subnet_message(Method::InstallCode, payload.encode())
.is_ok());
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None,
);
payload.wasm_module = wat::parse_str(DTS_INSTALL_WAT).unwrap();
// Install code on non-empty canister fails.
let message_id = test.subnet_message_raw(Method::InstallCode, payload.encode());
test.execute_subnet_message();
let result = check_ingress_status(test.ingress_status(&message_id));
assert_eq!(result,
Err(UserError::new(
ErrorCode::CanisterNonEmpty,
format!("Canister {} cannot be installed because the canister is not empty. Try installing with mode='reinstall' instead.", canister_id)))
);
}
#[test]
fn install_code_fails_when_not_enough_wasm_custom_sections_memory() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000_000)
.with_install_code_slice_instruction_limit(1_000_000_000)
.with_subnet_wasm_custom_sections_memory(32)
.with_manual_execution()
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: include_bytes!("../../../tests/test-data/custom_sections.wasm").to_vec(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
// Install code on canister with Wasm sections that are larger than the available memory on the subnet.
let message_id = test.subnet_message_raw(Method::InstallCode, payload.encode());
test.execute_subnet_message();
let result = check_ingress_status(test.ingress_status(&message_id));
assert!(result.is_err());
}
#[test]
fn install_code_succeeds_with_enough_wasm_custom_sections_memory() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000_000)
.with_install_code_slice_instruction_limit(1_000_000_000)
.with_subnet_wasm_custom_sections_memory(1024 * 1024) // 1MiB
.with_manual_execution()
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: include_bytes!("../../../tests/test-data/custom_sections.wasm").to_vec(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
// Install code on canister with Wasm sections that fit in the available memory on the subnet.
assert!(test
.subnet_message(Method::InstallCode, payload.encode())
.is_ok());
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None,
);
}
#[test]
fn install_code_respects_wasm_custom_sections_available_memory() {
// As we install canisters in a loop, using more memory spawns thousands of
// canister sandboxes, which lead to a few GiB memory usage.
let available_wasm_custom_sections_memory = 20 * 1024; // 20KiB
// This value might need adjustment if something changes in the canister's
// wasm that gets installed in the test.
let total_memory_taken_per_canister_in_bytes = 364249;
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000_000)
.with_install_code_slice_instruction_limit(1_000_000_000)
.with_subnet_wasm_custom_sections_memory(available_wasm_custom_sections_memory)
.with_manual_execution()
.build();
let subnet_available_memory_before = test.subnet_available_memory().get_execution_memory();
let mut iterations = 0;
loop {
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: include_bytes!("../../../tests/test-data/custom_sections.wasm").to_vec(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
if test
.subnet_message(Method::InstallCode, payload.encode())
.is_err()
{
// We expect that at some point there is not enough wasm custom sections
// memory left on the subnet and the request to install the canister
// will fail.
break;
}
iterations += 1;
}
// One more request to install a canister with wasm custom sections should fail.
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: include_bytes!("../../../tests/test-data/custom_sections.wasm").to_vec(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
let result = test.subnet_message(Method::InstallCode, payload.encode());
assert!(result.is_err());
assert_eq!(
test.subnet_available_memory().get_execution_memory(),
subnet_available_memory_before - iterations * total_memory_taken_per_canister_in_bytes
);
}
fn execute_install_code_message_dts_helper(
test: &mut ExecutionTest,
canister_id: CanisterId,
wasm: &str,
) -> MessageId {
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(wasm).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
// Send install code message.
let message_id = test.subnet_message_raw(Method::InstallCode, payload.encode());
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None,
);
// Start execution of install code.
let original_system_state = test.canister_state(canister_id).system_state.clone();
test.execute_subnet_message();
// Execute all slices.
for _ in 0..2 {
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::ContinueInstallCode
);
assert_eq!(
test.canister_state(canister_id).system_state.balance(),
original_system_state.balance()
- test
.cycles_account_manager()
.execution_cost(NumInstructions::from(1_000_000), test.subnet_size()),
);
test.execute_slice(canister_id);
}
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None
);
message_id
}
#[test]
fn install_code_with_start_with_err() {
let mut test = ExecutionTestBuilder::new()
.with_instruction_limit(1_000_000)
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let wasm: &str = r#"
(module
(func $start
(drop (memory.grow (i32.const 1)))
(memory.fill (i32.const 0) (i32.const 13) (i32.const 1000))
(memory.fill (i32.const 0) (i32.const 14) (i32.const 1000))
unreachable
)
(start $start)
(memory 0 20)
)"#;
let message_id = execute_install_code_message_dts_helper(&mut test, canister_id, wasm);
let result = check_ingress_status(test.ingress_status(&message_id));
assert_eq!(
result,
Err(UserError::new(
ErrorCode::CanisterTrapped,
format!("Canister {} trapped: unreachable", canister_id)
))
);
}
#[test]
fn install_code_with_start_with_success() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let wasm: &str = r#"
(module
(func $start
(drop (memory.grow (i32.const 1)))
(memory.fill (i32.const 0) (i32.const 13) (i32.const 1000))
(memory.fill (i32.const 0) (i32.const 14) (i32.const 1000))
)
(start $start)
(memory 0 20)
)"#;
let message_id = execute_install_code_message_dts_helper(&mut test, canister_id, wasm);
assert!(check_ingress_status(test.ingress_status(&message_id)).is_ok());
}
fn start_install_code_dts(
test: &mut ExecutionTest,
canister_id: CanisterId,
wasm: &str,
) -> MessageId {
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(wasm).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
// Send install code message.
let message_id = test.subnet_message_raw(Method::InstallCode, payload.encode());
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None,
);
// Start execution of install code.
test.execute_subnet_message();
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::ContinueInstallCode
);
message_id
}
fn execute_install_code_init_dts_helper(
test: &mut ExecutionTest,
canister_id: CanisterId,
wasm: &str,
) -> MessageId {
let message_id = start_install_code_dts(test, canister_id, wasm);
// Execute next slice.
test.execute_slice(canister_id);
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None
);
message_id
}
#[test]
fn install_code_with_init_method_with_error() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let wasm: &str = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_init")
(drop (memory.grow (i32.const 1)))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
(drop (call $stable_grow (i32.const 1)))
unreachable
)
(memory 0 20)
)"#;
let message_id = execute_install_code_init_dts_helper(&mut test, canister_id, wasm);
let result = check_ingress_status(test.ingress_status(&message_id));
assert_eq!(
result,
Err(UserError::new(
ErrorCode::CanisterTrapped,
format!("Canister {} trapped: unreachable", canister_id)
))
);
}
#[test]
fn install_code_with_init_method_success() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let wasm: &str = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_init")
(drop (memory.grow (i32.const 1)))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
(drop (call $stable_grow (i32.const 1)))
)
(memory 0 20)
)"#;
let message_id = execute_install_code_init_dts_helper(&mut test, canister_id, wasm);
assert!(check_ingress_status(test.ingress_status(&message_id)).is_ok());
}
#[test]
fn reserve_cycles_for_execution_fails_when_not_enough_cycles() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
// canister history memory usage at the beginning of attempted install
let canister_history_memory_usage = size_of::<CanisterChange>() + size_of::<PrincipalId>();
let freezing_threshold_cycles = test.cycles_account_manager().freeze_threshold_cycles(
ic_config::execution_environment::Config::default().default_freeze_threshold,
MemoryAllocation::BestEffort,
NumBytes::new(canister_history_memory_usage as u64),
NumBytes::new(0),
ComputeAllocation::zero(),
test.subnet_size(),
Cycles::zero(),
);
let canister_id = test.create_canister(Cycles::new(900_000) + freezing_threshold_cycles);
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(DTS_INSTALL_WAT).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
let original_balance = test.canister_state(canister_id).system_state.balance();
let message_id = test.dts_install_code(payload);
let minimum_balance = test.install_code_reserved_execution_cycles();
// Check reserve execution cycles fails due to not enough balance.
assert_eq!(
check_ingress_status(test.ingress_status(&message_id)),
Err(UserError::new(
ErrorCode::CanisterOutOfCycles,
format!("Canister installation failed with `Canister {} is out of cycles: please top up the canister with at least {} additional cycles`", canister_id, (freezing_threshold_cycles + minimum_balance) - original_balance)
))
);
}
#[test]
fn install_code_running_out_of_instructions() {
let mut test = ExecutionTestBuilder::new()
// Set the install message limit very low to hit it while executing.
.with_install_code_instruction_limit(1_500)
.with_install_code_slice_instruction_limit(1000)
.with_manual_execution()
.with_cost_to_compile_wasm_instruction(0)
.build();
let wasm: &str = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_init")
(drop (memory.grow (i32.const 1)))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
(drop (call $stable_grow (i32.const 1)))
unreachable
)
(memory 0 20)
)"#;
let canister_id = test.create_canister(Cycles::new(1_000_000_000_000_000));
let payload = InstallCodeArgs {
mode: CanisterInstallMode::Install,
canister_id: canister_id.get(),
wasm_module: wat::parse_str(wasm).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
// Send install code message and start execution.
let message_id = test.dts_install_code(payload);
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::ContinueInstallCode
);
test.execute_slice(canister_id);
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None
);
assert_eq!(
check_ingress_status(test.ingress_status(&message_id)),
Err(UserError::new(
ErrorCode::CanisterInstructionLimitExceeded,
format!(
"Canister {} exceeded the instruction limit for single message execution.",
canister_id
)
))
);
}
#[test]
fn dts_uninstall_with_aborted_install_code() {
let mut test = ExecutionTestBuilder::new()
.with_install_code_instruction_limit(1_000_000)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let wasm: &str = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_init")
(drop (memory.grow (i32.const 1)))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
(memory.fill (i32.const 0) (i32.const 34) (i32.const 1000))
)
(memory 0 20)
)"#;
let message_id = start_install_code_dts(&mut test, canister_id, wasm);
test.execute_slice(canister_id);
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::ContinueInstallCode,
);
test.abort_all_paused_executions();
test.uninstall_code(canister_id).unwrap();
while test.canister_state(canister_id).next_execution() == NextExecution::ContinueInstallCode {
test.execute_slice(canister_id);
}
let result = check_ingress_status(test.ingress_status(&message_id)).unwrap();
assert_eq!(result, WasmResult::Reply(EmptyBlob.encode()));
}
#[test]
fn dts_install_code_creates_entry_in_subnet_call_context_manager() {
const INSTRUCTION_LIMIT: u64 = 3_000_000;
let own_subnet = subnet_test_id(1);
let caller_canister = canister_test_id(1);
let mut test = ExecutionTestBuilder::new()
.with_own_subnet_id(own_subnet)
.with_install_code_instruction_limit(INSTRUCTION_LIMIT)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.with_caller(own_subnet, caller_canister)
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let controllers = vec![caller_canister.get(), test.user_id().get()];
test.canister_update_controller(canister_id, controllers)
.unwrap();
// SubnetCallContextManager does not contain any install code calls before executing the message.
assert_eq!(
test.state()
.metadata
.subnet_call_context_manager
.install_code_calls_len(),
0
);
let args = InstallCodeArgs {
canister_id: canister_id.get(),
mode: CanisterInstallMode::Install,
wasm_module: wat::parse_str(DTS_INSTALL_WAT).unwrap(),
arg: vec![],
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
test.inject_call_to_ic00(
Method::InstallCode,
args.encode(),
Cycles::new(1_000_000_000),
);
test.execute_subnet_message();
for _ in 0..4 {
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::ContinueInstallCode
);
// Check that the SubnetCallContextManager contains the call after paused execution.
assert_eq!(
test.state()
.metadata
.subnet_call_context_manager
.install_code_calls_len(),
1
);
test.execute_slice(canister_id);
}
assert_eq!(
test.canister_state(canister_id).next_execution(),
NextExecution::None
);
// Finished the execution of install code.
// Check that the SubnetCallContextManager does not contain the call anymore.
assert_eq!(
test.state()
.metadata
.subnet_call_context_manager
.install_code_calls_len(),
0
);
}
#[test]
fn subnet_call_context_manager_keeps_install_code_requests_when_abort() {
const INSTRUCTION_LIMIT: u64 = 3_000_000;
let own_subnet = subnet_test_id(1);
let caller_canister = canister_test_id(1);
let mut test = ExecutionTestBuilder::new()
.with_own_subnet_id(own_subnet)
.with_install_code_instruction_limit(INSTRUCTION_LIMIT)
.with_install_code_slice_instruction_limit(1_000)
.with_manual_execution()
.with_caller(own_subnet, caller_canister)
.build();
let canister_id = test
.create_canister_with_allocation(Cycles::new(1_000_000_000_000_000), None, None)
.unwrap();
let controllers = vec![caller_canister.get(), test.user_id().get()];
test.canister_update_controller(canister_id, controllers)
.unwrap();