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