Skip to content

Commit ade4c4e

Browse files
committed
make the new option actually do something
1 parent d82d701 commit ade4c4e

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ Several `-Z` flags are relevant for Miri:
180180
is popped from a borrow stack (which is where the tag becomes invalid and any
181181
future use of it will error). This helps you in finding out why UB is
182182
happening and where in your code would be a good place to look for it.
183+
* `-Zmiri-track-alloc-id=<id>` shows a backtrace when the given allocation is
184+
being allocated. This helps in debugging memory leaks.
183185

184186
Moreover, Miri recognizes some environment variables:
185187

src/diagnostics.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::*;
66
/// Miri specific diagnostics
77
pub enum NonHaltingDiagnostic {
88
PoppedTrackedPointerTag(Item),
9+
CreatedAlloc(AllocId),
910
}
1011

1112
/// Emit a custom diagnostic without going through the miri-engine machinery
@@ -97,9 +98,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9798
let this = self.eval_context_ref();
9899
DIAGNOSTICS.with(|diagnostics| {
99100
for e in diagnostics.borrow_mut().drain(..) {
101+
use NonHaltingDiagnostic::*;
100102
let msg = match e {
101-
NonHaltingDiagnostic::PoppedTrackedPointerTag(item) =>
103+
PoppedTrackedPointerTag(item) =>
102104
format!("popped tracked tag for item {:?}", item),
105+
CreatedAlloc(AllocId(id)) =>
106+
format!("created allocation with id {}", id),
103107
};
104108
report_msg(this, msg, false);
105109
}

src/machine.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
334334
alloc: Cow<'b, Allocation>,
335335
kind: Option<MemoryKind<Self::MemoryKinds>>,
336336
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
337+
if Some(id) == memory_extra.tracked_alloc_id {
338+
register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
339+
}
340+
337341
let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
338342
let alloc = alloc.into_owned();
339343
let (stacks, base_tag) =

0 commit comments

Comments
 (0)