]>
git.saurik.com Git - apple/xnu.git/blob - tests/bounded_ptr_src/arith.add_assign.cpp
3 // bounded_ptr& operator+=(std::ptrdiff_t n);
6 #include <libkern/c++/bounded_ptr.h>
11 #include <darwintest.h>
12 #include <darwintest_utils.h>
13 #include "test_utils.h"
15 #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
20 struct tracking_policy
{
28 bool tracking_policy::did_trap
= false;
31 template <typename T
, typename QualT
>
35 std::array
<T
, 5> array
= {T
{0}, T
{1}, T
{2}, T
{3}, T
{4}};
37 // Add-assign positive offsets
38 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
43 test_bounded_ptr
<QualT
> ptr(array
.begin(), array
.begin(), array
.end());
45 _assert(&ref
== &ptr
);
46 _assert(&*ptr
== &array
[0]);
49 test_bounded_ptr
<QualT
> ptr(array
.begin(), array
.begin(), array
.end());
51 _assert(&ref
== &ptr
);
52 _assert(&*ptr
== &array
[1]);
55 test_bounded_ptr
<QualT
> ptr(array
.begin(), array
.begin(), array
.end());
57 _assert(&ref
== &ptr
);
58 _assert(&*ptr
== &array
[2]);
61 test_bounded_ptr
<QualT
> ptr(array
.begin(), array
.begin(), array
.end());
63 _assert(&ref
== &ptr
);
64 _assert(&*ptr
== &array
[3]);
67 test_bounded_ptr
<QualT
> ptr(array
.begin(), array
.begin(), array
.end());
69 _assert(&ref
== &ptr
);
70 _assert(&*ptr
== &array
[4]);
73 test_bounded_ptr
<QualT
> ptr(array
.begin(), array
.begin(), array
.end());
75 _assert(&ref
== &ptr
);
76 _assert(ptr
== array
.end());
79 // Add-assign negative offsets
80 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
85 test_bounded_ptr
<QualT
> ptr(array
.end(), array
.begin(), array
.end());
87 _assert(&ref
== &ptr
);
88 _assert(ptr
== array
.end());
91 test_bounded_ptr
<QualT
> ptr(array
.end(), array
.begin(), array
.end());
92 auto& ref
= ptr
+= -1;
93 _assert(&ref
== &ptr
);
94 _assert(&*ptr
== &array
[4]);
97 test_bounded_ptr
<QualT
> ptr(array
.end(), array
.begin(), array
.end());
98 auto& ref
= ptr
+= -2;
99 _assert(&ref
== &ptr
);
100 _assert(&*ptr
== &array
[3]);
103 test_bounded_ptr
<QualT
> ptr(array
.end(), array
.begin(), array
.end());
104 auto& ref
= ptr
+= -3;
105 _assert(&ref
== &ptr
);
106 _assert(&*ptr
== &array
[2]);
109 test_bounded_ptr
<QualT
> ptr(array
.end(), array
.begin(), array
.end());
110 auto& ref
= ptr
+= -4;
111 _assert(&ref
== &ptr
);
112 _assert(&*ptr
== &array
[1]);
115 test_bounded_ptr
<QualT
> ptr(array
.end(), array
.begin(), array
.end());
116 auto& ref
= ptr
+= -5;
117 _assert(&ref
== &ptr
);
118 _assert(&*ptr
== &array
[0]);
121 // Make sure we trap on arithmetic overflow in the number of bytes calculation
123 std::ptrdiff_t sizeof_T
= sizeof(T
); // avoid promotion to unsigned in calculations
125 // largest (most positive) n for the number of bytes `n * sizeof(T)` not to overflow ptrdiff_t
126 std::ptrdiff_t max_n
= std::numeric_limits
<std::ptrdiff_t>::max() / sizeof_T
;
128 // smallest (most negative) n for the number of bytes `n * sizeof(T)` not to overflow ptrdiff_t
129 std::ptrdiff_t min_n
= std::numeric_limits
<std::ptrdiff_t>::min() / sizeof_T
;
131 // Overflow with a positive offset
133 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.begin(), array
.begin(), array
.end());
134 tracking_policy::did_trap
= false;
136 _assert(tracking_policy::did_trap
);
139 // Overflow with a negative offset
141 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.begin(), array
.begin(), array
.end());
142 tracking_policy::did_trap
= false;
144 _assert(tracking_policy::did_trap
);
148 // Make sure we trap on arithmetic overflow in the offset calculation
150 // To avoid running into the overflow of `n * sizeof(T)` when ptrdiff_t
151 // is the same size as int32_t, we test the offset overflow check by
152 // successive addition of smaller offsets.
154 // We basically push the offset right to its limit, and then push it
155 // past its limit to watch it overflow.
157 std::int64_t sizeof_T
= sizeof(T
); // avoid promotion to unsigned in calculations
159 // largest (most positive) n for the number of bytes `n * sizeof(T)` not to overflow the int32_t offset
160 std::int64_t max_n
= std::numeric_limits
<std::int32_t>::max() / sizeof_T
;
162 // smallest (most negative) n for the number of bytes `n * sizeof(T)` not to overflow the int32_t offset
163 std::int64_t min_n
= std::numeric_limits
<std::int32_t>::min() / sizeof_T
;
165 // Add positive offsets
167 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.begin(), array
.begin(), array
.end());
168 tracking_policy::did_trap
= false;
169 ptr
+= static_cast<ptrdiff_t>(max_n
/ 2);
170 _assert(!tracking_policy::did_trap
);
171 ptr
+= static_cast<ptrdiff_t>(max_n
/ 2);
172 _assert(!tracking_policy::did_trap
);
174 _assert(!tracking_policy::did_trap
); // offset is now right at its positive limit
176 _assert(tracking_policy::did_trap
);
179 // Add negative offsets
181 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.begin(), array
.begin(), array
.end());
182 tracking_policy::did_trap
= false;
183 ptr
+= static_cast<ptrdiff_t>(min_n
/ 2);
184 _assert(!tracking_policy::did_trap
);
185 ptr
+= static_cast<ptrdiff_t>(min_n
/ 2);
186 _assert(!tracking_policy::did_trap
);
188 _assert(!tracking_policy::did_trap
); // offset is now right at its negative limit
190 _assert(tracking_policy::did_trap
);
195 T_DECL(arith_add_assign
, "bounded_ptr.arith.add_assign") {
198 tests
<T
, T
volatile>();
199 tests
<T
, T
const volatile>();