Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 1 | ThreadSanitizer |
| 2 | =============== |
| 3 | |
| 4 | Introduction |
| 5 | ------------ |
| 6 | |
| 7 | ThreadSanitizer is a tool that detects data races. It consists of a compiler |
| 8 | instrumentation module and a run-time library. Typical slowdown introduced by |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 9 | ThreadSanitizer is about **5x-15x**. Typical memory overhead introduced by |
| 10 | ThreadSanitizer is about **5x-10x**. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 11 | |
| 12 | How to build |
| 13 | ------------ |
| 14 | |
| 15 | Follow the `Clang build instructions <../get_started.html>`_. CMake build is |
| 16 | supported. |
| 17 | |
| 18 | Supported Platforms |
| 19 | ------------------- |
| 20 | |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 21 | ThreadSanitizer is supported on Linux x86_64 (tested on Ubuntu 10.04 and 12.04). |
| 22 | Support for MacOS 10.7 (64-bit only) is planned for 2013. Support for 32-bit |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 23 | platforms is problematic and not yet planned. |
| 24 | |
| 25 | Usage |
| 26 | ----- |
| 27 | |
Peter Collingbourne | 52ca70d | 2013-04-09 04:35:11 +0000 | [diff] [blame] | 28 | Simply compile and link your program with ``-fsanitize=thread``. To get a |
| 29 | reasonable performance add ``-O1`` or higher. Use ``-g`` to get file names |
| 30 | and line numbers in the warning messages. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 31 | |
| 32 | Example: |
| 33 | |
| 34 | .. code-block:: c++ |
| 35 | |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 36 | % cat projects/compiler-rt/lib/tsan/lit_tests/tiny_race.c |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 37 | #include <pthread.h> |
| 38 | int Global; |
| 39 | void *Thread1(void *x) { |
| 40 | Global = 42; |
| 41 | return x; |
| 42 | } |
| 43 | int main() { |
| 44 | pthread_t t; |
| 45 | pthread_create(&t, NULL, Thread1, NULL); |
| 46 | Global = 43; |
| 47 | pthread_join(t, NULL); |
| 48 | return Global; |
| 49 | } |
| 50 | |
Peter Collingbourne | 52ca70d | 2013-04-09 04:35:11 +0000 | [diff] [blame] | 51 | $ clang -fsanitize=thread -g -O1 tiny_race.c |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 52 | |
| 53 | If a bug is detected, the program will print an error message to stderr. |
| 54 | Currently, ThreadSanitizer symbolizes its output using an external |
| 55 | ``addr2line`` process (this will be fixed in future). |
| 56 | |
| 57 | .. code-block:: bash |
| 58 | |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 59 | % ./a.out |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 60 | WARNING: ThreadSanitizer: data race (pid=19219) |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 61 | Write of size 4 at 0x7fcf47b21bc0 by thread T1: |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 62 | #0 Thread1 tiny_race.c:4 (exe+0x00000000a360) |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 63 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 64 | Previous write of size 4 at 0x7fcf47b21bc0 by main thread: |
| 65 | #0 main tiny_race.c:10 (exe+0x00000000a3b4) |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 66 | |
| 67 | Thread T1 (running) created at: |
| 68 | #0 pthread_create tsan_interceptors.cc:705 (exe+0x00000000c790) |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 69 | #1 main tiny_race.c:9 (exe+0x00000000a3a4) |
| 70 | |
Dmitry Vyukov | 728e212 | 2012-12-17 08:52:05 +0000 | [diff] [blame] | 71 | ``__has_feature(thread_sanitizer)`` |
| 72 | ------------------------------------ |
| 73 | |
| 74 | In some cases one may need to execute different code depending on whether |
| 75 | ThreadSanitizer is enabled. |
| 76 | :ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for |
| 77 | this purpose. |
| 78 | |
| 79 | .. code-block:: c |
| 80 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 81 | #if defined(__has_feature) |
| 82 | # if __has_feature(thread_sanitizer) |
Dmitry Vyukov | 728e212 | 2012-12-17 08:52:05 +0000 | [diff] [blame] | 83 | // code that builds only under ThreadSanitizer |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 84 | # endif |
Dmitry Vyukov | 728e212 | 2012-12-17 08:52:05 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 87 | ``__attribute__((no_sanitize_thread))`` |
| 88 | ----------------------------------------------- |
| 89 | |
| 90 | Some code should not be instrumented by ThreadSanitizer. |
| 91 | One may use the function attribute |
| 92 | :ref:`no_sanitize_thread <langext-thread_sanitizer>` |
| 93 | to disable instrumentation of plain (non-atomic) loads/stores in a particular function. |
| 94 | ThreadSanitizer may still instrument such functions to avoid false positives. |
| 95 | This attribute may not be |
| 96 | supported by other compilers, so we suggest to use it together with |
Evgeniy Stepanov | fa203cf | 2013-08-15 13:57:11 +0000 | [diff] [blame] | 97 | ``__has_feature(thread_sanitizer)``. |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 98 | |
Alexey Samsonov | 05654ff | 2013-08-07 08:23:32 +0000 | [diff] [blame] | 99 | Blacklist |
| 100 | --------- |
| 101 | |
| 102 | ThreadSanitizer supports ``src`` and ``fun`` entity types in |
| 103 | :doc:`SanitizerSpecialCaseList`, that can be used to suppress data race reports in |
| 104 | the specified source files or functions. |
| 105 | |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 106 | Limitations |
| 107 | ----------- |
| 108 | |
| 109 | * ThreadSanitizer uses more real memory than a native run. At the default |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 110 | settings the memory overhead is 5x plus 1Mb per each thread. Settings with 3x |
| 111 | (less accurate analysis) and 9x (more accurate analysis) overhead are also |
| 112 | available. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 113 | * ThreadSanitizer maps (but does not reserve) a lot of virtual address space. |
| 114 | This means that tools like ``ulimit`` may not work as usually expected. |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 115 | * Libc/libstdc++ static linking is not supported. |
Peter Collingbourne | 52ca70d | 2013-04-09 04:35:11 +0000 | [diff] [blame] | 116 | * Non-position-independent executables are not supported. Therefore, the |
| 117 | ``fsanitize=thread`` flag will cause Clang to act as though the ``-fPIE`` |
| 118 | flag had been supplied if compiling without ``-fPIC``, and as though the |
| 119 | ``-pie`` flag had been supplied if linking an executable. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 120 | |
| 121 | Current Status |
| 122 | -------------- |
| 123 | |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 124 | ThreadSanitizer is in beta stage. It is known to work on large C++ programs |
| 125 | using pthreads, but we do not promise anything (yet). C++11 threading is |
Dmitry Vyukov | 61dde19 | 2012-12-17 13:07:35 +0000 | [diff] [blame] | 126 | supported with llvm libc++. The test suite is integrated into CMake build |
Dmitry Vyukov | c092cba | 2012-12-17 07:16:54 +0000 | [diff] [blame] | 127 | and can be run with ``make check-tsan`` command. |
Sean Silva | 3872b46 | 2012-12-12 23:44:55 +0000 | [diff] [blame] | 128 | |
| 129 | We are actively working on enhancing the tool --- stay tuned. Any help, |
| 130 | especially in the form of minimized standalone tests is more than welcome. |
| 131 | |
| 132 | More Information |
| 133 | ---------------- |
| 134 | `http://code.google.com/p/thread-sanitizer <http://code.google.com/p/thread-sanitizer/>`_. |
| 135 | |