sanitizer_tls_get_addr.cc revision 276789
1276789Sdim//===-- sanitizer_tls_get_addr.cc -----------------------------------------===// 2276789Sdim// 3276789Sdim// The LLVM Compiler Infrastructure 4276789Sdim// 5276789Sdim// This file is distributed under the University of Illinois Open Source 6276789Sdim// License. See LICENSE.TXT for details. 7276789Sdim// 8276789Sdim//===----------------------------------------------------------------------===// 9276789Sdim// 10276789Sdim// Handle the __tls_get_addr call. 11276789Sdim// 12276789Sdim//===----------------------------------------------------------------------===// 13276789Sdim 14276789Sdim#include "sanitizer_tls_get_addr.h" 15276789Sdim 16276789Sdim#include "sanitizer_flags.h" 17276789Sdim#include "sanitizer_platform_interceptors.h" 18276789Sdim 19276789Sdimnamespace __sanitizer { 20276789Sdim#if SANITIZER_INTERCEPT_TLS_GET_ADDR 21276789Sdim 22276789Sdim// The actual parameter that comes to __tls_get_addr 23276789Sdim// is a pointer to a struct with two words in it: 24276789Sdimstruct TlsGetAddrParam { 25276789Sdim uptr dso_id; 26276789Sdim uptr offset; 27276789Sdim}; 28276789Sdim 29276789Sdim// Glibc starting from 2.19 allocates tls using __signal_safe_memalign, 30276789Sdim// which has such header. 31276789Sdimstruct Glibc_2_19_tls_header { 32276789Sdim uptr size; 33276789Sdim uptr start; 34276789Sdim}; 35276789Sdim 36276789Sdim// This must be static TLS 37276789Sdim__attribute__((tls_model("initial-exec"))) 38276789Sdimstatic __thread DTLS dtls; 39276789Sdim 40276789Sdim// Make sure we properly destroy the DTLS objects: 41276789Sdim// this counter should never get too large. 42276789Sdimstatic atomic_uintptr_t number_of_live_dtls; 43276789Sdim 44276789Sdimstatic const uptr kDestroyedThread = -1; 45276789Sdim 46276789Sdimstatic inline void DTLS_Deallocate(DTLS::DTV *dtv, uptr size) { 47276789Sdim if (!size) return; 48276789Sdim VPrintf(2, "__tls_get_addr: DTLS_Deallocate %p %zd\n", dtv, size); 49276789Sdim UnmapOrDie(dtv, size * sizeof(DTLS::DTV)); 50276789Sdim atomic_fetch_sub(&number_of_live_dtls, 1, memory_order_relaxed); 51276789Sdim} 52276789Sdim 53276789Sdimstatic inline void DTLS_Resize(uptr new_size) { 54276789Sdim if (dtls.dtv_size >= new_size) return; 55276789Sdim new_size = RoundUpToPowerOfTwo(new_size); 56276789Sdim new_size = Max(new_size, 4096UL / sizeof(DTLS::DTV)); 57276789Sdim DTLS::DTV *new_dtv = 58276789Sdim (DTLS::DTV *)MmapOrDie(new_size * sizeof(DTLS::DTV), "DTLS_Resize"); 59276789Sdim uptr num_live_dtls = 60276789Sdim atomic_fetch_add(&number_of_live_dtls, 1, memory_order_relaxed); 61276789Sdim VPrintf(2, "__tls_get_addr: DTLS_Resize %p %zd\n", &dtls, num_live_dtls); 62276789Sdim CHECK_LT(num_live_dtls, 1 << 20); 63276789Sdim uptr old_dtv_size = dtls.dtv_size; 64276789Sdim DTLS::DTV *old_dtv = dtls.dtv; 65276789Sdim if (old_dtv_size) 66276789Sdim internal_memcpy(new_dtv, dtls.dtv, dtls.dtv_size * sizeof(DTLS::DTV)); 67276789Sdim dtls.dtv = new_dtv; 68276789Sdim dtls.dtv_size = new_size; 69276789Sdim if (old_dtv_size) 70276789Sdim DTLS_Deallocate(old_dtv, old_dtv_size); 71276789Sdim} 72276789Sdim 73276789Sdimvoid DTLS_Destroy() { 74276789Sdim if (!common_flags()->intercept_tls_get_addr) return; 75276789Sdim VPrintf(2, "__tls_get_addr: DTLS_Destroy %p %zd\n", &dtls, dtls.dtv_size); 76276789Sdim uptr s = dtls.dtv_size; 77276789Sdim dtls.dtv_size = kDestroyedThread; // Do this before unmap for AS-safety. 78276789Sdim DTLS_Deallocate(dtls.dtv, s); 79276789Sdim} 80276789Sdim 81276789SdimDTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res) { 82276789Sdim if (!common_flags()->intercept_tls_get_addr) return 0; 83276789Sdim TlsGetAddrParam *arg = reinterpret_cast<TlsGetAddrParam *>(arg_void); 84276789Sdim uptr dso_id = arg->dso_id; 85276789Sdim if (dtls.dtv_size == kDestroyedThread) return 0; 86276789Sdim DTLS_Resize(dso_id + 1); 87276789Sdim if (dtls.dtv[dso_id].beg) return 0; 88276789Sdim uptr tls_size = 0; 89276789Sdim uptr tls_beg = reinterpret_cast<uptr>(res) - arg->offset; 90276789Sdim VPrintf(2, "__tls_get_addr: %p {%p,%p} => %p; tls_beg: %p; sp: %p " 91276789Sdim "num_live_dtls %zd\n", 92276789Sdim arg, arg->dso_id, arg->offset, res, tls_beg, &tls_beg, 93276789Sdim atomic_load(&number_of_live_dtls, memory_order_relaxed)); 94276789Sdim if (dtls.last_memalign_ptr == tls_beg) { 95276789Sdim tls_size = dtls.last_memalign_size; 96276789Sdim VPrintf(2, "__tls_get_addr: glibc <=2.18 suspected; tls={%p,%p}\n", 97276789Sdim tls_beg, tls_size); 98276789Sdim } else if ((tls_beg % 4096) == sizeof(Glibc_2_19_tls_header)) { 99276789Sdim // We may want to check gnu_get_libc_version(). 100276789Sdim Glibc_2_19_tls_header *header = (Glibc_2_19_tls_header *)tls_beg - 1; 101276789Sdim tls_size = header->size; 102276789Sdim tls_beg = header->start; 103276789Sdim VPrintf(2, "__tls_get_addr: glibc >=2.19 suspected; tls={%p %p}\n", 104276789Sdim tls_beg, tls_size); 105276789Sdim } else { 106276789Sdim VPrintf(2, "__tls_get_addr: Can't guess glibc version\n"); 107276789Sdim // This may happen inside the DTOR of main thread, so just ignore it. 108276789Sdim tls_size = 0; 109276789Sdim } 110276789Sdim dtls.dtv[dso_id].beg = tls_beg; 111276789Sdim dtls.dtv[dso_id].size = tls_size; 112276789Sdim return dtls.dtv + dso_id; 113276789Sdim} 114276789Sdim 115276789Sdimvoid DTLS_on_libc_memalign(void *ptr, uptr size) { 116276789Sdim if (!common_flags()->intercept_tls_get_addr) return; 117276789Sdim VPrintf(2, "DTLS_on_libc_memalign: %p %p\n", ptr, size); 118276789Sdim dtls.last_memalign_ptr = reinterpret_cast<uptr>(ptr); 119276789Sdim dtls.last_memalign_size = size; 120276789Sdim} 121276789Sdim 122276789SdimDTLS *DTLS_Get() { return &dtls; } 123276789Sdim 124276789Sdim#else 125276789Sdimvoid DTLS_on_libc_memalign(void *ptr, uptr size) {} 126276789SdimDTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res) { return 0; } 127276789SdimDTLS *DTLS_Get() { return 0; } 128276789Sdimvoid DTLS_Destroy() {} 129276789Sdim#endif // SANITIZER_INTERCEPT_TLS_GET_ADDR 130276789Sdim 131276789Sdim} // namespace __sanitizer 132