Skip to content

Commit b6fca87

Browse files
committed
Auto merge of #1334 - KrishnaSannasi:track-dealloc, r=RalfJung
add deallocation tracking fixes #1314
2 parents e4eceba + b77968e commit b6fca87

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ Miri adds its own set of `-Z` flags:
192192
for cryptographic use! Do not generate secret keys in Miri or perform other
193193
kinds of cryptographic operations that rely on proper random numbers.
194194
* `-Zmiri-track-alloc-id=<id>` shows a backtrace when the given allocation is
195-
being allocated. This helps in debugging memory leaks.
195+
being allocated or freed. This helps in debugging memory leaks and
196+
use after free bugs.
196197
* `-Zmiri-track-pointer-tag=<tag>` shows a backtrace when the given pointer tag
197198
is popped from a borrow stack (which is where the tag becomes invalid and any
198199
future use of it will error). This helps you in finding out why UB is

src/diagnostics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl MachineStopType for TerminationInfo {}
4242
pub enum NonHaltingDiagnostic {
4343
PoppedTrackedPointerTag(Item),
4444
CreatedAlloc(AllocId),
45+
FreedAlloc(AllocId),
4546
}
4647

4748
/// Emit a custom diagnostic without going through the miri-engine machinery
@@ -191,6 +192,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
191192
format!("popped tracked tag for item {:?}", item),
192193
CreatedAlloc(AllocId(id)) =>
193194
format!("created allocation with id {}", id),
195+
FreedAlloc(AllocId(id)) =>
196+
format!("freed allocation with id {}", id),
194197
};
195198
report_msg(this, "tracking was triggered", msg, vec![], false);
196199
}

src/machine.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub struct MemoryExtra {
116116
pub(crate) rng: RefCell<StdRng>,
117117

118118
/// An allocation ID to report when it is being allocated
119-
/// (helps for debugging memory leaks).
119+
/// (helps for debugging memory leaks and use after free bugs).
120120
tracked_alloc_id: Option<AllocId>,
121121

122122
/// Controls whether alignment of memory accesses is being checked.
@@ -466,6 +466,18 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
466466
(Cow::Owned(alloc), base_tag)
467467
}
468468

469+
#[inline(always)]
470+
fn before_deallocation(
471+
memory_extra: &mut Self::MemoryExtra,
472+
id: AllocId,
473+
) -> InterpResult<'tcx> {
474+
if Some(id) == memory_extra.tracked_alloc_id {
475+
register_diagnostic(NonHaltingDiagnostic::FreedAlloc(id));
476+
}
477+
478+
Ok(())
479+
}
480+
469481
#[inline(always)]
470482
fn tag_global_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag {
471483
if let Some(stacked_borrows) = &memory_extra.stacked_borrows {

0 commit comments

Comments
 (0)