Skip to main content

miri/concurrency/
data_race.rs

1//! Implementation of a data-race detector using Lamport Timestamps / Vector clocks
2//! based on the Dynamic Race Detection for C++:
3//! <https://www.doc.ic.ac.uk/~afd/homepages/papers/pdfs/2017/POPL.pdf>
4//! which does not report false-positives when fences are used, and gives better
5//! accuracy in presence of read-modify-write operations.
6//!
7//! The implementation contains modifications to correctly model the changes to the memory model in C++20
8//! regarding the weakening of release sequences: <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0982r1.html>.
9//! Relaxed stores now unconditionally block all currently active release sequences and so per-thread tracking of release
10//! sequences is not needed.
11//!
12//! The implementation also models races with memory allocation and deallocation via treating allocation and
13//! deallocation as a type of write internally for detecting data-races.
14//!
15//! Weak memory orders are explored but not all weak behaviours are exhibited, so it can still miss data-races
16//! but should not report false-positives
17//!
18//! Data-race definition from(<https://en.cppreference.com/w/cpp/language/memory_model#Threads_and_data_races>):
19//! a data race occurs between two memory accesses if they are on different threads, at least one operation
20//! is non-atomic, at least one operation is a write and neither access happens-before the other. Read the link
21//! for full definition.
22//!
23//! This re-uses vector indexes for threads that are known to be unable to report data-races, this is valid
24//! because it only re-uses vector indexes once all currently-active (not-terminated) threads have an internal
25//! vector clock that happens-after the join operation of the candidate thread. Threads that have not been joined
26//! on are not considered. Since the thread's vector clock will only increase and a data-race implies that
27//! there is some index x where `clock[x] > thread_clock`, when this is true `clock[candidate-idx] > thread_clock`
28//! can never hold and hence a data-race can never be reported in that vector index again.
29//! This means that the thread-index can be safely re-used, starting on the next timestamp for the newly created
30//! thread.
31//!
32//! The timestamps used in the data-race detector assign each sequence of non-atomic operations
33//! followed by a single atomic or concurrent operation a single timestamp.
34//! Write, Read, Write, ThreadJoin will be represented by a single timestamp value on a thread.
35//! This is because extra increment operations between the operations in the sequence are not
36//! required for accurate reporting of data-race values.
37//!
38//! As per the paper a threads timestamp is only incremented after a release operation is performed
39//! so some atomic operations that only perform acquires do not increment the timestamp. Due to shared
40//! code some atomic operations may increment the timestamp when not necessary but this has no effect
41//! on the data-race detection code.
42
43use std::cell::{Cell, Ref, RefCell, RefMut};
44use std::fmt::Debug;
45use std::mem;
46
47use rand::RngExt;
48use rustc_abi::{Align, HasDataLayout, Size};
49use rustc_ast::Mutability;
50use rustc_data_structures::fx::{FxHashMap, FxHashSet};
51use rustc_index::{Idx, IndexVec};
52use rustc_log::tracing;
53use rustc_middle::mir;
54use rustc_middle::ty::Ty;
55use rustc_span::Span;
56
57use super::vector_clock::{VClock, VTimestamp, VectorIdx};
58use super::weak_memory::EvalContextExt as _;
59use crate::concurrency::GlobalDataRaceHandler;
60use crate::diagnostics::RacingOp;
61use crate::intrinsics::AtomicRmwOp;
62use crate::*;
63
64pub type AllocState = VClockAlloc;
65
66/// Valid atomic read-write orderings, alias of atomic::Ordering (not non-exhaustive).
67#[derive(Copy, Clone, PartialEq, Eq, Debug)]
68pub enum AtomicRwOrd {
69    Relaxed,
70    Acquire,
71    Release,
72    AcqRel,
73    SeqCst,
74}
75
76/// Valid atomic read orderings, subset of atomic::Ordering.
77#[derive(Copy, Clone, PartialEq, Eq, Debug)]
78pub enum AtomicReadOrd {
79    Relaxed,
80    Acquire,
81    SeqCst,
82}
83
84/// Valid atomic write orderings, subset of atomic::Ordering.
85#[derive(Copy, Clone, PartialEq, Eq, Debug)]
86pub enum AtomicWriteOrd {
87    Relaxed,
88    Release,
89    SeqCst,
90}
91
92/// Valid atomic fence orderings, subset of atomic::Ordering.
93#[derive(Copy, Clone, PartialEq, Eq, Debug)]
94pub enum AtomicFenceOrd {
95    Acquire,
96    Release,
97    AcqRel,
98    SeqCst,
99}
100
101/// The current set of vector clocks describing the state
102/// of a thread, contains the happens-before clock and
103/// additional metadata to model atomic fence operations.
104#[derive(Clone, Default, Debug)]
105pub(super) struct ThreadClockSet {
106    /// The increasing clock representing timestamps
107    /// that happen-before this thread.
108    pub(super) clock: VClock,
109
110    /// The set of timestamps that will happen-before this
111    /// thread once it performs an acquire fence.
112    fence_acquire: VClock,
113
114    /// The last timestamp of happens-before relations that
115    /// have been released by this thread by a release fence.
116    fence_release: VClock,
117
118    /// Timestamps of the last SC write performed by each
119    /// thread, updated when this thread performs an SC fence.
120    /// This is never acquired into the thread's clock, it
121    /// just limits which old writes can be seen in weak memory emulation.
122    pub(super) write_seqcst: VClock,
123
124    /// Timestamps of the last SC fence performed by each
125    /// thread, updated when this thread performs an SC read.
126    /// This is never acquired into the thread's clock, it
127    /// just limits which old writes can be seen in weak memory emulation.
128    pub(super) read_seqcst: VClock,
129}
130
131impl ThreadClockSet {
132    /// Apply the effects of a release fence to this
133    /// set of thread vector clocks.
134    #[inline]
135    fn apply_release_fence(&mut self) {
136        self.fence_release.clone_from(&self.clock);
137    }
138
139    /// Apply the effects of an acquire fence to this
140    /// set of thread vector clocks.
141    #[inline]
142    fn apply_acquire_fence(&mut self) {
143        self.clock.join(&self.fence_acquire);
144    }
145
146    /// Increment the happens-before clock at a
147    /// known index.
148    #[inline]
149    fn increment_clock(&mut self, index: VectorIdx, current_span: Span) {
150        self.clock.increment_index(index, current_span);
151    }
152
153    /// Join the happens-before clock with that of
154    /// another thread, used to model thread join
155    /// operations.
156    fn join_with(&mut self, other: &ThreadClockSet) {
157        self.clock.join(&other.clock);
158    }
159}
160
161/// Error returned by finding a data race
162/// should be elaborated upon.
163#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
164pub struct DataRace;
165
166/// Externally stored memory cell clocks
167/// explicitly to reduce memory usage for the
168/// common case where no atomic operations
169/// exists on the memory cell.
170#[derive(Clone, PartialEq, Eq, Debug)]
171struct AtomicMemoryCellClocks {
172    /// The clock-vector of the timestamp of the last atomic
173    /// read operation performed by each thread.
174    /// This detects potential data-races between atomic read
175    /// and non-atomic write operations.
176    read_vector: VClock,
177
178    /// The clock-vector of the timestamp of the last atomic
179    /// write operation performed by each thread.
180    /// This detects potential data-races between atomic write
181    /// and non-atomic read or write operations.
182    write_vector: VClock,
183
184    /// Synchronization vector for acquire-release semantics
185    /// contains the vector of timestamps that will
186    /// happen-before a thread if an acquire-load is
187    /// performed on the data.
188    ///
189    /// With weak memory emulation, this is the clock of the most recent write. It is then only used
190    /// for release sequences, to integrate the most recent clock into the next one for RMWs.
191    sync_vector: VClock,
192
193    /// The size of accesses to this atomic location.
194    /// We use this to detect non-synchronized mixed-size accesses. Since all accesses must be
195    /// aligned to their size, this is sufficient to detect imperfectly overlapping accesses.
196    /// `None` indicates that we saw multiple different sizes, which is okay as long as all accesses are reads.
197    size: Option<Size>,
198}
199
200#[derive(Copy, Clone, PartialEq, Eq, Debug)]
201enum AtomicAccessType {
202    Load(AtomicReadOrd),
203    Store,
204    Rmw,
205}
206
207/// Type of a non-atomic read operation.
208#[derive(Copy, Clone, PartialEq, Eq, Debug)]
209pub enum NaReadType {
210    /// Standard unsynchronized write.
211    Read,
212
213    // An implicit read generated by a retag.
214    Retag,
215}
216
217impl NaReadType {
218    fn description(self) -> &'static str {
219        match self {
220            NaReadType::Read => "non-atomic read",
221            NaReadType::Retag => "retag read",
222        }
223    }
224}
225
226/// Type of a non-atomic write operation: allocating memory, non-atomic writes, and
227/// deallocating memory are all treated as writes for the purpose of the data-race detector.
228#[derive(Copy, Clone, PartialEq, Eq, Debug)]
229pub enum NaWriteType {
230    /// Allocate memory.
231    Allocate,
232
233    /// Standard unsynchronized write.
234    Write,
235
236    // An implicit write generated by a retag.
237    Retag,
238
239    /// Deallocate memory.
240    /// Note that when memory is deallocated first, later non-atomic accesses
241    /// will be reported as use-after-free, not as data races.
242    /// (Same for `Allocate` above.)
243    Deallocate,
244}
245
246impl NaWriteType {
247    fn description(self) -> &'static str {
248        match self {
249            NaWriteType::Allocate => "creating a new allocation",
250            NaWriteType::Write => "non-atomic write",
251            NaWriteType::Retag => "retag write",
252            NaWriteType::Deallocate => "deallocation",
253        }
254    }
255}
256
257#[derive(Copy, Clone, PartialEq, Eq, Debug)]
258enum AccessType {
259    NaRead(NaReadType),
260    NaWrite(NaWriteType),
261    AtomicLoad,
262    AtomicStore,
263    AtomicRmw,
264}
265
266/// Per-byte vector clock metadata for data-race detection.
267#[derive(Clone, PartialEq, Eq, Debug)]
268struct MemoryCellClocks {
269    /// The vector clock timestamp and the thread that did the last non-atomic write. We don't need
270    /// a full `VClock` here, it's always a single thread and nothing synchronizes, so the effective
271    /// clock is all-0 except for the thread that did the write.
272    write: (VectorIdx, VTimestamp),
273
274    /// The type of operation that the write index represents,
275    /// either newly allocated memory, a non-atomic write or
276    /// a deallocation of memory.
277    write_type: NaWriteType,
278
279    /// The vector clock of all non-atomic reads that happened since the last non-atomic write
280    /// (i.e., we join together the "singleton" clocks corresponding to each read). It is reset to
281    /// zero on each write operation.
282    read: VClock,
283
284    /// Atomic access tracking clocks.
285    /// For non-atomic memory this value is set to None.
286    /// For atomic memory, each byte carries this information.
287    atomic_ops: Option<Box<AtomicMemoryCellClocks>>,
288}
289
290/// Extra metadata associated with a thread.
291#[derive(Debug, Clone, Default)]
292struct ThreadExtraState {
293    /// The current vector index in use by the
294    /// thread currently, this is set to None
295    /// after the vector index has been re-used
296    /// and hence the value will never need to be
297    /// read during data-race reporting.
298    vector_index: Option<VectorIdx>,
299
300    /// Thread termination vector clock, this
301    /// is set on thread termination and is used
302    /// for joining on threads since the vector_index
303    /// may be re-used when the join operation occurs.
304    termination_vector_clock: Option<VClock>,
305}
306
307/// Global data-race detection state, contains the currently
308/// executing thread as well as the vector clocks associated
309/// with each of the threads.
310// FIXME: it is probably better to have one large RefCell, than to have so many small ones.
311#[derive(Debug, Clone)]
312pub struct GlobalState {
313    /// Set to true once the first additional
314    /// thread has launched, due to the dependency
315    /// between before and after a thread launch.
316    /// Any data-races must be recorded after this
317    /// so concurrent execution can ignore recording
318    /// any data-races.
319    multi_threaded: Cell<bool>,
320
321    /// A flag to mark we are currently performing
322    /// a data race free action (such as atomic access)
323    /// to suppress the race detector
324    ongoing_action_data_race_free: Cell<bool>,
325
326    /// Mapping of a vector index to a known set of thread
327    /// clocks, this is not directly mapping from a thread id
328    /// since it may refer to multiple threads.
329    vector_clocks: RefCell<IndexVec<VectorIdx, ThreadClockSet>>,
330
331    /// Mapping of a given vector index to the current thread
332    /// that the execution is representing, this may change
333    /// if a vector index is re-assigned to a new thread.
334    vector_info: RefCell<IndexVec<VectorIdx, ThreadId>>,
335
336    /// The mapping of a given thread to associated thread metadata.
337    thread_info: RefCell<IndexVec<ThreadId, ThreadExtraState>>,
338
339    /// Potential vector indices that could be re-used on thread creation
340    /// values are inserted here on after the thread has terminated and
341    /// been joined with, and hence may potentially become free
342    /// for use as the index for a new thread.
343    /// Elements in this set may still require the vector index to
344    /// report data-races, and can only be re-used after all
345    /// active vector clocks catch up with the threads timestamp.
346    reuse_candidates: RefCell<FxHashSet<VectorIdx>>,
347
348    /// We make SC fences act like RMWs on a global location.
349    /// To implement that, they all release and acquire into this clock.
350    last_sc_fence: RefCell<VClock>,
351
352    /// The timestamp of last SC write performed by each thread.
353    /// Threads only update their own index here!
354    last_sc_write_per_thread: RefCell<VClock>,
355
356    /// Track when an outdated (weak memory) load happens.
357    pub track_outdated_loads: bool,
358
359    /// Whether weak memory emulation is enabled
360    pub weak_memory: bool,
361}
362
363impl VisitProvenance for GlobalState {
364    fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
365        // We don't have any tags.
366    }
367}
368
369impl AccessType {
370    fn description(self, ty: Option<Ty<'_>>, size: Option<Size>) -> String {
371        let mut msg = String::new();
372
373        if let Some(size) = size {
374            if size == Size::ZERO {
375                // In this case there were multiple read accesses with different sizes and then a write.
376                // We will be reporting *one* of the other reads, but we don't have enough information
377                // to determine which one had which size.
378                assert!(self == AccessType::AtomicLoad);
379                assert!(ty.is_none());
380                return format!("multiple differently-sized atomic loads, including one load");
381            }
382            msg.push_str(&format!("{}-byte {}", size.bytes(), msg))
383        }
384
385        msg.push_str(match self {
386            AccessType::NaRead(w) => w.description(),
387            AccessType::NaWrite(w) => w.description(),
388            AccessType::AtomicLoad => "atomic load",
389            AccessType::AtomicStore => "atomic store",
390            AccessType::AtomicRmw => "atomic read-modify-write",
391        });
392
393        if let Some(ty) = ty {
394            msg.push_str(&format!(" of type `{ty}`"));
395        }
396
397        msg
398    }
399
400    fn is_atomic(self) -> bool {
401        match self {
402            AccessType::AtomicLoad | AccessType::AtomicStore | AccessType::AtomicRmw => true,
403            AccessType::NaRead(_) | AccessType::NaWrite(_) => false,
404        }
405    }
406
407    fn is_read(self) -> bool {
408        match self {
409            AccessType::AtomicLoad | AccessType::NaRead(_) => true,
410            AccessType::NaWrite(_) | AccessType::AtomicStore | AccessType::AtomicRmw => false,
411        }
412    }
413
414    fn is_retag(self) -> bool {
415        matches!(
416            self,
417            AccessType::NaRead(NaReadType::Retag) | AccessType::NaWrite(NaWriteType::Retag)
418        )
419    }
420}
421
422impl AtomicMemoryCellClocks {
423    fn new(size: Size) -> Self {
424        AtomicMemoryCellClocks {
425            read_vector: Default::default(),
426            write_vector: Default::default(),
427            sync_vector: Default::default(),
428            size: Some(size),
429        }
430    }
431}
432
433impl MemoryCellClocks {
434    /// Create a new set of clocks representing memory allocated
435    ///  at a given vector timestamp and index.
436    fn new(alloc: VTimestamp, alloc_index: VectorIdx) -> Self {
437        MemoryCellClocks {
438            read: VClock::default(),
439            write: (alloc_index, alloc),
440            write_type: NaWriteType::Allocate,
441            atomic_ops: None,
442        }
443    }
444
445    #[inline]
446    fn write_was_before(&self, other: &VClock) -> bool {
447        // This is the same as `self.write() <= other` but
448        // without actually manifesting a clock for `self.write`.
449        self.write.1 <= other[self.write.0]
450    }
451
452    #[inline]
453    fn write(&self) -> VClock {
454        VClock::new_with_index(self.write.0, self.write.1)
455    }
456
457    /// Load the internal atomic memory cells if they exist.
458    #[inline]
459    fn atomic(&self) -> Option<&AtomicMemoryCellClocks> {
460        self.atomic_ops.as_deref()
461    }
462
463    /// Load the internal atomic memory cells if they exist.
464    #[inline]
465    fn atomic_mut_unwrap(&mut self) -> &mut AtomicMemoryCellClocks {
466        self.atomic_ops.as_deref_mut().unwrap()
467    }
468
469    /// Load or create the internal atomic memory metadata if it does not exist. Also ensures we do
470    /// not do mixed-size atomic accesses, and updates the recorded atomic access size.
471    fn atomic_access(
472        &mut self,
473        thread_clocks: &ThreadClockSet,
474        size: Size,
475        write: bool,
476    ) -> Result<&mut AtomicMemoryCellClocks, DataRace> {
477        match self.atomic_ops {
478            Some(ref mut atomic) => {
479                // We are good if the size is the same or all atomic accesses are before our current time.
480                if atomic.size == Some(size) {
481                    Ok(atomic)
482                } else if atomic.read_vector <= thread_clocks.clock
483                    && atomic.write_vector <= thread_clocks.clock
484                {
485                    // We are fully ordered after all previous accesses, so we can change the size.
486                    atomic.size = Some(size);
487                    Ok(atomic)
488                } else if !write && atomic.write_vector <= thread_clocks.clock {
489                    // This is a read, and it is ordered after the last write. It's okay for the
490                    // sizes to mismatch, as long as no writes with a different size occur later.
491                    atomic.size = None;
492                    Ok(atomic)
493                } else {
494                    Err(DataRace)
495                }
496            }
497            None => {
498                self.atomic_ops = Some(Box::new(AtomicMemoryCellClocks::new(size)));
499                Ok(self.atomic_ops.as_mut().unwrap())
500            }
501        }
502    }
503
504    /// Update memory cell data-race tracking for atomic
505    /// load acquire semantics, is a no-op if this memory was
506    /// not used previously as atomic memory.
507    fn load_acquire(
508        &mut self,
509        thread_clocks: &mut ThreadClockSet,
510        index: VectorIdx,
511        access_size: Size,
512        sync_clock: Option<&VClock>,
513    ) -> Result<(), DataRace> {
514        self.atomic_read_detect(thread_clocks, index, access_size)?;
515        if let Some(sync_clock) = sync_clock.or_else(|| self.atomic().map(|a| &a.sync_vector)) {
516            thread_clocks.clock.join(sync_clock);
517        }
518        Ok(())
519    }
520
521    /// Update memory cell data-race tracking for atomic
522    /// load relaxed semantics, is a no-op if this memory was
523    /// not used previously as atomic memory.
524    fn load_relaxed(
525        &mut self,
526        thread_clocks: &mut ThreadClockSet,
527        index: VectorIdx,
528        access_size: Size,
529        sync_clock: Option<&VClock>,
530    ) -> Result<(), DataRace> {
531        self.atomic_read_detect(thread_clocks, index, access_size)?;
532        if let Some(sync_clock) = sync_clock.or_else(|| self.atomic().map(|a| &a.sync_vector)) {
533            thread_clocks.fence_acquire.join(sync_clock);
534        }
535        Ok(())
536    }
537
538    /// Update the memory cell data-race tracking for atomic
539    /// store release semantics.
540    fn store_release(
541        &mut self,
542        thread_clocks: &ThreadClockSet,
543        index: VectorIdx,
544        access_size: Size,
545    ) -> Result<(), DataRace> {
546        self.atomic_write_detect(thread_clocks, index, access_size)?;
547        let atomic = self.atomic_mut_unwrap(); // initialized by `atomic_write_detect`
548        atomic.sync_vector.clone_from(&thread_clocks.clock);
549        Ok(())
550    }
551
552    /// Update the memory cell data-race tracking for atomic
553    /// store relaxed semantics.
554    fn store_relaxed(
555        &mut self,
556        thread_clocks: &ThreadClockSet,
557        index: VectorIdx,
558        access_size: Size,
559    ) -> Result<(), DataRace> {
560        self.atomic_write_detect(thread_clocks, index, access_size)?;
561
562        // The handling of release sequences was changed in C++20 and so
563        // the code here is different to the paper since now all relaxed
564        // stores block release sequences. The exception for same-thread
565        // relaxed stores has been removed. We always overwrite the `sync_vector`,
566        // meaning the previous release sequence is broken.
567        let atomic = self.atomic_mut_unwrap();
568        atomic.sync_vector.clone_from(&thread_clocks.fence_release);
569        Ok(())
570    }
571
572    /// Update the memory cell data-race tracking for atomic
573    /// store release semantics for RMW operations.
574    fn rmw_release(
575        &mut self,
576        thread_clocks: &ThreadClockSet,
577        index: VectorIdx,
578        access_size: Size,
579    ) -> Result<(), DataRace> {
580        self.atomic_write_detect(thread_clocks, index, access_size)?;
581        let atomic = self.atomic_mut_unwrap();
582        // This *joining* of `sync_vector` implements release sequences: future
583        // reads of this location will acquire our clock *and* what was here before.
584        atomic.sync_vector.join(&thread_clocks.clock);
585        Ok(())
586    }
587
588    /// Update the memory cell data-race tracking for atomic
589    /// store relaxed semantics for RMW operations.
590    fn rmw_relaxed(
591        &mut self,
592        thread_clocks: &ThreadClockSet,
593        index: VectorIdx,
594        access_size: Size,
595    ) -> Result<(), DataRace> {
596        self.atomic_write_detect(thread_clocks, index, access_size)?;
597        let atomic = self.atomic_mut_unwrap();
598        // This *joining* of `sync_vector` implements release sequences: future
599        // reads of this location will acquire our fence clock *and* what was here before.
600        atomic.sync_vector.join(&thread_clocks.fence_release);
601        Ok(())
602    }
603
604    /// Detect data-races with an atomic read, caused by a non-atomic write that does
605    /// not happen-before the atomic-read.
606    fn atomic_read_detect(
607        &mut self,
608        thread_clocks: &ThreadClockSet,
609        index: VectorIdx,
610        access_size: Size,
611    ) -> Result<(), DataRace> {
612        trace!("Atomic read with vectors: {:#?} :: {:#?}", self, thread_clocks);
613        let atomic = self.atomic_access(thread_clocks, access_size, /*write*/ false)?;
614        atomic.read_vector.set_at_index(&thread_clocks.clock, index);
615        // Make sure the last non-atomic write was before this access.
616        if self.write_was_before(&thread_clocks.clock) { Ok(()) } else { Err(DataRace) }
617    }
618
619    /// Detect data-races with an atomic write, either with a non-atomic read or with
620    /// a non-atomic write.
621    fn atomic_write_detect(
622        &mut self,
623        thread_clocks: &ThreadClockSet,
624        index: VectorIdx,
625        access_size: Size,
626    ) -> Result<(), DataRace> {
627        trace!("Atomic write with vectors: {:#?} :: {:#?}", self, thread_clocks);
628        let atomic = self.atomic_access(thread_clocks, access_size, /*write*/ true)?;
629        atomic.write_vector.set_at_index(&thread_clocks.clock, index);
630        // Make sure the last non-atomic write and all non-atomic reads were before this access.
631        if self.write_was_before(&thread_clocks.clock) && self.read <= thread_clocks.clock {
632            Ok(())
633        } else {
634            Err(DataRace)
635        }
636    }
637
638    /// Detect races for non-atomic read operations at the current memory cell
639    /// returns true if a data-race is detected.
640    fn read_race_detect(
641        &mut self,
642        thread_clocks: &mut ThreadClockSet,
643        index: VectorIdx,
644        read_type: NaReadType,
645        current_span: Span,
646    ) -> Result<(), DataRace> {
647        trace!("Unsynchronized read with vectors: {:#?} :: {:#?}", self, thread_clocks);
648        if !current_span.is_dummy() {
649            thread_clocks.clock.index_mut(index).span = current_span;
650        }
651        thread_clocks.clock.index_mut(index).set_read_type(read_type);
652        // Check synchronization with non-atomic writes.
653        if !self.write_was_before(&thread_clocks.clock) {
654            return Err(DataRace);
655        }
656        // Check synchronization with atomic writes.
657        if !self.atomic().is_none_or(|atomic| atomic.write_vector <= thread_clocks.clock) {
658            return Err(DataRace);
659        }
660        // Record this access.
661        self.read.set_at_index(&thread_clocks.clock, index);
662        Ok(())
663    }
664
665    /// Detect races for non-atomic write operations at the current memory cell
666    /// returns true if a data-race is detected.
667    fn write_race_detect(
668        &mut self,
669        thread_clocks: &mut ThreadClockSet,
670        index: VectorIdx,
671        write_type: NaWriteType,
672        current_span: Span,
673    ) -> Result<(), DataRace> {
674        trace!("Unsynchronized write with vectors: {:#?} :: {:#?}", self, thread_clocks);
675        if !current_span.is_dummy() {
676            thread_clocks.clock.index_mut(index).span = current_span;
677        }
678        // Check synchronization with non-atomic accesses.
679        if !(self.write_was_before(&thread_clocks.clock) && self.read <= thread_clocks.clock) {
680            return Err(DataRace);
681        }
682        // Check synchronization with atomic accesses.
683        if !self.atomic().is_none_or(|atomic| {
684            atomic.write_vector <= thread_clocks.clock && atomic.read_vector <= thread_clocks.clock
685        }) {
686            return Err(DataRace);
687        }
688        // Record this access.
689        self.write = (index, thread_clocks.clock[index]);
690        self.write_type = write_type;
691        self.read.set_zero_vector();
692        // This is not an atomic location any more.
693        self.atomic_ops = None;
694        Ok(())
695    }
696}
697
698impl GlobalDataRaceHandler {
699    /// Select whether data race checking is disabled. This is solely an
700    /// implementation detail of `allow_data_races_*` and must not be used anywhere else!
701    fn set_ongoing_action_data_race_free(&self, enable: bool) {
702        match self {
703            GlobalDataRaceHandler::None => {}
704            GlobalDataRaceHandler::Vclocks(data_race) => {
705                let old = data_race.ongoing_action_data_race_free.replace(enable);
706                assert_ne!(old, enable, "cannot nest allow_data_races");
707            }
708            GlobalDataRaceHandler::Genmc(genmc_ctx) => {
709                genmc_ctx.set_ongoing_action_data_race_free(enable);
710            }
711        }
712    }
713}
714
715/// Evaluation context extensions.
716impl<'tcx> EvalContextExt<'tcx> for MiriInterpCx<'tcx> {}
717pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> {
718    /// Perform an atomic read operation at the memory location.
719    fn read_scalar_atomic(
720        &self,
721        place: &MPlaceTy<'tcx>,
722        atomic: AtomicReadOrd,
723    ) -> InterpResult<'tcx, Scalar> {
724        let this = self.eval_context_ref();
725        this.atomic_access_check(place, AtomicAccessType::Load(atomic))?;
726        // This will read from the last store in the modification order of this location. In case
727        // weak memory emulation is enabled, this may not be the store we will pick to actually read from and return.
728        // This is fine with StackedBorrow and race checks because they don't concern metadata on
729        // the *value* (including the associated provenance if this is an AtomicPtr) at this location.
730        // Only metadata on the location itself is used.
731
732        if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
733            let old_val = this.run_for_validation_ref(|this| this.read_scalar(place)).discard_err();
734            return genmc_ctx.atomic_load(
735                this,
736                place.ptr().addr(),
737                place.layout.size,
738                atomic,
739                old_val,
740            );
741        }
742
743        let scalar = this.allow_data_races_ref(move |this| this.read_scalar(place))?;
744        let buffered_scalar = this.buffered_atomic_read(place, atomic, scalar, |sync_clock| {
745            this.validate_atomic_load(place, atomic, sync_clock)
746        })?;
747        interp_ok(buffered_scalar.ok_or_else(|| err_ub!(InvalidUninitBytes(None)))?)
748    }
749
750    /// Perform an atomic write operation at the memory location.
751    fn write_scalar_atomic(
752        &mut self,
753        val: Scalar,
754        dest: &MPlaceTy<'tcx>,
755        atomic: AtomicWriteOrd,
756    ) -> InterpResult<'tcx> {
757        let this = self.eval_context_mut();
758        this.atomic_access_check(dest, AtomicAccessType::Store)?;
759
760        // Inform GenMC about the atomic store.
761        if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
762            let old_val = this.run_for_validation_ref(|this| this.read_scalar(dest)).discard_err();
763            if genmc_ctx.atomic_store(
764                this,
765                dest.ptr().addr(),
766                dest.layout.size,
767                val,
768                old_val,
769                atomic,
770            )? {
771                // The store might be the latest store in coherence order (determined by GenMC).
772                // If it is, we need to update the value in Miri's memory:
773                this.allow_data_races_mut(|this| this.write_scalar(val, dest))?;
774            }
775            return interp_ok(());
776        }
777
778        // Read the previous value so we can put it in the store buffer later.
779        let old_val = this.get_latest_nonatomic_val(dest);
780        this.allow_data_races_mut(move |this| this.write_scalar(val, dest))?;
781        this.validate_atomic_store(dest, atomic)?;
782        this.buffered_atomic_write(val, dest, atomic, old_val)
783    }
784
785    /// Perform an atomic RMW operation on a memory location.
786    fn atomic_rmw_op_immediate(
787        &mut self,
788        place: &MPlaceTy<'tcx>,
789        rhs: &ImmTy<'tcx>,
790        atomic_op: AtomicRmwOp,
791        ord: AtomicRwOrd,
792    ) -> InterpResult<'tcx, ImmTy<'tcx>> {
793        let this = self.eval_context_mut();
794        this.atomic_access_check(place, AtomicAccessType::Rmw)?;
795
796        let old = this.allow_data_races_mut(|this| this.read_immediate(place))?;
797
798        // Inform GenMC about the atomic rmw operation.
799        if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
800            let (old_val, new_val) = genmc_ctx.atomic_rmw_op(
801                this,
802                place.ptr().addr(),
803                place.layout.size,
804                atomic_op,
805                place.layout.backend_repr.is_signed(),
806                ord,
807                rhs.to_scalar(),
808                old.to_scalar(),
809            )?;
810            if let Some(new_val) = new_val {
811                this.allow_data_races_mut(|this| this.write_scalar(new_val, place))?;
812            }
813            return interp_ok(ImmTy::from_scalar(old_val, old.layout));
814        }
815
816        let val = match atomic_op {
817            AtomicRmwOp::MirOp { op, neg } => {
818                let val = this.binary_op(op, &old, rhs)?;
819                if neg { this.unary_op(mir::UnOp::Not, &val)? } else { val }
820            }
821            AtomicRmwOp::Max => {
822                let lt = this.binary_op(mir::BinOp::Lt, &old, rhs)?.to_scalar().to_bool()?;
823                if lt { rhs } else { &old }.clone()
824            }
825            AtomicRmwOp::Min => {
826                let lt = this.binary_op(mir::BinOp::Lt, &old, rhs)?.to_scalar().to_bool()?;
827                if lt { &old } else { rhs }.clone()
828            }
829        };
830
831        this.allow_data_races_mut(|this| this.write_immediate(*val, place))?;
832
833        this.validate_atomic_rmw(place, ord)?;
834
835        this.buffered_atomic_rmw(val.to_scalar(), place, ord, old.to_scalar())?;
836        interp_ok(old)
837    }
838
839    /// Perform an atomic exchange with a memory place and a new
840    /// scalar value, the old value is returned.
841    fn atomic_exchange_scalar(
842        &mut self,
843        place: &MPlaceTy<'tcx>,
844        new: Scalar,
845        atomic: AtomicRwOrd,
846    ) -> InterpResult<'tcx, Scalar> {
847        let this = self.eval_context_mut();
848        this.atomic_access_check(place, AtomicAccessType::Rmw)?;
849
850        let old = this.allow_data_races_mut(|this| this.read_scalar(place))?;
851        this.allow_data_races_mut(|this| this.write_scalar(new, place))?;
852
853        // Inform GenMC about the atomic atomic exchange.
854        if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
855            let (old_val, new_val) = genmc_ctx.atomic_exchange(
856                this,
857                place.ptr().addr(),
858                place.layout.size,
859                new,
860                atomic,
861                old,
862            )?;
863            // The store might be the latest store in coherence order (determined by GenMC).
864            // If it is, we need to update the value in Miri's memory:
865            if let Some(new_val) = new_val {
866                this.allow_data_races_mut(|this| this.write_scalar(new_val, place))?;
867            }
868            return interp_ok(old_val);
869        }
870
871        this.validate_atomic_rmw(place, atomic)?;
872
873        this.buffered_atomic_rmw(new, place, atomic, old)?;
874        interp_ok(old)
875    }
876
877    /// Perform an atomic compare and exchange at a given memory location.
878    /// On success an atomic RMW operation is performed and on failure
879    /// only an atomic read occurs. If `can_fail_spuriously` is true,
880    /// then we treat it as a "compare_exchange_weak" operation, and
881    /// some portion of the time fail even when the values are actually
882    /// identical.
883    fn atomic_compare_exchange_scalar(
884        &mut self,
885        place: &MPlaceTy<'tcx>,
886        expect_old: &ImmTy<'tcx>,
887        new: Scalar,
888        success: AtomicRwOrd,
889        fail: AtomicReadOrd,
890        can_fail_spuriously: bool,
891    ) -> InterpResult<'tcx, Immediate<Provenance>> {
892        let this = self.eval_context_mut();
893        this.atomic_access_check(place, AtomicAccessType::Rmw)?;
894
895        // Read as immediate for the sake of `binary_op()`
896        let old = this.allow_data_races_mut(|this| this.read_immediate(place))?;
897
898        // Inform GenMC about the atomic atomic compare exchange.
899        if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
900            let (old_value, new_value, cmpxchg_success) = genmc_ctx.atomic_compare_exchange(
901                this,
902                place.ptr().addr(),
903                place.layout.size,
904                this.read_scalar(expect_old)?,
905                new,
906                success,
907                fail,
908                can_fail_spuriously,
909                old.to_scalar(),
910            )?;
911            // The store might be the latest store in coherence order (determined by GenMC).
912            // If it is, we need to update the value in Miri's memory:
913            if let Some(new_value) = new_value {
914                this.allow_data_races_mut(|this| this.write_scalar(new_value, place))?;
915            }
916            return interp_ok(Immediate::ScalarPair(old_value, Scalar::from_bool(cmpxchg_success)));
917        }
918
919        // `binary_op` will bail if either of them is not a scalar.
920        let eq = this.binary_op(mir::BinOp::Eq, &old, expect_old)?;
921        // If the operation would succeed, but is "weak", fail some portion
922        // of the time, based on `success_rate`.
923        let success_rate = 1.0 - this.machine.cmpxchg_weak_failure_rate;
924        let cmpxchg_success = eq.to_scalar().to_bool()?
925            && if can_fail_spuriously {
926                this.machine.rng.get_mut().random_bool(success_rate)
927            } else {
928                true
929            };
930        let res = Immediate::ScalarPair(old.to_scalar(), Scalar::from_bool(cmpxchg_success));
931
932        // Update ptr depending on comparison.
933        // if successful, perform a full rw-atomic validation
934        // otherwise treat this as an atomic load with the fail ordering.
935        if cmpxchg_success {
936            this.allow_data_races_mut(|this| this.write_scalar(new, place))?;
937            this.validate_atomic_rmw(place, success)?;
938            this.buffered_atomic_rmw(new, place, success, old.to_scalar())?;
939        } else {
940            this.validate_atomic_load(place, fail, /* can use latest sync clock */ None)?;
941            // A failed compare exchange is equivalent to a load, reading from the latest store
942            // in the modification order.
943            // Since `old` is only a value and not the store element, we need to separately
944            // find it in our store buffer and perform load_impl on it.
945            this.perform_read_on_buffered_latest(place, fail)?;
946        }
947
948        // Return the old value.
949        interp_ok(res)
950    }
951
952    /// Update the data-race detector for an atomic fence on the current thread.
953    fn atomic_fence(&mut self, atomic: AtomicFenceOrd) -> InterpResult<'tcx> {
954        let this = self.eval_context_mut();
955        let machine = &this.machine;
956        match &this.machine.data_race {
957            GlobalDataRaceHandler::None => interp_ok(()),
958            GlobalDataRaceHandler::Vclocks(data_race) => data_race.atomic_fence(machine, atomic),
959            GlobalDataRaceHandler::Genmc(genmc_ctx) => genmc_ctx.atomic_fence(machine, atomic),
960        }
961    }
962
963    /// Calls the callback with the "release" clock of the current thread.
964    /// Other threads can acquire this clock in the future to establish synchronization
965    /// with this program point.
966    ///
967    /// The closure will only be invoked if data race handling is on.
968    fn release_clock<R>(
969        &self,
970        callback: impl FnOnce(&VClock) -> R,
971    ) -> InterpResult<'tcx, Option<R>> {
972        let this = self.eval_context_ref();
973        interp_ok(match &this.machine.data_race {
974            GlobalDataRaceHandler::None => None,
975            GlobalDataRaceHandler::Genmc(_genmc_ctx) =>
976                throw_unsup_format!(
977                    "this operation performs synchronization that is not supported in GenMC mode"
978                ),
979            GlobalDataRaceHandler::Vclocks(data_race) =>
980                Some(data_race.release_clock(&this.machine.threads, callback)),
981        })
982    }
983
984    /// Acquire the given clock into the current thread, establishing synchronization with
985    /// the moment when that clock snapshot was taken via `release_clock`.
986    fn acquire_clock(&self, clock: &VClock) -> InterpResult<'tcx> {
987        let this = self.eval_context_ref();
988        match &this.machine.data_race {
989            GlobalDataRaceHandler::None => {}
990            GlobalDataRaceHandler::Genmc(_genmc_ctx) =>
991                throw_unsup_format!(
992                    "this operation performs synchronization that is not supported in GenMC mode"
993                ),
994            GlobalDataRaceHandler::Vclocks(data_race) =>
995                data_race.acquire_clock(clock, &this.machine.threads),
996        }
997        interp_ok(())
998    }
999}
1000
1001/// Vector clock metadata for a logical memory allocation.
1002#[derive(Debug, Clone)]
1003pub struct VClockAlloc {
1004    /// Assigning each byte a MemoryCellClocks.
1005    alloc_ranges: RefCell<DedupRangeMap<MemoryCellClocks>>,
1006}
1007
1008impl VisitProvenance for VClockAlloc {
1009    fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
1010        // No tags or allocIds here.
1011    }
1012}
1013
1014impl VClockAlloc {
1015    /// Create a new data-race detector for newly allocated memory.
1016    pub fn new_allocation(
1017        global: &GlobalState,
1018        thread_mgr: &ThreadManager<'_>,
1019        len: Size,
1020        kind: MemoryKind,
1021        current_span: Span,
1022    ) -> VClockAlloc {
1023        // Determine the thread that did the allocation, and when it did it.
1024        let (alloc_timestamp, alloc_index) = match kind {
1025            // User allocated and stack memory should track allocation.
1026            MemoryKind::Machine(
1027                MiriMemoryKind::Rust
1028                | MiriMemoryKind::Miri
1029                | MiriMemoryKind::C
1030                | MiriMemoryKind::WinHeap
1031                | MiriMemoryKind::WinLocal
1032                | MiriMemoryKind::Mmap
1033                | MiriMemoryKind::SocketAddress,
1034            )
1035            | MemoryKind::Stack => {
1036                let (alloc_index, clocks) = global.active_thread_state(thread_mgr);
1037                let mut alloc_timestamp = clocks.clock[alloc_index];
1038                alloc_timestamp.span = current_span;
1039                (alloc_timestamp, alloc_index)
1040            }
1041            // Other global memory should trace races but be allocated at the 0 timestamp
1042            // (conceptually they are allocated on the main thread before everything).
1043            MemoryKind::Machine(
1044                MiriMemoryKind::Global
1045                | MiriMemoryKind::Machine
1046                | MiriMemoryKind::Runtime
1047                | MiriMemoryKind::ExternStatic
1048                | MiriMemoryKind::Tls,
1049            )
1050            | MemoryKind::CallerLocation =>
1051                (VTimestamp::ZERO, global.thread_index(ThreadId::MAIN_THREAD)),
1052        };
1053        VClockAlloc {
1054            alloc_ranges: RefCell::new(DedupRangeMap::new(
1055                len,
1056                MemoryCellClocks::new(alloc_timestamp, alloc_index),
1057            )),
1058        }
1059    }
1060
1061    // Find an index, if one exists where the value
1062    // in `l` is greater than the value in `r`.
1063    fn find_gt_index(l: &VClock, r: &VClock) -> Option<VectorIdx> {
1064        trace!("Find index where not {:?} <= {:?}", l, r);
1065        let l_slice = l.as_slice();
1066        let r_slice = r.as_slice();
1067        l_slice
1068            .iter()
1069            .zip(r_slice.iter())
1070            .enumerate()
1071            .find_map(|(idx, (&l, &r))| if l > r { Some(idx) } else { None })
1072            .or_else(|| {
1073                if l_slice.len() > r_slice.len() {
1074                    // By invariant, if l_slice is longer
1075                    // then one element must be larger.
1076                    // This just validates that this is true
1077                    // and reports earlier elements first.
1078                    let l_remainder_slice = &l_slice[r_slice.len()..];
1079                    let idx = l_remainder_slice
1080                        .iter()
1081                        .enumerate()
1082                        .find_map(|(idx, &r)| if r == VTimestamp::ZERO { None } else { Some(idx) })
1083                        .expect("Invalid VClock Invariant");
1084                    Some(idx + r_slice.len())
1085                } else {
1086                    None
1087                }
1088            })
1089            .map(VectorIdx::new)
1090    }
1091
1092    /// Report a data-race found in the program.
1093    /// This finds the two racing threads and the type
1094    /// of data-race that occurred. This will also
1095    /// return info about the memory location the data-race
1096    /// occurred in. The `ty` parameter is used for diagnostics, letting
1097    /// the user know which type was involved in the access.
1098    #[cold]
1099    #[inline(never)]
1100    fn report_data_race<'tcx>(
1101        global: &GlobalState,
1102        thread_mgr: &ThreadManager<'_>,
1103        mem_clocks: &MemoryCellClocks,
1104        access: AccessType,
1105        access_size: Size,
1106        ptr_dbg: interpret::Pointer<AllocId>,
1107        ty: Option<Ty<'_>>,
1108    ) -> InterpResult<'tcx> {
1109        let (active_index, active_clocks) = global.active_thread_state(thread_mgr);
1110        let mut other_size = None; // if `Some`, this was a size-mismatch race
1111        let write_clock;
1112        let (other_access, other_thread, other_clock) =
1113            // First check the atomic-nonatomic cases.
1114            if !access.is_atomic() &&
1115                let Some(atomic) = mem_clocks.atomic() &&
1116                let Some(idx) = Self::find_gt_index(&atomic.write_vector, &active_clocks.clock)
1117            {
1118                (AccessType::AtomicStore, idx, &atomic.write_vector)
1119            } else if !access.is_atomic() &&
1120                !access.is_read() &&
1121                let Some(atomic) = mem_clocks.atomic() &&
1122                let Some(idx) = Self::find_gt_index(&atomic.read_vector, &active_clocks.clock)
1123            {
1124                (AccessType::AtomicLoad, idx, &atomic.read_vector)
1125            // Then check races with non-atomic writes/reads.
1126            } else if mem_clocks.write.1 > active_clocks.clock[mem_clocks.write.0] {
1127                write_clock = mem_clocks.write();
1128                (AccessType::NaWrite(mem_clocks.write_type), mem_clocks.write.0, &write_clock)
1129            } else if !access.is_read() && let Some(idx) = Self::find_gt_index(&mem_clocks.read, &active_clocks.clock) {
1130                (AccessType::NaRead(mem_clocks.read[idx].read_type()), idx, &mem_clocks.read)
1131            // Finally, mixed-size races.
1132            } else if access.is_atomic() && let Some(atomic) = mem_clocks.atomic() && atomic.size != Some(access_size) {
1133                // This is only a race if we are not synchronized with all atomic accesses, so find
1134                // the one we are not synchronized with.
1135                other_size = Some(atomic.size.unwrap_or(Size::ZERO));
1136                if let Some(idx) = Self::find_gt_index(&atomic.write_vector, &active_clocks.clock)
1137                    {
1138                        (AccessType::AtomicStore, idx, &atomic.write_vector)
1139                    } else if let Some(idx) =
1140                        Self::find_gt_index(&atomic.read_vector, &active_clocks.clock)
1141                    {
1142                        (AccessType::AtomicLoad, idx, &atomic.read_vector)
1143                    } else {
1144                        unreachable!(
1145                            "Failed to report data-race for mixed-size access: no race found"
1146                        )
1147                    }
1148            } else {
1149                unreachable!("Failed to report data-race")
1150            };
1151
1152        // Load elaborated thread information about the racing thread actions.
1153        let active_thread_info = global.print_thread_metadata(thread_mgr, active_index);
1154        let other_thread_info = global.print_thread_metadata(thread_mgr, other_thread);
1155        let involves_non_atomic = !access.is_atomic() || !other_access.is_atomic();
1156
1157        // Throw the data-race detection.
1158        let extra = if other_size.is_some() {
1159            assert!(!involves_non_atomic);
1160            Some("overlapping unsynchronized atomic accesses must use the same access size")
1161        } else if access.is_read() && other_access.is_read() {
1162            panic!(
1163                "there should be no same-size read-read races\naccess: {access:?}\nother_access: {other_access:?}"
1164            )
1165        } else {
1166            None
1167        };
1168        Err(err_machine_stop!(TerminationInfo::DataRace {
1169            involves_non_atomic,
1170            extra,
1171            retag_explain: access.is_retag() || other_access.is_retag(),
1172            ptr: ptr_dbg,
1173            op1: RacingOp {
1174                action: other_access.description(None, other_size),
1175                thread_info: other_thread_info,
1176                span: other_clock.as_slice()[other_thread.index()].span_data(),
1177            },
1178            op2: RacingOp {
1179                action: access.description(ty, other_size.map(|_| access_size)),
1180                thread_info: active_thread_info,
1181                span: active_clocks.clock.as_slice()[active_index.index()].span_data(),
1182            },
1183        }))?
1184    }
1185
1186    /// Return the release/acquire synchronization clock for the given memory range.
1187    pub(super) fn sync_clock(&self, access_range: AllocRange) -> VClock {
1188        let alloc_ranges = self.alloc_ranges.borrow();
1189        let mut clock = VClock::default();
1190        for (_, mem_clocks) in alloc_ranges.iter(access_range.start, access_range.size) {
1191            if let Some(atomic) = mem_clocks.atomic() {
1192                clock.join(&atomic.sync_vector);
1193            }
1194        }
1195        clock
1196    }
1197
1198    /// Detect data-races for an unsynchronized read operation. It will not perform
1199    /// data-race detection if `race_detecting()` is false, either due to no threads
1200    /// being created or if it is temporarily disabled during a racy read or write
1201    /// operation for which data-race detection is handled separately, for example
1202    /// atomic read operations. The `ty` parameter is used for diagnostics, letting
1203    /// the user know which type was read.
1204    pub fn read_non_atomic<'tcx>(
1205        &self,
1206        alloc_id: AllocId,
1207        access_range: AllocRange,
1208        read_type: NaReadType,
1209        ty: Option<Ty<'_>>,
1210        machine: &MiriMachine<'_>,
1211    ) -> InterpResult<'tcx> {
1212        let current_span = machine.current_user_relevant_span();
1213        let global = machine.data_race.as_vclocks_ref().unwrap();
1214        if !global.race_detecting() {
1215            return interp_ok(());
1216        }
1217        let (index, mut thread_clocks) = global.active_thread_state_mut(&machine.threads);
1218        let mut alloc_ranges = self.alloc_ranges.borrow_mut();
1219        for (mem_clocks_range, mem_clocks) in
1220            alloc_ranges.iter_mut(access_range.start, access_range.size)
1221        {
1222            if let Err(DataRace) =
1223                mem_clocks.read_race_detect(&mut thread_clocks, index, read_type, current_span)
1224            {
1225                drop(thread_clocks);
1226                // Report data-race.
1227                return Self::report_data_race(
1228                    global,
1229                    &machine.threads,
1230                    mem_clocks,
1231                    AccessType::NaRead(read_type),
1232                    access_range.size,
1233                    interpret::Pointer::new(alloc_id, Size::from_bytes(mem_clocks_range.start)),
1234                    ty,
1235                );
1236            }
1237        }
1238        interp_ok(())
1239    }
1240
1241    /// Detect data-races for an unsynchronized write operation. It will not perform
1242    /// data-race detection if `race_detecting()` is false, either due to no threads
1243    /// being created or if it is temporarily disabled during a racy read or write
1244    /// operation. The `ty` parameter is used for diagnostics, letting
1245    /// the user know which type was written.
1246    pub fn write_non_atomic<'tcx>(
1247        &self,
1248        alloc_id: AllocId,
1249        access_range: AllocRange,
1250        write_type: NaWriteType,
1251        ty: Option<Ty<'_>>,
1252        machine: &MiriMachine<'_>,
1253    ) -> InterpResult<'tcx> {
1254        let current_span = machine.current_user_relevant_span();
1255        let global = machine.data_race.as_vclocks_ref().unwrap();
1256        if !global.race_detecting() {
1257            return interp_ok(());
1258        }
1259        let (index, mut thread_clocks) = global.active_thread_state_mut(&machine.threads);
1260        for (mem_clocks_range, mem_clocks) in
1261            self.alloc_ranges.borrow_mut().iter_mut(access_range.start, access_range.size)
1262        {
1263            if let Err(DataRace) =
1264                mem_clocks.write_race_detect(&mut thread_clocks, index, write_type, current_span)
1265            {
1266                drop(thread_clocks);
1267                // Report data-race
1268                return Self::report_data_race(
1269                    global,
1270                    &machine.threads,
1271                    mem_clocks,
1272                    AccessType::NaWrite(write_type),
1273                    access_range.size,
1274                    interpret::Pointer::new(alloc_id, Size::from_bytes(mem_clocks_range.start)),
1275                    ty,
1276                );
1277            }
1278        }
1279        interp_ok(())
1280    }
1281}
1282
1283/// Vector clock state for a stack frame (tracking the local variables
1284/// that do not have an allocation yet).
1285#[derive(Debug, Default)]
1286pub struct FrameState {
1287    local_clocks: RefCell<FxHashMap<mir::Local, LocalClocks>>,
1288}
1289
1290/// Stripped-down version of [`MemoryCellClocks`] for the clocks we need to keep track
1291/// of in a local that does not yet have addressable memory -- and hence can only
1292/// be accessed from the thread its stack frame belongs to, and cannot be access atomically.
1293#[derive(Debug)]
1294struct LocalClocks {
1295    write: VTimestamp,
1296    write_type: NaWriteType,
1297    read: VTimestamp,
1298}
1299
1300impl Default for LocalClocks {
1301    fn default() -> Self {
1302        Self { write: VTimestamp::ZERO, write_type: NaWriteType::Allocate, read: VTimestamp::ZERO }
1303    }
1304}
1305
1306impl FrameState {
1307    pub fn local_write(&self, local: mir::Local, storage_live: bool, machine: &MiriMachine<'_>) {
1308        let current_span = machine.current_user_relevant_span();
1309        let global = machine.data_race.as_vclocks_ref().unwrap();
1310        if !global.race_detecting() {
1311            return;
1312        }
1313        let (index, mut thread_clocks) = global.active_thread_state_mut(&machine.threads);
1314        // This should do the same things as `MemoryCellClocks::write_race_detect`.
1315        if !current_span.is_dummy() {
1316            thread_clocks.clock.index_mut(index).span = current_span;
1317        }
1318        let mut clocks = self.local_clocks.borrow_mut();
1319        if storage_live {
1320            let new_clocks = LocalClocks {
1321                write: thread_clocks.clock[index],
1322                write_type: NaWriteType::Allocate,
1323                read: VTimestamp::ZERO,
1324            };
1325            // There might already be an entry in the map for this, if the local was previously
1326            // live already.
1327            clocks.insert(local, new_clocks);
1328        } else {
1329            // This can fail to exist if `race_detecting` was false when the allocation
1330            // occurred, in which case we can backdate this to the beginning of time.
1331            let clocks = clocks.entry(local).or_default();
1332            clocks.write = thread_clocks.clock[index];
1333            clocks.write_type = NaWriteType::Write;
1334        }
1335    }
1336
1337    pub fn local_read(&self, local: mir::Local, machine: &MiriMachine<'_>) {
1338        let current_span = machine.current_user_relevant_span();
1339        let global = machine.data_race.as_vclocks_ref().unwrap();
1340        if !global.race_detecting() {
1341            return;
1342        }
1343        let (index, mut thread_clocks) = global.active_thread_state_mut(&machine.threads);
1344        // This should do the same things as `MemoryCellClocks::read_race_detect`.
1345        if !current_span.is_dummy() {
1346            thread_clocks.clock.index_mut(index).span = current_span;
1347        }
1348        thread_clocks.clock.index_mut(index).set_read_type(NaReadType::Read);
1349        // This can fail to exist if `race_detecting` was false when the allocation
1350        // occurred, in which case we can backdate this to the beginning of time.
1351        let mut clocks = self.local_clocks.borrow_mut();
1352        let clocks = clocks.entry(local).or_default();
1353        clocks.read = thread_clocks.clock[index];
1354    }
1355
1356    pub fn local_moved_to_memory(
1357        &self,
1358        local: mir::Local,
1359        alloc: &mut VClockAlloc,
1360        machine: &MiriMachine<'_>,
1361    ) {
1362        let global = machine.data_race.as_vclocks_ref().unwrap();
1363        if !global.race_detecting() {
1364            return;
1365        }
1366        let (index, _thread_clocks) = global.active_thread_state_mut(&machine.threads);
1367        // Get the time the last write actually happened. This can fail to exist if
1368        // `race_detecting` was false when the write occurred, in that case we can backdate this
1369        // to the beginning of time.
1370        let local_clocks = self.local_clocks.borrow_mut().remove(&local).unwrap_or_default();
1371        for (_mem_clocks_range, mem_clocks) in alloc.alloc_ranges.get_mut().iter_mut_all() {
1372            // The initialization write for this already happened, just at the wrong timestamp.
1373            // Check that the thread index matches what we expect.
1374            assert_eq!(mem_clocks.write.0, index);
1375            // Convert the local's clocks into memory clocks.
1376            mem_clocks.write = (index, local_clocks.write);
1377            mem_clocks.write_type = local_clocks.write_type;
1378            mem_clocks.read = VClock::new_with_index(index, local_clocks.read);
1379        }
1380    }
1381}
1382
1383impl<'tcx> EvalContextPrivExt<'tcx> for MiriInterpCx<'tcx> {}
1384trait EvalContextPrivExt<'tcx>: MiriInterpCxExt<'tcx> {
1385    /// Temporarily allow data-races to occur. This should only be used in
1386    /// one of these cases:
1387    /// - One of the appropriate `validate_atomic` functions will be called to
1388    ///   treat a memory access as atomic.
1389    /// - The memory being accessed should be treated as internal state, that
1390    ///   cannot be accessed by the interpreted program.
1391    /// - Execution of the interpreted program execution has halted.
1392    #[inline]
1393    fn allow_data_races_ref<R>(&self, op: impl FnOnce(&MiriInterpCx<'tcx>) -> R) -> R {
1394        let this = self.eval_context_ref();
1395        this.machine.data_race.set_ongoing_action_data_race_free(true);
1396        let result = op(this);
1397        this.machine.data_race.set_ongoing_action_data_race_free(false);
1398        result
1399    }
1400
1401    /// Same as `allow_data_races_ref`, this temporarily disables any data-race detection and
1402    /// so should only be used for atomic operations or internal state that the program cannot
1403    /// access.
1404    #[inline]
1405    fn allow_data_races_mut<R>(&mut self, op: impl FnOnce(&mut MiriInterpCx<'tcx>) -> R) -> R {
1406        let this = self.eval_context_mut();
1407        this.machine.data_race.set_ongoing_action_data_race_free(true);
1408        let result = op(this);
1409        this.machine.data_race.set_ongoing_action_data_race_free(false);
1410        result
1411    }
1412
1413    /// Checks that an atomic access is legal at the given place.
1414    fn atomic_access_check(
1415        &self,
1416        place: &MPlaceTy<'tcx>,
1417        access_type: AtomicAccessType,
1418    ) -> InterpResult<'tcx> {
1419        let this = self.eval_context_ref();
1420        // Check alignment requirements. Atomics must always be aligned to their size,
1421        // even if the type they wrap would be less aligned (e.g. AtomicU64 on 32bit must
1422        // be 8-aligned).
1423        let align = Align::from_bytes(place.layout.size.bytes()).unwrap();
1424        this.check_ptr_align(place.ptr(), align)?;
1425        // Ensure the allocation is mutable. Even failing (read-only) compare_exchange need mutable
1426        // memory on many targets (i.e., they segfault if that memory is mapped read-only), and
1427        // atomic loads can be implemented via compare_exchange on some targets. There could
1428        // possibly be some very specific exceptions to this, see
1429        // <https://github.com/rust-lang/miri/pull/2464#discussion_r939636130> for details.
1430        // We avoid `get_ptr_alloc` since we do *not* want to run the access hooks -- the actual
1431        // access will happen later.
1432        let (alloc_id, _offset, _prov) = this
1433            .ptr_try_get_alloc_id(place.ptr(), 0)
1434            .expect("there are no zero-sized atomic accesses");
1435        if this.get_alloc_mutability(alloc_id)? == Mutability::Not {
1436            // See if this is fine.
1437            match access_type {
1438                AtomicAccessType::Rmw | AtomicAccessType::Store => {
1439                    throw_ub_format!(
1440                        "atomic store and read-modify-write operations cannot be performed on read-only memory\n\
1441                        see <https://doc.rust-lang.org/nightly/std/sync/atomic/index.html#atomic-accesses-to-read-only-memory> for more information"
1442                    );
1443                }
1444                AtomicAccessType::Load(_)
1445                    if place.layout.size > this.tcx.data_layout().pointer_size() =>
1446                {
1447                    throw_ub_format!(
1448                        "large atomic load operations cannot be performed on read-only memory\n\
1449                        these operations often have to be implemented using read-modify-write operations, which require writeable memory\n\
1450                        see <https://doc.rust-lang.org/nightly/std/sync/atomic/index.html#atomic-accesses-to-read-only-memory> for more information"
1451                    );
1452                }
1453                AtomicAccessType::Load(o) if o != AtomicReadOrd::Relaxed => {
1454                    throw_ub_format!(
1455                        "non-relaxed atomic load operations cannot be performed on read-only memory\n\
1456                        these operations sometimes have to be implemented using read-modify-write operations, which require writeable memory\n\
1457                        see <https://doc.rust-lang.org/nightly/std/sync/atomic/index.html#atomic-accesses-to-read-only-memory> for more information"
1458                    );
1459                }
1460                _ => {
1461                    // Large relaxed loads are fine!
1462                }
1463            }
1464        }
1465        interp_ok(())
1466    }
1467
1468    /// Update the data-race detector for an atomic read occurring at the
1469    /// associated memory-place and on the current thread.
1470    fn validate_atomic_load(
1471        &self,
1472        place: &MPlaceTy<'tcx>,
1473        atomic: AtomicReadOrd,
1474        sync_clock: Option<&VClock>,
1475    ) -> InterpResult<'tcx> {
1476        let this = self.eval_context_ref();
1477        this.validate_atomic_op(
1478            place,
1479            atomic,
1480            AccessType::AtomicLoad,
1481            move |memory, clocks, index, atomic| {
1482                if atomic == AtomicReadOrd::Relaxed {
1483                    memory.load_relaxed(&mut *clocks, index, place.layout.size, sync_clock)
1484                } else {
1485                    memory.load_acquire(&mut *clocks, index, place.layout.size, sync_clock)
1486                }
1487            },
1488        )
1489    }
1490
1491    /// Update the data-race detector for an atomic write occurring at the
1492    /// associated memory-place and on the current thread.
1493    fn validate_atomic_store(
1494        &mut self,
1495        place: &MPlaceTy<'tcx>,
1496        atomic: AtomicWriteOrd,
1497    ) -> InterpResult<'tcx> {
1498        let this = self.eval_context_mut();
1499        this.validate_atomic_op(
1500            place,
1501            atomic,
1502            AccessType::AtomicStore,
1503            move |memory, clocks, index, atomic| {
1504                if atomic == AtomicWriteOrd::Relaxed {
1505                    memory.store_relaxed(clocks, index, place.layout.size)
1506                } else {
1507                    memory.store_release(clocks, index, place.layout.size)
1508                }
1509            },
1510        )
1511    }
1512
1513    /// Update the data-race detector for an atomic read-modify-write occurring
1514    /// at the associated memory place and on the current thread.
1515    fn validate_atomic_rmw(
1516        &mut self,
1517        place: &MPlaceTy<'tcx>,
1518        atomic: AtomicRwOrd,
1519    ) -> InterpResult<'tcx> {
1520        use AtomicRwOrd::*;
1521        let acquire = matches!(atomic, Acquire | AcqRel | SeqCst);
1522        let release = matches!(atomic, Release | AcqRel | SeqCst);
1523        let this = self.eval_context_mut();
1524        this.validate_atomic_op(
1525            place,
1526            atomic,
1527            AccessType::AtomicRmw,
1528            move |memory, clocks, index, _| {
1529                if acquire {
1530                    memory.load_acquire(clocks, index, place.layout.size, None)?;
1531                } else {
1532                    memory.load_relaxed(clocks, index, place.layout.size, None)?;
1533                }
1534                if release {
1535                    memory.rmw_release(clocks, index, place.layout.size)
1536                } else {
1537                    memory.rmw_relaxed(clocks, index, place.layout.size)
1538                }
1539            },
1540        )
1541    }
1542
1543    /// Returns the most recent *non-atomic* value stored in the given place.
1544    /// Errors if we don't need that (because we don't do store buffering) or if
1545    /// the most recent value is in fact atomic.
1546    fn get_latest_nonatomic_val(&self, place: &MPlaceTy<'tcx>) -> Result<Option<Scalar>, ()> {
1547        let this = self.eval_context_ref();
1548        // These cannot fail because `atomic_access_check` was done first.
1549        let (alloc_id, offset, _prov) = this.ptr_get_alloc_id(place.ptr(), 0).unwrap();
1550        let alloc_meta = &this.get_alloc_extra(alloc_id).unwrap().data_race;
1551        if alloc_meta.as_weak_memory_ref().is_none() {
1552            // No reason to read old value if we don't track store buffers.
1553            return Err(());
1554        }
1555        let data_race = alloc_meta.as_vclocks_ref().unwrap();
1556        // Only read old value if this is currently a non-atomic location.
1557        for (_range, clocks) in data_race.alloc_ranges.borrow_mut().iter(offset, place.layout.size)
1558        {
1559            // If this had an atomic write that's not before the non-atomic write, that should
1560            // already be in the store buffer. Initializing the store buffer now would use the
1561            // wrong `sync_clock` so we better make sure that does not happen.
1562            if clocks.atomic().is_some_and(|atomic| !(atomic.write_vector <= clocks.write())) {
1563                return Err(());
1564            }
1565        }
1566        // The program didn't actually do a read, so suppress the memory access hooks.
1567        // This is also a very special exception where we just ignore an error -- if this read
1568        // was UB e.g. because the memory is uninitialized, we don't want to know!
1569        Ok(this.run_for_validation_ref(|this| this.read_scalar(place)).discard_err())
1570    }
1571
1572    /// Generic atomic operation implementation
1573    fn validate_atomic_op<A: Debug + Copy>(
1574        &self,
1575        place: &MPlaceTy<'tcx>,
1576        atomic: A,
1577        access: AccessType,
1578        mut op: impl FnMut(
1579            &mut MemoryCellClocks,
1580            &mut ThreadClockSet,
1581            VectorIdx,
1582            A,
1583        ) -> Result<(), DataRace>,
1584    ) -> InterpResult<'tcx> {
1585        let this = self.eval_context_ref();
1586        assert!(access.is_atomic());
1587        let Some(data_race) = this.machine.data_race.as_vclocks_ref() else {
1588            return interp_ok(());
1589        };
1590        if !data_race.race_detecting() {
1591            return interp_ok(());
1592        }
1593        let size = place.layout.size;
1594        let (alloc_id, base_offset, _prov) = this.ptr_get_alloc_id(place.ptr(), 0)?;
1595        // Load and log the atomic operation.
1596        // Note that atomic loads are possible even from read-only allocations, so `get_alloc_extra_mut` is not an option.
1597        let alloc_meta = this.get_alloc_extra(alloc_id)?.data_race.as_vclocks_ref().unwrap();
1598        trace!(
1599            "Atomic op({}) with ordering {:?} on {:?} (size={})",
1600            access.description(None, None),
1601            &atomic,
1602            place.ptr(),
1603            size.bytes()
1604        );
1605
1606        let current_span = this.machine.current_user_relevant_span();
1607        // Perform the atomic operation.
1608        data_race.maybe_perform_sync_operation(
1609            &this.machine.threads,
1610            current_span,
1611            |index, mut thread_clocks| {
1612                for (mem_clocks_range, mem_clocks) in
1613                    alloc_meta.alloc_ranges.borrow_mut().iter_mut(base_offset, size)
1614                {
1615                    if let Err(DataRace) = op(mem_clocks, &mut thread_clocks, index, atomic) {
1616                        mem::drop(thread_clocks);
1617                        return VClockAlloc::report_data_race(
1618                            data_race,
1619                            &this.machine.threads,
1620                            mem_clocks,
1621                            access,
1622                            place.layout.size,
1623                            interpret::Pointer::new(
1624                                alloc_id,
1625                                Size::from_bytes(mem_clocks_range.start),
1626                            ),
1627                            None,
1628                        )
1629                        .map(|_| true);
1630                    }
1631                }
1632
1633                // This conservatively assumes all operations have release semantics
1634                interp_ok(true)
1635            },
1636        )?;
1637
1638        // Log changes to atomic memory.
1639        if tracing::enabled!(tracing::Level::TRACE) {
1640            for (_offset, mem_clocks) in alloc_meta.alloc_ranges.borrow().iter(base_offset, size) {
1641                trace!(
1642                    "Updated atomic memory({:?}, size={}) to {:#?}",
1643                    place.ptr(),
1644                    size.bytes(),
1645                    mem_clocks.atomic_ops
1646                );
1647            }
1648        }
1649
1650        interp_ok(())
1651    }
1652}
1653
1654impl GlobalState {
1655    /// Create a new global state, setup with just thread-id=0
1656    /// advanced to timestamp = 1.
1657    pub fn new(config: &MiriConfig) -> Self {
1658        let mut global_state = GlobalState {
1659            multi_threaded: Cell::new(false),
1660            ongoing_action_data_race_free: Cell::new(false),
1661            vector_clocks: RefCell::new(IndexVec::new()),
1662            vector_info: RefCell::new(IndexVec::new()),
1663            thread_info: RefCell::new(IndexVec::new()),
1664            reuse_candidates: RefCell::new(FxHashSet::default()),
1665            last_sc_fence: RefCell::new(VClock::default()),
1666            last_sc_write_per_thread: RefCell::new(VClock::default()),
1667            track_outdated_loads: config.track_outdated_loads,
1668            weak_memory: config.weak_memory_emulation,
1669        };
1670
1671        // Setup the main-thread since it is not explicitly created:
1672        // uses vector index and thread-id 0.
1673        let index = global_state.vector_clocks.get_mut().push(ThreadClockSet::default());
1674        global_state.vector_info.get_mut().push(ThreadId::MAIN_THREAD);
1675        global_state
1676            .thread_info
1677            .get_mut()
1678            .push(ThreadExtraState { vector_index: Some(index), termination_vector_clock: None });
1679
1680        global_state
1681    }
1682
1683    // We perform data race detection when there are more than 1 active thread
1684    // and we have not temporarily disabled race detection to perform something
1685    // data race free
1686    fn race_detecting(&self) -> bool {
1687        self.multi_threaded.get() && !self.ongoing_action_data_race_free.get()
1688    }
1689
1690    pub fn ongoing_action_data_race_free(&self) -> bool {
1691        self.ongoing_action_data_race_free.get()
1692    }
1693
1694    // Try to find vector index values that can potentially be re-used
1695    // by a new thread instead of a new vector index being created.
1696    fn find_vector_index_reuse_candidate(&self) -> Option<VectorIdx> {
1697        let mut reuse = self.reuse_candidates.borrow_mut();
1698        let vector_clocks = self.vector_clocks.borrow();
1699        for &candidate in reuse.iter() {
1700            let target_timestamp = vector_clocks[candidate].clock[candidate];
1701            if vector_clocks.iter_enumerated().all(|(clock_idx, clock)| {
1702                // The thread happens before the clock, and hence cannot report
1703                // a data-race with this the candidate index.
1704                let no_data_race = clock.clock[candidate] >= target_timestamp;
1705
1706                // The vector represents a thread that has terminated and hence cannot
1707                // report a data-race with the candidate index.
1708                let vector_terminated = reuse.contains(&clock_idx);
1709
1710                // The vector index cannot report a race with the candidate index
1711                // and hence allows the candidate index to be re-used.
1712                no_data_race || vector_terminated
1713            }) {
1714                // All vector clocks for each vector index are equal to
1715                // the target timestamp, and the thread is known to have
1716                // terminated, therefore this vector clock index cannot
1717                // report any more data-races.
1718                assert!(reuse.remove(&candidate));
1719                return Some(candidate);
1720            }
1721        }
1722        None
1723    }
1724
1725    // Hook for thread creation, enabled multi-threaded execution and marks
1726    // the current thread timestamp as happening-before the current thread.
1727    #[inline]
1728    pub fn thread_created(
1729        &mut self,
1730        thread_mgr: &ThreadManager<'_>,
1731        thread: ThreadId,
1732        current_span: Span,
1733    ) {
1734        let current_index = self.active_thread_index(thread_mgr);
1735
1736        // Enable multi-threaded execution, there are now at least two threads
1737        // so data-races are now possible.
1738        self.multi_threaded.set(true);
1739
1740        // Load and setup the associated thread metadata
1741        let mut thread_info = self.thread_info.borrow_mut();
1742        thread_info.ensure_contains_elem(thread, Default::default);
1743
1744        // Assign a vector index for the thread, attempting to re-use an old
1745        // vector index that can no longer report any data-races if possible.
1746        let created_index = if let Some(reuse_index) = self.find_vector_index_reuse_candidate() {
1747            // Now re-configure the re-use candidate, increment the clock
1748            // for the new sync use of the vector.
1749            let vector_clocks = self.vector_clocks.get_mut();
1750            vector_clocks[reuse_index].increment_clock(reuse_index, current_span);
1751
1752            // Locate the old thread the vector was associated with and update
1753            // it to represent the new thread instead.
1754            let vector_info = self.vector_info.get_mut();
1755            let old_thread = vector_info[reuse_index];
1756            vector_info[reuse_index] = thread;
1757
1758            // Mark the thread the vector index was associated with as no longer
1759            // representing a thread index.
1760            thread_info[old_thread].vector_index = None;
1761
1762            reuse_index
1763        } else {
1764            // No vector re-use candidates available, instead create
1765            // a new vector index.
1766            let vector_info = self.vector_info.get_mut();
1767            vector_info.push(thread)
1768        };
1769
1770        trace!("Creating thread = {:?} with vector index = {:?}", thread, created_index);
1771
1772        // Mark the chosen vector index as in use by the thread.
1773        thread_info[thread].vector_index = Some(created_index);
1774
1775        // Create a thread clock set if applicable.
1776        let vector_clocks = self.vector_clocks.get_mut();
1777        if created_index == vector_clocks.next_index() {
1778            vector_clocks.push(ThreadClockSet::default());
1779        }
1780
1781        // Now load the two clocks and configure the initial state.
1782        let (current, created) = vector_clocks.pick2_mut(current_index, created_index);
1783
1784        // Join the created with current, since the current threads
1785        // previous actions happen-before the created thread.
1786        created.join_with(current);
1787
1788        // Advance both threads after the synchronized operation.
1789        // Both operations are considered to have release semantics.
1790        current.increment_clock(current_index, current_span);
1791        created.increment_clock(created_index, current_span);
1792    }
1793
1794    /// Hook on a thread join to update the implicit happens-before relation between the joined
1795    /// thread (the joinee, the thread that someone waited on) and the current thread (the joiner,
1796    /// the thread who was waiting).
1797    #[inline]
1798    pub fn thread_joined(&mut self, threads: &ThreadManager<'_>, joinee: ThreadId) {
1799        let thread_info = self.thread_info.borrow();
1800        let thread_info = &thread_info[joinee];
1801
1802        // Load the associated vector clock for the terminated thread.
1803        let join_clock = thread_info
1804            .termination_vector_clock
1805            .as_ref()
1806            .expect("joined with thread but thread has not terminated");
1807        // Acquire that into the current thread.
1808        self.acquire_clock(join_clock, threads);
1809
1810        // Check the number of live threads, if the value is 1
1811        // then test for potentially disabling multi-threaded execution.
1812        // This has to happen after `acquire_clock`, otherwise there'll always
1813        // be some thread that has not synchronized yet.
1814        if let Some(current_index) = thread_info.vector_index {
1815            if threads.get_live_thread_count() == 1 {
1816                let vector_clocks = self.vector_clocks.get_mut();
1817                // May potentially be able to disable multi-threaded execution.
1818                let current_clock = &vector_clocks[current_index];
1819                if vector_clocks
1820                    .iter_enumerated()
1821                    .all(|(idx, clocks)| clocks.clock[idx] <= current_clock.clock[idx])
1822                {
1823                    // All thread terminations happen-before the current clock
1824                    // therefore no data-races can be reported until a new thread
1825                    // is created, so disable multi-threaded execution.
1826                    self.multi_threaded.set(false);
1827                }
1828            }
1829        }
1830    }
1831
1832    /// On thread termination, the vector clock may be re-used
1833    /// in the future once all remaining thread-clocks catch
1834    /// up with the time index of the terminated thread.
1835    /// This assigns thread termination with a unique index
1836    /// which will be used to join the thread
1837    /// This should be called strictly before any calls to
1838    /// `thread_joined`.
1839    #[inline]
1840    pub fn thread_terminated(&mut self, thread_mgr: &ThreadManager<'_>) {
1841        let current_thread = thread_mgr.active_thread();
1842        let current_index = self.active_thread_index(thread_mgr);
1843
1844        // Store the terminaion clock.
1845        let terminaion_clock = self.release_clock(thread_mgr, |clock| clock.clone());
1846        self.thread_info.get_mut()[current_thread].termination_vector_clock =
1847            Some(terminaion_clock);
1848
1849        // Add this thread's clock index as a candidate for re-use.
1850        let reuse = self.reuse_candidates.get_mut();
1851        reuse.insert(current_index);
1852    }
1853
1854    /// Update the data-race detector for an atomic fence on the current thread.
1855    fn atomic_fence<'tcx>(
1856        &self,
1857        machine: &MiriMachine<'tcx>,
1858        atomic: AtomicFenceOrd,
1859    ) -> InterpResult<'tcx> {
1860        let current_span = machine.current_user_relevant_span();
1861        self.maybe_perform_sync_operation(&machine.threads, current_span, |index, mut clocks| {
1862            trace!("Atomic fence on {:?} with ordering {:?}", index, atomic);
1863
1864            // Apply data-race detection for the current fences
1865            // this treats AcqRel and SeqCst as the same as an acquire
1866            // and release fence applied in the same timestamp.
1867            if atomic != AtomicFenceOrd::Release {
1868                // Either Acquire | AcqRel | SeqCst
1869                clocks.apply_acquire_fence();
1870            }
1871            if atomic == AtomicFenceOrd::SeqCst {
1872                // Behave like an RMW on the global fence location. This takes full care of
1873                // all the SC fence requirements, including C++17 ยง32.4 [atomics.order]
1874                // paragraph 6 (which would limit what future reads can see). It also rules
1875                // out many legal behaviors, but we don't currently have a model that would
1876                // be more precise.
1877                // Also see the second bullet on page 10 of
1878                // <https://www.cs.tau.ac.il/~orilahav/papers/popl21_robustness.pdf>.
1879                let mut sc_fence_clock = self.last_sc_fence.borrow_mut();
1880                sc_fence_clock.join(&clocks.clock);
1881                clocks.clock.join(&sc_fence_clock);
1882                // Also establish some sort of order with the last SC write that happened, globally
1883                // (but this is only respected by future reads).
1884                clocks.write_seqcst.join(&self.last_sc_write_per_thread.borrow());
1885            }
1886            // The release fence is last, since both of the above could alter our clock,
1887            // which should be part of what is being released.
1888            if atomic != AtomicFenceOrd::Acquire {
1889                // Either Release | AcqRel | SeqCst
1890                clocks.apply_release_fence();
1891            }
1892
1893            // Increment timestamp in case of release semantics.
1894            interp_ok(atomic != AtomicFenceOrd::Acquire)
1895        })
1896    }
1897
1898    /// Attempt to perform a synchronized operation, this
1899    /// will perform no operation if multi-threading is
1900    /// not currently enabled.
1901    /// Otherwise it will increment the clock for the current
1902    /// vector before and after the operation for data-race
1903    /// detection between any happens-before edges the
1904    /// operation may create.
1905    fn maybe_perform_sync_operation<'tcx>(
1906        &self,
1907        thread_mgr: &ThreadManager<'_>,
1908        current_span: Span,
1909        op: impl FnOnce(VectorIdx, RefMut<'_, ThreadClockSet>) -> InterpResult<'tcx, bool>,
1910    ) -> InterpResult<'tcx> {
1911        if self.multi_threaded.get() {
1912            let (index, clocks) = self.active_thread_state_mut(thread_mgr);
1913            if op(index, clocks)? {
1914                let (_, mut clocks) = self.active_thread_state_mut(thread_mgr);
1915                clocks.increment_clock(index, current_span);
1916            }
1917        }
1918        interp_ok(())
1919    }
1920
1921    /// Internal utility to identify a thread stored internally
1922    /// returns the id and the name for better diagnostics.
1923    fn print_thread_metadata(&self, thread_mgr: &ThreadManager<'_>, vector: VectorIdx) -> String {
1924        let thread = self.vector_info.borrow()[vector];
1925        let thread_name = thread_mgr.get_thread_display_name(thread);
1926        format!("thread `{thread_name}`")
1927    }
1928
1929    /// Acquire the given clock into the current thread, establishing synchronization with
1930    /// the moment when that clock snapshot was taken via `release_clock`.
1931    /// As this is an acquire operation, the thread timestamp is not
1932    /// incremented.
1933    pub fn acquire_clock<'tcx>(&self, clock: &VClock, threads: &ThreadManager<'tcx>) {
1934        let thread = threads.active_thread();
1935        let (_, mut clocks) = self.thread_state_mut(thread);
1936        clocks.clock.join(clock);
1937    }
1938
1939    /// Calls the given closure with the "release" clock of the current thread.
1940    /// Other threads can acquire this clock in the future to establish synchronization
1941    /// with this program point.
1942    pub fn release_clock<'tcx, R>(
1943        &self,
1944        threads: &ThreadManager<'tcx>,
1945        callback: impl FnOnce(&VClock) -> R,
1946    ) -> R {
1947        let thread = threads.active_thread();
1948        let span = threads.active_thread_ref().current_user_relevant_span();
1949        let (index, mut clocks) = self.thread_state_mut(thread);
1950        let r = callback(&clocks.clock);
1951        // Increment the clock, so that all following events cannot be confused with anything that
1952        // occurred before the release. Crucially, the callback is invoked on the *old* clock!
1953        clocks.increment_clock(index, span);
1954
1955        r
1956    }
1957
1958    fn thread_index(&self, thread: ThreadId) -> VectorIdx {
1959        self.thread_info.borrow()[thread].vector_index.expect("thread has no assigned vector")
1960    }
1961
1962    /// Load the vector index used by the given thread as well as the set of vector clocks
1963    /// used by the thread.
1964    #[inline]
1965    fn thread_state_mut(&self, thread: ThreadId) -> (VectorIdx, RefMut<'_, ThreadClockSet>) {
1966        let index = self.thread_index(thread);
1967        let ref_vector = self.vector_clocks.borrow_mut();
1968        let clocks = RefMut::map(ref_vector, |vec| &mut vec[index]);
1969        (index, clocks)
1970    }
1971
1972    /// Load the vector index used by the given thread as well as the set of vector clocks
1973    /// used by the thread.
1974    #[inline]
1975    fn thread_state(&self, thread: ThreadId) -> (VectorIdx, Ref<'_, ThreadClockSet>) {
1976        let index = self.thread_index(thread);
1977        let ref_vector = self.vector_clocks.borrow();
1978        let clocks = Ref::map(ref_vector, |vec| &vec[index]);
1979        (index, clocks)
1980    }
1981
1982    /// Load the current vector clock in use and the current set of thread clocks
1983    /// in use for the vector.
1984    #[inline]
1985    pub(super) fn active_thread_state(
1986        &self,
1987        thread_mgr: &ThreadManager<'_>,
1988    ) -> (VectorIdx, Ref<'_, ThreadClockSet>) {
1989        self.thread_state(thread_mgr.active_thread())
1990    }
1991
1992    /// Load the current vector clock in use and the current set of thread clocks
1993    /// in use for the vector mutably for modification.
1994    #[inline]
1995    pub(super) fn active_thread_state_mut(
1996        &self,
1997        thread_mgr: &ThreadManager<'_>,
1998    ) -> (VectorIdx, RefMut<'_, ThreadClockSet>) {
1999        self.thread_state_mut(thread_mgr.active_thread())
2000    }
2001
2002    /// Return the current thread, should be the same
2003    /// as the data-race active thread.
2004    #[inline]
2005    fn active_thread_index(&self, thread_mgr: &ThreadManager<'_>) -> VectorIdx {
2006        let active_thread_id = thread_mgr.active_thread();
2007        self.thread_index(active_thread_id)
2008    }
2009
2010    // SC ATOMIC STORE rule in the paper.
2011    pub(super) fn sc_write(&self, thread_mgr: &ThreadManager<'_>) {
2012        let (index, clocks) = self.active_thread_state(thread_mgr);
2013        self.last_sc_write_per_thread.borrow_mut().set_at_index(&clocks.clock, index);
2014    }
2015
2016    // SC ATOMIC READ rule in the paper.
2017    pub(super) fn sc_read(&self, thread_mgr: &ThreadManager<'_>) {
2018        let (.., mut clocks) = self.active_thread_state_mut(thread_mgr);
2019        clocks.read_seqcst.join(&self.last_sc_fence.borrow());
2020    }
2021}