]> git.lizzy.rs Git - rust.git/commitdiff
Add getpid shim
authorInfRandomness <infrandomness@gmail.com>
Wed, 8 Jun 2022 08:09:38 +0000 (10:09 +0200)
committerinfrandomness <infrandomness@gmail.com>
Sat, 11 Jun 2022 09:28:50 +0000 (11:28 +0200)
src/shims/env.rs
src/shims/unix/foreign_items.rs
src/shims/windows/foreign_items.rs
tests/pass/getpid.rs [new file with mode: 0644]

index 5cffc5c6d1d5b937ca7765b85e4b2a267f138422..a65919286117c1051360c2c92d06459b6eed565f 100644 (file)
@@ -465,4 +465,31 @@ fn update_environ(&mut self) -> InterpResult<'tcx> {
 
         Ok(())
     }
+
+    fn getpid(&mut self) -> InterpResult<'tcx, i32> {
+        let this = self.eval_context_mut();
+        let target_os = &this.tcx.sess.target.os;
+        assert!(
+            target_os == "linux" || target_os == "macos",
+            "`getpid` is only available for the UNIX target family"
+        );
+
+        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())
+    }
 }
index 32cf7e6f891ff83e7d0d6430a7e7278b32aca0e5..2d2a2c0399a8b8a93c849ab23f9df52bdc392f9a 100644 (file)
@@ -474,6 +474,12 @@ fn emulate_foreign_item_by_name(
                 this.write_null(dest)?;
             }
 
+            "getpid" => {
+                let [] = this.check_shim(abi, Abi::C { unwind: false}, link_name, args)?;
+                let result = this.getpid()?;
+                this.write_scalar(Scalar::from_i32(result), dest)?;
+            }
+
             // Platform-specific shims
             _ => {
                 match this.tcx.sess.target.os.as_ref() {
index 05f9aed1747651bf9b50b26532a90320db2a4fa6..8b7742e0a4ae32d4cc64e60c91bd7f2912ed7d0a 100644 (file)
@@ -419,6 +419,11 @@ fn emulate_foreign_item_by_name(
                 let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
                 this.write_scalar(Scalar::from_machine_isize(1, this), dest)?;
             }
+            "GetCurrentProcessId" if this.frame_in_std() => {
+                let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+                let result = this.GetCurrentProcessId()?;
+                this.write_scalar(Scalar::from_u32(result), dest)?;
+            }
 
             _ => return Ok(EmulateByNameResult::NotSupported),
         }
diff --git a/tests/pass/getpid.rs b/tests/pass/getpid.rs
new file mode 100644 (file)
index 0000000..258fdea
--- /dev/null
@@ -0,0 +1,9 @@
+// compile-flags: -Zmiri-disable-isolation
+
+fn getpid() -> u32 {
+    std::process::id()
+}
+
+fn main() {
+    getpid();
+}