From afc6713e4178fc4c375e7acea8898534f2e6fba3 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 13 Aug 2019 16:17:41 -0500 Subject: [PATCH] Reorganize shims::env::EnvVars --- src/eval.rs | 8 +++----- src/lib.rs | 1 + src/machine.rs | 7 ++++--- src/shims/env.rs | 9 ++++++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index 5e6c8129fb31..936ae5b89532 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -12,8 +12,8 @@ use crate::{ InterpResult, InterpError, InterpCx, StackPopCleanup, struct_error, Scalar, Tag, Pointer, FnVal, MemoryExtra, MiriMemoryKind, Evaluator, TlsEvalContextExt, HelpersEvalContextExt, + ShimsEnvVars, }; -use crate::shims::env::EnvVars; /// Configuration needed to spawn a Miri instance. #[derive(Clone)] @@ -40,6 +40,8 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( MemoryExtra::new(StdRng::seed_from_u64(config.seed.unwrap_or(0)), config.validate), ); + ShimsEnvVars::init(config.communicate, &mut ecx, &tcx); + let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id); let main_mir = ecx.load_mir(main_instance.def)?; @@ -164,10 +166,6 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( assert!(args.next().is_none(), "start lang item has more arguments than expected"); - if config.communicate { - EnvVars::init(&mut ecx, &tcx); - } - Ok(ecx) } diff --git a/src/lib.rs b/src/lib.rs index 58f572bf7011..216e41d4f838 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,7 @@ pub use crate::shims::foreign_items::EvalContextExt as ForeignItemsEvalContextEx pub use crate::shims::intrinsics::EvalContextExt as IntrinsicsEvalContextExt; pub use crate::shims::tls::{EvalContextExt as TlsEvalContextExt, TlsData}; pub use crate::shims::dlsym::{Dlsym, EvalContextExt as DlsymEvalContextExt}; +pub use crate::shims::env::{EnvVars as ShimsEnvVars}; pub use crate::operator::EvalContextExt as OperatorEvalContextExt; pub use crate::range_map::RangeMap; pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt}; diff --git a/src/machine.rs b/src/machine.rs index cc0c85d6603d..635b46bcdb04 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -14,7 +14,6 @@ use rustc::ty::{self, layout::{Size, LayoutOf}, TyCtxt}; use rustc::mir; use crate::*; -use crate::shims::env::EnvVars; // Some global facts about the emulated machine. pub const PAGE_SIZE: u64 = 4*1024; // FIXME: adjust to target architecture @@ -79,7 +78,7 @@ impl MemoryExtra { pub struct Evaluator<'tcx> { /// Environment variables set by `setenv`. /// Miri does not expose env vars from the host to the emulated program. - pub(crate) env_vars: EnvVars, + pub(crate) env_vars: ShimsEnvVars, /// Program arguments (`Option` because we can only initialize them after creating the ecx). /// These are *pointers* to argc/argv because macOS. @@ -101,7 +100,9 @@ pub struct Evaluator<'tcx> { impl<'tcx> Evaluator<'tcx> { pub(crate) fn new(communicate: bool) -> Self { Evaluator { - env_vars: EnvVars::default(), + // `env_vars` could be initialized properly here if `Memory` were available before + // calling this method. + env_vars: ShimsEnvVars::default(), argc: None, argv: None, cmd_line: None, diff --git a/src/shims/env.rs b/src/shims/env.rs index 09d87d27ebc2..4a15eb4cfb48 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -12,12 +12,15 @@ pub struct EnvVars { impl EnvVars { pub(crate) fn init<'mir, 'tcx>( + communicate: bool, ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>, tcx: &TyCtxt<'tcx>, ) { - for (name, value) in std::env::vars() { - let value = alloc_env_value(value.as_bytes(), ecx.memory_mut(), tcx); - ecx.machine.env_vars.map.insert(name.into_bytes(), value); + if communicate { + for (name, value) in std::env::vars() { + let value = alloc_env_value(value.as_bytes(), ecx.memory_mut(), tcx); + ecx.machine.env_vars.map.insert(name.into_bytes(), value); + } } }