Bitcoin Core  22.99.0
P2P Digital Currency
util.h
Go to the documentation of this file.
1 // Copyright (c) 2009-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 
5 #ifndef BITCOIN_TEST_FUZZ_UTIL_H
6 #define BITCOIN_TEST_FUZZ_UTIL_H
7 
8 #include <arith_uint256.h>
9 #include <attributes.h>
10 #include <chainparamsbase.h>
11 #include <coins.h>
12 #include <compat.h>
13 #include <consensus/amount.h>
14 #include <consensus/consensus.h>
15 #include <merkleblock.h>
16 #include <net.h>
17 #include <netaddress.h>
18 #include <netbase.h>
19 #include <primitives/transaction.h>
20 #include <script/script.h>
21 #include <script/standard.h>
22 #include <serialize.h>
23 #include <streams.h>
25 #include <test/fuzz/fuzz.h>
26 #include <test/util/net.h>
27 #include <txmempool.h>
28 #include <uint256.h>
29 #include <version.h>
30 
31 #include <algorithm>
32 #include <array>
33 #include <cstdint>
34 #include <cstdio>
35 #include <optional>
36 #include <string>
37 #include <vector>
38 
39 class PeerManager;
40 
41 template <typename... Callables>
42 size_t CallOneOf(FuzzedDataProvider& fuzzed_data_provider, Callables... callables)
43 {
44  constexpr size_t call_size{sizeof...(callables)};
45  static_assert(call_size >= 1);
46  const size_t call_index{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, call_size - 1)};
47 
48  size_t i{0};
49  ((i++ == call_index ? callables() : void()), ...);
50  return call_size;
51 }
52 
53 template <typename Collection>
54 auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
55 {
56  const auto sz = col.size();
57  assert(sz >= 1);
58  auto it = col.begin();
59  std::advance(it, fuzzed_data_provider.ConsumeIntegralInRange<decltype(sz)>(0, sz - 1));
60  return *it;
61 }
62 
63 [[nodiscard]] inline std::vector<uint8_t> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
64 {
65  const std::string s = max_length ?
66  fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
67  fuzzed_data_provider.ConsumeRandomLengthString();
68  return {s.begin(), s.end()};
69 }
70 
71 [[nodiscard]] inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
72 {
73  return BytesToBits(ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length));
74 }
75 
76 [[nodiscard]] inline CDataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
77 {
78  return CDataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length), SER_NETWORK, INIT_PROTO_VERSION};
79 }
80 
81 [[nodiscard]] inline std::vector<std::string> ConsumeRandomLengthStringVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16, const size_t max_string_length = 16) noexcept
82 {
83  const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
84  std::vector<std::string> r;
85  for (size_t i = 0; i < n_elements; ++i) {
86  r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length));
87  }
88  return r;
89 }
90 
91 template <typename T>
92 [[nodiscard]] inline std::vector<T> ConsumeRandomLengthIntegralVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16) noexcept
93 {
94  const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
95  std::vector<T> r;
96  for (size_t i = 0; i < n_elements; ++i) {
97  r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
98  }
99  return r;
100 }
101 
102 template <typename T>
103 [[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
104 {
105  const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
107  T obj;
108  try {
109  ds >> obj;
110  } catch (const std::ios_base::failure&) {
111  return std::nullopt;
112  }
113  return obj;
114 }
115 
116 template <typename WeakEnumType, size_t size>
117 [[nodiscard]] WeakEnumType ConsumeWeakEnum(FuzzedDataProvider& fuzzed_data_provider, const WeakEnumType (&all_types)[size]) noexcept
118 {
119  return fuzzed_data_provider.ConsumeBool() ?
120  fuzzed_data_provider.PickValueInArray<WeakEnumType>(all_types) :
121  WeakEnumType(fuzzed_data_provider.ConsumeIntegral<typename std::underlying_type<WeakEnumType>::type>());
122 }
123 
124 [[nodiscard]] inline opcodetype ConsumeOpcodeType(FuzzedDataProvider& fuzzed_data_provider) noexcept
125 {
126  return static_cast<opcodetype>(fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, MAX_OPCODE));
127 }
128 
129 [[nodiscard]] CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::optional<CAmount>& max = std::nullopt) noexcept;
130 
131 [[nodiscard]] int64_t ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional<int64_t>& min = std::nullopt, const std::optional<int64_t>& max = std::nullopt) noexcept;
132 
133 [[nodiscard]] CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider, const std::optional<std::vector<uint256>>& prevout_txids, const int max_num_in = 10, const int max_num_out = 10) noexcept;
134 
135 [[nodiscard]] CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, const size_t max_stack_elem_size = 32) noexcept;
136 
137 [[nodiscard]] CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const bool maybe_p2wsh = false) noexcept;
138 
139 [[nodiscard]] uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept;
140 
141 [[nodiscard]] inline CScriptNum ConsumeScriptNum(FuzzedDataProvider& fuzzed_data_provider) noexcept
142 {
143  return CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
144 }
145 
146 [[nodiscard]] inline uint160 ConsumeUInt160(FuzzedDataProvider& fuzzed_data_provider) noexcept
147 {
148  const std::vector<uint8_t> v160 = fuzzed_data_provider.ConsumeBytes<uint8_t>(160 / 8);
149  if (v160.size() != 160 / 8) {
150  return {};
151  }
152  return uint160{v160};
153 }
154 
155 [[nodiscard]] inline uint256 ConsumeUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
156 {
157  const std::vector<uint8_t> v256 = fuzzed_data_provider.ConsumeBytes<uint8_t>(256 / 8);
158  if (v256.size() != 256 / 8) {
159  return {};
160  }
161  return uint256{v256};
162 }
163 
164 [[nodiscard]] inline arith_uint256 ConsumeArithUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
165 {
166  return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
167 }
168 
169 [[nodiscard]] CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider, const CTransaction& tx) noexcept;
170 
171 [[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept;
172 
173 template <typename T>
174 [[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept
175 {
176  static_assert(std::is_integral<T>::value, "Integral required.");
177  if (std::numeric_limits<T>::is_signed) {
178  if (i > 0) {
179  if (j > 0) {
180  return i > (std::numeric_limits<T>::max() / j);
181  } else {
182  return j < (std::numeric_limits<T>::min() / i);
183  }
184  } else {
185  if (j > 0) {
186  return i < (std::numeric_limits<T>::min() / j);
187  } else {
188  return i != 0 && (j < (std::numeric_limits<T>::max() / i));
189  }
190  }
191  } else {
192  return j != 0 && i > std::numeric_limits<T>::max() / j;
193  }
194 }
195 
196 [[nodiscard]] bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept;
197 
201 template <typename T, size_t size>
202 void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider, const std::array<T, size>& errnos)
203 {
204  errno = fuzzed_data_provider.PickValueInArray(errnos);
205 }
206 
207 /*
208  * Sets a fuzzed errno in the range [0, 133 (EHWPOISON)]. Can be used from functions emulating
209  * standard library functions that set errno, or in other contexts where the value of errno
210  * might be relevant for the execution path that will be taken.
211  */
212 inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept
213 {
214  errno = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 133);
215 }
216 
221 [[nodiscard]] inline std::vector<uint8_t> ConsumeFixedLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t length) noexcept
222 {
223  std::vector<uint8_t> result(length);
224  const std::vector<uint8_t> random_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(length);
225  if (!random_bytes.empty()) {
226  std::memcpy(result.data(), random_bytes.data(), random_bytes.size());
227  }
228  return result;
229 }
230 
231 CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider) noexcept;
232 
233 inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept
234 {
235  return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
236 }
237 
238 inline CService ConsumeService(FuzzedDataProvider& fuzzed_data_provider) noexcept
239 {
240  return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
241 }
242 
243 inline CAddress ConsumeAddress(FuzzedDataProvider& fuzzed_data_provider) noexcept
244 {
245  return {ConsumeService(fuzzed_data_provider), ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS), fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
246 }
247 
248 template <bool ReturnUniquePtr = false>
249 auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<NodeId>& node_id_in = std::nullopt) noexcept
250 {
251  const NodeId node_id = node_id_in.value_or(fuzzed_data_provider.ConsumeIntegralInRange<NodeId>(0, std::numeric_limits<NodeId>::max()));
252  const ServiceFlags local_services = ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS);
253  const SOCKET socket = INVALID_SOCKET;
254  const CAddress address = ConsumeAddress(fuzzed_data_provider);
255  const uint64_t keyed_net_group = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
256  const uint64_t local_host_nonce = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
257  const CAddress addr_bind = ConsumeAddress(fuzzed_data_provider);
258  const std::string addr_name = fuzzed_data_provider.ConsumeRandomLengthString(64);
259  const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES);
260  const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false};
261  if constexpr (ReturnUniquePtr) {
262  return std::make_unique<CNode>(node_id, local_services, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion);
263  } else {
264  return CNode{node_id, local_services, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion};
265  }
266 }
267 inline std::unique_ptr<CNode> ConsumeNodeAsUniquePtr(FuzzedDataProvider& fdp, const std::optional<NodeId>& node_id_in = std::nullopt) { return ConsumeNode<true>(fdp, node_id_in); }
268 
269 void FillNode(FuzzedDataProvider& fuzzed_data_provider, ConnmanTestMsg& connman, PeerManager& peerman, CNode& node) noexcept;
270 
272 {
274  int64_t m_offset = 0;
275 
276 public:
277  FuzzedFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
278  {
279  }
280 
281  FILE* open();
282 
283  static ssize_t read(void* cookie, char* buf, size_t size);
284 
285  static ssize_t write(void* cookie, const char* buf, size_t size);
286 
287  static int seek(void* cookie, int64_t* offset, int whence);
288 
289  static int close(void* cookie);
290 };
291 
292 [[nodiscard]] inline FuzzedFileProvider ConsumeFile(FuzzedDataProvider& fuzzed_data_provider) noexcept
293 {
294  return {fuzzed_data_provider};
295 }
296 
298 {
301 
302 public:
303  FuzzedAutoFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}, m_fuzzed_file_provider{fuzzed_data_provider}
304  {
305  }
306 
308  {
310  }
311 };
312 
313 [[nodiscard]] inline FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider& fuzzed_data_provider) noexcept
314 {
315  return {fuzzed_data_provider};
316 }
317 
318 #define WRITE_TO_STREAM_CASE(type, consume) \
319  [&] { \
320  type o = consume; \
321  stream << o; \
322  }
323 template <typename Stream>
324 void WriteToStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
325 {
326  while (fuzzed_data_provider.ConsumeBool()) {
327  try {
328  CallOneOf(
329  fuzzed_data_provider,
330  WRITE_TO_STREAM_CASE(bool, fuzzed_data_provider.ConsumeBool()),
331  WRITE_TO_STREAM_CASE(char, fuzzed_data_provider.ConsumeIntegral<char>()),
332  WRITE_TO_STREAM_CASE(int8_t, fuzzed_data_provider.ConsumeIntegral<int8_t>()),
333  WRITE_TO_STREAM_CASE(uint8_t, fuzzed_data_provider.ConsumeIntegral<uint8_t>()),
334  WRITE_TO_STREAM_CASE(int16_t, fuzzed_data_provider.ConsumeIntegral<int16_t>()),
335  WRITE_TO_STREAM_CASE(uint16_t, fuzzed_data_provider.ConsumeIntegral<uint16_t>()),
336  WRITE_TO_STREAM_CASE(int32_t, fuzzed_data_provider.ConsumeIntegral<int32_t>()),
337  WRITE_TO_STREAM_CASE(uint32_t, fuzzed_data_provider.ConsumeIntegral<uint32_t>()),
338  WRITE_TO_STREAM_CASE(int64_t, fuzzed_data_provider.ConsumeIntegral<int64_t>()),
339  WRITE_TO_STREAM_CASE(uint64_t, fuzzed_data_provider.ConsumeIntegral<uint64_t>()),
340  WRITE_TO_STREAM_CASE(std::string, fuzzed_data_provider.ConsumeRandomLengthString(32)),
341  WRITE_TO_STREAM_CASE(std::vector<char>, ConsumeRandomLengthIntegralVector<char>(fuzzed_data_provider)));
342  } catch (const std::ios_base::failure&) {
343  break;
344  }
345  }
346 }
347 
348 #define READ_FROM_STREAM_CASE(type) \
349  [&] { \
350  type o; \
351  stream >> o; \
352  }
353 template <typename Stream>
354 void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
355 {
356  while (fuzzed_data_provider.ConsumeBool()) {
357  try {
358  CallOneOf(
359  fuzzed_data_provider,
360  READ_FROM_STREAM_CASE(bool),
361  READ_FROM_STREAM_CASE(char),
362  READ_FROM_STREAM_CASE(int8_t),
363  READ_FROM_STREAM_CASE(uint8_t),
364  READ_FROM_STREAM_CASE(int16_t),
365  READ_FROM_STREAM_CASE(uint16_t),
366  READ_FROM_STREAM_CASE(int32_t),
367  READ_FROM_STREAM_CASE(uint32_t),
368  READ_FROM_STREAM_CASE(int64_t),
369  READ_FROM_STREAM_CASE(uint64_t),
370  READ_FROM_STREAM_CASE(std::string),
371  READ_FROM_STREAM_CASE(std::vector<char>));
372  } catch (const std::ios_base::failure&) {
373  break;
374  }
375  }
376 }
377 
378 class FuzzedSock : public Sock
379 {
381 
387  mutable std::optional<uint8_t> m_peek_data;
388 
389 public:
390  explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);
391 
392  ~FuzzedSock() override;
393 
394  FuzzedSock& operator=(Sock&& other) override;
395 
396  void Reset() override;
397 
398  ssize_t Send(const void* data, size_t len, int flags) const override;
399 
400  ssize_t Recv(void* buf, size_t len, int flags) const override;
401 
402  int Connect(const sockaddr*, socklen_t) const override;
403 
404  std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
405 
406  int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
407 
408  bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
409 
410  bool IsConnected(std::string& errmsg) const override;
411 };
412 
413 [[nodiscard]] inline FuzzedSock ConsumeSock(FuzzedDataProvider& fuzzed_data_provider)
414 {
415  return FuzzedSock{fuzzed_data_provider};
416 }
417 
418 #endif // BITCOIN_TEST_FUZZ_UTIL_H
NodeId
int64_t NodeId
Definition: net.h:88
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
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:155
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:528
arith_uint256.h
merkleblock.h
ConsumeTime
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min=std::nullopt, const std::optional< int64_t > &max=std::nullopt) noexcept
Definition: util.cpp:281
assert
assert(!tx.IsCoinBase())
flags
int flags
Definition: bitcoin-tx.cpp:525
FuzzedSock::Reset
void Reset() override
Close if non-empty.
Definition: util.cpp:39
streams.h
ConsumeScript
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh=false) noexcept
Definition: util.cpp:340
transaction.h
FuzzedSock::Send
ssize_t Send(const void *data, size_t len, int flags) const override
send(2) wrapper.
Definition: util.cpp:44
FuzzedSock
Definition: util.h:378
CNetAddr
Network address.
Definition: netaddress.h:118
uint256.h
arith_uint256
256-bit unsigned big integer.
Definition: arith_uint256.h:250
FuzzedFileProvider::seek
static int seek(void *cookie, int64_t *offset, int whence)
Definition: util.cpp:575
chainparamsbase.h
ConsumeSock
FuzzedSock ConsumeSock(FuzzedDataProvider &fuzzed_data_provider)
Definition: util.h:413
CNode
Information about a peer.
Definition: net.h:394
FuzzedFileProvider::write
static ssize_t write(void *cookie, const char *buf, size_t size)
Definition: util.cpp:563
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
FuzzedSock::m_fuzzed_data_provider
FuzzedDataProvider & m_fuzzed_data_provider
Definition: util.h:380
opcodetype
opcodetype
Script opcodes.
Definition: script.h:66
FuzzedAutoFileProvider::m_fuzzed_file_provider
FuzzedFileProvider m_fuzzed_file_provider
Definition: util.h:300
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
ConsumeSequence
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:406
FuzzedSock::~FuzzedSock
~FuzzedSock() override
Definition: util.cpp:24
version.h
ConsumeOpcodeType
opcodetype ConsumeOpcodeType(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:124
WRITE_TO_STREAM_CASE
#define WRITE_TO_STREAM_CASE(type, consume)
Definition: util.h:318
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
txmempool.h
Sock
RAII helper class that manages a socket.
Definition: sock.h:26
CScriptWitness
Definition: script.h:557
ConnmanTestMsg
Definition: net.h:19
ConsumeNetAddr
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:481
ConsumeRandomLengthBitVector
std::vector< bool > ConsumeRandomLengthBitVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:71
SetFuzzedErrNo
void SetFuzzedErrNo(FuzzedDataProvider &fuzzed_data_provider, const std::array< T, size > &errnos)
Sets errno to a value selected from the given std::array errnos.
Definition: util.h:202
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:544
CScriptNum
Definition: script.h:219
ConnectionType::INBOUND
@ INBOUND
Inbound connections are those initiated by a peer.
ConsumeSubNet
CSubNet ConsumeSubNet(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:233
MultiplicationOverflow
bool MultiplicationOverflow(const T i, const T j) noexcept
Definition: util.h:174
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
ConsumeTxMemPoolEntry
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx) noexcept
Definition: util.cpp:455
ConsumeFile
FuzzedFileProvider ConsumeFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:292
INVALID_SOCKET
#define INVALID_SOCKET
Definition: compat.h:53
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:42
net.h
compat.h
ConsumeDataStream
CDataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:76
netaddress.h
FuzzedSock::GetSockOpt
int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
getsockopt(2) wrapper.
Definition: util.cpp:177
consensus.h
ALL_SERVICE_FLAGS
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
Definition: net.h:48
FuzzedDataProvider.h
ConsumeArithUInt256
arith_uint256 ConsumeArithUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:164
FuzzedAutoFileProvider::m_fuzzed_data_provider
FuzzedDataProvider & m_fuzzed_data_provider
Definition: util.h:299
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:63
standard.h
FuzzedAutoFileProvider
Definition: util.h:297
FuzzedFileProvider::FuzzedFileProvider
FuzzedFileProvider(FuzzedDataProvider &fuzzed_data_provider)
Definition: util.h:277
CSubNet
Definition: netaddress.h:475
READ_FROM_STREAM_CASE
#define READ_FROM_STREAM_CASE(type)
Definition: util.h:348
ConsumeUInt160
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:146
ConsumeNode
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: util.h:249
ALL_CONNECTION_TYPES
constexpr ConnectionType ALL_CONNECTION_TYPES[]
Definition: net.h:70
PeerManager
Definition: net_processing.h:37
FuzzedFileProvider::open
FILE * open()
Definition: util.cpp:503
uint256
256-bit opaque blob.
Definition: uint256.h:124
BytesToBits
std::vector< bool > BytesToBits(const std::vector< unsigned char > &bytes)
Definition: merkleblock.cpp:21
ConsumeMoney
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max=std::nullopt) noexcept
Definition: util.cpp:276
ConsumeService
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:238
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
script.h
coins.h
attributes.h
FuzzedSock::IsConnected
bool IsConnected(std::string &errmsg) const override
Check if still connected.
Definition: util.cpp:213
FillNode
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, PeerManager &peerman, CNode &node) noexcept
Definition: util.cpp:222
FuzzedFileProvider::read
static ssize_t read(void *cookie, char *buf, size_t size)
Definition: util.cpp:544
uint160
160-bit opaque blob.
Definition: uint256.h:113
FuzzedFileProvider::close
static int close(void *cookie)
Definition: util.cpp:603
CAddress
A CService with information about it as peer.
Definition: protocol.h:358
ConsumeTransaction
CMutableTransaction ConsumeTransaction(FuzzedDataProvider &fuzzed_data_provider, const std::optional< std::vector< uint256 >> &prevout_txids, const int max_num_in=10, const int max_num_out=10) noexcept
Definition: util.cpp:289
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
FuzzedSock::m_peek_data
std::optional< uint8_t > m_peek_data
Data to return when MSG_PEEK is used as a Recv() flag.
Definition: util.h:387
ConsumeAutoFile
FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:313
FuzzedAutoFileProvider::FuzzedAutoFileProvider
FuzzedAutoFileProvider(FuzzedDataProvider &fuzzed_data_provider)
Definition: util.h:303
ContainsSpentInput
bool ContainsSpentInput(const CTransaction &tx, const CCoinsViewCache &inputs) noexcept
Definition: util.cpp:470
fuzz.h
UintToArith256
arith_uint256 UintToArith256(const uint256 &a)
Definition: arith_uint256.cpp:253
CTxMemPoolEntry
Definition: txmempool.h:85
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedSock::Accept
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
Definition: util.cpp:163
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
Sock::Event
uint8_t Event
Definition: sock.h:118
ReadFromStream
void ReadFromStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
Definition: util.h:354
ByteUnit::T
@ T
ConsumeNodeAsUniquePtr
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: util.h:267
ConsumeScriptWitness
CScriptWitness ConsumeScriptWitness(FuzzedDataProvider &fuzzed_data_provider, const size_t max_stack_elem_size=32) noexcept
Definition: util.cpp:330
WriteToStream
void WriteToStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
Definition: util.h:324
FuzzedSock::FuzzedSock
FuzzedSock(FuzzedDataProvider &fuzzed_data_provider)
Definition: util.cpp:18
node
Definition: init.h:22
serialize.h
FuzzedSock::Recv
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
Definition: util.cpp:76
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
FuzzedSock::Wait
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
Definition: util.cpp:196
FuzzedFileProvider::m_offset
int64_t m_offset
Definition: util.h:274
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
ConsumeTxDestination
CTxDestination ConsumeTxDestination(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:417
ConsumeDeserializable
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:103
netbase.h
ConsumeFixedLengthByteVector
std::vector< uint8_t > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
Definition: util.h:221
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
FuzzedFileProvider
Definition: util.h:271
ConsumeWeakEnum
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:117
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
FuzzedSock::operator=
FuzzedSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
Definition: util.cpp:33
FuzzedSock::Connect
int Connect(const sockaddr *, socklen_t) const override
connect(2) wrapper.
Definition: util.cpp:141
ConsumeScriptNum
CScriptNum ConsumeScriptNum(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:141
FuzzedFileProvider::m_fuzzed_data_provider
FuzzedDataProvider & m_fuzzed_data_provider
Definition: util.h:273
amount.h
ConnectionType
ConnectionType
Different types of connections to a peer.
Definition: net.h:121
FuzzedAutoFileProvider::open
CAutoFile open()
Definition: util.h:307
ConsumeRandomLengthIntegralVector
std::vector< T > ConsumeRandomLengthIntegralVector(FuzzedDataProvider &fuzzed_data_provider, const size_t max_vector_size=16) noexcept
Definition: util.h:92
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204
FuzzedDataProvider::ConsumeBytes
std::vector< T > ConsumeBytes(size_t num_bytes)
Definition: FuzzedDataProvider.h:108
ConsumeAddress
CAddress ConsumeAddress(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:243
PickValue
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:54
INIT_PROTO_VERSION
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
SOCKET
unsigned int SOCKET
Definition: compat.h:41
MAX_OPCODE
static const unsigned int MAX_OPCODE
Definition: script.h:209