Skip to content

Commit 5dc60d9

Browse files
committed
move environ place to EnvVars
1 parent b4032a8 commit 5dc60d9

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

src/machine.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct AllocExtra {
7070

7171
/// Extra global memory data
7272
#[derive(Clone, Debug)]
73-
pub struct MemoryExtra<'tcx> {
73+
pub struct MemoryExtra {
7474
pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
7575
pub intptrcast: intptrcast::MemoryExtra,
7676

@@ -84,12 +84,9 @@ pub struct MemoryExtra<'tcx> {
8484
/// An allocation ID to report when it is being allocated
8585
/// (helps for debugging memory leaks).
8686
tracked_alloc_id: Option<AllocId>,
87-
88-
/// Place where the `environ` static is stored. Lazily initialized, but then never changes.
89-
pub(crate) environ: Option<MPlaceTy<'tcx, Tag>>,
9087
}
9188

92-
impl<'tcx> MemoryExtra<'tcx> {
89+
impl MemoryExtra {
9390
pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option<PtrId>, tracked_alloc_id: Option<AllocId>) -> Self {
9491
let stacked_borrows = if stacked_borrows {
9592
Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag))))
@@ -102,12 +99,11 @@ impl<'tcx> MemoryExtra<'tcx> {
10299
extern_statics: FxHashMap::default(),
103100
rng: RefCell::new(rng),
104101
tracked_alloc_id,
105-
environ: None,
106102
}
107103
}
108104

109105
/// Sets up the "extern statics" for this machine.
110-
pub fn init_extern_statics<'mir>(
106+
pub fn init_extern_statics<'tcx, 'mir>(
111107
this: &mut MiriEvalContext<'mir, 'tcx>,
112108
) -> InterpResult<'tcx> {
113109
let target_os = this.tcx.sess.target.target.target_os.as_str();
@@ -127,7 +123,7 @@ impl<'tcx> MemoryExtra<'tcx> {
127123
this.memory
128124
.extra
129125
.extern_statics
130-
.insert(Symbol::intern("environ"), this.memory.extra.environ.unwrap().ptr.assert_ptr().alloc_id)
126+
.insert(Symbol::intern("environ"), this.machine.env_vars.environ.unwrap().ptr.assert_ptr().alloc_id)
131127
.unwrap_none();
132128
}
133129
_ => {} // No "extern statics" supported on this platform
@@ -140,7 +136,7 @@ impl<'tcx> MemoryExtra<'tcx> {
140136
pub struct Evaluator<'tcx> {
141137
/// Environment variables set by `setenv`.
142138
/// Miri does not expose env vars from the host to the emulated program.
143-
pub(crate) env_vars: EnvVars,
139+
pub(crate) env_vars: EnvVars<'tcx>,
144140

145141
/// Program arguments (`Option` because we can only initialize them after creating the ecx).
146142
/// These are *pointers* to argc/argv because macOS.
@@ -214,7 +210,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
214210
type MemoryKinds = MiriMemoryKind;
215211

216212
type FrameExtra = FrameData<'tcx>;
217-
type MemoryExtra = MemoryExtra<'tcx>;
213+
type MemoryExtra = MemoryExtra;
218214
type AllocExtra = AllocExtra;
219215
type PointerTag = Tag;
220216
type ExtraFnVal = Dlsym;
@@ -340,7 +336,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
340336
}
341337

342338
fn init_allocation_extra<'b>(
343-
memory_extra: &MemoryExtra<'tcx>,
339+
memory_extra: &MemoryExtra,
344340
id: AllocId,
345341
alloc: Cow<'b, Allocation>,
346342
kind: Option<MemoryKind<Self::MemoryKinds>>,
@@ -377,7 +373,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
377373
}
378374

379375
#[inline(always)]
380-
fn tag_static_base_pointer(memory_extra: &MemoryExtra<'tcx>, id: AllocId) -> Self::PointerTag {
376+
fn tag_static_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag {
381377
if let Some(stacked_borrows) = memory_extra.stacked_borrows.as_ref() {
382378
stacked_borrows.borrow_mut().static_base_ptr(id)
383379
} else {

src/shims/env.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ use rustc::ty::layout::Size;
1010
use rustc_mir::interpret::Pointer;
1111

1212
#[derive(Default)]
13-
pub struct EnvVars {
13+
pub struct EnvVars<'tcx> {
1414
/// Stores pointers to the environment variables. These variables must be stored as
1515
/// null-terminated C strings with the `"{name}={value}"` format.
1616
map: FxHashMap<OsString, Pointer<Tag>>,
17+
18+
/// Place where the `environ` static is stored. Lazily initialized, but then never changes.
19+
pub(crate) environ: Option<MPlaceTy<'tcx, Tag>>,
1720
}
1821

19-
impl EnvVars {
20-
pub(crate) fn init<'mir, 'tcx>(
22+
impl<'tcx> EnvVars<'tcx> {
23+
pub(crate) fn init<'mir>(
2124
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
2225
excluded_env_vars: Vec<String>,
2326
) -> InterpResult<'tcx> {
@@ -160,15 +163,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
160163
fn update_environ(&mut self) -> InterpResult<'tcx> {
161164
let this = self.eval_context_mut();
162165
// Deallocate the old environ value, if any.
163-
if let Some(environ) = this.memory.extra.environ {
166+
if let Some(environ) = this.machine.env_vars.environ {
164167
let old_vars_ptr = this.read_scalar(environ.into())?.not_undef()?;
165168
this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Machine.into())?;
166169
} else {
167170
// No `environ` allocated yet, let's do that.
168171
let layout = this.layout_of(this.tcx.types.usize)?;
169172
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
170173
this.write_scalar(Scalar::from_machine_usize(0, &*this.tcx), place.into())?;
171-
this.memory.extra.environ = Some(place);
174+
this.machine.env_vars.environ = Some(place);
172175
}
173176

174177
// Collect all the pointers to each variable in a vector.
@@ -186,7 +189,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
186189
}
187190
this.write_scalar(
188191
vars_place.ptr,
189-
this.memory.extra.environ.unwrap().into(),
192+
this.machine.env_vars.environ.unwrap().into(),
190193
)?;
191194

192195
Ok(())

src/shims/foreign_items/posix/macos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5858

5959
// Environment related shims
6060
"_NSGetEnviron" => {
61-
this.write_scalar(this.memory.extra.environ.unwrap().ptr, dest)?;
61+
this.write_scalar(this.machine.env_vars.environ.unwrap().ptr, dest)?;
6262
}
6363

6464
// Time related shims

0 commit comments

Comments
 (0)