blob: 98d5307d824f9e9eb74cc35cfdef422ece2ce359 [file] [log] [blame]
Sean Silva709c44d2012-12-12 23:44:55 +00001ThreadSanitizer
2===============
3
4Introduction
5------------
6
7ThreadSanitizer is a tool that detects data races. It consists of a compiler
8instrumentation module and a run-time library. Typical slowdown introduced by
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +00009ThreadSanitizer is about **5x-15x**. Typical memory overhead introduced by
10ThreadSanitizer is about **5x-10x**.
Sean Silva709c44d2012-12-12 23:44:55 +000011
12How to build
13------------
14
Sylvestre Ledrubc5c3f52018-11-04 17:02:00 +000015Build LLVM/Clang with `CMake <https://llvm.org/docs/CMake.html>`_.
Sean Silva709c44d2012-12-12 23:44:55 +000016
17Supported Platforms
18-------------------
19
David Carlier59a339ab2018-07-25 13:55:06 +000020ThreadSanitizer is supported on the following OS:
21
Vitaly Bukab0bfac42019-03-05 21:10:42 +000022* Android aarch64, x86_64
23* Darwin arm64, x86_64
Vitaly Bukac3a0cd22019-03-05 20:53:34 +000024* FreeBSD
Vitaly Bukab0bfac42019-03-05 21:10:42 +000025* Linux aarch64, x86_64, powerpc64, powerpc64le
David Carlier59a339ab2018-07-25 13:55:06 +000026* NetBSD
David Carlier68a9c7c2018-07-25 14:27:14 +000027
Kostya Serebryany870baf82014-01-31 10:49:34 +000028Support for other 64-bit architectures is possible, contributions are welcome.
29Support for 32-bit platforms is problematic and is not planned.
Sean Silva709c44d2012-12-12 23:44:55 +000030
31Usage
32-----
33
Peter Collingbourne54d770c2013-04-09 04:35:11 +000034Simply compile and link your program with ``-fsanitize=thread``. To get a
35reasonable performance add ``-O1`` or higher. Use ``-g`` to get file names
36and line numbers in the warning messages.
Sean Silva709c44d2012-12-12 23:44:55 +000037
38Example:
39
George Burgess IVbc8cc5ac2016-06-21 02:19:43 +000040.. code-block:: console
Sean Silva709c44d2012-12-12 23:44:55 +000041
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +000042 % cat projects/compiler-rt/lib/tsan/lit_tests/tiny_race.c
Sean Silva709c44d2012-12-12 23:44:55 +000043 #include <pthread.h>
44 int Global;
45 void *Thread1(void *x) {
46 Global = 42;
47 return x;
48 }
49 int main() {
50 pthread_t t;
51 pthread_create(&t, NULL, Thread1, NULL);
52 Global = 43;
53 pthread_join(t, NULL);
54 return Global;
55 }
56
Peter Collingbourne54d770c2013-04-09 04:35:11 +000057 $ clang -fsanitize=thread -g -O1 tiny_race.c
Sean Silva709c44d2012-12-12 23:44:55 +000058
59If a bug is detected, the program will print an error message to stderr.
60Currently, ThreadSanitizer symbolizes its output using an external
61``addr2line`` process (this will be fixed in future).
62
63.. code-block:: bash
64
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +000065 % ./a.out
Sean Silva709c44d2012-12-12 23:44:55 +000066 WARNING: ThreadSanitizer: data race (pid=19219)
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +000067 Write of size 4 at 0x7fcf47b21bc0 by thread T1:
Sean Silva709c44d2012-12-12 23:44:55 +000068 #0 Thread1 tiny_race.c:4 (exe+0x00000000a360)
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +000069
Sean Silva709c44d2012-12-12 23:44:55 +000070 Previous write of size 4 at 0x7fcf47b21bc0 by main thread:
71 #0 main tiny_race.c:10 (exe+0x00000000a3b4)
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +000072
73 Thread T1 (running) created at:
74 #0 pthread_create tsan_interceptors.cc:705 (exe+0x00000000c790)
Sean Silva709c44d2012-12-12 23:44:55 +000075 #1 main tiny_race.c:9 (exe+0x00000000a3a4)
76
Dmitry Vyukova53767e2012-12-17 08:52:05 +000077``__has_feature(thread_sanitizer)``
78------------------------------------
79
80In some cases one may need to execute different code depending on whether
81ThreadSanitizer is enabled.
82:ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for
83this purpose.
84
85.. code-block:: c
86
Kostya Serebryany4c0fc992013-02-26 06:58:27 +000087 #if defined(__has_feature)
88 # if __has_feature(thread_sanitizer)
Dmitry Vyukova53767e2012-12-17 08:52:05 +000089 // code that builds only under ThreadSanitizer
Kostya Serebryany4c0fc992013-02-26 06:58:27 +000090 # endif
Dmitry Vyukova53767e2012-12-17 08:52:05 +000091 #endif
92
Anna Zaks80de16f2016-10-27 21:38:44 +000093``__attribute__((no_sanitize("thread")))``
Kostya Serebryany4c0fc992013-02-26 06:58:27 +000094-----------------------------------------------
95
Saleem Abdulrasool7f66d752015-10-19 01:24:08 +000096Some code should not be instrumented by ThreadSanitizer. One may use the
Anna Zaks80de16f2016-10-27 21:38:44 +000097function attribute ``no_sanitize("thread")`` to disable instrumentation of plain
Saleem Abdulrasool7f66d752015-10-19 01:24:08 +000098(non-atomic) loads/stores in a particular function. ThreadSanitizer still
99instruments such functions to avoid false positives and provide meaningful stack
100traces. This attribute may not be supported by other compilers, so we suggest
101to use it together with ``__has_feature(thread_sanitizer)``.
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000102
Alexander Potapenko8300d522021-08-17 13:19:15 +0200103``__attribute__((disable_sanitizer_instrumentation))``
104--------------------------------------------------------
105
106The ``disable_sanitizer_instrumentation`` attribute can be applied to functions
107to prevent all kinds of instrumentation. As a result, it may introduce false
108positives and incorrect stack traces. Therefore, it should be used with care,
109and only if absolutely required; for example for certain code that cannot
110tolerate any instrumentation and resulting side-effects. This attribute
111overrides ``no_sanitize("thread")``.
112
Nico Weberd7ec48d2021-05-04 09:50:43 -0400113Ignorelist
114----------
Alexey Samsonov2de68332013-08-07 08:23:32 +0000115
116ThreadSanitizer supports ``src`` and ``fun`` entity types in
Saleem Abdulrasool7f66d752015-10-19 01:24:08 +0000117:doc:`SanitizerSpecialCaseList`, that can be used to suppress data race reports
118in the specified source files or functions. Unlike functions marked with
Nico Weberd7ec48d2021-05-04 09:50:43 -0400119``no_sanitize("thread")`` attribute, ignored functions are not instrumented
Anna Zaks80de16f2016-10-27 21:38:44 +0000120at all. This can lead to false positives due to missed synchronization via
121atomic operations and missed stack frames in reports.
Alexey Samsonov2de68332013-08-07 08:23:32 +0000122
Sean Silva709c44d2012-12-12 23:44:55 +0000123Limitations
124-----------
125
126* ThreadSanitizer uses more real memory than a native run. At the default
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +0000127 settings the memory overhead is 5x plus 1Mb per each thread. Settings with 3x
128 (less accurate analysis) and 9x (more accurate analysis) overhead are also
129 available.
Sean Silva709c44d2012-12-12 23:44:55 +0000130* ThreadSanitizer maps (but does not reserve) a lot of virtual address space.
131 This means that tools like ``ulimit`` may not work as usually expected.
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +0000132* Libc/libstdc++ static linking is not supported.
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000133* Non-position-independent executables are not supported. Therefore, the
134 ``fsanitize=thread`` flag will cause Clang to act as though the ``-fPIE``
135 flag had been supplied if compiling without ``-fPIC``, and as though the
136 ``-pie`` flag had been supplied if linking an executable.
Sean Silva709c44d2012-12-12 23:44:55 +0000137
138Current Status
139--------------
140
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +0000141ThreadSanitizer is in beta stage. It is known to work on large C++ programs
142using pthreads, but we do not promise anything (yet). C++11 threading is
Dmitry Vyukovca2d2e12012-12-17 13:07:35 +0000143supported with llvm libc++. The test suite is integrated into CMake build
Dmitry Vyukovbe66bf12012-12-17 07:16:54 +0000144and can be run with ``make check-tsan`` command.
Sean Silva709c44d2012-12-12 23:44:55 +0000145
146We are actively working on enhancing the tool --- stay tuned. Any help,
147especially in the form of minimized standalone tests is more than welcome.
148
149More Information
150----------------
Alexey Samsonov2e2469d2015-12-04 00:38:13 +0000151`<https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual>`_