]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/data_race/dangling_thread_race.rs
Rollup merge of #101555 - jhpratt:stabilize-mixed_integer_ops, r=joshtriplett
[rust.git] / src / tools / miri / tests / fail / data_race / dangling_thread_race.rs
1 // We want to control preemption here.
2 //@compile-flags: -Zmiri-disable-isolation -Zmiri-preemption-rate=0
3
4 use std::mem;
5 use std::thread::{sleep, spawn};
6 use std::time::Duration;
7
8 #[derive(Copy, Clone)]
9 struct EvilSend<T>(pub T);
10
11 unsafe impl<T> Send for EvilSend<T> {}
12 unsafe impl<T> Sync for EvilSend<T> {}
13
14 fn main() {
15     let mut a = 0u32;
16     let b = &mut a as *mut u32;
17     let c = EvilSend(b);
18
19     let join = unsafe {
20         spawn(move || {
21             *c.0 = 32;
22         })
23     };
24
25     // Detach the thread and sleep until it terminates
26     mem::drop(join);
27     sleep(Duration::from_millis(200));
28
29     // Spawn and immediately join a thread
30     // to execute the join code-path
31     // and ensure that data-race detection
32     // remains enabled nevertheless.
33     spawn(|| ()).join().unwrap();
34
35     unsafe {
36         *c.0 = 64; //~ ERROR: Data race detected between Write on thread `main` and Write on thread `<unnamed>`
37     }
38 }