sanitizer_tls_get_addr.cc revision 296417
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 81296417Sdim#if defined(__powerpc64__) 82296417Sdim// This is glibc's TLS_DTV_OFFSET: 83296417Sdim// "Dynamic thread vector pointers point 0x8000 past the start of each 84296417Sdim// TLS block." 85296417Sdimstatic const uptr kDtvOffset = 0x8000; 86296417Sdim#else 87296417Sdimstatic const uptr kDtvOffset = 0; 88296417Sdim#endif 89296417Sdim 90288943SdimDTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res, 91288943Sdim uptr static_tls_begin, uptr static_tls_end) { 92276789Sdim if (!common_flags()->intercept_tls_get_addr) return 0; 93276789Sdim TlsGetAddrParam *arg = reinterpret_cast<TlsGetAddrParam *>(arg_void); 94276789Sdim uptr dso_id = arg->dso_id; 95276789Sdim if (dtls.dtv_size == kDestroyedThread) return 0; 96276789Sdim DTLS_Resize(dso_id + 1); 97276789Sdim if (dtls.dtv[dso_id].beg) return 0; 98276789Sdim uptr tls_size = 0; 99296417Sdim uptr tls_beg = reinterpret_cast<uptr>(res) - arg->offset - kDtvOffset; 100276789Sdim VPrintf(2, "__tls_get_addr: %p {%p,%p} => %p; tls_beg: %p; sp: %p " 101276789Sdim "num_live_dtls %zd\n", 102276789Sdim arg, arg->dso_id, arg->offset, res, tls_beg, &tls_beg, 103276789Sdim atomic_load(&number_of_live_dtls, memory_order_relaxed)); 104276789Sdim if (dtls.last_memalign_ptr == tls_beg) { 105276789Sdim tls_size = dtls.last_memalign_size; 106276789Sdim VPrintf(2, "__tls_get_addr: glibc <=2.18 suspected; tls={%p,%p}\n", 107276789Sdim tls_beg, tls_size); 108288943Sdim } else if (tls_beg >= static_tls_begin && tls_beg < static_tls_end) { 109288943Sdim // This is the static TLS block which was initialized / unpoisoned at thread 110288943Sdim // creation. 111288943Sdim VPrintf(2, "__tls_get_addr: static tls: %p\n", tls_beg); 112288943Sdim tls_size = 0; 113276789Sdim } else if ((tls_beg % 4096) == sizeof(Glibc_2_19_tls_header)) { 114276789Sdim // We may want to check gnu_get_libc_version(). 115276789Sdim Glibc_2_19_tls_header *header = (Glibc_2_19_tls_header *)tls_beg - 1; 116276789Sdim tls_size = header->size; 117276789Sdim tls_beg = header->start; 118276789Sdim VPrintf(2, "__tls_get_addr: glibc >=2.19 suspected; tls={%p %p}\n", 119276789Sdim tls_beg, tls_size); 120276789Sdim } else { 121276789Sdim VPrintf(2, "__tls_get_addr: Can't guess glibc version\n"); 122276789Sdim // This may happen inside the DTOR of main thread, so just ignore it. 123276789Sdim tls_size = 0; 124276789Sdim } 125276789Sdim dtls.dtv[dso_id].beg = tls_beg; 126276789Sdim dtls.dtv[dso_id].size = tls_size; 127276789Sdim return dtls.dtv + dso_id; 128276789Sdim} 129276789Sdim 130276789Sdimvoid DTLS_on_libc_memalign(void *ptr, uptr size) { 131276789Sdim if (!common_flags()->intercept_tls_get_addr) return; 132276789Sdim VPrintf(2, "DTLS_on_libc_memalign: %p %p\n", ptr, size); 133276789Sdim dtls.last_memalign_ptr = reinterpret_cast<uptr>(ptr); 134276789Sdim dtls.last_memalign_size = size; 135276789Sdim} 136276789Sdim 137276789SdimDTLS *DTLS_Get() { return &dtls; } 138276789Sdim 139276789Sdim#else 140276789Sdimvoid DTLS_on_libc_memalign(void *ptr, uptr size) {} 141276789SdimDTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res) { return 0; } 142276789SdimDTLS *DTLS_Get() { return 0; } 143276789Sdimvoid DTLS_Destroy() {} 144276789Sdim#endif // SANITIZER_INTERCEPT_TLS_GET_ADDR 145276789Sdim 146276789Sdim} // namespace __sanitizer 147