-
Notifications
You must be signed in to change notification settings - Fork 4
/
0033-hashconsing.rs
82 lines (69 loc) · 2.22 KB
/
0033-hashconsing.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
78
79
80
81
82
/*!
```rudra-poc
[target]
crate = "hashconsing"
version = "1.0.1"
[[target.peer]]
crate = "crossbeam-utils"
version = "0.8.0"
[report]
issue_url = "https://github.com/AdrienChampion/hashconsing/issues/1"
issue_date = 2020-11-10
rustsec_url = "https://github.com/RustSec/advisory-db/pull/584"
rustsec_id = "RUSTSEC-2020-0107"
[[bugs]]
analyzer = "SendSyncVariance"
bug_class = "SendSyncVariance"
bug_count = 2
rudra_report_locations = ["src/lib.rs:355:1: 355:38", "src/lib.rs:354:1: 354:38"]
```
!*/
#![forbid(unsafe_code)]
use hashconsing::{HConsed, HConsign, HashConsign};
use crossbeam_utils::thread;
use std::cell::Cell;
use std::hash::{Hash, Hasher};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum RefOrInt<'a> {
Ref(&'a u64),
Int(u64),
}
#[derive(PartialEq, Eq)]
struct HashableCell<T: Eq + PartialEq + Copy> {
cell: Cell<T>,
}
// Fake hashing function just so we can get a HConsed going.
impl<T: Eq + PartialEq + Copy> Hash for HashableCell<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
1024.hash(state);
}
}
static SOME_INT: u64 = 123;
fn main() {
let cell = Cell::new(RefOrInt::Ref(&SOME_INT));
let hashable_cell = HashableCell { cell: cell };
let mut factory: HConsign<_> = HConsign::empty();
let hcons_cell_ref = factory.mk(&hashable_cell);
thread::scope(|s| {
s.spawn(move |_| {
let smuggled_cell = &hcons_cell_ref.get().cell;
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) = hashable_cell.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);
}
}
});
}