Skip to content

Commit 494cde2

Browse files
author
Lander Brandt
committed
cleanup detection of unix OS
1 parent 71c246f commit 494cde2

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

src/helpers.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
438438
}
439439

440440
/// Helper function used inside the shims of foreign functions to assert that the target OS
441-
/// is `target_os`. It panics showing a message with the `name` of the foreign function
441+
/// is part of the UNIX family. It panics showing a message with the `name` of the foreign function
442442
/// if this is not the case.
443443
fn assert_target_os_is_unix(&self, name: &str) {
444444
assert!(
445-
matches!(
446-
self.eval_context_ref().tcx.sess.target.os.as_str(),
447-
"linux" | "macos" | "android"
448-
),
445+
target_os_is_unix(self.eval_context_ref().tcx.sess.target.os.as_str()),
449446
"`{}` is only available for supported UNIX family targets",
450447
name,
451448
);
@@ -788,3 +785,9 @@ pub fn simd_element_to_bool<'tcx>(elem: ImmTy<'tcx, Tag>) -> InterpResult<'tcx,
788785
_ => throw_ub_format!("each element of a SIMD mask must be all-0-bits or all-1-bits"),
789786
})
790787
}
788+
789+
/// Helper function used inside the shims of foreign functions to check that the `target_os` is
790+
/// part of the UNIX family.
791+
pub fn target_os_is_unix(target_os: &str) -> bool {
792+
matches!(target_os, "linux" | "macos" | "android")
793+
}

src/machine.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ impl MemoryExtra {
263263
symbol_name
264264
)
265265
});
266-
let (provenance, offset) =
267-
this.memory.create_fn_alloc(FnVal::Other(dlsym)).into_parts();
268-
let ptr = Pointer::new(Some(provenance), offset);
266+
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
267+
269268
let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into())?;
270269
this.write_pointer(ptr, &place.into())?;
271-
Self::add_extern_static(this, symbol_name, place.ptr);
270+
271+
Self::add_extern_static(this, symbol_name, place.ptr.into());
272272
},
273273
_ => {} // No "extern statics" supported on this target
274274
}

src/shims/dlsym.rs

+2-1
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::posix::dlsym as posix;
67
use shims::windows::dlsym as windows;
@@ -18,7 +19,7 @@ impl Dlsym {
1819
pub fn from_str(name: &[u8], target_os: &str) -> InterpResult<'static, Option<Dlsym>> {
1920
let name = &*String::from_utf8_lossy(name);
2021
Ok(match target_os {
21-
"linux" | "macos" | "android" =>
22+
target if target_os_is_unix(target) =>
2223
posix::Dlsym::from_str(name, target_os)?.map(Dlsym::Posix),
2324
"windows" => windows::Dlsym::from_str(name)?.map(Dlsym::Windows),
2425
os => bug!("dlsym not implemented for target_os {}", os),

src/shims/foreign_items.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc_target::{
2626
};
2727

2828
use super::backtrace::EvalContextExt as _;
29+
use crate::helpers::target_os_is_unix;
2930
use crate::*;
3031

3132
/// Returned by `emulate_foreign_item_by_name`.
@@ -687,7 +688,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
687688

688689
// Platform-specific shims
689690
_ => match this.tcx.sess.target.os.as_str() {
690-
"linux" | "macos" | "android" => return shims::posix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
691+
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),
691692
"windows" => return shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(this, link_name, abi, args, dest, ret),
692693
target => throw_unsup_format!("the target `{}` is not supported", target),
693694
}

0 commit comments

Comments
 (0)