2022-07-08 11:08:32 -05:00
|
|
|
//@compile-flags: -Zmiri-disable-isolation -Zmiri-disable-weak-memory-emulation -Zmiri-preemption-rate=0
|
2020-12-07 12:16:06 -06:00
|
|
|
|
|
|
|
use std::ptr::null_mut;
|
2022-06-19 22:33:59 -05:00
|
|
|
use std::sync::atomic::{AtomicPtr, Ordering};
|
|
|
|
use std::thread::{sleep, spawn};
|
2020-12-07 12:16:06 -06:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct EvilSend<T>(pub T);
|
|
|
|
|
|
|
|
unsafe impl<T> Send for EvilSend<T> {}
|
|
|
|
unsafe impl<T> Sync for EvilSend<T> {}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
// Shared atomic pointer
|
|
|
|
let pointer = AtomicPtr::new(null_mut::<usize>());
|
|
|
|
let ptr = EvilSend(&pointer as *const AtomicPtr<usize>);
|
|
|
|
|
|
|
|
// Note: this is scheduler-dependent
|
|
|
|
// the operations need to occur in
|
|
|
|
// order, otherwise the allocation is
|
|
|
|
// not visible to the other-thread to
|
|
|
|
// detect the race:
|
|
|
|
// 1. stack-allocate
|
|
|
|
// 2. read
|
|
|
|
// 3. stack-deallocate
|
|
|
|
unsafe {
|
|
|
|
let j1 = spawn(move || {
|
|
|
|
let pointer = &*ptr.0;
|
|
|
|
{
|
|
|
|
let mut stack_var = 0usize;
|
|
|
|
|
|
|
|
pointer.store(&mut stack_var as *mut _, Ordering::Release);
|
|
|
|
|
2020-12-09 07:35:42 -06:00
|
|
|
sleep(Duration::from_millis(200));
|
2020-12-07 12:16:06 -06:00
|
|
|
|
2021-03-02 03:44:06 -06:00
|
|
|
// Now `stack_var` gets deallocated.
|
2022-07-11 06:44:55 -05:00
|
|
|
} //~ ERROR: Data race detected between Deallocate on thread `<unnamed>` and Read on thread `<unnamed>`
|
2020-12-07 12:16:06 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
let j2 = spawn(move || {
|
|
|
|
let pointer = &*ptr.0;
|
|
|
|
*pointer.load(Ordering::Acquire)
|
|
|
|
});
|
|
|
|
|
|
|
|
j1.join().unwrap();
|
|
|
|
j2.join().unwrap();
|
|
|
|
}
|
|
|
|
}
|