2021-01-25 12:23:45 +01:00
|
|
|
// Copyright (c) 2020-2021 The Bitcoin Core developers
|
2020-01-22 20:23:48 +00:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <banman.h>
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <consensus/consensus.h>
|
|
|
|
#include <net.h>
|
|
|
|
#include <net_processing.h>
|
|
|
|
#include <protocol.h>
|
|
|
|
#include <scheduler.h>
|
|
|
|
#include <script/script.h>
|
|
|
|
#include <streams.h>
|
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
|
|
|
#include <test/fuzz/fuzz.h>
|
2020-12-28 21:52:40 +01:00
|
|
|
#include <test/fuzz/util.h>
|
2020-01-22 20:23:48 +00:00
|
|
|
#include <test/util/mining.h>
|
2020-05-10 20:12:25 -04:00
|
|
|
#include <test/util/net.h>
|
2020-01-22 20:23:48 +00:00
|
|
|
#include <test/util/setup_common.h>
|
2020-11-06 15:03:51 +01:00
|
|
|
#include <test/util/validation.h>
|
2021-01-31 00:55:54 +10:00
|
|
|
#include <txorphanage.h>
|
2020-01-22 20:23:48 +00:00
|
|
|
#include <validationinterface.h>
|
|
|
|
#include <version.h>
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <cassert>
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace {
|
2020-04-08 19:48:38 -04:00
|
|
|
const TestingSetup* g_setup;
|
2020-01-22 20:23:48 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-01-12 17:51:14 +01:00
|
|
|
size_t& GetNumMsgTypes()
|
|
|
|
{
|
|
|
|
static size_t g_num_msg_types{0};
|
|
|
|
return g_num_msg_types;
|
|
|
|
}
|
|
|
|
#define FUZZ_TARGET_MSG(msg_type) \
|
|
|
|
struct msg_type##_Count_Before_Main { \
|
|
|
|
msg_type##_Count_Before_Main() \
|
|
|
|
{ \
|
|
|
|
++GetNumMsgTypes(); \
|
|
|
|
} \
|
|
|
|
} const static g_##msg_type##_count_before_main; \
|
|
|
|
FUZZ_TARGET_INIT(process_message_##msg_type, initialize_process_message) \
|
|
|
|
{ \
|
|
|
|
fuzz_target(buffer, #msg_type); \
|
|
|
|
}
|
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
void initialize_process_message()
|
2020-01-22 20:23:48 +00:00
|
|
|
{
|
2021-01-12 18:23:59 +01:00
|
|
|
Assert(GetNumMsgTypes() == getAllNetMessageTypes().size()); // If this fails, add or remove the message type below
|
|
|
|
|
2021-01-25 12:23:45 +01:00
|
|
|
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
|
2021-01-15 15:31:50 -05:00
|
|
|
g_setup = testing_setup.get();
|
2020-01-22 20:23:48 +00:00
|
|
|
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
|
|
|
|
MineBlock(g_setup->m_node, CScript() << OP_TRUE);
|
|
|
|
}
|
|
|
|
SyncWithValidationInterfaceQueue();
|
|
|
|
}
|
|
|
|
|
2021-01-02 19:29:36 +01:00
|
|
|
void fuzz_target(FuzzBufferType buffer, const std::string& LIMIT_TO_MESSAGE_TYPE)
|
2020-01-22 20:23:48 +00:00
|
|
|
{
|
|
|
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
2021-01-10 16:41:52 +01:00
|
|
|
|
2021-03-23 11:04:18 +01:00
|
|
|
ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
|
|
|
|
TestChainState& chainstate = *static_cast<TestChainState*>(&g_setup->m_node.chainman->ActiveChainstate());
|
2021-01-10 16:41:52 +01:00
|
|
|
SetMockTime(1610000000); // any time to successfully reset ibd
|
2020-11-06 15:03:51 +01:00
|
|
|
chainstate.ResetIbd();
|
2021-01-10 16:41:52 +01:00
|
|
|
|
2020-01-22 20:23:48 +00:00
|
|
|
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
|
|
|
|
if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-28 21:52:40 +01:00
|
|
|
CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider).release();
|
2021-01-23 19:39:30 +01:00
|
|
|
|
2020-05-10 20:12:25 -04:00
|
|
|
connman.AddTestNode(p2p_node);
|
2020-08-29 10:31:11 +01:00
|
|
|
g_setup->m_node.peerman->InitializeNode(&p2p_node);
|
2021-11-22 16:06:55 +01:00
|
|
|
FillNode(fuzzed_data_provider, connman, *g_setup->m_node.peerman, p2p_node);
|
2020-12-28 21:52:40 +01:00
|
|
|
|
2021-01-10 16:41:52 +01:00
|
|
|
const auto mock_time = ConsumeTime(fuzzed_data_provider);
|
|
|
|
SetMockTime(mock_time);
|
|
|
|
|
2020-12-28 21:52:40 +01:00
|
|
|
// fuzzed_data_provider is fully consumed after this call, don't use it
|
|
|
|
CDataStream random_bytes_data_stream{fuzzed_data_provider.ConsumeRemainingBytes<unsigned char>(), SER_NETWORK, PROTOCOL_VERSION};
|
2020-01-22 20:23:48 +00:00
|
|
|
try {
|
2020-08-29 10:31:11 +01:00
|
|
|
g_setup->m_node.peerman->ProcessMessage(p2p_node, random_message_type, random_bytes_data_stream,
|
2020-11-06 15:03:51 +01:00
|
|
|
GetTime<std::chrono::microseconds>(), std::atomic<bool>{false});
|
2020-04-24 12:29:47 +00:00
|
|
|
} catch (const std::ios_base::failure&) {
|
2020-01-22 20:23:48 +00:00
|
|
|
}
|
2020-11-12 18:06:00 +01:00
|
|
|
{
|
|
|
|
LOCK(p2p_node.cs_sendProcessing);
|
|
|
|
g_setup->m_node.peerman->SendMessages(&p2p_node);
|
|
|
|
}
|
2020-01-22 20:23:48 +00:00
|
|
|
SyncWithValidationInterfaceQueue();
|
2020-05-04 20:16:22 -04:00
|
|
|
g_setup->m_node.connman->StopNodes();
|
2020-01-22 20:23:48 +00:00
|
|
|
}
|
2020-12-03 16:42:49 +01:00
|
|
|
|
|
|
|
FUZZ_TARGET_INIT(process_message, initialize_process_message) { fuzz_target(buffer, ""); }
|
2021-01-12 17:51:14 +01:00
|
|
|
FUZZ_TARGET_MSG(addr);
|
2021-01-12 18:23:59 +01:00
|
|
|
FUZZ_TARGET_MSG(addrv2);
|
2021-01-12 17:51:14 +01:00
|
|
|
FUZZ_TARGET_MSG(block);
|
|
|
|
FUZZ_TARGET_MSG(blocktxn);
|
2021-01-12 18:23:59 +01:00
|
|
|
FUZZ_TARGET_MSG(cfcheckpt);
|
|
|
|
FUZZ_TARGET_MSG(cfheaders);
|
|
|
|
FUZZ_TARGET_MSG(cfilter);
|
2021-01-12 17:51:14 +01:00
|
|
|
FUZZ_TARGET_MSG(cmpctblock);
|
|
|
|
FUZZ_TARGET_MSG(feefilter);
|
|
|
|
FUZZ_TARGET_MSG(filteradd);
|
|
|
|
FUZZ_TARGET_MSG(filterclear);
|
|
|
|
FUZZ_TARGET_MSG(filterload);
|
|
|
|
FUZZ_TARGET_MSG(getaddr);
|
|
|
|
FUZZ_TARGET_MSG(getblocks);
|
|
|
|
FUZZ_TARGET_MSG(getblocktxn);
|
2021-01-12 18:23:59 +01:00
|
|
|
FUZZ_TARGET_MSG(getcfcheckpt);
|
|
|
|
FUZZ_TARGET_MSG(getcfheaders);
|
|
|
|
FUZZ_TARGET_MSG(getcfilters);
|
2021-01-12 17:51:14 +01:00
|
|
|
FUZZ_TARGET_MSG(getdata);
|
|
|
|
FUZZ_TARGET_MSG(getheaders);
|
|
|
|
FUZZ_TARGET_MSG(headers);
|
|
|
|
FUZZ_TARGET_MSG(inv);
|
|
|
|
FUZZ_TARGET_MSG(mempool);
|
2021-01-12 18:23:59 +01:00
|
|
|
FUZZ_TARGET_MSG(merkleblock);
|
2021-01-12 17:51:14 +01:00
|
|
|
FUZZ_TARGET_MSG(notfound);
|
|
|
|
FUZZ_TARGET_MSG(ping);
|
|
|
|
FUZZ_TARGET_MSG(pong);
|
2021-01-12 18:23:59 +01:00
|
|
|
FUZZ_TARGET_MSG(sendaddrv2);
|
2021-01-12 17:51:14 +01:00
|
|
|
FUZZ_TARGET_MSG(sendcmpct);
|
|
|
|
FUZZ_TARGET_MSG(sendheaders);
|
|
|
|
FUZZ_TARGET_MSG(tx);
|
|
|
|
FUZZ_TARGET_MSG(verack);
|
|
|
|
FUZZ_TARGET_MSG(version);
|
2021-01-12 18:23:59 +01:00
|
|
|
FUZZ_TARGET_MSG(wtxidrelay);
|