Skip to content

Commit d1a4812

Browse files
committed
store futexes in per-allocation data rather than globally
1 parent a839fbf commit d1a4812

File tree

5 files changed

+122
-55
lines changed

5 files changed

+122
-55
lines changed

Diff for: src/tools/miri/src/concurrency/sync.rs

+40-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::cell::RefCell;
12
use std::collections::VecDeque;
23
use std::collections::hash_map::Entry;
34
use std::ops::Not;
5+
use std::rc::Rc;
46
use std::time::Duration;
57

68
use rustc_abi::Size;
@@ -121,6 +123,15 @@ struct Futex {
121123
clock: VClock,
122124
}
123125

126+
#[derive(Default, Clone)]
127+
pub struct FutexRef(Rc<RefCell<Futex>>);
128+
129+
impl VisitProvenance for FutexRef {
130+
fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
131+
// No provenance in `Futex`.
132+
}
133+
}
134+
124135
/// A thread waiting on a futex.
125136
#[derive(Debug)]
126137
struct FutexWaiter {
@@ -137,9 +148,6 @@ pub struct SynchronizationObjects {
137148
rwlocks: IndexVec<RwLockId, RwLock>,
138149
condvars: IndexVec<CondvarId, Condvar>,
139150
pub(super) init_onces: IndexVec<InitOnceId, InitOnce>,
140-
141-
/// Futex info for the futex at the given address.
142-
futexes: FxHashMap<u64, Futex>,
143151
}
144152

145153
// Private extension trait for local helper methods
@@ -184,7 +192,7 @@ impl SynchronizationObjects {
184192
}
185193

186194
impl<'tcx> AllocExtra<'tcx> {
187-
pub fn get_sync<T: 'static>(&self, offset: Size) -> Option<&T> {
195+
fn get_sync<T: 'static>(&self, offset: Size) -> Option<&T> {
188196
self.sync.get(&offset).and_then(|s| s.downcast_ref::<T>())
189197
}
190198
}
@@ -273,27 +281,32 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
273281

274282
/// Get the synchronization primitive associated with the given pointer,
275283
/// or initialize a new one.
284+
///
285+
/// Return `None` if this pointer does not point to at least 1 byte of mutable memory.
276286
fn get_sync_or_init<'a, T: 'static>(
277287
&'a mut self,
278288
ptr: Pointer,
279-
new: impl FnOnce(&'a mut MiriMachine<'tcx>) -> InterpResult<'tcx, T>,
280-
) -> InterpResult<'tcx, &'a T>
289+
new: impl FnOnce(&'a mut MiriMachine<'tcx>) -> T,
290+
) -> Option<&'a T>
281291
where
282292
'tcx: 'a,
283293
{
284294
let this = self.eval_context_mut();
285-
// Ensure there is memory behind this pointer, so that this allocation
286-
// is truly the only place where the data could be stored.
287-
this.check_ptr_access(ptr, Size::from_bytes(1), CheckInAllocMsg::InboundsTest)?;
288-
289-
let (alloc, offset, _) = this.ptr_get_alloc_id(ptr, 0)?;
290-
let (alloc_extra, machine) = this.get_alloc_extra_mut(alloc)?;
295+
if !this.ptr_try_get_alloc_id(ptr, 0).ok().is_some_and(|(alloc_id, offset, ..)| {
296+
let info = this.get_alloc_info(alloc_id);
297+
info.kind == AllocKind::LiveData && info.mutbl.is_mut() && offset < info.size
298+
}) {
299+
return None;
300+
}
301+
// This cannot fail now.
302+
let (alloc, offset, _) = this.ptr_get_alloc_id(ptr, 0).unwrap();
303+
let (alloc_extra, machine) = this.get_alloc_extra_mut(alloc).unwrap();
291304
// Due to borrow checker reasons, we have to do the lookup twice.
292305
if alloc_extra.get_sync::<T>(offset).is_none() {
293-
let new = new(machine)?;
306+
let new = new(machine);
294307
alloc_extra.sync.insert(offset, Box::new(new));
295308
}
296-
interp_ok(alloc_extra.get_sync::<T>(offset).unwrap())
309+
Some(alloc_extra.get_sync::<T>(offset).unwrap())
297310
}
298311

