1*67e74705SXin LiThreadSanitizer 2*67e74705SXin Li=============== 3*67e74705SXin Li 4*67e74705SXin LiIntroduction 5*67e74705SXin Li------------ 6*67e74705SXin Li 7*67e74705SXin LiThreadSanitizer is a tool that detects data races. It consists of a compiler 8*67e74705SXin Liinstrumentation module and a run-time library. Typical slowdown introduced by 9*67e74705SXin LiThreadSanitizer is about **5x-15x**. Typical memory overhead introduced by 10*67e74705SXin LiThreadSanitizer is about **5x-10x**. 11*67e74705SXin Li 12*67e74705SXin LiHow to build 13*67e74705SXin Li------------ 14*67e74705SXin Li 15*67e74705SXin LiBuild LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_. 16*67e74705SXin Li 17*67e74705SXin LiSupported Platforms 18*67e74705SXin Li------------------- 19*67e74705SXin Li 20*67e74705SXin LiThreadSanitizer is supported on Linux x86_64 (tested on Ubuntu 12.04). 21*67e74705SXin LiSupport for other 64-bit architectures is possible, contributions are welcome. 22*67e74705SXin LiSupport for 32-bit platforms is problematic and is not planned. 23*67e74705SXin Li 24*67e74705SXin LiUsage 25*67e74705SXin Li----- 26*67e74705SXin Li 27*67e74705SXin LiSimply compile and link your program with ``-fsanitize=thread``. To get a 28*67e74705SXin Lireasonable performance add ``-O1`` or higher. Use ``-g`` to get file names 29*67e74705SXin Liand line numbers in the warning messages. 30*67e74705SXin Li 31*67e74705SXin LiExample: 32*67e74705SXin Li 33*67e74705SXin Li.. code-block:: console 34*67e74705SXin Li 35*67e74705SXin Li % cat projects/compiler-rt/lib/tsan/lit_tests/tiny_race.c 36*67e74705SXin Li #include <pthread.h> 37*67e74705SXin Li int Global; 38*67e74705SXin Li void *Thread1(void *x) { 39*67e74705SXin Li Global = 42; 40*67e74705SXin Li return x; 41*67e74705SXin Li } 42*67e74705SXin Li int main() { 43*67e74705SXin Li pthread_t t; 44*67e74705SXin Li pthread_create(&t, NULL, Thread1, NULL); 45*67e74705SXin Li Global = 43; 46*67e74705SXin Li pthread_join(t, NULL); 47*67e74705SXin Li return Global; 48*67e74705SXin Li } 49*67e74705SXin Li 50*67e74705SXin Li $ clang -fsanitize=thread -g -O1 tiny_race.c 51*67e74705SXin Li 52*67e74705SXin LiIf a bug is detected, the program will print an error message to stderr. 53*67e74705SXin LiCurrently, ThreadSanitizer symbolizes its output using an external 54*67e74705SXin Li``addr2line`` process (this will be fixed in future). 55*67e74705SXin Li 56*67e74705SXin Li.. code-block:: bash 57*67e74705SXin Li 58*67e74705SXin Li % ./a.out 59*67e74705SXin Li WARNING: ThreadSanitizer: data race (pid=19219) 60*67e74705SXin Li Write of size 4 at 0x7fcf47b21bc0 by thread T1: 61*67e74705SXin Li #0 Thread1 tiny_race.c:4 (exe+0x00000000a360) 62*67e74705SXin Li 63*67e74705SXin Li Previous write of size 4 at 0x7fcf47b21bc0 by main thread: 64*67e74705SXin Li #0 main tiny_race.c:10 (exe+0x00000000a3b4) 65*67e74705SXin Li 66*67e74705SXin Li Thread T1 (running) created at: 67*67e74705SXin Li #0 pthread_create tsan_interceptors.cc:705 (exe+0x00000000c790) 68*67e74705SXin Li #1 main tiny_race.c:9 (exe+0x00000000a3a4) 69*67e74705SXin Li 70*67e74705SXin Li``__has_feature(thread_sanitizer)`` 71*67e74705SXin Li------------------------------------ 72*67e74705SXin Li 73*67e74705SXin LiIn some cases one may need to execute different code depending on whether 74*67e74705SXin LiThreadSanitizer is enabled. 75*67e74705SXin Li:ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for 76*67e74705SXin Lithis purpose. 77*67e74705SXin Li 78*67e74705SXin Li.. code-block:: c 79*67e74705SXin Li 80*67e74705SXin Li #if defined(__has_feature) 81*67e74705SXin Li # if __has_feature(thread_sanitizer) 82*67e74705SXin Li // code that builds only under ThreadSanitizer 83*67e74705SXin Li # endif 84*67e74705SXin Li #endif 85*67e74705SXin Li 86*67e74705SXin Li``__attribute__((no_sanitize_thread))`` 87*67e74705SXin Li----------------------------------------------- 88*67e74705SXin Li 89*67e74705SXin LiSome code should not be instrumented by ThreadSanitizer. One may use the 90*67e74705SXin Lifunction attribute `no_sanitize_thread` to disable instrumentation of plain 91*67e74705SXin Li(non-atomic) loads/stores in a particular function. ThreadSanitizer still 92*67e74705SXin Liinstruments such functions to avoid false positives and provide meaningful stack 93*67e74705SXin Litraces. This attribute may not be supported by other compilers, so we suggest 94*67e74705SXin Lito use it together with ``__has_feature(thread_sanitizer)``. 95*67e74705SXin Li 96*67e74705SXin LiBlacklist 97*67e74705SXin Li--------- 98*67e74705SXin Li 99*67e74705SXin LiThreadSanitizer supports ``src`` and ``fun`` entity types in 100*67e74705SXin Li:doc:`SanitizerSpecialCaseList`, that can be used to suppress data race reports 101*67e74705SXin Liin the specified source files or functions. Unlike functions marked with 102*67e74705SXin Li`no_sanitize_thread` attribute, blacklisted functions are not instrumented at 103*67e74705SXin Liall. This can lead to false positives due to missed synchronization via atomic 104*67e74705SXin Lioperations and missed stack frames in reports. 105*67e74705SXin Li 106*67e74705SXin LiLimitations 107*67e74705SXin Li----------- 108*67e74705SXin Li 109*67e74705SXin Li* ThreadSanitizer uses more real memory than a native run. At the default 110*67e74705SXin Li settings the memory overhead is 5x plus 1Mb per each thread. Settings with 3x 111*67e74705SXin Li (less accurate analysis) and 9x (more accurate analysis) overhead are also 112*67e74705SXin Li available. 113*67e74705SXin Li* ThreadSanitizer maps (but does not reserve) a lot of virtual address space. 114*67e74705SXin Li This means that tools like ``ulimit`` may not work as usually expected. 115*67e74705SXin Li* Libc/libstdc++ static linking is not supported. 116*67e74705SXin Li* Non-position-independent executables are not supported. Therefore, the 117*67e74705SXin Li ``fsanitize=thread`` flag will cause Clang to act as though the ``-fPIE`` 118*67e74705SXin Li flag had been supplied if compiling without ``-fPIC``, and as though the 119*67e74705SXin Li ``-pie`` flag had been supplied if linking an executable. 120*67e74705SXin Li 121*67e74705SXin LiCurrent Status 122*67e74705SXin Li-------------- 123*67e74705SXin Li 124*67e74705SXin LiThreadSanitizer is in beta stage. It is known to work on large C++ programs 125*67e74705SXin Liusing pthreads, but we do not promise anything (yet). C++11 threading is 126*67e74705SXin Lisupported with llvm libc++. The test suite is integrated into CMake build 127*67e74705SXin Liand can be run with ``make check-tsan`` command. 128*67e74705SXin Li 129*67e74705SXin LiWe are actively working on enhancing the tool --- stay tuned. Any help, 130*67e74705SXin Liespecially in the form of minimized standalone tests is more than welcome. 131*67e74705SXin Li 132*67e74705SXin LiMore Information 133*67e74705SXin Li---------------- 134*67e74705SXin Li`<https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual>`_ 135