From patchwork Wed Feb 10 15:05:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1439001 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=w5exUjnF; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DbNP71ZZxz9rx6 for ; Thu, 11 Feb 2021 02:05:44 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8D266398EC2F; Wed, 10 Feb 2021 15:05:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D266398EC2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1612969541; bh=59hgpe1E5llxLXBCPfHdmIdqxapWQQ4Dmng/M9Xf4NU=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=w5exUjnFbyt/+iQx9mgZGneAwZbYfktemW0pF/wblFhRNMrVzdYhSQZJAGnwYu7FY ssw4M6fE5IXfBvPkOFM2b77sdRoMgmt1Jhutle0xxZRgChynZYTuIgXNaplbtWRGM0 Dbu3zdvRt5YsoEt+azzUVlUWcojphfyt4RqovlzY= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id E672838618FF for ; Wed, 10 Feb 2021 15:05:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E672838618FF Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-149-KzRdm3GUOIm1SclK6I7CRw-1; Wed, 10 Feb 2021 10:05:36 -0500 X-MC-Unique: KzRdm3GUOIm1SclK6I7CRw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1532A192AB7A for ; Wed, 10 Feb 2021 15:05:34 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-197.ams2.redhat.com [10.36.112.197]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A45DA5D6DC; Wed, 10 Feb 2021 15:05:33 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 11AF5VwF1185919 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 10 Feb 2021 16:05:31 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 11AF5Vks1185918; Wed, 10 Feb 2021 16:05:31 +0100 Date: Wed, 10 Feb 2021 16:05:31 +0100 To: Jason Merrill Subject: [PATCH] c++, v2: Consider addresses of heap artificial vars always non-NULL [PR98988, PR99031] Message-ID: <20210210150531.GL4020736@tucnak> References: <20210209082504.GQ4020736@tucnak> <8a677de7-9ed1-4da7-ed27-ef8e631ebc13@redhat.com> <20210209201134.GD4020736@tucnak> <5c7be040-4a74-7fd3-6ceb-0f3457202338@redhat.com> MIME-Version: 1.0 In-Reply-To: <5c7be040-4a74-7fd3-6ceb-0f3457202338@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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: 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" On Tue, Feb 09, 2021 at 04:08:03PM -0500, Jason Merrill via Gcc-patches wrote: > > They are not in the symtab from the C++ FE. And we don't allow those VAR_DECLs or their > > addresses to leak into the IL anyway. > > But, they are TREE_STATIC because pretending they are automatic didn't > > really work (e.g. address of automatic variables is not a constant expression). > > The generic folding code uses maybe_nonzero_address predicate which > > apprently creates a symtab entry for them but as nothing uses them they > > won't be emitted. > > Ah. > > > If we wanted to avoid that, we'd need to either add some generic VAR_DECL > > bit for those vars or add a target hook that would return true for them and > > then handle them in the middle-end. > > I'm surprised that nonzero_address has such a limited set of things it will > actually believe have non-zero addresses with > -fno-delete-null-pointer-checks. But it seems that we should be able to > arrange to satisfy > > > if (definition && !DECL_EXTERNAL (decl) > > since these "variables" are indeed defined within the current translation > unit. Doing that seems to work and as added benefit it fixes another PR that has been filed recently. I need to create the varpool node explicitly and call a method that sets the definition member in there, but I can also unregister those varpool nodes at the end of constexpr processing, as the processing ensured they don't leak outside of the processing. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-02-10 Jakub Jelinek PR c++/98988 PR c++/99031 * constexpr.c: Include cgraph.h. (cxx_eval_call_expression): Call varpool_node::finalize_decl on heap artificial vars. (cxx_eval_outermost_constant_expr): Remove varpool nodes for heap artificial vars. * g++.dg/cpp2a/constexpr-new16.C: New test. * g++.dg/cpp2a/constexpr-new17.C: New test. Jakub --- gcc/cp/constexpr.c.jj 2021-02-09 10:05:28.820833601 +0100 +++ gcc/cp/constexpr.c 2021-02-10 11:31:16.567984809 +0100 @@ -35,6 +35,7 @@ along with GCC; see the file COPYING3. #include "timevar.h" #include "fold-const-call.h" #include "stor-layout.h" +#include "cgraph.h" static bool verify_constant (tree, bool, bool *, bool *); #define VERIFY_CONSTANT(X) \ @@ -2340,6 +2341,7 @@ cxx_eval_call_expression (const constexp type); DECL_ARTIFICIAL (var) = 1; TREE_STATIC (var) = 1; + varpool_node::finalize_decl (var); ctx->global->heap_vars.safe_push (var); ctx->global->values.put (var, NULL_TREE); return fold_convert (ptr_type_node, build_address (var)); @@ -7199,15 +7201,18 @@ cxx_eval_outermost_constant_expr (tree t non_constant_p = true; } FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) - if (DECL_NAME (heap_var) != heap_deleted_identifier) - { - if (!allow_non_constant && !non_constant_p) - error_at (DECL_SOURCE_LOCATION (heap_var), - "%qE is not a constant expression because allocated " - "storage has not been deallocated", t); - r = t; - non_constant_p = true; - } + { + if (DECL_NAME (heap_var) != heap_deleted_identifier) + { + if (!allow_non_constant && !non_constant_p) + error_at (DECL_SOURCE_LOCATION (heap_var), + "%qE is not a constant expression because allocated " + "storage has not been deallocated", t); + r = t; + non_constant_p = true; + } + varpool_node::get (heap_var)->remove (); + } } /* Check that immediate invocation does not return an expression referencing --- gcc/testsuite/g++.dg/cpp2a/constexpr-new16.C.jj 2021-02-10 11:11:00.117658382 +0100 +++ gcc/testsuite/g++.dg/cpp2a/constexpr-new16.C 2021-02-10 11:11:00.117658382 +0100 @@ -0,0 +1,13 @@ +// PR c++/98988 +// { dg-do compile { target c++20 } } +// { dg-options "-fno-delete-null-pointer-checks" } + +constexpr bool +foo () +{ + auto ptr = new int(); + delete ptr; + return true; +} + +static_assert (foo ()); --- gcc/testsuite/g++.dg/cpp2a/constexpr-new17.C.jj 2021-02-10 13:04:17.947606440 +0100 +++ gcc/testsuite/g++.dg/cpp2a/constexpr-new17.C 2021-02-10 13:04:06.417738050 +0100 @@ -0,0 +1,15 @@ +// PR c++/99031 +// { dg-do compile { target c++20 } } + +constexpr bool +foo () +{ + auto a = new int; + auto b = new int; + bool r = a == b; + delete b; + delete a; + return r; +} + +static_assert (!foo ());