blob: d1aeaa8a58c37f4cca59007ee7b1a019707e1fb8 [file] [log] [blame]
Sean Silva3872b462012-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 Vyukovc092cba2012-12-17 07:16:54 +00009ThreadSanitizer is about **5x-15x**. Typical memory overhead introduced by
10ThreadSanitizer is about **5x-10x**.
Sean Silva3872b462012-12-12 23:44:55 +000011
12How to build
13------------
14
Stephen Hines0e2c34f2015-03-23 12:09:02 -070015Build LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_.
Sean Silva3872b462012-12-12 23:44:55 +000016
17Supported Platforms
18-------------------
19
Stephen Hines651f13c2014-04-23 16:59:28 -070020ThreadSanitizer is supported on Linux x86_64 (tested on Ubuntu 12.04).
21Support for other 64-bit architectures is possible, contributions are welcome.
22Support for 32-bit platforms is problematic and is not planned.
Sean Silva3872b462012-12-12 23:44:55 +000023
24Usage
25-----
26
Peter Collingbourne52ca70d2013-04-09 04:35:11 +000027Simply compile and link your program with ``-fsanitize=thread``. To get a
28reasonable performance add ``-O1`` or higher. Use ``-g`` to get file names
29and line numbers in the warning messages.
Sean Silva3872b462012-12-12 23:44:55 +000030
31Example:
32
33.. code-block:: c++
34
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000035 % cat projects/compiler-rt/lib/tsan/lit_tests/tiny_race.c
Sean Silva3872b462012-12-12 23:44:55 +000036 #include <pthread.h>
37 int Global;
38 void *Thread1(void *x) {
39 Global = 42;
40 return x;
41 }
42 int main() {
43 pthread_t t;
44 pthread_create(&t, NULL, Thread1, NULL);
45 Global = 43;
46 pthread_join(t, NULL);
47 return Global;
48 }
49
Peter Collingbourne52ca70d2013-04-09 04:35:11 +000050 $ clang -fsanitize=thread -g -O1 tiny_race.c
Sean Silva3872b462012-12-12 23:44:55 +000051
52If a bug is detected, the program will print an error message to stderr.
53Currently, ThreadSanitizer symbolizes its output using an external
54``addr2line`` process (this will be fixed in future).
55
56.. code-block:: bash
57
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000058 % ./a.out
Sean Silva3872b462012-12-12 23:44:55 +000059 WARNING: ThreadSanitizer: data race (pid=19219)
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000060 Write of size 4 at 0x7fcf47b21bc0 by thread T1:
Sean Silva3872b462012-12-12 23:44:55 +000061 #0 Thread1 tiny_race.c:4 (exe+0x00000000a360)
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000062
Sean Silva3872b462012-12-12 23:44:55 +000063 Previous write of size 4 at 0x7fcf47b21bc0 by main thread:
64 #0 main tiny_race.c:10 (exe+0x00000000a3b4)
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000065
66 Thread T1 (running) created at:
67 #0 pthread_create tsan_interceptors.cc:705 (exe+0x00000000c790)
Sean Silva3872b462012-12-12 23:44:55 +000068 #1 main tiny_race.c:9 (exe+0x00000000a3a4)
69
Dmitry Vyukov728e2122012-12-17 08:52:05 +000070``__has_feature(thread_sanitizer)``
71------------------------------------
72
73In some cases one may need to execute different code depending on whether
74ThreadSanitizer is enabled.
75:ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for
76this purpose.
77
78.. code-block:: c
79
Kostya Serebryany85aee962013-02-26 06:58:27 +000080 #if defined(__has_feature)
81 # if __has_feature(thread_sanitizer)
Dmitry Vyukov728e2122012-12-17 08:52:05 +000082 // code that builds only under ThreadSanitizer
Kostya Serebryany85aee962013-02-26 06:58:27 +000083 # endif
Dmitry Vyukov728e2122012-12-17 08:52:05 +000084 #endif
85
Kostya Serebryany85aee962013-02-26 06:58:27 +000086``__attribute__((no_sanitize_thread))``
87-----------------------------------------------
88
89Some code should not be instrumented by ThreadSanitizer.
90One may use the function attribute
91:ref:`no_sanitize_thread <langext-thread_sanitizer>`
92to disable instrumentation of plain (non-atomic) loads/stores in a particular function.
Dmitry Vyukov39dc0162013-10-17 08:06:19 +000093ThreadSanitizer still instruments such functions to avoid false positives and
94provide meaningful stack traces.
Kostya Serebryany85aee962013-02-26 06:58:27 +000095This attribute may not be
96supported by other compilers, so we suggest to use it together with
Evgeniy Stepanovfa203cf2013-08-15 13:57:11 +000097``__has_feature(thread_sanitizer)``.
Kostya Serebryany85aee962013-02-26 06:58:27 +000098
Alexey Samsonov05654ff2013-08-07 08:23:32 +000099Blacklist
100---------
101
102ThreadSanitizer supports ``src`` and ``fun`` entity types in
103:doc:`SanitizerSpecialCaseList`, that can be used to suppress data race reports in
Dmitry Vyukov39dc0162013-10-17 08:06:19 +0000104the specified source files or functions. Unlike functions marked with
105:ref:`no_sanitize_thread <langext-thread_sanitizer>` attribute,
106blacklisted functions are not instrumented at all. This can lead to false positives
107due to missed synchronization via atomic operations and missed stack frames in reports.
Alexey Samsonov05654ff2013-08-07 08:23:32 +0000108
Sean Silva3872b462012-12-12 23:44:55 +0000109Limitations
110-----------
111
112* ThreadSanitizer uses more real memory than a native run. At the default
Dmitry Vyukovc092cba2012-12-17 07:16:54 +0000113 settings the memory overhead is 5x plus 1Mb per each thread. Settings with 3x
114 (less accurate analysis) and 9x (more accurate analysis) overhead are also
115 available.
Sean Silva3872b462012-12-12 23:44:55 +0000116* ThreadSanitizer maps (but does not reserve) a lot of virtual address space.
117 This means that tools like ``ulimit`` may not work as usually expected.
Dmitry Vyukovc092cba2012-12-17 07:16:54 +0000118* Libc/libstdc++ static linking is not supported.
Peter Collingbourne52ca70d2013-04-09 04:35:11 +0000119* Non-position-independent executables are not supported. Therefore, the
120 ``fsanitize=thread`` flag will cause Clang to act as though the ``-fPIE``
121 flag had been supplied if compiling without ``-fPIC``, and as though the
122 ``-pie`` flag had been supplied if linking an executable.
Sean Silva3872b462012-12-12 23:44:55 +0000123
124Current Status
125--------------
126
Dmitry Vyukovc092cba2012-12-17 07:16:54 +0000127ThreadSanitizer is in beta stage. It is known to work on large C++ programs
128using pthreads, but we do not promise anything (yet). C++11 threading is
Dmitry Vyukov61dde192012-12-17 13:07:35 +0000129supported with llvm libc++. The test suite is integrated into CMake build
Dmitry Vyukovc092cba2012-12-17 07:16:54 +0000130and can be run with ``make check-tsan`` command.
Sean Silva3872b462012-12-12 23:44:55 +0000131
132We are actively working on enhancing the tool --- stay tuned. Any help,
133especially in the form of minimized standalone tests is more than welcome.
134
135More Information
136----------------
137`http://code.google.com/p/thread-sanitizer <http://code.google.com/p/thread-sanitizer/>`_.
138