mnemosyne-local 0.3.0

Thread-local allocation engine for Mnemosyne
Documentation
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
use super::*;
use core::ptr::NonNull;
use mnemosyne_backend::MemoryBackendWrapper;
use mnemosyne_core::constants::{
    MAX_ALLOC_SIZE, PAGE_SHIFT, PAGE_SIZE, PAGES_PER_SEGMENT, SEGMENT_SIZE,
};
use mnemosyne_core::policy::StandardPolicy;
use mnemosyne_core::types::{Block, Segment};

#[test]
fn usable_size_returns_block_size_for_small_allocations() {
    // Mnemosyne rounds small allocation requests up to the next
    // size class, so the usable size should match `class_to_size`
    // for every (request, alignment) pair the small-alloc test
    // sweep exercises, regardless of the *requested* size.
    for &(req_size, req_align) in &[(8usize, 8usize), (16, 8), (32, 16), (64, 8), (1024, 8)] {
        let ptr =
            unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(req_size, req_align) };
        assert!(
            !ptr.is_null(),
            "alloc({req_size}, {req_align}) returned null"
        );

        let reported = unsafe { usable_size(ptr) };
        assert!(
            reported >= req_size,
            "usable_size({req_size}, {req_align}) = {reported} is below the request"
        );
        assert!(
            reported >= req_align,
            "usable_size({req_size}, {req_align}) = {reported} is below the adjusted minimum (alignment)"
        );
        // The reported size is whatever size class the page is
        // sliced into; verify it matches a real class.
        let ptr_val = ptr as usize;
        let segment_addr = ptr_val & !(SEGMENT_SIZE - 1);
        let segment = segment_addr as *mut Segment;
        let page_index = (ptr_val >> PAGE_SHIFT) & (PAGES_PER_SEGMENT - 1);
        let page = unsafe { &(*segment).pages[page_index] };
        assert_eq!(
            reported, page.block_size,
            "usable_size disagrees with the page's recorded block_size"
        );

        unsafe { thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr) };
    }
}

#[test]
fn usable_size_never_under_reports_across_every_size_class() {
    // The lower-bound counterpart to
    // `usable_size_does_not_over_report_past_mapping_end_for_huge_allocations`.
    // An under-report is the more dangerous direction for small
    // allocations: a `Vec` that trusts `usable_size` to compute spare
    // capacity would write past the reported window and corrupt an
    // adjacent block. Exhaustively prove `usable_size(ptr) >=
    // requested_size` for at least one representative request in every
    // small size class, plus the inter-class boundary bytes that the
    // size-class mapper rounds.
    use mnemosyne_core::NUM_SIZE_CLASSES;
    use mnemosyne_core::size_class::class_to_size;

    for class in 0..NUM_SIZE_CLASSES {
        let class_max = class_to_size(class);
        // Exercise the smallest request that lands in this class
        // (one byte past the previous class's max) and the class max
        // itself. Both must report at least the requested size.
        let prev_max = if class == 0 {
            0
        } else {
            class_to_size(class - 1)
        };
        for &req in &[prev_max + 1, class_max] {
            let ptr = unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(req, 8) };
            assert!(
                !ptr.is_null(),
                "alloc({req}) returned null for class {class}"
            );

            let reported = unsafe { usable_size(ptr) };
            assert!(
                reported >= req,
                "usable_size under-reported for class {class}: requested {req}, got {reported}"
            );
            // The reported value is the class block size, which must
            // be exactly `class_max` for any request in this class.
            assert_eq!(
                reported, class_max,
                "usable_size for request {req} (class {class}) should equal class max {class_max}"
            );

            unsafe { thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr) };
        }
    }
}

#[test]
fn usable_size_returns_payload_remainder_for_huge_allocations() {
    // Direct large allocation through the arena. The returned
    // pointer carries enough payload to cover the requested size,
    // and `usable_size` reports at least that much (it may report
    // more because the arena reserves alignment slack).
    let request = 4 * 1024 * 1024;
    for &align in &[8usize, 64 * 1024, 1024 * 1024, SEGMENT_SIZE] {
        // Safety: power-of-two alignment, non-zero size.
        let ptr = unsafe {
            mnemosyne_arena::allocate_large_or_huge::<MemoryBackendWrapper>(request, align, true)
        };
        assert!(!ptr.is_null(), "huge allocation failed for align {align}");

        let reported = unsafe { usable_size(ptr) };
        assert!(
            reported >= request,
            "usable_size = {reported} is below the requested huge size {request} for align {align}"
        );

        let recovered = unsafe { *((ptr as *mut *mut Segment).sub(1)) };
        let _released = unsafe {
            mnemosyne_arena::deallocate_large_or_huge::<MemoryBackendWrapper>(ptr, recovered)
        };
    }
}

