Skip to content

Commit 6702f15

Browse files
committed
epoll: iterate through output buffer then fetch an event from ready list
1 parent aea3cfd commit 6702f15

File tree

2 files changed

+94
-17
lines changed

2 files changed

+94
-17
lines changed

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

+30-17
Original file line numberDiff line numberDiff line change
@@ -436,23 +436,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
436436
let mut num_of_events: i32 = 0;
437437
let mut array_iter = this.project_array_fields(&events)?;
438438

439-
while let Some((epoll_key, epoll_return)) = ready_list.pop_first() {
440-
// If the file description is fully close, the entry for corresponding FdID in the
441-
// global epoll event interest table would be empty.
442-
if this.machine.epoll_interests.get_epoll_interest(epoll_key.0).is_some() {
443-
// Return notification to the caller if the file description is not fully closed.
444-
if let Some(des) = array_iter.next(this)? {
445-
this.write_int_fields_named(
446-
&[
447-
("events", epoll_return.events.into()),
448-
("u64", epoll_return.data.into()),
449-
],
450-
&des.1,
451-
)?;
452-
num_of_events = num_of_events.checked_add(1).unwrap();
453-
} else {
454-
break;
455-
}
439+
while let Some(des) = array_iter.next(this)? {
440+
if let Some(epoll_event_instance) = ready_list_next(this, &mut ready_list) {
441+
this.write_int_fields_named(
442+
&[
443+
("events", epoll_event_instance.events.into()),
444+
("u64", epoll_event_instance.data.into()),
445+
],
446+
&des.1,
447+
)?;
448+
num_of_events = num_of_events.strict_add(1);
449+
} else {
450+
break;
456451
}
457452
}
458453
Ok(Scalar::from_i32(num_of_events))
@@ -495,3 +490,21 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
495490
Ok(())
496491
}
497492
}
493+
494+
/// This function takes in ready list and returns EpollEventInstance with file description
495+
/// that is not closed.
496+
fn ready_list_next(
497+
ecx: &MiriInterpCx<'_>,
498+
ready_list: &mut BTreeMap<(FdId, i32), EpollEventInstance>,
499+
) -> Option<EpollEventInstance> {
500+
while let Some((epoll_key, epoll_event_instance)) = ready_list.pop_first() {
501+
// This ensures that we only return events that we are interested. The FD might have been closed since
502+
// the event was generated, in which case we are not interested anymore.
503+
// When a file description is fully closed, it gets removed from `machine.epoll_interests`,
504+
// so we skip events whose FD is not in that map anymore.
505+
if ecx.machine.epoll_interests.get_epoll_interest(epoll_key.0).is_some() {
506+
return Some(epoll_event_instance);
507+
}
508+
}
509+
return None;
510+
}

src/tools/miri/tests/pass-dep/libc/libc-epoll.rs

+64
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ fn main() {
2020
test_pointer();
2121
test_two_same_fd_in_same_epoll_instance();
2222
test_epoll_wait_maxevent_zero();
23+
test_epoll_lost_events();
24+
test_ready_list_fetching_logic();
2325
}
2426

2527
// Using `as` cast since `EPOLLET` wraps around
@@ -542,3 +544,65 @@ fn test_epoll_wait_maxevent_zero() {
542544
assert_eq!(e.raw_os_error(), Some(libc::EINVAL));
543545
assert_eq!(res, -1);
544546
}
547+
548+
// This is a test for https://github.com/rust-lang/miri/issues/3812,
549+
// epoll can lose events if they don't fit in the output buffer.
550+
fn test_epoll_lost_events() {
551+
// Create an epoll instance.
552+
let epfd = unsafe { libc::epoll_create1(0) };
553+
assert_ne!(epfd, -1);
554+
555+
// Create a socketpair instance.
556+
let mut fds = [-1, -1];
557+
let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
558+
assert_eq!(res, 0);
559+
560+
// Register both fd to the same epoll instance.
561+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: fds[0] as u64 };
562+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[0], &mut ev) };
563+
assert_eq!(res, 0);
564+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: fds[1] as u64 };
565+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fds[1], &mut ev) };
566+
assert_eq!(res, 0);
567+
568+
//Two notification should be received. But we only provide buffer for one event.
569+
let expected_event0 = u32::try_from(libc::EPOLLOUT).unwrap();
570+
let expected_value0 = fds[0] as u64;
571+
check_epoll_wait::<1>(epfd, &[(expected_event0, expected_value0)]);
572+
573+
// Previous event should be returned for the second epoll_wait.
574+
let expected_event1 = u32::try_from(libc::EPOLLOUT).unwrap();
575+
let expected_value1 = fds[1] as u64;
576+
check_epoll_wait::<1>(epfd, &[(expected_event1, expected_value1)]);
577+
}
578+
579+
// This is testing if closing an fd that is already in ready list will cause an empty entry in
580+
// returned notification.
581+
// Related discussion in https://github.com/rust-lang/miri/pull/3818#discussion_r1720679440.
582+
fn test_ready_list_fetching_logic() {
583+
// Create an epoll instance.
584+
let epfd = unsafe { libc::epoll_create1(0) };
585+
assert_ne!(epfd, -1);
586+
587+
// Create two eventfd instances.
588+
let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC;
589+
let fd0 = unsafe { libc::eventfd(0, flags) };
590+
let fd1 = unsafe { libc::eventfd(0, flags) };
591+
592+
// Register both fd to the same epoll instance. At this point, both of them are on the ready list.
593+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: fd0 as u64 };
594+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fd0, &mut ev) };
595+
assert_eq!(res, 0);
596+
let mut ev = libc::epoll_event { events: EPOLL_IN_OUT_ET, u64: fd1 as u64 };
597+
let res = unsafe { libc::epoll_ctl(epfd, libc::EPOLL_CTL_ADD, fd1, &mut ev) };
598+
assert_eq!(res, 0);
599+
600+
// Close fd0 so the first entry in the ready list will be empty.
601+
let res = unsafe { libc::close(fd0) };
602+
assert_eq!(res, 0);
603+
604+
// Notification for fd1 should be returned.
605+
let expected_event1 = u32::try_from(libc::EPOLLOUT).unwrap();
606+
let expected_value1 = fd1 as u64;
607+
check_epoll_wait::<1>(epfd, &[(expected_event1, expected_value1)]);
608+
}

0 commit comments

Comments
 (0)