Skip to content

Commit b4032a8

Browse files
committed
Auto merge of #1216 - RalfJung:env, r=RalfJung
move environ init to update_environ @christianpoveda I thought of a way to simplify initialization a bit. :)
2 parents e9a2545 + 87dbf10 commit b4032a8

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/shims/env.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ impl EnvVars {
3030
}
3131
}
3232
}
33-
// Initialize the `environ` static
34-
let layout = ecx.layout_of(ecx.tcx.types.usize)?;
35-
let place = ecx.allocate(layout, MiriMemoryKind::Machine.into());
36-
ecx.write_scalar(Scalar::from_machine_usize(0, &*ecx.tcx), place.into())?;
37-
ecx.memory.extra.environ = Some(place);
3833
ecx.update_environ()
3934
}
4035
}
@@ -160,17 +155,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
160155
}
161156
}
162157

163-
/// Updates the `environ` static. It should not be called before
164-
/// `EnvVars::init`.
158+
/// Updates the `environ` static.
159+
/// The first time it gets called, also initializes `extra.environ`.
165160
fn update_environ(&mut self) -> InterpResult<'tcx> {
166161
let this = self.eval_context_mut();
167-
// Deallocate the old environ value.
168-
let old_vars_ptr = this.read_scalar(this.memory.extra.environ.unwrap().into())?.not_undef()?;
169-
// The pointer itself can be null because `EnvVars::init` only
170-
// initializes the place for the static but not the static itself.
171-
if !this.is_null(old_vars_ptr)? {
162+
// Deallocate the old environ value, if any.
163+
if let Some(environ) = this.memory.extra.environ {
164+
let old_vars_ptr = this.read_scalar(environ.into())?.not_undef()?;
172165
this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Machine.into())?;
166+
} else {
167+
// No `environ` allocated yet, let's do that.
168+
let layout = this.layout_of(this.tcx.types.usize)?;
169+
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
170+
this.write_scalar(Scalar::from_machine_usize(0, &*this.tcx), place.into())?;
171+
this.memory.extra.environ = Some(place);
173172
}
173+
174174
// Collect all the pointers to each variable in a vector.
175175
let mut vars: Vec<Scalar<Tag>> = this.machine.env_vars.map.values().map(|&ptr| ptr.into()).collect();
176176
// Add the trailing null pointer.

0 commit comments

Comments
 (0)