]> git.lizzy.rs Git - rust.git/commitdiff
detect memory leaks
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 14 Feb 2017 14:35:13 +0000 (15:35 +0100)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 24 Feb 2017 09:41:15 +0000 (10:41 +0100)
src/eval_context.rs
src/memory.rs

index 1bf71e149dc34914689f5caecf59866b17b69ab9..fb83eff502a2d4eb09a0b1f4a05b867789f301dc 100644 (file)
@@ -1510,7 +1510,13 @@ pub fn eval_main<'a, 'tcx: 'a>(
     loop {
         match ecx.step() {
             Ok(true) => {}
-            Ok(false) => return,
+            Ok(false) => {
+                let leaks = ecx.memory.leak_report();
+                if leaks != 0 {
+                    tcx.sess.err("the evaluated program leaked memory");
+                }
+                return;
+            }
             Err(e) => {
                 report(tcx, &ecx, e);
                 return;
index 0c7b4971e1df7e974f4e81cf97987f4b6cf3a465..459a9bed4128fc6cbb7314578ab798d6413f806f 100644 (file)
@@ -585,6 +585,23 @@ pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
             }
         }
     }
+
+    pub fn leak_report(&self) -> usize {
+        trace!("### LEAK REPORT ###");
+        let leaks: Vec<_> = self.alloc_map
+            .iter()
+            .filter_map(|(&key, val)| {
+                if val.static_kind == StaticKind::NotStatic {
+                    Some(key)
+                } else {
+                    None
+                }
+            })
+            .collect();
+        let n = leaks.len();
+        self.dump_allocs(leaks);
+        n
+    }
 }
 
 fn dump_fn_def<'tcx>(fn_def: FunctionDefinition<'tcx>) -> String {