Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
)
}

/// Helper function used inside the shims of foreign functions to assert that the target OS
/// is part of the UNIX family. It panics showing a message with the `name` of the foreign function
/// if this is not the case.
fn assert_target_os_is_unix(&self, name: &str) {
assert!(
target_os_is_unix(self.eval_context_ref().tcx.sess.target.os.as_ref()),
"`{}` is only available for supported UNIX family targets",
name,
);
}

/// Get last error variable as a place, lazily allocating thread-local storage for it if
/// necessary.
fn last_error_place(&mut self) -> InterpResult<'tcx, MPlaceTy<'tcx, Tag>> {
Expand Down Expand Up @@ -895,3 +906,9 @@ impl std::fmt::Display for HexRange {
write!(f, "[{:#x}..{:#x}]", self.0.start.bytes(), self.0.end().bytes())
}
}

/// Helper function used inside the shims of foreign functions to check that
/// `target_os` is a supported UNIX OS.
pub fn target_os_is_unix(target_os: &str) -> bool {
matches!(target_os, "linux" | "macos")
}
4 changes: 3 additions & 1 deletion src/shims/dlsym.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_middle::mir;
use rustc_target::spec::abi::Abi;

use crate::helpers::target_os_is_unix;
use crate::*;
use shims::unix::dlsym as unix;
use shims::windows::dlsym as windows;
Expand All @@ -18,7 +19,8 @@ impl Dlsym {
pub fn from_str<'tcx>(name: &[u8], target_os: &str) -> InterpResult<'tcx, Option<Dlsym>> {
let name = &*String::from_utf8_lossy(name);
Ok(match target_os {
"linux" | "macos" => unix::Dlsym::from_str(name, target_os)?.map(Dlsym::Posix),
target if target_os_is_unix(target) =>
unix::Dlsym::from_str(name, target)?.map(Dlsym::Posix),
"windows" => windows::Dlsym::from_str(name)?.map(Dlsym::Windows),
os => bug!("dlsym not implemented for target_os {}", os),
})
Expand Down
39 changes: 8 additions & 31 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_data_structures::fx::FxHashMap;
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.
Expand Down Expand Up @@ -55,7 +56,7 @@ impl<'tcx> EnvVars<'tcx> {
};
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 =>
Expand Down Expand Up @@ -113,11 +114,7 @@ impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mi
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)?;
Expand Down Expand Up @@ -211,11 +208,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
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)?;
Expand Down Expand Up @@ -285,11 +278,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

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;
Expand Down Expand Up @@ -319,11 +308,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
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)?;
Expand Down Expand Up @@ -378,11 +363,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, '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",
"`chdir` 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)?)?;

Expand Down Expand Up @@ -468,11 +449,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

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.assert_target_os_is_unix("getpid");

this.check_no_isolation("`getpid`")?;

Expand Down
4 changes: 2 additions & 2 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_target::{
};

use super::backtrace::EvalContextExt as _;
use crate::helpers::convert::Truncate;
use crate::helpers::{convert::Truncate, target_os_is_unix};
use crate::*;

/// Returned by `emulate_foreign_item_by_name`.
Expand Down Expand Up @@ -702,7 +702,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

// Platform-specific shims
_ => match this.tcx.sess.target.os.as_ref() {
"linux" | "macos" => return shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
target if target_os_is_unix(target) => return shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
"windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
target => throw_unsup_format!("the target `{}` is not supported", target),
}
Expand Down