299312
#[inline]
@@ -690,7 +703,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
690703
/// On a timeout, `retval_timeout` is written to `dest` and `errno_timeout` is set as the last error.
691704
fn futex_wait(
692705
&mut self,
693-
addr: u64,
706+
futex_ref: FutexRef,
694707
bitset: u32,
695708
timeout: Option<(TimeoutClock, TimeoutAnchor, Duration)>,
696709
retval_succ: Scalar,
@@ -700,23 +713,25 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
700713
) {
701714
let this = self.eval_context_mut();
702715
let thread = this.active_thread();
703-
let futex = &mut this.machine.sync.futexes.entry(addr).or_default();
716+
let mut futex = futex_ref.0.borrow_mut();
704717
let waiters = &mut futex.waiters;
705718
assert!(waiters.iter().all(|waiter| waiter.thread != thread), "thread is already waiting");
706719
waiters.push_back(FutexWaiter { thread, bitset });
720+
drop(futex);
721+
707722
this.block_thread(
708-
BlockReason::Futex { addr },
723+
BlockReason::Futex,
709724
timeout,
710725
callback!(
711726
@capture<'tcx> {
712-
addr: u64,
727+
futex_ref: FutexRef,
713728
retval_succ: Scalar,
714729
retval_timeout: Scalar,
715730
dest: MPlaceTy<'tcx>,
716731
errno_timeout: IoError,
717732
}
718733
@unblock = |this| {
719-
let futex = this.machine.sync.futexes.get(&addr).unwrap();
734+
let futex = futex_ref.0.borrow();
720735
// Acquire the clock of the futex.
721736
if let Some(data_race) = &this.machine.data_race {
722737
data_race.acquire_clock(&futex.clock, &this.machine.threads);
@@ -728,7 +743,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
728743
@timeout = |this| {
729744
// Remove the waiter from the futex.
730745
let thread = this.active_thread();
731-
let futex = this.machine.sync.futexes.get_mut(&addr).unwrap();
746+
let mut futex = futex_ref.0.borrow_mut();
732747
futex.waiters.retain(|waiter| waiter.thread != thread);
733748
// Set errno and write return value.
734749
this.set_last_error(errno_timeout)?;
@@ -739,12 +754,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
739754
);
740755
}
741756

757+
/// Wake up the first thread in the queue that matches any of the bits in the bitset.
742758
/// Returns whether anything was woken.
743-
fn futex_wake(&mut self, addr: u64, bitset: u32) -> InterpResult<'tcx, bool> {
759+
fn futex_wake(&mut self, futex_ref: &FutexRef, bitset: u32) -> InterpResult<'tcx, bool> {
744760
let this = self.eval_context_mut();
745-
let Some(futex) = this.machine.sync.futexes.get_mut(&addr) else {
746-
return interp_ok(false);
747-
};
761+
let mut futex = futex_ref.0.borrow_mut();
748762
let data_race = &this.machine.data_race;
749763

750764
// Each futex-wake happens-before the end of the futex wait
@@ -757,7 +771,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
757771
return interp_ok(false);
758772
};
759773
let waiter = futex.waiters.remove(i).unwrap();
760-
this.unblock_thread(waiter.thread, BlockReason::Futex { addr })?;
774+
drop(futex);
775+
this.unblock_thread(waiter.thread, BlockReason::Futex)?;
761776
interp_ok(true)
762777
}
763778
}

Diff for: src/tools/miri/src/concurrency/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub enum BlockReason {
147147
/// Blocked on a reader-writer lock.
148148
RwLock(RwLockId),
149149
/// Blocked on a Futex variable.
150-
Futex { addr: u64 },
150+
Futex,
151151
/// Blocked on an InitOnce.
152152
InitOnce(InitOnceId),
153153
/// Blocked on epoll.

Diff for: src/tools/miri/src/shims/unix/linux/sync.rs

+43-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
use crate::concurrency::sync::FutexRef;
12
use crate::helpers::check_min_arg_count;
23
use crate::*;
34

5+
struct LinuxFutex {
6+
futex: FutexRef,
7+
}
8+
49
/// Implementation of the SYS_futex syscall.
510
/// `args` is the arguments *including* the syscall number.
611
pub fn futex<'tcx>(
@@ -27,7 +32,6 @@ pub fn futex<'tcx>(
2732

2833
// This is a vararg function so we have to bring our own type for this pointer.
2934
let addr = this.ptr_to_mplace(addr, this.machine.layouts.i32);
30-
let addr_usize = addr.ptr().addr().bytes();
3135

3236
let futex_private = this.eval_libc_i32("FUTEX_PRIVATE_FLAG");
3337
let futex_wait = this.eval_libc_i32("FUTEX_WAIT");
@@ -63,8 +67,7 @@ pub fn futex<'tcx>(
6367
};
6468

6569
if bitset == 0 {
66-
this.set_last_error_and_return(LibcError("EINVAL"), dest)?;
67-
return interp_ok(());
70+
return this.set_last_error_and_return(LibcError("EINVAL"), dest);
6871
}
6972

