From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676468772; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=I0g0+QqzktQQqrWue73EhpAadxDOz0A2QG9HzvbahiE=; b=cm97qaK4oaqZJS997L7ySun2TYv4fZPCUVEvq9JyI6i86wfnymQ0gr2lwraiIa1gjBCr2q wTgXS9IlpySNPJi5fIc9ZT5kWAoqDB2I/eSE1SSH9wclLWA2jdl1E3+zmsZ3Ic8TuwrFxu 2mGpJgyHdc4HsivFGYawHfCHZ41oVTA= 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-Google-Smtp-Source: AK7set8kDfN6qKrk7Yk+3m6uknNPRUzVr3LcqkJxlQ/WfgDf5LewZhbNq/6w4NuAQA+csPHbzhm26g== 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 From: Marek Polacek 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-Type: text/plain; charset=us-ascii 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 List-Id: 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 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" } */ +} base-commit: a75fa2518d4ad997cb11145f625b4aebac5b9293 -- 2.39.1