[test] fuzz: make test_one_input return void

The return value is always 0 and not used, so might as well return void
This commit is contained in:
MarcoFalke 2019-01-25 18:42:21 -05:00
parent d14ef5721f
commit fab4bed68a
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

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