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.
};
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 =>
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)?;
value_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",
- "`setenv` is only available for the UNIX target family"
- );
+ this.assert_target_os_is_unix("setenv");
let name_ptr = this.read_pointer(name_op)?;
let value_ptr = this.read_pointer(value_op)?;
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;
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)?;
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)?)?;
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())
+ }
}