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 1
1
// This should fail even without validation/SB
2
2
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
3
3
4
- #![ allow( dead_code, unused_variables, unaligned_references) ]
4
+ #![ allow( dead_code, unused_variables) ]
5
+
6
+ use std:: { ptr, mem} ;
5
7
6
8
#[ repr( packed) ]
7
9
struct Foo {
8
10
x : i32 ,
9
11
y : i32 ,
10
12
}
11
13
14
+ unsafe fn raw_to_ref < ' a , T > ( x : * const T ) -> & ' a T {
15
+ mem:: transmute ( x)
16
+ }
17
+
12
18
fn main ( ) {
13
19
// Try many times as this might work by chance.
14
20
for _ in 0 ..10 {
15
21
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;
18
25
}
19
26
}
Original file line number Diff line number Diff line change 1
1
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
2
2
--> $DIR/reference_to_packed.rs:LL:CC
3
3
|
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
6
6
|
7
7
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8
8
= 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