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
15 // This file defines dynamic annotations for use with dynamic analysis tool
16 // such as valgrind, PIN, etc.
17 //
18 // Dynamic annotation is a source code annotation that affects the generated
19 // code (that is, the annotation is not a comment). Each such annotation is
20 // attached to a particular instruction and/or to a particular object (address)
21 // in the program.
22 //
23 // The annotations that should be used by users are macros in all upper-case
24 // (e.g., ABSL_ANNOTATE_THREAD_NAME).
25 //
26 // Actual implementation of these macros may differ depending on the dynamic
27 // analysis tool being used.
28 //
29 // This file supports the following configurations:
30 // - Dynamic Annotations enabled (with static thread-safety warnings disabled).
31 // In this case, macros expand to functions implemented by Thread Sanitizer,
32 // when building with TSan. When not provided an external implementation,
33 // dynamic_annotations.cc provides no-op implementations.
34 //
35 // - Static Clang thread-safety warnings enabled.
36 // When building with a Clang compiler that supports thread-safety warnings,
37 // a subset of annotations can be statically-checked at compile-time. We
38 // expand these macros to static-inline functions that can be analyzed for
39 // thread-safety, but afterwards elided when building the final binary.
40 //
41 // - All annotations are disabled.
42 // If neither Dynamic Annotations nor Clang thread-safety warnings are
43 // enabled, then all annotation-macros expand to empty.
44
45 #ifndef ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
46 #define ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
47
48 #include <stddef.h>
49 #include <stdint.h>
50
51 #include "absl/base/attributes.h"
52 #include "absl/base/config.h"
53 #ifdef __cplusplus
54 #include "absl/base/macros.h"
55 #endif
56
57 #ifdef ABSL_HAVE_HWADDRESS_SANITIZER
58 #include <sanitizer/hwasan_interface.h>
59 #endif
60
61 // -------------------------------------------------------------------------
62 // Decide which features are enabled.
63
64 #ifdef ABSL_HAVE_THREAD_SANITIZER
65
66 #define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 1
67 #define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 1
68 #define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 1
69 #define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
70 #define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED 1
71
72 #else
73
74 #define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 0
75 #define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 0
76 #define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 0
77
78 // Clang provides limited support for static thread-safety analysis through a
79 // feature called Annotalysis. We configure macro-definitions according to
80 // whether Annotalysis support is available. When running in opt-mode, GCC
81 // will issue a warning, if these attributes are compiled. Only include them
82 // when compiling using Clang.
83
84 #if defined(__clang__)
85 #define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 1
86 #if !defined(SWIG)
87 #define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 1
88 #endif
89 #else
90 #define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
91 #endif
92
93 // Read/write annotations are enabled in Annotalysis mode; disabled otherwise.
94 #define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED \
95 ABSL_INTERNAL_ANNOTALYSIS_ENABLED
96
97 #endif // ABSL_HAVE_THREAD_SANITIZER
98
99 #ifdef __cplusplus
100 #define ABSL_INTERNAL_BEGIN_EXTERN_C extern "C" {
101 #define ABSL_INTERNAL_END_EXTERN_C } // extern "C"
102 #define ABSL_INTERNAL_GLOBAL_SCOPED(F) ::F
103 #define ABSL_INTERNAL_STATIC_INLINE inline
104 #else
105 #define ABSL_INTERNAL_BEGIN_EXTERN_C // empty
106 #define ABSL_INTERNAL_END_EXTERN_C // empty
107 #define ABSL_INTERNAL_GLOBAL_SCOPED(F) F
108 #define ABSL_INTERNAL_STATIC_INLINE static inline
109 #endif
110
111 // -------------------------------------------------------------------------
112 // Define race annotations.
113
114 #if ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 1
115 // Some of the symbols used in this section (e.g. AnnotateBenignRaceSized) are
116 // defined by the compiler-based sanitizer implementation, not by the Abseil
117 // library. Therefore they do not use ABSL_INTERNAL_C_SYMBOL.
118
119 // -------------------------------------------------------------
120 // Annotations that suppress errors. It is usually better to express the
121 // program's synchronization using the other annotations, but these can be used
122 // when all else fails.
123
124 // Report that we may have a benign race at `pointer`, with size
125 // "sizeof(*(pointer))". `pointer` must be a non-void* pointer. Insert at the
126 // point where `pointer` has been allocated, preferably close to the point
127 // where the race happens. See also ABSL_ANNOTATE_BENIGN_RACE_STATIC.
128 #define ABSL_ANNOTATE_BENIGN_RACE(pointer, description) \
129 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
130 (__FILE__, __LINE__, pointer, sizeof(*(pointer)), description)
131
132 // Same as ABSL_ANNOTATE_BENIGN_RACE(`address`, `description`), but applies to
133 // the memory range [`address`, `address`+`size`).
134 #define ABSL_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
135 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
136 (__FILE__, __LINE__, address, size, description)
137
138 // Enable (`enable`!=0) or disable (`enable`==0) race detection for all threads.
139 // This annotation could be useful if you want to skip expensive race analysis
140 // during some period of program execution, e.g. during initialization.
141 #define ABSL_ANNOTATE_ENABLE_RACE_DETECTION(enable) \
142 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateEnableRaceDetection) \
143 (__FILE__, __LINE__, enable)
144
145 // -------------------------------------------------------------
146 // Annotations useful for debugging.
147
148 // Report the current thread `name` to a race detector.
149 #define ABSL_ANNOTATE_THREAD_NAME(name) \
150 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateThreadName)(__FILE__, __LINE__, name)
151
152 // -------------------------------------------------------------
153 // Annotations useful when implementing locks. They are not normally needed by
154 // modules that merely use locks. The `lock` argument is a pointer to the lock
155 // object.
156
157 // Report that a lock has been created at address `lock`.
158 #define ABSL_ANNOTATE_RWLOCK_CREATE(lock) \
159 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
160
161 // Report that a linker initialized lock has been created at address `lock`.
162 #ifdef ABSL_HAVE_THREAD_SANITIZER
163 #define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
164 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreateStatic) \
165 (__FILE__, __LINE__, lock)
166 #else
167 #define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
168 ABSL_ANNOTATE_RWLOCK_CREATE(lock)
169 #endif
170
171 // Report that the lock at address `lock` is about to be destroyed.
172 #define ABSL_ANNOTATE_RWLOCK_DESTROY(lock) \
173 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
174
175 // Report that the lock at address `lock` has been acquired.
176 // `is_w`=1 for writer lock, `is_w`=0 for reader lock.
177 #define ABSL_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
178 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockAcquired) \
179 (__FILE__, __LINE__, lock, is_w)
180
181 // Report that the lock at address `lock` is about to be released.
182 // `is_w`=1 for writer lock, `is_w`=0 for reader lock.
183 #define ABSL_ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
184 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockReleased) \
185 (__FILE__, __LINE__, lock, is_w)
186
187 // Apply ABSL_ANNOTATE_BENIGN_RACE_SIZED to a static variable `static_var`.
188 #define ABSL_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
189 namespace { \
190 class static_var##_annotator { \
191 public: \
192 static_var##_annotator() { \
193 ABSL_ANNOTATE_BENIGN_RACE_SIZED(&static_var, sizeof(static_var), \
194 #static_var ": " description); \
195 } \
196 }; \
197 static static_var##_annotator the##static_var##_annotator; \
198 } // namespace
199
200 // Function prototypes of annotations provided by the compiler-based sanitizer
201 // implementation.
202 ABSL_INTERNAL_BEGIN_EXTERN_C
203 void AnnotateRWLockCreate(const char* file, int line,
204 const volatile void* lock);
205 void AnnotateRWLockCreateStatic(const char* file, int line,
206 const volatile void* lock);
207 void AnnotateRWLockDestroy(const char* file, int line,
208 const volatile void* lock);
209 void AnnotateRWLockAcquired(const char* file, int line,
210 const volatile void* lock, long is_w); // NOLINT
211 void AnnotateRWLockReleased(const char* file, int line,
212 const volatile void* lock, long is_w); // NOLINT
213 void AnnotateBenignRace(const char* file, int line,
214 const volatile void* address, const char* description);
215 void AnnotateBenignRaceSized(const char* file, int line,
216 const volatile void* address, size_t size,
217 const char* description);
218 void AnnotateThreadName(const char* file, int line, const char* name);
219 void AnnotateEnableRaceDetection(const char* file, int line, int enable);
220 ABSL_INTERNAL_END_EXTERN_C
221
222 #else // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 0
223
224 #define ABSL_ANNOTATE_RWLOCK_CREATE(lock) // empty
225 #define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) // empty
226 #define ABSL_ANNOTATE_RWLOCK_DESTROY(lock) // empty
227 #define ABSL_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
228 #define ABSL_ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
229 #define ABSL_ANNOTATE_BENIGN_RACE(address, description) // empty
230 #define ABSL_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) // empty
231 #define ABSL_ANNOTATE_THREAD_NAME(name) // empty
232 #define ABSL_ANNOTATE_ENABLE_RACE_DETECTION(enable) // empty
233 #define ABSL_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
234
235 #endif // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
236
237 // -------------------------------------------------------------------------
238 // Define memory annotations.
239
240 #ifdef ABSL_HAVE_MEMORY_SANITIZER
241
242 #include <sanitizer/msan_interface.h>
243
244 #define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
245 __msan_unpoison(address, size)
246
247 #define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
248 __msan_allocated_memory(address, size)
249
250 #else // !defined(ABSL_HAVE_MEMORY_SANITIZER)
251
252 // TODO(rogeeff): remove this branch
253 #ifdef ABSL_HAVE_THREAD_SANITIZER
254 #define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
255 do { \
256 (void)(address); \
257 (void)(size); \
258 } while (0)
259 #define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
260 do { \
261 (void)(address); \
262 (void)(size); \
263 } while (0)
264 #else
265
266 #define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) // empty
267 #define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) // empty
268
269 #endif
270
271 #endif // ABSL_HAVE_MEMORY_SANITIZER
272
273 // -------------------------------------------------------------------------
274 // Define IGNORE_READS_BEGIN/_END attributes.
275
276 #if defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
277
278 #define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE \
279 __attribute((exclusive_lock_function("*")))
280 #define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE \
281 __attribute((unlock_function("*")))
282
283 #else // !defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
284
285 #define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE // empty
286 #define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE // empty
287
288 #endif // defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
289
290 // -------------------------------------------------------------------------
291 // Define IGNORE_READS_BEGIN/_END annotations.
292
293 #if ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED == 1
294 // Some of the symbols used in this section (e.g. AnnotateIgnoreReadsBegin) are
295 // defined by the compiler-based implementation, not by the Abseil
296 // library. Therefore they do not use ABSL_INTERNAL_C_SYMBOL.
297
298 // Request the analysis tool to ignore all reads in the current thread until
299 // ABSL_ANNOTATE_IGNORE_READS_END is called. Useful to ignore intentional racey
300 // reads, while still checking other reads and all writes.
301 // See also ABSL_ANNOTATE_UNPROTECTED_READ.
302 #define ABSL_ANNOTATE_IGNORE_READS_BEGIN() \
303 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsBegin) \
304 (__FILE__, __LINE__)
305
306 // Stop ignoring reads.
307 #define ABSL_ANNOTATE_IGNORE_READS_END() \
308 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsEnd) \
309 (__FILE__, __LINE__)
310
311 // Function prototypes of annotations provided by the compiler-based sanitizer
312 // implementation.
313 ABSL_INTERNAL_BEGIN_EXTERN_C
314 void AnnotateIgnoreReadsBegin(const char* file, int line)
315 ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE;
316 void AnnotateIgnoreReadsEnd(const char* file,
317 int line) ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE;
318 ABSL_INTERNAL_END_EXTERN_C
319
320 #elif defined(ABSL_INTERNAL_ANNOTALYSIS_ENABLED)
321
322 // When Annotalysis is enabled without Dynamic Annotations, the use of
323 // static-inline functions allows the annotations to be read at compile-time,
324 // while still letting the compiler elide the functions from the final build.
325 //
326 // TODO(delesley) -- The exclusive lock here ignores writes as well, but
327 // allows IGNORE_READS_AND_WRITES to work properly.
328
329 #define ABSL_ANNOTATE_IGNORE_READS_BEGIN() \
330 ABSL_INTERNAL_GLOBAL_SCOPED( \
331 ABSL_INTERNAL_C_SYMBOL(AbslInternalAnnotateIgnoreReadsBegin)) \
332 ()
333
334 #define ABSL_ANNOTATE_IGNORE_READS_END() \
335 ABSL_INTERNAL_GLOBAL_SCOPED( \
336 ABSL_INTERNAL_C_SYMBOL(AbslInternalAnnotateIgnoreReadsEnd)) \
337 ()
338
339 ABSL_INTERNAL_STATIC_INLINE void ABSL_INTERNAL_C_SYMBOL(
340 AbslInternalAnnotateIgnoreReadsBegin)()
341 ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE {}
342
343 ABSL_INTERNAL_STATIC_INLINE void ABSL_INTERNAL_C_SYMBOL(
344 AbslInternalAnnotateIgnoreReadsEnd)()
345 ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE {}
346
347 #else
348
349 #define ABSL_ANNOTATE_IGNORE_READS_BEGIN() // empty
350 #define ABSL_ANNOTATE_IGNORE_READS_END() // empty
351
352 #endif
353
354 // -------------------------------------------------------------------------
355 // Define IGNORE_WRITES_BEGIN/_END annotations.
356
357 #if ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED == 1
358
359 // Similar to ABSL_ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead.
360 #define ABSL_ANNOTATE_IGNORE_WRITES_BEGIN() \
361 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
362
363 // Stop ignoring writes.
364 #define ABSL_ANNOTATE_IGNORE_WRITES_END() \
365 ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
366
367 // Function prototypes of annotations provided by the compiler-based sanitizer
368 // implementation.
369 ABSL_INTERNAL_BEGIN_EXTERN_C
370 void AnnotateIgnoreWritesBegin(const char* file, int line);
371 void AnnotateIgnoreWritesEnd(const char* file, int line);
372 ABSL_INTERNAL_END_EXTERN_C
373
374 #else
375
376 #define ABSL_ANNOTATE_IGNORE_WRITES_BEGIN() // empty
377 #define ABSL_ANNOTATE_IGNORE_WRITES_END() // empty
378
379 #endif
380
381 // -------------------------------------------------------------------------
382 // Define the ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
383 // primitive annotations defined above.
384 //
385 // Instead of doing
386 // ABSL_ANNOTATE_IGNORE_READS_BEGIN();
387 // ... = x;
388 // ABSL_ANNOTATE_IGNORE_READS_END();
389 // one can use
390 // ... = ABSL_ANNOTATE_UNPROTECTED_READ(x);
391
392 #if defined(ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED)
393
394 // Start ignoring all memory accesses (both reads and writes).
395 #define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
396 do { \
397 ABSL_ANNOTATE_IGNORE_READS_BEGIN(); \
398 ABSL_ANNOTATE_IGNORE_WRITES_BEGIN(); \
399 } while (0)
400
401 // Stop ignoring both reads and writes.
402 #define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END() \
403 do { \
404 ABSL_ANNOTATE_IGNORE_WRITES_END(); \
405 ABSL_ANNOTATE_IGNORE_READS_END(); \
406 } while (0)
407
408 #ifdef __cplusplus
409 // ABSL_ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
410 #define ABSL_ANNOTATE_UNPROTECTED_READ(x) \
411 absl::base_internal::AnnotateUnprotectedRead(x)
412
413 namespace absl {
414 ABSL_NAMESPACE_BEGIN
415 namespace base_internal {
416
417 template <typename T>
AnnotateUnprotectedRead(const volatile T & x)418 inline T AnnotateUnprotectedRead(const volatile T& x) { // NOLINT
419 ABSL_ANNOTATE_IGNORE_READS_BEGIN();
420 T res = x;
421 ABSL_ANNOTATE_IGNORE_READS_END();
422 return res;
423 }
424
425 } // namespace base_internal
426 ABSL_NAMESPACE_END
427 } // namespace absl
428 #endif
429
430 #else
431
432 #define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
433 #define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
434 #define ABSL_ANNOTATE_UNPROTECTED_READ(x) (x)
435
436 #endif
437
438 // -------------------------------------------------------------------------
439 // Address sanitizer annotations
440
441 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
442 // Describe the current state of a contiguous container such as e.g.
443 // std::vector or std::string. For more details see
444 // sanitizer/common_interface_defs.h, which is provided by the compiler.
445 #include <sanitizer/common_interface_defs.h>
446
447 #define ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
448 __sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
449 #define ABSL_ADDRESS_SANITIZER_REDZONE(name) \
450 struct { \
451 alignas(8) char x[8]; \
452 } name
453
454 #else
455
456 #define ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) // empty
457 #define ABSL_ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "")
458
459 #endif // ABSL_HAVE_ADDRESS_SANITIZER
460
461 // -------------------------------------------------------------------------
462 // HWAddress sanitizer annotations
463
464 #ifdef __cplusplus
465 namespace absl {
466 #ifdef ABSL_HAVE_HWADDRESS_SANITIZER
467 // Under HWASAN changes the tag of the pointer.
468 template <typename T>
HwasanTagPointer(T * ptr,uintptr_t tag)469 T* HwasanTagPointer(T* ptr, uintptr_t tag) {
470 return reinterpret_cast<T*>(__hwasan_tag_pointer(ptr, tag));
471 }
472 #else
473 template <typename T>
474 T* HwasanTagPointer(T* ptr, uintptr_t) {
475 return ptr;
476 }
477 #endif
478 } // namespace absl
479 #endif
480
481 // -------------------------------------------------------------------------
482 // Undefine the macros intended only for this file.
483
484 #undef ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
485 #undef ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED
486 #undef ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED
487 #undef ABSL_INTERNAL_ANNOTALYSIS_ENABLED
488 #undef ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED
489 #undef ABSL_INTERNAL_BEGIN_EXTERN_C
490 #undef ABSL_INTERNAL_END_EXTERN_C
491 #undef ABSL_INTERNAL_STATIC_INLINE
492
493 #endif // ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
494