]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/data_race/dealloc_read_race1.rs
Auto merge of #101969 - reez12g:issue-101306, r=reez12g
[rust.git] / src / tools / miri / tests / fail / data_race / dealloc_read_race1.rs
1 // We want to control preemption here.
2 //@compile-flags: -Zmiri-preemption-rate=0
3
4 use std::thread::spawn;
5
6 #[derive(Copy, Clone)]
7 struct EvilSend<T>(pub T);
8
9 unsafe impl<T> Send for EvilSend<T> {}
10 unsafe impl<T> Sync for EvilSend<T> {}
11
12 extern "Rust" {
13     fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
14 }
15
16 pub fn main() {
17     // Shared atomic pointer
18     let pointer: *mut usize = Box::into_raw(Box::new(0usize));
19     let ptr = EvilSend(pointer);
20
21     unsafe {
22         let j1 = spawn(move || {
23             let _val = *ptr.0;
24         });
25
26         let j2 = spawn(move || {
27             __rust_dealloc(
28                 //~^ ERROR: Data race detected between Deallocate on thread `<unnamed>` and Read on thread `<unnamed>`
29                 ptr.0 as *mut _,
30                 std::mem::size_of::<usize>(),
31                 std::mem::align_of::<usize>(),
32             );
33         });
34
35         j1.join().unwrap();
36         j2.join().unwrap();
37     }
38 }