[committed,v2] libstdc++: Define feature test macros for hardened implementation
Checks
Commit Message
The P3471R4 and P3697R1 proposals adding hardened preconditions also
defined feature test macros that indicate whether the hardened
preconditions are checked. For our implementation, those checks are
enabled by defining _GLIBCXX_ASSERTIONS.
To implement this, I've added all the __cpp_lib_hardened_xxx macros at
the end of bits/version.def, because the simplest way to define many of
them seems to be to make the depend on the macro for the main feature
and additionally depend on defined(_GLIBCXX_ASSERTIONS).
The hardening checks were added to each component as follows:
std::array r11-4854-g6db082477ad839
std::bitset r16-7919-g1b404c574450a4
std::deque r7-1526-gbd2ee798d5a5a3
std::expected since first commit
std::forward_list r15-5721-ge7aa614d7372b5
std::inplace_vector since first commit
std::common_iterator since first commit
std::counted_iterator since first commit
std::list r15-5721-ge7aa614d7372b5
std::shared_ptr<T[]> since first commit
std::optional r8-711-g2c27a627a31034
std::ranges::view_interface since first commit
std::span r10-2983-gb5c433ce11a140
std::basic_stacktrace since first commit
std::basic_string r6-3197-g2f1e8e7c4730bb
std::basic_string_view r11-2881-g3eefb302d2bd85
std::valarray r6-3197-g2f1e8e7c4730bb
std::vector r6-3197-g2f1e8e7c4730bb
This commit does not define __cpp_lib_hardened_mdspan, that was added in
r17-1258-g19def756dec1fe earlier today.
This commit does not include the LWG 4577 changes to harden
view_interface::operator[] which will be added later (with a new value
for the __cpp_lib_hardened_view_interface macro).
libstdc++-v3/ChangeLog:
* include/bits/version.def: Define macros for hardened
preconditions.
* include/bits/version.h: Regenerate.
* include/std/array: Define "want" macro for hardened
precondition macro.
* include/std/bitset: Likewise.
* include/std/deque: Likewise.
* include/std/expected: Likewise.
* include/std/forward_list: Likewise.
* include/std/inplace_vector: Likewise.
* include/std/iterator: Likewise.
* include/std/list: Likewise.
* include/std/memory: Likewise.
* include/std/optional: Likewise.
* include/std/ranges: Likewise.
* include/std/span: Likewise.
* include/std/stacktrace: Likewise.
* include/std/string: Likewise.
* include/std/string_view: Likewise.
* include/std/valarray: Likewise.
* include/std/vector: Likewise.
* testsuite/23_containers/array/tuple_interface/get_neg.cc:
Adjust dg-error line numbers.
Reviewed-by: Nathan Myers <nmyers@redhat.com>
Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
---
v2: dropped mdspan macro, more detail in commit message.
Tested x86_64-linux. Pushed to trunk.
libstdc++-v3/include/bits/version.def | 150 ++++++++++++++-
libstdc++-v3/include/bits/version.h | 180 ++++++++++++++++++
libstdc++-v3/include/std/array | 1 +
libstdc++-v3/include/std/bitset | 1 +
libstdc++-v3/include/std/deque | 1 +
libstdc++-v3/include/std/expected | 1 +
libstdc++-v3/include/std/forward_list | 1 +
libstdc++-v3/include/std/inplace_vector | 1 +
libstdc++-v3/include/std/iterator | 2 +
libstdc++-v3/include/std/list | 1 +
libstdc++-v3/include/std/memory | 1 +
libstdc++-v3/include/std/optional | 1 +
libstdc++-v3/include/std/ranges | 1 +
libstdc++-v3/include/std/span | 1 +
libstdc++-v3/include/std/stacktrace | 1 +
libstdc++-v3/include/std/string | 1 +
libstdc++-v3/include/std/string_view | 1 +
libstdc++-v3/include/std/valarray | 1 +
libstdc++-v3/include/std/vector | 1 +
.../array/tuple_interface/get_neg.cc | 6 +-
20 files changed, 350 insertions(+), 4 deletions(-)
@@ -2448,7 +2448,107 @@ ftms = {
name = valarray;
values = {
v = 202511;
- cxxmin = 26;
+ cxxmin = 26;
+ };
+};
+
+ftms = {
+ name = hardened_array;
+ values = {
+ v = 202502;
+ cxxmin = 11;
+ extra_cond = "defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_basic_stacktrace;
+ values = {
+ v = 202502;
+ cxxmin = 23;
+ hosted = yes;
+ extra_cond = "defined(__glibcxx_stacktrace) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_basic_string;
+ values = {
+ v = 202502;
+ extra_cond = "defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_basic_string_view;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_string_view) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_bitset;
+ values = {
+ v = 202502;
+ extra_cond = "defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_common_iterator;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_ranges) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_counted_iterator;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_ranges) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_deque;
+ values = {
+ v = 202502;
+ extra_cond = "defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_expected;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_expected) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_forward_list;
+ values = {
+ v = 202502;
+ cxxmin = 11;
+ extra_cond = "defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_inplace_vector;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_inplace_vector) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_list;
+ values = {
+ v = 202502;
+ extra_cond = "defined(_GLIBCXX_ASSERTIONS)";
};
};
@@ -2460,6 +2560,54 @@ ftms = {
};
};
+ftms = {
+ name = hardened_optional;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_optional) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_shared_ptr_array;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_shared_ptr_arrays) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_span;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_span) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_valarray;
+ values = {
+ v = 202502;
+ extra_cond = "defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_vector;
+ values = {
+ v = 202502;
+ extra_cond = "defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
+ftms = {
+ name = hardened_view_interface;
+ values = {
+ v = 202502;
+ extra_cond = "defined(__glibcxx_ranges) && defined(_GLIBCXX_ASSERTIONS)";
+ };
+};
+
// Standard test specifications.
stds[97] = ">= 199711L";
stds[03] = ">= 199711L";
@@ -2710,6 +2710,126 @@
#endif /* !defined(__cpp_lib_valarray) */
#undef __glibcxx_want_valarray
+#if !defined(__cpp_lib_hardened_array)
+# if (__cplusplus >= 201103L) && (defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_array 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_array)
+# define __cpp_lib_hardened_array 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_array) */
+#undef __glibcxx_want_hardened_array
+
+#if !defined(__cpp_lib_hardened_basic_stacktrace)
+# if (__cplusplus >= 202100L) && _GLIBCXX_HOSTED && (defined(__glibcxx_stacktrace) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_basic_stacktrace 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_basic_stacktrace)
+# define __cpp_lib_hardened_basic_stacktrace 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_basic_stacktrace) */
+#undef __glibcxx_want_hardened_basic_stacktrace
+
+#if !defined(__cpp_lib_hardened_basic_string)
+# if (defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_basic_string 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_basic_string)
+# define __cpp_lib_hardened_basic_string 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_basic_string) */
+#undef __glibcxx_want_hardened_basic_string
+
+#if !defined(__cpp_lib_hardened_basic_string_view)
+# if (defined(__glibcxx_string_view) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_basic_string_view 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_basic_string_view)
+# define __cpp_lib_hardened_basic_string_view 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_basic_string_view) */
+#undef __glibcxx_want_hardened_basic_string_view
+
+#if !defined(__cpp_lib_hardened_bitset)
+# if (defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_bitset 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_bitset)
+# define __cpp_lib_hardened_bitset 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_bitset) */
+#undef __glibcxx_want_hardened_bitset
+
+#if !defined(__cpp_lib_hardened_common_iterator)
+# if (defined(__glibcxx_ranges) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_common_iterator 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_common_iterator)
+# define __cpp_lib_hardened_common_iterator 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_common_iterator) */
+#undef __glibcxx_want_hardened_common_iterator
+
+#if !defined(__cpp_lib_hardened_counted_iterator)
+# if (defined(__glibcxx_ranges) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_counted_iterator 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_counted_iterator)
+# define __cpp_lib_hardened_counted_iterator 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_counted_iterator) */
+#undef __glibcxx_want_hardened_counted_iterator
+
+#if !defined(__cpp_lib_hardened_deque)
+# if (defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_deque 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_deque)
+# define __cpp_lib_hardened_deque 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_deque) */
+#undef __glibcxx_want_hardened_deque
+
+#if !defined(__cpp_lib_hardened_expected)
+# if (defined(__glibcxx_expected) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_expected 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_expected)
+# define __cpp_lib_hardened_expected 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_expected) */
+#undef __glibcxx_want_hardened_expected
+
+#if !defined(__cpp_lib_hardened_forward_list)
+# if (__cplusplus >= 201103L) && (defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_forward_list 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_forward_list)
+# define __cpp_lib_hardened_forward_list 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_forward_list) */
+#undef __glibcxx_want_hardened_forward_list
+
+#if !defined(__cpp_lib_hardened_inplace_vector)
+# if (defined(__glibcxx_inplace_vector) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_inplace_vector 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_inplace_vector)
+# define __cpp_lib_hardened_inplace_vector 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_inplace_vector) */
+#undef __glibcxx_want_hardened_inplace_vector
+
+#if !defined(__cpp_lib_hardened_list)
+# if (defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_list 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_list)
+# define __cpp_lib_hardened_list 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_list) */
+#undef __glibcxx_want_hardened_list
+
#if !defined(__cpp_lib_hardened_mdspan)
# if (defined(__glibcxx_mdspan) && defined(_GLIBCXX_ASSERTIONS))
# define __glibcxx_hardened_mdspan 202502L
@@ -2720,4 +2840,64 @@
#endif /* !defined(__cpp_lib_hardened_mdspan) */
#undef __glibcxx_want_hardened_mdspan
+#if !defined(__cpp_lib_hardened_optional)
+# if (defined(__glibcxx_optional) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_optional 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_optional)
+# define __cpp_lib_hardened_optional 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_optional) */
+#undef __glibcxx_want_hardened_optional
+
+#if !defined(__cpp_lib_hardened_shared_ptr_array)
+# if (defined(__glibcxx_shared_ptr_arrays) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_shared_ptr_array 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_shared_ptr_array)
+# define __cpp_lib_hardened_shared_ptr_array 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_shared_ptr_array) */
+#undef __glibcxx_want_hardened_shared_ptr_array
+
+#if !defined(__cpp_lib_hardened_span)
+# if (defined(__glibcxx_span) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_span 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_span)
+# define __cpp_lib_hardened_span 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_span) */
+#undef __glibcxx_want_hardened_span
+
+#if !defined(__cpp_lib_hardened_valarray)
+# if (defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_valarray 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_valarray)
+# define __cpp_lib_hardened_valarray 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_valarray) */
+#undef __glibcxx_want_hardened_valarray
+
+#if !defined(__cpp_lib_hardened_vector)
+# if (defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_vector 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_vector)
+# define __cpp_lib_hardened_vector 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_vector) */
+#undef __glibcxx_want_hardened_vector
+
+#if !defined(__cpp_lib_hardened_view_interface)
+# if (defined(__glibcxx_ranges) && defined(_GLIBCXX_ASSERTIONS))
+# define __glibcxx_hardened_view_interface 202502L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_hardened_view_interface)
+# define __cpp_lib_hardened_view_interface 202502L
+# endif
+# endif
+#endif /* !defined(__cpp_lib_hardened_view_interface) */
+#undef __glibcxx_want_hardened_view_interface
+
#undef __glibcxx_want_all
@@ -49,6 +49,7 @@
#define __glibcxx_want_array_constexpr
#define __glibcxx_want_freestanding_array
+#define __glibcxx_want_hardened_array
#define __glibcxx_want_nonmember_container_access
#define __glibcxx_want_to_array
#include <bits/version.h>
@@ -62,6 +62,7 @@
#define __glibcxx_want_constexpr_bitset
#define __glibcxx_want_bitset // ...construct from string_view
+#define __glibcxx_want_hardened_bitset
#include <bits/version.h>
#ifdef __cpp_lib_bitset // ...construct from string_view
@@ -75,6 +75,7 @@
#define __glibcxx_want_allocator_traits_is_always_equal
#define __glibcxx_want_containers_ranges
#define __glibcxx_want_erase_if
+#define __glibcxx_want_hardened_deque
#define __glibcxx_want_nonmember_container_access
#include <bits/version.h>
@@ -37,6 +37,7 @@
#define __glibcxx_want_freestanding_expected
#define __glibcxx_want_constrained_equality
#define __glibcxx_want_constexpr_exceptions
+#define __glibcxx_want_hardened_expected
#include <bits/version.h>
#ifdef __cpp_lib_expected // C++ >= 23 && __cpp_concepts >= 202002L
@@ -51,6 +51,7 @@
#define __glibcxx_want_allocator_traits_is_always_equal
#define __glibcxx_want_containers_ranges
#define __glibcxx_want_erase_if
+#define __glibcxx_want_hardened_forward_list
#define __glibcxx_want_incomplete_container_elements
#define __glibcxx_want_list_remove_return_type
#define __glibcxx_want_nonmember_container_access
@@ -32,6 +32,7 @@
#pragma GCC system_header
+#define __glibcxx_want_hardened_inplace_vector
#define __glibcxx_want_inplace_vector
#include <bits/version.h>
@@ -71,6 +71,8 @@
#define __glibcxx_want_array_constexpr
#define __glibcxx_want_constexpr_iterator
+#define __glibcxx_want_hardened_common_iterator
+#define __glibcxx_want_hardened_counted_iterator
#define __glibcxx_want_make_reverse_iterator
#define __glibcxx_want_move_iterator_concept
#define __glibcxx_want_nonmember_container_access
@@ -75,6 +75,7 @@
#define __glibcxx_want_allocator_traits_is_always_equal
#define __glibcxx_want_containers_ranges
#define __glibcxx_want_erase_if
+#define __glibcxx_want_hardened_list
#define __glibcxx_want_incomplete_container_elements
#define __glibcxx_want_list_remove_return_type
#define __glibcxx_want_nonmember_container_access
@@ -110,6 +110,7 @@
#define __glibcxx_want_constexpr_dynamic_alloc
#define __glibcxx_want_constexpr_memory
#define __glibcxx_want_enable_shared_from_this
+#define __glibcxx_want_hardened_shared_ptr_array
#define __glibcxx_want_indirect
#define __glibcxx_want_is_sufficiently_aligned
#define __glibcxx_want_make_unique
@@ -39,6 +39,7 @@
#define __glibcxx_want_optional_range_support
#define __glibcxx_want_constrained_equality
#define __glibcxx_want_constexpr_exceptions
+#define __glibcxx_want_hardened_optional
#include <bits/version.h>
#ifdef __cpp_lib_optional // C++ >= 17
@@ -56,6 +56,7 @@
#include <bits/refwrap.h>
#define __glibcxx_want_algorithm_default_value_type
+#define __glibcxx_want_hardened_view_interface
#define __glibcxx_want_ranges
#define __glibcxx_want_ranges_as_const
#define __glibcxx_want_ranges_as_rvalue
@@ -40,6 +40,7 @@
#define __glibcxx_want_span
#define __glibcxx_want_span_initializer_list
+#define __glibcxx_want_hardened_span
#include <bits/version.h>
#ifdef __cpp_lib_span // C++ >= 20 && concepts
@@ -34,6 +34,7 @@
#define __glibcxx_want_stacktrace
#define __glibcxx_want_formatters
+#define __glibcxx_want_hardened_basic_stacktrace
#include <bits/version.h>
#ifdef __cpp_lib_stacktrace // C++ >= 23 && hosted && HAVE_STACKTRACE
@@ -72,6 +72,7 @@
#define __glibcxx_want_constexpr_string
#define __glibcxx_want_containers_ranges
#define __glibcxx_want_erase_if
+#define __glibcxx_want_hardened_basic_string
#define __glibcxx_want_nonmember_container_access
#define __glibcxx_want_string_resize_and_overwrite
#define __glibcxx_want_string_subview
@@ -40,6 +40,7 @@
#define __glibcxx_want_constexpr_char_traits
#define __glibcxx_want_constexpr_string_view
#define __glibcxx_want_freestanding_string_view
+#define __glibcxx_want_hardened_basic_string_view
#define __glibcxx_want_starts_ends_with
#define __glibcxx_want_string_contains
#define __glibcxx_want_string_subview
@@ -46,6 +46,7 @@
#include <bits/range_access.h>
#endif
+#define __glibcxx_want_hardened_valarray
#define __glibcxx_want_valarray
#include <bits/version.h>
@@ -83,6 +83,7 @@
#define __glibcxx_want_constexpr_vector
#define __glibcxx_want_containers_ranges
#define __glibcxx_want_erase_if
+#define __glibcxx_want_hardened_vector
#define __glibcxx_want_incomplete_container_elements
#define __glibcxx_want_nonmember_container_access
#include <bits/version.h>
@@ -26,6 +26,6 @@ int n1 = std::get<1>(a);
int n2 = std::get<1>(std::move(a));
int n3 = std::get<1>(ca);
-// { dg-error "static assertion failed" "" { target *-*-* } 396 }
-// { dg-error "static assertion failed" "" { target *-*-* } 405 }
-// { dg-error "static assertion failed" "" { target *-*-* } 414 }
+// { dg-error "static assertion failed" "" { target *-*-* } 397 }
+// { dg-error "static assertion failed" "" { target *-*-* } 406 }
+// { dg-error "static assertion failed" "" { target *-*-* } 415 }