From patchwork Thu Sep 12 08:49:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1984566 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Pit9OO7O; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X4BSr1b0zz1y25 for ; Thu, 12 Sep 2024 19:08:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 72983385C6D2 for ; Thu, 12 Sep 2024 09:08:56 +0000 (GMT) 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 ESMTP id 2221B3858C98 for ; Thu, 12 Sep 2024 09:08:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2221B3858C98 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2221B3858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726132108; cv=none; b=gvvJ8yfY3E3KXoUHcAb2YAXcN/Yb01VnxY4CnaLVJxpIqG9JWyfhxKd61191fn70PkDeBvJzuXVPZo/CAf6YAAZ2Y2alXr2jtENwncoyCuP4Awuxp8mFqxxeIVrVxKFUfyHPNFeuxBIGTjgxBS0PLous/X397tnj56yqMMpl5N8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726132108; c=relaxed/simple; bh=JBs6cLA+8uL05lTWmzkLib1rkV62NfRGaDA0lXBCiBI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ut7pmzzaLRwnvwRUunjaTDj6a685BGgQcCL8gpx3nu/XWd37q4D3mz/tZEET6ItFDX8byQ710Xbr04bS6JM7zs0aiXBdTSpFFQUBbEDX2rnE67lUE2RZXffb8OHWNCfUqQPvWb/Wi2e+R/5HhGkeg0ytOgmU4kP6vtRjeNhue30= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726132106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mgvG8rsrJvpQmNS2o9MdMcPw65kLVuO+5sM1ZmIKP6Y=; b=Pit9OO7O1dqXu8GRbTAAHO+mahDqJ4DzZx4kuBbJWYryw5RqARGE5aamHrXoiFdKDXc2+u c+gC+brVBYEop8d14IZi5nX3Sui93CcHOCK1VscGkOgTqmxVM8W487wcezatzFWqBxfSxT WEXxjfxkilmTEO0moy4igyChZFdL2Ns= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-13-0Bv9phELOOa0q08zimH4cQ-1; Thu, 12 Sep 2024 05:08:23 -0400 X-MC-Unique: 0Bv9phELOOa0q08zimH4cQ-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 512E21955D4D; Thu, 12 Sep 2024 09:08:20 +0000 (UTC) Received: from localhost (unknown [10.42.28.101]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2F1B73000238; Thu, 12 Sep 2024 09:08:18 +0000 (UTC) From: Jonathan Wakely To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH 1/2] c++: Make __builtin_launder reject invalid types [PR116673] Date: Thu, 12 Sep 2024 09:49:45 +0100 Message-ID: <20240912090816.294481-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. OK for trunk? -- >8 -- The standard says that std::launder is ill-formed for function pointers and cv void pointers, so there's no reason for __builtin_launder to accept them. This change allows implementations of std::launder to defer to the built-in for error checking, although libstdc++ will continue to diagnose it directly for more user-friendly diagnostics. PR c++/116673 gcc/cp/ChangeLog: * semantics.cc (finish_builtin_launder): Diagnose function pointers and cv void pointers. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/launder10.C: New test. --- gcc/cp/semantics.cc | 17 +++++++++++++---- gcc/testsuite/g++.dg/cpp1z/launder10.C | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/launder10.C diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 63212afafb3..b194b01f865 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -13482,11 +13482,20 @@ finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain) arg = decay_conversion (arg, complain); if (error_operand_p (arg)) return error_mark_node; - if (!type_dependent_expression_p (arg) - && !TYPE_PTR_P (TREE_TYPE (arg))) + if (!type_dependent_expression_p (arg)) { - error_at (loc, "non-pointer argument to %<__builtin_launder%>"); - return error_mark_node; + tree type = TREE_TYPE (arg); + if (!TYPE_PTR_P (type)) + { + error_at (loc, "non-pointer argument to %<__builtin_launder%>"); + return error_mark_node; + } + else if (!object_type_p (TREE_TYPE (type))) + { + // std::launder is ill-formed for function and cv void pointers. + error_at (loc, "invalid argument to %<__builtin_launder%>"); + return error_mark_node; + } } if (processing_template_decl) arg = orig_arg; diff --git a/gcc/testsuite/g++.dg/cpp1z/launder10.C b/gcc/testsuite/g++.dg/cpp1z/launder10.C new file mode 100644 index 00000000000..7c15eeb891f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/launder10.C @@ -0,0 +1,15 @@ +// PR c++/116673 +// { dg-do compile } + +void +bar (void *p) +{ + __builtin_launder (bar); // { dg-error {invalid argument to '__builtin_launder'} } + __builtin_launder (p); // { dg-error {invalid argument to '__builtin_launder'} } + const void* cp = p; + __builtin_launder (cp); // { dg-error {invalid argument to '__builtin_launder'} } + volatile void* vp = p; + __builtin_launder (vp); // { dg-error {invalid argument to '__builtin_launder'} } + const volatile void* cvp = p; + __builtin_launder (cvp); // { dg-error {invalid argument to '__builtin_launder'} } +}