public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r10-10682] sanitizer: Use glibc _thread_db_sizeof_pthread symbol if present
Date: Tue, 10 May 2022 08:24:07 +0000 (GMT)	[thread overview]
Message-ID: <20220510082407.7C0EF38346B0@sourceware.org> (raw)

https://gcc.gnu.org/g:fefb97a53606fad6060e217b9b642dbbd3a1e299

commit r10-10682-gfefb97a53606fad6060e217b9b642dbbd3a1e299
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Feb 15 11:18:56 2022 +0100

    sanitizer: Use glibc _thread_db_sizeof_pthread symbol if present
    
    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.
    
    2022-02-15  Jakub Jelinek  <jakub@redhat.com>
    
            * sanitizer_common/sanitizer_linux_libcdep.cpp: Cherry-pick
            llvm-project revision ef14b78d9a144ba81ba02083fe21eb286a88732b.
    
    (cherry picked from commit c4c0aa60891daeb4ea5a7c265bd681038f6d8271)

Diff:
---
 .../sanitizer_common/sanitizer_linux_libcdep.cpp    | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/libsanitizer/sanitizer_common/sanitizer_linux_libcdep.cpp b/libsanitizer/sanitizer_common/sanitizer_linux_libcdep.cpp
index e09d568d802..7f7191b9c22 100644
--- a/libsanitizer/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/libsanitizer/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -267,10 +267,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;
@@ -310,8 +308,21 @@ uptr ThreadDescriptorSize() {
 #elif defined(__s390__)
   val = FIRST_32_SECOND_64(1152, 1776); // valid for glibc 2.22
 #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;
 }


                 reply	other threads:[~2022-05-10  8:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220510082407.7C0EF38346B0@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).