]> git.lizzy.rs Git - rust.git/commitdiff
move environ init to update_environ
authorRalf Jung <post@ralfj.de>
Sun, 8 Mar 2020 16:18:53 +0000 (17:18 +0100)
committerRalf Jung <post@ralfj.de>
Sun, 8 Mar 2020 16:21:02 +0000 (17:21 +0100)
src/shims/env.rs

index aaecbebc360320c92855c8961c33a748e0dd2eb0..b06db5676f3e9e5e7927d94c00c2cab9307d67c2 100644 (file)
@@ -30,11 +30,6 @@ pub(crate) fn init<'mir, 'tcx>(
                 }
             }
         }
-        // Initialize the `environ` static
-        let layout = ecx.layout_of(ecx.tcx.types.usize)?;
-        let place = ecx.allocate(layout, MiriMemoryKind::Machine.into());
-        ecx.write_scalar(Scalar::from_machine_usize(0, &*ecx.tcx), place.into())?;
-        ecx.memory.extra.environ = Some(place);
         ecx.update_environ()
     }
 }
@@ -160,17 +155,22 @@ fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
         }
     }
 
-    /// Updates the `environ` static. It should not be called before
-    /// `EnvVars::init`.
+    /// Updates the `environ` static.
+    /// The first time it gets called, also initializes `extra.environ`.
     fn update_environ(&mut self) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
-        // Deallocate the old environ value.
-        let old_vars_ptr = this.read_scalar(this.memory.extra.environ.unwrap().into())?.not_undef()?;
-        // The pointer itself can be null because `EnvVars::init` only
-        // initializes the place for the static but not the static itself.
-        if !this.is_null(old_vars_ptr)? {
+        // Deallocate the old environ value, if any.
+        if let Some(environ) = this.memory.extra.environ {
+            let old_vars_ptr = this.read_scalar(environ.into())?.not_undef()?;
             this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Machine.into())?;
+        } else {
+            // No `environ` allocated yet, let's do that.
+            let layout = this.layout_of(this.tcx.types.usize)?;
+            let place = this.allocate(layout, MiriMemoryKind::Machine.into());
+            this.write_scalar(Scalar::from_machine_usize(0, &*this.tcx), place.into())?;
+            this.memory.extra.environ = Some(place);
         }
+
         // Collect all the pointers to each variable in a vector.
         let mut vars: Vec<Scalar<Tag>> = this.machine.env_vars.map.values().map(|&ptr| ptr.into()).collect();
         // Add the trailing null pointer.