From 2d827c953ffbc0369794e60e4ea8269ba5bc1400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Fri, 19 Apr 2024 23:10:04 +0200 Subject: [PATCH 1/2] Add `-Zmiri-env-set` to set environment variables without modifying the host environment This option allows to pass environment variables to the interpreted program without needing to modify the host environment (which may have undesired effects in some cases). --- README.md | 4 ++++ src/bin/miri.rs | 5 +++++ src/eval.rs | 5 ++++- src/shims/env.rs | 34 ++++++++++++++++++++++----------- tests/pass/shims/env/var-set.rs | 7 +++++++ 5 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 tests/pass/shims/env/var-set.rs diff --git a/README.md b/README.md index 948f1ee6c6..fb2ac0b267 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. + 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())); +} From 506ffc90c00688bd6c953ed953cf708415dc8b00 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 23 Apr 2024 11:17:36 +0200 Subject: [PATCH 2/2] Missing word at the end of sentence --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fb2ac0b267..95b99e9a54 100644 --- a/README.md +++ b/README.md @@ -311,7 +311,7 @@ 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. +* `-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.