Skip to main content

miri/shims/unix/linux_like/
sync.rs

1use crate::concurrency::sync::{FutexRef, SyncObj};
2use crate::shims::sig::Varargs;
3use crate::*;
4
5struct LinuxFutex {
6    futex: FutexRef,
7}
8
9impl SyncObj for LinuxFutex {}
10
11/// Implementation of the SYS_futex syscall.
12/// `args` is the arguments *including* the syscall number.
13pub fn futex<'tcx>(
14    ecx: &mut MiriInterpCx<'tcx>,
15    varargs: Varargs<'tcx, '_>,
16    dest: &MPlaceTy<'tcx>,
17) -> InterpResult<'tcx> {
18    let ([addr, op, val], varargs) =
19        ecx.check_varargs(shim_varargs![*_, i32, u32], varargs, "syscall(SYS_futex, ...)")?;
20
21    // See <https://man7.org/linux/man-pages/man2/futex.2.html> for docs.
22    // The first three arguments (after the syscall number itself) are the same to all futex operations:
23    //     (uint32_t *addr, int op, uint32_t val).
24    // We checked above that these definitely exist.
25    let addr = ecx.read_pointer(addr)?;
26    let op = ecx.read_scalar(op)?.to_i32()?;
27    let val = ecx.read_scalar(val)?.to_u32()?;
28
29    // This is a vararg function so we have to bring our own type for this pointer.
30    let addr = ecx.ptr_to_mplace(addr, ecx.machine.layouts.i32);
31
32    let futex_private = ecx.eval_libc_i32("FUTEX_PRIVATE_FLAG");
33    let futex_wait = ecx.eval_libc_i32("FUTEX_WAIT");
34    let futex_wait_bitset = ecx.eval_libc_i32("FUTEX_WAIT_BITSET");
35    let futex_wake = ecx.eval_libc_i32("FUTEX_WAKE");
36    let futex_wake_bitset = ecx.eval_libc_i32("FUTEX_WAKE_BITSET");
37    let futex_realtime = ecx.eval_libc_i32("FUTEX_CLOCK_REALTIME");
38
39    // FUTEX_PRIVATE enables an optimization that stops it from working across processes.
40    // Miri doesn't support that anyway, so we ignore that flag.
41    match op & !futex_private {
42        // FUTEX_WAIT: (int *addr, int op = FUTEX_WAIT, int val, const timespec *timeout)
43        // Blocks the thread if *addr still equals val. Wakes up when FUTEX_WAKE is called on the same address,
44        // or *timeout expires. `timeout == null` for an infinite timeout.
45        //
46        // FUTEX_WAIT_BITSET: (int *addr, int op = FUTEX_WAIT_BITSET, int val, const timespec *timeout, int *_ignored, unsigned int bitset)
47        // This is identical to FUTEX_WAIT, except:
48        //  - The timeout is absolute rather than relative.
49        //  - You can specify the bitset to selecting what WAKE operations to respond to.
50        op if op & !futex_realtime == futex_wait || op & !futex_realtime == futex_wait_bitset => {
51            let wait_bitset = op & !futex_realtime == futex_wait_bitset;
52
53            let (timeout, bitset) = if wait_bitset {
54                let ([timeout, uaddr2, bitset], _) = ecx.check_varargs(
55                    shim_varargs![*_, *_, u32],
56                    varargs,
57                    "syscall(SYS_futex, ...)",
58                )?;
59                let uaddr2 = ecx.read_pointer(uaddr2)?;
60                if !ecx.ptr_is_null(uaddr2)? {
61                    throw_ub_format!("`uaddr2` pointer must be null for `FUTEX_WAIT_BITSET`");
62                }
63                (timeout, ecx.read_scalar(bitset)?.to_u32()?)
64            } else {
65                let ([timeout], _) =
66                    ecx.check_varargs(shim_varargs![*_], varargs, "syscall(SYS_futex, ...)")?;
67                (timeout, u32::MAX)
68            };
69
70            if bitset == 0 {
71                return ecx.set_errno_and_return_neg1(LibcError("EINVAL"), dest);
72            }
73
74            let timeout = ecx.deref_pointer_as(timeout, ecx.libc_ty_layout("timespec"))?;
75            let deadline = if ecx.ptr_is_null(timeout.ptr())? {
76                None
77            } else {
78                let Some(duration) = ecx.read_timespec(&timeout)? else {
79                    return ecx.set_errno_and_return_neg1(LibcError("EINVAL"), dest);
80                };
81                let timeout_clock = if op & futex_realtime == futex_realtime {
82                    ecx.check_no_isolation(
83                        "`futex` syscall with `op=FUTEX_WAIT` and non-null timeout with `FUTEX_CLOCK_REALTIME`",
84                    )?;
85                    TimeoutClock::RealTime
86                } else {
87                    TimeoutClock::Monotonic
88                };
89                let timeout_style = if wait_bitset {
90                    // FUTEX_WAIT_BITSET uses an absolute timestamp.
91                    TimeoutStyle::Absolute
92                } else {
93                    // FUTEX_WAIT uses a relative timestamp.
94                    TimeoutStyle::Relative
95                };
96                Some(ecx.machine.timeout(timeout_clock, timeout_style, duration))
97            };
98            // There may be a concurrent thread changing the value of addr
99            // and then invoking the FUTEX_WAKE syscall. It is critical that the
100            // effects of this and the other thread are correctly observed,
101            // otherwise we will deadlock.
102            //
103            // There are two scenarios to consider, depending on whether WAIT or WAKE goes first:
104            // 1. If we (FUTEX_WAIT) execute first, we'll push ourselves into the waiters queue and
105            //    go to sleep. They (FUTEX_WAKE) will see us in the queue and wake us up. It doesn't
106            //    matter how the addr write is ordered.
107            // 2. If they (FUTEX_WAKE) execute first, that means the addr write is also before us
108            //    (FUTEX_WAIT). It is crucial that we observe addr's new value. If we see an
109            //    outdated value that happens to equal the expected val, then we'll put ourselves to
110            //    sleep with no one to wake us up, so we end up with a deadlock. This is prevented
111            //    by having a SeqCst fence inside FUTEX_WAKE syscall, and another SeqCst fence here
112            //    in FUTEX_WAIT. The atomic read on addr after the SeqCst fence is guaranteed not to
113            //    see any value older than the addr write immediately before calling FUTEX_WAKE.
114            //    We'll see futex_val != val and return without sleeping.
115            //
116            //    Note that the fences do not create any happens-before relationship.
117            //    The read sees the write immediately before the fence not because
118            //    one happens after the other, but is instead due to a guarantee unique
119            //    to SeqCst fences that restricts what an atomic read placed AFTER the
120            //    fence can see. The read still has to be atomic, otherwise it's a data
121            //    race. This guarantee cannot be achieved with acquire-release fences
122            //    since they only talk about reads placed BEFORE a fence - and places
123            //    no restrictions on what the read itself can see, only that there is
124            //    a happens-before between the fences IF the read happens to see the
125            //    right value. This is useless to us, since we need the read itself
126            //    to see an up-to-date value.
127            //
128            // The above case distinction is valid since both FUTEX_WAIT and FUTEX_WAKE
129            // contain a SeqCst fence, therefore inducing a total order between the operations.
130            // It is also critical that the fence, the atomic load, and the comparison in FUTEX_WAIT
131            // altogether happen atomically. If the other thread's fence in FUTEX_WAKE
132            // gets interleaved after our fence, then we lose the guarantee on the
133            // atomic load being up-to-date; if the other thread's write on addr and FUTEX_WAKE
134            // call are interleaved after the load but before the comparison, then we get a TOCTOU
135            // race condition, and go to sleep thinking the other thread will wake us up,
136            // even though they have already finished.
137            //
138            // Thankfully, preemptions cannot happen inside a Miri shim, so we do not need to
139            // do anything special to guarantee fence-load-comparison atomicity.
140            ecx.atomic_fence(AtomicFenceOrd::SeqCst)?;
141            // Read an `i32` through the pointer, regardless of any wrapper types.
142            // It's not uncommon for `addr` to be passed as another type than `*mut i32`, such as `*const AtomicI32`.
143            // We do an acquire read -- it only seems reasonable that if we observe a value here, we
144            // actually establish an ordering with that value.
145            let futex_val = ecx.read_scalar_atomic(&addr, AtomicReadOrd::Acquire)?.to_u32()?;
146            if val == futex_val {
147                // The value still matches, so we block the thread and make it wait for FUTEX_WAKE.
148
149                // This cannot fail since we already did an atomic acquire read on that pointer.
150                // Acquire reads are only allowed on mutable memory.
151                let futex_ref = ecx
152                    .get_sync_or_init(addr.ptr(), |_| LinuxFutex { futex: Default::default() })
153                    .unwrap()
154                    .futex
155                    .clone();
156
157                let dest = dest.clone();
158                ecx.futex_wait(
159                    futex_ref,
160                    bitset,
161                    deadline,
162                    callback!(
163                        @capture<'tcx> {
164                            dest: MPlaceTy<'tcx>,
165                        }
166                        |ecx, unblock: UnblockKind| match unblock {
167                            UnblockKind::Ready => {
168                                ecx.write_int(0, &dest)
169                            }
170                            UnblockKind::TimedOut => {
171                                ecx.set_errno_and_return_neg1(LibcError("ETIMEDOUT"), &dest)
172                            }
173                        }
174                    ),
175                );
176            } else {
177                // The futex value doesn't match the expected value, so we return failure
178                // right away without sleeping: -1 and errno set to EAGAIN.
179                return ecx.set_errno_and_return_neg1(LibcError("EAGAIN"), dest);
180            }
181        }
182        // FUTEX_WAKE: (int *addr, int op = FUTEX_WAKE, int val)
183        // Wakes at most `val` threads waiting on the futex at `addr`.
184        // Returns the amount of threads woken up.
185        // Does not access the futex value at *addr.
186        // FUTEX_WAKE_BITSET: (int *addr, int op = FUTEX_WAKE, int val, const timespect *_unused, int *_unused, unsigned int bitset)
187        // Same as FUTEX_WAKE, but allows you to specify a bitset to select which threads to wake up.
188        op if op == futex_wake || op == futex_wake_bitset => {
189            let Some(futex_ref) =
190                ecx.get_sync_or_init(addr.ptr(), |_| LinuxFutex { futex: Default::default() })
191            else {
192                // No AllocId, or no live allocation at that AllocId.
193                // Return an error code. (That seems nicer than silently doing something non-intuitive.)
194                // This means that if an address gets reused by a new allocation,
195                // we'll use an independent futex queue for this... that seems acceptable.
196                return ecx.set_errno_and_return_neg1(LibcError("EFAULT"), dest);
197            };
198            let futex_ref = futex_ref.futex.clone();
199
200            let bitset = if op == futex_wake_bitset {
201                let ([timeout, uaddr2, bitset], _) = ecx.check_varargs(
202                    shim_varargs![*_, *_, u32],
203                    varargs,
204                    "syscall(SYS_futex, ...)",
205                )?;
206                let timeout = ecx.read_pointer(timeout)?;
207                if !ecx.ptr_is_null(timeout)? {
208                    throw_ub_format!("`timeout` pointer must be null for `FUTEX_WAKE_BITSET`");
209                }
210                let uaddr2 = ecx.read_pointer(uaddr2)?;
211                if !ecx.ptr_is_null(uaddr2)? {
212                    throw_ub_format!("`uaddr2` pointer must be null for `FUTEX_WAKE_BITSET`");
213                }
214                ecx.read_scalar(bitset)?.to_u32()?
215            } else {
216                u32::MAX
217            };
218            if bitset == 0 {
219                return ecx.set_errno_and_return_neg1(LibcError("EINVAL"), dest);
220            }
221            // Together with the SeqCst fence in futex_wait, this makes sure that futex_wait
222            // will see the latest value on addr which could be changed by our caller
223            // before doing the syscall.
224            ecx.atomic_fence(AtomicFenceOrd::SeqCst)?;
225            let woken = ecx.futex_wake(&futex_ref, bitset, val.try_into().unwrap())?;
226            ecx.write_scalar(Scalar::from_target_isize(woken.try_into().unwrap(), ecx), dest)?;
227        }
228        op => throw_unsup_format!("Miri does not support `futex` syscall with op={}", op),
229    }
230
231    interp_ok(())
232}