sanitizer_tls_get_addr.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_atomic.h"
  14. #include "sanitizer_flags.h"
  15. #include "sanitizer_platform_interceptors.h"
  16. namespace __sanitizer {
  17. #if SANITIZER_INTERCEPT_TLS_GET_ADDR
  18. // The actual parameter that comes to __tls_get_addr
  19. // is a pointer to a struct with two words in it:
  20. struct TlsGetAddrParam {
  21. uptr dso_id;
  22. uptr offset;
  23. };
  24. // Glibc starting from 2.19 allocates tls using __signal_safe_memalign,
  25. // which has such header.
  26. struct Glibc_2_19_tls_header {
  27. uptr size;
  28. uptr start;
  29. };
  30. // This must be static TLS
  31. __attribute__((tls_model("initial-exec")))
  32. static __thread DTLS dtls;
  33. // Make sure we properly destroy the DTLS objects:
  34. // this counter should never get too large.
  35. static atomic_uintptr_t number_of_live_dtls;
  36. static const uptr kDestroyedThread = -1;
  37. static void DTLS_Deallocate(DTLS::DTVBlock *block) {
  38. VReport(2, "__tls_get_addr: DTLS_Deallocate %p\n", (void *)block);
  39. UnmapOrDie(block, sizeof(DTLS::DTVBlock));
  40. atomic_fetch_sub(&number_of_live_dtls, 1, memory_order_relaxed);
  41. }
  42. static DTLS::DTVBlock *DTLS_NextBlock(atomic_uintptr_t *cur) {
  43. uptr v = atomic_load(cur, memory_order_acquire);
  44. if (v == kDestroyedThread)
  45. return nullptr;
  46. DTLS::DTVBlock *next = (DTLS::DTVBlock *)v;
  47. if (next)
  48. return next;
  49. DTLS::DTVBlock *new_dtv =
  50. (DTLS::DTVBlock *)MmapOrDie(sizeof(DTLS::DTVBlock), "DTLS_NextBlock");
  51. uptr prev = 0;
  52. if (!atomic_compare_exchange_strong(cur, &prev, (uptr)new_dtv,
  53. memory_order_seq_cst)) {
  54. UnmapOrDie(new_dtv, sizeof(DTLS::DTVBlock));
  55. return (DTLS::DTVBlock *)prev;
  56. }
  57. uptr num_live_dtls =
  58. atomic_fetch_add(&number_of_live_dtls, 1, memory_order_relaxed);
  59. VReport(2, "__tls_get_addr: DTLS_NextBlock %p %zd\n", (void *)&dtls,
  60. num_live_dtls);
  61. return new_dtv;
  62. }
  63. static DTLS::DTV *DTLS_Find(uptr id) {
  64. VReport(2, "__tls_get_addr: DTLS_Find %p %zd\n", (void *)&dtls, id);
  65. static constexpr uptr kPerBlock = ARRAY_SIZE(DTLS::DTVBlock::dtvs);
  66. DTLS::DTVBlock *cur = DTLS_NextBlock(&dtls.dtv_block);
  67. if (!cur)
  68. return nullptr;
  69. for (; id >= kPerBlock; id -= kPerBlock) cur = DTLS_NextBlock(&cur->next);
  70. return cur->dtvs + id;
  71. }
  72. void DTLS_Destroy() {
  73. if (!common_flags()->intercept_tls_get_addr) return;
  74. VReport(2, "__tls_get_addr: DTLS_Destroy %p\n", (void *)&dtls);
  75. DTLS::DTVBlock *block = (DTLS::DTVBlock *)atomic_exchange(
  76. &dtls.dtv_block, kDestroyedThread, memory_order_release);
  77. while (block) {
  78. DTLS::DTVBlock *next =
  79. (DTLS::DTVBlock *)atomic_load(&block->next, memory_order_acquire);
  80. DTLS_Deallocate(block);
  81. block = next;
  82. }
  83. }
  84. #if defined(__powerpc64__) || defined(__mips__)
  85. // This is glibc's TLS_DTV_OFFSET:
  86. // "Dynamic thread vector pointers point 0x8000 past the start of each
  87. // TLS block." (sysdeps/<arch>/dl-tls.h)
  88. static const uptr kDtvOffset = 0x8000;
  89. #elif defined(__riscv)
  90. // This is glibc's TLS_DTV_OFFSET:
  91. // "Dynamic thread vector pointers point 0x800 past the start of each
  92. // TLS block." (sysdeps/riscv/dl-tls.h)
  93. static const uptr kDtvOffset = 0x800;
  94. #else
  95. static const uptr kDtvOffset = 0;
  96. #endif
  97. DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
  98. uptr static_tls_begin, uptr static_tls_end) {
  99. if (!common_flags()->intercept_tls_get_addr) return 0;
  100. TlsGetAddrParam *arg = reinterpret_cast<TlsGetAddrParam *>(arg_void);
  101. uptr dso_id = arg->dso_id;
  102. DTLS::DTV *dtv = DTLS_Find(dso_id);
  103. if (!dtv || dtv->beg)
  104. return 0;
  105. uptr tls_size = 0;
  106. uptr tls_beg = reinterpret_cast<uptr>(res) - arg->offset - kDtvOffset;
  107. VReport(2,
  108. "__tls_get_addr: %p {0x%zx,0x%zx} => %p; tls_beg: 0x%zx; sp: %p "
  109. "num_live_dtls %zd\n",
  110. (void *)arg, arg->dso_id, arg->offset, res, tls_beg, (void *)&tls_beg,
  111. atomic_load(&number_of_live_dtls, memory_order_relaxed));
  112. if (dtls.last_memalign_ptr == tls_beg) {
  113. tls_size = dtls.last_memalign_size;
  114. VReport(2, "__tls_get_addr: glibc <=2.18 suspected; tls={0x%zx,0x%zx}\n",
  115. tls_beg, tls_size);
  116. } else if (tls_beg >= static_tls_begin && tls_beg < static_tls_end) {
  117. // This is the static TLS block which was initialized / unpoisoned at thread
  118. // creation.
  119. VReport(2, "__tls_get_addr: static tls: 0x%zx\n", tls_beg);
  120. tls_size = 0;
  121. } else if ((tls_beg % 4096) == sizeof(Glibc_2_19_tls_header)) {
  122. // We may want to check gnu_get_libc_version().
  123. Glibc_2_19_tls_header *header = (Glibc_2_19_tls_header *)tls_beg - 1;
  124. tls_size = header->size;
  125. tls_beg = header->start;
  126. VReport(2, "__tls_get_addr: glibc >=2.19 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