tsan_interface.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //===-- tsan_interface.h ----------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. // Public interface header for TSan.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_TSAN_INTERFACE_H
  14. #define SANITIZER_TSAN_INTERFACE_H
  15. #include <sanitizer/common_interface_defs.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. // __tsan_release establishes a happens-before relation with a preceding
  20. // __tsan_acquire on the same address.
  21. void __tsan_acquire(void *addr);
  22. void __tsan_release(void *addr);
  23. // Annotations for custom mutexes.
  24. // The annotations allow to get better reports (with sets of locked mutexes),
  25. // detect more types of bugs (e.g. mutex misuses, races between lock/unlock and
  26. // destruction and potential deadlocks) and improve precision and performance
  27. // (by ignoring individual atomic operations in mutex code). However, the
  28. // downside is that annotated mutex code itself is not checked for correctness.
  29. // Mutex creation flags are passed to __tsan_mutex_create annotation.
  30. // If mutex has no constructor and __tsan_mutex_create is not called,
  31. // the flags may be passed to __tsan_mutex_pre_lock/__tsan_mutex_post_lock
  32. // annotations.
  33. // Mutex has static storage duration and no-op constructor and destructor.
  34. // This effectively makes tsan ignore destroy annotation.
  35. static const unsigned __tsan_mutex_linker_init = 1 << 0;
  36. // Mutex is write reentrant.
  37. static const unsigned __tsan_mutex_write_reentrant = 1 << 1;
  38. // Mutex is read reentrant.
  39. static const unsigned __tsan_mutex_read_reentrant = 1 << 2;
  40. // Mutex does not have static storage duration, and must not be used after
  41. // its destructor runs. The opposite of __tsan_mutex_linker_init.
  42. // If this flag is passed to __tsan_mutex_destroy, then the destruction
  43. // is ignored unless this flag was previously set on the mutex.
  44. static const unsigned __tsan_mutex_not_static = 1 << 8;
  45. // Mutex operation flags:
  46. // Denotes read lock operation.
  47. static const unsigned __tsan_mutex_read_lock = 1 << 3;
  48. // Denotes try lock operation.
  49. static const unsigned __tsan_mutex_try_lock = 1 << 4;
  50. // Denotes that a try lock operation has failed to acquire the mutex.
  51. static const unsigned __tsan_mutex_try_lock_failed = 1 << 5;
  52. // Denotes that the lock operation acquires multiple recursion levels.
  53. // Number of levels is passed in recursion parameter.
  54. // This is useful for annotation of e.g. Java builtin monitors,
  55. // for which wait operation releases all recursive acquisitions of the mutex.
  56. static const unsigned __tsan_mutex_recursive_lock = 1 << 6;
  57. // Denotes that the unlock operation releases all recursion levels.
  58. // Number of released levels is returned and later must be passed to
  59. // the corresponding __tsan_mutex_post_lock annotation.
  60. static const unsigned __tsan_mutex_recursive_unlock = 1 << 7;
  61. // Convenient composed constants.
  62. static const unsigned __tsan_mutex_try_read_lock =
  63. __tsan_mutex_read_lock | __tsan_mutex_try_lock;
  64. static const unsigned __tsan_mutex_try_read_lock_failed =
  65. __tsan_mutex_try_read_lock | __tsan_mutex_try_lock_failed;
  66. // Annotate creation of a mutex.
  67. // Supported flags: mutex creation flags.
  68. void __tsan_mutex_create(void *addr, unsigned flags);
  69. // Annotate destruction of a mutex.
  70. // Supported flags:
  71. // - __tsan_mutex_linker_init
  72. // - __tsan_mutex_not_static
  73. void __tsan_mutex_destroy(void *addr, unsigned flags);
  74. // Annotate start of lock operation.
  75. // Supported flags:
  76. // - __tsan_mutex_read_lock
  77. // - __tsan_mutex_try_lock
  78. // - all mutex creation flags
  79. void __tsan_mutex_pre_lock(void *addr, unsigned flags);
  80. // Annotate end of lock operation.
  81. // Supported flags:
  82. // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_lock)
  83. // - __tsan_mutex_try_lock (must match __tsan_mutex_pre_lock)
  84. // - __tsan_mutex_try_lock_failed
  85. // - __tsan_mutex_recursive_lock
  86. // - all mutex creation flags
  87. void __tsan_mutex_post_lock(void *addr, unsigned flags, int recursion);
  88. // Annotate start of unlock operation.
  89. // Supported flags:
  90. // - __tsan_mutex_read_lock
  91. // - __tsan_mutex_recursive_unlock
  92. int __tsan_mutex_pre_unlock(void *addr, unsigned flags);
  93. // Annotate end of unlock operation.
  94. // Supported flags:
  95. // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_unlock)
  96. void __tsan_mutex_post_unlock(void *addr, unsigned flags);
  97. // Annotate start/end of notify/signal/broadcast operation.
  98. // Supported flags: none.
  99. void __tsan_mutex_pre_signal(void *addr, unsigned flags);
  100. void __tsan_mutex_post_signal(void *addr, unsigned flags);
  101. // Annotate start/end of a region of code where lock/unlock/signal operation
  102. // diverts to do something else unrelated to the mutex. This can be used to
  103. // annotate, for example, calls into cooperative scheduler or contention
  104. // profiling code.
  105. // These annotations must be called only from within
  106. // __tsan_mutex_pre/post_lock, __tsan_mutex_pre/post_unlock,
  107. // __tsan_mutex_pre/post_signal regions.
  108. // Supported flags: none.
  109. void __tsan_mutex_pre_divert(void *addr, unsigned flags);
  110. void __tsan_mutex_post_divert(void *addr, unsigned flags);
  111. // External race detection API.
  112. // Can be used by non-instrumented libraries to detect when their objects are
  113. // being used in an unsafe manner.
  114. // - __tsan_external_read/__tsan_external_write annotates the logical reads
  115. // and writes of the object at the specified address. 'caller_pc' should
  116. // be the PC of the library user, which the library can obtain with e.g.
  117. // `__builtin_return_address(0)`.
  118. // - __tsan_external_register_tag registers a 'tag' with the specified name,
  119. // which is later used in read/write annotations to denote the object type
  120. // - __tsan_external_assign_tag can optionally mark a heap object with a tag
  121. void *__tsan_external_register_tag(const char *object_type);
  122. void __tsan_external_register_header(void *tag, const char *header);
  123. void __tsan_external_assign_tag(void *addr, void *tag);
  124. void __tsan_external_read(void *addr, void *caller_pc, void *tag);
  125. void __tsan_external_write(void *addr, void *caller_pc, void *tag);
  126. // Fiber switching API.
  127. // - TSAN context for fiber can be created by __tsan_create_fiber
  128. // and freed by __tsan_destroy_fiber.
  129. // - TSAN context of current fiber or thread can be obtained
  130. // by calling __tsan_get_current_fiber.
  131. // - __tsan_switch_to_fiber should be called immediately before switch
  132. // to fiber, such as call of swapcontext.
  133. // - Fiber name can be set by __tsan_set_fiber_name.
  134. void *__tsan_get_current_fiber(void);
  135. void *__tsan_create_fiber(unsigned flags);
  136. void __tsan_destroy_fiber(void *fiber);
  137. void __tsan_switch_to_fiber(void *fiber, unsigned flags);
  138. void __tsan_set_fiber_name(void *fiber, const char *name);
  139. // Flags for __tsan_switch_to_fiber:
  140. // Do not establish a happens-before relation between fibers
  141. static const unsigned __tsan_switch_to_fiber_no_sync = 1 << 0;
  142. // User-provided callback invoked on TSan initialization.
  143. void __tsan_on_initialize();
  144. // User-provided callback invoked on TSan shutdown.
  145. // `failed` - Nonzero if TSan did detect issues, zero otherwise.
  146. // Return `0` if TSan should exit as if no issues were detected. Return nonzero
  147. // if TSan should exit as if issues were detected.
  148. int __tsan_on_finalize(int failed);
  149. // Release TSan internal memory in a best-effort manner.
  150. void __tsan_flush_memory();
  151. #ifdef __cplusplus
  152. } // extern "C"
  153. #endif
  154. #endif // SANITIZER_TSAN_INTERFACE_H