Skip to content
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

rustc should give compile error when try to enable sanitizer while statically link to libc like gcc does #85459

Closed
12101111 opened this issue May 19, 2021 · 1 comment · Fixed by #85559
Labels
A-sanitizers Area: Sanitizers for correctness and code quality. C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness requires-nightly This issue requires a nightly compiler in some way.

Comments

@12101111
Copy link
Contributor

12101111 commented May 19, 2021

Combination such compiler option will either lead to a link error, or a runtime segmentation fault.

Address sanitizer

Consider this C code:

#include <stdlib.h>
int main(int argc, char **argv) {
  int *array = (int*) malloc(100);
  free(array);
  return *array;
}

Compile it using gcc with -fsanitize=address -static on glibc system:

> LANG=C gcc -O1 -g -fsanitize=address -fno-omit-frame-pointer useafterfree.c -static
gcc: error: cannot specify -static with -fsanitize=address

Compile it using clang with -fsanitize=address -static on glibc system:

> clang -O1 -g -fsanitize=address -fno-omit-frame-pointer useafterfree.c -static
/usr/bin/ld: /usr/lib/clang/11.1.0/lib/linux/libclang_rt.asan-x86_64.a(asan_linux.cpp.o): in function `__asan::AsanDoesNotSupportStaticLinkage()':
(.text+0xd3): undefined reference to `_DYNAMIC'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

Compile it using clang with -fsanitize=address -static on musl system:

> clang -O1 -g -fsanitize=address -fno-omit-frame-pointer useafterfree.c --unwindlib=libunwind -static
ld.lld: error: undefined hidden symbol: _DYNAMIC
>>> referenced by crt1.c
>>>               /usr/lib/gcc/x86_64-gentoo-linux-musl/11.1.0/../../../crt1.o:(.text+0x9)
>>> referenced by asan_linux.cpp.o:(__asan::AsanDoesNotSupportStaticLinkage()) in archive /usr/lib/llvm/12/bin/../../../../lib/clang/12.0.0/lib/linux/libclang_rt.asan-x86_64.a
>>> referenced by __init_tls.c:0 (src/env/__init_tls.c:0)
>>>               __init_tls.lo:(static_init_tls) in archive /usr/lib/gcc/x86_64-gentoo-linux-musl/11.1.0/../../../libc.a
>>> referenced 2 more times
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

( I don't enable GCC's sanitizer support on musl system, but the result should same )

The same code in rust:

fn main() {
    let array = vec![42; 100];
    let ptr = array.as_ptr();
    drop(array);
    assert_eq!(unsafe { *ptr }, 42);
}

x86_64-unknown-linux-gnu system:

