dynamic_annotations.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // This file defines dynamic annotations for use with dynamic analysis tool
  15. // such as valgrind, PIN, etc.
  16. //
  17. // Dynamic annotation is a source code annotation that affects the generated
  18. // code (that is, the annotation is not a comment). Each such annotation is
  19. // attached to a particular instruction and/or to a particular object (address)
  20. // in the program.
  21. //
  22. // The annotations that should be used by users are macros in all upper-case
  23. // (e.g., ANNOTATE_THREAD_NAME).
  24. //
  25. // Actual implementation of these macros may differ depending on the dynamic
  26. // analysis tool being used.
  27. //
  28. // This file supports the following configurations:
  29. // - Dynamic Annotations enabled (with static thread-safety warnings disabled).
  30. // In this case, macros expand to functions implemented by Thread Sanitizer,
  31. // when building with TSan. When not provided an external implementation,
  32. // dynamic_annotations.cc provides no-op implementations.
  33. //
  34. // - Static Clang thread-safety warnings enabled.
  35. // When building with a Clang compiler that supports thread-safety warnings,
  36. // a subset of annotations can be statically-checked at compile-time. We
  37. // expand these macros to static-inline functions that can be analyzed for
  38. // thread-safety, but afterwards elided when building the final binary.
  39. //
  40. // - All annotations are disabled.
  41. // If neither Dynamic Annotations nor Clang thread-safety warnings are
  42. // enabled, then all annotation-macros expand to empty.
  43. #ifndef ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_
  44. #define ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_
  45. #include <stddef.h>
  46. #include "absl/base/config.h"
  47. // -------------------------------------------------------------------------
  48. // Decide which features are enabled
  49. #ifndef DYNAMIC_ANNOTATIONS_ENABLED
  50. #define DYNAMIC_ANNOTATIONS_ENABLED 0
  51. #endif
  52. #if defined(__clang__) && !defined(SWIG)
  53. #define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 1
  54. #endif
  55. #if DYNAMIC_ANNOTATIONS_ENABLED != 0
  56. #define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 1
  57. #define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 1
  58. #define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 1
  59. #define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
  60. #define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED 1
  61. #else
  62. #define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 0
  63. #define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 0
  64. #define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 0
  65. // Clang provides limited support for static thread-safety analysis through a
  66. // feature called Annotalysis. We configure macro-definitions according to
  67. // whether Annotalysis support is available. When running in opt-mode, GCC
  68. // will issue a warning, if these attributes are compiled. Only include them
  69. // when compiling using Clang.
  70. // ANNOTALYSIS_ENABLED == 1 when IGNORE_READ_ATTRIBUTE_ENABLED == 1
  71. #define ABSL_INTERNAL_ANNOTALYSIS_ENABLED \
  72. defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
  73. // Read/write annotations are enabled in Annotalysis mode; disabled otherwise.
  74. #define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED \
  75. ABSL_INTERNAL_ANNOTALYSIS_ENABLED
  76. #endif
  77. // Memory annotations are also made available to LLVM's Memory Sanitizer
  78. #if defined(ABSL_HAVE_MEMORY_SANITIZER) && !defined(__native_client__)
  79. #define ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED 1
  80. #endif
  81. #ifndef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
  82. #define ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED 0
  83. #endif
  84. #ifdef __cplusplus
  85. #define ABSL_INTERNAL_BEGIN_EXTERN_C extern "C" {
  86. #define ABSL_INTERNAL_END_EXTERN_C } // extern "C"
  87. #define ABSL_INTERNAL_GLOBAL_SCOPED(F) ::F
  88. #define ABSL_INTERNAL_STATIC_INLINE inline
  89. #else
  90. #define ABSL_INTERNAL_BEGIN_EXTERN_C // empty
  91. #define ABSL_INTERNAL_END_EXTERN_C // empty
  92. #define ABSL_INTERNAL_GLOBAL_SCOPED(F) F
  93. #define ABSL_INTERNAL_STATIC_INLINE static inline
  94. #endif
  95. // -------------------------------------------------------------------------
  96. // Define race annotations.
  97. #if ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 1
  98. // -------------------------------------------------------------
  99. // Annotations that suppress errors. It is usually better to express the
  100. // program's synchronization using the other annotations, but these can be used
  101. // when all else fails.
  102. // Report that we may have a benign race at `pointer`, with size
  103. // "sizeof(*(pointer))". `pointer` must be a non-void* pointer. Insert at the
  104. // point where `pointer` has been allocated, preferably close to the point
  105. // where the race happens. See also ANNOTATE_BENIGN_RACE_STATIC.
  106. #define ANNOTATE_BENIGN_RACE(pointer, description) \
  107. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
  108. (__FILE__, __LINE__, pointer, sizeof(*(pointer)), description)
  109. // Same as ANNOTATE_BENIGN_RACE(`address`, `description`), but applies to
  110. // the memory range [`address`, `address`+`size`).
  111. #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
  112. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
  113. (__FILE__, __LINE__, address, size, description)
  114. // Enable (`enable`!=0) or disable (`enable`==0) race detection for all threads.
  115. // This annotation could be useful if you want to skip expensive race analysis
  116. // during some period of program execution, e.g. during initialization.
  117. #define ANNOTATE_ENABLE_RACE_DETECTION(enable) \
  118. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateEnableRaceDetection) \
  119. (__FILE__, __LINE__, enable)
  120. // -------------------------------------------------------------
  121. // Annotations useful for debugging.
  122. // Report the current thread `name` to a race detector.
  123. #define ANNOTATE_THREAD_NAME(name) \
  124. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateThreadName)(__FILE__, __LINE__, name)
  125. // -------------------------------------------------------------
  126. // Annotations useful when implementing locks. They are not normally needed by
  127. // modules that merely use locks. The `lock` argument is a pointer to the lock
  128. // object.
  129. // Report that a lock has been created at address `lock`.
  130. #define ANNOTATE_RWLOCK_CREATE(lock) \
  131. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
  132. // Report that a linker initialized lock has been created at address `lock`.
  133. #ifdef ABSL_HAVE_THREAD_SANITIZER
  134. #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
  135. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreateStatic) \
  136. (__FILE__, __LINE__, lock)
  137. #else
  138. #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) ANNOTATE_RWLOCK_CREATE(lock)
  139. #endif
  140. // Report that the lock at address `lock` is about to be destroyed.
  141. #define ANNOTATE_RWLOCK_DESTROY(lock) \
  142. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
  143. // Report that the lock at address `lock` has been acquired.
  144. // `is_w`=1 for writer lock, `is_w`=0 for reader lock.
  145. #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
  146. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockAcquired) \
  147. (__FILE__, __LINE__, lock, is_w)
  148. // Report that the lock at address `lock` is about to be released.
  149. // `is_w`=1 for writer lock, `is_w`=0 for reader lock.
  150. #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
  151. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockReleased) \
  152. (__FILE__, __LINE__, lock, is_w)
  153. // Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable `static_var`.
  154. #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
  155. namespace { \
  156. class static_var##_annotator { \
  157. public: \
  158. static_var##_annotator() { \
  159. ANNOTATE_BENIGN_RACE_SIZED(&static_var, sizeof(static_var), \
  160. #static_var ": " description); \
  161. } \
  162. }; \
  163. static static_var##_annotator the##static_var##_annotator; \
  164. } // namespace
  165. #else // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 0
  166. #define ANNOTATE_RWLOCK_CREATE(lock) // empty
  167. #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) // empty
  168. #define ANNOTATE_RWLOCK_DESTROY(lock) // empty
  169. #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
  170. #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
  171. #define ANNOTATE_BENIGN_RACE(address, description) // empty
  172. #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) // empty
  173. #define ANNOTATE_THREAD_NAME(name) // empty
  174. #define ANNOTATE_ENABLE_RACE_DETECTION(enable) // empty
  175. #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
  176. #endif // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
  177. // -------------------------------------------------------------------------
  178. // Define memory annotations.
  179. #if ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 1
  180. #include <sanitizer/msan_interface.h>
  181. #define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
  182. __msan_unpoison(address, size)
  183. #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
  184. __msan_allocated_memory(address, size)
  185. #else // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 0
  186. #if DYNAMIC_ANNOTATIONS_ENABLED == 1
  187. #define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
  188. do { \
  189. (void)(address); \
  190. (void)(size); \
  191. } while (0)
  192. #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
  193. do { \
  194. (void)(address); \
  195. (void)(size); \
  196. } while (0)
  197. #else
  198. #define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) // empty
  199. #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) // empty
  200. #endif
  201. #endif // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
  202. // -------------------------------------------------------------------------
  203. // Define IGNORE_READS_BEGIN/_END attributes.
  204. #if defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
  205. #define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE \
  206. __attribute((exclusive_lock_function("*")))
  207. #define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE \
  208. __attribute((unlock_function("*")))
  209. #else // !defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
  210. #define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE // empty
  211. #define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE // empty
  212. #endif // defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
  213. // -------------------------------------------------------------------------
  214. // Define IGNORE_READS_BEGIN/_END annotations.
  215. #if ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED == 1
  216. // Request the analysis tool to ignore all reads in the current thread until
  217. // ANNOTATE_IGNORE_READS_END is called. Useful to ignore intentional racey
  218. // reads, while still checking other reads and all writes.
  219. // See also ANNOTATE_UNPROTECTED_READ.
  220. #define ANNOTATE_IGNORE_READS_BEGIN() \
  221. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsBegin)(__FILE__, __LINE__)
  222. // Stop ignoring reads.
  223. #define ANNOTATE_IGNORE_READS_END() \
  224. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsEnd)(__FILE__, __LINE__)
  225. #elif defined(ABSL_INTERNAL_ANNOTALYSIS_ENABLED)
  226. // When Annotalysis is enabled without Dynamic Annotations, the use of
  227. // static-inline functions allows the annotations to be read at compile-time,
  228. // while still letting the compiler elide the functions from the final build.
  229. //
  230. // TODO(delesley) -- The exclusive lock here ignores writes as well, but
  231. // allows IGNORE_READS_AND_WRITES to work properly.
  232. #define ANNOTATE_IGNORE_READS_BEGIN() \
  233. ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsBegin)()
  234. #define ANNOTATE_IGNORE_READS_END() \
  235. ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsEnd)()
  236. #else
  237. #define ANNOTATE_IGNORE_READS_BEGIN() // empty
  238. #define ANNOTATE_IGNORE_READS_END() // empty
  239. #endif
  240. // -------------------------------------------------------------------------
  241. // Define IGNORE_WRITES_BEGIN/_END annotations.
  242. #if ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED == 1
  243. // Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead.
  244. #define ANNOTATE_IGNORE_WRITES_BEGIN() \
  245. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
  246. // Stop ignoring writes.
  247. #define ANNOTATE_IGNORE_WRITES_END() \
  248. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
  249. #else
  250. #define ANNOTATE_IGNORE_WRITES_BEGIN() // empty
  251. #define ANNOTATE_IGNORE_WRITES_END() // empty
  252. #endif
  253. // -------------------------------------------------------------------------
  254. // Define the ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
  255. // primitive annotations defined above.
  256. //
  257. // Instead of doing
  258. // ANNOTATE_IGNORE_READS_BEGIN();
  259. // ... = x;
  260. // ANNOTATE_IGNORE_READS_END();
  261. // one can use
  262. // ... = ANNOTATE_UNPROTECTED_READ(x);
  263. #if defined(ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED)
  264. // Start ignoring all memory accesses (both reads and writes).
  265. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
  266. do { \
  267. ANNOTATE_IGNORE_READS_BEGIN(); \
  268. ANNOTATE_IGNORE_WRITES_BEGIN(); \
  269. } while (0)
  270. // Stop ignoring both reads and writes.
  271. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
  272. do { \
  273. ANNOTATE_IGNORE_WRITES_END(); \
  274. ANNOTATE_IGNORE_READS_END(); \
  275. } while (0)
  276. #ifdef __cplusplus
  277. // ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
  278. #define ANNOTATE_UNPROTECTED_READ(x) \
  279. absl::base_internal::AnnotateUnprotectedRead(x)
  280. #endif
  281. #else
  282. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
  283. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
  284. #define ANNOTATE_UNPROTECTED_READ(x) (x)
  285. #endif
  286. // -------------------------------------------------------------------------
  287. // Address sanitizer annotations
  288. #ifdef ABSL_HAVE_ADDRESS_SANITIZER
  289. // Describe the current state of a contiguous container such as e.g.
  290. // std::vector or std::string. For more details see
  291. // sanitizer/common_interface_defs.h, which is provided by the compiler.
  292. #include <sanitizer/common_interface_defs.h>
  293. #define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
  294. __sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
  295. #define ADDRESS_SANITIZER_REDZONE(name) \
  296. struct { \
  297. char x[8] __attribute__((aligned(8))); \
  298. } name
  299. #else
  300. #define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid)
  301. #define ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "")
  302. #endif // ABSL_HAVE_ADDRESS_SANITIZER
  303. // -------------------------------------------------------------------------
  304. // Undefine the macros intended only for this file.
  305. #undef ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
  306. #undef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
  307. #undef ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED
  308. #undef ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED
  309. #undef ABSL_INTERNAL_ANNOTALYSIS_ENABLED
  310. #undef ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED
  311. #undef ABSL_INTERNAL_BEGIN_EXTERN_C
  312. #undef ABSL_INTERNAL_END_EXTERN_C
  313. #undef ABSL_INTERNAL_STATIC_INLINE
  314. #endif // ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_