Skip to content

Miri: print leak report even without tracing #66663

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

Merged
merged 2 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/librustc_mir/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
if let LocalValue::Live(Operand::Indirect(MemPlace { ptr, .. })) = local {
trace!("deallocating local");
let ptr = ptr.to_ptr()?;
self.memory.dump_alloc(ptr.alloc_id);
if log_enabled!(::log::Level::Trace) {
self.memory.dump_alloc(ptr.alloc_id);
}
self.memory.deallocate_local(ptr)?;
};
Ok(())
Expand Down
27 changes: 15 additions & 12 deletions src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
Ok(())
}

/// For debugging, print an allocation and all allocations it points to, recursively.
/// Print an allocation and all allocations it points to, recursively.
/// This prints directly to stderr, ignoring RUSTC_LOG! It is up to the caller to
/// control for this.
pub fn dump_alloc(&self, id: AllocId) {
self.dump_allocs(vec![id]);
}
Expand Down Expand Up @@ -674,7 +676,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
}
}

trace!(
eprintln!(
"{}({} bytes, alignment {}){}",
msg,
alloc.size.bytes(),
Expand All @@ -695,15 +697,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
pos = i + self.pointer_size();
}
trace!("{}", msg);
eprintln!("{}", msg);
}
}

/// For debugging, print a list of allocations and all allocations they point to, recursively.
/// Print a list of allocations and all allocations they point to, recursively.
/// This prints directly to stderr, ignoring RUSTC_LOG! It is up to the caller to
/// control for this.
pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
if !log_enabled!(::log::Level::Trace) {
return;
}
allocs.sort();
allocs.dedup();
let mut allocs_to_print = VecDeque::from(allocs);
Expand Down Expand Up @@ -735,13 +736,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
);
}
Some(GlobalAlloc::Function(func)) => {
trace!("{} {}", msg, func);
eprintln!("{} {}", msg, func);
}
Some(GlobalAlloc::Static(did)) => {
trace!("{} {:?}", msg, did);
eprintln!("{} {:?}", msg, did);
}
None => {
trace!("{} (deallocated)", msg);
eprintln!("{} (deallocated)", msg);
}
}
},
Expand All @@ -751,12 +752,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
}

pub fn leak_report(&self) -> usize {
trace!("### LEAK REPORT ###");
let leaks: Vec<_> = self.alloc_map.filter_map_collect(|&id, &(kind, _)| {
if kind.may_leak() { None } else { Some(id) }
});
let n = leaks.len();
self.dump_allocs(leaks);
if n > 0 {
eprintln!("### LEAK REPORT ###");
self.dump_allocs(leaks);
}
n
}

Expand Down