-
Notifications
You must be signed in to change notification settings - Fork 5
/
0066-kekbit.rs
75 lines (62 loc) · 1.83 KB
/
0066-kekbit.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
/*!
```rudra-poc
[target]
crate = "kekbit"
version = "0.3.3"
[[target.peer]]
crate = "tempdir"
version = "0.3.7"
[report]
issue_url = "https://github.com/motoras/kekbit/issues/34"
issue_date = 2020-12-18
rustsec_url = "https://github.com/RustSec/advisory-db/pull/706"
rustsec_id = "RUSTSEC-2020-0129"
[[bugs]]
analyzer = "SendSyncVariance"
bug_class = "SendSyncVariance"
rudra_report_locations = ["src/core/writer.rs:82:1: 82:49"]
```
!*/
#![forbid(unsafe_code)]
use std::marker::PhantomData;
use std::thread;
use kekbit::api::Handler;
use kekbit::core::{shm_writer, Metadata, TickUnit::Nanos};
// non-Send type that panics when dropped in a wrong thread
struct NonSend {
created_thread: thread::ThreadId,
// Ensure `NonSend` type does not implement `Send` trait
_marker: PhantomData<*mut ()>,
}
impl NonSend {
pub fn new() -> Self {
NonSend {
created_thread: thread::current().id(),
_marker: PhantomData,
}
}
}
impl Drop for NonSend {
fn drop(&mut self) {
if thread::current().id() != self.created_thread {
panic!("NonSend destructor is running on a wrong thread!");
}
}
}
impl Handler for NonSend {}
fn main() {
// Example code from: https://docs.rs/kekbit/0.3.3/kekbit/core/fn.shm_writer.html#examples
const FOREVER: u64 = 99_999_999_999;
let writer_id = 1850;
let channel_id = 42;
let capacity = 3000;
let max_msg_len = 100;
let metadata = Metadata::new(writer_id, channel_id, capacity, max_msg_len, FOREVER, Nanos);
let test_tmp_dir = tempdir::TempDir::new("kekbit").unwrap();
let writer = shm_writer(&test_tmp_dir.path(), &metadata, NonSend::new()).unwrap();
let handle = thread::spawn(move || {
// `NonSend` is sent to another thread via `ShmWriter`
drop(writer);
});
handle.join().unwrap();
}