Marshall Clow | 29ae2ba | 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 | |
| 11 | // A set of routines to use when fuzzing the algorithms in libc++ |
| 12 | // Each one tests a single algorithm. |
| 13 | // |
| 14 | // They all have the form of: |
| 15 | // int `algorithm`(const uint8_t *data, size_t size); |
| 16 | // |
| 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. |
| 19 | // |
| 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. |
| 23 | |
| 24 | |
| 25 | |
| 26 | #include "fuzzing.h" |
| 27 | #include <vector> |
| 28 | #include <algorithm> |
| 29 | |
| 30 | #include <iostream> |
| 31 | |
| 32 | // If we had C++14, we could use the four iterator version of is_permutation |
| 33 | |
| 34 | namespace fuzzing { |
| 35 | |
| 36 | // This is a struct we can use to test the stable_XXX algorithms. |
| 37 | // perform the operation on the key, then check the order of the payload. |
| 38 | |
| 39 | struct stable_test { |
| 40 | uint8_t key; |
| 41 | uint8_t payload; |
| 42 | |
| 43 | stable_test(uint8_t k) : key(k), payload(0) {} |
| 44 | stable_test(uint8_t k, uint8_t p) : key(k), payload(p) {} |
| 45 | }; |
| 46 | |
| 47 | void swap(stable_test &lhs, stable_test &rhs) |
| 48 | { |
| 49 | using std::swap; |
| 50 | swap(lhs.key, rhs.key); |
| 51 | swap(lhs.payload, rhs.payload); |
| 52 | } |
| 53 | |
| 54 | struct key_less |
| 55 | { |
| 56 | bool operator () (const stable_test &lhs, const stable_test &rhs) const |
| 57 | { |
| 58 | return lhs.key < rhs.key; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | struct payload_less |
| 63 | { |
| 64 | bool operator () (const stable_test &lhs, const stable_test &rhs) const |
| 65 | { |
| 66 | return lhs.payload < rhs.payload; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | struct total_less |
| 71 | { |
| 72 | bool operator () (const stable_test &lhs, const stable_test &rhs) const |
| 73 | { |
| 74 | return lhs.key == rhs.key ? lhs.payload < rhs.payload : lhs.key < rhs.key; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | bool operator==(const stable_test &lhs, const stable_test &rhs) |
| 79 | { |
| 80 | return lhs.key == rhs.key && lhs.payload == rhs.payload; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | template<typename T> |
| 85 | struct is_even |
| 86 | { |
| 87 | bool operator () (const T &t) const |
| 88 | { |
| 89 | return t % 2 == 0; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | template<> |
| 95 | struct is_even<stable_test> |
| 96 | { |
| 97 | bool operator () (const stable_test &t) const |
| 98 | { |
| 99 | return t.key % 2 == 0; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | // == sort == |
| 104 | |
| 105 | int sort(const uint8_t *data, size_t size) |
| 106 | { |
| 107 | std::vector<uint8_t> working(data, data + size); |
| 108 | std::sort(working.begin(), working.end()); |
| 109 | |
| 110 | if (!std::is_sorted(working.begin(), working.end())) return 1; |
| 111 | if (!std::is_permutation(data, data + size, working.begin())) return 99; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // == stable_sort == |
| 117 | |
| 118 | int stable_sort(const uint8_t *data, size_t size) |
| 119 | { |
| 120 | std::vector<stable_test> input; |
| 121 | for (size_t i = 0; i < size; ++i) |
| 122 | input.push_back(stable_test(data[i], i)); |
| 123 | std::vector<stable_test> working = input; |
| 124 | std::stable_sort(working.begin(), working.end(), key_less()); |
| 125 | |
| 126 | if (!std::is_sorted(working.begin(), working.end(), key_less())) return 1; |
| 127 | auto iter = working.begin(); |
| 128 | while (iter != working.end()) |
| 129 | { |
| 130 | auto range = std::equal_range(iter, working.end(), *iter, key_less()); |
| 131 | if (!std::is_sorted(range.first, range.second, total_less())) return 2; |
| 132 | iter = range.second; |
| 133 | } |
| 134 | if (!std::is_permutation(input.begin(), input.end(), working.begin())) return 99; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | // == partition == |
| 139 | |
| 140 | int partition(const uint8_t *data, size_t size) |
| 141 | { |
| 142 | std::vector<uint8_t> working(data, data + size); |
| 143 | auto iter = std::partition(working.begin(), working.end(), is_even<uint8_t>()); |
| 144 | |
| 145 | if (!std::all_of (working.begin(), iter, is_even<uint8_t>())) return 1; |
| 146 | if (!std::none_of(iter, working.end(), is_even<uint8_t>())) return 2; |
| 147 | if (!std::is_permutation(data, data + size, working.begin())) return 99; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | // == stable_partition == |
| 153 | |
| 154 | int stable_partition (const uint8_t *data, size_t size) |
| 155 | { |
| 156 | std::vector<stable_test> input; |
| 157 | for (size_t i = 0; i < size; ++i) |
| 158 | input.push_back(stable_test(data[i], i)); |
| 159 | std::vector<stable_test> working = input; |
| 160 | auto iter = std::stable_partition(working.begin(), working.end(), is_even<stable_test>()); |
| 161 | |
| 162 | if (!std::all_of (working.begin(), iter, is_even<stable_test>())) return 1; |
| 163 | if (!std::none_of(iter, working.end(), is_even<stable_test>())) return 2; |
| 164 | if (!std::is_sorted(working.begin(), iter, payload_less())) return 3; |
| 165 | if (!std::is_sorted(iter, working.end(), payload_less())) return 4; |
| 166 | if (!std::is_permutation(input.begin(), input.end(), working.begin())) return 99; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | // == nth_element == |
| 171 | // use the first element as a position into the data |
| 172 | int nth_element (const uint8_t *data, size_t size) |
| 173 | { |
| 174 | if (size <= 1) return 0; |
| 175 | const size_t partition_point = data[0] % size; |
| 176 | std::vector<uint8_t> working(data + 1, data + size); |
| 177 | const auto partition_iter = working.begin() + partition_point; |
| 178 | std::nth_element(working.begin(), partition_iter, working.end()); |
| 179 | |
| 180 | // nth may be the end iterator, in this case nth_element has no effect. |
| 181 | if (partition_iter == working.end()) |
| 182 | { |
| 183 | if (!std::equal(data + 1, data + size, working.begin())) return 98; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | const uint8_t nth = *partition_iter; |
| 188 | if (!std::all_of(working.begin(), partition_iter, [=](uint8_t v) { return v <= nth; })) |
| 189 | return 1; |
| 190 | if (!std::all_of(partition_iter, working.end(), [=](uint8_t v) { return v >= nth; })) |
| 191 | return 2; |
| 192 | if (!std::is_permutation(data + 1, data + size, working.begin())) return 99; |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | // == partial_sort == |
| 199 | // use the first element as a position into the data |
| 200 | int partial_sort (const uint8_t *data, size_t size) |
| 201 | { |
| 202 | if (size <= 1) return 0; |
| 203 | const size_t sort_point = data[0] % size; |
| 204 | std::vector<uint8_t> working(data + 1, data + size); |
| 205 | const auto sort_iter = working.begin() + sort_point; |
| 206 | std::partial_sort(working.begin(), sort_iter, working.end()); |
| 207 | |
| 208 | if (sort_iter != working.end()) |
| 209 | { |
| 210 | const uint8_t nth = *std::min_element(sort_iter, working.end()); |
| 211 | if (!std::all_of(working.begin(), sort_iter, [=](uint8_t v) { return v <= nth; })) |
| 212 | return 1; |
| 213 | if (!std::all_of(sort_iter, working.end(), [=](uint8_t v) { return v >= nth; })) |
| 214 | return 2; |
| 215 | } |
| 216 | if (!std::is_sorted(working.begin(), sort_iter)) return 3; |
| 217 | if (!std::is_permutation(data + 1, data + size, working.begin())) return 99; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | } // namespace fuzzing |