Skip to content

Commit 3e089b0

Browse files
FrankReholi-obk
authored andcommitted
epoll: add data_race test
This test demonstrates the need to synchronize the clock of the thread waking up from an epoll_wait from the thread that issued the epoll awake event.
1 parent 97510cd commit 3e089b0

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//@only-target: linux
2+
// test_epoll_race depends on a deterministic schedule.
3+
//@compile-flags: -Zmiri-preemption-rate=0
4+
5+
use std::convert::TryInto;
6+
use std::thread;
7+
8+
fn main() {
9+
test_epoll_race();
10+
}
11+
12+
// Using `as` cast since `EPOLLET` wraps around
13+
const EPOLL_IN_OUT_ET: u32 = (libc::EPOLLIN | libc::EPOLLOUT | libc::EPOLLET) as _;
14+
15+
#[track_caller]
16+
fn check_epoll_wait<const N: usize>(
17+
epfd: i32,
18+
expected_notifications: &[(u32, u64)],
19+
timeout: i32,
20+
) {
21+
let epoll_event = libc::epoll_event { events: 0, u64: 0 };
22+
let mut array: [libc::epoll_event; N] = [epoll_event; N];
23+
let maxsize = N;
24+
let array_ptr = array.as_mut_ptr();
25+
let res = unsafe { libc::epoll_wait(epfd, array_ptr, maxsize.try_into().unwrap(), timeout) };
26+
if res < 0 {
27+
panic!("epoll_wait failed: {}", std::io::Error::last_os_error());
28+
}
29+
assert_eq!(
30+
res,
31+
expected_notifications.len().try_into().unwrap(),
32+
"got wrong number of notifications"
33+
);
34+
let slice = unsafe { std::slice::from_raw_parts(array_ptr, res.try_into().unwrap()) };
35+
for (return_event, expected_event) in slice.iter().zip(expected_notifications.iter()) {
36+
let event = return_event.events;
37+
let data = return_event.u64;
38+
assert_eq!(event, expected_event.0, "got wrong events");
39+
assert_eq!(data, expected_event.1, "got wrong data");
40+
}
41+
}
42+
43+
// This test shows a data_race before epoll had vector clocks added.
44+
fn test_epoll_race() {
45+
// Create an epoll instance.
46+
let epfd = unsafe { libc::epoll_create1(0) };
47+
assert_ne!(epfd, -1);
48+
49+
// Create an eventfd instance.
50+
let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC;
51+
let fd = unsafe { libc::eventfd(0, flags) };
52+
53+
// Register eventfd with the epoll instance.
54+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: fd as u64 };
55+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fd, &mut ev) };
56+
assert_eq!(res, 0);
57+
58+
static mut VAL: u8 = 0;
59+
let thread1 = thread::spawn(move || {
60+
// Write to the static mut variable.
61+
unsafe { VAL = 1 };
62+
// Write to the eventfd instance.
63+
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes();
64+
let res = unsafe { libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8) };
65+
// read returns number of bytes that have been read, which is always 8.
66+
assert_eq!(res, 8);
67+
});
68+
thread::yield_now();
69+
// epoll_wait for the event to happen.
70+
let expected_event = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap();
71+
let expected_value = u64::try_from(fd).unwrap();
72+
check_epoll_wait::<8>(epfd, &[(expected_event, expected_value)], -1);
73+
// Read from the static mut variable.
74+
#[allow(static_mut_refs)]
75+
unsafe {
76+
assert_eq!(VAL, 1) //~ ERROR: Data race detected
77+
};
78+
thread1.join().unwrap();
79+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: Undefined Behavior: Data race detected between (1) non-atomic write on thread `unnamed-ID` and (2) retag read of type `u8` on thread `main` at ALLOC. (2) just happened here
2+
--> tests/fail-dep/libc/libc-epoll-blocking.rs:LL:CC
3+
|
4+
LL | assert_eq!(VAL, 1)
5+
| ^^^^^^^^^^^^^^^^^^ Data race detected between (1) non-atomic write on thread `unnamed-ID` and (2) retag read of type `u8` on thread `main` at ALLOC. (2) just happened here
6+
|
7+
help: and (1) occurred earlier here
8+
--> tests/fail-dep/libc/libc-epoll-blocking.rs:LL:CC
9+
|
10+
LL | unsafe { VAL = 1 };
11+
| ^^^^^^^
12+
= help: retags occur on all (re)borrows and as well as when references are copied or moved
13+
= help: retags permit optimizations that insert speculative reads or writes
14+
= help: therefore from the perspective of data races, a retag has the same implications as a read or write
15+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
16+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
17+
= note: BACKTRACE (of the first span):
18+
= note: inside `test_epoll_race` at RUSTLIB/core/src/macros/mod.rs:LL:CC
19+
note: inside `main`
20+
--> tests/fail-dep/libc/libc-epoll-blocking.rs:LL:CC
21+
|
22+
LL | test_epoll_race();
23+
| ^^^^^^^^^^^^^^^^^
24+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
26+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
27+
28+
error: aborting due to 1 previous error
29+

0 commit comments

Comments
 (0)