#[test]
fn usable_size_does_not_over_report_past_mapping_end_for_huge_allocations() {
    // Strict assertion that catches the SEGMENT_ALIGN-1 byte over-report
    // that resulted from using segment_ptr (aligned_addr) as the
    // mapping base instead of segment.raw_alloc_ptr. We compute the
    // distance from ptr to the end of the *actual* OS mapping
    // (raw_alloc_ptr + huge_size) and assert usable_size never exceeds it.
    let request = 4 * 1024 * 1024;
    for &align in &[8usize, 64 * 1024, 1024 * 1024, SEGMENT_SIZE] {
        // Safety: power-of-two alignment, non-zero size.
        let ptr = unsafe {
            mnemosyne_arena::allocate_large_or_huge::<MemoryBackendWrapper>(request, align, true)
        };
        assert!(!ptr.is_null(), "huge allocation failed for align {align}");

        let recovered = unsafe { *((ptr as *mut *mut Segment).sub(1)) };
        let huge_size = unsafe { (*recovered).pages[0].block_size };
        let raw_ptr = unsafe { (*recovered).raw_alloc_ptr } as usize;
        let mapping_end = raw_ptr + huge_size;
        let actual_remaining = mapping_end - ptr as usize;

        let reported = unsafe { usable_size(ptr) };
        assert!(
            reported <= actual_remaining,
            "usable_size {} exceeds remaining mapping {} (raw_ptr={:#x}, ptr={:?}, huge_size={}) for align {align}",
            reported,
            actual_remaining,
            raw_ptr,
            ptr,
            huge_size,
        );
        assert!(
            reported >= request,
            "usable_size {} is below requested {} for align {align}",
            reported,
            request,
        );

        let _released = unsafe {
            mnemosyne_arena::deallocate_large_or_huge::<MemoryBackendWrapper>(ptr, recovered)
        };
    }
}

#[test]
fn usable_size_returns_zero_for_null_pointer() {
    let reported = unsafe { usable_size(core::ptr::null_mut()) };
    assert_eq!(reported, 0);
}

#[test]
fn small_alloc_returns_block_aligned_ptr_outside_metadata_page() {
    // The small-free classifier in `thread_free` relies on three
    // invariants: `page_index >= 1`, `page_index < PAGES_PER_SEGMENT`,
    // and `(ptr - page_start) % page.block_size == 0`. Verify each one
    // against the live allocation grid that customers actually observe.
    for &(req_size, req_align) in &[(8usize, 8usize), (16, 8), (32, 16), (64, 8), (1024, 8)] {
        let ptr =
            unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(req_size, req_align) };
        assert!(
            !ptr.is_null(),
            "alloc({req_size}, {req_align}) returned null"
        );

        let ptr_val = ptr as usize;
        let segment_addr = ptr_val & !(SEGMENT_SIZE - 1);
        let segment = segment_addr as *mut Segment;
        let page_index = (ptr_val >> PAGE_SHIFT) & (PAGES_PER_SEGMENT - 1);

        assert!(
            page_index >= 1,
            "alloc({req_size}, {req_align}) ptr {ptr:?} landed in metadata Page 0"
        );
        assert!(
            page_index < PAGES_PER_SEGMENT,
            "alloc({req_size}, {req_align}) page_index {page_index} >= PAGES_PER_SEGMENT"
        );
        let page = unsafe { &(*segment).pages[page_index] };
        assert!(
            page.block_size > 0,
            "alloc({req_size}, {req_align}) targeted an uninitialized page"
        );
        let offset = ptr_val & (PAGE_SIZE - 1);
        assert_eq!(
            offset % page.block_size,
            0,
            "alloc({req_size}, {req_align}) ptr is not aligned to block stride {} of its size class",
            page.block_size,
        );

        unsafe { thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr) };
    }
}

