[compiler-rt] ef14b78 - [sanitizer] Use _thread_db_sizeof_pthread to obtain struct pthread size

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 8 12:46:45 PST 2022


Author: Florian Weimer
Date: 2022-02-08T12:46:41-08:00
New Revision: ef14b78d9a144ba81ba02083fe21eb286a88732b

URL: https://github.com/llvm/llvm-project/commit/ef14b78d9a144ba81ba02083fe21eb286a88732b
DIFF: https://github.com/llvm/llvm-project/commit/ef14b78d9a144ba81ba02083fe21eb286a88732b.diff

LOG: [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 #52989.

Reviewed By: #sanitizers, MaskRay, vitalybuka

Differential Revision: https://reviews.llvm.org/D119007

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index b025a5e4fb644..eeb157d1c4280 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -220,10 +220,8 @@ void InitTlsSize() { }
 // sizeof(struct pthread) from glibc.
 static atomic_uintptr_t thread_descriptor_size;
 
-uptr ThreadDescriptorSize() {
-  uptr val = atomic_load_relaxed(&thread_descriptor_size);
-  if (val)
-    return val;
+static uptr ThreadDescriptorSizeFallback() {
+  uptr val = 0;
 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
   int major;
   int minor;
@@ -285,8 +283,21 @@ uptr ThreadDescriptorSize() {
 #elif defined(__powerpc64__)
   val = 1776; // from glibc.ppc64le 2.20-8.fc21
 #endif
+  return val;
+}
+
+uptr ThreadDescriptorSize() {
+  uptr val = atomic_load_relaxed(&thread_descriptor_size);
   if (val)
-    atomic_store_relaxed(&thread_descriptor_size, val);
+    return val;
+  // _thread_db_sizeof_pthread is a GLIBC_PRIVATE symbol that is exported in
+  // glibc 2.34 and later.
+  if (unsigned *psizeof = static_cast<unsigned *>(
+          dlsym(RTLD_DEFAULT, "_thread_db_sizeof_pthread")))
+    val = *psizeof;
+  if (!val)
+    val = ThreadDescriptorSizeFallback();
+  atomic_store_relaxed(&thread_descriptor_size, val);
   return val;
 }
 


        


More information about the llvm-commits mailing list