diff --git a/src/test/test_bitcoin_fuzzy.cpp b/src/test/test_bitcoin_fuzzy.cpp index 88c082ff66..6588ac38ec 100644 --- a/src/test/test_bitcoin_fuzzy.cpp +++ b/src/test/test_bitcoin_fuzzy.cpp @@ -54,25 +54,27 @@ enum TEST_ID { TEST_ID_END }; -static bool read_stdin(std::vector &data) { +static bool read_stdin(std::vector& data) +{ uint8_t buffer[1024]; - ssize_t length=0; - while((length = read(STDIN_FILENO, buffer, 1024)) > 0) { - data.insert(data.end(), buffer, buffer+length); + ssize_t length = 0; + while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) { + data.insert(data.end(), buffer, buffer + length); - if (data.size() > (1<<20)) return false; + if (data.size() > (1 << 20)) return false; } - return length==0; + return length == 0; } -static int test_one_input(std::vector buffer) { - if (buffer.size() < sizeof(uint32_t)) return 0; +void test_one_input(std::vector buffer) +{ + if (buffer.size() < sizeof(uint32_t)) return; uint32_t test_id = 0xffffffff; memcpy(&test_id, buffer.data(), sizeof(uint32_t)); buffer.erase(buffer.begin(), buffer.begin() + sizeof(uint32_t)); - if (test_id >= TEST_ID_END) return 0; + if (test_id >= TEST_ID_END) return; CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION); try { @@ -80,7 +82,7 @@ static int test_one_input(std::vector buffer) { ds >> nVersion; ds.SetVersion(nVersion); } catch (const std::ios_base::failure& e) { - return 0; + return; } switch(test_id) { @@ -90,7 +92,7 @@ static int test_one_input(std::vector buffer) { { CBlock block; ds >> block; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CTRANSACTION_DESERIALIZE: @@ -98,7 +100,7 @@ static int test_one_input(std::vector buffer) { try { CTransaction tx(deserialize, ds); - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CBLOCKLOCATOR_DESERIALIZE: @@ -107,7 +109,7 @@ static int test_one_input(std::vector buffer) { { CBlockLocator bl; ds >> bl; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CBLOCKMERKLEROOT: @@ -118,7 +120,7 @@ static int test_one_input(std::vector buffer) { ds >> block; bool mutated; BlockMerkleRoot(block, &mutated); - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CADDRMAN_DESERIALIZE: @@ -127,7 +129,7 @@ static int test_one_input(std::vector buffer) { { CAddrMan am; ds >> am; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CBLOCKHEADER_DESERIALIZE: @@ -136,7 +138,7 @@ static int test_one_input(std::vector buffer) { { CBlockHeader bh; ds >> bh; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CBANENTRY_DESERIALIZE: @@ -145,7 +147,7 @@ static int test_one_input(std::vector buffer) { { CBanEntry be; ds >> be; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CTXUNDO_DESERIALIZE: @@ -154,7 +156,7 @@ static int test_one_input(std::vector buffer) { { CTxUndo tu; ds >> tu; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CBLOCKUNDO_DESERIALIZE: @@ -163,7 +165,7 @@ static int test_one_input(std::vector buffer) { { CBlockUndo bu; ds >> bu; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CCOINS_DESERIALIZE: @@ -172,7 +174,7 @@ static int test_one_input(std::vector buffer) { { Coin coin; ds >> coin; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CNETADDR_DESERIALIZE: @@ -181,7 +183,7 @@ static int test_one_input(std::vector buffer) { { CNetAddr na; ds >> na; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CSERVICE_DESERIALIZE: @@ -190,7 +192,7 @@ static int test_one_input(std::vector buffer) { { CService s; ds >> s; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CMESSAGEHEADER_DESERIALIZE: @@ -200,8 +202,8 @@ static int test_one_input(std::vector buffer) { { CMessageHeader mh(pchMessageStart); ds >> mh; - if (!mh.IsValid(pchMessageStart)) {return 0;} - } catch (const std::ios_base::failure& e) {return 0;} + if (!mh.IsValid(pchMessageStart)) {return;} + } catch (const std::ios_base::failure& e) {return;} break; } case CADDRESS_DESERIALIZE: @@ -210,7 +212,7 @@ static int test_one_input(std::vector buffer) { { CAddress a; ds >> a; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CINV_DESERIALIZE: @@ -219,7 +221,7 @@ static int test_one_input(std::vector buffer) { { CInv i; ds >> i; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CBLOOMFILTER_DESERIALIZE: @@ -228,7 +230,7 @@ static int test_one_input(std::vector buffer) { { CBloomFilter bf; ds >> bf; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CDISKBLOCKINDEX_DESERIALIZE: @@ -237,7 +239,7 @@ static int test_one_input(std::vector buffer) { { CDiskBlockIndex dbi; ds >> dbi; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } case CTXOUTCOMPRESSOR_DESERIALIZE: @@ -247,7 +249,7 @@ static int test_one_input(std::vector buffer) { try { ds >> toc; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } @@ -257,7 +259,7 @@ static int test_one_input(std::vector buffer) { { BlockTransactions bt; ds >> bt; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } @@ -267,29 +269,31 @@ static int test_one_input(std::vector buffer) { { BlockTransactionsRequest btr; ds >> btr; - } catch (const std::ios_base::failure& e) {return 0;} + } catch (const std::ios_base::failure& e) {return;} break; } default: - return 0; + return; } - return 0; + return; } -static std::unique_ptr globalVerifyHandle; -void initialize() { - globalVerifyHandle = MakeUnique(); +void initialize() +{ + const static auto verify_handle = MakeUnique(); } // This function is used by libFuzzer -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ test_one_input(std::vector(data, data + size)); return 0; } // This function is used by libFuzzer -extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { +extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) +{ initialize(); return 0; } @@ -312,20 +316,18 @@ int main(int argc, char **argv) #ifdef __AFL_LOOP // Enable AFL persistent mode. Requires compilation using afl-clang-fast++. // See fuzzing.md for details. - int ret = 0; while (__AFL_LOOP(1000)) { std::vector buffer; if (!read_stdin(buffer)) { continue; } - ret = test_one_input(buffer); + test_one_input(buffer); } - return ret; #else std::vector buffer; if (!read_stdin(buffer)) { return 0; } - return test_one_input(buffer); + test_one_input(buffer); #endif }