2

I have some weird multithreading bugs, so I wanted to use the gcc thread sanitizer to find them. However, when I compile with -sanitize=thread the resulting binary segfaults immediatly with the following stacktrace:

#0  0x0000000000000000 in ?? ()
#1  0x00007ffff5911cee in ?? () from /usr/lib/x86_64-linux-gnu/libtsan.so.0
#2  0x00007ffff593408d in ?? () from /usr/lib/x86_64-linux-gnu/libtsan.so.0
#3  0x00007ffff58eeb98 in ?? () from /usr/lib/x86_64-linux-gnu/libtsan.so.0
#4  0x00007ffff58ef0ce in __interceptor___cxa_atexit ()
   from /usr/lib/x86_64-linux-gnu/libtsan.so.0
#5  0x00007ffff55d1e56 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7de74ea in call_init (l=<optimised out>, argc=argc@entry=1, 
    argv=argv@entry=0x7fffffffdeb8, env=env@entry=0x7fffffffdec8)
    at dl-init.c:72
#7  0x00007ffff7de75fb in call_init (env=0x7fffffffdec8, argv=0x7fffffffdeb8, 
    argc=1, l=<optimised out>) at dl-init.c:30
#8  _dl_init (main_map=0x7ffff7ffe168, argc=1, argv=0x7fffffffdeb8, 
    env=0x7fffffffdec8) at dl-init.c:120
#9  0x00007ffff7dd7cfa in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#10 0x0000000000000001 in ?? ()
#11 0x00007fffffffe22b in ?? ()
#12 0x0000000000000000 in ?? ()

I tried adding the -static-libtsan attribute, but now I get the following error:

FATAL: ThreadSanitizer CHECK failed: ../../../../src/libsanitizer/sanitizer_common/sanitizer_allocator.h:1203 "((IsAligned(p, page_size_))) != (0)" (0x0, 0x0)
    #0 __tsan::TsanCheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) <null> (sql+0x0000004aeb53)
    #1 __tsan::TsanCheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) <null> (sql+0x0000004aeb5b)
    #2 __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) <null> (sql+0x0000004b3af3)
    #3 __tsan::user_free(__tsan::ThreadState*, unsigned long, void*, bool) <null> (sql+0x00000049e6fc)
    #4 operator delete(void*) <null> (sql+0x00000045f6ec)
    #5 __static_initialization_and_destruction_0(int, int) [clone .constprop.123] <null> (sql+0x00000044a4ab)
    #6 __libc_csu_init <null> (sql+0x00000254468c)
    #7 __libc_start_main <null> (libc.so.6+0x0000000207be)
    #8 _start <null> (sql+0x000000459e18)

How can I fix this? What am I doing wrong here?

gexicide
  • 38,535
  • 21
  • 92
  • 152

2 Answers2

0

I haven't used TSAN w/gcc so can't help there, but you might want to give it a shot with clang (https://clang.llvm.org/). Since TSAN is part of clang, bugs tend to get fixed there first.

If you need help building clang, I've also written an article that might be useful: http://btorpey.github.io/blog/2015/01/02/building-clang/

WallStProg
  • 391
  • 4
  • 8
0

Answer found here: https://groups.google.com/forum/#!topic/thread-sanitizer/5cxyhIrl_SE

You don't need to give -ltsan (at least when using clang). You need to give -fsanitize=thread both when compiling and linking. -fsanitize=thread when linking will ensure that all proper libraries get linked in.

This worked for me for both clang and gcc

JodiTheTigger
  • 415
  • 5
  • 7