Skip to content

Commit a19598c

Browse files
tests: Add fuzzing harness for functions in system.h (ArgsManager)
1 parent 9ddfce6 commit a19598c

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

src/Makefile.test.include

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ FUZZ_TARGETS = \
6464
test/fuzz/parse_numbers \
6565
test/fuzz/parse_script \
6666
test/fuzz/parse_univalue \
67-
test/fuzz/prevector \
6867
test/fuzz/partial_merkle_tree_deserialize \
6968
test/fuzz/partially_signed_transaction_deserialize \
7069
test/fuzz/pow \
7170
test/fuzz/prefilled_transaction_deserialize \
7271
test/fuzz/primitives_transaction \
73-
test/fuzz/process_messages \
72+
test/fuzz/prevector \
7473
test/fuzz/process_message \
7574
test/fuzz/process_message_addr \
7675
test/fuzz/process_message_block \
@@ -96,6 +95,7 @@ FUZZ_TARGETS = \
9695
test/fuzz/process_message_tx \
9796
test/fuzz/process_message_verack \
9897
test/fuzz/process_message_version \
98+
test/fuzz/process_messages \
9999
test/fuzz/protocol \
100100
test/fuzz/psbt \
101101
test/fuzz/psbt_input_deserialize \
@@ -116,6 +116,7 @@ FUZZ_TARGETS = \
116116
test/fuzz/string \
117117
test/fuzz/strprintf \
118118
test/fuzz/sub_net_deserialize \
119+
test/fuzz/system \
119120
test/fuzz/timedata \
120121
test/fuzz/transaction \
121122
test/fuzz/tx_in \
@@ -969,6 +970,12 @@ test_fuzz_sub_net_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
969970
test_fuzz_sub_net_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
970971
test_fuzz_sub_net_deserialize_SOURCES = test/fuzz/deserialize.cpp
971972

973+
test_fuzz_system_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
974+
test_fuzz_system_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
975+
test_fuzz_system_LDADD = $(FUZZ_SUITE_LD_COMMON)
976+
test_fuzz_system_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
977+
test_fuzz_system_SOURCES = test/fuzz/system.cpp
978+
972979
test_fuzz_timedata_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
973980
test_fuzz_timedata_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
974981
test_fuzz_timedata_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/system.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <test/fuzz/FuzzedDataProvider.h>
6+
#include <test/fuzz/fuzz.h>
7+
#include <test/fuzz/util.h>
8+
#include <util/system.h>
9+
10+
#include <cstdint>
11+
#include <string>
12+
#include <vector>
13+
14+
namespace {
15+
std::string GetArgumentName(const std::string& name)
16+
{
17+
size_t idx = name.find('=');
18+
if (idx == std::string::npos) {
19+
idx = name.size();
20+
}
21+
return name.substr(0, idx);
22+
}
23+
} // namespace
24+
25+
void test_one_input(const std::vector<uint8_t>& buffer)
26+
{
27+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
28+
ArgsManager args_manager{};
29+
30+
if (fuzzed_data_provider.ConsumeBool()) {
31+
SetupHelpOptions(args_manager);
32+
}
33+
34+
while (fuzzed_data_provider.ConsumeBool()) {
35+
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 7)) {
36+
case 0: {
37+
args_manager.SelectConfigNetwork(fuzzed_data_provider.ConsumeRandomLengthString(16));
38+
break;
39+
}
40+
case 1: {
41+
args_manager.SoftSetArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeRandomLengthString(16));
42+
break;
43+
}
44+
case 2: {
45+
args_manager.ForceSetArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeRandomLengthString(16));
46+
break;
47+
}
48+
case 3: {
49+
args_manager.SoftSetBoolArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeBool());
50+
break;
51+
}
52+
case 4: {
53+
const OptionsCategory options_category = fuzzed_data_provider.PickValueInArray<OptionsCategory>({OptionsCategory::OPTIONS, OptionsCategory::CONNECTION, OptionsCategory::WALLET, OptionsCategory::WALLET_DEBUG_TEST, OptionsCategory::ZMQ, OptionsCategory::DEBUG_TEST, OptionsCategory::CHAINPARAMS, OptionsCategory::NODE_RELAY, OptionsCategory::BLOCK_CREATION, OptionsCategory::RPC, OptionsCategory::GUI, OptionsCategory::COMMANDS, OptionsCategory::REGISTER_COMMANDS, OptionsCategory::HIDDEN});
54+
// Avoid hitting:
55+
// util/system.cpp:425: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
56+
const std::string argument_name = GetArgumentName(fuzzed_data_provider.ConsumeRandomLengthString(16));
57+
if (args_manager.GetArgFlags(argument_name) != nullopt) {
58+
break;
59+
}
60+
args_manager.AddArg(argument_name, fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeIntegral<unsigned int>(), options_category);
61+
break;
62+
}
63+
case 5: {
64+
// Avoid hitting:
65+
// util/system.cpp:425: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
66+
const std::vector<std::string> names = ConsumeRandomLengthStringVector(fuzzed_data_provider);
67+
std::vector<std::string> hidden_arguments;
68+
for (const std::string& name : names) {
69+
const std::string hidden_argument = GetArgumentName(name);
70+
if (args_manager.GetArgFlags(hidden_argument) != nullopt) {
71+
continue;
72+
}
73+
if (std::find(hidden_arguments.begin(), hidden_arguments.end(), hidden_argument) != hidden_arguments.end()) {
74+
continue;
75+
}
76+
hidden_arguments.push_back(hidden_argument);
77+
}
78+
args_manager.AddHiddenArgs(hidden_arguments);
79+
break;
80+
}
81+
case 6: {
82+
args_manager.ClearArgs();
83+
break;
84+
}
85+
case 7: {
86+
const std::vector<std::string> random_arguments = ConsumeRandomLengthStringVector(fuzzed_data_provider);
87+
std::vector<const char*> argv;
88+
argv.resize(random_arguments.size());
89+
for (const std::string& random_argument : random_arguments) {
90+
argv.push_back(random_argument.c_str());
91+
}
92+
try {
93+
std::string error;
94+
(void)args_manager.ParseParameters(argv.size(), argv.data(), error);
95+
} catch (const std::logic_error&) {
96+
}
97+
break;
98+
}
99+
}
100+
}
101+
102+
const std::string s1 = fuzzed_data_provider.ConsumeRandomLengthString(16);
103+
const std::string s2 = fuzzed_data_provider.ConsumeRandomLengthString(16);
104+
const int64_t i64 = fuzzed_data_provider.ConsumeIntegral<int64_t>();
105+
const bool b = fuzzed_data_provider.ConsumeBool();
106+
107+
(void)args_manager.GetArg(s1, i64);
108+
(void)args_manager.GetArg(s1, s2);
109+
(void)args_manager.GetArgFlags(s1);
110+
(void)args_manager.GetArgs(s1);
111+
(void)args_manager.GetBoolArg(s1, b);
112+
try {
113+
(void)args_manager.GetChainName();
114+
} catch (const std::runtime_error&) {
115+
}
116+
(void)args_manager.GetHelpMessage();
117+
(void)args_manager.GetUnrecognizedSections();
118+
(void)args_manager.GetUnsuitableSectionOnlyArgs();
119+
(void)args_manager.IsArgNegated(s1);
120+
(void)args_manager.IsArgSet(s1);
121+
122+
(void)HelpRequested(args_manager);
123+
}

0 commit comments

Comments
 (0)