[libc++][hardening] Add an ABI macro _LIBCPP_ABI_BOUNDED_ITERATORS.

Use the new macro instead of `_LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING`.

Differential Revision: https://reviews.llvm.org/D153895
This commit is contained in:
varconst 2023-06-27 16:41:02 -07:00
parent 7075f9d926
commit be02f912d6
4 changed files with 26 additions and 11 deletions

View File

@ -86,6 +86,8 @@
// ... add new file formats here ...
# endif
// ABI {
# if _LIBCPP_ABI_VERSION >= 2
// Change short string representation so that string data starts at offset 0,
// improving its alignment in some cases.
@ -191,6 +193,19 @@
# define _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
# endif
// Changes the iterator type of select containers (see below) to a bounded iterator that keeps track of whether it's
// within the bounds of the original container and asserts it on every dereference.
//
// ABI impact: changes the iterator type of the relevant containers.
//
// Supported containers:
// - `span`;
// - `string_view`;
// - `array`.
// #define _LIBCPP_ABI_BOUNDED_ITERATORS
// } ABI
# define _LIBCPP_TOSTRING2(x) #x
# define _LIBCPP_TOSTRING(x) _LIBCPP_TOSTRING2(x)

View File

@ -27,8 +27,8 @@
# define _LIBCPP_DEBUG_STRICT_WEAK_ORDERING_CHECK
#endif
#if defined(_LIBCPP_ENABLE_DEBUG_MODE) && !defined(_LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING)
# define _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#if defined(_LIBCPP_ENABLE_DEBUG_MODE) && !defined(_LIBCPP_ABI_BOUNDED_ITERATORS)
# define _LIBCPP_ABI_BOUNDED_ITERATORS
#endif
#ifdef _LIBCPP_ENABLE_DEBUG_MODE

View File

@ -214,7 +214,7 @@ public:
using const_pointer = const _Tp *;
using reference = _Tp &;
using const_reference = const _Tp &;
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
using iterator = __bounded_iter<pointer>;
#else
using iterator = __wrap_iter<pointer>;
@ -359,14 +359,14 @@ public:
// [span.iter], span iterator support
_LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
return std::__make_bounded_iter(data(), data(), data() + size());
#else
return iterator(this, data());
#endif
}
_LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
return std::__make_bounded_iter(data() + size(), data(), data() + size());
#else
return iterator(this, data() + size());
@ -398,7 +398,7 @@ public:
using const_pointer = const _Tp *;
using reference = _Tp &;
using const_reference = const _Tp &;
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
using iterator = __bounded_iter<pointer>;
#else
using iterator = __wrap_iter<pointer>;
@ -525,14 +525,14 @@ public:
// [span.iter], span iterator support
_LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
return std::__make_bounded_iter(data(), data(), data() + size());
#else
return iterator(this, data());
#endif
}
_LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
return std::__make_bounded_iter(data() + size(), data(), data() + size());
#else
return iterator(this, data() + size());

View File

@ -274,7 +274,7 @@ public:
using const_pointer = const _CharT*;
using reference = _CharT&;
using const_reference = const _CharT&;
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
using const_iterator = __bounded_iter<const_pointer>;
#else
using const_iterator = const_pointer; // See [string.view.iterators]
@ -355,7 +355,7 @@ public:
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
const_iterator cbegin() const _NOEXCEPT {
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
return std::__make_bounded_iter(data(), data(), data() + size());
#else
return __data_;
@ -364,7 +364,7 @@ public:
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
const_iterator cend() const _NOEXCEPT {
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
return std::__make_bounded_iter(data() + size(), data(), data() + size());
#else
return __data_ + __size_;