Skip to content

Commit 5cdee07

Browse files
committed
Auto merge of rust-lang#3946 - FrankReh:fix-over-synchronization-of-epoll, r=RalfJung
Fix over synchronization of epoll Fixes rust-lang#3944. The clock used by epoll is now per event generated, rather than by the `epoll's` ready_list. The same epoll tests that existed before are unchanged and still pass. Also the `tokio` test case we had worked on last week still passes with this change. This change does beg the question of how the epoll event states should change. Perhaps rather than expose public crate bool fields, so setters should be provided that include a clock parameter or an optional clock parameter. Also should all the epoll event possibilities have their clock sync tested the way these commit lay out testing. In this first go around, only the pipe's EPOLLIN is tested. The EPOLLOUT might deserve testing too, as would the eventfd. Any future source of epoll events would also fit into that category.
2 parents 8e8dd57 + 4560606 commit 5cdee07

File tree

3 files changed

+128
-11
lines changed

3 files changed

+128
-11
lines changed

src/tools/miri/src/shims/unix/linux/epoll.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ pub struct EpollEventInstance {
3232
events: u32,
3333
/// Original data retrieved from `epoll_event` during `epoll_ctl`.
3434
data: u64,
35+
/// The release clock associated with this event.
36+
clock: VClock,
3537
}
3638

3739
impl EpollEventInstance {
3840
pub fn new(events: u32, data: u64) -> EpollEventInstance {
39-
EpollEventInstance { events, data }
41+
EpollEventInstance { events, data, clock: Default::default() }
4042
}
4143
}
4244

