ThreadSanitizer.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ThreadSanitizer
  2. ===============
  3. Introduction
  4. ------------
  5. ThreadSanitizer is a tool that detects data races. It consists of a compiler
  6. instrumentation module and a run-time library. Typical slowdown introduced by
  7. ThreadSanitizer is about **5x-15x**. Typical memory overhead introduced by
  8. ThreadSanitizer is about **5x-10x**.
  9. How to build
  10. ------------
  11. Build LLVM/Clang with `CMake <https://llvm.org/docs/CMake.html>`_.
  12. Supported Platforms
  13. -------------------
  14. ThreadSanitizer is supported on the following OS:
  15. * Android aarch64, x86_64
  16. * Darwin arm64, x86_64
  17. * FreeBSD
  18. * Linux aarch64, x86_64, powerpc64, powerpc64le
  19. * NetBSD
  20. Support for other 64-bit architectures is possible, contributions are welcome.
  21. Support for 32-bit platforms is problematic and is not planned.
  22. Usage
  23. -----
  24. Simply compile and link your program with ``-fsanitize=thread``. To get a
  25. reasonable performance add ``-O1`` or higher. Use ``-g`` to get file names
  26. and line numbers in the warning messages.
  27. Example:
  28. .. code-block:: console
  29. % cat projects/compiler-rt/lib/tsan/lit_tests/tiny_race.c
  30. #include <pthread.h>
  31. int Global;
  32. void *Thread1(void *x) {
  33. Global = 42;
  34. return x;
  35. }
  36. int main() {
  37. pthread_t t;
  38. pthread_create(&t, NULL, Thread1, NULL);
  39. Global = 43;
  40. pthread_join(t, NULL);
  41. return Global;
  42. }
  43. $ clang -fsanitize=thread -g -O1 tiny_race.c
  44. If a bug is detected, the program will print an error message to stderr.
  45. Currently, ThreadSanitizer symbolizes its output using an external
  46. ``addr2line`` process (this will be fixed in future).
  47. .. code-block:: bash
  48. % ./a.out
  49. WARNING: ThreadSanitizer: data race (pid=19219)
  50. Write of size 4 at 0x7fcf47b21bc0 by thread T1:
  51. #0 Thread1 tiny_race.c:4 (exe+0x00000000a360)
  52. Previous write of size 4 at 0x7fcf47b21bc0 by main thread:
  53. #0 main tiny_race.c:10 (exe+0x00000000a3b4)
  54. Thread T1 (running) created at:
  55. #0 pthread_create tsan_interceptors.cc:705 (exe+0x00000000c790)
  56. #1 main tiny_race.c:9 (exe+0x00000000a3a4)
  57. ``__has_feature(thread_sanitizer)``
  58. ------------------------------------
  59. In some cases one may need to execute different code depending on whether
  60. ThreadSanitizer is enabled.
  61. :ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for
  62. this purpose.
  63. .. code-block:: c
  64. #if defined(__has_feature)
  65. # if __has_feature(thread_sanitizer)
  66. // code that builds only under ThreadSanitizer
  67. # endif
  68. #endif
  69. ``__attribute__((no_sanitize("thread")))``
  70. -----------------------------------------------
  71. Some code should not be instrumented by ThreadSanitizer. One may use the
  72. function attribute ``no_sanitize("thread")`` to disable instrumentation of plain
  73. (non-atomic) loads/stores in a particular function. ThreadSanitizer still
  74. instruments such functions to avoid false positives and provide meaningful stack
  75. traces. This attribute may not be supported by other compilers, so we suggest
  76. to use it together with ``__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
  81. in the specified source files or functions. Unlike functions marked with
  82. ``no_sanitize("thread")`` attribute, blacklisted functions are not instrumented
  83. at all. This can lead to false positives due to missed synchronization via
  84. 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. `<https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual>`_