unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] add attribute none to pthread_setspecific (BZ #27714)
@ 2021-04-22 21:30 Martin Sebor via Libc-alpha
  2021-04-22 22:26 ` Martin Sebor via Libc-alpha
                   ` (2 more replies)
  0 siblings, 3 replies; 21+ messages in thread
From: Martin Sebor via Libc-alpha @ 2021-04-22 21:30 UTC (permalink / raw)
  To: GNU C Library

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

GCC 11 warns when a pointer to an uninitialized object is passed
to a function that takes a const-qualified argument.  This is done
on the assumption that most such functions read from the object.
For the rare case of a function that doesn't, GCC 11 extended
attribute access to add a new mode called none.

POSIX pthread_setspecific() is one such rare function that takes
a const void* argument but that doesn't read from the object it
points to.  To suppress the -Wmaybe-uninitialized issued by GCC
11 when the address of an uninitialized object is passed to it
(e.g., the result of malloc()), the attached patch #defines
__attr_access_none in cdefs.h and uses the macro on the function
in sysdeps/htl/pthread.h and sysdeps/nptl/pthread.h.

Martin

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

diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index 8e244a77cf..ac56be4d87 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -592,8 +592,14 @@ _Static_assert (0, "IEEE 128-bits long double requires redirection on this platf
    array according to access mode, or at least one element when
    size-index is not provided:
      access (access-mode, <ref-index> [, <size-index>])  */
-#define __attr_access(x) __attribute__ ((__access__ x))
+#  define __attr_access(x) __attribute__ ((__access__ x))
+#  if __GNUC_PREREQ (11, 0)
+#    define __attr_access_none(pos) __attribute__ ((__access__ (__none__, pos)))
+#  endif
 #else
 #  define __attr_access(x)
+#  define __attr_access_none(pos)
+#endif
+
 
 /* Specify that a function such as setjmp or vfork may return
diff --git a/nptl/tst-thread-setspecific.c b/nptl/tst-thread-setspecific.c
new file mode 100644
index 0000000000..bda61c6333
--- /dev/null
+++ b/nptl/tst-thread-setspecific.c
@@ -0,0 +1,43 @@
+/* Test to verify that passing a pointer to an uninitialized object
+   to pthread_setspecific doesn't trigger bogus uninitialized warnings.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <pthread.h>
+#include <stdlib.h>
+
+/* Turn uninitialized warnings into errors to detect the problem.
+   See BZ #27714.  */
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic error "-Wmaybe-uninitialized"
+#pragma GCC diagnostic error "-Wuninitialized"
+
+int do_test (void)
+{
+  void *p = malloc (1);   /* Deliberately uninitialized.  */
+  pthread_setspecific (pthread_self (), p);
+
+  void *q = pthread_getspecific (pthread_self ());
+
+  return p == q;
+}
+
+#pragma GCC diagnostic pop
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/sysdeps/htl/pthread.h b/sysdeps/htl/pthread.h
index 0923ad0002..6bcf97d692 100644
--- a/sysdeps/htl/pthread.h
+++ b/sysdeps/htl/pthread.h
@@ -822,7 +822,7 @@ extern void *pthread_getspecific (pthread_key_t __key) __THROW;
 
 /* Set the caller thread's thread specific value of KEY to VALUE.  */
 extern int pthread_setspecific (pthread_key_t __key, const void *__value)
-	__THROW;
+	__THROW __attr_access_none (2);
 \f
 
 /* Dynamic package initialization.  */
diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h
index 23bcd51d91..7c14d0fef7 100644
--- a/sysdeps/nptl/pthread.h
+++ b/sysdeps/nptl/pthread.h
@@ -1171,7 +1171,8 @@ extern void *pthread_getspecific (pthread_key_t __key) __THROW;
 
 /* Store POINTER in the thread-specific data slot identified by KEY. */
 extern int pthread_setspecific (pthread_key_t __key,
-				const void *__pointer) __THROW ;
+				const void *__pointer)
+  __THROW __attr_access_none (2);
 
 
 #ifdef __USE_XOPEN2K

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

end of thread, other threads:[~2021-04-29 16:16 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-22 21:30 [PATCH] add attribute none to pthread_setspecific (BZ #27714) Martin Sebor via Libc-alpha
2021-04-22 22:26 ` Martin Sebor via Libc-alpha
2021-04-23  0:11 ` Paul Eggert
2021-04-23 15:24   ` Martin Sebor via Libc-alpha
2021-04-23 20:19     ` Paul Eggert
2021-04-23 21:29       ` Martin Sebor via Libc-alpha
2021-04-24  0:27         ` Paul Eggert
2021-04-26 19:38           ` Martin Sebor via Libc-alpha
2021-04-27  4:41 ` Florian Weimer via Libc-alpha
2021-04-27 19:07   ` Martin Sebor via Libc-alpha
2021-04-27 21:07     ` Joseph Myers
2021-04-27 21:46       ` Martin Sebor via Libc-alpha
2021-04-27 21:58         ` Joseph Myers
2021-04-27 22:57           ` Martin Sebor via Libc-alpha
2021-04-28  1:09             ` Martin Sebor via Libc-alpha
2021-04-28  7:32               ` Florian Weimer via Libc-alpha
2021-04-28 14:49                 ` Martin Sebor via Libc-alpha
2021-04-29  7:45                   ` Florian Weimer via Libc-alpha
2021-04-29 14:55                     ` Martin Sebor via Libc-alpha
2021-04-29 16:16                       ` Florian Weimer via Libc-alpha
2021-04-28  1:30             ` H.J. Lu via Libc-alpha

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