Skip to content

Commit

Permalink
This adds a getpid shim
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ncMN committed Jun 8, 2022
1 parent eedc78d commit 2a6b1ec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,40 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, '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"
);

if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("getpid", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied)?;

return Ok(-1);
}

// 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 need to fit that u32 into an i32 and pray
Ok(std::process::id() as i32)
}

#[allow(non_snake_case)]
fn GetCurrentProcessId(&mut self) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();
this.assert_target_os("windows", "GetProcessId");

if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`GetProcessId`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied)?;
return Ok(0);
}

Ok(std::process::id() as i32)
}
}

5 changes: 5 additions & 0 deletions src/shims/unix/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_null(dest)?;
}

"getpid" => {
let result = this.getpid()?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}

// Platform-specific shims
_ => {
match this.tcx.sess.target.os.as_ref() {
Expand Down
4 changes: 4 additions & 0 deletions src/shims/windows/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
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 result = this.GetCurrentProcessId()?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}

_ => return Ok(EmulateByNameResult::NotSupported),
}
Expand Down

0 comments on commit 2a6b1ec

Please sign in to comment.