diff mbox series

[committed] sanitizer: Use glibc _thread_db_sizeof_pthread symbol if present

Message ID 20220215102309.GN2646553@tucnak
State New
Headers show
Series [committed] sanitizer: Use glibc _thread_db_sizeof_pthread symbol if present | expand

Commit Message

Jakub Jelinek Feb. 15, 2022, 10:23 a.m. UTC
Hi!

I've cherry-picked following fix from llvm-project.  Recent glibcs
have _thread_db_sizeof_pthread symbol variable which contains the
size of struct pthread, so that sanitizers don't need to guess that
and risk that it will change again.

The patch is from Florian Weimer.

Bootstrapped/regtested on x86_64-linux and i686-linux, the former
both with old glibc that doesn't have the new symbol and a new one.

Committed to trunk.

2022-02-15  Jakub Jelinek  <jakub@redhat.com>

	* sanitizer_common/sanitizer_linux_libcdep.cpp: Cherry-pick
	llvm-project revision ef14b78d9a144ba81ba02083fe21eb286a88732b.



	Jakub
diff mbox series

Patch

--- libsanitizer/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ libsanitizer/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;
 }