Skip to content

Commit 43f6163

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 cb3d77d commit 43f6163

File tree

10 files changed

+80
-32
lines changed

10 files changed

+80
-32
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/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: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,38 @@
145145
// The macro below is used for all classes whose ABI have changed as part of fixing these bugs.
146146
#define _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS __attribute__((__abi_tag__("llvm18_nua")))
147147

148-
// Changes the iterator type of select containers (see below) to a bounded iterator that keeps track of whether it's
149-
// within the bounds of the original container and asserts it on every dereference.
148+
// Historical equivalent to defining `_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW` and
149+
// `_LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN`.
150+
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
151+
# ifndef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
152+
# define _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
153+
# endif
154+
# ifndef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
155+
# define _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
156+
# endif
157+
#endif
158+
159+
// Changes the iterator type of `basic_string_view` to a bounded iterator that keeps track of whether it's within the
160+
// bounds of the original container and asserts it on every dereference and when performing iterator arithmetic.
150161
//
151-
// ABI impact: changes the iterator type of the relevant containers.
162+
// ABI impact: changes the iterator type of `basic_string_view` and its specializations, such as `string_view` and
163+
// `wstring_view`.
164+
// #define _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW
165+
166+
// Changes the iterator type of `span` to a bounded iterator that keeps track of whether it's within the
167+
// bounds of the original container and asserts it on every dereference and when performing iterator arithmetic.
152168
//
153-
// Supported containers:
154-
// - `span`;
155-
// - `string_view`.
156-
// #define _LIBCPP_ABI_BOUNDED_ITERATORS
169+
// ABI impact: changes the iterator type of `span`.
170+
// #define _LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN
157171

158172
// Changes the iterator type of `basic_string` to a bounded iterator that keeps track of whether it's within the bounds
159-
// of the original container and asserts it on every dereference and when performing iterator arithmetics.
173+
// of the original container and asserts it on every dereference and when performing iterator arithmetic.
160174
//
161175
// ABI impact: changes the iterator type of `basic_string` and its specializations, such as `string` and `wstring`.
162176
// #define _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
163177

164178
// Changes the iterator type of `vector` to a bounded iterator that keeps track of whether it's within the bounds of the
165-
// original container and asserts it on every dereference and when performing iterator arithmetics. Note: this doesn't
179+
// original container and asserts it on every dereference and when performing iterator arithmetic. Note: this doesn't
166180
// yet affect `vector<bool>`.
167181
//
168182
// ABI impact: changes the iterator type of `vector` (except `vector<bool>`).

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>

libcxx/utils/libcxx/test/features.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,9 @@ def _mingwSupportsModules(cfg):
365365
macros = {
366366
"_LIBCPP_NO_VCRUNTIME": "libcpp-no-vcruntime",
367367
"_LIBCPP_ABI_VERSION": "libcpp-abi-version",
368-
"_LIBCPP_ABI_BOUNDED_ITERATORS": "libcpp-has-abi-bounded-iterators",
369368
"_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING": "libcpp-has-abi-bounded-iterators-in-string",
369+
"_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING_VIEW": "libcpp-has-abi-bounded-iterators-in-string-view",
370+
"_LIBCPP_ABI_BOUNDED_ITERATORS_IN_SPAN": "libcpp-has-abi-bounded-iterators-in-span",
370371
"_LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR": "libcpp-has-abi-bounded-iterators-in-vector",
371372
"_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY": "libcpp-has-abi-bounded-iterators-in-std-array",
372373
"_LIBCPP_ABI_BOUNDED_UNIQUE_PTR": "libcpp-has-abi-bounded-unique_ptr",

0 commit comments

Comments
 (0)