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>
9 #include <util/system.h>
10 
11 #include <cstdint>
12 #include <string>
13 #include <vector>
14 
15 namespace {
16 void initialize_system()
17 {
18  static const auto testing_setup = MakeNoLogFileContext<>();
19 }
20 
21 std::string GetArgumentName(const std::string& name)
22 {
23  size_t idx = name.find('=');
24  if (idx == std::string::npos) {
25  idx = name.size();
26  }
27  return name.substr(0, idx);
28 }
29 
30 FUZZ_TARGET_INIT(system, initialize_system)
31 {
32  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33  ArgsManager args_manager{};
34 
35  if (fuzzed_data_provider.ConsumeBool()) {
36  SetupHelpOptions(args_manager);
37  }
38 
39  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 3000)
40  {
41  CallOneOf(
42  fuzzed_data_provider,
43  [&] {
44  args_manager.SelectConfigNetwork(fuzzed_data_provider.ConsumeRandomLengthString(16));
45  },
46  [&] {
47  args_manager.SoftSetArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeRandomLengthString(16));
48  },
49  [&] {
50  args_manager.ForceSetArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeRandomLengthString(16));
51  },
52  [&] {
53  args_manager.SoftSetBoolArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeBool());
54  },
55  [&] {
57  // Avoid hitting:
58  // util/system.cpp:425: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
59  const std::string argument_name = GetArgumentName(fuzzed_data_provider.ConsumeRandomLengthString(16));
60  if (args_manager.GetArgFlags(argument_name) != std::nullopt) {
61  return;
62  }
63  args_manager.AddArg(argument_name, fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeIntegral<unsigned int>() & ~ArgsManager::COMMAND, options_category);
64  },
65  [&] {
66  // Avoid hitting:
67  // util/system.cpp:425: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
68  const std::vector<std::string> names = ConsumeRandomLengthStringVector(fuzzed_data_provider);
69  std::vector<std::string> hidden_arguments;
70  for (const std::string& name : names) {
71  const std::string hidden_argument = GetArgumentName(name);
72  if (args_manager.GetArgFlags(hidden_argument) != std::nullopt) {
73  continue;
74  }
75  if (std::find(hidden_arguments.begin(), hidden_arguments.end(), hidden_argument) != hidden_arguments.end()) {
76  continue;
77  }
78  hidden_arguments.push_back(hidden_argument);
79  }
80  args_manager.AddHiddenArgs(hidden_arguments);
81  },
82  [&] {
83  args_manager.ClearArgs();
84  },
85  [&] {
86  const std::vector<std::string> random_arguments = ConsumeRandomLengthStringVector(fuzzed_data_provider);
87  std::vector<const char*> argv;
88  argv.reserve(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  });
98  }
99 
100  const std::string s1 = fuzzed_data_provider.ConsumeRandomLengthString(16);
101  const std::string s2 = fuzzed_data_provider.ConsumeRandomLengthString(16);
102  const int64_t i64 = fuzzed_data_provider.ConsumeIntegral<int64_t>();
103  const bool b = fuzzed_data_provider.ConsumeBool();
104 
105  (void)args_manager.GetIntArg(s1, i64);
106  (void)args_manager.GetArg(s1, s2);
107  (void)args_manager.GetArgFlags(s1);
108  (void)args_manager.GetArgs(s1);
109  (void)args_manager.GetBoolArg(s1, b);
110  try {
111  (void)args_manager.GetChainName();
112  } catch (const std::runtime_error&) {
113  }
114  (void)args_manager.GetHelpMessage();
115  (void)args_manager.GetUnrecognizedSections();
116  (void)args_manager.GetUnsuitableSectionOnlyArgs();
117  (void)args_manager.IsArgNegated(s1);
118  (void)args_manager.IsArgSet(s1);
119 
120  (void)HelpRequested(args_manager);
121 }
122 } // namespace
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:81
OptionsCategory::RPC
@ RPC
setup_common.h
util.h
OptionsCategory::CONNECTION
@ CONNECTION
HelpRequested
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:734
SetupHelpOptions
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:739
OptionsCategory::HIDDEN
@ HIDDEN
OptionsCategory::NODE_RELAY
@ NODE_RELAY
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:42
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:52
OptionsCategory::BLOCK_CREATION
@ BLOCK_CREATION
system.h
ArgsManager::COMMAND
@ COMMAND
Definition: system.h:182
ArgsManager
Definition: system.h:158
OptionsCategory::WALLET_DEBUG_TEST
@ WALLET_DEBUG_TEST
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
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
OptionsCategory::CHAINPARAMS
@ CHAINPARAMS
FUZZ_TARGET_INIT
#define FUZZ_TARGET_INIT(name, init_fun)
Definition: fuzz.h:34
OptionsCategory::GUI
@ GUI