Skip to content

Commit ef16173

Browse files
Merge #7080: backport: Merge bitcoin#29009, 29079
b954bf7 Merge bitcoin#29079: fuzz: Limit p2p fuzz targets to MAX_PROTOCOL_MESSAGE_LENGTH (fanquake) 3a2af90 Merge bitcoin#29009: fuzz: p2p: Detect peer deadlocks (fanquake) Pull request description: bitcoin backports Top commit has no ACKs. Tree-SHA512: 24d802079c010928c93a5dd9d9a75a3c99cc7ba24ecb83dfd9ed3e23d7e46174535862058e22376d9bc611ace69b53ecaa33a9548e541b1f981bfb8ce89ae608
2 parents a4d5c04 + b954bf7 commit ef16173

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

src/test/fuzz/process_message.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
// Copyright (c) 2020 The Bitcoin Core developers
1+
// Copyright (c) 2020-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <consensus/consensus.h>
66
#include <net.h>
77
#include <protocol.h>
88
#include <script/script.h>
9-
#include <serialize.h>
10-
#include <streams.h>
119
#include <sync.h>
1210
#include <test/fuzz/FuzzedDataProvider.h>
1311
#include <test/fuzz/fuzz.h>
@@ -19,7 +17,6 @@
1917
#include <validationinterface.h>
2018
#include <version.h>
2119

22-
#include <atomic>
2320
#include <cstdlib>
2421
#include <iostream>
2522
#include <memory>
@@ -72,13 +69,22 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)
7269
const auto mock_time = ConsumeTime(fuzzed_data_provider);
7370
SetMockTime(mock_time);
7471

75-
// fuzzed_data_provider is fully consumed after this call, don't use it
76-
CDataStream random_bytes_data_stream{fuzzed_data_provider.ConsumeRemainingBytes<unsigned char>(), SER_NETWORK, PROTOCOL_VERSION};
77-
try {
78-
g_setup->m_node.peerman->ProcessMessage(p2p_node, random_message_type, random_bytes_data_stream, GetTime<std::chrono::microseconds>(), std::atomic<bool>{false});
79-
} catch (const std::ios_base::failure& e) {
72+
CSerializedNetMsg net_msg;
73+
net_msg.m_type = random_message_type;
74+
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
75+
76+
connman.FlushSendBuffer(p2p_node);
77+
(void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
78+
79+
bool more_work{true};
80+
while (more_work) {
81+
p2p_node.fPauseSend = false;
82+
try {
83+
more_work = connman.ProcessMessagesOnce(p2p_node);
84+
} catch (const std::ios_base::failure&) {
85+
}
86+
g_setup->m_node.peerman->SendMessages(&p2p_node);
8087
}
81-
g_setup->m_node.peerman->SendMessages(&p2p_node);
8288
SyncWithValidationInterfaceQueue();
8389
g_setup->m_node.connman->StopNodes();
8490
}

src/test/fuzz/process_messages.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,23 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)
6262

6363
CSerializedNetMsg net_msg;
6464
net_msg.m_type = random_message_type;
65-
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
65+
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
6666

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

6969
connman.FlushSendBuffer(random_node);
7070
(void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
71-
random_node.fPauseSend = false;
7271

73-
try {
74-
connman.ProcessMessagesOnce(random_node);
75-
} catch (const std::ios_base::failure&) {
72+
bool more_work{true};
73+
while (more_work) { // Ensure that every message is eventually processed in some way or another
74+
random_node.fPauseSend = false;
75+
76+
try {
77+
more_work = connman.ProcessMessagesOnce(random_node);
78+
} catch (const std::ios_base::failure&) {
79+
}
80+
g_setup->m_node.peerman->SendMessages(&random_node);
7681
}
77-
g_setup->m_node.peerman->SendMessages(&random_node);
7882
}
7983
SyncWithValidationInterfaceQueue();
8084
g_setup->m_node.connman->StopNodes();

src/test/util/net.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ struct ConnmanTestMsg : public CConnman {
7070
bool relay_txs)
7171
EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex);
7272

73-
void ProcessMessagesOnce(CNode& node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); }
73+
bool ProcessMessagesOnce(CNode& node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex)
74+
{
75+
return m_msgproc->ProcessMessages(&node, flagInterruptMsgProc);
76+
}
7477

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

0 commit comments

Comments
 (0)