/src/botan/src/fuzzer/fuzzers.h
Line | Count | Source |
1 | | /* |
2 | | * (C) 2015,2016,2017 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #ifndef BOTAN_FUZZER_DRIVER_H_ |
8 | | #define BOTAN_FUZZER_DRIVER_H_ |
9 | | |
10 | | #include <botan/chacha_rng.h> |
11 | | #include <botan/exceptn.h> |
12 | | #include <botan/internal/target_info.h> |
13 | | #include <fstream> |
14 | | #include <iostream> |
15 | | #include <stdint.h> |
16 | | #include <stdlib.h> // for setenv |
17 | | #include <vector> |
18 | | |
19 | | static constexpr size_t max_fuzzer_input_size = 8192; |
20 | | |
21 | | extern void fuzz(std::span<const uint8_t> in); |
22 | | |
23 | | // Need to declare these before defining them; |
24 | | extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv); |
25 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t in[], size_t len); |
26 | | |
27 | | // NOLINTNEXTLINE(*-definitions-in-headers) |
28 | 83 | extern "C" int LLVMFuzzerInitialize(int* /*argc*/, char*** /*argv*/) { |
29 | | /* |
30 | | * This disables the mlock pool, as overwrites within the pool are |
31 | | * opaque to ASan or other instrumentation. |
32 | | */ |
33 | 83 | ::setenv("BOTAN_MLOCK_POOL_SIZE", "0", 1); |
34 | 83 | return 0; |
35 | 83 | } |
36 | | |
37 | | // Called by main() in libFuzzer or in main for AFL below |
38 | | // NOLINTNEXTLINE(*-definitions-in-headers) |
39 | 73.5k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t in[], size_t len) { |
40 | 73.5k | if(len <= max_fuzzer_input_size) { |
41 | 73.2k | try { |
42 | 73.2k | fuzz(std::span<const uint8_t>(in, len)); |
43 | 73.2k | } catch(const std::exception& e) { |
44 | 0 | std::cerr << "Uncaught exception from fuzzer driver " << e.what() << "\n"; |
45 | 0 | abort(); |
46 | 0 | } catch(...) { |
47 | 0 | std::cerr << "Uncaught exception from fuzzer driver (unknown type)\n"; |
48 | 0 | abort(); |
49 | 0 | } |
50 | 73.2k | } |
51 | 73.5k | return 0; |
52 | 73.5k | } |
53 | | |
54 | | // Some helpers for the fuzzer jigs |
55 | | |
56 | 10.1k | inline std::shared_ptr<Botan::RandomNumberGenerator> fuzzer_rng_as_shared() { |
57 | 10.1k | static const std::shared_ptr<Botan::ChaCha_RNG> rng = |
58 | 10.1k | std::make_shared<Botan::ChaCha_RNG>(Botan::secure_vector<uint8_t>(32)); |
59 | 10.1k | return rng; |
60 | 10.1k | } |
61 | | |
62 | 3.36k | inline Botan::RandomNumberGenerator& fuzzer_rng() { |
63 | 3.36k | return *fuzzer_rng_as_shared(); |
64 | 3.36k | } |
65 | | |
66 | | // TODO use a constexpr function with std::source_location |
67 | | // NOLINTNEXTLINE(*-macro-usage) |
68 | | #define FUZZER_WRITE_AND_CRASH(expr) \ |
69 | | /* NOLINTNEXTLINE(*-avoid-do-while) */ \ |
70 | 0 | do { \ |
71 | 0 | std::cerr << expr << " @ Line " << __LINE__ << " in " << __FILE__ << "\n"; /* NOLINT(*-macro-paren*) */ \ |
72 | 0 | abort(); \ |
73 | 0 | } while(0) |
74 | | |
75 | | // TODO use a constexpr function with std::source_location |
76 | | // NOLINTNEXTLINE(*-macro-usage) |
77 | | #define FUZZER_ASSERT_EQUAL(x, y) \ |
78 | | /* NOLINTNEXTLINE(*-avoid-do-while) */ \ |
79 | 23.0k | do { \ |
80 | 23.0k | if((x) != (y)) { \ |
81 | 0 | FUZZER_WRITE_AND_CRASH(#x << " = " << (x) << " != " << #y << " = " << (y) << "\n"); \ |
82 | 0 | } \ |
83 | 23.0k | } while(0) |
84 | | |
85 | | // TODO use a constexpr function with std::source_location |
86 | | // NOLINTNEXTLINE(*-macro-usage) |
87 | | #define FUZZER_ASSERT_TRUE(e) \ |
88 | | /* NOLINTNEXTLINE(*-avoid-do-while) */ \ |
89 | 37.8k | do { \ |
90 | 37.8k | /* NOLINTNEXTLINE(*-simplify-boolean-expr) */ \ |
91 | 37.8k | if(!(e)) { \ |
92 | 0 | FUZZER_WRITE_AND_CRASH("Expression " << #e << " was false"); \ |
93 | 0 | } \ |
94 | 37.8k | } while(0) |
95 | | |
96 | | #if defined(BOTAN_FUZZER_IS_TEST) |
97 | | |
98 | | inline int fuzz_files(char* files[]) { |
99 | | for(size_t i = 0; files[i] != nullptr; ++i) { |
100 | | std::ifstream in(files[i]); |
101 | | |
102 | | if(in.good()) { |
103 | | std::vector<uint8_t> buf(max_fuzzer_input_size); |
104 | | in.read(reinterpret_cast<char*>(buf.data()), static_cast<std::streamsize>(buf.size())); |
105 | | const size_t got = in.gcount(); |
106 | | buf.resize(got); |
107 | | buf.shrink_to_fit(); |
108 | | |
109 | | LLVMFuzzerTestOneInput(buf.data(), got); |
110 | | } |
111 | | } |
112 | | |
113 | | return 0; |
114 | | } |
115 | | |
116 | | #endif |
117 | | |
118 | | #if defined(BOTAN_FUZZER_IS_AFL) || defined(BOTAN_FUZZER_IS_TEST) |
119 | | |
120 | | /* Stub for AFL */ |
121 | | |
122 | | #if defined(BOTAN_FUZZER_IS_AFL) && !defined(__AFL_COMPILER) |
123 | | #error "Build configured for AFL but not being compiled by AFL compiler" |
124 | | #endif |
125 | | |
126 | | // NOLINTNEXTLINE(*-definitions-in-headers) |
127 | | int main(int argc, char* argv[]) { |
128 | | LLVMFuzzerInitialize(&argc, &argv); |
129 | | |
130 | | #if defined(BOTAN_FUZZER_IS_TEST) |
131 | | if(argc > 1) { |
132 | | return fuzz_files(&argv[1]); |
133 | | } |
134 | | #endif |
135 | | |
136 | | #if defined(__AFL_LOOP) |
137 | | while(__AFL_LOOP(1000)) |
138 | | #endif |
139 | | { |
140 | | std::vector<uint8_t> buf(max_fuzzer_input_size); |
141 | | std::cin.read(reinterpret_cast<char*>(buf.data()), static_cast<std::streamsize>(buf.size())); |
142 | | const size_t got = std::cin.gcount(); |
143 | | |
144 | | buf.resize(got); |
145 | | buf.shrink_to_fit(); |
146 | | |
147 | | LLVMFuzzerTestOneInput(buf.data(), got); |
148 | | } |
149 | | } |
150 | | |
151 | | #elif defined(BOTAN_FUZZER_IS_KLEE) |
152 | | |
153 | | #include <klee/klee.h> |
154 | | |
155 | | // NOLINTNEXTLINE(*-definitions-in-headers) |
156 | | int main(int argc, char* argv[]) { |
157 | | LLVMFuzzerInitialize(&argc, &argv); |
158 | | |
159 | | uint8_t input[max_fuzzer_input_size] = {0}; |
160 | | klee_make_symbolic(&input, sizeof(input), "input"); |
161 | | |
162 | | size_t input_len = klee_range(0, sizeof(input), "input_len"); |
163 | | |
164 | | LLVMFuzzerTestOneInput(input, input_len); |
165 | | } |
166 | | |
167 | | #endif |
168 | | |
169 | | #endif |