indicate better which kind of memory got leaked

This commit is contained in:
Ralf Jung 2020-04-04 13:34:18 +02:00
parent aecaeab5ec
commit 1f3e2478b2
2 changed files with 16 additions and 12 deletions

View File

@ -79,7 +79,7 @@ fn get_mut(&mut self, k: K) -> Option<&mut V> {
/// and some use case dependent behaviour can instead be applied.
pub trait Machine<'mir, 'tcx>: Sized {
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
type MemoryKind: ::std::fmt::Debug + MayLeak + Eq + 'static;
type MemoryKind: ::std::fmt::Debug + ::std::fmt::Display + MayLeak + Eq + 'static;
/// Tag tracked alongside every pointer. This is used to implement "Stacked Borrows"
/// <https://www.ralfj.de/blog/2018/08/07/stacked-borrows.html>.

View File

@ -9,6 +9,7 @@
use std::borrow::Cow;
use std::collections::VecDeque;
use std::convert::TryFrom;
use std::fmt;
use std::ptr;
use rustc_ast::ast::Mutability;
@ -46,6 +47,17 @@ fn may_leak(self) -> bool {
}
}
impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MemoryKind::Stack => write!(f, "stack variable"),
MemoryKind::Vtable => write!(f, "vtable"),
MemoryKind::CallerLocation => write!(f, "caller location"),
MemoryKind::Machine(m) => write!(f, "{}", m),
}
}
}
/// Used by `get_size_and_align` to indicate whether the allocation needs to be live.
#[derive(Debug, Copy, Clone)]
pub enum AllocCheck {
@ -259,7 +271,7 @@ pub fn deallocate(
if alloc_kind != kind {
throw_ub_format!(
"deallocating `{:?}` memory using `{:?}` deallocation operation",
"deallocating {} memory using {} deallocation operation",
alloc_kind,
kind
);
@ -677,22 +689,14 @@ fn write_allocation_track_relocs<'tcx, Tag, Extra>(
match self.alloc_map.get(id) {
Some(&(kind, ref alloc)) => {
// normal alloc
match kind {
MemoryKind::Stack => eprint!(" (stack variable, "),
MemoryKind::Vtable => eprint!(" (vtable, "),
MemoryKind::CallerLocation => eprint!(" (caller_location, "),
MemoryKind::Machine(m) if Some(m) == M::GLOBAL_KIND => {
eprint!(" (global, ")
}
MemoryKind::Machine(m) => eprint!(" ({:?}, ", m),
};
eprint!(" ({}, ", kind);
write_allocation_track_relocs(self.tcx, &mut allocs_to_print, alloc);
}
None => {
// global alloc
match self.tcx.alloc_map.lock().get(id) {
Some(GlobalAlloc::Memory(alloc)) => {
eprint!(" (global, ");
eprint!(" (unchanged global, ");
write_allocation_track_relocs(self.tcx, &mut allocs_to_print, alloc);
}
Some(GlobalAlloc::Function(func)) => {