From 494cde2bbf222f4bce9e7fb89eda8e9c8d2559ec Mon Sep 17 00:00:00 2001 From: Lander Brandt Date: Wed, 9 Mar 2022 13:28:50 -0800 Subject: [PATCH] cleanup detection of unix OS --- src/helpers.rs | 13 ++++++++----- src/machine.rs | 8 ++++---- src/shims/dlsym.rs | 3 ++- src/shims/foreign_items.rs | 3 ++- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 0b37b78b6d..7386482339 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -438,14 +438,11 @@ 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 `target_os`. It panics showing a message with the `name` of the foreign function + /// 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!( - matches!( - self.eval_context_ref().tcx.sess.target.os.as_str(), - "linux" | "macos" | "android" - ), + target_os_is_unix(self.eval_context_ref().tcx.sess.target.os.as_str()), "`{}` is only available for supported UNIX family targets", name, ); @@ -788,3 +785,9 @@ pub fn simd_element_to_bool<'tcx>(elem: ImmTy<'tcx, Tag>) -> InterpResult<'tcx, _ => throw_ub_format!("each element of a SIMD mask must be all-0-bits or all-1-bits"), }) } + +/// Helper function used inside the shims of foreign functions to check that the `target_os` is +/// part of the UNIX family. +pub fn target_os_is_unix(target_os: &str) -> bool { + matches!(target_os, "linux" | "macos" | "android") +} diff --git a/src/machine.rs b/src/machine.rs index 5984aee984..5f878a5a66 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -263,12 +263,12 @@ impl MemoryExtra { symbol_name ) }); - let (provenance, offset) = - this.memory.create_fn_alloc(FnVal::Other(dlsym)).into_parts(); - let ptr = Pointer::new(Some(provenance), offset); + let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym)); + let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into())?; this.write_pointer(ptr, &place.into())?; - Self::add_extern_static(this, symbol_name, place.ptr); + + Self::add_extern_static(this, symbol_name, place.ptr.into()); }, _ => {} // No "extern statics" supported on this target } diff --git a/src/shims/dlsym.rs b/src/shims/dlsym.rs index 4befaf26aa..5d59450a08 100644 --- a/src/shims/dlsym.rs +++ b/src/shims/dlsym.rs @@ -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::posix::dlsym as posix; use shims::windows::dlsym as windows; @@ -18,7 +19,7 @@ impl Dlsym { pub fn from_str(name: &[u8], target_os: &str) -> InterpResult<'static, Option> { let name = &*String::from_utf8_lossy(name); Ok(match target_os { - "linux" | "macos" | "android" => + target if target_os_is_unix(target) => posix::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), diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 64ec9dcf8c..46fa795c1c 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -26,6 +26,7 @@ use rustc_target::{ }; use super::backtrace::EvalContextExt as _; +use crate::helpers::target_os_is_unix; use crate::*; /// Returned by `emulate_foreign_item_by_name`. @@ -687,7 +688,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // Platform-specific shims _ => match this.tcx.sess.target.os.as_str() { - "linux" | "macos" | "android" => return shims::posix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret), + target if target_os_is_unix(target) => return shims::posix::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), }