Skip to content

Commit 3cc6795

Browse files
committed
Auto merge of #1106 - RalfJung:memleak-ignore, r=RalfJung
add flag to ignore memory leaks
2 parents 74c3ea5 + 0a63637 commit 3cc6795

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Several `-Z` flags are relevant for Miri:
159159
* `-Zmiri-disable-isolation` disables host host isolation. As a consequence,
160160
the program has access to host resources such as environment variables and
161161
randomness (and, eventually, file systems and more).
162+
* `-Zmiri-ignore-leaks` disables the memory leak checker.
162163
* `-Zmiri-env-exclude=<var>` keeps the `var` environment variable isolated from
163164
the host. Can be used multiple times to exclude several variables. The `TERM`
164165
environment variable is excluded by default.

benches/helpers/miri_helper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
2828
let config = miri::MiriConfig {
2929
validate: true,
3030
communicate: false,
31+
ignore_leaks: false,
3132
excluded_env_vars: vec![],
3233
args: vec![],
3334
seed: None,

src/bin/miri-rustc-tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
4141
let config = MiriConfig {
4242
validate: true,
4343
communicate: false,
44+
ignore_leaks: false,
4445
excluded_env_vars: vec![],
4546
args: vec![],
4647
seed: None,
@@ -60,6 +61,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
6061
let config = MiriConfig {
6162
validate: true,
6263
communicate: false,
64+
ignore_leaks: false,
6365
excluded_env_vars: vec![],
6466
args: vec![],
6567
seed: None

src/bin/miri.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ fn main() {
124124
// Parse our arguments and split them across `rustc` and `miri`.
125125
let mut validate = true;
126126
let mut communicate = false;
127+
let mut ignore_leaks = false;
127128
let mut seed: Option<u64> = None;
128129
let mut rustc_args = vec![];
129130
let mut miri_args = vec![];
@@ -145,6 +146,9 @@ fn main() {
145146
"-Zmiri-disable-isolation" => {
146147
communicate = true;
147148
},
149+
"-Zmiri-ignore-leaks" => {
150+
ignore_leaks = true;
151+
},
148152
"--" => {
149153
after_dashdash = true;
150154
}
@@ -200,6 +204,7 @@ fn main() {
200204
let miri_config = miri::MiriConfig {
201205
validate,
202206
communicate,
207+
ignore_leaks,
203208
excluded_env_vars,
204209
seed,
205210
args: miri_args,

src/eval.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub struct MiriConfig {
1818
pub validate: bool,
1919
/// Determines if communication with the host environment is enabled.
2020
pub communicate: bool,
21+
/// Determines if memory leaks should be ignored.
22+
pub ignore_leaks: bool,
2123
/// Environment variables that should always be isolated from the host.
2224
pub excluded_env_vars: Vec<String>,
2325
/// Command-line arguments passed to the interpreted program.
@@ -169,6 +171,11 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
169171
/// Returns `Some(return_code)` if program executed completed.
170172
/// Returns `None` if an evaluation error occured.
171173
pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) -> Option<i64> {
174+
// FIXME: We always ignore leaks on some platforms where we do not
175+
// correctly implement TLS destructors.
176+
let target_os = tcx.sess.target.target.target_os.to_lowercase();
177+
let ignore_leaks = config.ignore_leaks || target_os == "windows" || target_os == "macos";
178+
172179
let (mut ecx, ret_place) = match create_ecx(tcx, main_id, config) {
173180
Ok(v) => v,
174181
Err(mut err) => {
@@ -190,10 +197,6 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
190197
// Process the result.
191198
match res {
192199
Ok(return_code) => {
193-
// Disable the leak test on some platforms where we do not
194-
// correctly implement TLS destructors.
195-
let target_os = ecx.tcx.tcx.sess.target.target.target_os.to_lowercase();
196-
let ignore_leaks = target_os == "windows" || target_os == "macos";
197200
if !ignore_leaks {
198201
let leaks = ecx.memory.leak_report();
199202
if leaks != 0 {

tests/run-pass/memleak_ignored.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// compile-flags: -Zmiri-ignore-leaks
2+
3+
fn main() {
4+
std::mem::forget(Box::new(42));
5+
}

0 commit comments

Comments
 (0)