Bitcoin Core  24.99.0
P2P Digital Currency
deserialize.cpp
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 #include <addrdb.h>
6 #include <addrman.h>
7 #include <addrman_impl.h>
8 #include <blockencodings.h>
9 #include <blockfilter.h>
10 #include <chain.h>
11 #include <coins.h>
12 #include <compressor.h>
13 #include <consensus/merkle.h>
14 #include <key.h>
15 #include <merkleblock.h>
16 #include <net.h>
17 #include <netbase.h>
18 #include <netgroup.h>
19 #include <node/utxo_snapshot.h>
20 #include <primitives/block.h>
21 #include <protocol.h>
22 #include <psbt.h>
23 #include <pubkey.h>
24 #include <script/keyorigin.h>
25 #include <streams.h>
26 #include <test/util/setup_common.h>
27 #include <undo.h>
28 #include <util/system.h>
29 #include <version.h>
30 
31 #include <exception>
32 #include <optional>
33 #include <stdexcept>
34 #include <stdint.h>
35 #include <unistd.h>
36 
37 #include <test/fuzz/fuzz.h>
38 
40 
41 namespace {
42 const BasicTestingSetup* g_setup;
43 } // namespace
44 
46 {
47  static const auto testing_setup = MakeNoLogFileContext<>();
48  g_setup = testing_setup.get();
49 }
50 
51 #define FUZZ_TARGET_DESERIALIZE(name, code) \
52  FUZZ_TARGET_INIT(name, initialize_deserialize) \
53  { \
54  try { \
55  code \
56  } catch (const invalid_fuzzing_input_exception&) { \
57  } \
58  }
59 
60 namespace {
61 
62 struct invalid_fuzzing_input_exception : public std::exception {
63 };
64 
65 template <typename T>
66 CDataStream Serialize(const T& obj, const int version = INIT_PROTO_VERSION, const int ser_type = SER_NETWORK)
67 {
68  CDataStream ds(ser_type, version);
69  ds << obj;
70  return ds;
71 }
72 
73 template <typename T>
74 T Deserialize(CDataStream ds)
75 {
76  T obj;
77  ds >> obj;
78  return obj;
79 }
80 
81 template <typename T>
82 void DeserializeFromFuzzingInput(FuzzBufferType buffer, T& obj, const std::optional<int> protocol_version = std::nullopt, const int ser_type = SER_NETWORK)
83 {
84  CDataStream ds(buffer, ser_type, INIT_PROTO_VERSION);
85  if (protocol_version) {
86  ds.SetVersion(*protocol_version);
87  } else {
88  try {
89  int version;
90  ds >> version;
91  ds.SetVersion(version);
92  } catch (const std::ios_base::failure&) {
93  throw invalid_fuzzing_input_exception();
94  }
95  }
96  try {
97  ds >> obj;
98  } catch (const std::ios_base::failure&) {
99  throw invalid_fuzzing_input_exception();
100  }
101  assert(buffer.empty() || !Serialize(obj).empty());
102 }
103 
104 template <typename T>
105 void AssertEqualAfterSerializeDeserialize(const T& obj, const int version = INIT_PROTO_VERSION, const int ser_type = SER_NETWORK)
106 {
107  assert(Deserialize<T>(Serialize(obj, version, ser_type)) == obj);
108 }
109 
110 } // namespace
111 
112 FUZZ_TARGET_DESERIALIZE(block_filter_deserialize, {
113  BlockFilter block_filter;
114  DeserializeFromFuzzingInput(buffer, block_filter);
115 })
116 FUZZ_TARGET_DESERIALIZE(addr_info_deserialize, {
117  AddrInfo addr_info;
118  DeserializeFromFuzzingInput(buffer, addr_info);
119 })
120 FUZZ_TARGET_DESERIALIZE(block_file_info_deserialize, {
121  CBlockFileInfo block_file_info;
122  DeserializeFromFuzzingInput(buffer, block_file_info);
123 })
124 FUZZ_TARGET_DESERIALIZE(block_header_and_short_txids_deserialize, {
125  CBlockHeaderAndShortTxIDs block_header_and_short_txids;
126  DeserializeFromFuzzingInput(buffer, block_header_and_short_txids);
127 })
128 FUZZ_TARGET_DESERIALIZE(fee_rate_deserialize, {
129  CFeeRate fee_rate;
130  DeserializeFromFuzzingInput(buffer, fee_rate);
132 })
133 FUZZ_TARGET_DESERIALIZE(merkle_block_deserialize, {
134  CMerkleBlock merkle_block;
135  DeserializeFromFuzzingInput(buffer, merkle_block);
136 })
137 FUZZ_TARGET_DESERIALIZE(out_point_deserialize, {
138  COutPoint out_point;
139  DeserializeFromFuzzingInput(buffer, out_point);
141 })
142 FUZZ_TARGET_DESERIALIZE(partial_merkle_tree_deserialize, {
143  CPartialMerkleTree partial_merkle_tree;
144  DeserializeFromFuzzingInput(buffer, partial_merkle_tree);
145 })
146 FUZZ_TARGET_DESERIALIZE(pub_key_deserialize, {
147  CPubKey pub_key;
148  DeserializeFromFuzzingInput(buffer, pub_key);
150 })
151 FUZZ_TARGET_DESERIALIZE(script_deserialize, {
152  CScript script;
154 })
155 FUZZ_TARGET_DESERIALIZE(tx_in_deserialize, {
156  CTxIn tx_in;
157  DeserializeFromFuzzingInput(buffer, tx_in);
159 })
160 FUZZ_TARGET_DESERIALIZE(flat_file_pos_deserialize, {
161  FlatFilePos flat_file_pos;
162  DeserializeFromFuzzingInput(buffer, flat_file_pos);
164 })
165 FUZZ_TARGET_DESERIALIZE(key_origin_info_deserialize, {
166  KeyOriginInfo key_origin_info;
167  DeserializeFromFuzzingInput(buffer, key_origin_info);
168  AssertEqualAfterSerializeDeserialize(key_origin_info);
169 })
170 FUZZ_TARGET_DESERIALIZE(partially_signed_transaction_deserialize, {
171  PartiallySignedTransaction partially_signed_transaction;
172  DeserializeFromFuzzingInput(buffer, partially_signed_transaction);
173 })
174 FUZZ_TARGET_DESERIALIZE(prefilled_transaction_deserialize, {
175  PrefilledTransaction prefilled_transaction;
176  DeserializeFromFuzzingInput(buffer, prefilled_transaction);
177 })
178 FUZZ_TARGET_DESERIALIZE(psbt_input_deserialize, {
179  PSBTInput psbt_input;
180  DeserializeFromFuzzingInput(buffer, psbt_input);
181 })
182 FUZZ_TARGET_DESERIALIZE(psbt_output_deserialize, {
183  PSBTOutput psbt_output;
184  DeserializeFromFuzzingInput(buffer, psbt_output);
185 })
186 FUZZ_TARGET_DESERIALIZE(block_deserialize, {
187  CBlock block;
189 })
190 FUZZ_TARGET_DESERIALIZE(blocklocator_deserialize, {
191  CBlockLocator bl;
192  DeserializeFromFuzzingInput(buffer, bl);
193 })
194 FUZZ_TARGET_DESERIALIZE(blockmerkleroot, {
195  CBlock block;
196  DeserializeFromFuzzingInput(buffer, block);
197  bool mutated;
199 })
200 FUZZ_TARGET_DESERIALIZE(addrman_deserialize, {
201  NetGroupManager netgroupman{std::vector<bool>()};
202  AddrMan am(netgroupman,
203  /*deterministic=*/false,
204  g_setup->m_node.args->GetIntArg("-checkaddrman", 0));
205  DeserializeFromFuzzingInput(buffer, am);
206 })
207 FUZZ_TARGET_DESERIALIZE(blockheader_deserialize, {
208  CBlockHeader bh;
210 })
211 FUZZ_TARGET_DESERIALIZE(txundo_deserialize, {
212  CTxUndo tu;
213  DeserializeFromFuzzingInput(buffer, tu);
214 })
215 FUZZ_TARGET_DESERIALIZE(blockundo_deserialize, {
216  CBlockUndo bu;
218 })
219 FUZZ_TARGET_DESERIALIZE(coins_deserialize, {
220  Coin coin;
221  DeserializeFromFuzzingInput(buffer, coin);
222 })
223 FUZZ_TARGET_DESERIALIZE(netaddr_deserialize, {
224  CNetAddr na;
226  if (na.IsAddrV1Compatible()) {
228  }
230 })
231 FUZZ_TARGET_DESERIALIZE(service_deserialize, {
232  CService s;
233  DeserializeFromFuzzingInput(buffer, s);
234  if (s.IsAddrV1Compatible()) {
236  }
238  CService s1;
242  CService s2;
245 })
246 FUZZ_TARGET_DESERIALIZE(messageheader_deserialize, {
247  CMessageHeader mh;
249  (void)mh.IsCommandValid();
250 })
251 FUZZ_TARGET_DESERIALIZE(address_deserialize_v1_notime, {
252  CAddress a;
254  // A CAddress without nTime (as is expected under INIT_PROTO_VERSION) will roundtrip
255  // in all 5 formats (with/without nTime, v1/v2, network/disk)
261 })
262 FUZZ_TARGET_DESERIALIZE(address_deserialize_v1_withtime, {
263  CAddress a;
265  // A CAddress in V1 mode will roundtrip in all 4 formats that have nTime.
270 })
271 FUZZ_TARGET_DESERIALIZE(address_deserialize_v2, {
272  CAddress a;
274  // A CAddress in V2 mode will roundtrip in both V2 formats, and also in the V1 formats
275  // with time if it's V1 compatible.
276  if (a.IsAddrV1Compatible()) {
279  }
282 })
283 FUZZ_TARGET_DESERIALIZE(inv_deserialize, {
284  CInv i;
286 })
287 FUZZ_TARGET_DESERIALIZE(bloomfilter_deserialize, {
288  CBloomFilter bf;
289  DeserializeFromFuzzingInput(buffer, bf);
290 })
291 FUZZ_TARGET_DESERIALIZE(diskblockindex_deserialize, {
292  CDiskBlockIndex dbi;
294 })
295 FUZZ_TARGET_DESERIALIZE(txoutcompressor_deserialize, {
296  CTxOut to;
297  auto toc = Using<TxOutCompression>(to);
298  DeserializeFromFuzzingInput(buffer, toc);
299 })
300 FUZZ_TARGET_DESERIALIZE(blocktransactions_deserialize, {
303 })
304 FUZZ_TARGET_DESERIALIZE(blocktransactionsrequest_deserialize, {
306  DeserializeFromFuzzingInput(buffer, btr);
307 })
308 FUZZ_TARGET_DESERIALIZE(snapshotmetadata_deserialize, {
309  SnapshotMetadata snapshot_metadata;
310  DeserializeFromFuzzingInput(buffer, snapshot_metadata);
311 })
312 FUZZ_TARGET_DESERIALIZE(uint160_deserialize, {
313  uint160 u160;
314  DeserializeFromFuzzingInput(buffer, u160);
316 })
317 FUZZ_TARGET_DESERIALIZE(uint256_deserialize, {
318  uint256 u256;
321 })
322 // Classes intentionally not covered in this file since their deserialization code is
323 // fuzzed elsewhere:
324 // * Deserialization of CTxOut is fuzzed in test/fuzz/tx_out.cpp
325 // * Deserialization of CMutableTransaction is fuzzed in src/test/fuzz/transaction.cpp
Extended statistics about a CAddress.
Definition: addrman_impl.h:40
Stochastic address manager.
Definition: addrman.h:87
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:112
A CService with information about it as peer.
Definition: protocol.h:361
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
Definition: block.h:69
Undo information for a CBlock.
Definition: undo.h:64
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:45
void SetVersion(int n)
Definition: streams.h:362
Used to marshal pointers into hashes for db storage.
Definition: chain.h:382
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
inv message data
Definition: protocol.h:478
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:125
Message header.
Definition: protocol.h:27
bool IsCommandValid() const
Definition: protocol.cpp:109
Network address.
Definition: netaddress.h:120
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Definition: netaddress.cpp:499
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:55
An encapsulated public key.
Definition: pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:520
An input of a transaction.
Definition: transaction.h:75
An output of a transaction.
Definition: transaction.h:158
Undo information for a CTransaction.
Definition: undo.h:54
A UTXO entry.
Definition: coins.h:31
Netgroup manager.
Definition: netgroup.h:16
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:97
constexpr bool empty() const noexcept
Definition: span.h:188
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:25
160-bit opaque blob.
Definition: uint256.h:94
256-bit opaque blob.
Definition: uint256.h:105
AssertEqualAfterSerializeDeserialize(flat_file_pos)
BlockMerkleRoot(block, &mutated)
bool mutated
DeserializeFromFuzzingInput(buffer, addr_info)
#define FUZZ_TARGET_DESERIALIZE(name, code)
Definition: deserialize.cpp:51
void initialize_deserialize()
Definition: deserialize.cpp:45
#define T(expected, seed, data)
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition: netaddress.h:33
@ SER_DISK
Definition: serialize.h:132
@ SER_NETWORK
Definition: serialize.h:131
void Serialize(Stream &, char)=delete
Basic testing setup.
Definition: setup_common.h:79
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:121
A structure for PSBTs which contain per-input information.
Definition: psbt.h:192
A structure for PSBTs which contains per output information.
Definition: psbt.h:711
A version of CTransaction with the PSBT format.
Definition: psbt.h:947
assert(!tx.IsCoinBase())
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12