File: assert.subscript.pass.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,245,064 kB
  • sloc: cpp: 7,619,731; ansic: 1,434,018; asm: 1,058,748; python: 252,740; f90: 94,671; objc: 70,685; lisp: 42,813; pascal: 18,401; sh: 8,601; ml: 5,111; perl: 4,720; makefile: 3,676; awk: 3,523; javascript: 2,409; xml: 892; fortran: 770
file content (207 lines) | stat: -rw-r--r-- 7,638 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: has-unix-headers
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-hardening-mode=none
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

// <memory>
//
// unique_ptr<T[]>
//
// T& operator[](std::size_t);

// This test ensures that we catch an out-of-bounds access in std::unique_ptr<T[]>::operator[]
// when unique_ptr has the appropriate ABI configuration.

#include <memory>
#include <cstddef>
#include <string>

#include "check_assertion.h"
#include "type_algorithms.h"
#include "test_macros.h"

struct MyDeleter {
  MyDeleter() = default;

  // required to exercise converting move-constructor
  template <class T>
  MyDeleter(std::default_delete<T> const&) {}

  // required to exercise converting move-assignment
  template <class T>
  MyDeleter& operator=(std::default_delete<T> const&) {
    return *this;
  }

  template <class T>
  void operator()(T* ptr) const {
    delete[] ptr;
  }
};

