Skip to content

Commit

Permalink
an EpollEventInterest does not need to ref both its FD and its ready …
Browse files Browse the repository at this point in the history
…list
  • Loading branch information
RalfJung committed Dec 29, 2024
1 parent 2e1f587 commit 30059e1
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/shims/unix/linux_like/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ pub struct EpollEventInterest {
/// but only u64 is supported for now.
/// <https://man7.org/linux/man-pages/man3/epoll_event.3type.html>
data: u64,
/// Ready list of the epoll instance under which this EpollEventInterest is registered.
ready_list: Rc<ReadyList>,
/// The epoll file description that this EpollEventInterest is registered under.
/// This is weak to avoid cycles, but an upgrade is always guaranteed to succeed
/// because only the `Epoll` holds a strong ref to a `EpollEventInterest`.
weak_epfd: WeakFileDescriptionRef<Epoll>,
}

Expand Down Expand Up @@ -273,12 +273,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let Some(epfd) = this.machine.fds.get(epfd_value) else {
return this.set_last_error_and_return_i32(LibcError("EBADF"));
};
let epoll_file_description = epfd
let epfd = epfd
.downcast::<Epoll>()
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_ctl`"))?;

let mut interest_list = epoll_file_description.interest_list.borrow_mut();
let ready_list = &epoll_file_description.ready_list;
let mut interest_list = epfd.interest_list.borrow_mut();

let Some(fd_ref) = this.machine.fds.get(fd) else {
return this.set_last_error_and_return_i32(LibcError("EBADF"));
Expand Down Expand Up @@ -345,8 +344,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
fd_num: fd,
events,
data,
ready_list: Rc::clone(ready_list),
weak_epfd: FileDescriptionRef::downgrade(&epoll_file_description),
weak_epfd: FileDescriptionRef::downgrade(&epfd),
}));
// Notification will be returned for current epfd if there is event in the file
// descriptor we registered.
Expand Down Expand Up @@ -379,7 +377,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
drop(epoll_interest);

// Remove related epoll_interest from ready list.
ready_list.mapping.borrow_mut().remove(&epoll_key);
epfd.ready_list.mapping.borrow_mut().remove(&epoll_key);

// Remove dangling EpollEventInterest from its global table.
// .unwrap() below should succeed because the file description id must have registered
Expand Down Expand Up @@ -592,14 +590,15 @@ fn check_and_update_one_event_interest<'tcx>(
// Get the bitmask of ready events for a file description.
let ready_events_bitmask = fd_ref.as_unix().get_epoll_ready_events()?.get_event_bitmask(ecx);
let epoll_event_interest = interest.borrow();
let epfd = epoll_event_interest.weak_epfd.upgrade().unwrap();
// This checks if any of the events specified in epoll_event_interest.events
// match those in ready_events.
let flags = epoll_event_interest.events & ready_events_bitmask;
// If there is any event that we are interested in being specified as ready,
// insert an epoll_return to the ready list.
if flags != 0 {
let epoll_key = (id, epoll_event_interest.fd_num);
let ready_list = &mut epoll_event_interest.ready_list.mapping.borrow_mut();
let mut ready_list = epfd.ready_list.mapping.borrow_mut();
let mut event_instance = EpollEventInstance::new(flags, epoll_event_interest.data);
// If we are tracking data races, remember the current clock so we can sync with it later.
ecx.release_clock(|clock| {
Expand Down

0 comments on commit 30059e1

Please sign in to comment.