From patchwork Thu Sep 12 19:59:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1984852 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=elrnx4x6; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X4Svy6FfXz1y1l for ; Fri, 13 Sep 2024 05:59:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8152A385DC1B for ; Thu, 12 Sep 2024 19:59: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 B57233858D26 for ; Thu, 12 Sep 2024 19:59:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B57233858D26 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 B57233858D26 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=1726171170; cv=none; b=rYxP8RGXdvkhKwCMNv65f9mZt84CrwbzPF2+qSiQFZlnL0OWw621LAADCNcfIwUGr71f3LIbt6HqfiX2qqyshyABZ8QY+je7LazMvyuHAATZzg7Lqfk1C2lZUJV9BOVRMWVak9oXpnCAB4f1Hl0pZ8mHYRKpGobruIR/wmi436w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726171170; c=relaxed/simple; bh=7xEowm6EwU+7y2NPXvMQ3T8Yp3kY/2HCd2mH1jKxfbI=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=hunYFUDzq8UgU0CvjNixu0N1o/5buTeH7oeux/J5Mq7j+ppbc+nSa9qcfa19KAN5ajkCH3q5YzmSyPsKTMeOAYhHde454qwXe9HEj/Th/uWwhcK08O4/jEPqv1Pmhj+V7KKSXdMS5s5p33vzerKEtlX22mwX0QtngBk1+CeD6XM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726171166; 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=k6387Q3rM5y2dbGyR0V/XavbhBzIluTEMiM/rmW4Kfg=; b=elrnx4x6wjOKNpiIPis7sopGzoy0s4frZnyiYxKCOwtA7QuYwHu8umOnrBHux+A8ZFF7ni z06XeNkKLgWQrKKcE8NvgTeU5uxrCeMjS0rTGyV5lOyM53Y9aUMz0ewFj32qoDXOdO+rL9 kqvUltTeiufaB5RPsLFj9FMzV6KhRmo= Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-359-8jvlZmdfMsCb_2XeRNYbag-1; Thu, 12 Sep 2024 15:59:25 -0400 X-MC-Unique: 8jvlZmdfMsCb_2XeRNYbag-1 Received: by mail-yb1-f198.google.com with SMTP id 3f1490d57ef6-e1ab008280aso2546334276.0 for ; Thu, 12 Sep 2024 12:59:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726171164; x=1726775964; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=k6387Q3rM5y2dbGyR0V/XavbhBzIluTEMiM/rmW4Kfg=; b=nwA9R7krr3W0bhvHcgmXrWoIfw5WiSOerGBWgGHQi+IO9shFXPBuO7ftTZgsBxN0JJ smWIpK3P/bgPCSbzJodYAowOn1baFA3PlDWUvHDn6Dk3ilr2D561CGXRYLHZoPmRAYss It+PeGQhEpxLl9HOSRk00Y0tZxfkSYryz9+NlIAbaYwvNgAqJCKf1XrKUm+EhOFLCmCY Y0tZUTMhAZ8sITYrDDucuYGuyhq+yo1vubK+UR3lFT9uoHgWUp91BqT/WncmUILFlxNx R5D5A10L7B0BxG7bTumq3qA3Dee27nA9FKVWA8eyUOwjcMI7SbKmHK10LvHaUe1UgCrR zy0A== X-Gm-Message-State: AOJu0Yz0O7URXtSeKNMNdh+A0YHh9IGc9hYj/M8izxOwXBSku/EdiJBW Yw7WULbuUx65Pxg8SLqbNqQpEz28B44XUFvzzcM18sZ7YXFsOD2wiDNR9TBEwD1PeLKJGfmWT9p iIQK/K1RfwAXiWh3liTHv5yciob0iZw3xSNyIdYAtkMy6vz8nn7Iey9MNNkZYRGZDNxNjnkFMnE nJKPQZwBHNzGGbfjN3ll5IkOJ3hkmVKg== X-Received: by 2002:a05:6902:2782:b0:e1a:b102:47f0 with SMTP id 3f1490d57ef6-e1d9dc3dee1mr3514447276.41.1726171164394; Thu, 12 Sep 2024 12:59:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHxFmFL63zu3CIBeqpQm+H3hhWaQoNm2MVVE5G1Wuw4ZV9FtjAb31/rfQY9i2phqbD5UWpaQFZ9iTiJld+QKDY= X-Received: by 2002:a05:6902:2782:b0:e1a:b102:47f0 with SMTP id 3f1490d57ef6-e1d9dc3dee1mr3514438276.41.1726171164106; Thu, 12 Sep 2024 12:59:24 -0700 (PDT) MIME-Version: 1.0 References: <20240912090816.294481-1-jwakely@redhat.com> <01b6c559-f802-468f-aae4-874058187a82@redhat.com> In-Reply-To: <01b6c559-f802-468f-aae4-874058187a82@redhat.com> From: Jonathan Wakely Date: Thu, 12 Sep 2024 20:59:08 +0100 Message-ID: Subject: [PATCH 1/2 v2] c++: Make __builtin_launder reject invalid types [PR116673] To: Jason Merrill Cc: gcc Patches , "libstdc++" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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=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.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 On Thu, 12 Sept 2024 at 16:02, Jason Merrill wrote: > > On 9/12/24 4:49 AM, Jonathan Wakely wrote: > > 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%>"); > > Let's be more specific by combining both errors into > > "type %qT of argument to %<__builtin_launder"> is not a pointer to > object type" > > The tests can also be combined to !TYPE_PTROB_P. > > OK with that change. Thanks, here's what I pushed. commit 9fe57e4879de93b6e3c7b4c226f42d5f3a48474f Author: Jonathan Wakely Date: Wed Sep 11 11:47:44 2024 c++: Make __builtin_launder reject invalid types [PR116673] 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/launder2.C: Adjust dg-error strings. * g++.dg/cpp1z/launder10.C: New test. diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 63212afafb3..8219d6410b8 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -13482,10 +13482,10 @@ 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) && !TYPE_PTROB_P (TREE_TYPE (arg))) { - error_at (loc, "non-pointer argument to %<__builtin_launder%>"); + error_at (loc, "type %qT of argument to %<__builtin_launder%> " + "is not a pointer to object type", TREE_TYPE (arg)); return error_mark_node; } if (processing_template_decl) diff --git a/gcc/testsuite/g++.dg/cpp1z/launder10.C b/gcc/testsuite/g++.dg/cpp1z/launder10.C new file mode 100644 index 00000000000..2109a2e3839 --- /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 {argument to '__builtin_launder'} } + __builtin_launder (p); // { dg-error {argument to '__builtin_launder'} } + const void* cp = p; + __builtin_launder (cp); // { dg-error {argument to '__builtin_launder'} } + volatile void* vp = p; + __builtin_launder (vp); // { dg-error {argument to '__builtin_launder'} } + const volatile void* cvp = p; + __builtin_launder (cvp); // { dg-error {argument to '__builtin_launder'} } +} diff --git a/gcc/testsuite/g++.dg/cpp1z/launder2.C b/gcc/testsuite/g++.dg/cpp1z/launder2.C index 9cd1779704b..a2d44861265 100644 --- a/gcc/testsuite/g++.dg/cpp1z/launder2.C +++ b/gcc/testsuite/g++.dg/cpp1z/launder2.C @@ -4,11 +4,11 @@ int a; int *b = __builtin_launder (); // { dg-error "wrong number of arguments to" } int *c = __builtin_launder (&a, 2); // { dg-error "wrong number of arguments to" } int *d = __builtin_launder (&a); -int e = __builtin_launder (a); // { dg-error "non-pointer argument to" } +int e = __builtin_launder (a); // { dg-error "not a pointer to object type" } int &f = a; -int g = __builtin_launder (f); // { dg-error "non-pointer argument to" } +int g = __builtin_launder (f); // { dg-error "not a pointer to object type" } -template T f1 (T x) { return __builtin_launder (x); } // { dg-error "non-pointer argument to" } +template T f1 (T x) { return __builtin_launder (x); } // { dg-error "not a pointer to object type" } template T f2 (T x) { return __builtin_launder (x); } int h = f1 (a);