tsan_interface.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //===-- tsan_interface.h ----------------------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file is a part of ThreadSanitizer (TSan), a race detector.
  11. //
  12. // Public interface header for TSan.
  13. //===----------------------------------------------------------------------===//
  14. #ifndef SANITIZER_TSAN_INTERFACE_H
  15. #define SANITIZER_TSAN_INTERFACE_H
  16. #include <sanitizer/common_interface_defs.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. // __tsan_release establishes a happens-before relation with a preceding
  21. // __tsan_acquire on the same address.
  22. void __tsan_acquire(void *addr);
  23. void __tsan_release(void *addr);
  24. // Annotations for custom mutexes.
  25. // The annotations allow to get better reports (with sets of locked mutexes),
  26. // detect more types of bugs (e.g. mutex misuses, races between lock/unlock and
  27. // destruction and potential deadlocks) and improve precision and performance
  28. // (by ignoring individual atomic operations in mutex code). However, the
  29. // downside is that annotated mutex code itself is not checked for correctness.
  30. // Mutex creation flags are passed to __tsan_mutex_create annotation.
  31. // If mutex has no constructor and __tsan_mutex_create is not called,
  32. // the flags may be passed to __tsan_mutex_pre_lock/__tsan_mutex_post_lock
  33. // annotations.
  34. // Mutex has static storage duration and no-op constructor and destructor.
  35. // This effectively makes tsan ignore destroy annotation.
  36. const unsigned __tsan_mutex_linker_init = 1 << 0;
  37. // Mutex is write reentrant.
  38. const unsigned __tsan_mutex_write_reentrant = 1 << 1;
  39. // Mutex is read reentrant.
  40. const unsigned __tsan_mutex_read_reentrant = 1 << 2;
  41. // Mutex does not have static storage duration, and must not be used after
  42. // its destructor runs. The opposite of __tsan_mutex_linker_init.
  43. // If this flag is passed to __tsan_mutex_destroy, then the destruction
  44. // is ignored unless this flag was previously set on the mutex.
  45. const unsigned __tsan_mutex_not_static = 1 << 8;
  46. // Mutex operation flags:
  47. // Denotes read lock operation.
  48. const unsigned __tsan_mutex_read_lock = 1 << 3;
  49. // Denotes try lock operation.
  50. const unsigned __tsan_mutex_try_lock = 1 << 4;
  51. // Denotes that a try lock operation has failed to acquire the mutex.
  52. const unsigned __tsan_mutex_try_lock_failed = 1 << 5;
  53. // Denotes that the lock operation acquires multiple recursion levels.
  54. // Number of levels is passed in recursion parameter.
  55. // This is useful for annotation of e.g. Java builtin monitors,
  56. // for which wait operation releases all recursive acquisitions of the mutex.
  57. const unsigned __tsan_mutex_recursive_lock = 1 << 6;
  58. // Denotes that the unlock operation releases all recursion levels.
  59. // Number of released levels is returned and later must be passed to
  60. // the corresponding __tsan_mutex_post_lock annotation.
  61. const unsigned __tsan_mutex_recursive_unlock = 1 << 7;
  62. // Annotate creation of a mutex.
  63. // Supported flags: mutex creation flags.
  64. void __tsan_mutex_create(void *addr, unsigned flags);
  65. // Annotate destruction of a mutex.
  66. // Supported flags:
  67. // - __tsan_mutex_linker_init
  68. // - __tsan_mutex_not_static
  69. void __tsan_mutex_destroy(void *addr, unsigned flags);
  70. // Annotate start of lock operation.
  71. // Supported flags:
  72. // - __tsan_mutex_read_lock
  73. // - __tsan_mutex_try_lock
  74. // - all mutex creation flags
  75. void __tsan_mutex_pre_lock(void *addr, unsigned flags);
  76. // Annotate end of lock operation.
  77. // Supported flags:
  78. // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_lock)
  79. // - __tsan_mutex_try_lock (must match __tsan_mutex_pre_lock)
  80. // - __tsan_mutex_try_lock_failed
  81. // - __tsan_mutex_recursive_lock
  82. // - all mutex creation flags
  83. void __tsan_mutex_post_lock(void *addr, unsigned flags, int recursion);
  84. // Annotate start of unlock operation.
  85. // Supported flags:
  86. // - __tsan_mutex_read_lock
  87. // - __tsan_mutex_recursive_unlock
  88. int __tsan_mutex_pre_unlock(void *addr, unsigned flags);
  89. // Annotate end of unlock operation.
  90. // Supported flags:
  91. // - __tsan_mutex_read_lock (must match __tsan_mutex_pre_unlock)
  92. void __tsan_mutex_post_unlock(void *addr, unsigned flags);
  93. // Annotate start/end of notify/signal/broadcast operation.
  94. // Supported flags: none.
  95. void __tsan_mutex_pre_signal(void *addr, unsigned flags);
  96. void __tsan_mutex_post_signal(void *addr, unsigned flags);
  97. // Annotate start/end of a region of code where lock/unlock/signal operation
  98. // diverts to do something else unrelated to the mutex. This can be used to
  99. // annotate, for example, calls into cooperative scheduler or contention
  100. // profiling code.
  101. // These annotations must be called only from within
  102. // __tsan_mutex_pre/post_lock, __tsan_mutex_pre/post_unlock,
  103. // __tsan_mutex_pre/post_signal regions.
  104. // Supported flags: none.
  105. void __tsan_mutex_pre_divert(void *addr, unsigned flags);
  106. void __tsan_mutex_post_divert(void *addr, unsigned flags);
  107. // External race detection API.
  108. // Can be used by non-instrumented libraries to detect when their objects are
  109. // being used in an unsafe manner.
  110. // - __tsan_external_read/__tsan_external_write annotates the logical reads
  111. // and writes of the object at the specified address. 'caller_pc' should
  112. // be the PC of the library user, which the library can obtain with e.g.
  113. // `__builtin_return_address(0)`.
  114. // - __tsan_external_register_tag registers a 'tag' with the specified name,
  115. // which is later used in read/write annotations to denote the object type
  116. // - __tsan_external_assign_tag can optionally mark a heap object with a tag
  117. void *__tsan_external_register_tag(const char *object_type);
  118. void __tsan_external_register_header(void *tag, const char *header);
  119. void __tsan_external_assign_tag(void *addr, void *tag);
  120. void __tsan_external_read(void *addr, void *caller_pc, void *tag);
  121. void __tsan_external_write(void *addr, void *caller_pc, void *tag);
  122. #ifdef __cplusplus
  123. } // extern "C"
  124. #endif
  125. #endif // SANITIZER_TSAN_INTERFACE_H