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