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