Skip to content

Commit 052b689

Browse files
Merge pull request dashpay#5835 from kwvg/bps_jan_02
backport: merge bitcoin#20332, bitcoin#20908, bitcoin#21336, bitcoin#21443, bitcoin#21142, bitcoin#21512, bitcoin#20833, bitcoin#21522, bitcoin#21553, bitcoin#21970, bitcoin#24336, partial bitcoin#21606 (fuzzing harness backports: part 7)
2 parents cb09d35 + 24ae76c commit 052b689

22 files changed

+541
-64
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ src/dash-gui
1313
src/dash-node
1414
src/dash-tx
1515
src/dash-wallet
16-
src/test/fuzz/*
17-
!src/test/fuzz/*.*
16+
src/test/fuzz/fuzz
1817
src/test/test_dash
1918
src/qt/test/test_dash-qt
2019
src/qt/res/css/colors/*

configure.ac

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,12 +1960,12 @@ if test x$bitcoin_enable_qt != xno; then
19601960
fi
19611961
echo " with zmq = $use_zmq"
19621962
if test x$enable_fuzz == xno; then
1963-
echo " with test = $use_tests"
1963+
echo " with test = $use_tests"
19641964
else
1965-
echo " with test = not building test_dash because fuzzing is enabled"
1966-
echo " with fuzz = $enable_fuzz"
1965+
echo " with test = not building test_dash because fuzzing is enabled"
19671966
fi
1968-
echo " with bench = $use_bench"
1967+
echo " with fuzz binary = $enable_fuzz_binary"
1968+
echo " with bench = $use_bench"
19691969
echo " with upnp = $use_upnp"
19701970
echo " with natpmp = $use_natpmp"
19711971
echo " use asm = $use_asm"

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ test_fuzz_fuzz_SOURCES = \
327327
test/fuzz/transaction.cpp \
328328
test/fuzz/tx_in.cpp \
329329
test/fuzz/tx_out.cpp \
330+
test/fuzz/tx_pool.cpp \
330331
test/fuzz/validation_load_mempool.cpp \
331332
test/fuzz/versionbits.cpp
332333
endif # ENABLE_FUZZ_BINARY

src/Makefile.test_util.include

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ TEST_UTIL_H = \
1313
test/util/logging.h \
1414
test/util/mining.h \
1515
test/util/net.h \
16+
test/util/script.h \
1617
test/util/setup_common.h \
1718
test/util/str.h \
1819
test/util/transaction_utils.h \
1920
test/util/wallet.h \
21+
test/util/validation.h \
2022
test/util/xoroshiro128plusplus.h
2123

2224
libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
@@ -27,9 +29,11 @@ libtest_util_a_SOURCES = \
2729
test/util/logging.cpp \
2830
test/util/mining.cpp \
2931
test/util/net.cpp \
32+
test/util/script.cpp \
3033
test/util/setup_common.cpp \
3134
test/util/str.cpp \
3235
test/util/transaction_utils.cpp \
36+
test/util/validation.cpp \
3337
test/util/wallet.cpp \
3438
$(TEST_UTIL_H)
3539

src/test/fuzz/coins_view.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <chainparams.h>
77
#include <chainparamsbase.h>
88
#include <coins.h>
9+
#include <consensus/tx_check.h>
910
#include <consensus/tx_verify.h>
1011
#include <consensus/validation.h>
1112
#include <key.h>
@@ -229,11 +230,13 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
229230
// consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState&, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
230231
return;
231232
}
232-
try {
233-
(void)Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out);
234-
assert(MoneyRange(tx_fee_out));
235-
} catch (const std::runtime_error&) {
233+
TxValidationState dummy;
234+
if (!CheckTransaction(transaction, dummy)) {
235+
// It is not allowed to call CheckTxInputs if CheckTransaction failed
236+
return;
236237
}
238+
(void)Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out);
239+
assert(MoneyRange(tx_fee_out));
237240
},
238241
[&] {
239242
const CTransaction transaction{random_mutable_transaction};

src/test/fuzz/netbase_dns_lookup.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,22 @@
1212
#include <string>
1313
#include <vector>
1414

15-
namespace {
16-
FuzzedDataProvider* fuzzed_data_provider_ptr = nullptr;
17-
18-
std::vector<CNetAddr> fuzzed_dns_lookup_function(const std::string& name, bool allow_lookup)
19-
{
20-
std::vector<CNetAddr> resolved_addresses;
21-
while (fuzzed_data_provider_ptr->ConsumeBool()) {
22-
resolved_addresses.push_back(ConsumeNetAddr(*fuzzed_data_provider_ptr));
23-
}
24-
return resolved_addresses;
25-
}
26-
} // namespace
27-
2815
FUZZ_TARGET(netbase_dns_lookup)
2916
{
3017
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
31-
fuzzed_data_provider_ptr = &fuzzed_data_provider;
3218
const std::string name = fuzzed_data_provider.ConsumeRandomLengthString(512);
3319
const unsigned int max_results = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
3420
const bool allow_lookup = fuzzed_data_provider.ConsumeBool();
3521
const uint16_t default_port = fuzzed_data_provider.ConsumeIntegral<uint16_t>();
22+
23+
auto fuzzed_dns_lookup_function = [&](const std::string&, bool) {
24+
std::vector<CNetAddr> resolved_addresses;
25+
while (fuzzed_data_provider.ConsumeBool()) {
26+
resolved_addresses.push_back(ConsumeNetAddr(fuzzed_data_provider));
27+
}
28+
return resolved_addresses;
29+
};
30+
3631
{
3732
std::vector<CNetAddr> resolved_addresses;
3833
if (LookupHost(name, resolved_addresses, max_results, allow_lookup, fuzzed_dns_lookup_function)) {
@@ -73,5 +68,4 @@ FUZZ_TARGET(netbase_dns_lookup)
7368
assert(resolved_subnet.IsValid());
7469
}
7570
}
76-
fuzzed_data_provider_ptr = nullptr;
7771
}

src/test/fuzz/pow.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ FUZZ_TARGET_INIT(pow, initialize_pow)
3535
}
3636
CBlockIndex current_block{*block_header};
3737
{
38-
CBlockIndex* previous_block = !blocks.empty() ? &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)] : nullptr;
38+
CBlockIndex* previous_block = blocks.empty() ? nullptr : &PickValue(fuzzed_data_provider, blocks);
3939
const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0;
4040
if (fuzzed_data_provider.ConsumeBool()) {
4141
current_block.pprev = previous_block;
@@ -64,9 +64,9 @@ FUZZ_TARGET_INIT(pow, initialize_pow)
6464
}
6565
}
6666
{
67-
const CBlockIndex* to = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)];
68-
const CBlockIndex* from = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)];
69-
const CBlockIndex* tip = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)];
67+
const CBlockIndex* to = &PickValue(fuzzed_data_provider, blocks);
68+
const CBlockIndex* from = &PickValue(fuzzed_data_provider, blocks);
69+
const CBlockIndex* tip = &PickValue(fuzzed_data_provider, blocks);
7070
try {
7171
(void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params);
7272
} catch (const uint_error&) {

src/test/fuzz/process_message.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <test/util/mining.h>
1818
#include <test/util/net.h>
1919
#include <test/util/setup_common.h>
20+
#include <test/util/validation.h>
2021
#include <validationinterface.h>
2122
#include <version.h>
2223

@@ -73,7 +74,12 @@ void initialize_process_message()
7374
void fuzz_target(FuzzBufferType buffer, const std::string& LIMIT_TO_MESSAGE_TYPE)
7475
{
7576
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
76-
ConnmanTestMsg& connman = *(ConnmanTestMsg*)g_setup->m_node.connman.get();
77+
78+
ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
79+
TestChainState& chainstate = *static_cast<TestChainState*>(&g_setup->m_node.chainman->ActiveChainstate());
80+
SetMockTime(1610000000); // any time to successfully reset ibd
81+
chainstate.ResetIbd();
82+
7783
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
7884
if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
7985
return;
@@ -86,6 +92,9 @@ void fuzz_target(FuzzBufferType buffer, const std::string& LIMIT_TO_MESSAGE_TYPE
8692
g_setup->m_node.peerman->InitializeNode(&p2p_node);
8793
FillNode(fuzzed_data_provider, p2p_node, /* init_version */ successfully_connected);
8894

