From 4b7d2e82dbc7a3c090acaa0bfca52ab2c7fa1c18 Mon Sep 17 00:00:00 2001 From: Konstantin Varlamov Date: Mon, 6 Nov 2023 08:53:14 -1000 Subject: [PATCH 1/6] [libc++][hardening] Categorize more 'valid-element-access' checks. --- libcxx/include/__algorithm/ranges_max.h | 5 +-- libcxx/include/__algorithm/ranges_min.h | 5 +-- libcxx/include/__algorithm/ranges_minmax.h | 5 +-- libcxx/include/__algorithm/sample.h | 8 +---- libcxx/include/__format/formatter_output.h | 6 ++-- .../include/__format/parser_std_format_spec.h | 6 ++-- libcxx/include/__iterator/common_iterator.h | 32 +++++++++---------- libcxx/include/__iterator/counted_iterator.h | 11 ++++--- libcxx/include/__ranges/subrange.h | 4 +-- libcxx/include/__ranges/view_interface.h | 10 +++--- .../include/__utility/is_pointer_in_range.h | 2 +- libcxx/include/experimental/__simd/vec_ext.h | 4 +-- libcxx/src/support/ibm/xlocale_zos.cpp | 2 +- .../alg.sorting/assert.min.max.pass.cpp | 2 +- 14 files changed, 51 insertions(+), 51 deletions(-) diff --git a/libcxx/include/__algorithm/ranges_max.h b/libcxx/include/__algorithm/ranges_max.h index 782ce2670f055..0f89cb2ff5bf2 100644 --- a/libcxx/include/__algorithm/ranges_max.h +++ b/libcxx/include/__algorithm/ranges_max.h @@ -54,7 +54,8 @@ struct __fn { indirect_strict_weak_order> _Comp = ranges::less> _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Tp operator()(initializer_list<_Tp> __il, _Comp __comp = {}, _Proj __proj = {}) const { - _LIBCPP_ASSERT_UNCATEGORIZED(__il.begin() != __il.end(), "initializer_list must contain at least one element"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( + __il.begin() != __il.end(), "initializer_list must contain at least one element"); auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool { return std::invoke(__comp, __rhs, __lhs); }; return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp_lhs_rhs_swapped, __proj); @@ -69,7 +70,7 @@ struct __fn { auto __first = ranges::begin(__r); auto __last = ranges::end(__r); - _LIBCPP_ASSERT_UNCATEGORIZED(__first != __last, "range must contain at least one element"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range must contain at least one element"); if constexpr (forward_range<_Rp> && !__is_cheap_to_copy>) { auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool { diff --git a/libcxx/include/__algorithm/ranges_min.h b/libcxx/include/__algorithm/ranges_min.h index be15b4536734d..8757358cdf37d 100644 --- a/libcxx/include/__algorithm/ranges_min.h +++ b/libcxx/include/__algorithm/ranges_min.h @@ -53,7 +53,8 @@ struct __fn { indirect_strict_weak_order> _Comp = ranges::less> _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Tp operator()(initializer_list<_Tp> __il, _Comp __comp = {}, _Proj __proj = {}) const { - _LIBCPP_ASSERT_UNCATEGORIZED(__il.begin() != __il.end(), "initializer_list must contain at least one element"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( + __il.begin() != __il.end(), "initializer_list must contain at least one element"); return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp, __proj); } @@ -65,7 +66,7 @@ struct __fn { operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const { auto __first = ranges::begin(__r); auto __last = ranges::end(__r); - _LIBCPP_ASSERT_UNCATEGORIZED(__first != __last, "range must contain at least one element"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range must contain at least one element"); if constexpr (forward_range<_Rp> && !__is_cheap_to_copy>) { return *ranges::__min_element_impl(__first, __last, __comp, __proj); } else { diff --git a/libcxx/include/__algorithm/ranges_minmax.h b/libcxx/include/__algorithm/ranges_minmax.h index a5b5cf9bd0ab9..22a62b620c936 100644 --- a/libcxx/include/__algorithm/ranges_minmax.h +++ b/libcxx/include/__algorithm/ranges_minmax.h @@ -65,7 +65,8 @@ struct __fn { indirect_strict_weak_order> _Comp = ranges::less> _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<_Type> operator()(initializer_list<_Type> __il, _Comp __comp = {}, _Proj __proj = {}) const { - _LIBCPP_ASSERT_UNCATEGORIZED(__il.begin() != __il.end(), "initializer_list has to contain at least one element"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( + __il.begin() != __il.end(), "initializer_list has to contain at least one element"); auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj); return ranges::minmax_result<_Type>{*__iters.first, *__iters.second}; } @@ -80,7 +81,7 @@ struct __fn { auto __last = ranges::end(__r); using _ValueT = range_value_t<_Range>; - _LIBCPP_ASSERT_UNCATEGORIZED(__first != __last, "range has to contain at least one element"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range has to contain at least one element"); if constexpr (forward_range<_Range>) { // Special-case the one element case. Avoid repeatedly initializing objects from the result of an iterator diff --git a/libcxx/include/__algorithm/sample.h b/libcxx/include/__algorithm/sample.h index cc29dd686f6be..6285b8aed1713 100644 --- a/libcxx/include/__algorithm/sample.h +++ b/libcxx/include/__algorithm/sample.h @@ -77,19 +77,13 @@ _LIBCPP_HIDE_FROM_ABI _SampleIterator __sample( return __output_iter; } -template _LIBCPP_HIDE_FROM_ABI _SampleIterator __sample( _PopulationIterator __first, _PopulationSentinel __last, _SampleIterator __output_iter, _Distance __n, _UniformRandomNumberGenerator& __g) { - _LIBCPP_ASSERT_UNCATEGORIZED(__n >= 0, "N must be a positive number."); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n >= 0, "N must be a positive number."); using _PopIterCategory = typename _IterOps<_AlgPolicy>::template __iterator_category<_PopulationIterator>; using _Difference = typename _IterOps<_AlgPolicy>::template __difference_type<_PopulationIterator>; diff --git a/libcxx/include/__format/formatter_output.h b/libcxx/include/__format/formatter_output.h index 89854f67f5fc1..31e06425703ae 100644 --- a/libcxx/include/__format/formatter_output.h +++ b/libcxx/include/__format/formatter_output.h @@ -246,7 +246,7 @@ __write(_Iterator __first, output_iterator&> auto __out_it, __format_spec::__parsed_specifications<_ParserCharT> __specs, ptrdiff_t __size) -> decltype(__out_it) { - _LIBCPP_ASSERT_UNCATEGORIZED(__first <= __last, "Not a valid range"); + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range"); return __formatter::__write(basic_string_view{__first, __last}, std::move(__out_it), __specs, __size); } @@ -259,7 +259,7 @@ __write(_Iterator __first, _Iterator __last, output_iterator&> auto __out_it, __format_spec::__parsed_specifications<_ParserCharT> __specs) -> decltype(__out_it) { - _LIBCPP_ASSERT_UNCATEGORIZED(__first <= __last, "Not a valid range"); + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range"); return __formatter::__write(__first, __last, std::move(__out_it), __specs, __last - __first); } @@ -273,7 +273,7 @@ _LIBCPP_HIDE_FROM_ABI auto __write_transformed( output_iterator auto __out_it, __format_spec::__parsed_specifications<_ParserCharT> __specs, _UnaryOperation __op) -> decltype(__out_it) { - _LIBCPP_ASSERT_UNCATEGORIZED(__first <= __last, "Not a valid range"); + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range"); ptrdiff_t __size = __last - __first; if (__size >= __specs.__width_) diff --git a/libcxx/include/__format/parser_std_format_spec.h b/libcxx/include/__format/parser_std_format_spec.h index 9a91179fdfb52..e38729db965c3 100644 --- a/libcxx/include/__format/parser_std_format_spec.h +++ b/libcxx/include/__format/parser_std_format_spec.h @@ -591,7 +591,7 @@ class _LIBCPP_TEMPLATE_VIS __parser { || (same_as<_CharT, wchar_t> && sizeof(wchar_t) == 2) # endif _LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end, bool __use_range_fill) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( __begin != __end, "when called with an empty input the function will cause " "undefined behavior by evaluating data not in the input"); @@ -624,7 +624,7 @@ class _LIBCPP_TEMPLATE_VIS __parser { template requires(same_as<_CharT, wchar_t> && sizeof(wchar_t) == 4) _LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end, bool __use_range_fill) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( __begin != __end, "when called with an empty input the function will cause " "undefined behavior by evaluating data not in the input"); @@ -652,7 +652,7 @@ class _LIBCPP_TEMPLATE_VIS __parser { // range-fill and tuple-fill are identical template _LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end, bool __use_range_fill) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( __begin != __end, "when called with an empty input the function will cause " "undefined behavior by evaluating data not in the input"); diff --git a/libcxx/include/__iterator/common_iterator.h b/libcxx/include/__iterator/common_iterator.h index cc49d62cd04dd..7b3f4610d5319 100644 --- a/libcxx/include/__iterator/common_iterator.h +++ b/libcxx/include/__iterator/common_iterator.h @@ -77,7 +77,7 @@ class common_iterator { requires convertible_to && convertible_to _LIBCPP_HIDE_FROM_ABI constexpr common_iterator(const common_iterator<_I2, _S2>& __other) : __hold_([&]() -> variant<_Iter, _Sent> { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !__other.__hold_.valueless_by_exception(), "Attempted to construct from a valueless common_iterator"); if (__other.__hold_.index() == 0) return variant<_Iter, _Sent>{in_place_index<0>, std::__unchecked_get<0>(__other.__hold_)}; @@ -88,7 +88,7 @@ class common_iterator { requires convertible_to && convertible_to && assignable_from<_Iter&, const _I2&> && assignable_from<_Sent&, const _S2&> _LIBCPP_HIDE_FROM_ABI common_iterator& operator=(const common_iterator<_I2, _S2>& __other) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !__other.__hold_.valueless_by_exception(), "Attempted to assign from a valueless common_iterator"); auto __idx = __hold_.index(); @@ -110,7 +110,7 @@ class common_iterator { } _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( std::holds_alternative<_Iter>(__hold_), "Attempted to dereference a non-dereferenceable common_iterator"); return *std::__unchecked_get<_Iter>(__hold_); } @@ -118,7 +118,7 @@ class common_iterator { _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const requires __dereferenceable { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( std::holds_alternative<_Iter>(__hold_), "Attempted to dereference a non-dereferenceable common_iterator"); return *std::__unchecked_get<_Iter>(__hold_); } @@ -129,7 +129,7 @@ class common_iterator { __i.operator->(); } || is_reference_v> || constructible_from, iter_reference_t<_I2>>) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( std::holds_alternative<_Iter>(__hold_), "Attempted to dereference a non-dereferenceable common_iterator"); if constexpr (is_pointer_v<_Iter> || requires(const _Iter& __i) { __i.operator->(); }) { return std::__unchecked_get<_Iter>(__hold_); @@ -142,14 +142,14 @@ class common_iterator { } _LIBCPP_HIDE_FROM_ABI common_iterator& operator++() { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( std::holds_alternative<_Iter>(__hold_), "Attempted to increment a non-dereferenceable common_iterator"); ++std::__unchecked_get<_Iter>(__hold_); return *this; } _LIBCPP_HIDE_FROM_ABI decltype(auto) operator++(int) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( std::holds_alternative<_Iter>(__hold_), "Attempted to increment a non-dereferenceable common_iterator"); if constexpr (forward_iterator<_Iter>) { auto __tmp = *this; @@ -170,9 +170,9 @@ class common_iterator { requires sentinel_for<_Sent, _I2> _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !__x.__hold_.valueless_by_exception(), "Attempted to compare a valueless common_iterator"); - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !__y.__hold_.valueless_by_exception(), "Attempted to compare a valueless common_iterator"); auto __x_index = __x.__hold_.index(); @@ -191,9 +191,9 @@ class common_iterator { requires sentinel_for<_Sent, _I2> && equality_comparable_with<_Iter, _I2> _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !__x.__hold_.valueless_by_exception(), "Attempted to compare a valueless common_iterator"); - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !__y.__hold_.valueless_by_exception(), "Attempted to compare a valueless common_iterator"); auto __x_index = __x.__hold_.index(); @@ -215,9 +215,9 @@ class common_iterator { requires sized_sentinel_for<_Sent, _I2> _LIBCPP_HIDE_FROM_ABI friend constexpr iter_difference_t<_I2> operator-(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !__x.__hold_.valueless_by_exception(), "Attempted to subtract from a valueless common_iterator"); - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !__y.__hold_.valueless_by_exception(), "Attempted to subtract a valueless common_iterator"); auto __x_index = __x.__hold_.index(); @@ -239,7 +239,7 @@ class common_iterator { iter_move(const common_iterator& __i) noexcept(noexcept(ranges::iter_move(std::declval()))) requires input_iterator<_Iter> { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( std::holds_alternative<_Iter>(__i.__hold_), "Attempted to iter_move a non-dereferenceable common_iterator"); return ranges::iter_move(std::__unchecked_get<_Iter>(__i.__hold_)); } @@ -248,9 +248,9 @@ class common_iterator { _LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) noexcept( noexcept(ranges::iter_swap(std::declval(), std::declval()))) { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( std::holds_alternative<_Iter>(__x.__hold_), "Attempted to iter_swap a non-dereferenceable common_iterator"); - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( std::holds_alternative<_I2>(__y.__hold_), "Attempted to iter_swap a non-dereferenceable common_iterator"); return ranges::iter_swap(std::__unchecked_get<_Iter>(__x.__hold_), std::__unchecked_get<_I2>(__y.__hold_)); } diff --git a/libcxx/include/__iterator/counted_iterator.h b/libcxx/include/__iterator/counted_iterator.h index c72ac677ff2f8..008c52fa87ce0 100644 --- a/libcxx/include/__iterator/counted_iterator.h +++ b/libcxx/include/__iterator/counted_iterator.h @@ -105,14 +105,14 @@ class counted_iterator _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Iter> count() const noexcept { return __count_; } _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() { - _LIBCPP_ASSERT_UNCATEGORIZED(__count_ > 0, "Iterator is equal to or past end."); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count_ > 0, "Iterator is equal to or past end."); return *__current_; } _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const requires __dereferenceable { - _LIBCPP_ASSERT_UNCATEGORIZED(__count_ > 0, "Iterator is equal to or past end."); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count_ > 0, "Iterator is equal to or past end."); return *__current_; } @@ -229,7 +229,7 @@ class counted_iterator _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](iter_difference_t<_Iter> __n) const requires random_access_iterator<_Iter> { - _LIBCPP_ASSERT_UNCATEGORIZED(__n < __count_, "Subscript argument must be less than size."); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < __count_, "Subscript argument must be less than size."); return __current_[__n]; } @@ -253,7 +253,7 @@ class counted_iterator iter_move(const counted_iterator& __i) noexcept(noexcept(ranges::iter_move(__i.__current_))) requires input_iterator<_Iter> { - _LIBCPP_ASSERT_UNCATEGORIZED(__i.__count_ > 0, "Iterator must not be past end of range."); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i.__count_ > 0, "Iterator must not be past end of range."); return ranges::iter_move(__i.__current_); } @@ -261,7 +261,8 @@ class counted_iterator _LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(const counted_iterator& __x, const counted_iterator<_I2>& __y) noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_))) { - _LIBCPP_ASSERT_UNCATEGORIZED(__x.__count_ > 0 && __y.__count_ > 0, "Iterators must not be past end of range."); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( + __x.__count_ > 0 && __y.__count_ > 0, "Iterators must not be past end of range."); return ranges::iter_swap(__x.__current_, __y.__current_); } diff --git a/libcxx/include/__ranges/subrange.h b/libcxx/include/__ranges/subrange.h index a7a3c1efa70e5..a41978275b787 100644 --- a/libcxx/include/__ranges/subrange.h +++ b/libcxx/include/__ranges/subrange.h @@ -101,8 +101,8 @@ class _LIBCPP_TEMPLATE_VIS subrange : public view_interface) - _LIBCPP_ASSERT_UNCATEGORIZED((__end_ - __begin_) == static_cast>(__n), - "std::ranges::subrange was passed an invalid size hint"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS((__end_ - __begin_) == static_cast>(__n), + "std::ranges::subrange was passed an invalid size hint"); } template <__different_from _Range> diff --git a/libcxx/include/__ranges/view_interface.h b/libcxx/include/__ranges/view_interface.h index 3216e0bd6ff20..84dd1c316de37 100644 --- a/libcxx/include/__ranges/view_interface.h +++ b/libcxx/include/__ranges/view_interface.h @@ -109,7 +109,7 @@ class view_interface { _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) front() requires forward_range<_D2> { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !empty(), "Precondition `!empty()` not satisfied. `.front()` called on an empty view."); return *ranges::begin(__derived()); } @@ -118,7 +118,7 @@ class view_interface { _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) front() const requires forward_range { - _LIBCPP_ASSERT_UNCATEGORIZED( + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( !empty(), "Precondition `!empty()` not satisfied. `.front()` called on an empty view."); return *ranges::begin(__derived()); } @@ -127,7 +127,8 @@ class view_interface { _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) back() requires bidirectional_range<_D2> && common_range<_D2> { - _LIBCPP_ASSERT_UNCATEGORIZED(!empty(), "Precondition `!empty()` not satisfied. `.back()` called on an empty view."); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( + !empty(), "Precondition `!empty()` not satisfied. `.back()` called on an empty view."); return *ranges::prev(ranges::end(__derived())); } @@ -135,7 +136,8 @@ class view_interface { _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) back() const requires bidirectional_range && common_range { - _LIBCPP_ASSERT_UNCATEGORIZED(!empty(), "Precondition `!empty()` not satisfied. `.back()` called on an empty view."); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( + !empty(), "Precondition `!empty()` not satisfied. `.back()` called on an empty view."); return *ranges::prev(ranges::end(__derived())); } diff --git a/libcxx/include/__utility/is_pointer_in_range.h b/libcxx/include/__utility/is_pointer_in_range.h index e859562e7457a..68cdfea6f9452 100644 --- a/libcxx/include/__utility/is_pointer_in_range.h +++ b/libcxx/include/__utility/is_pointer_in_range.h @@ -35,7 +35,7 @@ template > { _Tp __data __attribute__((__vector_size__(std::__bit_ceil((sizeof(_Tp) * _Np))))); _LIBCPP_HIDE_FROM_ABI _Tp __get(size_t __idx) const noexcept { - _LIBCPP_ASSERT_UNCATEGORIZED(__idx >= 0 && __idx < _Np, "Index is out of bounds"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx >= 0 && __idx < _Np, "Index is out of bounds"); return __data[__idx]; } _LIBCPP_HIDE_FROM_ABI void __set(size_t __idx, _Tp __v) noexcept { - _LIBCPP_ASSERT_UNCATEGORIZED(__idx >= 0 && __idx < _Np, "Index is out of bounds"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx >= 0 && __idx < _Np, "Index is out of bounds"); __data[__idx] = __v; } }; diff --git a/libcxx/src/support/ibm/xlocale_zos.cpp b/libcxx/src/support/ibm/xlocale_zos.cpp index 4c20997b4eb79..9a90e08e11cf9 100644 --- a/libcxx/src/support/ibm/xlocale_zos.cpp +++ b/libcxx/src/support/ibm/xlocale_zos.cpp @@ -103,7 +103,7 @@ locale_t uselocale(locale_t newloc) { tokenized.push_back(s); } - _LIBCPP_ASSERT_UNCATEGORIZED(tokenized.size() >= _NCAT, "locale-name list is too short"); + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(tokenized.size() >= _NCAT, "locale-name list is too short"); previous_loc->lc_collate = tokenized[LC_COLLATE]; previous_loc->lc_ctype = tokenized[LC_CTYPE]; diff --git a/libcxx/test/libcxx/algorithms/alg.sorting/assert.min.max.pass.cpp b/libcxx/test/libcxx/algorithms/alg.sorting/assert.min.max.pass.cpp index b23b4d4530eec..bd9dfd4549c4e 100644 --- a/libcxx/test/libcxx/algorithms/alg.sorting/assert.min.max.pass.cpp +++ b/libcxx/test/libcxx/algorithms/alg.sorting/assert.min.max.pass.cpp @@ -10,7 +10,7 @@ // REQUIRES: has-unix-headers // UNSUPPORTED: c++03, c++11, c++14, c++17 -// REQUIRES: libcpp-hardening-mode={{extensive|debug}} +// UNSUPPORTED: libcpp-hardening-mode=none // XFAIL: availability-verbose_abort-missing #include From 6e801584dc405368b1f705a7d1d3a596ce44949d Mon Sep 17 00:00:00 2001 From: Konstantin Varlamov Date: Fri, 15 Dec 2023 16:10:03 -0800 Subject: [PATCH 2/6] Address feedback (test iterator operations) --- .../counted.iterator/assert.pass.cpp | 42 ++++++++++++++ .../iterators.common/assert.pass.cpp | 57 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp create mode 100644 libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp diff --git a/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp b/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp new file mode 100644 index 0000000000000..0c154b7b76d7b --- /dev/null +++ b/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03 +// UNSUPPORTED: libcpp-hardening-mode=unchecked +// XFAIL: availability-verbose_abort-missing + +#include + +#include "check_assertion.h" +#include "test_iterators.h" + +int main(int, char**) { + using Iter = std::counted_iterator; + int a[] = {1, 2, 3}; + Iter valid_i(a, 1); + + { + Iter i; + + TEST_LIBCPP_ASSERT_FAILURE(*i, "Iterator is equal to or past end."); + TEST_LIBCPP_ASSERT_FAILURE(i[999], "Subscript argument must be less than size."); + TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_move(i), "Iterator must not be past end of range."); + TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(i, valid_i), "Iterators must not be past end of range."); + TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(valid_i, i), "Iterators must not be past end of range."); + std::ranges::iter_swap(valid_i, valid_i); // Ok + } + + { // Check the `const` overload of `operator*`. + const Iter i; + + TEST_LIBCPP_ASSERT_FAILURE(*i, "Iterator is equal to or past end."); + } + + return 0; +} diff --git a/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp b/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp new file mode 100644 index 0000000000000..d81b13efe8e32 --- /dev/null +++ b/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03 +// UNSUPPORTED: libcpp-hardening-mode=unchecked +// XFAIL: availability-verbose_abort-missing + +#include + +#include "check_assertion.h" +#include "test_iterators.h" + +int main(int, char**) { + using Iter = std::common_iterator>; + int a[] = {1, 2, 3}; + sentinel_wrapper s; + Iter valid_i = a; + + { + Iter i = s; + + TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable common_iterator"); + + TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-dereferenceable common_iterator"); + TEST_LIBCPP_ASSERT_FAILURE(i++, "Attempted to increment a non-dereferenceable common_iterator"); + + TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_move(i), "Attempted to iter_move a non-dereferenceable common_iterator"); + + TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(i, valid_i), "Attempted to iter_swap a non-dereferenceable common_iterator"); + TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(valid_i, i), "Attempted to iter_swap a non-dereferenceable common_iterator"); + std::ranges::iter_swap(valid_i, valid_i); // Ok + } + + { // Check the `const` overload of `operator*`. + const Iter i = s; + TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable common_iterator"); + } + + { // Check `operator->`. + struct Foo { + int x = 0; + }; + + std::common_iterator> i = sentinel_wrapper(); + TEST_LIBCPP_ASSERT_FAILURE(i->x, "Attempted to dereference a non-dereferenceable common_iterator"); + } + + // TODO: check `valueless_by_exception + + return 0; +} From aca4fd38c1e1af977388ed12862cc6a189a69d33 Mon Sep 17 00:00:00 2001 From: Konstantin Varlamov Date: Fri, 15 Dec 2023 16:25:40 -0800 Subject: [PATCH 3/6] Quick fix to new tests --- .../counted.iterator/assert.pass.cpp | 4 ++-- .../iterators.common/assert.pass.cpp | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp b/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp index 0c154b7b76d7b..92f3b998abadd 100644 --- a/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp +++ b/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// // REQUIRES: has-unix-headers -// UNSUPPORTED: c++03 +// UNSUPPORTED: c++03, c++11, c++14, c++17 // UNSUPPORTED: libcpp-hardening-mode=unchecked // XFAIL: availability-verbose_abort-missing @@ -18,7 +18,7 @@ int main(int, char**) { using Iter = std::counted_iterator; - int a[] = {1, 2, 3}; + int a[] = {1, 2, 3}; Iter valid_i(a, 1); { diff --git a/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp b/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp index d81b13efe8e32..1097cf9e71549 100644 --- a/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp +++ b/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// // REQUIRES: has-unix-headers -// UNSUPPORTED: c++03 +// UNSUPPORTED: c++03, c++11, c++14, c++17 // UNSUPPORTED: libcpp-hardening-mode=unchecked // XFAIL: availability-verbose_abort-missing @@ -18,7 +18,7 @@ int main(int, char**) { using Iter = std::common_iterator>; - int a[] = {1, 2, 3}; + int a[] = {1, 2, 3}; sentinel_wrapper s; Iter valid_i = a; @@ -30,10 +30,13 @@ int main(int, char**) { TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-dereferenceable common_iterator"); TEST_LIBCPP_ASSERT_FAILURE(i++, "Attempted to increment a non-dereferenceable common_iterator"); - TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_move(i), "Attempted to iter_move a non-dereferenceable common_iterator"); + TEST_LIBCPP_ASSERT_FAILURE( + std::ranges::iter_move(i), "Attempted to iter_move a non-dereferenceable common_iterator"); - TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(i, valid_i), "Attempted to iter_swap a non-dereferenceable common_iterator"); - TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(valid_i, i), "Attempted to iter_swap a non-dereferenceable common_iterator"); + TEST_LIBCPP_ASSERT_FAILURE( + std::ranges::iter_swap(i, valid_i), "Attempted to iter_swap a non-dereferenceable common_iterator"); + TEST_LIBCPP_ASSERT_FAILURE( + std::ranges::iter_swap(valid_i, i), "Attempted to iter_swap a non-dereferenceable common_iterator"); std::ranges::iter_swap(valid_i, valid_i); // Ok } From 1864bb1feaeb2fa18372aa3bc0fba744bc6ef287 Mon Sep 17 00:00:00 2001 From: Konstantin Varlamov Date: Tue, 19 Dec 2023 01:48:39 -0800 Subject: [PATCH 4/6] Fix bad merge --- libcxx/include/__algorithm/sample.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libcxx/include/__algorithm/sample.h b/libcxx/include/__algorithm/sample.h index 6285b8aed1713..ebe5180b7eeca 100644 --- a/libcxx/include/__algorithm/sample.h +++ b/libcxx/include/__algorithm/sample.h @@ -77,6 +77,12 @@ _LIBCPP_HIDE_FROM_ABI _SampleIterator __sample( return __output_iter; } +template _LIBCPP_HIDE_FROM_ABI _SampleIterator __sample( _PopulationIterator __first, _PopulationSentinel __last, From 38f0ba99a0addff07d2d09b4ce372b9cd4d70f61 Mon Sep 17 00:00:00 2001 From: Konstantin Varlamov Date: Tue, 19 Dec 2023 02:18:28 -0800 Subject: [PATCH 5/6] Fix the name of the mode in tests --- .../iterators/predef.iterators/counted.iterator/assert.pass.cpp | 2 +- .../iterators/predef.iterators/iterators.common/assert.pass.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp b/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp index 92f3b998abadd..f803b2cad75be 100644 --- a/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp +++ b/libcxx/test/libcxx/iterators/predef.iterators/counted.iterator/assert.pass.cpp @@ -8,7 +8,7 @@ // REQUIRES: has-unix-headers // UNSUPPORTED: c++03, c++11, c++14, c++17 -// UNSUPPORTED: libcpp-hardening-mode=unchecked +// UNSUPPORTED: libcpp-hardening-mode=none // XFAIL: availability-verbose_abort-missing #include diff --git a/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp b/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp index 1097cf9e71549..aae90c24018b5 100644 --- a/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp +++ b/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp @@ -8,7 +8,7 @@ // REQUIRES: has-unix-headers // UNSUPPORTED: c++03, c++11, c++14, c++17 -// UNSUPPORTED: libcpp-hardening-mode=unchecked +// UNSUPPORTED: libcpp-hardening-mode=none // XFAIL: availability-verbose_abort-missing #include From 4946f4045611085832a8846ccf6559504f573154 Mon Sep 17 00:00:00 2001 From: Konstantin Varlamov Date: Wed, 20 Dec 2023 17:12:43 -0800 Subject: [PATCH 6/6] Remove a TODO --- .../iterators/predef.iterators/iterators.common/assert.pass.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp b/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp index aae90c24018b5..ea4574fc1a9cc 100644 --- a/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp +++ b/libcxx/test/libcxx/iterators/predef.iterators/iterators.common/assert.pass.cpp @@ -54,7 +54,5 @@ int main(int, char**) { TEST_LIBCPP_ASSERT_FAILURE(i->x, "Attempted to dereference a non-dereferenceable common_iterator"); } - // TODO: check `valueless_by_exception - return 0; }