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