2020-08-16 10:08:34 -05:00
|
|
|
// should find the bug even without validation and stacked borrows, but gets masked by optimizations
|
2020-04-18 02:15:59 -05:00
|
|
|
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Zmir-opt-level=0
|
2020-04-17 13:43:54 -05:00
|
|
|
|
|
|
|
#[repr(align(256))]
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct MuchAlign;
|
|
|
|
|
|
|
|
fn main() {
|
2022-06-22 01:48:09 -05:00
|
|
|
// Try many times as this might work by chance.
|
2022-06-22 00:57:06 -05:00
|
|
|
for _ in 0..10 {
|
2020-08-18 03:11:54 -05:00
|
|
|
let buf = [0u32; 256];
|
|
|
|
// `buf` is sufficiently aligned for `layout.align` on a `dyn Debug`, but not
|
|
|
|
// for the actual alignment required by `MuchAlign`.
|
|
|
|
// We craft a wide reference `&dyn Debug` with the vtable for `MuchAlign`. That should be UB,
|
|
|
|
// as the reference is not aligned to its dynamic alignment requirements.
|
|
|
|
let mut ptr = &MuchAlign as &dyn std::fmt::Debug;
|
|
|
|
// Overwrite the data part of `ptr` so it points to `buf`.
|
2022-06-22 00:57:06 -05:00
|
|
|
unsafe {
|
|
|
|
(&mut ptr as *mut _ as *mut *const u8).write(&buf as *const _ as *const u8);
|
|
|
|
}
|
2020-08-18 03:11:54 -05:00
|
|
|
// Re-borrow that. This should be UB.
|
2022-05-25 11:15:37 -05:00
|
|
|
let _ptr = &*ptr; //~ERROR alignment 256 is required
|
2020-08-18 03:11:54 -05:00
|
|
|
}
|
2020-04-17 13:43:54 -05:00
|
|
|
}
|