ThreadSanitizer.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ThreadSanitizer
  2. ===============
  3. Introduction
  4. ------------
  5. NOTE: this document applies to the original Clang project, not the DirectX
  6. Compiler. It's made available for informational purposes only.
  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
  9. ThreadSanitizer is about **5x-15x**. Typical memory overhead introduced by
  10. ThreadSanitizer is about **5x-10x**.
  11. How to build
  12. ------------
  13. Build LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_.
  14. Supported Platforms
  15. -------------------
  16. ThreadSanitizer is supported on Linux x86_64 (tested on Ubuntu 12.04).
  17. Support for other 64-bit architectures is possible, contributions are welcome.
  18. Support for 32-bit platforms is problematic and is not planned.
  19. Usage
  20. -----
  21. Simply compile and link your program with ``-fsanitize=thread``. To get a
  22. reasonable performance add ``-O1`` or higher. Use ``-g`` to get file names
  23. and line numbers in the warning messages.
  24. Example:
  25. .. code-block:: c++
  26. % cat projects/compiler-rt/lib/tsan/lit_tests/tiny_race.c
  27. #include <pthread.h>
  28. int Global;
  29. void *Thread1(void *x) {
  30. Global = 42;
  31. return x;
  32. }
  33. int main() {
  34. pthread_t t;
  35. pthread_create(&t, NULL, Thread1, NULL);
  36. Global = 43;
  37. pthread_join(t, NULL);
  38. return Global;
  39. }
  40. $ clang -fsanitize=thread -g -O1 tiny_race.c
  41. If a bug is detected, the program will print an error message to stderr.
  42. Currently, ThreadSanitizer symbolizes its output using an external
  43. ``addr2line`` process (this will be fixed in future).
  44. .. code-block:: bash
  45. % ./a.out
  46. WARNING: ThreadSanitizer: data race (pid=19219)
  47. Write of size 4 at 0x7fcf47b21bc0 by thread T1:
  48. #0 Thread1 tiny_race.c:4 (exe+0x00000000a360)
  49. Previous write of size 4 at 0x7fcf47b21bc0 by main thread:
  50. #0 main tiny_race.c:10 (exe+0x00000000a3b4)
  51. Thread T1 (running) created at:
  52. #0 pthread_create tsan_interceptors.cc:705 (exe+0x00000000c790)
  53. #1 main tiny_race.c:9 (exe+0x00000000a3a4)
  54. ``__has_feature(thread_sanitizer)``
  55. ------------------------------------
  56. In some cases one may need to execute different code depending on whether
  57. ThreadSanitizer is enabled.
  58. :ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for
  59. this purpose.
  60. .. code-block:: c
  61. #if defined(__has_feature)
  62. # if __has_feature(thread_sanitizer)
  63. // code that builds only under ThreadSanitizer
  64. # endif
  65. #endif
  66. ``__attribute__((no_sanitize_thread))``
  67. -----------------------------------------------
  68. Some code should not be instrumented by ThreadSanitizer.
  69. One may use the function attribute
  70. :ref:`no_sanitize_thread <langext-thread_sanitizer>`
  71. to disable instrumentation of plain (non-atomic) loads/stores in a particular function.
  72. ThreadSanitizer still instruments such functions to avoid false positives and
  73. provide meaningful stack traces.
  74. This attribute may not be
  75. supported by other compilers, so we suggest to use it together with
  76. ``__has_feature(thread_sanitizer)``.
  77. Blacklist
  78. ---------
  79. ThreadSanitizer supports ``src`` and ``fun`` entity types in
  80. :doc:`SanitizerSpecialCaseList`, that can be used to suppress data race reports in
  81. the specified source files or functions. Unlike functions marked with
  82. :ref:`no_sanitize_thread <langext-thread_sanitizer>` attribute,
  83. blacklisted functions are not instrumented at all. This can lead to false positives
  84. due to missed synchronization via atomic operations and missed stack frames in reports.
  85. Limitations
  86. -----------
  87. * ThreadSanitizer uses more real memory than a native run. At the default
  88. settings the memory overhead is 5x plus 1Mb per each thread. Settings with 3x
  89. (less accurate analysis) and 9x (more accurate analysis) overhead are also
  90. available.
  91. * ThreadSanitizer maps (but does not reserve) a lot of virtual address space.
  92. This means that tools like ``ulimit`` may not work as usually expected.
  93. * Libc/libstdc++ static linking is not supported.
  94. * Non-position-independent executables are not supported. Therefore, the
  95. ``fsanitize=thread`` flag will cause Clang to act as though the ``-fPIE``
  96. flag had been supplied if compiling without ``-fPIC``, and as though the
  97. ``-pie`` flag had been supplied if linking an executable.
  98. Current Status
  99. --------------
  100. ThreadSanitizer is in beta stage. It is known to work on large C++ programs
  101. using pthreads, but we do not promise anything (yet). C++11 threading is
  102. supported with llvm libc++. The test suite is integrated into CMake build
  103. and can be run with ``make check-tsan`` command.
  104. We are actively working on enhancing the tool --- stay tuned. Any help,
  105. especially in the form of minimized standalone tests is more than welcome.
  106. More Information
  107. ----------------
  108. `http://code.google.com/p/thread-sanitizer <http://code.google.com/p/thread-sanitizer/>`_.