Skip to content

Commit dd66aaf

Browse files
authored
[sanitizer] Allow to override GetDTLSRange (llvm#108348)
And rename it into __sanitizer_get_dtls_size. The test will be in a separate patch, as I expected reverts of the test.
1 parent 82a3646 commit dd66aaf

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interface.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ INTERFACE_FUNCTION(__sanitizer_verify_double_ended_contiguous_container)
2222
INTERFACE_WEAK_FUNCTION(__sanitizer_on_print)
2323
INTERFACE_WEAK_FUNCTION(__sanitizer_report_error_summary)
2424
INTERFACE_WEAK_FUNCTION(__sanitizer_sandbox_on_notify)
25+
INTERFACE_WEAK_FUNCTION(__sanitizer_get_dtls_size)
2526
// Sanitizer weak hooks
2627
INTERFACE_WEAK_FUNCTION(__sanitizer_weak_hook_memcmp)
2728
INTERFACE_WEAK_FUNCTION(__sanitizer_weak_hook_strcmp)

compiler-rt/lib/sanitizer_common/sanitizer_interface_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args);
4949
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void
5050
__sanitizer_report_error_summary(const char *error_summary);
5151

52+
// Returns size of dynamically allocated block. This function can be overridden
53+
// by the client.
54+
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE __sanitizer::uptr
55+
__sanitizer_get_dtls_size(const void *tls_begin);
56+
5257
SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump();
5358
SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage(
5459
const __sanitizer::uptr *pcs, const __sanitizer::uptr len);

compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,15 @@ SANITIZER_WEAK_ATTRIBUTE
110110
const void *__sanitizer_get_allocated_begin(const void *p);
111111
}
112112

113-
static uptr GetDTLSRange(uptr tls_beg) {
114-
const void *start = __sanitizer_get_allocated_begin((void *)tls_beg);
113+
SANITIZER_INTERFACE_WEAK_DEF(uptr, __sanitizer_get_dtls_size,
114+
const void *tls_begin) {
115+
const void *start = __sanitizer_get_allocated_begin(tls_begin);
115116
if (!start)
116117
return 0;
117-
CHECK_EQ(start, (void *)tls_beg);
118+
CHECK_EQ(start, tls_begin);
118119
uptr tls_size = __sanitizer_get_allocated_size(start);
119120
VReport(2, "__tls_get_addr: glibc DTLS suspected; tls={%p,0x%zx}\n",
120-
(void *)tls_beg, tls_size);
121+
tls_begin, tls_size);
121122
return tls_size;
122123
}
123124

@@ -143,8 +144,8 @@ DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
143144
VReport(2, "__tls_get_addr: static tls: %p\n", (void *)tls_beg);
144145
tls_size = 0;
145146
} else {
146-
tls_size = GetDTLSRange(tls_beg);
147-
if (tls_size) {
147+
tls_size = __sanitizer_get_dtls_size(reinterpret_cast<void *>(tls_beg));
148+
if (!tls_size) {
148149
VReport(2, "__tls_get_addr: Can't guess glibc version\n");
149150
// This may happen inside the DTOR of main thread, so just ignore it.
150151
}
@@ -162,6 +163,9 @@ bool DTLSInDestruction(DTLS *dtls) {
162163
}
163164

164165
#else
166+
SANITIZER_INTERFACE_WEAK_DEF(uptr, __sanitizer_get_dtls_size, const void *) {
167+
return 0;
168+
}
165169
DTLS::DTV *DTLS_on_tls_get_addr(void *arg, void *res,
166170
unsigned long, unsigned long) { return 0; }
167171
DTLS *DTLS_Get() { return 0; }

compiler-rt/lib/sanitizer_common/weak_symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
___sanitizer_free_hook
2+
___sanitizer_get_dtls_size
23
___sanitizer_malloc_hook
34
___sanitizer_report_error_summary
45
___sanitizer_sandbox_on_notify

0 commit comments

Comments
 (0)