#[test]
fn reentrant_current_segment_local_free_uses_metadata_fast_path() {
    let ptr = unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(32, 8) };
    assert!(
        !ptr.is_null(),
        "reentrant local-free setup allocation failed"
    );

    let ptr_val = ptr as usize;
    let segment_addr = ptr_val & !(SEGMENT_SIZE - 1);
    let segment = segment_addr as *mut Segment;
    let page_index = (ptr_val >> PAGE_SHIFT) & (PAGES_PER_SEGMENT - 1);
    let page = unsafe { &mut (*segment).pages[page_index] };

    assert_eq!(page.alloc_count, 1);
    assert!(
        page.thread_free.is_empty(),
        "thread_free list should start empty before reentrant free"
    );

    MemoryBackendWrapper::with_allocator(|_| {
        unsafe { thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr) };
    });

    assert_eq!(page.alloc_count, 0);
    assert!(
        page.thread_free.is_empty(),
        "current-segment local free should not enqueue into page-local thread_free"
    );
    assert_eq!(page.free.map(NonNull::as_ptr), Some(ptr as *mut Block));
}

#[test]
fn current_segment_free_keeps_occupancy_mask_conservative() {
    let ptr = unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(32, 8) };
    assert!(!ptr.is_null(), "current-segment mask allocation failed");

    let ptr_val = ptr as usize;
    let segment_addr = ptr_val & !(SEGMENT_SIZE - 1);
    let segment = segment_addr as *mut Segment;
    let page_index = (ptr_val >> PAGE_SHIFT) & (PAGES_PER_SEGMENT - 1);
    let mask = 1u32 << page_index;

    assert!(
        unsafe { (*segment).is_current },
        "test allocation must come from the current slicing segment"
    );
    assert_ne!(
        unsafe { (*segment).page_occupied_mask } & mask,
        0,
        "allocation must mark the owning page occupied"
    );

    unsafe { thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr) };

    assert_eq!(unsafe { (*segment).pages[page_index].alloc_count }, 0);
    assert_ne!(
        unsafe { (*segment).page_occupied_mask } & mask,
        0,
        "current-segment free keeps a conservative mask bit for hot reuse"
    );
}

#[test]
fn thread_alloc_rejects_invalid_alignment_requests() {
    for &align in &[0usize, 3, 6, 12, SEGMENT_SIZE * 2] {
        let ptr = unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(64, align) };
        assert!(
            ptr.is_null(),
            "invalid alignment {align} should be rejected"
        );
    }
}

#[test]
fn thread_alloc_rejects_zero_size_requests() {
    for &align in &[1usize, 8, 16, PAGE_SIZE] {
        let ptr = unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(0, align) };
        assert!(ptr.is_null(), "zero-size allocation should be rejected");
    }
}

#[test]
fn thread_alloc_rejects_size_above_layout_bound() {
    let ptr =
        unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(MAX_ALLOC_SIZE + 1, 8) };
    assert!(
        ptr.is_null(),
        "above-MAX_ALLOC_SIZE thread_alloc returned {ptr:?}"
    );
}

#[test]
fn thread_alloc_layout_uses_layout_validated_fast_entry() {
    let ptr = unsafe { thread_alloc_layout::<StandardPolicy, MemoryBackendWrapper>(64, 8) };
    assert!(
        !ptr.is_null(),
        "Layout-validated thread_alloc fast entry returned null"
    );
    unsafe { thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr) };

    let oversized = unsafe {
        thread_alloc_layout::<StandardPolicy, MemoryBackendWrapper>(64, SEGMENT_SIZE * 2)
    };
    assert!(
        oversized.is_null(),
        "Layout-validated oversized alignment returned {oversized:?}"
    );
}

#[test]
fn thread_alloc_cold_charges_one_defrag_operation_per_page_refill() {
    let _guard = crate::local_alloc::TEST_LOCK
        .lock()
        .expect("local allocator test lock was poisoned");

    let worker = std::thread::spawn(|| {
        let before = MemoryBackendWrapper::with_allocator(|alloc| {
            assert_eq!(
                alloc.page_refills, 0,
                "fresh worker allocator should start with no page refills"
            );
            alloc.defrag_counter
        })
        .expect("fresh worker allocator slot must initialize");

        let ptr = unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(8192, 8) };
        assert!(!ptr.is_null(), "8192-byte allocation failed");

        let after = MemoryBackendWrapper::with_allocator(|alloc| {
            (alloc.defrag_counter, alloc.page_refills)
        })
        .expect("worker allocator slot must remain accessible");

        unsafe { thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr) };

        (before, after.0, after.1)
    });

    let (before, after, refills) = worker
        .join()
        .expect("defrag accounting worker thread panicked");
    assert_eq!(refills, 1, "single cold allocation should refill one page");
    assert_eq!(
        after,
        before + 1,
        "single page refill should charge exactly one defrag operation"
    );
}

