Skip to content

Commit 89323bf

Browse files
committed
pthread_mutex: store mutex ID outside adressable memory, so it can be trusted
1 parent bd8f2af commit 89323bf

File tree

7 files changed

+181
-127
lines changed

7 files changed

+181
-127
lines changed

src/tools/miri/src/concurrency/sync.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ pub struct SynchronizationObjects {
167167
mutexes: IndexVec<MutexId, Mutex>,
168168
rwlocks: IndexVec<RwLockId, RwLock>,
169169
condvars: IndexVec<CondvarId, Condvar>,
170-
futexes: FxHashMap<u64, Futex>,
171170
pub(super) init_onces: IndexVec<InitOnceId, InitOnce>,
171+
172+
/// Futex info for the futex at the given address.
173+
futexes: FxHashMap<u64, Futex>,
172174
}
173175

174176
// Private extension trait for local helper methods
@@ -277,17 +279,9 @@ pub(super) trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
277279
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
278280
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
279281
/// Eagerly create and initialize a new mutex.
280-
fn mutex_create(
281-
&mut self,
282-
lock: &MPlaceTy<'tcx>,
283-
offset: u64,
284-
data: Option<Box<dyn Any>>,
285-
) -> InterpResult<'tcx, MutexId> {
282+
fn mutex_create(&mut self) -> MutexId {
286283
let this = self.eval_context_mut();
287-
this.create_id(lock, offset, |ecx| &mut ecx.machine.sync.mutexes, Mutex {
288-
data,
289-
..Default::default()
290-
})
284+
this.machine.sync.mutexes.push(Default::default())
291285
}
292286

293287
/// Lazily create a new mutex.

src/tools/miri/src/helpers.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
223223
}
224224

225225
/// Evaluates the scalar at the specified path.
226-
fn eval_path(&self, path: &[&str]) -> OpTy<'tcx> {
226+
fn eval_path(&self, path: &[&str]) -> MPlaceTy<'tcx> {
227227
let this = self.eval_context_ref();
228228
let instance = resolve_path(*this.tcx, path, Namespace::ValueNS);
229229
// We don't give a span -- this isn't actually used directly by the program anyway.
230-
let const_val = this.eval_global(instance).unwrap_or_else(|err| {
230+
this.eval_global(instance).unwrap_or_else(|err| {
231231
panic!("failed to evaluate required Rust item: {path:?}\n{err:?}")
232-
});
233-
const_val.into()
232+
})
234233
}
235234
fn eval_path_scalar(&self, path: &[&str]) -> Scalar {
236235
let this = self.eval_context_ref();

src/tools/miri/src/machine.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Global machine state as well as implementation of the interpreter engine
22
//! `Machine` trait.
33
4+
use std::any::Any;
45
use std::borrow::Cow;
56
use std::cell::RefCell;
67
use std::collections::hash_map::Entry;
@@ -336,6 +337,11 @@ pub struct AllocExtra<'tcx> {
336337
/// if this allocation is leakable. The backtrace is not
337338
/// pruned yet; that should be done before printing it.
338339
pub backtrace: Option<Vec<FrameInfo<'tcx>>>,
340+
/// Synchronization primitives like to attach extra data to particular addresses. We store that
341+
/// inside the relevant allocation, to ensure that everything is removed when the allocation is
342+
/// freed.
343+
/// This maps offsets to synchronization-primitive-specific data.
344+
pub sync: FxHashMap<Size, Box<dyn Any>>,
339345
}
340346

341347
// We need a `Clone` impl because the machine passes `Allocation` through `Cow`...
@@ -348,7 +354,7 @@ impl<'tcx> Clone for AllocExtra<'tcx> {
348354

349355
impl VisitProvenance for AllocExtra<'_> {
350356
fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
351-
let AllocExtra { borrow_tracker, data_race, weak_memory, backtrace: _ } = self;
357+
let AllocExtra { borrow_tracker, data_race, weak_memory, backtrace: _, sync: _ } = self;
352358

353359
borrow_tracker.visit_provenance(visit);
354360
data_race.visit_provenance(visit);
@@ -1187,7 +1193,13 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
11871193
.insert(id, (ecx.machine.current_span(), None));
11881194
}
11891195

1190-
interp_ok(AllocExtra { borrow_tracker, data_race, weak_memory, backtrace })
1196+
interp_ok(AllocExtra {
1197+
borrow_tracker,
1198+
data_race,
1199+
weak_memory,
1200+
backtrace,
1201+
sync: FxHashMap::default(),
1202+
})
11911203
}
11921204

11931205
fn adjust_alloc_root_pointer(

0 commit comments

Comments
 (0)