-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
when an address gets reused, establish a happens-before link in the d…
…ata race model
- Loading branch information
Showing
7 changed files
with
125 additions
and
36 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -6,3 +6,5 @@ pub mod init_once; | |
pub mod thread; | ||
mod vector_clock; | ||
pub mod weak_memory; | ||
|
||
pub use vector_clock::VClock; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//! Regression test for <https://github.com/rust-lang/miri/issues/3450>: | ||
//! When the address gets reused, there should be a happens-before relation. | ||
#![feature(strict_provenance)] | ||
#![feature(sync_unsafe_cell)] | ||
|
||
use std::cell::SyncUnsafeCell; | ||
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; | ||
use std::thread; | ||
|
||
static ADDR: AtomicUsize = AtomicUsize::new(0); | ||
static VAL: SyncUnsafeCell<i32> = SyncUnsafeCell::new(0); | ||
|
||
fn addr() -> usize { | ||
let alloc = Box::new(42); | ||
<*const i32>::addr(&*alloc) | ||
} | ||
|
||
fn thread1() { | ||
unsafe { | ||
VAL.get().write(42); | ||
} | ||
let alloc = addr(); | ||
ADDR.store(alloc, Relaxed); | ||
} | ||
|
||
fn thread2() -> bool { | ||
// We try to get an allocation at the same address. If we fail too often, try again with a | ||
// different allocation to ensure it is still in Miri's reuse cache. | ||
for _ in 0..16 { | ||
let alloc = addr(); | ||
let addr = ADDR.load(Relaxed); | ||
if alloc == addr { | ||
// If the new allocation is at the same address as the old one, there must be a | ||
// happens-before relationship between them. Therefore, we can read VAL without racing | ||
// and must observe the write above. | ||
let val = unsafe { VAL.get().read() }; | ||
assert_eq!(val, 42); | ||
return true; | ||
} | ||
} | ||
|
||
false | ||
} | ||
|
||
fn main() { | ||
let mut success = false; | ||
while !success { | ||
let t1 = thread::spawn(thread1); | ||
let t2 = thread::spawn(thread2); | ||
t1.join().unwrap(); | ||
success = t2.join().unwrap(); | ||
|
||
// Reset everything. | ||
ADDR.store(0, Relaxed); | ||
unsafe { | ||
VAL.get().write(0); | ||
} | ||
} | ||
} |