Skip to content

Commit 465dcf1

Browse files
committed
global allocations: don't make up a super-high VectorIdx, just use the main thread
1 parent 6b0ce8b commit 465dcf1

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

src/tools/miri/src/concurrency/data_race.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ impl VClockAlloc {
847847
kind: MemoryKind,
848848
current_span: Span,
849849
) -> VClockAlloc {
850+
// Determine the thread that did the allocation, and when it did it.
850851
let (alloc_timestamp, alloc_index) = match kind {
851852
// User allocated and stack memory should track allocation.
852853
MemoryKind::Machine(
@@ -864,15 +865,16 @@ impl VClockAlloc {
864865
(alloc_timestamp, alloc_index)
865866
}
866867
// Other global memory should trace races but be allocated at the 0 timestamp
867-
// (conceptually they are allocated before everything).
868+
// (conceptually they are allocated on the main thread before everything).
868869
MemoryKind::Machine(
869870
MiriMemoryKind::Global
870871
| MiriMemoryKind::Machine
871872
| MiriMemoryKind::Runtime
872873
| MiriMemoryKind::ExternStatic
873874
| MiriMemoryKind::Tls,
874875
)
875-
| MemoryKind::CallerLocation => (VTimestamp::ZERO, VectorIdx::MAX_INDEX),
876+
| MemoryKind::CallerLocation =>
877+
(VTimestamp::ZERO, global.thread_index(ThreadId::MAIN_THREAD)),
876878
};
877879
VClockAlloc {
878880
alloc_ranges: RefCell::new(RangeMap::new(
@@ -1454,7 +1456,7 @@ impl GlobalState {
14541456
// Setup the main-thread since it is not explicitly created:
14551457
// uses vector index and thread-id 0.
14561458
let index = global_state.vector_clocks.get_mut().push(ThreadClockSet::default());
1457-
global_state.vector_info.get_mut().push(ThreadId::new(0));
1459+
global_state.vector_info.get_mut().push(ThreadId::MAIN_THREAD);
14581460
global_state
14591461
.thread_info
14601462
.get_mut()
@@ -1725,13 +1727,15 @@ impl GlobalState {
17251727
Ref::map(clocks, |c| &c.clock)
17261728
}
17271729

1730+
fn thread_index(&self, thread: ThreadId) -> VectorIdx {
1731+
self.thread_info.borrow()[thread].vector_index.expect("thread has no assigned vector")
1732+
}
1733+
17281734
/// Load the vector index used by the given thread as well as the set of vector clocks
17291735
/// used by the thread.
17301736
#[inline]
17311737
fn thread_state_mut(&self, thread: ThreadId) -> (VectorIdx, RefMut<'_, ThreadClockSet>) {
1732-
let index = self.thread_info.borrow()[thread]
1733-
.vector_index
1734-
.expect("Loading thread state for thread with no assigned vector");
1738+
let index = self.thread_index(thread);
17351739
let ref_vector = self.vector_clocks.borrow_mut();
17361740
let clocks = RefMut::map(ref_vector, |vec| &mut vec[index]);
17371741
(index, clocks)
@@ -1741,9 +1745,7 @@ impl GlobalState {
17411745
/// used by the thread.
17421746
#[inline]
17431747
fn thread_state(&self, thread: ThreadId) -> (VectorIdx, Ref<'_, ThreadClockSet>) {
1744-
let index = self.thread_info.borrow()[thread]
1745-
.vector_index
1746-
.expect("Loading thread state for thread with no assigned vector");
1748+
let index = self.thread_index(thread);
17471749
let ref_vector = self.vector_clocks.borrow();
17481750
let clocks = Ref::map(ref_vector, |vec| &vec[index]);
17491751
(index, clocks)
@@ -1774,9 +1776,7 @@ impl GlobalState {
17741776
#[inline]
17751777
fn current_index(&self, thread_mgr: &ThreadManager<'_, '_>) -> VectorIdx {
17761778
let active_thread_id = thread_mgr.get_active_thread_id();
1777-
self.thread_info.borrow()[active_thread_id]
1778-
.vector_index
1779-
.expect("active thread has no assigned vector")
1779+
self.thread_index(active_thread_id)
17801780
}
17811781

17821782
// SC ATOMIC STORE rule in the paper.

src/tools/miri/src/concurrency/thread.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ impl ThreadId {
5757
pub fn to_u32(self) -> u32 {
5858
self.0
5959
}
60+
61+
pub const MAIN_THREAD: ThreadId = ThreadId(0);
6062
}
6163

6264
impl Idx for ThreadId {
@@ -401,7 +403,7 @@ impl<'mir, 'tcx> Default for ThreadManager<'mir, 'tcx> {
401403
// Create the main thread and add it to the list of threads.
402404
threads.push(Thread::new(Some("main"), None));
403405
Self {
404-
active_thread: ThreadId::new(0),
406+
active_thread: ThreadId::MAIN_THREAD,
405407
threads,
406408
sync: SynchronizationState::default(),
407409
thread_local_alloc_ids: Default::default(),
@@ -416,10 +418,12 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
416418
ecx: &mut MiriInterpCx<'mir, 'tcx>,
417419
on_main_stack_empty: StackEmptyCallback<'mir, 'tcx>,
418420
) {
419-
ecx.machine.threads.threads[ThreadId::new(0)].on_stack_empty = Some(on_main_stack_empty);
421+
ecx.machine.threads.threads[ThreadId::MAIN_THREAD].on_stack_empty =
422+
Some(on_main_stack_empty);
420423
if ecx.tcx.sess.target.os.as_ref() != "windows" {
421424
// The main thread can *not* be joined on except on windows.
422-
ecx.machine.threads.threads[ThreadId::new(0)].join_status = ThreadJoinStatus::Detached;
425+
ecx.machine.threads.threads[ThreadId::MAIN_THREAD].join_status =
426+
ThreadJoinStatus::Detached;
423427
}
424428
}
425429

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ use super::data_race::NaReadType;
1313
/// but in some cases one vector index may be shared with
1414
/// multiple thread ids if it's safe to do so.
1515
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
16-
pub struct VectorIdx(u32);
16+
pub(super) struct VectorIdx(u32);
1717

1818
impl VectorIdx {
1919
#[inline(always)]
20-
pub fn to_u32(self) -> u32 {
20+
fn to_u32(self) -> u32 {
2121
self.0
2222
}
23-
24-
pub const MAX_INDEX: VectorIdx = VectorIdx(u32::MAX);
2523
}
2624

2725
impl Idx for VectorIdx {

0 commit comments

Comments
 (0)