]> git.lizzy.rs Git - rust.git/blobdiff - src/shims/env.rs
Add `assert_target_os_is_unix` function
[rust.git] / src / shims / env.rs
index 7be26de45916a07cf4fd5bd3cc42e34d25014861..85ecd2b719f207300f271681fae2407c13f84282 100644 (file)
@@ -1,4 +1,3 @@
-use std::convert::TryFrom;
 use std::env;
 use std::ffi::{OsStr, OsString};
 use std::io::ErrorKind;
@@ -9,6 +8,7 @@
 use rustc_middle::ty::layout::LayoutOf;
 use rustc_target::abi::Size;
 
+use crate::helpers::target_os_is_unix;
 use crate::*;
 
 /// Check whether an operation that writes to a target buffer was successful.
@@ -51,12 +51,12 @@ pub(crate) fn init<'mir>(
         if ecx.machine.communicate() || !config.forwarded_env_vars.is_empty() {
             for (name, value) in env::vars_os() {
                 let forward = match ecx.machine.communicate() {
-                    true => !excluded_env_vars.iter().any(|v| v.as_str() == &name),
-                    false => config.forwarded_env_vars.iter().any(|v| v.as_str() == &name),
+                    true => !excluded_env_vars.iter().any(|v| **v == name),
+                    false => config.forwarded_env_vars.iter().any(|v| **v == name),
                 };
                 if forward {
                     let var_ptr = match target_os {
-                        "linux" | "macos" =>
+                        target if target_os_is_unix(target) =>
                             alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx)?,
                         "windows" => alloc_env_var_as_wide_str(name.as_ref(), value.as_ref(), ecx)?,
                         unsupported =>
@@ -65,7 +65,7 @@ pub(crate) fn init<'mir>(
                                 unsupported
                             ),
                     };
-                    ecx.machine.env_vars.map.insert(OsString::from(name), var_ptr);
+                    ecx.machine.env_vars.map.insert(name, var_ptr);
                 }
             }
         }
@@ -114,11 +114,7 @@ impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mi
 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
     fn getenv(&mut self, name_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
         let this = self.eval_context_mut();
-        let target_os = &this.tcx.sess.target.os;
-        assert!(
-            target_os == "linux" || target_os == "macos",
-            "`getenv` is only available for the UNIX target family"
-        );
+        this.assert_target_os_is_unix("getenv");
 
         let name_ptr = this.read_pointer(name_op)?;
         let name = this.read_os_str_from_c_str(name_ptr)?;
@@ -211,12 +207,8 @@ fn setenv(
         name_op: &OpTy<'tcx, Tag>,
         value_op: &OpTy<'tcx, Tag>,
     ) -> InterpResult<'tcx, i32> {
-        let mut this = self.eval_context_mut();
-        let target_os = &this.tcx.sess.target.os;
-        assert!(
-            target_os == "linux" || target_os == "macos",
-            "`setenv` is only available for the UNIX target family"
-        );
+        let this = self.eval_context_mut();
+        this.assert_target_os_is_unix("setenv");
 
         let name_ptr = this.read_pointer(name_op)?;
         let value_ptr = this.read_pointer(value_op)?;
@@ -230,7 +222,7 @@ fn setenv(
             }
         }
         if let Some((name, value)) = new {
-            let var_ptr = alloc_env_var_as_c_str(&name, &value, &mut this)?;
+            let var_ptr = alloc_env_var_as_c_str(&name, &value, this)?;
             if let Some(var) = this.machine.env_vars.map.insert(name, var_ptr) {
                 this.deallocate_ptr(var, None, MiriMemoryKind::Runtime.into())?;
             }
@@ -250,7 +242,7 @@ fn SetEnvironmentVariableW(
         name_op: &OpTy<'tcx, Tag>,  // LPCWSTR
         value_op: &OpTy<'tcx, Tag>, // LPCWSTR
     ) -> InterpResult<'tcx, i32> {
-        let mut this = self.eval_context_mut();
+        let this = self.eval_context_mut();
         this.assert_target_os("windows", "SetEnvironmentVariableW");
 
         let name_ptr = this.read_pointer(name_op)?;
@@ -275,7 +267,7 @@ fn SetEnvironmentVariableW(
             Ok(1) // return non-zero on success
         } else {
             let value = this.read_os_str_from_wide_str(value_ptr)?;
-            let var_ptr = alloc_env_var_as_wide_str(&name, &value, &mut this)?;
+            let var_ptr = alloc_env_var_as_wide_str(&name, &value, this)?;
             if let Some(var) = this.machine.env_vars.map.insert(name, var_ptr) {
                 this.deallocate_ptr(var, None, MiriMemoryKind::Runtime.into())?;
             }
@@ -286,11 +278,7 @@ fn SetEnvironmentVariableW(
 
     fn unsetenv(&mut self, name_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
         let this = self.eval_context_mut();
-        let target_os = &this.tcx.sess.target.os;
-        assert!(
-            target_os == "linux" || target_os == "macos",
-            "`unsetenv` is only available for the UNIX target family"
-        );
+        this.assert_target_os_is_unix("unsetenv");
 
         let name_ptr = this.read_pointer(name_op)?;
         let mut success = None;
@@ -320,14 +308,10 @@ fn getcwd(
         size_op: &OpTy<'tcx, Tag>,
     ) -> InterpResult<'tcx, Pointer<Option<Tag>>> {
         let this = self.eval_context_mut();
-        let target_os = &this.tcx.sess.target.os;
-        assert!(
-            target_os == "linux" || target_os == "macos",
-            "`getcwd` is only available for the UNIX target family"
-        );
+        this.assert_target_os_is_unix("getcwd");
 
-        let buf = this.read_pointer(&buf_op)?;
-        let size = this.read_scalar(&size_op)?.to_machine_usize(&*this.tcx)?;
+        let buf = this.read_pointer(buf_op)?;
+        let size = this.read_scalar(size_op)?.to_machine_usize(&*this.tcx)?;
 
         if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
             this.reject_in_isolation("`getcwd`", reject_with)?;
@@ -379,11 +363,7 @@ fn GetCurrentDirectoryW(
 
     fn chdir(&mut self, path_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
         let this = self.eval_context_mut();
-        let target_os = &this.tcx.sess.target.os;
-        assert!(
-            target_os == "linux" || target_os == "macos",
-            "`getcwd` is only available for the UNIX target family"
-        );
+        this.assert_target_os_is_unix("chdir");
 
         let path = this.read_path_from_c_str(this.read_pointer(path_op)?)?;
 
@@ -466,4 +446,27 @@ fn update_environ(&mut self) -> InterpResult<'tcx> {
 
         Ok(())
     }
+
+    fn getpid(&mut self) -> InterpResult<'tcx, i32> {
+        let this = self.eval_context_mut();
+        this.assert_target_os_is_unix("getpid");
+
+        this.check_no_isolation("`getpid`")?;
+
+        // The reason we need to do this wacky of a conversion is because
+        // `libc::getpid` returns an i32, however, `std::process::id()` return an u32.
+        // So we un-do the conversion that stdlib does and turn it back into an i32.
+
+        Ok(std::process::id() as i32)
+    }
+
+    #[allow(non_snake_case)]
+    fn GetCurrentProcessId(&mut self) -> InterpResult<'tcx, u32> {
+        let this = self.eval_context_mut();
+        this.assert_target_os("windows", "GetCurrentProcessId");
+
+        this.check_no_isolation("`GetCurrentProcessId`")?;
+
+        Ok(std::process::id())
+    }
 }