From 5dc60d974b0a76a1f4107790ea2249993d529e80 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Sun, 8 Mar 2020 11:54:47 -0500 Subject: [PATCH] move environ place to EnvVars --- src/machine.rs | 20 ++++++++------------ src/shims/env.rs | 15 +++++++++------ src/shims/foreign_items/posix/macos.rs | 2 +- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index d21ff3289757..f69606e48f52 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -70,7 +70,7 @@ pub struct AllocExtra { /// Extra global memory data #[derive(Clone, Debug)] -pub struct MemoryExtra<'tcx> { +pub struct MemoryExtra { pub stacked_borrows: Option, pub intptrcast: intptrcast::MemoryExtra, @@ -84,12 +84,9 @@ pub struct MemoryExtra<'tcx> { /// An allocation ID to report when it is being allocated /// (helps for debugging memory leaks). tracked_alloc_id: Option, - - /// Place where the `environ` static is stored. Lazily initialized, but then never changes. - pub(crate) environ: Option>, } -impl<'tcx> MemoryExtra<'tcx> { +impl MemoryExtra { pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option, tracked_alloc_id: Option) -> Self { let stacked_borrows = if stacked_borrows { Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag)))) @@ -102,12 +99,11 @@ impl<'tcx> MemoryExtra<'tcx> { extern_statics: FxHashMap::default(), rng: RefCell::new(rng), tracked_alloc_id, - environ: None, } } /// Sets up the "extern statics" for this machine. - pub fn init_extern_statics<'mir>( + pub fn init_extern_statics<'tcx, 'mir>( this: &mut MiriEvalContext<'mir, 'tcx>, ) -> InterpResult<'tcx> { let target_os = this.tcx.sess.target.target.target_os.as_str(); @@ -127,7 +123,7 @@ impl<'tcx> MemoryExtra<'tcx> { this.memory .extra .extern_statics - .insert(Symbol::intern("environ"), this.memory.extra.environ.unwrap().ptr.assert_ptr().alloc_id) + .insert(Symbol::intern("environ"), this.machine.env_vars.environ.unwrap().ptr.assert_ptr().alloc_id) .unwrap_none(); } _ => {} // No "extern statics" supported on this platform @@ -140,7 +136,7 @@ impl<'tcx> MemoryExtra<'tcx> { 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: EnvVars<'tcx>, /// Program arguments (`Option` because we can only initialize them after creating the ecx). /// These are *pointers* to argc/argv because macOS. @@ -214,7 +210,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> { type MemoryKinds = MiriMemoryKind; type FrameExtra = FrameData<'tcx>; - type MemoryExtra = MemoryExtra<'tcx>; + type MemoryExtra = MemoryExtra; type AllocExtra = AllocExtra; type PointerTag = Tag; type ExtraFnVal = Dlsym; @@ -340,7 +336,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> { } fn init_allocation_extra<'b>( - memory_extra: &MemoryExtra<'tcx>, + memory_extra: &MemoryExtra, id: AllocId, alloc: Cow<'b, Allocation>, kind: Option>, @@ -377,7 +373,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> { } #[inline(always)] - fn tag_static_base_pointer(memory_extra: &MemoryExtra<'tcx>, id: AllocId) -> Self::PointerTag { + fn tag_static_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag { if let Some(stacked_borrows) = memory_extra.stacked_borrows.as_ref() { stacked_borrows.borrow_mut().static_base_ptr(id) } else { diff --git a/src/shims/env.rs b/src/shims/env.rs index b06db5676f3e..7c6b6c942e5e 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -10,14 +10,17 @@ use rustc::ty::layout::Size; use rustc_mir::interpret::Pointer; #[derive(Default)] -pub struct EnvVars { +pub struct EnvVars<'tcx> { /// Stores pointers to the environment variables. These variables must be stored as /// null-terminated C strings with the `"{name}={value}"` format. map: FxHashMap>, + + /// Place where the `environ` static is stored. Lazily initialized, but then never changes. + pub(crate) environ: Option>, } -impl EnvVars { - pub(crate) fn init<'mir, 'tcx>( +impl<'tcx> EnvVars<'tcx> { + pub(crate) fn init<'mir>( ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>, excluded_env_vars: Vec, ) -> InterpResult<'tcx> { @@ -160,7 +163,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx fn update_environ(&mut self) -> InterpResult<'tcx> { let this = self.eval_context_mut(); // Deallocate the old environ value, if any. - if let Some(environ) = this.memory.extra.environ { + if let Some(environ) = this.machine.env_vars.environ { let old_vars_ptr = this.read_scalar(environ.into())?.not_undef()?; this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Machine.into())?; } else { @@ -168,7 +171,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let layout = this.layout_of(this.tcx.types.usize)?; let place = this.allocate(layout, MiriMemoryKind::Machine.into()); this.write_scalar(Scalar::from_machine_usize(0, &*this.tcx), place.into())?; - this.memory.extra.environ = Some(place); + this.machine.env_vars.environ = Some(place); } // Collect all the pointers to each variable in a vector. @@ -186,7 +189,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } this.write_scalar( vars_place.ptr, - this.memory.extra.environ.unwrap().into(), + this.machine.env_vars.environ.unwrap().into(), )?; Ok(()) diff --git a/src/shims/foreign_items/posix/macos.rs b/src/shims/foreign_items/posix/macos.rs index c5c6423e8501..0d067cc04138 100644 --- a/src/shims/foreign_items/posix/macos.rs +++ b/src/shims/foreign_items/posix/macos.rs @@ -58,7 +58,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // Environment related shims "_NSGetEnviron" => { - this.write_scalar(this.memory.extra.environ.unwrap().ptr, dest)?; + this.write_scalar(this.machine.env_vars.environ.unwrap().ptr, dest)?; } // Time related shims