Cannot pass LD_PRELOAD with cargo

My project need to use LD_PRELOAD to change some native functions. But it seems LD_PRELOAD cannot work with both cargo run and cargo test. LD_RPELOAD only works on cargo, but does not work on the program/tests that cargo will execute.

Even export LD_PRELOAD=./mylib.so cannot work.

An example:

$ ./target/release/ld_preload_rs 
Hello, world!

$ LD_PRELOAD=./mylib.so ./target/release/ld_preload_rs 
Library is Loaded!
Hello, world!

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/ld_preload_rs`
Hello, world!

$ LD_PRELOAD=./mylib.so cargo run
Library is Loaded!   
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/ld_preload_rs`
Hello, world!

$ export LD_PRELOAD=./mylib.so
$ cargo run
Library is Loaded!   
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/ld_preload_rs`
Hello, world!
1 Like

Looks like it loaded?

No, it is just loaded for cargo, not for the program I'm running.
It should be loaded twice when using cargo, like:

$ cargo run
Library is Loaded!   
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/ld_preload_rs`
Library is Loaded!
Hello, world!

Yes, I see.

I can't reproduce with

fn main() {
        for (key, value) in std::env::vars_os() {
            println!("{:?}: {:?}", key.to_str(), value.to_str());
        }
}
$ LD_PRELOAD="xyzzy" cargo run | fgrep LD_
ERROR: ld.so: object 'xyzzy' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR: ld.so: object 'xyzzy' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/adf`
ERROR: ld.so: object 'xyzzy' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
Some("LD_LIBRARY_PATH"): Some("/home/rust/adf/target/debug/deps:/home/rust/adf/target/debug:/home/rust/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib:/home/rust/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib")
Some("LD_PRELOAD"): Some("xyzzy")

But I did notice something odd; environment output seems to get cached sometimes:

$ LD_PRELOAD="/usr/lib/libvorbisidec.so.1" cargo test | fgrep LD_
ERROR: ld.so: object 'xyzzy' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR: ld.so: object 'xyzzy' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running target/debug/deps/adf-57e3894efe848bee

$ fgrep xyzzy -r .
./target/debug/.fingerprint/adf-e2c5c24c6ddeacf5/output-bin-adf:ERROR: ld.so: object 'xyzzy' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
./target/debug/.fingerprint/adf-e2c5c24c6ddeacf5/output-bin-adf:ERROR: ld.so: object 'xyzzy' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.

So perhaps try a cargo clean and then retry?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.