Skip to content

Commit b7dbb5e

Browse files
committed
also consider boxes like unique references
1 parent 36b97cd commit b7dbb5e

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/stacked_borrows.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -661,14 +661,14 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for MiriEvalContext<'a, 'mir, 'tcx> {
661661
// Primitives of reference type, that is the one thing we are interested in.
662662
fn visit_primitive(&mut self, place: MPlaceTy<'tcx, Borrow>) -> EvalResult<'tcx>
663663
{
664-
match place.layout.ty.sty {
665-
ty::Ref(_, _, mutbl) => {
666-
let val = self.ecx.read_immediate(place.into())?;
667-
let val = self.ecx.retag_reference(val, mutbl)?;
668-
self.ecx.write_immediate(val, place.into())?;
669-
}
670-
_ => {}, // nothing to do
671-
}
664+
let mutbl = match place.layout.ty.sty {
665+
ty::Ref(_, _, mutbl) => mutbl,
666+
ty::Adt(..) if place.layout.ty.is_box() => MutMutable,
667+
_ => return Ok(()), // nothing to do
668+
};
669+
let val = self.ecx.read_immediate(place.into())?;
670+
let val = self.ecx.retag_reference(val, mutbl)?;
671+
self.ecx.write_immediate(val, place.into())?;
672672
Ok(())
673673
}
674674
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
fn demo_mut_advanced_unique(mut our: Box<i32>) -> i32 {
2+
unknown_code_1(&*our);
3+
4+
// This "re-asserts" uniqueness of the reference: After writing, we know
5+
// our tag is at the top of the stack.
6+
*our = 5;
7+
8+
unknown_code_2();
9+
10+
// We know this will return 5
11+
*our //~ ERROR does not exist on the stack
12+
}
13+
14+
// Now comes the evil context
15+
use std::ptr;
16+
17+
static mut LEAK: *mut i32 = ptr::null_mut();
18+
19+
fn unknown_code_1(x: &i32) { unsafe {
20+
LEAK = x as *const _ as *mut _;
21+
} }
22+
23+
fn unknown_code_2() { unsafe {
24+
*LEAK = 7;
25+
} }
26+
27+
fn main() {
28+
assert_eq!(demo_mut_advanced_unique(Box::new(0)), 5);
29+
}

0 commit comments

Comments
 (0)