File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
tests/compile-fail/unaligned_pointers Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change
1
+ // should find the bug even without these
2
+ // compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
3
+
4
+ #[ repr( align( 256 ) ) ]
5
+ #[ derive( Debug ) ]
6
+ struct MuchAlign ;
7
+
8
+ fn main ( ) {
9
+ let buf = [ 0u32 ; 256 ] ;
10
+ // `buf` is sufficiently aligned for `layout.align` on a `dyn Debug`, but not
11
+ // for the actual alignment required by `MuchAlign`.
12
+ // We craft a wide reference `&dyn Debug` with the vtable for `MuchAlign`. That should be UB,
13
+ // as the reference is not aligned to its dynamic alignment requirements.
14
+ let mut ptr = & MuchAlign as & dyn std:: fmt:: Debug ;
15
+ // Overwrite the data part of `ptr`.
16
+ unsafe { ( & mut ptr as * mut _ as * mut * const u8 ) . write ( & buf as * const _ as * const u8 ) ; }
17
+ // Re-borrow that. This should be UB.
18
+ let _ptr = & * ptr; //~ ERROR accessing memory with alignment 4, but alignment 256 is required
19
+ }
You can’t perform that action at this time.
0 commit comments