-
Notifications
You must be signed in to change notification settings - Fork 4
/
0161-parallel-event-emitter.rs
77 lines (66 loc) · 1.98 KB
/
0161-parallel-event-emitter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*!
```rudra-poc
[target]
crate = "parallel-event-emitter"
version = "0.2.4"
[report]
issue_url = "https://github.com/novacrazy/parallel-event-emitter/issues/2"
issue_date = 2021-03-02
[[bugs]]
analyzer = "SendSyncVariance"
bug_class = "SendSyncVariance"
rudra_report_locations = ["src/lib.rs:237:5: 237:50"]
```
!*/
#![forbid(unsafe_code)]
use parallel_event_emitter::ParallelEventEmitter;
use std::cell::Cell;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
// A simple tagged union used to demonstrate problems with data races in Cell.
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
enum RefOrInt {
Ref(&'static u64),
Int(u64),
}
static SOME_INT: u64 = 123;
#[derive(PartialEq, Eq, Clone)]
struct Foo(Cell<RefOrInt>);
impl Hash for Foo {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.get().hash(state);
}
}
fn main() {
let non_sync_key = Foo(Cell::new(RefOrInt::Ref(&SOME_INT)));
let mut emit0 = ParallelEventEmitter::new();
emit0.add_listener(
non_sync_key,
|| Ok(()) // dummy listener
);
let emit0 = Arc::new(emit0);
let emit1 = emit0.clone();
std::thread::spawn(move || {
let emit1 = emit1;
emit1.event_names_visitor(|key: &Foo| {
loop {
// Repeatedly write Ref(&addr) and Int(0xdeadbeef) into the cell.
key.0.set(RefOrInt::Ref(&SOME_INT));
key.0.set(RefOrInt::Int(0xdeadbeef));
}
});
});
emit0.event_names_visitor(|key: &Foo| {
loop {
if let RefOrInt::Ref(addr) = key.0.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;
}
println!("Pointer is now: {:p}", addr);
println!("Dereferencing addr will now segfault: {}", *addr);
}
}
});
}