Skip to content

Commit

Permalink
Box::into_raw: make Miri understand that this is a box-to-raw cast
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 16, 2024
1 parent 97009d2 commit 774e465
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <TAG> was created by a Unique retag at offsets [0x0..0x4]
help: <TAG> was created by a SharedReadWrite retag at offsets [0x0..0x4]
--> $DIR/newtype_pair_retagging.rs:LL:CC
|
LL | let ptr = Box::into_raw(Box::new(0i32));
Expand Down
2 changes: 1 addition & 1 deletion tests/fail/both_borrows/newtype_retagging.stack.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <TAG> was created by a Unique retag at offsets [0x0..0x4]
help: <TAG> was created by a SharedReadWrite retag at offsets [0x0..0x4]
--> $DIR/newtype_retagging.rs:LL:CC
|
LL | let ptr = Box::into_raw(Box::new(0i32));
Expand Down
28 changes: 28 additions & 0 deletions tests/pass/issues/issue-miri-3473.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//@revisions: stack tree
//@[tree]compile-flags: -Zmiri-tree-borrows
use std::cell::UnsafeCell;

#[repr(C)]
#[derive(Default)]
struct Node {
_meta: UnsafeCell<usize>,
value: usize,
}

impl Node {
fn value(&self) -> &usize {
&self.value
}
}

/// This used to cause Stacked Borrows errors because of trouble around conversion
/// from Box to raw pointer.
fn main() {
unsafe {
let a = Box::into_raw(Box::new(Node::default()));
let ptr = &*a;
*UnsafeCell::raw_get(a.cast::<UnsafeCell<usize>>()) = 2;
assert_eq!(*ptr.value(), 0);
drop(Box::from_raw(a));
}
}
12 changes: 12 additions & 0 deletions tests/pass/stacked-borrows/stacked-borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn main() {
wide_raw_ptr_in_tuple();
not_unpin_not_protected();
write_does_not_invalidate_all_aliases();
box_into_raw_allows_interior_mutable_alias();
}

// Make sure that reading from an `&mut` does, like reborrowing to `&`,
Expand Down Expand Up @@ -263,3 +264,14 @@ fn write_does_not_invalidate_all_aliases() {
other::lib2();
assert_eq!(*x, 1337); // oops, the value changed! I guess not all pointers were invalidated
}

fn box_into_raw_allows_interior_mutable_alias() { unsafe {
let b = Box::new(std::cell::Cell::new(42));
let raw = Box::into_raw(b);
let c = &*raw;
let d = raw.cast::<i32>(); // bypassing `Cell` -- only okay in Miri tests
// `c` and `d` should permit arbitrary aliasing with each other now.
*d = 1;
c.set(2);
drop(Box::from_raw(raw));
} }

0 comments on commit 774e465

Please sign in to comment.