unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Martin Sebor via Libc-alpha <libc-alpha@sourceware.org>
To: GNU C Library <libc-alpha@sourceware.org>
Subject: [RFC patch] avoid warning on accesses to hardwired address
Date: Thu, 8 Jul 2021 18:55:26 -0600	[thread overview]
Message-ID: <59b05c40-d0a8-233a-27c5-104b24bdc9b4@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1457 bytes --]

Thanks to recent code refactoring in GCC 12, -Warray-bounds has
started to diagnose accesses to constant addresses just like many
other flow based warnings do (e.g., -Wstringop-overflow).
The warnings are meant to help detect accesses resulting from
invalid arithmetic on null pointers.  There may be a better way
to detect them but GCC doesn't have the detection yet.  This
warning change has in turn exposed Glibc's uses of this trick
in the implementation of the THREAD_SELF macro.

I have tried a few approaches to avoid the warning but none worked
or seemed satisfactory:

1) Using #pragma GCC diagnostic doesn't work in macros.
2) Using _Pragma doesn't work there either due to a GCC limitation.
3) Declaring the pointer volatile works but prevents accesses to
    it from being eliminated when the result isn't used (something
    Glibc apparently cares about).
4) Defining a simple static inline function to wrap the code and
    the #pragmas doesn't work because the header is #included in
    files where struct pthread isn't defined.
5) Introducing a global variable with the address and initializing
    it in some .c file seems too heavy-weight.
6) Falling back on the pre-GCC 6 asm would be safer but seems like
    a step back.

Finally I have come up with the attached solution that combines (4)
and (1).  If (6) is preferable I'm happy to go that route.  If there
are other alternatives I'd be glad to consider them as well.

Thanks
Martin

[-- Attachment #2: glibc-thread_self.diff --]
[-- Type: text/x-patch, Size: 974 bytes --]

diff --git a/sysdeps/x86_64/nptl/tls.h b/sysdeps/x86_64/nptl/tls.h
index a78c4f4d01..a5bd245aa9 100644
--- a/sysdeps/x86_64/nptl/tls.h
+++ b/sysdeps/x86_64/nptl/tls.h
@@ -180,8 +180,20 @@ _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80,
    assignments like
 	pthread_descr self = thread_self();
    do not get optimized away.  */
-# if __GNUC_PREREQ (6, 0)
-#  define THREAD_SELF \
+
+# if __GNUC_PREREQ (12, 0)
+/* Hide access to a hardwided address to avoid GCC -Warray-bounds.  */
+static inline void *__thread_self (size_t __off)
+{
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Warray-bounds"
+  return *(void *__seg_fs *) __off;
+#pragma GCC diagnostic pop
+}
+#  define THREAD_SELF							\
+  ((struct pthread *)__thread_self (offsetof (struct pthread, header.self)))
+# elif __GNUC_PREREQ (6, 0)
+#  define THREAD_SELF							\
   (*(struct pthread *__seg_fs *) offsetof (struct pthread, header.self))
 # else
 #  define THREAD_SELF \

             reply	other threads:[~2021-07-09  0:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-09  0:55 Martin Sebor via Libc-alpha [this message]
2021-07-09  6:34 ` [RFC patch] avoid warning on accesses to hardwired address Florian Weimer via Libc-alpha
2021-07-21 13:06   ` Florian Weimer via Libc-alpha
2021-07-21 16:17     ` Martin Sebor via Libc-alpha
2021-08-16 11:27 ` Martin Liška
2021-08-16 15:24   ` Martin Sebor via Libc-alpha

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

  List information: https://www.gnu.org/software/libc/involved.html

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

  git send-email \
    --in-reply-to=59b05c40-d0a8-233a-27c5-104b24bdc9b4@gmail.com \
    --to=libc-alpha@sourceware.org \
    --cc=msebor@gmail.com \
    /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).