Skip to content

Commit e2206d3

Browse files
Rollup merge of #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/rust#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 #3473.
2 parents f6aa487 + 774e465 commit e2206d3

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

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));

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));

tests/pass/issues/issue-miri-3473.rs

+28
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+
}

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)