#[test]
fn hardened_policy_round_trip_alloc_free() {
    use mnemosyne_hardened::HardenedPolicy;

    let _guard = crate::local_alloc::TEST_LOCK
        .lock()
        .expect("local allocator test lock was poisoned");

    let ptr = unsafe { thread_alloc::<HardenedPolicy, MemoryBackendWrapper>(32, 8) };
    assert!(!ptr.is_null(), "HardenedPolicy small allocation failed");

    // Verify that the memory is zero-initialized (since HardenedPolicy inherits from SecurePolicy, which zero-initializes)
    let slice = unsafe { core::slice::from_raw_parts(ptr, 32) };
    for &byte in slice {
        assert_eq!(
            byte, 0,
            "HardenedPolicy allocation was not zero-initialized"
        );
    }

    // Verify that we can write to it
    unsafe {
        core::ptr::write_bytes(ptr, 0x42, 32);
    }

    // Free the pointer
    unsafe {
        thread_free::<HardenedPolicy, MemoryBackendWrapper>(ptr);
    }
}

#[test]
fn hardened_policy_detects_freelist_tamper() {
    use mnemosyne_hardened::HardenedPolicy;

    let _guard = crate::local_alloc::TEST_LOCK
        .lock()
        .expect("local allocator test lock was poisoned");

    // We want to verify that tamper detection works under HardenedPolicy.
    // Let's allocate two blocks on a fresh page of class 0 (16 bytes).
    // Since we want them on the same page, we can allocate them in sequence.
    let ptr1 = unsafe { thread_alloc::<HardenedPolicy, MemoryBackendWrapper>(16, 8) };
    let ptr2 = unsafe { thread_alloc::<HardenedPolicy, MemoryBackendWrapper>(16, 8) };
    assert!(!ptr1.is_null());
    assert!(!ptr2.is_null());

    // Free them in sequence so they end up in the thread-local free list
    unsafe {
        thread_free::<HardenedPolicy, MemoryBackendWrapper>(ptr1);
        thread_free::<HardenedPolicy, MemoryBackendWrapper>(ptr2);
    }

    // Now, `page.free` points to `ptr2`, and `ptr2` contains the encrypted pointer to `ptr1`.
    // Let's tamper with the encrypted next pointer in `ptr2`.
    // The block metadata stores the encrypted pointer in the first `Option<NonNull<Block>>` slot of the block.
    let val2 = ptr2 as *mut usize;
    unsafe {
        let original_val = *val2;
        // Corrupt the pointer (e.g. flip a bit in the address portion)
        *val2 = original_val ^ 0x08;
    }

    // Now, try to allocate. The first allocation gets `ptr2` (which is successful).
    let ptr3 = unsafe { thread_alloc::<HardenedPolicy, MemoryBackendWrapper>(16, 8) };
    assert_eq!(ptr3, ptr2);

    // The second allocation would follow the tampered pointer to `ptr1`.
    // Since we flipped a bit, the decrypted address is incorrect and fails to match `ptr1`.
    // In particular, the page's free pointer now contains garbage.
    let ptr_val = ptr3 as usize;
    let segment_addr = ptr_val & !(SEGMENT_SIZE - 1);
    let segment = segment_addr as *mut Segment;
    let page_index = (ptr_val >> PAGE_SHIFT) & (PAGES_PER_SEGMENT - 1);
    let page = unsafe { (*segment).pages.get_unchecked(page_index) };

    let free_head = page.free.map(|p| p.as_ptr() as usize);
    assert_ne!(
        free_head,
        Some(ptr1 as usize),
        "HardenedPolicy failed to obscure/randomize the tampered pointer"
    );
}

#[test]
fn test_dealloc_path() {
    let ptr = unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(1024, 8) };
    assert!(!ptr.is_null());
    unsafe { thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr) };
}

