Skip to content

Commit

Permalink
Add target_os_is_unix helper
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ncMN committed Jun 11, 2022
1 parent 31da2d9 commit 3670c1a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,3 +895,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_os)?.map(Dlsym::Posix),
"windows" => windows::Dlsym::from_str(name)?.map(Dlsym::Windows),
os => bug!("dlsym not implemented for target_os {}", os),
})
Expand Down
3 changes: 2 additions & 1 deletion 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
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

0 comments on commit 3670c1a

Please sign in to comment.