Description
Currently, we have a test that asserts that code is not allowed to rely on any 'extra' information provided by the intprcast (e.g. a [u16; 2]
array that happens to have alignment '4').
However, this is a perfectly legitimate thing for code to do. I propose that we add a intprcast-alignment
option which enables the following behavior:
When we check the alignment for a memory access, we see if we have a recorded base address for the allocation (i.e. if a pointer within the allocation was ever cast to an integer).
If we do not have a base address, we use the current alignment checking behavior (i.e. check the static alignment of the type). This will catch cases where the code is definitely wrong - if the pointer was never cast to an integer, the code cannot possibly know that it happened to have 'extra' alignment.
If we do have a base address, then we do the alignment check based on the actual base address. This will allow some incorrect code, like:
let mut my_arr: [u8; 100]`
my_arr.as_ptr() as usuze; // Dummy cast
unsafe { *(my_arr.as_mut_ptr() as *mut u16) = 25 }
Depending on what alignment we pick for the base address of my_addr
, this may or may not work. This means that whether or not this program passes now depends on the random seed.
While this isn't ideal, I don't see a way of allowing the intptrcast_alignment_check to pass without also causing 'spurious passes' (code which really should fail, but doesn't).