Closed
Description
The following example passes Miri, but has UB in LLVM:
unsafe fn test(mut x: Box<i32>, y: *const i32) -> i32 {
// We will call this in a way that x and y alias.
*x = 5;
std::mem::forget(x);
*y // this invalidates x, but that's fine since Box can be invalidated during the function
}
fn main() { unsafe {
let mut v = 42;
let ptr = &mut v as *mut i32;
test(Box::from_raw(ptr), ptr);
} }
The reason for this is that we allow a Box
pointer to be invalidated while test
runs (which is necessary because the function might deallocate it), so Stacked Borrows says it is fine to use an aliasing pointer (y
) while test
runs as long as we don't use x
again afterwards.