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.133.124]) by sourceware.org (Postfix) with ESMTPS id C7567385840C for ; Thu, 7 Dec 2023 20:56:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C7567385840C 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 C7567385840C 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=1701982567; cv=none; b=AUP5IMoi8Ow0qY07WosRAyJu05hKsaLeY/9xVDTsp522ax7umnV3JESY7eXPiajxRgWqNzx00UaCN1xAY8P/h5GXzwbZ/X2jCp0dUGnEeftm3GUgOH1bBzvYcZoLY26RnJXy4N0km8T6wa8XAmcKCiWMMeu6x2AWj0+6b4gCYbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701982567; c=relaxed/simple; bh=/J7DS3UT7Jvj/GT9A4d9jm7ipPegfANvGhuDrVCFCeI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PgNQ5uf0ukwgE6NaFEuOXYev2XA8IjqS88belLedoY6Z15aPDWa9qXB4v7EtDITuE11zI5+EYKTUP824HbtlNGcGZTMdckreTG30BFHkrNgdP0x0e3UblbA0BgNoIJjmwvH/vSbZmB+/6/BIU5MTXznY2GNrJUBabp7c8NqiUZ8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701982563; 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=kIduQ8qDuZqhX7LlTSv1sxWa0M6Ie6oc8TlTJNotTTU=; b=VcUwsgD4DIXsnmMkZMvDbTutltnLDBW1yLG84r7DJMYqW3UPrefzXoE2vKZaiqqg/ciDBc NxiQyH/TcxluYDnLMg4bowowrSMBaSuXmsrfE8RgEUxQ7ffQ+C+6ZP1G1UX5Cc0LRaeeD8 M/dktBcdpEUC2GDnCdamngAynuuz4co= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-684-7T5EaJ9XNLKUqPvsTVeepw-1; Thu, 07 Dec 2023 15:56:00 -0500 X-MC-Unique: 7T5EaJ9XNLKUqPvsTVeepw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id CD393862DE0; Thu, 7 Dec 2023 20:55:59 +0000 (UTC) Received: from localhost (unknown [10.42.28.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id 96F361C060AF; Thu, 7 Dec 2023 20:55:59 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix recent changes to __glibcxx_assert [PR112882] Date: Thu, 7 Dec 2023 20:55:45 +0000 Message-ID: <20231207205558.950070-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 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,T_SCC_BODY_TEXT_LINE 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: Tested x86_64-linux. Pushed to trunk. -- >8 -- The changes in r14-6198-g5e8a30d8b8f4d7 were broken, as I used _GLIBCXX17_CONSTEXPR for the 'if _GLIBCXX17_CONSTEXPR (true)' condition, forgetting that it would also be used for the is_constant_evaluated() check. Using 'if constexpr (std::is_constant_evaluated())' is a bug. Additionally, relying on __glibcxx_assert_fail to give a "not a constant expression" error is a problem because at -O0 an undefined reference to __glibcxx_assert_fail is present in the compiled code. This means you can't use libstdc++ headers without also linking to libstdc++ for the symbol definition. This fix rewrites the __glibcxx_assert macro again. This still avoids doing the duplicate checks, once for constexpr and once at runtime (if _GLIBCXX_ASSERTIONS is defined). When _GLIBCXX_ASSERTIONS is defined we still rely on __glibcxx_assert_fail to give a "not a constant expression" error during constant evaluation (because when assertions are defined it's not a problem to emit a reference to the symbol). But when that macro is not defined, we use a new inline (but not constexpr) overload of __glibcxx_assert_fail to cause compilation to fail. That inline function doesn't cause an undefined reference to a symbol in the library (and will be optimized away anyway). We can also add always_inline to the __is_constant_evaluated function, although this doesn't actually matter for -O0 and it's always inlined with any optimization enabled. libstdc++-v3/ChangeLog: PR libstdc++/112882 * include/bits/c++config (__is_constant_evaluated): Add always_inline attribute. (_GLIBCXX_DO_ASSERT): Remove macro. (__glibcxx_assert): Define separately for assertions-enabled and constexpr-only cases. --- libstdc++-v3/include/bits/c++config | 33 ++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config index 284d24d933f..25d37428fc1 100644 --- a/libstdc++-v3/include/bits/c++config +++ b/libstdc++-v3/include/bits/c++config @@ -538,6 +538,7 @@ namespace std // This can be used without checking if the compiler supports the feature. // The macro _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED can be used to check if // the compiler support is present to make this function work as expected. + __attribute__((__always_inline__)) _GLIBCXX_CONSTEXPR inline bool __is_constant_evaluated() _GLIBCXX_NOEXCEPT { @@ -598,19 +599,31 @@ namespace std #endif #if defined(_GLIBCXX_ASSERTIONS) -# define _GLIBCXX_DO_ASSERT true -#elif _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED -# define _GLIBCXX_DO_ASSERT std::__is_constant_evaluated() -#else -# define _GLIBCXX_DO_ASSERT false -#endif - +// Enable runtime assertion checks, and also check in constant expressions. # define __glibcxx_assert(cond) \ do { \ - if _GLIBCXX17_CONSTEXPR (_GLIBCXX_DO_ASSERT) \ - if (__builtin_expect(!bool(cond), false)) \ - _GLIBCXX_ASSERT_FAIL(cond); \ + if (__builtin_expect(!bool(cond), false)) \ + _GLIBCXX_ASSERT_FAIL(cond); \ } while (false) +#elif _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED +// Only check assertions during constant evaluation. +namespace std +{ + __attribute__((__always_inline__,__visibility__("default"))) + inline void + __glibcxx_assert_fail() + { } +} +# define __glibcxx_assert(cond) \ + do { \ + if (std::__is_constant_evaluated()) \ + if (__builtin_expect(!bool(cond), false)) \ + std::__glibcxx_assert_fail(); \ + } while (false) +#else +// Don't check any assertions. +# define __glibcxx_assert(cond) +#endif // Macro indicating that TSAN is in use. #if __SANITIZE_THREAD__ -- 2.43.0