Skip to content

Commit 564729f

Browse files
committed
[libc++] Introduce _LIBCPP_ABI_BOUNDED_ITERATORS_IN_{STRING_VIEW,SPAN}
For consistency with other containers, introduce per-container macros to enable bounded iterators in span and string_view. This patch also keeps the original macro around for backwards compatibility, although we probably want to deprecate and eventually remove it in a separate patch since the name can lead to confusion.
1 parent dbec6e4 commit 564729f

File tree

11 files changed

+68
-25
lines changed

11 files changed

+68
-25
lines changed

libcxx/cmake/caches/Generic-hardening-mode-fast-with-abi-breaks.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
set(LIBCXX_HARDENING_MODE "fast" CACHE STRING "")
22
set(_defines
3-
_LIBCPP_ABI_BOUNDED_ITERATORS
3+
_LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
44
_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
5+
_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
56
_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
67
_LIBCPP_ABI_BOUNDED_UNIQUE_PTR
78
_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY

libcxx/docs/ABIGuarantees.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ This changes the ``iterator`` and ``const_iterator`` of ``array`` and ``string_v
178178
``__wrap_iter`` instead, which makes it less likely for users to depend on non-portable implementation details. This is
179179
especially useful because enabling bounded iterators hardening requires code not to make these assumptions.
180180

181-
``_LIBCPP_ABI_BOUNDED_ITERATORS``, ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING``, ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR``, and ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY``
182-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
181+
``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_{STRING,STRING_VIEW,SPAN,VECTOR,STD_ARRAY}``
182+
-------------------------------------------------------------------------------
183183
These flags change the ``iterator`` member of various classes to reference hardened iterators instead. See the
184184
:ref:`hardening documentation <hardening>` for more details.
185185

libcxx/docs/Hardening.rst

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,30 +314,36 @@ itself) to enable additional hardening checks. This is done by passing these
314314
macros as ``-DLIBCXX_ABI_DEFINES="_LIBCPP_ABI_FOO;_LIBCPP_ABI_BAR;etc"`` at
315315
CMake configuration time. The available options are:
316316

317-
- ``_LIBCPP_ABI_BOUNDED_ITERATORS`` -- changes the iterator type of select
318-
containers (see below) to a bounded iterator that keeps track of whether it's
319-
within the bounds of the original container and asserts valid bounds on every
320-
dereference.
317+
- ``_LIBCPP_ABI_BOUNDED_ITERATORS`` -- historical equivalent to defining both
318+
``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW`` and ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN``.
321319

322-
ABI impact: changes the iterator type of the relevant containers.
320+
- ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW`` -- changes the iterator type of
321+
``basic_string_view`` to a bounded iterator that keeps track of whether it's within
322+
the bounds of the original container and asserts it on every dereference and
323+
when performing iterator arithmetic.
324+
325+
ABI impact: changes the iterator type of ``basic_string_view`` and its
326+
specializations, such as ``string_view`` and ``wstring_view``.
323327

324-
Supported containers:
328+
- ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN`` -- changes the iterator type of ``span``
329+
to a bounded iterator that keeps track of whether it's within the bounds of the
330+
original container and asserts it on every dereference and when performing iterator
331+
arithmetic.
325332

326-
- ``span``;
327-
- ``string_view``.
333+
ABI impact: changes the iterator type of ``span``.
328334

329335
- ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING`` -- changes the iterator type of
330336
``basic_string`` to a bounded iterator that keeps track of whether it's within
331337
the bounds of the original container and asserts it on every dereference and
332-
when performing iterator arithmetics.
338+
when performing iterator arithmetic.
333339

334340
ABI impact: changes the iterator type of ``basic_string`` and its
335341
specializations, such as ``string`` and ``wstring``.
336342

337343
- ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR`` -- changes the iterator type of
338344
``vector`` to a bounded iterator that keeps track of whether it's within the
339345
bounds of the original container and asserts it on every dereference and when
340-
performing iterator arithmetics. Note: this doesn't yet affect
346+
performing iterator arithmetic. Note: this doesn't yet affect
341347
``vector<bool>``.
342348

343349
ABI impact: changes the iterator type of ``vector`` (except ``vector<bool>``).

libcxx/docs/ReleaseNotes/21.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Improvements and New Features
7676
- The ``bitset::to_string`` function has been optimized, resulting in a performance improvement of up to 8.3x for bitsets
7777
with uniformly distributed zeros and ones, and up to 13.5x and 16.1x for sparse and dense bitsets, respectively.
7878

79+
- The ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW`` and ``_LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN`` macros were added.
80+
These macros control bounded iterators in ``string_view`` and ``span`` respectively. This was previously controled by
81+
the single macro ``_LIBCPP_ABI_BOUNDED_ITERATORS``.
82+
7983
Deprecations and Removals
8084
-------------------------
8185

