Blame SOURCES/gcc11-libsanitizer-pthread.patch

8af166
Backported from LLVM upstream:
8af166
8af166
commit ef14b78d9a144ba81ba02083fe21eb286a88732b
8af166
Author: Florian Weimer <fweimer@redhat.com>
8af166
Date:   Tue Feb 8 12:46:41 2022 -0800
8af166
8af166
    [sanitizer] Use _thread_db_sizeof_pthread to obtain struct pthread size
8af166
    
8af166
    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.
8af166
    
8af166
    Fixes #52989.
8af166
    
8af166
    Reviewed By: #sanitizers, MaskRay, vitalybuka
8af166
    
8af166
    Differential Revision: https://reviews.llvm.org/D119007
8af166
8af166
--- a/libsanitizer/sanitizer_common/sanitizer_linux_libcdep.cpp
8af166
+++ b/libsanitizer/sanitizer_common/sanitizer_linux_libcdep.cpp
8af166
@@ -265,10 +265,8 @@ void InitTlsSize() { }
8af166
 // sizeof(struct pthread) from glibc.
8af166
 static atomic_uintptr_t thread_descriptor_size;
8af166
 
8af166
-uptr ThreadDescriptorSize() {
8af166
-  uptr val = atomic_load_relaxed(&thread_descriptor_size);
8af166
-  if (val)
8af166
-    return val;
8af166
+static uptr ThreadDescriptorSizeFallback() {
8af166
+  uptr val = 0;
8af166
 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
8af166
   int major;
8af166
   int minor;
8af166
@@ -323,8 +321,21 @@ uptr ThreadDescriptorSize() {
8af166
 #elif defined(__s390__)
8af166
   val = FIRST_32_SECOND_64(1152, 1776); // valid for glibc 2.22
8af166
 #endif
8af166
+  return val;
8af166
+}
8af166
+
8af166
+uptr ThreadDescriptorSize() {
8af166
+  uptr val = atomic_load_relaxed(&thread_descriptor_size);
8af166
   if (val)
8af166
-    atomic_store_relaxed(&thread_descriptor_size, val);
8af166
+    return val;
8af166
+  // _thread_db_sizeof_pthread is a GLIBC_PRIVATE symbol that is exported in
8af166
+  // glibc 2.34 and later.
8af166
+  if (unsigned *psizeof = static_cast<unsigned *>(
8af166
+          dlsym(RTLD_DEFAULT, "_thread_db_sizeof_pthread")))
8af166
+    val = *psizeof;
8af166
+  if (!val)
8af166
+    val = ThreadDescriptorSizeFallback();
8af166
+  atomic_store_relaxed(&thread_descriptor_size, val);
8af166
   return val;
8af166
 }
8af166