]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/unaligned_pointers/dyn_alignment.rs
Rollup merge of #101642 - SkiFire13:fix-inplace-collection-leak, r=the8472
[rust.git] / src / tools / miri / tests / fail / unaligned_pointers / dyn_alignment.rs
1 // should find the bug even without validation and stacked borrows, but gets masked by optimizations
2 //@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Zmir-opt-level=0
3
4 #[repr(align(256))]
5 #[derive(Debug)]
6 struct MuchAlign;
7
8 fn main() {
9     // Try many times as this might work by chance.
10     for _ in 0..10 {
11         let buf = [0u32; 256];
12         // `buf` is sufficiently aligned for `layout.align` on a `dyn Debug`, but not
13         // for the actual alignment required by `MuchAlign`.
14         // We craft a wide reference `&dyn Debug` with the vtable for `MuchAlign`. That should be UB,
15         // as the reference is not aligned to its dynamic alignment requirements.
16         let mut ptr = &MuchAlign as &dyn std::fmt::Debug;
17         // Overwrite the data part of `ptr` so it points to `buf`.
18         unsafe {
19             (&mut ptr as *mut _ as *mut *const u8).write(&buf as *const _ as *const u8);
20         }
21         // Re-borrow that. This should be UB.
22         let _ptr = &*ptr; //~ERROR: alignment 256 is required
23     }
24 }