Add target_os_is_unix helper

This commit is contained in:
infrandomness 2022-06-11 15:37:49 +02:00
parent ada7b72a87
commit 58d00aa642
4 changed files with 13 additions and 4 deletions

View File

@ -895,3 +895,9 @@ impl std::fmt::Display for HexRange {
write!(f, "[{:#x}..{:#x}]", self.0.start.bytes(), self.0.end().bytes()) 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")
}

View File

@ -1,6 +1,7 @@
use rustc_middle::mir; use rustc_middle::mir;
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use crate::helpers::target_os_is_unix;
use crate::*; use crate::*;
use shims::unix::dlsym as unix; use shims::unix::dlsym as unix;
use shims::windows::dlsym as windows; use shims::windows::dlsym as windows;
@ -18,7 +19,8 @@ impl Dlsym {
pub fn from_str<'tcx>(name: &[u8], target_os: &str) -> InterpResult<'tcx, Option<Dlsym>> { pub fn from_str<'tcx>(name: &[u8], target_os: &str) -> InterpResult<'tcx, Option<Dlsym>> {
let name = &*String::from_utf8_lossy(name); let name = &*String::from_utf8_lossy(name);
Ok(match target_os { 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), "windows" => windows::Dlsym::from_str(name)?.map(Dlsym::Windows),
os => bug!("dlsym not implemented for target_os {}", os), os => bug!("dlsym not implemented for target_os {}", os),
}) })

View File

@ -8,6 +8,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::layout::LayoutOf;
use rustc_target::abi::Size; use rustc_target::abi::Size;
use crate::helpers::target_os_is_unix;
use crate::*; use crate::*;
/// Check whether an operation that writes to a target buffer was successful. /// Check whether an operation that writes to a target buffer was successful.
@ -55,7 +56,7 @@ impl<'tcx> EnvVars<'tcx> {
}; };
if forward { if forward {
let var_ptr = match target_os { 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)?, 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)?, "windows" => alloc_env_var_as_wide_str(name.as_ref(), value.as_ref(), ecx)?,
unsupported => unsupported =>

View File

@ -22,7 +22,7 @@ use rustc_target::{
}; };
use super::backtrace::EvalContextExt as _; use super::backtrace::EvalContextExt as _;
use crate::helpers::convert::Truncate; use crate::helpers::{convert::Truncate, target_os_is_unix};
use crate::*; use crate::*;
/// Returned by `emulate_foreign_item_by_name`. /// Returned by `emulate_foreign_item_by_name`.
@ -702,7 +702,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// Platform-specific shims // Platform-specific shims
_ => match this.tcx.sess.target.os.as_ref() { _ => 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), "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), target => throw_unsup_format!("the target `{}` is not supported", target),
} }