libcxx/include/__configuration/abi.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@
113113
# define _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
114114
#endif
115115

116+
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
117+
# ifndef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
118+
# define _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
119+
# endif
120+
# ifndef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
121+
# define _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
122+
# endif
123+
#endif
124+
116125
// Tracks the bounds of the array owned by std::unique_ptr<T[]>, allowing it to trap when accessed out-of-bounds.
117126
// Note that limited bounds checking is also available outside of this ABI configuration, but only some categories
118127
// of types can be checked.

libcxx/include/span

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public:
240240
using const_pointer = const _Tp*;
241241
using reference = _Tp&;
242242
using const_reference = const _Tp&;
243-
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
243+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
244244
using iterator = __bounded_iter<pointer>;
245245
# else
246246
using iterator = __wrap_iter<pointer>;
@@ -383,14 +383,14 @@ public:
383383

384384
// [span.iter], span iterator support
385385
_LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept {
386-
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
386+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
387387
return std::__make_bounded_iter(data(), data(), data() + size());
388388
# else
389389
return iterator(data());
390390
# endif
391391
}
392392
_LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept {
393-
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
393+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
394394
return std::__make_bounded_iter(data() + size(), data(), data() + size());
395395
# else
396396
return iterator(data() + size());
@@ -423,7 +423,7 @@ public:
423423
using const_pointer = const _Tp*;
424424
using reference = _Tp&;
425425
using const_reference = const _Tp&;
426-
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
426+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
427427
using iterator = __bounded_iter<pointer>;
428428
# else
429429
using iterator = __wrap_iter<pointer>;
@@ -548,14 +548,14 @@ public:
548548

549549
// [span.iter], span iterator support
550550
_LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept {
551-
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
551+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
552552
return std::__make_bounded_iter(data(), data(), data() + size());
553553
# else
554554
return iterator(data());
555555
# endif
556556
}
557557
_LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept {
558-
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
558+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
559559
return std::__make_bounded_iter(data() + size(), data(), data() + size());
560560
# else
561561
return iterator(data() + size());

libcxx/include/string_view

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public:
287287
using const_pointer = const _CharT*;
288288
using reference = _CharT&;
289289
using const_reference = const _CharT&;
290-
# if defined(_LIBCPP_ABI_BOUNDED_ITERATORS)
290+
# if defined(_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW)
291291
using const_iterator = __bounded_iter<const_pointer>;
292292
# elif defined(_LIBCPP_ABI_USE_WRAP_ITER_IN_STD_STRING_VIEW)
293293
using const_iterator = __wrap_iter<const_pointer>;
@@ -365,15 +365,15 @@ public:
365365
_LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return cend(); }
366366

367367
_LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
368-
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
368+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
369369
return std::__make_bounded_iter(data(), data(), data() + size());
370370
# else
371371
return const_iterator(__data_);
372372
# endif
373373
}
374374

375375
_LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
376-
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
376+
# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
377377
return std::__make_bounded_iter(data() + size(), data(), data() + size());
378378
# else
379379
return const_iterator(__data_ + __size_);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// This test ensures that setting _LIBCPP_ABI_BOUNDED_ITERATORS enabled bounded
10+
// iterators in std::span and std::string_view, for historical reasons.
11+
12+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ABI_BOUNDED_ITERATORS
13+
14+
#include <version>
15+
16+
#ifndef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
17+
# error _LIBCPP_ABI_BOUNDED_ITERATORS should enable bounded iterators in std::span
18+
#endif
19+
20+
#ifndef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
21+
# error _LIBCPP_ABI_BOUNDED_ITERATORS should enable bounded iterators in std::string_view
22+
#endif

libcxx/test/libcxx/containers/views/views.span/assert.iterator-indexing.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// Make sure that std::span's iterators check for OOB accesses when the debug mode is enabled.
1111

12-
// REQUIRES: has-unix-headers, libcpp-has-abi-bounded-iterators
12+
// REQUIRES: has-unix-headers, libcpp-has-abi-bounded-iterators-in-span
1313
// UNSUPPORTED: libcpp-hardening-mode=none
1414

1515
#include <span>

libcxx/test/libcxx/strings/string.view/string.view.iterators/assert.iterator-indexing.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// Make sure that std::string_view's iterators check for OOB accesses when the debug mode is enabled.
1010

11-
// REQUIRES: has-unix-headers, libcpp-has-abi-bounded-iterators
11+
// REQUIRES: has-unix-headers, libcpp-has-abi-bounded-iterators-in-string-view
1212
// UNSUPPORTED: libcpp-hardening-mode=none
1313

1414
#include <iterator>

0 commit comments

Comments
 (0)