Skip to content

Commit aedc34c

Browse files
committed
deallocate old environ
1 parent 7882dfb commit aedc34c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub struct MemoryExtra<'tcx> {
8585
/// (helps for debugging memory leaks).
8686
tracked_alloc_id: Option<AllocId>,
8787

88-
/// Place where the `environ` static is stored.
88+
/// Place where the `environ` static is stored. Its value should not change after initialization.
8989
pub(crate) environ: Option<MPlaceTy<'tcx, Tag>>,
9090
}
9191

src/shims/env.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
155155
}
156156
}
157157

158+
/// Updates the `environ` static. It should not be called before
159+
/// `MemoryExtra::init_extern_statics`.
158160
fn update_environ(&mut self) -> InterpResult<'tcx> {
159161
let this = self.eval_context_mut();
162+
// Deallocate the old environ value.
163+
let old_vars_ptr = this.read_scalar(this.memory.extra.environ.unwrap().into())?.not_undef()?;
164+
// The pointer itself can be null because `MemoryExtra::init_extern_statics` only
165+
// initializes the place for the static but not the static itself.
166+
if !this.is_null(old_vars_ptr)? {
167+
this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Machine.into())?;
168+
}
160169
// Collect all the pointers to each variable in a vector.
161170
let mut vars: Vec<Scalar<Tag>> = this.machine.env_vars.map.values().map(|&ptr| ptr.into()).collect();
162171
// Add the trailing null pointer.
@@ -170,7 +179,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
170179
let place = this.mplace_field(vars_place, idx as u64)?;
171180
this.write_scalar(var, place.into())?;
172181
}
173-
174182
this.write_scalar(
175183
vars_place.ptr,
176184
this.memory.extra.environ.unwrap().into(),

0 commit comments

Comments
 (0)