sanitizer_tls_get_addr.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //===-- sanitizer_tls_get_addr.cpp ----------------------------------------===//
  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. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_tls_get_addr.h"
  13. #include "sanitizer_allocator_interface.h"
  14. #include "sanitizer_atomic.h"
  15. #include "sanitizer_flags.h"
  16. #include "sanitizer_platform_interceptors.h"
  17. namespace __sanitizer {
  18. #if SANITIZER_INTERCEPT_TLS_GET_ADDR
  19. // The actual parameter that comes to __tls_get_addr
  20. // is a pointer to a struct with two words in it:
  21. struct TlsGetAddrParam {
  22. uptr dso_id;
  23. uptr offset;
  24. };
  25. // This must be static TLS
  26. __attribute__((tls_model("initial-exec")))
  27. static __thread DTLS dtls;
  28. // Make sure we properly destroy the DTLS objects:
  29. // this counter should never get too large.
  30. static atomic_uintptr_t number_of_live_dtls;
  31. static const uptr kDestroyedThread = -1;
  32. static void DTLS_Deallocate(DTLS::DTVBlock *block) {
  33. VReport(2, "__tls_get_addr: DTLS_Deallocate %p\n", (void *)block);
  34. UnmapOrDie(block, sizeof(DTLS::DTVBlock));
  35. atomic_fetch_sub(&number_of_live_dtls, 1, memory_order_relaxed);
  36. }
  37. static DTLS::DTVBlock *DTLS_NextBlock(atomic_uintptr_t *cur) {
  38. uptr v = atomic_load(cur, memory_order_acquire);
  39. if (v == kDestroyedThread)
  40. return nullptr;
  41. DTLS::DTVBlock *next = (DTLS::DTVBlock *)v;
  42. if (next)
  43. return next;
  44. DTLS::DTVBlock *new_dtv =
  45. (DTLS::DTVBlock *)MmapOrDie(sizeof(DTLS::DTVBlock), "DTLS_NextBlock");
  46. uptr prev = 0;
  47. if (!atomic_compare_exchange_strong(cur, &prev, (uptr)new_dtv,
  48. memory_order_seq_cst)) {
  49. UnmapOrDie(new_dtv, sizeof(DTLS::DTVBlock));
  50. return (DTLS::DTVBlock *)prev;
  51. }
  52. uptr num_live_dtls =
  53. atomic_fetch_add(&number_of_live_dtls, 1, memory_order_relaxed);
  54. VReport(2, "__tls_get_addr: DTLS_NextBlock %p %zd\n", (void *)&dtls,
  55. num_live_dtls);
  56. return new_dtv;
  57. }
  58. static DTLS::DTV *DTLS_Find(uptr id) {
  59. VReport(2, "__tls_get_addr: DTLS_Find %p %zd\n", (void *)&dtls, id);
  60. static constexpr uptr kPerBlock = ARRAY_SIZE(DTLS::DTVBlock::dtvs);
  61. DTLS::DTVBlock *cur = DTLS_NextBlock(&dtls.dtv_block);
  62. if (!cur)
  63. return nullptr;
  64. for (; id >= kPerBlock; id -= kPerBlock) cur = DTLS_NextBlock(&cur->next);
  65. return cur->dtvs + id;
  66. }
  67. void DTLS_Destroy() {
  68. if (!common_flags()->intercept_tls_get_addr) return;
  69. VReport(2, "__tls_get_addr: DTLS_Destroy %p\n", (void *)&dtls);
  70. DTLS::DTVBlock *block = (DTLS::DTVBlock *)atomic_exchange(
  71. &dtls.dtv_block, kDestroyedThread, memory_order_release);
  72. while (block) {
  73. DTLS::DTVBlock *next =
  74. (DTLS::DTVBlock *)atomic_load(&block->next, memory_order_acquire);
  75. DTLS_Deallocate(block);
  76. block = next;
  77. }
  78. }
  79. #if defined(__powerpc64__) || defined(__mips__)
  80. // This is glibc's TLS_DTV_OFFSET:
  81. // "Dynamic thread vector pointers point 0x8000 past the start of each
  82. // TLS block." (sysdeps/<arch>/dl-tls.h)
  83. static const uptr kDtvOffset = 0x8000;
  84. #elif defined(__riscv)
  85. // This is glibc's TLS_DTV_OFFSET:
  86. // "Dynamic thread vector pointers point 0x800 past the start of each
  87. // TLS block." (sysdeps/riscv/dl-tls.h)
  88. static const uptr kDtvOffset = 0x800;
  89. #else
  90. static const uptr kDtvOffset = 0;
  91. #endif
  92. extern "C" {
  93. SANITIZER_WEAK_ATTRIBUTE
  94. uptr __sanitizer_get_allocated_size(const void *p);
  95. SANITIZER_WEAK_ATTRIBUTE
  96. const void *__sanitizer_get_allocated_begin(const void *p);
  97. }
  98. DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
  99. uptr static_tls_begin, uptr static_tls_end) {
  100. if (!common_flags()->intercept_tls_get_addr) return 0;
  101. TlsGetAddrParam *arg = reinterpret_cast<TlsGetAddrParam *>(arg_void);
  102. uptr dso_id = arg->dso_id;
  103. DTLS::DTV *dtv = DTLS_Find(dso_id);
  104. if (!dtv || dtv->beg)
  105. return 0;
  106. uptr tls_size = 0;
  107. uptr tls_beg = reinterpret_cast<uptr>(res) - arg->offset - kDtvOffset;
  108. VReport(2,
  109. "__tls_get_addr: %p {0x%zx,0x%zx} => %p; tls_beg: 0x%zx; sp: %p "
  110. "num_live_dtls %zd\n",
  111. (void *)arg, arg->dso_id, arg->offset, res, tls_beg, (void *)&tls_beg,
  112. atomic_load(&number_of_live_dtls, memory_order_relaxed));
  113. if (dtls.last_memalign_ptr == tls_beg) {
  114. tls_size = dtls.last_memalign_size;
  115. VReport(2, "__tls_get_addr: glibc <=2.24 suspected; tls={0x%zx,0x%zx}\n",
  116. tls_beg, tls_size);
  117. } else if (tls_beg >= static_tls_begin && tls_beg < static_tls_end) {
  118. // This is the static TLS block which was initialized / unpoisoned at thread
  119. // creation.
  120. VReport(2, "__tls_get_addr: static tls: 0x%zx\n", tls_beg);
  121. tls_size = 0;
  122. } else if (const void *start =
  123. __sanitizer_get_allocated_begin((void *)tls_beg)) {
  124. tls_beg = (uptr)start;
  125. tls_size = __sanitizer_get_allocated_size(start);
  126. VReport(2, "__tls_get_addr: glibc >=2.25 suspected; tls={0x%zx,0x%zx}\n",
  127. tls_beg, tls_size);
  128. } else {
  129. VReport(2, "__tls_get_addr: Can't guess glibc version\n");
  130. // This may happen inside the DTOR of main thread, so just ignore it.
  131. tls_size = 0;
  132. }
  133. dtv->beg = tls_beg;
  134. dtv->size = tls_size;
  135. return dtv;
  136. }
  137. void DTLS_on_libc_memalign(void *ptr, uptr size) {
  138. if (!common_flags()->intercept_tls_get_addr) return;
  139. VReport(2, "DTLS_on_libc_memalign: %p 0x%zx\n", ptr, size);
  140. dtls.last_memalign_ptr = reinterpret_cast<uptr>(ptr);
  141. dtls.last_memalign_size = size;
  142. }
  143. DTLS *DTLS_Get() { return &dtls; }
  144. bool DTLSInDestruction(DTLS *dtls) {
  145. return atomic_load(&dtls->dtv_block, memory_order_relaxed) ==
  146. kDestroyedThread;
  147. }
  148. #else
  149. void DTLS_on_libc_memalign(void *ptr, uptr size) {}
  150. DTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res,
  151. unsigned long, unsigned long) { return 0; }
  152. DTLS *DTLS_Get() { return 0; }
  153. void DTLS_Destroy() {}
  154. bool DTLSInDestruction(DTLS *dtls) {
  155. UNREACHABLE("dtls is unsupported on this platform!");
  156. }
  157. #endif // SANITIZER_INTERCEPT_TLS_GET_ADDR
  158. } // namespace __sanitizer