> rustc --target x86_64-unknown-linux-gnu -g -Z sanitizer=address -Ctarget-feature=+crt-static use-after-free.rs
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-Wl,--as-needed" "-Wl,-Bstatic" "-Wl,--whole-archive" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc-nightly_rt.asan.a" "-Wl,--no-whole-archive" "-L" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "use-after-free.use_after_free.7rcbfp3g-cgu.0.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.1.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.10.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.11.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.12.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.13.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.14.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.15.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.2.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.3.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.4.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.5.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.6.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.7.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.8.rcgu.o" "use-after-free.use_after_free.7rcbfp3g-cgu.9.rcgu.o" "-o" "use-after-free" "use-after-free.4tvmi6p3xldfen0k.rcgu.o" "-Wl,--gc-sections" "-static" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-21256c14e4504640.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-986e7fea4474d4d2.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-91d85f45babdb06e.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-756509c31813f9a6.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-5c2ef587e740c0cc.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-ad8f9371d31db1b3.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-402820fb3b4d3dd8.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-5e8e903f07dc8747.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-7ab9ae5080c07187.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-c8e8d47c6bf14de2.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-5a02dbb58abd015a.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-cedc8fec61444895.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-a1c96351c52bc56a.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-e41c6d24baeb2249.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-62493b241881590c.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-a1fd7734706d5518.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-c8ded1707ad10767.rlib" "-Wl,--end-group" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-1eec8756baa5bc24.rlib" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-lgcc_eh" "-lgcc" "-Wl,-Bdynamic"
  = note: /usr/bin/ld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc-nightly_rt.asan.a(asan_interceptors.cpp.o): in function `InitializeCommonInterceptors()':
          /rustc/llvm/src/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:10115: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
          /usr/bin/ld: /rustc/llvm/src/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:10121: warning: Using 'gethostbyname2_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
          /usr/bin/ld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc-nightly_rt.asan.a(asan_linux.cpp.o): in function `__asan::AsanDoesNotSupportStaticLinkage()':
          /rustc/llvm/src/llvm-project/compiler-rt/lib/asan/asan_linux.cpp:88: undefined reference to `_DYNAMIC'
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

x86_64-unknown-linux-musl system:

> rustc --target x86_64-unknown-linux-musl -g -Z sanitizer=address  use-after-free.rs
> ./use-after-free
zsh: segmentation fault (core dumped)  ./use-after-free
> lldb use-after-free
(lldb) target create "use-after-free"
Current executable set to '/tmp/use-after-free' (x86_64).
(lldb) r
Process 37117 launched: '/tmp/use-after-free' (x86_64)
Process 37117 stopped
* thread #1, name = 'use-after-free', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
    frame #0: 0x0000000000000000
error: memory read failed for 0x0
(lldb) bt
* thread #1, name = 'use-after-free', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
  * frame #0: 0x0000000000000000
    frame #1: 0x00007ffff769515d use-after-free`::__interceptor_vsnprintf(str=0x0000000000000000, size=0, format="Symbol not found: %s", ap=<unavailable>) at sanitizer_common_interceptors.inc:1651:1
    frame #2: 0x00007ffff771650c use-after-free`__dl_vseterr + 135
    frame #3: 0x00007ffff77165ea use-after-free`__dl_seterr + 140
    frame #4: 0x00007ffff7718689 use-after-free`stub_dlsym + 15
    frame #5: 0x00007ffff76b9405 use-after-free`__interception::InterceptFunction(char const*, unsigned long*, unsigned long, unsigned long) at interception_linux.cpp:42:21
    frame #6: 0x00007ffff76b9400 use-after-free`__interception::InterceptFunction(name="mmap", ptr_to_real=0x00007ffff7764f38, func=140737343963632, wrapper=140737343963632) at interception_linux.cpp:61
    frame #7: 0x00007ffff769ffc4 use-after-free`__asan::InitializeAsanInterceptors() at sanitizer_common_interceptors.inc:10041:3
    frame #8: 0x00007ffff769ff89 use-after-free`__asan::InitializeAsanInterceptors() at asan_interceptors.cpp:617
    frame #9: 0x00007ffff76b679b use-after-free`__asan::(anonymous)() at asan_rtl.cpp:452:29
    frame #10: 0x00007ffff76d6956 use-after-free`asan.module_ctor + 6
    frame #11: 0x00007ffff7715f82 use-after-free`libc_start_init + 24
    frame #12: 0x00007ffff7715fa7 use-after-free`libc_start_main_stage2 + 29
    frame #13: 0x00007ffff763853f use-after-free`_start + 22
(lldb)

The crt-static is enabled on musl by default, disable it make sanitizer works:

