]> git.lizzy.rs Git - rust.git/commitdiff
memory reachable through globals is not a leak any more; adjust for better memory...
authorRalf Jung <post@ralfj.de>
Sat, 4 Apr 2020 11:35:30 +0000 (13:35 +0200)
committerRalf Jung <post@ralfj.de>
Tue, 7 Apr 2020 17:03:41 +0000 (19:03 +0200)
src/machine.rs
tests/run-pass/leak-in-static.rs [new file with mode: 0644]
tests/run-pass/panic/catch_panic.rs

index f794453228b84d1fea3d68e971ca661226ce083c..ab4fe4a2178d44d7321946d687648e685cef0283 100644 (file)
@@ -6,6 +6,7 @@
 use std::num::NonZeroU64;
 use std::rc::Rc;
 use std::time::Instant;
+use std::fmt;
 
 use log::trace;
 use rand::rngs::StdRng;
@@ -62,6 +63,31 @@ fn into(self) -> MemoryKind<MiriMemoryKind> {
     }
 }
 
+impl MayLeak for MiriMemoryKind {
+    #[inline(always)]
+    fn may_leak(self) -> bool {
+        use self::MiriMemoryKind::*;
+        match self {
+            Rust | C | WinHeap | Env => false,
+            Machine | Global => true,
+        }
+    }
+}
+
+impl fmt::Display for MiriMemoryKind {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        use self::MiriMemoryKind::*;
+        match self {
+            Rust => write!(f, "Rust heap"),
+            C => write!(f, "C heap"),
+            WinHeap => write!(f, "Windows heap"),
+            Machine => write!(f, "machine-managed memory"),
+            Env => write!(f, "environment variable"),
+            Global => write!(f, "global"),
+        }
+    }
+}
+
 /// Extra per-allocation data
 #[derive(Debug, Clone)]
 pub struct AllocExtra {
@@ -491,14 +517,3 @@ fn memory_deallocated<'tcx>(
         }
     }
 }
-
-impl MayLeak for MiriMemoryKind {
-    #[inline(always)]
-    fn may_leak(self) -> bool {
-        use self::MiriMemoryKind::*;
-        match self {
-            Rust | C | WinHeap | Env => false,
-            Machine | Global => true,
-        }
-    }
-}
diff --git a/tests/run-pass/leak-in-static.rs b/tests/run-pass/leak-in-static.rs
new file mode 100644 (file)
index 0000000..b12cbbf
--- /dev/null
@@ -0,0 +1,8 @@
+static mut LEAKER: Option<Box<Vec<i32>>> = None;
+
+fn main() {
+    // Having memory "leaked" in globals is allowed.
+    unsafe {
+        LEAKER = Some(Box::new(vec![0; 42]));
+    }
+}
index 6408c940d98a4be5afc735db653178767a2f8544..7689b85f765037c8e15dd96d88fe074b363346a2 100644 (file)
@@ -77,9 +77,6 @@ fn main() {
     test(None, |_old_val| { debug_assert!(false); loop {} });
     test(None, |_old_val| { unsafe { (1 as *const i32).read() }; loop {} }); // trigger debug-assertion in libstd
 
-    // Cleanup: reset to default hook.
-    drop(std::panic::take_hook());
-
     eprintln!("Success!"); // Make sure we get this in stderr
 }