]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/reading_half_a_pointer.rs
Preparing for merge from rustc
[rust.git] / src / tools / miri / tests / fail / reading_half_a_pointer.rs
1 #![allow(dead_code)]
2
3 // We use packed structs to get around alignment restrictions
4 #[repr(packed)]
5 struct Data {
6     pad: u8,
7     ptr: &'static i32,
8 }
9
10 // But we need to gurantee some alignment
11 struct Wrapper {
12     align: u64,
13     data: Data,
14 }
15
16 static G: i32 = 0;
17
18 fn main() {
19     let mut w = Wrapper { align: 0, data: Data { pad: 0, ptr: &G } };
20
21     // Get a pointer to the beginning of the Data struct (one u8 byte, then the pointer bytes).
22     // Thanks to the wrapper, we know this is aligned-enough to perform a load at ptr size.
23     // We load at pointer type, so having a relocation is ok -- but here, the relocation
24     // starts 1 byte to the right, so using it would actually be wrong!
25     let d_alias = &mut w.data as *mut _ as *mut *const u8;
26     unsafe {
27         let x = *d_alias;
28         let _val = *x; //~ERROR: is a dangling pointer (it has no provenance)
29     }
30 }