From patchwork Thu Jun 27 08:45:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1953064 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=fp5J5rxO; 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 4W8sgT6VbNz20X6 for ; Thu, 27 Jun 2024 18:49:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 173F63838A09 for ; Thu, 27 Jun 2024 08:49:08 +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 ESMTPS id 9AAEA383A60D for ; Thu, 27 Jun 2024 08:48:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9AAEA383A60D 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 9AAEA383A60D 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=1719478117; cv=none; b=nuJOcEChYCRT4kNd6Zt+/BLxiMfHmcc6Tt/MXkAEPxlwTzK8R3lsJvRKUnSHaKgIcw734oKU70fU7iNlNtWu1AwEihxHmdHlQVCNvnau87lpy1wJ8NLavPNWUAozBoCIHXbliYqOojGTlrFCwFnJJ/ci43cFk7iggRX+3RNl444= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719478117; c=relaxed/simple; bh=bngTaIECn9doOoalmiPsUVAGAMt1HI8gpJLAFWpyous=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=hAC1BocePJH9RsZX1DvGxB9M4iwmv2QmZIrUQKjRUBq/lHSy0RSKgS38ZBqJRsmfLUR1g69i4icDWACiXNZkx3MmFdXkNLRd0fO0qWerZsV5CkjXv3f/516DTa96X0paMTz+dA14+TMyZGCVBGTHIhM+LAZvLgjq81NwP3uwhWY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719478110; 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=FDx8rbJI6D+kqFNIRuiV4nxjWyopdbDO6Vw3aAPtSo0=; b=fp5J5rxO0h4LXO/10c7ybLzHSuZP3q82k+vYTE5zJPXkzIiYvTgcNZ73t8r1a+6WR0pfSR qVPPCMWO9/oiEDNPdYm+iu6aYTWjwrMZJHodY+RGQ9KKq3+eSG7lbz0vADnFeqP3CF9xH/ YzEzcK2Mvd2prr6SEQVtQWgIO2FAIQQ= Received: from mx-prod-mc-02.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-572-si0WsFdLPDyyChLDoO8uOg-1; Thu, 27 Jun 2024 04:48:26 -0400 X-MC-Unique: si0WsFdLPDyyChLDoO8uOg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0F7BB1955DCD; Thu, 27 Jun 2024 08:48:24 +0000 (UTC) Received: from localhost (unknown [10.42.28.171]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5FEC519560A3; Thu, 27 Jun 2024 08:48:22 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] libstdc++: Enable more debug assertions during constant evaluation [PR111250] Date: Thu, 27 Jun 2024 09:45:38 +0100 Message-ID: <20240627084821.95065-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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_H4, 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 Tested x86_64-linux. Pushed to trunk. Worth backporting, I think, but I'll wait a bit. -- >8 -- Some of our debug assertions expand to nothing unless _GLIBCXX_ASSERTIONS is defined, which means they are not checked during constant evaluation. By making them unconditionally expand to a __glibcxx_assert expression they will be checked during constant evaluation. This allows us to diagnose more instances of undefined behaviour at compile-time, such as accessing a vector past-the-end. libstdc++-v3/ChangeLog: PR libstdc++/111250 * include/debug/assertions.h (__glibcxx_requires_non_empty_range) (__glibcxx_requires_nonempty, __glibcxx_requires_subscript): Define to __glibcxx_assert expressions or to debug mode __glibcxx_check_xxx expressions. * testsuite/23_containers/array/element_access/constexpr_c++17.cc: Add checks for out-of-bounds accesses in constant expressions. * testsuite/23_containers/vector/element_access/constexpr.cc: Likewise. --- libstdc++-v3/include/debug/assertions.h | 14 +++--- .../array/element_access/constexpr_c++17.cc | 44 +++++++++++++++++++ .../vector/element_access/constexpr.cc | 24 ++++++++-- 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/debug/assertions.h b/libstdc++-v3/include/debug/assertions.h index fff1ae8def0..20441e33897 100644 --- a/libstdc++-v3/include/debug/assertions.h +++ b/libstdc++-v3/include/debug/assertions.h @@ -31,12 +31,7 @@ #include -#ifndef _GLIBCXX_ASSERTIONS -# define __glibcxx_requires_non_empty_range(_First,_Last) -# define __glibcxx_requires_nonempty() -# define __glibcxx_requires_subscript(_N) -#else - +#ifndef _GLIBCXX_DEBUG // Verify that [_First, _Last) forms a non-empty iterator range. # define __glibcxx_requires_non_empty_range(_First,_Last) \ __glibcxx_assert(_First != _Last) @@ -45,6 +40,13 @@ // Verify that the container is nonempty # define __glibcxx_requires_nonempty() \ __glibcxx_assert(!this->empty()) +#else // Use the more verbose Debug Mode checks. +# define __glibcxx_requires_non_empty_range(_First,_Last) \ + __glibcxx_check_non_empty_range(_First,_Last) +# define __glibcxx_requires_nonempty() \ + __glibcxx_check_nonempty() +# define __glibcxx_requires_subscript(_N) \ + __glibcxx_check_subscript(_N) #endif #if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED diff --git a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc index a14ad487b42..19ab1cc1f8e 100644 --- a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc +++ b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc @@ -66,3 +66,47 @@ constexpr bool test_zero() } static_assert( test_zero() ); + +#ifdef __cpp_concepts +template + constexpr std::false_type + access_empty() { return {}; } + +template + requires (std::bool_constant<&std::array{}.at(0) != nullptr>::value) + constexpr std::true_type + access_empty() { return {}; } + +template + requires (std::bool_constant<&std::array{}[0] != nullptr>::value) + constexpr std::true_type + access_empty() { return {}; } + +template + requires (std::bool_constant<&std::array{}.front() != nullptr>::value) + constexpr std::true_type + access_empty() { return {}; } + +template + requires (std::bool_constant<&std::array{}.back() != nullptr>::value) + constexpr std::true_type + access_empty() { return {}; } + +static_assert( ! access_empty() ); + +template + constexpr std::false_type + access_past_the_end() { return {}; } + +template + requires (std::bool_constant{}.at(0) != nullptr>::value) + constexpr std::true_type + access_past_the_end() { return {}; } + +template + requires (std::bool_constant<&std::array{}[1] != nullptr>::value) + constexpr std::true_type + access_past_the_end() { return {}; } + +static_assert( ! access_past_the_end() ); +#endif diff --git a/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc b/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc index 19c91d28cd6..358ded47ad9 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc @@ -85,23 +85,39 @@ template access_empty() { return {}; } template - requires (std::bool_constant<(std::vector().at(0), true)>::value) + requires (std::bool_constant<&std::vector().at(0) != nullptr>::value) constexpr std::true_type access_empty() { return {}; } template - requires (std::bool_constant<(std::vector()[0], true)>::value) + requires (std::bool_constant<&std::vector()[0] != nullptr>::value) constexpr std::true_type access_empty() { return {}; } template - requires (std::bool_constant<(std::vector().front(), true)>::value) + requires (std::bool_constant<&std::vector().front() != nullptr>::value) constexpr std::true_type access_empty() { return {}; } template - requires (std::bool_constant<(std::vector().back(), true)>::value) + requires (std::bool_constant<&std::vector().back() != nullptr>::value) constexpr std::true_type access_empty() { return {}; } static_assert( ! access_empty() ); + +template + constexpr std::false_type + access_past_the_end() { return {}; } + +template + requires (std::bool_constant<&std::vector(3).at(3) != nullptr>::value) + constexpr std::true_type + access_past_the_end() { return {}; } + +template + requires (std::bool_constant<&std::vector(3)[3] != nullptr>::value) + constexpr std::true_type + access_past_the_end() { return {}; } + +static_assert( ! access_past_the_end() );