Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple data-race detector #1617

Merged
merged 17 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ fn main() {
"-Zmiri-disable-stacked-borrows" => {
miri_config.stacked_borrows = false;
}
"-Zmiri-disable-data-race-detector" => {
miri_config.data_race_detector = false;
}
"-Zmiri-disable-alignment-check" => {
miri_config.check_alignment = miri::AlignmentCheck::None;
}
Expand Down
1,104 changes: 620 additions & 484 deletions src/data_race.rs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub struct MiriConfig {
pub tracked_alloc_id: Option<AllocId>,
/// Whether to track raw pointers in stacked borrows.
pub track_raw: bool,
/// Determine if data race detection should be enabled
pub data_race_detector: bool,
}

impl Default for MiriConfig {
Expand All @@ -65,6 +67,7 @@ impl Default for MiriConfig {
tracked_call_id: None,
tracked_alloc_id: None,
track_raw: false,
data_race_detector: true,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub use crate::shims::tls::{EvalContextExt as _, TlsData};
pub use crate::shims::EvalContextExt as _;

pub use crate::data_race::{
AtomicReadOp, AtomicWriteOp, AtomicRWOp, AtomicFenceOp, DataRaceLockHandle,
AtomicReadOp, AtomicWriteOp, AtomicRwOp, AtomicFenceOp,
EvalContextExt as DataRaceEvalContextExt
};
pub use crate::diagnostics::{
Expand All @@ -81,7 +81,7 @@ pub use crate::sync::{
EvalContextExt as SyncEvalContextExt, CondvarId, MutexId, RwLockId
};
pub use crate::vector_clock::{
VClock, VSmallClockSet, VectorIdx, VTimestamp
VClock, VSmallClockMap, VectorIdx, VTimestamp
};

/// Insert rustc arguments at the beginning of the argument list that Miri wants to be
Expand Down
31 changes: 23 additions & 8 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,16 @@ impl fmt::Display for MiriMemoryKind {
pub struct AllocExtra {
/// Stacked Borrows state is only added if it is enabled.
pub stacked_borrows: Option<stacked_borrows::AllocExtra>,
/// Data race detection via the use of a vector-clock.
pub data_race: data_race::AllocExtra,
/// Data race detection via the use of a vector-clock,
/// this is only added if it is enabled.
pub data_race: Option<data_race::AllocExtra>,
}

/// Extra global memory data
#[derive(Clone, Debug)]
pub struct MemoryExtra {
pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
pub data_race: data_race::MemoryExtra,
pub data_race: Option<data_race::MemoryExtra>,
pub intptrcast: intptrcast::MemoryExtra,

/// Mapping extern static names to their canonical allocation.
Expand Down Expand Up @@ -147,7 +148,11 @@ impl MemoryExtra {
} else {
None
};
let data_race = Rc::new(data_race::GlobalState::new());
let data_race = if config.data_race_detector {
Some(Rc::new(data_race::GlobalState::new()))
}else{
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
None
};
MemoryExtra {
stacked_borrows,
data_race,
Expand Down Expand Up @@ -472,7 +477,11 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
// No stacks, no tag.
(None, Tag::Untagged)
};
let race_alloc = data_race::AllocExtra::new_allocation(&memory_extra.data_race, alloc.size);
let race_alloc = if let Some(data_race) = &memory_extra.data_race {
Some(data_race::AllocExtra::new_allocation(&data_race, alloc.size))
} else {
None
};
let mut stacked_borrows = memory_extra.stacked_borrows.as_ref().map(|sb| sb.borrow_mut());
let alloc: Allocation<Tag, Self::AllocExtra> = alloc.with_tags_and_extra(
|alloc| {
Expand Down Expand Up @@ -590,7 +599,9 @@ impl AllocationExtra<Tag> for AllocExtra {
ptr: Pointer<Tag>,
size: Size,
) -> InterpResult<'tcx> {
alloc.extra.data_race.read(ptr, size)?;
if let Some(data_race) = &alloc.extra.data_race {
data_race.read(ptr, size)?;
}
if let Some(stacked_borrows) = &alloc.extra.stacked_borrows {
stacked_borrows.memory_read(ptr, size)
} else {
Expand All @@ -604,7 +615,9 @@ impl AllocationExtra<Tag> for AllocExtra {
ptr: Pointer<Tag>,
size: Size,
) -> InterpResult<'tcx> {
alloc.extra.data_race.write(ptr, size)?;
if let Some(data_race) = &mut alloc.extra.data_race {
data_race.write(ptr, size)?;
}
if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
stacked_borrows.memory_written(ptr, size)
} else {
Expand All @@ -618,7 +631,9 @@ impl AllocationExtra<Tag> for AllocExtra {
ptr: Pointer<Tag>,
size: Size,
) -> InterpResult<'tcx> {
alloc.extra.data_race.deallocate(ptr, size)?;
if let Some(data_race) = &mut alloc.extra.data_race {
data_race.deallocate(ptr, size)?;
}
if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
stacked_borrows.memory_deallocated(ptr, size)
} else {
Expand Down
Loading