add interesting data race test

This commit is contained in:
Ralf Jung 2022-06-05 12:14:57 -04:00
parent 5a1b09eb9f
commit b2832008e2
2 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,24 @@
// compile-flags: -Zmiri-disable-isolation
// ignore-windows: Concurrency on Windows is not supported yet.
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering, fence};
use std::time::Duration;
use std::thread;
fn main() {
static mut V: u32 = 0;
let a = Arc::new(AtomicUsize::default());
let b = a.clone();
thread::spawn(move || {
unsafe { V = 1 }
b.store(1, Ordering::SeqCst);
});
thread::sleep(Duration::from_millis(100));
fence(Ordering::SeqCst);
// Imagine the other thread's actions happening here.
assert_eq!(a.load(Ordering::Relaxed), 1);
// The fence is useless, since it did not happen-after the `store` in the other thread.
// Hence this is a data race.
// Also see https://github.com/rust-lang/miri/issues/2192.
unsafe { V = 2 } //~ERROR Data race detected
}

View File

@ -0,0 +1,17 @@
warning: thread support is experimental and incomplete: weak memory effects are not emulated.
error: Undefined Behavior: Data race detected between Write on Thread(id = 0, name = "main") and Write on Thread(id = 1) at ALLOC (current vector clock = VClock, conflicting timestamp = VClock)
--> $DIR/fence_after_load.rs:LL:CC
|
LL | unsafe { V = 2 }
| ^^^^^ Data race detected between Write on Thread(id = 0, name = "main") and Write on Thread(id = 1) at ALLOC (current vector clock = VClock, conflicting timestamp = VClock)
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: inside `main` at $DIR/fence_after_load.rs:LL:CC
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
error: aborting due to previous error; 1 warning emitted