Skip to content

Commit

Permalink
Auto merge of #2472 - RalfJung:backtrace, r=RalfJung
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 6, 2022
2 parents f0cd098 + 141d5a6 commit aa53f3f
Show file tree
Hide file tree
Showing 25 changed files with 23 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/bin/miri.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#![feature(rustc_private, stmt_expr_attributes)]
#![allow(clippy::manual_range_contains, clippy::useless_format)]
#![allow(
clippy::manual_range_contains,
clippy::useless_format,
clippy::field_reassign_with_default
)]

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

fn main() {
// Snapshot a copy of the environment before `rustc` starts messing with it.
// (`install_ice_hook` might change `RUST_BACKTRACE`.)
let env_snapshot = env::vars_os().collect::<Vec<_>>();

// Earliest rustc setup.
rustc_driver::install_ice_hook();

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

// Parse our arguments and split them across `rustc` and `miri`.
let mut miri_config = miri::MiriConfig::default();
miri_config.env = env_snapshot;

let mut rustc_args = vec![];
let mut after_dashdash = false;

Expand Down
6 changes: 5 additions & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Main evaluator loop and setting up the initial stack frame.
use std::collections::HashSet;
use std::ffi::OsStr;
use std::ffi::{OsStr, OsString};
use std::iter;
use std::panic::{self, AssertUnwindSafe};
use std::thread;
Expand Down Expand Up @@ -72,6 +72,9 @@ pub enum BacktraceStyle {
/// Configuration needed to spawn a Miri instance.
#[derive(Clone)]
pub struct MiriConfig {
/// The host environment snapshot to use as basis for what is provided to the interpreted program.
/// (This is still subject to isolation as well as `excluded_env_vars` and `forwarded_env_vars`.)
pub env: Vec<(OsString, OsString)>,
/// Determine if validity checking is enabled.
pub validate: bool,
/// Determines if Stacked Borrows is enabled.
Expand Down Expand Up @@ -130,6 +133,7 @@ pub struct MiriConfig {
impl Default for MiriConfig {
fn default() -> MiriConfig {
MiriConfig {
env: vec![],
validate: true,
stacked_borrows: true,
check_alignment: AlignmentCheck::Int,
Expand Down
8 changes: 4 additions & 4 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ impl<'tcx> EnvVars<'tcx> {

// Skip the loop entirely if we don't want to forward anything.
if ecx.machine.communicate() || !config.forwarded_env_vars.is_empty() {
for (name, value) in env::vars_os() {
for (name, value) in &config.env {
// Always forward what is in `forwarded_env_vars`; that list can take precedence over excluded_env_vars.
let forward = config.forwarded_env_vars.iter().any(|v| **v == name)
let forward = config.forwarded_env_vars.iter().any(|v| **v == *name)
|| (ecx.machine.communicate()
&& !excluded_env_vars.iter().any(|v| **v == name));
&& !excluded_env_vars.iter().any(|v| **v == *name));
if forward {
let var_ptr = match target_os {
target if target_os_is_unix(target) =>
Expand All @@ -65,7 +65,7 @@ impl<'tcx> EnvVars<'tcx> {
unsupported
),
};
ecx.machine.env_vars.map.insert(name, var_ptr);
ecx.machine.env_vars.map.insert(name.clone(), var_ptr);
}
}
}
Expand Down
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.
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.
File renamed without changes.
1 change: 1 addition & 0 deletions tests/pass/backtrace/backtrace-global-alloc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@compile-flags: -Zmiri-disable-isolation
//@rustc-env: RUST_BACKTRACE=1

#![feature(backtrace)]

Expand Down
1 change: 1 addition & 0 deletions tests/pass/backtrace/backtrace-std.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@compile-flags: -Zmiri-disable-isolation
//@rustc-env: RUST_BACKTRACE=1

#![feature(backtrace)]

Expand Down

0 comments on commit aa53f3f

Please sign in to comment.