Skip to main content

miri/shims/unix/linux_like/
epoll.rs

1use std::cell::RefCell;
2use std::collections::{BTreeMap, BTreeSet, VecDeque};
3use std::io;
4use std::ops::Bound;
5use std::time::Duration;
6
7use rustc_abi::FieldIdx;
8
9use crate::concurrency::VClock;
10use crate::shims::files::{
11    DynFileDescriptionRef, FdId, FdNum, FileDescription, FileDescriptionRef, WeakFileDescriptionRef,
12};
13use crate::shims::unix::UnixFileDescription;
14use crate::*;
15
16type EpollEventKey = (FdId, FdNum);
17
18/// An `Epoll` file descriptor connects file handles and epoll events
19#[derive(Debug, Default)]
20pub struct Epoll {
21    /// A map of EpollEventInterests registered under this epoll instance. Each entry is
22    /// differentiated using FdId and file descriptor value.
23    interest_list: RefCell<BTreeMap<EpollEventKey, EpollEventInterest>>,
24    /// The subset of interests that is currently considered "ready". Stored separately so we
25    /// can access it more efficiently.
26    ready_set: RefCell<BTreeSet<EpollEventKey>>,
27    /// The queue of threads blocked on this epoll instance.
28    queue: RefCell<VecDeque<ThreadId>>,
29}
30
31impl VisitProvenance for Epoll {
32    fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
33        // No provenance anywhere in this type.
34    }
35}
36
37/// Returns the range of all EpollEventKey for the given FD ID.
38fn range_for_id(id: FdId) -> std::ops::RangeInclusive<EpollEventKey> {
39    (id, 0)..=(id, i32::MAX)
40}
41
42/// Tracks the events that this epoll is interested in for a given file descriptor.
43#[derive(Debug)]
44pub struct EpollEventInterest {
45    /// The events bitmask the epoll is interested in.
46    relevant_events: u32,
47    /// The currently active events for this file descriptor.
48    active_events: u32,
49    /// The vector clock for wakeups.
50    clock: VClock,
51    /// User-defined data associated with this interest.
52    /// libc's data field in epoll_event can store integer or pointer,
53    /// but only u64 is supported for now.
54    /// <https://man7.org/linux/man-pages/man3/epoll_event.3type.html>
55    data: u64,
56}
57
58/// Struct reflecting the readiness of a file description.
59#[derive(Debug)]
60pub struct EpollReadiness {
61    /// The associated file is available for read(2) operations, in the sense that a read will not block.
62    /// (I.e., returning EOF is considered "ready".)
63    pub epollin: bool,
64    /// The associated file is available for write(2) operations, in the sense that a write will not block.
65    pub epollout: bool,
66    /// Stream socket peer closed connection, or shut down writing
67    /// half of connection.
68    pub epollrdhup: bool,
69    /// For stream socket, this event merely indicates that the peer
70    /// closed its end of the channel.
71    /// Unlike epollrdhup, this should only be set when the stream is fully closed.
72    /// epollrdhup also gets set when only the write half is closed, which is possible
73    /// via `shutdown(_, SHUT_WR)`.
74    pub epollhup: bool,
75    /// Error condition happened on the associated file descriptor.
76    pub epollerr: bool,
77}
78
79impl EpollReadiness {
80    pub fn empty() -> Self {
81        EpollReadiness {
82            epollin: false,
83            epollout: false,
84            epollrdhup: false,
85            epollhup: false,
86            epollerr: false,
87        }
88    }
89
90    pub fn get_event_bitmask<'tcx>(&self, ecx: &MiriInterpCx<'tcx>) -> u32 {
91        let epollin = ecx.eval_libc_u32("EPOLLIN");
92        let epollout = ecx.eval_libc_u32("EPOLLOUT");
93        let epollrdhup = ecx.eval_libc_u32("EPOLLRDHUP");
94        let epollhup = ecx.eval_libc_u32("EPOLLHUP");
95        let epollerr = ecx.eval_libc_u32("EPOLLERR");
96
97        let mut bitmask = 0;
98        if self.epollin {
99            bitmask |= epollin;
100        }
101        if self.epollout {
102            bitmask |= epollout;
103        }
104        if self.epollrdhup {
105            bitmask |= epollrdhup;
106        }
107        if self.epollhup {
108            bitmask |= epollhup;
109        }
110        if self.epollerr {
111            bitmask |= epollerr;
112        }
113        bitmask
114    }
115}
116
117// Best-effort mapping from cross platform readiness to epoll readiness.
118impl From<&BlockingIoSourceReadiness> for EpollReadiness {
119    fn from(readiness: &BlockingIoSourceReadiness) -> Self {
120        Self {
121            epollin: readiness.readable,
122            epollout: readiness.writable,
123            epollrdhup: readiness.read_closed,
124            epollhup: readiness.write_closed,
125            epollerr: readiness.error,
126        }
127    }
128}
129
130impl FileDescription for Epoll {
131    fn name(&self) -> &'static str {
132        "epoll"
133    }
134
135    fn metadata<'tcx>(
136        &self,
137    ) -> InterpResult<'tcx, Either<io::Result<std::fs::Metadata>, &'static str>> {
138        // On Linux, epoll is an "anonymous inode" reported as S_IFREG.
139        interp_ok(Either::Right("S_IFREG"))
140    }
141
142    fn destroy<'tcx>(
143        mut self,
144        self_id: FdId,
145        _communicate_allowed: bool,
146        ecx: &mut MiriInterpCx<'tcx>,
147    ) -> InterpResult<'tcx, io::Result<()>> {
148        // If we were interested in some FDs, we can remove that now.
149        let mut ids = self.interest_list.get_mut().keys().map(|(id, _num)| *id).collect::<Vec<_>>();
150        ids.dedup(); // they come out of the map sorted
151        for id in ids {
152            ecx.machine.epoll_interests.remove(id, self_id);
153        }
154        interp_ok(Ok(()))
155    }
156
157    fn as_unix<'tcx>(&self, _ecx: &MiriInterpCx<'tcx>) -> &dyn UnixFileDescription {
158        self
159    }
160}
161
162impl UnixFileDescription for Epoll {}
163
164/// The table of all EpollEventInterest.
165/// This tracks, for each file description, which epoll instances have an interest in events
166/// for this file description. The `FdId` is the ID of the epoll instance, so that we can recognize
167/// it later when it is slated for removal. The vector is sorted by that ID.
168pub struct EpollInterestTable(BTreeMap<FdId, Vec<(FdId, WeakFileDescriptionRef<Epoll>)>>);
169
170impl EpollInterestTable {
171    pub(crate) fn new() -> Self {
172        EpollInterestTable(BTreeMap::new())
173    }
174
175    fn insert(&mut self, id: FdId, epoll: &FileDescriptionRef<Epoll>) {
176        let epolls = self.0.entry(id).or_default();
177        let idx = epolls
178            .binary_search_by_key(&epoll.id(), |&(id, _)| id)
179            .expect_err("trying to add an epoll that's already in the list");
180        epolls.insert(idx, (epoll.id(), FileDescriptionRef::downgrade(epoll)));
181    }
182
183    fn remove(&mut self, id: FdId, epoll_id: FdId) {
184        let epolls = self.0.entry(id).or_default();
185        let idx = epolls
186            .binary_search_by_key(&epoll_id, |&(id, _)| id)
187            .expect("trying to remove an epoll that's not in the list");
188        epolls.remove(idx);
189    }
190
191    fn get_epolls(&self, id: FdId) -> Option<impl Iterator<Item = &WeakFileDescriptionRef<Epoll>>> {
192        self.0.get(&id).map(|epolls| epolls.iter().map(|(_id, epoll)| epoll))
193    }
194
195    pub fn remove_epolls(&mut self, id: FdId) {
196        if let Some(epolls) = self.0.remove(&id) {
197            for epoll in epolls.iter().filter_map(|(_id, epoll)| epoll.upgrade()) {
198                // This is a still-live epoll with interest in this FD. Remove all
199                // relevant interests (including from the ready set).
200                epoll
201                    .interest_list
202                    .borrow_mut()
203                    .extract_if(range_for_id(id), |_, _| true)
204                    // Consume the iterator.
205                    .for_each(drop);
206                epoll
207                    .ready_set
208                    .borrow_mut()
209                    .extract_if(range_for_id(id), |_| true)
210                    // Consume the iterator.
211                    .for_each(drop);
212            }
213        }
214    }
215}
216
217impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
218pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
219    /// This function returns a file descriptor referring to the new `Epoll` instance. This file
220    /// descriptor is used for all subsequent calls to the epoll interface. If the `flags` argument
221    /// is 0, then this function is the same as `epoll_create()`.
222    ///
223    /// <https://linux.die.net/man/2/epoll_create1>
224    fn epoll_create1(&mut self, flags: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
225        let this = self.eval_context_mut();
226
227        let flags = this.read_scalar(flags)?.to_i32()?;
228
229        let epoll_cloexec = this.eval_libc_i32("EPOLL_CLOEXEC");
230
231        // Miri does not support exec, so EPOLL_CLOEXEC flag has no effect.
232        if flags != epoll_cloexec && flags != 0 {
233            throw_unsup_format!(
234                "epoll_create1: flag {:#x} is unsupported, only 0 or EPOLL_CLOEXEC are allowed",
235                flags
236            );
237        }
238
239        let fd = this.machine.fds.insert_new(Epoll::default());
240        interp_ok(Scalar::from_i32(fd))
241    }
242
243    /// This function performs control operations on the `Epoll` instance referred to by the file
244    /// descriptor `epfd`. It requests that the operation `op` be performed for the target file
245    /// descriptor, `fd`.
246    ///
247    /// Valid values for the op argument are:
248    /// `EPOLL_CTL_ADD` - Register the target file descriptor `fd` on the `Epoll` instance referred
249    /// to by the file descriptor `epfd` and associate the event `event` with the internal file
250    /// linked to `fd`.
251    /// `EPOLL_CTL_MOD` - Change the event `event` associated with the target file descriptor `fd`.
252    /// `EPOLL_CTL_DEL` - Deregister the target file descriptor `fd` from the `Epoll` instance
253    /// referred to by `epfd`. The `event` is ignored and can be null.
254    ///
255    /// <https://linux.die.net/man/2/epoll_ctl>
256    fn epoll_ctl(
257        &mut self,
258        epfd: &OpTy<'tcx>,
259        op: &OpTy<'tcx>,
260        fd: &OpTy<'tcx>,
261        event: &OpTy<'tcx>,
262    ) -> InterpResult<'tcx, Scalar> {
263        let this = self.eval_context_mut();
264
265        let epfd_value = this.read_scalar(epfd)?.to_i32()?;
266        let op = this.read_scalar(op)?.to_i32()?;
267        let fd = this.read_scalar(fd)?.to_i32()?;
268        let event = this.deref_pointer_as(event, this.libc_ty_layout("epoll_event"))?;
269
270        let epoll_ctl_add = this.eval_libc_i32("EPOLL_CTL_ADD");
271        let epoll_ctl_mod = this.eval_libc_i32("EPOLL_CTL_MOD");
272        let epoll_ctl_del = this.eval_libc_i32("EPOLL_CTL_DEL");
273        let epollin = this.eval_libc_u32("EPOLLIN");
274        let epollout = this.eval_libc_u32("EPOLLOUT");
275        let epollrdhup = this.eval_libc_u32("EPOLLRDHUP");
276        let epollet = this.eval_libc_u32("EPOLLET");
277        let epollhup = this.eval_libc_u32("EPOLLHUP");
278        let epollerr = this.eval_libc_u32("EPOLLERR");
279
280        // Throw EFAULT if epfd and fd have the same value.
281        if epfd_value == fd {
282            return this.set_last_error_and_return_i32(LibcError("EFAULT"));
283        }
284
285        // Check if epfd is a valid epoll file descriptor.
286        let Some(epfd) = this.machine.fds.get(epfd_value) else {
287            return this.set_last_error_and_return_i32(LibcError("EBADF"));
288        };
289        let epfd = epfd
290            .downcast::<Epoll>()
291            .ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_ctl`"))?;
292
293        let mut interest_list = epfd.interest_list.borrow_mut();
294
295        let Some(fd_ref) = this.machine.fds.get(fd) else {
296            return this.set_last_error_and_return_i32(LibcError("EBADF"));
297        };
298        let id = fd_ref.id();
299
300        if op == epoll_ctl_add || op == epoll_ctl_mod {
301            // Read event bitmask and data from epoll_event passed by caller.
302            let mut events =
303                this.read_scalar(&this.project_field(&event, FieldIdx::ZERO)?)?.to_u32()?;
304            let data = this.read_scalar(&this.project_field(&event, FieldIdx::ONE)?)?.to_u64()?;
305
306            // Unset the flag we support to discover if any unsupported flags are used.
307            let mut flags = events;
308            // epoll_wait(2) will always wait for epollhup and epollerr; it is not
309            // necessary to set it in events when calling epoll_ctl().
310            // So we will always set these two event types.
311            events |= epollhup;
312            events |= epollerr;
313
314            if events & epollet != epollet {
315                // We only support edge-triggered notification for now.
316                throw_unsup_format!("epoll_ctl: epollet flag must be included.");
317            } else {
318                flags &= !epollet;
319            }
320            if flags & epollin == epollin {
321                flags &= !epollin;
322            }
323            if flags & epollout == epollout {
324                flags &= !epollout;
325            }
326            if flags & epollrdhup == epollrdhup {
327                flags &= !epollrdhup;
328            }
329            if flags & epollhup == epollhup {
330                flags &= !epollhup;
331            }
332            if flags & epollerr == epollerr {
333                flags &= !epollerr;
334            }
335            if flags != 0 {
336                throw_unsup_format!(
337                    "epoll_ctl: encountered unknown unsupported flags {:#x}",
338                    flags
339                );
340            }
341
342            // Add new interest to list. Experiments show that we need to reset all state
343            // on `EPOLL_CTL_MOD`, including the edge tracking.
344            let epoll_key = (id, fd);
345            if op == epoll_ctl_add {
346                if interest_list.range(range_for_id(id)).next().is_none() {
347                    // This is the first time this FD got added to this epoll.
348                    // Remember that in the global list so we get notified about FD events.
349                    this.machine.epoll_interests.insert(id, &epfd);
350                }
351                let new_interest = EpollEventInterest {
352                    relevant_events: events,
353                    data,
354                    active_events: 0,
355                    clock: VClock::default(),
356                };
357                if interest_list.try_insert(epoll_key, new_interest).is_err() {
358                    // We already had interest in this.
359                    return this.set_last_error_and_return_i32(LibcError("EEXIST"));
360                }
361            } else {
362                // Modify the existing interest.
363                let Some(interest) = interest_list.get_mut(&epoll_key) else {
364                    return this.set_last_error_and_return_i32(LibcError("ENOENT"));
365                };
366                interest.relevant_events = events;
367                interest.data = data;
368            }
369
370            let active_events = fd_ref.as_unix(this).epoll_active_events()?.get_event_bitmask(this);
371
372            // Deliver events for the new interest.
373            update_readiness(
374                this,
375                &epfd,
376                active_events,
377                /* force_edge */ true,
378                move |callback| {
379                    // Need to release the RefCell when this closure returns, so we have to move
380                    // it into the closure, so we have to do a re-lookup here.
381                    callback(epoll_key, interest_list.get_mut(&epoll_key).unwrap())
382                },
383            )?;
384
385            interp_ok(Scalar::from_i32(0))
386        } else if op == epoll_ctl_del {
387            let epoll_key = (id, fd);
388
389            // Remove epoll_event_interest from interest_list and ready_set.
390            if interest_list.remove(&epoll_key).is_none() {
391                // We did not have interest in this.
392                return this.set_last_error_and_return_i32(LibcError("ENOENT"));
393            };
394            epfd.ready_set.borrow_mut().remove(&epoll_key);
395            // If this was the last interest in this FD, remove us from the global list
396            // of who is interested in this FD.
397            if interest_list.range(range_for_id(id)).next().is_none() {
398                this.machine.epoll_interests.remove(id, epfd.id());
399            }
400
401            interp_ok(Scalar::from_i32(0))
402        } else {
403            throw_unsup_format!("unsupported epoll_ctl operation: {op}");
404        }
405    }
406
407    /// The `epoll_wait()` system call waits for events on the `Epoll`
408    /// instance referred to by the file descriptor `epfd`. The buffer
409    /// pointed to by `events` is used to return information from the ready
410    /// list about file descriptors in the interest list that have some
411    /// events available. Up to `maxevents` are returned by `epoll_wait()`.
412    /// The `maxevents` argument must be greater than zero.
413    ///
414    /// The `timeout` argument specifies the number of milliseconds that
415    /// `epoll_wait()` will block. Time is measured against the
416    /// CLOCK_MONOTONIC clock. If the timeout is zero, the function will not block,
417    /// while if the timeout is -1, the function will block
418    /// until at least one event has been retrieved (or an error
419    /// occurred).
420    ///
421    /// A call to `epoll_wait()` will block until either:
422    /// • a file descriptor delivers an event;
423    /// • the call is interrupted by a signal handler; or
424    /// • the timeout expires.
425    ///
426    /// Note that the timeout interval will be rounded up to the system
427    /// clock granularity, and kernel scheduling delays mean that the
428    /// blocking interval may overrun by a small amount. Specifying a
429    /// timeout of -1 causes `epoll_wait()` to block indefinitely, while
430    /// specifying a timeout equal to zero cause `epoll_wait()` to return
431    /// immediately, even if no events are available.
432    ///
433    /// On success, `epoll_wait()` returns the number of file descriptors
434    /// ready for the requested I/O, or zero if no file descriptor became
435    /// ready during the requested timeout milliseconds. On failure,
436    /// `epoll_wait()` returns -1 and errno is set to indicate the error.
437    ///
438    /// <https://man7.org/linux/man-pages/man2/epoll_wait.2.html>
439    fn epoll_wait(
440        &mut self,
441        epfd: &OpTy<'tcx>,
442        events_op: &OpTy<'tcx>,
443        maxevents: &OpTy<'tcx>,
444        timeout: &OpTy<'tcx>,
445        dest: &MPlaceTy<'tcx>,
446    ) -> InterpResult<'tcx> {
447        let this = self.eval_context_mut();
448
449        let epfd_value = this.read_scalar(epfd)?.to_i32()?;
450        let events = this.read_immediate(events_op)?;
451        let maxevents = this.read_scalar(maxevents)?.to_i32()?;
452        let timeout = this.read_scalar(timeout)?.to_i32()?;
453
454        if epfd_value <= 0 || maxevents <= 0 {
455            return this.set_last_error_and_return(LibcError("EINVAL"), dest);
456        }
457
458        // This needs to come after the maxevents value check, or else maxevents.try_into().unwrap()
459        // will fail.
460        let event = this.deref_pointer_as(
461            &events,
462            this.libc_array_ty_layout("epoll_event", maxevents.try_into().unwrap()),
463        )?;
464
465        let Some(epfd) = this.machine.fds.get(epfd_value) else {
466            return this.set_last_error_and_return(LibcError("EBADF"), dest);
467        };
468        let Some(epfd) = epfd.downcast::<Epoll>() else {
469            return this.set_last_error_and_return(LibcError("EBADF"), dest);
470        };
471
472        if timeout == 0 || !epfd.ready_set.borrow().is_empty() {
473            // If the timeout is 0 or there is a ready event, we can return immediately.
474            return_ready_list(&epfd, dest, &event, this)?;
475        } else {
476            // Blocking
477            let timeout = match timeout {
478                0.. => {
479                    let duration = Duration::from_millis(timeout.try_into().unwrap());
480                    Some((TimeoutClock::Monotonic, TimeoutAnchor::Relative, duration))
481                }
482                -1 => None,
483                ..-1 => {
484                    throw_unsup_format!(
485                        "epoll_wait: Only timeout values greater than or equal to -1 are supported."
486                    );
487                }
488            };
489            // Record this thread as blocked.
490            epfd.queue.borrow_mut().push_back(this.active_thread());
491            // And block it.
492            let dest = dest.clone();
493            // We keep a strong ref to the underlying `Epoll` to make sure it sticks around.
494            // This means there'll be a leak if we never wake up, but that anyway would imply
495            // a thread is permanently blocked so this is fine.
496            this.block_thread(
497                BlockReason::Epoll { epfd: epfd.clone() },
498                timeout,
499                callback!(
500                    @capture<'tcx> {
501                        epfd: FileDescriptionRef<Epoll>,
502                        dest: MPlaceTy<'tcx>,
503                        event: MPlaceTy<'tcx>,
504                    }
505                    |this, unblock: UnblockKind| {
506                        match unblock {
507                            UnblockKind::Ready => {
508                                let events = return_ready_list(&epfd, &dest, &event, this)?;
509                                assert!(events > 0, "we got woken up with no events to deliver");
510                                interp_ok(())
511                            },
512                            UnblockKind::TimedOut => {
513                                // Remove the current active thread_id from the blocked thread_id list.
514                                epfd
515                                    .queue.borrow_mut()
516                                    .retain(|&id| id != this.active_thread());
517                                this.write_int(0, &dest)?;
518                                interp_ok(())
519                            },
520                        }
521                    }
522                ),
523            );
524        }
525        interp_ok(())
526    }
527
528    /// For a specific file description, get its currently active events and send it to everyone who
529    /// registered interest in this FD. This function must be called whenever the result of
530    /// `epoll_active_events` might change.
531    ///
532    /// If `force_edge` is set, edge-triggered interests will be triggered even if the set of
533    /// ready events did not change. This can lead to spurious wakeups. Use with caution!
534    fn update_epoll_active_events(
535        &mut self,
536        fd_ref: DynFileDescriptionRef,
537        force_edge: bool,
538    ) -> InterpResult<'tcx> {
539        let this = self.eval_context_mut();
540        let id = fd_ref.id();
541        // Figure out who is interested in this. We need to clone this list since we can't prove
542        // that `send_active_events_to_interest` won't mutate it.
543        let Some(epolls) = this.machine.epoll_interests.get_epolls(id) else {
544            return interp_ok(());
545        };
546        let epolls = epolls
547            .map(|weak| {
548                weak.upgrade()
549                    .expect("someone forgot to remove the garbage from `machine.epoll_interests`")
550            })
551            .collect::<Vec<_>>();
552        let active_events = fd_ref.as_unix(this).epoll_active_events()?.get_event_bitmask(this);
553        for epoll in epolls {
554            update_readiness(this, &epoll, active_events, force_edge, |callback| {
555                for (&key, interest) in epoll.interest_list.borrow_mut().range_mut(range_for_id(id))
556                {
557                    callback(key, interest)?;
558                }
559                interp_ok(())
560            })?;
561        }
562
563        interp_ok(())
564    }
565
566    /// Recursively check whether the [`Epoll`] file description contains
567    /// interests which are host I/O source file descriptions.
568    fn has_epoll_host_interests(&self, epfd: &FileDescriptionRef<Epoll>) -> bool {
569        let this = self.eval_context_ref();
570        epfd.interest_list.borrow().iter().any(|((fd_id, _fd_num), _)| {
571            // By looking up whether the file description is currently registered,
572            // we get whether it's a host I/O source file description.
573            this.machine.blocking_io.contains_source(fd_id)
574        })
575    }
576}
577
578/// Call this when the interests denoted by `for_each_interest` have their active event set changed
579/// to `active_events`. The list is provided indirectly via the `for_each_interest` closure, which
580/// will call its argument closure for each relevant interest.
581///
582/// Any `RefCell` should be released by the time `for_each_interest` returns since we will then
583/// be waking up threads which might require access to those `RefCell`.
584fn update_readiness<'tcx>(
585    ecx: &mut MiriInterpCx<'tcx>,
586    epoll: &FileDescriptionRef<Epoll>,
587    active_events: u32,
588    force_edge: bool,
589    for_each_interest: impl FnOnce(
590        &mut dyn FnMut(EpollEventKey, &mut EpollEventInterest) -> InterpResult<'tcx>,
591    ) -> InterpResult<'tcx>,
592) -> InterpResult<'tcx> {
593    let mut ready_set = epoll.ready_set.borrow_mut();
594    for_each_interest(&mut |key, interest| {
595        // Update the ready events tracked in this interest.
596        let new_readiness = interest.relevant_events & active_events;
597        let prev_readiness = std::mem::replace(&mut interest.active_events, new_readiness);
598        if new_readiness == 0 {
599            // Un-trigger this, there's nothing left to report here.
600            ready_set.remove(&key);
601        } else if force_edge || new_readiness != prev_readiness & new_readiness {
602            // Either we force an "edge" to be detected, or there's a bit set in `new`
603            // that was not set in `prev`. In both cases, this is ready now.
604            ready_set.insert(key);
605            ecx.release_clock(|clock| {
606                interest.clock.join(clock);
607            })?;
608        }
609        interp_ok(())
610    })?;
611    // While there are events ready to be delivered, wake up a thread to receive them.
612    while !ready_set.is_empty()
613        && let Some(thread_id) = epoll.queue.borrow_mut().pop_front()
614    {
615        drop(ready_set); // release the "lock" so the unblocked thread can have it
616        ecx.unblock_thread(thread_id, BlockReason::Epoll { epfd: epoll.clone() })?;
617        ready_set = epoll.ready_set.borrow_mut();
618    }
619
620    interp_ok(())
621}
622
623/// Stores the ready list of the `epfd` epoll instance into `events` (which must be an array),
624/// and the number of returned events into `dest`.
625fn return_ready_list<'tcx>(
626    epfd: &FileDescriptionRef<Epoll>,
627    dest: &MPlaceTy<'tcx>,
628    events: &MPlaceTy<'tcx>,
629    ecx: &mut MiriInterpCx<'tcx>,
630) -> InterpResult<'tcx, i32> {
631    let mut interest_list = epfd.interest_list.borrow_mut();
632    let mut ready_set = epfd.ready_set.borrow_mut();
633    let mut num_of_events: i32 = 0;
634    let mut array_iter = ecx.project_array_fields(events)?;
635
636    // Sanity-check to ensure that all event info is up-to-date.
637    if cfg!(debug_assertions) {
638        for (key, interest) in interest_list.iter() {
639            // Ensure this matches the latest readiness of this FD.
640            // We have to do an FD lookup by ID for this. The FdNum might be already closed.
641            let fd = ecx.machine.fds.fds.values().find(|fd| fd.id() == key.0).unwrap();
642            let current_active = fd.as_unix(ecx).epoll_active_events()?.get_event_bitmask(ecx);
643            assert_eq!(interest.active_events, current_active & interest.relevant_events);
644        }
645    }
646
647    // While there is a slot to store another event, and an event to store, deliver that event.
648    // We can't use an iterator over `ready_set` as we want to remove elements as we go,
649    // so we track the most recently delivered event to find the next one. We track it as a lower
650    // bound that we can pass to `BTreeSet::range`.
651    let mut event_lower_bound = Bound::Unbounded;
652    while let Some(slot) = array_iter.next(ecx)?
653        && let Some(&key) = ready_set.range((event_lower_bound, Bound::Unbounded)).next()
654    {
655        let interest = interest_list.get_mut(&key).expect("non-existent event in ready set");
656        // Deliver event to caller.
657        ecx.write_int_fields_named(
658            &[("events", interest.active_events.into()), ("u64", interest.data.into())],
659            &slot.1,
660        )?;
661        num_of_events = num_of_events.strict_add(1);
662        // Synchronize receiving thread with the event of interest.
663        ecx.acquire_clock(&interest.clock)?;
664        // This was an edge-triggered event, so remove it from the ready set.
665        ready_set.remove(&key);
666        // Go find the next event.
667        event_lower_bound = Bound::Excluded(key);
668    }
669    ecx.write_int(num_of_events, dest)?;
670    interp_ok(num_of_events)
671}