@@ -92,7 +94,6 @@ pub struct EpollReadyEvents {
9294
#[derive(Debug, Default)]
9395
struct ReadyList {
9496
mapping: RefCell<BTreeMap<(FdId, i32), EpollEventInstance>>,
95-
clock: RefCell<VClock>,
9697
}
9798

9899
impl EpollReadyEvents {
@@ -567,11 +568,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
567568

568569
let epoll = epfd.downcast::<Epoll>().unwrap();
569570

570-
// Synchronize running thread to the epoll ready list.
571-
this.release_clock(|clock| {
572-
epoll.ready_list.clock.borrow_mut().join(clock);
573-
});
574-
575571
if let Some(thread_id) = epoll.thread_id.borrow_mut().pop() {
576572
waiter.push(thread_id);
577573
};
@@ -627,7 +623,11 @@ fn check_and_update_one_event_interest<'tcx>(
627623
if flags != 0 {
628624
let epoll_key = (id, epoll_event_interest.fd_num);
629625
let ready_list = &mut epoll_event_interest.ready_list.mapping.borrow_mut();
630-
let event_instance = EpollEventInstance::new(flags, epoll_event_interest.data);
626+
let mut event_instance = EpollEventInstance::new(flags, epoll_event_interest.data);
627+
// If we are tracking data races, remember the current clock so we can sync with it later.
628+
ecx.release_clock(|clock| {
629+
event_instance.clock.join(clock);
630+
});
631631
// Triggers the notification by inserting it to the ready list.
632632
ready_list.insert(epoll_key, event_instance);
633633
interp_ok(true)
@@ -654,9 +654,6 @@ fn blocking_epoll_callback<'tcx>(
654654

655655
let ready_list = epoll_file_description.get_ready_list();
656656

657-
// Synchronize waking thread from the epoll ready list.
658-
ecx.acquire_clock(&ready_list.clock.borrow());
659-
660657
let mut ready_list = ready_list.mapping.borrow_mut();
661658
let mut num_of_events: i32 = 0;
662659
let mut array_iter = ecx.project_array_fields(events)?;
@@ -670,6 +667,9 @@ fn blocking_epoll_callback<'tcx>(
670667
],
671668
&des.1,
672669
)?;
670+
// Synchronize waking thread with the event of interest.
671+
ecx.acquire_clock(&epoll_event_instance.clock);
672+
673673
num_of_events = num_of_events.strict_add(1);
674674
} else {
675675
break;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//! This ensures that when an epoll_wait wakes up and there are multiple events,
2+
//! and we only read one of them, we do not synchronize with the other events
3+
//! and therefore still report a data race for things that need to see the second event
4+
//! to be considered synchronized.
5+
//@only-target: linux
6+
// ensure deterministic schedule
7+
//@compile-flags: -Zmiri-preemption-rate=0
8+
9+
use std::convert::TryInto;
10+
use std::thread;
11+
use std::thread::spawn;
12+
13+
#[track_caller]
14+
fn check_epoll_wait<const N: usize>(epfd: i32, expected_notifications: &[(u32, u64)]) {
15+
let epoll_event = libc::epoll_event { events: 0, u64: 0 };
16+
let mut array: [libc::epoll_event; N] = [epoll_event; N];
17+
let maxsize = N;
18+
let array_ptr = array.as_mut_ptr();
19+
let res = unsafe { libc::epoll_wait(epfd, array_ptr, maxsize.try_into().unwrap(), 0) };
20+
if res < 0 {
21+
panic!("epoll_wait failed: {}", std::io::Error::last_os_error());
22+
}
23+
assert_eq!(
24+
res,
25+
expected_notifications.len().try_into().unwrap(),
26+
"got wrong number of notifications"
27+
);
28+
let slice = unsafe { std::slice::from_raw_parts(array_ptr, res.try_into().unwrap()) };
29+
for (return_event, expected_event) in slice.iter().zip(expected_notifications.iter()) {
30+
let event = return_event.events;
31+
let data = return_event.u64;
32+
assert_eq!(event, expected_event.0, "got wrong events");
33+
assert_eq!(data, expected_event.1, "got wrong data");
34+
}
35+
}
36+
37+
fn main() {
38+
// Create an epoll instance.
39+
let epfd = unsafe { libc::epoll_create1(0) };
40+
assert_ne!(epfd, -1);
41+
42+
// Create two socketpair instances.
43+
let mut fds_a = [-1, -1];
44+
let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds_a.as_mut_ptr()) };
45+
assert_eq!(res, 0);
46+
47+
let mut fds_b = [-1, -1];
48+
let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds_b.as_mut_ptr()) };
49+
assert_eq!(res, 0);
50+
51+
// Register both pipe read ends.
52+
let mut ev = libc::epoll_event {
53+
events: (libc::EPOLLIN | libc::EPOLLET) as _,
54+
u64: u64::try_from(fds_a[1]).unwrap(),
55+
};
56+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds_a[1], &mut ev) };
57+
assert_eq!(res, 0);
58+
59+
let mut ev = libc::epoll_event {
60+
events: (libc::EPOLLIN | libc::EPOLLET) as _,
61+
u64: u64::try_from(fds_b[1]).unwrap(),
62+
};
63+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds_b[1], &mut ev) };
64+
assert_eq!(res, 0);
65+
66+
static mut VAL_ONE: u8 = 40; // This one will be read soundly.
67+
static mut VAL_TWO: u8 = 50; // This one will be read unsoundly.
68+
let thread1 = spawn(move || {
69+
unsafe { VAL_ONE = 41 };
70+
71+
let data = "abcde".as_bytes().as_ptr();
72+
let res = unsafe { libc::write(fds_a[0], data as *const libc::c_void, 5) };
73+
assert_eq!(res, 5);
74+
75+
unsafe { VAL_TWO = 51 };
76+
77+
let res = unsafe { libc::write(fds_b[0], data as *const libc::c_void, 5) };
78+
assert_eq!(res, 5);
79+
});
80+
thread::yield_now();
81+
82+
// With room for one event: check result from epoll_wait.
83+
let expected_event = u32::try_from(libc::EPOLLIN).unwrap();
84+
let expected_value = u64::try_from(fds_a[1]).unwrap();
85+
check_epoll_wait::<1>(epfd, &[(expected_event, expected_value)]);
86+
87+
// Since we only received one event, we have synchronized with
88+
// the write to VAL_ONE but not with the one to VAL_TWO.
89+
unsafe {
90+
assert_eq!({ VAL_ONE }, 41) // This one is not UB
91+
};
92+
unsafe {
93+
assert_eq!({ VAL_TWO }, 51) //~ERROR: Data race detected
94+
};
95+
96+
thread1.join().unwrap();
97+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: Data race detected between (1) non-atomic write on thread `unnamed-ID` and (2) non-atomic read on thread `main` at ALLOC. (2) just happened here
2+
--> tests/fail-dep/libc/libc-epoll-data-race.rs:LL:CC
3+
|
4+
LL | assert_eq!({ VAL_TWO }, 51)
5+
| ^^^^^^^ Data race detected between (1) non-atomic write on thread `unnamed-ID` and (2) non-atomic read on thread `main` at ALLOC. (2) just happened here
6+
|
7+
help: and (1) occurred earlier here
8+
--> tests/fail-dep/libc/libc-epoll-data-race.rs:LL:CC
9+
|
10+
LL | unsafe { VAL_TWO = 51 };
11+
| ^^^^^^^^^^^^
12+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
13+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
14+
= note: BACKTRACE (of the first span):
15+
= note: inside `main` at tests/fail-dep/libc/libc-epoll-data-race.rs:LL:CC
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to 1 previous error
20+

0 commit comments

Comments
 (0)