sanitizer_tls_get_addr.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===-- sanitizer_tls_get_addr.h --------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Handle the __tls_get_addr call.
  10. //
  11. // All this magic is specific to glibc and is required to workaround
  12. // the lack of interface that would tell us about the Dynamic TLS (DTLS).
  13. // https://sourceware.org/bugzilla/show_bug.cgi?id=16291
  14. //
  15. // Before 2.25: every DTLS chunk is allocated with __libc_memalign,
  16. // which we intercept and thus know where is the DTLS.
  17. //
  18. // Since 2.25: DTLS chunks are allocated with malloc. We could co-opt
  19. // the malloc interceptor to keep track of the last allocation, similar
  20. // to how we handle __libc_memalign; however, this adds some overhead
  21. // (since malloc, unlike __libc_memalign, is commonly called), and
  22. // requires care to avoid false negatives for LeakSanitizer.
  23. // Instead, we rely on our internal allocators - which keep track of all
  24. // its allocations - to determine if an address points to a malloc
  25. // allocation.
  26. //
  27. // There exists a since-deprecated version of Google's internal glibc fork
  28. // that used __signal_safe_memalign. DTLS_on_tls_get_addr relied on a
  29. // heuristic check (is the allocation 16 bytes from the start of a page
  30. // boundary?), which was sometimes erroneous:
  31. // https://bugs.chromium.org/p/chromium/issues/detail?id=1275223#c15
  32. // Since that check has no practical use anymore, we have removed it.
  33. //
  34. //===----------------------------------------------------------------------===//
  35. #ifndef SANITIZER_TLS_GET_ADDR_H
  36. #define SANITIZER_TLS_GET_ADDR_H
  37. #include "sanitizer_atomic.h"
  38. #include "sanitizer_common.h"
  39. namespace __sanitizer {
  40. struct DTLS {
  41. // Array of DTLS chunks for the current Thread.
  42. // If beg == 0, the chunk is unused.
  43. struct DTV {
  44. uptr beg, size;
  45. };
  46. struct DTVBlock {
  47. atomic_uintptr_t next;
  48. DTV dtvs[(4096UL - sizeof(next)) / sizeof(DTLS::DTV)];
  49. };
  50. static_assert(sizeof(DTVBlock) <= 4096UL, "Unexpected block size");
  51. atomic_uintptr_t dtv_block;
  52. // Auxiliary fields, don't access them outside sanitizer_tls_get_addr.cpp
  53. uptr last_memalign_size;
  54. uptr last_memalign_ptr;
  55. };
  56. template <typename Fn>
  57. void ForEachDVT(DTLS *dtls, const Fn &fn) {
  58. DTLS::DTVBlock *block =
  59. (DTLS::DTVBlock *)atomic_load(&dtls->dtv_block, memory_order_acquire);
  60. while (block) {
  61. int id = 0;
  62. for (auto &d : block->dtvs) fn(d, id++);
  63. block = (DTLS::DTVBlock *)atomic_load(&block->next, memory_order_acquire);
  64. }
  65. }
  66. // Returns pointer and size of a linker-allocated TLS block.
  67. // Each block is returned exactly once.
  68. DTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res, uptr static_tls_begin,
  69. uptr static_tls_end);
  70. void DTLS_on_libc_memalign(void *ptr, uptr size);
  71. DTLS *DTLS_Get();
  72. void DTLS_Destroy(); // Make sure to call this before the thread is destroyed.
  73. // Returns true if DTLS of suspended thread is in destruction process.
  74. bool DTLSInDestruction(DTLS *dtls);
  75. } // namespace __sanitizer
  76. #endif // SANITIZER_TLS_GET_ADDR_H