Skip to content

Commit 5b71857

Browse files
committed
give machine more control over what counts as memory leak
1 parent 21934c8 commit 5b71857

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ impl<K: Hash + Eq, V> interpret::AllocMap<K, V> for FxHashMap<K, V> {
333333
type CompileTimeEvalContext<'a, 'mir, 'tcx> =
334334
EvalContext<'a, 'mir, 'tcx, CompileTimeInterpreter<'a, 'mir, 'tcx>>;
335335

336+
impl interpret::MayLeak for ! {
337+
#[inline(always)]
338+
fn may_leak(self) -> bool {
339+
// `self` is uninhabited
340+
self
341+
}
342+
}
343+
336344
impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
337345
for CompileTimeInterpreter<'a, 'mir, 'tcx>
338346
{

src/librustc_mir/interpret/machine.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ use rustc::ty::{self, layout::TyLayout, query::TyCtxtAt};
2222

2323
use super::{EvalContext, PlaceTy, OpTy, MemoryKind};
2424

25+
/// Whether this kind of memory is allowed to leak
26+
pub trait MayLeak: Copy {
27+
fn may_leak(self) -> bool;
28+
}
29+
2530
/// The functionality needed by memory to manage its allocations
2631
pub trait AllocMap<K: Hash + Eq, V> {
2732
/// Test if the map contains the given key.
@@ -63,7 +68,7 @@ pub trait AllocMap<K: Hash + Eq, V> {
6368
/// and some use case dependent behaviour can instead be applied.
6469
pub trait Machine<'a, 'mir, 'tcx>: Sized {
6570
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
66-
type MemoryKinds: ::std::fmt::Debug + Copy + Eq + 'static;
71+
type MemoryKinds: ::std::fmt::Debug + MayLeak + Eq + 'static;
6772

6873
/// Tag tracked alongside every pointer. This is used to implement "Stacked Borrows"
6974
/// <https://www.ralfj.de/blog/2018/08/07/stacked-borrows.html>.

src/librustc_mir/interpret/memory.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_data_structures::fx::{FxHashSet, FxHashMap};
3232

3333
use syntax::ast::Mutability;
3434

35-
use super::{Machine, AllocMap, ScalarMaybeUndef};
35+
use super::{Machine, AllocMap, MayLeak, ScalarMaybeUndef};
3636

3737
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
3838
pub enum MemoryKind<T> {
@@ -44,6 +44,17 @@ pub enum MemoryKind<T> {
4444
Machine(T),
4545
}
4646

47+
impl<T: MayLeak> MayLeak for MemoryKind<T> {
48+
#[inline]
49+
fn may_leak(self) -> bool {
50+
match self {
51+
MemoryKind::Stack => false,
52+
MemoryKind::Vtable => true,
53+
MemoryKind::Machine(k) => k.may_leak()
54+
}
55+
}
56+
}
57+
4758
// `Memory` has to depend on the `Machine` because some of its operations
4859
// (e.g. `get`) call a `Machine` hook.
4960
pub struct Memory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>> {
@@ -584,13 +595,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
584595
pub fn leak_report(&self) -> usize {
585596
trace!("### LEAK REPORT ###");
586597
let leaks: Vec<_> = self.alloc_map.filter_map_collect(|&id, &(kind, _)| {
587-
// exclude statics and vtables
588-
let exclude = match kind {
589-
MemoryKind::Stack => false,
590-
MemoryKind::Vtable => true,
591-
MemoryKind::Machine(k) => Some(k) == M::STATIC_KIND,
592-
};
593-
if exclude { None } else { Some(id) }
598+
if kind.may_leak() { None } else { Some(id) }
594599
});
595600
let n = leaks.len();
596601
self.dump_allocs(leaks);

src/librustc_mir/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub use self::place::{Place, PlaceTy, MemPlace, MPlaceTy};
3232

3333
pub use self::memory::{Memory, MemoryKind};
3434

35-
pub use self::machine::{Machine, AllocMap};
35+
pub use self::machine::{Machine, AllocMap, MayLeak};
3636

3737
pub use self::operand::{ScalarMaybeUndef, Value, ValTy, Operand, OpTy};
3838

0 commit comments

Comments
 (0)