]> git.lizzy.rs Git - rust.git/blob - src/data_race.rs
Auto merge of #2141 - saethlin:early-diagnostics-ice, r=RalfJung
[rust.git] / src / 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 //! This does not explore weak memory orders and so 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 sequentially consistent ordering corresponds to the ordering that the threads
33 //! are currently scheduled, this means that the data-race detector has no additional
34 //! logic for sequentially consistent accesses at the moment since they are indistinguishable
35 //! from acquire/release operations. If weak memory orderings are explored then this
36 //! may need to change or be updated accordingly.
37 //!
38 //! Per the C++ spec for the memory model a sequentially consistent operation:
39 //!   "A load operation with this memory order performs an acquire operation,
40 //!    a store performs a release operation, and read-modify-write performs
41 //!    both an acquire operation and a release operation, plus a single total
42 //!    order exists in which all threads observe all modifications in the same
43 //!    order (see Sequentially-consistent ordering below) "
44 //! So in the absence of weak memory effects a seq-cst load & a seq-cst store is identical
45 //! to an acquire load and a release store given the global sequentially consistent order
46 //! of the schedule.
47 //!
48 //! The timestamps used in the data-race detector assign each sequence of non-atomic operations
49 //! followed by a single atomic or concurrent operation a single timestamp.
50 //! Write, Read, Write, ThreadJoin will be represented by a single timestamp value on a thread.
51 //! This is because extra increment operations between the operations in the sequence are not
52 //! required for accurate reporting of data-race values.
53 //!
54 //! As per the paper a threads timestamp is only incremented after a release operation is performed
55 //! so some atomic operations that only perform acquires do not increment the timestamp. Due to shared
56 //! code some atomic operations may increment the timestamp when not necessary but this has no effect
57 //! on the data-race detection code.
58 //!
59 //! FIXME:
60 //! currently we have our own local copy of the currently active thread index and names, this is due
61 //! in part to the inability to access the current location of threads.active_thread inside the AllocExtra
62 //! read, write and deallocate functions and should be cleaned up in the future.
63
64 use std::{
65     cell::{Cell, Ref, RefCell, RefMut},
66     fmt::Debug,
67     mem,
68 };
69
70 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
71 use rustc_index::vec::{Idx, IndexVec};
72 use rustc_middle::{mir, ty::layout::TyAndLayout};
73 use rustc_target::abi::Size;
74
75 use crate::*;
76
77 pub type AllocExtra = VClockAlloc;
78
79 /// Valid atomic read-write operations, alias of atomic::Ordering (not non-exhaustive).
80 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
81 pub enum AtomicRwOp {
82     Relaxed,
83     Acquire,
84     Release,
85     AcqRel,
86     SeqCst,
87 }
88
89 /// Valid atomic read operations, subset of atomic::Ordering.
90 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
91 pub enum AtomicReadOp {
92     Relaxed,
93     Acquire,
94     SeqCst,
95 }
96
97 /// Valid atomic write operations, subset of atomic::Ordering.
98 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
99 pub enum AtomicWriteOp {
100     Relaxed,
101     Release,
102     SeqCst,
103 }
104
105 /// Valid atomic fence operations, subset of atomic::Ordering.
106 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
107 pub enum AtomicFenceOp {
108     Acquire,
109     Release,
110     AcqRel,
111     SeqCst,
112 }
113
114 /// The current set of vector clocks describing the state
115 /// of a thread, contains the happens-before clock and
116 /// additional metadata to model atomic fence operations.
117 #[derive(Clone, Default, Debug)]
118 struct ThreadClockSet {
119     /// The increasing clock representing timestamps
120     /// that happen-before this thread.
121     clock: VClock,
122
123     /// The set of timestamps that will happen-before this
124     /// thread once it performs an acquire fence.
125     fence_acquire: VClock,
126
127     /// The last timestamp of happens-before relations that
128     /// have been released by this thread by a fence.
129     fence_release: VClock,
130 }
131
132 impl ThreadClockSet {
133     /// Apply the effects of a release fence to this
134     /// set of thread vector clocks.
135     #[inline]
136     fn apply_release_fence(&mut self) {
137         self.fence_release.clone_from(&self.clock);
138     }
139
140     /// Apply the effects of an acquire fence to this
141     /// set of thread vector clocks.
142     #[inline]
143     fn apply_acquire_fence(&mut self) {
144         self.clock.join(&self.fence_acquire);
145     }
146
147     /// Increment the happens-before clock at a
148     /// known index.
149     #[inline]
150     fn increment_clock(&mut self, index: VectorIdx) {
151         self.clock.increment_index(index);
152     }
153
154     /// Join the happens-before clock with that of
155     /// another thread, used to model thread join
156     /// operations.
157     fn join_with(&mut self, other: &ThreadClockSet) {
158         self.clock.join(&other.clock);
159     }
160 }
161
162 /// Error returned by finding a data race
163 /// should be elaborated upon.
164 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
165 pub struct DataRace;
166
167 /// Externally stored memory cell clocks
168 /// explicitly to reduce memory usage for the
169 /// common case where no atomic operations
170 /// exists on the memory cell.
171 #[derive(Clone, PartialEq, Eq, Default, Debug)]
172 struct AtomicMemoryCellClocks {
173     /// The clock-vector of the timestamp of the last atomic
174     /// read operation performed by each thread.
175     /// This detects potential data-races between atomic read
176     /// and non-atomic write operations.
177     read_vector: VClock,
178
179     /// The clock-vector of the timestamp of the last atomic
180     /// write operation performed by each thread.
181     /// This detects potential data-races between atomic write
182     /// and non-atomic read or write operations.
183     write_vector: VClock,
184
185     /// Synchronization vector for acquire-release semantics
186     /// contains the vector of timestamps that will
187     /// happen-before a thread if an acquire-load is
188     /// performed on the data.
189     sync_vector: VClock,
190 }
191
192 /// Type of write operation: allocating memory
193 /// non-atomic writes and deallocating memory
194 /// are all treated as writes for the purpose
195 /// of the data-race detector.
196 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
197 enum WriteType {
198     /// Allocate memory.
199     Allocate,
200
201     /// Standard unsynchronized write.
202     Write,
203
204     /// Deallocate memory.
205     /// Note that when memory is deallocated first, later non-atomic accesses
206     /// will be reported as use-after-free, not as data races.
207     /// (Same for `Allocate` above.)
208     Deallocate,
209 }
210 impl WriteType {
211     fn get_descriptor(self) -> &'static str {
212         match self {
213             WriteType::Allocate => "Allocate",
214             WriteType::Write => "Write",
215             WriteType::Deallocate => "Deallocate",
216         }
217     }
218 }
219
220 /// Memory Cell vector clock metadata
221 /// for data-race detection.
222 #[derive(Clone, PartialEq, Eq, Debug)]
223 struct MemoryCellClocks {
224     /// The vector-clock timestamp of the last write
225     /// corresponding to the writing threads timestamp.
226     write: VTimestamp,
227
228     /// The identifier of the vector index, corresponding to a thread
229     /// that performed the last write operation.
230     write_index: VectorIdx,
231
232     /// The type of operation that the write index represents,
233     /// either newly allocated memory, a non-atomic write or
234     /// a deallocation of memory.
235     write_type: WriteType,
236
237     /// The vector-clock of the timestamp of the last read operation
238     /// performed by a thread since the last write operation occurred.
239     /// It is reset to zero on each write operation.
240     read: VClock,
241
242     /// Atomic acquire & release sequence tracking clocks.
243     /// For non-atomic memory in the common case this
244     /// value is set to None.
245     atomic_ops: Option<Box<AtomicMemoryCellClocks>>,
246 }
247
248 impl MemoryCellClocks {
249     /// Create a new set of clocks representing memory allocated
250     ///  at a given vector timestamp and index.
251     fn new(alloc: VTimestamp, alloc_index: VectorIdx) -> Self {
252         MemoryCellClocks {
253             read: VClock::default(),
254             write: alloc,
255             write_index: alloc_index,
256             write_type: WriteType::Allocate,
257             atomic_ops: None,
258         }
259     }
260
261     /// Load the internal atomic memory cells if they exist.
262     #[inline]
263     fn atomic(&self) -> Option<&AtomicMemoryCellClocks> {
264         match &self.atomic_ops {
265             Some(op) => Some(&*op),
266             None => None,
267         }
268     }
269
270     /// Load or create the internal atomic memory metadata
271     /// if it does not exist.
272     #[inline]
273     fn atomic_mut(&mut self) -> &mut AtomicMemoryCellClocks {
274         self.atomic_ops.get_or_insert_with(Default::default)
275     }
276
277     /// Update memory cell data-race tracking for atomic
278     /// load acquire semantics, is a no-op if this memory was
279     /// not used previously as atomic memory.
280     fn load_acquire(
281         &mut self,
282         clocks: &mut ThreadClockSet,
283         index: VectorIdx,
284     ) -> Result<(), DataRace> {
285         self.atomic_read_detect(clocks, index)?;
286         if let Some(atomic) = self.atomic() {
287             clocks.clock.join(&atomic.sync_vector);
288         }
289         Ok(())
290     }
291
292     /// Update memory cell data-race tracking for atomic
293     /// load relaxed semantics, is a no-op if this memory was
294     /// not used previously as atomic memory.
295     fn load_relaxed(
296         &mut self,
297         clocks: &mut ThreadClockSet,
298         index: VectorIdx,
299     ) -> Result<(), DataRace> {
300         self.atomic_read_detect(clocks, index)?;
301         if let Some(atomic) = self.atomic() {
302             clocks.fence_acquire.join(&atomic.sync_vector);
303         }
304         Ok(())
305     }
306
307     /// Update the memory cell data-race tracking for atomic
308     /// store release semantics.
309     fn store_release(&mut self, clocks: &ThreadClockSet, index: VectorIdx) -> Result<(), DataRace> {
310         self.atomic_write_detect(clocks, index)?;
311         let atomic = self.atomic_mut();
312         atomic.sync_vector.clone_from(&clocks.clock);
313         Ok(())
314     }
315
316     /// Update the memory cell data-race tracking for atomic
317     /// store relaxed semantics.
318     fn store_relaxed(&mut self, clocks: &ThreadClockSet, index: VectorIdx) -> Result<(), DataRace> {
319         self.atomic_write_detect(clocks, index)?;
320
321         // The handling of release sequences was changed in C++20 and so
322         // the code here is different to the paper since now all relaxed
323         // stores block release sequences. The exception for same-thread
324         // relaxed stores has been removed.
325         let atomic = self.atomic_mut();
326         atomic.sync_vector.clone_from(&clocks.fence_release);
327         Ok(())
328     }
329
330     /// Update the memory cell data-race tracking for atomic
331     /// store release semantics for RMW operations.
332     fn rmw_release(&mut self, clocks: &ThreadClockSet, index: VectorIdx) -> Result<(), DataRace> {
333         self.atomic_write_detect(clocks, index)?;
334         let atomic = self.atomic_mut();
335         atomic.sync_vector.join(&clocks.clock);
336         Ok(())
337     }
338
339     /// Update the memory cell data-race tracking for atomic
340     /// store relaxed semantics for RMW operations.
341     fn rmw_relaxed(&mut self, clocks: &ThreadClockSet, index: VectorIdx) -> Result<(), DataRace> {
342         self.atomic_write_detect(clocks, index)?;
343         let atomic = self.atomic_mut();
344         atomic.sync_vector.join(&clocks.fence_release);
345         Ok(())
346     }
347
348     /// Detect data-races with an atomic read, caused by a non-atomic write that does
349     /// not happen-before the atomic-read.
350     fn atomic_read_detect(
351         &mut self,
352         clocks: &ThreadClockSet,
353         index: VectorIdx,
354     ) -> Result<(), DataRace> {
355         log::trace!("Atomic read with vectors: {:#?} :: {:#?}", self, clocks);
356         if self.write <= clocks.clock[self.write_index] {
357             let atomic = self.atomic_mut();
358             atomic.read_vector.set_at_index(&clocks.clock, index);
359             Ok(())
360         } else {
361             Err(DataRace)
362         }
363     }
364
365     /// Detect data-races with an atomic write, either with a non-atomic read or with
366     /// a non-atomic write.
367     fn atomic_write_detect(
368         &mut self,
369         clocks: &ThreadClockSet,
370         index: VectorIdx,
371     ) -> Result<(), DataRace> {
372         log::trace!("Atomic write with vectors: {:#?} :: {:#?}", self, clocks);
373         if self.write <= clocks.clock[self.write_index] && self.read <= clocks.clock {
374             let atomic = self.atomic_mut();
375             atomic.write_vector.set_at_index(&clocks.clock, index);
376             Ok(())
377         } else {
378             Err(DataRace)
379         }
380     }
381
382     /// Detect races for non-atomic read operations at the current memory cell
383     /// returns true if a data-race is detected.
384     fn read_race_detect(
385         &mut self,
386         clocks: &ThreadClockSet,
387         index: VectorIdx,
388     ) -> Result<(), DataRace> {
389         log::trace!("Unsynchronized read with vectors: {:#?} :: {:#?}", self, clocks);
390         if self.write <= clocks.clock[self.write_index] {
391             let race_free = if let Some(atomic) = self.atomic() {
392                 atomic.write_vector <= clocks.clock
393             } else {
394                 true
395             };
396             if race_free {
397                 self.read.set_at_index(&clocks.clock, index);
398                 Ok(())
399             } else {
400                 Err(DataRace)
401             }
402         } else {
403             Err(DataRace)
404         }
405     }
406
407     /// Detect races for non-atomic write operations at the current memory cell
408     /// returns true if a data-race is detected.
409     fn write_race_detect(
410         &mut self,
411         clocks: &ThreadClockSet,
412         index: VectorIdx,
413         write_type: WriteType,
414     ) -> Result<(), DataRace> {
415         log::trace!("Unsynchronized write with vectors: {:#?} :: {:#?}", self, clocks);
416         if self.write <= clocks.clock[self.write_index] && self.read <= clocks.clock {
417             let race_free = if let Some(atomic) = self.atomic() {
418                 atomic.write_vector <= clocks.clock && atomic.read_vector <= clocks.clock
419             } else {
420                 true
421             };
422             if race_free {
423                 self.write = clocks.clock[index];
424                 self.write_index = index;
425                 self.write_type = write_type;
426                 self.read.set_zero_vector();
427                 Ok(())
428             } else {
429                 Err(DataRace)
430             }
431         } else {
432             Err(DataRace)
433         }
434     }
435 }
436
437 /// Evaluation context extensions.
438 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {}
439 pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
440     /// Temporarily allow data-races to occur. This should only be used in
441     /// one of these cases:
442     /// - One of the appropriate `validate_atomic` functions will be called to
443     /// to treat a memory access as atomic.
444     /// - The memory being accessed should be treated as internal state, that
445     /// cannot be accessed by the interpreted program.
446     /// - Execution of the interpreted program execution has halted.
447     #[inline]
448     fn allow_data_races_ref<R>(&self, op: impl FnOnce(&MiriEvalContext<'mir, 'tcx>) -> R) -> R {
449         let this = self.eval_context_ref();
450         let old = if let Some(data_race) = &this.machine.data_race {
451             data_race.multi_threaded.replace(false)
452         } else {
453             false
454         };
455         let result = op(this);
456         if let Some(data_race) = &this.machine.data_race {
457             data_race.multi_threaded.set(old);
458         }
459         result
460     }
461
462     /// Same as `allow_data_races_ref`, this temporarily disables any data-race detection and
463     /// so should only be used for atomic operations or internal state that the program cannot
464     /// access.
465     #[inline]
466     fn allow_data_races_mut<R>(
467         &mut self,
468         op: impl FnOnce(&mut MiriEvalContext<'mir, 'tcx>) -> R,
469     ) -> R {
470         let this = self.eval_context_mut();
471         let old = if let Some(data_race) = &this.machine.data_race {
472             data_race.multi_threaded.replace(false)
473         } else {
474             false
475         };
476         let result = op(this);
477         if let Some(data_race) = &this.machine.data_race {
478             data_race.multi_threaded.set(old);
479         }
480         result
481     }
482
483     /// Atomic variant of read_scalar_at_offset.
484     fn read_scalar_at_offset_atomic(
485         &self,
486         op: &OpTy<'tcx, Tag>,
487         offset: u64,
488         layout: TyAndLayout<'tcx>,
489         atomic: AtomicReadOp,
490     ) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
491         let this = self.eval_context_ref();
492         let value_place = this.deref_operand_and_offset(op, offset, layout)?;
493         this.read_scalar_atomic(&value_place, atomic)
494     }
495
496     /// Atomic variant of write_scalar_at_offset.
497     fn write_scalar_at_offset_atomic(
498         &mut self,
499         op: &OpTy<'tcx, Tag>,
500         offset: u64,
501         value: impl Into<ScalarMaybeUninit<Tag>>,
502         layout: TyAndLayout<'tcx>,
503         atomic: AtomicWriteOp,
504     ) -> InterpResult<'tcx> {
505         let this = self.eval_context_mut();
506         let value_place = this.deref_operand_and_offset(op, offset, layout)?;
507         this.write_scalar_atomic(value.into(), &value_place, atomic)
508     }
509
510     /// Perform an atomic read operation at the memory location.
511     fn read_scalar_atomic(
512         &self,
513         place: &MPlaceTy<'tcx, Tag>,
514         atomic: AtomicReadOp,
515     ) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
516         let this = self.eval_context_ref();
517         let scalar = this.allow_data_races_ref(move |this| this.read_scalar(&place.into()))?;
518         this.validate_atomic_load(place, atomic)?;
519         Ok(scalar)
520     }
521
522     /// Perform an atomic write operation at the memory location.
523     fn write_scalar_atomic(
524         &mut self,
525         val: ScalarMaybeUninit<Tag>,
526         dest: &MPlaceTy<'tcx, Tag>,
527         atomic: AtomicWriteOp,
528     ) -> InterpResult<'tcx> {
529         let this = self.eval_context_mut();
530         this.allow_data_races_mut(move |this| this.write_scalar(val, &(*dest).into()))?;
531         this.validate_atomic_store(dest, atomic)
532     }
533
534     /// Perform an atomic operation on a memory location.
535     fn atomic_op_immediate(
536         &mut self,
537         place: &MPlaceTy<'tcx, Tag>,
538         rhs: &ImmTy<'tcx, Tag>,
539         op: mir::BinOp,
540         neg: bool,
541         atomic: AtomicRwOp,
542     ) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> {
543         let this = self.eval_context_mut();
544
545         let old = this.allow_data_races_mut(|this| this.read_immediate(&place.into()))?;
546
547         // Atomics wrap around on overflow.
548         let val = this.binary_op(op, &old, rhs)?;
549         let val = if neg { this.unary_op(mir::UnOp::Not, &val)? } else { val };
550         this.allow_data_races_mut(|this| this.write_immediate(*val, &(*place).into()))?;
551
552         this.validate_atomic_rmw(place, atomic)?;
553         Ok(old)
554     }
555
556     /// Perform an atomic exchange with a memory place and a new
557     /// scalar value, the old value is returned.
558     fn atomic_exchange_scalar(
559         &mut self,
560         place: &MPlaceTy<'tcx, Tag>,
561         new: ScalarMaybeUninit<Tag>,
562         atomic: AtomicRwOp,
563     ) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
564         let this = self.eval_context_mut();
565
566         let old = this.allow_data_races_mut(|this| this.read_scalar(&place.into()))?;
567         this.allow_data_races_mut(|this| this.write_scalar(new, &(*place).into()))?;
568         this.validate_atomic_rmw(place, atomic)?;
569         Ok(old)
570     }
571
572     /// Perform an conditional atomic exchange with a memory place and a new
573     /// scalar value, the old value is returned.
574     fn atomic_min_max_scalar(
575         &mut self,
576         place: &MPlaceTy<'tcx, Tag>,
577         rhs: ImmTy<'tcx, Tag>,
578         min: bool,
579         atomic: AtomicRwOp,
580     ) -> InterpResult<'tcx, ImmTy<'tcx, Tag>> {
581         let this = self.eval_context_mut();
582
583         let old = this.allow_data_races_mut(|this| this.read_immediate(&place.into()))?;
584         let lt = this.binary_op(mir::BinOp::Lt, &old, &rhs)?.to_scalar()?.to_bool()?;
585
586         let new_val = if min {
587             if lt { &old } else { &rhs }
588         } else {
589             if lt { &rhs } else { &old }
590         };
591
592         this.allow_data_races_mut(|this| this.write_immediate(**new_val, &(*place).into()))?;
593
594         this.validate_atomic_rmw(place, atomic)?;
595
596         // Return the old value.
597         Ok(old)
598     }
599
600     /// Perform an atomic compare and exchange at a given memory location.
601     /// On success an atomic RMW operation is performed and on failure
602     /// only an atomic read occurs. If `can_fail_spuriously` is true,
603     /// then we treat it as a "compare_exchange_weak" operation, and
604     /// some portion of the time fail even when the values are actually
605     /// identical.
606     fn atomic_compare_exchange_scalar(
607         &mut self,
608         place: &MPlaceTy<'tcx, Tag>,
609         expect_old: &ImmTy<'tcx, Tag>,
610         new: ScalarMaybeUninit<Tag>,
611         success: AtomicRwOp,
612         fail: AtomicReadOp,
613         can_fail_spuriously: bool,
614     ) -> InterpResult<'tcx, Immediate<Tag>> {
615         use rand::Rng as _;
616         let this = self.eval_context_mut();
617
618         // Failure ordering cannot be stronger than success ordering, therefore first attempt
619         // to read with the failure ordering and if successful then try again with the success
620         // read ordering and write in the success case.
621         // Read as immediate for the sake of `binary_op()`
622         let old = this.allow_data_races_mut(|this| this.read_immediate(&(place.into())))?;
623         // `binary_op` will bail if either of them is not a scalar.
624         let eq = this.binary_op(mir::BinOp::Eq, &old, expect_old)?;
625         // If the operation would succeed, but is "weak", fail some portion
626         // of the time, based on `rate`.
627         let rate = this.machine.cmpxchg_weak_failure_rate;
628         let cmpxchg_success = eq.to_scalar()?.to_bool()?
629             && (!can_fail_spuriously || this.machine.rng.get_mut().gen::<f64>() < rate);
630         let res = Immediate::ScalarPair(
631             old.to_scalar_or_uninit(),
632             Scalar::from_bool(cmpxchg_success).into(),
633         );
634
635         // Update ptr depending on comparison.
636         // if successful, perform a full rw-atomic validation
637         // otherwise treat this as an atomic load with the fail ordering.
638         if cmpxchg_success {
639             this.allow_data_races_mut(|this| this.write_scalar(new, &(*place).into()))?;
640             this.validate_atomic_rmw(place, success)?;
641         } else {
642             this.validate_atomic_load(place, fail)?;
643         }
644
645         // Return the old value.
646         Ok(res)
647     }
648
649     /// Update the data-race detector for an atomic read occurring at the
650     /// associated memory-place and on the current thread.
651     fn validate_atomic_load(
652         &self,
653         place: &MPlaceTy<'tcx, Tag>,
654         atomic: AtomicReadOp,
655     ) -> InterpResult<'tcx> {
656         let this = self.eval_context_ref();
657         this.validate_atomic_op(
658             place,
659             atomic,
660             "Atomic Load",
661             move |memory, clocks, index, atomic| {
662                 if atomic == AtomicReadOp::Relaxed {
663                     memory.load_relaxed(&mut *clocks, index)
664                 } else {
665                     memory.load_acquire(&mut *clocks, index)
666                 }
667             },
668         )
669     }
670
671     /// Update the data-race detector for an atomic write occurring at the
672     /// associated memory-place and on the current thread.
673     fn validate_atomic_store(
674         &mut self,
675         place: &MPlaceTy<'tcx, Tag>,
676         atomic: AtomicWriteOp,
677     ) -> InterpResult<'tcx> {
678         let this = self.eval_context_mut();
679         this.validate_atomic_op(
680             place,
681             atomic,
682             "Atomic Store",
683             move |memory, clocks, index, atomic| {
684                 if atomic == AtomicWriteOp::Relaxed {
685                     memory.store_relaxed(clocks, index)
686                 } else {
687                     memory.store_release(clocks, index)
688                 }
689             },
690         )
691     }
692
693     /// Update the data-race detector for an atomic read-modify-write occurring
694     /// at the associated memory place and on the current thread.
695     fn validate_atomic_rmw(
696         &mut self,
697         place: &MPlaceTy<'tcx, Tag>,
698         atomic: AtomicRwOp,
699     ) -> InterpResult<'tcx> {
700         use AtomicRwOp::*;
701         let acquire = matches!(atomic, Acquire | AcqRel | SeqCst);
702         let release = matches!(atomic, Release | AcqRel | SeqCst);
703         let this = self.eval_context_mut();
704         this.validate_atomic_op(place, atomic, "Atomic RMW", move |memory, clocks, index, _| {
705             if acquire {
706                 memory.load_acquire(clocks, index)?;
707             } else {
708                 memory.load_relaxed(clocks, index)?;
709             }
710             if release {
711                 memory.rmw_release(clocks, index)
712             } else {
713                 memory.rmw_relaxed(clocks, index)
714             }
715         })
716     }
717
718     /// Update the data-race detector for an atomic fence on the current thread.
719     fn validate_atomic_fence(&mut self, atomic: AtomicFenceOp) -> InterpResult<'tcx> {
720         let this = self.eval_context_mut();
721         if let Some(data_race) = &mut this.machine.data_race {
722             data_race.maybe_perform_sync_operation(move |index, mut clocks| {
723                 log::trace!("Atomic fence on {:?} with ordering {:?}", index, atomic);
724
725                 // Apply data-race detection for the current fences
726                 // this treats AcqRel and SeqCst as the same as an acquire
727                 // and release fence applied in the same timestamp.
728                 if atomic != AtomicFenceOp::Release {
729                     // Either Acquire | AcqRel | SeqCst
730                     clocks.apply_acquire_fence();
731                 }
732                 if atomic != AtomicFenceOp::Acquire {
733                     // Either Release | AcqRel | SeqCst
734                     clocks.apply_release_fence();
735                 }
736
737                 // Increment timestamp in case of release semantics.
738                 Ok(atomic != AtomicFenceOp::Acquire)
739             })
740         } else {
741             Ok(())
742         }
743     }
744 }
745
746 /// Vector clock metadata for a logical memory allocation.
747 #[derive(Debug, Clone)]
748 pub struct VClockAlloc {
749     /// Assigning each byte a MemoryCellClocks.
750     alloc_ranges: RefCell<RangeMap<MemoryCellClocks>>,
751 }
752
753 impl VClockAlloc {
754     /// Create a new data-race detector for newly allocated memory.
755     pub fn new_allocation(
756         global: &GlobalState,
757         len: Size,
758         kind: MemoryKind<MiriMemoryKind>,
759     ) -> VClockAlloc {
760         let (alloc_timestamp, alloc_index) = match kind {
761             // User allocated and stack memory should track allocation.
762             MemoryKind::Machine(
763                 MiriMemoryKind::Rust | MiriMemoryKind::C | MiriMemoryKind::WinHeap,
764             )
765             | MemoryKind::Stack => {
766                 let (alloc_index, clocks) = global.current_thread_state();
767                 let alloc_timestamp = clocks.clock[alloc_index];
768                 (alloc_timestamp, alloc_index)
769             }
770             // Other global memory should trace races but be allocated at the 0 timestamp.
771             MemoryKind::Machine(
772                 MiriMemoryKind::Global
773                 | MiriMemoryKind::Machine
774                 | MiriMemoryKind::Runtime
775                 | MiriMemoryKind::ExternStatic
776                 | MiriMemoryKind::Tls,
777             )
778             | MemoryKind::CallerLocation => (0, VectorIdx::MAX_INDEX),
779         };
780         VClockAlloc {
781             alloc_ranges: RefCell::new(RangeMap::new(
782                 len,
783                 MemoryCellClocks::new(alloc_timestamp, alloc_index),
784             )),
785         }
786     }
787
788     // Find an index, if one exists where the value
789     // in `l` is greater than the value in `r`.
790     fn find_gt_index(l: &VClock, r: &VClock) -> Option<VectorIdx> {
791         log::trace!("Find index where not {:?} <= {:?}", l, r);
792         let l_slice = l.as_slice();
793         let r_slice = r.as_slice();
794         l_slice
795             .iter()
796             .zip(r_slice.iter())
797             .enumerate()
798             .find_map(|(idx, (&l, &r))| if l > r { Some(idx) } else { None })
799             .or_else(|| {
800                 if l_slice.len() > r_slice.len() {
801                     // By invariant, if l_slice is longer
802                     // then one element must be larger.
803                     // This just validates that this is true
804                     // and reports earlier elements first.
805                     let l_remainder_slice = &l_slice[r_slice.len()..];
806                     let idx = l_remainder_slice
807                         .iter()
808                         .enumerate()
809                         .find_map(|(idx, &r)| if r == 0 { None } else { Some(idx) })
810                         .expect("Invalid VClock Invariant");
811                     Some(idx + r_slice.len())
812                 } else {
813                     None
814                 }
815             })
816             .map(VectorIdx::new)
817     }
818
819     /// Report a data-race found in the program.
820     /// This finds the two racing threads and the type
821     /// of data-race that occurred. This will also
822     /// return info about the memory location the data-race
823     /// occurred in.
824     #[cold]
825     #[inline(never)]
826     fn report_data_race<'tcx>(
827         global: &GlobalState,
828         range: &MemoryCellClocks,
829         action: &str,
830         is_atomic: bool,
831         ptr_dbg: Pointer<AllocId>,
832     ) -> InterpResult<'tcx> {
833         let (current_index, current_clocks) = global.current_thread_state();
834         let write_clock;
835         let (other_action, other_thread, other_clock) = if range.write
836             > current_clocks.clock[range.write_index]
837         {
838             // Convert the write action into the vector clock it
839             // represents for diagnostic purposes.
840             write_clock = VClock::new_with_index(range.write_index, range.write);
841             (range.write_type.get_descriptor(), range.write_index, &write_clock)
842         } else if let Some(idx) = Self::find_gt_index(&range.read, &current_clocks.clock) {
843             ("Read", idx, &range.read)
844         } else if !is_atomic {
845             if let Some(atomic) = range.atomic() {
846                 if let Some(idx) = Self::find_gt_index(&atomic.write_vector, &current_clocks.clock)
847                 {
848                     ("Atomic Store", idx, &atomic.write_vector)
849                 } else if let Some(idx) =
850                     Self::find_gt_index(&atomic.read_vector, &current_clocks.clock)
851                 {
852                     ("Atomic Load", idx, &atomic.read_vector)
853                 } else {
854                     unreachable!(
855                         "Failed to report data-race for non-atomic operation: no race found"
856                     )
857                 }
858             } else {
859                 unreachable!(
860                     "Failed to report data-race for non-atomic operation: no atomic component"
861                 )
862             }
863         } else {
864             unreachable!("Failed to report data-race for atomic operation")
865         };
866
867         // Load elaborated thread information about the racing thread actions.
868         let current_thread_info = global.print_thread_metadata(current_index);
869         let other_thread_info = global.print_thread_metadata(other_thread);
870
871         // Throw the data-race detection.
872         throw_ub_format!(
873             "Data race detected between {} on {} and {} on {} at {:?} (current vector clock = {:?}, conflicting timestamp = {:?})",
874             action,
875             current_thread_info,
876             other_action,
877             other_thread_info,
878             ptr_dbg,
879             current_clocks.clock,
880             other_clock
881         )
882     }
883
884     /// Detect data-races for an unsynchronized read operation, will not perform
885     /// data-race detection if `multi-threaded` is false, either due to no threads
886     /// being created or if it is temporarily disabled during a racy read or write
887     /// operation for which data-race detection is handled separately, for example
888     /// atomic read operations.
889     pub fn read<'tcx>(
890         &self,
891         alloc_id: AllocId,
892         range: AllocRange,
893         global: &GlobalState,
894     ) -> InterpResult<'tcx> {
895         if global.multi_threaded.get() {
896             let (index, clocks) = global.current_thread_state();
897             let mut alloc_ranges = self.alloc_ranges.borrow_mut();
898             for (offset, range) in alloc_ranges.iter_mut(range.start, range.size) {
899                 if let Err(DataRace) = range.read_race_detect(&*clocks, index) {
900                     // Report data-race.
901                     return Self::report_data_race(
902                         global,
903                         range,
904                         "Read",
905                         false,
906                         Pointer::new(alloc_id, offset),
907                     );
908                 }
909             }
910             Ok(())
911         } else {
912             Ok(())
913         }
914     }
915
916     // Shared code for detecting data-races on unique access to a section of memory
917     fn unique_access<'tcx>(
918         &mut self,
919         alloc_id: AllocId,
920         range: AllocRange,
921         write_type: WriteType,
922         global: &mut GlobalState,
923     ) -> InterpResult<'tcx> {
924         if global.multi_threaded.get() {
925             let (index, clocks) = global.current_thread_state();
926             for (offset, range) in self.alloc_ranges.get_mut().iter_mut(range.start, range.size) {
927                 if let Err(DataRace) = range.write_race_detect(&*clocks, index, write_type) {
928                     // Report data-race
929                     return Self::report_data_race(
930                         global,
931                         range,
932                         write_type.get_descriptor(),
933                         false,
934                         Pointer::new(alloc_id, offset),
935                     );
936                 }
937             }
938             Ok(())
939         } else {
940             Ok(())
941         }
942     }
943
944     /// Detect data-races for an unsynchronized write operation, will not perform
945     /// data-race threads if `multi-threaded` is false, either due to no threads
946     /// being created or if it is temporarily disabled during a racy read or write
947     /// operation
948     pub fn write<'tcx>(
949         &mut self,
950         alloc_id: AllocId,
951         range: AllocRange,
952         global: &mut GlobalState,
953     ) -> InterpResult<'tcx> {
954         self.unique_access(alloc_id, range, WriteType::Write, global)
955     }
956
957     /// Detect data-races for an unsynchronized deallocate operation, will not perform
958     /// data-race threads if `multi-threaded` is false, either due to no threads
959     /// being created or if it is temporarily disabled during a racy read or write
960     /// operation
961     pub fn deallocate<'tcx>(
962         &mut self,
963         alloc_id: AllocId,
964         range: AllocRange,
965         global: &mut GlobalState,
966     ) -> InterpResult<'tcx> {
967         self.unique_access(alloc_id, range, WriteType::Deallocate, global)
968     }
969 }
970
971 impl<'mir, 'tcx: 'mir> EvalContextPrivExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {}
972 trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
973     /// Generic atomic operation implementation
974     fn validate_atomic_op<A: Debug + Copy>(
975         &self,
976         place: &MPlaceTy<'tcx, Tag>,
977         atomic: A,
978         description: &str,
979         mut op: impl FnMut(
980             &mut MemoryCellClocks,
981             &mut ThreadClockSet,
982             VectorIdx,
983             A,
984         ) -> Result<(), DataRace>,
985     ) -> InterpResult<'tcx> {
986         let this = self.eval_context_ref();
987         if let Some(data_race) = &this.machine.data_race {
988             if data_race.multi_threaded.get() {
989                 let size = place.layout.size;
990                 let (alloc_id, base_offset, _tag) = this.ptr_get_alloc_id(place.ptr)?;
991                 // Load and log the atomic operation.
992                 // Note that atomic loads are possible even from read-only allocations, so `get_alloc_extra_mut` is not an option.
993                 let alloc_meta = &this.get_alloc_extra(alloc_id)?.data_race.as_ref().unwrap();
994                 log::trace!(
995                     "Atomic op({}) with ordering {:?} on {:?} (size={})",
996                     description,
997                     &atomic,
998                     place.ptr,
999                     size.bytes()
1000                 );
1001
1002                 // Perform the atomic operation.
1003                 data_race.maybe_perform_sync_operation(|index, mut clocks| {
1004                     for (offset, range) in
1005                         alloc_meta.alloc_ranges.borrow_mut().iter_mut(base_offset, size)
1006                     {
1007                         if let Err(DataRace) = op(range, &mut *clocks, index, atomic) {
1008                             mem::drop(clocks);
1009                             return VClockAlloc::report_data_race(
1010                                 data_race,
1011                                 range,
1012                                 description,
1013                                 true,
1014                                 Pointer::new(alloc_id, offset),
1015                             )
1016                             .map(|_| true);
1017                         }
1018                     }
1019
1020                     // This conservatively assumes all operations have release semantics
1021                     Ok(true)
1022                 })?;
1023
1024                 // Log changes to atomic memory.
1025                 if log::log_enabled!(log::Level::Trace) {
1026                     for (_offset, range) in alloc_meta.alloc_ranges.borrow().iter(base_offset, size)
1027                     {
1028                         log::trace!(
1029                             "Updated atomic memory({:?}, size={}) to {:#?}",
1030                             place.ptr,
1031                             size.bytes(),
1032                             range.atomic_ops
1033                         );
1034                     }
1035                 }
1036             }
1037         }
1038         Ok(())
1039     }
1040 }
1041
1042 /// Extra metadata associated with a thread.
1043 #[derive(Debug, Clone, Default)]
1044 struct ThreadExtraState {
1045     /// The current vector index in use by the
1046     /// thread currently, this is set to None
1047     /// after the vector index has been re-used
1048     /// and hence the value will never need to be
1049     /// read during data-race reporting.
1050     vector_index: Option<VectorIdx>,
1051
1052     /// The name of the thread, updated for better
1053     /// diagnostics when reporting detected data
1054     /// races.
1055     thread_name: Option<Box<str>>,
1056
1057     /// Thread termination vector clock, this
1058     /// is set on thread termination and is used
1059     /// for joining on threads since the vector_index
1060     /// may be re-used when the join operation occurs.
1061     termination_vector_clock: Option<VClock>,
1062 }
1063
1064 /// Global data-race detection state, contains the currently
1065 /// executing thread as well as the vector-clocks associated
1066 /// with each of the threads.
1067 // FIXME: it is probably better to have one large RefCell, than to have so many small ones.
1068 #[derive(Debug, Clone)]
1069 pub struct GlobalState {
1070     /// Set to true once the first additional
1071     /// thread has launched, due to the dependency
1072     /// between before and after a thread launch.
1073     /// Any data-races must be recorded after this
1074     /// so concurrent execution can ignore recording
1075     /// any data-races.
1076     multi_threaded: Cell<bool>,
1077
1078     /// Mapping of a vector index to a known set of thread
1079     /// clocks, this is not directly mapping from a thread id
1080     /// since it may refer to multiple threads.
1081     vector_clocks: RefCell<IndexVec<VectorIdx, ThreadClockSet>>,
1082
1083     /// Mapping of a given vector index to the current thread
1084     /// that the execution is representing, this may change
1085     /// if a vector index is re-assigned to a new thread.
1086     vector_info: RefCell<IndexVec<VectorIdx, ThreadId>>,
1087
1088     /// The mapping of a given thread to associated thread metadata.
1089     thread_info: RefCell<IndexVec<ThreadId, ThreadExtraState>>,
1090
1091     /// The current vector index being executed.
1092     current_index: Cell<VectorIdx>,
1093
1094     /// Potential vector indices that could be re-used on thread creation
1095     /// values are inserted here on after the thread has terminated and
1096     /// been joined with, and hence may potentially become free
1097     /// for use as the index for a new thread.
1098     /// Elements in this set may still require the vector index to
1099     /// report data-races, and can only be re-used after all
1100     /// active vector-clocks catch up with the threads timestamp.
1101     reuse_candidates: RefCell<FxHashSet<VectorIdx>>,
1102
1103     /// Counts the number of threads that are currently active
1104     /// if the number of active threads reduces to 1 and then
1105     /// a join operation occurs with the remaining main thread
1106     /// then multi-threaded execution may be disabled.
1107     active_thread_count: Cell<usize>,
1108
1109     /// This contains threads that have terminated, but not yet joined
1110     /// and so cannot become re-use candidates until a join operation
1111     /// occurs.
1112     /// The associated vector index will be moved into re-use candidates
1113     /// after the join operation occurs.
1114     terminated_threads: RefCell<FxHashMap<ThreadId, VectorIdx>>,
1115 }
1116
1117 impl GlobalState {
1118     /// Create a new global state, setup with just thread-id=0
1119     /// advanced to timestamp = 1.
1120     pub fn new() -> Self {
1121         let mut global_state = GlobalState {
1122             multi_threaded: Cell::new(false),
1123             vector_clocks: RefCell::new(IndexVec::new()),
1124             vector_info: RefCell::new(IndexVec::new()),
1125             thread_info: RefCell::new(IndexVec::new()),
1126             current_index: Cell::new(VectorIdx::new(0)),
1127             active_thread_count: Cell::new(1),
1128             reuse_candidates: RefCell::new(FxHashSet::default()),
1129             terminated_threads: RefCell::new(FxHashMap::default()),
1130         };
1131
1132         // Setup the main-thread since it is not explicitly created:
1133         // uses vector index and thread-id 0, also the rust runtime gives
1134         // the main-thread a name of "main".
1135         let index = global_state.vector_clocks.get_mut().push(ThreadClockSet::default());
1136         global_state.vector_info.get_mut().push(ThreadId::new(0));
1137         global_state.thread_info.get_mut().push(ThreadExtraState {
1138             vector_index: Some(index),
1139             thread_name: Some("main".to_string().into_boxed_str()),
1140             termination_vector_clock: None,
1141         });
1142
1143         global_state
1144     }
1145
1146     // Try to find vector index values that can potentially be re-used
1147     // by a new thread instead of a new vector index being created.
1148     fn find_vector_index_reuse_candidate(&self) -> Option<VectorIdx> {
1149         let mut reuse = self.reuse_candidates.borrow_mut();
1150         let vector_clocks = self.vector_clocks.borrow();
1151         let vector_info = self.vector_info.borrow();
1152         let terminated_threads = self.terminated_threads.borrow();
1153         for &candidate in reuse.iter() {
1154             let target_timestamp = vector_clocks[candidate].clock[candidate];
1155             if vector_clocks.iter_enumerated().all(|(clock_idx, clock)| {
1156                 // The thread happens before the clock, and hence cannot report
1157                 // a data-race with this the candidate index.
1158                 let no_data_race = clock.clock[candidate] >= target_timestamp;
1159
1160                 // The vector represents a thread that has terminated and hence cannot
1161                 // report a data-race with the candidate index.
1162                 let thread_id = vector_info[clock_idx];
1163                 let vector_terminated =
1164                     reuse.contains(&clock_idx) || terminated_threads.contains_key(&thread_id);
1165
1166                 // The vector index cannot report a race with the candidate index
1167                 // and hence allows the candidate index to be re-used.
1168                 no_data_race || vector_terminated
1169             }) {
1170                 // All vector clocks for each vector index are equal to
1171                 // the target timestamp, and the thread is known to have
1172                 // terminated, therefore this vector clock index cannot
1173                 // report any more data-races.
1174                 assert!(reuse.remove(&candidate));
1175                 return Some(candidate);
1176             }
1177         }
1178         None
1179     }
1180
1181     // Hook for thread creation, enabled multi-threaded execution and marks
1182     // the current thread timestamp as happening-before the current thread.
1183     #[inline]
1184     pub fn thread_created(&mut self, thread: ThreadId) {
1185         let current_index = self.current_index();
1186
1187         // Increment the number of active threads.
1188         let active_threads = self.active_thread_count.get();
1189         self.active_thread_count.set(active_threads + 1);
1190
1191         // Enable multi-threaded execution, there are now two threads
1192         // so data-races are now possible.
1193         self.multi_threaded.set(true);
1194
1195         // Load and setup the associated thread metadata
1196         let mut thread_info = self.thread_info.borrow_mut();
1197         thread_info.ensure_contains_elem(thread, Default::default);
1198
1199         // Assign a vector index for the thread, attempting to re-use an old
1200         // vector index that can no longer report any data-races if possible.
1201         let created_index = if let Some(reuse_index) = self.find_vector_index_reuse_candidate() {
1202             // Now re-configure the re-use candidate, increment the clock
1203             // for the new sync use of the vector.
1204             let vector_clocks = self.vector_clocks.get_mut();
1205             vector_clocks[reuse_index].increment_clock(reuse_index);
1206
1207             // Locate the old thread the vector was associated with and update
1208             // it to represent the new thread instead.
1209             let vector_info = self.vector_info.get_mut();
1210             let old_thread = vector_info[reuse_index];
1211             vector_info[reuse_index] = thread;
1212
1213             // Mark the thread the vector index was associated with as no longer
1214             // representing a thread index.
1215             thread_info[old_thread].vector_index = None;
1216
1217             reuse_index
1218         } else {
1219             // No vector re-use candidates available, instead create
1220             // a new vector index.
1221             let vector_info = self.vector_info.get_mut();
1222             vector_info.push(thread)
1223         };
1224
1225         log::trace!("Creating thread = {:?} with vector index = {:?}", thread, created_index);
1226
1227         // Mark the chosen vector index as in use by the thread.
1228         thread_info[thread].vector_index = Some(created_index);
1229
1230         // Create a thread clock set if applicable.
1231         let vector_clocks = self.vector_clocks.get_mut();
1232         if created_index == vector_clocks.next_index() {
1233             vector_clocks.push(ThreadClockSet::default());
1234         }
1235
1236         // Now load the two clocks and configure the initial state.
1237         let (current, created) = vector_clocks.pick2_mut(current_index, created_index);
1238
1239         // Join the created with current, since the current threads
1240         // previous actions happen-before the created thread.
1241         created.join_with(current);
1242
1243         // Advance both threads after the synchronized operation.
1244         // Both operations are considered to have release semantics.
1245         current.increment_clock(current_index);
1246         created.increment_clock(created_index);
1247     }
1248
1249     /// Hook on a thread join to update the implicit happens-before relation
1250     /// between the joined thread and the current thread.
1251     #[inline]
1252     pub fn thread_joined(&mut self, current_thread: ThreadId, join_thread: ThreadId) {
1253         let clocks_vec = self.vector_clocks.get_mut();
1254         let thread_info = self.thread_info.get_mut();
1255
1256         // Load the vector clock of the current thread.
1257         let current_index = thread_info[current_thread]
1258             .vector_index
1259             .expect("Performed thread join on thread with no assigned vector");
1260         let current = &mut clocks_vec[current_index];
1261
1262         // Load the associated vector clock for the terminated thread.
1263         let join_clock = thread_info[join_thread]
1264             .termination_vector_clock
1265             .as_ref()
1266             .expect("Joined with thread but thread has not terminated");
1267
1268         // The join thread happens-before the current thread
1269         // so update the current vector clock.
1270         // Is not a release operation so the clock is not incremented.
1271         current.clock.join(join_clock);
1272
1273         // Check the number of active threads, if the value is 1
1274         // then test for potentially disabling multi-threaded execution.
1275         let active_threads = self.active_thread_count.get();
1276         if active_threads == 1 {
1277             // May potentially be able to disable multi-threaded execution.
1278             let current_clock = &clocks_vec[current_index];
1279             if clocks_vec
1280                 .iter_enumerated()
1281                 .all(|(idx, clocks)| clocks.clock[idx] <= current_clock.clock[idx])
1282             {
1283                 // All thread terminations happen-before the current clock
1284                 // therefore no data-races can be reported until a new thread
1285                 // is created, so disable multi-threaded execution.
1286                 self.multi_threaded.set(false);
1287             }
1288         }
1289
1290         // If the thread is marked as terminated but not joined
1291         // then move the thread to the re-use set.
1292         let termination = self.terminated_threads.get_mut();
1293         if let Some(index) = termination.remove(&join_thread) {
1294             let reuse = self.reuse_candidates.get_mut();
1295             reuse.insert(index);
1296         }
1297     }
1298
1299     /// On thread termination, the vector-clock may re-used
1300     /// in the future once all remaining thread-clocks catch
1301     /// up with the time index of the terminated thread.
1302     /// This assigns thread termination with a unique index
1303     /// which will be used to join the thread
1304     /// This should be called strictly before any calls to
1305     /// `thread_joined`.
1306     #[inline]
1307     pub fn thread_terminated(&mut self) {
1308         let current_index = self.current_index();
1309
1310         // Increment the clock to a unique termination timestamp.
1311         let vector_clocks = self.vector_clocks.get_mut();
1312         let current_clocks = &mut vector_clocks[current_index];
1313         current_clocks.increment_clock(current_index);
1314
1315         // Load the current thread id for the executing vector.
1316         let vector_info = self.vector_info.get_mut();
1317         let current_thread = vector_info[current_index];
1318
1319         // Load the current thread metadata, and move to a terminated
1320         // vector state. Setting up the vector clock all join operations
1321         // will use.
1322         let thread_info = self.thread_info.get_mut();
1323         let current = &mut thread_info[current_thread];
1324         current.termination_vector_clock = Some(current_clocks.clock.clone());
1325
1326         // Add this thread as a candidate for re-use after a thread join
1327         // occurs.
1328         let termination = self.terminated_threads.get_mut();
1329         termination.insert(current_thread, current_index);
1330
1331         // Reduce the number of active threads, now that a thread has
1332         // terminated.
1333         let mut active_threads = self.active_thread_count.get();
1334         active_threads -= 1;
1335         self.active_thread_count.set(active_threads);
1336     }
1337
1338     /// Hook for updating the local tracker of the currently
1339     /// enabled thread, should always be updated whenever
1340     /// `active_thread` in thread.rs is updated.
1341     #[inline]
1342     pub fn thread_set_active(&self, thread: ThreadId) {
1343         let thread_info = self.thread_info.borrow();
1344         let vector_idx = thread_info[thread]
1345             .vector_index
1346             .expect("Setting thread active with no assigned vector");
1347         self.current_index.set(vector_idx);
1348     }
1349
1350     /// Hook for updating the local tracker of the threads name
1351     /// this should always mirror the local value in thread.rs
1352     /// the thread name is used for improved diagnostics
1353     /// during a data-race.
1354     #[inline]
1355     pub fn thread_set_name(&mut self, thread: ThreadId, name: String) {
1356         let name = name.into_boxed_str();
1357         let thread_info = self.thread_info.get_mut();
1358         thread_info[thread].thread_name = Some(name);
1359     }
1360
1361     /// Attempt to perform a synchronized operation, this
1362     /// will perform no operation if multi-threading is
1363     /// not currently enabled.
1364     /// Otherwise it will increment the clock for the current
1365     /// vector before and after the operation for data-race
1366     /// detection between any happens-before edges the
1367     /// operation may create.
1368     fn maybe_perform_sync_operation<'tcx>(
1369         &self,
1370         op: impl FnOnce(VectorIdx, RefMut<'_, ThreadClockSet>) -> InterpResult<'tcx, bool>,
1371     ) -> InterpResult<'tcx> {
1372         if self.multi_threaded.get() {
1373             let (index, clocks) = self.current_thread_state_mut();
1374             if op(index, clocks)? {
1375                 let (_, mut clocks) = self.current_thread_state_mut();
1376                 clocks.increment_clock(index);
1377             }
1378         }
1379         Ok(())
1380     }
1381
1382     /// Internal utility to identify a thread stored internally
1383     /// returns the id and the name for better diagnostics.
1384     fn print_thread_metadata(&self, vector: VectorIdx) -> String {
1385         let thread = self.vector_info.borrow()[vector];
1386         let thread_name = &self.thread_info.borrow()[thread].thread_name;
1387         if let Some(name) = thread_name {
1388             let name: &str = name;
1389             format!("Thread(id = {:?}, name = {:?})", thread.to_u32(), &*name)
1390         } else {
1391             format!("Thread(id = {:?})", thread.to_u32())
1392         }
1393     }
1394
1395     /// Acquire a lock, express that the previous call of
1396     /// `validate_lock_release` must happen before this.
1397     /// As this is an acquire operation, the thread timestamp is not
1398     /// incremented.
1399     pub fn validate_lock_acquire(&self, lock: &VClock, thread: ThreadId) {
1400         let (_, mut clocks) = self.load_thread_state_mut(thread);
1401         clocks.clock.join(lock);
1402     }
1403
1404     /// Release a lock handle, express that this happens-before
1405     /// any subsequent calls to `validate_lock_acquire`.
1406     /// For normal locks this should be equivalent to `validate_lock_release_shared`
1407     /// since an acquire operation should have occurred before, however
1408     /// for futex & condvar operations this is not the case and this
1409     /// operation must be used.
1410     pub fn validate_lock_release(&self, lock: &mut VClock, thread: ThreadId) {
1411         let (index, mut clocks) = self.load_thread_state_mut(thread);
1412         lock.clone_from(&clocks.clock);
1413         clocks.increment_clock(index);
1414     }
1415
1416     /// Release a lock handle, express that this happens-before
1417     /// any subsequent calls to `validate_lock_acquire` as well
1418     /// as any previous calls to this function after any
1419     /// `validate_lock_release` calls.
1420     /// For normal locks this should be equivalent to `validate_lock_release`.
1421     /// This function only exists for joining over the set of concurrent readers
1422     /// in a read-write lock and should not be used for anything else.
1423     pub fn validate_lock_release_shared(&self, lock: &mut VClock, thread: ThreadId) {
1424         let (index, mut clocks) = self.load_thread_state_mut(thread);
1425         lock.join(&clocks.clock);
1426         clocks.increment_clock(index);
1427     }
1428
1429     /// Load the vector index used by the given thread as well as the set of vector clocks
1430     /// used by the thread.
1431     #[inline]
1432     fn load_thread_state_mut(&self, thread: ThreadId) -> (VectorIdx, RefMut<'_, ThreadClockSet>) {
1433         let index = self.thread_info.borrow()[thread]
1434             .vector_index
1435             .expect("Loading thread state for thread with no assigned vector");
1436         let ref_vector = self.vector_clocks.borrow_mut();
1437         let clocks = RefMut::map(ref_vector, |vec| &mut vec[index]);
1438         (index, clocks)
1439     }
1440
1441     /// Load the current vector clock in use and the current set of thread clocks
1442     /// in use for the vector.
1443     #[inline]
1444     fn current_thread_state(&self) -> (VectorIdx, Ref<'_, ThreadClockSet>) {
1445         let index = self.current_index();
1446         let ref_vector = self.vector_clocks.borrow();
1447         let clocks = Ref::map(ref_vector, |vec| &vec[index]);
1448         (index, clocks)
1449     }
1450
1451     /// Load the current vector clock in use and the current set of thread clocks
1452     /// in use for the vector mutably for modification.
1453     #[inline]
1454     fn current_thread_state_mut(&self) -> (VectorIdx, RefMut<'_, ThreadClockSet>) {
1455         let index = self.current_index();
1456         let ref_vector = self.vector_clocks.borrow_mut();
1457         let clocks = RefMut::map(ref_vector, |vec| &mut vec[index]);
1458         (index, clocks)
1459     }
1460
1461     /// Return the current thread, should be the same
1462     /// as the data-race active thread.
1463     #[inline]
1464     fn current_index(&self) -> VectorIdx {
1465         self.current_index.get()
1466     }
1467 }