ThreadSanitizer (tsan) - 来自共享库的有意义的信息

ThreadSanitizer (tsan) - Meaningful information from shared library

我的应用程序使用了 wxWidgets 库,它是通过 gcc 5.1.0 从源代码构建的,使用 -g 和 -O0

我使用 clang++36 -g -fsanitize=thread -stdlib=libc++ 编译了我的应用程序,并使用 clang++36 -g -fsanitize=thread -stdlib=libc++ -lc++abi 链接了它。它动态链接到 wxWidgets。

我收到的警告之一是:

==================
WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock) (pid=52741)
  Cycle in lock order graph: M115 (0x7d080000ea60) => M976 (0x7d0800000100) => M115

  Mutex M976 acquired here while holding mutex M115 in main thread:
    #0 pthread_mutex_lock /home/xxx/sourceInstallations/llvm-3.6.0/projects/compiler-rt/lib/tsan/../sanitizer_common/sanitizer_common_interceptors.inc:3008 (wxDebugSleep+0x00000043b0ef)
    #1 <null> <null> (libwx_baseu-3.0.so.0+0x0000002376fa)
    #2 _start <null> (wxDebugSleep+0x00000041be4e)

    Hint: use TSAN_OPTIONS=second_deadlock_stack=1 to get more informative warning message

  Mutex M115 acquired here while holding mutex M976 in main thread:
    #0 pthread_mutex_lock /home/xxx/sourceInstallations/llvm-3.6.0/projects/compiler-rt/lib/tsan/../sanitizer_common/sanitizer_common_interceptors.inc:3008 (wxDebugSleep+0x00000043b0ef)
    #1 <null> <null> (libwx_baseu-3.0.so.0+0x0000002376fa)
    #2 wxCriticalSectionLocker::wxCriticalSectionLocker(wxCriticalSection&) /usr/local/include/wx-3.0/wx/thread.h:307:9 (wxDebugSleep+0x000000473216)
    #3 <null> <null> (libwx_baseu-3.0.so.0+0x00000018b297)
    #4 _start <null> (wxDebugSleep+0x00000041be4e)

SUMMARY: ThreadSanitizer: lock-order-inversion (potential deadlock) ??:0 ??
==================

我不高兴是因为:(1) 我想尝试一下在 wxWidgets 库中查找线程错误; (2) 我希望能够使抑制文件的运行达到或接近一个警告与抑制文件的比率。

因此,我 recompiled/linked 通过 clang 3.6.0 从源代码获取 wxWidgets 库,添加 -fsanitize=thread -stdlib=libc++ -lc++abi。祈祷吧,它完成得很好。

运行 在我的 wxWidgets gcc 构建目录中执行 sudo make uninstall,在我的 wxWidgets clang 构建目录中执行 sudo install。

上面的警告现在显示:

==================
WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock) (pid=68453)
  Cycle in lock order graph: M115 (0x7d080000ea60) => M976 (0x7d0800000100) => M115

  Mutex M976 acquired here while holding mutex M115 in main thread:
    #0 pthread_mutex_lock /home/xxx/sourceInstallations/llvm-3.6.0/projects/compiler-rt/lib/tsan/../sanitizer_common/sanitizer_common_interceptors.inc:3008 (wxDebugSleep+0x00000043b0ef)
    #1 <null> <null> (libwx_baseu-3.0.so.0+0x0000003c24f9)
    #2 <null> <null> (libwx_baseu-3.0.so.0+0x0000003d0387)
    #3 <null> <null> (libwx_baseu-3.0.so.0+0x0000003d0000)
    #4 <null> <null> (libwx_baseu-3.0.so.0+0x0000001bd91c)
    #5 <null> <null> (libwx_baseu-3.0.so.0+0x000000279cd6)
    #6 <null> <null> (libwx_baseu-3.0.so.0+0x00000027a6da)
    #7 <null> <null> (libwx_baseu-3.0.so.0+0x00000024445d)
    #8 <null> <null> (libwx_baseu-3.0.so.0+0x000000244243)
    #9 <null> <null> (libwx_baseu-3.0.so.0+0x000000245a67)
    #10 <null> <null> (libwx_baseu-3.0.so.0+0x000000246856)
    #11 <null> <null> (libwx_baseu-3.0.so.0+0x000000245430)
    #12 <null> <null> (libwx_baseu-3.0.so.0+0x000000245934)
    #13 main /home/xxx/code/testing/wxDebugSleep/wxDebugSleep.cpp:11:1 (wxDebugSleep+0x000000472e9c)

    Hint: use TSAN_OPTIONS=second_deadlock_stack=1 to get more informative warning message

  Mutex M115 acquired here while holding mutex M976 in main thread:
    #0 pthread_mutex_lock /home/xxx/sourceInstallations/llvm-3.6.0/projects/compiler-rt/lib/tsan/../sanitizer_common/sanitizer_common_interceptors.inc:3008 (wxDebugSleep+0x00000043b0ef)
    #1 <null> <null> (libwx_baseu-3.0.so.0+0x0000003c24f9)
    #2 <null> <null> (libwx_baseu-3.0.so.0+0x0000003d0387)
    #3 wxCriticalSection::Enter(void) /usr/local/include/wx-3.0/wx/thread.h:291:52 (wxDebugSleep+0x00000047c570)
    #4 wxCriticalSectionLocker::wxCriticalSectionLocker(wxCriticalSection&) /usr/local/include/wx-3.0/wx/thread.h:307:9 (wxDebugSleep+0x000000473216)
    #5 <null> <null> (libwx_baseu-3.0.so.0+0x000000245cf0)
    #6 <null> <null> (libwx_baseu-3.0.so.0+0x000000246949)
    #7 <null> <null> (libwx_baseu-3.0.so.0+0x00000024574b)
    #8 <null> <null> (libwx_baseu-3.0.so.0+0x000000245934)
    #9 main /home/xxx/code/testing/wxDebugSleep/wxDebugSleep.cpp:11:1 (wxDebugSleep+0x000000472e9c)

SUMMARY: ThreadSanitizer: lock-order-inversion (potential deadlock) ??:0 ??
==================

我在运行程序的环境中定义了TSAN_OPTIONS=second_deadlock_stack=1,并没有改变输出。

好吧,这是一些进步。我确定我会使用错误的术语,但这就像它缺少库的符号文件。

我已经使用 clang & -fsanitize=thread(ldd 和时间戳)检查过它是否动态链接到新库。

我检查过该库是否使用 -g 和 -O0 进行编译(即使它可能更高。)

以防万一,FreeBSD 10.1 64 位。 Clang 是从源代码编译的。

问题 1 - 如何从共享库中获取 "stack trace" 以显示文件名和行号?

问题2 - 如果做不到,如何做一个好的压制文件?问题是 wxWidgets 调用了很多我的代码,所以我不认为我可以阻止包括库在内的任何堆栈。而且,当然,即使我可以使用偏移量制作抑制文件,如果我重新编译库,所有这些都可能会改变。

问题是 FreeBSD 10.1 及之前的版本存在错误,导致 llvm-symbolizer 无法正常工作。 llvm-symbolizer 是 TSAN 获取符号信息的方式。更具体地说,FreeBSD 给出了 dlpi_name 而没有 dl_iterate_phdr 中的路径。这是在 https://reviews.freebsd.org/D932 修补的。该补丁在 FreeBSD 10-STABLE 中可用(在回答这个问题时就像 10.2 beta)并且应该在 FreeBSD 10.2 及更高版本中。

顺便说一句,“-Wl,--version-script...”和“-Wl,-soname”工作正常。