From b2832008e236d20fdb1ba7d724d389295d1d2493 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 5 Jun 2022 12:14:57 -0400 Subject: [PATCH] add interesting data race test --- tests/fail/data_race/fence_after_load.rs | 24 ++++++++++++++++++++ tests/fail/data_race/fence_after_load.stderr | 17 ++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/fail/data_race/fence_after_load.rs create mode 100644 tests/fail/data_race/fence_after_load.stderr diff --git a/tests/fail/data_race/fence_after_load.rs b/tests/fail/data_race/fence_after_load.rs new file mode 100644 index 00000000000..b3817159338 --- /dev/null +++ b/tests/fail/data_race/fence_after_load.rs @@ -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 +} diff --git a/tests/fail/data_race/fence_after_load.stderr b/tests/fail/data_race/fence_after_load.stderr new file mode 100644 index 00000000000..14452391327 --- /dev/null +++ b/tests/fail/data_race/fence_after_load.stderr @@ -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 +