forked from rust-lang/miri
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make unaligned_reference a hard error
- Loading branch information
Showing
2 changed files
with
12 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
// This should fail even without validation/SB | ||
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows | ||
|
||
#![allow(dead_code, unused_variables, unaligned_references)] | ||
#![allow(dead_code, unused_variables)] | ||
|
||
use std::{ptr, mem}; | ||
|
||
#[repr(packed)] | ||
struct Foo { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
unsafe fn raw_to_ref<'a, T>(x: *const T) -> &'a T { | ||
mem::transmute(x) | ||
} | ||
|
||
fn main() { | ||
// Try many times as this might work by chance. | ||
for _ in 0..20 { | ||
let foo = Foo { x: 42, y: 99 }; | ||
let p = &foo.x; | ||
let i = *p; //~ERROR: alignment 4 is required | ||
// There seem to be implicit reborrows, which make the error already appear here | ||
let p: &i32 = unsafe { raw_to_ref(ptr::addr_of!(foo.x)) }; //~ERROR: alignment 4 is required | ||
let i = *p; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters