2021-01-25 12:23:45 +01:00
|
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
2019-01-25 18:35:36 -05:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <test/fuzz/fuzz.h>
|
|
|
|
|
2020-06-11 08:58:46 +02:00
|
|
|
#include <fs.h>
|
2021-05-20 19:01:46 +00:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <netbase.h>
|
2019-09-27 11:53:34 -04:00
|
|
|
#include <test/util/setup_common.h>
|
2020-12-03 16:42:49 +01:00
|
|
|
#include <util/check.h>
|
2021-05-20 19:01:46 +00:00
|
|
|
#include <util/sock.h>
|
2019-09-27 11:53:34 -04:00
|
|
|
|
2019-10-23 21:46:53 +00:00
|
|
|
#include <cstdint>
|
2021-05-20 19:01:46 +00:00
|
|
|
#include <exception>
|
2020-06-11 08:58:46 +02:00
|
|
|
#include <fstream>
|
2021-10-08 18:11:40 +02:00
|
|
|
#include <functional>
|
2020-06-11 08:58:46 +02:00
|
|
|
#include <map>
|
2021-05-20 19:01:46 +00:00
|
|
|
#include <memory>
|
2021-05-21 19:43:15 +00:00
|
|
|
#include <string>
|
2020-06-11 08:58:46 +02:00
|
|
|
#include <tuple>
|
2019-01-25 18:35:36 -05:00
|
|
|
#include <unistd.h>
|
2019-10-23 21:46:53 +00:00
|
|
|
#include <vector>
|
2019-01-25 18:35:36 -05:00
|
|
|
|
2019-09-27 11:53:34 -04:00
|
|
|
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
|
|
|
|
|
2021-10-26 17:26:16 +02:00
|
|
|
/**
|
|
|
|
* A copy of the command line arguments that start with `--`.
|
|
|
|
* First `LLVMFuzzerInitialize()` is called, which saves the arguments to `g_args`.
|
|
|
|
* Later, depending on the fuzz test, `G_TEST_COMMAND_LINE_ARGUMENTS()` may be
|
|
|
|
* called by `BasicTestingSetup` constructor to fetch those arguments and store
|
|
|
|
* them in `BasicTestingSetup::m_node::args`.
|
|
|
|
*/
|
|
|
|
static std::vector<const char*> g_args;
|
|
|
|
|
|
|
|
static void SetArgs(int argc, char** argv) {
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
// Only take into account arguments that start with `--`. The others are for the fuzz engine:
|
|
|
|
// `fuzz -runs=1 fuzz_seed_corpus/address_deserialize_v2 --checkaddrman=5`
|
|
|
|
if (strlen(argv[i]) > 2 && argv[i][0] == '-' && argv[i][1] == '-') {
|
|
|
|
g_args.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS = []() {
|
|
|
|
return g_args;
|
|
|
|
};
|
2021-10-08 18:11:40 +02:00
|
|
|
|
2021-02-08 10:13:08 +01:00
|
|
|
std::map<std::string_view, std::tuple<TypeTestOneInput, TypeInitialize, TypeHidden>>& FuzzTargets()
|
2020-12-03 16:42:49 +01:00
|
|
|
{
|
2021-02-08 10:13:08 +01:00
|
|
|
static std::map<std::string_view, std::tuple<TypeTestOneInput, TypeInitialize, TypeHidden>> g_fuzz_targets;
|
2020-12-03 16:42:49 +01:00
|
|
|
return g_fuzz_targets;
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:13:08 +01:00
|
|
|
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, TypeInitialize init, TypeHidden hidden)
|
2020-12-03 16:42:49 +01:00
|
|
|
{
|
2021-02-08 10:13:08 +01:00
|
|
|
const auto it_ins = FuzzTargets().try_emplace(name, std::move(target), std::move(init), hidden);
|
2020-12-03 16:42:49 +01:00
|
|
|
Assert(it_ins.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
static TypeTestOneInput* g_test_one_input{nullptr};
|
|
|
|
|
|
|
|
void initialize()
|
|
|
|
{
|
2021-05-20 19:01:46 +00:00
|
|
|
// Terminate immediately if a fuzzing harness ever tries to create a TCP socket.
|
|
|
|
CreateSock = [](const CService&) -> std::unique_ptr<Sock> { std::terminate(); };
|
|
|
|
|
2021-05-21 19:43:15 +00:00
|
|
|
// Terminate immediately if a fuzzing harness ever tries to perform a DNS lookup.
|
|
|
|
g_dns_lookup = [](const std::string& name, bool allow_lookup) {
|
|
|
|
if (allow_lookup) {
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
return WrappedGetAddrInfo(name, false);
|
|
|
|
};
|
|
|
|
|
2021-05-07 11:04:01 +02:00
|
|
|
bool should_abort{false};
|
2020-12-03 16:42:49 +01:00
|
|
|
if (std::getenv("PRINT_ALL_FUZZ_TARGETS_AND_ABORT")) {
|
|
|
|
for (const auto& t : FuzzTargets()) {
|
2021-02-08 10:13:08 +01:00
|
|
|
if (std::get<2>(t.second)) continue;
|
2020-12-03 16:42:49 +01:00
|
|
|
std::cout << t.first << std::endl;
|
|
|
|
}
|
2021-05-07 11:04:01 +02:00
|
|
|
should_abort = true;
|
2020-12-03 16:42:49 +01:00
|
|
|
}
|
2021-05-07 11:04:01 +02:00
|
|
|
if (const char* out_path = std::getenv("WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) {
|
|
|
|
std::cout << "Writing all fuzz target names to '" << out_path << "'." << std::endl;
|
2020-06-11 08:58:46 +02:00
|
|
|
std::ofstream out_stream{out_path, std::ios::binary};
|
2021-05-07 11:04:01 +02:00
|
|
|
for (const auto& t : FuzzTargets()) {
|
|
|
|
if (std::get<2>(t.second)) continue;
|
|
|
|
out_stream << t.first << std::endl;
|
|
|
|
}
|
|
|
|
should_abort = true;
|
|
|
|
}
|
|
|
|
Assert(!should_abort);
|
2020-12-03 16:42:49 +01:00
|
|
|
std::string_view fuzz_target{Assert(std::getenv("FUZZ"))};
|
|
|
|
const auto it = FuzzTargets().find(fuzz_target);
|
|
|
|
Assert(it != FuzzTargets().end());
|
|
|
|
Assert(!g_test_one_input);
|
|
|
|
g_test_one_input = &std::get<0>(it->second);
|
|
|
|
std::get<1>(it->second)();
|
|
|
|
}
|
|
|
|
|
2021-02-11 11:28:38 +01:00
|
|
|
#if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
|
2019-01-25 18:35:36 -05:00
|
|
|
static bool read_stdin(std::vector<uint8_t>& data)
|
|
|
|
{
|
|
|
|
uint8_t buffer[1024];
|
|
|
|
ssize_t length = 0;
|
|
|
|
while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) {
|
|
|
|
data.insert(data.end(), buffer, buffer + length);
|
|
|
|
}
|
|
|
|
return length == 0;
|
|
|
|
}
|
2020-01-27 14:51:33 +08:00
|
|
|
#endif
|
2019-01-25 18:35:36 -05:00
|
|
|
|
|
|
|
// This function is used by libFuzzer
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
|
|
|
{
|
2020-12-03 16:42:49 +01:00
|
|
|
static const auto& test_one_input = *Assert(g_test_one_input);
|
2021-01-02 19:29:36 +01:00
|
|
|
test_one_input({data, size});
|
2019-01-25 18:35:36 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is used by libFuzzer
|
|
|
|
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
|
|
|
|
{
|
2021-10-26 17:26:16 +02:00
|
|
|
SetArgs(*argc, *argv);
|
2019-01-25 18:35:36 -05:00
|
|
|
initialize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-11 11:28:38 +01:00
|
|
|
#if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
|
2021-02-18 13:25:30 -05:00
|
|
|
int main(int argc, char** argv)
|
2019-01-25 18:35:36 -05:00
|
|
|
{
|
|
|
|
initialize();
|
2020-12-03 16:42:49 +01:00
|
|
|
static const auto& test_one_input = *Assert(g_test_one_input);
|
2019-01-25 18:35:36 -05:00
|
|
|
#ifdef __AFL_INIT
|
|
|
|
// Enable AFL deferred forkserver mode. Requires compilation using
|
|
|
|
// afl-clang-fast++. See fuzzing.md for details.
|
|
|
|
__AFL_INIT();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __AFL_LOOP
|
|
|
|
// Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
|
|
|
|
// See fuzzing.md for details.
|
|
|
|
while (__AFL_LOOP(1000)) {
|
|
|
|
std::vector<uint8_t> buffer;
|
|
|
|
if (!read_stdin(buffer)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
test_one_input(buffer);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
std::vector<uint8_t> buffer;
|
|
|
|
if (!read_stdin(buffer)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
test_one_input(buffer);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2020-01-27 14:51:33 +08:00
|
|
|
#endif
|