fuzzing.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. // A set of routines to use when fuzzing the algorithms in libc++
  11. // Each one tests a single algorithm.
  12. //
  13. // They all have the form of:
  14. // int `algorithm`(const uint8_t *data, size_t size);
  15. //
  16. // They perform the operation, and then check to see if the results are correct.
  17. // If so, they return zero, and non-zero otherwise.
  18. //
  19. // For example, sort calls std::sort, then checks two things:
  20. // (1) The resulting vector is sorted
  21. // (2) The resulting vector contains the same elements as the original data.
  22. #include "fuzzing.h"
  23. #include <vector>
  24. #include <algorithm>
  25. #include <functional>
  26. #include <regex>
  27. #include <iostream>
  28. // If we had C++14, we could use the four iterator version of is_permutation and equal
  29. namespace fuzzing {
  30. // This is a struct we can use to test the stable_XXX algorithms.
  31. // perform the operation on the key, then check the order of the payload.
  32. struct stable_test {
  33. uint8_t key;
  34. size_t payload;
  35. stable_test(uint8_t k) : key(k), payload(0) {}
  36. stable_test(uint8_t k, size_t p) : key(k), payload(p) {}
  37. };
  38. void swap(stable_test &lhs, stable_test &rhs)
  39. {
  40. using std::swap;
  41. swap(lhs.key, rhs.key);
  42. swap(lhs.payload, rhs.payload);
  43. }
  44. struct key_less
  45. {
  46. bool operator () (const stable_test &lhs, const stable_test &rhs) const
  47. {
  48. return lhs.key < rhs.key;
  49. }
  50. };
  51. struct payload_less
  52. {
  53. bool operator () (const stable_test &lhs, const stable_test &rhs) const
  54. {
  55. return lhs.payload < rhs.payload;
  56. }
  57. };
  58. struct total_less
  59. {
  60. bool operator () (const stable_test &lhs, const stable_test &rhs) const
  61. {
  62. return lhs.key == rhs.key ? lhs.payload < rhs.payload : lhs.key < rhs.key;
  63. }
  64. };
  65. bool operator==(const stable_test &lhs, const stable_test &rhs)
  66. {
  67. return lhs.key == rhs.key && lhs.payload == rhs.payload;
  68. }
  69. template<typename T>
  70. struct is_even
  71. {
  72. bool operator () (const T &t) const
  73. {
  74. return t % 2 == 0;
  75. }
  76. };
  77. template<>
  78. struct is_even<stable_test>
  79. {
  80. bool operator () (const stable_test &t) const
  81. {
  82. return t.key % 2 == 0;
  83. }
  84. };
  85. typedef std::vector<uint8_t> Vec;
  86. typedef std::vector<stable_test> StableVec;
  87. // == sort ==
  88. int sort(const uint8_t *data, size_t size)
  89. {
  90. Vec working(data, data + size);
  91. std::sort(working.begin(), working.end());
  92. if (!std::is_sorted(working.begin(), working.end())) return 1;
  93. if (!std::is_permutation(data, data + size, working.begin())) return 99;
  94. return 0;
  95. }
  96. // == stable_sort ==
  97. int stable_sort(const uint8_t *data, size_t size)
  98. {
  99. StableVec input;
  100. for (size_t i = 0; i < size; ++i)
  101. input.push_back(stable_test(data[i], i));
  102. StableVec working = input;
  103. std::stable_sort(working.begin(), working.end(), key_less());
  104. if (!std::is_sorted(working.begin(), working.end(), key_less())) return 1;
  105. auto iter = working.begin();
  106. while (iter != working.end())
  107. {
  108. auto range = std::equal_range(iter, working.end(), *iter, key_less());
  109. if (!std::is_sorted(range.first, range.second, total_less())) return 2;
  110. iter = range.second;
  111. }
  112. if (!std::is_permutation(input.begin(), input.end(), working.begin())) return 99;
  113. return 0;
  114. }
  115. // == partition ==
  116. int partition(const uint8_t *data, size_t size)
  117. {
  118. Vec working(data, data + size);
  119. auto iter = std::partition(working.begin(), working.end(), is_even<uint8_t>());
  120. if (!std::all_of (working.begin(), iter, is_even<uint8_t>())) return 1;
  121. if (!std::none_of(iter, working.end(), is_even<uint8_t>())) return 2;
  122. if (!std::is_permutation(data, data + size, working.begin())) return 99;
  123. return 0;
  124. }
  125. // == partition_copy ==
  126. int partition_copy(const uint8_t *data, size_t size)
  127. {
  128. Vec v1, v2;
  129. auto iter = std::partition_copy(data, data + size,
  130. std::back_inserter<Vec>(v1), std::back_inserter<Vec>(v2),
  131. is_even<uint8_t>());
  132. // The two vectors should add up to the original size
  133. if (v1.size() + v2.size() != size) return 1;
  134. // All of the even values should be in the first vector, and none in the second
  135. if (!std::all_of (v1.begin(), v1.end(), is_even<uint8_t>())) return 2;
  136. if (!std::none_of(v2.begin(), v2.end(), is_even<uint8_t>())) return 3;
  137. // Every value in both vectors has to be in the original
  138. for (auto v: v1)
  139. if (std::find(data, data + size, v) == data + size) return 4;
  140. for (auto v: v2)
  141. if (std::find(data, data + size, v) == data + size) return 5;
  142. return 0;
  143. }
  144. // == stable_partition ==
  145. int stable_partition (const uint8_t *data, size_t size)
  146. {
  147. StableVec input;
  148. for (size_t i = 0; i < size; ++i)
  149. input.push_back(stable_test(data[i], i));
  150. StableVec working = input;
  151. auto iter = std::stable_partition(working.begin(), working.end(), is_even<stable_test>());
  152. if (!std::all_of (working.begin(), iter, is_even<stable_test>())) return 1;
  153. if (!std::none_of(iter, working.end(), is_even<stable_test>())) return 2;
  154. if (!std::is_sorted(working.begin(), iter, payload_less())) return 3;
  155. if (!std::is_sorted(iter, working.end(), payload_less())) return 4;
  156. if (!std::is_permutation(input.begin(), input.end(), working.begin())) return 99;
  157. return 0;
  158. }
  159. // == nth_element ==
  160. // use the first element as a position into the data
  161. int nth_element (const uint8_t *data, size_t size)
  162. {
  163. if (size <= 1) return 0;
  164. const size_t partition_point = data[0] % size;
  165. Vec working(data + 1, data + size);
  166. const auto partition_iter = working.begin() + partition_point;
  167. std::nth_element(working.begin(), partition_iter, working.end());
  168. // nth may be the end iterator, in this case nth_element has no effect.
  169. if (partition_iter == working.end())
  170. {
  171. if (!std::equal(data + 1, data + size, working.begin())) return 98;
  172. }
  173. else
  174. {
  175. const uint8_t nth = *partition_iter;
  176. if (!std::all_of(working.begin(), partition_iter, [=](uint8_t v) { return v <= nth; }))
  177. return 1;
  178. if (!std::all_of(partition_iter, working.end(), [=](uint8_t v) { return v >= nth; }))
  179. return 2;
  180. if (!std::is_permutation(data + 1, data + size, working.begin())) return 99;
  181. }
  182. return 0;
  183. }
  184. // == partial_sort ==
  185. // use the first element as a position into the data
  186. int partial_sort (const uint8_t *data, size_t size)
  187. {
  188. if (size <= 1) return 0;
  189. const size_t sort_point = data[0] % size;
  190. Vec working(data + 1, data + size);
  191. const auto sort_iter = working.begin() + sort_point;
  192. std::partial_sort(working.begin(), sort_iter, working.end());
  193. if (sort_iter != working.end())
  194. {
  195. const uint8_t nth = *std::min_element(sort_iter, working.end());
  196. if (!std::all_of(working.begin(), sort_iter, [=](uint8_t v) { return v <= nth; }))
  197. return 1;
  198. if (!std::all_of(sort_iter, working.end(), [=](uint8_t v) { return v >= nth; }))
  199. return 2;
  200. }
  201. if (!std::is_sorted(working.begin(), sort_iter)) return 3;
  202. if (!std::is_permutation(data + 1, data + size, working.begin())) return 99;
  203. return 0;
  204. }
  205. // == partial_sort_copy ==
  206. // use the first element as a count
  207. int partial_sort_copy (const uint8_t *data, size_t size)
  208. {
  209. if (size <= 1) return 0;
  210. const size_t num_results = data[0] % size;
  211. Vec results(num_results);
  212. (void) std::partial_sort_copy(data + 1, data + size, results.begin(), results.end());
  213. // The results have to be sorted
  214. if (!std::is_sorted(results.begin(), results.end())) return 1;
  215. // All the values in results have to be in the original data
  216. for (auto v: results)
  217. if (std::find(data + 1, data + size, v) == data + size) return 2;
  218. // The things in results have to be the smallest N in the original data
  219. Vec sorted(data + 1, data + size);
  220. std::sort(sorted.begin(), sorted.end());
  221. if (!std::equal(results.begin(), results.end(), sorted.begin())) return 3;
  222. return 0;
  223. }
  224. // The second sequence has been "uniqued"
  225. template <typename Iter1, typename Iter2>
  226. static bool compare_unique(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2)
  227. {
  228. assert(first1 != last1 && first2 != last2);
  229. if (*first1 != *first2) return false;
  230. uint8_t last_value = *first1;
  231. ++first1; ++first2;
  232. while(first1 != last1 && first2 != last2)
  233. {
  234. // Skip over dups in the first sequence
  235. while (*first1 == last_value)
  236. if (++first1 == last1) return false;
  237. if (*first1 != *first2) return false;
  238. last_value = *first1;
  239. ++first1; ++first2;
  240. }
  241. // Still stuff left in the 'uniqued' sequence - oops
  242. if (first1 == last1 && first2 != last2) return false;
  243. // Still stuff left in the original sequence - better be all the same
  244. while (first1 != last1)
  245. {
  246. if (*first1 != last_value) return false;
  247. ++first1;
  248. }
  249. return true;
  250. }
  251. // == unique ==
  252. int unique (const uint8_t *data, size_t size)
  253. {
  254. Vec working(data, data + size);
  255. std::sort(working.begin(), working.end());
  256. Vec results = working;
  257. Vec::iterator new_end = std::unique(results.begin(), results.end());
  258. Vec::iterator it; // scratch iterator
  259. // Check the size of the unique'd sequence.
  260. // it should only be zero if the input sequence was empty.
  261. if (results.begin() == new_end)
  262. return working.size() == 0 ? 0 : 1;
  263. // 'results' is sorted
  264. if (!std::is_sorted(results.begin(), new_end)) return 2;
  265. // All the elements in 'results' must be different
  266. it = results.begin();
  267. uint8_t prev_value = *it++;
  268. for (; it != new_end; ++it)
  269. {
  270. if (*it == prev_value) return 3;
  271. prev_value = *it;
  272. }
  273. // Every element in 'results' must be in 'working'
  274. for (it = results.begin(); it != new_end; ++it)
  275. if (std::find(working.begin(), working.end(), *it) == working.end())
  276. return 4;
  277. // Every element in 'working' must be in 'results'
  278. for (auto v : working)
  279. if (std::find(results.begin(), new_end, v) == new_end)
  280. return 5;
  281. return 0;
  282. }
  283. // == unique_copy ==
  284. int unique_copy (const uint8_t *data, size_t size)
  285. {
  286. Vec working(data, data + size);
  287. std::sort(working.begin(), working.end());
  288. Vec results;
  289. (void) std::unique_copy(working.begin(), working.end(),
  290. std::back_inserter<Vec>(results));
  291. Vec::iterator it; // scratch iterator
  292. // Check the size of the unique'd sequence.
  293. // it should only be zero if the input sequence was empty.
  294. if (results.size() == 0)
  295. return working.size() == 0 ? 0 : 1;
  296. // 'results' is sorted
  297. if (!std::is_sorted(results.begin(), results.end())) return 2;
  298. // All the elements in 'results' must be different
  299. it = results.begin();
  300. uint8_t prev_value = *it++;
  301. for (; it != results.end(); ++it)
  302. {
  303. if (*it == prev_value) return 3;
  304. prev_value = *it;
  305. }
  306. // Every element in 'results' must be in 'working'
  307. for (auto v : results)
  308. if (std::find(working.begin(), working.end(), v) == working.end())
  309. return 4;
  310. // Every element in 'working' must be in 'results'
  311. for (auto v : working)
  312. if (std::find(results.begin(), results.end(), v) == results.end())
  313. return 5;
  314. return 0;
  315. }
  316. // -- regex fuzzers
  317. static int regex_helper(const uint8_t *data, size_t size, std::regex::flag_type flag)
  318. {
  319. if (size > 0)
  320. {
  321. try
  322. {
  323. std::string s((const char *)data, size);
  324. std::regex re(s, flag);
  325. return std::regex_match(s, re) ? 1 : 0;
  326. }
  327. catch (std::regex_error &ex) {}
  328. }
  329. return 0;
  330. }
  331. int regex_ECMAScript (const uint8_t *data, size_t size)
  332. {
  333. (void) regex_helper(data, size, std::regex_constants::ECMAScript);
  334. return 0;
  335. }
  336. int regex_POSIX (const uint8_t *data, size_t size)
  337. {
  338. (void) regex_helper(data, size, std::regex_constants::basic);
  339. return 0;
  340. }
  341. int regex_extended (const uint8_t *data, size_t size)
  342. {
  343. (void) regex_helper(data, size, std::regex_constants::extended);
  344. return 0;
  345. }
  346. int regex_awk (const uint8_t *data, size_t size)
  347. {
  348. (void) regex_helper(data, size, std::regex_constants::awk);
  349. return 0;
  350. }
  351. int regex_grep (const uint8_t *data, size_t size)
  352. {
  353. (void) regex_helper(data, size, std::regex_constants::grep);
  354. return 0;
  355. }
  356. int regex_egrep (const uint8_t *data, size_t size)
  357. {
  358. (void) regex_helper(data, size, std::regex_constants::egrep);
  359. return 0;
  360. }
  361. // -- heap fuzzers
  362. int make_heap (const uint8_t *data, size_t size)
  363. {
  364. Vec working(data, data + size);
  365. std::make_heap(working.begin(), working.end());
  366. if (!std::is_heap(working.begin(), working.end())) return 1;
  367. if (!std::is_permutation(data, data + size, working.begin())) return 99;
  368. return 0;
  369. }
  370. int push_heap (const uint8_t *data, size_t size)
  371. {
  372. if (size < 2) return 0;
  373. // Make a heap from the first half of the data
  374. Vec working(data, data + size);
  375. auto iter = working.begin() + (size / 2);
  376. std::make_heap(working.begin(), iter);
  377. if (!std::is_heap(working.begin(), iter)) return 1;
  378. // Now push the rest onto the heap, one at a time
  379. ++iter;
  380. for (; iter != working.end(); ++iter) {
  381. std::push_heap(working.begin(), iter);
  382. if (!std::is_heap(working.begin(), iter)) return 2;
  383. }
  384. if (!std::is_permutation(data, data + size, working.begin())) return 99;
  385. return 0;
  386. }
  387. int pop_heap (const uint8_t *data, size_t size)
  388. {
  389. if (size < 2) return 0;
  390. Vec working(data, data + size);
  391. std::make_heap(working.begin(), working.end());
  392. // Pop things off, one at a time
  393. auto iter = --working.end();
  394. while (iter != working.begin()) {
  395. std::pop_heap(working.begin(), iter);
  396. if (!std::is_heap(working.begin(), --iter)) return 2;
  397. }
  398. return 0;
  399. }
  400. // -- search fuzzers
  401. int search (const uint8_t *data, size_t size)
  402. {
  403. if (size < 2) return 0;
  404. const size_t pat_size = data[0] * (size - 1) / std::numeric_limits<uint8_t>::max();
  405. assert(pat_size <= size - 1);
  406. const uint8_t *pat_begin = data + 1;
  407. const uint8_t *pat_end = pat_begin + pat_size;
  408. const uint8_t *data_end = data + size;
  409. assert(pat_end <= data_end);
  410. // std::cerr << "data[0] = " << size_t(data[0]) << " ";
  411. // std::cerr << "Pattern size = " << pat_size << "; corpus is " << size - 1 << std::endl;
  412. auto it = std::search(pat_end, data_end, pat_begin, pat_end);
  413. if (it != data_end) // not found
  414. if (!std::equal(pat_begin, pat_end, it))
  415. return 1;
  416. return 0;
  417. }
  418. template <typename S>
  419. static int search_helper (const uint8_t *data, size_t size)
  420. {
  421. if (size < 2) return 0;
  422. const size_t pat_size = data[0] * (size - 1) / std::numeric_limits<uint8_t>::max();
  423. const uint8_t *pat_begin = data + 1;
  424. const uint8_t *pat_end = pat_begin + pat_size;
  425. const uint8_t *data_end = data + size;
  426. auto it = std::search(pat_end, data_end, S(pat_begin, pat_end));
  427. if (it != data_end) // not found
  428. if (!std::equal(pat_begin, pat_end, it))
  429. return 1;
  430. return 0;
  431. }
  432. // These are still in std::experimental
  433. // int search_boyer_moore (const uint8_t *data, size_t size)
  434. // {
  435. // return search_helper<std::boyer_moore_searcher<const uint8_t *>>(data, size);
  436. // }
  437. //
  438. // int search_boyer_moore_horspool (const uint8_t *data, size_t size)
  439. // {
  440. // return search_helper<std::boyer_moore_horspool_searcher<const uint8_t *>>(data, size);
  441. // }
  442. // -- set operation fuzzers
  443. template <typename S>
  444. static void set_helper (const uint8_t *data, size_t size, Vec &v1, Vec &v2)
  445. {
  446. assert(size > 1);
  447. const size_t pat_size = data[0] * (size - 1) / std::numeric_limits<uint8_t>::max();
  448. const uint8_t *pat_begin = data + 1;
  449. const uint8_t *pat_end = pat_begin + pat_size;
  450. const uint8_t *data_end = data + size;
  451. v1.assign(pat_begin, pat_end);
  452. v2.assign(pat_end, data_end);
  453. std::sort(v1.begin(), v1.end());
  454. std::sort(v2.begin(), v2.end());
  455. }
  456. } // namespace fuzzing