Skip to content

Commit f8bea98

Browse files
[libc++][hardening] Constrain construction and use static_assert
... for `__{bounded.wrap}_iter`. This PR restricts construction to cases where reference types of source/destination iterators are (`T&`, `T&`) or (`T&`, `const T&`) ( where `T` can be const). We can't assert `__libcpp_is_contiguous_iterator` for `__wrap_iter` currently because `__wrap_iter` is currently used for wrapping user-defined fancy pointers.
1 parent 343a810 commit f8bea98

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

libcxx/include/__iterator/bounded_iter.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
#include <__config>
1717
#include <__iterator/iterator_traits.h>
1818
#include <__memory/pointer_traits.h>
19+
#include <__type_traits/conjunction.h>
20+
#include <__type_traits/disjunction.h>
1921
#include <__type_traits/enable_if.h>
2022
#include <__type_traits/integral_constant.h>
23+
#include <__type_traits/is_constructible.h>
2124
#include <__type_traits/is_convertible.h>
25+
#include <__type_traits/is_same.h>
26+
#include <__type_traits/make_const_lvalue_ref.h>
2227
#include <__utility/move.h>
2328

2429
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -47,8 +52,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD
4752
// pointer, it is undefined at the language level (see [expr.add]). If
4853
// bounded iterators exhibited this undefined behavior, we risk compiler
4954
// optimizations deleting non-redundant bounds checks.
50-
template <class _Iterator, class = __enable_if_t< __libcpp_is_contiguous_iterator<_Iterator>::value > >
55+
template <class _Iterator>
5156
struct __bounded_iter {
57+
static_assert(__libcpp_is_contiguous_iterator<_Iterator>::value,
58+
"Only contiguous iterators can be adapted by __bounded_iter.");
59+
5260
using value_type = typename iterator_traits<_Iterator>::value_type;
5361
using difference_type = typename iterator_traits<_Iterator>::difference_type;
5462
using pointer = typename iterator_traits<_Iterator>::pointer;
@@ -67,7 +75,13 @@ struct __bounded_iter {
6775
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter const&) = default;
6876
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter&&) = default;
6977

