Skip to content

Commit 989f63b

Browse files
committed
Auto merge of #3856 - jder:mac-native-libs, r=RalfJung
Enable native libraries on macOS Fixes #3595 by using `-fvisibility=hidden` and the visibility attribute supported by both gcc and clang rather than the previous gcc-only mechanism for symbol hiding. Also brings over cfg changes from #3594 which enable native-lib functionality on all unixes. Thanks for taking a look, feedback very welcome! cc `@RalfJung`
2 parents d0f23b4 + de96082 commit 989f63b

File tree

14 files changed

+78
-52
lines changed

14 files changed

+78
-52
lines changed

src/tools/miri/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ features = ['unprefixed_malloc_on_supported_platforms']
3737

3838
[target.'cfg(unix)'.dependencies]
3939
libc = "0.2"
40-
41-
[target.'cfg(target_os = "linux")'.dependencies]
4240
libffi = "3.2.0"
4341
libloading = "0.8"
4442

src/tools/miri/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ to Miri failing to detect cases of undefined behavior in a program.
383383
file descriptors will be mixed up.
384384
This is **work in progress**; currently, only integer arguments and return values are
385385
supported (and no, pointer/integer casts to work around this limitation will not work;
386-
they will fail horribly). It also only works on Linux hosts for now.
386+
they will fail horribly). It also only works on Unix hosts for now.
387387
* `-Zmiri-measureme=<name>` enables `measureme` profiling for the interpreted program.
388388
This can be used to find which parts of your program are executing slowly under Miri.
389389
The profile is written out to a file inside a directory called `<name>`, and can be processed

src/tools/miri/src/machine.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,9 @@ pub struct MiriMachine<'tcx> {
535535
pub(crate) basic_block_count: u64,
536536

537537
/// Handle of the optional shared object file for native functions.
538-
#[cfg(target_os = "linux")]
538+
#[cfg(unix)]
539539
pub native_lib: Option<(libloading::Library, std::path::PathBuf)>,
540-
#[cfg(not(target_os = "linux"))]
540+
#[cfg(not(unix))]
541541
pub native_lib: Option<!>,
542542

543543
/// Run a garbage collector for BorTags every N basic blocks.
@@ -678,7 +678,7 @@ impl<'tcx> MiriMachine<'tcx> {
678678
report_progress: config.report_progress,
679679
basic_block_count: 0,
680680
clock: Clock::new(config.isolated_op == IsolatedOp::Allow),
681-
#[cfg(target_os = "linux")]
681+
#[cfg(unix)]
682682
native_lib: config.native_lib.as_ref().map(|lib_file_path| {
683683
let target_triple = layout_cx.tcx.sess.opts.target_triple.triple();
684684
// Check if host target == the session target.
@@ -700,9 +700,9 @@ impl<'tcx> MiriMachine<'tcx> {
700700
lib_file_path.clone(),
701701
)
702702
}),
703-
#[cfg(not(target_os = "linux"))]
703+
#[cfg(not(unix))]
704704
native_lib: config.native_lib.as_ref().map(|_| {
705-
panic!("loading external .so files is only supported on Linux")
705+
panic!("calling functions from native libraries via FFI is only supported on Unix")
706706
}),
707707
gc_interval: config.gc_interval,
708708
since_gc: 0,

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
226226
let this = self.eval_context_mut();
227227

228228
// First deal with any external C functions in linked .so file.
229-
#[cfg(target_os = "linux")]
229+
#[cfg(unix)]
230230
if this.machine.native_lib.as_ref().is_some() {
231231
use crate::shims::native_lib::EvalContextExt as _;
232232
// An Ok(false) here means that the function being called was not exported

src/tools/miri/src/shims/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
mod alloc;
44
mod backtrace;
5-
#[cfg(target_os = "linux")]
5+
#[cfg(unix)]
66
mod native_lib;
77
mod unix;
88
mod wasi;

src/tools/miri/tests/native-lib/fail/function_not_in_so.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//@only-target-linux
1+
// Only works on Unix targets
2+
//@ignore-target-windows
3+
//@ignore-target-wasm
24
//@only-on-host
35
//@normalize-stderr-test: "OS `.*`" -> "$$OS"
46

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Only works on Unix targets
2+
//@ignore-target-windows
3+
//@ignore-target-wasm
4+
//@only-on-host
5+
//@normalize-stderr-test: "OS `.*`" -> "$$OS"
6+
7+
extern "C" {
8+
fn not_exported();
9+
}
10+
11+
fn main() {
12+
unsafe {
13+
not_exported(); //~ ERROR: unsupported operation: can't call foreign function `not_exported`
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: unsupported operation: can't call foreign function `not_exported` on $OS
2+
--> $DIR/private_function.rs:LL:CC
3+
|
4+
LL | not_exported();
5+
| ^^^^^^^^^^^^^^ can't call foreign function `not_exported` on $OS
6+
|
7+
= help: if this is a basic API commonly used on this target, please report an issue with Miri
8+
= help: however, note that Miri does not aim to support every FFI function out there; for instance, we will not support APIs for things such as GUIs, scripting languages, or databases
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/private_function.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

src/tools/miri/tests/native-lib/native-lib.map

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/tools/miri/tests/native-lib/pass/ptr_read_access.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//@only-target-linux
1+
// Only works on Unix targets
2+
//@ignore-target-windows
3+
//@ignore-target-wasm
24
//@only-on-host
35

46
fn main() {

0 commit comments

Comments
 (0)