From patchwork Wed Feb 15 13:46:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 57554 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp199150wrn; Wed, 15 Feb 2023 05:47:40 -0800 (PST) X-Google-Smtp-Source: AK7set+at4zQA7wFtVjfLIDQkaHcQ9mwxsohgKpGvwa4thft1eJUc17W++4K8Nu7oAGQ3pGxAYr0 X-Received: by 2002:a17:907:2145:b0:885:5e22:c038 with SMTP id rk5-20020a170907214500b008855e22c038mr2447086ejb.33.1676468860003; Wed, 15 Feb 2023 05:47:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676468859; cv=none; d=google.com; s=arc-20160816; b=Txz9knhlJMCSYDNP9dKn5C+VStkqUPUn8VZATwlzFKjr9X3q1gdbidIvDB13mzqblS Aql7h/Bbdb9ng+iUS+U9PVRWETo7xSkKk9W7xE/eKai2c4TBMga2oKNKs2zfV2YIMFPH Rbgo0Tyt66+N5JV7tNQykpWobC61ghFAajuSvlUgu+zOzJ/sT3mJzbopPcqRE1SWy81n 5r2gNvB8UnjJKIFG3WnpgSfuSpFaWR7I6xyt1jOeKfpK88MQB3gb+c/T7FOFBs7bpPgl 8TES4WlNdMn9bYR066IF0VYA2vUbKPmAdjXgcpziwVdc+uMuj4f9TfvrOQHK2HO0rAae o/JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:user-agent:in-reply-to:mime-version:references :message-id:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=I0g0+QqzktQQqrWue73EhpAadxDOz0A2QG9HzvbahiE=; b=hrqTYOXXPEtBqWPp3qQc+axzbgX3fPDKp+ELaKypO8icS9IKbg+EPLd8zsR/HEYMY0 IkaS4vuxlJouycI4awaGxsFfOimRLxGeCHCNlakY+tzfKPzO7z1LR3xsxIqdVjGnAizR gtdxPFqI/hgrFq4sdmtjT50NwcB77gmsssTmyoP63qqEt47F0FAQmBA2nwiJRzHLI2US jc+XYgaMjqVnWDXfPVD9e/DqSAvcW4LFe6bxFpoEaTrZkSOCyyPDQL5gw72AbxENnBuF 8eJV8kgaoyQoXuYDndbr5XtaKvlA94rJ5HlRnIzs0Sr0SbzlJpiIJKS/GsIVfL1QS0vm V6uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YIK7VHSe; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id kb24-20020a170907925800b008b138238f2esi3551415ejb.957.2023.02.15.05.47.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Feb 2023 05:47:39 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YIK7VHSe; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 32D3F38493DD for ; Wed, 15 Feb 2023 13:47:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 32D3F38493DD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676468829; bh=I0g0+QqzktQQqrWue73EhpAadxDOz0A2QG9HzvbahiE=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=YIK7VHSe/Qbo8dd6zRwMa4okMVmOcRu1DSWNf1fb/nCyxh9kno086TKl0A/J+EnBy v4uBTF3h/oRVqp0beIEFed2LF7aaKlPPtp4+ojTFkRknSXs4J9EbSlOjgg+e7hzrpZ GKbjAdkA2zRMfHaY0vScD1OOrDEUL6kmEOFpUrzw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 1B259385E443 for ; Wed, 15 Feb 2023 13:46:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B259385E443 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-655-bdPcJ4jCNyqVbpJYV7znwQ-1; Wed, 15 Feb 2023 08:46:11 -0500 X-MC-Unique: bdPcJ4jCNyqVbpJYV7znwQ-1 Received: by mail-qv1-f70.google.com with SMTP id e5-20020a056214110500b0053547681552so10523720qvs.8 for ; Wed, 15 Feb 2023 05:46:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I0g0+QqzktQQqrWue73EhpAadxDOz0A2QG9HzvbahiE=; b=6MAujf/Vqx0kDwyeq8z/yzJvi+NZUBMfi0t9Xg+9wNsyWfK9Q7oVhkl3wj6O6rxqUC I2iEJC/deeNPgkeqDivhVbWtiehxoFx/ThC0reWRUJYXT+r6kZRyhZzxPP6lzoZmpqba cHKX/j0+14TjRoq5ujRfDBdWmUAhPAuIsjxcx7SnpjaJgkSlxGrWwhqNQagMLMgr5nV2 l60csY4lU0Ft1SAuNIkYOLkIT+7OAHIUb3c/n+nvSLThlX2+wCVb8O4JTHWRFApih6yI HUlOV8f5cn8FgCiyjyQ29qqAnd7ApY6DfJdXVmxOBxNdfKVO+AfI3dretNEP2/BH68fX ELEw== X-Gm-Message-State: AO0yUKWt8zoNdWY/l97yupseMD/wwBIHxSRFbGQljunc6qJ18XFnKv7y Eh0h3oYpD/WRBDORhHFse+Znh1rZdHsPwzHeLmH7a0IFVOuyk8IwkTaahbqoK5sCiW47ovKXb7Y e76O7V64Ymp4jvQGILFbvKd4= X-Received: by 2002:ac8:57cc:0:b0:3ba:13dd:f253 with SMTP id w12-20020ac857cc000000b003ba13ddf253mr3359448qta.7.1676468770538; Wed, 15 Feb 2023 05:46:10 -0800 (PST) X-Received: by 2002:ac8:57cc:0:b0:3ba:13dd:f253 with SMTP id w12-20020ac857cc000000b003ba13ddf253mr3359409qta.7.1676468770100; Wed, 15 Feb 2023 05:46:10 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id f30-20020ac8015e000000b003b9bd163403sm13130713qtg.4.2023.02.15.05.46.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Feb 2023 05:46:09 -0800 (PST) Date: Wed, 15 Feb 2023 08:46:07 -0500 To: Jakub Jelinek Cc: GCC Patches Subject: [PATCH v2] warn-access: wrong -Wdangling-pointer with labels [PR106080] Message-ID: References: <20230215034815.1276847-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.9 (2022-11-12) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757867363709308301?= X-GMAIL-MSGID: =?utf-8?q?1757905011743337733?= On Wed, Feb 15, 2023 at 10:50:08AM +0100, Jakub Jelinek wrote: > On Tue, Feb 14, 2023 at 10:48:15PM -0500, Marek Polacek via Gcc-patches wrote: > > -Wdangling-pointer warns when the address of a label escapes. This > > causes grief in OCaml () as > > well as in the kernel: > > because it uses > > > > #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) > > > > to get the PC. -Wdangling-pointer is documented to warn about pointers > > to objects. However, it uses is_auto_decl which checks DECL_P, but DECL_P > > is also true for a label/enumerator/function declaration, none of which is > > an object. Rather, it should use auto_var_p which correctly checks VAR_P > > and PARM_DECL. > > and RESULT_DECL ;) > > > Bootstrapped/regtested on ppc64le-pc-linux-gnu, ok for trunk and 12? > > > > PR middle-end/106080 > > > > gcc/ChangeLog: > > > > * gimple-ssa-warn-access.cc (is_auto_decl): Remove. Use auto_var_p > > instead. > > > > gcc/testsuite/ChangeLog: > > > > * c-c++-common/Wdangling-pointer-10.c: New test. > > * c-c++-common/Wdangling-pointer-9.c: New test. > > > --- /dev/null > > +++ b/gcc/testsuite/c-c++-common/Wdangling-pointer-9.c > > @@ -0,0 +1,9 @@ > > +/* PR middle-end/106080 */ > > +/* { dg-do compile } */ > > +/* { dg-options "-O2 -Wdangling-pointer" } */ > > + > > +void > > +foo (void **failaddr) > > +{ > > + *failaddr = ({ __label__ __here; __here: &&__here; }); > > +} > > Perhaps add dg-bogus above just to make it more clear what > we are testing in the test? Ok, here it is. Ok? -- >8 -- -Wdangling-pointer warns when the address of a label escapes. This causes grief in OCaml () as well as in the kernel: because it uses #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) to get the PC. -Wdangling-pointer is documented to warn about pointers to objects. However, it uses is_auto_decl which checks DECL_P, but DECL_P is also true for a label/enumerator/function declaration, none of which is an object. Rather, it should use auto_var_p which correctly checks VAR_P and PARM_DECL. PR middle-end/106080 gcc/ChangeLog: * gimple-ssa-warn-access.cc (is_auto_decl): Remove. Use auto_var_p instead. gcc/testsuite/ChangeLog: * c-c++-common/Wdangling-pointer-10.c: New test. * c-c++-common/Wdangling-pointer-9.c: New test. --- gcc/gimple-ssa-warn-access.cc | 19 +++++-------------- .../c-c++-common/Wdangling-pointer-10.c | 12 ++++++++++++ .../c-c++-common/Wdangling-pointer-9.c | 9 +++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wdangling-pointer-10.c create mode 100644 gcc/testsuite/c-c++-common/Wdangling-pointer-9.c base-commit: a75fa2518d4ad997cb11145f625b4aebac5b9293 diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index ad9dac54874..2eab1d59abd 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -4326,15 +4326,6 @@ pass_waccess::check_call (gcall *stmt) check_nonstring_args (stmt); } - -/* Return true of X is a DECL with automatic storage duration. */ - -static inline bool -is_auto_decl (tree x) -{ - return DECL_P (x) && !DECL_EXTERNAL (x) && !TREE_STATIC (x); -} - /* Check non-call STMT for invalid accesses. */ void @@ -4363,7 +4354,7 @@ pass_waccess::check_stmt (gimple *stmt) while (handled_component_p (lhs)) lhs = TREE_OPERAND (lhs, 0); - if (is_auto_decl (lhs)) + if (auto_var_p (lhs)) m_clobbers.remove (lhs); return; } @@ -4383,7 +4374,7 @@ pass_waccess::check_stmt (gimple *stmt) while (handled_component_p (arg)) arg = TREE_OPERAND (arg, 0); - if (!is_auto_decl (arg)) + if (!auto_var_p (arg)) return; gimple **pclobber = m_clobbers.get (arg); @@ -4467,7 +4458,7 @@ void pass_waccess::check_dangling_uses (tree var, tree decl, bool maybe /* = false */, bool objref /* = false */) { - if (!decl || !is_auto_decl (decl)) + if (!decl || !auto_var_p (decl)) return; gimple **pclob = m_clobbers.get (decl); @@ -4528,7 +4519,7 @@ pass_waccess::check_dangling_stores (basic_block bb, if (!m_ptr_qry.get_ref (lhs, stmt, &lhs_ref, 0)) continue; - if (is_auto_decl (lhs_ref.ref)) + if (auto_var_p (lhs_ref.ref)) continue; if (DECL_P (lhs_ref.ref)) @@ -4573,7 +4564,7 @@ pass_waccess::check_dangling_stores (basic_block bb, || rhs_ref.deref != -1) continue; - if (!is_auto_decl (rhs_ref.ref)) + if (!auto_var_p (rhs_ref.ref)) continue; auto_diagnostic_group d; diff --git a/gcc/testsuite/c-c++-common/Wdangling-pointer-10.c b/gcc/testsuite/c-c++-common/Wdangling-pointer-10.c new file mode 100644 index 00000000000..ef553bdf2ce --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wdangling-pointer-10.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wdangling-pointer" } */ + +struct S { + int x; +}; + +void g (int **p) +{ + struct S s = {}; + *p = &s.x; /* { dg-warning "address of local variable" } */ +} diff --git a/gcc/testsuite/c-c++-common/Wdangling-pointer-9.c b/gcc/testsuite/c-c++-common/Wdangling-pointer-9.c new file mode 100644 index 00000000000..f6c92855404 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wdangling-pointer-9.c @@ -0,0 +1,9 @@ +/* PR middle-end/106080 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wdangling-pointer" } */ + +void +foo (void **failaddr) +{ + *failaddr = ({ __label__ __here; __here: &&__here; }); /* { dg-bogus "address of local variable" } */ +}