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