blob: d4d0e602cc4a79b23d2d13d1bcda7a7c52aa3880 [file] [log] [blame]
Marshall Clowbaa6fb32017-10-04 22:23:03 +00001// -*- 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>
Marshall Clowd84736b2017-10-12 14:48:09 +000029#include <regex>
Marshall Clowbaa6fb32017-10-04 22:23:03 +000030
31// If we had C++14, we could use the four iterator version of is_permutation
32
33namespace fuzzing {
34
35// This is a struct we can use to test the stable_XXX algorithms.
36// perform the operation on the key, then check the order of the payload.
37
38struct stable_test {
39 uint8_t key;
40 uint8_t payload;
41
42 stable_test(uint8_t k) : key(k), payload(0) {}
43 stable_test(uint8_t k, uint8_t p) : key(k), payload(p) {}
44 };
45
46void swap(stable_test &lhs, stable_test &rhs)
47{
48 using std::swap;
49 swap(lhs.key, rhs.key);
50 swap(lhs.payload, rhs.payload);
51}
52
53struct key_less
54{
55 bool operator () (const stable_test &lhs, const stable_test &rhs) const
56 {
57 return lhs.key < rhs.key;
58 }
59};
60
61struct payload_less
62{
63 bool operator () (const stable_test &lhs, const stable_test &rhs) const
64 {
65 return lhs.payload < rhs.payload;
66 }
67};
68
69struct total_less
70{
71 bool operator () (const stable_test &lhs, const stable_test &rhs) const
72 {
73 return lhs.key == rhs.key ? lhs.payload < rhs.payload : lhs.key < rhs.key;
74 }
75};
76
77bool operator==(const stable_test &lhs, const stable_test &rhs)
78{
79 return lhs.key == rhs.key && lhs.payload == rhs.payload;
80}
81
82
83template<typename T>
84struct is_even
85{
86 bool operator () (const T &t) const
87 {
88 return t % 2 == 0;
89 }
90};
91
92
93template<>
94struct is_even<stable_test>
95{
96 bool operator () (const stable_test &t) const
97 {
98 return t.key % 2 == 0;
99 }
100};
101
102// == sort ==
103
104int sort(const uint8_t *data, size_t size)
105{
106 std::vector<uint8_t> working(data, data + size);
107 std::sort(working.begin(), working.end());
108
109 if (!std::is_sorted(working.begin(), working.end())) return 1;
110 if (!std::is_permutation(data, data + size, working.begin())) return 99;
111 return 0;
112}
113
114
115// == stable_sort ==
116
117int stable_sort(const uint8_t *data, size_t size)
118{
119 std::vector<stable_test> input;
120 for (size_t i = 0; i < size; ++i)
121 input.push_back(stable_test(data[i], i));
122 std::vector<stable_test> working = input;
123 std::stable_sort(working.begin(), working.end(), key_less());
124
125 if (!std::is_sorted(working.begin(), working.end(), key_less())) return 1;
126 auto iter = working.begin();
127 while (iter != working.end())
128 {
129 auto range = std::equal_range(iter, working.end(), *iter, key_less());
130 if (!std::is_sorted(range.first, range.second, total_less())) return 2;
131 iter = range.second;
132 }
133 if (!std::is_permutation(input.begin(), input.end(), working.begin())) return 99;
134 return 0;
135}
136
137// == partition ==
138
139int partition(const uint8_t *data, size_t size)
140{
141 std::vector<uint8_t> working(data, data + size);
142 auto iter = std::partition(working.begin(), working.end(), is_even<uint8_t>());
143
144 if (!std::all_of (working.begin(), iter, is_even<uint8_t>())) return 1;
145 if (!std::none_of(iter, working.end(), is_even<uint8_t>())) return 2;
146 if (!std::is_permutation(data, data + size, working.begin())) return 99;
147 return 0;
148}
149
150
151// == stable_partition ==
152
153int stable_partition (const uint8_t *data, size_t size)
154{
155 std::vector<stable_test> input;
156 for (size_t i = 0; i < size; ++i)
157 input.push_back(stable_test(data[i], i));
158 std::vector<stable_test> working = input;
159 auto iter = std::stable_partition(working.begin(), working.end(), is_even<stable_test>());
160
161 if (!std::all_of (working.begin(), iter, is_even<stable_test>())) return 1;
162 if (!std::none_of(iter, working.end(), is_even<stable_test>())) return 2;
163 if (!std::is_sorted(working.begin(), iter, payload_less())) return 3;
164 if (!std::is_sorted(iter, working.end(), payload_less())) return 4;
165 if (!std::is_permutation(input.begin(), input.end(), working.begin())) return 99;
166 return 0;
167}
168
169// == nth_element ==
170// use the first element as a position into the data
171int nth_element (const uint8_t *data, size_t size)
172{
173 if (size <= 1) return 0;
174 const size_t partition_point = data[0] % size;
175 std::vector<uint8_t> working(data + 1, data + size);
176 const auto partition_iter = working.begin() + partition_point;
177 std::nth_element(working.begin(), partition_iter, working.end());
178
179// nth may be the end iterator, in this case nth_element has no effect.
180 if (partition_iter == working.end())
181 {
182 if (!std::equal(data + 1, data + size, working.begin())) return 98;
183 }
184 else
185 {
186 const uint8_t nth = *partition_iter;
187 if (!std::all_of(working.begin(), partition_iter, [=](uint8_t v) { return v <= nth; }))
188 return 1;
189 if (!std::all_of(partition_iter, working.end(), [=](uint8_t v) { return v >= nth; }))
190 return 2;
191 if (!std::is_permutation(data + 1, data + size, working.begin())) return 99;
192 }
193
194 return 0;
195}
196
197// == partial_sort ==
198// use the first element as a position into the data
199int partial_sort (const uint8_t *data, size_t size)
200{
201 if (size <= 1) return 0;
202 const size_t sort_point = data[0] % size;
203 std::vector<uint8_t> working(data + 1, data + size);
204 const auto sort_iter = working.begin() + sort_point;
205 std::partial_sort(working.begin(), sort_iter, working.end());
206
207 if (sort_iter != working.end())
208 {
209 const uint8_t nth = *std::min_element(sort_iter, working.end());
210 if (!std::all_of(working.begin(), sort_iter, [=](uint8_t v) { return v <= nth; }))
211 return 1;
212 if (!std::all_of(sort_iter, working.end(), [=](uint8_t v) { return v >= nth; }))
213 return 2;
214 }
215 if (!std::is_sorted(working.begin(), sort_iter)) return 3;
216 if (!std::is_permutation(data + 1, data + size, working.begin())) return 99;
217
218 return 0;
219}
220
Marshall Clowd84736b2017-10-12 14:48:09 +0000221
222// -- regex fuzzers
223
224static int regex_helper(const uint8_t *data, size_t size, std::regex::flag_type flag)
225{
226 if (size > 0)
227 {
228 try
229 {
230 std::string s((const char *)data, size);
231 std::regex re(s, flag);
232 return std::regex_match(s, re) ? 1 : 0;
233 }
234 catch (std::regex_error &ex) {}
235 }
236 return 0;
237}
238
239
240int regex_ECMAScript (const uint8_t *data, size_t size)
241{
242 (void) regex_helper(data, size, std::regex_constants::ECMAScript);
243 return 0;
244}
245
246int regex_POSIX (const uint8_t *data, size_t size)
247{
248 (void) regex_helper(data, size, std::regex_constants::basic);
249 return 0;
250}
251
252int regex_extended (const uint8_t *data, size_t size)
253{
254 (void) regex_helper(data, size, std::regex_constants::extended);
255 return 0;
256}
257
258int regex_awk (const uint8_t *data, size_t size)
259{
260 (void) regex_helper(data, size, std::regex_constants::awk);
261 return 0;
262}
263
264int regex_grep (const uint8_t *data, size_t size)
265{
266 (void) regex_helper(data, size, std::regex_constants::grep);
267 return 0;
268}
269
270int regex_egrep (const uint8_t *data, size_t size)
271{
272 (void) regex_helper(data, size, std::regex_constants::egrep);
273 return 0;
274}
275
Marshall Clowbaa6fb32017-10-04 22:23:03 +0000276} // namespace fuzzing