Branch data Line data Source code
1 : : // Copyright (c) 2009-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <test/fuzz/fuzz.h>
6 : :
7 : : #include <netaddress.h>
8 : : #include <netbase.h>
9 : : #include <test/fuzz/util/check_globals.h>
10 : : #include <test/util/coverage.h>
11 : : #include <test/util/random.h>
12 : : #include <test/util/setup_common.h>
13 : : #include <util/check.h>
14 : : #include <util/fs.h>
15 : : #include <util/sock.h>
16 : : #include <util/time.h>
17 : :
18 : : #include <algorithm>
19 : : #include <csignal>
20 : : #include <cstdint>
21 : : #include <cstdio>
22 : : #include <cstdlib>
23 : : #include <cstring>
24 : : #include <exception>
25 : : #include <fstream>
26 : : #include <functional>
27 : : #include <iostream>
28 : : #include <map>
29 : : #include <memory>
30 : : #include <random>
31 : : #include <string>
32 : : #include <tuple>
33 : : #include <utility>
34 : : #include <vector>
35 : :
36 : : #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && defined(__AFL_FUZZ_INIT)
37 : : __AFL_FUZZ_INIT();
38 : : #endif
39 : :
40 : : const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
41 : :
42 : : /**
43 : : * A copy of the command line arguments that start with `--`.
44 : : * First `LLVMFuzzerInitialize()` is called, which saves the arguments to `g_args`.
45 : : * Later, depending on the fuzz test, `G_TEST_COMMAND_LINE_ARGUMENTS()` may be
46 : : * called by `BasicTestingSetup` constructor to fetch those arguments and store
47 : : * them in `BasicTestingSetup::m_node::args`.
48 : : */
49 : : static std::vector<const char*> g_args;
50 : :
51 : 0 : static void SetArgs(int argc, char** argv) {
52 [ # # ]: 0 : for (int i = 1; i < argc; ++i) {
53 : : // Only take into account arguments that start with `--`. The others are for the fuzz engine:
54 : : // `fuzz -runs=1 fuzz_corpora/address_deserialize_v2 --checkaddrman=5`
55 [ # # # # : 0 : if (strlen(argv[i]) > 2 && argv[i][0] == '-' && argv[i][1] == '-') {
# # ]
56 : 0 : g_args.push_back(argv[i]);
57 : : }
58 : : }
59 : 0 : }
60 : :
61 : 1572 : const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS = []() {
62 : 1572 : return g_args;
63 : : };
64 : :
65 : 103058 : struct FuzzTarget {
66 : : const TypeTestOneInput test_one_input;
67 : : const FuzzTargetOptions opts;
68 : : };
69 : :
70 : 51982 : auto& FuzzTargets()
71 : : {
72 [ + + + - ]: 52208 : static std::map<std::string_view, FuzzTarget> g_fuzz_targets;
73 : 51982 : return g_fuzz_targets;
74 : : }
75 : :
76 : 51529 : void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, FuzzTargetOptions opts)
77 : : {
78 [ + - ]: 103058 : const auto [it, ins]{FuzzTargets().try_emplace(name, FuzzTarget /* temporary can be dropped after Apple-Clang-16 ? */ {std::move(target), std::move(opts)})};
79 : 51529 : Assert(ins);
80 : 51529 : }
81 : :
82 : : static std::string_view g_fuzz_target;
83 : : static const TypeTestOneInput* g_test_one_input{nullptr};
84 : :
85 : 203742 : static void test_one_input(FuzzBufferType buffer)
86 : : {
87 : 203742 : CheckGlobals check{};
88 [ + - + - ]: 203742 : (*Assert(g_test_one_input))(buffer);
89 : 203742 : }
90 : :
91 : 1572 : const std::function<std::string()> G_TEST_GET_FULL_NAME{[]{
92 : 1572 : return std::string{g_fuzz_target};
93 : : }};
94 : :
95 : 227 : static void initialize()
96 : : {
97 : 227 : CheckGlobals check{};
98 : : // By default, make the RNG deterministic with a fixed seed. This will affect all
99 : : // randomness during the fuzz test, except:
100 : : // - GetStrongRandBytes(), which is used for the creation of private key material.
101 : : // - Randomness obtained before this call in g_rng_temp_path_init
102 [ + - ]: 227 : SeedRandomStateForTest(SeedRand::ZEROS);
103 : :
104 : : // Set time to the genesis block timestamp for deterministic initialization.
105 [ + - ]: 227 : SetMockTime(1231006505);
106 : :
107 : : // Terminate immediately if a fuzzing harness ever tries to create a socket.
108 : : // Individual tests can override this by pointing CreateSock to a mocked alternative.
109 : 227 : CreateSock = [](int, int, int) -> std::unique_ptr<Sock> { std::terminate(); };
110 : :
111 : : // Terminate immediately if a fuzzing harness ever tries to perform a DNS lookup.
112 : 526662 : g_dns_lookup = [](const std::string& name, bool allow_lookup) {
113 [ - + ]: 526435 : if (allow_lookup) {
114 : 0 : std::terminate();
115 : : }
116 : 526435 : return WrappedGetAddrInfo(name, false);
117 : 227 : };
118 : :
119 : 227 : bool should_exit{false};
120 [ + + ]: 227 : if (std::getenv("PRINT_ALL_FUZZ_TARGETS_AND_ABORT")) {
121 [ + - + + : 228 : for (const auto& [name, t] : FuzzTargets()) {
+ + ]
122 [ + + ]: 227 : if (t.opts.hidden) continue;
123 [ + - + - ]: 227 : std::cout << name << std::endl;
124 : : }
125 : : should_exit = true;
126 : : }
127 [ - + ]: 227 : if (const char* out_path = std::getenv("WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) {
128 [ # # # # : 0 : std::cout << "Writing all fuzz target names to '" << out_path << "'." << std::endl;
# # # # ]
129 [ # # ]: 0 : std::ofstream out_stream{out_path, std::ios::binary};
130 [ # # # # : 0 : for (const auto& [name, t] : FuzzTargets()) {
# # ]
131 [ # # ]: 0 : if (t.opts.hidden) continue;
132 [ # # # # ]: 0 : out_stream << name << std::endl;
133 : : }
134 : 0 : should_exit = true;
135 : 0 : }
136 [ + + ]: 227 : if (should_exit) {
137 : 1 : std::exit(EXIT_SUCCESS);
138 : : }
139 [ + - ]: 226 : if (const auto* env_fuzz{std::getenv("FUZZ")}) {
140 : : // To allow for easier fuzz executable binary modification,
141 [ + - + - : 452 : static std::string g_copy{env_fuzz}; // create copy to avoid compiler optimizations, and
+ - ]
142 [ + - ]: 226 : g_fuzz_target = g_copy.c_str(); // strip string after the first null-char.
143 : : } else {
144 [ # # # # ]: 0 : std::cerr << "Must select fuzz target with the FUZZ env var." << std::endl;
145 [ # # # # ]: 0 : std::cerr << "Hint: Set the PRINT_ALL_FUZZ_TARGETS_AND_ABORT=1 env var to see all compiled targets." << std::endl;
146 : 0 : std::exit(EXIT_FAILURE);
147 : : }
148 [ + - ]: 226 : const auto it = FuzzTargets().find(g_fuzz_target);
149 [ + - - + ]: 226 : if (it == FuzzTargets().end()) {
150 [ # # # # : 0 : std::cerr << "No fuzz target compiled for " << g_fuzz_target << "." << std::endl;
# # # # ]
151 : 0 : std::exit(EXIT_FAILURE);
152 : : }
153 : 226 : if constexpr (!G_FUZZING_BUILD && !G_ABORT_ON_FAILED_ASSUME) {
154 : : std::cerr << "Must compile with -DBUILD_FOR_FUZZING=ON or in Debug mode to execute a fuzz target." << std::endl;
155 : : std::exit(EXIT_FAILURE);
156 : : }
157 : 226 : if (!EnableFuzzDeterminism()) {
158 : : if (std::getenv("FUZZ_NONDETERMINISM")) {
159 : : std::cerr << "Warning: FUZZ_NONDETERMINISM env var set, results may be inconsistent with fuzz build" << std::endl;
160 : : } else {
161 : : g_enable_dynamic_fuzz_determinism = true;
162 : : assert(EnableFuzzDeterminism());
163 : : }
164 : : }
165 [ + - ]: 226 : Assert(!g_test_one_input);
166 [ + - ]: 226 : g_test_one_input = &it->second.test_one_input;
167 [ + - ]: 226 : it->second.opts.init();
168 : :
169 [ + - ]: 226 : ResetCoverageCounters();
170 : 226 : }
171 : :
172 : : #if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
173 : 0 : static bool read_stdin(std::vector<uint8_t>& data)
174 : : {
175 : 0 : std::istream::char_type buffer[1024];
176 : 0 : std::streamsize length;
177 [ # # ]: 0 : while ((std::cin.read(buffer, 1024), length = std::cin.gcount()) > 0) {
178 : 0 : data.insert(data.end(), buffer, buffer + length);
179 : : }
180 : 0 : return length == 0;
181 : : }
182 : : #endif
183 : :
184 : : #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
185 : 203742 : static bool read_file(fs::path p, std::vector<uint8_t>& data)
186 : : {
187 : 203742 : uint8_t buffer[1024];
188 : 203742 : FILE* f = fsbridge::fopen(p, "rb");
189 [ + - ]: 203742 : if (f == nullptr) return false;
190 : 4674057 : do {
191 : 4674057 : const size_t length = fread(buffer, sizeof(uint8_t), sizeof(buffer), f);
192 [ + - ]: 4674057 : if (ferror(f)) return false;
193 : 4674057 : data.insert(data.end(), buffer, buffer + length);
194 [ + + ]: 4674057 : } while (!feof(f));
195 : 203742 : fclose(f);
196 : 203742 : return true;
197 : : }
198 : : #endif
199 : :
200 : : #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
201 : : static fs::path g_input_path;
202 : 0 : void signal_handler(int signal)
203 : : {
204 [ # # ]: 0 : if (signal == SIGABRT) {
205 : 0 : std::cerr << "Error processing input " << g_input_path << std::endl;
206 : : } else {
207 : 0 : std::cerr << "Unexpected signal " << signal << " received\n";
208 : : }
209 : 0 : std::_Exit(EXIT_FAILURE);
210 : : }
211 : : #endif
212 : :
213 : : // This function is used by libFuzzer
214 : 0 : extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
215 : : {
216 : 0 : test_one_input({data, size});
217 : 0 : return 0;
218 : : }
219 : :
220 : : // This function is used by libFuzzer
221 : 0 : extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
222 : : {
223 : 0 : SetArgs(*argc, *argv);
224 : 0 : initialize();
225 : 0 : return 0;
226 : : }
227 : :
228 : : #if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
229 : 227 : int main(int argc, char** argv)
230 : : {
231 : 227 : initialize();
232 : : #ifdef __AFL_LOOP
233 : : // Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
234 : : // See fuzzing.md for details.
235 : : const uint8_t* buffer = __AFL_FUZZ_TESTCASE_BUF;
236 : : while (__AFL_LOOP(100000)) {
237 : : size_t buffer_len = __AFL_FUZZ_TESTCASE_LEN;
238 : : test_one_input({buffer, buffer_len});
239 : : }
240 : : #else
241 : 226 : std::vector<uint8_t> buffer;
242 [ - + ]: 226 : if (argc <= 1) {
243 [ # # # # ]: 0 : if (!read_stdin(buffer)) {
244 : : return 0;
245 : : }
246 [ # # ]: 0 : test_one_input(buffer);
247 : : return 0;
248 : : }
249 : 226 : std::signal(SIGABRT, signal_handler);
250 : 226 : const auto start_time{Now<SteadySeconds>()};
251 : 226 : int tested = 0;
252 [ + + ]: 452 : for (int i = 1; i < argc; ++i) {
253 [ + - ]: 226 : fs::path input_path(*(argv + i));
254 [ + - + - ]: 226 : if (fs::is_directory(input_path)) {
255 : 226 : std::vector<fs::path> files;
256 [ + - + - : 204194 : for (fs::directory_iterator it(input_path); it != fs::directory_iterator(); ++it) {
+ + + + ]
257 [ + - - + ]: 203742 : if (!fs::is_regular_file(it->path())) continue;
258 [ + - ]: 203742 : files.emplace_back(it->path());
259 : 0 : }
260 [ + - ]: 452 : std::ranges::shuffle(files, std::mt19937{std::random_device{}()});
261 [ + + ]: 203968 : for (const auto& input_path : files) {
262 [ + - ]: 203742 : g_input_path = input_path;
263 [ + - + - : 203742 : Assert(read_file(input_path, buffer));
+ - ]
264 [ + - ]: 203742 : test_one_input(buffer);
265 : 203742 : ++tested;
266 [ + - ]: 407484 : buffer.clear();
267 : : }
268 : 226 : } else {
269 [ # # ]: 0 : g_input_path = input_path;
270 [ # # # # : 0 : Assert(read_file(input_path, buffer));
# # ]
271 [ # # ]: 0 : test_one_input(buffer);
272 : 0 : ++tested;
273 [ - - ]: 226 : buffer.clear();
274 : : }
275 : 226 : }
276 : 226 : const auto end_time{Now<SteadySeconds>()};
277 [ + - + - : 226 : std::cout << g_fuzz_target << ": succeeded against " << tested << " files in " << count_seconds(end_time - start_time) << "s." << std::endl;
+ - + - +
- + - +
- ]
278 : : #endif
279 : : return 0;
280 : 226 : }
281 : : #endif
|