Skip to content

Commit 57d2831

Browse files
committed
Also added multi arg support for allocs and calls
- Changed parsing to handle comma separated lists - Changed types in config evaluator and global to use HashSet - Adjusted Readme
1 parent 8630d86 commit 57d2831

File tree

5 files changed

+50
-35
lines changed

5 files changed

+50
-35
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,10 @@ environment variable:
314314
ensure alignment. (The standard library `align_to` method works fine in both
315315
modes; under symbolic alignment it only fills the middle slice when the
316316
allocation guarantees sufficient alignment.)
317-
* `-Zmiri-track-alloc-id=<id>` shows a backtrace when the given allocation is
317+
* `-Zmiri-track-alloc-id=<id-list>` shows a backtrace when the given allocations are
318318
being allocated or freed. This helps in debugging memory leaks and
319319
use after free bugs.
320-
* `-Zmiri-track-call-id=<id>` shows a backtrace when the given call id is
320+
* `-Zmiri-track-call-id=<id-list>` shows a backtrace when the given call ids are
321321
assigned to a stack frame. This helps in debugging UB related to Stacked
322322
Borrows "protectors".
323323
* `-Zmiri-track-pointer-tag=<tag-list>` shows a backtrace when a given pointer tag

src/bin/miri.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,16 @@ fn main() {
397397
.push(arg.strip_prefix("-Zmiri-env-forward=").unwrap().to_owned());
398398
}
399399
arg if arg.starts_with("-Zmiri-track-pointer-tag=") => {
400-
let ids: Vec<u64> = match arg.strip_prefix("-Zmiri-track-pointer-tag=").unwrap().split(',').map(|id| id.parse::<u64>()).collect() {
400+
let ids: Vec<u64> = match arg
401+
.strip_prefix("-Zmiri-track-pointer-tag=")
402+
.unwrap()
403+
.split(',')
404+
.map(str::parse::<u64>).collect()
405+
{
401406
Ok(ids) => ids,
402407
Err(err) =>
403408
panic!(
404-
"-Zmiri-track-pointer-tag requires a comma seperated list of valid `u64` arguments: {}",
409+
"-Zmiri-track-pointer-tag requires a comma separated list of valid `u64` arguments: {}",
405410
err
406411
),
407412
};
@@ -414,30 +419,39 @@ fn main() {
414419
}
415420
}
416421
arg if arg.starts_with("-Zmiri-track-call-id=") => {
417-
let id: u64 = match arg.strip_prefix("-Zmiri-track-call-id=").unwrap().parse() {
418-
Ok(id) => id,
422+
let ids: Vec<u64> = match arg
423+
.strip_prefix("-Zmiri-track-call-id=")
424+
.unwrap()
425+
.split(',')
426+
.map(str::parse::<u64>)
427+
.collect()
428+
{
429+
Ok(ids) => ids,
419430
Err(err) =>
420-
panic!("-Zmiri-track-call-id requires a valid `u64` argument: {}", err),
431+
panic!("-Zmiri-track-call-id requires a comma separated list of valid `u64` arguments: {}", err),
421432
};
422-
if let Some(id) = miri::CallId::new(id) {
423-
miri_config.tracked_call_id = Some(id);
424-
} else {
425-
panic!("-Zmiri-track-call-id requires a nonzero argument");
433+
for id in ids.into_iter().map(miri::CallId::new) {
434+
if let Some(id) = id {
435+
miri_config.tracked_call_ids.insert(id);
436+
} else {
437+
panic!("-Zmiri-track-call-id requires a nonzero argument");
438+
}
426439
}
427440
}
428441
arg if arg.starts_with("-Zmiri-track-alloc-id=") => {
429-
let id = match arg
442+
let ids: Vec<miri::AllocId> = match arg
430443
.strip_prefix("-Zmiri-track-alloc-id=")
431444
.unwrap()
432-
.parse()
433-
.ok()
434-
.and_then(NonZeroU64::new)
445+
.split(',')
446+
.map(str::parse::<u64>)
447+
.map(|id| id.ok().and_then(NonZeroU64::new).map(miri::AllocId))
448+
.collect()
435449
{
436-
Some(id) => id,
450+
Some(ids) => ids,
437451
None =>
438-
panic!("-Zmiri-track-alloc-id requires a valid non-zero `u64` argument"),
452+
panic!("-Zmiri-track-alloc-id requires a comma separated list of valid non-zero `u64` arguments"),
439453
};
440-
miri_config.tracked_alloc_id = Some(miri::AllocId(id));
454+
miri_config.tracked_alloc_ids.extend(ids);
441455
}
442456
arg if arg.starts_with("-Zmiri-compare-exchange-weak-failure-rate=") => {
443457
let rate = match arg

src/eval.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ pub struct MiriConfig {
9595
pub seed: Option<u64>,
9696
/// The stacked borrows pointer ids to report about
9797
pub tracked_pointer_tags: HashSet<PtrId>,
98-
/// The stacked borrows call ID to report about
99-
pub tracked_call_id: Option<CallId>,
100-
/// The allocation id to report about.
101-
pub tracked_alloc_id: Option<AllocId>,
98+
/// The stacked borrows call IDs to report about
99+
pub tracked_call_ids: HashSet<CallId>,
100+
/// The allocation ids to report about.
101+
pub tracked_alloc_ids: HashSet<AllocId>,
102102
/// Whether to track raw pointers in stacked borrows.
103103
pub tag_raw: bool,
104104
/// Determine if data race detection should be enabled
@@ -133,8 +133,8 @@ impl Default for MiriConfig {
133133
args: vec![],
134134
seed: None,
135135
tracked_pointer_tags: Default::default(),
136-
tracked_call_id: None,
137-
tracked_alloc_id: None,
136+
tracked_call_ids: Default::default(),
137+
tracked_alloc_ids: Default::default(),
138138
tag_raw: false,
139139
data_race_detector: true,
140140
cmpxchg_weak_failure_rate: 0.8,

src/machine.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::cell::RefCell;
66
use std::fmt;
77
use std::num::NonZeroU64;
88
use std::time::Instant;
9+
use std::collections::HashSet;
910

1011
use rand::rngs::StdRng;
1112
use rand::SeedableRng;
@@ -281,9 +282,9 @@ pub struct Evaluator<'mir, 'tcx> {
281282
/// Needs to be queried by ptr_to_int, hence needs interior mutability.
282283
pub(crate) rng: RefCell<StdRng>,
283284

284-
/// An allocation ID to report when it is being allocated
285+
/// The allocation IDs to report when they are being allocated
285286
/// (helps for debugging memory leaks and use after free bugs).
286-
tracked_alloc_id: Option<AllocId>,
287+
tracked_alloc_ids: HashSet<AllocId>,
287288

288289
/// Controls whether alignment of memory accesses is being checked.
289290
pub(crate) check_alignment: AlignmentCheck,
@@ -304,7 +305,7 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
304305
let stacked_borrows = if config.stacked_borrows {
305306
Some(RefCell::new(stacked_borrows::GlobalStateInner::new(
306307
config.tracked_pointer_tags.clone(),
307-
config.tracked_call_id,
308+
config.tracked_call_ids.clone(),
308309
config.tag_raw,
309310
)))
310311
} else {
@@ -340,7 +341,7 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
340341
local_crates,
341342
extern_statics: FxHashMap::default(),
342343
rng: RefCell::new(rng),
343-
tracked_alloc_id: config.tracked_alloc_id,
344+
tracked_alloc_ids: config.tracked_alloc_ids.clone(),
344345
check_alignment: config.check_alignment,
345346
cmpxchg_weak_failure_rate: config.cmpxchg_weak_failure_rate,
346347
}
@@ -557,7 +558,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
557558
alloc: Cow<'b, Allocation>,
558559
kind: Option<MemoryKind<Self::MemoryKind>>,
559560
) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>> {
560-
if Some(id) == ecx.machine.tracked_alloc_id {
561+
if ecx.machine.tracked_alloc_ids.contains(&id) {
561562
register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
562563
}
563564

@@ -666,7 +667,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
666667
tag: Tag,
667668
range: AllocRange,
668669
) -> InterpResult<'tcx> {
669-
if Some(tag.alloc_id) == machine.tracked_alloc_id {
670+
if machine.tracked_alloc_ids.contains(&tag.alloc_id) {
670671
register_diagnostic(NonHaltingDiagnostic::FreedAlloc(tag.alloc_id));
671672
}
672673
if let Some(data_race) = &mut alloc_extra.data_race {

src/stacked_borrows.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ pub struct GlobalStateInner {
107107
active_calls: FxHashSet<CallId>,
108108
/// The pointer ids to trace
109109
tracked_pointer_tags: HashSet<PtrId>,
110-
/// The call id to trace
111-
tracked_call_id: Option<CallId>,
110+
/// The call ids to trace
111+
tracked_call_ids: HashSet<CallId>,
112112
/// Whether to track raw pointers.
113113
tag_raw: bool,
114114
}
@@ -160,7 +160,7 @@ impl fmt::Display for RefKind {
160160
impl GlobalStateInner {
161161
pub fn new(
162162
tracked_pointer_tags: HashSet<PtrId>,
163-
tracked_call_id: Option<CallId>,
163+
tracked_call_ids: HashSet<CallId>,
164164
tag_raw: bool,
165165
) -> Self {
166166
GlobalStateInner {
@@ -169,7 +169,7 @@ impl GlobalStateInner {
169169
next_call_id: NonZeroU64::new(1).unwrap(),
170170
active_calls: FxHashSet::default(),
171171
tracked_pointer_tags,
172-
tracked_call_id,
172+
tracked_call_ids,
173173
tag_raw,
174174
}
175175
}
@@ -186,7 +186,7 @@ impl GlobalStateInner {
186186
pub fn new_call(&mut self) -> CallId {
187187
let id = self.next_call_id;
188188
trace!("new_call: Assigning ID {}", id);
189-
if Some(id) == self.tracked_call_id {
189+
if self.tracked_call_ids.contains(&id) {
190190
register_diagnostic(NonHaltingDiagnostic::CreatedCallId(id));
191191
}
192192
assert!(self.active_calls.insert(id));

0 commit comments

Comments
 (0)