public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [RFC patch] avoid warning on accesses to hardwired address
@ 2021-07-09  0:55 Martin Sebor
  2021-07-09  6:34 ` Florian Weimer
  2021-08-16 11:27 ` Martin Liška
  0 siblings, 2 replies; 6+ messages in thread
From: Martin Sebor @ 2021-07-09  0:55 UTC (permalink / raw)
  To: GNU C Library

[-- 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 \

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-16 15:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-09  0:55 [RFC patch] avoid warning on accesses to hardwired address Martin Sebor
2021-07-09  6:34 ` Florian Weimer
2021-07-21 13:06   ` Florian Weimer
2021-07-21 16:17     ` Martin Sebor
2021-08-16 11:27 ` Martin Liška
2021-08-16 15:24   ` Martin Sebor

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).