Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/test/fuzz/process_message.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Copyright (c) 2020 The Bitcoin Core developers
// Copyright (c) 2020-present 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 <protocol.h>
#include <script/script.h>
#include <serialize.h>
#include <streams.h>
#include <sync.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
Expand All @@ -19,7 +17,6 @@
#include <validationinterface.h>
#include <version.h>

#include <atomic>
#include <cstdlib>
#include <iostream>
#include <memory>
Expand Down Expand Up @@ -72,13 +69,22 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)
const auto mock_time = ConsumeTime(fuzzed_data_provider);
SetMockTime(mock_time);

// 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};
try {
g_setup->m_node.peerman->ProcessMessage(p2p_node, random_message_type, random_bytes_data_stream, GetTime<std::chrono::microseconds>(), std::atomic<bool>{false});
} catch (const std::ios_base::failure& e) {
CSerializedNetMsg net_msg;
net_msg.m_type = random_message_type;
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);

connman.FlushSendBuffer(p2p_node);
(void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));

bool more_work{true};
while (more_work) {
p2p_node.fPauseSend = false;
try {
more_work = connman.ProcessMessagesOnce(p2p_node);
} catch (const std::ios_base::failure&) {
}
g_setup->m_node.peerman->SendMessages(&p2p_node);
}
g_setup->m_node.peerman->SendMessages(&p2p_node);
SyncWithValidationInterfaceQueue();
g_setup->m_node.connman->StopNodes();
}
16 changes: 10 additions & 6 deletions src/test/fuzz/process_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,23 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)

CSerializedNetMsg net_msg;
net_msg.m_type = random_message_type;
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);

CNode& random_node = *PickValue(fuzzed_data_provider, peers);

connman.FlushSendBuffer(random_node);
(void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
random_node.fPauseSend = false;

try {
connman.ProcessMessagesOnce(random_node);
} catch (const std::ios_base::failure&) {
bool more_work{true};
while (more_work) { // Ensure that every message is eventually processed in some way or another
random_node.fPauseSend = false;

try {
more_work = connman.ProcessMessagesOnce(random_node);
} catch (const std::ios_base::failure&) {
}
g_setup->m_node.peerman->SendMessages(&random_node);
}
g_setup->m_node.peerman->SendMessages(&random_node);
}
SyncWithValidationInterfaceQueue();
g_setup->m_node.connman->StopNodes();
Expand Down
5 changes: 4 additions & 1 deletion src/test/util/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ struct ConnmanTestMsg : public CConnman {
bool relay_txs)
EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex);

void ProcessMessagesOnce(CNode& node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); }
bool ProcessMessagesOnce(CNode& node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex)
{
return m_msgproc->ProcessMessages(&node, flagInterruptMsgProc);
}

void NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const;

Expand Down
Loading