From 76d99c37c90496425fb7f3f8e2be4e9c16f276af Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 6 Aug 2022 15:21:08 -0400 Subject: [PATCH 1/3] fix RUSTC_BACKTRACE always being set --- src/bin/miri.rs | 7 +++++++ src/eval.rs | 6 +++++- src/shims/env.rs | 8 ++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/bin/miri.rs b/src/bin/miri.rs index 489eb959906..430f9e2f637 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -306,6 +306,11 @@ fn parse_comma_list(input: &str) -> Result, 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::>(); + + // Earliest rustc setup. rustc_driver::install_ice_hook(); // If the environment asks us to actually be rustc, then do that. @@ -333,6 +338,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; diff --git a/src/eval.rs b/src/eval.rs index cb24dea3a88..981776e3eeb 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -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; @@ -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. @@ -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, diff --git a/src/shims/env.rs b/src/shims/env.rs index db1ddf6291f..07b9cf18192 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -49,11 +49,11 @@ pub(crate) fn init<'mir>( // 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) => @@ -65,7 +65,7 @@ pub(crate) fn init<'mir>( unsupported ), }; - ecx.machine.env_vars.map.insert(name, var_ptr); + ecx.machine.env_vars.map.insert(name.clone(), var_ptr); } } } From d2ba40e9e1ff18f14b2d75462c0f7d7afcae7e76 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 6 Aug 2022 15:29:43 -0400 Subject: [PATCH 2/3] make tests pass again --- src/bin/miri.rs | 6 +++++- tests/pass/backtrace/backtrace-global-alloc.rs | 1 + tests/pass/backtrace/backtrace-std.rs | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bin/miri.rs b/src/bin/miri.rs index 430f9e2f637..dcccdecc6cf 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -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; diff --git a/tests/pass/backtrace/backtrace-global-alloc.rs b/tests/pass/backtrace/backtrace-global-alloc.rs index 45d6535bc13..406a5b62333 100644 --- a/tests/pass/backtrace/backtrace-global-alloc.rs +++ b/tests/pass/backtrace/backtrace-global-alloc.rs @@ -1,4 +1,5 @@ //@compile-flags: -Zmiri-disable-isolation +//@rustc-env: RUST_BACKTRACE=1 #![feature(backtrace)] diff --git a/tests/pass/backtrace/backtrace-std.rs b/tests/pass/backtrace/backtrace-std.rs index 488b87ede8f..9106310e513 100644 --- a/tests/pass/backtrace/backtrace-std.rs +++ b/tests/pass/backtrace/backtrace-std.rs @@ -1,4 +1,5 @@ //@compile-flags: -Zmiri-disable-isolation +//@rustc-env: RUST_BACKTRACE=1 #![feature(backtrace)] From 141d5a6396d1763d2e753edd60d3e6311c13c4c9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 6 Aug 2022 15:30:00 -0400 Subject: [PATCH 3/3] avoid panic/panic folder --- tests/panic/{panic => }/div-by-zero-2.rs | 0 tests/panic/{panic => }/div-by-zero-2.stderr | 0 tests/panic/{panic => }/overflowing-lsh-neg.rs | 0 tests/panic/{panic => }/overflowing-lsh-neg.stderr | 0 tests/panic/{panic => }/overflowing-rsh-1.rs | 0 tests/panic/{panic => }/overflowing-rsh-1.stderr | 0 tests/panic/{panic => }/overflowing-rsh-2.rs | 0 tests/panic/{panic => }/overflowing-rsh-2.stderr | 0 tests/panic/{panic => }/panic1.rs | 0 tests/panic/{panic => }/panic1.stderr | 0 tests/panic/{panic => }/panic2.rs | 0 tests/panic/{panic => }/panic2.stderr | 0 tests/panic/{panic => }/panic3.rs | 0 tests/panic/{panic => }/panic3.stderr | 0 tests/panic/{panic => }/panic4.rs | 0 tests/panic/{panic => }/panic4.stderr | 0 tests/panic/{panic => }/unsupported_foreign_function.rs | 0 tests/panic/{panic => }/unsupported_foreign_function.stderr | 0 tests/panic/{panic => }/unsupported_syscall.rs | 0 tests/panic/{panic => }/unsupported_syscall.stderr | 0 20 files changed, 0 insertions(+), 0 deletions(-) rename tests/panic/{panic => }/div-by-zero-2.rs (100%) rename tests/panic/{panic => }/div-by-zero-2.stderr (100%) rename tests/panic/{panic => }/overflowing-lsh-neg.rs (100%) rename tests/panic/{panic => }/overflowing-lsh-neg.stderr (100%) rename tests/panic/{panic => }/overflowing-rsh-1.rs (100%) rename tests/panic/{panic => }/overflowing-rsh-1.stderr (100%) rename tests/panic/{panic => }/overflowing-rsh-2.rs (100%) rename tests/panic/{panic => }/overflowing-rsh-2.stderr (100%) rename tests/panic/{panic => }/panic1.rs (100%) rename tests/panic/{panic => }/panic1.stderr (100%) rename tests/panic/{panic => }/panic2.rs (100%) rename tests/panic/{panic => }/panic2.stderr (100%) rename tests/panic/{panic => }/panic3.rs (100%) rename tests/panic/{panic => }/panic3.stderr (100%) rename tests/panic/{panic => }/panic4.rs (100%) rename tests/panic/{panic => }/panic4.stderr (100%) rename tests/panic/{panic => }/unsupported_foreign_function.rs (100%) rename tests/panic/{panic => }/unsupported_foreign_function.stderr (100%) rename tests/panic/{panic => }/unsupported_syscall.rs (100%) rename tests/panic/{panic => }/unsupported_syscall.stderr (100%) diff --git a/tests/panic/panic/div-by-zero-2.rs b/tests/panic/div-by-zero-2.rs similarity index 100% rename from tests/panic/panic/div-by-zero-2.rs rename to tests/panic/div-by-zero-2.rs diff --git a/tests/panic/panic/div-by-zero-2.stderr b/tests/panic/div-by-zero-2.stderr similarity index 100% rename from tests/panic/panic/div-by-zero-2.stderr rename to tests/panic/div-by-zero-2.stderr diff --git a/tests/panic/panic/overflowing-lsh-neg.rs b/tests/panic/overflowing-lsh-neg.rs similarity index 100% rename from tests/panic/panic/overflowing-lsh-neg.rs rename to tests/panic/overflowing-lsh-neg.rs diff --git a/tests/panic/panic/overflowing-lsh-neg.stderr b/tests/panic/overflowing-lsh-neg.stderr similarity index 100% rename from tests/panic/panic/overflowing-lsh-neg.stderr rename to tests/panic/overflowing-lsh-neg.stderr diff --git a/tests/panic/panic/overflowing-rsh-1.rs b/tests/panic/overflowing-rsh-1.rs similarity index 100% rename from tests/panic/panic/overflowing-rsh-1.rs rename to tests/panic/overflowing-rsh-1.rs diff --git a/tests/panic/panic/overflowing-rsh-1.stderr b/tests/panic/overflowing-rsh-1.stderr similarity index 100% rename from tests/panic/panic/overflowing-rsh-1.stderr rename to tests/panic/overflowing-rsh-1.stderr diff --git a/tests/panic/panic/overflowing-rsh-2.rs b/tests/panic/overflowing-rsh-2.rs similarity index 100% rename from tests/panic/panic/overflowing-rsh-2.rs rename to tests/panic/overflowing-rsh-2.rs diff --git a/tests/panic/panic/overflowing-rsh-2.stderr b/tests/panic/overflowing-rsh-2.stderr similarity index 100% rename from tests/panic/panic/overflowing-rsh-2.stderr rename to tests/panic/overflowing-rsh-2.stderr diff --git a/tests/panic/panic/panic1.rs b/tests/panic/panic1.rs similarity index 100% rename from tests/panic/panic/panic1.rs rename to tests/panic/panic1.rs diff --git a/tests/panic/panic/panic1.stderr b/tests/panic/panic1.stderr similarity index 100% rename from tests/panic/panic/panic1.stderr rename to tests/panic/panic1.stderr diff --git a/tests/panic/panic/panic2.rs b/tests/panic/panic2.rs similarity index 100% rename from tests/panic/panic/panic2.rs rename to tests/panic/panic2.rs diff --git a/tests/panic/panic/panic2.stderr b/tests/panic/panic2.stderr similarity index 100% rename from tests/panic/panic/panic2.stderr rename to tests/panic/panic2.stderr diff --git a/tests/panic/panic/panic3.rs b/tests/panic/panic3.rs similarity index 100% rename from tests/panic/panic/panic3.rs rename to tests/panic/panic3.rs diff --git a/tests/panic/panic/panic3.stderr b/tests/panic/panic3.stderr similarity index 100% rename from tests/panic/panic/panic3.stderr rename to tests/panic/panic3.stderr diff --git a/tests/panic/panic/panic4.rs b/tests/panic/panic4.rs similarity index 100% rename from tests/panic/panic/panic4.rs rename to tests/panic/panic4.rs diff --git a/tests/panic/panic/panic4.stderr b/tests/panic/panic4.stderr similarity index 100% rename from tests/panic/panic/panic4.stderr rename to tests/panic/panic4.stderr diff --git a/tests/panic/panic/unsupported_foreign_function.rs b/tests/panic/unsupported_foreign_function.rs similarity index 100% rename from tests/panic/panic/unsupported_foreign_function.rs rename to tests/panic/unsupported_foreign_function.rs diff --git a/tests/panic/panic/unsupported_foreign_function.stderr b/tests/panic/unsupported_foreign_function.stderr similarity index 100% rename from tests/panic/panic/unsupported_foreign_function.stderr rename to tests/panic/unsupported_foreign_function.stderr diff --git a/tests/panic/panic/unsupported_syscall.rs b/tests/panic/unsupported_syscall.rs similarity index 100% rename from tests/panic/panic/unsupported_syscall.rs rename to tests/panic/unsupported_syscall.rs diff --git a/tests/panic/panic/unsupported_syscall.stderr b/tests/panic/unsupported_syscall.stderr similarity index 100% rename from tests/panic/panic/unsupported_syscall.stderr rename to tests/panic/unsupported_syscall.stderr