Skip to content

Commit 893aa20

Browse files
tests: Add fuzzing harness for CheckBlock(...) and other CBlock related functions
1 parent ec8dcb0 commit 893aa20

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ FUZZ_TARGETS = \
99
test/fuzz/addrman_deserialize \
1010
test/fuzz/banentry_deserialize \
1111
test/fuzz/bech32 \
12+
test/fuzz/block \
1213
test/fuzz/block_deserialize \
1314
test/fuzz/block_file_info_deserialize \
1415
test/fuzz/block_filter_deserialize \
@@ -229,6 +230,12 @@ test_test_bitcoin_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS)
229230
endif
230231

231232
if ENABLE_FUZZ
233+
test_fuzz_block_SOURCES = $(FUZZ_SUITE) test/fuzz/block.cpp
234+
test_fuzz_block_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
235+
test_fuzz_block_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
236+
test_fuzz_block_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
237+
test_fuzz_block_LDADD = $(FUZZ_SUITE_LD_COMMON)
238+
232239
test_fuzz_block_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
233240
test_fuzz_block_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DBLOCK_DESERIALIZE=1
234241
test_fuzz_block_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

src/test/fuzz/block.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2019 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 <chainparams.h>
6+
#include <consensus/merkle.h>
7+
#include <consensus/validation.h>
8+
#include <core_io.h>
9+
#include <core_memusage.h>
10+
#include <pubkey.h>
11+
#include <primitives/block.h>
12+
#include <streams.h>
13+
#include <test/fuzz/fuzz.h>
14+
#include <validation.h>
15+
#include <version.h>
16+
17+
#include <cassert>
18+
#include <string>
19+
20+
void initialize()
21+
{
22+
const static auto verify_handle = MakeUnique<ECCVerifyHandle>();
23+
SelectParams(CBaseChainParams::REGTEST);
24+
}
25+
26+
void test_one_input(const std::vector<uint8_t>& buffer)
27+
{
28+
CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
29+
CBlock block;
30+
try {
31+
int nVersion;
32+
ds >> nVersion;
33+
ds.SetVersion(nVersion);
34+
ds >> block;
35+
} catch (const std::ios_base::failure&) {
36+
return;
37+
}
38+
const Consensus::Params& consensus_params = Params().GetConsensus();
39+
BlockValidationState validation_state_pow_and_merkle;
40+
const bool valid_incl_pow_and_merkle = CheckBlock(block, validation_state_pow_and_merkle, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ true);
41+
BlockValidationState validation_state_pow;
42+
const bool valid_incl_pow = CheckBlock(block, validation_state_pow, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ false);
43+
BlockValidationState validation_state_merkle;
44+
const bool valid_incl_merkle = CheckBlock(block, validation_state_merkle, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ true);
45+
BlockValidationState validation_state_none;
46+
const bool valid_incl_none = CheckBlock(block, validation_state_none, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ false);
47+
if (valid_incl_pow_and_merkle) {
48+
assert(valid_incl_pow && valid_incl_merkle && valid_incl_none);
49+
} else if (valid_incl_merkle || valid_incl_pow) {
50+
assert(valid_incl_none);
51+
}
52+
(void)block.GetHash();
53+
(void)block.ToString();
54+
(void)BlockMerkleRoot(block);
55+
if (!block.vtx.empty()) {
56+
// TODO: Avoid array index out of bounds error in BlockWitnessMerkleRoot
57+
// when block.vtx.empty().
58+
(void)BlockWitnessMerkleRoot(block);
59+
}
60+
(void)GetBlockWeight(block);
61+
(void)GetWitnessCommitmentIndex(block);
62+
(void)RecursiveDynamicUsage(block);
63+
}

0 commit comments

Comments
 (0)