Skip to content

Incompatible ASan runtimes when Rust executable links with C shared library that dynamic links asan runtime #114127

Description

@hehaoqian

Issues:

  1. Address sanitized Rust program can compile, but fails to run,
    when it links a shared library that dynamic links to libasan.so,
    which is provided by gcc 7.5
  2. The error message is
==82818==Your application is linked against incompatible ASan runtimes.
  1. It can run with expected result if the shared lib static links to asan runtime, by -static-libasan in C link flags

The cause:

  1. When using RUSTFLAGS=-Zsanitizer=address, it seems rustc always static links asan runtime,
    by static link librustc-nightly_rt.asan.a bundled with rustc installation
  2. I think static links libasan, is not compatible with dynamic linked libasan
  3. 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

  1. 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
  2. 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.

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-sanitizersArea: Sanitizers for correctness and code qualityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions