-
Notifications
You must be signed in to change notification settings - Fork 5
/
0162-internment.rs
80 lines (68 loc) · 1.87 KB
/
0162-internment.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
/*!
```rudra-poc
[target]
crate = "internment"
version = "0.3.13"
[report]
issue_url = "https://github.com/droundy/internment/issues/20"
issue_date = 2021-03-03
rustsec_url = "https://github.com/RustSec/advisory-db/pull/807"
rustsec_id = "RUSTSEC-2021-0036"
[[bugs]]
analyzer = "SendSyncVariance"
bug_class = "SendSyncVariance"
rudra_report_locations = ["src/lib.rs:116:1: 116:37"]
```
!*/
#![forbid(unsafe_code)]
use internment::Intern;
use std::borrow::Borrow;
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(Debug, PartialEq, Eq, Clone)]
struct Foo(Cell<RefOrInt>);
impl Hash for Foo {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.get().hash(state);
}
}
impl Foo {
fn set(&self, v: RefOrInt) {
self.0.set(v);
}
fn get(&self) -> RefOrInt {
self.0.get()
}
}
fn main() {
let non_sync = Foo(Cell::new(RefOrInt::Ref(&SOME_INT)));
let i0 = Arc::new(Intern::new(non_sync));
let i1 = i0.clone();
std::thread::spawn(move || {
let i1 = i1;
loop {
// Repeatedly write Ref(&addr) and Int(0xdeadbeef) into the cell.
i1.set(RefOrInt::Ref(&SOME_INT));
i1.set(RefOrInt::Int(0xdeadbeef));
}
});
loop {
if let RefOrInt::Ref(addr) = i0.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);
}
}
}