template <class WithCookie, class NoCookie>
void test() {
  LIBCPP_STATIC_ASSERT(std::__has_array_cookie<WithCookie>::value);
  LIBCPP_STATIC_ASSERT(!std::__has_array_cookie<NoCookie>::value);

  // For types with an array cookie, we can always detect OOB accesses. Note that reliance on an array
  // cookie is limited to the default deleter, since a unique_ptr with a custom deleter may not have
  // been allocated with `new T[n]`.
  {
    {
      std::unique_ptr<WithCookie[]> ptr(new WithCookie[5]);
      assert(&ptr[1] == ptr.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(ptr[6], "unique_ptr<T[]>::operator[](index): index out of range");
    }
    {
      std::unique_ptr<WithCookie[]> ptr = std::make_unique<WithCookie[]>(5);
      assert(&ptr[1] == ptr.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(ptr[6], "unique_ptr<T[]>::operator[](index): index out of range");
    }
#if TEST_STD_VER >= 20
    {
      std::unique_ptr<WithCookie[]> ptr = std::make_unique_for_overwrite<WithCookie[]>(5);
      assert(&ptr[1] == ptr.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(ptr[6] = WithCookie(), "unique_ptr<T[]>::operator[](index): index out of range");
    }
#endif
  }

  // For types that don't have an array cookie, things are a bit more complicated. We can detect OOB accesses
  // only when the unique_ptr is created via an API where the size is passed down to the library so that we
  // can store it inside the unique_ptr. That requires the appropriate ABI configuration to be enabled.
  //
  // Note that APIs that allow the size to be passed down to the library only support the default deleter
  // as of writing this test.
#if defined(_LIBCPP_ABI_BOUNDED_UNIQUE_PTR)
  {
    {
      std::unique_ptr<NoCookie[]> ptr = std::make_unique<NoCookie[]>(5);
      assert(&ptr[1] == ptr.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(ptr[6], "unique_ptr<T[]>::operator[](index): index out of range");
    }
#  if TEST_STD_VER >= 20
    {
      std::unique_ptr<NoCookie[]> ptr = std::make_unique_for_overwrite<NoCookie[]>(5);
      assert(&ptr[1] == ptr.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(ptr[6] = NoCookie(), "unique_ptr<T[]>::operator[](index): index out of range");
    }
#  endif
  }
#endif

  // Make sure that we carry the bounds information properly through conversions, assignments, etc.
  // These tests are only relevant when the ABI setting is enabled (with a stateful bounds-checker).
#if defined(_LIBCPP_ABI_BOUNDED_UNIQUE_PTR)
  types::for_each(types::type_list<NoCookie, WithCookie>(), []<class T> {
    // Bounds carried through move construction
    {
      std::unique_ptr<T[]> ptr = std::make_unique<T[]>(5);
      std::unique_ptr<T[]> other(std::move(ptr));
      assert(&other[1] == other.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(other[6], "unique_ptr<T[]>::operator[](index): index out of range");
    }

    // Bounds carried through move assignment
    {
      std::unique_ptr<T[]> ptr = std::make_unique<T[]>(5);
      std::unique_ptr<T[]> other;
      other = std::move(ptr);
      assert(&other[1] == other.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(other[6], "unique_ptr<T[]>::operator[](index): index out of range");
    }

    // Bounds carried through converting move-constructor
    {
      std::unique_ptr<T[]> ptr = std::make_unique<T[]>(5);
      std::unique_ptr<T[], MyDeleter> other(std::move(ptr));
      assert(&other[1] == other.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(other[6], "unique_ptr<T[]>::operator[](index): index out of range");
    }

    // Bounds carried through converting move-assignment
    {
      std::unique_ptr<T[]> ptr = std::make_unique<T[]>(5);
      std::unique_ptr<T[], MyDeleter> other;
      other = std::move(ptr);
      assert(&other[1] == other.get() + 1); // ensure no assertion
      TEST_LIBCPP_ASSERT_FAILURE(other[6], "unique_ptr<T[]>::operator[](index): index out of range");
    }
  });
#endif
}

template <std::size_t Size>
struct NoCookie {
  char padding[Size];
};

template <std::size_t Size>
struct WithCookie {
  WithCookie() = default;
  WithCookie(WithCookie const&) {}
  WithCookie& operator=(WithCookie const&) { return *this; }
  ~WithCookie() {}
  char padding[Size];
};

template <std::size_t Size>
struct alignas(128) OveralignedNoCookie {
  char padding[Size];
};

template <std::size_t Size>
struct alignas(128) OveralignedWithCookie {
  OveralignedWithCookie() = default;
  OveralignedWithCookie(OveralignedWithCookie const&) {}
  OveralignedWithCookie& operator=(OveralignedWithCookie const&) { return *this; }
  ~OveralignedWithCookie() {}
  char padding[Size];
};

// These types have a different ABI alignment (alignof) and preferred alignment (__alignof) on some platforms.
// Make sure things work with these types because array cookies can be sensitive to preferred alignment on some
// platforms.
struct WithCookiePreferredAlignment {
  WithCookiePreferredAlignment() = default;
  WithCookiePreferredAlignment(WithCookiePreferredAlignment const&) {}
  WithCookiePreferredAlignment& operator=(WithCookiePreferredAlignment const&) { return *this; }
  ~WithCookiePreferredAlignment() {}
  long double data;
};
struct NoCookiePreferredAlignment {
  long double data;
};

int main(int, char**) {
  test<WithCookie<1>, NoCookie<1>>();
  test<WithCookie<2>, NoCookie<2>>();
  test<WithCookie<3>, NoCookie<3>>();
  test<WithCookie<4>, NoCookie<4>>();
  test<WithCookie<8>, NoCookie<8>>();
  test<WithCookie<16>, NoCookie<16>>();
  test<WithCookie<32>, NoCookie<32>>();
  test<WithCookie<256>, NoCookie<256>>();

  test<OveralignedWithCookie<1>, OveralignedNoCookie<1>>();
  test<OveralignedWithCookie<2>, OveralignedNoCookie<2>>();
  test<OveralignedWithCookie<3>, OveralignedNoCookie<3>>();
  test<OveralignedWithCookie<4>, OveralignedNoCookie<4>>();
  test<OveralignedWithCookie<8>, OveralignedNoCookie<8>>();
  test<OveralignedWithCookie<16>, OveralignedNoCookie<16>>();
  test<OveralignedWithCookie<32>, OveralignedNoCookie<32>>();
  test<OveralignedWithCookie<256>, OveralignedNoCookie<256>>();

  test<std::string, int>();
  test<WithCookiePreferredAlignment, NoCookiePreferredAlignment>();

  return 0;
}