From patchwork Thu Apr 22 21:30:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 43102 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 357933896804; Thu, 22 Apr 2021 21:30:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 357933896804 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1619127019; bh=An450dewAHXDkC5/VDkz059hH6zKKqqHW5b945mz3As=; h=Subject:To:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VAwFWDmKQrShq1ks2W174VZrvVWvYRit4FkdgC52+jEplACqRtfo0/M3sG1JV6t9+ sGhEq/Jd9xqTiXAxOgFw0fCaKIcVb8Udt26J92PpJocbTWTi9YJpV8mmvqCIsihJSE 95S/OkZavN5EHF7Tr3v2hpvzMQB4COPWtI2FoRd0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by sourceware.org (Postfix) with ESMTPS id 0FC7D381DCC8 for ; Thu, 22 Apr 2021 21:30:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0FC7D381DCC8 Received: by mail-qt1-x82d.google.com with SMTP id o2so21600361qtr.4 for ; Thu, 22 Apr 2021 14:30:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-language; bh=An450dewAHXDkC5/VDkz059hH6zKKqqHW5b945mz3As=; b=CCPwUdlvmo0hdRvVoCaEJ75BsASALyd2q1aNGj5kYPf/CqRSy+SfssjRpp8/AluKws rdPdlkHXwJ25foYKzWQ7AZypayvvstJnusvZrZ+bhCZy4oFhqaslO6bx2x3nExEdUhy2 mW7d3npu5upOavYQlKfvrsFYUp7f+Uyf2pgyf/FfxyvVzNq0W7eWUe9YDnGeTqme05Bz gazhI4SUAjKy+3KGhIWLRskzWJIDL5xDe1lj1pk/lLxgioJMcnUxqjgP1iEdxR9xmLQd e7nPPfhxPO5ONtcSc8YPDDx/scTpIqq3FzNaPsz3DddHaNxD6mF7gbAnOgHzsKNB21fy lG6w== X-Gm-Message-State: AOAM530iSgFs6VI1hfIGaaMICUuynFf4+rPhxVOAK8oKnZpxgKj6Rye9 KHF1mIKfFesNkep99irt2f46VnNH74k= X-Google-Smtp-Source: ABdhPJySfAB3X0NTNzYqHu66cyaD/WEs8hiH3HDp7au0uq38Aj+4qKJGyROR/3FHh7r1RoidbjVWJw== X-Received: by 2002:ac8:5495:: with SMTP id h21mr551949qtq.266.1619127016471; Thu, 22 Apr 2021 14:30:16 -0700 (PDT) Received: from [192.168.0.41] (71-218-14-121.hlrn.qwest.net. [71.218.14.121]) by smtp.gmail.com with ESMTPSA id 132sm3060272qkn.52.2021.04.22.14.30.15 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 22 Apr 2021 14:30:16 -0700 (PDT) Subject: [PATCH] add attribute none to pthread_setspecific (BZ #27714) To: GNU C Library Message-ID: <2ec7fadb-cc15-a005-f708-d2adecc8cc39@gmail.com> Date: Thu, 22 Apr 2021 15:30:15 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Sebor via Libc-alpha From: Martin Sebor Reply-To: Martin Sebor Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" 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 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, [, ]) */ -#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 + . */ + +#include +#include + +/* 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); /* 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