1 // Copyright (c) 2009-2016 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.
5 #if defined(HAVE_CONFIG_H)
6 #include "config/bitcoin-config.h"
9 #include "consensus/merkle.h"
10 #include "primitives/block.h"
11 #include "script/script.h"
15 #include "compressor.h"
22 #include "blockencodings.h"
32 CTRANSACTION_DESERIALIZE
,
33 CBLOCKLOCATOR_DESERIALIZE
,
36 CBLOCKHEADER_DESERIALIZE
,
37 CBANENTRY_DESERIALIZE
,
39 CBLOCKUNDO_DESERIALIZE
,
43 CMESSAGEHEADER_DESERIALIZE
,
46 CBLOOMFILTER_DESERIALIZE
,
47 CDISKBLOCKINDEX_DESERIALIZE
,
48 CTXOUTCOMPRESSOR_DESERIALIZE
,
49 BLOCKTRANSACTIONS_DESERIALIZE
,
50 BLOCKTRANSACTIONSREQUEST_DESERIALIZE
,
54 bool read_stdin(std::vector
<uint8_t> &data
) {
57 while((length
= read(STDIN_FILENO
, buffer
, 1024)) > 0) {
58 data
.insert(data
.end(), buffer
, buffer
+length
);
60 if (data
.size() > (1<<20)) return false;
65 int test_one_input(std::vector
<uint8_t> buffer
) {
66 if (buffer
.size() < sizeof(uint32_t)) return 0;
68 uint32_t test_id
= 0xffffffff;
69 memcpy(&test_id
, buffer
.data(), sizeof(uint32_t));
70 buffer
.erase(buffer
.begin(), buffer
.begin() + sizeof(uint32_t));
72 if (test_id
>= TEST_ID_END
) return 0;
74 CDataStream
ds(buffer
, SER_NETWORK
, INIT_PROTO_VERSION
);
78 ds
.SetVersion(nVersion
);
79 } catch (const std::ios_base::failure
& e
) {
84 case CBLOCK_DESERIALIZE
:
90 } catch (const std::ios_base::failure
& e
) {return 0;}
93 case CTRANSACTION_DESERIALIZE
:
97 CTransaction
tx(deserialize
, ds
);
98 } catch (const std::ios_base::failure
& e
) {return 0;}
101 case CBLOCKLOCATOR_DESERIALIZE
:
107 } catch (const std::ios_base::failure
& e
) {return 0;}
110 case CBLOCKMERKLEROOT
:
117 BlockMerkleRoot(block
, &mutated
);
118 } catch (const std::ios_base::failure
& e
) {return 0;}
121 case CADDRMAN_DESERIALIZE
:
127 } catch (const std::ios_base::failure
& e
) {return 0;}
130 case CBLOCKHEADER_DESERIALIZE
:
136 } catch (const std::ios_base::failure
& e
) {return 0;}
139 case CBANENTRY_DESERIALIZE
:
145 } catch (const std::ios_base::failure
& e
) {return 0;}
148 case CTXUNDO_DESERIALIZE
:
154 } catch (const std::ios_base::failure
& e
) {return 0;}
157 case CBLOCKUNDO_DESERIALIZE
:
163 } catch (const std::ios_base::failure
& e
) {return 0;}
166 case CCOINS_DESERIALIZE
:
172 } catch (const std::ios_base::failure
& e
) {return 0;}
175 case CNETADDR_DESERIALIZE
:
181 } catch (const std::ios_base::failure
& e
) {return 0;}
184 case CSERVICE_DESERIALIZE
:
190 } catch (const std::ios_base::failure
& e
) {return 0;}
193 case CMESSAGEHEADER_DESERIALIZE
:
195 CMessageHeader::MessageStartChars pchMessageStart
= {0x00, 0x00, 0x00, 0x00};
198 CMessageHeader
mh(pchMessageStart
);
200 if (!mh
.IsValid(pchMessageStart
)) {return 0;}
201 } catch (const std::ios_base::failure
& e
) {return 0;}
204 case CADDRESS_DESERIALIZE
:
210 } catch (const std::ios_base::failure
& e
) {return 0;}
213 case CINV_DESERIALIZE
:
219 } catch (const std::ios_base::failure
& e
) {return 0;}
222 case CBLOOMFILTER_DESERIALIZE
:
228 } catch (const std::ios_base::failure
& e
) {return 0;}
231 case CDISKBLOCKINDEX_DESERIALIZE
:
237 } catch (const std::ios_base::failure
& e
) {return 0;}
240 case CTXOUTCOMPRESSOR_DESERIALIZE
:
243 CTxOutCompressor
toc(to
);
247 } catch (const std::ios_base::failure
& e
) {return 0;}
251 case BLOCKTRANSACTIONS_DESERIALIZE
:
255 BlockTransactions bt
;
257 } catch (const std::ios_base::failure
& e
) {return 0;}
261 case BLOCKTRANSACTIONSREQUEST_DESERIALIZE
:
265 BlockTransactionsRequest btr
;
267 } catch (const std::ios_base::failure
& e
) {return 0;}
277 static std::unique_ptr
<ECCVerifyHandle
> globalVerifyHandle
;
279 globalVerifyHandle
= std::unique_ptr
<ECCVerifyHandle
>(new ECCVerifyHandle());
282 // This function is used by libFuzzer
283 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
) {
284 test_one_input(std::vector
<uint8_t>(data
, data
+ size
));
288 // This function is used by libFuzzer
289 extern "C" int LLVMFuzzerInitialize(int *argc
, char ***argv
) {
294 // Disabled under WIN32 due to clash with Cygwin's WinMain.
296 // Declare main(...) "weak" to allow for libFuzzer linking. libFuzzer provides
297 // the main(...) function.
298 __attribute__((weak
))
300 int main(int argc
, char **argv
)
304 // Enable AFL deferred forkserver mode. Requires compilation using
305 // afl-clang-fast++. See fuzzing.md for details.
310 // Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
311 // See fuzzing.md for details.
313 while (__AFL_LOOP(1000)) {
314 std::vector
<uint8_t> buffer
;
315 if (!read_stdin(buffer
)) {
318 ret
= test_one_input(buffer
);
322 std::vector
<uint8_t> buffer
;
323 if (!read_stdin(buffer
)) {
326 return test_one_input(buffer
);