Skip to content

Commit aa53f3f

Browse files
committed
Auto merge of rust-lang#2472 - RalfJung:backtrace, r=RalfJung
fix RUSTC_BACKTRACE always being set I kept wondering why Miri programs, whenever isolation is disabled, behave as if RUSTC_BACKTRACE was set. Finally I realized it's because some early rustc setup code sets that env var, and that is then propagated to the interpreted program. So fix that by taking a copy of the environment before any rustc setup, and use that copy as the basis for what is provided to the interpreted program.
2 parents f0cd098 + 141d5a6 commit aa53f3f

25 files changed

+23
-6
lines changed

src/bin/miri.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#![feature(rustc_private, stmt_expr_attributes)]
2-
#![allow(clippy::manual_range_contains, clippy::useless_format)]
2+
#![allow(
3+
clippy::manual_range_contains,
4+
clippy::useless_format,
5+
clippy::field_reassign_with_default
6+
)]
37

48
extern crate rustc_data_structures;
59
extern crate rustc_driver;
@@ -306,6 +310,11 @@ fn parse_comma_list<T: FromStr>(input: &str) -> Result<Vec<T>, T::Err> {
306310
}
307311

308312
fn main() {
313+
// Snapshot a copy of the environment before `rustc` starts messing with it.
314+
// (`install_ice_hook` might change `RUST_BACKTRACE`.)
315+
let env_snapshot = env::vars_os().collect::<Vec<_>>();
316+
317+
// Earliest rustc setup.
309318
rustc_driver::install_ice_hook();
310319

311320
// If the environment asks us to actually be rustc, then do that.
@@ -333,6 +342,8 @@ fn main() {
333342

334343
// Parse our arguments and split them across `rustc` and `miri`.
335344
let mut miri_config = miri::MiriConfig::default();
345+
miri_config.env = env_snapshot;
346+
336347
let mut rustc_args = vec![];
337348
let mut after_dashdash = false;
338349

src/eval.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Main evaluator loop and setting up the initial stack frame.
22
33
use std::collections::HashSet;
4-
use std::ffi::OsStr;
4+
use std::ffi::{OsStr, OsString};
55
use std::iter;
66
use std::panic::{self, AssertUnwindSafe};
77
use std::thread;
@@ -72,6 +72,9 @@ pub enum BacktraceStyle {
7272
/// Configuration needed to spawn a Miri instance.
7373
#[derive(Clone)]
7474
pub struct MiriConfig {
75+
/// The host environment snapshot to use as basis for what is provided to the interpreted program.
76+
/// (This is still subject to isolation as well as `excluded_env_vars` and `forwarded_env_vars`.)
77+
pub env: Vec<(OsString, OsString)>,
7578
/// Determine if validity checking is enabled.
7679
pub validate: bool,
7780
/// Determines if Stacked Borrows is enabled.
@@ -130,6 +133,7 @@ pub struct MiriConfig {
130133
impl Default for MiriConfig {
131134
fn default() -> MiriConfig {
132135
MiriConfig {
136+
env: vec![],
133137
validate: true,
134138
stacked_borrows: true,
135139
check_alignment: AlignmentCheck::Int,

src/shims/env.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ impl<'tcx> EnvVars<'tcx> {
4949

5050
// Skip the loop entirely if we don't want to forward anything.
5151
if ecx.machine.communicate() || !config.forwarded_env_vars.is_empty() {
52-
for (name, value) in env::vars_os() {
52+
for (name, value) in &config.env {
5353
// Always forward what is in `forwarded_env_vars`; that list can take precedence over excluded_env_vars.
54-
let forward = config.forwarded_env_vars.iter().any(|v| **v == name)
54+
let forward = config.forwarded_env_vars.iter().any(|v| **v == *name)
5555
|| (ecx.machine.communicate()
56-
&& !excluded_env_vars.iter().any(|v| **v == name));
56+
&& !excluded_env_vars.iter().any(|v| **v == *name));
5757
if forward {
5858
let var_ptr = match target_os {
5959
target if target_os_is_unix(target) =>
@@ -65,7 +65,7 @@ impl<'tcx> EnvVars<'tcx> {
6565
unsupported
6666
),
6767
};
68-
ecx.machine.env_vars.map.insert(name, var_ptr);
68+
ecx.machine.env_vars.map.insert(name.clone(), var_ptr);
6969
}
7070
}
7171
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/pass/backtrace/backtrace-global-alloc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@compile-flags: -Zmiri-disable-isolation
2+
//@rustc-env: RUST_BACKTRACE=1
23

34
#![feature(backtrace)]
45

tests/pass/backtrace/backtrace-std.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@compile-flags: -Zmiri-disable-isolation
2+
//@rustc-env: RUST_BACKTRACE=1
23

34
#![feature(backtrace)]
45

0 commit comments

Comments
 (0)