Description
Issues:
- Address sanitized Rust program can compile, but fails to run,
when it links a shared library that dynamic links tolibasan.so
,
which is provided by gcc 7.5 - The error message is
==82818==Your application is linked against incompatible ASan runtimes.
- It can run with expected result if the shared lib static links to asan runtime, by
-static-libasan
in C link flags
The cause:
- When using
RUSTFLAGS=-Zsanitizer=address
, it seems rustc always static links asan runtime,
by static linklibrustc-nightly_rt.asan.a
bundled with rustc installation - I think static links libasan, is not compatible with dynamic linked libasan
- I don't think rustc provides a way to opt out of static linking
librustc-nightly_rt.asan.a
Compatibility of asan runtimes of gcc7.5.0 and rustc
- The version of asan in gcc7.5.0 is 8
According to__asan_version_mismatch_check_v8
in https://github.com/gcc-mirror/gcc/blob/releases/gcc-7.5.0/libsanitizer/asan/asan_init_version.h - The version of asan in rustc is also 8.
According to GetAsanVersion function in https://github.com/rust-lang/llvm-project/blob/rustc/16.0-2023-06-05/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Steps to reproduce
// segfault.c
int segfault(void) {
return *(int *)0x41414141;
}
Create C shared lib by gcc -shared -fPIC segfault.c -o libsegfault.so -fsanitize=address
// build.rs
fn main() {
println!("cargo:rustc-link-search=.");
println!("cargo:rustc-link-lib=segfault");
}
// src/main.rs
extern "C" {
fn segfault() -> i32;
}
fn main() {
println!("Hello world!");
unsafe { segfault() };
}
Producing Rust executable with
RUSTFLAGS="-Z sanitizer=address" cargo +nightly build -Z build-std
Then run the final executable, set environment variable LD_LIBRARY_PATH
if needed
Actual result:
==82818==Your application is linked against incompatible ASan runtimes.
Expected result:
==108960==ERROR: AddressSanitizer: SEGV on unknown address 0x000041414141
Meta
Target platform: x86_64-unknown-linux-gnu
rustc --version --verbose
:
rustc 1.73.0-nightly (399b06823 2023-07-20)
gcc --version
gcc (GCC) 7.5.0
Extra notes:
I asked ChatGPT
gcc address sanitizer, shared link or static link?
It answers:
When using the GCC (GNU Compiler Collection) AddressSanitizer (ASan),
it is generally recommended to use it with shared libraries (dynamic linking) rather than static linking.
AddressSanitizer is a powerful runtime memory error detector that can find various memory-related issues like out-of-bounds access, use-after-free, and memory leaks.