#[test]
fn test_double_free_aborts_process() {
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_DOUBLE_FREE_ABORT_TEST").is_ok() {
        unsafe {
            let ptr = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);
            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);
        }
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_double_free_aborts_process")
        .arg("--exact")
        .env("RUN_DOUBLE_FREE_ABORT_TEST", "1")
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}

#[test]
fn test_reclaim_overflow_aborts_process() {
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_RECLAIM_OVERFLOW_ABORT_TEST").is_ok() {
        unsafe {
            let ptr = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            let ptr_val = ptr as usize;
            let segment_addr = ptr_val & !(SEGMENT_SIZE - 1);
            let segment = segment_addr as *mut Segment;
            let page_index = (ptr_val >> PAGE_SHIFT) & (PAGES_PER_SEGMENT - 1);
            let page = &mut (*segment).pages[page_index];

            // Manually reduce alloc_count to 0 so count (1) > alloc_count (0) during reclaim.
            page.set_alloc_count_for_segment(segment, page_index, 0);

            // Push the block directly to the thread_free queue.
            let block = ptr as *mut Block;
            page.thread_free
                .push::<StandardPolicy>(NonNull::new_unchecked(block));

            // Run reclaim, which should detect count (1) > alloc_count (0) and abort.
            page.reclaim_thread_free::<StandardPolicy>();
        }
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_reclaim_overflow_aborts_process")
        .arg("--exact")
        .env("RUN_RECLAIM_OVERFLOW_ABORT_TEST", "1")
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}

#[test]
fn test_cross_thread_double_free_aborts_process() {
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_CROSS_THREAD_DOUBLE_FREE_ABORT_TEST").is_ok() {
        let ptr = unsafe { thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8) };
        let ptr_val = ptr as usize;
        let handle = std::thread::spawn(move || unsafe {
            let ptr = ptr_val as *mut u8;
            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);
            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);
        });
        let _ = handle.join();
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_cross_thread_double_free_aborts_process")
        .arg("--exact")
        .env("RUN_CROSS_THREAD_DOUBLE_FREE_ABORT_TEST", "1")
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}

#[test]
fn test_local_immediate_double_free_aborts_process() {
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_LOCAL_IMMEDIATE_DOUBLE_FREE_ABORT_TEST").is_ok() {
        unsafe {
            let ptr1 = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            let _ptr2 = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr1);
            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr1);
        }
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_local_immediate_double_free_aborts_process")
        .arg("--exact")
        .env("RUN_LOCAL_IMMEDIATE_DOUBLE_FREE_ABORT_TEST", "1")
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}

#[test]
fn test_cpu_cache_double_free_aborts_process() {
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_CPU_CACHE_DOUBLE_FREE_ABORT_TEST").is_ok() {
        unsafe {
            crate::per_cpu::PER_CPU_CACHE_ENABLED
                .store(true, core::sync::atomic::Ordering::Relaxed);
            crate::per_cpu::enable_cpu_cache();
            let ptr = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            let ptr_val = ptr as usize;
            let handle = std::thread::spawn(move || {
                let ptr = ptr_val as *mut u8;
                thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);
                thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);
            });
            let _ = handle.join();
        }
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_cpu_cache_double_free_aborts_process")
        .arg("--exact")
        .env("RUN_CPU_CACHE_DOUBLE_FREE_ABORT_TEST", "1")
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}

#[test]
fn test_large_alloc_metadata_corruption_aborts_process() {
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_LARGE_ALLOC_METADATA_CORRUPTION_ABORT_TEST").is_ok() {
        unsafe {
            let ptr = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(65536, 8);
            assert!(!ptr.is_null());

            // Corrupt the metadata slot immediately preceding the payload.
            let metadata_slot = (ptr as *mut usize).sub(1);
            metadata_slot.write(0x1337); // Invalid segment pointer alignment.

            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);
        }
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_large_alloc_metadata_corruption_aborts_process")
        .arg("--exact")
        .env("RUN_LARGE_ALLOC_METADATA_CORRUPTION_ABORT_TEST", "1")
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}

