1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions // bounded_ptr& operator+=(std::ptrdiff_t n);
4bb611c8fSApple OSS Distributions //
5bb611c8fSApple OSS Distributions
6bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
7bb611c8fSApple OSS Distributions #include <array>
8bb611c8fSApple OSS Distributions #include <cstddef>
9bb611c8fSApple OSS Distributions #include <cstdint>
10bb611c8fSApple OSS Distributions #include <limits>
11bb611c8fSApple OSS Distributions #include <darwintest.h>
12bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
13bb611c8fSApple OSS Distributions #include "test_utils.h"
14bb611c8fSApple OSS Distributions
15bb611c8fSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
16bb611c8fSApple OSS Distributions
17bb611c8fSApple OSS Distributions struct T { int i; };
18bb611c8fSApple OSS Distributions
19bb611c8fSApple OSS Distributions namespace {
20bb611c8fSApple OSS Distributions struct tracking_policy {
21bb611c8fSApple OSS Distributions static bool did_trap;
22bb611c8fSApple OSS Distributions static void
trap__anonb3a20ade0111::tracking_policy23bb611c8fSApple OSS Distributions trap(char const*)
24bb611c8fSApple OSS Distributions {
25bb611c8fSApple OSS Distributions did_trap = true;
26bb611c8fSApple OSS Distributions }
27bb611c8fSApple OSS Distributions };
28bb611c8fSApple OSS Distributions bool tracking_policy::did_trap = false;
29bb611c8fSApple OSS Distributions }
30bb611c8fSApple OSS Distributions
31bb611c8fSApple OSS Distributions template <typename T, typename QualT>
32bb611c8fSApple OSS Distributions static void
tests()33bb611c8fSApple OSS Distributions tests()
34bb611c8fSApple OSS Distributions {
35bb611c8fSApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
36bb611c8fSApple OSS Distributions
37bb611c8fSApple OSS Distributions // Add-assign positive offsets
38bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
39bb611c8fSApple OSS Distributions // ^ ^
40bb611c8fSApple OSS Distributions // | |
41bb611c8fSApple OSS Distributions // begin,ptr end
42bb611c8fSApple OSS Distributions {
43bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
44bb611c8fSApple OSS Distributions auto& ref = ptr += 0;
45bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
46bb611c8fSApple OSS Distributions _assert(&*ptr == &array[0]);
47bb611c8fSApple OSS Distributions }
48bb611c8fSApple OSS Distributions {
49bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
50bb611c8fSApple OSS Distributions auto& ref = ptr += 1;
51bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
52bb611c8fSApple OSS Distributions _assert(&*ptr == &array[1]);
53bb611c8fSApple OSS Distributions }
54bb611c8fSApple OSS Distributions {
55bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
56bb611c8fSApple OSS Distributions auto& ref = ptr += 2;
57bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
58bb611c8fSApple OSS Distributions _assert(&*ptr == &array[2]);
59bb611c8fSApple OSS Distributions }
60bb611c8fSApple OSS Distributions {
61bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
62bb611c8fSApple OSS Distributions auto& ref = ptr += 3;
63bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
64bb611c8fSApple OSS Distributions _assert(&*ptr == &array[3]);
65bb611c8fSApple OSS Distributions }
66bb611c8fSApple OSS Distributions {
67bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
68bb611c8fSApple OSS Distributions auto& ref = ptr += 4;
69bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
70bb611c8fSApple OSS Distributions _assert(&*ptr == &array[4]);
71bb611c8fSApple OSS Distributions }
72bb611c8fSApple OSS Distributions {
73bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
74bb611c8fSApple OSS Distributions auto& ref = ptr += 5;
75bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
76bb611c8fSApple OSS Distributions _assert(ptr == array.end());
77bb611c8fSApple OSS Distributions }
78bb611c8fSApple OSS Distributions
79bb611c8fSApple OSS Distributions // Add-assign negative offsets
80bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
81bb611c8fSApple OSS Distributions // ^ ^
82bb611c8fSApple OSS Distributions // | |
83bb611c8fSApple OSS Distributions // begin end,ptr
84bb611c8fSApple OSS Distributions {
85bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
86bb611c8fSApple OSS Distributions auto& ref = ptr += 0;
87bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
88bb611c8fSApple OSS Distributions _assert(ptr == array.end());
89bb611c8fSApple OSS Distributions }
90bb611c8fSApple OSS Distributions {
91bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
92bb611c8fSApple OSS Distributions auto& ref = ptr += -1;
93bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
94bb611c8fSApple OSS Distributions _assert(&*ptr == &array[4]);
95bb611c8fSApple OSS Distributions }
96bb611c8fSApple OSS Distributions {
97bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
98bb611c8fSApple OSS Distributions auto& ref = ptr += -2;
99bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
100bb611c8fSApple OSS Distributions _assert(&*ptr == &array[3]);
101bb611c8fSApple OSS Distributions }
102bb611c8fSApple OSS Distributions {
103bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
104bb611c8fSApple OSS Distributions auto& ref = ptr += -3;
105bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
106bb611c8fSApple OSS Distributions _assert(&*ptr == &array[2]);
107bb611c8fSApple OSS Distributions }
108bb611c8fSApple OSS Distributions {
109bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
110bb611c8fSApple OSS Distributions auto& ref = ptr += -4;
111bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
112bb611c8fSApple OSS Distributions _assert(&*ptr == &array[1]);
113bb611c8fSApple OSS Distributions }
114bb611c8fSApple OSS Distributions {
115bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.end(), array.begin(), array.end());
116bb611c8fSApple OSS Distributions auto& ref = ptr += -5;
117bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
118bb611c8fSApple OSS Distributions _assert(&*ptr == &array[0]);
119bb611c8fSApple OSS Distributions }
120bb611c8fSApple OSS Distributions
121bb611c8fSApple OSS Distributions // Make sure we trap on arithmetic overflow in the number of bytes calculation
122bb611c8fSApple OSS Distributions {
123bb611c8fSApple OSS Distributions std::ptrdiff_t sizeof_T = sizeof(T); // avoid promotion to unsigned in calculations
124bb611c8fSApple OSS Distributions
125bb611c8fSApple OSS Distributions // largest (most positive) n for the number of bytes `n * sizeof(T)` not to overflow ptrdiff_t
126bb611c8fSApple OSS Distributions std::ptrdiff_t max_n = std::numeric_limits<std::ptrdiff_t>::max() / sizeof_T;
127bb611c8fSApple OSS Distributions
128bb611c8fSApple OSS Distributions // smallest (most negative) n for the number of bytes `n * sizeof(T)` not to overflow ptrdiff_t
129bb611c8fSApple OSS Distributions std::ptrdiff_t min_n = std::numeric_limits<std::ptrdiff_t>::min() / sizeof_T;
130bb611c8fSApple OSS Distributions
131bb611c8fSApple OSS Distributions // Overflow with a positive offset
132bb611c8fSApple OSS Distributions {
133bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin(), array.end());
134bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
135bb611c8fSApple OSS Distributions ptr += max_n + 1;
136bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
137bb611c8fSApple OSS Distributions }
138bb611c8fSApple OSS Distributions
139bb611c8fSApple OSS Distributions // Overflow with a negative offset
140bb611c8fSApple OSS Distributions {
141bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin(), array.end());
142bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
143bb611c8fSApple OSS Distributions ptr += min_n - 1;
144bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
145bb611c8fSApple OSS Distributions }
146bb611c8fSApple OSS Distributions }
147bb611c8fSApple OSS Distributions
148bb611c8fSApple OSS Distributions // Make sure we trap on arithmetic overflow in the offset calculation
149bb611c8fSApple OSS Distributions //
150bb611c8fSApple OSS Distributions // To avoid running into the overflow of `n * sizeof(T)` when ptrdiff_t
151bb611c8fSApple OSS Distributions // is the same size as int32_t, we test the offset overflow check by
152bb611c8fSApple OSS Distributions // successive addition of smaller offsets.
153bb611c8fSApple OSS Distributions //
154bb611c8fSApple OSS Distributions // We basically push the offset right to its limit, and then push it
155bb611c8fSApple OSS Distributions // past its limit to watch it overflow.
156bb611c8fSApple OSS Distributions {
157bb611c8fSApple OSS Distributions std::int64_t sizeof_T = sizeof(T); // avoid promotion to unsigned in calculations
158bb611c8fSApple OSS Distributions
159bb611c8fSApple OSS Distributions // largest (most positive) n for the number of bytes `n * sizeof(T)` not to overflow the int32_t offset
160bb611c8fSApple OSS Distributions std::int64_t max_n = std::numeric_limits<std::int32_t>::max() / sizeof_T;
161bb611c8fSApple OSS Distributions
162bb611c8fSApple OSS Distributions // smallest (most negative) n for the number of bytes `n * sizeof(T)` not to overflow the int32_t offset
163bb611c8fSApple OSS Distributions std::int64_t min_n = std::numeric_limits<std::int32_t>::min() / sizeof_T;
164bb611c8fSApple OSS Distributions
165bb611c8fSApple OSS Distributions // Add positive offsets
166bb611c8fSApple OSS Distributions {
167bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin(), array.end());
168bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
169bb611c8fSApple OSS Distributions ptr += static_cast<ptrdiff_t>(max_n / 2);
170bb611c8fSApple OSS Distributions _assert(!tracking_policy::did_trap);
171bb611c8fSApple OSS Distributions ptr += static_cast<ptrdiff_t>(max_n / 2);
172bb611c8fSApple OSS Distributions _assert(!tracking_policy::did_trap);
173bb611c8fSApple OSS Distributions ptr += (max_n % 2);
174bb611c8fSApple OSS Distributions _assert(!tracking_policy::did_trap); // offset is now right at its positive limit
175bb611c8fSApple OSS Distributions ptr += 1;
176bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
177bb611c8fSApple OSS Distributions }
178bb611c8fSApple OSS Distributions
179bb611c8fSApple OSS Distributions // Add negative offsets
180bb611c8fSApple OSS Distributions {
181bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin(), array.end());
182bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
183bb611c8fSApple OSS Distributions ptr += static_cast<ptrdiff_t>(min_n / 2);
184bb611c8fSApple OSS Distributions _assert(!tracking_policy::did_trap);
185bb611c8fSApple OSS Distributions ptr += static_cast<ptrdiff_t>(min_n / 2);
186bb611c8fSApple OSS Distributions _assert(!tracking_policy::did_trap);
187bb611c8fSApple OSS Distributions ptr += (min_n % 2);
188bb611c8fSApple OSS Distributions _assert(!tracking_policy::did_trap); // offset is now right at its negative limit
189bb611c8fSApple OSS Distributions ptr += -1;
190bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
191bb611c8fSApple OSS Distributions }
192bb611c8fSApple OSS Distributions }
193bb611c8fSApple OSS Distributions }
194bb611c8fSApple OSS Distributions
195*8d741a5dSApple OSS Distributions T_DECL(arith_add_assign, "bounded_ptr.arith.add_assign", T_META_TAG_VM_PREFERRED) {
196bb611c8fSApple OSS Distributions tests<T, T>();
197bb611c8fSApple OSS Distributions tests<T, T const>();
198bb611c8fSApple OSS Distributions tests<T, T volatile>();
199bb611c8fSApple OSS Distributions tests<T, T const volatile>();
200bb611c8fSApple OSS Distributions }
201