> rustc --target x86_64-unknown-linux-musl -g -Z sanitizer=address -C target-feature=-crt-static use-after-free.rs
> ./use-after-free
=================================================================
==11679==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x5606f0286b76 bp 0x7ffc933e0730 sp 0x7ffc933e0728
READ of size 4 at 0x614000000040 thread T0
    #0 0x5606f0286b75 in use_after_free::main::hd67444d0a9ab0b12 /tmp/use-after-free.rs:5:25
    #1 0x5606f028512a in core::ops::function::FnOnce::call_once::hf0066ba3fb7d9316 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:227:5
    #2 0x5606f0285824 in std::sys_common::backtrace::__rust_begin_short_backtrace::h039e6bbe2d8f8280 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/sys_common/backtrace.rs:125:18
    #3 0x5606f0284643 in std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h3a34146054417093 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:49:18
    #4 0x5606f0298678 in core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::h443ce774ba259e6b /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:259:13
    #5 0x5606f0298678 in std::panicking::try::do_call::h1984458153b1f78e /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panicking.rs:401:40
    #6 0x5606f0298678 in std::panicking::try::h4337f335f0d0b14c /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panicking.rs:365:19
    #7 0x5606f0298678 in std::panic::catch_unwind::h5bacacb3e888c4d6 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panic.rs:433:14
    #8 0x5606f0298678 in std::rt::lang_start_internal::hee984e623c043710 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:34:21
    #9 0x5606f02845af in std::rt::lang_start::hef9290f42a8f1b8a /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:48:5
    #10 0x5606f0286f1b in main (/tmp/use-after-free+0xecf1b)
    #11 0x7fd6c015e588 in libc_start_main_stage2 /usr/src/debug/sys-libs/musl-1.2.2-r2/musl-1.2.2/src/env/__libc_start_main.c:94:7

0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0)
freed by thread T0 here:
    #0 0x5606f02535d7 in free /checkout/src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:127:3
    #1 0x5606f027b12c in alloc::alloc::dealloc::h513f6f31c280f216 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/alloc.rs:104:14
    #2 0x5606f027b4d5 in _$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$::deallocate::he32da7f15b2b4f6e /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/alloc.rs:239:22
    #3 0x5606f0279949 in _$LT$alloc..raw_vec..RawVec$LT$T$C$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::h0c6326d9a99b0440 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/raw_vec.rs:521:22
    #4 0x5606f02853c9 in core::ptr::drop_in_place$LT$alloc..raw_vec..RawVec$LT$i32$GT$$GT$::hb13a97468692a292 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ptr/mod.rs:192:1
    #5 0x5606f027ba95 in core::mem::drop::h369ec49231cf9282 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/mem/mod.rs:891:24
    #6 0x5606f028512a in core::ops::function::FnOnce::call_once::hf0066ba3fb7d9316 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:227:5
    #7 0x5606f0284643 in std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h3a34146054417093 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:49:18

previously allocated by thread T0 here:
    #0 0x5606f025379f in malloc /checkout/src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3
    #1 0x5606f0279cbb in alloc::alloc::alloc::h0accaf8fe1496f6b /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/alloc.rs:86:14
    #2 0x5606f0279f67 in alloc::alloc::Global::alloc_impl::h4bd67d5bd2f0c331 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/alloc.rs:166:73
    #3 0x5606f027ba20 in _$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$::allocate::hc392a1bbb11a25a7 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/alloc.rs:226:9
    #4 0x5606f0277843 in alloc::raw_vec::RawVec$LT$T$C$A$GT$::allocate_in::he9a19d70657f5911 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/raw_vec.rs:203:45
    #5 0x5606f0278e8b in alloc::raw_vec::RawVec$LT$T$C$A$GT$::with_capacity_in::h083a9dd897fd54be /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/raw_vec.rs:142:9
    #6 0x5606f0283708 in alloc::vec::from_elem::h2408619de949e29a /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/vec/mod.rs:2291:5
    #7 0x5606f028512a in core::ops::function::FnOnce::call_once::hf0066ba3fb7d9316 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:227:5
    #8 0x5606f0284643 in std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h3a34146054417093 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:49:18

SUMMARY: AddressSanitizer: heap-use-after-free /tmp/use-after-free.rs:5:25 in use_after_free::main::hd67444d0a9ab0b12
Shadow bytes around the buggy address:
  0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
  0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
  0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==11679==ABORTING

Thread sanitizer

static mut V: i32 = 0;

fn main() {
    let thread = std::thread::spawn(|| unsafe { V = 1 });
    unsafe { V = -1 };
    assert_eq!(unsafe { V }, -1);
    thread.join().unwrap();
}

x86_64-unknown-linux-gnu system:

