-
Notifications
You must be signed in to change notification settings - Fork 4
/
0021-futures-intrusive.rs
72 lines (60 loc) · 2.04 KB
/
0021-futures-intrusive.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
/*!
```rudra-poc
[target]
crate = "futures-intrusive"
version = "0.3.1"
[[target.peer]]
crate = "crossbeam-utils"
version = "0.8.0"
[report]
issue_url = "https://github.com/Matthias247/futures-intrusive/issues/53"
issue_date = 2020-10-31
rustsec_url = "https://github.com/RustSec/advisory-db/pull/482"
rustsec_id = "RUSTSEC-2020-0072"
[[bugs]]
analyzer = "SendSyncVariance"
guide = "Manual"
bug_class = "SendSyncVariance"
rudra_report_locations = ["src/sync/mutex.rs:410:1: 413:2"]
```
!*/
#![forbid(unsafe_code)]
use futures_intrusive::sync::{GenericMutexGuard, Mutex};
use crossbeam_utils::thread;
use std::cell::Cell;
static SOME_INT: u64 = 123;
fn main() {
#[derive(Debug, Clone, Copy)]
enum RefOrInt<'a> {
Ref(&'a u64),
Int(u64),
}
let cell = Cell::new(RefOrInt::Ref(&SOME_INT));
let futures_mutex: Mutex<Cell<_>> = Mutex::new(cell, false);
let mutex_guard: GenericMutexGuard<_, Cell<_>> = futures_mutex.try_lock().unwrap();
thread::scope(|s| {
let guard_ref = &mutex_guard;
let child = s.spawn(move |_| {
let smuggled = &(**guard_ref);
println!("In the thread: {:p} {:?}", smuggled, *smuggled);
loop {
// Repeatedly write Ref(&addr) and Int(0xdeadbeef) into the cell.
smuggled.set(RefOrInt::Ref(&SOME_INT));
smuggled.set(RefOrInt::Int(0xdeadbeef));
}
});
println!("In main: {:p} {:?}", &(*mutex_guard), *mutex_guard);
loop {
if let RefOrInt::Ref(addr) = mutex_guard.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);
}
}
});
}