Skip to content

Commit 7e50abc

Browse files
tests: Add EvalScript(...) fuzzing harness
1 parent bebb637 commit 7e50abc

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ FUZZ_TARGETS = \
1717
test/fuzz/bloomfilter_deserialize \
1818
test/fuzz/coins_deserialize \
1919
test/fuzz/diskblockindex_deserialize \
20+
test/fuzz/eval_script \
2021
test/fuzz/inv_deserialize \
2122
test/fuzz/messageheader_deserialize \
2223
test/fuzz/netaddr_deserialize \
@@ -299,6 +300,12 @@ test_fuzz_diskblockindex_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
299300
test_fuzz_diskblockindex_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
300301
test_fuzz_diskblockindex_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
301302

303+
test_fuzz_eval_script_SOURCES = $(FUZZ_SUITE) test/fuzz/eval_script.cpp
304+
test_fuzz_eval_script_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
305+
test_fuzz_eval_script_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
306+
test_fuzz_eval_script_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
307+
test_fuzz_eval_script_LDADD = $(FUZZ_SUITE_LD_COMMON)
308+
302309
test_fuzz_txoutcompressor_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
303310
test_fuzz_txoutcompressor_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DTXOUTCOMPRESSOR_DESERIALIZE=1
304311
test_fuzz_txoutcompressor_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

src/test/fuzz/eval_script.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2009-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 <script/interpreter.h>
6+
#include <test/fuzz/FuzzedDataProvider.h>
7+
#include <test/fuzz/fuzz.h>
8+
9+
#include <limits>
10+
11+
void test_one_input(const std::vector<uint8_t>& buffer)
12+
{
13+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
14+
const unsigned int flags = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
15+
const std::vector<uint8_t> script_bytes = [&] {
16+
if (fuzzed_data_provider.remaining_bytes() != 0) {
17+
return fuzzed_data_provider.ConsumeRemainingBytes<uint8_t>();
18+
} else {
19+
// Avoid UBSan warning:
20+
// test/fuzz/FuzzedDataProvider.h:212:17: runtime error: null pointer passed as argument 1, which is declared to never be null
21+
// /usr/include/string.h:43:28: note: nonnull attribute specified here
22+
return std::vector<uint8_t>();
23+
}
24+
}();
25+
const CScript script(script_bytes.begin(), script_bytes.end());
26+
for (const auto sig_version : {SigVersion::BASE, SigVersion::WITNESS_V0}) {
27+
std::vector<std::vector<unsigned char>> stack;
28+
(void)EvalScript(stack, script, flags, BaseSignatureChecker(), sig_version, nullptr);
29+
}
30+
}

0 commit comments

Comments
 (0)