95+
const auto mock_time = ConsumeTime(fuzzed_data_provider);
96+
SetMockTime(mock_time);
97+
8998
// fuzzed_data_provider is fully consumed after this call, don't use it
9099
CDataStream random_bytes_data_stream{fuzzed_data_provider.ConsumeRemainingBytes<unsigned char>(), SER_NETWORK, PROTOCOL_VERSION};
91100
try {
@@ -109,8 +118,16 @@ FUZZ_TARGET_MSG(blocktxn);
109118
FUZZ_TARGET_MSG(cfcheckpt);
110119
FUZZ_TARGET_MSG(cfheaders);
111120
FUZZ_TARGET_MSG(cfilter);
121+
FUZZ_TARGET_MSG(clsig);
112122
FUZZ_TARGET_MSG(cmpctblock);
113-
FUZZ_TARGET_MSG(feefilter);
123+
FUZZ_TARGET_MSG(dsa);
124+
FUZZ_TARGET_MSG(dsc);
125+
FUZZ_TARGET_MSG(dsf);
126+
FUZZ_TARGET_MSG(dsi);
127+
FUZZ_TARGET_MSG(dsq);
128+
FUZZ_TARGET_MSG(dss);
129+
FUZZ_TARGET_MSG(dssu);
130+
FUZZ_TARGET_MSG(dstx);
114131
FUZZ_TARGET_MSG(filteradd);
115132
FUZZ_TARGET_MSG(filterclear);
116133
FUZZ_TARGET_MSG(filterload);
@@ -122,17 +139,47 @@ FUZZ_TARGET_MSG(getcfheaders);
122139
FUZZ_TARGET_MSG(getcfilters);
123140
FUZZ_TARGET_MSG(getdata);
124141
FUZZ_TARGET_MSG(getheaders);
142+
FUZZ_TARGET_MSG(getheaders2);
143+
FUZZ_TARGET_MSG(getmnlistd);
144+
FUZZ_TARGET_MSG(getqrinfo);
145+
FUZZ_TARGET_MSG(getsporks);
146+
FUZZ_TARGET_MSG(govobj);
147+
FUZZ_TARGET_MSG(govobjvote);
148+
FUZZ_TARGET_MSG(govsync);
125149
FUZZ_TARGET_MSG(headers);
150+
FUZZ_TARGET_MSG(headers2);
126151
FUZZ_TARGET_MSG(inv);
152+
FUZZ_TARGET_MSG(isdlock);
127153
FUZZ_TARGET_MSG(mempool);
128154
FUZZ_TARGET_MSG(merkleblock);
155+
FUZZ_TARGET_MSG(mnauth);
156+
FUZZ_TARGET_MSG(mnlistdiff);
129157
FUZZ_TARGET_MSG(notfound);
130158
FUZZ_TARGET_MSG(ping);
131159
FUZZ_TARGET_MSG(pong);
160+
FUZZ_TARGET_MSG(qbsigs);
161+
FUZZ_TARGET_MSG(qcomplaint);
162+
FUZZ_TARGET_MSG(qcontrib);
163+
FUZZ_TARGET_MSG(qdata);
164+
FUZZ_TARGET_MSG(qfcommit);
165+
FUZZ_TARGET_MSG(qgetdata);
166+
FUZZ_TARGET_MSG(qgetsigs);
167+
FUZZ_TARGET_MSG(qjustify);
168+
FUZZ_TARGET_MSG(qpcommit);
169+
FUZZ_TARGET_MSG(qrinfo);
170+
FUZZ_TARGET_MSG(qsendrecsigs);
171+
FUZZ_TARGET_MSG(qsigrec);
172+
FUZZ_TARGET_MSG(qsigsesann);
173+
FUZZ_TARGET_MSG(qsigshare);
174+
FUZZ_TARGET_MSG(qsigsinv);
175+
FUZZ_TARGET_MSG(qwatch);
132176
FUZZ_TARGET_MSG(sendaddrv2);
133177
FUZZ_TARGET_MSG(sendcmpct);
178+
FUZZ_TARGET_MSG(senddsq);
134179
FUZZ_TARGET_MSG(sendheaders);
180+
FUZZ_TARGET_MSG(sendheaders2);
181+
FUZZ_TARGET_MSG(spork);
182+
FUZZ_TARGET_MSG(ssc);
135183
FUZZ_TARGET_MSG(tx);
136184
FUZZ_TARGET_MSG(verack);
137185
FUZZ_TARGET_MSG(version);
138-
FUZZ_TARGET_MSG(wtxidrelay);

src/test/fuzz/process_messages.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <test/util/mining.h>
1313
#include <test/util/net.h>
1414
#include <test/util/setup_common.h>
15+
#include <test/util/validation.h>
1516
#include <validation.h>
1617
#include <validationinterface.h>
1718

@@ -33,9 +34,12 @@ FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
3334
{
3435
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
3536

36-
ConnmanTestMsg& connman = *(ConnmanTestMsg*)g_setup->m_node.connman.get();
37-
std::vector<CNode*> peers;
37+
ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
38+
TestChainState& chainstate = *static_cast<TestChainState*>(&g_setup->m_node.chainman->ActiveChainstate());
39+
SetMockTime(1610000000); // any time to successfully reset ibd
40+
chainstate.ResetIbd();
3841

42+
std::vector<CNode*> peers;
3943
const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
4044
for (int i = 0; i < num_peers_to_add; ++i) {
4145
peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
@@ -53,11 +57,14 @@ FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
5357
while (fuzzed_data_provider.ConsumeBool()) {
5458
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
5559

60+
const auto mock_time = ConsumeTime(fuzzed_data_provider);
61+
SetMockTime(mock_time);
62+
5663
CSerializedNetMsg net_msg;
5764
net_msg.command = random_message_type;
5865
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
5966

60-
CNode& random_node = *peers.at(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, peers.size() - 1));
67+
CNode& random_node = *PickValue(fuzzed_data_provider, peers);
6168

6269
(void)connman.ReceiveMsgFrom(random_node, net_msg);
6370
random_node.fPauseSend = false;

src/test/fuzz/psbt.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <test/fuzz/FuzzedDataProvider.h>
56
#include <test/fuzz/fuzz.h>
67

78
#include <node/psbt.h>
89
#include <psbt.h>
910
#include <pubkey.h>
1011
#include <script/script.h>
1112
#include <streams.h>
13+
#include <util/check.h>
1214
#include <version.h>
1315

1416
#include <cstdint>
@@ -18,10 +20,10 @@
1820

1921
FUZZ_TARGET(psbt)
2022
{
23+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2124
PartiallySignedTransaction psbt_mut;
22-
const std::string raw_psbt{buffer.begin(), buffer.end()};
2325
std::string error;
24-
if (!DecodeRawPSBT(psbt_mut, raw_psbt, error)) {
26+
if (!DecodeRawPSBT(psbt_mut, fuzzed_data_provider.ConsumeRandomLengthString(), error)) {
2527
return;
2628
}
2729
const PartiallySignedTransaction psbt = psbt_mut;
@@ -66,6 +68,20 @@ FUZZ_TARGET(psbt)
6668
const PartiallySignedTransaction psbt_from_tx{result};
6769
}
6870

71+
PartiallySignedTransaction psbt_merge;
72+
if (!DecodeRawPSBT(psbt_merge, fuzzed_data_provider.ConsumeRandomLengthString(), error)) {
73+
psbt_merge = psbt;
74+
}
75+
psbt_mut = psbt;
76+
(void)psbt_mut.Merge(psbt_merge);
77+
psbt_mut = psbt;
78+
(void)CombinePSBTs(psbt_mut, {psbt_mut, psbt_merge});
6979
psbt_mut = psbt;
70-
(void)psbt_mut.Merge(psbt);
80+
for (unsigned int i = 0; i < psbt_merge.tx->vin.size(); ++i) {
81+
(void)psbt_mut.AddInput(psbt_merge.tx->vin[i], psbt_merge.inputs[i]);
82+
}
83+
for (unsigned int i = 0; i < psbt_merge.tx->vout.size(); ++i) {
84+
Assert(psbt_mut.AddOutput(psbt_merge.tx->vout[i], psbt_merge.outputs[i]));
85+
}
86+
psbt_mut.unknown.insert(psbt_merge.unknown.begin(), psbt_merge.unknown.end());
7187
}

0 commit comments

Comments
 (0)