Rust + AddressSanitizer + FFI not firing on segfaults

#1

Hi,

I’m interested in creating an rlib which wraps/provides a nice interface to a cpp library. Ideally during development I’d compile the cpp lib with ASan, then link it into my rust test executable to better catch memory corruption errors. However, when I do this ASan seems to stop working. I figure I’ve misunderstood something, but as hoping I could get pointed in the right direction.

C version:

// segfault.c
int segfault(void) {
        return *(int *)0x41414141;
}
// main.c
int segfault(void);

int main(void) {
        return segfault();
}
$ cc -fsanitize=address -o segfault.o -c segfault.c
$ ar rcs libsegfault.a segfault.o
$ cc -fsanitize=address -o x main.c libsegfault.a
$ ./x
AddressSanitizer:DEADLYSIGNAL
=================================================================
==130483==ERROR: AddressSanitizer: SEGV on unknown address 0x000041414141 (pc 0x5638b1adf1b0 bp 0x7ffef7688e60 sp 0x7ffef7688e60 T0)
==130483==The signal is caused by a READ memory access.
    #0 0x5638b1adf1af in segfault (/home/x/rust-asan-test/x+0x11af)
    #1 0x5638b1adf15d in main (/home/x/rust-asan-test/x+0x115d)
    #2 0x7fcf837c309a in __libc_start_main ../csu/libc-start.c:308
    #3 0x5638b1adf099 in _start (/home/x/rust-asan-test/x+0x1099)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/home/x/rust-asan-test/x+0x11af) in segfault
==130483==ABORTING

Rust version:

// build.rs
fn main() {
    println!("cargo:rustc-link-search=.");
    println!("cargo:rustc-link-lib=static=segfault");
}
// src/main.rs
extern "C" {
    fn segfault() -> i32;
}
fn main() {
    unsafe { segfault() };
}
$ RUSTFLAGS="-Z sanitizer=address" cargo run
   Compiling rust-asan-test v0.1.0 (/home/x/rust-asan-test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.41s
     Running `target/debug/rust-asan-test`
Segmentation fault

This is linux x64, tried with clang 7.0.1 and gcc 8.3. Is this possible/have I done something dumb?

0 Likes