Skip to content

Commit e735796

Browse files
committed
Rework to work with machine hook.
1 parent 81c4eb7 commit e735796

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

src/data_race.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use rustc_target::abi::Size;
7676
use crate::{
7777
ImmTy, Immediate, InterpResult, MPlaceTy, MemPlaceMeta, MiriEvalContext, MiriEvalContextExt,
7878
OpTy, Pointer, RangeMap, ScalarMaybeUninit, Tag, ThreadId, VClock, VTimestamp,
79-
VectorIdx,
79+
VectorIdx, MemoryKind, MiriMemoryKind
8080
};
8181

8282
pub type AllocExtra = VClockAlloc;
@@ -674,6 +674,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
674674
Ok(())
675675
}
676676
}
677+
678+
fn reset_vector_clocks(
679+
&mut self,
680+
ptr: Pointer<Tag>,
681+
size: Size
682+
) -> InterpResult<'tcx> {
683+
let this = self.eval_context_mut();
684+
if let Some(data_race) = &mut this.memory.extra.data_race {
685+
if data_race.multi_threaded.get() {
686+
let alloc_meta = this.memory.get_raw_mut(ptr.alloc_id)?.extra.data_race.as_mut().unwrap();
687+
alloc_meta.reset_clocks(ptr.offset, size);
688+
}
689+
}
690+
Ok(())
691+
}
677692
}
678693

679694
/// Vector clock metadata for a logical memory allocation.
@@ -688,7 +703,18 @@ pub struct VClockAlloc {
688703

689704
impl VClockAlloc {
690705
/// Create a new data-race detector for newly allocated memory.
691-
pub fn new_allocation(global: &MemoryExtra, len: Size, track_alloc: bool) -> VClockAlloc {
706+
pub fn new_allocation(global: &MemoryExtra, len: Size, kind: MemoryKind<MiriMemoryKind>) -> VClockAlloc {
707+
let track_alloc = match kind {
708+
// User allocated and stack memory should track allocation.
709+
MemoryKind::Machine(
710+
MiriMemoryKind::Rust | MiriMemoryKind::C | MiriMemoryKind::WinHeap
711+
) | MemoryKind::Stack => true,
712+
// Other global memory should trace races but be allocated at the 0 timestamp.
713+
MemoryKind::Machine(
714+
MiriMemoryKind::Global | MiriMemoryKind::Machine | MiriMemoryKind::Env |
715+
MiriMemoryKind::ExternStatic | MiriMemoryKind::Tls
716+
) | MemoryKind::CallerLocation | MemoryKind::Vtable => false
717+
};
692718
let (alloc_timestamp, alloc_index) = if track_alloc {
693719
let (alloc_index, clocks) = global.current_thread_state();
694720
let alloc_timestamp = clocks.clock[alloc_index];
@@ -704,6 +730,14 @@ impl VClockAlloc {
704730
}
705731
}
706732

733+
fn reset_clocks(&mut self, offset: Size, len: Size) {
734+
let mut alloc_ranges = self.alloc_ranges.borrow_mut();
735+
for (_, range) in alloc_ranges.iter_mut(offset, len) {
736+
// Reset the portion of the range
737+
*range = MemoryCellClocks::new(0, VectorIdx::MAX_INDEX);
738+
}
739+
}
740+
707741
// Find an index, if one exists where the value
708742
// in `l` is greater than the value in `r`.
709743
fn find_gt_index(l: &VClock, r: &VClock) -> Option<VectorIdx> {

src/machine.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -478,27 +478,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
478478
(None, Tag::Untagged)
479479
};
480480
let race_alloc = if let Some(data_race) = &memory_extra.data_race {
481-
match kind {
482-
// V-Table generation is lazy and so racy, so do not track races.
483-
// Also V-Tables are read only so no data races can be occur.
484-
// Must be disabled since V-Tables are initialized via interpreter
485-
// writes on demand and can incorrectly cause the data-race detector
486-
// to trigger.
487-
MemoryKind::Vtable => None,
488-
// User allocated and stack memory should track allocation.
489-
MemoryKind::Machine(
490-
MiriMemoryKind::Rust | MiriMemoryKind::C | MiriMemoryKind::WinHeap
491-
) | MemoryKind::Stack => Some(
492-
data_race::AllocExtra::new_allocation(&data_race, alloc.size, true)
493-
),
494-
// Other global memory should trace races but be allocated at the 0 timestamp.
495-
MemoryKind::Machine(
496-
MiriMemoryKind::Global | MiriMemoryKind::Machine | MiriMemoryKind::Env |
497-
MiriMemoryKind::ExternStatic | MiriMemoryKind::Tls
498-
) | MemoryKind::CallerLocation => Some(
499-
data_race::AllocExtra::new_allocation(&data_race, alloc.size, false)
500-
)
501-
}
481+
Some(data_race::AllocExtra::new_allocation(&data_race, alloc.size, kind))
502482
} else {
503483
None
504484
};
@@ -530,6 +510,18 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
530510
Ok(())
531511
}
532512

513+
514+
fn after_static_mem_initialized(
515+
ecx: &mut InterpCx<'mir, 'tcx, Self>,
516+
ptr: Pointer<Self::PointerTag>,
517+
size: Size,
518+
) -> InterpResult<'tcx> {
519+
if ecx.memory.extra.data_race.is_some() {
520+
ecx.reset_vector_clocks(ptr, size)?;
521+
}
522+
Ok(())
523+
}
524+
533525
#[inline(always)]
534526
fn tag_global_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag {
535527
if let Some(stacked_borrows) = &memory_extra.stacked_borrows {

0 commit comments

Comments
 (0)