mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-06 10:18:44 -05:00
85fee49c39
abb6fa7285
fuzz: Initialize a full TestingSetup where appropriate (Carl Dong)713314abfa
fuzz: Consolidate fuzzing TestingSetup initialization (Carl Dong) Pull request description: ``` Previously, the {Basic,}TestingSetup for fuzzers were set up in many ways: 1. Calling InitializeFuzzingContext, which implicitly constructs a static const BasicTestingSetup 2. Directly constructing a static const BasicTestingSetup in the initialize_* function 3. Directly constructing a static TestingSetup and reproducing the initialization arguments (I'm assuming because InitializeFuzzingContext only initializes a BasicTestingSetup) The new, relatively-simple MakeFuzzingContext function allows us to consolidate these methods of initialization by being flexible enough to be used in all situations. It: 1. Is templated so that we can choose to initialize any of the *TestingSetup classes 2. Has sane defaults which are often used in fuzzers but are also easily overridable 3. Returns a unique_ptr, explicitly transferring ownership to the caller to deal with according to its situation ``` ~~Question for fuzzing people: was it intentional that `src/test/fuzz/net.cpp` would directly instantiate the `BasicTestingSetup` and thus omit the `"-nodebuglogfile"` flag?~~ [Answered](https://github.com/bitcoin/bitcoin/pull/20946#issuecomment-761537108) ACKs for top commit: MarcoFalke: ACKabb6fa7285
Tree-SHA512: 96a5ca6f4cd5ea0e9483b60165b31ae3e9003918c700a7f6ade48010f419f2a6312e10b816b3187f1d263798827571866e4c4ac0bbfb2e0c79dfad254cda68e7
84 lines
2.9 KiB
C++
84 lines
2.9 KiB
C++
// Copyright (c) 2020 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <consensus/consensus.h>
|
|
#include <net.h>
|
|
#include <net_processing.h>
|
|
#include <protocol.h>
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
|
#include <test/fuzz/fuzz.h>
|
|
#include <test/fuzz/util.h>
|
|
#include <test/util/mining.h>
|
|
#include <test/util/net.h>
|
|
#include <test/util/setup_common.h>
|
|
#include <test/util/validation.h>
|
|
#include <util/memory.h>
|
|
#include <validation.h>
|
|
#include <validationinterface.h>
|
|
|
|
namespace {
|
|
const TestingSetup* g_setup;
|
|
} // namespace
|
|
|
|
void initialize_process_messages()
|
|
{
|
|
static const auto testing_setup = MakeFuzzingContext<const TestingSetup>();
|
|
g_setup = testing_setup.get();
|
|
for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
|
|
MineBlock(g_setup->m_node, CScript() << OP_TRUE);
|
|
}
|
|
SyncWithValidationInterfaceQueue();
|
|
}
|
|
|
|
FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
|
|
{
|
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
|
|
|
ConnmanTestMsg& connman = *(ConnmanTestMsg*)g_setup->m_node.connman.get();
|
|
TestChainState& chainstate = *(TestChainState*)&g_setup->m_node.chainman->ActiveChainstate();
|
|
SetMockTime(1610000000); // any time to successfully reset ibd
|
|
chainstate.ResetIbd();
|
|
|
|
std::vector<CNode*> peers;
|
|
const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
|
|
for (int i = 0; i < num_peers_to_add; ++i) {
|
|
peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
|
|
CNode& p2p_node = *peers.back();
|
|
FillNode(fuzzed_data_provider, p2p_node);
|
|
|
|
p2p_node.fSuccessfullyConnected = true;
|
|
p2p_node.fPauseSend = false;
|
|
g_setup->m_node.peerman->InitializeNode(&p2p_node);
|
|
|
|
connman.AddTestNode(p2p_node);
|
|
}
|
|
|
|
while (fuzzed_data_provider.ConsumeBool()) {
|
|
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
|
|
|
|
const auto mock_time = ConsumeTime(fuzzed_data_provider);
|
|
SetMockTime(mock_time);
|
|
|
|
CSerializedNetMsg net_msg;
|
|
net_msg.m_type = random_message_type;
|
|
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
|
|
|
CNode& random_node = *peers.at(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, peers.size() - 1));
|
|
|
|
(void)connman.ReceiveMsgFrom(random_node, net_msg);
|
|
random_node.fPauseSend = false;
|
|
|
|
try {
|
|
connman.ProcessMessagesOnce(random_node);
|
|
} catch (const std::ios_base::failure&) {
|
|
}
|
|
{
|
|
LOCK(random_node.cs_sendProcessing);
|
|
g_setup->m_node.peerman->SendMessages(&random_node);
|
|
}
|
|
}
|
|
SyncWithValidationInterfaceQueue();
|
|
LOCK2(::cs_main, g_cs_orphans); // See init.cpp for rationale for implicit locking order requirement
|
|
g_setup->m_node.connman->StopNodes();
|
|
}
|