#[test]
fn test_large_alloc_segment_invariant_corruption_aborts_process() {
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_LARGE_ALLOC_SEGMENT_INVARIANT_CORRUPTION_ABORT_TEST").is_ok() {
        unsafe {
            let ptr = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(65536, 8);
            assert!(!ptr.is_null());

            // Retrieve the valid segment pointer from the metadata slot.
            let segment_ptr = *((ptr as *mut *mut Segment).sub(1));

            // Corrupt the segment header's raw_alloc_ptr to violate the alignment offset invariant.
            (*segment_ptr).raw_alloc_ptr = core::ptr::null_mut();

            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);
        }
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_large_alloc_segment_invariant_corruption_aborts_process")
        .arg("--exact")
        .env(
            "RUN_LARGE_ALLOC_SEGMENT_INVARIANT_CORRUPTION_ABORT_TEST",
            "1",
        )
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}

#[test]
fn test_free_list_corruption_out_of_bounds_aborts_process() {
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_FREE_LIST_CORRUPTION_OUT_OF_BOUNDS_ABORT_TEST").is_ok() {
        unsafe {
            // Allocate a small block
            let ptr = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            assert!(!ptr.is_null());

            // Get containing segment and page
            let ptr_val = ptr as usize;
            let segment_addr = ptr_val & !(SEGMENT_SIZE - 1);
            let segment = segment_addr as *mut Segment;
            let page_index = (ptr_val >> PAGE_SHIFT) & (PAGES_PER_SEGMENT - 1);

            // Free the block to put it on the free list
            thread_free::<StandardPolicy, MemoryBackendWrapper>(ptr);

            // Corrupt the next pointer inside the block.
            // Since it's on page.free, we can decrypt/encrypt the next pointer.
            // Let's write an out-of-bounds address (e.g., 0x12345678) as the next pointer.
            let cookie = (*segment).keys[page_index];
            let corrupt_block = ptr as *mut Block;
            // Write corrupted next pointer
            let bad_ptr = 0x12345678 as *mut Block;
            (*corrupt_block).set_next::<StandardPolicy>(NonNull::new(bad_ptr), cookie);

            // Allocate again. This should pop the corrupted next pointer, but
            // when pop_block retrieves it from page.free, it will validate it and abort!
            let _ptr_new = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            let _ptr_another = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
        }
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_free_list_corruption_out_of_bounds_aborts_process")
        .arg("--exact")
        .env("RUN_FREE_LIST_CORRUPTION_OUT_OF_BOUNDS_ABORT_TEST", "1")
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}

#[test]
fn test_thread_free_cycle_aborts_process() {
    use mnemosyne_core::policy::AllocPolicy;
    use std::env;
    use std::process::Command;
    use std::string::String;

    if env::var("RUN_THREAD_FREE_CYCLE_ABORT_TEST").is_ok() {
        unsafe {
            let ptr1 = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            let ptr2 = thread_alloc::<StandardPolicy, MemoryBackendWrapper>(16, 8);
            let ptr1_val = ptr1 as usize;
            let segment_addr = ptr1_val & !(SEGMENT_SIZE - 1);
            let segment = segment_addr as *mut Segment;
            let page_index = (ptr1_val >> PAGE_SHIFT) & (PAGES_PER_SEGMENT - 1);
            let page = &mut (*segment).pages[page_index];

            let block1 = ptr1 as *mut Block;
            let block2 = ptr2 as *mut Block;
            let cookie = if StandardPolicy::ENABLE_FREE_LIST_ENCRYPTION {
                (*segment).keys[page_index]
            } else {
                0
            };

            // Push block2 then block1 to build list block1 -> block2 -> None
            page.thread_free
                .push::<StandardPolicy>(NonNull::new_unchecked(block2));
            page.thread_free
                .push::<StandardPolicy>(NonNull::new_unchecked(block1));

            // Manually link block2 to block1 to form cycle block1 -> block2 -> block1
            (*block2).set_next::<StandardPolicy>(NonNull::new(block1), cookie);

            // Run reclaim, which should walk the cycle, detect visited (3) > count (2), and abort.
            page.reclaim_thread_free::<StandardPolicy>();
        }
        return;
    }

    let current_exe = env::current_exe().unwrap();
    let output = Command::new(current_exe)
        .arg("tests::test_thread_free_cycle_aborts_process")
        .arg("--exact")
        .env("RUN_THREAD_FREE_CYCLE_ABORT_TEST", "1")
        .output()
        .unwrap();

    if output.status.success() {
        std::println!(
            "Subprocess stdout:\n{}",
            String::from_utf8_lossy(&output.stdout)
        );
        std::println!(
            "Subprocess stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        panic!("Subprocess succeeded but was expected to abort!");
    }
}