> rustc --target x86_64-unknown-linux-gnu -g -Z sanitizer=thread -Ctarget-feature=+crt-static data-race.rs
> lldb ./data-race
(lldb) target create "./data-race"
Current executable set to '/tmp/data-race' (x86_64).
(lldb) r
Process 31912 launched: '/tmp/data-race' (x86_64)
Process 31912 stopped
* thread #1, name = 'data-race', stop reason = signal SIGSEGV: invalid address (fault address: 0xfffffffffffc03d0)
    frame #0: 0x000000000041370d data-race`::__interceptor_memcpy() [inlined] cur_thread_init at tsan_rtl.h:470:7
(lldb) bt
* thread #1, name = 'data-race', stop reason = signal SIGSEGV: invalid address (fault address: 0xfffffffffffc03d0)
  * frame #0: 0x000000000041370d data-race`::__interceptor_memcpy() [inlined] cur_thread_init at tsan_rtl.h:470:7
    frame #1: 0x0000000000413706 data-race`::__interceptor_memcpy() at sanitizer_common_interceptors.inc:810
    frame #2: 0x00000000004adb63 data-race`__libc_setup_tls + 323
    frame #3: 0x00000000004ad0f6 data-race`__libc_start_main + 1782
    frame #4: 0x0000000000403caa data-race`_start + 42

x86_64-unknown-linux-musl system:

> rustc --target x86_64-unknown-linux-musl -g -Z sanitizer=thread data-race.rs
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-Wl,--as-needed" "-nostartfiles" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbeginS.o" "-Wl,-Bstatic" "-Wl,--whole-archive" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc-nightly_rt.tsan.a" "-Wl,--no-whole-archive" "-L" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib" "-L" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "data-race.data_race.7rcbfp3g-cgu.0.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.1.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.10.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.11.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.12.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.13.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.14.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.15.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.2.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.3.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.4.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.5.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.6.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.7.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.8.rcgu.o" "data-race.data_race.7rcbfp3g-cgu.9.rcgu.o" "-o" "data-race" "data-race.41y57o6b39iya3rh.rcgu.o" "-Wl,--gc-sections" "-static-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,--start-group" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-adf528d372a28473.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-ba85beab84d56f31.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-8e0ed322aa81e3bf.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-22a1ad77c898fd0f.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-4a2366535c95aae7.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-6ccaff62092822fc.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-158019b8b22c2e82.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-ffbf746b700d6467.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-b17d0f1c2c3afc1b.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-39a44a46f42c92b0.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-9f9afb16499a5305.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-f2e73ac26b03b976.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-19717748cdece5e2.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-683c2ad1ec876f13.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-bc18a38f44606206.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-9af05494f0573e1b.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-60eab00334ec8288.rlib" "-Wl,--end-group" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-3ce947a09e01658c.rlib" "-Wl,-Bdynamic" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
  = note: ld.lld: error: undefined symbol: __gcc_personality_v0
          >>> referenced by data_race.7rcbfp3g-cgu.0
          >>>               data-race.data_race.7rcbfp3g-cgu.0.rcgu.o:(DW.ref.__gcc_personality_v0)
          clang-12: error: linker command failed with exit code 1 (use -v to see invocation)


