Skip to content

Commit

Permalink
Auto merge of rust-lang#3593 - RalfJung:native-lib, r=RalfJung
Browse files Browse the repository at this point in the history
rename 'extern-so' to 'native-lib'

Calling "extern" functions is not super clear IMO (extern to what), but saying that we are calling "native" functions from inside the interpreter makes it very clear I think.
  • Loading branch information
bors committed May 10, 2024
2 parents d3f4d06 + 25a3b66 commit 6f4c7d9
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 333 deletions.
2 changes: 1 addition & 1 deletion src/tools/miri/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ tex/*/out
perf.data
perf.data.old
flamegraph.svg
tests/extern-so/libtestlib.so
tests/native-lib/libtestlib.so
.auto-*
12 changes: 6 additions & 6 deletions src/tools/miri/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,17 @@ to Miri failing to detect cases of undefined behavior in a program.
this flag is **unsound**.
* `-Zmiri-disable-weak-memory-emulation` disables the emulation of some C++11 weak
memory effects.
* `-Zmiri-extern-so-file=<path to a shared object file>` is an experimental flag for providing support
for FFI calls. Functions not provided by that file are still executed via the usual Miri shims.
**WARNING**: If an invalid/incorrect `.so` file is specified, this can cause undefined behaviour in Miri itself!
And of course, Miri cannot do any checks on the actions taken by the external code.
* `-Zmiri-native-lib=<path to a shared object file>` is an experimental flag for providing support
for calling native functions from inside the interpreter via FFI. Functions not provided by that
file are still executed via the usual Miri shims.
**WARNING**: If an invalid/incorrect `.so` file is specified, this can cause Undefined Behavior in Miri itself!
And of course, Miri cannot do any checks on the actions taken by the native code.
Note that Miri has its own handling of file descriptors, so if you want to replace *some* functions
working on file descriptors, you will have to replace *all* of them, or the two kinds of
file descriptors will be mixed up.
This is **work in progress**; currently, only integer arguments and return values are
supported (and no, pointer/integer casts to work around this limitation will not work;
they will fail horribly). It also only works on unix hosts for now.
Follow [the discussion on supporting other types](https://github.com/rust-lang/miri/issues/2365).
they will fail horribly). It also only works on Linux hosts for now.
* `-Zmiri-measureme=<name>` enables `measureme` profiling for the interpreted program.
This can be used to find which parts of your program are executing slowly under Miri.
The profile is written out to a file inside a directory called `<name>`, and can be processed
Expand Down
13 changes: 5 additions & 8 deletions src/tools/miri/src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,18 +575,15 @@ fn main() {
"full" => BacktraceStyle::Full,
_ => show_error!("-Zmiri-backtrace may only be 0, 1, or full"),
};
} else if let Some(param) = arg.strip_prefix("-Zmiri-extern-so-file=") {
} else if let Some(param) = arg.strip_prefix("-Zmiri-native-lib=") {
let filename = param.to_string();
if std::path::Path::new(&filename).exists() {
if let Some(other_filename) = miri_config.external_so_file {
show_error!(
"-Zmiri-extern-so-file is already set to {}",
other_filename.display()
);
if let Some(other_filename) = miri_config.native_lib {
show_error!("-Zmiri-native-lib is already set to {}", other_filename.display());
}
miri_config.external_so_file = Some(filename.into());
miri_config.native_lib = Some(filename.into());
} else {
show_error!("-Zmiri-extern-so-file `{}` does not exist", filename);
show_error!("-Zmiri-native-lib `{}` does not exist", filename);
}
} else if let Some(param) = arg.strip_prefix("-Zmiri-num-cpus=") {
let num_cpus = param
Expand Down
6 changes: 3 additions & 3 deletions src/tools/miri/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ pub struct MiriConfig {
/// Whether Stacked Borrows and Tree Borrows retagging should recurse into fields of datatypes.
pub retag_fields: RetagFields,
/// The location of a shared object file to load when calling external functions
/// FIXME! consider allowing users to specify paths to multiple SO files, or to a directory
pub external_so_file: Option<PathBuf>,
/// FIXME! consider allowing users to specify paths to multiple files, or to a directory
pub native_lib: Option<PathBuf>,
/// Run a garbage collector for BorTags every N basic blocks.
pub gc_interval: u32,
/// The number of CPUs to be reported by miri.
Expand Down Expand Up @@ -188,7 +188,7 @@ impl Default for MiriConfig {
preemption_rate: 0.01, // 1%
report_progress: None,
retag_fields: RetagFields::Yes,
external_so_file: None,
native_lib: None,
gc_interval: 10_000,
num_cpus: 1,
page_size: None,
Expand Down
12 changes: 6 additions & 6 deletions src/tools/miri/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,11 @@ pub struct MiriMachine<'mir, 'tcx> {
// The total number of blocks that have been executed.
pub(crate) basic_block_count: u64,

/// Handle of the optional shared object file for external functions.
/// Handle of the optional shared object file for native functions.
#[cfg(target_os = "linux")]
pub external_so_lib: Option<(libloading::Library, std::path::PathBuf)>,
pub native_lib: Option<(libloading::Library, std::path::PathBuf)>,
#[cfg(not(target_os = "linux"))]
pub external_so_lib: Option<!>,
pub native_lib: Option<!>,

/// Run a garbage collector for BorTags every N basic blocks.
pub(crate) gc_interval: u32,
Expand Down Expand Up @@ -665,7 +665,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
basic_block_count: 0,
clock: Clock::new(config.isolated_op == IsolatedOp::Allow),
#[cfg(target_os = "linux")]
external_so_lib: config.external_so_file.as_ref().map(|lib_file_path| {
native_lib: config.native_lib.as_ref().map(|lib_file_path| {
let target_triple = layout_cx.tcx.sess.opts.target_triple.triple();
// Check if host target == the session target.
if env!("TARGET") != target_triple {
Expand All @@ -687,7 +687,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
)
}),
#[cfg(not(target_os = "linux"))]
external_so_lib: config.external_so_file.as_ref().map(|_| {
native_lib: config.native_lib.as_ref().map(|_| {
panic!("loading external .so files is only supported on Linux")
}),
gc_interval: config.gc_interval,
Expand Down Expand Up @@ -802,7 +802,7 @@ impl VisitProvenance for MiriMachine<'_, '_> {
preemption_rate: _,
report_progress: _,
basic_block_count: _,
external_so_lib: _,
native_lib: _,
gc_interval: _,
since_gc: _,
num_cpus: _,
Expand Down
Loading

0 comments on commit 6f4c7d9

Please sign in to comment.