]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/weak_memory/racing_mixed_size_read.rs
Rollup merge of #101118 - devnexen:fs_getmode_bsd, r=Mark-Simulacrum
[rust.git] / src / tools / miri / tests / fail / weak_memory / racing_mixed_size_read.rs
1 // We want to control preemption here.
2 //@compile-flags: -Zmiri-preemption-rate=0
3
4 use std::sync::atomic::Ordering::*;
5 use std::sync::atomic::{AtomicU16, AtomicU32};
6 use std::thread::spawn;
7
8 fn static_atomic(val: u32) -> &'static AtomicU32 {
9     let ret = Box::leak(Box::new(AtomicU32::new(val)));
10     ret
11 }
12
13 fn split_u32_ptr(dword: *const u32) -> *const [u16; 2] {
14     unsafe { std::mem::transmute::<*const u32, *const [u16; 2]>(dword) }
15 }
16
17 // Racing mixed size reads may cause two loads to read-from
18 // the same store but observe different values, which doesn't make
19 // sense under the formal model so we forbade this.
20 pub fn main() {
21     let x = static_atomic(0);
22
23     let j1 = spawn(move || {
24         x.load(Relaxed);
25     });
26
27     let j2 = spawn(move || {
28         let x_ptr = x as *const AtomicU32 as *const u32;
29         let x_split = split_u32_ptr(x_ptr);
30         unsafe {
31             let hi = x_split as *const u16 as *const AtomicU16;
32             (*hi).load(Relaxed); //~ ERROR: imperfectly overlapping
33         }
34     });
35
36     j1.join().unwrap();
37     j2.join().unwrap();
38 }