sanitizer_tls_get_addr.cc revision 288943
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 81288943SdimDTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res, 82288943Sdim uptr static_tls_begin, uptr static_tls_end) { 83276789Sdim if (!common_flags()->intercept_tls_get_addr) return 0; 84276789Sdim TlsGetAddrParam *arg = reinterpret_cast<TlsGetAddrParam *>(arg_void); 85276789Sdim uptr dso_id = arg->dso_id; 86276789Sdim if (dtls.dtv_size == kDestroyedThread) return 0; 87276789Sdim DTLS_Resize(dso_id + 1); 88276789Sdim if (dtls.dtv[dso_id].beg) return 0; 89276789Sdim uptr tls_size = 0; 90276789Sdim uptr tls_beg = reinterpret_cast<uptr>(res) - arg->offset; 91276789Sdim VPrintf(2, "__tls_get_addr: %p {%p,%p} => %p; tls_beg: %p; sp: %p " 92276789Sdim "num_live_dtls %zd\n", 93276789Sdim arg, arg->dso_id, arg->offset, res, tls_beg, &tls_beg, 94276789Sdim atomic_load(&number_of_live_dtls, memory_order_relaxed)); 95276789Sdim if (dtls.last_memalign_ptr == tls_beg) { 96276789Sdim tls_size = dtls.last_memalign_size; 97276789Sdim VPrintf(2, "__tls_get_addr: glibc <=2.18 suspected; tls={%p,%p}\n", 98276789Sdim tls_beg, tls_size); 99288943Sdim } else if (tls_beg >= static_tls_begin && tls_beg < static_tls_end) { 100288943Sdim // This is the static TLS block which was initialized / unpoisoned at thread 101288943Sdim // creation. 102288943Sdim VPrintf(2, "__tls_get_addr: static tls: %p\n", tls_beg); 103288943Sdim tls_size = 0; 104276789Sdim } else if ((tls_beg % 4096) == sizeof(Glibc_2_19_tls_header)) { 105276789Sdim // We may want to check gnu_get_libc_version(). 106276789Sdim Glibc_2_19_tls_header *header = (Glibc_2_19_tls_header *)tls_beg - 1; 107276789Sdim tls_size = header->size; 108276789Sdim tls_beg = header->start; 109276789Sdim VPrintf(2, "__tls_get_addr: glibc >=2.19 suspected; tls={%p %p}\n", 110276789Sdim tls_beg, tls_size); 111276789Sdim } else { 112276789Sdim VPrintf(2, "__tls_get_addr: Can't guess glibc version\n"); 113276789Sdim // This may happen inside the DTOR of main thread, so just ignore it. 114276789Sdim tls_size = 0; 115276789Sdim } 116276789Sdim dtls.dtv[dso_id].beg = tls_beg; 117276789Sdim dtls.dtv[dso_id].size = tls_size; 118276789Sdim return dtls.dtv + dso_id; 119276789Sdim} 120276789Sdim 121276789Sdimvoid DTLS_on_libc_memalign(void *ptr, uptr size) { 122276789Sdim if (!common_flags()->intercept_tls_get_addr) return; 123276789Sdim VPrintf(2, "DTLS_on_libc_memalign: %p %p\n", ptr, size); 124276789Sdim dtls.last_memalign_ptr = reinterpret_cast<uptr>(ptr); 125276789Sdim dtls.last_memalign_size = size; 126276789Sdim} 127276789Sdim 128276789SdimDTLS *DTLS_Get() { return &dtls; } 129276789Sdim 130276789Sdim#else 131276789Sdimvoid DTLS_on_libc_memalign(void *ptr, uptr size) {} 132276789SdimDTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res) { return 0; } 133276789SdimDTLS *DTLS_Get() { return 0; } 134276789Sdimvoid DTLS_Destroy() {} 135276789Sdim#endif // SANITIZER_INTERCEPT_TLS_GET_ADDR 136276789Sdim 137276789Sdim} // namespace __sanitizer 138