@@ -46,6 +46,20 @@ impl<'tcx> EnvVars<'tcx> {
46
46
}
47
47
ecx. update_environ ( )
48
48
}
49
+
50
+ pub ( crate ) fn cleanup < ' mir > (
51
+ ecx : & mut InterpCx < ' mir , ' tcx , Evaluator < ' tcx > > ,
52
+ ) -> InterpResult < ' tcx > {
53
+ // Deallocate individual env vars.
54
+ for ( _name, ptr) in ecx. machine . env_vars . map . drain ( ) {
55
+ ecx. memory . deallocate ( ptr, None , MiriMemoryKind :: Env . into ( ) ) ?;
56
+ }
57
+ // Deallocate environ var list.
58
+ let environ = ecx. machine . env_vars . environ . take ( ) . unwrap ( ) ;
59
+ let old_vars_ptr = ecx. read_scalar ( environ. into ( ) ) ?. not_undef ( ) ?;
60
+ ecx. memory . deallocate ( ecx. force_ptr ( old_vars_ptr) ?, None , MiriMemoryKind :: Env . into ( ) ) ?;
61
+ Ok ( ( ) )
62
+ }
49
63
}
50
64
51
65
fn alloc_env_var_as_c_str < ' mir , ' tcx > (
@@ -56,7 +70,7 @@ fn alloc_env_var_as_c_str<'mir, 'tcx>(
56
70
let mut name_osstring = name. to_os_string ( ) ;
57
71
name_osstring. push ( "=" ) ;
58
72
name_osstring. push ( value) ;
59
- Ok ( ecx. alloc_os_str_as_c_str ( name_osstring. as_os_str ( ) , MiriMemoryKind :: Machine . into ( ) ) )
73
+ Ok ( ecx. alloc_os_str_as_c_str ( name_osstring. as_os_str ( ) , MiriMemoryKind :: Env . into ( ) ) )
60
74
}
61
75
62
76
fn alloc_env_var_as_wide_str < ' mir , ' tcx > (
@@ -67,7 +81,7 @@ fn alloc_env_var_as_wide_str<'mir, 'tcx>(
67
81
let mut name_osstring = name. to_os_string ( ) ;
68
82
name_osstring. push ( "=" ) ;
69
83
name_osstring. push ( value) ;
70
- Ok ( ecx. alloc_os_str_as_wide_str ( name_osstring. as_os_str ( ) , MiriMemoryKind :: Machine . into ( ) ) )
84
+ Ok ( ecx. alloc_os_str_as_wide_str ( name_osstring. as_os_str ( ) , MiriMemoryKind :: Env . into ( ) ) )
71
85
}
72
86
73
87
impl < ' mir , ' tcx > EvalContextExt < ' mir , ' tcx > for crate :: MiriEvalContext < ' mir , ' tcx > { }
@@ -146,7 +160,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
146
160
}
147
161
// Allocate environment block & Store environment variables to environment block.
148
162
// Final null terminator(block terminator) is added by `alloc_os_str_to_wide_str`.
149
- // FIXME: MemoryKind should be `Machine `, blocked on https://github.com/rust-lang/rust/pull/70479.
163
+ // FIXME: MemoryKind should be `Env `, blocked on https://github.com/rust-lang/rust/pull/70479.
150
164
let envblock_ptr = this. alloc_os_str_as_wide_str ( & env_vars, MiriMemoryKind :: WinHeap . into ( ) ) ;
151
165
// If the function succeeds, the return value is a pointer to the environment block of the current process.
152
166
Ok ( envblock_ptr. into ( ) )
@@ -158,7 +172,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
158
172
this. assert_target_os ( "windows" , "FreeEnvironmentStringsW" ) ;
159
173
160
174
let env_block_ptr = this. read_scalar ( env_block_op) ?. not_undef ( ) ?;
161
- // FIXME: MemoryKind should be `Machine `, blocked on https://github.com/rust-lang/rust/pull/70479.
175
+ // FIXME: MemoryKind should be `Env `, blocked on https://github.com/rust-lang/rust/pull/70479.
162
176
let result = this. memory . deallocate ( this. force_ptr ( env_block_ptr) ?, None , MiriMemoryKind :: WinHeap . into ( ) ) ;
163
177
// If the function succeeds, the return value is nonzero.
164
178
Ok ( result. is_ok ( ) as i32 )
@@ -188,7 +202,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
188
202
let var_ptr = alloc_env_var_as_c_str ( & name, & value, & mut this) ?;
189
203
if let Some ( var) = this. machine . env_vars . map . insert ( name, var_ptr) {
190
204
this. memory
191
- . deallocate ( var, None , MiriMemoryKind :: Machine . into ( ) ) ?;
205
+ . deallocate ( var, None , MiriMemoryKind :: Env . into ( ) ) ?;
192
206
}
193
207
this. update_environ ( ) ?;
194
208
Ok ( 0 ) // return zero on success
@@ -225,7 +239,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
225
239
} else if this. is_null ( value_ptr) ? {
226
240
// Delete environment variable `{name}`
227
241
if let Some ( var) = this. machine . env_vars . map . remove ( & name) {
228
- this. memory . deallocate ( var, None , MiriMemoryKind :: Machine . into ( ) ) ?;
242
+ this. memory . deallocate ( var, None , MiriMemoryKind :: Env . into ( ) ) ?;
229
243
this. update_environ ( ) ?;
230
244
}
231
245
Ok ( 1 ) // return non-zero on success
@@ -234,7 +248,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
234
248
let var_ptr = alloc_env_var_as_wide_str ( & name, & value, & mut this) ?;
235
249
if let Some ( var) = this. machine . env_vars . map . insert ( name, var_ptr) {
236
250
this. memory
237
- . deallocate ( var, None , MiriMemoryKind :: Machine . into ( ) ) ?;
251
+ . deallocate ( var, None , MiriMemoryKind :: Env . into ( ) ) ?;
238
252
}
239
253
this. update_environ ( ) ?;
240
254
Ok ( 1 ) // return non-zero on success
@@ -257,7 +271,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
257
271
if let Some ( old) = success {
258
272
if let Some ( var) = old {
259
273
this. memory
260
- . deallocate ( var, None , MiriMemoryKind :: Machine . into ( ) ) ?;
274
+ . deallocate ( var, None , MiriMemoryKind :: Env . into ( ) ) ?;
261
275
}
262
276
this. update_environ ( ) ?;
263
277
Ok ( 0 )
@@ -314,12 +328,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
314
328
/// The first time it gets called, also initializes `extra.environ`.
315
329
fn update_environ ( & mut self ) -> InterpResult < ' tcx > {
316
330
let this = self . eval_context_mut ( ) ;
317
- // Deallocate the old environ value , if any.
331
+ // Deallocate the old environ list , if any.
318
332
if let Some ( environ) = this. machine . env_vars . environ {
319
333
let old_vars_ptr = this. read_scalar ( environ. into ( ) ) ?. not_undef ( ) ?;
320
- this. memory . deallocate ( this. force_ptr ( old_vars_ptr) ?, None , MiriMemoryKind :: Machine . into ( ) ) ?;
334
+ this. memory . deallocate ( this. force_ptr ( old_vars_ptr) ?, None , MiriMemoryKind :: Env . into ( ) ) ?;
321
335
} else {
322
336
// No `environ` allocated yet, let's do that.
337
+ // This is memory backing an extern static, hence `Machine`, not `Env`.
323
338
let layout = this. layout_of ( this. tcx . types . usize ) ?;
324
339
let place = this. allocate ( layout, MiriMemoryKind :: Machine . into ( ) ) ;
325
340
this. write_scalar ( Scalar :: from_machine_usize ( 0 , & * this. tcx ) , place. into ( ) ) ?;
@@ -334,7 +349,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
334
349
let tcx = this. tcx ;
335
350
let vars_layout =
336
351
this. layout_of ( tcx. mk_array ( tcx. types . usize , u64:: try_from ( vars. len ( ) ) . unwrap ( ) ) ) ?;
337
- let vars_place = this. allocate ( vars_layout, MiriMemoryKind :: Machine . into ( ) ) ;
352
+ let vars_place = this. allocate ( vars_layout, MiriMemoryKind :: Env . into ( ) ) ;
338
353
for ( idx, var) in vars. into_iter ( ) . enumerate ( ) {
339
354
let place = this. mplace_field ( vars_place, idx) ?;
340
355
this. write_scalar ( var, place. into ( ) ) ?;
0 commit comments