Skip to content

Commit

Permalink
cleanup detection of unix OS
Browse files Browse the repository at this point in the history
  • Loading branch information
Lander Brandt committed Mar 9, 2022
1 parent 71c246f commit 494cde2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -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")
}
8 changes: 4 additions & 4 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 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::posix::dlsym as posix;
use shims::windows::dlsym as windows;
Expand All @@ -18,7 +19,7 @@ impl Dlsym {
pub fn from_str(name: &[u8], target_os: &str) -> InterpResult<'static, Option<Dlsym>> {
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),
Expand Down
3 changes: 2 additions & 1 deletion src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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),
}
Expand Down

0 comments on commit 494cde2

Please sign in to comment.