llvm-project
179 строк · 6.1 Кб
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
13#include "sanitizer_tls_get_addr.h"14
15#include "sanitizer_allocator_interface.h"16#include "sanitizer_atomic.h"17#include "sanitizer_flags.h"18#include "sanitizer_platform_interceptors.h"19
20namespace __sanitizer {21#if SANITIZER_INTERCEPT_TLS_GET_ADDR22
23// The actual parameter that comes to __tls_get_addr
24// is a pointer to a struct with two words in it:
25struct TlsGetAddrParam {26uptr dso_id;27uptr offset;28};29
30// This must be static TLS
31__attribute__((tls_model("initial-exec")))32static __thread DTLS dtls;33
34// Make sure we properly destroy the DTLS objects:
35// this counter should never get too large.
36static atomic_uintptr_t number_of_live_dtls;37
38static const uptr kDestroyedThread = -1;39
40static void DTLS_Deallocate(DTLS::DTVBlock *block) {41VReport(2, "__tls_get_addr: DTLS_Deallocate %p\n", (void *)block);42UnmapOrDie(block, sizeof(DTLS::DTVBlock));43atomic_fetch_sub(&number_of_live_dtls, 1, memory_order_relaxed);44}
45
46static DTLS::DTVBlock *DTLS_NextBlock(atomic_uintptr_t *cur) {47uptr v = atomic_load(cur, memory_order_acquire);48if (v == kDestroyedThread)49return nullptr;50DTLS::DTVBlock *next = (DTLS::DTVBlock *)v;51if (next)52return next;53DTLS::DTVBlock *new_dtv =54(DTLS::DTVBlock *)MmapOrDie(sizeof(DTLS::DTVBlock), "DTLS_NextBlock");55uptr prev = 0;56if (!atomic_compare_exchange_strong(cur, &prev, (uptr)new_dtv,57memory_order_seq_cst)) {58UnmapOrDie(new_dtv, sizeof(DTLS::DTVBlock));59return (DTLS::DTVBlock *)prev;60}61uptr num_live_dtls =62atomic_fetch_add(&number_of_live_dtls, 1, memory_order_relaxed);63VReport(2, "__tls_get_addr: DTLS_NextBlock %p %zd\n", (void *)&dtls,64num_live_dtls);65return new_dtv;66}
67
68static DTLS::DTV *DTLS_Find(uptr id) {69VReport(2, "__tls_get_addr: DTLS_Find %p %zd\n", (void *)&dtls, id);70static constexpr uptr kPerBlock = ARRAY_SIZE(DTLS::DTVBlock::dtvs);71DTLS::DTVBlock *cur = DTLS_NextBlock(&dtls.dtv_block);72if (!cur)73return nullptr;74for (; id >= kPerBlock; id -= kPerBlock) cur = DTLS_NextBlock(&cur->next);75return cur->dtvs + id;76}
77
78void DTLS_Destroy() {79if (!common_flags()->intercept_tls_get_addr) return;80VReport(2, "__tls_get_addr: DTLS_Destroy %p\n", (void *)&dtls);81DTLS::DTVBlock *block = (DTLS::DTVBlock *)atomic_exchange(82&dtls.dtv_block, kDestroyedThread, memory_order_release);83while (block) {84DTLS::DTVBlock *next =85(DTLS::DTVBlock *)atomic_load(&block->next, memory_order_acquire);86DTLS_Deallocate(block);87block = next;88}89}
90
91#if defined(__powerpc64__) || defined(__mips__)92// This is glibc's TLS_DTV_OFFSET:
93// "Dynamic thread vector pointers point 0x8000 past the start of each
94// TLS block." (sysdeps/<arch>/dl-tls.h)
95static const uptr kDtvOffset = 0x8000;96#elif defined(__riscv)97// This is glibc's TLS_DTV_OFFSET:
98// "Dynamic thread vector pointers point 0x800 past the start of each
99// TLS block." (sysdeps/riscv/dl-tls.h)
100static const uptr kDtvOffset = 0x800;101#else102static const uptr kDtvOffset = 0;103#endif104
105extern "C" {106SANITIZER_WEAK_ATTRIBUTE
107uptr __sanitizer_get_allocated_size(const void *p);108
109SANITIZER_WEAK_ATTRIBUTE
110const void *__sanitizer_get_allocated_begin(const void *p);111}
112
113DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,114uptr static_tls_begin, uptr static_tls_end) {115if (!common_flags()->intercept_tls_get_addr) return 0;116TlsGetAddrParam *arg = reinterpret_cast<TlsGetAddrParam *>(arg_void);117uptr dso_id = arg->dso_id;118DTLS::DTV *dtv = DTLS_Find(dso_id);119if (!dtv || dtv->beg)120return 0;121uptr tls_size = 0;122uptr tls_beg = reinterpret_cast<uptr>(res) - arg->offset - kDtvOffset;123VReport(2,124"__tls_get_addr: %p {0x%zx,0x%zx} => %p; tls_beg: 0x%zx; sp: %p "125"num_live_dtls %zd\n",126(void *)arg, arg->dso_id, arg->offset, res, tls_beg, (void *)&tls_beg,127atomic_load(&number_of_live_dtls, memory_order_relaxed));128if (dtls.last_memalign_ptr == tls_beg) {129tls_size = dtls.last_memalign_size;130VReport(2, "__tls_get_addr: glibc <=2.24 suspected; tls={0x%zx,0x%zx}\n",131tls_beg, tls_size);132} else if (tls_beg >= static_tls_begin && tls_beg < static_tls_end) {133// This is the static TLS block which was initialized / unpoisoned at thread134// creation.135VReport(2, "__tls_get_addr: static tls: 0x%zx\n", tls_beg);136tls_size = 0;137} else if (const void *start =138__sanitizer_get_allocated_begin((void *)tls_beg)) {139tls_beg = (uptr)start;140tls_size = __sanitizer_get_allocated_size(start);141VReport(2, "__tls_get_addr: glibc >=2.25 suspected; tls={0x%zx,0x%zx}\n",142tls_beg, tls_size);143} else {144VReport(2, "__tls_get_addr: Can't guess glibc version\n");145// This may happen inside the DTOR of main thread, so just ignore it.146tls_size = 0;147}148dtv->beg = tls_beg;149dtv->size = tls_size;150return dtv;151}
152
153void DTLS_on_libc_memalign(void *ptr, uptr size) {154if (!common_flags()->intercept_tls_get_addr) return;155VReport(2, "DTLS_on_libc_memalign: %p 0x%zx\n", ptr, size);156dtls.last_memalign_ptr = reinterpret_cast<uptr>(ptr);157dtls.last_memalign_size = size;158}
159
160DTLS *DTLS_Get() { return &dtls; }161
162bool DTLSInDestruction(DTLS *dtls) {163return atomic_load(&dtls->dtv_block, memory_order_relaxed) ==164kDestroyedThread;165}
166
167#else168void DTLS_on_libc_memalign(void *ptr, uptr size) {}169DTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res,170unsigned long, unsigned long) { return 0; }171DTLS *DTLS_Get() { return 0; }172void DTLS_Destroy() {}173bool DTLSInDestruction(DTLS *dtls) {174UNREACHABLE("dtls is unsupported on this platform!");175}
176
177#endif // SANITIZER_INTERCEPT_TLS_GET_ADDR178
179} // namespace __sanitizer180