]>
git.saurik.com Git - apple/xnu.git/blob - tests/bounded_ptr_src/deref.cpp
3 // T& operator*() const;
4 // T* operator->() const;
7 #include <libkern/c++/bounded_ptr.h>
9 #include <darwintest.h>
10 #include <darwintest_utils.h>
11 #include "test_utils.h"
13 #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
18 operator==(T
const volatile& a
, T
const& b
)
25 struct tracking_policy
{
33 bool tracking_policy::did_trap
= false;
36 template <typename T
, typename QualT
>
40 std::array
<T
, 5> array
= {T
{0}, T
{1}, T
{2}, T
{3}, T
{4}};
43 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
47 test_bounded_ptr
<QualT
> ptr(array
.begin() + 0, array
.begin(), array
.end());
50 _assert(&ref
== &array
[0]);
53 _assert(&ptr
->i
== &array
[0].i
);
56 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
60 test_bounded_ptr
<QualT
> ptr(array
.begin() + 1, array
.begin(), array
.end());
63 _assert(&ref
== &array
[1]);
66 _assert(&ptr
->i
== &array
[1].i
);
69 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
73 test_bounded_ptr
<QualT
> ptr(array
.begin() + 2, array
.begin(), array
.end());
76 _assert(&ref
== &array
[2]);
79 _assert(&ptr
->i
== &array
[2].i
);
82 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
86 test_bounded_ptr
<QualT
> ptr(array
.begin() + 4, array
.begin(), array
.end());
89 _assert(&ref
== &array
[4]);
92 _assert(&ptr
->i
== &array
[4].i
);
95 // Make sure we don't trap when dereferencing an in-bounds pointer
97 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
101 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.begin() + 1, array
.begin(), array
.end());
103 tracking_policy::did_trap
= false;
106 _assert(!tracking_policy::did_trap
);
109 // Make sure we trap when dereferencing an out-of-bounds pointer
111 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
115 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.end() - 1, array
.begin(), array
.end() - 2);
117 tracking_policy::did_trap
= false;
119 _assert(tracking_policy::did_trap
);
121 tracking_policy::did_trap
= false;
123 _assert(tracking_policy::did_trap
);
126 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
130 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.begin(), array
.begin() + 1, array
.end());
132 tracking_policy::did_trap
= false;
134 _assert(tracking_policy::did_trap
);
136 tracking_policy::did_trap
= false;
138 _assert(tracking_policy::did_trap
);
141 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
143 // | (just a bit off) | |
145 T
* t3
= const_cast<T
*>(array
.begin() + 3);
146 char* just_off
= reinterpret_cast<char*>(t3
) + 1; // 1 byte off
147 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(reinterpret_cast<QualT
*>(just_off
), array
.begin(), array
.end() - 1);
149 tracking_policy::did_trap
= false;
151 _assert(tracking_policy::did_trap
);
153 tracking_policy::did_trap
= false;
155 _assert(tracking_policy::did_trap
);
159 T_DECL(deref
, "bounded_ptr.deref") {
162 tests
<T
, T
volatile>();
163 tests
<T
, T
const volatile>();
165 // Make sure that we don't hard-error in the definition of operator*
166 // when instantiating a `bounded_ptr<cv-void>`
167 test_bounded_ptr
<void> p1
;
168 test_bounded_ptr
<void const> p2
;
169 test_bounded_ptr
<void volatile> p3
;
170 test_bounded_ptr
<void const volatile> p4
;