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