Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------- fuzzing.cpp -------------------------------===// |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 11 | // A set of routines to use when fuzzing the algorithms in libc++ |
| 12 | // Each one tests a single algorithm. |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 13 | // |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 14 | // They all have the form of: |
| 15 | // int `algorithm`(const uint8_t *data, size_t size); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 16 | // |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 17 | // They perform the operation, and then check to see if the results are correct. |
| 18 | // If so, they return zero, and non-zero otherwise. |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 19 | // |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 20 | // For example, sort calls std::sort, then checks two things: |
| 21 | // (1) The resulting vector is sorted |
| 22 | // (2) The resulting vector contains the same elements as the original data. |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | |
| 26 | #include "fuzzing.h" |
| 27 | #include <vector> |
| 28 | #include <algorithm> |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 29 | #include <functional> |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 30 | #include <regex> |
Marshall Clow | 294dd86 | 2017-11-08 20:25:47 +0000 | [diff] [blame] | 31 | #include <cassert> |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 32 | |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 33 | #include <iostream> |
| 34 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 35 | // If we had C++14, we could use the four iterator version of is_permutation and equal |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 36 | |
| 37 | namespace fuzzing { |
| 38 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 39 | // This is a struct we can use to test the stable_XXX algorithms. |
| 40 | // perform the operation on the key, then check the order of the payload. |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 41 | |
| 42 | struct stable_test { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 43 | uint8_t key; |
| 44 | size_t payload; |
| 45 | |
| 46 | stable_test(uint8_t k) : key(k), payload(0) {} |
| 47 | stable_test(uint8_t k, size_t p) : key(k), payload(p) {} |
| 48 | }; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 49 | |
| 50 | void swap(stable_test &lhs, stable_test &rhs) |
| 51 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 52 | using std::swap; |
| 53 | swap(lhs.key, rhs.key); |
| 54 | swap(lhs.payload, rhs.payload); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | struct key_less |
| 58 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 59 | bool operator () (const stable_test &lhs, const stable_test &rhs) const |
| 60 | { |
| 61 | return lhs.key < rhs.key; |
| 62 | } |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | struct payload_less |
| 66 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 67 | bool operator () (const stable_test &lhs, const stable_test &rhs) const |
| 68 | { |
| 69 | return lhs.payload < rhs.payload; |
| 70 | } |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | struct total_less |
| 74 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 75 | bool operator () (const stable_test &lhs, const stable_test &rhs) const |
| 76 | { |
| 77 | return lhs.key == rhs.key ? lhs.payload < rhs.payload : lhs.key < rhs.key; |
| 78 | } |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | bool operator==(const stable_test &lhs, const stable_test &rhs) |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 82 | { |
| 83 | return lhs.key == rhs.key && lhs.payload == rhs.payload; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
| 87 | template<typename T> |
| 88 | struct is_even |
| 89 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 90 | bool operator () (const T &t) const |
| 91 | { |
| 92 | return t % 2 == 0; |
| 93 | } |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | |
| 97 | template<> |
| 98 | struct is_even<stable_test> |
| 99 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 100 | bool operator () (const stable_test &t) const |
| 101 | { |
| 102 | return t.key % 2 == 0; |
| 103 | } |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 106 | typedef std::vector<uint8_t> Vec; |
| 107 | typedef std::vector<stable_test> StableVec; |
Marshall Clow | 161681a | 2018-01-19 03:17:45 +0000 | [diff] [blame] | 108 | typedef StableVec::const_iterator SVIter; |
| 109 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 110 | // Cheap version of is_permutation |
| 111 | // Builds a set of buckets for each of the key values. |
| 112 | // Sums all the payloads. |
| 113 | // Not 100% perfect, but _way_ faster |
Marshall Clow | 161681a | 2018-01-19 03:17:45 +0000 | [diff] [blame] | 114 | bool is_permutation(SVIter first1, SVIter last1, SVIter first2) |
| 115 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 116 | size_t xBuckets[256] = {0}; |
| 117 | size_t xPayloads[256] = {0}; |
| 118 | size_t yBuckets[256] = {0}; |
| 119 | size_t yPayloads[256] = {0}; |
Marshall Clow | 161681a | 2018-01-19 03:17:45 +0000 | [diff] [blame] | 120 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 121 | for (; first1 != last1; ++first1, ++first2) |
| 122 | { |
| 123 | xBuckets [first1->key]++; |
| 124 | xPayloads[first1->key] += first1->payload; |
Marshall Clow | 161681a | 2018-01-19 03:17:45 +0000 | [diff] [blame] | 125 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 126 | yBuckets [first2->key]++; |
| 127 | yPayloads[first2->key] += first2->payload; |
| 128 | } |
| 129 | |
| 130 | for (size_t i = 0; i < 256; ++i) |
| 131 | { |
| 132 | if (xBuckets[i] != yBuckets[i]) |
| 133 | return false; |
| 134 | if (xPayloads[i] != yPayloads[i]) |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | return true; |
Marshall Clow | 161681a | 2018-01-19 03:17:45 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | template <typename Iter1, typename Iter2> |
| 142 | bool is_permutation(Iter1 first1, Iter1 last1, Iter2 first2) |
| 143 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 144 | static_assert((std::is_same<typename std::iterator_traits<Iter1>::value_type, uint8_t>::value), ""); |
| 145 | static_assert((std::is_same<typename std::iterator_traits<Iter2>::value_type, uint8_t>::value), ""); |
Marshall Clow | 161681a | 2018-01-19 03:17:45 +0000 | [diff] [blame] | 146 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 147 | size_t xBuckets[256] = {0}; |
| 148 | size_t yBuckets[256] = {0}; |
| 149 | |
| 150 | for (; first1 != last1; ++first1, ++first2) |
| 151 | { |
| 152 | xBuckets [*first1]++; |
| 153 | yBuckets [*first2]++; |
| 154 | } |
| 155 | |
| 156 | for (size_t i = 0; i < 256; ++i) |
| 157 | if (xBuckets[i] != yBuckets[i]) |
| 158 | return false; |
| 159 | |
| 160 | return true; |
Marshall Clow | 161681a | 2018-01-19 03:17:45 +0000 | [diff] [blame] | 161 | } |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 162 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 163 | // == sort == |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 164 | int sort(const uint8_t *data, size_t size) |
| 165 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 166 | Vec working(data, data + size); |
| 167 | std::sort(working.begin(), working.end()); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 168 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 169 | if (!std::is_sorted(working.begin(), working.end())) return 1; |
| 170 | if (!fuzzing::is_permutation(data, data + size, working.cbegin())) return 99; |
| 171 | return 0; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 175 | // == stable_sort == |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 176 | int stable_sort(const uint8_t *data, size_t size) |
| 177 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 178 | StableVec input; |
| 179 | for (size_t i = 0; i < size; ++i) |
| 180 | input.push_back(stable_test(data[i], i)); |
| 181 | StableVec working = input; |
| 182 | std::stable_sort(working.begin(), working.end(), key_less()); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 183 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 184 | if (!std::is_sorted(working.begin(), working.end(), key_less())) return 1; |
| 185 | auto iter = working.begin(); |
| 186 | while (iter != working.end()) |
| 187 | { |
| 188 | auto range = std::equal_range(iter, working.end(), *iter, key_less()); |
| 189 | if (!std::is_sorted(range.first, range.second, total_less())) return 2; |
| 190 | iter = range.second; |
| 191 | } |
| 192 | if (!fuzzing::is_permutation(input.cbegin(), input.cend(), working.cbegin())) return 99; |
| 193 | return 0; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 196 | // == partition == |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 197 | int partition(const uint8_t *data, size_t size) |
| 198 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 199 | Vec working(data, data + size); |
| 200 | auto iter = std::partition(working.begin(), working.end(), is_even<uint8_t>()); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 201 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 202 | if (!std::all_of (working.begin(), iter, is_even<uint8_t>())) return 1; |
| 203 | if (!std::none_of(iter, working.end(), is_even<uint8_t>())) return 2; |
| 204 | if (!fuzzing::is_permutation(data, data + size, working.cbegin())) return 99; |
| 205 | return 0; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 209 | // == partition_copy == |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 210 | int partition_copy(const uint8_t *data, size_t size) |
| 211 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 212 | Vec v1, v2; |
| 213 | auto iter = std::partition_copy(data, data + size, |
| 214 | std::back_inserter<Vec>(v1), std::back_inserter<Vec>(v2), |
| 215 | is_even<uint8_t>()); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 216 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 217 | // The two vectors should add up to the original size |
| 218 | if (v1.size() + v2.size() != size) return 1; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 219 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 220 | // All of the even values should be in the first vector, and none in the second |
| 221 | if (!std::all_of (v1.begin(), v1.end(), is_even<uint8_t>())) return 2; |
| 222 | if (!std::none_of(v2.begin(), v2.end(), is_even<uint8_t>())) return 3; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 223 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 224 | // Every value in both vectors has to be in the original |
| 225 | |
| 226 | // Make a copy of the input, and sort it |
| 227 | Vec v0{data, data + size}; |
| 228 | std::sort(v0.begin(), v0.end()); |
| 229 | |
| 230 | // Sort each vector and ensure that all of the elements appear in the original input |
| 231 | std::sort(v1.begin(), v1.end()); |
| 232 | if (!std::includes(v0.begin(), v0.end(), v1.begin(), v1.end())) return 4; |
| 233 | |
| 234 | std::sort(v2.begin(), v2.end()); |
| 235 | if (!std::includes(v0.begin(), v0.end(), v2.begin(), v2.end())) return 5; |
| 236 | |
| 237 | // This, while simple, is really slow - 20 seconds on a 500K element input. |
| 238 | // for (auto v: v1) |
| 239 | // if (std::find(data, data + size, v) == data + size) return 4; |
| 240 | // |
| 241 | // for (auto v: v2) |
| 242 | // if (std::find(data, data + size, v) == data + size) return 5; |
| 243 | |
| 244 | return 0; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 247 | // == stable_partition == |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 248 | int stable_partition (const uint8_t *data, size_t size) |
| 249 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 250 | StableVec input; |
| 251 | for (size_t i = 0; i < size; ++i) |
| 252 | input.push_back(stable_test(data[i], i)); |
| 253 | StableVec working = input; |
| 254 | auto iter = std::stable_partition(working.begin(), working.end(), is_even<stable_test>()); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 255 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 256 | if (!std::all_of (working.begin(), iter, is_even<stable_test>())) return 1; |
| 257 | if (!std::none_of(iter, working.end(), is_even<stable_test>())) return 2; |
| 258 | if (!std::is_sorted(working.begin(), iter, payload_less())) return 3; |
| 259 | if (!std::is_sorted(iter, working.end(), payload_less())) return 4; |
| 260 | if (!fuzzing::is_permutation(input.cbegin(), input.cend(), working.cbegin())) return 99; |
| 261 | return 0; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 264 | // == nth_element == |
| 265 | // use the first element as a position into the data |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 266 | int nth_element (const uint8_t *data, size_t size) |
| 267 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 268 | if (size <= 1) return 0; |
| 269 | const size_t partition_point = data[0] % size; |
| 270 | Vec working(data + 1, data + size); |
| 271 | const auto partition_iter = working.begin() + partition_point; |
| 272 | std::nth_element(working.begin(), partition_iter, working.end()); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 273 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 274 | // nth may be the end iterator, in this case nth_element has no effect. |
| 275 | if (partition_iter == working.end()) |
| 276 | { |
| 277 | if (!std::equal(data + 1, data + size, working.begin())) return 98; |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | const uint8_t nth = *partition_iter; |
| 282 | if (!std::all_of(working.begin(), partition_iter, [=](uint8_t v) { return v <= nth; })) |
| 283 | return 1; |
| 284 | if (!std::all_of(partition_iter, working.end(), [=](uint8_t v) { return v >= nth; })) |
| 285 | return 2; |
| 286 | if (!fuzzing::is_permutation(data + 1, data + size, working.cbegin())) return 99; |
| 287 | } |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 288 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 289 | return 0; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 292 | // == partial_sort == |
| 293 | // use the first element as a position into the data |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 294 | int partial_sort (const uint8_t *data, size_t size) |
| 295 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 296 | if (size <= 1) return 0; |
| 297 | const size_t sort_point = data[0] % size; |
| 298 | Vec working(data + 1, data + size); |
| 299 | const auto sort_iter = working.begin() + sort_point; |
| 300 | std::partial_sort(working.begin(), sort_iter, working.end()); |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 301 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 302 | if (sort_iter != working.end()) |
| 303 | { |
| 304 | const uint8_t nth = *std::min_element(sort_iter, working.end()); |
| 305 | if (!std::all_of(working.begin(), sort_iter, [=](uint8_t v) { return v <= nth; })) |
| 306 | return 1; |
| 307 | if (!std::all_of(sort_iter, working.end(), [=](uint8_t v) { return v >= nth; })) |
| 308 | return 2; |
| 309 | } |
| 310 | if (!std::is_sorted(working.begin(), sort_iter)) return 3; |
| 311 | if (!fuzzing::is_permutation(data + 1, data + size, working.cbegin())) return 99; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 312 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 313 | return 0; |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 316 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 317 | // == partial_sort_copy == |
| 318 | // use the first element as a count |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 319 | int partial_sort_copy (const uint8_t *data, size_t size) |
| 320 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 321 | if (size <= 1) return 0; |
| 322 | const size_t num_results = data[0] % size; |
| 323 | Vec results(num_results); |
| 324 | (void) std::partial_sort_copy(data + 1, data + size, results.begin(), results.end()); |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 325 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 326 | // The results have to be sorted |
| 327 | if (!std::is_sorted(results.begin(), results.end())) return 1; |
| 328 | // All the values in results have to be in the original data |
| 329 | for (auto v: results) |
| 330 | if (std::find(data + 1, data + size, v) == data + size) return 2; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 331 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 332 | // The things in results have to be the smallest N in the original data |
| 333 | Vec sorted(data + 1, data + size); |
| 334 | std::sort(sorted.begin(), sorted.end()); |
| 335 | if (!std::equal(results.begin(), results.end(), sorted.begin())) return 3; |
| 336 | return 0; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 339 | // The second sequence has been "uniqued" |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 340 | template <typename Iter1, typename Iter2> |
| 341 | static bool compare_unique(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2) |
| 342 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 343 | assert(first1 != last1 && first2 != last2); |
| 344 | if (*first1 != *first2) return false; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 345 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 346 | uint8_t last_value = *first1; |
| 347 | ++first1; ++first2; |
| 348 | while(first1 != last1 && first2 != last2) |
| 349 | { |
| 350 | // Skip over dups in the first sequence |
| 351 | while (*first1 == last_value) |
| 352 | if (++first1 == last1) return false; |
| 353 | if (*first1 != *first2) return false; |
| 354 | last_value = *first1; |
| 355 | ++first1; ++first2; |
| 356 | } |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 357 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 358 | // Still stuff left in the 'uniqued' sequence - oops |
| 359 | if (first1 == last1 && first2 != last2) return false; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 360 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 361 | // Still stuff left in the original sequence - better be all the same |
| 362 | while (first1 != last1) |
| 363 | { |
| 364 | if (*first1 != last_value) return false; |
| 365 | ++first1; |
| 366 | } |
| 367 | return true; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 370 | // == unique == |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 371 | int unique (const uint8_t *data, size_t size) |
| 372 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 373 | Vec working(data, data + size); |
| 374 | std::sort(working.begin(), working.end()); |
| 375 | Vec results = working; |
| 376 | Vec::iterator new_end = std::unique(results.begin(), results.end()); |
| 377 | Vec::iterator it; // scratch iterator |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 378 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 379 | // Check the size of the unique'd sequence. |
| 380 | // it should only be zero if the input sequence was empty. |
| 381 | if (results.begin() == new_end) |
| 382 | return working.size() == 0 ? 0 : 1; |
| 383 | |
| 384 | // 'results' is sorted |
| 385 | if (!std::is_sorted(results.begin(), new_end)) return 2; |
| 386 | |
| 387 | // All the elements in 'results' must be different |
| 388 | it = results.begin(); |
| 389 | uint8_t prev_value = *it++; |
| 390 | for (; it != new_end; ++it) |
| 391 | { |
| 392 | if (*it == prev_value) return 3; |
| 393 | prev_value = *it; |
| 394 | } |
| 395 | |
| 396 | // Every element in 'results' must be in 'working' |
| 397 | for (it = results.begin(); it != new_end; ++it) |
| 398 | if (std::find(working.begin(), working.end(), *it) == working.end()) |
| 399 | return 4; |
| 400 | |
| 401 | // Every element in 'working' must be in 'results' |
| 402 | for (auto v : working) |
| 403 | if (std::find(results.begin(), new_end, v) == new_end) |
| 404 | return 5; |
| 405 | |
| 406 | return 0; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 409 | // == unique_copy == |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 410 | int unique_copy (const uint8_t *data, size_t size) |
| 411 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 412 | Vec working(data, data + size); |
| 413 | std::sort(working.begin(), working.end()); |
| 414 | Vec results; |
| 415 | (void) std::unique_copy(working.begin(), working.end(), |
| 416 | std::back_inserter<Vec>(results)); |
| 417 | Vec::iterator it; // scratch iterator |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 418 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 419 | // Check the size of the unique'd sequence. |
| 420 | // it should only be zero if the input sequence was empty. |
| 421 | if (results.size() == 0) |
| 422 | return working.size() == 0 ? 0 : 1; |
| 423 | |
| 424 | // 'results' is sorted |
| 425 | if (!std::is_sorted(results.begin(), results.end())) return 2; |
| 426 | |
| 427 | // All the elements in 'results' must be different |
| 428 | it = results.begin(); |
| 429 | uint8_t prev_value = *it++; |
| 430 | for (; it != results.end(); ++it) |
| 431 | { |
| 432 | if (*it == prev_value) return 3; |
| 433 | prev_value = *it; |
| 434 | } |
| 435 | |
| 436 | // Every element in 'results' must be in 'working' |
| 437 | for (auto v : results) |
| 438 | if (std::find(working.begin(), working.end(), v) == working.end()) |
| 439 | return 4; |
| 440 | |
| 441 | // Every element in 'working' must be in 'results' |
| 442 | for (auto v : working) |
| 443 | if (std::find(results.begin(), results.end(), v) == results.end()) |
| 444 | return 5; |
| 445 | |
| 446 | return 0; |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 450 | // -- regex fuzzers |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 451 | static int regex_helper(const uint8_t *data, size_t size, std::regex::flag_type flag) |
| 452 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 453 | if (size > 0) |
| 454 | { |
| 455 | try |
| 456 | { |
| 457 | std::string s((const char *)data, size); |
| 458 | std::regex re(s, flag); |
| 459 | return std::regex_match(s, re) ? 1 : 0; |
| 460 | } |
| 461 | catch (std::regex_error &ex) {} |
| 462 | } |
| 463 | return 0; |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
| 467 | int regex_ECMAScript (const uint8_t *data, size_t size) |
| 468 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 469 | (void) regex_helper(data, size, std::regex_constants::ECMAScript); |
| 470 | return 0; |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | int regex_POSIX (const uint8_t *data, size_t size) |
| 474 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 475 | (void) regex_helper(data, size, std::regex_constants::basic); |
| 476 | return 0; |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | int regex_extended (const uint8_t *data, size_t size) |
| 480 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 481 | (void) regex_helper(data, size, std::regex_constants::extended); |
| 482 | return 0; |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | int regex_awk (const uint8_t *data, size_t size) |
| 486 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 487 | (void) regex_helper(data, size, std::regex_constants::awk); |
| 488 | return 0; |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | int regex_grep (const uint8_t *data, size_t size) |
| 492 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 493 | (void) regex_helper(data, size, std::regex_constants::grep); |
| 494 | return 0; |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | int regex_egrep (const uint8_t *data, size_t size) |
| 498 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 499 | (void) regex_helper(data, size, std::regex_constants::egrep); |
| 500 | return 0; |
Marshall Clow | d84736b | 2017-10-12 14:48:09 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 503 | // -- heap fuzzers |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 504 | int make_heap (const uint8_t *data, size_t size) |
| 505 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 506 | Vec working(data, data + size); |
| 507 | std::make_heap(working.begin(), working.end()); |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 508 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 509 | if (!std::is_heap(working.begin(), working.end())) return 1; |
| 510 | if (!fuzzing::is_permutation(data, data + size, working.cbegin())) return 99; |
| 511 | return 0; |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | int push_heap (const uint8_t *data, size_t size) |
| 515 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 516 | if (size < 2) return 0; |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 517 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 518 | // Make a heap from the first half of the data |
| 519 | Vec working(data, data + size); |
| 520 | auto iter = working.begin() + (size / 2); |
| 521 | std::make_heap(working.begin(), iter); |
| 522 | if (!std::is_heap(working.begin(), iter)) return 1; |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 523 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 524 | // Now push the rest onto the heap, one at a time |
| 525 | ++iter; |
| 526 | for (; iter != working.end(); ++iter) { |
| 527 | std::push_heap(working.begin(), iter); |
| 528 | if (!std::is_heap(working.begin(), iter)) return 2; |
| 529 | } |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 530 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 531 | if (!fuzzing::is_permutation(data, data + size, working.cbegin())) return 99; |
| 532 | return 0; |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | int pop_heap (const uint8_t *data, size_t size) |
| 536 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 537 | if (size < 2) return 0; |
| 538 | Vec working(data, data + size); |
| 539 | std::make_heap(working.begin(), working.end()); |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 540 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 541 | // Pop things off, one at a time |
| 542 | auto iter = --working.end(); |
| 543 | while (iter != working.begin()) { |
| 544 | std::pop_heap(working.begin(), iter); |
| 545 | if (!std::is_heap(working.begin(), --iter)) return 2; |
| 546 | } |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 547 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 548 | return 0; |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 552 | // -- search fuzzers |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 553 | int search (const uint8_t *data, size_t size) |
| 554 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 555 | if (size < 2) return 0; |
| 556 | |
| 557 | const size_t pat_size = data[0] * (size - 1) / std::numeric_limits<uint8_t>::max(); |
| 558 | assert(pat_size <= size - 1); |
| 559 | const uint8_t *pat_begin = data + 1; |
| 560 | const uint8_t *pat_end = pat_begin + pat_size; |
| 561 | const uint8_t *data_end = data + size; |
| 562 | assert(pat_end <= data_end); |
| 563 | // std::cerr << "data[0] = " << size_t(data[0]) << " "; |
| 564 | // std::cerr << "Pattern size = " << pat_size << "; corpus is " << size - 1 << std::endl; |
| 565 | auto it = std::search(pat_end, data_end, pat_begin, pat_end); |
| 566 | if (it != data_end) // not found |
| 567 | if (!std::equal(pat_begin, pat_end, it)) |
| 568 | return 1; |
| 569 | return 0; |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | template <typename S> |
| 573 | static int search_helper (const uint8_t *data, size_t size) |
| 574 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 575 | if (size < 2) return 0; |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 576 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 577 | const size_t pat_size = data[0] * (size - 1) / std::numeric_limits<uint8_t>::max(); |
| 578 | const uint8_t *pat_begin = data + 1; |
| 579 | const uint8_t *pat_end = pat_begin + pat_size; |
| 580 | const uint8_t *data_end = data + size; |
| 581 | |
| 582 | auto it = std::search(pat_end, data_end, S(pat_begin, pat_end)); |
| 583 | if (it != data_end) // not found |
| 584 | if (!std::equal(pat_begin, pat_end, it)) |
| 585 | return 1; |
| 586 | return 0; |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 589 | // These are still in std::experimental |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 590 | // int search_boyer_moore (const uint8_t *data, size_t size) |
| 591 | // { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 592 | // return search_helper<std::boyer_moore_searcher<const uint8_t *>>(data, size); |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 593 | // } |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 594 | // |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 595 | // int search_boyer_moore_horspool (const uint8_t *data, size_t size) |
| 596 | // { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 597 | // return search_helper<std::boyer_moore_horspool_searcher<const uint8_t *>>(data, size); |
Marshall Clow | a97ab84 | 2017-10-23 23:19:30 +0000 | [diff] [blame] | 598 | // } |
| 599 | |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 600 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 601 | // -- set operation fuzzers |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 602 | template <typename S> |
| 603 | static void set_helper (const uint8_t *data, size_t size, Vec &v1, Vec &v2) |
| 604 | { |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 605 | assert(size > 1); |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 606 | |
Marshall Clow | 43ff7d9 | 2018-11-04 17:57:25 +0000 | [diff] [blame] | 607 | const size_t pat_size = data[0] * (size - 1) / std::numeric_limits<uint8_t>::max(); |
| 608 | const uint8_t *pat_begin = data + 1; |
| 609 | const uint8_t *pat_end = pat_begin + pat_size; |
| 610 | const uint8_t *data_end = data + size; |
| 611 | v1.assign(pat_begin, pat_end); |
| 612 | v2.assign(pat_end, data_end); |
| 613 | |
| 614 | std::sort(v1.begin(), v1.end()); |
| 615 | std::sort(v2.begin(), v2.end()); |
Marshall Clow | 974677c | 2017-10-30 19:51:58 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Marshall Clow | baa6fb3 | 2017-10-04 22:23:03 +0000 | [diff] [blame] | 618 | } // namespace fuzzing |