Skip to content

Commit 5ce7a04

Browse files
authored
Merge pull request #4437 from RalfJung/env-cleanup
skip env var memory for leak check
2 parents 0d43e2f + 510040f commit 5ce7a04

File tree

5 files changed

+9
-50
lines changed

5 files changed

+9
-50
lines changed

src/tools/miri/src/concurrency/data_race.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -971,14 +971,6 @@ pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> {
971971
}
972972
}
973973

974-
/// After all threads are done running, this allows data races to occur for subsequent
975-
/// 'administrative' machine accesses (that logically happen outside of the Abstract Machine).
976-
fn allow_data_races_all_threads_done(&mut self) {
977-
let this = self.eval_context_ref();
978-
assert!(this.have_all_terminated());
979-
this.machine.data_race.set_ongoing_action_data_race_free(true);
980-
}
981-
982974
/// Calls the callback with the "release" clock of the current thread.
983975
/// Other threads can acquire this clock in the future to establish synchronization
984976
/// with this program point.

src/tools/miri/src/eval.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,6 @@ impl<'tcx> MainThreadState<'tcx> {
283283
// to be like a global `static`, so that all memory reached by it is considered to "not leak".
284284
this.terminate_active_thread(TlsAllocAction::Leak)?;
285285

286-
// Machine cleanup. Only do this if all threads have terminated; threads that are still running
287-
// might cause Stacked Borrows errors (https://github.com/rust-lang/miri/issues/2396).
288-
if this.have_all_terminated() {
289-
// Even if all threads have terminated, we have to beware of data races since some threads
290-
// might not have joined the main thread (https://github.com/rust-lang/miri/issues/2020,
291-
// https://github.com/rust-lang/miri/issues/2508).
292-
this.allow_data_races_all_threads_done();
293-
EnvVars::cleanup(this).expect("error during env var cleanup");
294-
}
295-
296286
// Stop interpreter loop.
297287
throw_machine_stop!(TerminationInfo::Exit { code: exit_code, leak_check: true });
298288
}

src/tools/miri/src/machine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ pub enum MiriMemoryKind {
130130
WinHeap,
131131
/// Windows "local" memory (to be freed with `LocalFree`)
132132
WinLocal,
133-
/// Memory for args, errno, and other parts of the machine-managed environment.
133+
/// Memory for args, errno, env vars, and other parts of the machine-managed environment.
134134
/// This memory may leak.
135135
Machine,
136-
/// Memory allocated by the runtime (e.g. env vars). Separate from `Machine`
137-
/// because we clean it up and leak-check it.
136+
/// Memory allocated by the runtime, e.g. for readdir. Separate from `Machine` because we clean
137+
/// it up (or expect the user to invoke operations that clean it up) and leak-check it.
138138
Runtime,
139139
/// Globals copied from `tcx`.
140140
/// This memory may leak.

src/tools/miri/src/shims/env.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ impl<'tcx> EnvVars<'tcx> {
5959
interp_ok(())
6060
}
6161

62-
pub(crate) fn cleanup(ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>) -> InterpResult<'tcx> {
63-
let this = ecx.eval_context_mut();
64-
match this.machine.env_vars {
65-
EnvVars::Unix(_) => UnixEnvVars::cleanup(this),
66-
EnvVars::Windows(_) => interp_ok(()), // no cleanup needed
67-
EnvVars::Uninit => interp_ok(()),
68-
}
69-
}
70-
7162
pub(crate) fn unix(&self) -> &UnixEnvVars<'tcx> {
7263
match self {
7364
EnvVars::Unix(env) => env,

src/tools/miri/src/shims/unix/env.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use std::env;
12
use std::ffi::{OsStr, OsString};
23
use std::io::ErrorKind;
3-
use std::{env, mem};
44

55
use rustc_abi::{FieldIdx, Size};
66
use rustc_data_structures::fx::FxHashMap;
@@ -50,20 +50,6 @@ impl<'tcx> UnixEnvVars<'tcx> {
5050
interp_ok(UnixEnvVars { map: env_vars_machine, environ })
5151
}
5252

53-
pub(crate) fn cleanup(ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>) -> InterpResult<'tcx> {
54-
// Deallocate individual env vars.
55-
let env_vars = mem::take(&mut ecx.machine.env_vars.unix_mut().map);
56-
for (_name, ptr) in env_vars {
57-
ecx.deallocate_ptr(ptr, None, MiriMemoryKind::Runtime.into())?;
58-
}
59-
// Deallocate environ var list.
60-
let environ = &ecx.machine.env_vars.unix().environ;
61-
let old_vars_ptr = ecx.read_pointer(environ)?;
62-
ecx.deallocate_ptr(old_vars_ptr, None, MiriMemoryKind::Runtime.into())?;
63-
64-
interp_ok(())
65-
}
66-
6753
pub(crate) fn environ(&self) -> Pointer {
6854
self.environ.ptr()
6955
}
@@ -112,7 +98,7 @@ fn alloc_env_var<'tcx>(
11298
let mut name_osstring = name.to_os_string();
11399
name_osstring.push("=");
114100
name_osstring.push(value);
115-
ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Runtime.into())
101+
ecx.alloc_os_str_as_c_str(name_osstring.as_os_str(), MiriMemoryKind::Machine.into())
116102
}
117103

118104
/// Allocates an `environ` block with the given list of pointers.
@@ -128,7 +114,7 @@ fn alloc_environ_block<'tcx>(
128114
ecx.machine.layouts.mut_raw_ptr.ty,
129115
u64::try_from(vars.len()).unwrap(),
130116
))?;
131-
let vars_place = ecx.allocate(vars_layout, MiriMemoryKind::Runtime.into())?;
117+
let vars_place = ecx.allocate(vars_layout, MiriMemoryKind::Machine.into())?;
132118
for (idx, var) in vars.into_iter_enumerated() {
133119
let place = ecx.project_field(&vars_place, idx)?;
134120
ecx.write_pointer(var, &place)?;
@@ -171,7 +157,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
171157
if let Some((name, value)) = new {
172158
let var_ptr = alloc_env_var(this, &name, &value)?;
173159
if let Some(var) = this.machine.env_vars.unix_mut().map.insert(name, var_ptr) {
174-
this.deallocate_ptr(var, None, MiriMemoryKind::Runtime.into())?;
160+
this.deallocate_ptr(var, None, MiriMemoryKind::Machine.into())?;
175161
}
176162
this.update_environ()?;
177163
interp_ok(Scalar::from_i32(0)) // return zero on success
@@ -195,7 +181,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
195181
}
196182
if let Some(old) = success {
197183
if let Some(var) = old {
198-
this.deallocate_ptr(var, None, MiriMemoryKind::Runtime.into())?;
184+
this.deallocate_ptr(var, None, MiriMemoryKind::Machine.into())?;
199185
}
200186
this.update_environ()?;
201187
interp_ok(Scalar::from_i32(0))
@@ -253,7 +239,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
253239
// Deallocate the old environ list.
254240
let environ = this.machine.env_vars.unix().environ.clone();
255241
let old_vars_ptr = this.read_pointer(&environ)?;
256-
this.deallocate_ptr(old_vars_ptr, None, MiriMemoryKind::Runtime.into())?;
242+
this.deallocate_ptr(old_vars_ptr, None, MiriMemoryKind::Machine.into())?;
257243

258244
// Write the new list.
259245
let vals = this.machine.env_vars.unix().map.values().copied().collect();

0 commit comments

Comments
 (0)