Skip to content

Commit 51cfa95

Browse files
Rollup merge of rust-lang#124013 - RalfJung:box-to-raw, r=oli-obk
Box::into_raw: make Miri understand that this is a box-to-raw cast Turns out rust-lang#122647 went a bit too far in cleaning up `Box`... we still need a hack in `Box::into_raw`. The nicer fix would be to make Stacked Borrows not care about reference-to-raw-pointer casts, but it's unclear whether that will ever be possible without going to full Tree Borrows. Fixes rust-lang/miri#3473.
2 parents 4764dce + 8606efa commit 51cfa95

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

library/alloc/src/boxed.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
10581058
#[stable(feature = "box_raw", since = "1.4.0")]
10591059
#[inline]
10601060
pub fn into_raw(b: Self) -> *mut T {
1061-
Self::into_raw_with_allocator(b).0
1061+
// Make sure Miri realizes that we transition from a noalias pointer to a raw pointer here.
1062+
unsafe { addr_of_mut!(*&mut *Self::into_raw_with_allocator(b).0) }
10621063
}
10631064

10641065
/// Consumes the `Box`, returning a wrapped raw pointer and the allocator.
@@ -1112,7 +1113,10 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11121113
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
11131114
let mut b = mem::ManuallyDrop::new(b);
11141115
// We carefully get the raw pointer out in a way that Miri's aliasing model understands what
1115-
// is happening: using the primitive "deref" of `Box`.
1116+
// is happening: using the primitive "deref" of `Box`. In case `A` is *not* `Global`, we
1117+
// want *no* aliasing requirements here!
1118+
// In case `A` *is* `Global`, this does not quite have the right behavior; `into_raw`
1119+
// works around that.
11161120
let ptr = addr_of_mut!(**b);
11171121
let alloc = unsafe { ptr::read(&b.1) };
11181122
(ptr, alloc)

src/tools/miri/tests/fail/both_borrows/newtype_pair_retagging.stack.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
9-
help: <TAG> was created by a Unique retag at offsets [0x0..0x4]
9+
help: <TAG> was created by a SharedReadWrite retag at offsets [0x0..0x4]
1010
--> $DIR/newtype_pair_retagging.rs:LL:CC
1111
|
1212
LL | let ptr = Box::into_raw(Box::new(0i32));

src/tools/miri/tests/fail/both_borrows/newtype_retagging.stack.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
9-
help: <TAG> was created by a Unique retag at offsets [0x0..0x4]
9+
help: <TAG> was created by a SharedReadWrite retag at offsets [0x0..0x4]
1010
--> $DIR/newtype_retagging.rs:LL:CC
1111
|
1212
LL | let ptr = Box::into_raw(Box::new(0i32));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@revisions: stack tree
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
3+
use std::cell::UnsafeCell;
4+
5+
#[repr(C)]
6+
#[derive(Default)]
7+
struct Node {
8+
_meta: UnsafeCell<usize>,
9+
value: usize,
10+
}
11+
12+
impl Node {
13+
fn value(&self) -> &usize {
14+
&self.value
15+
}
16+
}
17+
18+
/// This used to cause Stacked Borrows errors because of trouble around conversion
19+
/// from Box to raw pointer.
20+
fn main() {
21+
unsafe {
22+
let a = Box::into_raw(Box::new(Node::default()));
23+
let ptr = &*a;
24+
*UnsafeCell::raw_get(a.cast::<UnsafeCell<usize>>()) = 2;
25+
assert_eq!(*ptr.value(), 0);
26+
drop(Box::from_raw(a));
27+
}
28+
}

src/tools/miri/tests/pass/stacked-borrows/stacked-borrows.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn main() {
2020
wide_raw_ptr_in_tuple();
2121
not_unpin_not_protected();
2222
write_does_not_invalidate_all_aliases();
23+
box_into_raw_allows_interior_mutable_alias();
2324
}
2425

2526
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
@@ -263,3 +264,14 @@ fn write_does_not_invalidate_all_aliases() {
263264
other::lib2();
264265
assert_eq!(*x, 1337); // oops, the value changed! I guess not all pointers were invalidated
265266
}
267+
268+
fn box_into_raw_allows_interior_mutable_alias() { unsafe {
269+
let b = Box::new(std::cell::Cell::new(42));
270+
let raw = Box::into_raw(b);
271+
let c = &*raw;
272+
let d = raw.cast::<i32>(); // bypassing `Cell` -- only okay in Miri tests
273+
// `c` and `d` should permit arbitrary aliasing with each other now.
274+
*d = 1;
275+
c.set(2);
276+
drop(Box::from_raw(raw));
277+
} }

0 commit comments

Comments
 (0)