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 // http://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
16 #ifndef ABSL_DEBUGGING_INTERNAL_ADDRESS_IS_READABLE_H_
17 #define ABSL_DEBUGGING_INTERNAL_ADDRESS_IS_READABLE_H_
18
19 namespace absl {
20 namespace debug_internal {
21
22 // Return whether the byte at *addr is readable, without faulting.
23 // Save and restores errno.
24 bool AddressIsReadable(const void *addr);
25
26 } // namespace debug_internal
27 } // namespace absl
28
29 #endif // ABSL_DEBUGGING_INTERNAL_ADDRESS_IS_READABLE_H_
30 /*
31 * Copyright 2017 The Abseil Authors.
32 *
33 * Licensed under the Apache License, Version 2.0 (the "License");
34 * you may not use this file except in compliance with the License.
35 * You may obtain a copy of the License at
36 *
37 * http://www.apache.org/licenses/LICENSE-2.0
38 *
39 * Unless required by applicable law or agreed to in writing, software
40 * distributed under the License is distributed on an "AS IS" BASIS,
41 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 * See the License for the specific language governing permissions and
43 * limitations under the License.
44 */
45
46 // Allow dynamic symbol lookup for in-memory Elf images.
47
48 #ifndef ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_
49 #define ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_
50
51 // Including this will define the __GLIBC__ macro if glibc is being
52 // used.
53 #include <climits>
54
55 // Maybe one day we can rewrite this file not to require the elf
56 // symbol extensions in glibc, but for right now we need them.
57 #ifdef ABSL_HAVE_ELF_MEM_IMAGE
58 #error ABSL_HAVE_ELF_MEM_IMAGE cannot be directly set
59 #endif
60
61 #if defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__) && \
62 !defined(__asmjs__)
63 #define ABSL_HAVE_ELF_MEM_IMAGE 1
64 #endif
65
66 #if ABSL_HAVE_ELF_MEM_IMAGE
67
68 #include <link.h> // for ElfW
69
70 namespace absl {
71 namespace debug_internal {
72
73 // An in-memory ELF image (may not exist on disk).
74 class ElfMemImage {
75 public:
76 // Sentinel: there could never be an elf image at this address.
77 static const void *const kInvalidBase;
78
79 // Information about a single vdso symbol.
80 // All pointers are into .dynsym, .dynstr, or .text of the VDSO.
81 // Do not free() them or modify through them.
82 struct SymbolInfo {
83 const char *name; // E.g. "__vdso_getcpu"
84 const char *version; // E.g. "LINUX_2.6", could be ""
85 // for unversioned symbol.
86 const void *address; // Relocated symbol address.
87 const ElfW(Sym) *symbol; // Symbol in the dynamic symbol table.
88 };
89
90 // Supports iteration over all dynamic symbols.
91 class SymbolIterator {
92 public:
93 friend class ElfMemImage;
94 const SymbolInfo *operator->() const;
95 const SymbolInfo &operator*() const;
96 SymbolIterator& operator++();
97 bool operator!=(const SymbolIterator &rhs) const;
98 bool operator==(const SymbolIterator &rhs) const;
99 private:
100 SymbolIterator(const void *const image, int index);
101 void Update(int incr);
102 SymbolInfo info_;
103 int index_;
104 const void *const image_;
105 };
106
107
108 explicit ElfMemImage(const void *base);
109 void Init(const void *base);
IsPresent() const110 bool IsPresent() const { return ehdr_ != nullptr; }
111 const ElfW(Phdr)* GetPhdr(int index) const;
112 const ElfW(Sym)* GetDynsym(int index) const;
113 const ElfW(Versym)* GetVersym(int index) const;
114 const ElfW(Verdef)* GetVerdef(int index) const;
115 const ElfW(Verdaux)* GetVerdefAux(const ElfW(Verdef) *verdef) const;
116 const char* GetDynstr(ElfW(Word) offset) const;
117 const void* GetSymAddr(const ElfW(Sym) *sym) const;
118 const char* GetVerstr(ElfW(Word) offset) const;
119 int GetNumSymbols() const;
120
121 SymbolIterator begin() const;
122 SymbolIterator end() const;
123
124 // Look up versioned dynamic symbol in the image.
125 // Returns false if image is not present, or doesn't contain given
126 // symbol/version/type combination.
127 // If info_out is non-null, additional details are filled in.
128 bool LookupSymbol(const char *name, const char *version,
129 int symbol_type, SymbolInfo *info_out) const;
130
131 // Find info about symbol (if any) which overlaps given address.
132 // Returns true if symbol was found; false if image isn't present
133 // or doesn't have a symbol overlapping given address.
134 // If info_out is non-null, additional details are filled in.
135 bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
136
137 private:
138 const ElfW(Ehdr) *ehdr_;
139 const ElfW(Sym) *dynsym_;
140 const ElfW(Versym) *versym_;
141 const ElfW(Verdef) *verdef_;
142 const ElfW(Word) *hash_;
143 const char *dynstr_;
144 size_t strsize_;
145 size_t verdefnum_;
146 ElfW(Addr) link_base_; // Link-time base (p_vaddr of first PT_LOAD).
147 };
148
149 } // namespace debug_internal
150 } // namespace absl
151
152 #endif // ABSL_HAVE_ELF_MEM_IMAGE
153
154 #endif // ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_
155 /*
156 * Copyright 2017 The Abseil Authors.
157 *
158 * Licensed under the Apache License, Version 2.0 (the "License");
159 * you may not use this file except in compliance with the License.
160 * You may obtain a copy of the License at
161 *
162 * http://www.apache.org/licenses/LICENSE-2.0
163 *
164 * Unless required by applicable law or agreed to in writing, software
165 * distributed under the License is distributed on an "AS IS" BASIS,
166 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
167 * See the License for the specific language governing permissions and
168 * limitations under the License.
169
170 * Defines ABSL_STACKTRACE_INL_HEADER to the *-inl.h containing
171 * actual unwinder implementation.
172 * This header is "private" to stacktrace.cc.
173 * DO NOT include it into any other files.
174 */
175 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
176 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
177
178 // First, test platforms which only support a stub.
179 #if ABSL_STACKTRACE_INL_HEADER
180 #error ABSL_STACKTRACE_INL_HEADER cannot be directly set
181 #elif defined(__native_client__) || defined(__APPLE__) || \
182 defined(__ANDROID__) || defined(__myriad2__) || defined(asmjs__) || \
183 defined(__Fuchsia__)
184 #define ABSL_STACKTRACE_INL_HEADER \
185 "stacktrace_internal/stacktrace_unimplemented-inl.inc"
186
187 // Next, test for Mips and Windows.
188 // TODO(marmstrong): Mips case, remove the check for ABSL_STACKTRACE_INL_HEADER
189 #elif defined(__mips__) && !defined(ABSL_STACKTRACE_INL_HEADER)
190 #define ABSL_STACKTRACE_INL_HEADER \
191 "stacktrace_internal/stacktrace_unimplemented-inl.inc"
192 #elif defined(_WIN32) // windows
193 #define ABSL_STACKTRACE_INL_HEADER \
194 "stacktrace_internal/stacktrace_win32-inl.inc"
195
196 // Finally, test NO_FRAME_POINTER.
197 #elif !defined(NO_FRAME_POINTER)
198 # if defined(__i386__) || defined(__x86_64__)
199 #define ABSL_STACKTRACE_INL_HEADER \
200 "stacktrace_internal/stacktrace_x86-inl.inc"
201 # elif defined(__ppc__) || defined(__PPC__)
202 #define ABSL_STACKTRACE_INL_HEADER \
203 "stacktrace_internal/stacktrace_powerpc-inl.inc"
204 # elif defined(__aarch64__)
205 #define ABSL_STACKTRACE_INL_HEADER \
206 "stacktrace_internal/stacktrace_aarch64-inl.inc"
207 # elif defined(__arm__)
208 #define ABSL_STACKTRACE_INL_HEADER \
209 "stacktrace_internal/stacktrace_arm-inl.inc"
210 # endif
211 #else // defined(NO_FRAME_POINTER)
212 # if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
213 #define ABSL_STACKTRACE_INL_HEADER \
214 "stacktrace_internal/stacktrace_unimplemented-inl.inc"
215 # elif defined(__ppc__) || defined(__PPC__)
216 // Use glibc's backtrace.
217 #define ABSL_STACKTRACE_INL_HEADER \
218 "stacktrace_internal/stacktrace_generic-inl.inc"
219 # elif defined(__arm__)
220 # error stacktrace without frame pointer is not supported on ARM
221 # endif
222 #endif // NO_FRAME_POINTER
223
224 #if !defined(ABSL_STACKTRACE_INL_HEADER)
225 #error Not supported yet
226 #endif
227
228 #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
229 //
230 // Copyright 2017 The Abseil Authors.
231 //
232 // Licensed under the Apache License, Version 2.0 (the "License");
233 // you may not use this file except in compliance with the License.
234 // You may obtain a copy of the License at
235 //
236 // http://www.apache.org/licenses/LICENSE-2.0
237 //
238 // Unless required by applicable law or agreed to in writing, software
239 // distributed under the License is distributed on an "AS IS" BASIS,
240 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
241 // See the License for the specific language governing permissions and
242 // limitations under the License.
243 //
244
245 // Allow dynamic symbol lookup in the kernel VDSO page.
246 //
247 // VDSO stands for "Virtual Dynamic Shared Object" -- a page of
248 // executable code, which looks like a shared library, but doesn't
249 // necessarily exist anywhere on disk, and which gets mmap()ed into
250 // every process by kernels which support VDSO, such as 2.6.x for 32-bit
251 // executables, and 2.6.24 and above for 64-bit executables.
252 //
253 // More details could be found here:
254 // http://www.trilithium.com/johan/2005/08/linux-gate/
255 //
256 // VDSOSupport -- a class representing kernel VDSO (if present).
257 //
258 // Example usage:
259 // VDSOSupport vdso;
260 // VDSOSupport::SymbolInfo info;
261 // typedef (*FN)(unsigned *, void *, void *);
262 // FN fn = nullptr;
263 // if (vdso.LookupSymbol("__vdso_getcpu", "LINUX_2.6", STT_FUNC, &info)) {
264 // fn = reinterpret_cast<FN>(info.address);
265 // }
266
267 #ifndef ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_
268 #define ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_
269
270 #include <atomic>
271
272
273 #ifdef ABSL_HAVE_ELF_MEM_IMAGE
274
275 #ifdef ABSL_HAVE_VDSO_SUPPORT
276 #error ABSL_HAVE_VDSO_SUPPORT cannot be directly set
277 #else
278 #define ABSL_HAVE_VDSO_SUPPORT 1
279 #endif
280
281 namespace absl {
282 namespace debug_internal {
283
284 // NOTE: this class may be used from within tcmalloc, and can not
285 // use any memory allocation routines.
286 class VDSOSupport {
287 public:
288 VDSOSupport();
289
290 typedef ElfMemImage::SymbolInfo SymbolInfo;
291 typedef ElfMemImage::SymbolIterator SymbolIterator;
292
293 // On PowerPC64 VDSO symbols can either be of type STT_FUNC or STT_NOTYPE
294 // depending on how the kernel is built. The kernel is normally built with
295 // STT_NOTYPE type VDSO symbols. Let's make things simpler first by using a
296 // compile-time constant.
297 #ifdef __powerpc64__
298 enum { kVDSOSymbolType = STT_NOTYPE };
299 #else
300 enum { kVDSOSymbolType = STT_FUNC };
301 #endif
302
303 // Answers whether we have a vdso at all.
IsPresent() const304 bool IsPresent() const { return image_.IsPresent(); }
305
306 // Allow to iterate over all VDSO symbols.
begin() const307 SymbolIterator begin() const { return image_.begin(); }
end() const308 SymbolIterator end() const { return image_.end(); }
309
310 // Look up versioned dynamic symbol in the kernel VDSO.
311 // Returns false if VDSO is not present, or doesn't contain given
312 // symbol/version/type combination.
313 // If info_out != nullptr, additional details are filled in.
314 bool LookupSymbol(const char *name, const char *version,
315 int symbol_type, SymbolInfo *info_out) const;
316
317 // Find info about symbol (if any) which overlaps given address.
318 // Returns true if symbol was found; false if VDSO isn't present
319 // or doesn't have a symbol overlapping given address.
320 // If info_out != nullptr, additional details are filled in.
321 bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
322
323 // Used only for testing. Replace real VDSO base with a mock.
324 // Returns previous value of vdso_base_. After you are done testing,
325 // you are expected to call SetBase() with previous value, in order to
326 // reset state to the way it was.
327 const void *SetBase(const void *s);
328
329 // Computes vdso_base_ and returns it. Should be called as early as
330 // possible; before any thread creation, chroot or setuid.
331 static const void *Init();
332
333 private:
334 // image_ represents VDSO ELF image in memory.
335 // image_.ehdr_ == nullptr implies there is no VDSO.
336 ElfMemImage image_;
337
338 // Cached value of auxv AT_SYSINFO_EHDR, computed once.
339 // This is a tri-state:
340 // kInvalidBase => value hasn't been determined yet.
341 // 0 => there is no VDSO.
342 // else => vma of VDSO Elf{32,64}_Ehdr.
343 //
344 // When testing with mock VDSO, low bit is set.
345 // The low bit is always available because vdso_base_ is
346 // page-aligned.
347 static std::atomic<const void *> vdso_base_;
348
349 // NOLINT on 'long' because these routines mimic kernel api.
350 // The 'cache' parameter may be used by some versions of the kernel,
351 // and should be nullptr or point to a static buffer containing at
352 // least two 'long's.
353 static long InitAndGetCPU(unsigned *cpu, void *cache, // NOLINT 'long'.
354 void *unused);
355 static long GetCPUViaSyscall(unsigned *cpu, void *cache, // NOLINT 'long'.
356 void *unused);
357 typedef long (*GetCpuFn)(unsigned *cpu, void *cache, // NOLINT 'long'.
358 void *unused);
359
360 // This function pointer may point to InitAndGetCPU,
361 // GetCPUViaSyscall, or __vdso_getcpu at different stages of initialization.
362 static std::atomic<GetCpuFn> getcpu_fn_;
363
364 friend int GetCPU(void); // Needs access to getcpu_fn_.
365
366 VDSOSupport(const VDSOSupport&) = delete;
367 VDSOSupport& operator=(const VDSOSupport&) = delete;
368 };
369
370 // Same as sched_getcpu() on later glibc versions.
371 // Return current CPU, using (fast) __vdso_getcpu@LINUX_2.6 if present,
372 // otherwise use syscall(SYS_getcpu,...).
373 // May return -1 with errno == ENOSYS if the kernel doesn't
374 // support SYS_getcpu.
375 int GetCPU();
376
377 } // namespace debug_internal
378 } // namespace absl
379
380 #endif // ABSL_HAVE_ELF_MEM_IMAGE
381
382 #endif // ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_
383 // Copyright 2017 The Abseil Authors.
384 //
385 // Licensed under the Apache License, Version 2.0 (the "License");
386 // you may not use this file except in compliance with the License.
387 // You may obtain a copy of the License at
388 //
389 // http://www.apache.org/licenses/LICENSE-2.0
390 //
391 // Unless required by applicable law or agreed to in writing, software
392 // distributed under the License is distributed on an "AS IS" BASIS,
393 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
394 // See the License for the specific language governing permissions and
395 // limitations under the License.
396 //
397 // This header file defines macros for declaring attributes for functions,
398 // types, and variables.
399 //
400 // These macros are used within Abseil and allow the compiler to optimize, where
401 // applicable, certain function calls.
402 //
403 // This file is used for both C and C++!
404 //
405 // Most macros here are exposing GCC or Clang features, and are stubbed out for
406 // other compilers.
407 //
408 // GCC attributes documentation:
409 // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html
410 // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Variable-Attributes.html
411 // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Type-Attributes.html
412 //
413 // Most attributes in this file are already supported by GCC 4.7. However, some
414 // of them are not supported in older version of Clang. Thus, we check
415 // `__has_attribute()` first. If the check fails, we check if we are on GCC and
416 // assume the attribute exists on GCC (which is verified on GCC 4.7).
417 //
418 // -----------------------------------------------------------------------------
419 // Sanitizer Attributes
420 // -----------------------------------------------------------------------------
421 //
422 // Sanitizer-related attributes are not "defined" in this file (and indeed
423 // are not defined as such in any file). To utilize the following
424 // sanitizer-related attributes within your builds, define the following macros
425 // within your build using a `-D` flag, along with the given value for
426 // `-fsanitize`:
427 //
428 // * `ADDRESS_SANITIZER` + `-fsanitize=address` (Clang, GCC 4.8)
429 // * `MEMORY_SANITIZER` + `-fsanitize=memory` (Clang-only)
430 // * `THREAD_SANITIZER + `-fsanitize=thread` (Clang, GCC 4.8+)
431 // * `UNDEFINED_BEHAVIOR_SANITIZER` + `-fsanitize=undefined` (Clang, GCC 4.9+)
432 // * `CONTROL_FLOW_INTEGRITY` + -fsanitize=cfi (Clang-only)
433 //
434 // Example:
435 //
436 // // Enable branches in the Abseil code that are tagged for ASan:
437 // $ bazel -D ADDRESS_SANITIZER -fsanitize=address *target*
438 //
439 // Since these macro names are only supported by GCC and Clang, we only check
440 // for `__GNUC__` (GCC or Clang) and the above macros.
441 #ifndef ABSL_BASE_ATTRIBUTES_H_
442 #define ABSL_BASE_ATTRIBUTES_H_
443
444 // ABSL_HAVE_ATTRIBUTE
445 //
446 // A function-like feature checking macro that is a wrapper around
447 // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
448 // nonzero constant integer if the attribute is supported or 0 if not.
449 //
450 // It evaluates to zero if `__has_attribute` is not defined by the compiler.
451 //
452 // GCC: https://gcc.gnu.org/gcc-5/changes.html
453 // Clang: https://clang.llvm.org/docs/LanguageExtensions.html
454 #ifdef __has_attribute
455 #define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x)
456 #else
457 #define ABSL_HAVE_ATTRIBUTE(x) 0
458 #endif
459
460 // ABSL_HAVE_CPP_ATTRIBUTE
461 //
462 // A function-like feature checking macro that accepts C++11 style attributes.
463 // It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
464 // (http://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
465 // find `__has_cpp_attribute`, will evaluate to 0.
466 #if defined(__cplusplus) && defined(__has_cpp_attribute)
467 // NOTE: requiring __cplusplus above should not be necessary, but
468 // works around https://bugs.llvm.org/show_bug.cgi?id=23435.
469 #define ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
470 #else
471 #define ABSL_HAVE_CPP_ATTRIBUTE(x) 0
472 #endif
473
474 // -----------------------------------------------------------------------------
475 // Function Attributes
476 // -----------------------------------------------------------------------------
477 //
478 // GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
479 // Clang: https://clang.llvm.org/docs/AttributeReference.html
480
481 // ABSL_PRINTF_ATTRIBUTE
482 // ABSL_SCANF_ATTRIBUTE
483 //
484 // Tells the compiler to perform `printf` format std::string checking if the
485 // compiler supports it; see the 'format' attribute in
486 // <http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>.
487 //
488 // Note: As the GCC manual states, "[s]ince non-static C++ methods
489 // have an implicit 'this' argument, the arguments of such methods
490 // should be counted from two, not one."
491 #if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__))
492 #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
493 __attribute__((__format__(__printf__, string_index, first_to_check)))
494 #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
495 __attribute__((__format__(__scanf__, string_index, first_to_check)))
496 #else
497 #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
498 #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
499 #endif
500
501 // ABSL_ATTRIBUTE_ALWAYS_INLINE
502 // ABSL_ATTRIBUTE_NOINLINE
503 //
504 // Forces functions to either inline or not inline. Introduced in gcc 3.1.
505 #if ABSL_HAVE_ATTRIBUTE(always_inline) || \
506 (defined(__GNUC__) && !defined(__clang__))
507 #define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
508 #define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1
509 #else
510 #define ABSL_ATTRIBUTE_ALWAYS_INLINE
511 #endif
512
513 #if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
514 #define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline))
515 #define ABSL_HAVE_ATTRIBUTE_NOINLINE 1
516 #else
517 #define ABSL_ATTRIBUTE_NOINLINE
518 #endif
519
520 // ABSL_ATTRIBUTE_NO_TAIL_CALL
521 //
522 // Prevents the compiler from optimizing away stack frames for functions which
523 // end in a call to another function.
524 #if ABSL_HAVE_ATTRIBUTE(disable_tail_calls)
525 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
526 #define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls))
527 #elif defined(__GNUC__) && !defined(__clang__)
528 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
529 #define ABSL_ATTRIBUTE_NO_TAIL_CALL \
530 __attribute__((optimize("no-optimize-sibling-calls")))
531 #else
532 #define ABSL_ATTRIBUTE_NO_TAIL_CALL
533 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0
534 #endif
535
536 // ABSL_ATTRIBUTE_WEAK
537 //
538 // Tags a function as weak for the purposes of compilation and linking.
539 #if ABSL_HAVE_ATTRIBUTE(weak) || (defined(__GNUC__) && !defined(__clang__))
540 #undef ABSL_ATTRIBUTE_WEAK
541 #define ABSL_ATTRIBUTE_WEAK __attribute__((weak))
542 #define ABSL_HAVE_ATTRIBUTE_WEAK 1
543 #else
544 #define ABSL_ATTRIBUTE_WEAK
545 #define ABSL_HAVE_ATTRIBUTE_WEAK 0
546 #endif
547
548 // ABSL_ATTRIBUTE_NONNULL
549 //
550 // Tells the compiler either (a) that a particular function parameter
551 // should be a non-null pointer, or (b) that all pointer arguments should
552 // be non-null.
553 //
554 // Note: As the GCC manual states, "[s]ince non-static C++ methods
555 // have an implicit 'this' argument, the arguments of such methods
556 // should be counted from two, not one."
557 //
558 // Args are indexed starting at 1.
559 //
560 // For non-static class member functions, the implicit `this` argument
561 // is arg 1, and the first explicit argument is arg 2. For static class member
562 // functions, there is no implicit `this`, and the first explicit argument is
563 // arg 1.
564 //
565 // Example:
566 //
567 // /* arg_a cannot be null, but arg_b can */
568 // void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1);
569 //
570 // class C {
571 // /* arg_a cannot be null, but arg_b can */
572 // void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2);
573 //
574 // /* arg_a cannot be null, but arg_b can */
575 // static void StaticMethod(void* arg_a, void* arg_b)
576 // ABSL_ATTRIBUTE_NONNULL(1);
577 // };
578 //
579 // If no arguments are provided, then all pointer arguments should be non-null.
580 //
581 // /* No pointer arguments may be null. */
582 // void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL();
583 //
584 // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
585 // ABSL_ATTRIBUTE_NONNULL does not.
586 #if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
587 #define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
588 #else
589 #define ABSL_ATTRIBUTE_NONNULL(...)
590 #endif
591
592 // ABSL_ATTRIBUTE_NORETURN
593 //
594 // Tells the compiler that a given function never returns.
595 #if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
596 #define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
597 #elif defined(_MSC_VER)
598 #define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
599 #else
600 #define ABSL_ATTRIBUTE_NORETURN
601 #endif
602
603 // ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
604 //
605 // Tells the AddressSanitizer (or other memory testing tools) to ignore a given
606 // function. Useful for cases when a function reads random locations on stack,
607 // calls _exit from a cloned subprocess, deliberately accesses buffer
608 // out of bounds or does other scary things with memory.
609 // NOTE: GCC supports AddressSanitizer(asan) since 4.8.
610 // https://gcc.gnu.org/gcc-4.8/changes.html
611 #if defined(__GNUC__) && defined(ADDRESS_SANITIZER)
612 #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
613 #else
614 #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
615 #endif
616
617 // ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
618 //
619 // Tells the MemorySanitizer to relax the handling of a given function. All
620 // "Use of uninitialized value" warnings from such functions will be suppressed,
621 // and all values loaded from memory will be considered fully initialized.
622 // This attribute is similar to the ADDRESS_SANITIZER attribute above, but deals
623 // with initialized-ness rather than addressability issues.
624 // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
625 #if defined(__GNUC__) && defined(MEMORY_SANITIZER)
626 #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
627 #else
628 #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
629 #endif
630
631 // ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
632 //
633 // Tells the ThreadSanitizer to not instrument a given function.
634 // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
635 // https://gcc.gnu.org/gcc-4.8/changes.html
636 #if defined(__GNUC__) && defined(THREAD_SANITIZER)
637 #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
638 #else
639 #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
640 #endif
641
642 // ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
643 //
644 // Tells the UndefinedSanitizer to ignore a given function. Useful for cases
645 // where certain behavior (eg. devision by zero) is being used intentionally.
646 // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
647 // https://gcc.gnu.org/gcc-4.9/changes.html
648 #if defined(__GNUC__) && \
649 (defined(UNDEFINED_BEHAVIOR_SANITIZER) || defined(ADDRESS_SANITIZER))
650 #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
651 __attribute__((no_sanitize("undefined")))
652 #else
653 #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
654 #endif
655
656 // ABSL_ATTRIBUTE_NO_SANITIZE_CFI
657 //
658 // Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
659 // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
660 #if defined(__GNUC__) && defined(CONTROL_FLOW_INTEGRITY)
661 #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
662 #else
663 #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
664 #endif
665
666 // ABSL_HAVE_ATTRIBUTE_SECTION
667 //
668 // Indicates whether labeled sections are supported. Labeled sections are not
669 // supported on Darwin/iOS.
670 #ifdef ABSL_HAVE_ATTRIBUTE_SECTION
671 #error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
672 #elif (ABSL_HAVE_ATTRIBUTE(section) || \
673 (defined(__GNUC__) && !defined(__clang__))) && \
674 !defined(__APPLE__)
675 #define ABSL_HAVE_ATTRIBUTE_SECTION 1
676
677 // ABSL_ATTRIBUTE_SECTION
678 //
679 // Tells the compiler/linker to put a given function into a section and define
680 // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
681 // This functionality is supported by GNU linker. Any function annotated with
682 // `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
683 // whatever section its caller is placed into.
684 //
685 #ifndef ABSL_ATTRIBUTE_SECTION
686 #define ABSL_ATTRIBUTE_SECTION(name) \
687 __attribute__((section(#name))) __attribute__((noinline))
688 #endif
689
690 // ABSL_ATTRIBUTE_SECTION_VARIABLE
691 //
692 // Tells the compiler/linker to put a given variable into a section and define
693 // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
694 // This functionality is supported by GNU linker.
695 #ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
696 #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
697 #endif
698
699 // ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
700 //
701 // A weak section declaration to be used as a global declaration
702 // for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
703 // even without functions with ABSL_ATTRIBUTE_SECTION(name).
704 // ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
705 // a no-op on ELF but not on Mach-O.
706 //
707 #ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
708 #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
709 extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
710 extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
711 #endif
712 #ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
713 #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
714 #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
715 #endif
716
717 // ABSL_ATTRIBUTE_SECTION_START
718 //
719 // Returns `void*` pointers to start/end of a section of code with
720 // functions having ABSL_ATTRIBUTE_SECTION(name).
721 // Returns 0 if no such functions exist.
722 // One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
723 // link.
724 //
725 #define ABSL_ATTRIBUTE_SECTION_START(name) \
726 (reinterpret_cast<void *>(__start_##name))
727 #define ABSL_ATTRIBUTE_SECTION_STOP(name) \
728 (reinterpret_cast<void *>(__stop_##name))
729 #else // !ABSL_HAVE_ATTRIBUTE_SECTION
730
731 #define ABSL_HAVE_ATTRIBUTE_SECTION 0
732
733 // provide dummy definitions
734 #define ABSL_ATTRIBUTE_SECTION(name)
735 #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
736 #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
737 #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
738 #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
739 #define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
740 #define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
741 #endif // ABSL_ATTRIBUTE_SECTION
742
743 // ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
744 //
745 // Support for aligning the stack on 32-bit x86.
746 #if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
747 (defined(__GNUC__) && !defined(__clang__))
748 #if defined(__i386__)
749 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
750 __attribute__((force_align_arg_pointer))
751 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
752 #elif defined(__x86_64__)
753 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
754 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
755 #else // !__i386__ && !__x86_64
756 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
757 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
758 #endif // __i386__
759 #else
760 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
761 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
762 #endif
763
764 // ABSL_MUST_USE_RESULT
765 //
766 // Tells the compiler to warn about unused return values for functions declared
767 // with this macro. The macro must appear as the very first part of a function
768 // declaration or definition:
769 //
770 // Example:
771 //
772 // ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
773 //
774 // This placement has the broadest compatibility with GCC, Clang, and MSVC, with
775 // both defs and decls, and with GCC-style attributes, MSVC declspec, C++11
776 // and C++17 attributes.
777 //
778 // ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
779 // warning. For that, warn_unused_result is used only for clang but not for gcc.
780 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
781 //
782 // Note: past advice was to place the macro after the argument list.
783 #if ABSL_HAVE_ATTRIBUTE(nodiscard)
784 #define ABSL_MUST_USE_RESULT [[nodiscard]]
785 #elif defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
786 #define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
787 #else
788 #define ABSL_MUST_USE_RESULT
789 #endif
790
791 // ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
792 //
793 // Tells GCC that a function is hot or cold. GCC can use this information to
794 // improve static analysis, i.e. a conditional branch to a cold function
795 // is likely to be not-taken.
796 // This annotation is used for function declarations.
797 //
798 // Example:
799 //
800 // int foo() ABSL_ATTRIBUTE_HOT;
801 #if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
802 #define ABSL_ATTRIBUTE_HOT __attribute__((hot))
803 #else
804 #define ABSL_ATTRIBUTE_HOT
805 #endif
806
807 #if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
808 #define ABSL_ATTRIBUTE_COLD __attribute__((cold))
809 #else
810 #define ABSL_ATTRIBUTE_COLD
811 #endif
812
813 // ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
814 //
815 // We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
816 // macro used as an attribute to mark functions that must always or never be
817 // instrumented by XRay. Currently, this is only supported in Clang/LLVM.
818 //
819 // For reference on the LLVM XRay instrumentation, see
820 // http://llvm.org/docs/XRay.html.
821 //
822 // A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
823 // will always get the XRay instrumentation sleds. These sleds may introduce
824 // some binary size and runtime overhead and must be used sparingly.
825 //
826 // These attributes only take effect when the following conditions are met:
827 //
828 // * The file/target is built in at least C++11 mode, with a Clang compiler
829 // that supports XRay attributes.
830 // * The file/target is built with the -fxray-instrument flag set for the
831 // Clang/LLVM compiler.
832 // * The function is defined in the translation unit (the compiler honors the
833 // attribute in either the definition or the declaration, and must match).
834 //
835 // There are cases when, even when building with XRay instrumentation, users
836 // might want to control specifically which functions are instrumented for a
837 // particular build using special-case lists provided to the compiler. These
838 // special case lists are provided to Clang via the
839 // -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
840 // attributes in source take precedence over these special-case lists.
841 //
842 // To disable the XRay attributes at build-time, users may define
843 // ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
844 // packages/targets, as this may lead to conflicting definitions of functions at
845 // link-time.
846 //
847 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
848 !defined(ABSL_NO_XRAY_ATTRIBUTES)
849 #define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
850 #define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
851 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
852 #define ABSL_XRAY_LOG_ARGS(N) \
853 [[clang::xray_always_instrument, clang::xray_log_args(N)]]
854 #else
855 #define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
856 #endif
857 #else
858 #define ABSL_XRAY_ALWAYS_INSTRUMENT
859 #define ABSL_XRAY_NEVER_INSTRUMENT
860 #define ABSL_XRAY_LOG_ARGS(N)
861 #endif
862
863 // -----------------------------------------------------------------------------
864 // Variable Attributes
865 // -----------------------------------------------------------------------------
866
867 // ABSL_ATTRIBUTE_UNUSED
868 //
869 // Prevents the compiler from complaining about or optimizing away variables
870 // that appear unused.
871 #if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
872 #undef ABSL_ATTRIBUTE_UNUSED
873 #define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
874 #else
875 #define ABSL_ATTRIBUTE_UNUSED
876 #endif
877
878 // ABSL_ATTRIBUTE_INITIAL_EXEC
879 //
880 // Tells the compiler to use "initial-exec" mode for a thread-local variable.
881 // See http://people.redhat.com/drepper/tls.pdf for the gory details.
882 #if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
883 #define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
884 #else
885 #define ABSL_ATTRIBUTE_INITIAL_EXEC
886 #endif
887
888 // ABSL_ATTRIBUTE_PACKED
889 //
890 // Prevents the compiler from padding a structure to natural alignment
891 #if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
892 #define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
893 #else
894 #define ABSL_ATTRIBUTE_PACKED
895 #endif
896
897 // ABSL_CONST_INIT
898 //
899 // A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
900 // not compile (on supported platforms) unless the variable has a constant
901 // initializer. This is useful for variables with static and thread storage
902 // duration, because it guarantees that they will not suffer from the so-called
903 // "static init order fiasco".
904 //
905 // Example:
906 //
907 // ABSL_CONST_INIT static MyType my_var = MakeMyType(...);
908 //
909 // Note that this attribute is redundant if the variable is declared constexpr.
910 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
911 // NOLINTNEXTLINE(whitespace/braces)
912 #define ABSL_CONST_INIT [[clang::require_constant_initialization]]
913 #else
914 #define ABSL_CONST_INIT
915 #endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
916
917 #endif // ABSL_BASE_ATTRIBUTES_H_
918 //
919 // Copyright 2017 The Abseil Authors.
920 //
921 // Licensed under the Apache License, Version 2.0 (the "License");
922 // you may not use this file except in compliance with the License.
923 // You may obtain a copy of the License at
924 //
925 // http://www.apache.org/licenses/LICENSE-2.0
926 //
927 // Unless required by applicable law or agreed to in writing, software
928 // distributed under the License is distributed on an "AS IS" BASIS,
929 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
930 // See the License for the specific language governing permissions and
931 // limitations under the License.
932 //
933 // -----------------------------------------------------------------------------
934 // File: config.h
935 // -----------------------------------------------------------------------------
936 //
937 // This header file defines a set of macros for checking the presence of
938 // important compiler and platform features. Such macros can be used to
939 // produce portable code by parameterizing compilation based on the presence or
940 // lack of a given feature.
941 //
942 // We define a "feature" as some interface we wish to program to: for example,
943 // a library function or system call. A value of `1` indicates support for
944 // that feature; any other value indicates the feature support is undefined.
945 //
946 // Example:
947 //
948 // Suppose a programmer wants to write a program that uses the 'mmap()' system
949 // call. The Abseil macro for that feature (`ABSL_HAVE_MMAP`) allows you to
950 // selectively include the `mmap.h` header and bracket code using that feature
951 // in the macro:
952 //
953 //
954 // #ifdef ABSL_HAVE_MMAP
955 // #include "sys/mman.h"
956 // #endif //ABSL_HAVE_MMAP
957 //
958 // ...
959 // #ifdef ABSL_HAVE_MMAP
960 // void *ptr = mmap(...);
961 // ...
962 // #endif // ABSL_HAVE_MMAP
963
964 #ifndef ABSL_BASE_CONFIG_H_
965 #define ABSL_BASE_CONFIG_H_
966
967 // Included for the __GLIBC__ macro (or similar macros on other systems).
968 #include <limits.h>
969
970 #ifdef __cplusplus
971 // Included for __GLIBCXX__, _LIBCPP_VERSION
972 #include <cstddef>
973 #endif // __cplusplus
974
975 #if defined(__APPLE__)
976 // Included for TARGET_OS_IPHONE, __IPHONE_OS_VERSION_MIN_REQUIRED,
977 // __IPHONE_8_0.
978 #include <Availability.h>
979 #include <TargetConditionals.h>
980 #endif
981
982
983 // -----------------------------------------------------------------------------
984 // Compiler Feature Checks
985 // -----------------------------------------------------------------------------
986
987 // ABSL_HAVE_BUILTIN()
988 //
989 // Checks whether the compiler supports a Clang Feature Checking Macro, and if
990 // so, checks whether it supports the provided builtin function "x" where x
991 // is one of the functions noted in
992 // https://clang.llvm.org/docs/LanguageExtensions.html
993 //
994 // Note: Use this macro to avoid an extra level of #ifdef __has_builtin check.
995 // http://releases.llvm.org/3.3/tools/clang/docs/LanguageExtensions.html
996 #ifdef __has_builtin
997 #define ABSL_HAVE_BUILTIN(x) __has_builtin(x)
998 #else
999 #define ABSL_HAVE_BUILTIN(x) 0
1000 #endif
1001
1002 // ABSL_HAVE_TLS is defined to 1 when __thread should be supported.
1003 // We assume __thread is supported on Linux when compiled with Clang or compiled
1004 // against libstdc++ with _GLIBCXX_HAVE_TLS defined.
1005 #ifdef ABSL_HAVE_TLS
1006 #error ABSL_HAVE_TLS cannot be directly set
1007 #elif defined(__linux__) && (defined(__clang__) || defined(_GLIBCXX_HAVE_TLS))
1008 #define ABSL_HAVE_TLS 1
1009 #endif
1010
1011 // There are platforms for which TLS should not be used even though the compiler
1012 // makes it seem like it's supported (Android NDK < r12b for example).
1013 // This is primarily because of linker problems and toolchain misconfiguration:
1014 // Abseil does not intend to support this indefinitely. Currently, the newest
1015 // toolchain that we intend to support that requires this behavior is the
1016 // r11 NDK - allowing for a 5 year support window on that means this option
1017 // is likely to be removed around June of 2021.
1018 #if defined(__ANDROID__) && defined(__clang__)
1019 #if __has_include(<android/ndk-version.h>)
1020 #include <android/ndk-version.h>
1021 #endif
1022 // TLS isn't supported until NDK r12b per
1023 // https://developer.android.com/ndk/downloads/revision_history.html
1024 // Since NDK r16, `__NDK_MAJOR__` and `__NDK_MINOR__` are defined in
1025 // <android/ndk-version.h>. For NDK < r16, users should define these macros,
1026 // e.g. `-D__NDK_MAJOR__=11 -D__NKD_MINOR__=0` for NDK r11.
1027 #if defined(__NDK_MAJOR__) && defined(__NDK_MINOR__) && \
1028 ((__NDK_MAJOR__ < 12) || ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))
1029 #undef ABSL_HAVE_TLS
1030 #endif
1031 #endif // defined(__ANDROID__) && defined(__clang__)
1032
1033 // ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
1034 //
1035 // Checks whether `std::is_trivially_destructible<T>` is supported.
1036 //
1037 // Notes: All supported compilers using libc++ support this feature, as does
1038 // gcc >= 4.8.1 using libstdc++, and Visual Studio.
1039 #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
1040 #error ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE cannot be directly set
1041 #elif defined(_LIBCPP_VERSION) || \
1042 (!defined(__clang__) && defined(__GNUC__) && defined(__GLIBCXX__) && \
1043 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) || \
1044 defined(_MSC_VER)
1045 #define ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE 1
1046 #endif
1047
1048 // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
1049 //
1050 // Checks whether `std::is_trivially_default_constructible<T>` and
1051 // `std::is_trivially_copy_constructible<T>` are supported.
1052
1053 // ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
1054 //
1055 // Checks whether `std::is_trivially_copy_assignable<T>` is supported.
1056
1057 // Notes: Clang with libc++ supports these features, as does gcc >= 5.1 with
1058 // either libc++ or libstdc++, and Visual Studio.
1059 #if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE)
1060 #error ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE cannot be directly set
1061 #elif defined(ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE)
1062 #error ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE cannot directly set
1063 #elif (defined(__clang__) && defined(_LIBCPP_VERSION)) || \
1064 (!defined(__clang__) && defined(__GNUC__) && \
1065 (__GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)) && \
1066 (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__))) || \
1067 defined(_MSC_VER)
1068 #define ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE 1
1069 #define ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE 1
1070 #endif
1071
1072 // ABSL_HAVE_THREAD_LOCAL
1073 //
1074 // Checks whether C++11's `thread_local` storage duration specifier is
1075 // supported.
1076 #ifdef ABSL_HAVE_THREAD_LOCAL
1077 #error ABSL_HAVE_THREAD_LOCAL cannot be directly set
1078 #elif !defined(__apple_build_version__) || \
1079 ((__apple_build_version__ >= 8000042) && \
1080 !(TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0))
1081 // Notes: Xcode's clang did not support `thread_local` until version
1082 // 8, and even then not for all iOS < 9.0.
1083 #define ABSL_HAVE_THREAD_LOCAL 1
1084 #endif
1085
1086 // ABSL_HAVE_INTRINSIC_INT128
1087 //
1088 // Checks whether the __int128 compiler extension for a 128-bit integral type is
1089 // supported.
1090 //
1091 // Notes: __SIZEOF_INT128__ is defined by Clang and GCC when __int128 is
1092 // supported, except on ppc64 and aarch64 where __int128 exists but has exhibits
1093 // a sporadic compiler crashing bug. Nvidia's nvcc also defines __GNUC__ and
1094 // __SIZEOF_INT128__ but not all versions actually support __int128.
1095 #ifdef ABSL_HAVE_INTRINSIC_INT128
1096 #error ABSL_HAVE_INTRINSIC_INT128 cannot be directly set
1097 #elif (defined(__clang__) && defined(__SIZEOF_INT128__) && \
1098 !defined(__ppc64__) && !defined(__aarch64__)) || \
1099 (defined(__CUDACC__) && defined(__SIZEOF_INT128__) && \
1100 __CUDACC_VER__ >= 70000) || \
1101 (!defined(__clang__) && !defined(__CUDACC__) && defined(__GNUC__) && \
1102 defined(__SIZEOF_INT128__))
1103 #define ABSL_HAVE_INTRINSIC_INT128 1
1104 #endif
1105
1106 // ABSL_HAVE_EXCEPTIONS
1107 //
1108 // Checks whether the compiler both supports and enables exceptions. Many
1109 // compilers support a "no exceptions" mode that disables exceptions.
1110 //
1111 // Generally, when ABSL_HAVE_EXCEPTIONS is not defined:
1112 //
1113 // * Code using `throw` and `try` may not compile.
1114 // * The `noexcept` specifier will still compile and behave as normal.
1115 // * The `noexcept` operator may still return `false`.
1116 //
1117 // For further details, consult the compiler's documentation.
1118 #ifdef ABSL_HAVE_EXCEPTIONS
1119 #error ABSL_HAVE_EXCEPTIONS cannot be directly set.
1120
1121 #elif defined(__clang__)
1122 // TODO(calabrese)
1123 // Switch to using __cpp_exceptions when we no longer support versions < 3.6.
1124 // For details on this check, see:
1125 // http://releases.llvm.org/3.6.0/tools/clang/docs/ReleaseNotes.html#the-exceptions-macro
1126 #if defined(__EXCEPTIONS) && __has_feature(cxx_exceptions)
1127 #define ABSL_HAVE_EXCEPTIONS 1
1128 #endif // defined(__EXCEPTIONS) && __has_feature(cxx_exceptions)
1129
1130 // Handle remaining special cases and default to exceptions being supported.
1131 #elif !(defined(__GNUC__) && (__GNUC__ < 5) && !defined(__EXCEPTIONS)) && \
1132 !(defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__cpp_exceptions)) && \
1133 !(defined(_MSC_VER) && !defined(_CPPUNWIND))
1134 #define ABSL_HAVE_EXCEPTIONS 1
1135 #endif
1136
1137 // -----------------------------------------------------------------------------
1138 // Platform Feature Checks
1139 // -----------------------------------------------------------------------------
1140
1141 // Currently supported operating systems and associated preprocessor
1142 // symbols:
1143 //
1144 // Linux and Linux-derived __linux__
1145 // Android __ANDROID__ (implies __linux__)
1146 // Linux (non-Android) __linux__ && !__ANDROID__
1147 // Darwin (Mac OS X and iOS) __APPLE__
1148 // Akaros (http://akaros.org) __ros__
1149 // Windows _WIN32
1150 // NaCL __native_client__
1151 // AsmJS __asmjs__
1152 // Fuschia __Fuchsia__
1153 //
1154 // Note that since Android defines both __ANDROID__ and __linux__, one
1155 // may probe for either Linux or Android by simply testing for __linux__.
1156
1157 // ABSL_HAVE_MMAP
1158 //
1159 // Checks whether the platform has an mmap(2) implementation as defined in
1160 // POSIX.1-2001.
1161 #ifdef ABSL_HAVE_MMAP
1162 #error ABSL_HAVE_MMAP cannot be directly set
1163 #elif defined(__linux__) || defined(__APPLE__) || defined(__ros__) || \
1164 defined(__native_client__) || defined(__asmjs__) || defined(__Fuchsia__)
1165 #define ABSL_HAVE_MMAP 1
1166 #endif
1167
1168 // ABSL_HAVE_PTHREAD_GETSCHEDPARAM
1169 //
1170 // Checks whether the platform implements the pthread_(get|set)schedparam(3)
1171 // functions as defined in POSIX.1-2001.
1172 #ifdef ABSL_HAVE_PTHREAD_GETSCHEDPARAM
1173 #error ABSL_HAVE_PTHREAD_GETSCHEDPARAM cannot be directly set
1174 #elif defined(__linux__) || defined(__APPLE__) || defined(__ros__)
1175 #define ABSL_HAVE_PTHREAD_GETSCHEDPARAM 1
1176 #endif
1177
1178 // ABSL_HAVE_SCHED_YIELD
1179 //
1180 // Checks whether the platform implements sched_yield(2) as defined in
1181 // POSIX.1-2001.
1182 #ifdef ABSL_HAVE_SCHED_YIELD
1183 #error ABSL_HAVE_SCHED_YIELD cannot be directly set
1184 #elif defined(__linux__) || defined(__ros__) || defined(__native_client__)
1185 #define ABSL_HAVE_SCHED_YIELD 1
1186 #endif
1187
1188 // ABSL_HAVE_SEMAPHORE_H
1189 //
1190 // Checks whether the platform supports the <semaphore.h> header and sem_open(3)
1191 // family of functions as standardized in POSIX.1-2001.
1192 //
1193 // Note: While Apple provides <semaphore.h> for both iOS and macOS, it is
1194 // explicitly deprecated and will cause build failures if enabled for those
1195 // platforms. We side-step the issue by not defining it here for Apple
1196 // platforms.
1197 #ifdef ABSL_HAVE_SEMAPHORE_H
1198 #error ABSL_HAVE_SEMAPHORE_H cannot be directly set
1199 #elif defined(__linux__) || defined(__ros__)
1200 #define ABSL_HAVE_SEMAPHORE_H 1
1201 #endif
1202
1203 // ABSL_HAVE_ALARM
1204 //
1205 // Checks whether the platform supports the <signal.h> header and alarm(2)
1206 // function as standardized in POSIX.1-2001.
1207 #ifdef ABSL_HAVE_ALARM
1208 #error ABSL_HAVE_ALARM cannot be directly set
1209 #elif defined(__GOOGLE_GRTE_VERSION__)
1210 // feature tests for Google's GRTE
1211 #define ABSL_HAVE_ALARM 1
1212 #elif defined(__GLIBC__)
1213 // feature test for glibc
1214 #define ABSL_HAVE_ALARM 1
1215 #elif defined(_MSC_VER)
1216 // feature tests for Microsoft's library
1217 #elif defined(__native_client__)
1218 #else
1219 // other standard libraries
1220 #define ABSL_HAVE_ALARM 1
1221 #endif
1222
1223 // ABSL_IS_LITTLE_ENDIAN
1224 // ABSL_IS_BIG_ENDIAN
1225 //
1226 // Checks the endianness of the platform.
1227 //
1228 // Notes: uses the built in endian macros provided by GCC (since 4.6) and
1229 // Clang (since 3.2); see
1230 // https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html.
1231 // Otherwise, if _WIN32, assume little endian. Otherwise, bail with an error.
1232 #if defined(ABSL_IS_BIG_ENDIAN)
1233 #error "ABSL_IS_BIG_ENDIAN cannot be directly set."
1234 #endif
1235 #if defined(ABSL_IS_LITTLE_ENDIAN)
1236 #error "ABSL_IS_LITTLE_ENDIAN cannot be directly set."
1237 #endif
1238
1239 #if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
1240 __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
1241 #define ABSL_IS_LITTLE_ENDIAN 1
1242 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
1243 __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1244 #define ABSL_IS_BIG_ENDIAN 1
1245 #elif defined(_WIN32)
1246 #define ABSL_IS_LITTLE_ENDIAN 1
1247 #else
1248 #error "absl endian detection needs to be set up for your compiler"
1249 #endif
1250
1251 // ABSL_HAVE_STD_ANY
1252 //
1253 // Checks whether C++17 std::any is available by checking whether <any> exists.
1254 #ifdef ABSL_HAVE_STD_ANY
1255 #error "ABSL_HAVE_STD_ANY cannot be directly set."
1256 #endif
1257
1258 #ifdef __has_include
1259 #if __has_include(<any>) && __cplusplus >= 201703L
1260 #define ABSL_HAVE_STD_ANY 1
1261 #endif
1262 #endif
1263
1264 // ABSL_HAVE_STD_OPTIONAL
1265 //
1266 // Checks whether C++17 std::optional is available.
1267 #ifdef ABSL_HAVE_STD_OPTIONAL
1268 #error "ABSL_HAVE_STD_OPTIONAL cannot be directly set."
1269 #endif
1270
1271 #ifdef __has_include
1272 #if __has_include(<optional>) && __cplusplus >= 201703L
1273 #define ABSL_HAVE_STD_OPTIONAL 1
1274 #endif
1275 #endif
1276
1277 // ABSL_HAVE_STD_STRING_VIEW
1278 //
1279 // Checks whether C++17 std::string_view is available.
1280 #ifdef ABSL_HAVE_STD_STRING_VIEW
1281 #error "ABSL_HAVE_STD_STRING_VIEW cannot be directly set."
1282 #endif
1283
1284 #ifdef __has_include
1285 #if __has_include(<string_view>) && __cplusplus >= 201703L
1286 #define ABSL_HAVE_STD_STRING_VIEW 1
1287 #endif
1288 #endif
1289
1290 #endif // ABSL_BASE_CONFIG_H_
1291 //
1292 // Copyright 2017 The Abseil Authors.
1293 //
1294 // Licensed under the Apache License, Version 2.0 (the "License");
1295 // you may not use this file except in compliance with the License.
1296 // You may obtain a copy of the License at
1297 //
1298 // http://www.apache.org/licenses/LICENSE-2.0
1299 //
1300 // Unless required by applicable law or agreed to in writing, software
1301 // distributed under the License is distributed on an "AS IS" BASIS,
1302 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1303 // See the License for the specific language governing permissions and
1304 // limitations under the License.
1305 //
1306 // -----------------------------------------------------------------------------
1307 // File: optimization.h
1308 // -----------------------------------------------------------------------------
1309 //
1310 // This header file defines portable macros for performance optimization.
1311
1312 #ifndef ABSL_BASE_OPTIMIZATION_H_
1313 #define ABSL_BASE_OPTIMIZATION_H_
1314
1315
1316 // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION
1317 //
1318 // Instructs the compiler to avoid optimizing tail-call recursion. Use of this
1319 // macro is useful when you wish to preserve the existing function order within
1320 // a stack trace for logging, debugging, or profiling purposes.
1321 //
1322 // Example:
1323 //
1324 // int f() {
1325 // int result = g();
1326 // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
1327 // return result;
1328 // }
1329 #if defined(__pnacl__)
1330 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
1331 #elif defined(__clang__)
1332 // Clang will not tail call given inline volatile assembly.
1333 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
1334 #elif defined(__GNUC__)
1335 // GCC will not tail call given inline volatile assembly.
1336 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
1337 #elif defined(_MSC_VER)
1338 #include <intrin.h>
1339 // The __nop() intrinsic blocks the optimisation.
1340 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
1341 #else
1342 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
1343 #endif
1344
1345 // ABSL_CACHELINE_SIZE
1346 //
1347 // Explicitly defines the size of the L1 cache for purposes of alignment.
1348 // Setting the cacheline size allows you to specify that certain objects be
1349 // aligned on a cacheline boundary with `ABSL_CACHELINE_ALIGNED` declarations.
1350 // (See below.)
1351 //
1352 // NOTE: this macro should be replaced with the following C++17 features, when
1353 // those are generally available:
1354 //
1355 // * `std::hardware_constructive_interference_size`
1356 // * `std::hardware_destructive_interference_size`
1357 //
1358 // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
1359 // for more information.
1360 #if defined(__GNUC__)
1361 // Cache line alignment
1362 #if defined(__i386__) || defined(__x86_64__)
1363 #define ABSL_CACHELINE_SIZE 64
1364 #elif defined(__powerpc64__)
1365 #define ABSL_CACHELINE_SIZE 128
1366 #elif defined(__aarch64__)
1367 // We would need to read special register ctr_el0 to find out L1 dcache size.
1368 // This value is a good estimate based on a real aarch64 machine.
1369 #define ABSL_CACHELINE_SIZE 64
1370 #elif defined(__arm__)
1371 // Cache line sizes for ARM: These values are not strictly correct since
1372 // cache line sizes depend on implementations, not architectures. There
1373 // are even implementations with cache line sizes configurable at boot
1374 // time.
1375 #if defined(__ARM_ARCH_5T__)
1376 #define ABSL_CACHELINE_SIZE 32
1377 #elif defined(__ARM_ARCH_7A__)
1378 #define ABSL_CACHELINE_SIZE 64
1379 #endif
1380 #endif
1381
1382 #ifndef ABSL_CACHELINE_SIZE
1383 // A reasonable default guess. Note that overestimates tend to waste more
1384 // space, while underestimates tend to waste more time.
1385 #define ABSL_CACHELINE_SIZE 64
1386 #endif
1387
1388 // ABSL_CACHELINE_ALIGNED
1389 //
1390 // Indicates that the declared object be cache aligned using
1391 // `ABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to
1392 // load a set of related objects in the L1 cache for performance improvements.
1393 // Cacheline aligning objects properly allows constructive memory sharing and
1394 // prevents destructive (or "false") memory sharing.
1395 //
1396 // NOTE: this macro should be replaced with usage of `alignas()` using
1397 // `std::hardware_constructive_interference_size` and/or
1398 // `std::hardware_destructive_interference_size` when available within C++17.
1399 //
1400 // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
1401 // for more information.
1402 //
1403 // On some compilers, `ABSL_CACHELINE_ALIGNED` expands to
1404 // `__attribute__((aligned(ABSL_CACHELINE_SIZE)))`. For compilers where this is
1405 // not known to work, the macro expands to nothing.
1406 //
1407 // No further guarantees are made here. The result of applying the macro
1408 // to variables and types is always implementation-defined.
1409 //
1410 // WARNING: It is easy to use this attribute incorrectly, even to the point
1411 // of causing bugs that are difficult to diagnose, crash, etc. It does not
1412 // of itself guarantee that objects are aligned to a cache line.
1413 //
1414 // Recommendations:
1415 //
1416 // 1) Consult compiler documentation; this comment is not kept in sync as
1417 // toolchains evolve.
1418 // 2) Verify your use has the intended effect. This often requires inspecting
1419 // the generated machine code.
1420 // 3) Prefer applying this attribute to individual variables. Avoid
1421 // applying it to types. This tends to localize the effect.
1422 #define ABSL_CACHELINE_ALIGNED __attribute__((aligned(ABSL_CACHELINE_SIZE)))
1423
1424 #else // not GCC
1425 #define ABSL_CACHELINE_SIZE 64
1426 #define ABSL_CACHELINE_ALIGNED
1427 #endif
1428
1429 // ABSL_PREDICT_TRUE, ABSL_PREDICT_FALSE
1430 //
1431 // Enables the compiler to prioritize compilation using static analysis for
1432 // likely paths within a boolean branch.
1433 //
1434 // Example:
1435 //
1436 // if (ABSL_PREDICT_TRUE(expression)) {
1437 // return result; // Faster if more likely
1438 // } else {
1439 // return 0;
1440 // }
1441 //
1442 // Compilers can use the information that a certain branch is not likely to be
1443 // taken (for instance, a CHECK failure) to optimize for the common case in
1444 // the absence of better information (ie. compiling gcc with `-fprofile-arcs`).
1445 #if ABSL_HAVE_BUILTIN(__builtin_expect) || \
1446 (defined(__GNUC__) && !defined(__clang__))
1447 #define ABSL_PREDICT_FALSE(x) (__builtin_expect(x, 0))
1448 #define ABSL_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
1449 #else
1450 #define ABSL_PREDICT_FALSE(x) x
1451 #define ABSL_PREDICT_TRUE(x) x
1452 #endif
1453
1454 #endif // ABSL_BASE_OPTIMIZATION_H_
1455 //
1456 // Copyright 2017 The Abseil Authors.
1457 //
1458 // Licensed under the Apache License, Version 2.0 (the "License");
1459 // you may not use this file except in compliance with the License.
1460 // You may obtain a copy of the License at
1461 //
1462 // http://www.apache.org/licenses/LICENSE-2.0
1463 //
1464 // Unless required by applicable law or agreed to in writing, software
1465 // distributed under the License is distributed on an "AS IS" BASIS,
1466 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1467 // See the License for the specific language governing permissions and
1468 // limitations under the License.
1469 //
1470 // -----------------------------------------------------------------------------
1471 // File: macros.h
1472 // -----------------------------------------------------------------------------
1473 //
1474 // This header file defines the set of language macros used within Abseil code.
1475 // For the set of macros used to determine supported compilers and platforms,
1476 // see absl/base/config.h instead.
1477 //
1478 // This code is compiled directly on many platforms, including client
1479 // platforms like Windows, Mac, and embedded systems. Before making
1480 // any changes here, make sure that you're not breaking any platforms.
1481 //
1482
1483 #ifndef ABSL_BASE_MACROS_H_
1484 #define ABSL_BASE_MACROS_H_
1485
1486 #include <cstddef>
1487
1488
1489 // ABSL_ARRAYSIZE()
1490 //
1491 // Returns the # of elements in an array as a compile-time constant, which can
1492 // be used in defining new arrays. If you use this macro on a pointer by
1493 // mistake, you will get a compile-time error.
1494 //
1495 // Note: this template function declaration is used in defining arraysize.
1496 // Note that the function doesn't need an implementation, as we only
1497 // use its type.
1498 namespace absl {
1499 namespace macros_internal {
1500 template <typename T, size_t N>
1501 char (&ArraySizeHelper(T (&array)[N]))[N];
1502 } // namespace macros_internal
1503 } // namespace absl
1504 #define ABSL_ARRAYSIZE(array) \
1505 (sizeof(::absl::macros_internal::ArraySizeHelper(array)))
1506
1507 // kLinkerInitialized
1508 //
1509 // An enum used only as a constructor argument to indicate that a variable has
1510 // static storage duration, and that the constructor should do nothing to its
1511 // state. Use of this macro indicates to the reader that it is legal to
1512 // declare a static instance of the class, provided the constructor is given
1513 // the absl::base_internal::kLinkerInitialized argument.
1514 //
1515 // Normally, it is unsafe to declare a static variable that has a constructor or
1516 // a destructor because invocation order is undefined. However, if the type can
1517 // be zero-initialized (which the loader does for static variables) into a valid
1518 // state and the type's destructor does not affect storage, then a constructor
1519 // for static initialization can be declared.
1520 //
1521 // Example:
1522 // // Declaration
1523 // explicit MyClass(absl::base_internal:LinkerInitialized x) {}
1524 //
1525 // // Invocation
1526 // static MyClass my_global(absl::base_internal::kLinkerInitialized);
1527 namespace absl {
1528 namespace base_internal {
1529 enum LinkerInitialized {
1530 kLinkerInitialized = 0,
1531 };
1532 } // namespace base_internal
1533 } // namespace absl
1534
1535 // ABSL_FALLTHROUGH_INTENDED
1536 //
1537 // Annotates implicit fall-through between switch labels, allowing a case to
1538 // indicate intentional fallthrough and turn off warnings about any lack of a
1539 // `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by
1540 // a semicolon and can be used in most places where `break` can, provided that
1541 // no statements exist between it and the next switch label.
1542 //
1543 // Example:
1544 //
1545 // switch (x) {
1546 // case 40:
1547 // case 41:
1548 // if (truth_is_out_there) {
1549 // ++x;
1550 // ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
1551 // // in comments
1552 // } else {
1553 // return x;
1554 // }
1555 // case 42:
1556 // ...
1557 //
1558 // Notes: when compiled with clang in C++11 mode, the ABSL_FALLTHROUGH_INTENDED
1559 // macro is expanded to the [[clang::fallthrough]] attribute, which is analysed
1560 // when performing switch labels fall-through diagnostic
1561 // (`-Wimplicit-fallthrough`). See clang documentation on language extensions
1562 // for details:
1563 // http://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
1564 //
1565 // When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro
1566 // has no effect on diagnostics. In any case this macro has no effect on runtime
1567 // behavior and performance of code.
1568 #ifdef ABSL_FALLTHROUGH_INTENDED
1569 #error "ABSL_FALLTHROUGH_INTENDED should not be defined."
1570 #endif
1571
1572 // TODO(zhangxy): Use c++17 standard [[fallthrough]] macro, when supported.
1573 #if defined(__clang__) && defined(__has_warning)
1574 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
1575 #define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
1576 #endif
1577 #elif defined(__GNUC__) && __GNUC__ >= 7
1578 #define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
1579 #endif
1580
1581 #ifndef ABSL_FALLTHROUGH_INTENDED
1582 #define ABSL_FALLTHROUGH_INTENDED \
1583 do { \
1584 } while (0)
1585 #endif
1586
1587 // ABSL_DEPRECATED()
1588 //
1589 // Marks a deprecated class, struct, enum, function, method and variable
1590 // declarations. The macro argument is used as a custom diagnostic message (e.g.
1591 // suggestion of a better alternative).
1592 //
1593 // Example:
1594 //
1595 // class ABSL_DEPRECATED("Use Bar instead") Foo {...};
1596 // ABSL_DEPRECATED("Use Baz instead") void Bar() {...}
1597 //
1598 // Every usage of a deprecated entity will trigger a warning when compiled with
1599 // clang's `-Wdeprecated-declarations` option. This option is turned off by
1600 // default, but the warnings will be reported by clang-tidy.
1601 #if defined(__clang__) && __cplusplus >= 201103L && defined(__has_warning)
1602 #define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
1603 #endif
1604
1605 #ifndef ABSL_DEPRECATED
1606 #define ABSL_DEPRECATED(message)
1607 #endif
1608
1609 // ABSL_BAD_CALL_IF()
1610 //
1611 // Used on a function overload to trap bad calls: any call that matches the
1612 // overload will cause a compile-time error. This macro uses a clang-specific
1613 // "enable_if" attribute, as described at
1614 // http://clang.llvm.org/docs/AttributeReference.html#enable-if
1615 //
1616 // Overloads which use this macro should be bracketed by
1617 // `#ifdef ABSL_BAD_CALL_IF`.
1618 //
1619 // Example:
1620 //
1621 // int isdigit(int c);
1622 // #ifdef ABSL_BAD_CALL_IF
1623 // int isdigit(int c)
1624 // ABSL_BAD_CALL_IF(c <= -1 || c > 255,
1625 // "'c' must have the value of an unsigned char or EOF");
1626 // #endif // ABSL_BAD_CALL_IF
1627
1628 #if defined(__clang__)
1629 # if __has_attribute(enable_if)
1630 # define ABSL_BAD_CALL_IF(expr, msg) \
1631 __attribute__((enable_if(expr, "Bad call trap"), unavailable(msg)))
1632 # endif
1633 #endif
1634
1635 // ABSL_ASSERT()
1636 //
1637 // In C++11, `assert` can't be used portably within constexpr functions.
1638 // ABSL_ASSERT functions as a runtime assert but works in C++11 constexpr
1639 // functions. Example:
1640 //
1641 // constexpr double Divide(double a, double b) {
1642 // return ABSL_ASSERT(b != 0), a / b;
1643 // }
1644 //
1645 // This macro is inspired by
1646 // https://akrzemi1.wordpress.com/2017/05/18/asserts-in-constexpr-functions/
1647 #if defined(NDEBUG)
1648 #define ABSL_ASSERT(expr) (false ? (void)(expr) : (void)0)
1649 #else
1650 #define ABSL_ASSERT(expr) \
1651 (ABSL_PREDICT_TRUE((expr)) ? (void)0 : [] { assert(false && #expr); }())
1652 #endif
1653
1654 #endif // ABSL_BASE_MACROS_H_
1655 /*
1656 * Copyright 2017 The Abseil Authors.
1657 *
1658 * Licensed under the Apache License, Version 2.0 (the "License");
1659 * you may not use this file except in compliance with the License.
1660 * You may obtain a copy of the License at
1661 *
1662 * http://www.apache.org/licenses/LICENSE-2.0
1663 *
1664 * Unless required by applicable law or agreed to in writing, software
1665 * distributed under the License is distributed on an "AS IS" BASIS,
1666 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1667 * See the License for the specific language governing permissions and
1668 * limitations under the License.
1669 */
1670 /* This file defines dynamic annotations for use with dynamic analysis
1671 tool such as valgrind, PIN, etc.
1672
1673 Dynamic annotation is a source code annotation that affects
1674 the generated code (that is, the annotation is not a comment).
1675 Each such annotation is attached to a particular
1676 instruction and/or to a particular object (address) in the program.
1677
1678 The annotations that should be used by users are macros in all upper-case
1679 (e.g., ANNOTATE_THREAD_NAME).
1680
1681 Actual implementation of these macros may differ depending on the
1682 dynamic analysis tool being used.
1683
1684 This file supports the following configurations:
1685 - Dynamic Annotations enabled (with static thread-safety warnings disabled).
1686 In this case, macros expand to functions implemented by Thread Sanitizer,
1687 when building with TSan. When not provided an external implementation,
1688 dynamic_annotations.cc provides no-op implementations.
1689
1690 - Static Clang thread-safety warnings enabled.
1691 When building with a Clang compiler that supports thread-safety warnings,
1692 a subset of annotations can be statically-checked at compile-time. We
1693 expand these macros to static-inline functions that can be analyzed for
1694 thread-safety, but afterwards elided when building the final binary.
1695
1696 - All annotations are disabled.
1697 If neither Dynamic Annotations nor Clang thread-safety warnings are
1698 enabled, then all annotation-macros expand to empty. */
1699
1700 #ifndef ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
1701 #define ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
1702
1703 #ifndef DYNAMIC_ANNOTATIONS_ENABLED
1704 # define DYNAMIC_ANNOTATIONS_ENABLED 0
1705 #endif
1706
1707 #if defined(__native_client__)
1708 #include "nacl/dynamic_annotations.h"
1709
1710 // Stub out the macros missing from the NaCl version.
1711 #ifndef ANNOTATE_CONTIGUOUS_CONTAINER
1712 #define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid)
1713 #endif
1714 #ifndef ANNOTATE_RWLOCK_CREATE_STATIC
1715 #define ANNOTATE_RWLOCK_CREATE_STATIC(lock)
1716 #endif
1717 #ifndef ADDRESS_SANITIZER_REDZONE
1718 #define ADDRESS_SANITIZER_REDZONE(name)
1719 #endif
1720 #ifndef ANNOTATE_MEMORY_IS_UNINITIALIZED
1721 #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size)
1722 #endif
1723
1724 #else /* !__native_client__ */
1725
1726 #if DYNAMIC_ANNOTATIONS_ENABLED != 0
1727
1728 /* -------------------------------------------------------------
1729 Annotations that suppress errors. It is usually better to express the
1730 program's synchronization using the other annotations, but these can
1731 be used when all else fails. */
1732
1733 /* Report that we may have a benign race at "pointer", with size
1734 "sizeof(*(pointer))". "pointer" must be a non-void* pointer. Insert at the
1735 point where "pointer" has been allocated, preferably close to the point
1736 where the race happens. See also ANNOTATE_BENIGN_RACE_STATIC. */
1737 #define ANNOTATE_BENIGN_RACE(pointer, description) \
1738 AnnotateBenignRaceSized(__FILE__, __LINE__, pointer, \
1739 sizeof(*(pointer)), description)
1740
1741 /* Same as ANNOTATE_BENIGN_RACE(address, description), but applies to
1742 the memory range [address, address+size). */
1743 #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
1744 AnnotateBenignRaceSized(__FILE__, __LINE__, address, size, description)
1745
1746 /* Enable (enable!=0) or disable (enable==0) race detection for all threads.
1747 This annotation could be useful if you want to skip expensive race analysis
1748 during some period of program execution, e.g. during initialization. */
1749 #define ANNOTATE_ENABLE_RACE_DETECTION(enable) \
1750 AnnotateEnableRaceDetection(__FILE__, __LINE__, enable)
1751
1752 /* -------------------------------------------------------------
1753 Annotations useful for debugging. */
1754
1755 /* Report the current thread name to a race detector. */
1756 #define ANNOTATE_THREAD_NAME(name) \
1757 AnnotateThreadName(__FILE__, __LINE__, name)
1758
1759 /* -------------------------------------------------------------
1760 Annotations useful when implementing locks. They are not
1761 normally needed by modules that merely use locks.
1762 The "lock" argument is a pointer to the lock object. */
1763
1764 /* Report that a lock has been created at address "lock". */
1765 #define ANNOTATE_RWLOCK_CREATE(lock) \
1766 AnnotateRWLockCreate(__FILE__, __LINE__, lock)
1767
1768 /* Report that a linker initialized lock has been created at address "lock".
1769 */
1770 #ifdef THREAD_SANITIZER
1771 #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
1772 AnnotateRWLockCreateStatic(__FILE__, __LINE__, lock)
1773 #else
1774 #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) ANNOTATE_RWLOCK_CREATE(lock)
1775 #endif
1776
1777 /* Report that the lock at address "lock" is about to be destroyed. */
1778 #define ANNOTATE_RWLOCK_DESTROY(lock) \
1779 AnnotateRWLockDestroy(__FILE__, __LINE__, lock)
1780
1781 /* Report that the lock at address "lock" has been acquired.
1782 is_w=1 for writer lock, is_w=0 for reader lock. */
1783 #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
1784 AnnotateRWLockAcquired(__FILE__, __LINE__, lock, is_w)
1785
1786 /* Report that the lock at address "lock" is about to be released. */
1787 #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
1788 AnnotateRWLockReleased(__FILE__, __LINE__, lock, is_w)
1789
1790 #else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
1791
1792 #define ANNOTATE_RWLOCK_CREATE(lock) /* empty */
1793 #define ANNOTATE_RWLOCK_CREATE_STATIC(lock) /* empty */
1794 #define ANNOTATE_RWLOCK_DESTROY(lock) /* empty */
1795 #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) /* empty */
1796 #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) /* empty */
1797 #define ANNOTATE_BENIGN_RACE(address, description) /* empty */
1798 #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) /* empty */
1799 #define ANNOTATE_THREAD_NAME(name) /* empty */
1800 #define ANNOTATE_ENABLE_RACE_DETECTION(enable) /* empty */
1801
1802 #endif /* DYNAMIC_ANNOTATIONS_ENABLED */
1803
1804 /* These annotations are also made available to LLVM's Memory Sanitizer */
1805 #if DYNAMIC_ANNOTATIONS_ENABLED == 1 || defined(MEMORY_SANITIZER)
1806 #define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
1807 AnnotateMemoryIsInitialized(__FILE__, __LINE__, address, size)
1808
1809 #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
1810 AnnotateMemoryIsUninitialized(__FILE__, __LINE__, address, size)
1811 #else
1812 #define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) /* empty */
1813 #define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) /* empty */
1814 #endif /* DYNAMIC_ANNOTATIONS_ENABLED || MEMORY_SANITIZER */
1815 /* TODO(delesley) -- Replace __CLANG_SUPPORT_DYN_ANNOTATION__ with the
1816 appropriate feature ID. */
1817 #if defined(__clang__) && (!defined(SWIG)) \
1818 && defined(__CLANG_SUPPORT_DYN_ANNOTATION__)
1819
1820 #if DYNAMIC_ANNOTATIONS_ENABLED == 0
1821 #define ANNOTALYSIS_ENABLED
1822 #endif
1823
1824 /* When running in opt-mode, GCC will issue a warning, if these attributes are
1825 compiled. Only include them when compiling using Clang. */
1826 #define ATTRIBUTE_IGNORE_READS_BEGIN \
1827 __attribute((exclusive_lock_function("*")))
1828 #define ATTRIBUTE_IGNORE_READS_END \
1829 __attribute((unlock_function("*")))
1830 #else
1831 #define ATTRIBUTE_IGNORE_READS_BEGIN /* empty */
1832 #define ATTRIBUTE_IGNORE_READS_END /* empty */
1833 #endif /* defined(__clang__) && ... */
1834
1835 #if (DYNAMIC_ANNOTATIONS_ENABLED != 0) || defined(ANNOTALYSIS_ENABLED)
1836 #define ANNOTATIONS_ENABLED
1837 #endif
1838
1839 #if (DYNAMIC_ANNOTATIONS_ENABLED != 0)
1840
1841 /* Request the analysis tool to ignore all reads in the current thread
1842 until ANNOTATE_IGNORE_READS_END is called.
1843 Useful to ignore intentional racey reads, while still checking
1844 other reads and all writes.
1845 See also ANNOTATE_UNPROTECTED_READ. */
1846 #define ANNOTATE_IGNORE_READS_BEGIN() \
1847 AnnotateIgnoreReadsBegin(__FILE__, __LINE__)
1848
1849 /* Stop ignoring reads. */
1850 #define ANNOTATE_IGNORE_READS_END() \
1851 AnnotateIgnoreReadsEnd(__FILE__, __LINE__)
1852
1853 /* Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead. */
1854 #define ANNOTATE_IGNORE_WRITES_BEGIN() \
1855 AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
1856
1857 /* Stop ignoring writes. */
1858 #define ANNOTATE_IGNORE_WRITES_END() \
1859 AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
1860
1861 /* Clang provides limited support for static thread-safety analysis
1862 through a feature called Annotalysis. We configure macro-definitions
1863 according to whether Annotalysis support is available. */
1864 #elif defined(ANNOTALYSIS_ENABLED)
1865
1866 #define ANNOTATE_IGNORE_READS_BEGIN() \
1867 StaticAnnotateIgnoreReadsBegin(__FILE__, __LINE__)
1868
1869 #define ANNOTATE_IGNORE_READS_END() \
1870 StaticAnnotateIgnoreReadsEnd(__FILE__, __LINE__)
1871
1872 #define ANNOTATE_IGNORE_WRITES_BEGIN() \
1873 StaticAnnotateIgnoreWritesBegin(__FILE__, __LINE__)
1874
1875 #define ANNOTATE_IGNORE_WRITES_END() \
1876 StaticAnnotateIgnoreWritesEnd(__FILE__, __LINE__)
1877
1878 #else
1879 #define ANNOTATE_IGNORE_READS_BEGIN() /* empty */
1880 #define ANNOTATE_IGNORE_READS_END() /* empty */
1881 #define ANNOTATE_IGNORE_WRITES_BEGIN() /* empty */
1882 #define ANNOTATE_IGNORE_WRITES_END() /* empty */
1883 #endif
1884
1885 /* Implement the ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
1886 primitive annotations defined above. */
1887 #if defined(ANNOTATIONS_ENABLED)
1888
1889 /* Start ignoring all memory accesses (both reads and writes). */
1890 #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
1891 do { \
1892 ANNOTATE_IGNORE_READS_BEGIN(); \
1893 ANNOTATE_IGNORE_WRITES_BEGIN(); \
1894 }while (0)
1895
1896 /* Stop ignoring both reads and writes. */
1897 #define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
1898 do { \
1899 ANNOTATE_IGNORE_WRITES_END(); \
1900 ANNOTATE_IGNORE_READS_END(); \
1901 }while (0)
1902
1903 #else
1904 #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() /* empty */
1905 #define ANNOTATE_IGNORE_READS_AND_WRITES_END() /* empty */
1906 #endif
1907
1908 /* Use the macros above rather than using these functions directly. */
1909 #include <stddef.h>
1910 #ifdef __cplusplus
1911 extern "C" {
1912 #endif
1913 void AnnotateRWLockCreate(const char *file, int line,
1914 const volatile void *lock);
1915 void AnnotateRWLockCreateStatic(const char *file, int line,
1916 const volatile void *lock);
1917 void AnnotateRWLockDestroy(const char *file, int line,
1918 const volatile void *lock);
1919 void AnnotateRWLockAcquired(const char *file, int line,
1920 const volatile void *lock, long is_w); /* NOLINT */
1921 void AnnotateRWLockReleased(const char *file, int line,
1922 const volatile void *lock, long is_w); /* NOLINT */
1923 void AnnotateBenignRace(const char *file, int line,
1924 const volatile void *address,
1925 const char *description);
1926 void AnnotateBenignRaceSized(const char *file, int line,
1927 const volatile void *address,
1928 size_t size,
1929 const char *description);
1930 void AnnotateThreadName(const char *file, int line,
1931 const char *name);
1932 void AnnotateEnableRaceDetection(const char *file, int line, int enable);
1933 void AnnotateMemoryIsInitialized(const char *file, int line,
1934 const volatile void *mem, size_t size);
1935 void AnnotateMemoryIsUninitialized(const char *file, int line,
1936 const volatile void *mem, size_t size);
1937
1938 /* Annotations expand to these functions, when Dynamic Annotations are enabled.
1939 These functions are either implemented as no-op calls, if no Sanitizer is
1940 attached, or provided with externally-linked implementations by a library
1941 like ThreadSanitizer. */
1942 void AnnotateIgnoreReadsBegin(const char *file, int line)
1943 ATTRIBUTE_IGNORE_READS_BEGIN;
1944 void AnnotateIgnoreReadsEnd(const char *file, int line)
1945 ATTRIBUTE_IGNORE_READS_END;
1946 void AnnotateIgnoreWritesBegin(const char *file, int line);
1947 void AnnotateIgnoreWritesEnd(const char *file, int line);
1948
1949 #if defined(ANNOTALYSIS_ENABLED)
1950 /* When Annotalysis is enabled without Dynamic Annotations, the use of
1951 static-inline functions allows the annotations to be read at compile-time,
1952 while still letting the compiler elide the functions from the final build.
1953
1954 TODO(delesley) -- The exclusive lock here ignores writes as well, but
1955 allows IGNORE_READS_AND_WRITES to work properly. */
1956 #pragma GCC diagnostic push
1957 #pragma GCC diagnostic ignored "-Wunused-function"
StaticAnnotateIgnoreReadsBegin(const char * file,int line)1958 static inline void StaticAnnotateIgnoreReadsBegin(const char *file, int line)
1959 ATTRIBUTE_IGNORE_READS_BEGIN { (void)file; (void)line; }
StaticAnnotateIgnoreReadsEnd(const char * file,int line)1960 static inline void StaticAnnotateIgnoreReadsEnd(const char *file, int line)
1961 ATTRIBUTE_IGNORE_READS_END { (void)file; (void)line; }
StaticAnnotateIgnoreWritesBegin(const char * file,int line)1962 static inline void StaticAnnotateIgnoreWritesBegin(
1963 const char *file, int line) { (void)file; (void)line; }
StaticAnnotateIgnoreWritesEnd(const char * file,int line)1964 static inline void StaticAnnotateIgnoreWritesEnd(
1965 const char *file, int line) { (void)file; (void)line; }
1966 #pragma GCC diagnostic pop
1967 #endif
1968
1969 /* Return non-zero value if running under valgrind.
1970
1971 If "valgrind.h" is included into dynamic_annotations.cc,
1972 the regular valgrind mechanism will be used.
1973 See http://valgrind.org/docs/manual/manual-core-adv.html about
1974 RUNNING_ON_VALGRIND and other valgrind "client requests".
1975 The file "valgrind.h" may be obtained by doing
1976 svn co svn://svn.valgrind.org/valgrind/trunk/include
1977
1978 If for some reason you can't use "valgrind.h" or want to fake valgrind,
1979 there are two ways to make this function return non-zero:
1980 - Use environment variable: export RUNNING_ON_VALGRIND=1
1981 - Make your tool intercept the function RunningOnValgrind() and
1982 change its return value.
1983 */
1984 int RunningOnValgrind(void);
1985
1986 /* ValgrindSlowdown returns:
1987 * 1.0, if (RunningOnValgrind() == 0)
1988 * 50.0, if (RunningOnValgrind() != 0 && getenv("VALGRIND_SLOWDOWN") == NULL)
1989 * atof(getenv("VALGRIND_SLOWDOWN")) otherwise
1990 This function can be used to scale timeout values:
1991 EXAMPLE:
1992 for (;;) {
1993 DoExpensiveBackgroundTask();
1994 SleepForSeconds(5 * ValgrindSlowdown());
1995 }
1996 */
1997 double ValgrindSlowdown(void);
1998
1999 #ifdef __cplusplus
2000 }
2001 #endif
2002
2003 /* ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
2004
2005 Instead of doing
2006 ANNOTATE_IGNORE_READS_BEGIN();
2007 ... = x;
2008 ANNOTATE_IGNORE_READS_END();
2009 one can use
2010 ... = ANNOTATE_UNPROTECTED_READ(x); */
2011 #if defined(__cplusplus) && defined(ANNOTATIONS_ENABLED)
2012 template <typename T>
ANNOTATE_UNPROTECTED_READ(const volatile T & x)2013 inline T ANNOTATE_UNPROTECTED_READ(const volatile T &x) { /* NOLINT */
2014 ANNOTATE_IGNORE_READS_BEGIN();
2015 T res = x;
2016 ANNOTATE_IGNORE_READS_END();
2017 return res;
2018 }
2019 #else
2020 #define ANNOTATE_UNPROTECTED_READ(x) (x)
2021 #endif
2022
2023 #if DYNAMIC_ANNOTATIONS_ENABLED != 0 && defined(__cplusplus)
2024 /* Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable. */
2025 #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
2026 namespace { \
2027 class static_var ## _annotator { \
2028 public: \
2029 static_var ## _annotator() { \
2030 ANNOTATE_BENIGN_RACE_SIZED(&static_var, \
2031 sizeof(static_var), \
2032 # static_var ": " description); \
2033 } \
2034 }; \
2035 static static_var ## _annotator the ## static_var ## _annotator;\
2036 } // namespace
2037 #else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
2038 #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) /* empty */
2039 #endif /* DYNAMIC_ANNOTATIONS_ENABLED */
2040
2041 #ifdef ADDRESS_SANITIZER
2042 /* Describe the current state of a contiguous container such as e.g.
2043 * std::vector or std::string. For more details see
2044 * sanitizer/common_interface_defs.h, which is provided by the compiler. */
2045 #include <sanitizer/common_interface_defs.h>
2046 #define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
2047 __sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
2048 #define ADDRESS_SANITIZER_REDZONE(name) \
2049 struct { char x[8] __attribute__ ((aligned (8))); } name
2050 #else
2051 #define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid)
2052 #define ADDRESS_SANITIZER_REDZONE(name)
2053 #endif // ADDRESS_SANITIZER
2054
2055 /* Undefine the macros intended only in this file. */
2056 #undef ANNOTALYSIS_ENABLED
2057 #undef ANNOTATIONS_ENABLED
2058 #undef ATTRIBUTE_IGNORE_READS_BEGIN
2059 #undef ATTRIBUTE_IGNORE_READS_END
2060
2061 #endif /* !__native_client__ */
2062
2063 #endif /* ABSL_BASE_DYNAMIC_ANNOTATIONS_H_ */
2064
2065 #define ABSL_RAW_CHECK(cond, msg) assert((cond) && (msg))
2066 #define ABSL_RAW_LOG(sev, ...) do {} while(0)
2067
2068 // Copyright 2017 The Abseil Authors.
2069 //
2070 // Licensed under the Apache License, Version 2.0 (the "License");
2071 // you may not use this file except in compliance with the License.
2072 // You may obtain a copy of the License at
2073 //
2074 // http://www.apache.org/licenses/LICENSE-2.0
2075 //
2076 // Unless required by applicable law or agreed to in writing, software
2077 // distributed under the License is distributed on an "AS IS" BASIS,
2078 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2079 // See the License for the specific language governing permissions and
2080 // limitations under the License.
2081
2082 // Produce stack trace.
2083 //
2084 // There are three different ways we can try to get the stack trace:
2085 //
2086 // 1) Our hand-coded stack-unwinder. This depends on a certain stack
2087 // layout, which is used by gcc (and those systems using a
2088 // gcc-compatible ABI) on x86 systems, at least since gcc 2.95.
2089 // It uses the frame pointer to do its work.
2090 //
2091 // 2) The libunwind library. This is still in development, and as a
2092 // separate library adds a new dependency, but doesn't need a frame
2093 // pointer. It also doesn't call malloc.
2094 //
2095 // 3) The gdb unwinder -- also the one used by the c++ exception code.
2096 // It's obviously well-tested, but has a fatal flaw: it can call
2097 // malloc() from the unwinder. This is a problem because we're
2098 // trying to use the unwinder to instrument malloc().
2099 //
2100 // Note: if you add a new implementation here, make sure it works
2101 // correctly when absl::GetStackTrace() is called with max_depth == 0.
2102 // Some code may do that.
2103
2104
2105 #include <atomic>
2106
2107
2108 #if defined(ABSL_STACKTRACE_INL_HEADER)
2109 #include ABSL_STACKTRACE_INL_HEADER
2110 #else
2111 # error Cannot calculate stack trace: will need to write for your environment
2112 # include "stacktrace_internal/stacktrace_aarch64-inl.inc"
2113 # include "stacktrace_internal/stacktrace_arm-inl.inc"
2114 # include "stacktrace_internal/stacktrace_generic-inl.inc"
2115 # include "stacktrace_internal/stacktrace_powerpc-inl.inc"
2116 # include "stacktrace_internal/stacktrace_unimplemented-inl.inc"
2117 # include "stacktrace_internal/stacktrace_win32-inl.inc"
2118 # include "stacktrace_internal/stacktrace_x86-inl.inc"
2119 #endif
2120
2121 namespace absl {
2122 namespace {
2123
2124 typedef int (*Unwinder)(void**, int*, int, int, const void*, int*);
2125 std::atomic<Unwinder> custom;
2126
2127 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
Unwind(void ** result,int * sizes,int max_depth,int skip_count,const void * uc,int * min_dropped_frames)2128 ABSL_ATTRIBUTE_ALWAYS_INLINE inline int Unwind(void** result, int* sizes,
2129 int max_depth, int skip_count,
2130 const void* uc,
2131 int* min_dropped_frames) {
2132 Unwinder f = &UnwindImpl<IS_STACK_FRAMES, IS_WITH_CONTEXT>;
2133 Unwinder g = custom.load(std::memory_order_acquire);
2134 if (g != nullptr) f = g;
2135
2136 // Add 1 to skip count for the unwinder function itself
2137 int size = (*f)(result, sizes, max_depth, skip_count + 1, uc,
2138 min_dropped_frames);
2139 // To disable tail call to (*f)(...)
2140 ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
2141 return size;
2142 }
2143
2144 } // anonymous namespace
2145
GetStackFrames(void ** result,int * sizes,int max_depth,int skip_count)2146 int GetStackFrames(void** result, int* sizes, int max_depth, int skip_count) {
2147 return Unwind<true, false>(result, sizes, max_depth, skip_count, nullptr,
2148 nullptr);
2149 }
2150
GetStackFramesWithContext(void ** result,int * sizes,int max_depth,int skip_count,const void * uc,int * min_dropped_frames)2151 int GetStackFramesWithContext(void** result, int* sizes, int max_depth,
2152 int skip_count, const void* uc,
2153 int* min_dropped_frames) {
2154 return Unwind<true, true>(result, sizes, max_depth, skip_count, uc,
2155 min_dropped_frames);
2156 }
2157
GetStackTrace(void ** result,int max_depth,int skip_count)2158 int GetStackTrace(void** result, int max_depth, int skip_count) {
2159 return Unwind<false, false>(result, nullptr, max_depth, skip_count, nullptr,
2160 nullptr);
2161 }
2162
GetStackTraceWithContext(void ** result,int max_depth,int skip_count,const void * uc,int * min_dropped_frames)2163 int GetStackTraceWithContext(void** result, int max_depth, int skip_count,
2164 const void* uc, int* min_dropped_frames) {
2165 return Unwind<false, true>(result, nullptr, max_depth, skip_count, uc,
2166 min_dropped_frames);
2167 }
2168
SetStackUnwinder(Unwinder w)2169 void SetStackUnwinder(Unwinder w) {
2170 custom.store(w, std::memory_order_release);
2171 }
2172
DefaultStackUnwinder(void ** pcs,int * sizes,int depth,int skip,const void * uc,int * min_dropped_frames)2173 int DefaultStackUnwinder(void** pcs, int* sizes, int depth, int skip,
2174 const void* uc, int* min_dropped_frames) {
2175 skip++; // For this function
2176 Unwinder f = nullptr;
2177 if (sizes == nullptr) {
2178 if (uc == nullptr) {
2179 f = &UnwindImpl<false, false>;
2180 } else {
2181 f = &UnwindImpl<false, true>;
2182 }
2183 } else {
2184 if (uc == nullptr) {
2185 f = &UnwindImpl<true, false>;
2186 } else {
2187 f = &UnwindImpl<true, true>;
2188 }
2189 }
2190 volatile int x = 0;
2191 int n = (*f)(pcs, sizes, depth, skip, uc, min_dropped_frames);
2192 x = 1; (void) x; // To disable tail call to (*f)(...)
2193 return n;
2194 }
2195
2196 } // namespace absl
2197 // Copyright 2017 The Abseil Authors.
2198 //
2199 // Licensed under the Apache License, Version 2.0 (the "License");
2200 // you may not use this file except in compliance with the License.
2201 // You may obtain a copy of the License at
2202 //
2203 // http://www.apache.org/licenses/LICENSE-2.0
2204 //
2205 // Unless required by applicable law or agreed to in writing, software
2206 // distributed under the License is distributed on an "AS IS" BASIS,
2207 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2208 // See the License for the specific language governing permissions and
2209 // limitations under the License.
2210
2211 // base::AddressIsReadable() probes an address to see whether it is readable,
2212 // without faulting.
2213
2214
2215 #if !defined(__linux__) || defined(__ANDROID__)
2216
2217 namespace absl {
2218 namespace debug_internal {
2219
2220 // On platforms other than Linux, just return true.
AddressIsReadable(const void *)2221 bool AddressIsReadable(const void* /* addr */) { return true; }
2222
2223 } // namespace debug_internal
2224 } // namespace absl
2225
2226 #else
2227
2228 #include <fcntl.h>
2229 #include <sys/syscall.h>
2230 #include <unistd.h>
2231 #include <atomic>
2232 #include <cerrno>
2233 #include <cstdint>
2234
2235
2236 namespace absl {
2237 namespace debug_internal {
2238
2239 // Pack a pid and two file descriptors into a 64-bit word,
2240 // using 16, 24, and 24 bits for each respectively.
Pack(uint64_t pid,uint64_t read_fd,uint64_t write_fd)2241 static uint64_t Pack(uint64_t pid, uint64_t read_fd, uint64_t write_fd) {
2242 ABSL_RAW_CHECK((read_fd >> 24) == 0 && (write_fd >> 24) == 0,
2243 "fd out of range");
2244 return (pid << 48) | ((read_fd & 0xffffff) << 24) | (write_fd & 0xffffff);
2245 }
2246
2247 // Unpack x into a pid and two file descriptors, where x was created with
2248 // Pack().
Unpack(uint64_t x,int * pid,int * read_fd,int * write_fd)2249 static void Unpack(uint64_t x, int *pid, int *read_fd, int *write_fd) {
2250 *pid = x >> 48;
2251 *read_fd = (x >> 24) & 0xffffff;
2252 *write_fd = x & 0xffffff;
2253 }
2254
2255 // Return whether the byte at *addr is readable, without faulting.
2256 // Save and restores errno. Returns true on systems where
2257 // unimplemented.
2258 // This is a namespace-scoped variable for correct zero-initialization.
2259 static std::atomic<uint64_t> pid_and_fds; // initially 0, an invalid pid.
AddressIsReadable(const void * addr)2260 bool AddressIsReadable(const void *addr) {
2261 int save_errno = errno;
2262 // We test whether a byte is readable by using write(). Normally, this would
2263 // be done via a cached file descriptor to /dev/null, but linux fails to
2264 // check whether the byte is readable when the destination is /dev/null, so
2265 // we use a cached pipe. We store the pid of the process that created the
2266 // pipe to handle the case where a process forks, and the child closes all
2267 // the file descriptors and then calls this routine. This is not perfect:
2268 // the child could use the routine, then close all file descriptors and then
2269 // use this routine again. But the likely use of this routine is when
2270 // crashing, to test the validity of pages when dumping the stack. Beware
2271 // that we may leak file descriptors, but we're unlikely to leak many.
2272 int bytes_written;
2273 int current_pid = getpid() & 0xffff; // we use only the low order 16 bits
2274 do { // until we do not get EBADF trying to use file descriptors
2275 int pid;
2276 int read_fd;
2277 int write_fd;
2278 uint64_t local_pid_and_fds = pid_and_fds.load(std::memory_order_relaxed);
2279 Unpack(local_pid_and_fds, &pid, &read_fd, &write_fd);
2280 while (current_pid != pid) {
2281 int p[2];
2282 // new pipe
2283 if (pipe(p) != 0) {
2284 ABSL_RAW_LOG(FATAL, "Failed to create pipe, errno=%d", errno);
2285 }
2286 fcntl(p[0], F_SETFD, FD_CLOEXEC);
2287 fcntl(p[1], F_SETFD, FD_CLOEXEC);
2288 uint64_t new_pid_and_fds = Pack(current_pid, p[0], p[1]);
2289 if (pid_and_fds.compare_exchange_strong(
2290 local_pid_and_fds, new_pid_and_fds, std::memory_order_relaxed,
2291 std::memory_order_relaxed)) {
2292 local_pid_and_fds = new_pid_and_fds; // fds exposed to other threads
2293 } else { // fds not exposed to other threads; we can close them.
2294 close(p[0]);
2295 close(p[1]);
2296 local_pid_and_fds = pid_and_fds.load(std::memory_order_relaxed);
2297 }
2298 Unpack(local_pid_and_fds, &pid, &read_fd, &write_fd);
2299 }
2300 errno = 0;
2301 // Use syscall(SYS_write, ...) instead of write() to prevent ASAN
2302 // and other checkers from complaining about accesses to arbitrary
2303 // memory.
2304 do {
2305 bytes_written = syscall(SYS_write, write_fd, addr, 1);
2306 } while (bytes_written == -1 && errno == EINTR);
2307 if (bytes_written == 1) { // remove the byte from the pipe
2308 char c;
2309 while (read(read_fd, &c, 1) == -1 && errno == EINTR) {
2310 }
2311 }
2312 if (errno == EBADF) { // Descriptors invalid.
2313 // If pid_and_fds contains the problematic file descriptors we just used,
2314 // this call will forget them, and the loop will try again.
2315 pid_and_fds.compare_exchange_strong(local_pid_and_fds, 0,
2316 std::memory_order_relaxed,
2317 std::memory_order_relaxed);
2318 }
2319 } while (errno == EBADF);
2320 errno = save_errno;
2321 return bytes_written == 1;
2322 }
2323
2324 } // namespace debug_internal
2325 } // namespace absl
2326
2327 #endif
2328 // Copyright 2017 The Abseil Authors.
2329 //
2330 // Licensed under the Apache License, Version 2.0 (the "License");
2331 // you may not use this file except in compliance with the License.
2332 // You may obtain a copy of the License at
2333 //
2334 // http://www.apache.org/licenses/LICENSE-2.0
2335 //
2336 // Unless required by applicable law or agreed to in writing, software
2337 // distributed under the License is distributed on an "AS IS" BASIS,
2338 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2339 // See the License for the specific language governing permissions and
2340 // limitations under the License.
2341
2342 // Allow dynamic symbol lookup in an in-memory Elf image.
2343 //
2344
2345
2346 #ifdef ABSL_HAVE_ELF_MEM_IMAGE // defined in elf_mem_image.h
2347
2348 #include <string.h>
2349 #include <cassert>
2350 #include <cstddef>
2351
2352 // From binutils/include/elf/common.h (this doesn't appear to be documented
2353 // anywhere else).
2354 //
2355 // /* This flag appears in a Versym structure. It means that the symbol
2356 // is hidden, and is only visible with an explicit version number.
2357 // This is a GNU extension. */
2358 // #define VERSYM_HIDDEN 0x8000
2359 //
2360 // /* This is the mask for the rest of the Versym information. */
2361 // #define VERSYM_VERSION 0x7fff
2362
2363 #define VERSYM_VERSION 0x7fff
2364
2365 namespace absl {
2366 namespace debug_internal {
2367
2368 namespace {
2369
2370 #if __WORDSIZE == 32
2371 const int kElfClass = ELFCLASS32;
ElfBind(const ElfW (Sym)* symbol)2372 int ElfBind(const ElfW(Sym) *symbol) { return ELF32_ST_BIND(symbol->st_info); }
ElfType(const ElfW (Sym)* symbol)2373 int ElfType(const ElfW(Sym) *symbol) { return ELF32_ST_TYPE(symbol->st_info); }
2374 #elif __WORDSIZE == 64
2375 const int kElfClass = ELFCLASS64;
2376 int ElfBind(const ElfW(Sym) *symbol) { return ELF64_ST_BIND(symbol->st_info); }
2377 int ElfType(const ElfW(Sym) *symbol) { return ELF64_ST_TYPE(symbol->st_info); }
2378 #else
2379 const int kElfClass = -1;
2380 int ElfBind(const ElfW(Sym) *) {
2381 ABSL_RAW_LOG(FATAL, "Unexpected word size");
2382 return 0;
2383 }
2384 int ElfType(const ElfW(Sym) *) {
2385 ABSL_RAW_LOG(FATAL, "Unexpected word size");
2386 return 0;
2387 }
2388 #endif
2389
2390 // Extract an element from one of the ELF tables, cast it to desired type.
2391 // This is just a simple arithmetic and a glorified cast.
2392 // Callers are responsible for bounds checking.
2393 template <typename T>
GetTableElement(const ElfW (Ehdr)* ehdr,ElfW (Off)table_offset,ElfW (Word)element_size,size_t index)2394 const T *GetTableElement(const ElfW(Ehdr) * ehdr, ElfW(Off) table_offset,
2395 ElfW(Word) element_size, size_t index) {
2396 return reinterpret_cast<const T*>(reinterpret_cast<const char *>(ehdr)
2397 + table_offset
2398 + index * element_size);
2399 }
2400
2401 } // namespace
2402
2403 const void *const ElfMemImage::kInvalidBase =
2404 reinterpret_cast<const void *>(~0L);
2405
ElfMemImage(const void * base)2406 ElfMemImage::ElfMemImage(const void *base) {
2407 ABSL_RAW_CHECK(base != kInvalidBase, "bad pointer");
2408 Init(base);
2409 }
2410
GetNumSymbols() const2411 int ElfMemImage::GetNumSymbols() const {
2412 if (!hash_) {
2413 return 0;
2414 }
2415 // See http://www.caldera.com/developers/gabi/latest/ch5.dynamic.html#hash
2416 return hash_[1];
2417 }
2418
ElfW(Sym)2419 const ElfW(Sym) *ElfMemImage::GetDynsym(int index) const {
2420 ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
2421 return dynsym_ + index;
2422 }
2423
ElfW(Versym)2424 const ElfW(Versym) *ElfMemImage::GetVersym(int index) const {
2425 ABSL_RAW_CHECK(index < GetNumSymbols(), "index out of range");
2426 return versym_ + index;
2427 }
2428
ElfW(Phdr)2429 const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const {
2430 ABSL_RAW_CHECK(index < ehdr_->e_phnum, "index out of range");
2431 return GetTableElement<ElfW(Phdr)>(ehdr_,
2432 ehdr_->e_phoff,
2433 ehdr_->e_phentsize,
2434 index);
2435 }
2436
GetDynstr(ElfW (Word)offset) const2437 const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
2438 ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
2439 return dynstr_ + offset;
2440 }
2441
GetSymAddr(const ElfW (Sym)* sym) const2442 const void *ElfMemImage::GetSymAddr(const ElfW(Sym) *sym) const {
2443 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE) {
2444 // Symbol corresponds to "special" (e.g. SHN_ABS) section.
2445 return reinterpret_cast<const void *>(sym->st_value);
2446 }
2447 ABSL_RAW_CHECK(link_base_ < sym->st_value, "symbol out of range");
2448 return GetTableElement<char>(ehdr_, 0, 1, sym->st_value) - link_base_;
2449 }
2450
ElfW(Verdef)2451 const ElfW(Verdef) *ElfMemImage::GetVerdef(int index) const {
2452 ABSL_RAW_CHECK(0 <= index && static_cast<size_t>(index) <= verdefnum_,
2453 "index out of range");
2454 const ElfW(Verdef) *version_definition = verdef_;
2455 while (version_definition->vd_ndx < index && version_definition->vd_next) {
2456 const char *const version_definition_as_char =
2457 reinterpret_cast<const char *>(version_definition);
2458 version_definition =
2459 reinterpret_cast<const ElfW(Verdef) *>(version_definition_as_char +
2460 version_definition->vd_next);
2461 }
2462 return version_definition->vd_ndx == index ? version_definition : nullptr;
2463 }
2464
ElfW(Verdaux)2465 const ElfW(Verdaux) *ElfMemImage::GetVerdefAux(
2466 const ElfW(Verdef) *verdef) const {
2467 return reinterpret_cast<const ElfW(Verdaux) *>(verdef+1);
2468 }
2469
GetVerstr(ElfW (Word)offset) const2470 const char *ElfMemImage::GetVerstr(ElfW(Word) offset) const {
2471 ABSL_RAW_CHECK(offset < strsize_, "offset out of range");
2472 return dynstr_ + offset;
2473 }
2474
Init(const void * base)2475 void ElfMemImage::Init(const void *base) {
2476 ehdr_ = nullptr;
2477 dynsym_ = nullptr;
2478 dynstr_ = nullptr;
2479 versym_ = nullptr;
2480 verdef_ = nullptr;
2481 hash_ = nullptr;
2482 strsize_ = 0;
2483 verdefnum_ = 0;
2484 link_base_ = ~0L; // Sentinel: PT_LOAD .p_vaddr can't possibly be this.
2485 if (!base) {
2486 return;
2487 }
2488 const intptr_t base_as_uintptr_t = reinterpret_cast<uintptr_t>(base);
2489 // Fake VDSO has low bit set.
2490 const bool fake_vdso = ((base_as_uintptr_t & 1) != 0);
2491 base = reinterpret_cast<const void *>(base_as_uintptr_t & ~1);
2492 const char *const base_as_char = reinterpret_cast<const char *>(base);
2493 if (base_as_char[EI_MAG0] != ELFMAG0 || base_as_char[EI_MAG1] != ELFMAG1 ||
2494 base_as_char[EI_MAG2] != ELFMAG2 || base_as_char[EI_MAG3] != ELFMAG3) {
2495 assert(false);
2496 return;
2497 }
2498 int elf_class = base_as_char[EI_CLASS];
2499 if (elf_class != kElfClass) {
2500 assert(false);
2501 return;
2502 }
2503 switch (base_as_char[EI_DATA]) {
2504 case ELFDATA2LSB: {
2505 if (__LITTLE_ENDIAN != __BYTE_ORDER) {
2506 assert(false);
2507 return;
2508 }
2509 break;
2510 }
2511 case ELFDATA2MSB: {
2512 if (__BIG_ENDIAN != __BYTE_ORDER) {
2513 assert(false);
2514 return;
2515 }
2516 break;
2517 }
2518 default: {
2519 assert(false);
2520 return;
2521 }
2522 }
2523
2524 ehdr_ = reinterpret_cast<const ElfW(Ehdr) *>(base);
2525 const ElfW(Phdr) *dynamic_program_header = nullptr;
2526 for (int i = 0; i < ehdr_->e_phnum; ++i) {
2527 const ElfW(Phdr) *const program_header = GetPhdr(i);
2528 switch (program_header->p_type) {
2529 case PT_LOAD:
2530 if (!~link_base_) {
2531 link_base_ = program_header->p_vaddr;
2532 }
2533 break;
2534 case PT_DYNAMIC:
2535 dynamic_program_header = program_header;
2536 break;
2537 }
2538 }
2539 if (!~link_base_ || !dynamic_program_header) {
2540 assert(false);
2541 // Mark this image as not present. Can not recur infinitely.
2542 Init(nullptr);
2543 return;
2544 }
2545 ptrdiff_t relocation =
2546 base_as_char - reinterpret_cast<const char *>(link_base_);
2547 ElfW(Dyn) *dynamic_entry =
2548 reinterpret_cast<ElfW(Dyn) *>(dynamic_program_header->p_vaddr +
2549 relocation);
2550 for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
2551 ElfW(Xword) value = dynamic_entry->d_un.d_val;
2552 if (fake_vdso) {
2553 // A complication: in the real VDSO, dynamic entries are not relocated
2554 // (it wasn't loaded by a dynamic loader). But when testing with a
2555 // "fake" dlopen()ed vdso library, the loader relocates some (but
2556 // not all!) of them before we get here.
2557 if (dynamic_entry->d_tag == DT_VERDEF) {
2558 // The only dynamic entry (of the ones we care about) libc-2.3.6
2559 // loader doesn't relocate.
2560 value += relocation;
2561 }
2562 } else {
2563 // Real VDSO. Everything needs to be relocated.
2564 value += relocation;
2565 }
2566 switch (dynamic_entry->d_tag) {
2567 case DT_HASH:
2568 hash_ = reinterpret_cast<ElfW(Word) *>(value);
2569 break;
2570 case DT_SYMTAB:
2571 dynsym_ = reinterpret_cast<ElfW(Sym) *>(value);
2572 break;
2573 case DT_STRTAB:
2574 dynstr_ = reinterpret_cast<const char *>(value);
2575 break;
2576 case DT_VERSYM:
2577 versym_ = reinterpret_cast<ElfW(Versym) *>(value);
2578 break;
2579 case DT_VERDEF:
2580 verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
2581 break;
2582 case DT_VERDEFNUM:
2583 verdefnum_ = dynamic_entry->d_un.d_val;
2584 break;
2585 case DT_STRSZ:
2586 strsize_ = dynamic_entry->d_un.d_val;
2587 break;
2588 default:
2589 // Unrecognized entries explicitly ignored.
2590 break;
2591 }
2592 }
2593 if (!hash_ || !dynsym_ || !dynstr_ || !versym_ ||
2594 !verdef_ || !verdefnum_ || !strsize_) {
2595 assert(false); // invalid VDSO
2596 // Mark this image as not present. Can not recur infinitely.
2597 Init(nullptr);
2598 return;
2599 }
2600 }
2601
LookupSymbol(const char * name,const char * version,int type,SymbolInfo * info_out) const2602 bool ElfMemImage::LookupSymbol(const char *name,
2603 const char *version,
2604 int type,
2605 SymbolInfo *info_out) const {
2606 for (const SymbolInfo& info : *this) {
2607 if (strcmp(info.name, name) == 0 && strcmp(info.version, version) == 0 &&
2608 ElfType(info.symbol) == type) {
2609 if (info_out) {
2610 *info_out = info;
2611 }
2612 return true;
2613 }
2614 }
2615 return false;
2616 }
2617
LookupSymbolByAddress(const void * address,SymbolInfo * info_out) const2618 bool ElfMemImage::LookupSymbolByAddress(const void *address,
2619 SymbolInfo *info_out) const {
2620 for (const SymbolInfo& info : *this) {
2621 const char *const symbol_start =
2622 reinterpret_cast<const char *>(info.address);
2623 const char *const symbol_end = symbol_start + info.symbol->st_size;
2624 if (symbol_start <= address && address < symbol_end) {
2625 if (info_out) {
2626 // Client wants to know details for that symbol (the usual case).
2627 if (ElfBind(info.symbol) == STB_GLOBAL) {
2628 // Strong symbol; just return it.
2629 *info_out = info;
2630 return true;
2631 } else {
2632 // Weak or local. Record it, but keep looking for a strong one.
2633 *info_out = info;
2634 }
2635 } else {
2636 // Client only cares if there is an overlapping symbol.
2637 return true;
2638 }
2639 }
2640 }
2641 return false;
2642 }
2643
SymbolIterator(const void * const image,int index)2644 ElfMemImage::SymbolIterator::SymbolIterator(const void *const image, int index)
2645 : index_(index), image_(image) {
2646 }
2647
operator ->() const2648 const ElfMemImage::SymbolInfo *ElfMemImage::SymbolIterator::operator->() const {
2649 return &info_;
2650 }
2651
operator *() const2652 const ElfMemImage::SymbolInfo& ElfMemImage::SymbolIterator::operator*() const {
2653 return info_;
2654 }
2655
operator ==(const SymbolIterator & rhs) const2656 bool ElfMemImage::SymbolIterator::operator==(const SymbolIterator &rhs) const {
2657 return this->image_ == rhs.image_ && this->index_ == rhs.index_;
2658 }
2659
operator !=(const SymbolIterator & rhs) const2660 bool ElfMemImage::SymbolIterator::operator!=(const SymbolIterator &rhs) const {
2661 return !(*this == rhs);
2662 }
2663
operator ++()2664 ElfMemImage::SymbolIterator &ElfMemImage::SymbolIterator::operator++() {
2665 this->Update(1);
2666 return *this;
2667 }
2668
begin() const2669 ElfMemImage::SymbolIterator ElfMemImage::begin() const {
2670 SymbolIterator it(this, 0);
2671 it.Update(0);
2672 return it;
2673 }
2674
end() const2675 ElfMemImage::SymbolIterator ElfMemImage::end() const {
2676 return SymbolIterator(this, GetNumSymbols());
2677 }
2678
Update(int increment)2679 void ElfMemImage::SymbolIterator::Update(int increment) {
2680 const ElfMemImage *image = reinterpret_cast<const ElfMemImage *>(image_);
2681 ABSL_RAW_CHECK(image->IsPresent() || increment == 0, "");
2682 if (!image->IsPresent()) {
2683 return;
2684 }
2685 index_ += increment;
2686 if (index_ >= image->GetNumSymbols()) {
2687 index_ = image->GetNumSymbols();
2688 return;
2689 }
2690 const ElfW(Sym) *symbol = image->GetDynsym(index_);
2691 const ElfW(Versym) *version_symbol = image->GetVersym(index_);
2692 ABSL_RAW_CHECK(symbol && version_symbol, "");
2693 const char *const symbol_name = image->GetDynstr(symbol->st_name);
2694 const ElfW(Versym) version_index = version_symbol[0] & VERSYM_VERSION;
2695 const ElfW(Verdef) *version_definition = nullptr;
2696 const char *version_name = "";
2697 if (symbol->st_shndx == SHN_UNDEF) {
2698 // Undefined symbols reference DT_VERNEED, not DT_VERDEF, and
2699 // version_index could well be greater than verdefnum_, so calling
2700 // GetVerdef(version_index) may trigger assertion.
2701 } else {
2702 version_definition = image->GetVerdef(version_index);
2703 }
2704 if (version_definition) {
2705 // I am expecting 1 or 2 auxiliary entries: 1 for the version itself,
2706 // optional 2nd if the version has a parent.
2707 ABSL_RAW_CHECK(
2708 version_definition->vd_cnt == 1 || version_definition->vd_cnt == 2,
2709 "wrong number of entries");
2710 const ElfW(Verdaux) *version_aux = image->GetVerdefAux(version_definition);
2711 version_name = image->GetVerstr(version_aux->vda_name);
2712 }
2713 info_.name = symbol_name;
2714 info_.version = version_name;
2715 info_.address = image->GetSymAddr(symbol);
2716 info_.symbol = symbol;
2717 }
2718
2719 } // namespace debug_internal
2720 } // namespace absl
2721
2722 #endif // ABSL_HAVE_ELF_MEM_IMAGE
2723 // Copyright 2017 The Abseil Authors.
2724 //
2725 // Licensed under the Apache License, Version 2.0 (the "License");
2726 // you may not use this file except in compliance with the License.
2727 // You may obtain a copy of the License at
2728 //
2729 // http://www.apache.org/licenses/LICENSE-2.0
2730 //
2731 // Unless required by applicable law or agreed to in writing, software
2732 // distributed under the License is distributed on an "AS IS" BASIS,
2733 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2734 // See the License for the specific language governing permissions and
2735 // limitations under the License.
2736
2737 // Allow dynamic symbol lookup in the kernel VDSO page.
2738 //
2739 // VDSOSupport -- a class representing kernel VDSO (if present).
2740
2741
2742 #ifdef ABSL_HAVE_VDSO_SUPPORT // defined in vdso_support.h
2743
2744 #include <fcntl.h>
2745 #include <sys/syscall.h>
2746 #include <unistd.h>
2747
2748
2749 #ifndef AT_SYSINFO_EHDR
2750 #define AT_SYSINFO_EHDR 33 // for crosstoolv10
2751 #endif
2752
2753 namespace absl {
2754 namespace debug_internal {
2755
2756 std::atomic<const void *> VDSOSupport::vdso_base_(
2757 debug_internal::ElfMemImage::kInvalidBase);
2758 std::atomic<VDSOSupport::GetCpuFn> VDSOSupport::getcpu_fn_(&InitAndGetCPU);
VDSOSupport()2759 VDSOSupport::VDSOSupport()
2760 // If vdso_base_ is still set to kInvalidBase, we got here
2761 // before VDSOSupport::Init has been called. Call it now.
2762 : image_(vdso_base_.load(std::memory_order_relaxed) ==
2763 debug_internal::ElfMemImage::kInvalidBase
2764 ? Init()
2765 : vdso_base_.load(std::memory_order_relaxed)) {}
2766
2767 // NOTE: we can't use GoogleOnceInit() below, because we can be
2768 // called by tcmalloc, and none of the *once* stuff may be functional yet.
2769 //
2770 // In addition, we hope that the VDSOSupportHelper constructor
2771 // causes this code to run before there are any threads, and before
2772 // InitGoogle() has executed any chroot or setuid calls.
2773 //
2774 // Finally, even if there is a race here, it is harmless, because
2775 // the operation should be idempotent.
Init()2776 const void *VDSOSupport::Init() {
2777 if (vdso_base_.load(std::memory_order_relaxed) ==
2778 debug_internal::ElfMemImage::kInvalidBase) {
2779 {
2780 // Valgrind zaps AT_SYSINFO_EHDR and friends from the auxv[]
2781 // on stack, and so glibc works as if VDSO was not present.
2782 // But going directly to kernel via /proc/self/auxv below bypasses
2783 // Valgrind zapping. So we check for Valgrind separately.
2784 if (RunningOnValgrind()) {
2785 vdso_base_.store(nullptr, std::memory_order_relaxed);
2786 getcpu_fn_.store(&GetCPUViaSyscall, std::memory_order_relaxed);
2787 return nullptr;
2788 }
2789 int fd = open("/proc/self/auxv", O_RDONLY);
2790 if (fd == -1) {
2791 // Kernel too old to have a VDSO.
2792 vdso_base_.store(nullptr, std::memory_order_relaxed);
2793 getcpu_fn_.store(&GetCPUViaSyscall, std::memory_order_relaxed);
2794 return nullptr;
2795 }
2796 ElfW(auxv_t) aux;
2797 while (read(fd, &aux, sizeof(aux)) == sizeof(aux)) {
2798 if (aux.a_type == AT_SYSINFO_EHDR) {
2799 vdso_base_.store(reinterpret_cast<void *>(aux.a_un.a_val),
2800 std::memory_order_relaxed);
2801 break;
2802 }
2803 }
2804 close(fd);
2805 }
2806 if (vdso_base_.load(std::memory_order_relaxed) ==
2807 debug_internal::ElfMemImage::kInvalidBase) {
2808 // Didn't find AT_SYSINFO_EHDR in auxv[].
2809 vdso_base_.store(nullptr, std::memory_order_relaxed);
2810 }
2811 }
2812 GetCpuFn fn = &GetCPUViaSyscall; // default if VDSO not present.
2813 if (vdso_base_.load(std::memory_order_relaxed)) {
2814 VDSOSupport vdso;
2815 SymbolInfo info;
2816 if (vdso.LookupSymbol("__vdso_getcpu", "LINUX_2.6", STT_FUNC, &info)) {
2817 fn = reinterpret_cast<GetCpuFn>(const_cast<void *>(info.address));
2818 }
2819 }
2820 // Subtle: this code runs outside of any locks; prevent compiler
2821 // from assigning to getcpu_fn_ more than once.
2822 getcpu_fn_.store(fn, std::memory_order_relaxed);
2823 return vdso_base_.load(std::memory_order_relaxed);
2824 }
2825
SetBase(const void * base)2826 const void *VDSOSupport::SetBase(const void *base) {
2827 ABSL_RAW_CHECK(base != debug_internal::ElfMemImage::kInvalidBase,
2828 "internal error");
2829 const void *old_base = vdso_base_.load(std::memory_order_relaxed);
2830 vdso_base_.store(base, std::memory_order_relaxed);
2831 image_.Init(base);
2832 // Also reset getcpu_fn_, so GetCPU could be tested with simulated VDSO.
2833 getcpu_fn_.store(&InitAndGetCPU, std::memory_order_relaxed);
2834 return old_base;
2835 }
2836
LookupSymbol(const char * name,const char * version,int type,SymbolInfo * info) const2837 bool VDSOSupport::LookupSymbol(const char *name,
2838 const char *version,
2839 int type,
2840 SymbolInfo *info) const {
2841 return image_.LookupSymbol(name, version, type, info);
2842 }
2843
LookupSymbolByAddress(const void * address,SymbolInfo * info_out) const2844 bool VDSOSupport::LookupSymbolByAddress(const void *address,
2845 SymbolInfo *info_out) const {
2846 return image_.LookupSymbolByAddress(address, info_out);
2847 }
2848
2849 // NOLINT on 'long' because this routine mimics kernel api.
GetCPUViaSyscall(unsigned * cpu,void *,void *)2850 long VDSOSupport::GetCPUViaSyscall(unsigned *cpu, // NOLINT(runtime/int)
2851 void *, void *) {
2852 #ifdef SYS_getcpu
2853 return syscall(SYS_getcpu, cpu, nullptr, nullptr);
2854 #else
2855 // x86_64 never implemented sys_getcpu(), except as a VDSO call.
2856 errno = ENOSYS;
2857 return -1;
2858 #endif
2859 }
2860
2861 // Use fast __vdso_getcpu if available.
InitAndGetCPU(unsigned * cpu,void * x,void * y)2862 long VDSOSupport::InitAndGetCPU(unsigned *cpu, // NOLINT(runtime/int)
2863 void *x, void *y) {
2864 Init();
2865 GetCpuFn fn = getcpu_fn_.load(std::memory_order_relaxed);
2866 ABSL_RAW_CHECK(fn != &InitAndGetCPU, "Init() did not set getcpu_fn_");
2867 return (*fn)(cpu, x, y);
2868 }
2869
2870 // This function must be very fast, and may be called from very
2871 // low level (e.g. tcmalloc). Hence I avoid things like
2872 // GoogleOnceInit() and ::operator new.
2873 ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
GetCPU()2874 int GetCPU() {
2875 unsigned cpu;
2876 int ret_code = (*VDSOSupport::getcpu_fn_)(&cpu, nullptr, nullptr);
2877 return ret_code == 0 ? cpu : ret_code;
2878 }
2879
2880 // We need to make sure VDSOSupport::Init() is called before
2881 // InitGoogle() does any setuid or chroot calls. If VDSOSupport
2882 // is used in any global constructor, this will happen, since
2883 // VDSOSupport's constructor calls Init. But if not, we need to
2884 // ensure it here, with a global constructor of our own. This
2885 // is an allowed exception to the normal rule against non-trivial
2886 // global constructors.
2887 static class VDSOInitHelper {
2888 public:
VDSOInitHelper()2889 VDSOInitHelper() { VDSOSupport::Init(); }
2890 } vdso_init_helper;
2891
2892 } // namespace debug_internal
2893 } // namespace absl
2894
2895 #endif // ABSL_HAVE_VDSO_SUPPORT
2896 // Copyright 2017 The Abseil Authors.
2897 //
2898 // Licensed under the Apache License, Version 2.0 (the "License");
2899 // you may not use this file except in compliance with the License.
2900 // You may obtain a copy of the License at
2901 //
2902 // http://www.apache.org/licenses/LICENSE-2.0
2903 //
2904 // Unless required by applicable law or agreed to in writing, software
2905 // distributed under the License is distributed on an "AS IS" BASIS,
2906 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2907 // See the License for the specific language governing permissions and
2908 // limitations under the License.
2909
2910 #include <stdlib.h>
2911 #include <string.h>
2912
2913
2914 #ifndef __has_feature
2915 #define __has_feature(x) 0
2916 #endif
2917
2918 /* Compiler-based ThreadSanitizer defines
2919 DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL = 1
2920 and provides its own definitions of the functions. */
2921
2922 #ifndef DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL
2923 # define DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL 0
2924 #endif
2925
2926 /* Each function is empty and called (via a macro) only in debug mode.
2927 The arguments are captured by dynamic tools at runtime. */
2928
2929 #if DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 && !defined(__native_client__)
2930
2931 #if __has_feature(memory_sanitizer)
2932 #include <sanitizer/msan_interface.h>
2933 #endif
2934
2935 #ifdef __cplusplus
2936 extern "C" {
2937 #endif
2938
AnnotateRWLockCreate(const char *,int,const volatile void *)2939 void AnnotateRWLockCreate(const char *, int,
2940 const volatile void *){}
AnnotateRWLockDestroy(const char *,int,const volatile void *)2941 void AnnotateRWLockDestroy(const char *, int,
2942 const volatile void *){}
AnnotateRWLockAcquired(const char *,int,const volatile void *,long)2943 void AnnotateRWLockAcquired(const char *, int,
2944 const volatile void *, long){}
AnnotateRWLockReleased(const char *,int,const volatile void *,long)2945 void AnnotateRWLockReleased(const char *, int,
2946 const volatile void *, long){}
AnnotateBenignRace(const char *,int,const volatile void *,const char *)2947 void AnnotateBenignRace(const char *, int,
2948 const volatile void *,
2949 const char *){}
AnnotateBenignRaceSized(const char *,int,const volatile void *,size_t,const char *)2950 void AnnotateBenignRaceSized(const char *, int,
2951 const volatile void *,
2952 size_t,
2953 const char *) {}
AnnotateThreadName(const char *,int,const char *)2954 void AnnotateThreadName(const char *, int,
2955 const char *){}
AnnotateIgnoreReadsBegin(const char *,int)2956 void AnnotateIgnoreReadsBegin(const char *, int){}
AnnotateIgnoreReadsEnd(const char *,int)2957 void AnnotateIgnoreReadsEnd(const char *, int){}
AnnotateIgnoreWritesBegin(const char *,int)2958 void AnnotateIgnoreWritesBegin(const char *, int){}
AnnotateIgnoreWritesEnd(const char *,int)2959 void AnnotateIgnoreWritesEnd(const char *, int){}
AnnotateEnableRaceDetection(const char *,int,int)2960 void AnnotateEnableRaceDetection(const char *, int, int){}
AnnotateMemoryIsInitialized(const char *,int,const volatile void * mem,size_t size)2961 void AnnotateMemoryIsInitialized(const char *, int,
2962 const volatile void *mem, size_t size) {
2963 #if __has_feature(memory_sanitizer)
2964 __msan_unpoison(mem, size);
2965 #else
2966 (void)mem;
2967 (void)size;
2968 #endif
2969 }
2970
AnnotateMemoryIsUninitialized(const char *,int,const volatile void * mem,size_t size)2971 void AnnotateMemoryIsUninitialized(const char *, int,
2972 const volatile void *mem, size_t size) {
2973 #if __has_feature(memory_sanitizer)
2974 __msan_allocated_memory(mem, size);
2975 #else
2976 (void)mem;
2977 (void)size;
2978 #endif
2979 }
2980
GetRunningOnValgrind(void)2981 static int GetRunningOnValgrind(void) {
2982 #ifdef RUNNING_ON_VALGRIND
2983 if (RUNNING_ON_VALGRIND) return 1;
2984 #endif
2985 char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND");
2986 if (running_on_valgrind_str) {
2987 return strcmp(running_on_valgrind_str, "0") != 0;
2988 }
2989 return 0;
2990 }
2991
2992 /* See the comments in dynamic_annotations.h */
RunningOnValgrind(void)2993 int RunningOnValgrind(void) {
2994 static volatile int running_on_valgrind = -1;
2995 int local_running_on_valgrind = running_on_valgrind;
2996 /* C doesn't have thread-safe initialization of statics, and we
2997 don't want to depend on pthread_once here, so hack it. */
2998 ANNOTATE_BENIGN_RACE(&running_on_valgrind, "safe hack");
2999 if (local_running_on_valgrind == -1)
3000 running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind();
3001 return local_running_on_valgrind;
3002 }
3003
3004 /* See the comments in dynamic_annotations.h */
ValgrindSlowdown(void)3005 double ValgrindSlowdown(void) {
3006 /* Same initialization hack as in RunningOnValgrind(). */
3007 static volatile double slowdown = 0.0;
3008 double local_slowdown = slowdown;
3009 ANNOTATE_BENIGN_RACE(&slowdown, "safe hack");
3010 if (RunningOnValgrind() == 0) {
3011 return 1.0;
3012 }
3013 if (local_slowdown == 0.0) {
3014 char *env = getenv("VALGRIND_SLOWDOWN");
3015 slowdown = local_slowdown = env ? atof(env) : 50.0;
3016 }
3017 return local_slowdown;
3018 }
3019
3020 #ifdef __cplusplus
3021 } // extern "C"
3022 #endif
3023 #endif /* DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 */
3024