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'} } +} From patchwork Thu Sep 12 08:49:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1984567 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=HKr0O7T1; 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 4X4BSs6M5zz1y25 for ; Thu, 12 Sep 2024 19:09:01 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E2D63858C60 for ; Thu, 12 Sep 2024 09:08:59 +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.133.124]) by sourceware.org (Postfix) with ESMTP id 902353858C32 for ; Thu, 12 Sep 2024 09:08:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 902353858C32 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 902353858C32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726132112; cv=none; b=iNFfYmrKtMrZFfmBwTy4/vN1hh0TLiuFRipxdx4ZWBmEGu87PZ5sSwirNH60FVc/T12RrD/agAC4YUgd65SfUdexkTGYS3rDosQ+qqypqMKWN/1D28PTVjwmxTUeQpj6sx+af7y2pMaB+ApV+V35G2xkf9sckXEclhyPrrrLfMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726132112; c=relaxed/simple; bh=Ac5/p+J4et9PIqkj42CtPCo4lGzA5a4x5DyehmRSNBU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Li+bszVsgIetgSbFWsZJzVMAlNzmE/PXx3jpAxybrBXwxCBgmH8lMZ5qkmKA6mQTLElBY/uJrzoLD3EH0vYFTxPsalD6OT1RMjP2iy64XJsN7iDi7hby2v5IOTuOn2RRhbhvVWyvdvxYUE+rCJ7aSgQbx9I39frkPVTC11HJePI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726132109; 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: in-reply-to:in-reply-to:references:references; bh=sKupaHGQJJ990JSnKW1Np9TYpLwUZYdDLSS/A8YCWy0=; b=HKr0O7T1K69ndLAd9g06+z2XNFJjVA/j5ehG67a6g6c/L6LPg6r3bwsU0q2xyrwlZ8vXIN HGOmDSRovm06wetXOtwgaRI1MleJgUE3f5MaLiSSJA9pIt8v5TTWeK5ZCtQUgvPUXKSgS0 C/34677oVERu0+PlR5cLvOJiuBbbQ0I= Received: from mx-prod-mc-01.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-436--im5aDpkNGSsJ-jQalNXAg-1; Thu, 12 Sep 2024 05:08:25 -0400 X-MC-Unique: -im5aDpkNGSsJ-jQalNXAg-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B59E41944AA4; Thu, 12 Sep 2024 09:08:23 +0000 (UTC) Received: from localhost (unknown [10.42.28.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C4C7219560AA; Thu, 12 Sep 2024 09:08:22 +0000 (UTC) From: Jonathan Wakely To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH 2/2] libstdc++: Simplify std::launder definition Date: Thu, 12 Sep 2024 09:49:46 +0100 Message-ID: <20240912090816.294481-2-jwakely@redhat.com> In-Reply-To: <20240912090816.294481-1-jwakely@redhat.com> References: <20240912090816.294481-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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. Both GCC and Clang support the __is_function built-in, so we should get the static_assert here. For a compiler that doesn't support it, we rely on __builtin_launder to diagnose function pointers. That's true for Clang, and PATCH 1/2 makes it true for G++. We might want to consider splitting a frequently-used subset of into a new header that we can include in . Nearly every header outside of libsupc++ includes , but they don't all need all of it. -- >8 -- A single static assert is a much simpler way to implement the compile-time preconditions on std::launder than an overload set of deleted functions and function templates. The only difficulty is that doesn't include so we can't use std::is_function and std::is_void for the checks. That can be worked around though, by using the __is_same and __is_function built-ins. If the __is_function built-in isn't supported then the __builtin_launder built-in will give an error anyway, since the commit preceding this one. We can also remove the redundant __cplusplus >= 201703L check around the definitions of std::launder and the interference constants, which are already guarded by the appropriate feature test macros. libstdc++-v3/ChangeLog: * libsupc++/new (launder): Add static_assert and remove deleted overloads. * testsuite/18_support/launder/requirements_neg.cc: Adjust expected diagnostics. --- libstdc++-v3/libsupc++/new | 36 ++++++++----------- .../18_support/launder/requirements_neg.cc | 15 ++++---- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/libstdc++-v3/libsupc++/new b/libstdc++-v3/libsupc++/new index 2e2038e1a82..af5c7690bb9 100644 --- a/libstdc++-v3/libsupc++/new +++ b/libstdc++-v3/libsupc++/new @@ -198,7 +198,6 @@ inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPT { } //@} } // extern "C++" -#if __cplusplus >= 201703L namespace std { #ifdef __cpp_lib_launder // C++ >= 17 && HAVE_BUILTIN_LAUNDER @@ -206,33 +205,28 @@ namespace std template [[nodiscard]] constexpr _Tp* launder(_Tp* __p) noexcept - { return __builtin_launder(__p); } - - // The program is ill-formed if T is a function type or - // (possibly cv-qualified) void. - - template - void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUAL) = delete; - template - void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUAL) = delete; - - void launder(void*) = delete; - void launder(const void*) = delete; - void launder(volatile void*) = delete; - void launder(const volatile void*) = delete; + { + if constexpr (__is_same(const volatile _Tp, const volatile void)) + static_assert(!__is_same(const volatile _Tp, const volatile void), + "std::launder argument must not be a void pointer"); +#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) + else if constexpr (__is_function(_Tp)) + static_assert(!__is_function(_Tp), + "std::launder argument must not be a function pointer"); +#endif + else + return __builtin_launder(__p); + return nullptr; + } #endif // __cpp_lib_launder #ifdef __cpp_lib_hardware_interference_size // C++ >= 17 && defined(gcc_dest_sz) inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE; inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE; #endif // __cpp_lib_hardware_interference_size -} -#endif // C++17 // Emitted despite the FTM potentially being undefined. -#if __cplusplus > 201703L -namespace std -{ +#if __cplusplus >= 202002L /// Tag type used to declare a class-specific operator delete that can /// invoke the destructor before deallocating the memory. struct destroying_delete_t @@ -241,8 +235,8 @@ namespace std }; /// Tag variable of type destroying_delete_t. inline constexpr destroying_delete_t destroying_delete{}; -} #endif // C++20 +} #pragma GCC visibility pop diff --git a/libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc b/libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc index 2808ebf614d..82ce0b35a8c 100644 --- a/libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc +++ b/libstdc++-v3/testsuite/18_support/launder/requirements_neg.cc @@ -25,14 +25,17 @@ int f2(const char*, ...); void test01() { - std::launder( &f1 ); // { dg-error "deleted function" } - std::launder( &f2 ); // { dg-error "deleted function" } + std::launder( &f1 ); // { dg-error "here" } + std::launder( &f2 ); // { dg-error "here" } void* p = nullptr; - std::launder( p ); // { dg-error "deleted function" } + std::launder( p ); // { dg-error "here" } const void* cp = nullptr; - std::launder( cp ); // { dg-error "deleted function" } + std::launder( cp ); // { dg-error "here" } volatile void* vp = nullptr; - std::launder( vp ); // { dg-error "deleted function" } + std::launder( vp ); // { dg-error "here" } const volatile void* cvp = nullptr; - std::launder( cvp ); // { dg-error "deleted function" } + std::launder( cvp ); // { dg-error "here" } } +// { dg-error "std::launder argument must not be a void pointer" "" { target *-*-* } 0 } +// { dg-error "std::launder argument must not be a function pointer" "" { target *-*-* } 0 } +// { dg-warning "ignoring return value" "nodiscard" { target *-*-* } 0 }