Skip to main content

miri/shims/
readiness.rs

1use std::assert_matches;
2use std::cell::RefCell;
3use std::collections::{BTreeMap, VecDeque};
4use std::rc::{Rc, Weak};
5
6use crate::concurrency::VClock;
7use crate::shims::files::{DynFileDescriptionRef, FdNum, WeakDynFileDescriptionRef};
8use crate::shims::*;
9use crate::*;
10
11/// Struct reflecting the readiness of a file description.
12#[derive(Debug, Copy, Clone, PartialEq)]
13pub struct Readiness {
14    /// Boolean whether the file description is readable.
15    pub readable: bool,
16    /// Boolean whether the file description is writable.
17    pub writable: bool,
18    /// Boolean whether the read end of the file description
19    /// is closed.
20    pub read_closed: bool,
21    /// Boolean whether the write end of the file description
22    /// is closed.
23    pub write_closed: bool,
24    /// Boolean whether the file description has an error.
25    pub error: bool,
26}
27
28impl std::ops::BitAnd for Readiness {
29    type Output = Readiness;
30
31    fn bitand(self, rhs: Readiness) -> Self::Output {
32        Readiness {
33            readable: self.readable && rhs.readable,
34            writable: self.writable && rhs.writable,
35            read_closed: self.read_closed && rhs.read_closed,
36            write_closed: self.write_closed && rhs.write_closed,
37            error: self.error && rhs.error,
38        }
39    }
40}
41
42impl std::ops::BitOr for Readiness {
43    type Output = Readiness;
44
45    fn bitor(self, rhs: Readiness) -> Self::Output {
46        Readiness {
47            readable: self.readable | rhs.readable,
48            writable: self.writable | rhs.writable,
49            read_closed: self.read_closed | rhs.read_closed,
50            write_closed: self.write_closed | rhs.write_closed,
51            error: self.error | rhs.error,
52        }
53    }
54}
55
56impl std::ops::BitOrAssign for Readiness {
57    fn bitor_assign(&mut self, rhs: Self) {
58        self.readable |= rhs.readable;
59        self.writable |= rhs.writable;
60        self.read_closed |= rhs.read_closed;
61        self.write_closed |= rhs.write_closed;
62        self.error |= rhs.error;
63    }
64}
65
66impl Readiness {
67    pub const EMPTY: Readiness = Readiness {
68        readable: false,
69        writable: false,
70        read_closed: false,
71        write_closed: false,
72        error: false,
73    };
74}
75
76bitflags::bitflags! {
77    #[derive(Debug, Copy, Clone, PartialEq)]
78    pub struct ReadinessUpdateFlags: u32 {
79        const DEFAULT = 0;
80        /// Trigger edge-triggered even if the set of ready events did not change. This can lead to
81        /// spurious wakeups. Use with caution!
82        const FORCE_EDGE = 1 << 0;
83        /// Do not treat this as a release event from the current thread. This can lead to incorrect
84        /// data race reports. Use with caution!
85        const NO_RELEASE_CLOCK = 1 << 1;
86    }
87}
88
89type ReadinessInterestKey = (FdId, FdNum);
90
91/// Returns the range of all [`ReadinessInterestKey`] for the given FD ID.
92fn range_for_id(id: FdId) -> std::ops::RangeInclusive<ReadinessInterestKey> {
93    (id, 0)..=(id, FdNum::MAX)
94}
95
96#[derive(Debug, Clone)]
97pub struct ReadinessInterest {
98    /// The FD we are interested in.
99    watched_fd: WeakDynFileDescriptionRef,
100    /// The mask of events the interest is interested in
101    /// for this file descriptor.
102    pub relevant: Readiness,
103    /// Boolean whether this is an edge-triggered interest.
104    /// When [`false`] it's a level-triggered interest instead.
105    pub is_edge_triggered: bool,
106    /// Data attached to the interest.
107    // FIXME: In the future we might want to support more data types,
108    // then this should no longer be a `u64` but a `dyn Any` instead.
109    pub data: u64,
110    /// The currently active readiness for this file descriptor.
111    active: Readiness,
112}
113
114impl ReadinessInterest {
115    pub fn active(&self) -> &Readiness {
116        &self.active
117    }
118}
119
120/// A struct which stores [`ReadinessInterest`]s for a set of file descriptions
121/// together with which interests are currently satisfied, and a list of
122/// threads which should be unblocked once a [`ReadinessInterest`] of the
123/// watcher is fulfilled.
124#[derive(Debug, Default)]
125pub struct ReadinessWatcher {
126    /// A map of [`ReadinessInterest`]s registered for this watcher. Each entry is
127    /// identified using a [`FdId`] [`FdNum`] tuple.
128    interests: RefCell<BTreeMap<ReadinessInterestKey, ReadinessInterest>>,
129    /// The subset of interests that is currently considered "ready". Stored separately so we
130    /// can access it more efficiently.
131    /// This is implemented as a queue so that with level-triggered interests, all events eventually
132    /// get returned from [`ReadinessWatcher::get_ready_interests`]. The queue does not contain any
133    /// duplicates.
134    ready: RefCell<VecDeque<ReadinessInterestKey>>,
135    /// The queue of threads blocked on this watcher.
136    queue: RefCell<VecDeque<ThreadId>>,
137}
138
139impl Drop for ReadinessWatcher {
140    fn drop(&mut self) {
141        // Remove ourselves from the FDs we were interested in, at most once per description.
142        let mut last_id = None;
143        for ((id, _fd_num), interest) in self.interests.borrow().iter() {
144            // We'll see interested sorted by ID. Only do something once for each ID.
145            if Some(id) == last_id {
146                continue;
147            }
148            last_id = Some(id);
149            // If the FD still exists, remove ourselves from it.
150            if let Some(fd) = interest.watched_fd.upgrade() {
151                // We can't easily figure out who in that list is us, but we can just remove
152                // everything that has no more strong refs.
153                let mut watchers = fd.readiness_watched().unwrap().watchers.borrow_mut();
154                watchers.retain(|w| w.strong_count() > 0);
155            }
156        }
157    }
158}
159
160impl ReadinessWatcher {
161    /// Add an interest for the file description to which the file descriptor
162    /// `fd_num` belongs.
163    /// `relevant` contains the readiness mask of relevant events.
164    /// `is_edge_triggered` specifies whether the interest is edge-triggered
165    /// ([`true`]) or level-triggered ([`false`]).
166    /// `data` is the user-data which is associated with the interest.
167    ///
168    /// The function returns `Ok(())` when the interest was successfully
169    /// added, and `Err(())` when an interest with this key was already registered.
170    pub fn add_interest<'tcx>(
171        self: &Rc<Self>,
172        fd_num: FdNum,
173        relevant: Readiness,
174        is_edge_triggered: bool,
175        data: u64,
176        ecx: &mut MiriInterpCx<'tcx>,
177    ) -> InterpResult<'tcx, Result<(), ()>> {
178        let fd_ref = ecx.machine.fds.get(fd_num).expect("File description should exist");
179        let fd_id = fd_ref.id();
180        let key = (fd_id, fd_num);
181
182        let interest = ReadinessInterest {
183            watched_fd: FileDescriptionRef::downgrade(&fd_ref),
184            active: Readiness::EMPTY,
185            relevant,
186            is_edge_triggered,
187            data,
188        };
189        let mut interests = self.interests.borrow_mut();
190        if interests.range(range_for_id(fd_id)).next().is_none() {
191            // This is the first time this FD got added to the watcher.
192            // Let's make sure it can be watched, and add ourselves to its list.
193            let watched = fd_ref.readiness_watched().ok_or_else(|| {
194                err_unsup_format!("I/O readiness watching not supported for {}", fd_ref.name())
195            })?;
196            watched.insert(self);
197        }
198        if interests.try_insert(key, interest).is_err() {
199            return interp_ok(Err(()));
200        }
201
202        // After adding a new interest for a fd, we need to forcefully update
203        // the readiness of this fd.
204
205        ecx.update_readiness(
206            self,
207            fd_ref.readiness(),
208            /* force_edge */ true,
209            move |callback| {
210                // Need to release the RefCell when this closure returns, so we have to move
211                // it into the closure, so we have to do a re-lookup here.
212                callback(key, interests.get_mut(&key).unwrap())
213            },
214        )?;
215
216        interp_ok(Ok(()))
217    }
218
219    /// Update the interest which is registered for `key`.
220    /// `cb` gets invoked with a mutable reference to the registered
221    /// [`ReadinessInterest`].
222    ///
223    /// This function returns [`None`] when no interest is registered
224    /// for the specified `key`.
225    pub fn update_interest<'tcx>(
226        self: &Rc<Self>,
227        key: ReadinessInterestKey,
228        ecx: &mut MiriInterpCx<'tcx>,
229        cb: impl FnOnce(&mut ReadinessInterest),
230    ) -> InterpResult<'tcx, Option<()>> {
231        let mut interests = self.interests.borrow_mut();
232        let Some(interest) = interests.get_mut(&key) else { return interp_ok(None) };
233        cb(interest);
234
235        // After updating an interest for a fd, we need to forcefully update
236        // the readiness of this fd.
237
238        let fd_ref = ecx.machine.fds.get(key.1).expect("File description should exist");
239        ecx.update_readiness(
240            self,
241            fd_ref.readiness(),
242            /* force_edge */ true,
243            move |callback| {
244                // Need to release the RefCell when this closure returns, so we have to move
245                // it into the closure, so we have to do a re-lookup here.
246                callback(key, interests.get_mut(&key).unwrap())
247            },
248        )?;
249
250        interp_ok(Some(()))
251    }
252
253    /// Remove the interest registered for `key`.
254    ///
255    /// This function returns [`None`] when no interest is registered
256    /// for the specified `key`.
257    pub fn remove_interest(self: &Rc<ReadinessWatcher>, key: ReadinessInterestKey) -> Option<()> {
258        let mut interests = self.interests.borrow_mut();
259
260        let Some(interest) = interests.remove(&key) else {
261            // We did not have interest in this.
262            return None;
263        };
264        let fd_ref = interest.watched_fd.upgrade().expect("dead watched");
265
266        // Remove the ready event for this key, should one exist.
267        let mut ready_events = self.ready.borrow_mut();
268        if let Some(idx) = ready_events.iter().position(|k| k == &key) {
269            ready_events.remove(idx);
270        }
271        // If this was the last interest in this FD, remove us from the global list
272        // of who is interested in this FD.
273        if interests.range(range_for_id(key.0)).next().is_none() {
274            fd_ref.readiness_watched()?.remove(self);
275        }
276
277        Some(())
278    }
279
280    /// Add the thread with id `thread_id` to the queue of
281    /// blocked threads which will be unblocked when the
282    /// watcher becomes ready.
283    pub fn add_blocked_thread(&self, thread_id: ThreadId) {
284        self.queue.borrow_mut().push_back(thread_id);
285    }
286
287    /// Remove all threads with id `thread_id` from the queue
288    /// of blocked threads which will be unblocked when the
289    /// watcher becomes ready.
290    pub fn remove_blocked_thread(&self, thread_id: ThreadId) {
291        self.queue.borrow_mut().retain(|id| id != &thread_id);
292    }
293
294    /// Get the amount of interests which are registered to this
295    /// watcher and which are currently ready.
296    pub fn ready_count(&self) -> usize {
297        self.ready.borrow().len()
298    }
299
300    /// Get at most the first `max` ready interests from the ready queue.
301    ///
302    /// If the interest is a level-triggered interest, it's automatically
303    /// added to the end of the queue again such that it will only be reported
304    /// after all other ready interest have been returned.
305    ///
306    /// This method returns at most every event from the ready queue once.
307    /// This ensures that every returned interest is unique, even when there
308    /// are level-triggered interests.
309    pub fn get_ready_interests<'tcx>(
310        &self,
311        max: usize,
312        ecx: &mut MiriInterpCx<'tcx>,
313    ) -> InterpResult<'tcx, Vec<ReadinessInterest>> {
314        // The goal below is to do work proportional to how much we're actually reporting to the
315        // client, i.e., how many interests of this watcher are ready. In particular, we do *not*
316        // iterate over all interests of this watcher, or all watchers, or all FDs being watched, or
317        // anything like that.
318
319        let interests = self.interests.borrow();
320        let mut ready = self.ready.borrow_mut();
321
322        // Sanity-check to ensure that all event info is up-to-date.
323        if cfg!(debug_assertions) {
324            for interest in interests.values() {
325                // Ensure this matches the latest readiness of this FD.
326                let fd = interest.watched_fd.upgrade().expect("dead watched");
327                let current_active = fd.readiness();
328                assert_eq!(interest.active(), &(current_active & interest.relevant));
329            }
330        }
331
332        let mut ready_interests = Vec::with_capacity(max.min(ready.len()));
333        let mut re_add_interests = Vec::new();
334
335        // Continue while we haven't yet gotten `max` events, and while there are more to add.
336        while ready_interests.len() < max
337            && let Some(key) = ready.pop_front()
338        {
339            let interest = interests.get(&key).expect("non-existing interest in ready set");
340            let fd = interest.watched_fd.upgrade().expect("dead watched");
341
342            if !interest.is_edge_triggered {
343                // This is a level-triggered interest, so we need to re-add the event:
344                // <https://github.com/torvalds/linux/blob/HEAD/fs/eventpoll.c#L1835-L1847>.
345                // We delay adding them back so they do not get picked up by later loop iterations.
346                re_add_interests.push(key);
347            }
348
349            ready_interests.push(interest.clone());
350            // We now "see" the readiness of this FD, so make the data race system aware of that.
351            ecx.acquire_clock(&fd.readiness_watched().unwrap().ready_clock.borrow())?;
352        }
353        // Add back the level-triggered ones.
354        ready.extend(re_add_interests);
355
356        interp_ok(ready_interests)
357    }
358}
359
360impl VisitProvenance for ReadinessWatcher {
361    fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {}
362}
363
364/// Data about a file description that can be watched for readiness
365/// (meant to be stored inside said file description).
366#[derive(Debug, Default)]
367pub struct ReadinessWatched {
368    /// List of readiness watchers that are interested in us.
369    watchers: RefCell<Vec<Weak<ReadinessWatcher>>>,
370    /// Vector clock for the most recent change to our readiness.
371    /// (Ideally this would be one clock per readiness flag, but we're not bothering with that.)
372    ready_clock: RefCell<VClock>,
373}
374
375impl Drop for ReadinessWatched {
376    fn drop(&mut self) {
377        // "A file descriptor is removed from an interest list only after all the file
378        // descriptors referring to the underlying open file description have been closed."
379        // So, remove ourselves from all interest lists.
380        for watcher in self.watchers.borrow().iter() {
381            // If the watcher still exists, remove ourselves from it.
382            let Some(watcher) = watcher.upgrade() else { continue };
383            // We can't easily figure out who in that list is us, but we can just remove everything
384            // that has no more strong refs.
385            let mut ready_events = watcher.ready.borrow_mut();
386            watcher.interests.borrow_mut().retain(|key, interest| {
387                if interest.watched_fd.is_closed() {
388                    // We'll remove this one. Also remove it from the ready queue if applicable!
389                    if let Some(idx) = ready_events.iter().position(|k| k == key) {
390                        ready_events.remove(idx);
391                    }
392                    false // do not retain
393                } else {
394                    true // do retain
395                }
396            });
397        }
398    }
399}
400
401impl ReadinessWatched {
402    fn insert(&self, watcher: &Rc<ReadinessWatcher>) {
403        let mut watchers = self.watchers.borrow_mut();
404        if cfg!(debug_assertions) {
405            // Ensure uniqueness.
406            assert_matches!(
407                watchers.iter().find(|elem| elem.as_ptr() == Rc::as_ptr(watcher)),
408                None,
409                "watcher has already been added to this watched fd",
410            );
411        }
412        watchers.push(Rc::downgrade(watcher));
413    }
414
415    fn remove(&self, watcher: &Rc<ReadinessWatcher>) {
416        let mut watchers = self.watchers.borrow_mut();
417        // We need to do a linear scan to find the watcher to remove. That's not ideal, but removing
418        // a watched FD from an epoll is rare so it's not worth the non-trivial effort it would take
419        // to make this more efficient.
420        let idx = watchers
421            .iter()
422            .position(|elem| elem.as_ptr() == Rc::as_ptr(watcher))
423            .expect("watcher has no registered interest in the provided watched fd");
424        watchers.remove(idx);
425    }
426
427    /// Returns whether the watched FD has any readiness watcher with a blocked thread watching it.
428    pub fn has_watcher_with_blocked_thread(&self) -> bool {
429        let watchers = self.watchers.borrow();
430        // See if any of those watchers has a blocked thread.
431        watchers.iter().any(|w| w.upgrade().expect("dead watcher").queue.borrow().len() > 0)
432    }
433
434    /// Remove all interes in the given file descriptor, which must refer to the file description
435    /// that contains `self`.
436    pub fn remove_file_num_interests(&self, fd_id: FdId, fd_num: FdNum) {
437        // We make a copy since `remove_interest` may want to mutate `watchers`. This is not
438        // very efficient, but this code anyway only runs on `close` on Illumos.
439        let watchers = self.watchers.borrow().clone();
440        for watcher in watchers.iter() {
441            let watcher = watcher.upgrade().expect("dead watcher");
442            // Remove this interest, if it exists.
443            watcher.remove_interest((fd_id, fd_num));
444        }
445    }
446}
447
448impl<'tcx> EvalContextExt<'tcx> for MiriInterpCx<'tcx> {}
449pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> {
450    /// For a specific file description, get its current readiness and send it to everyone who
451    /// registered interest in this FD. This function must be called whenever the result of
452    /// `FileDescription::readiness` might change.
453    fn update_fd_readiness(
454        &mut self,
455        fd: DynFileDescriptionRef,
456        flags: ReadinessUpdateFlags,
457    ) -> InterpResult<'tcx> {
458        let ecx = self.eval_context_mut();
459        let fd_id = fd.id();
460        let watched = fd.readiness_watched().unwrap();
461
462        if !flags.contains(ReadinessUpdateFlags::NO_RELEASE_CLOCK) {
463            // We capture a vector clock even if there is nobody watching, since someone might get
464            // interested in the future and then synchronize with this event.
465            // FIXME: currently we do this even if the readiness did not change!
466            ecx.release_clock(|clock| {
467                watched.ready_clock.borrow_mut().join(clock);
468            })?;
469        }
470
471        let watchers_ref = watched.watchers.borrow();
472        // Need to make a copy so below we can unblock threads which may need the same `RefCell`.
473        let watchers =
474            watchers_ref.iter().map(|w| w.upgrade().expect("dead watcher")).collect::<Vec<_>>();
475        if watchers.is_empty() {
476            return interp_ok(());
477        };
478        drop(watchers_ref);
479
480        let active_readiness = fd.readiness();
481        for watcher in watchers {
482            ecx.update_readiness(
483                &watcher,
484                active_readiness,
485                flags.contains(ReadinessUpdateFlags::FORCE_EDGE),
486                |callback| {
487                    for (&key, interest) in
488                        watcher.interests.borrow_mut().range_mut(range_for_id(fd_id))
489                    {
490                        callback(key, interest)?;
491                    }
492                    interp_ok(())
493                },
494            )?;
495        }
496
497        interp_ok(())
498    }
499}
500
501impl<'tcx> EvalContextPrivExt<'tcx> for MiriInterpCx<'tcx> {}
502pub trait EvalContextPrivExt<'tcx>: MiriInterpCxExt<'tcx> {
503    /// Call this when the interests denoted by `for_each_interest` have their active readiness changed
504    /// to `active`. The list is provided indirectly via the `for_each_interest` closure, which
505    /// will call its argument closure for each relevant interest.
506    ///
507    /// Any [`RefCell`]s should be released by the time `for_each_interest` returns since we will then
508    /// be waking up threads which might require access to those [`RefCell`]s.
509    fn update_readiness(
510        &mut self,
511        watcher: &Rc<ReadinessWatcher>,
512        active: Readiness,
513        force_edge: bool,
514        for_each_interest: impl FnOnce(
515            &mut dyn FnMut(ReadinessInterestKey, &mut ReadinessInterest) -> InterpResult<'tcx>,
516        ) -> InterpResult<'tcx>,
517    ) -> InterpResult<'tcx> {
518        let ecx = self.eval_context_mut();
519        let mut ready = watcher.ready.borrow_mut();
520        for_each_interest(&mut |key, interest| {
521            let new_readiness = interest.relevant & active;
522            let prev_readiness = std::mem::replace(&mut interest.active, new_readiness);
523            if new_readiness == Readiness::EMPTY {
524                // Un-trigger this, there's nothing left to report here.
525                if let Some(idx) = ready.iter().position(|k| k == &key) {
526                    ready.remove(idx);
527                }
528            } else if force_edge || new_readiness != prev_readiness & new_readiness {
529                // Either we force an "edge" to be detected or there's a bit set in `new_readiness`
530                // that was not set in `prev_readiness`. In both cases, this is ready now.
531
532                // We need to ensure that this event is not already part of the `ready` queue
533                // before enqueueing, as Linux does it with epoll:
534                // <https://github.com/torvalds/linux/blob/13dce771bbad42da6ecf086446d8ddfd1fce3a1b/fs/eventpoll.c#L1290-L1293>
535                if !ready.contains(&key) {
536                    ready.push_back(key);
537                }
538            }
539            interp_ok(())
540        })?;
541
542        // While there are events ready to be delivered, wake up a thread to receive them.
543        while !ready.is_empty()
544            && let Some(thread_id) = watcher.queue.borrow_mut().pop_front()
545        {
546            drop(ready);
547            ecx.unblock_thread(thread_id, BlockReason::Readiness)?;
548            ready = watcher.ready.borrow_mut();
549        }
550        interp_ok(())
551    }
552}