From 3e03054ef0056e728a081b03cde8546f91c822aa Mon Sep 17 00:00:00 2001 From: InfRandomness Date: Wed, 8 Jun 2022 10:09:38 +0200 Subject: [PATCH] Add getpid shim --- src/shims/env.rs | 27 +++++++++++++++++++++++++++ src/shims/unix/foreign_items.rs | 6 ++++++ src/shims/windows/foreign_items.rs | 5 +++++ tests/pass/getpid.rs | 9 +++++++++ 4 files changed, 47 insertions(+) create mode 100644 tests/pass/getpid.rs diff --git a/src/shims/env.rs b/src/shims/env.rs index 5cffc5c6d1d..a6591928611 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -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()) + } } diff --git a/src/shims/unix/foreign_items.rs b/src/shims/unix/foreign_items.rs index 32cf7e6f891..2d2a2c0399a 100644 --- a/src/shims/unix/foreign_items.rs +++ b/src/shims/unix/foreign_items.rs @@ -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() { diff --git a/src/shims/windows/foreign_items.rs b/src/shims/windows/foreign_items.rs index 05f9aed1747..8b7742e0a4a 100644 --- a/src/shims/windows/foreign_items.rs +++ b/src/shims/windows/foreign_items.rs @@ -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 index 00000000000..258fdeaa849 --- /dev/null +++ b/tests/pass/getpid.rs @@ -0,0 +1,9 @@ +// compile-flags: -Zmiri-disable-isolation + +fn getpid() -> u32 { + std::process::id() +} + +fn main() { + getpid(); +} -- 2.44.0