Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory reachable through globals is not a leak #1301

Merged
merged 2 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e6cef0445779724b469ab7b9a8d3c05d9e848ca8
42abbd8878d3b67238f3611b0587c704ba94f39c
37 changes: 26 additions & 11 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::cell::RefCell;
use std::num::NonZeroU64;
use std::rc::Rc;
use std::time::Instant;
use std::fmt;

use log::trace;
use rand::rngs::StdRng;
Expand Down Expand Up @@ -62,6 +63,31 @@ impl Into<MemoryKind<MiriMemoryKind>> for 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 {
Expand Down Expand Up @@ -491,14 +517,3 @@ impl AllocationExtra<Tag> for AllocExtra {
}
}
}

impl MayLeak for MiriMemoryKind {
#[inline(always)]
fn may_leak(self) -> bool {
use self::MiriMemoryKind::*;
match self {
Rust | C | WinHeap | Env => false,
Machine | Global => true,
}
}
}
2 changes: 1 addition & 1 deletion tests/compile-fail/stack_free.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Validation/SB changes why we fail
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows

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

fn main() {
let x = 42;
Expand Down
8 changes: 8 additions & 0 deletions tests/run-pass/leak-in-static.rs
Original file line number Diff line number Diff line change
@@ -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]));
}
}
3 changes: 0 additions & 3 deletions tests/run-pass/panic/catch_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down