Skip to content

Commit a0e756b

Browse files
authored
Rollup merge of #66663 - RalfJung:miri-leaks, r=oli-obk
Miri: print leak report even without tracing Currently, the rustup-installed Miri has no way to actually print a leak report (as `trace!` is compiled out). Make it print that per default instead when there is a leak. r? @oli-obk
2 parents a49f23e + 9233a54 commit a0e756b

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/librustc_mir/interpret/eval_context.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
705705
if let LocalValue::Live(Operand::Indirect(MemPlace { ptr, .. })) = local {
706706
trace!("deallocating local");
707707
let ptr = ptr.to_ptr()?;
708-
self.memory.dump_alloc(ptr.alloc_id);
708+
if log_enabled!(::log::Level::Trace) {
709+
self.memory.dump_alloc(ptr.alloc_id);
710+
}
709711
self.memory.deallocate_local(ptr)?;
710712
};
711713
Ok(())

src/librustc_mir/interpret/memory.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
635635
Ok(())
636636
}
637637

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

677-
trace!(
679+
eprintln!(
678680
"{}({} bytes, alignment {}){}",
679681
msg,
680682
alloc.size.bytes(),
@@ -695,15 +697,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
695697
write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
696698
pos = i + self.pointer_size();
697699
}
698-
trace!("{}", msg);
700+
eprintln!("{}", msg);
699701
}
700702
}
701703

702-
/// For debugging, print a list of allocations and all allocations they point to, recursively.
704+
/// Print a list of allocations and all allocations they point to, recursively.
705+
/// This prints directly to stderr, ignoring RUSTC_LOG! It is up to the caller to
706+
/// control for this.
703707
pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
704-
if !log_enabled!(::log::Level::Trace) {
705-
return;
706-
}
707708
allocs.sort();
708709
allocs.dedup();
709710
let mut allocs_to_print = VecDeque::from(allocs);
@@ -735,13 +736,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
735736
);
736737
}
737738
Some(GlobalAlloc::Function(func)) => {
738-
trace!("{} {}", msg, func);
739+
eprintln!("{} {}", msg, func);
739740
}
740741
Some(GlobalAlloc::Static(did)) => {
741-
trace!("{} {:?}", msg, did);
742+
eprintln!("{} {:?}", msg, did);
742743
}
743744
None => {
744-
trace!("{} (deallocated)", msg);
745+
eprintln!("{} (deallocated)", msg);
745746
}
746747
}
747748
},
@@ -751,12 +752,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
751752
}
752753

753754
pub fn leak_report(&self) -> usize {
754-
trace!("### LEAK REPORT ###");
755755
let leaks: Vec<_> = self.alloc_map.filter_map_collect(|&id, &(kind, _)| {
756756
if kind.may_leak() { None } else { Some(id) }
757757
});
758758
let n = leaks.len();
759-
self.dump_allocs(leaks);
759+
if n > 0 {
760+
eprintln!("### LEAK REPORT ###");
761+
self.dump_allocs(leaks);
762+
}
760763
n
761764
}
762765

0 commit comments

Comments
 (0)