You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dynamic_annotations.h 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 \
  86. extern "C" \
  87. {
  88. #define ABSL_INTERNAL_END_EXTERN_C } // extern "C"
  89. #define ABSL_INTERNAL_GLOBAL_SCOPED(F) ::F
  90. #define ABSL_INTERNAL_STATIC_INLINE inline
  91. #else
  92. #define ABSL_INTERNAL_BEGIN_EXTERN_C // empty
  93. #define ABSL_INTERNAL_END_EXTERN_C // empty
  94. #define ABSL_INTERNAL_GLOBAL_SCOPED(F) F
  95. #define ABSL_INTERNAL_STATIC_INLINE static inline
  96. #endif
  97. // -------------------------------------------------------------------------
  98. // Define race annotations.
  99. #if ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 1
  100. // -------------------------------------------------------------
  101. // Annotations that suppress errors. It is usually better to express the
  102. // program's synchronization using the other annotations, but these can be used
  103. // when all else fails.
  104. // Report that we may have a benign race at `pointer`, with size
  105. // "sizeof(*(pointer))". `pointer` must be a non-void* pointer. Insert at the
  106. // point where `pointer` has been allocated, preferably close to the point
  107. // where the race happens. See also ANNOTATE_BENIGN_RACE_STATIC.
  108. #define ANNOTATE_BENIGN_RACE(pointer, description) \
  109. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
  110. (__FILE__, __LINE__, pointer, sizeof(*(pointer)), description)
  111. // Same as ANNOTATE_BENIGN_RACE(`address`, `description`), but applies to
  112. // the memory range [`address`, `address`+`size`).
  113. #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
  114. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
  115. (__FILE__, __LINE__, address, size, description)
  116. // Enable (`enable`!=0) or disable (`enable`==0) race detection for all threads.
  117. // This annotation could be useful if you want to skip expensive race analysis
  118. // during some period of program execution, e.g. during initialization.
  119. #define ANNOTATE_ENABLE_RACE_DETECTION(enable) \
  120. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateEnableRaceDetection) \
  121. (__FILE__, __LINE__, enable)
  122. // -------------------------------------------------------------
  123. // Annotations useful for debugging.
  124. // Report the current thread `name` to a race detector.
  125. #define ANNOTATE_THREAD_NAME(name) \
  126. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateThreadName) \
  127. (__FILE__, __LINE__, name)
  128. // -------------------------------------------------------------
  129. // Annotations useful when implementing locks. They are not normally needed by
  130. // modules that merely use locks. The `lock` argument is a pointer to the lock
  131. // object.
  132. // Report that a lock has been created at address `lock`.
  133. #define ANNOTATE_RWLOCK_CREATE(lock) \
  134. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreate) \
  135. (__FILE__, __LINE__, lock)
  136. // Report that a linker initialized lock has been created at address `lock`.
  137. #ifdef ABSL_HAVE_THREAD_SANITIZER
  138. #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
  139. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreateStatic) \
  140. (__FILE__, __LINE__, lock)
  141. #else
  142. #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) ANNOTATE_RWLOCK_CREATE(lock)
  143. #endif
  144. // Report that the lock at address `lock` is about to be destroyed.
  145. #define ANNOTATE_RWLOCK_DESTROY(lock) \
  146. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockDestroy) \
  147. (__FILE__, __LINE__, lock)
  148. // Report that the lock at address `lock` has been acquired.
  149. // `is_w`=1 for writer lock, `is_w`=0 for reader lock.
  150. #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
  151. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockAcquired) \
  152. (__FILE__, __LINE__, lock, is_w)
  153. // Report that the lock at address `lock` is about to be released.
  154. // `is_w`=1 for writer lock, `is_w`=0 for reader lock.
  155. #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
  156. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockReleased) \
  157. (__FILE__, __LINE__, lock, is_w)
  158. // Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable `static_var`.
  159. #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
  160. namespace \
  161. { \
  162. class static_var##_annotator \
  163. { \
  164. public: \
  165. static_var##_annotator() \
  166. { \
  167. ANNOTATE_BENIGN_RACE_SIZED(&static_var, sizeof(static_var), #static_var ": " description); \
  168. } \
  169. }; \
  170. static static_var##_annotator the##static_var##_annotator; \
  171. } // namespace
  172. #else // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 0
  173. #define ANNOTATE_RWLOCK_CREATE(lock) // empty
  174. #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) // empty
  175. #define ANNOTATE_RWLOCK_DESTROY(lock) // empty
  176. #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
  177. #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
  178. #define ANNOTATE_BENIGN_RACE(address, description) // empty
  179. #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) // empty
  180. #define ANNOTATE_THREAD_NAME(name) // empty
  181. #define ANNOTATE_ENABLE_RACE_DETECTION(enable) // empty
  182. #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
  183. #endif // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
  184. // -------------------------------------------------------------------------
  185. // Define memory annotations.
  186. #if ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 1
  187. #include <sanitizer/msan_interface.h>
  188. #define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
  189. __msan_unpoison(address, size)
  190. #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
  191. __msan_allocated_memory(address, size)
  192. #else // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 0
  193. #if DYNAMIC_ANNOTATIONS_ENABLED == 1
  194. #define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
  195. do \
  196. { \
  197. (void)(address); \
  198. (void)(size); \
  199. } while (0)
  200. #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
  201. do \
  202. { \
  203. (void)(address); \
  204. (void)(size); \
  205. } while (0)
  206. #else
  207. #define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) // empty
  208. #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) // empty
  209. #endif
  210. #endif // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
  211. // -------------------------------------------------------------------------
  212. // Define IGNORE_READS_BEGIN/_END attributes.
  213. #if defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
  214. #define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE \
  215. __attribute((exclusive_lock_function("*")))
  216. #define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE \
  217. __attribute((unlock_function("*")))
  218. #else // !defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
  219. #define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE // empty
  220. #define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE // empty
  221. #endif // defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
  222. // -------------------------------------------------------------------------
  223. // Define IGNORE_READS_BEGIN/_END annotations.
  224. #if ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED == 1
  225. // Request the analysis tool to ignore all reads in the current thread until
  226. // ANNOTATE_IGNORE_READS_END is called. Useful to ignore intentional racey
  227. // reads, while still checking other reads and all writes.
  228. // See also ANNOTATE_UNPROTECTED_READ.
  229. #define ANNOTATE_IGNORE_READS_BEGIN() \
  230. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsBegin) \
  231. (__FILE__, __LINE__)
  232. // Stop ignoring reads.
  233. #define ANNOTATE_IGNORE_READS_END() \
  234. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsEnd) \
  235. (__FILE__, __LINE__)
  236. #elif defined(ABSL_INTERNAL_ANNOTALYSIS_ENABLED)
  237. // When Annotalysis is enabled without Dynamic Annotations, the use of
  238. // static-inline functions allows the annotations to be read at compile-time,
  239. // while still letting the compiler elide the functions from the final build.
  240. //
  241. // TODO(delesley) -- The exclusive lock here ignores writes as well, but
  242. // allows IGNORE_READS_AND_WRITES to work properly.
  243. #define ANNOTATE_IGNORE_READS_BEGIN() \
  244. ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsBegin) \
  245. ()
  246. #define ANNOTATE_IGNORE_READS_END() \
  247. ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsEnd) \
  248. ()
  249. #else
  250. #define ANNOTATE_IGNORE_READS_BEGIN() // empty
  251. #define ANNOTATE_IGNORE_READS_END() // empty
  252. #endif
  253. // -------------------------------------------------------------------------
  254. // Define IGNORE_WRITES_BEGIN/_END annotations.
  255. #if ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED == 1
  256. // Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead.
  257. #define ANNOTATE_IGNORE_WRITES_BEGIN() \
  258. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesBegin) \
  259. (__FILE__, __LINE__)
  260. // Stop ignoring writes.
  261. #define ANNOTATE_IGNORE_WRITES_END() \
  262. ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesEnd) \
  263. (__FILE__, __LINE__)
  264. #else
  265. #define ANNOTATE_IGNORE_WRITES_BEGIN() // empty
  266. #define ANNOTATE_IGNORE_WRITES_END() // empty
  267. #endif
  268. // -------------------------------------------------------------------------
  269. // Define the ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
  270. // primitive annotations defined above.
  271. //
  272. // Instead of doing
  273. // ANNOTATE_IGNORE_READS_BEGIN();
  274. // ... = x;
  275. // ANNOTATE_IGNORE_READS_END();
  276. // one can use
  277. // ... = ANNOTATE_UNPROTECTED_READ(x);
  278. #if defined(ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED)
  279. // Start ignoring all memory accesses (both reads and writes).
  280. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
  281. do \
  282. { \
  283. ANNOTATE_IGNORE_READS_BEGIN(); \
  284. ANNOTATE_IGNORE_WRITES_BEGIN(); \
  285. } while (0)
  286. // Stop ignoring both reads and writes.
  287. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
  288. do \
  289. { \
  290. ANNOTATE_IGNORE_WRITES_END(); \
  291. ANNOTATE_IGNORE_READS_END(); \
  292. } while (0)
  293. #ifdef __cplusplus
  294. // ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
  295. #define ANNOTATE_UNPROTECTED_READ(x) \
  296. absl::base_internal::AnnotateUnprotectedRead(x)
  297. #endif
  298. #else
  299. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
  300. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
  301. #define ANNOTATE_UNPROTECTED_READ(x) (x)
  302. #endif
  303. // -------------------------------------------------------------------------
  304. // Address sanitizer annotations
  305. #ifdef ABSL_HAVE_ADDRESS_SANITIZER
  306. // Describe the current state of a contiguous container such as e.g.
  307. // std::vector or std::string. For more details see
  308. // sanitizer/common_interface_defs.h, which is provided by the compiler.
  309. #include <sanitizer/common_interface_defs.h>
  310. #define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
  311. __sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
  312. #define ADDRESS_SANITIZER_REDZONE(name) \
  313. struct \
  314. { \
  315. char x[8] __attribute__((aligned(8))); \
  316. } name
  317. #else
  318. #define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid)
  319. #define ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "")
  320. #endif // ABSL_HAVE_ADDRESS_SANITIZER
  321. // -------------------------------------------------------------------------
  322. // Undefine the macros intended only for this file.
  323. #undef ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
  324. #undef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
  325. #undef ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED
  326. #undef ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED
  327. #undef ABSL_INTERNAL_ANNOTALYSIS_ENABLED
  328. #undef ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED
  329. #undef ABSL_INTERNAL_BEGIN_EXTERN_C
  330. #undef ABSL_INTERNAL_END_EXTERN_C
  331. #undef ABSL_INTERNAL_STATIC_INLINE
  332. #endif // ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_