/src/cppitertools/fuzz_cppitertools.cpp
Line | Count | Source |
1 | | /* Copyright 2023 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | #include <chain.hpp> |
13 | | #include <combinations.hpp> |
14 | | #include <compress.hpp> |
15 | | #include <cycle.hpp> |
16 | | #include <groupby.hpp> |
17 | | |
18 | | #include <fuzzer/FuzzedDataProvider.h> |
19 | | |
20 | | #include <iterator> |
21 | | #include <string> |
22 | | #include <utility> |
23 | | #include <vector> |
24 | | |
25 | | using iter::chain; |
26 | | using iter::combinations; |
27 | | using iter::compress; |
28 | | using iter::cycle; |
29 | | using iter::groupby; |
30 | | |
31 | 536 | void FuzzChained(const uint8_t *data, size_t size) { |
32 | 536 | FuzzedDataProvider fdp(data, size); |
33 | 536 | std::vector<char> v; |
34 | 61.0k | for (int i = 0; i < fdp.ConsumeIntegralInRange<int>(1, 1024); i++) { |
35 | 60.5k | v.push_back((char)fdp.ConsumeIntegral<char>()); |
36 | 60.5k | } |
37 | 536 | const auto ch = chain(v, v, v); |
38 | 536 | std::vector<char> v2(std::begin(ch), std::end(ch)); |
39 | 536 | } |
40 | | |
41 | 20.9k | int length(const std::string &s) { return s.size(); } |
42 | | |
43 | 536 | void FuzzGroupby(const uint8_t *data, size_t size) { |
44 | 536 | FuzzedDataProvider fdp(data, size); |
45 | 536 | std::vector<std::string> v; |
46 | 15.1k | for (int i = 0; i < fdp.ConsumeIntegralInRange<int>(1, 1024); i++) { |
47 | 14.6k | v.push_back(fdp.ConsumeRandomLengthString()); |
48 | 14.6k | } |
49 | 3.39k | for (auto &&gb : groupby(v, length)) { |
50 | 3.39k | } |
51 | 536 | } |
52 | | |
53 | 536 | void FuzzCycle(const uint8_t *data, size_t size) { |
54 | 536 | FuzzedDataProvider fdp(data, size); |
55 | 536 | std::vector<int> v; |
56 | 50.7k | for (int i = 0; i < fdp.ConsumeIntegralInRange<int>(1, 1024); i++) { |
57 | 50.1k | v.push_back(fdp.ConsumeIntegral<int>()); |
58 | 50.1k | } |
59 | 536 | auto ch = cycle(v); |
60 | | |
61 | 536 | std::vector<int> o; |
62 | 536 | size_t count = 0; |
63 | 804k | for (auto val : ch) { |
64 | 804k | o.push_back(val); |
65 | 804k | count++; |
66 | 804k | if (count > 1500) { |
67 | 536 | break; |
68 | 536 | } |
69 | 804k | } |
70 | 536 | } |
71 | | |
72 | 536 | void FuzzCombinations(const uint8_t *data, size_t size) { |
73 | 536 | FuzzedDataProvider fdp(data, size); |
74 | 536 | std::string s = fdp.ConsumeRandomLengthString(); |
75 | 536 | std::vector<std::vector<char>> sc; |
76 | 536 | size_t count = 0; |
77 | 310k | for (auto &&v : combinations(s, fdp.ConsumeIntegralInRange(1, 16))) { |
78 | 310k | sc.emplace_back(std::begin(v), std::end(v)); |
79 | 310k | count++; |
80 | 310k | if (count > 1500) { |
81 | 183 | break; |
82 | 183 | } |
83 | 310k | } |
84 | 536 | } |
85 | | |
86 | 536 | void FuzzCompress(const uint8_t *data, size_t size) { |
87 | 536 | FuzzedDataProvider fdp(data, size); |
88 | 536 | std::vector<int> ivec; |
89 | 536 | std::vector<bool> bvec; |
90 | 54.1k | for (int i = 0; i < 100; i++) { |
91 | 53.6k | ivec.push_back(fdp.ConsumeIntegralInRange(1, 1000000)); |
92 | 53.6k | bvec.push_back(fdp.ConsumeBool()); |
93 | 53.6k | } |
94 | 536 | auto c = compress(ivec, bvec); |
95 | 536 | std::vector<int> v(std::begin(c), std::end(c)); |
96 | 536 | const auto &c2 = c; |
97 | 536 | (void)(std::begin(c) == std::end(c2)); |
98 | 536 | } |
99 | | |
100 | 536 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
101 | 536 | FuzzChained(data, size); |
102 | 536 | FuzzGroupby(data, size); |
103 | 536 | FuzzCycle(data, size); |
104 | 536 | FuzzCombinations(data, size); |
105 | 536 | FuzzCompress(data, size); |
106 | 536 | return 0; |
107 | 536 | } |