Skip to content

Commit ef14b78

Browse files
fweimer-rhMaskRay
authored andcommitted
[sanitizer] Use _thread_db_sizeof_pthread to obtain struct pthread size
This symbol has been exported (as an internal GLIBC_PRIVATE symbol) from libc.so.6 starting with glibc 2.34. glibc uses it internally for its libthread_db implementation to enable thread debugging on GDB, so it is unlikely to go away for now. Fixes llvm#52989. Reviewed By: #sanitizers, MaskRay, vitalybuka Differential Revision: https://reviews.llvm.org/D119007
1 parent 7e7ecef commit ef14b78

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,8 @@ void InitTlsSize() { }
220220
// sizeof(struct pthread) from glibc.
221221
static atomic_uintptr_t thread_descriptor_size;
222222

223-
uptr ThreadDescriptorSize() {
224-
uptr val = atomic_load_relaxed(&thread_descriptor_size);
225-
if (val)
226-
return val;
223+
static uptr ThreadDescriptorSizeFallback() {
224+
uptr val = 0;
227225
#if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
228226
int major;
229227
int minor;
@@ -285,8 +283,21 @@ uptr ThreadDescriptorSize() {
285283
#elif defined(__powerpc64__)
286284
val = 1776; // from glibc.ppc64le 2.20-8.fc21
287285
#endif
286+
return val;
287+
}
288+
289+
uptr ThreadDescriptorSize() {
290+
uptr val = atomic_load_relaxed(&thread_descriptor_size);
288291
if (val)
289-
atomic_store_relaxed(&thread_descriptor_size, val);
292+
return val;
293+
// _thread_db_sizeof_pthread is a GLIBC_PRIVATE symbol that is exported in
294+
// glibc 2.34 and later.
295+
if (unsigned *psizeof = static_cast<unsigned *>(
296+
dlsym(RTLD_DEFAULT, "_thread_db_sizeof_pthread")))
297+
val = *psizeof;
298+
if (!val)
299+
val = ThreadDescriptorSizeFallback();
300+
atomic_store_relaxed(&thread_descriptor_size, val);
290301
return val;
291302
}
292303

0 commit comments

Comments
 (0)