Closed
Description
Consider the following example (due to @ssomers):
#![feature(raw_ref_macros)]
use std::ptr;
struct Part {
_lame: i32,
}
#[repr(C)]
struct Whole {
part: Part,
extra: i32,
}
fn main() {
let it = Box::new(Whole { part: Part { _lame: 0 }, extra: 42 });
let whole = ptr::raw_mut!(*Box::leak(it));
let part = unsafe { ptr::raw_mut!((*whole).part) };
let typed = unsafe { &mut *(part as *mut Whole) };
assert!(typed.extra == 42);
}
Arguably, this is using raw_mut!
correctly, but Miri flags this code as UB when using -Zmiri-track-raw-pointers
. The problem is that at ptr::raw_mut!((*whole).part)
, a raw retag happens, which generates a fresh tag for part
that is only valid for the memory of the part
field, not for the extra
field.