-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #3137 - RalfJung:data-race, r=oli-obk
Detect mixed-size and mixed-atomicity non-synchronized accesses Fixes #2303
- Loading branch information
Showing
18 changed files
with
415 additions
and
224 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//@compile-flags: -Zmiri-preemption-rate=0.0 -Zmiri-disable-weak-memory-emulation | ||
use std::sync::atomic::{AtomicU16, AtomicU8, Ordering}; | ||
use std::thread; | ||
|
||
fn convert(a: &AtomicU16) -> &[AtomicU8; 2] { | ||
unsafe { std::mem::transmute(a) } | ||
} | ||
|
||
// We can't allow mixed-size accesses; they are not possible in C++ and even | ||
// Intel says you shouldn't do it. | ||
fn main() { | ||
let a = AtomicU16::new(0); | ||
let a16 = &a; | ||
let a8 = convert(a16); | ||
|
||
thread::scope(|s| { | ||
s.spawn(|| { | ||
a16.load(Ordering::SeqCst); | ||
}); | ||
s.spawn(|| { | ||
a8[0].load(Ordering::SeqCst); | ||
//~^ ERROR: Race condition detected between (1) 2-byte Atomic Load on thread `<unnamed>` and (2) 1-byte (different-size) Atomic Load on thread `<unnamed>` | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: Undefined Behavior: Race condition detected between (1) 2-byte Atomic Load on thread `<unnamed>` and (2) 1-byte (different-size) Atomic Load on thread `<unnamed>` at ALLOC. (2) just happened here | ||
--> $DIR/mixed_size_read.rs:LL:CC | ||
Check failure on line 2 in tests/fail/data_race/mixed_size_read.stderr GitHub Actions / build (ubuntu-latest, x86_64-unknown-linux-gnu)actual output differs from expected
|
||
| | ||
LL | a8[0].load(Ordering::SeqCst); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Race condition detected between (1) 2-byte Atomic Load on thread `<unnamed>` and (2) 1-byte (different-size) Atomic Load on thread `<unnamed>` at ALLOC. (2) just happened here | ||
| | ||
help: and (1) occurred earlier here | ||
--> $DIR/mixed_size_read.rs:LL:CC | ||
Check failure on line 8 in tests/fail/data_race/mixed_size_read.stderr GitHub Actions / build (ubuntu-latest, x86_64-unknown-linux-gnu)actual output differs from expected
|
||
| | ||
LL | a16.load(Ordering::SeqCst); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= 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: BACKTRACE (of the first span): | ||
= note: inside closure at $DIR/mixed_size_read.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//@compile-flags: -Zmiri-preemption-rate=0.0 -Zmiri-disable-weak-memory-emulation | ||
use std::sync::atomic::{AtomicU16, AtomicU8, Ordering}; | ||
use std::thread; | ||
|
||
fn convert(a: &AtomicU16) -> &[AtomicU8; 2] { | ||
unsafe { std::mem::transmute(a) } | ||
} | ||
|
||
// We can't allow mixed-size accesses; they are not possible in C++ and even | ||
// Intel says you shouldn't do it. | ||
fn main() { | ||
let a = AtomicU16::new(0); | ||
let a16 = &a; | ||
let a8 = convert(a16); | ||
|
||
thread::scope(|s| { | ||
s.spawn(|| { | ||
a16.store(1, Ordering::SeqCst); | ||
}); | ||
s.spawn(|| { | ||
a8[0].store(1, Ordering::SeqCst); | ||
//~^ ERROR: Race condition detected between (1) 2-byte Atomic Store on thread `<unnamed>` and (2) 1-byte (different-size) Atomic Store on thread `<unnamed>` | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: Undefined Behavior: Race condition detected between (1) 2-byte Atomic Store on thread `<unnamed>` and (2) 1-byte (different-size) Atomic Store on thread `<unnamed>` at ALLOC. (2) just happened here | ||
--> $DIR/mixed_size_write.rs:LL:CC | ||
Check failure on line 2 in tests/fail/data_race/mixed_size_write.stderr GitHub Actions / build (ubuntu-latest, x86_64-unknown-linux-gnu)actual output differs from expected
|
||
| | ||
LL | a8[0].store(1, Ordering::SeqCst); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Race condition detected between (1) 2-byte Atomic Store on thread `<unnamed>` and (2) 1-byte (different-size) Atomic Store on thread `<unnamed>` at ALLOC. (2) just happened here | ||
| | ||
help: and (1) occurred earlier here | ||
--> $DIR/mixed_size_write.rs:LL:CC | ||
Check failure on line 8 in tests/fail/data_race/mixed_size_write.stderr GitHub Actions / build (ubuntu-latest, x86_64-unknown-linux-gnu)actual output differs from expected
|
||
| | ||
LL | a16.store(1, Ordering::SeqCst); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= 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: BACKTRACE (of the first span): | ||
= note: inside closure at $DIR/mixed_size_write.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//@compile-flags: -Zmiri-preemption-rate=0.0 | ||
use std::sync::atomic::{AtomicU16, Ordering}; | ||
use std::thread; | ||
|
||
// Make sure races between atomic and non-atomic reads are detected. | ||
// This seems harmless but C++ does not allow them, so we can't allow them for now either. | ||
// This test coverse the case where the non-atomic access come first. | ||
fn main() { | ||
let a = AtomicU16::new(0); | ||
|
||
thread::scope(|s| { | ||
s.spawn(|| { | ||
let ptr = &a as *const AtomicU16 as *mut u16; | ||
unsafe { ptr.read() }; | ||
}); | ||
s.spawn(|| { | ||
thread::yield_now(); | ||
|
||
// We also put a non-atomic access here, but that should *not* be reported. | ||
let ptr = &a as *const AtomicU16 as *mut u16; | ||
unsafe { ptr.read() }; | ||
// Then do the atomic access. | ||
a.load(Ordering::SeqCst); | ||
//~^ ERROR: Data race detected between (1) Read on thread `<unnamed>` and (2) Atomic Load on thread `<unnamed>` | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: Undefined Behavior: Data race detected between (1) Read on thread `<unnamed>` and (2) Atomic Load on thread `<unnamed>` at ALLOC. (2) just happened here | ||
--> $DIR/read_read_race1.rs:LL:CC | ||
| | ||
LL | a.load(Ordering::SeqCst); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ Data race detected between (1) Read on thread `<unnamed>` and (2) Atomic Load on thread `<unnamed>` at ALLOC. (2) just happened here | ||
| | ||
help: and (1) occurred earlier here | ||
--> $DIR/read_read_race1.rs:LL:CC | ||
| | ||
LL | unsafe { ptr.read() }; | ||
| ^^^^^^^^^^ | ||
= 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: BACKTRACE (of the first span): | ||
= note: inside closure at $DIR/read_read_race1.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//@compile-flags: -Zmiri-preemption-rate=0.0 | ||
use std::sync::atomic::{AtomicU16, Ordering}; | ||
use std::thread; | ||
|
||
// Make sure races between atomic and non-atomic reads are detected. | ||
// This seems harmless but C++ does not allow them, so we can't allow them for now either. | ||
// This test coverse the case where the atomic access come first. | ||
fn main() { | ||
let a = AtomicU16::new(0); | ||
|
||
thread::scope(|s| { | ||
s.spawn(|| { | ||
// We also put a non-atomic access here, but that should *not* be reported. | ||
let ptr = &a as *const AtomicU16 as *mut u16; | ||
unsafe { ptr.read() }; | ||
// Then do the atomic access. | ||
a.load(Ordering::SeqCst); | ||
}); | ||
s.spawn(|| { | ||
thread::yield_now(); | ||
|
||
let ptr = &a as *const AtomicU16 as *mut u16; | ||
unsafe { ptr.read() }; | ||
//~^ ERROR: Data race detected between (1) Atomic Load on thread `<unnamed>` and (2) Read on thread `<unnamed>` | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: Undefined Behavior: Data race detected between (1) Atomic Load on thread `<unnamed>` and (2) Read on thread `<unnamed>` at ALLOC. (2) just happened here | ||
--> $DIR/read_read_race2.rs:LL:CC | ||
| | ||
LL | unsafe { ptr.read() }; | ||
| ^^^^^^^^^^ Data race detected between (1) Atomic Load on thread `<unnamed>` and (2) Read on thread `<unnamed>` at ALLOC. (2) just happened here | ||
| | ||
help: and (1) occurred earlier here | ||
--> $DIR/read_read_race2.rs:LL:CC | ||
| | ||
LL | a.load(Ordering::SeqCst); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= 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: BACKTRACE (of the first span): | ||
= note: inside closure at $DIR/read_read_race2.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.