sanitizer_tls_get_addr.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===-- sanitizer_tls_get_addr.cc -----------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Handle the __tls_get_addr call.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "sanitizer_tls_get_addr.h"
  12. #include "sanitizer_flags.h"
  13. #include "sanitizer_platform_interceptors.h"
  14. namespace __sanitizer {
  15. #if SANITIZER_INTERCEPT_TLS_GET_ADDR
  16. // The actual parameter that comes to __tls_get_addr
  17. // is a pointer to a struct with two words in it:
  18. struct TlsGetAddrParam {
  19. uptr dso_id;
  20. uptr offset;
  21. };
  22. // Glibc starting from 2.19 allocates tls using __signal_safe_memalign,
  23. // which has such header.
  24. struct Glibc_2_19_tls_header {
  25. uptr size;
  26. uptr start;
  27. };
  28. // This must be static TLS
  29. __attribute__((tls_model("initial-exec")))
  30. static __thread DTLS dtls;
  31. // Make sure we properly destroy the DTLS objects:
  32. // this counter should never get too large.
  33. static atomic_uintptr_t number_of_live_dtls;
  34. static const uptr kDestroyedThread = -1;
  35. static inline void DTLS_Deallocate(DTLS::DTV *dtv, uptr size) {
  36. if (!size) return;
  37. VPrintf(2, "__tls_get_addr: DTLS_Deallocate %p %zd\n", dtv, size);
  38. UnmapOrDie(dtv, size * sizeof(DTLS::DTV));
  39. atomic_fetch_sub(&number_of_live_dtls, 1, memory_order_relaxed);
  40. }
  41. static inline void DTLS_Resize(uptr new_size) {
  42. if (dtls.dtv_size >= new_size) return;
  43. new_size = RoundUpToPowerOfTwo(new_size);
  44. new_size = Max(new_size, 4096UL / sizeof(DTLS::DTV));
  45. DTLS::DTV *new_dtv =
  46. (DTLS::DTV *)MmapOrDie(new_size * sizeof(DTLS::DTV), "DTLS_Resize");
  47. uptr num_live_dtls =
  48. atomic_fetch_add(&number_of_live_dtls, 1, memory_order_relaxed);
  49. VPrintf(2, "__tls_get_addr: DTLS_Resize %p %zd\n", &dtls, num_live_dtls);
  50. CHECK_LT(num_live_dtls, 1 << 20);
  51. uptr old_dtv_size = dtls.dtv_size;
  52. DTLS::DTV *old_dtv = dtls.dtv;
  53. if (old_dtv_size)
  54. internal_memcpy(new_dtv, dtls.dtv, dtls.dtv_size * sizeof(DTLS::DTV));
  55. dtls.dtv = new_dtv;
  56. dtls.dtv_size = new_size;
  57. if (old_dtv_size)
  58. DTLS_Deallocate(old_dtv, old_dtv_size);
  59. }
  60. void DTLS_Destroy() {
  61. if (!common_flags()->intercept_tls_get_addr) return;
  62. VPrintf(2, "__tls_get_addr: DTLS_Destroy %p %zd\n", &dtls, dtls.dtv_size);
  63. uptr s = dtls.dtv_size;
  64. dtls.dtv_size = kDestroyedThread; // Do this before unmap for AS-safety.
  65. DTLS_Deallocate(dtls.dtv, s);
  66. }
  67. DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res) {
  68. if (!common_flags()->intercept_tls_get_addr) return 0;
  69. TlsGetAddrParam *arg = reinterpret_cast<TlsGetAddrParam *>(arg_void);
  70. uptr dso_id = arg->dso_id;
  71. if (dtls.dtv_size == kDestroyedThread) return 0;
  72. DTLS_Resize(dso_id + 1);
  73. if (dtls.dtv[dso_id].beg) return 0;
  74. uptr tls_size = 0;
  75. uptr tls_beg = reinterpret_cast<uptr>(res) - arg->offset;
  76. VPrintf(2, "__tls_get_addr: %p {%p,%p} => %p; tls_beg: %p; sp: %p "
  77. "num_live_dtls %zd\n",
  78. arg, arg->dso_id, arg->offset, res, tls_beg, &tls_beg,
  79. atomic_load(&number_of_live_dtls, memory_order_relaxed));
  80. if (dtls.last_memalign_ptr == tls_beg) {
  81. tls_size = dtls.last_memalign_size;
  82. VPrintf(2, "__tls_get_addr: glibc <=2.18 suspected; tls={%p,%p}\n",
  83. tls_beg, tls_size);
  84. } else if ((tls_beg % 4096) == sizeof(Glibc_2_19_tls_header)) {
  85. // We may want to check gnu_get_libc_version().
  86. Glibc_2_19_tls_header *header = (Glibc_2_19_tls_header *)tls_beg - 1;
  87. tls_size = header->size;
  88. tls_beg = header->start;
  89. VPrintf(2, "__tls_get_addr: glibc >=2.19 suspected; tls={%p %p}\n",
  90. tls_beg, tls_size);
  91. } else {
  92. VPrintf(2, "__tls_get_addr: Can't guess glibc version\n");
  93. // This may happen inside the DTOR of main thread, so just ignore it.
  94. tls_size = 0;
  95. }
  96. dtls.dtv[dso_id].beg = tls_beg;
  97. dtls.dtv[dso_id].size = tls_size;
  98. return dtls.dtv + dso_id;
  99. }
  100. void DTLS_on_libc_memalign(void *ptr, uptr size) {
  101. if (!common_flags()->intercept_tls_get_addr) return;
  102. VPrintf(2, "DTLS_on_libc_memalign: %p %p\n", ptr, size);
  103. dtls.last_memalign_ptr = reinterpret_cast<uptr>(ptr);
  104. dtls.last_memalign_size = size;
  105. }
  106. DTLS *DTLS_Get() { return &dtls; }
  107. #else
  108. void DTLS_on_libc_memalign(void *ptr, uptr size) {}
  109. DTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res) { return 0; }
  110. DTLS *DTLS_Get() { return 0; }
  111. void DTLS_Destroy() {}
  112. #endif // SANITIZER_INTERCEPT_TLS_GET_ADDR
  113. } // namespace __sanitizer