-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from o0Ignition0o/igni/atomicbox_send_sync
AtomicBox should have bounds on its Send/Sync traits
- Loading branch information
Showing
4 changed files
with
53 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[test] | ||
fn atomic_box_data_race_regression_test() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/samples/atomic_box_data_race.rs"); | ||
} |
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,44 @@ | ||
use lever::sync::atomics::AtomicBox; | ||
|
||
use crossbeam_utils::thread; | ||
use std::cell::Cell; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
enum RefOrInt<'a> { | ||
Ref(&'a u64), | ||
Int(u64), | ||
} | ||
static SOME_INT: u64 = 123; | ||
|
||
// https://github.com/vertexclique/lever/issues/15#issue-740069651 | ||
// Run this one in release mode for it to fail | ||
fn main() { | ||
let cell = Cell::new(RefOrInt::Ref(&SOME_INT)); | ||
let atomic_box = AtomicBox::new(&cell); | ||
|
||
thread::scope(|s| { | ||
s.spawn(move |_| { | ||
let smuggled_cell = atomic_box.get(); | ||
|
||
loop { | ||
// Repeatedly write Ref(&addr) and Int(0xdeadbeef) into the cell. | ||
smuggled_cell.set(RefOrInt::Ref(&SOME_INT)); | ||
smuggled_cell.set(RefOrInt::Int(0xdeadbeef)); | ||
} | ||
}); | ||
|
||
loop { | ||
if let RefOrInt::Ref(addr) = cell.get() { | ||
// Hope that between the time we pattern match the object as a | ||
// `Ref`, it gets written to by the other thread. | ||
if addr as *const u64 == &SOME_INT as *const u64 { | ||
continue; | ||
} | ||
|
||
// Due to the data race, obtaining Ref(0xdeadbeef) is possible | ||
println!("Pointer is now: {:p}", addr); | ||
println!("Dereferencing addr will now segfault: {}", *addr); | ||
} | ||
} | ||
}); | ||
} |