Skip to content

Commit 0fbe710

Browse files
committed
Auto merge of rust-lang#2194 - RalfJung:race, r=RalfJung
add interesting data race test This interesting testcase came up in rust-lang/miri#2192.
2 parents d312b34 + b283200 commit 0fbe710

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-flags: -Zmiri-disable-isolation
2+
// ignore-windows: Concurrency on Windows is not supported yet.
3+
use std::sync::Arc;
4+
use std::sync::atomic::{AtomicUsize, Ordering, fence};
5+
use std::time::Duration;
6+
use std::thread;
7+
8+
fn main() {
9+
static mut V: u32 = 0;
10+
let a = Arc::new(AtomicUsize::default());
11+
let b = a.clone();
12+
thread::spawn(move || {
13+
unsafe { V = 1 }
14+
b.store(1, Ordering::SeqCst);
15+
});
16+
thread::sleep(Duration::from_millis(100));
17+
fence(Ordering::SeqCst);
18+
// Imagine the other thread's actions happening here.
19+
assert_eq!(a.load(Ordering::Relaxed), 1);
20+
// The fence is useless, since it did not happen-after the `store` in the other thread.
21+
// Hence this is a data race.
22+
// Also see https://github.com/rust-lang/miri/issues/2192.
23+
unsafe { V = 2 } //~ERROR Data race detected
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: thread support is experimental and incomplete: weak memory effects are not emulated.
2+
3+
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)
4+
--> $DIR/fence_after_load.rs:LL:CC
5+
|
6+
LL | unsafe { V = 2 }
7+
| ^^^^^ 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)
8+
|
9+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
10+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
11+
12+
= note: inside `main` at $DIR/fence_after_load.rs:LL:CC
13+
14+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
15+
16+
error: aborting due to previous error; 1 warning emitted
17+

0 commit comments

Comments
 (0)