1 // Copyright (c) 2009-2018 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 <addrman.h>
6 #include <blockencodings.h>
7 #include <chain.h>
8 #include <coins.h>
9 #include <compressor.h>
10 #include <consensus/merkle.h>
11 #include <net.h>
12 #include <primitives/block.h>
13 #include <protocol.h>
14 #include <pubkey.h>
15 #include <script/script.h>
16 #include <streams.h>
17 #include <undo.h>
18 #include <version.h>
19 
20 #include <stdint.h>
21 #include <unistd.h>
22 
23 #include <algorithm>
24 #include <memory>
25 #include <vector>
26 
27 #include <test/fuzz/fuzz.h>
28 
test_one_input(std::vector<uint8_t> buffer)29 void test_one_input(std::vector<uint8_t> buffer)
30 {
31     CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
32     try {
33         int nVersion;
34         ds >> nVersion;
35         ds.SetVersion(nVersion);
36     } catch (const std::ios_base::failure& e) {
37         return;
38     }
39 
40 #if BLOCK_DESERIALIZE
41             try
42             {
43                 CBlock block;
44                 ds >> block;
45             } catch (const std::ios_base::failure& e) {return;}
46 #elif TRANSACTION_DESERIALIZE
47             try
48             {
49                 CTransaction tx(deserialize, ds);
50             } catch (const std::ios_base::failure& e) {return;}
51 #elif BLOCKLOCATOR_DESERIALIZE
52             try
53             {
54                 CBlockLocator bl;
55                 ds >> bl;
56             } catch (const std::ios_base::failure& e) {return;}
57 #elif BLOCKMERKLEROOT
58             try
59             {
60                 CBlock block;
61                 ds >> block;
62                 bool mutated;
63                 BlockMerkleRoot(block, &mutated);
64             } catch (const std::ios_base::failure& e) {return;}
65 #elif ADDRMAN_DESERIALIZE
66             try
67             {
68                 CAddrMan am;
69                 ds >> am;
70             } catch (const std::ios_base::failure& e) {return;}
71 #elif BLOCKHEADER_DESERIALIZE
72             try
73             {
74                 CBlockHeader bh;
75                 ds >> bh;
76             } catch (const std::ios_base::failure& e) {return;}
77 #elif BANENTRY_DESERIALIZE
78             try
79             {
80                 CBanEntry be;
81                 ds >> be;
82             } catch (const std::ios_base::failure& e) {return;}
83 #elif TXUNDO_DESERIALIZE
84             try
85             {
86                 CTxUndo tu;
87                 ds >> tu;
88             } catch (const std::ios_base::failure& e) {return;}
89 #elif BLOCKUNDO_DESERIALIZE
90             try
91             {
92                 CBlockUndo bu;
93                 ds >> bu;
94             } catch (const std::ios_base::failure& e) {return;}
95 #elif COINS_DESERIALIZE
96             try
97             {
98                 Coin coin;
99                 ds >> coin;
100             } catch (const std::ios_base::failure& e) {return;}
101 #elif NETADDR_DESERIALIZE
102             try
103             {
104                 CNetAddr na;
105                 ds >> na;
106             } catch (const std::ios_base::failure& e) {return;}
107 #elif SERVICE_DESERIALIZE
108             try
109             {
110                 CService s;
111                 ds >> s;
112             } catch (const std::ios_base::failure& e) {return;}
113 #elif MESSAGEHEADER_DESERIALIZE
114             CMessageHeader::MessageStartChars pchMessageStart = {0x00, 0x00, 0x00, 0x00};
115             try
116             {
117                 CMessageHeader mh(pchMessageStart);
118                 ds >> mh;
119                 if (!mh.IsValid(pchMessageStart)) {return;}
120             } catch (const std::ios_base::failure& e) {return;}
121 #elif ADDRESS_DESERIALIZE
122             try
123             {
124                 CAddress a;
125                 ds >> a;
126             } catch (const std::ios_base::failure& e) {return;}
127 #elif INV_DESERIALIZE
128             try
129             {
130                 CInv i;
131                 ds >> i;
132             } catch (const std::ios_base::failure& e) {return;}
133 #elif BLOOMFILTER_DESERIALIZE
134             try
135             {
136                 CBloomFilter bf;
137                 ds >> bf;
138             } catch (const std::ios_base::failure& e) {return;}
139 #elif DISKBLOCKINDEX_DESERIALIZE
140             try
141             {
142                 CDiskBlockIndex dbi;
143                 ds >> dbi;
144             } catch (const std::ios_base::failure& e) {return;}
145 #elif TXOUTCOMPRESSOR_DESERIALIZE
146             CTxOut to;
147             CTxOutCompressor toc(to);
148             try
149             {
150                 ds >> toc;
151             } catch (const std::ios_base::failure& e) {return;}
152 #elif BLOCKTRANSACTIONS_DESERIALIZE
153             try
154             {
155                 BlockTransactions bt;
156                 ds >> bt;
157             } catch (const std::ios_base::failure& e) {return;}
158 #elif BLOCKTRANSACTIONSREQUEST_DESERIALIZE
159             try
160             {
161                 BlockTransactionsRequest btr;
162                 ds >> btr;
163             } catch (const std::ios_base::failure& e) {return;}
164 #else
165 #error Need at least one fuzz target to compile
166 #endif
167 }
168