70-
template <class _OtherIterator, __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value, int> = 0>
78+
template < class _OtherIterator,
79+
__enable_if_t<
80+
_And<is_constructible<_Iterator, const _OtherIterator&>,
81+
is_convertible<const _OtherIterator&, _Iterator>,
82+
_Or<is_same<reference, __iter_reference<_OtherIterator> >,
83+
is_same<reference, __make_const_lvalue_ref<__iter_reference<_OtherIterator> > > > >::value,
84+
int> = 0>
7185
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bounded_iter(__bounded_iter<_OtherIterator> const& __other) _NOEXCEPT
7286
: __current_(__other.__current_),
7387
__begin_(__other.__begin_),
@@ -247,7 +261,7 @@ struct __bounded_iter {
247261
private:
248262
template <class>
249263
friend struct pointer_traits;
250-
template <class, class>
264+
template <class>
251265
friend struct __bounded_iter;
252266
_Iterator __current_; // current iterator
253267
_Iterator __begin_, __end_; // valid range represented as [begin, end]

libcxx/include/__iterator/wrap_iter.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
#include <__iterator/iterator_traits.h>
1818
#include <__memory/addressof.h>
1919
#include <__memory/pointer_traits.h>
20+
#include <__type_traits/conjunction.h>
21+
#include <__type_traits/disjunction.h>
2022
#include <__type_traits/enable_if.h>
2123
#include <__type_traits/integral_constant.h>
24+
#include <__type_traits/is_constructible.h>
2225
#include <__type_traits/is_convertible.h>
26+
#include <__type_traits/is_same.h>
27+
#include <__type_traits/make_const_lvalue_ref.h>
2328

2429
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2530
# pragma GCC system_header
@@ -45,9 +50,15 @@ class __wrap_iter {
4550

4651
public:
4752
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter() _NOEXCEPT : __i_() {}
48-
template <class _Up, __enable_if_t<is_convertible<_Up, iterator_type>::value, int> = 0>
49-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_Up>& __u) _NOEXCEPT
50-
: __i_(__u.base()) {}
53+
template <
54+
class _OtherIter,
55+
__enable_if_t< _And<is_constructible<_Iter, const _OtherIter&>,
56+
is_convertible<const _OtherIter&, _Iter>,
57+
_Or<is_same<reference, __iter_reference<_OtherIter> >,
58+
is_same<reference, __make_const_lvalue_ref<__iter_reference<_OtherIter> > > > >::value,
59+
int> = 0>
60+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_OtherIter>& __u) _NOEXCEPT
61+
: __i_(__u.__i_) {}
5162
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT { return *__i_; }
5263
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
5364
return std::__to_address(__i_);
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+
10+
// <iterator>
11+
12+
// __bounded_iter<_Iter>
13+
14+
// Verify that __bounded_iter does not accept non-contiguous iterators as determined by __libcpp_is_contiguous_iterator.
15+
// static_assert should be used, see https://github.com/llvm/llvm-project/issues/115002.
16+
// __wrap_iter cannot be so handled because it may directly wrap user-defined fancy pointers in libc++'s vector.
17+
18+
#include <deque>
19+
#include <vector>
20+
21+
// expected-error-re@*:* {{static assertion failed due to requirement {{.*}}Only contiguous iterators can be adapted by __bounded_iter.}}
22+
std::__bounded_iter<std::deque<int>::iterator> bit;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
10+
// <iterator>
11+
12+
// __bounded_iter<_Iter>
13+
// __wrap_iter<_Iter>
14+
15+
// Verify that libc++-wrapped iterators do not permit slicing conversion or construction.
16+
17+
#include <array>
18+
#include <vector>
19+
#include <span>
20+
#include <type_traits>
21+
22+
#include "test_macros.h"
23+
24+
struct Base {};
25+
struct Derived : Base {};
26+
27+
#ifdef _LIBCPP_ABI_USE_WRAP_ITER_IN_STD_ARRAY
28+
static_assert(!std::is_convertible<std::array<Derived, 1>::iterator, std::array<Base, 1>::iterator>::value, "");
29+
static_assert(!std::is_convertible<std::array<Derived, 1>::iterator, std::array<Base, 1>::const_iterator>::value, "");
30+
static_assert(!std::is_convertible<std::array<Derived, 1>::const_iterator, std::array<Base, 1>::const_iterator>::value,
31+
"");
32+
static_assert(!std::is_constructible<std::array<Base, 1>::iterator, std::array<Derived, 1>::iterator>::value, "");
33+
static_assert(!std::is_constructible<std::array<Base, 1>::iterator, std::array<Derived, 1>::const_iterator>::value, "");
34+
static_assert(
35+
!std::is_constructible<std::array<Base, 1>::const_iterator, std::array<Derived, 1>::const_iterator>::value, "");
36+
#endif
37+
38+
static_assert(!std::is_convertible<std::vector<Derived>::iterator, std::vector<Base>::iterator>::value, "");
39+
static_assert(!std::is_convertible<std::vector<Derived>::iterator, std::vector<Base>::const_iterator>::value, "");
40+
static_assert(!std::is_convertible<std::vector<Derived>::const_iterator, std::vector<Base>::const_iterator>::value, "");
41+
static_assert(!std::is_constructible<std::vector<Base>::iterator, std::vector<Derived>::iterator>::value, "");
42+
static_assert(!std::is_constructible<std::vector<Base>::iterator, std::vector<Derived>::const_iterator>::value, "");
43+
static_assert(!std::is_constructible<std::vector<Base>::const_iterator, std::vector<Derived>::const_iterator>::value,
44+
"");
45+
46+
#if TEST_STD_VER >= 20
47+
static_assert(!std::is_convertible_v<std::span<Derived>::iterator, std::span<Base>::iterator>);
48+
static_assert(!std::is_convertible_v<std::span<Derived>::iterator, std::span<Base>::const_iterator>);
49+
static_assert(!std::is_convertible_v<std::span<Derived>::const_iterator, std::span<Base>::const_iterator>);
50+
static_assert(!std::is_constructible_v<std::span<Base>::iterator, std::vector<Derived>::iterator>);
51+
static_assert(!std::is_constructible_v<std::span<Base>::iterator, std::vector<Derived>::const_iterator>);
52+
static_assert(!std::is_constructible_v<std::span<Base>::const_iterator, std::vector<Derived>::const_iterator>);
53+
#endif

0 commit comments

Comments
 (0)