error: aborting due to previous error
> rustc --target x86_64-unknown-linux-musl -g -Z sanitizer=thread -Ctarget-feature=-crt-static data-race.rs
> ./data-race
==================
WARNING: ThreadSanitizer: data race (pid=31254)
  Write of size 4 at 0x559c13e33000 by thread T1:
    #0 data_race::main::_$u7b$$u7b$closure$u7d$$u7d$::h5425793b00e03c82 /tmp/data-race.rs:4:49 (data-race+0xb6fb6)
    #1 std::sys_common::backtrace::__rust_begin_short_backtrace::h0c43622592bcd42b /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/sys_common/backtrace.rs:125:18 (data-race+0xb6342)
    #2 std::thread::Builder::spawn_unchecked::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::he1f0cb6d014f0414 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/thread/mod.rs:481:17 (data-race+0xb7e62)
    #3 _$LT$std..panic..AssertUnwindSafe$LT$F$GT$$u20$as$u20$core..ops..function..FnOnce$LT$$LP$$RP$$GT$$GT$::call_once::h660f3006249ddeb6 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panic.rs:346:9 (data-race+0xb6622)
    #4 std::panicking::try::do_call::he0360dcee9353d32 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panicking.rs:401:40 (data-race+0xb9644)
    #5 __rust_try <null> (data-race+0xb97ab)
    #6 std::panicking::try::habe2877bdc0442fc /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panicking.rs:365:19 (data-race+0xb9560)
    #7 std::panic::catch_unwind::ha18db59c1b466881 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panic.rs:433:14 (data-race+0xb6662)
    #8 std::thread::Builder::spawn_unchecked::_$u7b$$u7b$closure$u7d$$u7d$::hbab43888eaac5ed4 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/thread/mod.rs:480:30 (data-race+0xb7c7d)
    #9 core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h530014c6fb3b4bcf /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:227:5 (data-race+0xb9db1)
    #10 _$LT$alloc..boxed..Box$LT$F$C$A$GT$$u20$as$u20$core..ops..function..FnOnce$LT$Args$GT$$GT$::call_once::h86bce8a1db04edb7 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/boxed.rs:1575:9 (data-race+0xcf0b6)
    #11 _$LT$alloc..boxed..Box$LT$F$C$A$GT$$u20$as$u20$core..ops..function..FnOnce$LT$Args$GT$$GT$::call_once::h05d43cce12a14700 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/alloc/src/boxed.rs:1575:9 (data-race+0xcf0b6)
    #12 std::sys::unix::thread::Thread::new::thread_start::h8509010ee939454d /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/sys/unix/thread.rs:71:17 (data-race+0xcf0b6)

  Previous write of size 4 at 0x559c13e33000 by main thread:
    #0 data_race::main::he6faf34f54450c2d /tmp/data-race.rs:5:14 (data-race+0xb6e0d)
    #1 core::ops::function::FnOnce::call_once::h481df0edccf5a53d /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:227:5 (data-race+0xb9ede)
    #2 std::sys_common::backtrace::__rust_begin_short_backtrace::h1142b335c5133f08 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/sys_common/backtrace.rs:125:18 (data-race+0xb63d1)
    #3 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::hb27e65b2ba5fe698 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:49:18 (data-race+0xb9c8e)
    #4 core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::h443ce774ba259e6b /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:259:13 (data-race+0xcd518)
    #5 std::panicking::try::do_call::h1984458153b1f78e /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panicking.rs:401:40 (data-race+0xcd518)
    #6 std::panicking::try::h4337f335f0d0b14c /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panicking.rs:365:19 (data-race+0xcd518)
    #7 std::panic::catch_unwind::h5bacacb3e888c4d6 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panic.rs:433:14 (data-race+0xcd518)
    #8 std::rt::lang_start_internal::hee984e623c043710 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:34:21 (data-race+0xcd518)
    #9 main <null> (data-race+0xb7016)

  Location is global 'data_race::V::h6d3c71c01326dbb6' of size 4 at 0x559c13e33000 (data-race+0x000000b1b000)

  Thread T1 (tid=31256, running) created by main thread at:
    #0 pthread_create /checkout/src/llvm-project/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp:965:3 (data-race+0x8de9d)
    #1 std::sys::unix::thread::Thread::new::h9f8b95f1ab5ef8e1 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/sys/unix/thread.rs:50:19 (data-race+0xcef34)
    #2 std::thread::Builder::spawn::h8d5ed25372280dca /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/thread/mod.rs:388:18 (data-race+0xb7ed4)
    #3 std::thread::spawn::h362a5e2df80b92df /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/thread/mod.rs:627:5 (data-race+0xb726b)
    #4 data_race::main::he6faf34f54450c2d /tmp/data-race.rs:4:18 (data-race+0xb6dfd)
    #5 core::ops::function::FnOnce::call_once::h481df0edccf5a53d /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:227:5 (data-race+0xb9ede)
    #6 std::sys_common::backtrace::__rust_begin_short_backtrace::h1142b335c5133f08 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/sys_common/backtrace.rs:125:18 (data-race+0xb63d1)
    #7 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::hb27e65b2ba5fe698 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:49:18 (data-race+0xb9c8e)
    #8 core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::h443ce774ba259e6b /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/core/src/ops/function.rs:259:13 (data-race+0xcd518)
    #9 std::panicking::try::do_call::h1984458153b1f78e /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panicking.rs:401:40 (data-race+0xcd518)
    #10 std::panicking::try::h4337f335f0d0b14c /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panicking.rs:365:19 (data-race+0xcd518)
    #11 std::panic::catch_unwind::h5bacacb3e888c4d6 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/panic.rs:433:14 (data-race+0xcd518)
    #12 std::rt::lang_start_internal::hee984e623c043710 /rustc/4e3e6db011c5b482d2bef8ba02274657f93b5e0d/library/std/src/rt.rs:34:21 (data-race+0xcd518)
    #13 main <null> (data-race+0xb7016)

