-
Notifications
You must be signed in to change notification settings - Fork 296
/
tests.rs
6537 lines (6042 loc) · 236 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 candid::{Decode, Encode};
use ic_base_types::{NumSeconds, PrincipalId};
use ic_cycles_account_manager::ResourceSaturation;
use ic_embedders::wasm_utils::instrumentation::instruction_to_cost_new;
use ic_error_types::{ErrorCode, RejectCode};
use ic_ic00_types::{CanisterChange, CanisterHttpResponsePayload, SkipPreUpgrade};
use ic_interfaces::execution_environment::{HypervisorError, SubnetAvailableMemory};
use ic_nns_constants::CYCLES_MINTING_CANISTER_ID;
use ic_registry_subnet_type::SubnetType;
use ic_replicated_state::canister_state::{NextExecution, WASM_PAGE_SIZE_IN_BYTES};
use ic_replicated_state::testing::CanisterQueuesTesting;
use ic_replicated_state::{
canister_state::execution_state::CustomSectionType, ExportedFunctions, Global, PageIndex,
};
use ic_replicated_state::{CanisterStatus, NumWasmPages, PageMap};
use ic_sys::PAGE_SIZE;
use ic_test_utilities::assert_utils::assert_balance_equals;
use ic_test_utilities_execution_environment::{
assert_empty_reply, check_ingress_status, get_reply, wasm_compilation_cost,
wat_compilation_cost, ExecutionTest, ExecutionTestBuilder,
};
use ic_test_utilities_metrics::fetch_int_counter;
use ic_test_utilities_metrics::{fetch_histogram_stats, HistogramStats};
use ic_types::{
ingress::{IngressState, IngressStatus, WasmResult},
messages::CanisterTask,
messages::MAX_INTER_CANISTER_PAYLOAD_IN_BYTES,
methods::WasmMethod,
CanisterId, ComputeAllocation, Cycles, NumBytes, NumInstructions, MAX_STABLE_MEMORY_IN_BYTES,
};
use ic_universal_canister::{call_args, wasm, UNIVERSAL_CANISTER_WASM};
use proptest::prelude::*;
use proptest::test_runner::{TestRng, TestRunner};
use std::collections::BTreeSet;
use std::mem::size_of;
use std::time::Duration;
const MAX_NUM_INSTRUCTIONS: NumInstructions = NumInstructions::new(1_000_000_000);
const BALANCE_EPSILON: Cycles = Cycles::new(10_000_000);
#[test]
fn ic0_canister_status_works() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "canister_status"
(func $canister_status (result i32))
)
(func (export "canister_update test")
(if (i32.ne (call $canister_status) (i32.const 1))
(then unreachable)
)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![0, 1, 2, 3]);
assert_empty_reply(result);
}
#[test]
fn ic0_msg_arg_data_size_works() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "msg_arg_data_size"
(func $msg_arg_data_size (result i32))
)
(func (export "canister_update test")
(if (i32.ne (call $msg_arg_data_size) (i32.const 4))
(then (unreachable))
)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![0, 1, 2, 3]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable_grow_works() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable_size" (func $stable_size (result i32)))
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_update test")
;; Grow the memory by 1 page and verify that the return value
;; is the previous number of pages, which should be 0.
(if (i32.ne (call $stable_grow (i32.const 1)) (i32.const 0))
(then (unreachable))
)
;; Grow the memory by 5 more pages and verify that the return value
;; is the previous number of pages, which should be 1.
(if (i32.ne (call $stable_grow (i32.const 5)) (i32.const 1))
(then (unreachable))
)
;; Stable memory size now should be 6
(if (i32.ne (call $stable_size) (i32.const 6))
(then (unreachable))
)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable_grow_returns_neg_one_when_exceeding_memory_limit() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable_size" (func $stable_size (result i32)))
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_update test")
;; Grow the memory by 1000 pages and verify that the return value
;; is -1 because the grow should fail.
(if (i32.ne (call $stable_grow (i32.const 1000)) (i32.const -1))
(then (unreachable))
)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
test.canister_update_allocations_settings(canister_id, None, Some(30 * 1024 * 1024))
.unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable64_size_works() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_size" (func $stable_size (result i64)))
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_update test")
;; Grow the memory by 6 pages and verify that the return value
;; is the previous number of pages, which should be 0.
(if (i32.ne (call $stable_grow (i32.const 6)) (i32.const 0))
(then (unreachable))
)
;; Stable memory size now should be 6
(if (i64.ne (call $stable_size) (i64.const 6))
(then (unreachable))
)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable_write_increases_heap_delta() {
let mut test = ExecutionTestBuilder::new().build();
fn wat(bytes: usize) -> String {
format!(
r#"(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(import "ic0" "stable_write"
(func $stable_write (param $offset i32) (param $src i32) (param $size i32))
)
(func (export "canister_update test")
(drop (call $stable_grow (i32.const 1)))
(call $stable_write (i32.const 0) (i32.const 0) (i32.const {}))
)
(memory 1)
)"#,
bytes
)
}
let canister_id = test.canister_from_wat(wat(4097)).unwrap();
assert_eq!(NumBytes::from(0), test.state().metadata.heap_delta_estimate);
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
// We wrote more than 1 page but less than 2 pages so we expect 2 pages in
// heap delta.
assert_eq!(
NumBytes::from(8192),
test.state().metadata.heap_delta_estimate
);
let canister_id = test.canister_from_wat(wat(8192)).unwrap();
let heap_delta_estimate_before = test.state().metadata.heap_delta_estimate;
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
// We wrote exactly 2 pages so we expect 2 pages in heap delta.
assert_eq!(
heap_delta_estimate_before + NumBytes::from(8192),
test.state().metadata.heap_delta_estimate
);
}
#[test]
fn ic0_stable64_write_increases_heap_delta() {
let mut test = ExecutionTestBuilder::new().build();
fn wat(bytes: usize) -> String {
format!(
r#"(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable64_write"
(func $stable64_write (param $offset i64) (param $src i64) (param $size i64))
)
(func (export "canister_update test")
(drop (call $stable64_grow (i64.const 1)))
(call $stable64_write (i64.const 0) (i64.const 0) (i64.const {}))
)
(memory 1)
)"#,
bytes
)
}
let canister_id = test.canister_from_wat(wat(4097)).unwrap();
assert_eq!(NumBytes::from(0), test.state().metadata.heap_delta_estimate);
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
// We wrote more than 1 page but less than 2 pages so we expect 2 pages in
// heap delta.
assert_eq!(
NumBytes::from(8192),
test.state().metadata.heap_delta_estimate
);
let canister_id = test.canister_from_wat(wat(8192)).unwrap();
let heap_delta_estimate_before = test.state().metadata.heap_delta_estimate;
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
// We wrote exactly 2 pages so we expect 2 pages in heap delta.
assert_eq!(
heap_delta_estimate_before + NumBytes::from(8192),
test.state().metadata.heap_delta_estimate
);
}
#[test]
fn ic0_stable64_grow_does_not_change_heap_delta() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_size" (func $stable64_size (result i64)))
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(func (export "canister_update test")
(if (i64.ne (call $stable64_grow (i64.const 1)) (i64.const 0))
(then (unreachable))
)
(if (i64.ne (call $stable64_size) (i64.const 1))
(then (unreachable))
)
)
(memory 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
assert_eq!(NumBytes::from(0), test.state().metadata.heap_delta_estimate);
}
#[test]
fn ic0_grow_handles_overflow() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_update test")
;; Grow the memory by 10 pages.
(drop (call $stable_grow (i32.const 10)))
;; Grow the memory up to 64 * 1024 + 1 pages.
;; This should fail since it's bigger than the maximum number of memory
;; pages that can be used with the 32-bit API and return -1.
(if (i32.ne (call $stable_grow (i32.const 65527)) (i32.const -1))
(then (unreachable))
)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_grow_can_reach_max_number_of_pages() {
let mut test = ExecutionTestBuilder::new()
.with_initial_canister_cycles(3_000_000_000_000)
.build();
let wat = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_update test")
;; Grow the memory by 10 pages.
(drop (call $stable_grow (i32.const 10)))
;; Grow the memory up to 64 * 1024 pages which is the maximum allowed.
;; The result should be the previous number of pages (10).
(if (i32.ne (call $stable_grow (i32.const 65526)) (i32.const 10))
(then (unreachable))
)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable64_grow_works() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_size" (func $stable64_size (result i64)))
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(func (export "canister_update test")
;; Grow the memory by 1 page and verify that the return value
;; is the previous number of pages, which should be 0.
(if (i64.ne (call $stable64_grow (i64.const 1)) (i64.const 0))
(then (unreachable))
)
;; Grow the memory by 5 more pages and verify that the return value
;; is the previous number of pages, which should be 1.
(if (i64.ne (call $stable64_grow (i64.const 5)) (i64.const 1))
(then (unreachable))
)
;; Grow the memory by 2^64-1 more pages. This should fail.
(if (i64.ne (call $stable64_grow (i64.const 18446744073709551615)) (i64.const -1))
(then (unreachable))
)
;; Stable memory size now should be 6.
(if (i64.ne (call $stable64_size) (i64.const 6))
(then (unreachable))
)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable64_grow_beyond_max_pages_returns_neg_one() {
let mut test = ExecutionTestBuilder::new().build();
let wat = format!(
r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(func (export "canister_update test")
;; Grow the memory by the maximum number of pages + 1. This should fail.
(if (i64.ne (call $stable64_grow (i64.const {})) (i64.const -1))
(then (unreachable))
)
)
)"#,
(MAX_STABLE_MEMORY_IN_BYTES / WASM_PAGE_SIZE_IN_BYTES as u64) + 1
);
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable_grow_by_0_traps_if_memory_exceeds_4gb() {
let mut test = ExecutionTestBuilder::new()
.with_initial_canister_cycles(3_000_000_000_000)
.build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_update test")
;; Grow the memory to 4GiB.
(if (i64.ne (call $stable64_grow (i64.const 65536)) (i64.const 0))
(then (unreachable))
)
;; Grow the memory by 0 pages using 32-bit API. This should succeed.
(if (i32.ne (call $stable_grow (i32.const 0)) (i32.const 65536))
(then (unreachable))
)
;; Grow the memory by 1 page. This should succeed.
(if (i64.ne (call $stable64_grow (i64.const 1)) (i64.const 65536))
(then (unreachable))
)
;; Grow the memory by 0 pages using 32-bit API. This should trap.
(drop (call $stable_grow (i32.const 0)))
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err
.description()
.contains("32 bit stable memory api used on a memory larger than 4GB"));
}
#[test]
fn ic0_stable_size_traps_if_memory_exceeds_4gb() {
let mut test = ExecutionTestBuilder::new()
.with_initial_canister_cycles(3_000_000_000_000)
.build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable_size" (func $stable_size (result i32)))
(func (export "canister_update test")
;; Grow the memory to 4GiB + 1 page.
(if (i64.ne (call $stable64_grow (i64.const 65537)) (i64.const 0))
(then (unreachable))
)
;; This should trap because stable memory is too big for 32-bit API.
(drop (call $stable_size))
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err
.description()
.contains("32 bit stable memory api used on a memory larger than 4GB"));
}
#[test]
fn ic0_stable_grow_traps_if_stable_memory_exceeds_4gb() {
let mut test = ExecutionTestBuilder::new()
.with_initial_canister_cycles(3_000_000_000_000)
.build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(func (export "canister_update test")
;; Grow the memory to 4GiB.
(if (i64.ne (call $stable64_grow (i64.const 65536)) (i64.const 0))
(then (unreachable))
)
;; Grow the memory by 0 pages using 32-bit API. This should succeed.
(if (i32.ne (call $stable_grow (i32.const 0)) (i32.const 65536))
(then (unreachable))
)
;; Grow the memory by 1 page. This should succeed.
(if (i64.ne (call $stable64_grow (i64.const 1)) (i64.const 65536))
(then (unreachable))
)
;; Grow the memory by 100 pages using 32-bit API. This should trap.
(drop (call $stable_grow (i32.const 100)))
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err
.description()
.contains("32 bit stable memory api used on a memory larger than 4GB"));
}
#[test]
fn ic0_stable_read_and_write_work() {
let mut test = ExecutionTestBuilder::new().build();
let wat = 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 $swap
;; Swap the first 8 bytes from "abcdefgh" to "efghabcd"
;; (and vice-versa in a repeated call) using stable memory
;; Grow stable memory by 1 page.
(drop (call $stable_grow (i32.const 1)))
;; stable_memory[0..4] = heap[0..4]
(call $stable_write (i32.const 0) (i32.const 0) (i32.const 4))
;; stable_memory[60000..60004] = heap[4..8]
(call $stable_write (i32.const 60000) (i32.const 4) (i32.const 4))
;; heap[0..4] = stable_memory[60000..60004]
(call $stable_read (i32.const 0) (i32.const 60000) (i32.const 4))
;; heap[4..8] = stable_memory[0..4]
(call $stable_read (i32.const 4) (i32.const 0) (i32.const 4))
)
(func $test
(call $swap)
(call $read)
)
(func $read
;; Return the first 8 bytes of the heap.
(call $msg_reply_data_append
(i32.const 0) ;; heap offset = 0
(i32.const 8)) ;; length = 8
(call $msg_reply) ;; call reply
)
(memory 1)
(start $swap)
(export "canister_query read" (func $read))
(export "canister_update test" (func $test))
(data (i32.const 0) "abcdefgh") ;; Initial contents of the heap.
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "read", vec![]).unwrap();
assert_eq!(WasmResult::Reply(b"efghabcd".to_vec()), result); // swapped in `start`
let result = test.ingress(canister_id, "test", vec![]).unwrap();
assert_eq!(WasmResult::Reply(b"abcdefgh".to_vec()), result); // swapped again in `test`
}
#[test]
fn ic0_stable_read_traps_if_out_of_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = 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)))
(func (export "canister_update test")
;; Grow stable memory by 1 page (64kb)
(drop (call $stable_grow (i32.const 1)))
;; Reading from stable memory just after the page should trap.
(call $stable_read (i32.const 0) (i32.const 65536) (i32.const 1))
)
(memory 2 2)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("stable memory out of bounds"));
}
#[test]
fn ic0_stable_read_handles_overflows() {
let mut test = ExecutionTestBuilder::new().build();
let wat = 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)))
(func (export "canister_update test")
;; Grow the memory by 1 page.
(drop (call $stable_grow (i32.const 1)))
;; Ensure reading from stable memory with overflow doesn't panic.
(call $stable_read (i32.const 0) (i32.const 1) (i32.const 4294967295))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("stable memory out of bounds"));
}
#[test]
fn ic0_stable_write_traps_if_out_of_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(import "ic0" "stable_write"
(func $stable_write (param $offset i32) (param $src i32) (param $size i32))
)
(func (export "canister_update test")
;; Grow stable memory by 1 page (64kb)
(drop (call $stable_grow (i32.const 1)))
;; Writing to stable memory just after the page should trap.
(call $stable_write (i32.const 65536) (i32.const 0) (i32.const 1))
)
(memory 2 2)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("stable memory out of bounds"));
}
#[test]
fn ic0_stable_write_handles_overflows() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(import "ic0" "stable_write"
(func $stable_write (param $offset i32) (param $src i32) (param $size i32)))
(func (export "canister_update test")
;; Grow the memory by 1 page.
(drop (call $stable_grow (i32.const 1)))
;; Ensure writing to stable memory with overflow doesn't panic.
(call $stable_write (i32.const 4294967295) (i32.const 0) (i32.const 10))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("stable memory out of bounds"));
}
#[test]
fn ic0_stable_read_traps_if_heap_is_out_of_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = 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))
)
(func (export "canister_update test")
;; Grow the stable memory by 2 pages (128kb).
(drop (call $stable_grow (i32.const 2)))
;; An attempt to copy a page and a byte to the heap should fail.
(call $stable_read (i32.const 0) (i32.const 0) (i32.const 65537))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("heap out of bounds"));
}
#[test]
fn ic0_stable_write_traps_if_heap_is_out_of_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(import "ic0" "stable_write"
(func $stable_write (param $offset i32) (param $src i32) (param $size i32))
)
(func (export "canister_update test")
;; Grow the stable memory by 2 pages (128kb).
(drop (call $stable_grow (i32.const 2)))
;; An attempt to copy a page and a byte from the heap should fail.
(call $stable_write (i32.const 0) (i32.const 0) (i32.const 65537))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("heap out of bounds"));
}
#[test]
fn ic0_stable_write_works_at_max_size() {
let mut test = ExecutionTestBuilder::new()
.with_initial_canister_cycles(3_000_000_000_000)
.build();
let wat = r#"
(module
(import "ic0" "stable_grow" (func $stable_grow (param i32) (result i32)))
(import "ic0" "stable_write"
(func $stable_write (param $offset i32) (param $src i32) (param $size i32))
)
(func (export "canister_update test")
;; Grow stable memory to maximum size.
(drop (call $stable_grow (i32.const 65536)))
;; Write to stable memory from position 10 till the end (including).
(call $stable_write (i32.const 4294967286) (i32.const 0) (i32.const 10))
)
(memory 65536)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable_read_does_not_trap_if_in_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = 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))
)
(func (export "canister_update test")
;; Grow stable memory by 1 page (64kb).
(drop (call $stable_grow (i32.const 1)))
;; Reading from stable memory at end of page should not fail.
(call $stable_read (i32.const 0) (i32.const 0) (i32.const 65536))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable_read_works_at_max_size() {
let mut test = ExecutionTestBuilder::new()
.with_initial_canister_cycles(3_000_000_000_000)
.build();
let wat = 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))
)
(func (export "canister_update test")
;; Grow stable memory to maximum size.
(drop (call $stable_grow (i32.const 65536)))
;; Read from position at index 10 till the end of stable memory (including).
(call $stable_read (i32.const 0) (i32.const 4294967286) (i32.const 10))
)
(memory 65536)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn ic0_stable64_read_traps_if_out_of_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable64_read"
(func $stable64_read (param $dst i64) (param $offset i64) (param $size i64))
)
(func (export "canister_update test")
;; Grow stable memory by 1 page (64kb)
(drop (call $stable64_grow (i64.const 1)))
;; Reading from stable memory just after the page should trap.
(call $stable64_read (i64.const 0) (i64.const 65536) (i64.const 1))
)
(memory 2 2)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("stable memory out of bounds"));
}
#[test]
fn ic0_stable64_read_handles_overflows() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable64_read"
(func $stable64_read (param $dst i64) (param $offset i64) (param $size i64))
)
(func (export "canister_update test")
;; Grow the memory by 1 page.
(drop (call $stable64_grow (i64.const 1)))
;; Ensure reading from stable memory with overflow doesn't panic.
(call $stable64_read (i64.const 0) (i64.const 18446744073709551615) (i64.const 10))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("stable memory out of bounds"));
}
#[test]
fn ic0_stable64_write_traps_if_out_of_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable64_write"
(func $stable64_write (param $offset i64) (param $src i64) (param $size i64))
)
(func (export "canister_update test")
;; Grow stable memory by 1 page (64kb)
(drop (call $stable64_grow (i64.const 1)))
;; Writing to stable memory just after the page should trap.
(call $stable64_write (i64.const 65536) (i64.const 0) (i64.const 1))
)
(memory 2 2)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("stable memory out of bounds"));
}
#[test]
fn ic0_stable64_write_handles_overflows() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable64_write"
(func $stable64_write (param $offset i64) (param $src i64) (param $size i64))
)
(func (export "canister_update test")
;; Grow the memory by 1 page.
(drop (call $stable64_grow (i64.const 1)))
;; Ensure writing to stable memory with overflow doesn't panic.
(call $stable64_write (i64.const 18446744073709551615) (i64.const 0) (i64.const 10))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("stable memory out of bounds"));
}
#[test]
fn ic0_stable64_read_traps_if_heap_is_out_of_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable64_read"
(func $stable64_read (param $dst i64) (param $offset i64) (param $size i64))
)
(func (export "canister_update test")
;; Grow the stable memory by 2 pages (128kb).
(drop (call $stable64_grow (i64.const 2)))
;; An attempt to copy a page and a byte to the heap should fail.
(call $stable64_read (i64.const 0) (i64.const 0) (i64.const 65537))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("heap out of bounds"));
}
#[test]
fn ic0_stable64_write_traps_if_heap_is_out_of_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable64_write"
(func $stable64_write (param $offset i64) (param $src i64) (param $size i64))
)
(func (export "canister_update test")
;; Grow the stable memory by 2 pages (128kb).
(drop (call $stable64_grow (i64.const 2)))
;; An attempt to copy a page and a byte from the heap should fail.
(call $stable64_write (i64.const 0) (i64.const 0) (i64.const 65537))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let err = test.ingress(canister_id, "test", vec![]).unwrap_err();
assert_eq!(ErrorCode::CanisterTrapped, err.code());
assert!(err.description().contains("heap out of bounds"));
}
#[test]
fn ic0_stable64_read_does_not_trap_if_in_bounds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "stable64_grow" (func $stable64_grow (param i64) (result i64)))
(import "ic0" "stable64_read"
(func $stable64_read (param $dst i64) (param $offset i64) (param $size i64))
)
(func (export "canister_update test")
;; Grow stable memory by 1 page (64kb).
(drop (call $stable64_grow (i64.const 1)))
;; Reading from stable memory at end of page should succeed.
(call $stable64_read (i64.const 0) (i64.const 0) (i64.const 65536))
)
(memory 1 1)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
let result = test.ingress(canister_id, "test", vec![]);
assert_empty_reply(result);
}
#[test]
fn time_with_5_nanoseconds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "time" (func $time (result i64)))
(import "ic0" "msg_reply" (func $msg_reply))
(func (export "canister_update test")
(if (i64.ne (call $time) (i64.const 5))
(then (unreachable))
)
(call $msg_reply)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
test.advance_time(Duration::new(0, 5));
let result = test.ingress(canister_id, "test", vec![]).unwrap();
assert_eq!(result, WasmResult::Reply(vec![]));
}
#[test]
fn ic0_time_with_5_seconds() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "time" (func $time (result i64)))
(import "ic0" "msg_reply" (func $msg_reply))
(func (export "canister_update test")
(if (i64.ne (call $time) (i64.const 5000000000))
(then (unreachable))
)
(call $msg_reply)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
test.advance_time(Duration::new(5, 0));
let result = test.ingress(canister_id, "test", vec![]).unwrap();
assert_eq!(result, WasmResult::Reply(vec![]));
}
#[test]
fn ic0_global_timer_set_returns_previous_value() {
let mut test = ExecutionTestBuilder::new().build();
let wat = r#"
(module
(import "ic0" "global_timer_set"
(func $global_timer_set (param i64) (result i64))
)
(import "ic0" "msg_reply" (func $msg_reply))
(func (export "canister_update test")
;; Initially the timer should not be set, i.e. be zero
(if (i64.ne
(call $global_timer_set (i64.const 1))
(i64.const 0)
)
(then (unreachable))
)
;; Expect the timer is set to 1 now
(if (i64.ne
(call $global_timer_set (i64.const 0))
(i64.const 1)
)
(then (unreachable))
)
(call $msg_reply)
)
)"#;
let canister_id = test.canister_from_wat(wat).unwrap();
test.advance_time(Duration::new(5, 0));
let result = test.ingress(canister_id, "test", vec![]).unwrap();
assert_eq!(result, WasmResult::Reply(vec![]));
}
#[test]
fn ic0_canister_version_returns_correct_value() {
let mut test = ExecutionTestBuilder::new().build();
let canister_id = test.universal_canister().unwrap();
let ctr = wasm().canister_version().reply_int64().build();
let result = test.ingress(canister_id, "query", ctr.clone()).unwrap();
let expected_ctr: u64 = 1;
assert_eq!(
result,
WasmResult::Reply(expected_ctr.to_le_bytes().to_vec())
);
let result = test.ingress(canister_id, "update", ctr.clone()).unwrap();
// Update increases the `canister_version` only AFTER the execution,
// so the result is plus 0, as no update has been finished yet.
let expected_ctr: u64 = 1;
assert_eq!(
result,
WasmResult::Reply(expected_ctr.to_le_bytes().to_vec())
);
test.upgrade_canister(canister_id, vec![]).unwrap_err();
test.upgrade_canister(canister_id, UNIVERSAL_CANISTER_WASM.to_vec())
.unwrap();
let result = test.ingress(canister_id, "update", ctr.clone()).unwrap();
// Plus 1 for the previous ingress message.
let expected_ctr: u64 = 2 + 1;
assert_eq!(
result,
WasmResult::Reply(expected_ctr.to_le_bytes().to_vec())
);
test.uninstall_code(canister_id).unwrap();
test.install_canister(canister_id, vec![]).unwrap_err();
test.install_canister(canister_id, UNIVERSAL_CANISTER_WASM.to_vec())