Skip to content

calling as_ptr from __asan_default_options segfaults with -C debuginfo=2 #50066

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

Closed
Hello71 opened this issue Apr 18, 2018 · 4 comments
Closed
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Hello71
Copy link

Hello71 commented Apr 18, 2018

in the process of working around #41807, I found that the following code fragment results in a segmentation fault when run after compilation with -Z sanitizer=address -C debuginfo=2.

const ASAN_DEFAULT_OPTIONS: &'static [u8] = b"detect_odr_violation=1\0";

#[no_mangle]
pub extern "C" fn __asan_default_options() -> *const u8 {
    ASAN_DEFAULT_OPTIONS.as_ptr()
}

fn main() {
    println!("Hello World!");
}
$ rustc test.rs -Z sanitizer=address
$ ./test
Hello World!
$ rustc test.rs -Z sanitizer=address -C debuginfo=2
$ ./test
sh: segmentation fault (core dumped)  ./test
$ rust-gdb ./test
[ ... ]
Program received signal SIGSEGV, Segmentation fault.
0x000055555555e11d in alloc::slice::<impl [T]>::as_ptr (self=&[u8](len: 140737488345840) = {...})
    at /checkout/src/liballoc/slice.rs:474
(gdb) bt
#0  0x000055555555e11d in alloc::slice::<impl [T]>::as_ptr (self=&[u8](len: 140737488345840) = {...})
    at /checkout/src/liballoc/slice.rs:474
#1  0x000055555555e2ae in __asan_default_options () at test.rs:5
#2  0x000055555556da92 in __asan::MaybeCallAsanDefaultOptions ()
    at /checkout/src/libcompiler_builtins/compiler-rt/lib/asan/asan_flags.cc:31
#3  __asan::InitializeFlags () at /checkout/src/libcompiler_builtins/compiler-rt/lib/asan/asan_flags.cc:115
#4  0x000055555555cd25 in __asan::AsanInitInternal ()
    at /checkout/src/libcompiler_builtins/compiler-rt/lib/asan/asan_rtl.cc:380
#5  0x00007ffff7de75de in _dl_init () from /lib64/ld-linux-x86-64.so.2
#6  0x00007ffff7dd8f6a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#7  0x0000000000000001 in ?? ()
#8  0x00007fffffffe062 in ?? ()
#9  0x0000000000000000 in ?? ()

Interestingly, adding -C opt-level=1 fixes the issue, as does manually inlining as_ptr (i.e. ASAN_DEFAULT_OPTIONS as *const [u8] as *const u8).

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 27, 2018
@tmiasko
Copy link
Contributor

tmiasko commented Oct 31, 2019

The address sanitizer needs to complete initialization before calling
instrumented functions is possible.

The __asan_default_options itself is never instrumented (the functions with
names starting with __asan_ are excluded from instrumentation), but it cannot
call other functions that might be, like as_ptr for example.

@Hello71
Copy link
Author

Hello71 commented Oct 31, 2019

It'd be nice if it didn't crash though. Also, isn't it still a problem that it's contingent on -C opt-level=1 and possibly also -C debuginfo=2? What's the point of instrumenting as_ptr if it gets inlined anyways?

@tmiasko
Copy link
Contributor

tmiasko commented Oct 31, 2019

What's the point of instrumenting as_ptr if it gets inlined anyways?

It isn't inlined actually when the issue occurs.

It'd be nice if it didn't crash though.

You could try reporting issue upstream to google/sanitizers or bugs.llvm.org,
since I don't think there is much that could be done on rustc side of things,
nor is the issue specific to rustc.

@Mark-Simulacrum
Copy link
Member

I'm going to close this as expected, if perhaps undesirable, behavior, and changes here being out of scope for rustc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants