Skip to content

Commit 0256c4d

Browse files
committed
detect memory leaks
1 parent 96907bf commit 0256c4d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/eval_context.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,13 @@ pub fn eval_main<'a, 'tcx: 'a>(
15021502
loop {
15031503
match ecx.step() {
15041504
Ok(true) => {}
1505-
Ok(false) => return,
1505+
Ok(false) => {
1506+
let leaks = ecx.memory.leak_report();
1507+
if leaks != 0 {
1508+
tcx.sess.err("the evaluated program leaked memory");
1509+
}
1510+
return;
1511+
}
15061512
Err(e) => {
15071513
report(tcx, &ecx, e);
15081514
return;

src/memory.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,23 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
585585
}
586586
}
587587
}
588+
589+
pub fn leak_report(&self) -> usize {
590+
trace!("### LEAK REPORT ###");
591+
let leaks: Vec<_> = self.alloc_map
592+
.iter()
593+
.filter_map(|(&key, val)| {
594+
if val.static_kind == StaticKind::NotStatic {
595+
Some(key)
596+
} else {
597+
None
598+
}
599+
})
600+
.collect();
601+
let n = leaks.len();
602+
self.dump_allocs(leaks);
603+
n
604+
}
588605
}
589606

590607
fn dump_fn_def<'tcx>(fn_def: FunctionDefinition<'tcx>) -> String {

0 commit comments

Comments
 (0)