Skip to content

Commit 9115be1

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).
1 parent 9b058bb commit 9115be1

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

libcxx/include/__iterator/bounded_iter.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
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>
2123
#include <__type_traits/is_convertible.h>
24+
#include <__type_traits/is_same.h>
25+
#include <__type_traits/make_const_lvalue_ref.h>
2226
#include <__utility/move.h>
2327

2428
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -47,8 +51,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD
4751
// pointer, it is undefined at the language level (see [expr.add]). If
4852
// bounded iterators exhibited this undefined behavior, we risk compiler
4953
// optimizations deleting non-redundant bounds checks.
50-
template <class _Iterator, class = __enable_if_t< __libcpp_is_contiguous_iterator<_Iterator>::value > >
54+
template <class _Iterator>
5155
struct __bounded_iter {
56+
static_assert(__libcpp_is_contiguous_iterator<_Iterator>::value,
57+
"Only contiguous iterators can be adapted by __bounded_iter.");
58+
5259
using value_type = typename iterator_traits<_Iterator>::value_type;
5360
using difference_type = typename iterator_traits<_Iterator>::difference_type;
5461
using pointer = typename iterator_traits<_Iterator>::pointer;
@@ -67,7 +74,13 @@ struct __bounded_iter {
6774
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter const&) = default;
6875
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter&&) = default;
6976

70-
template <class _OtherIterator, __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value, int> = 0>
77+
template < class _OtherIterator,
78+
__enable_if_t<
79+
_And<is_constructible<_Iterator, const _OtherIterator&>,
80+
is_convertible<const _OtherIterator&, _Iterator>,
81+
_Or<is_same<reference, __iter_reference<_OtherIterator> >,
82+
is_same<reference, __make_const_lvalue_ref<__iter_reference<_OtherIterator> > > > >::value,
83+
int> = 0>
7184
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bounded_iter(__bounded_iter<_OtherIterator> const& __other) _NOEXCEPT
7285
: __current_(__other.__current_),
7386
__begin_(__other.__begin_),
@@ -247,7 +260,7 @@ struct __bounded_iter {
247260
private:
248261
template <class>
249262
friend struct pointer_traits;
250-
template <class, class>
263+
template <class>
251264
friend struct __bounded_iter;
252265
_Iterator __current_; // current iterator
253266
_Iterator __begin_, __end_; // valid range represented as [begin, end]

libcxx/include/__iterator/wrap_iter.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
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>
2224
#include <__type_traits/is_convertible.h>
25+
#include <__type_traits/is_same.h>
26+
#include <__type_traits/make_const_lvalue_ref.h>
2327

2428
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2529
# pragma GCC system_header
@@ -29,6 +33,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2933

3034
template <class _Iter>
3135
class __wrap_iter {
36+
static_assert(__libcpp_is_contiguous_iterator<_Iter>::value,
37+
"Only contiguous iterators can be adapted by __wrap_iter.");
38+
3239
public:
3340
typedef _Iter iterator_type;
3441
typedef typename iterator_traits<iterator_type>::value_type value_type;
@@ -45,9 +52,15 @@ class __wrap_iter {
4552

4653
public:
4754
_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()) {}
55+
template <
56+
class _OtherIter,
57+
__enable_if_t< _And<is_constructible<_Iter, const _OtherIter&>,
58+
is_convertible<const _OtherIter&, _Iter>,
59+
_Or<is_same<reference, __iter_reference<_OtherIter> >,
60+
is_same<reference, __make_const_lvalue_ref<__iter_reference<_OtherIter> > > > >::value,
61+
int> = 0>
62+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_OtherIter>& __u) _NOEXCEPT
63+
: __i_(__u.__i_) {}
5164
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT { return *__i_; }
5265
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
5366
return std::__to_address(__i_);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 these wrappers do not accept non-contiguous iterators as determined by
16+
// __libcpp_is_contiguous_iterator.
17+
// static_assert should be used, see https://github.com/llvm/llvm-project/issues/115002.
18+
19+
#include <deque>
20+
#include <iterator>
21+
22+
// expected-error-re@*:* {{static assertion failed due to requirement {{.*}}Only contiguous iterators can be adapted by __bounded_iter.}}
23+
std::__bounded_iter<std::deque<int>::iterator> bit;
24+
// expected-error-re@*:* {{static assertion failed due to requirement {{.*}}Only contiguous iterators can be adapted by __wrap_iter.}}
25+
std::__wrap_iter<std::deque<int>::iterator> wit;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
static_assert(!std::is_convertible<std::array<Derived, 1>::iterator, std::array<Base, 1>::iterator>::value, "");
28+
static_assert(!std::is_convertible<std::array<Derived, 1>::iterator, std::array<Base, 1>::const_iterator>::value, "");
29+
static_assert(!std::is_convertible<std::array<Derived, 1>::const_iterator, std::array<Base, 1>::const_iterator>::value,
30+
"");
31+
static_assert(!std::is_constructible<std::array<Base, 1>::iterator, std::array<Derived, 1>::iterator>::value, "");
32+
static_assert(!std::is_constructible<std::array<Base, 1>::iterator, std::array<Derived, 1>::const_iterator>::value, "");
33+
static_assert(!std::is_constructible<std::array<Base, 1>::const_iterator, std::array<Derived, 1>::const_iterator>::value, "");
34+
35+
static_assert(!std::is_convertible<std::vector<Derived>::iterator, std::vector<Base>::iterator>::value, "");
36+
static_assert(!std::is_convertible<std::vector<Derived>::iterator, std::vector<Base>::const_iterator>::value, "");
37+
static_assert(!std::is_convertible<std::vector<Derived>::const_iterator, std::vector<Base>::const_iterator>::value, "");
38+
static_assert(!std::is_constructible<std::vector<Base>::iterator, std::vector<Derived>::iterator>::value, "");
39+
static_assert(!std::is_constructible<std::vector<Base>::iterator, std::vector<Derived>::const_iterator>::value, "");
40+
static_assert(!std::is_constructible<std::vector<Base>::const_iterator, std::vector<Derived>::const_iterator>::value, "");
41+
42+
#if TEST_STD_VER >= 20
43+
static_assert(!std::is_convertible_v<std::span<Derived>::iterator, std::span<Base>::iterator>);
44+
static_assert(!std::is_convertible_v<std::span<Derived>::iterator, std::span<Base>::const_iterator>);
45+
static_assert(!std::is_convertible_v<std::span<Derived>::const_iterator, std::span<Base>::const_iterator>);
46+
static_assert(!std::is_constructible_v<std::span<Base>::iterator, std::vector<Derived>::iterator>);
47+
static_assert(!std::is_constructible_v<std::span<Base>::iterator, std::vector<Derived>::const_iterator>);
48+
static_assert(!std::is_constructible_v<std::span<Base>::const_iterator, std::vector<Derived>::const_iterator>);
49+
#endif

0 commit comments

Comments
 (0)