1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions // T* discard_bounds() const;
4bb611c8fSApple OSS Distributions //
5bb611c8fSApple OSS Distributions
6bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
7bb611c8fSApple OSS Distributions #include <array>
8bb611c8fSApple OSS Distributions #include <darwintest.h>
9bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
10bb611c8fSApple OSS Distributions #include "test_utils.h"
11bb611c8fSApple OSS Distributions
12bb611c8fSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
13bb611c8fSApple OSS Distributions
14bb611c8fSApple OSS Distributions struct T { int i; };
15bb611c8fSApple OSS Distributions
16bb611c8fSApple OSS Distributions namespace {
17bb611c8fSApple OSS Distributions struct tracking_policy {
18bb611c8fSApple OSS Distributions static bool did_trap;
19bb611c8fSApple OSS Distributions static void
trap__anon855895ef0111::tracking_policy20bb611c8fSApple OSS Distributions trap(char const*)
21bb611c8fSApple OSS Distributions {
22bb611c8fSApple OSS Distributions did_trap = true;
23bb611c8fSApple OSS Distributions }
24bb611c8fSApple OSS Distributions };
25bb611c8fSApple OSS Distributions bool tracking_policy::did_trap = false;
26bb611c8fSApple OSS Distributions }
27bb611c8fSApple OSS Distributions
28bb611c8fSApple OSS Distributions template <typename T, typename QualT>
29bb611c8fSApple OSS Distributions static void
tests()30bb611c8fSApple OSS Distributions tests()
31bb611c8fSApple OSS Distributions {
32bb611c8fSApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
33bb611c8fSApple OSS Distributions
34bb611c8fSApple OSS Distributions {
35bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
36bb611c8fSApple OSS Distributions // ^ ^
37bb611c8fSApple OSS Distributions // | |
38bb611c8fSApple OSS Distributions // begin, ptr end
39bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin() + 0, array.begin(), array.end());
40bb611c8fSApple OSS Distributions QualT* raw = ptr.discard_bounds();
41bb611c8fSApple OSS Distributions _assert(raw == &array[0]);
42bb611c8fSApple OSS Distributions }
43bb611c8fSApple OSS Distributions {
44bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
45bb611c8fSApple OSS Distributions // ^ ^ ^
46bb611c8fSApple OSS Distributions // | | |
47bb611c8fSApple OSS Distributions // begin ptr end
48bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin() + 1, array.begin(), array.end());
49bb611c8fSApple OSS Distributions QualT* raw = ptr.discard_bounds();
50bb611c8fSApple OSS Distributions _assert(raw == &array[1]);
51bb611c8fSApple OSS Distributions }
52bb611c8fSApple OSS Distributions {
53bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
54bb611c8fSApple OSS Distributions // ^ ^ ^
55bb611c8fSApple OSS Distributions // | | |
56bb611c8fSApple OSS Distributions // begin ptr end
57bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin() + 2, array.begin(), array.end());
58bb611c8fSApple OSS Distributions QualT* raw = ptr.discard_bounds();
59bb611c8fSApple OSS Distributions _assert(raw == &array[2]);
60bb611c8fSApple OSS Distributions }
61bb611c8fSApple OSS Distributions {
62bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
63bb611c8fSApple OSS Distributions // ^ ^ ^
64bb611c8fSApple OSS Distributions // | | |
65bb611c8fSApple OSS Distributions // begin ptr end
66bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin() + 4, array.begin(), array.end());
67bb611c8fSApple OSS Distributions QualT* raw = ptr.discard_bounds();
68bb611c8fSApple OSS Distributions _assert(raw == &array[4]);
69bb611c8fSApple OSS Distributions }
70bb611c8fSApple OSS Distributions // Make sure we don't trap when discarding the bounds of an in-bounds pointer
71bb611c8fSApple OSS Distributions {
72bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
73bb611c8fSApple OSS Distributions // ^ ^ ^
74bb611c8fSApple OSS Distributions // | | |
75bb611c8fSApple OSS Distributions // begin ptr end
76bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin() + 1, array.begin(), array.end());
77bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
78bb611c8fSApple OSS Distributions (void)*ptr;
79bb611c8fSApple OSS Distributions (void)ptr->i;
80bb611c8fSApple OSS Distributions _assert(!tracking_policy::did_trap);
81bb611c8fSApple OSS Distributions }
82bb611c8fSApple OSS Distributions
83bb611c8fSApple OSS Distributions // Make sure we trap when discarding the bounds of an out-of-bounds pointer
84bb611c8fSApple OSS Distributions {
85bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
86bb611c8fSApple OSS Distributions // ^ ^ ^
87bb611c8fSApple OSS Distributions // | | |
88bb611c8fSApple OSS Distributions // begin end ptr
89bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.end() - 1, array.begin(), array.end() - 2);
90bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
91bb611c8fSApple OSS Distributions (void)ptr.discard_bounds();
92bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
93bb611c8fSApple OSS Distributions }
94bb611c8fSApple OSS Distributions {
95bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
96bb611c8fSApple OSS Distributions // ^ ^ ^
97bb611c8fSApple OSS Distributions // | | |
98bb611c8fSApple OSS Distributions // ptr begin end
99bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(array.begin(), array.begin() + 1, array.end());
100bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
101bb611c8fSApple OSS Distributions (void)ptr.discard_bounds();
102bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
103bb611c8fSApple OSS Distributions }
104bb611c8fSApple OSS Distributions {
105bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
106bb611c8fSApple OSS Distributions // ^ ^ ^
107bb611c8fSApple OSS Distributions // | (just a bit off) | |
108bb611c8fSApple OSS Distributions // begin ptr end
109bb611c8fSApple OSS Distributions T* t3 = const_cast<T*>(array.begin() + 3);
110bb611c8fSApple OSS Distributions char* just_off = reinterpret_cast<char*>(t3) + 1; // 1 byte off
111bb611c8fSApple OSS Distributions libkern::bounded_ptr<QualT, tracking_policy> ptr(reinterpret_cast<QualT*>(just_off), array.begin(), array.end() - 1);
112bb611c8fSApple OSS Distributions
113bb611c8fSApple OSS Distributions tracking_policy::did_trap = false;
114bb611c8fSApple OSS Distributions (void)ptr.discard_bounds();
115bb611c8fSApple OSS Distributions _assert(tracking_policy::did_trap);
116bb611c8fSApple OSS Distributions }
117bb611c8fSApple OSS Distributions }
118bb611c8fSApple OSS Distributions
119*8d741a5dSApple OSS Distributions T_DECL(discard_bounds, "bounded_ptr.discard_bounds", T_META_TAG_VM_PREFERRED) {
120bb611c8fSApple OSS Distributions tests<T, T>();
121bb611c8fSApple OSS Distributions tests<T, T const>();
122bb611c8fSApple OSS Distributions tests<T, T volatile>();
123bb611c8fSApple OSS Distributions tests<T, T const volatile>();
124bb611c8fSApple OSS Distributions }
125