File tree Expand file tree Collapse file tree 2 files changed +12
-5
lines changed
src/tools/miri/tests/fail/unaligned_pointers Expand file tree Collapse file tree 2 files changed +12
-5
lines changed Original file line number Diff line number Diff line change 11// This should fail even without validation/SB
22//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33
4- #![ allow( dead_code, unused_variables, unaligned_references) ]
4+ #![ allow( dead_code, unused_variables) ]
5+
6+ use std:: { ptr, mem} ;
57
68#[ repr( packed) ]
79struct Foo {
810 x : i32 ,
911 y : i32 ,
1012}
1113
14+ unsafe fn raw_to_ref < ' a , T > ( x : * const T ) -> & ' a T {
15+ mem:: transmute ( x)
16+ }
17+
1218fn main ( ) {
1319 // Try many times as this might work by chance.
1420 for _ in 0 ..10 {
1521 let foo = Foo { x : 42 , y : 99 } ;
16- let p = & foo. x ;
17- let i = * p; //~ERROR: alignment 4 is required
22+ // There seem to be implicit reborrows, which make the error already appear here
23+ let p: & i32 = unsafe { raw_to_ref ( ptr:: addr_of!( foo. x) ) } ; //~ERROR: alignment 4 is required
24+ let i = * p;
1825 }
1926}
Original file line number Diff line number Diff line change 11error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
22 --> $DIR/reference_to_packed.rs:LL:CC
33 |
4- LL | let i = *p ;
5- | ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
4+ LL | let p: &i32 = unsafe { raw_to_ref(ptr::addr_of!(foo.x)) } ;
5+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
66 |
77 = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88 = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
You can’t perform that action at this time.
0 commit comments