tsan_interface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 SANITIZER_CDECL __tsan_acquire(void *addr);
  22. void SANITIZER_CDECL __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 SANITIZER_CDECL __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 SANITIZER_CDECL __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 SANITIZER_CDECL __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 SANITIZER_CDECL __tsan_mutex_post_lock(void *addr, unsigned flags,
  88. int recursion);
  89. // Annotate start of unlock operation.
  90. // Supported flags:
  91. // - __tsan_mutex_read_lock
  92. // - __tsan_mutex_recursive_unlock
  93. int SANITIZER_CDECL __tsan_mutex_pre_unlock(void *addr, unsigned flags);
  94. // Annotate end of unlock operation.
  95. // Supported flags:
  96. // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_unlock)
  97. void SANITIZER_CDECL __tsan_mutex_post_unlock(void *addr, unsigned flags);
  98. // Annotate start/end of notify/signal/broadcast operation.
  99. // Supported flags: none.
  100. void SANITIZER_CDECL __tsan_mutex_pre_signal(void *addr, unsigned flags);
  101. void SANITIZER_CDECL __tsan_mutex_post_signal(void *addr, unsigned flags);
  102. // Annotate start/end of a region of code where lock/unlock/signal operation
  103. // diverts to do something else unrelated to the mutex. This can be used to
  104. // annotate, for example, calls into cooperative scheduler or contention
  105. // profiling code.
  106. // These annotations must be called only from within
  107. // __tsan_mutex_pre/post_lock, __tsan_mutex_pre/post_unlock,
  108. // __tsan_mutex_pre/post_signal regions.
  109. // Supported flags: none.
  110. void SANITIZER_CDECL __tsan_mutex_pre_divert(void *addr, unsigned flags);
  111. void SANITIZER_CDECL __tsan_mutex_post_divert(void *addr, unsigned flags);
  112. // Check that the current thread does not hold any mutexes,
  113. // report a bug report otherwise.
  114. void SANITIZER_CDECL __tsan_check_no_mutexes_held();
  115. // External race detection API.
  116. // Can be used by non-instrumented libraries to detect when their objects are
  117. // being used in an unsafe manner.
  118. // - __tsan_external_read/__tsan_external_write annotates the logical reads
  119. // and writes of the object at the specified address. 'caller_pc' should
  120. // be the PC of the library user, which the library can obtain with e.g.
  121. // `__builtin_return_address(0)`.
  122. // - __tsan_external_register_tag registers a 'tag' with the specified name,
  123. // which is later used in read/write annotations to denote the object type
  124. // - __tsan_external_assign_tag can optionally mark a heap object with a tag
  125. void *SANITIZER_CDECL __tsan_external_register_tag(const char *object_type);
  126. void SANITIZER_CDECL __tsan_external_register_header(void *tag,
  127. const char *header);
  128. void SANITIZER_CDECL __tsan_external_assign_tag(void *addr, void *tag);
  129. void SANITIZER_CDECL __tsan_external_read(void *addr, void *caller_pc,
  130. void *tag);
  131. void SANITIZER_CDECL __tsan_external_write(void *addr, void *caller_pc,
  132. void *tag);
  133. // Fiber switching API.
  134. // - TSAN context for fiber can be created by __tsan_create_fiber
  135. // and freed by __tsan_destroy_fiber.
  136. // - TSAN context of current fiber or thread can be obtained
  137. // by calling __tsan_get_current_fiber.
  138. // - __tsan_switch_to_fiber should be called immediately before switch
  139. // to fiber, such as call of swapcontext.
  140. // - Fiber name can be set by __tsan_set_fiber_name.
  141. void *SANITIZER_CDECL __tsan_get_current_fiber(void);
  142. void *SANITIZER_CDECL __tsan_create_fiber(unsigned flags);
  143. void SANITIZER_CDECL __tsan_destroy_fiber(void *fiber);
  144. void SANITIZER_CDECL __tsan_switch_to_fiber(void *fiber, unsigned flags);
  145. void SANITIZER_CDECL __tsan_set_fiber_name(void *fiber, const char *name);
  146. // Flags for __tsan_switch_to_fiber:
  147. // Do not establish a happens-before relation between fibers
  148. static const unsigned __tsan_switch_to_fiber_no_sync = 1 << 0;
  149. // User-provided callback invoked on TSan initialization.
  150. void SANITIZER_CDECL __tsan_on_initialize();
  151. // User-provided callback invoked on TSan shutdown.
  152. // `failed` - Nonzero if TSan did detect issues, zero otherwise.
  153. // Return `0` if TSan should exit as if no issues were detected. Return nonzero
  154. // if TSan should exit as if issues were detected.
  155. int SANITIZER_CDECL __tsan_on_finalize(int failed);
  156. // Release TSan internal memory in a best-effort manner.
  157. void SANITIZER_CDECL __tsan_flush_memory();
  158. // User-provided default TSAN options.
  159. const char *SANITIZER_CDECL __tsan_default_options(void);
  160. // User-provided default TSAN suppressions.
  161. const char *SANITIZER_CDECL __tsan_default_suppressions(void);
  162. /// Returns a report's description.
  163. ///
  164. /// Returns a report's description (issue type), number of duplicate issues
  165. /// found, counts of array data (stack traces, memory operations, locations,
  166. /// mutexes, threads, unique thread IDs) and a stack trace of a <c>sleep()</c>
  167. /// call (if one was involved in the issue).
  168. ///
  169. /// \param report Opaque pointer to the current report.
  170. /// \param[out] description Report type description.
  171. /// \param[out] count Count of duplicate issues.
  172. /// \param[out] stack_count Count of stack traces.
  173. /// \param[out] mop_count Count of memory operations.
  174. /// \param[out] loc_count Count of locations.
  175. /// \param[out] mutex_count Count of mutexes.
  176. /// \param[out] thread_count Count of threads.
  177. /// \param[out] unique_tid_count Count of unique thread IDs.
  178. /// \param sleep_trace A buffer to store the stack trace of a <c>sleep()</c>
  179. /// call.
  180. /// \param trace_size Size in bytes of the trace buffer.
  181. /// \returns Returns 1 if successful, 0 if not.
  182. int SANITIZER_CDECL __tsan_get_report_data(
  183. void *report, const char **description, int *count, int *stack_count,
  184. int *mop_count, int *loc_count, int *mutex_count, int *thread_count,
  185. int *unique_tid_count, void **sleep_trace, unsigned long trace_size);
  186. /// Returns information about stack traces included in the report.
  187. ///
  188. /// \param report Opaque pointer to the current report.
  189. /// \param idx Index to the report's stacks.
  190. /// \param trace A buffer to store the stack trace.
  191. /// \param trace_size Size in bytes of the trace buffer.
  192. /// \returns Returns 1 if successful, 0 if not.
  193. int SANITIZER_CDECL __tsan_get_report_stack(void *report, unsigned long idx,
  194. void **trace,
  195. unsigned long trace_size);
  196. /// Returns information about memory operations included in the report.
  197. ///
  198. /// \param report Opaque pointer to the current report.
  199. /// \param idx Index to the report's memory operations.
  200. /// \param[out] tid Thread ID of the memory operation.
  201. /// \param[out] addr Address of the memory operation.
  202. /// \param[out] size Size of the memory operation.
  203. /// \param[out] write Write flag of the memory operation.
  204. /// \param[out] atomic Atomicity flag of the memory operation.
  205. /// \param trace A buffer to store the stack trace.
  206. /// \param trace_size Size in bytes of the trace buffer.
  207. /// \returns Returns 1 if successful, 0 if not.
  208. int SANITIZER_CDECL __tsan_get_report_mop(void *report, unsigned long idx,
  209. int *tid, void **addr, int *size,
  210. int *write, int *atomic, void **trace,
  211. unsigned long trace_size);
  212. /// Returns information about locations included in the report.
  213. ///
  214. /// \param report Opaque pointer to the current report.
  215. /// \param idx Index to the report's locations.
  216. /// \param[out] type Type of the location.
  217. /// \param[out] addr Address of the location.
  218. /// \param[out] start Start of the location.
  219. /// \param[out] size Size of the location.
  220. /// \param[out] tid Thread ID of the location.
  221. /// \param[out] fd File descriptor of the location.
  222. /// \param[out] suppressable Suppressable flag.
  223. /// \param trace A buffer to store the stack trace.
  224. /// \param trace_size Size in bytes of the trace buffer.
  225. /// \returns Returns 1 if successful, 0 if not.
  226. int SANITIZER_CDECL __tsan_get_report_loc(void *report, unsigned long idx,
  227. const char **type, void **addr,
  228. void **start, unsigned long *size,
  229. int *tid, int *fd, int *suppressable,
  230. void **trace,
  231. unsigned long trace_size);
  232. /// Returns information about mutexes included in the report.
  233. ///
  234. /// \param report Opaque pointer to the current report.
  235. /// \param idx Index to the report's mutexes.
  236. /// \param[out] mutex_id Id of the mutex.
  237. /// \param[out] addr Address of the mutex.
  238. /// \param[out] destroyed Destroyed mutex flag.
  239. /// \param trace A buffer to store the stack trace.
  240. /// \param trace_size Size in bytes of the trace buffer.
  241. /// \returns Returns 1 if successful, 0 if not.
  242. int SANITIZER_CDECL __tsan_get_report_mutex(void *report, unsigned long idx,
  243. uint64_t *mutex_id, void **addr,
  244. int *destroyed, void **trace,
  245. unsigned long trace_size);
  246. /// Returns information about threads included in the report.
  247. ///
  248. /// \param report Opaque pointer to the current report.
  249. /// \param idx Index to the report's threads.
  250. /// \param[out] tid Thread ID of the thread.
  251. /// \param[out] os_id Operating system's ID of the thread.
  252. /// \param[out] running Running flag of the thread.
  253. /// \param[out] name Name of the thread.
  254. /// \param[out] parent_tid ID of the parent thread.
  255. /// \param trace A buffer to store the stack trace.
  256. /// \param trace_size Size in bytes of the trace buffer.
  257. /// \returns Returns 1 if successful, 0 if not.
  258. int SANITIZER_CDECL __tsan_get_report_thread(void *report, unsigned long idx,
  259. int *tid, uint64_t *os_id,
  260. int *running, const char **name,
  261. int *parent_tid, void **trace,
  262. unsigned long trace_size);
  263. /// Returns information about unique thread IDs included in the report.
  264. ///
  265. /// \param report Opaque pointer to the current report.
  266. /// \param idx Index to the report's unique thread IDs.
  267. /// \param[out] tid Unique thread ID of the report.
  268. /// \returns Returns 1 if successful, 0 if not.
  269. int SANITIZER_CDECL __tsan_get_report_unique_tid(void *report,
  270. unsigned long idx, int *tid);
  271. /// Returns the current report.
  272. ///
  273. /// If TSan is currently reporting a detected issue on the current thread,
  274. /// returns an opaque pointer to the current report. Otherwise returns NULL.
  275. /// \returns An opaque pointer to the current report. Otherwise returns NULL.
  276. void *SANITIZER_CDECL __tsan_get_current_report();
  277. #ifdef __cplusplus
  278. } // extern "C"
  279. #endif
  280. #endif // SANITIZER_TSAN_INTERFACE_H