@@ -30,11 +30,6 @@ impl EnvVars {
30
30
}
31
31
}
32
32
}
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) ;
38
33
ecx. update_environ ( )
39
34
}
40
35
}
@@ -160,17 +155,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
160
155
}
161
156
}
162
157
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 `.
165
160
fn update_environ ( & mut self ) -> InterpResult < ' tcx > {
166
161
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 ( ) ?;
172
165
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) ;
173
172
}
173
+
174
174
// Collect all the pointers to each variable in a vector.
175
175
let mut vars: Vec < Scalar < Tag > > = this. machine . env_vars . map . values ( ) . map ( |& ptr| ptr. into ( ) ) . collect ( ) ;
176
176
// Add the trailing null pointer.
0 commit comments