• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11
10 
11 #include <algorithm>
12 #include <cstddef>
13 #include <cstdint>
14 #include <iterator>
15 #include <vector>
16 
17 #include "fuzz.h"
18 
LLVMFuzzerTestOneInput(const std::uint8_t * data,std::size_t size)19 extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
20     auto is_even = [](auto t) {
21         return t % 2 == 0;
22     };
23 
24     std::vector<std::uint8_t> v1, v2;
25     auto iter = std::partition_copy(data, data + size,
26         std::back_inserter<std::vector<std::uint8_t>>(v1),
27         std::back_inserter<std::vector<std::uint8_t>>(v2),
28         is_even);
29     ((void)iter);
30     // The two vectors should add up to the original size
31     if (v1.size() + v2.size() != size)
32         return 1;
33 
34     // All of the even values should be in the first vector, and none in the second
35     if (!std::all_of(v1.begin(), v1.end(), is_even))
36         return 2;
37     if (!std::none_of(v2.begin(), v2.end(), is_even))
38         return 3;
39 
40     // Every value in both vectors has to be in the original
41 
42     // Make a copy of the input, and sort it
43     std::vector<std::uint8_t> v0{data, data + size};
44     std::sort(v0.begin(), v0.end());
45 
46     // Sort each vector and ensure that all of the elements appear in the original input
47     std::sort(v1.begin(), v1.end());
48     if (!std::includes(v0.begin(), v0.end(), v1.begin(), v1.end()))
49         return 4;
50 
51     std::sort(v2.begin(), v2.end());
52     if (!std::includes(v0.begin(), v0.end(), v2.begin(), v2.end()))
53         return 5;
54 
55     // This, while simple, is really slow - 20 seconds on a 500K element input.
56     //  for (auto v: v1)
57     //      if (std::find(data, data + size, v) == data + size)
58     //          return 4;
59     //
60     //  for (auto v: v2)
61     //      if (std::find(data, data + size, v) == data + size)
62     //          return 5;
63 
64     return 0;
65 }
66