Skip to content

Commit 927b001

Browse files
committed
Merge bitcoin/bitcoin#27766: fuzz: Change LIMIT_TO_MESSAGE_TYPE from a compile-time to a run-time setting
1111c9a fuzz: Change LIMIT_TO_MESSAGE_TYPE from a compile-time to a run-time setting (MarcoFalke) Pull request description: The `process_message_${msg_type}` fuzz targets have many issues: * In a context where each fuzz target must be a separate binary, this bloats the storage requirements by the number of message types. * The qa-assets repo for fuzz inputs also bloats, because each input in the type specific folder (`./process_message_${msg_type}`) is accompanied by a similar input in the general folder (`./process_message`) or a in another specific folder. The size seems to be ~3GB for the sum of all folders vs 0.3GB for the general folder. * Handling of different folders for each message type and one general folder for all message types (and unknown message types) is undocumented and unclear. Cross-pollination is encouraged, I guess, but who does it? * It is unclear if the fuzz target has any value at all, given that any bug that is found here should also be found by the `process_messages` fuzz target, and historically always has been? So maybe it can even be removed completely in the future? * (minor nit): When adding a new message type, the message type has to be added to this fuzz target as well. Fix all issues by turning the compile-time setting into a run-time setting, thus removing the extra executables and fuzz folders. The same approach is also taken by the `rpc` fuzz target. If someone wants to limit their fuzzing to a specific message type, they can still do it. For example, ``` LIMIT_TO_MESSAGE_TYPE=inv FUZZ=process_message ./src/test/fuzz/fuzz ACKs for top commit: dergoegge: ACK 1111c9a Tree-SHA512: 9495538d9bc83b24671a44e9311a4e82ce5b2fa89e431e42772dcfa19f675a9fe2dd8cd3d3b15b154c8b7f400e57ee08a976d37e55a5425778ced0ee85fb984c
2 parents 10c4a46 + 1111c9a commit 927b001

File tree

2 files changed

+18
-64
lines changed

2 files changed

+18
-64
lines changed

src/test/fuzz/process_message.cpp

Lines changed: 17 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <banman.h>
6-
#include <chainparams.h>
75
#include <consensus/consensus.h>
86
#include <net.h>
97
#include <net_processing.h>
8+
#include <primitives/transaction.h>
109
#include <protocol.h>
11-
#include <scheduler.h>
1210
#include <script/script.h>
11+
#include <serialize.h>
12+
#include <span.h>
1313
#include <streams.h>
14+
#include <sync.h>
1415
#include <test/fuzz/FuzzedDataProvider.h>
1516
#include <test/fuzz/fuzz.h>
1617
#include <test/fuzz/util.h>
@@ -20,42 +21,32 @@
2021
#include <test/util/setup_common.h>
2122
#include <test/util/validation.h>
2223
#include <util/chaintype.h>
24+
#include <util/check.h>
25+
#include <util/time.h>
26+
#include <validation.h>
2327
#include <validationinterface.h>
2428
#include <version.h>
2529

30+
2631
#include <atomic>
27-
#include <cassert>
28-
#include <chrono>
29-
#include <cstdint>
30-
#include <iosfwd>
32+
#include <cstdlib>
3133
#include <iostream>
3234
#include <memory>
3335
#include <string>
36+
#include <string_view>
37+
#include <vector>
3438

3539
namespace {
3640
const TestingSetup* g_setup;
41+
std::string_view LIMIT_TO_MESSAGE_TYPE{};
3742
} // namespace
3843

39-
size_t& GetNumMsgTypes()
40-
{
41-
static size_t g_num_msg_types{0};
42-
return g_num_msg_types;
43-
}
44-
#define FUZZ_TARGET_MSG(msg_type) \
45-
struct msg_type##_Count_Before_Main { \
46-
msg_type##_Count_Before_Main() \
47-
{ \
48-
++GetNumMsgTypes(); \
49-
} \
50-
} const static g_##msg_type##_count_before_main; \
51-
FUZZ_TARGET_INIT(process_message_##msg_type, initialize_process_message) \
52-
{ \
53-
fuzz_target(buffer, #msg_type); \
54-
}
55-
5644
void initialize_process_message()
5745
{
58-
Assert(GetNumMsgTypes() == getAllNetMessageTypes().size()); // If this fails, add or remove the message type below
46+
if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
47+
LIMIT_TO_MESSAGE_TYPE = val;
48+
Assert(std::count(getAllNetMessageTypes().begin(), getAllNetMessageTypes().end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
49+
}
5950

6051
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
6152
/*chain_type=*/ChainType::REGTEST,
@@ -67,7 +58,7 @@ void initialize_process_message()
6758
SyncWithValidationInterfaceQueue();
6859
}
6960

70-
void fuzz_target(FuzzBufferType buffer, const std::string& LIMIT_TO_MESSAGE_TYPE)
61+
FUZZ_TARGET_INIT(process_message, initialize_process_message)
7162
{
7263
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
7364

@@ -101,40 +92,3 @@ void fuzz_target(FuzzBufferType buffer, const std::string& LIMIT_TO_MESSAGE_TYPE
10192
SyncWithValidationInterfaceQueue();
10293
g_setup->m_node.connman->StopNodes();
10394
}
104-
105-
FUZZ_TARGET_INIT(process_message, initialize_process_message) { fuzz_target(buffer, ""); }
106-
FUZZ_TARGET_MSG(addr);
107-
FUZZ_TARGET_MSG(addrv2);
108-
FUZZ_TARGET_MSG(block);
109-
FUZZ_TARGET_MSG(blocktxn);
110-
FUZZ_TARGET_MSG(cfcheckpt);
111-
FUZZ_TARGET_MSG(cfheaders);
112-
FUZZ_TARGET_MSG(cfilter);
113-
FUZZ_TARGET_MSG(cmpctblock);
114-
FUZZ_TARGET_MSG(feefilter);
115-
FUZZ_TARGET_MSG(filteradd);
116-
FUZZ_TARGET_MSG(filterclear);
117-
FUZZ_TARGET_MSG(filterload);
118-
FUZZ_TARGET_MSG(getaddr);
119-
FUZZ_TARGET_MSG(getblocks);
120-
FUZZ_TARGET_MSG(getblocktxn);
121-
FUZZ_TARGET_MSG(getcfcheckpt);
122-
FUZZ_TARGET_MSG(getcfheaders);
123-
FUZZ_TARGET_MSG(getcfilters);
124-
FUZZ_TARGET_MSG(getdata);
125-
FUZZ_TARGET_MSG(getheaders);
126-
FUZZ_TARGET_MSG(headers);
127-
FUZZ_TARGET_MSG(inv);
128-
FUZZ_TARGET_MSG(mempool);
129-
FUZZ_TARGET_MSG(merkleblock);
130-
FUZZ_TARGET_MSG(notfound);
131-
FUZZ_TARGET_MSG(ping);
132-
FUZZ_TARGET_MSG(pong);
133-
FUZZ_TARGET_MSG(sendaddrv2);
134-
FUZZ_TARGET_MSG(sendcmpct);
135-
FUZZ_TARGET_MSG(sendheaders);
136-
FUZZ_TARGET_MSG(sendtxrcncl);
137-
FUZZ_TARGET_MSG(tx);
138-
FUZZ_TARGET_MSG(verack);
139-
FUZZ_TARGET_MSG(version);
140-
FUZZ_TARGET_MSG(wtxidrelay);

src/test/util/setup_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <common/args.h>
99
#include <key.h>
1010
#include <node/caches.h>
11-
#include <node/context.h>
11+
#include <node/context.h> // IWYU pragma: export
1212
#include <primitives/transaction.h>
1313
#include <pubkey.h>
1414
#include <random.h>

0 commit comments

Comments
 (0)