Skip to content

Commit 680176a

Browse files
authored
Merge pull request #4389 from RalfJung/native-lib-search-order
native_lib: skip to next .so if function was in dependency of the first
2 parents f5457e4 + 11581df commit 680176a

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/tools/miri/README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,22 @@ to Miri failing to detect cases of undefined behavior in a program.
396396
* `-Zmiri-force-intrinsic-fallback` forces the use of the "fallback" body for all intrinsics that
397397
have one. This is useful to test the fallback bodies, but should not be used otherwise. It is
398398
**unsound** since the fallback body might not be checking for all UB.
399-
* `-Zmiri-native-lib=<path to a shared object file>` is an experimental flag for providing support
400-
for calling native functions from inside the interpreter via FFI. The flag is supported only on
401-
Unix systems. Functions not provided by that file are still executed via the usual Miri shims. If
402-
a path to a directory is specified, all files in that directory are included nonrecursively. This
403-
flag can be passed multiple times to specify multiple files and/or directories.
399+
* `-Zmiri-native-lib=<path to a shared object file or folder>` is an experimental flag for providing
400+
support for calling native functions from inside the interpreter via FFI. The flag is supported
401+
only on Unix systems. Functions not provided by that file are still executed via the usual Miri
402+
shims. If a path to a directory is specified, all files in that directory are included
403+
non-recursively. This flag can be passed multiple times to specify multiple files and/or
404+
directories.
404405
**WARNING**: If an invalid/incorrect `.so` file is specified, this can cause Undefined Behavior in
405406
Miri itself! And of course, Miri often cannot do any checks on the actions taken by the native code.
406407
Note that Miri has its own handling of file descriptors, so if you want to replace *some*
407408
functions working on file descriptors, you will have to replace *all* of them, or the two kinds of
408-
file descriptors will be mixed up. This is **work in progress**; currently, only integer and
409-
pointers arguments and return values are supported and memory allocated by the native code cannot
410-
be accessed from Rust (only the other way around). Native code must not spawn threads that keep
411-
running in the background after the call has returned to Rust and that access Rust-allocated
412-
memory. Finally, the flag is **unsound** in the sense that Miri stops tracking details such as
409+
file descriptors will be mixed up.
410+
This is **work in progress**; currently, only integer and pointers arguments and return values are
411+
supported and memory allocated by the native code cannot be accessed from Rust (only the other way
412+
around). Native code must not spawn threads that keep running in the background after the call has
413+
returned to Rust and that access Rust-allocated memory.
414+
Finally, the flag is **unsound** in the sense that Miri stops tracking details such as
413415
initialization and provenance on memory shared with native code, so it is easily possible to write
414416
code that has UB which is missed by Miri.
415417
* `-Zmiri-measureme=<name>` enables `measureme` profiling for the interpreted program.

src/tools/miri/src/shims/native_lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,19 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
114114
// using the `libc` crate where this interface is public.
115115
let mut info = std::mem::MaybeUninit::<libc::Dl_info>::zeroed();
116116
unsafe {
117-
if libc::dladdr(fn_ptr, info.as_mut_ptr()) != 0 {
118-
let info = info.assume_init();
119-
#[cfg(target_os = "cygwin")]
120-
let fname_ptr = info.dli_fname.as_ptr();
121-
#[cfg(not(target_os = "cygwin"))]
122-
let fname_ptr = info.dli_fname;
123-
assert!(!fname_ptr.is_null());
124-
if std::ffi::CStr::from_ptr(fname_ptr).to_str().unwrap()
125-
!= lib_path.to_str().unwrap()
126-
{
127-
return None;
128-
}
117+
let res = libc::dladdr(fn_ptr, info.as_mut_ptr());
118+
assert!(res != 0, "failed to load info about function we already loaded");
119+
let info = info.assume_init();
120+
#[cfg(target_os = "cygwin")]
121+
let fname_ptr = info.dli_fname.as_ptr();
122+
#[cfg(not(target_os = "cygwin"))]
123+
let fname_ptr = info.dli_fname;
124+
assert!(!fname_ptr.is_null());
125+
if std::ffi::CStr::from_ptr(fname_ptr).to_str().unwrap()
126+
!= lib_path.to_str().unwrap()
127+
{
128+
// The function is not actually in this .so, check the next one.
129+
continue;
129130
}
130131
}
131132

0 commit comments

Comments
 (0)