Skip to content

Commit 325682a

Browse files
committed
Auto merge of #1301 - RalfJung:global-leaks, r=RalfJung
memory reachable through globals is not a leak Blocked on rust-lang/rust#70762 Fixes #940
2 parents 3342f15 + 7841f44 commit 325682a

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e6cef0445779724b469ab7b9a8d3c05d9e848ca8
1+
42abbd8878d3b67238f3611b0587c704ba94f39c

src/machine.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::cell::RefCell;
66
use std::num::NonZeroU64;
77
use std::rc::Rc;
88
use std::time::Instant;
9+
use std::fmt;
910

1011
use log::trace;
1112
use rand::rngs::StdRng;
@@ -69,6 +70,31 @@ impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
6970
}
7071
}
7172

73+
impl MayLeak for MiriMemoryKind {
74+
#[inline(always)]
75+
fn may_leak(self) -> bool {
76+
use self::MiriMemoryKind::*;
77+
match self {
78+
Rust | C | WinHeap | Env => false,
79+
Machine | Global => true,
80+
}
81+
}
82+
}
83+
84+
impl fmt::Display for MiriMemoryKind {
85+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86+
use self::MiriMemoryKind::*;
87+
match self {
88+
Rust => write!(f, "Rust heap"),
89+
C => write!(f, "C heap"),
90+
WinHeap => write!(f, "Windows heap"),
91+
Machine => write!(f, "machine-managed memory"),
92+
Env => write!(f, "environment variable"),
93+
Global => write!(f, "global"),
94+
}
95+
}
96+
}
97+
7298
/// Extra per-allocation data
7399
#[derive(Debug, Clone)]
74100
pub struct AllocExtra {
@@ -525,14 +551,3 @@ impl AllocationExtra<Tag> for AllocExtra {
525551
}
526552
}
527553
}
528-
529-
impl MayLeak for MiriMemoryKind {
530-
#[inline(always)]
531-
fn may_leak(self) -> bool {
532-
use self::MiriMemoryKind::*;
533-
match self {
534-
Rust | C | WinHeap | Env => false,
535-
Machine | Global => true,
536-
}
537-
}
538-
}

tests/compile-fail/stack_free.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Validation/SB changes why we fail
22
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

4-
// error-pattern: deallocating `Stack` memory using `Machine(Rust)` deallocation operation
4+
// error-pattern: deallocating stack variable memory using Rust heap deallocation operation
55

66
fn main() {
77
let x = 42;

tests/run-pass/leak-in-static.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
static mut LEAKER: Option<Box<Vec<i32>>> = None;
2+
3+
fn main() {
4+
// Having memory "leaked" in globals is allowed.
5+
unsafe {
6+
LEAKER = Some(Box::new(vec![0; 42]));
7+
}
8+
}

tests/run-pass/panic/catch_panic.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ fn main() {
7777
test(None, |_old_val| { debug_assert!(false); loop {} });
7878
test(None, |_old_val| { unsafe { (1 as *const i32).read() }; loop {} }); // trigger debug-assertion in libstd
7979

80-
// Cleanup: reset to default hook.
81-
drop(std::panic::take_hook());
82-
8380
eprintln!("Success!"); // Make sure we get this in stderr
8481
}
8582

0 commit comments

Comments
 (0)