Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading a build.rs compiled .so with -Zmiri-native-lib #4077

Open
Nemo157 opened this issue Dec 6, 2024 · 4 comments
Open

Support loading a build.rs compiled .so with -Zmiri-native-lib #4077

Nemo157 opened this issue Dec 6, 2024 · 4 comments
Labels
A-native Area: calling native functions via FFI C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement

Comments

@Nemo157
Copy link
Member

Nemo157 commented Dec 6, 2024

It's common for projects to build C dependencies in the build-scripts of -sys dependencies. To be able to use these along with miri's new FFI support requires some weird contortions:

> cargo miri test
...
error: unsupported operation: can't call foreign function ...
> MIRIFLAGS=-Zmiri-native-lib=target/shared/miri/x86_64-unknown-linux-gnu/debug/build/foobar-sys-fb396848c056a26f/out/libfoobar.so cargo miri test

(along with making the build script detect CARGO_CFG_MIRI and switch to outputting a shared object instead of the normal staticlib).

It'd be nice if there were some way to inform miri about the native lib from the build script, similar to how we can tell the linker about the native libraries (maybe there's some way miri could reuse these cargo commands?).

cargo:rustc-link-lib=static=foobar
cargo:rustc-link-search=native=$OUT_DIR
@RalfJung
Copy link
Member

RalfJung commented Dec 6, 2024

Yeah, so far we haven't thought at all about automation or integration here, just about the basic infrastructure. These are some good starting points, thanks!

I do think we still want to require some explicit opt-in in MIRIFLAGS though. Miri has to behave a bit differently in native-libs mode, there is the risk of missing UB, the C code could mess up Miri's internal state entirely -- this is not something we should just do silently.

@Nemo157
Copy link
Member Author

Nemo157 commented Dec 6, 2024

Maybe it could be supported by parsing the rustc-link-search flags, but not the link-lib ones, so the end-user still needs to list the libraries to load, but doesn't have to find the correct out-paths

MIRIFLAGS=-Zmiri-native-lib=libfoobar.so cargo miri test

I don't actually know, does miri have any kind of search path for the libraries right now?

@RalfJung
Copy link
Member

RalfJung commented Dec 6, 2024

We just call libloading::Library::new(lib_file_path). I have no idea if that has a search path.

@bjorn3
Copy link
Member

bjorn3 commented Dec 6, 2024

I have no idea if that has a search path.

That follows the runtime dylib search path (LD_LIBRARY_PATH on most Unixes, DYLD_LIBRARY_PATH on macOS and PATH on Windows) At least on the systems I tried, the dynamic linker will respect those env vars even if you set them at runtime right before loading a dynamic library rather than before starting the program.

@RalfJung RalfJung added C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement A-native Area: calling native functions via FFI labels Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-native Area: calling native functions via FFI C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement
Projects
None yet
Development

No branches or pull requests

3 participants