1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions // T& operator*() const;
4bb611c8fSApple OSS Distributions // T* operator->() const;
5bb611c8fSApple OSS Distributions //
6bb611c8fSApple OSS Distributions
7bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
8bb611c8fSApple OSS Distributions #include <array>
9bb611c8fSApple OSS Distributions #include <darwintest.h>
10bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
11bb611c8fSApple OSS Distributions #include "test_utils.h"
12bb611c8fSApple OSS Distributions
13bb611c8fSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
14bb611c8fSApple OSS Distributions
15bb611c8fSApple OSS Distributions struct T {
16bb611c8fSApple OSS Distributions int i;
17bb611c8fSApple OSS Distributions friend constexpr bool
operator ==(T const volatile & a,T const & b)18bb611c8fSApple OSS Distributions operator==(T const volatile& a, T const& b)
19bb611c8fSApple OSS Distributions {
20bb611c8fSApple OSS Distributions return a.i == b.i;
21bb611c8fSApple OSS Distributions }
22bb611c8fSApple OSS Distributions };
23bb611c8fSApple OSS Distributions
24bb611c8fSApple OSS Distributions namespace {
25bb611c8fSApple OSS Distributions struct tracking_policy {
26bb611c8fSApple OSS Distributions static bool did_trap;
27bb611c8fSApple OSS Distributions static void
trap__anon8f0c52910111::tracking_policy28bb611c8fSApple OSS Distributions trap(char const*)
29bb611c8fSApple OSS Distributions {
30bb611c8fSApple OSS Distributions did_trap = true;
31bb611c8fSApple OSS Distributions }
32bb611c8fSApple OSS Distributions };
33bb611c8fSApple OSS Distributions bool tracking_policy::did_trap = false;
34bb611c8fSApple OSS Distributions }
35bb611c8fSApple OSS Distributions
36bb611c8fSApple OSS Distributions template <typename T, typename QualT>
37bb611c8fSApple OSS Distributions static void
tests()38bb611c8fSApple OSS Distributions tests()
39bb611c8fSApple OSS Distributions {
40bb611c8fSApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
41bb611c8fSApple OSS Distributions
42bb611c8fSApple OSS Distributions {
43bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
44bb611c8fSApple OSS Distributions // ^ ^
45bb611c8fSApple OSS Distributions // | |
46bb611c8fSApple OSS Distributions // begin, ptr end
47bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin() + 0, array.begin(), array.end());
48bb611c8fSApple OSS Distributions QualT& ref = *ptr;
49bb611c8fSApple OSS Distributions _assert(ref == T{0});
50bb611c8fSApple OSS Distributions _assert(&ref == &array[0]);
51bb611c8fSApple OSS Distributions
52bb611c8fSApple OSS Distributions _assert(ptr->i == 0);
53bb611c8fSApple OSS Distributions _assert(&ptr->i == &array[0].i);
54bb611c8fSApple OSS Distributions }
55bb611c8fSApple OSS Distributions {
56bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
57bb611c8fSApple OSS Distributions // ^ ^ ^
58bb611c8fSApple OSS Distributions // | | |
59bb611c8fSApple OSS Distributions // begin ptr end
60bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin() + 1, array.begin(), array.end());
61bb611c8fSApple OSS Distributions QualT& ref = *ptr;
62bb611c8fSApple OSS Distributions _assert(ref == T{1});
63bb611c8fSApple OSS Distributions _assert(&ref == &array[1]);
64bb611c8fSApple OSS Distributions
65bb611c8fSApple OSS Distributions _assert(ptr->i == 1);
66bb611c8fSApple OSS Distributions _assert(&ptr->i == &array[1].i);
67bb611c8fSApple OSS Distributions }
68bb611c8fSApple OSS Distributions {
69bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
70bb611c8fSApple OSS Distributions // ^ ^ ^
71bb611c8fSApple OSS Distributions // | | |
72bb611c8fSApple OSS Distributions // begin ptr end
73bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin() + 2, array.begin(), array.end());
74bb611c8fSApple OSS Distributions QualT& ref = *ptr;
75bb611c8fSApple OSS Distributions _assert(ref == T{2});
76bb611c8fSApple OSS Distributions _assert(&ref == &array[2]);
77bb611c8fSApple OSS Distributions
78bb611c8fSApple OSS Distributions _assert(ptr->i == 2);
79bb611c8fSApple OSS Distributions _assert(&ptr->i == &array[2].i);
80bb611c8fSApple OSS Distributions }
81bb611c8fSApple OSS Distributions {
82bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
83bb611c8fSApple OSS Distributions // ^ ^ ^
84bb611c8fSApple OSS Distributions // | | |
85bb611c8fSApple OSS Distributions // begin ptr end
86bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin() + 4, array.begin(), array.end());
87bb611c8fSApple OSS Distributions QualT& ref = *ptr;
88bb611c8fSApple OSS Distributions _assert(ref == T{4});
89bb611c8fSApple OSS Distributions _assert(&ref == &array[4]);
90bb611c8fSApple OSS Distributions
91bb611c8fSApple OSS Distributions _assert(ptr->i == 4);
92bb611c8fSApple OSS Distributions _assert(&ptr->i == &array[4].i);
93bb611c8fSApple OSS Distributions }
94bb611c8fSApple OSS Distributions
95bb611c8fSApple OSS Distributions // Make sure we don't trap when dereferencing an in-bounds pointer
96bb611c8fSApple OSS Distributions {
97bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
98bb611c8fSApple OSS Distributions // ^ ^ ^
99bb611c8fSApple OSS Distributions // | | |
100bb611c8fSApple OSS Distributions // begin ptr end
101bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin() + 1, array.begin(), array.end());
102bb611c8fSApple OSS Distributions
103bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
104bb611c8fSApple OSS Distributions (void)*ptr;
105bb611c8fSApple OSS Distributions (void)ptr->i;
106bb611c8fSApple OSS Distributions _assert(!tracking_policy::did_trap);
107bb611c8fSApple OSS Distributions }
108bb611c8fSApple OSS Distributions
109bb611c8fSApple OSS Distributions // Make sure we trap when dereferencing an out-of-bounds pointer
110bb611c8fSApple OSS Distributions {
111bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
112bb611c8fSApple OSS Distributions // ^ ^ ^
113bb611c8fSApple OSS Distributions // | | |
114bb611c8fSApple OSS Distributions // begin end ptr
115bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.end() - 1, array.begin(), array.end() - 2);
116bb611c8fSApple OSS Distributions
117bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
118bb611c8fSApple OSS Distributions (void)*ptr;
119bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
120bb611c8fSApple OSS Distributions
121bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
122bb611c8fSApple OSS Distributions (void)ptr->i;
123bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
124bb611c8fSApple OSS Distributions }
125bb611c8fSApple OSS Distributions {
126bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
127bb611c8fSApple OSS Distributions // ^ ^ ^
128bb611c8fSApple OSS Distributions // | | |
129bb611c8fSApple OSS Distributions // ptr begin end
130bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin() + 1, array.end());
131bb611c8fSApple OSS Distributions
132bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
133bb611c8fSApple OSS Distributions (void)*ptr;
134bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
135bb611c8fSApple OSS Distributions
136bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
137bb611c8fSApple OSS Distributions (void)ptr->i;
138bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
139bb611c8fSApple OSS Distributions }
140bb611c8fSApple OSS Distributions {
141bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
142bb611c8fSApple OSS Distributions // ^ ^ ^
143bb611c8fSApple OSS Distributions // | (just a bit off) | |
144bb611c8fSApple OSS Distributions // begin ptr end
145bb611c8fSApple OSS Distributions T* t3 = const_cast<T*>(array.begin() + 3);
146bb611c8fSApple OSS Distributions char* just_off = reinterpret_cast<char*>(t3) + 1; // 1 byte off
147bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(reinterpret_cast<QualT*>(just_off), array.begin(), array.end() - 1);
148bb611c8fSApple OSS Distributions
149bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
150bb611c8fSApple OSS Distributions (void)*ptr;
151bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
152bb611c8fSApple OSS Distributions
153bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
154bb611c8fSApple OSS Distributions (void)ptr->i;
155bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
156bb611c8fSApple OSS Distributions }
157bb611c8fSApple OSS Distributions }
158bb611c8fSApple OSS Distributions
159*8d741a5dSApple OSS Distributions T_DECL(deref, "bounded_ptr.deref", T_META_TAG_VM_PREFERRED) {
160bb611c8fSApple OSS Distributions tests<T, T>();
161bb611c8fSApple OSS Distributions tests<T, T const>();
162bb611c8fSApple OSS Distributions tests<T, T volatile>();
163bb611c8fSApple OSS Distributions tests<T, T const volatile>();
164bb611c8fSApple OSS Distributions
165bb611c8fSApple OSS Distributions // Make sure that we don't hard-error in the definition of operator*
166bb611c8fSApple OSS Distributions // when instantiating a `bounded_ptr<cv-void>`
167bb611c8fSApple OSS Distributions test_bounded_ptr<void> p1;
168bb611c8fSApple OSS Distributions test_bounded_ptr<void const> p2;
169bb611c8fSApple OSS Distributions test_bounded_ptr<void volatile> p3;
170bb611c8fSApple OSS Distributions test_bounded_ptr<void const volatile> p4;
171bb611c8fSApple OSS Distributions }
172