dynamic_annotations.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /* Copyright (c) 2008, Google Inc.
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * ---
  31. * Author: Kostya Serebryany
  32. */
  33. /* This file defines dynamic annotations for use with dynamic analysis
  34. tool such as valgrind, PIN, etc.
  35. Dynamic annotation is a source code annotation that affects
  36. the generated code (that is, the annotation is not a comment).
  37. Each such annotation is attached to a particular
  38. instruction and/or to a particular object (address) in the program.
  39. The annotations that should be used by users are macros in all upper-case
  40. (e.g., ANNOTATE_NEW_MEMORY).
  41. Actual implementation of these macros may differ depending on the
  42. dynamic analysis tool being used.
  43. See http://code.google.com/p/data-race-test/ for more information.
  44. This file supports the following dynamic analysis tools:
  45. - None (DYNAMIC_ANNOTATIONS_ENABLED is not defined or zero).
  46. Macros are defined empty.
  47. - ThreadSanitizer, Helgrind, DRD (DYNAMIC_ANNOTATIONS_ENABLED is 1).
  48. Macros are defined as calls to non-inlinable empty functions
  49. that are intercepted by Valgrind. */
  50. #ifndef BASE_DYNAMIC_ANNOTATIONS_H_
  51. #define BASE_DYNAMIC_ANNOTATIONS_H_
  52. #ifndef DYNAMIC_ANNOTATIONS_ENABLED
  53. # define DYNAMIC_ANNOTATIONS_ENABLED 0
  54. #endif
  55. #if DYNAMIC_ANNOTATIONS_ENABLED != 0
  56. /* -------------------------------------------------------------
  57. Annotations useful when implementing condition variables such as CondVar,
  58. using conditional critical sections (Await/LockWhen) and when constructing
  59. user-defined synchronization mechanisms.
  60. The annotations ANNOTATE_HAPPENS_BEFORE() and ANNOTATE_HAPPENS_AFTER() can
  61. be used to define happens-before arcs in user-defined synchronization
  62. mechanisms: the race detector will infer an arc from the former to the
  63. latter when they share the same argument pointer.
  64. Example 1 (reference counting):
  65. void Unref() {
  66. ANNOTATE_HAPPENS_BEFORE(&refcount_);
  67. if (AtomicDecrementByOne(&refcount_) == 0) {
  68. ANNOTATE_HAPPENS_AFTER(&refcount_);
  69. delete this;
  70. }
  71. }
  72. Example 2 (message queue):
  73. void MyQueue::Put(Type *e) {
  74. MutexLock lock(&mu_);
  75. ANNOTATE_HAPPENS_BEFORE(e);
  76. PutElementIntoMyQueue(e);
  77. }
  78. Type *MyQueue::Get() {
  79. MutexLock lock(&mu_);
  80. Type *e = GetElementFromMyQueue();
  81. ANNOTATE_HAPPENS_AFTER(e);
  82. return e;
  83. }
  84. Note: when possible, please use the existing reference counting and message
  85. queue implementations instead of inventing new ones. */
  86. /* Report that wait on the condition variable at address "cv" has succeeded
  87. and the lock at address "lock" is held. */
  88. #define ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) \
  89. AnnotateCondVarWait(__FILE__, __LINE__, cv, lock)
  90. /* Report that wait on the condition variable at "cv" has succeeded. Variant
  91. w/o lock. */
  92. #define ANNOTATE_CONDVAR_WAIT(cv) \
  93. AnnotateCondVarWait(__FILE__, __LINE__, cv, NULL)
  94. /* Report that we are about to signal on the condition variable at address
  95. "cv". */
  96. #define ANNOTATE_CONDVAR_SIGNAL(cv) \
  97. AnnotateCondVarSignal(__FILE__, __LINE__, cv)
  98. /* Report that we are about to signal_all on the condition variable at "cv". */
  99. #define ANNOTATE_CONDVAR_SIGNAL_ALL(cv) \
  100. AnnotateCondVarSignalAll(__FILE__, __LINE__, cv)
  101. /* Annotations for user-defined synchronization mechanisms. */
  102. #define ANNOTATE_HAPPENS_BEFORE(obj) ANNOTATE_CONDVAR_SIGNAL(obj)
  103. #define ANNOTATE_HAPPENS_AFTER(obj) ANNOTATE_CONDVAR_WAIT(obj)
  104. /* Report that the bytes in the range [pointer, pointer+size) are about
  105. to be published safely. The race checker will create a happens-before
  106. arc from the call ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size) to
  107. subsequent accesses to this memory.
  108. Note: this annotation may not work properly if the race detector uses
  109. sampling, i.e. does not observe all memory accesses.
  110. */
  111. #define ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size) \
  112. AnnotatePublishMemoryRange(__FILE__, __LINE__, pointer, size)
  113. /* DEPRECATED. Don't use it. */
  114. #define ANNOTATE_UNPUBLISH_MEMORY_RANGE(pointer, size) \
  115. AnnotateUnpublishMemoryRange(__FILE__, __LINE__, pointer, size)
  116. /* DEPRECATED. Don't use it. */
  117. #define ANNOTATE_SWAP_MEMORY_RANGE(pointer, size) \
  118. do { \
  119. ANNOTATE_UNPUBLISH_MEMORY_RANGE(pointer, size); \
  120. ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size); \
  121. } while (0)
  122. /* Instruct the tool to create a happens-before arc between mu->Unlock() and
  123. mu->Lock(). This annotation may slow down the race detector and hide real
  124. races. Normally it is used only when it would be difficult to annotate each
  125. of the mutex's critical sections individually using the annotations above.
  126. This annotation makes sense only for hybrid race detectors. For pure
  127. happens-before detectors this is a no-op. For more details see
  128. http://code.google.com/p/data-race-test/wiki/PureHappensBeforeVsHybrid . */
  129. #define ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) \
  130. AnnotateMutexIsUsedAsCondVar(__FILE__, __LINE__, mu)
  131. /* Deprecated. Use ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX. */
  132. #define ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) \
  133. AnnotateMutexIsUsedAsCondVar(__FILE__, __LINE__, mu)
  134. /* -------------------------------------------------------------
  135. Annotations useful when defining memory allocators, or when memory that
  136. was protected in one way starts to be protected in another. */
  137. /* Report that a new memory at "address" of size "size" has been allocated.
  138. This might be used when the memory has been retrieved from a free list and
  139. is about to be reused, or when a the locking discipline for a variable
  140. changes. */
  141. #define ANNOTATE_NEW_MEMORY(address, size) \
  142. AnnotateNewMemory(__FILE__, __LINE__, address, size)
  143. /* -------------------------------------------------------------
  144. Annotations useful when defining FIFO queues that transfer data between
  145. threads. */
  146. /* Report that the producer-consumer queue (such as ProducerConsumerQueue) at
  147. address "pcq" has been created. The ANNOTATE_PCQ_* annotations
  148. should be used only for FIFO queues. For non-FIFO queues use
  149. ANNOTATE_HAPPENS_BEFORE (for put) and ANNOTATE_HAPPENS_AFTER (for get). */
  150. #define ANNOTATE_PCQ_CREATE(pcq) \
  151. AnnotatePCQCreate(__FILE__, __LINE__, pcq)
  152. /* Report that the queue at address "pcq" is about to be destroyed. */
  153. #define ANNOTATE_PCQ_DESTROY(pcq) \
  154. AnnotatePCQDestroy(__FILE__, __LINE__, pcq)
  155. /* Report that we are about to put an element into a FIFO queue at address
  156. "pcq". */
  157. #define ANNOTATE_PCQ_PUT(pcq) \
  158. AnnotatePCQPut(__FILE__, __LINE__, pcq)
  159. /* Report that we've just got an element from a FIFO queue at address "pcq". */
  160. #define ANNOTATE_PCQ_GET(pcq) \
  161. AnnotatePCQGet(__FILE__, __LINE__, pcq)
  162. /* -------------------------------------------------------------
  163. Annotations that suppress errors. It is usually better to express the
  164. program's synchronization using the other annotations, but these can
  165. be used when all else fails. */
  166. /* Report that we may have a benign race at "pointer", with size
  167. "sizeof(*(pointer))". "pointer" must be a non-void* pointer. Insert at the
  168. point where "pointer" has been allocated, preferably close to the point
  169. where the race happens. See also ANNOTATE_BENIGN_RACE_STATIC. */
  170. #define ANNOTATE_BENIGN_RACE(pointer, description) \
  171. AnnotateBenignRaceSized(__FILE__, __LINE__, pointer, \
  172. sizeof(*(pointer)), description)
  173. /* Same as ANNOTATE_BENIGN_RACE(address, description), but applies to
  174. the memory range [address, address+size). */
  175. #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
  176. AnnotateBenignRaceSized(__FILE__, __LINE__, address, size, description)
  177. /* Request the analysis tool to ignore all reads in the current thread
  178. until ANNOTATE_IGNORE_READS_END is called.
  179. Useful to ignore intentional racey reads, while still checking
  180. other reads and all writes.
  181. See also ANNOTATE_UNPROTECTED_READ. */
  182. #define ANNOTATE_IGNORE_READS_BEGIN() \
  183. AnnotateIgnoreReadsBegin(__FILE__, __LINE__)
  184. /* Stop ignoring reads. */
  185. #define ANNOTATE_IGNORE_READS_END() \
  186. AnnotateIgnoreReadsEnd(__FILE__, __LINE__)
  187. /* Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes. */
  188. #define ANNOTATE_IGNORE_WRITES_BEGIN() \
  189. AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
  190. /* Stop ignoring writes. */
  191. #define ANNOTATE_IGNORE_WRITES_END() \
  192. AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
  193. /* Start ignoring all memory accesses (reads and writes). */
  194. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
  195. do {\
  196. ANNOTATE_IGNORE_READS_BEGIN();\
  197. ANNOTATE_IGNORE_WRITES_BEGIN();\
  198. }while(0)\
  199. /* Stop ignoring all memory accesses. */
  200. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
  201. do {\
  202. ANNOTATE_IGNORE_WRITES_END();\
  203. ANNOTATE_IGNORE_READS_END();\
  204. }while(0)\
  205. /* Enable (enable!=0) or disable (enable==0) race detection for all threads.
  206. This annotation could be useful if you want to skip expensive race analysis
  207. during some period of program execution, e.g. during initialization. */
  208. #define ANNOTATE_ENABLE_RACE_DETECTION(enable) \
  209. AnnotateEnableRaceDetection(__FILE__, __LINE__, enable)
  210. /* -------------------------------------------------------------
  211. Annotations useful for debugging. */
  212. /* Request to trace every access to "address". */
  213. #define ANNOTATE_TRACE_MEMORY(address) \
  214. AnnotateTraceMemory(__FILE__, __LINE__, address)
  215. /* Report the current thread name to a race detector. */
  216. #define ANNOTATE_THREAD_NAME(name) \
  217. AnnotateThreadName(__FILE__, __LINE__, name)
  218. /* -------------------------------------------------------------
  219. Annotations useful when implementing locks. They are not
  220. normally needed by modules that merely use locks.
  221. The "lock" argument is a pointer to the lock object. */
  222. /* Report that a lock has been created at address "lock". */
  223. #define ANNOTATE_RWLOCK_CREATE(lock) \
  224. AnnotateRWLockCreate(__FILE__, __LINE__, lock)
  225. /* Report that the lock at address "lock" is about to be destroyed. */
  226. #define ANNOTATE_RWLOCK_DESTROY(lock) \
  227. AnnotateRWLockDestroy(__FILE__, __LINE__, lock)
  228. /* Report that the lock at address "lock" has been acquired.
  229. is_w=1 for writer lock, is_w=0 for reader lock. */
  230. #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
  231. AnnotateRWLockAcquired(__FILE__, __LINE__, lock, is_w)
  232. /* Report that the lock at address "lock" is about to be released. */
  233. #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
  234. AnnotateRWLockReleased(__FILE__, __LINE__, lock, is_w)
  235. /* -------------------------------------------------------------
  236. Annotations useful when implementing barriers. They are not
  237. normally needed by modules that merely use barriers.
  238. The "barrier" argument is a pointer to the barrier object. */
  239. /* Report that the "barrier" has been initialized with initial "count".
  240. If 'reinitialization_allowed' is true, initialization is allowed to happen
  241. multiple times w/o calling barrier_destroy() */
  242. #define ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) \
  243. AnnotateBarrierInit(__FILE__, __LINE__, barrier, count, \
  244. reinitialization_allowed)
  245. /* Report that we are about to enter barrier_wait("barrier"). */
  246. #define ANNOTATE_BARRIER_WAIT_BEFORE(barrier) \
  247. AnnotateBarrierWaitBefore(__FILE__, __LINE__, barrier)
  248. /* Report that we just exited barrier_wait("barrier"). */
  249. #define ANNOTATE_BARRIER_WAIT_AFTER(barrier) \
  250. AnnotateBarrierWaitAfter(__FILE__, __LINE__, barrier)
  251. /* Report that the "barrier" has been destroyed. */
  252. #define ANNOTATE_BARRIER_DESTROY(barrier) \
  253. AnnotateBarrierDestroy(__FILE__, __LINE__, barrier)
  254. /* -------------------------------------------------------------
  255. Annotations useful for testing race detectors. */
  256. /* Report that we expect a race on the variable at "address".
  257. Use only in unit tests for a race detector. */
  258. #define ANNOTATE_EXPECT_RACE(address, description) \
  259. AnnotateExpectRace(__FILE__, __LINE__, address, description)
  260. /* A no-op. Insert where you like to test the interceptors. */
  261. #define ANNOTATE_NO_OP(arg) \
  262. AnnotateNoOp(__FILE__, __LINE__, arg)
  263. /* Force the race detector to flush its state. The actual effect depends on
  264. * the implementation of the detector. */
  265. #define ANNOTATE_FLUSH_STATE() \
  266. AnnotateFlushState(__FILE__, __LINE__)
  267. #else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
  268. #define ANNOTATE_RWLOCK_CREATE(lock) /* empty */
  269. #define ANNOTATE_RWLOCK_DESTROY(lock) /* empty */
  270. #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) /* empty */
  271. #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) /* empty */
  272. #define ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) /* */
  273. #define ANNOTATE_BARRIER_WAIT_BEFORE(barrier) /* empty */
  274. #define ANNOTATE_BARRIER_WAIT_AFTER(barrier) /* empty */
  275. #define ANNOTATE_BARRIER_DESTROY(barrier) /* empty */
  276. #define ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) /* empty */
  277. #define ANNOTATE_CONDVAR_WAIT(cv) /* empty */
  278. #define ANNOTATE_CONDVAR_SIGNAL(cv) /* empty */
  279. #define ANNOTATE_CONDVAR_SIGNAL_ALL(cv) /* empty */
  280. #define ANNOTATE_HAPPENS_BEFORE(obj) /* empty */
  281. #define ANNOTATE_HAPPENS_AFTER(obj) /* empty */
  282. #define ANNOTATE_PUBLISH_MEMORY_RANGE(address, size) /* empty */
  283. #define ANNOTATE_UNPUBLISH_MEMORY_RANGE(address, size) /* empty */
  284. #define ANNOTATE_SWAP_MEMORY_RANGE(address, size) /* empty */
  285. #define ANNOTATE_PCQ_CREATE(pcq) /* empty */
  286. #define ANNOTATE_PCQ_DESTROY(pcq) /* empty */
  287. #define ANNOTATE_PCQ_PUT(pcq) /* empty */
  288. #define ANNOTATE_PCQ_GET(pcq) /* empty */
  289. #define ANNOTATE_NEW_MEMORY(address, size) /* empty */
  290. #define ANNOTATE_EXPECT_RACE(address, description) /* empty */
  291. #define ANNOTATE_BENIGN_RACE(address, description) /* empty */
  292. #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) /* empty */
  293. #define ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) /* empty */
  294. #define ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) /* empty */
  295. #define ANNOTATE_TRACE_MEMORY(arg) /* empty */
  296. #define ANNOTATE_THREAD_NAME(name) /* empty */
  297. #define ANNOTATE_IGNORE_READS_BEGIN() /* empty */
  298. #define ANNOTATE_IGNORE_READS_END() /* empty */
  299. #define ANNOTATE_IGNORE_WRITES_BEGIN() /* empty */
  300. #define ANNOTATE_IGNORE_WRITES_END() /* empty */
  301. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() /* empty */
  302. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() /* empty */
  303. #define ANNOTATE_ENABLE_RACE_DETECTION(enable) /* empty */
  304. #define ANNOTATE_NO_OP(arg) /* empty */
  305. #define ANNOTATE_FLUSH_STATE() /* empty */
  306. #endif /* DYNAMIC_ANNOTATIONS_ENABLED */
  307. /* Macro definitions for GCC attributes that allow static thread safety
  308. analysis to recognize and use some of the dynamic annotations as
  309. escape hatches.
  310. TODO(lcwu): remove the check for __SUPPORT_DYN_ANNOTATION__ once the
  311. default crosstool/GCC supports these GCC attributes. */
  312. #define ANNOTALYSIS_STATIC_INLINE
  313. #define ANNOTALYSIS_SEMICOLON_OR_EMPTY_BODY ;
  314. #define ANNOTALYSIS_IGNORE_READS_BEGIN
  315. #define ANNOTALYSIS_IGNORE_READS_END
  316. #define ANNOTALYSIS_IGNORE_WRITES_BEGIN
  317. #define ANNOTALYSIS_IGNORE_WRITES_END
  318. #define ANNOTALYSIS_UNPROTECTED_READ
  319. #if defined(__GNUC__) && (!defined(SWIG)) && (!defined(__clang__)) && \
  320. defined(__SUPPORT_TS_ANNOTATION__) && defined(__SUPPORT_DYN_ANNOTATION__)
  321. #if DYNAMIC_ANNOTATIONS_ENABLED == 0
  322. #define ANNOTALYSIS_ONLY 1
  323. #undef ANNOTALYSIS_STATIC_INLINE
  324. #define ANNOTALYSIS_STATIC_INLINE static inline
  325. #undef ANNOTALYSIS_SEMICOLON_OR_EMPTY_BODY
  326. #define ANNOTALYSIS_SEMICOLON_OR_EMPTY_BODY { (void)file; (void)line; }
  327. #endif
  328. /* Only emit attributes when annotalysis is enabled. */
  329. #if defined(__SUPPORT_TS_ANNOTATION__) && defined(__SUPPORT_DYN_ANNOTATION__)
  330. #undef ANNOTALYSIS_IGNORE_READS_BEGIN
  331. #define ANNOTALYSIS_IGNORE_READS_BEGIN __attribute__ ((ignore_reads_begin))
  332. #undef ANNOTALYSIS_IGNORE_READS_END
  333. #define ANNOTALYSIS_IGNORE_READS_END __attribute__ ((ignore_reads_end))
  334. #undef ANNOTALYSIS_IGNORE_WRITES_BEGIN
  335. #define ANNOTALYSIS_IGNORE_WRITES_BEGIN __attribute__ ((ignore_writes_begin))
  336. #undef ANNOTALYSIS_IGNORE_WRITES_END
  337. #define ANNOTALYSIS_IGNORE_WRITES_END __attribute__ ((ignore_writes_end))
  338. #undef ANNOTALYSIS_UNPROTECTED_READ
  339. #define ANNOTALYSIS_UNPROTECTED_READ __attribute__ ((unprotected_read))
  340. #endif
  341. #endif // defined(__GNUC__) && (!defined(SWIG)) && (!defined(__clang__))
  342. /* Use the macros above rather than using these functions directly. */
  343. #ifdef __cplusplus
  344. extern "C" {
  345. #endif
  346. void AnnotateRWLockCreate(const char *file, int line,
  347. const volatile void *lock);
  348. void AnnotateRWLockDestroy(const char *file, int line,
  349. const volatile void *lock);
  350. void AnnotateRWLockAcquired(const char *file, int line,
  351. const volatile void *lock, long is_w);
  352. void AnnotateRWLockReleased(const char *file, int line,
  353. const volatile void *lock, long is_w);
  354. void AnnotateBarrierInit(const char *file, int line,
  355. const volatile void *barrier, long count,
  356. long reinitialization_allowed);
  357. void AnnotateBarrierWaitBefore(const char *file, int line,
  358. const volatile void *barrier);
  359. void AnnotateBarrierWaitAfter(const char *file, int line,
  360. const volatile void *barrier);
  361. void AnnotateBarrierDestroy(const char *file, int line,
  362. const volatile void *barrier);
  363. void AnnotateCondVarWait(const char *file, int line,
  364. const volatile void *cv,
  365. const volatile void *lock);
  366. void AnnotateCondVarSignal(const char *file, int line,
  367. const volatile void *cv);
  368. void AnnotateCondVarSignalAll(const char *file, int line,
  369. const volatile void *cv);
  370. void AnnotatePublishMemoryRange(const char *file, int line,
  371. const volatile void *address,
  372. long size);
  373. void AnnotateUnpublishMemoryRange(const char *file, int line,
  374. const volatile void *address,
  375. long size);
  376. void AnnotatePCQCreate(const char *file, int line,
  377. const volatile void *pcq);
  378. void AnnotatePCQDestroy(const char *file, int line,
  379. const volatile void *pcq);
  380. void AnnotatePCQPut(const char *file, int line,
  381. const volatile void *pcq);
  382. void AnnotatePCQGet(const char *file, int line,
  383. const volatile void *pcq);
  384. void AnnotateNewMemory(const char *file, int line,
  385. const volatile void *address,
  386. long size);
  387. void AnnotateExpectRace(const char *file, int line,
  388. const volatile void *address,
  389. const char *description);
  390. void AnnotateBenignRace(const char *file, int line,
  391. const volatile void *address,
  392. const char *description);
  393. void AnnotateBenignRaceSized(const char *file, int line,
  394. const volatile void *address,
  395. long size,
  396. const char *description);
  397. void AnnotateMutexIsUsedAsCondVar(const char *file, int line,
  398. const volatile void *mu);
  399. void AnnotateTraceMemory(const char *file, int line,
  400. const volatile void *arg);
  401. void AnnotateThreadName(const char *file, int line,
  402. const char *name);
  403. ANNOTALYSIS_STATIC_INLINE
  404. void AnnotateIgnoreReadsBegin(const char *file, int line)
  405. ANNOTALYSIS_IGNORE_READS_BEGIN ANNOTALYSIS_SEMICOLON_OR_EMPTY_BODY
  406. ANNOTALYSIS_STATIC_INLINE
  407. void AnnotateIgnoreReadsEnd(const char *file, int line)
  408. ANNOTALYSIS_IGNORE_READS_END ANNOTALYSIS_SEMICOLON_OR_EMPTY_BODY
  409. ANNOTALYSIS_STATIC_INLINE
  410. void AnnotateIgnoreWritesBegin(const char *file, int line)
  411. ANNOTALYSIS_IGNORE_WRITES_BEGIN ANNOTALYSIS_SEMICOLON_OR_EMPTY_BODY
  412. ANNOTALYSIS_STATIC_INLINE
  413. void AnnotateIgnoreWritesEnd(const char *file, int line)
  414. ANNOTALYSIS_IGNORE_WRITES_END ANNOTALYSIS_SEMICOLON_OR_EMPTY_BODY
  415. void AnnotateEnableRaceDetection(const char *file, int line, int enable);
  416. void AnnotateNoOp(const char *file, int line,
  417. const volatile void *arg);
  418. void AnnotateFlushState(const char *file, int line);
  419. /* Return non-zero value if running under valgrind.
  420. If "valgrind.h" is included into dynamic_annotations.c,
  421. the regular valgrind mechanism will be used.
  422. See http://valgrind.org/docs/manual/manual-core-adv.html about
  423. RUNNING_ON_VALGRIND and other valgrind "client requests".
  424. The file "valgrind.h" may be obtained by doing
  425. svn co svn://svn.valgrind.org/valgrind/trunk/include
  426. If for some reason you can't use "valgrind.h" or want to fake valgrind,
  427. there are two ways to make this function return non-zero:
  428. - Use environment variable: export RUNNING_ON_VALGRIND=1
  429. - Make your tool intercept the function RunningOnValgrind() and
  430. change its return value.
  431. */
  432. int RunningOnValgrind(void);
  433. /* ValgrindSlowdown returns:
  434. * 1.0, if (RunningOnValgrind() == 0)
  435. * 50.0, if (RunningOnValgrind() != 0 && getenv("VALGRIND_SLOWDOWN") == NULL)
  436. * atof(getenv("VALGRIND_SLOWDOWN")) otherwise
  437. This function can be used to scale timeout values:
  438. EXAMPLE:
  439. for (;;) {
  440. DoExpensiveBackgroundTask();
  441. SleepForSeconds(5 * ValgrindSlowdown());
  442. }
  443. */
  444. double ValgrindSlowdown(void);
  445. #ifdef __cplusplus
  446. }
  447. #endif
  448. #if DYNAMIC_ANNOTATIONS_ENABLED != 0 && defined(__cplusplus)
  449. /* ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
  450. Instead of doing
  451. ANNOTATE_IGNORE_READS_BEGIN();
  452. ... = x;
  453. ANNOTATE_IGNORE_READS_END();
  454. one can use
  455. ... = ANNOTATE_UNPROTECTED_READ(x); */
  456. template <class T>
  457. inline T ANNOTATE_UNPROTECTED_READ(const volatile T &x)
  458. ANNOTALYSIS_UNPROTECTED_READ {
  459. ANNOTATE_IGNORE_READS_BEGIN();
  460. T res = x;
  461. ANNOTATE_IGNORE_READS_END();
  462. return res;
  463. }
  464. /* Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable. */
  465. #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
  466. namespace { \
  467. class static_var ## _annotator { \
  468. public: \
  469. static_var ## _annotator() { \
  470. ANNOTATE_BENIGN_RACE_SIZED(&static_var, \
  471. sizeof(static_var), \
  472. # static_var ": " description); \
  473. } \
  474. }; \
  475. static static_var ## _annotator the ## static_var ## _annotator;\
  476. }
  477. #else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
  478. #define ANNOTATE_UNPROTECTED_READ(x) (x)
  479. #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) /* empty */
  480. #endif /* DYNAMIC_ANNOTATIONS_ENABLED */
  481. /* Annotalysis, a GCC based static analyzer, is able to understand and use
  482. some of the dynamic annotations defined in this file. However, dynamic
  483. annotations are usually disabled in the opt mode (to avoid additional
  484. runtime overheads) while Annotalysis only works in the opt mode.
  485. In order for Annotalysis to use these dynamic annotations when they
  486. are disabled, we re-define these annotations here. Note that unlike the
  487. original macro definitions above, these macros are expanded to calls to
  488. static inline functions so that the compiler will be able to remove the
  489. calls after the analysis. */
  490. #ifdef ANNOTALYSIS_ONLY
  491. #undef ANNOTALYSIS_ONLY
  492. /* Undefine and re-define the macros that the static analyzer understands. */
  493. #undef ANNOTATE_IGNORE_READS_BEGIN
  494. #define ANNOTATE_IGNORE_READS_BEGIN() \
  495. AnnotateIgnoreReadsBegin(__FILE__, __LINE__)
  496. #undef ANNOTATE_IGNORE_READS_END
  497. #define ANNOTATE_IGNORE_READS_END() \
  498. AnnotateIgnoreReadsEnd(__FILE__, __LINE__)
  499. #undef ANNOTATE_IGNORE_WRITES_BEGIN
  500. #define ANNOTATE_IGNORE_WRITES_BEGIN() \
  501. AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
  502. #undef ANNOTATE_IGNORE_WRITES_END
  503. #define ANNOTATE_IGNORE_WRITES_END() \
  504. AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
  505. #undef ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN
  506. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
  507. do { \
  508. ANNOTATE_IGNORE_READS_BEGIN(); \
  509. ANNOTATE_IGNORE_WRITES_BEGIN(); \
  510. }while(0) \
  511. #undef ANNOTATE_IGNORE_READS_AND_WRITES_END
  512. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
  513. do { \
  514. ANNOTATE_IGNORE_WRITES_END(); \
  515. ANNOTATE_IGNORE_READS_END(); \
  516. }while(0) \
  517. #if defined(__cplusplus)
  518. #undef ANNOTATE_UNPROTECTED_READ
  519. template <class T>
  520. inline T ANNOTATE_UNPROTECTED_READ(const volatile T &x)
  521. ANNOTALYSIS_UNPROTECTED_READ {
  522. ANNOTATE_IGNORE_READS_BEGIN();
  523. T res = x;
  524. ANNOTATE_IGNORE_READS_END();
  525. return res;
  526. }
  527. #endif /* __cplusplus */
  528. #endif /* ANNOTALYSIS_ONLY */
  529. /* Undefine the macros intended only in this file. */
  530. #undef ANNOTALYSIS_STATIC_INLINE
  531. #undef ANNOTALYSIS_SEMICOLON_OR_EMPTY_BODY
  532. #endif /* BASE_DYNAMIC_ANNOTATIONS_H_ */