1 //===-- sanitizer_tls_get_addr.cpp ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Handle the __tls_get_addr call.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_tls_get_addr.h"
15 #include "sanitizer_atomic.h"
16 #include "sanitizer_flags.h"
17 #include "sanitizer_platform_interceptors.h"
19 namespace __sanitizer
{
20 #if SANITIZER_INTERCEPT_TLS_GET_ADDR
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
{
29 // Glibc starting from 2.19 allocates tls using __signal_safe_memalign,
30 // which has such header.
31 struct Glibc_2_19_tls_header
{
36 // This must be static TLS
37 __attribute__((tls_model("initial-exec")))
38 static __thread DTLS dtls
;
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
;
44 static const uptr kDestroyedThread
= -1;
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
);
52 static DTLS::DTVBlock
*DTLS_NextBlock(atomic_uintptr_t
*cur
) {
53 uptr v
= atomic_load(cur
, memory_order_acquire
);
54 if (v
== kDestroyedThread
)
56 DTLS::DTVBlock
*next
= (DTLS::DTVBlock
*)v
;
59 DTLS::DTVBlock
*new_dtv
=
60 (DTLS::DTVBlock
*)MmapOrDie(sizeof(DTLS::DTVBlock
), "DTLS_NextBlock");
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
;
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
);
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
);
79 for (; id
>= kPerBlock
; id
-= kPerBlock
) cur
= DTLS_NextBlock(&cur
->next
);
80 return cur
->dtvs
+ id
;
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
);
89 DTLS::DTVBlock
*next
=
90 (DTLS::DTVBlock
*)atomic_load(&block
->next
, memory_order_acquire
);
91 DTLS_Deallocate(block
);
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;
107 static const uptr kDtvOffset
= 0;
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
)
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",
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
131 VReport(2, "__tls_get_addr: static tls: %p\n", tls_beg
);
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",
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.
146 dtv
->size
= tls_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
;
157 DTLS
*DTLS_Get() { return &dtls
; }
159 bool DTLSInDestruction(DTLS
*dtls
) {
160 return atomic_load(&dtls
->dtv_block
, memory_order_relaxed
) ==
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!");
174 #endif // SANITIZER_INTERCEPT_TLS_GET_ADDR
176 } // namespace __sanitizer