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
#[macro_export]
macro_rules! alloc_tests {
    ( $TestRegion:path ) => {
        use libc::c_void;
        use std::sync::Arc;
        use $TestRegion as TestRegion;
        use $crate::alloc::Limits;
        use $crate::context::{Context, ContextHandle};
        use $crate::instance::InstanceInternal;
        use $crate::module::{GlobalValue, HeapSpec, MockModuleBuilder};
        use $crate::region::Region;
        use $crate::val::Val;

        const LIMITS_HEAP_MEM_SIZE: usize = 16 * 64 * 1024;
        const LIMITS_HEAP_ADDRSPACE_SIZE: usize = 8 * 1024 * 1024;
        const LIMITS_STACK_SIZE: usize = 64 * 1024;
        const LIMITS_GLOBALS_SIZE: usize = 4 * 1024;

        const LIMITS: Limits = Limits {
            heap_memory_size: LIMITS_HEAP_MEM_SIZE,
            heap_address_space_size: LIMITS_HEAP_ADDRSPACE_SIZE,
            stack_size: LIMITS_STACK_SIZE,
            globals_size: LIMITS_GLOBALS_SIZE,
        };

        const SPEC_HEAP_RESERVED_SIZE: u64 = LIMITS_HEAP_ADDRSPACE_SIZE as u64 / 2;
        const SPEC_HEAP_GUARD_SIZE: u64 = LIMITS_HEAP_ADDRSPACE_SIZE as u64 / 2;

        // one wasm page, not host page
        const ONEPAGE_INITIAL_SIZE: u64 = 64 * 1024;
        const ONEPAGE_MAX_SIZE: u64 = 64 * 1024;

        const ONE_PAGE_HEAP: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: SPEC_HEAP_GUARD_SIZE,
            initial_size: ONEPAGE_INITIAL_SIZE,
            max_size: Some(ONEPAGE_MAX_SIZE),
        };

        const THREEPAGE_INITIAL_SIZE: u64 = 64 * 1024;
        const THREEPAGE_MAX_SIZE: u64 = 3 * 64 * 1024;

        const THREE_PAGE_MAX_HEAP: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: 0,
            initial_size: THREEPAGE_INITIAL_SIZE,
            max_size: Some(THREEPAGE_MAX_SIZE),
        };

        /// This test shows an `AllocHandle` passed to `Region::allocate_runtime` will have its heap
        /// and stack of the correct size and read/writability.
        #[test]
        fn allocate_runtime_works() {
            let region = TestRegion::create(1, &LIMITS).expect("region created");
            let mut inst = region
                .new_instance(
                    MockModuleBuilder::new()
                        .with_heap_spec(ONE_PAGE_HEAP)
                        .build(),
                )
                .expect("new_instance succeeds");

            let heap_len = inst.alloc().heap_len();
            assert_eq!(heap_len, ONEPAGE_INITIAL_SIZE as usize);

            let heap = unsafe { inst.alloc_mut().heap_mut() };

            assert_eq!(heap[0], 0);
            heap[0] = 0xFF;
            assert_eq!(heap[0], 0xFF);

            assert_eq!(heap[heap_len - 1], 0);
            heap[heap_len - 1] = 0xFF;
            assert_eq!(heap[heap_len - 1], 0xFF);

            let stack = unsafe { inst.alloc_mut().stack_mut() };
            assert_eq!(stack.len(), LIMITS_STACK_SIZE);

            assert_eq!(stack[0], 0);
            stack[0] = 0xFF;
            assert_eq!(stack[0], 0xFF);

            assert_eq!(stack[LIMITS_STACK_SIZE - 1], 0);
            stack[LIMITS_STACK_SIZE - 1] = 0xFF;
            assert_eq!(stack[LIMITS_STACK_SIZE - 1], 0xFF);
        }

        /// This test shows the heap works properly after a single expand.
        #[test]
        fn expand_heap_once() {
            expand_heap_once_template(THREE_PAGE_MAX_HEAP)
        }

        fn expand_heap_once_template(heap_spec: HeapSpec) {
            let region = TestRegion::create(1, &LIMITS).expect("region created");
            let module = MockModuleBuilder::new()
                .with_heap_spec(heap_spec.clone())
                .build();
            let mut inst = region
                .new_instance(module.clone())
                .expect("new_instance succeeds");

            let heap_len = inst.alloc().heap_len();
            assert_eq!(heap_len, heap_spec.initial_size as usize);

            let new_heap_area = inst
                .alloc_mut()
                .expand_heap(64 * 1024, module.as_ref())
                .expect("expand_heap succeeds");
            assert_eq!(heap_len, new_heap_area as usize);

            let new_heap_len = inst.alloc().heap_len();
            assert_eq!(new_heap_len, heap_len + (64 * 1024));

            let heap = unsafe { inst.alloc_mut().heap_mut() };
            assert_eq!(heap[new_heap_len - 1], 0);
            heap[new_heap_len - 1] = 0xFF;
            assert_eq!(heap[new_heap_len - 1], 0xFF);
        }

        /// This test shows the heap works properly after two expands.
        #[test]
        fn expand_heap_twice() {
            let region = TestRegion::create(1, &LIMITS).expect("region created");
            let module = MockModuleBuilder::new()
                .with_heap_spec(THREE_PAGE_MAX_HEAP)
                .build();
            let mut inst = region
                .new_instance(module.clone())
                .expect("new_instance succeeds");

            let heap_len = inst.alloc().heap_len();
            assert_eq!(heap_len, THREEPAGE_INITIAL_SIZE as usize);

            let new_heap_area = inst
                .alloc_mut()
                .expand_heap(64 * 1024, module.as_ref())
                .expect("expand_heap succeeds");
            assert_eq!(heap_len, new_heap_area as usize);

            let new_heap_len = inst.alloc().heap_len();
            assert_eq!(new_heap_len, heap_len + (64 * 1024));

            let second_new_heap_area = inst
                .alloc_mut()
                .expand_heap(64 * 1024, module.as_ref())
                .expect("expand_heap succeeds");
            assert_eq!(new_heap_len, second_new_heap_area as usize);

            let second_new_heap_len = inst.alloc().heap_len();
            assert_eq!(second_new_heap_len as u64, THREEPAGE_MAX_SIZE);

            let heap = unsafe { inst.alloc_mut().heap_mut() };
            assert_eq!(heap[new_heap_len - 1], 0);
            heap[new_heap_len - 1] = 0xFF;
            assert_eq!(heap[new_heap_len - 1], 0xFF);
        }

        /// This test shows that if you try to expand past the max given by the heap spec, the
        /// expansion fails, but the existing heap can still be used. This test uses a region with
        /// multiple slots in order to exercise more edge cases with adjacent managed memory.
        #[test]
        fn expand_past_spec_max() {
            let region = TestRegion::create(10, &LIMITS).expect("region created");
            let module = MockModuleBuilder::new()
                .with_heap_spec(THREE_PAGE_MAX_HEAP)
                .build();
            let mut inst = region
                .new_instance(module.clone())
                .expect("new_instance succeeds");

            let heap_len = inst.alloc().heap_len();
            assert_eq!(heap_len, THREEPAGE_INITIAL_SIZE as usize);

            let new_heap_area = inst
                .alloc_mut()
                .expand_heap(THREEPAGE_MAX_SIZE as u32, module.as_ref());
            assert!(new_heap_area.is_err(), "heap expansion past spec fails");

            let new_heap_len = inst.alloc().heap_len();
            assert_eq!(new_heap_len, heap_len);

            let heap = unsafe { inst.alloc_mut().heap_mut() };
            assert_eq!(heap[new_heap_len - 1], 0);
            heap[new_heap_len - 1] = 0xFF;
            assert_eq!(heap[new_heap_len - 1], 0xFF);
        }

        const EXPANDPASTLIMIT_INITIAL_SIZE: u64 = LIMITS_HEAP_MEM_SIZE as u64 - (64 * 1024);
        const EXPANDPASTLIMIT_MAX_SIZE: u64 = LIMITS_HEAP_MEM_SIZE as u64 + (64 * 1024);
        const EXPAND_PAST_LIMIT_SPEC: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: SPEC_HEAP_GUARD_SIZE,
            initial_size: EXPANDPASTLIMIT_INITIAL_SIZE,
            max_size: Some(EXPANDPASTLIMIT_MAX_SIZE),
        };

        /// This test shows that a heap refuses to grow past the alloc limits, even if the runtime
        /// spec says it can grow bigger. This test uses a region with multiple slots in order to
        /// exercise more edge cases with adjacent managed memory.
        #[test]
        fn expand_past_heap_limit() {
            let region = TestRegion::create(10, &LIMITS).expect("region created");
            let module = MockModuleBuilder::new()
                .with_heap_spec(EXPAND_PAST_LIMIT_SPEC)
                .build();
            let mut inst = region
                .new_instance(module.clone())
                .expect("new_instance succeeds");

            let heap_len = inst.alloc().heap_len();
            assert_eq!(heap_len, EXPANDPASTLIMIT_INITIAL_SIZE as usize);

            let new_heap_area = inst
                .alloc_mut()
                .expand_heap(64 * 1024, module.as_ref())
                .expect("expand_heap succeeds");
            assert_eq!(heap_len, new_heap_area as usize);

            let new_heap_len = inst.alloc().heap_len();
            assert_eq!(new_heap_len, LIMITS_HEAP_MEM_SIZE);

            let past_limit_heap_area = inst.alloc_mut().expand_heap(64 * 1024, module.as_ref());
            assert!(
                past_limit_heap_area.is_err(),
                "heap expansion past limit fails"
            );

            let still_heap_len = inst.alloc().heap_len();
            assert_eq!(still_heap_len, LIMITS_HEAP_MEM_SIZE);

            let heap = unsafe { inst.alloc_mut().heap_mut() };
            assert_eq!(heap[new_heap_len - 1], 0);
            heap[new_heap_len - 1] = 0xFF;
            assert_eq!(heap[new_heap_len - 1], 0xFF);
        }

        const INITIAL_OVERSIZE_HEAP: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: SPEC_HEAP_GUARD_SIZE,
            initial_size: SPEC_HEAP_RESERVED_SIZE + (64 * 1024),
            max_size: None,
        };

        /// This test shows that a heap refuses to grow past the alloc limits, even if the runtime
        /// spec says it can grow bigger. This test uses a region with multiple slots in order to
        /// exercise more edge cases with adjacent managed memory.
        #[test]
        fn reject_initial_oversize_heap() {
            let region = TestRegion::create(10, &LIMITS).expect("region created");
            let res = region.new_instance(
                MockModuleBuilder::new()
                    .with_heap_spec(INITIAL_OVERSIZE_HEAP)
                    .build(),
            );
            assert!(res.is_err(), "new_instance fails");
        }

        /// This test shows that we reject limits with a larger memory size than address space size
        #[test]
        fn reject_undersized_address_space() {
            const LIMITS: Limits = Limits {
                heap_memory_size: LIMITS_HEAP_ADDRSPACE_SIZE + 4096,
                heap_address_space_size: LIMITS_HEAP_ADDRSPACE_SIZE,
                stack_size: LIMITS_STACK_SIZE,
                globals_size: LIMITS_GLOBALS_SIZE,
            };
            let res = TestRegion::create(10, &LIMITS);
            assert!(res.is_err(), "region creation fails");
        }

        const SMALL_GUARD_HEAP: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: SPEC_HEAP_GUARD_SIZE - 1,
            initial_size: LIMITS_HEAP_MEM_SIZE as u64,
            max_size: None,
        };

        /// This test shows that a heap spec with a guard size smaller than the limits is
        /// allowed.
        #[test]
        fn accept_small_guard_heap() {
            let region = TestRegion::create(1, &LIMITS).expect("region created");
            let _inst = region
                .new_instance(
                    MockModuleBuilder::new()
                        .with_heap_spec(SMALL_GUARD_HEAP)
                        .build(),
                )
                .expect("new_instance succeeds");
        }

        const LARGE_GUARD_HEAP: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: SPEC_HEAP_GUARD_SIZE + 1,
            initial_size: ONEPAGE_INITIAL_SIZE,
            max_size: None,
        };

        /// This test shows that a `HeapSpec` with a guard size larger than the limits is not
        /// allowed.
        #[test]
        fn reject_large_guard_heap() {
            let region = TestRegion::create(1, &LIMITS).expect("region created");
            let res = region.new_instance(
                MockModuleBuilder::new()
                    .with_heap_spec(LARGE_GUARD_HEAP)
                    .build(),
            );
            assert!(res.is_err(), "new_instance fails");
        }

        /// This test shows that a `Slot` can be reused after an `AllocHandle` is dropped, and that
        /// its memory is reset.
        #[test]
        fn reuse_slot_works() {
            fn peek_n_poke(region: &Arc<TestRegion>) {
                let mut inst = region
                    .new_instance(
                        MockModuleBuilder::new()
                            .with_heap_spec(ONE_PAGE_HEAP)
                            .build(),
                    )
                    .expect("new_instance succeeds");

                let heap_len = inst.alloc().heap_len();
                assert_eq!(heap_len, ONEPAGE_INITIAL_SIZE as usize);

                let heap = unsafe { inst.alloc_mut().heap_mut() };

                assert_eq!(heap[0], 0);
                heap[0] = 0xFF;
                assert_eq!(heap[0], 0xFF);

                assert_eq!(heap[heap_len - 1], 0);
                heap[heap_len - 1] = 0xFF;
                assert_eq!(heap[heap_len - 1], 0xFF);

                let stack = unsafe { inst.alloc_mut().stack_mut() };
                assert_eq!(stack.len(), LIMITS_STACK_SIZE);

                assert_eq!(stack[0], 0);
                stack[0] = 0xFF;
                assert_eq!(stack[0], 0xFF);

                assert_eq!(stack[LIMITS_STACK_SIZE - 1], 0);
                stack[LIMITS_STACK_SIZE - 1] = 0xFF;
                assert_eq!(stack[LIMITS_STACK_SIZE - 1], 0xFF);

                let globals = unsafe { inst.alloc_mut().globals_mut() };
                assert_eq!(
                    globals.len(),
                    LIMITS_GLOBALS_SIZE / std::mem::size_of::<GlobalValue>()
                );

                unsafe {
                    assert_eq!(globals[0].i_64, 0);
                    globals[0].i_64 = 0xFF;
                    assert_eq!(globals[0].i_64, 0xFF);
                }

                unsafe {
                    assert_eq!(globals[globals.len() - 1].i_64, 0);
                    globals[globals.len() - 1].i_64 = 0xFF;
                    assert_eq!(globals[globals.len() - 1].i_64, 0xFF);
                }

                let sigstack = unsafe { inst.alloc_mut().sigstack_mut() };
                assert_eq!(sigstack.len(), libc::SIGSTKSZ);

                assert_eq!(sigstack[0], 0);
                sigstack[0] = 0xFF;
                assert_eq!(sigstack[0], 0xFF);

                assert_eq!(sigstack[sigstack.len() - 1], 0);
                sigstack[sigstack.len() - 1] = 0xFF;
                assert_eq!(sigstack[sigstack.len() - 1], 0xFF);
            }

            // with a region size of 1, the slot must be reused
            let region = TestRegion::create(1, &LIMITS).expect("region created");

            peek_n_poke(&region);
            peek_n_poke(&region);
        }

        /// This test shows that the reset method clears the heap and resets its protections.
        #[test]
        fn alloc_reset() {
            let region = TestRegion::create(1, &LIMITS).expect("region created");
            let module = MockModuleBuilder::new()
                .with_heap_spec(THREE_PAGE_MAX_HEAP)
                .build();
            let mut inst = region
                .new_instance(module.clone())
                .expect("new_instance succeeds");

            let heap_len = inst.alloc().heap_len();
            assert_eq!(heap_len, THREEPAGE_INITIAL_SIZE as usize);

            let heap = unsafe { inst.alloc_mut().heap_mut() };

            assert_eq!(heap[0], 0);
            heap[0] = 0xFF;
            assert_eq!(heap[0], 0xFF);

            assert_eq!(heap[heap_len - 1], 0);
            heap[heap_len - 1] = 0xFF;
            assert_eq!(heap[heap_len - 1], 0xFF);

            // Making a new mock module here because the borrow checker doesn't like referencing
            // `inst.module` while `inst.alloc()` is borrowed mutably. The `Instance` tests don't have
            // this weirdness
            inst.alloc_mut()
                .reset_heap(module.as_ref())
                .expect("reset succeeds");

            let reset_heap_len = inst.alloc().heap_len();
            assert_eq!(reset_heap_len, THREEPAGE_INITIAL_SIZE as usize);

            let heap = unsafe { inst.alloc_mut().heap_mut() };

            assert_eq!(heap[0], 0);
            heap[0] = 0xFF;
            assert_eq!(heap[0], 0xFF);

            assert_eq!(heap[reset_heap_len - 1], 0);
            heap[reset_heap_len - 1] = 0xFF;
            assert_eq!(heap[reset_heap_len - 1], 0xFF);
        }

        /// This test shows that the reset method clears the heap and restores it to the spec
        /// initial size after growing the heap.
        #[test]
        fn alloc_grow_reset() {
            let region = TestRegion::create(1, &LIMITS).expect("region created");
            let module = MockModuleBuilder::new()
                .with_heap_spec(THREE_PAGE_MAX_HEAP)
                .build();
            let mut inst = region
                .new_instance(module.clone())
                .expect("new_instance succeeds");

            let heap_len = inst.alloc().heap_len();
            assert_eq!(heap_len, THREEPAGE_INITIAL_SIZE as usize);

            let heap = unsafe { inst.alloc_mut().heap_mut() };

            assert_eq!(heap[0], 0);
            heap[0] = 0xFF;
            assert_eq!(heap[0], 0xFF);

            assert_eq!(heap[heap_len - 1], 0);
            heap[heap_len - 1] = 0xFF;
            assert_eq!(heap[heap_len - 1], 0xFF);

            let new_heap_area = inst
                .alloc_mut()
                .expand_heap(
                    (THREEPAGE_MAX_SIZE - THREEPAGE_INITIAL_SIZE) as u32,
                    module.as_ref(),
                )
                .expect("expand_heap succeeds");
            assert_eq!(heap_len, new_heap_area as usize);

            let new_heap_len = inst.alloc().heap_len();
            assert_eq!(new_heap_len, THREEPAGE_MAX_SIZE as usize);

            // Making a new mock module here because the borrow checker doesn't like referencing
            // `inst.module` while `inst.alloc()` is borrowed mutably. The `Instance` tests don't have
            // this weirdness
            inst.alloc_mut()
                .reset_heap(module.as_ref())
                .expect("reset succeeds");

            let reset_heap_len = inst.alloc().heap_len();
            assert_eq!(reset_heap_len, THREEPAGE_INITIAL_SIZE as usize);

            let heap = unsafe { inst.alloc_mut().heap_mut() };

            assert_eq!(heap[0], 0);
            heap[0] = 0xFF;
            assert_eq!(heap[0], 0xFF);

            assert_eq!(heap[reset_heap_len - 1], 0);
            heap[reset_heap_len - 1] = 0xFF;
            assert_eq!(heap[reset_heap_len - 1], 0xFF);
        }

        const GUARDLESS_HEAP: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: 0,
            initial_size: ONEPAGE_INITIAL_SIZE,
            max_size: None,
        };

        /// This test shows the alloc works even with a zero guard size.
        #[test]
        fn guardless_heap_create() {
            let region = TestRegion::create(1, &LIMITS).expect("region created");
            let mut inst = region
                .new_instance(
                    MockModuleBuilder::new()
                        .with_heap_spec(GUARDLESS_HEAP)
                        .build(),
                )
                .expect("new_instance succeeds");

            let heap_len = inst.alloc().heap_len();
            assert_eq!(heap_len, ONEPAGE_INITIAL_SIZE as usize);

            let heap = unsafe { inst.alloc_mut().heap_mut() };

            assert_eq!(heap[0], 0);
            heap[0] = 0xFF;
            assert_eq!(heap[0], 0xFF);

            assert_eq!(heap[heap_len - 1], 0);
            heap[heap_len - 1] = 0xFF;
            assert_eq!(heap[heap_len - 1], 0xFF);

            let stack = unsafe { inst.alloc_mut().stack_mut() };
            assert_eq!(stack.len(), LIMITS_STACK_SIZE);

            assert_eq!(stack[0], 0);
            stack[0] = 0xFF;
            assert_eq!(stack[0], 0xFF);

            assert_eq!(stack[LIMITS_STACK_SIZE - 1], 0);
            stack[LIMITS_STACK_SIZE - 1] = 0xFF;
            assert_eq!(stack[LIMITS_STACK_SIZE - 1], 0xFF);
        }

        /// This test shows a guardless heap works properly after a single expand.
        #[test]
        fn guardless_expand_heap_once() {
            expand_heap_once_template(GUARDLESS_HEAP)
        }

        const INITIAL_EMPTY_HEAP: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: SPEC_HEAP_GUARD_SIZE,
            initial_size: 0,
            max_size: None,
        };

        /// This test shows an initially-empty heap works properly after a single expand.
        #[test]
        fn initial_empty_expand_heap_once() {
            expand_heap_once_template(INITIAL_EMPTY_HEAP)
        }

        const INITIAL_EMPTY_GUARDLESS_HEAP: HeapSpec = HeapSpec {
            reserved_size: SPEC_HEAP_RESERVED_SIZE,
            guard_size: 0,
            initial_size: 0,
            max_size: None,
        };

        /// This test shows an initially-empty, guardless heap works properly after a single
        /// expand.
        #[test]
        fn initial_empty_guardless_expand_heap_once() {
            expand_heap_once_template(INITIAL_EMPTY_GUARDLESS_HEAP)
        }

        const CONTEXT_TEST_LIMITS: Limits = Limits {
            heap_memory_size: 4096,
            heap_address_space_size: 2 * 4096,
            stack_size: 4096,
            globals_size: 4096,
        };
        const CONTEXT_TEST_INITIAL_SIZE: u64 = 4096;
        const CONTEXT_TEST_HEAP: HeapSpec = HeapSpec {
            reserved_size: 4096,
            guard_size: 4096,
            initial_size: CONTEXT_TEST_INITIAL_SIZE,
            max_size: Some(4096),
        };

        /// This test shows that alloced memory will create a heap and a stack that child context
        /// code can use.
        #[test]
        fn context_alloc_child() {
            extern "C" fn heap_touching_child(heap: *mut u8) {
                let heap = unsafe {
                    std::slice::from_raw_parts_mut(heap, CONTEXT_TEST_INITIAL_SIZE as usize)
                };
                heap[0] = 123;
                heap[4095] = 45;
            }

            let region = TestRegion::create(1, &CONTEXT_TEST_LIMITS).expect("region created");
            let mut inst = region
                .new_instance(
                    MockModuleBuilder::new()
                        .with_heap_spec(CONTEXT_TEST_HEAP)
                        .build(),
                )
                .expect("new_instance succeeds");

            let mut parent = ContextHandle::new();
            unsafe {
                let heap_ptr = inst.alloc_mut().heap_mut().as_ptr() as *mut c_void;
                let mut child = ContextHandle::create_and_init(
                    inst.alloc_mut().stack_u64_mut(),
                    heap_touching_child as usize,
                    &[Val::CPtr(heap_ptr)],
                )
                .expect("context init succeeds");
                Context::swap(&mut parent, &mut child);
                assert_eq!(inst.alloc().heap()[0], 123);
                assert_eq!(inst.alloc().heap()[4095], 45);
            }
        }

        /// This test shows that an alloced memory will create a heap and stack, the child code can
        /// write a pattern to that stack, and we can read back that same pattern after it is done
        /// running.
        #[test]
        fn context_stack_pattern() {
            const STACK_PATTERN_LENGTH: usize = 1024;
            extern "C" fn stack_pattern_child(heap: *mut u64) {
                let heap = unsafe {
                    std::slice::from_raw_parts_mut(heap, CONTEXT_TEST_INITIAL_SIZE as usize / 8)
                };
                let mut onthestack = [0u8; STACK_PATTERN_LENGTH];
                // While not used, this array is load-bearing! A function that executes after the
                // guest completes, `instance_kill_state_exit_guest_region`, may end up using
                // sufficient stack space to trample over values in this function's call frame.
                //
                // Padding it out with a duplicate pattern makes enough space for `onthestack` to
                // not be clobbered.
                let mut ignored = [0u8; STACK_PATTERN_LENGTH];
                for i in 0..STACK_PATTERN_LENGTH {
                    ignored[i] = (i % 256) as u8;
                    onthestack[i] = (i % 256) as u8;
                }
                heap[0] = onthestack.as_ptr() as u64;
            }

            let region = TestRegion::create(1, &CONTEXT_TEST_LIMITS).expect("region created");
            let mut inst = region
                .new_instance(
                    MockModuleBuilder::new()
                        .with_heap_spec(CONTEXT_TEST_HEAP)
                        .build(),
                )
                .expect("new_instance succeeds");

            let mut parent = ContextHandle::new();
            unsafe {
                let heap_ptr = inst.alloc_mut().heap_mut().as_ptr() as *mut c_void;
                let mut child = ContextHandle::create_and_init(
                    inst.alloc_mut().stack_u64_mut(),
                    stack_pattern_child as usize,
                    &[Val::CPtr(heap_ptr)],
                )
                .expect("context init succeeds");
                Context::swap(&mut parent, &mut child);

                let stack_pattern = inst.alloc().heap_u64()[0] as usize;
                assert!(stack_pattern > inst.alloc().slot().stack as usize);
                assert!(
                    stack_pattern + STACK_PATTERN_LENGTH < inst.alloc().slot().stack_top() as usize
                );
                let stack_pattern =
                    std::slice::from_raw_parts(stack_pattern as *const u8, STACK_PATTERN_LENGTH);
                for i in 0..STACK_PATTERN_LENGTH {
                    assert_eq!(stack_pattern[i], (i % 256) as u8);
                }
            }
        }

        #[test]
        fn drop_region_first() {
            let region = TestRegion::create(1, &Limits::default()).expect("region can be created");
            let inst = region
                .new_instance(MockModuleBuilder::new().build())
                .expect("new_instance succeeds");
            drop(region);
            drop(inst);
        }
    };
}

#[cfg(test)]
alloc_tests!(crate::region::mmap::MmapRegion);