Skip to content

Commit c5f7a7d

Browse files
committed
Auto merge of rust-lang#2215 - InfRandomness:getpid_shim, r=RalfJung
Getpid shim
2 parents 7656fc4 + 3e03054 commit c5f7a7d

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

src/shims/env.rs

+27
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,31 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
465465

466466
Ok(())
467467
}
468+
469+
fn getpid(&mut self) -> InterpResult<'tcx, i32> {
470+
let this = self.eval_context_mut();
471+
let target_os = &this.tcx.sess.target.os;
472+
assert!(
473+
target_os == "linux" || target_os == "macos",
474+
"`getpid` is only available for the UNIX target family"
475+
);
476+
477+
this.check_no_isolation("`getpid`")?;
478+
479+
// The reason we need to do this wacky of a conversion is because
480+
// `libc::getpid` returns an i32, however, `std::process::id()` return an u32.
481+
// So we un-do the conversion that stdlib does and turn it back into an i32.
482+
483+
Ok(std::process::id() as i32)
484+
}
485+
486+
#[allow(non_snake_case)]
487+
fn GetCurrentProcessId(&mut self) -> InterpResult<'tcx, u32> {
488+
let this = self.eval_context_mut();
489+
this.assert_target_os("windows", "GetCurrentProcessId");
490+
491+
this.check_no_isolation("`GetCurrentProcessId`")?;
492+
493+
Ok(std::process::id())
494+
}
468495
}

src/shims/unix/foreign_items.rs

+6
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
474474
this.write_null(dest)?;
475475
}
476476

477+
"getpid" => {
478+
let [] = this.check_shim(abi, Abi::C { unwind: false}, link_name, args)?;
479+
let result = this.getpid()?;
480+
this.write_scalar(Scalar::from_i32(result), dest)?;
481+
}
482+
477483
// Platform-specific shims
478484
_ => {
479485
match this.tcx.sess.target.os.as_ref() {

src/shims/windows/foreign_items.rs

+5
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
419419
let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
420420
this.write_scalar(Scalar::from_machine_isize(1, this), dest)?;
421421
}
422+
"GetCurrentProcessId" if this.frame_in_std() => {
423+
let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
424+
let result = this.GetCurrentProcessId()?;
425+
this.write_scalar(Scalar::from_u32(result), dest)?;
426+
}
422427

423428
_ => return Ok(EmulateByNameResult::NotSupported),
424429
}

tests/pass/getpid.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// compile-flags: -Zmiri-disable-isolation
2+
3+
fn getpid() -> u32 {
4+
std::process::id()
5+
}
6+
7+
fn main() {
8+
getpid();
9+
}

0 commit comments

Comments
 (0)