Skip to main content

miri/shims/unix/linux_like/
eventfd.rs

1//! Linux `eventfd` implementation.
2use std::cell::{Cell, RefCell};
3use std::io;
4use std::io::ErrorKind;
5
6use crate::concurrency::VClock;
7use crate::shims::files::{FileDescription, FileDescriptionRef};
8use crate::shims::unix::UnixFileDescription;
9use crate::*;
10
11/// Maximum value that the eventfd counter can hold.
12const MAX_COUNTER: u64 = u64::MAX - 1;
13
14/// A kind of file descriptor created by `eventfd`.
15/// The `Event` type isn't currently written to by `eventfd`.
16/// The interface is meant to keep track of objects associated
17/// with a file descriptor. For more information see the man
18/// page below:
19///
20/// <https://man.netbsd.org/eventfd.2>
21#[derive(Debug)]
22struct EventFd {
23    /// The object contains an unsigned 64-bit integer (uint64_t) counter that is maintained by the
24    /// kernel. This counter is initialized with the value specified in the argument initval.
25    counter: Cell<u64>,
26    is_nonblock: bool,
27    clock: RefCell<VClock>,
28    /// A list of thread ids blocked on eventfd::read.
29    blocked_read_tid: RefCell<Vec<ThreadId>>,
30    /// A list of thread ids blocked on eventfd::write.
31    blocked_write_tid: RefCell<Vec<ThreadId>>,
32    /// State for being watched by epoll.
33    watched: ReadinessWatched,
34}
35
36impl FileDescription for EventFd {
37    fn name(&self) -> &'static str {
38        "event"
39    }
40
41    fn metadata<'tcx>(
42        &self,
43    ) -> InterpResult<'tcx, Either<io::Result<std::fs::Metadata>, &'static str>> {
44        // On Linux, eventfd is an "anonymous inode" reported as S_IFREG.
45        interp_ok(Either::Right("S_IFREG"))
46    }
47
48    /// Read the counter in the buffer and return the counter if succeeded.
49    fn read<'tcx>(
50        self: FileDescriptionRef<Self>,
51        _communicate_allowed: bool,
52        ptr: Pointer,
53        len: usize,
54        ecx: &mut MiriInterpCx<'tcx>,
55        finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
56    ) -> InterpResult<'tcx> {
57        // We're treating the buffer as a `u64`.
58        let ty = ecx.machine.layouts.u64;
59        // Check the size of slice, and return error only if the size of the slice < 8.
60        if len < ty.size.bytes_usize() {
61            return finish.call(ecx, Err(ErrorKind::InvalidInput.into()));
62        }
63
64        // Turn the pointer into a place at the right type.
65        let buf_place = ecx.ptr_to_mplace_unaligned(ptr, ty);
66
67        eventfd_read(buf_place, self, ecx, finish)
68    }
69
70    /// A write call adds the 8-byte integer value supplied in
71    /// its buffer (in native endianness) to the counter.  The maximum value that may be
72    /// stored in the counter is the largest unsigned 64-bit value
73    /// minus 1 (i.e., 0xfffffffffffffffe).  If the addition would
74    /// cause the counter's value to exceed the maximum, then the
75    /// write either blocks until a read is performed on the
76    /// file descriptor, or fails with the error EAGAIN if the
77    /// file descriptor has been made nonblocking.
78    ///
79    /// A write fails with the error EINVAL if the size of the
80    /// supplied buffer is less than 8 bytes, or if an attempt is
81    /// made to write the value 0xffffffffffffffff.
82    fn write<'tcx>(
83        self: FileDescriptionRef<Self>,
84        _communicate_allowed: bool,
85        ptr: Pointer,
86        len: usize,
87        ecx: &mut MiriInterpCx<'tcx>,
88        finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
89    ) -> InterpResult<'tcx> {
90        // We're treating the buffer as a `u64`.
91        let ty = ecx.machine.layouts.u64;
92        // Check the size of slice, and return error if the size is wrong. The docs say we only
93        // error when the size is too small, but Linux seems to also error when the size is too big.
94        if len != ty.layout.size.bytes_usize() {
95            return finish.call(ecx, Err(ErrorKind::InvalidInput.into()));
96        }
97
98        // Turn the pointer into a place at the right type.
99        let buf_place = ecx.ptr_to_mplace_unaligned(ptr, ty);
100
101        eventfd_write(buf_place, self, ecx, finish)
102    }
103
104    fn readiness_watched(&self) -> Option<&ReadinessWatched> {
105        Some(&self.watched)
106    }
107
108    fn readiness(&self) -> Readiness {
109        // We only check the "readable" and "writable" readiness for eventfd. If other event flags
110        // need to be supported in the future, the check should be added here.
111
112        Readiness {
113            readable: self.counter.get() != 0,
114            writable: self.counter.get() != MAX_COUNTER,
115            ..Readiness::EMPTY
116        }
117    }
118
119    fn as_unix<'tcx>(
120        self: FileDescriptionRef<Self>,
121        _ecx: &MiriInterpCx<'tcx>,
122    ) -> FileDescriptionRef<dyn UnixFileDescription> {
123        self
124    }
125}
126
127impl UnixFileDescription for EventFd {}
128
129impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
130pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
131    /// This function creates an `Event` that is used as an event wait/notify mechanism by
132    /// user-space applications, and by the kernel to notify user-space applications of events.
133    /// The `Event` contains an `u64` counter maintained by the kernel. The counter is initialized
134    /// with the value specified in the `initval` argument.
135    ///
136    /// A new file descriptor referring to the `Event` is returned. The `read`, `write`, `poll`,
137    /// `select`, and `close` operations can be performed on the file descriptor. For more
138    /// information on these operations, see the man page linked below.
139    ///
140    /// The `flags` are not currently implemented for eventfd.
141    /// The `flags` may be bitwise ORed to change the behavior of `eventfd`:
142    /// `EFD_CLOEXEC` - Set the close-on-exec (`FD_CLOEXEC`) flag on the new file descriptor.
143    /// `EFD_NONBLOCK` - Set the `O_NONBLOCK` file status flag on the new open file description.
144    /// `EFD_SEMAPHORE` - miri does not support semaphore-like semantics.
145    ///
146    /// <https://linux.die.net/man/2/eventfd>
147    fn eventfd(&mut self, val: &OpTy<'tcx>, flags: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
148        let this = self.eval_context_mut();
149
150        let val = this.read_scalar(val)?.to_u32()?;
151        let mut flags = this.read_scalar(flags)?.to_i32()?;
152
153        let efd_cloexec = this.eval_libc_i32("EFD_CLOEXEC");
154        let efd_nonblock = this.eval_libc_i32("EFD_NONBLOCK");
155        let efd_semaphore = this.eval_libc_i32("EFD_SEMAPHORE");
156
157        if flags & efd_semaphore == efd_semaphore {
158            throw_unsup_format!("eventfd: EFD_SEMAPHORE is unsupported");
159        }
160
161        let mut is_nonblock = false;
162        // Unset the flag that we support.
163        // After unloading, flags != 0 means other flags are used.
164        if flags & efd_cloexec == efd_cloexec {
165            // cloexec is ignored because Miri does not support exec.
166            flags &= !efd_cloexec;
167        }
168        if flags & efd_nonblock == efd_nonblock {
169            flags &= !efd_nonblock;
170            is_nonblock = true;
171        }
172        if flags != 0 {
173            throw_unsup_format!("eventfd: encountered unknown unsupported flags {:#x}", flags);
174        }
175
176        let fds = &mut this.machine.fds;
177
178        let fd_value = fds.insert_new(EventFd {
179            counter: Cell::new(val.into()),
180            is_nonblock,
181            clock: RefCell::new(VClock::default()),
182            blocked_read_tid: RefCell::new(Vec::new()),
183            blocked_write_tid: RefCell::new(Vec::new()),
184            watched: ReadinessWatched::default(),
185        });
186
187        interp_ok(Scalar::from_i32(fd_value))
188    }
189}
190
191/// Block thread if the value addition will exceed u64::MAX -1,
192/// else just add the user-supplied value to current counter.
193fn eventfd_write<'tcx>(
194    buf_place: MPlaceTy<'tcx>,
195    eventfd: FileDescriptionRef<EventFd>,
196    ecx: &mut MiriInterpCx<'tcx>,
197    finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
198) -> InterpResult<'tcx> {
199    // Figure out which value we should add.
200    let num = ecx.read_scalar(&buf_place)?.to_u64()?;
201    // u64::MAX as input is invalid because the maximum value of counter is u64::MAX - 1.
202    if num == u64::MAX {
203        return finish.call(ecx, Err(ErrorKind::InvalidInput.into()));
204    }
205
206    match eventfd.counter.get().checked_add(num) {
207        Some(new_count @ 0..=MAX_COUNTER) => {
208            // Future `read` calls will synchronize with this write, so update the FD clock.
209            ecx.release_clock(|clock| {
210                eventfd.clock.borrow_mut().join(clock);
211            })?;
212
213            // Store new counter value.
214            eventfd.counter.set(new_count);
215
216            // Unblock *all* threads previously blocked on `read`.
217            // We need to take out the blocked thread ids and unblock them together,
218            // because `unblock_threads` may block them again and end up re-adding the
219            // thread to the blocked list.
220            let waiting_threads = std::mem::take(&mut *eventfd.blocked_read_tid.borrow_mut());
221            // FIXME: We can randomize the order of unblocking.
222            for thread_id in waiting_threads {
223                ecx.unblock_thread(thread_id, BlockReason::Eventfd)?;
224            }
225
226            // The state changed; we check and update the status of all supported event
227            // types for current file description.
228            // Linux seems to cause spurious wakeups here, and Tokio seems to rely on that
229            // (see <https://github.com/rust-lang/miri/pull/4676#discussion_r2510528994>
230            // and also <https://www.illumos.org/issues/16700>).
231            ecx.update_fd_readiness(eventfd, ReadinessUpdateFlags::FORCE_EDGE)?;
232
233            // Return how many bytes we consumed from the user-provided buffer.
234            return finish.call(ecx, Ok(buf_place.layout.size.bytes_usize()));
235        }
236        None | Some(u64::MAX) => {
237            // We can't update the state, so we have to block.
238            if eventfd.is_nonblock {
239                return finish.call(ecx, Err(ErrorKind::WouldBlock.into()));
240            }
241
242            eventfd.blocked_write_tid.borrow_mut().push(ecx.active_thread());
243
244            ecx.block_thread(
245                BlockReason::Eventfd,
246                None,
247                callback!(
248                    @capture<'tcx> {
249                        num: u64,
250                        buf_place: MPlaceTy<'tcx>,
251                        finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
252                        eventfd: FileDescriptionRef<EventFd>,
253                    }
254                    |this, unblock: UnblockKind| {
255                        assert_eq!(unblock, UnblockKind::Ready);
256                        eventfd_write(buf_place, eventfd, this, finish)
257                    }
258                ),
259            );
260        }
261    };
262    interp_ok(())
263}
264
265/// Block thread if the current counter is 0,
266/// else just return the current counter value to the caller and set the counter to 0.
267fn eventfd_read<'tcx>(
268    buf_place: MPlaceTy<'tcx>,
269    eventfd: FileDescriptionRef<EventFd>,
270    ecx: &mut MiriInterpCx<'tcx>,
271    finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
272) -> InterpResult<'tcx> {
273    // Set counter to 0, get old value.
274    let counter = eventfd.counter.replace(0);
275
276    // Block when counter == 0.
277    if counter == 0 {
278        if eventfd.is_nonblock {
279            return finish.call(ecx, Err(ErrorKind::WouldBlock.into()));
280        }
281
282        eventfd.blocked_read_tid.borrow_mut().push(ecx.active_thread());
283
284        ecx.block_thread(
285            BlockReason::Eventfd,
286            None,
287            callback!(
288                @capture<'tcx> {
289                    buf_place: MPlaceTy<'tcx>,
290                    finish: DynMachineCallback<'tcx, Result<usize, IoError>>,
291                    eventfd: FileDescriptionRef<EventFd>,
292                }
293                |this, unblock: UnblockKind| {
294                    assert_eq!(unblock, UnblockKind::Ready);
295                    eventfd_read(buf_place, eventfd, this, finish)
296                }
297            ),
298        );
299    } else {
300        // Synchronize with all prior `write` calls to this FD.
301        ecx.acquire_clock(&eventfd.clock.borrow())?;
302
303        // Return old counter value into user-space buffer.
304        ecx.write_int(counter, &buf_place)?;
305
306        // Unblock *all* threads previously blocked on `write`.
307        // We need to take out the blocked thread ids and unblock them together,
308        // because `unblock_threads` may block them again and end up re-adding the
309        // thread to the blocked list.
310        let waiting_threads = std::mem::take(&mut *eventfd.blocked_write_tid.borrow_mut());
311        // FIXME: We can randomize the order of unblocking.
312        for thread_id in waiting_threads {
313            ecx.unblock_thread(thread_id, BlockReason::Eventfd)?;
314        }
315
316        // The state changed; we check and update the status of all supported event
317        // types for current file description.
318        // Linux seems to always emit do notifications here, even if we were already writable.
319        ecx.update_fd_readiness(eventfd, ReadinessUpdateFlags::FORCE_EDGE)?;
320
321        // Tell userspace how many bytes we put into the buffer.
322        return finish.call(ecx, Ok(buf_place.layout.size.bytes_usize()));
323    }
324    interp_ok(())
325}