Skip to content

Commit ee8afea

Browse files
committed
Auto merge of #935 - christianpoveda:blacklist-env-vars, r=RalfJung
Exclude environment variables from host communication related issue: #933 r? @RalfJung
2 parents e3b87f6 + abcda6d commit ee8afea

File tree

7 files changed

+31
-4
lines changed

7 files changed

+31
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ Several `-Z` flags are relevant for Miri:
160160
* `-Zmiri-disable-isolation` disables host host isolation. As a consequence,
161161
the program has access to host resources such as environment variables and
162162
randomness (and, eventually, file systems and more).
163+
* `-Zmiri-env-exclude=<var>` keeps the `var` environment variable isolated from
164+
the host. Can be used multiple times to exclude several variables.
163165
* `-Zmir-opt-level` controls how many MIR optimizations are performed. Miri
164166
overrides the default to be `0`; be advised that using any higher level can
165167
make Miri miss bugs in your program because they got optimized away.

benches/helpers/miri_helper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
2828
let config = miri::MiriConfig {
2929
validate: true,
3030
communicate: false,
31+
excluded_env_vars: vec![],
3132
args: vec![],
3233
seed: None,
3334
};

src/bin/miri-rustc-tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
5151
let config = MiriConfig {
5252
validate: true,
5353
communicate: false,
54+
excluded_env_vars: vec![],
5455
args: vec![],
5556
seed: None,
5657
};
@@ -69,6 +70,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
6970
let config = MiriConfig {
7071
validate: true,
7172
communicate: false,
73+
excluded_env_vars: vec![],
7274
args: vec![],
7375
seed: None
7476
};

src/bin/miri.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ fn main() {
135135
let mut rustc_args = vec![];
136136
let mut miri_args = vec![];
137137
let mut after_dashdash = false;
138+
let mut excluded_env_vars = vec![];
138139
for arg in std::env::args() {
139140
if rustc_args.is_empty() {
140141
// Very first arg: for `rustc`.
@@ -175,6 +176,9 @@ fn main() {
175176
seed = Some(u64::from_be_bytes(bytes));
176177

177178
},
179+
arg if arg.starts_with("-Zmiri-env-exclude=") => {
180+
excluded_env_vars.push(arg.trim_start_matches("-Zmiri-env-exclude=").to_owned());
181+
},
178182
_ => {
179183
rustc_args.push(arg);
180184
}
@@ -200,7 +204,13 @@ fn main() {
200204

201205
debug!("rustc arguments: {:?}", rustc_args);
202206
debug!("miri arguments: {:?}", miri_args);
203-
let miri_config = miri::MiriConfig { validate, communicate, args: miri_args, seed };
207+
let miri_config = miri::MiriConfig {
208+
validate,
209+
communicate,
210+
excluded_env_vars,
211+
seed,
212+
args: miri_args,
213+
};
204214
let result = rustc_driver::report_ices_to_stderr_if_any(move || {
205215
rustc_driver::run_compiler(&rustc_args, &mut MiriCompilerCalls { miri_config }, None, None)
206216
}).and_then(|result| result);

src/eval.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ pub struct MiriConfig {
2222
pub validate: bool,
2323
/// Determines if communication with the host environment is enabled.
2424
pub communicate: bool,
25+
/// Environment variables that should always be isolated from the host.
26+
pub excluded_env_vars: Vec<String>,
27+
/// Command-line arguments passed to the interpreted program.
2528
pub args: Vec<String>,
2629
/// The seed to use when non-determinism or randomness are required (e.g. ptr-to-int cast, `getrandom()`).
2730
pub seed: Option<u64>,
@@ -40,7 +43,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
4043
MemoryExtra::new(StdRng::seed_from_u64(config.seed.unwrap_or(0)), config.validate),
4144
);
4245
// Complete initialization.
43-
EnvVars::init(&mut ecx);
46+
EnvVars::init(&mut ecx, config.excluded_env_vars);
4447

4548
// Setup first stack-frame
4649
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);

src/shims/env.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ pub struct EnvVars {
1515
impl EnvVars {
1616
pub(crate) fn init<'mir, 'tcx>(
1717
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
18+
excluded_env_vars: Vec<String>,
1819
) {
1920
if ecx.machine.communicate {
2021
for (name, value) in std::env::vars() {
21-
let var_ptr = alloc_env_var(name.as_bytes(), value.as_bytes(), ecx.memory_mut());
22-
ecx.machine.env_vars.map.insert(name.into_bytes(), var_ptr);
22+
if !excluded_env_vars.contains(&name) {
23+
let var_ptr = alloc_env_var(name.as_bytes(), value.as_bytes(), ecx.memory_mut());
24+
ecx.machine.env_vars.map.insert(name.into_bytes(), var_ptr);
25+
}
2326
}
2427
}
2528
}

tests/run-pass/env-exclude.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ignore-windows: TODO env var emulation stubbed out on Windows
2+
// compile-flags: -Zmiri-disable-isolation -Zmiri-env-exclude=MIRI_ENV_VAR_TEST
3+
4+
fn main() {
5+
assert!(std::env::var("MIRI_ENV_VAR_TEST").is_err());
6+
}

0 commit comments

Comments
 (0)