Currently, this code does not have (language) UB:
#[repr(align(8))]
struct S {
f: (),
}
fn main() {
let mut x = Box::new(S { f: () });
unsafe { (&raw mut x).cast::<usize>().write(1) };
let _val = &(*x).f;
}
This is because the *x desugars to projecting to the underlying NonNull inside the Box, and then deref'ing that. That is UB if the pointer is null, but does not care about alignment.
Basically, this is where Box behaves more like a library type than a language type. That nicely avoids Miri having to deal with the custom allocator on a Box, but it also means we are losing some information on the opsem level.
I feel like we should probably preserve the actual Box deref for Miri so that we can fully check this case for UB.
Currently, this code does not have (language) UB:
This is because the
*xdesugars to projecting to the underlying NonNull inside the Box, and then deref'ing that. That is UB if the pointer is null, but does not care about alignment.Basically, this is where Box behaves more like a library type than a language type. That nicely avoids Miri having to deal with the custom allocator on a
Box, but it also means we are losing some information on the opsem level.I feel like we should probably preserve the actual Box deref for Miri so that we can fully check this case for UB.