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