diff --git a/README.md b/README.md index 948f1ee6c6..95b99e9a54 100644 --- a/README.md +++ b/README.md @@ -311,6 +311,10 @@ environment variable. We first document the most relevant and most commonly used * `-Zmiri-env-forward=` forwards the `var` environment variable to the interpreted program. Can be used multiple times to forward several variables. Execution will still be deterministic if the value of forwarded variables stays the same. Has no effect if `-Zmiri-disable-isolation` is set. +* `-Zmiri-env-set==` sets the `var` environment variable to `value` in the interpreted program. + It can be used to pass environment variables without needing to alter the host environment. It can + be used multiple times to set several variables. If `-Zmiri-disable-isolation` or `-Zmiri-env-forward` + is set, values set with this option will have priority over values from the host environment. * `-Zmiri-ignore-leaks` disables the memory leak checker, and also allows some remaining threads to exist when the main thread exits. * `-Zmiri-isolation-error=` configures Miri's response to operations diff --git a/src/bin/miri.rs b/src/bin/miri.rs index 3f7a965e9d..c3315edac2 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -498,6 +498,11 @@ fn main() { ); } else if let Some(param) = arg.strip_prefix("-Zmiri-env-forward=") { miri_config.forwarded_env_vars.push(param.to_owned()); + } else if let Some(param) = arg.strip_prefix("-Zmiri-env-set=") { + let Some((name, value)) = param.split_once('=') else { + show_error!("-Zmiri-env-set requires an argument of the form ="); + }; + miri_config.set_env_vars.insert(name.to_owned(), value.to_owned()); } else if let Some(param) = arg.strip_prefix("-Zmiri-track-pointer-tag=") { let ids: Vec = match parse_comma_list(param) { Ok(ids) => ids, diff --git a/src/eval.rs b/src/eval.rs index df0ede1e1b..3623d97e75 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -9,7 +9,7 @@ use std::thread; use crate::concurrency::thread::TlsAllocAction; use crate::diagnostics::report_leaks; -use rustc_data_structures::fx::FxHashSet; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir::def::Namespace; use rustc_hir::def_id::DefId; use rustc_middle::ty::{ @@ -100,6 +100,8 @@ pub struct MiriConfig { pub ignore_leaks: bool, /// Environment variables that should always be forwarded from the host. pub forwarded_env_vars: Vec, + /// Additional environment variables that should be set in the interpreted program. + pub set_env_vars: FxHashMap, /// Command-line arguments passed to the interpreted program. pub args: Vec, /// The seed to use when non-determinism or randomness are required (e.g. ptr-to-int cast, `getrandom()`). @@ -163,6 +165,7 @@ impl Default for MiriConfig { isolated_op: IsolatedOp::Reject(RejectOpWith::Abort), ignore_leaks: false, forwarded_env_vars: vec![], + set_env_vars: FxHashMap::default(), args: vec![], seed: None, tracked_pointer_tags: FxHashSet::default(), diff --git a/src/shims/env.rs b/src/shims/env.rs index 1779189c9c..d97873ce72 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -44,21 +44,15 @@ impl<'tcx> EnvVars<'tcx> { let forward = ecx.machine.communicate() || config.forwarded_env_vars.iter().any(|v| **v == *name); if forward { - let var_ptr = match ecx.tcx.sess.target.os.as_ref() { - _ if ecx.target_os_is_unix() => - alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx)?, - "windows" => alloc_env_var_as_wide_str(name.as_ref(), value.as_ref(), ecx)?, - unsupported => - throw_unsup_format!( - "environment support for target OS `{}` not yet available", - unsupported - ), - }; - ecx.machine.env_vars.map.insert(name.clone(), var_ptr); + add_env_var(ecx, name, value)?; } } } + for (name, value) in &config.set_env_vars { + add_env_var(ecx, OsStr::new(name), OsStr::new(value))?; + } + // Initialize the `environ` pointer when needed. if ecx.target_os_is_unix() { // This is memory backing an extern static, hence `ExternStatic`, not `Env`. @@ -89,6 +83,24 @@ impl<'tcx> EnvVars<'tcx> { } } +fn add_env_var<'mir, 'tcx>( + ecx: &mut InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>, + name: &OsStr, + value: &OsStr, +) -> InterpResult<'tcx, ()> { + let var_ptr = match ecx.tcx.sess.target.os.as_ref() { + _ if ecx.target_os_is_unix() => alloc_env_var_as_c_str(name, value, ecx)?, + "windows" => alloc_env_var_as_wide_str(name, value, ecx)?, + unsupported => + throw_unsup_format!( + "environment support for target OS `{}` not yet available", + unsupported + ), + }; + ecx.machine.env_vars.map.insert(name.to_os_string(), var_ptr); + Ok(()) +} + fn alloc_env_var_as_c_str<'mir, 'tcx>( name: &OsStr, value: &OsStr, diff --git a/tests/pass/shims/env/var-set.rs b/tests/pass/shims/env/var-set.rs new file mode 100644 index 0000000000..2875b6c815 --- /dev/null +++ b/tests/pass/shims/env/var-set.rs @@ -0,0 +1,7 @@ +// Test a value set on the host (MIRI_ENV_VAR_TEST) and one that is not. +//@compile-flags: -Zmiri-env-set=MIRI_ENV_VAR_TEST=test_value_1 -Zmiri-env-set=TEST_VAR_2=test_value_2 + +fn main() { + assert_eq!(std::env::var("MIRI_ENV_VAR_TEST"), Ok("test_value_1".to_owned())); + assert_eq!(std::env::var("TEST_VAR_2"), Ok("test_value_2".to_owned())); +}