Skip to content

Commit 58d00aa

Browse files
committed
Add target_os_is_unix helper
1 parent ada7b72 commit 58d00aa

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

src/helpers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,3 +895,9 @@ impl std::fmt::Display for HexRange {
895895
write!(f, "[{:#x}..{:#x}]", self.0.start.bytes(), self.0.end().bytes())
896896
}
897897
}
898+
899+
/// Helper function used inside the shims of foreign functions to check that
900+
/// `target_os` is a supported UNIX OS.
901+
pub fn target_os_is_unix(target_os: &str) -> bool {
902+
matches!(target_os, "linux" | "macos")
903+
}

src/shims/dlsym.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_middle::mir;
22
use rustc_target::spec::abi::Abi;
33

4+
use crate::helpers::target_os_is_unix;
45
use crate::*;
56
use shims::unix::dlsym as unix;
67
use shims::windows::dlsym as windows;
@@ -18,7 +19,8 @@ impl Dlsym {
1819
pub fn from_str<'tcx>(name: &[u8], target_os: &str) -> InterpResult<'tcx, Option<Dlsym>> {
1920
let name = &*String::from_utf8_lossy(name);
2021
Ok(match target_os {
21-
"linux" | "macos" => unix::Dlsym::from_str(name, target_os)?.map(Dlsym::Posix),
22+
target if target_os_is_unix(target) =>
23+
unix::Dlsym::from_str(name, target)?.map(Dlsym::Posix),
2224
"windows" => windows::Dlsym::from_str(name)?.map(Dlsym::Windows),
2325
os => bug!("dlsym not implemented for target_os {}", os),
2426
})

src/shims/env.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_data_structures::fx::FxHashMap;
88
use rustc_middle::ty::layout::LayoutOf;
99
use rustc_target::abi::Size;
1010

11+
use crate::helpers::target_os_is_unix;
1112
use crate::*;
1213

1314
/// Check whether an operation that writes to a target buffer was successful.
@@ -55,7 +56,7 @@ impl<'tcx> EnvVars<'tcx> {
5556
};
5657
if forward {
5758
let var_ptr = match target_os {
58-
"linux" | "macos" =>
59+
target if target_os_is_unix(target) =>
5960
alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx)?,
6061
"windows" => alloc_env_var_as_wide_str(name.as_ref(), value.as_ref(), ecx)?,
6162
unsupported =>

src/shims/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_target::{
2222
};
2323

2424
use super::backtrace::EvalContextExt as _;
25-
use crate::helpers::convert::Truncate;
25+
use crate::helpers::{convert::Truncate, target_os_is_unix};
2626
use crate::*;
2727

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

703703
// Platform-specific shims
704704
_ => match this.tcx.sess.target.os.as_ref() {
705-
"linux" | "macos" => return shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
705+
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),
706706
"windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
707707
target => throw_unsup_format!("the target `{}` is not supported", target),
708708
}

0 commit comments

Comments
 (0)