[libcxx-commits] [libcxx] be02f91 - [libc++][hardening] Add an ABI macro `_LIBCPP_ABI_BOUNDED_ITERATORS`.

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 27 16:41:39 PDT 2023


Author: varconst
Date: 2023-06-27T16:41:29-07:00
New Revision: be02f912d6b9b515a1d1b30cf06dfff1c085ea34

URL: https://github.com/llvm/llvm-project/commit/be02f912d6b9b515a1d1b30cf06dfff1c085ea34
DIFF: https://github.com/llvm/llvm-project/commit/be02f912d6b9b515a1d1b30cf06dfff1c085ea34.diff

LOG: [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

Added: 
    

Modified: 
    libcxx/include/__config
    libcxx/include/__debug
    libcxx/include/span
    libcxx/include/string_view

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__config b/libcxx/include/__config
index 6d2b3e14ed705..e829a45183c7f 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -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)
 

diff  --git a/libcxx/include/__debug b/libcxx/include/__debug
index fc3599bab7c56..d7bbdf39deb30 100644
--- a/libcxx/include/__debug
+++ b/libcxx/include/__debug
@@ -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

diff  --git a/libcxx/include/span b/libcxx/include/span
index 0464c94fac598..723cbf4f1d3f1 100644
--- a/libcxx/include/span
+++ b/libcxx/include/span
@@ -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());

diff  --git a/libcxx/include/string_view b/libcxx/include/string_view
index 38a93130b87be..a7d04ca9845be 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -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_;


        


More information about the libcxx-commits mailing list