SUMMARY: ThreadSanitizer: data race /tmp/data-race.rs:4:49 in data_race::main::_$u7b$$u7b$closure$u7d$$u7d$::h5425793b00e03c82
==================
ThreadSanitizer: reported 1 warnings

Leak sanitizer

fn main() {
    std::mem::forget(vec![10;0]);
}

x86_64-unknown-linux-gnu system:

> rustc --target x86_64-unknown-linux-gnu -g -Z sanitizer=leak -Ctarget-feature=+crt-static forget.rs
> ./forget
==6109==Sanitizer CHECK failed: /checkout/src/llvm-project/compiler-rt/lib/lsan/lsan_interceptors.cpp:55 ((!lsan_init_is_running)) != (0) (0, 0)

x86_64-unknown-linux-musl system:

> rustc --target x86_64-unknown-linux-musl -g -Z sanitizer=leak -Ctarget-feature=+crt-static forget.rs
> ./forget
zsh: segmentation fault (core dumped)  ./forget
> lldb ./forget
(lldb) target create "./forget"
Current executable set to '/tmp/forget' (x86_64).
(lldb) r
Process 7018 launched: '/tmp/forget' (x86_64)
Process 7018 stopped
* thread #1, name = 'forget', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
    frame #0: 0x0000000000000000
error: memory read failed for 0x0
(lldb) bt
* thread #1, name = 'forget', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
  * frame #0: 0x0000000000000000
    frame #1: 0x00007ffff7717037 forget`std::rt::lang_start_internal::hee984e623c043710 [inlined] std::sys::unix::init::reset_sigpipe::h7a2caafd6df55210 at mod.rs:119:17
    frame #2: 0x00007ffff7717027 forget`std::rt::lang_start_internal::hee984e623c043710 [inlined] std::sys::unix::init::h90a84edd8425fcc8 at mod.rs:62
    frame #3: 0x00007ffff7716f72 forget`std::rt::lang_start_internal::hee984e623c043710 [inlined] std::sys_common::rt::init::h3947e78f9de3a69e at rt.rs:15
    frame #4: 0x00007ffff7716f72 forget`std::rt::lang_start_internal::hee984e623c043710 at rt.rs:32
    frame #5: 0x00007ffff7704ea0 forget`std::rt::lang_start::h97aa25b0f579616a(main=(forget`forget::main::h4fe3477e9b974083 at forget.rs:1), argc=1, argv=0x00007fffffffbd58) at rt.rs:48:5
    frame #6: 0x00007ffff770586c forget`main + 28
    frame #7: 0x00007ffff773944d forget`libc_start_main_stage2 + 39
    frame #8: 0x00007ffff76e5530 forget`_start + 22
@12101111 12101111 added the C-bug Category: This is a bug. label May 19, 2021
@jonas-schievink jonas-schievink added the A-sanitizers Area: Sanitizers for correctness and code quality. label May 19, 2021
@jyn514 jyn514 added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label May 19, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 19, 2021
@jyn514 jyn514 added the requires-nightly This issue requires a nightly compiler in some way. label May 19, 2021
@apiraino
Copy link
Contributor

Removing prioritization flag as this is a nightly-only feature that still needs some work. For reference, the tracking issue #47174

@rustbot label -I-prioritize

@rustbot rustbot removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 19, 2021
@bors bors closed this as completed in aab93ca May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sanitizers Area: Sanitizers for correctness and code quality. C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants