Skip to content

Commit 61b58bd

Browse files
committed
add option to track all read/write accesses to tracked allocations
1 parent af37aca commit 61b58bd

File tree

16 files changed

+38
-14
lines changed

16 files changed

+38
-14
lines changed

src/bin/miri.rs

+2
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ fn main() {
534534
),
535535
};
536536
miri_config.tracked_alloc_ids.extend(ids);
537+
} else if arg == "-Zmiri-track-alloc-accesses" {
538+
miri_config.track_alloc_accesses = true;
537539
} else if let Some(param) = arg.strip_prefix("-Zmiri-compare-exchange-weak-failure-rate=") {
538540
let rate = match param.parse::<f64>() {
539541
Ok(rate) if rate >= 0.0 && rate <= 1.0 => rate,

src/borrow_tracker/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ impl VisitProvenance for GlobalStateInner {
122122
/// We need interior mutable access to the global state.
123123
pub type GlobalState = RefCell<GlobalStateInner>;
124124

125-
/// Indicates which kind of access is being performed.
126-
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
127-
pub enum AccessKind {
128-
Read,
129-
Write,
130-
}
131-
132125
impl fmt::Display for AccessKind {
133126
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134127
match self {

src/borrow_tracker/stacked_borrows/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashSet;
55
use rustc_span::{Span, SpanData};
66
use rustc_target::abi::Size;
77

8-
use crate::borrow_tracker::{AccessKind, GlobalStateInner, ProtectorKind};
8+
use crate::borrow_tracker::{GlobalStateInner, ProtectorKind};
99
use crate::*;
1010

1111
/// Error reporting

src/borrow_tracker/stacked_borrows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_target::abi::{Abi, Size};
1616

1717
use crate::borrow_tracker::{
1818
stacked_borrows::diagnostics::{AllocHistory, DiagnosticCx, DiagnosticCxBuilder},
19-
AccessKind, GlobalStateInner, ProtectorKind,
19+
GlobalStateInner, ProtectorKind,
2020
};
2121
use crate::*;
2222

src/borrow_tracker/tree_borrows/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::borrow_tracker::tree_borrows::{
99
tree::LocationState,
1010
unimap::UniIndex,
1111
};
12-
use crate::borrow_tracker::{AccessKind, ProtectorKind};
12+
use crate::borrow_tracker::ProtectorKind;
1313
use crate::*;
1414

1515
/// Cause of an access: either a real access or one

src/borrow_tracker/tree_borrows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_target::abi::{Abi, Size};
22

3-
use crate::borrow_tracker::{AccessKind, GlobalState, GlobalStateInner, ProtectorKind};
3+
use crate::borrow_tracker::{GlobalState, GlobalStateInner, ProtectorKind};
44
use rustc_middle::{
55
mir::{Mutability, RetagKind},
66
ty::{

src/borrow_tracker/tree_borrows/perms.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33

44
use crate::borrow_tracker::tree_borrows::diagnostics::TransitionError;
55
use crate::borrow_tracker::tree_borrows::tree::AccessRelatedness;
6-
use crate::borrow_tracker::AccessKind;
6+
use crate::AccessKind;
77

88
/// The activation states of a pointer.
99
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

src/borrow_tracker/tree_borrows/tree.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::borrow_tracker::tree_borrows::{
2424
unimap::{UniEntry, UniIndex, UniKeyMap, UniValMap},
2525
Permission,
2626
};
27-
use crate::borrow_tracker::{AccessKind, GlobalState, ProtectorKind};
27+
use crate::borrow_tracker::{GlobalState, ProtectorKind};
2828
use crate::*;
2929

3030
mod tests;

src/diagnostics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ pub enum NonHaltingDiagnostic {
116116
CreatedCallId(CallId),
117117
CreatedAlloc(AllocId, Size, Align, MemoryKind<MiriMemoryKind>),
118118
FreedAlloc(AllocId),
119+
AccessedAlloc(AllocId, AccessKind),
119120
RejectedIsolatedOp(String),
120121
ProgressReport {
121122
block_count: u64, // how many basic blocks have been run so far
@@ -538,6 +539,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
538539
| PoppedPointerTag(..)
539540
| CreatedCallId(..)
540541
| CreatedAlloc(..)
542+
| AccessedAlloc(..)
541543
| FreedAlloc(..)
542544
| ProgressReport { .. }
543545
| WeakMemoryOutdatedLoad => ("tracking was triggered".to_string(), DiagLevel::Note),
@@ -559,6 +561,8 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
559561
size = size.bytes(),
560562
align = align.bytes(),
561563
),
564+
AccessedAlloc(AllocId(id), access_kind) =>
565+
format!("{access_kind} access to allocation with id {id}"),
562566
FreedAlloc(AllocId(id)) => format!("freed allocation with id {id}"),
563567
RejectedIsolatedOp(ref op) =>
564568
format!("{op} was made to return an error due to isolation"),

src/eval.rs

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ pub struct MiriConfig {
112112
pub tracked_call_ids: FxHashSet<CallId>,
113113
/// The allocation ids to report about.
114114
pub tracked_alloc_ids: FxHashSet<AllocId>,
115+
/// For the tracked alloc ids, also report read/write accesses.
116+
pub track_alloc_accesses: bool,
115117
/// Determine if data race detection should be enabled
116118
pub data_race_detector: bool,
117119
/// Determine if weak memory emulation should be enabled. Requires data race detection to be enabled
@@ -169,6 +171,7 @@ impl Default for MiriConfig {
169171
tracked_pointer_tags: FxHashSet::default(),
170172
tracked_call_ids: FxHashSet::default(),
171173
tracked_alloc_ids: FxHashSet::default(),
174+
track_alloc_accesses: false,
172175
data_race_detector: true,
173176
weak_memory_emulation: true,
174177
track_outdated_loads: false,

src/helpers.rs

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ use rand::RngCore;
2222

2323
use crate::*;
2424

25+
/// Indicates which kind of access is being performed.
26+
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
27+
pub enum AccessKind {
28+
Read,
29+
Write,
30+
}
31+
2532
// This mapping should match `decode_error_kind` in
2633
// <https://github.com/rust-lang/rust/blob/master/library/std/src/sys/pal/unix/mod.rs>.
2734
const UNIX_IO_ERROR_TABLE: &[(&str, std::io::ErrorKind)] = {

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub use crate::diagnostics::{
120120
pub use crate::eval::{
121121
create_ecx, eval_entry, AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, RejectOpWith,
122122
};
123-
pub use crate::helpers::EvalContextExt as _;
123+
pub use crate::helpers::{AccessKind, EvalContextExt as _};
124124
pub use crate::intptrcast::{EvalContextExt as _, ProvenanceMode};
125125
pub use crate::machine::{
126126
AllocExtra, FrameExtra, MiriInterpCx, MiriInterpCxExt, MiriMachine, MiriMemoryKind,

src/machine.rs

+12
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ pub struct MiriMachine<'mir, 'tcx> {
512512
/// The allocation IDs to report when they are being allocated
513513
/// (helps for debugging memory leaks and use after free bugs).
514514
tracked_alloc_ids: FxHashSet<AllocId>,
515+
/// For the tracked alloc ids, also report read/write accesses.
516+
track_alloc_accesses: bool,
515517

516518
/// Controls whether alignment of memory accesses is being checked.
517519
pub(crate) check_alignment: AlignmentCheck,
@@ -654,6 +656,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
654656
extern_statics: FxHashMap::default(),
655657
rng: RefCell::new(rng),
656658
tracked_alloc_ids: config.tracked_alloc_ids.clone(),
659+
track_alloc_accesses: config.track_alloc_accesses,
657660
check_alignment: config.check_alignment,
658661
cmpxchg_weak_failure_rate: config.cmpxchg_weak_failure_rate,
659662
mute_stdout_stderr: config.mute_stdout_stderr,
@@ -793,6 +796,7 @@ impl VisitProvenance for MiriMachine<'_, '_> {
793796
local_crates: _,
794797
rng: _,
795798
tracked_alloc_ids: _,
799+
track_alloc_accesses: _,
796800
check_alignment: _,
797801
cmpxchg_weak_failure_rate: _,
798802
mute_stdout_stderr: _,
@@ -1235,6 +1239,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
12351239
(alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra),
12361240
range: AllocRange,
12371241
) -> InterpResult<'tcx> {
1242+
if machine.track_alloc_accesses && machine.tracked_alloc_ids.contains(&alloc_id) {
1243+
machine
1244+
.emit_diagnostic(NonHaltingDiagnostic::AccessedAlloc(alloc_id, AccessKind::Read));
1245+
}
12381246
if let Some(data_race) = &alloc_extra.data_race {
12391247
data_race.read(alloc_id, range, machine)?;
12401248
}
@@ -1255,6 +1263,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
12551263
(alloc_id, prov_extra): (AllocId, Self::ProvenanceExtra),
12561264
range: AllocRange,
12571265
) -> InterpResult<'tcx> {
1266+
if machine.track_alloc_accesses && machine.tracked_alloc_ids.contains(&alloc_id) {
1267+
machine
1268+
.emit_diagnostic(NonHaltingDiagnostic::AccessedAlloc(alloc_id, AccessKind::Write));
1269+
}
12581270
if let Some(data_race) = &mut alloc_extra.data_race {
12591271
data_race.write(alloc_id, range, machine)?;
12601272
}

tests/fail-dep/concurrency/windows_join_main.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: deadlock: the evaluated program deadlocked
44
LL | assert_eq!(WaitForSingleObject(MAIN_THREAD, INFINITE), WAIT_OBJECT_0);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program deadlocked
66
|
7+
= note: BACKTRACE on thread `unnamed-ID`:
78
= note: inside closure at RUSTLIB/core/src/macros/mod.rs:LL:CC
89
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
910

tests/fail-dep/concurrency/windows_join_self.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: deadlock: the evaluated program deadlocked
44
LL | assert_eq!(WaitForSingleObject(native, INFINITE), WAIT_OBJECT_0);
55
| ^ the evaluated program deadlocked
66
|
7+
= note: BACKTRACE on thread `unnamed-ID`:
78
= note: inside closure at $DIR/windows_join_self.rs:LL:CC
89

910
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/memleak_rc.32bit.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: memory leaked: ALLOC (Rust heap, size: 16, align: 4), allocated here:
44
LL | __rust_alloc(layout.size(), layout.align())
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: BACKTRACE:
78
= note: inside `std::alloc::alloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
89
= note: inside `std::alloc::Global::alloc_impl` at RUSTLIB/alloc/src/alloc.rs:LL:CC
910
= note: inside `<std::alloc::Global as std::alloc::Allocator>::allocate` at RUSTLIB/alloc/src/alloc.rs:LL:CC

0 commit comments

Comments
 (0)