public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: GNU C Library <libc-alpha@sourceware.org>
Subject: [PATCH] add attribute none to pthread_setspecific (BZ #27714)
Date: Thu, 22 Apr 2021 15:30:15 -0600	[thread overview]
Message-ID: <2ec7fadb-cc15-a005-f708-d2adecc8cc39@gmail.com> (raw)

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

             reply	other threads:[~2021-04-22 21:30 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-22 21:30 Martin Sebor [this message]
2021-04-22 22:26 ` Martin Sebor
2021-04-23  0:11 ` Paul Eggert
2021-04-23 15:24   ` Martin Sebor
2021-04-23 20:19     ` Paul Eggert
2021-04-23 21:29       ` Martin Sebor
2021-04-24  0:27         ` Paul Eggert
2021-04-26 19:38           ` Martin Sebor
2021-04-27  4:41 ` Florian Weimer
2021-04-27 19:07   ` Martin Sebor
2021-04-27 21:07     ` Joseph Myers
2021-04-27 21:46       ` Martin Sebor
2021-04-27 21:58         ` Joseph Myers
2021-04-27 22:57           ` Martin Sebor
2021-04-28  1:09             ` Martin Sebor
2021-04-28  7:32               ` Florian Weimer
2021-04-28 14:49                 ` Martin Sebor
2021-04-29  7:45                   ` Florian Weimer
2021-04-29 14:55                     ` Martin Sebor
2021-04-29 16:16                       ` Florian Weimer
2021-04-28  1:30             ` H.J. Lu

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=2ec7fadb-cc15-a005-f708-d2adecc8cc39@gmail.com \
    --to=msebor@gmail.com \
    --cc=libc-alpha@sourceware.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).