]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/data_race/dealloc_write_race2.rs
Rollup merge of #101555 - jhpratt:stabilize-mixed_integer_ops, r=joshtriplett
[rust.git] / src / tools / miri / tests / fail / data_race / dealloc_write_race2.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 pub fn main() {
16     // Shared atomic pointer
17     let pointer: *mut usize = Box::into_raw(Box::new(0usize));
18     let ptr = EvilSend(pointer);
19
20     unsafe {
21         let j1 = spawn(move || {
22             __rust_dealloc(
23                 ptr.0 as *mut _,
24                 std::mem::size_of::<usize>(),
25                 std::mem::align_of::<usize>(),
26             );
27         });
28
29         let j2 = spawn(move || {
30             // Also an error of the form: Data race detected between Write on thread `<unnamed>` and Deallocate on thread `<unnamed>`
31             // but the invalid allocation is detected first.
32             *ptr.0 = 2; //~ ERROR: dereferenced after this allocation got freed
33         });
34
35         j1.join().unwrap();
36         j2.join().unwrap();
37     }
38 }