Skip to content

Commit cb1b4a6

Browse files
committed
reuse_pool: only do acquire_clock if we reused from a different thread
1 parent 7dcfb54 commit cb1b4a6

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

src/tools/miri/src/alloc_addresses/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
171171
memory_kind,
172172
ecx.get_active_thread(),
173173
) {
174-
if let Some(data_race) = &ecx.machine.data_race {
174+
if let Some(clock) = clock
175+
&& let Some(data_race) = &ecx.machine.data_race
176+
{
175177
data_race.acquire_clock(&clock, ecx.get_active_thread());
176178
}
177179
reuse_addr

src/tools/miri/src/alloc_addresses/reuse_pool.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ impl ReusePool {
7878
subpool.insert(pos, (addr, size, thread, clock));
7979
}
8080

81+
/// Returns the address to use and optionally a clock we have to synchronize with.
8182
pub fn take_addr(
8283
&mut self,
8384
rng: &mut impl Rng,
8485
size: Size,
8586
align: Align,
8687
kind: MemoryKind,
8788
thread: ThreadId,
88-
) -> Option<(u64, VClock)> {
89+
) -> Option<(u64, Option<VClock>)> {
8990
// Determine whether we'll even attempt a reuse. As above, we don't do reuse for stack addresses.
9091
if kind == MemoryKind::Stack || !rng.gen_bool(self.address_reuse_rate) {
9192
return None;
@@ -122,6 +123,7 @@ impl ReusePool {
122123
let (chosen_addr, chosen_size, chosen_thread, clock) = subpool.remove(idx);
123124
debug_assert!(chosen_size >= size && chosen_addr % align.bytes() == 0);
124125
debug_assert!(cross_thread_reuse || chosen_thread == thread);
125-
Some((chosen_addr, clock))
126+
// No synchronization needed if we reused from the current thread.
127+
Some((chosen_addr, if chosen_thread == thread { None } else { Some(clock) }))
126128
}
127129
}

src/tools/miri/src/concurrency/vector_clock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl VClock {
152152
}
153153

154154
/// Get a mutable slice to the internal vector with minimum `min_len`
155-
/// elements, to preserve invariants this vector must modify
155+
/// elements. To preserve invariants, the caller must modify
156156
/// the `min_len`-1 nth element to a non-zero value
157157
#[inline]
158158
fn get_mut_with_min_len(&mut self, min_len: usize) -> &mut [VTimestamp] {

0 commit comments

Comments
 (0)