Bitcoin Core  22.99.0
P2P Digital Currency
system.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 
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 FUZZ_TARGET(system)
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  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 3000)
35  {
36  CallOneOf(
37  fuzzed_data_provider,
38  [&] {
39  args_manager.SelectConfigNetwork(fuzzed_data_provider.ConsumeRandomLengthString(16));
40  },
41  [&] {
42  args_manager.SoftSetArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeRandomLengthString(16));
43  },
44  [&] {
45  args_manager.ForceSetArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeRandomLengthString(16));
46  },
47  [&] {
48  args_manager.SoftSetBoolArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeBool());
49  },
50  [&] {
52  // Avoid hitting:
53  // util/system.cpp:425: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
54  const std::string argument_name = GetArgumentName(fuzzed_data_provider.ConsumeRandomLengthString(16));
55  if (args_manager.GetArgFlags(argument_name) != std::nullopt) {
56  return;
57  }
58  args_manager.AddArg(argument_name, fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeIntegral<unsigned int>() & ~ArgsManager::COMMAND, options_category);
59  },
60  [&] {
61  // Avoid hitting:
62  // util/system.cpp:425: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
63  const std::vector<std::string> names = ConsumeRandomLengthStringVector(fuzzed_data_provider);
64  std::vector<std::string> hidden_arguments;
65  for (const std::string& name : names) {
66  const std::string hidden_argument = GetArgumentName(name);
67  if (args_manager.GetArgFlags(hidden_argument) != std::nullopt) {
68  continue;
69  }
70  if (std::find(hidden_arguments.begin(), hidden_arguments.end(), hidden_argument) != hidden_arguments.end()) {
71  continue;
72  }
73  hidden_arguments.push_back(hidden_argument);
74  }
75  args_manager.AddHiddenArgs(hidden_arguments);
76  },
77  [&] {
78  args_manager.ClearArgs();
79  },
80  [&] {
81  const std::vector<std::string> random_arguments = ConsumeRandomLengthStringVector(fuzzed_data_provider);
82  std::vector<const char*> argv;
83  argv.reserve(random_arguments.size());
84  for (const std::string& random_argument : random_arguments) {
85  argv.push_back(random_argument.c_str());
86  }
87  try {
88  std::string error;
89  (void)args_manager.ParseParameters(argv.size(), argv.data(), error);
90  } catch (const std::logic_error&) {
91  }
92  });
93  }
94 
95  const std::string s1 = fuzzed_data_provider.ConsumeRandomLengthString(16);
96  const std::string s2 = fuzzed_data_provider.ConsumeRandomLengthString(16);
97  const int64_t i64 = fuzzed_data_provider.ConsumeIntegral<int64_t>();
98  const bool b = fuzzed_data_provider.ConsumeBool();
99 
100  (void)args_manager.GetArg(s1, i64);
101  (void)args_manager.GetArg(s1, s2);
102  (void)args_manager.GetArgFlags(s1);
103  (void)args_manager.GetArgs(s1);
104  (void)args_manager.GetBoolArg(s1, b);
105  try {
106  (void)args_manager.GetChainName();
107  } catch (const std::runtime_error&) {
108  }
109  (void)args_manager.GetHelpMessage();
110  (void)args_manager.GetUnrecognizedSections();
111  (void)args_manager.GetUnsuitableSectionOnlyArgs();
112  (void)args_manager.IsArgNegated(s1);
113  (void)args_manager.IsArgSet(s1);
114 
115  (void)HelpRequested(args_manager);
116 }
ConsumeRandomLengthStringVector
std::vector< std::string > ConsumeRandomLengthStringVector(FuzzedDataProvider &fuzzed_data_provider, const size_t max_vector_size=16, const size_t max_string_length=16) noexcept
Definition: util.h:79
OptionsCategory::RPC
@ RPC
util.h
FuzzedDataProvider::ConsumeRandomLengthString
std::string ConsumeRandomLengthString(size_t max_length)
Definition: FuzzedDataProvider.h:152
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
OptionsCategory::CONNECTION
@ CONNECTION
FUZZ_TARGET
FUZZ_TARGET(system)
Definition: system.cpp:25
HelpRequested
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:737
SetupHelpOptions
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:742
OptionsCategory::HIDDEN
@ HIDDEN
OptionsCategory::NODE_RELAY
@ NODE_RELAY
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
FuzzedDataProvider.h
OptionsCategory::OPTIONS
@ OPTIONS
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
OptionsCategory
OptionsCategory
Definition: system.h:133
OptionsCategory::ZMQ
@ ZMQ
name
const char * name
Definition: rest.cpp:43
OptionsCategory::BLOCK_CREATION
@ BLOCK_CREATION
system.h
ArgsManager::COMMAND
@ COMMAND
Definition: system.h:176
ArgsManager
Definition: system.h:158
OptionsCategory::WALLET_DEBUG_TEST
@ WALLET_DEBUG_TEST
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
OptionsCategory::WALLET
@ WALLET
OptionsCategory::DEBUG_TEST
@ DEBUG_TEST
OptionsCategory::COMMANDS
@ COMMANDS
OptionsCategory::REGISTER_COMMANDS
@ REGISTER_COMMANDS
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
OptionsCategory::CHAINPARAMS
@ CHAINPARAMS
OptionsCategory::GUI
@ GUI