1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file defines dynamic annotations for use with dynamic analysis
6 // tool such as valgrind, PIN, etc.
7 //
8 // Dynamic annotation is a source code annotation that affects
9 // the generated code (that is, the annotation is not a comment).
10 // Each such annotation is attached to a particular
11 // instruction and/or to a particular object (address) in the program.
12 //
13 // The annotations that should be used by users are macros in all upper-case
14 // (e.g., ANNOTATE_NEW_MEMORY).
15 //
16 // Actual implementation of these macros may differ depending on the
17 // dynamic analysis tool being used.
18 //
19 // This file supports the following dynamic analysis tools:
20 // - None (NVALGRIND is defined).
21 // Macros are defined empty.
22 // - ThreadSanitizer (NVALGRIND is not defined).
23 // Macros are defined as calls to non-inlinable empty functions
24 // that are intercepted by ThreadSanitizer.
25 //
26 #ifndef BASE_DYNAMIC_ANNOTATIONS_H_
27 #define BASE_DYNAMIC_ANNOTATIONS_H_
28
29 #include "base/third_party/valgrind/valgrind.h"
30
31 #ifndef NVALGRIND
32 // -------------------------------------------------------------
33 // Annotations useful when implementing condition variables such as CondVar,
34 // using conditional critical sections (Await/LockWhen) and when constructing
35 // user-defined synchronization mechanisms.
36 //
37 // The annotations ANNOTATE_HAPPENS_BEFORE() and ANNOTATE_HAPPENS_AFTER() can
38 // be used to define happens-before arcs in user-defined synchronization
39 // mechanisms: the race detector will infer an arc from the former to the
40 // latter when they share the same argument pointer.
41 //
42 // Example 1 (reference counting):
43 //
44 // void Unref() {
45 // ANNOTATE_HAPPENS_BEFORE(&refcount_);
46 // if (AtomicDecrementByOne(&refcount_) == 0) {
47 // ANNOTATE_HAPPENS_AFTER(&refcount_);
48 // delete this;
49 // }
50 // }
51 //
52 // Example 2 (message queue):
53 //
54 // void MyQueue::Put(Type *e) {
55 // MutexLock lock(&mu_);
56 // ANNOTATE_HAPPENS_BEFORE(e);
57 // PutElementIntoMyQueue(e);
58 // }
59 //
60 // Type *MyQueue::Get() {
61 // MutexLock lock(&mu_);
62 // Type *e = GetElementFromMyQueue();
63 // ANNOTATE_HAPPENS_AFTER(e);
64 // return e;
65 // }
66 //
67 // Note: when possible, please use the existing reference counting and message
68 // queue implementations instead of inventing new ones.
69
70 // Report that wait on the condition variable at address "cv" has succeeded
71 // and the lock at address "lock" is held.
72 #define ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) \
73 AnnotateCondVarWait(__FILE__, __LINE__, cv, lock)
74
75 // Report that wait on the condition variable at "cv" has succeeded. Variant
76 // w/o lock.
77 #define ANNOTATE_CONDVAR_WAIT(cv) \
78 AnnotateCondVarWait(__FILE__, __LINE__, cv, NULL)
79
80 // Report that we are about to signal on the condition variable at address
81 // "cv".
82 #define ANNOTATE_CONDVAR_SIGNAL(cv) \
83 AnnotateCondVarSignal(__FILE__, __LINE__, cv)
84
85 // Report that we are about to signal_all on the condition variable at "cv".
86 #define ANNOTATE_CONDVAR_SIGNAL_ALL(cv) \
87 AnnotateCondVarSignalAll(__FILE__, __LINE__, cv)
88
89 // Annotations for user-defined synchronization mechanisms.
90 #define ANNOTATE_HAPPENS_BEFORE(obj) ANNOTATE_CONDVAR_SIGNAL(obj)
91 #define ANNOTATE_HAPPENS_AFTER(obj) ANNOTATE_CONDVAR_WAIT(obj)
92
93 // Report that the bytes in the range [pointer, pointer+size) are about
94 // to be published safely. The race checker will create a happens-before
95 // arc from the call ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size) to
96 // subsequent accesses to this memory.
97 #define ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size) \
98 AnnotatePublishMemoryRange(__FILE__, __LINE__, pointer, size)
99
100 // Instruct the tool to create a happens-before arc between mu->Unlock() and
101 // mu->Lock(). This annotation may slow down the race detector; normally it
102 // is used only when it would be difficult to annotate each of the mutex's
103 // critical sections individually using the annotations above.
104 #define ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) \
105 AnnotateMutexIsUsedAsCondVar(__FILE__, __LINE__, mu)
106
107 // -------------------------------------------------------------
108 // Annotations useful when defining memory allocators, or when memory that
109 // was protected in one way starts to be protected in another.
110
111 // Report that a new memory at "address" of size "size" has been allocated.
112 // This might be used when the memory has been retrieved from a free list and
113 // is about to be reused, or when a the locking discipline for a variable
114 // changes.
115 #define ANNOTATE_NEW_MEMORY(address, size) \
116 AnnotateNewMemory(__FILE__, __LINE__, address, size)
117
118 // -------------------------------------------------------------
119 // Annotations useful when defining FIFO queues that transfer data between
120 // threads.
121
122 // Report that the producer-consumer queue (such as ProducerConsumerQueue) at
123 // address "pcq" has been created. The ANNOTATE_PCQ_* annotations
124 // should be used only for FIFO queues. For non-FIFO queues use
125 // ANNOTATE_HAPPENS_BEFORE (for put) and ANNOTATE_HAPPENS_AFTER (for get).
126 #define ANNOTATE_PCQ_CREATE(pcq) \
127 AnnotatePCQCreate(__FILE__, __LINE__, pcq)
128
129 // Report that the queue at address "pcq" is about to be destroyed.
130 #define ANNOTATE_PCQ_DESTROY(pcq) \
131 AnnotatePCQDestroy(__FILE__, __LINE__, pcq)
132
133 // Report that we are about to put an element into a FIFO queue at address
134 // "pcq".
135 #define ANNOTATE_PCQ_PUT(pcq) \
136 AnnotatePCQPut(__FILE__, __LINE__, pcq)
137
138 // Report that we've just got an element from a FIFO queue at address "pcq".
139 #define ANNOTATE_PCQ_GET(pcq) \
140 AnnotatePCQGet(__FILE__, __LINE__, pcq)
141
142 // -------------------------------------------------------------
143 // Annotations that suppress errors. It is usually better to express the
144 // program's synchronization using the other annotations, but these can
145 // be used when all else fails.
146
147 // Report that we may have a benign race on at "address".
148 // Insert at the point where "address" has been allocated, preferably close
149 // to the point where the race happens.
150 // See also ANNOTATE_BENIGN_RACE_STATIC.
151 #define ANNOTATE_BENIGN_RACE(address, description) \
152 AnnotateBenignRace(__FILE__, __LINE__, address, description)
153
154 // Request the analysis tool to ignore all reads in the current thread
155 // until ANNOTATE_IGNORE_READS_END is called.
156 // Useful to ignore intentional racey reads, while still checking
157 // other reads and all writes.
158 // See also ANNOTATE_UNPROTECTED_READ.
159 #define ANNOTATE_IGNORE_READS_BEGIN() \
160 AnnotateIgnoreReadsBegin(__FILE__, __LINE__)
161
162 // Stop ignoring reads.
163 #define ANNOTATE_IGNORE_READS_END() \
164 AnnotateIgnoreReadsEnd(__FILE__, __LINE__)
165
166 // Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes.
167 #define ANNOTATE_IGNORE_WRITES_BEGIN() \
168 AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
169
170 // Stop ignoring writes.
171 #define ANNOTATE_IGNORE_WRITES_END() \
172 AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
173
174 // Start ignoring all memory accesses (reads and writes).
175 #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
176 do {\
177 ANNOTATE_IGNORE_READS_BEGIN();\
178 ANNOTATE_IGNORE_WRITES_BEGIN();\
179 }while(0)\
180
181 // Stop ignoring all memory accesses.
182 #define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
183 do {\
184 ANNOTATE_IGNORE_WRITES_END();\
185 ANNOTATE_IGNORE_READS_END();\
186 }while(0)\
187
188 // -------------------------------------------------------------
189 // Annotations useful for debugging.
190
191 // Request to trace every access to "address".
192 #define ANNOTATE_TRACE_MEMORY(address) \
193 AnnotateTraceMemory(__FILE__, __LINE__, address)
194
195 // Report the current thread name to a race detector.
196 #define ANNOTATE_THREAD_NAME(name) \
197 AnnotateThreadName(__FILE__, __LINE__, name)
198
199 // -------------------------------------------------------------
200 // Annotations useful when implementing locks. They are not
201 // normally needed by modules that merely use locks.
202 // The "lock" argument is a pointer to the lock object.
203
204 // Report that a lock has been created at address "lock".
205 #define ANNOTATE_RWLOCK_CREATE(lock) \
206 AnnotateRWLockCreate(__FILE__, __LINE__, lock)
207
208 // Report that the lock at address "lock" is about to be destroyed.
209 #define ANNOTATE_RWLOCK_DESTROY(lock) \
210 AnnotateRWLockDestroy(__FILE__, __LINE__, lock)
211
212 // Report that the lock at address "lock" has been acquired.
213 // is_w=1 for writer lock, is_w=0 for reader lock.
214 #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
215 AnnotateRWLockAcquired(__FILE__, __LINE__, lock, is_w)
216
217 // Report that the lock at address "lock" is about to be released.
218 #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
219 AnnotateRWLockReleased(__FILE__, __LINE__, lock, is_w)
220
221 // -------------------------------------------------------------
222 // Annotations useful for testing race detectors.
223
224 // Report that we expect a race on the variable at "address".
225 // Use only in unit tests for a race detector.
226 #define ANNOTATE_EXPECT_RACE(address, description) \
227 AnnotateExpectRace(__FILE__, __LINE__, address, description)
228
229 // A no-op. Insert where you like to test the interceptors.
230 #define ANNOTATE_NO_OP(arg) \
231 AnnotateNoOp(__FILE__, __LINE__, arg)
232
233 // Use the macros above rather than using these functions directly.
234 extern "C" void AnnotateRWLockCreate(const char *file, int line,
235 const volatile void *lock);
236 extern "C" void AnnotateRWLockDestroy(const char *file, int line,
237 const volatile void *lock);
238 extern "C" void AnnotateRWLockAcquired(const char *file, int line,
239 const volatile void *lock, long is_w);
240 extern "C" void AnnotateRWLockReleased(const char *file, int line,
241 const volatile void *lock, long is_w);
242 extern "C" void AnnotateCondVarWait(const char *file, int line,
243 const volatile void *cv,
244 const volatile void *lock);
245 extern "C" void AnnotateCondVarSignal(const char *file, int line,
246 const volatile void *cv);
247 extern "C" void AnnotateCondVarSignalAll(const char *file, int line,
248 const volatile void *cv);
249 extern "C" void AnnotatePublishMemoryRange(const char *file, int line,
250 const volatile void *address,
251 long size);
252 extern "C" void AnnotatePCQCreate(const char *file, int line,
253 const volatile void *pcq);
254 extern "C" void AnnotatePCQDestroy(const char *file, int line,
255 const volatile void *pcq);
256 extern "C" void AnnotatePCQPut(const char *file, int line,
257 const volatile void *pcq);
258 extern "C" void AnnotatePCQGet(const char *file, int line,
259 const volatile void *pcq);
260 extern "C" void AnnotateNewMemory(const char *file, int line,
261 const volatile void *address,
262 long size);
263 extern "C" void AnnotateExpectRace(const char *file, int line,
264 const volatile void *address,
265 const char *description);
266 extern "C" void AnnotateBenignRace(const char *file, int line,
267 const volatile void *address,
268 const char *description);
269 extern "C" void AnnotateMutexIsUsedAsCondVar(const char *file, int line,
270 const volatile void *mu);
271 extern "C" void AnnotateTraceMemory(const char *file, int line,
272 const volatile void *arg);
273 extern "C" void AnnotateThreadName(const char *file, int line,
274 const char *name);
275 extern "C" void AnnotateIgnoreReadsBegin(const char *file, int line);
276 extern "C" void AnnotateIgnoreReadsEnd(const char *file, int line);
277 extern "C" void AnnotateIgnoreWritesBegin(const char *file, int line);
278 extern "C" void AnnotateIgnoreWritesEnd(const char *file, int line);
279 extern "C" void AnnotateNoOp(const char *file, int line,
280 const volatile void *arg);
281
282 // ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
283 //
284 // Instead of doing
285 // ANNOTATE_IGNORE_READS_BEGIN();
286 // ... = x;
287 // ANNOTATE_IGNORE_READS_END();
288 // one can use
289 // ... = ANNOTATE_UNPROTECTED_READ(x);
290 template <class T>
ANNOTATE_UNPROTECTED_READ(const volatile T & x)291 inline T ANNOTATE_UNPROTECTED_READ(const volatile T &x) {
292 ANNOTATE_IGNORE_READS_BEGIN();
293 T res = x;
294 ANNOTATE_IGNORE_READS_END();
295 return res;
296 }
297
298 // Apply ANNOTATE_BENIGN_RACE to a static variable.
299 #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
300 namespace { \
301 class static_var ## _annotator { \
302 public: \
303 static_var ## _annotator() { \
304 ANNOTATE_BENIGN_RACE(&static_var, \
305 # static_var ": " description); \
306 } \
307 }; \
308 static static_var ## _annotator the ## static_var ## _annotator;\
309 }
310
311 #else
312 // NVALGRIND is defined, empty macros.
313
314 #define ANNOTATE_RWLOCK_CREATE(lock) // empty
315 #define ANNOTATE_RWLOCK_DESTROY(lock) // empty
316 #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
317 #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
318 #define ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) // empty
319 #define ANNOTATE_CONDVAR_WAIT(cv) // empty
320 #define ANNOTATE_CONDVAR_SIGNAL(cv) // empty
321 #define ANNOTATE_CONDVAR_SIGNAL_ALL(cv) // empty
322 #define ANNOTATE_HAPPENS_BEFORE(obj) // empty
323 #define ANNOTATE_HAPPENS_AFTER(obj) // empty
324 #define ANNOTATE_PUBLISH_MEMORY_RANGE(address, size) // empty
325 #define ANNOTATE_PUBLISH_OBJECT(address) // empty
326 #define ANNOTATE_PCQ_CREATE(pcq) // empty
327 #define ANNOTATE_PCQ_DESTROY(pcq) // empty
328 #define ANNOTATE_PCQ_PUT(pcq) // empty
329 #define ANNOTATE_PCQ_GET(pcq) // empty
330 #define ANNOTATE_NEW_MEMORY(address, size) // empty
331 #define ANNOTATE_EXPECT_RACE(address, description) // empty
332 #define ANNOTATE_BENIGN_RACE(address, description) // empty
333 #define ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) // empty
334 #define ANNOTATE_TRACE_MEMORY(arg) // empty
335 #define ANNOTATE_THREAD_NAME(name) // empty
336 #define ANNOTATE_IGNORE_READS_BEGIN() // empty
337 #define ANNOTATE_IGNORE_READS_END() // empty
338 #define ANNOTATE_IGNORE_WRITES_BEGIN() // empty
339 #define ANNOTATE_IGNORE_WRITES_END() // empty
340 #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
341 #define ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
342 #define ANNOTATE_NO_OP(arg) // empty
343 #define ANNOTATE_UNPROTECTED_READ(x) (x)
344 #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
345
346 #endif // NVALGRIND
347
348 // Return non-zero value if running under valgrind.
349 extern "C" int RunningOnValgrind();
350
351 #endif // BASE_DYNAMIC_ANNOTATIONS_H_
352