Skip to content

Commit 70ef250

Browse files
authored
Merge pull request #4306 from yoctocell/fix-unsafecell-inside-box
Tree Borrows: Correctly handle interior mutable data in `Box`
2 parents f8a8bbd + 45d4bb2 commit 70ef250

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'tcx> NewPermission {
168168
pointee.is_unpin(*cx.tcx, cx.typing_env()).then_some(()).map(|()| {
169169
// Regular `Unpin` box, give it `noalias` but only a weak protector
170170
// because it is valid to deallocate it within the function.
171-
let ty_is_freeze = ty.is_freeze(*cx.tcx, cx.typing_env());
171+
let ty_is_freeze = pointee.is_freeze(*cx.tcx, cx.typing_env());
172172
let protected = kind == RetagKind::FnEntry;
173173
let initial_state = Permission::new_reserved(ty_is_freeze, protected);
174174
Self {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//@compile-flags: -Zmiri-tree-borrows
2+
#![feature(box_as_ptr)]
3+
#[path = "../../utils/mod.rs"]
4+
#[macro_use]
5+
mod utils;
6+
7+
use std::cell::UnsafeCell;
8+
9+
pub fn main() {
10+
let cell = UnsafeCell::new(42);
11+
let box1 = Box::new(cell);
12+
13+
unsafe {
14+
let ptr1: *mut UnsafeCell<i32> = Box::into_raw(box1);
15+
name!(ptr1);
16+
17+
let mut box2 = Box::from_raw(ptr1);
18+
// `ptr2` will be a descendant of `ptr1`.
19+
let ptr2: *mut UnsafeCell<i32> = Box::as_mut_ptr(&mut box2);
20+
name!(ptr2);
21+
22+
// We perform a write through `x`.
23+
// Because `ptr1` is ReservedIM, a child write will make it transition to Active.
24+
// Because `ptr2` is ReservedIM, a foreign write doesn't have any effect on it.
25+
let x = (*ptr1).get();
26+
*x = 1;
27+
28+
// We can still read from `ptr2`.
29+
let val = *(*ptr2).get();
30+
assert_eq!(val, 1);
31+
32+
let alloc_id = alloc_id!(ptr1);
33+
print_state!(alloc_id);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
──────────────────────────────────────────────────
2+
Warning: this tree is indicative only. Some tags may have been hidden.
3+
0.. 4
4+
| Act | └─┬──<TAG=root of the allocation>
5+
| Act | └─┬──<TAG=ptr1>
6+
| ReIM| └────<TAG=ptr2>
7+
──────────────────────────────────────────────────

0 commit comments

Comments
 (0)