7073
let timeout = this.deref_pointer_as(timeout, this.libc_ty_layout("timespec"))?;
@@ -99,19 +102,18 @@ pub fn futex<'tcx>(
99102
// effects of this and the other thread are correctly observed,
100103
// otherwise we will deadlock.
101104
//
102-
// There are two scenarios to consider:
103-
// 1. If we (FUTEX_WAIT) execute first, we'll push ourselves into
104-
// the waiters queue and go to sleep. They (addr write & FUTEX_WAKE)
105-
// will see us in the queue and wake us up.
106-
// 2. If they (addr write & FUTEX_WAKE) execute first, we must observe
107-
// addr's new value. If we see an outdated value that happens to equal
108-
// the expected val, then we'll put ourselves to sleep with no one to wake us
109-
// up, so we end up with a deadlock. This is prevented by having a SeqCst
110-
// fence inside FUTEX_WAKE syscall, and another SeqCst fence
111-
// below, the atomic read on addr after the SeqCst fence is guaranteed
112-
// not to see any value older than the addr write immediately before
113-
// calling FUTEX_WAKE. We'll see futex_val != val and return without
114-
// sleeping.
105+
// There are two scenarios to consider, depending on whether WAIT or WAKE goes first:
106+
// 1. If we (FUTEX_WAIT) execute first, we'll push ourselves into the waiters queue and
107+
// go to sleep. They (FUTEX_WAKE) will see us in the queue and wake us up. It doesn't
108+
// matter how the addr write is ordered.
109+
// 2. If they (FUTEX_WAKE) execute first, that means the addr write is also before us
110+
// (FUTEX_WAIT). It is crucial that we observe addr's new value. If we see an
111+
// outdated value that happens to equal the expected val, then we'll put ourselves to
112+
// sleep with no one to wake us up, so we end up with a deadlock. This is prevented
113+
// by having a SeqCst fence inside FUTEX_WAKE syscall, and another SeqCst fence here
114+
// in FUTEX_WAIT. The atomic read on addr after the SeqCst fence is guaranteed not to
115+
// see any value older than the addr write immediately before calling FUTEX_WAKE.
116+
// We'll see futex_val != val and return without sleeping.
115117
//
116118
// Note that the fences do not create any happens-before relationship.
117119
// The read sees the write immediately before the fence not because
@@ -140,11 +142,22 @@ pub fn futex<'tcx>(
140142
this.atomic_fence(AtomicFenceOrd::SeqCst)?;
141143
// Read an `i32` through the pointer, regardless of any wrapper types.
142144
// It's not uncommon for `addr` to be passed as another type than `*mut i32`, such as `*const AtomicI32`.
143-
let futex_val = this.read_scalar_atomic(&addr, AtomicReadOrd::Relaxed)?.to_i32()?;
145+
// We do an acquire read -- it only seems reasonable that if we observe a value here, we
146+
// actually establish an ordering with that value.
147+
let futex_val = this.read_scalar_atomic(&addr, AtomicReadOrd::Acquire)?.to_i32()?;
144148
if val == futex_val {
145149
// The value still matches, so we block the thread and make it wait for FUTEX_WAKE.
150+
151+
// This cannot fail since we already did an atomic acquire read on that pointer.
152+
// Acquire reads are only allowed on mutable memory.
153+
let futex_ref = this
154+
.get_sync_or_init(addr.ptr(), |_| LinuxFutex { futex: Default::default() })
155+
.unwrap()
156+
.futex
157+
.clone();
158+
146159
this.futex_wait(
147-
addr_usize,
160+
futex_ref,
148161
bitset,
149162
timeout,
150163
Scalar::from_target_isize(0, this), // retval_succ
@@ -165,6 +178,17 @@ pub fn futex<'tcx>(
165178
// FUTEX_WAKE_BITSET: (int *addr, int op = FUTEX_WAKE, int val, const timespect *_unused, int *_unused, unsigned int bitset)
166179
// Same as FUTEX_WAKE, but allows you to specify a bitset to select which threads to wake up.
167180
op if op == futex_wake || op == futex_wake_bitset => {
181+
let Some(futex_ref) =
182+
this.get_sync_or_init(addr.ptr(), |_| LinuxFutex { futex: Default::default() })
183+
else {
184+
// No AllocId, or no live allocation at that AllocId.
185+
// Return an error code. (That seems nicer than silently doing something non-intuitive.)
186+
// This means that if an address gets reused by a new allocation,
187+
// we'll use an independent futex queue for this... that seems acceptable.
188+
return this.set_last_error_and_return(LibcError("EFAULT"), dest);
189+
};
190+
let futex_ref = futex_ref.futex.clone();
191+
168192
let bitset = if op == futex_wake_bitset {
169193
let [_, _, _, _, timeout, uaddr2, bitset] =
170194
check_min_arg_count("`syscall(SYS_futex, FUTEX_WAKE_BITSET, ...)`", args)?;
@@ -184,7 +208,7 @@ pub fn futex<'tcx>(
184208
let mut n = 0;
185209
#[expect(clippy::arithmetic_side_effects)]
186210
for _ in 0..val {
187-
if this.futex_wake(addr_usize, bitset)? {
211+
if this.futex_wake(&futex_ref, bitset)? {
188212
n += 1;
189213
} else {
190214
break;

Diff for: src/tools/miri/src/shims/windows/sync.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ use std::time::Duration;
33
use rustc_abi::Size;
44

55
use crate::concurrency::init_once::InitOnceStatus;
6+
use crate::concurrency::sync::FutexRef;
67
use crate::*;
78

89
#[derive(Copy, Clone)]
910
struct WindowsInitOnce {
1011
id: InitOnceId,
1112
}
1213

14+
struct WindowsFutex {
15+
futex: FutexRef,
16+
}
17+
1318
impl<'tcx> EvalContextExtPriv<'tcx> for crate::MiriInterpCx<'tcx> {}
1419
trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
1520
// Windows sync primitives are pointer sized.
@@ -168,8 +173,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
168173
let size = this.read_target_usize(size_op)?;
169174
let timeout_ms = this.read_scalar(timeout_op)?.to_u32()?;
170175

171-
let addr = ptr.addr().bytes();
172-
173176
if size > 8 || !size.is_power_of_two() {
174177
let invalid_param = this.eval_windows("c", "ERROR_INVALID_PARAMETER");
175178
this.set_last_error(invalid_param)?;
@@ -190,13 +193,21 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
190193

191194
let layout = this.machine.layouts.uint(size).unwrap();
192195
let futex_val =
193-
this.read_scalar_atomic(&this.ptr_to_mplace(ptr, layout), AtomicReadOrd::Relaxed)?;
196+
this.read_scalar_atomic(&this.ptr_to_mplace(ptr, layout), AtomicReadOrd::Acquire)?;
194197
let compare_val = this.read_scalar(&this.ptr_to_mplace(compare, layout))?;
195198

196199
if futex_val == compare_val {
197200
// If the values are the same, we have to block.
201+
202+
// This cannot fail since we already did an atomic acquire read on that pointer.
203+
let futex_ref = this
204+
.get_sync_or_init(ptr, |_| WindowsFutex { futex: Default::default() })
205+
.unwrap()
206+
.futex
207+
.clone();
208+
198209
this.futex_wait(
199-
addr,
210+
futex_ref,
200211
u32::MAX, // bitset
201212
timeout,
202213
Scalar::from_i32(1), // retval_succ
@@ -219,8 +230,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
219230
// See the Linux futex implementation for why this fence exists.
220231
this.atomic_fence(AtomicFenceOrd::SeqCst)?;
221232

222-
let addr = ptr.addr().bytes();
223-
this.futex_wake(addr, u32::MAX)?;
233+
let Some(futex_ref) =
234+
this.get_sync_or_init(ptr, |_| WindowsFutex { futex: Default::default() })
235+
else {
236+
// Seems like this cannot return an error, so we just wake nobody.
237+
return interp_ok(());
238+
};
239+
let futex_ref = futex_ref.futex.clone();
240+
241+
this.futex_wake(&futex_ref, u32::MAX)?;
224242

225243
interp_ok(())
226244
}
@@ -232,8 +250,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
232250
// See the Linux futex implementation for why this fence exists.
233251
this.atomic_fence(AtomicFenceOrd::SeqCst)?;
234252

235-
let addr = ptr.addr().bytes();
236-
while this.futex_wake(addr, u32::MAX)? {}
253+
let Some(futex_ref) =
254+
this.get_sync_or_init(ptr, |_| WindowsFutex { futex: Default::default() })
255+
else {
256+
// Seems like this cannot return an error, so we just wake nobody.
257+
return interp_ok(());
258+
};
259+
let futex_ref = futex_ref.futex.clone();
260+
261+
while this.futex_wake(&futex_ref, u32::MAX)? {}
237262

238263
interp_ok(())
239264
}

Diff for: src/tools/miri/tests/pass-dep/concurrency/linux-futex.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ fn wake_dangling() {
4141
let ptr: *const i32 = &*futex;
4242
drop(futex);
4343

44-
// Wake 1 waiter. Expect zero waiters woken up, as nobody is waiting.
44+
// Expect error since this is now "unmapped" memory.
45+
// parking_lot relies on this:
46+
// <https://github.com/Amanieu/parking_lot/blob/ca920b31312839013b4455aba1d53a4aede21b2f/core/src/thread_parker/linux.rs#L138-L145>
4547
unsafe {
46-
assert_eq!(libc::syscall(libc::SYS_futex, ptr, libc::FUTEX_WAKE, 1), 0);
48+
assert_eq!(libc::syscall(libc::SYS_futex, ptr, libc::FUTEX_WAKE, 1), -1);
49+
assert_eq!(io::Error::last_os_error().raw_os_error().unwrap(), libc::EFAULT);
4750
}
4851
}
4952

0 commit comments

Comments
 (0)