diff options
| author | Xinglu Chen <public@yoctocell.xyz> | 2025-05-02 19:46:09 +0200 |
|---|---|---|
| committer | Xinglu Chen <public@yoctocell.xyz> | 2025-05-02 19:56:45 +0200 |
| commit | 45d4bb2541c240087c86fa1eb742c04feabe9392 (patch) | |
| tree | c831669ef46d3c0a170686b13b02c0aa03ba4d65 | |
| parent | 8b3637635b46d74dfa7bcd1a7d30910c02b48ec4 (diff) | |
| download | rust-45d4bb2541c240087c86fa1eb742c04feabe9392.tar.gz rust-45d4bb2541c240087c86fa1eb742c04feabe9392.zip | |
Construct test so that it would fail for old code
| -rw-r--r-- | src/tools/miri/tests/pass/tree_borrows/cell-inside-box.rs | 24 | ||||
| -rw-r--r-- | src/tools/miri/tests/pass/tree_borrows/cell-inside-box.stderr | 3 |
2 files changed, 22 insertions, 5 deletions
diff --git a/src/tools/miri/tests/pass/tree_borrows/cell-inside-box.rs b/src/tools/miri/tests/pass/tree_borrows/cell-inside-box.rs index a2eb4ede44b..adf2f4e845b 100644 --- a/src/tools/miri/tests/pass/tree_borrows/cell-inside-box.rs +++ b/src/tools/miri/tests/pass/tree_borrows/cell-inside-box.rs @@ -8,12 +8,28 @@ use std::cell::UnsafeCell; pub fn main() { let cell = UnsafeCell::new(42); - let mut root = Box::new(cell); + let box1 = Box::new(cell); - let a = Box::as_mut_ptr(&mut root); unsafe { - name!(a); - let alloc_id = alloc_id!(a); + let ptr1: *mut UnsafeCell<i32> = Box::into_raw(box1); + name!(ptr1); + + let mut box2 = Box::from_raw(ptr1); + // `ptr2` will be a descendant of `ptr1`. + let ptr2: *mut UnsafeCell<i32> = Box::as_mut_ptr(&mut box2); + name!(ptr2); + + // We perform a write through `x`. + // Because `ptr1` is ReservedIM, a child write will make it transition to Active. + // Because `ptr2` is ReservedIM, a foreign write doesn't have any effect on it. + let x = (*ptr1).get(); + *x = 1; + + // We can still read from `ptr2`. + let val = *(*ptr2).get(); + assert_eq!(val, 1); + + let alloc_id = alloc_id!(ptr1); print_state!(alloc_id); } } diff --git a/src/tools/miri/tests/pass/tree_borrows/cell-inside-box.stderr b/src/tools/miri/tests/pass/tree_borrows/cell-inside-box.stderr index 89421380a3f..5dbfff718b1 100644 --- a/src/tools/miri/tests/pass/tree_borrows/cell-inside-box.stderr +++ b/src/tools/miri/tests/pass/tree_borrows/cell-inside-box.stderr @@ -2,5 +2,6 @@ Warning: this tree is indicative only. Some tags may have been hidden. 0.. 4 | Act | └─┬──<TAG=root of the allocation> -| ReIM| └────<TAG=a> +| Act | └─┬──<TAG=ptr1> +| ReIM| └────<TAG=ptr2> ────────────────────────────────────────────────── |
