Skip to content

Commit 5f9cd62

Browse files
author
MarcoFalke
committed
Merge #18455: tests: Add fuzzing harness for functions/classes in flatfile.h, merkleblock.h, random.h, serialize.h and span.h
11a520f tests: Add fuzzing harness for functions/classes in random.h (practicalswift) 64d277b tests: Add fuzzing harness for LimitedString (serialize.h) (practicalswift) f205cf7 tests: Add fuzzing harness for functions/classes in span.h (practicalswift) 9718f38 tests: Add fuzzing harness for functions/classes in merkleblock.h (practicalswift) a16ea05 tests: Add fuzzing harness for functions/classes in flatfile.h (practicalswift) Pull request description: * Add fuzzing harness for functions/classes in `flatfile.h` * Add fuzzing harness for functions/classes in `merkleblock.h` * Add fuzzing harness for functions/classes in `span.h` * Add fuzzing harness for `LimitedString` (`serialize.h`) * Add fuzzing harness for functions/classes in `random.h` Top commit has no ACKs. Tree-SHA512: 6f7e0f946f1062d51216990cde9672b4e896335152548ace3d8711e4969c3e3c8566d01d915b72adcda5c1caa9c2e34da6b7473b55a229f5b77239d3b0ba4b67
2 parents 6cfb3db + 11a520f commit 5f9cd62

File tree

8 files changed

+208
-9
lines changed

8 files changed

+208
-9
lines changed

src/Makefile.test.include

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ FUZZ_TARGETS = \
3535
test/fuzz/fee_rate \
3636
test/fuzz/fee_rate_deserialize \
3737
test/fuzz/flat_file_pos_deserialize \
38+
test/fuzz/flatfile \
3839
test/fuzz/float \
3940
test/fuzz/hex \
4041
test/fuzz/integer \
@@ -44,6 +45,7 @@ FUZZ_TARGETS = \
4445
test/fuzz/key_origin_info_deserialize \
4546
test/fuzz/locale \
4647
test/fuzz/merkle_block_deserialize \
48+
test/fuzz/merkleblock \
4749
test/fuzz/messageheader_deserialize \
4850
test/fuzz/multiplication_overflow \
4951
test/fuzz/net_permissions \
@@ -89,6 +91,7 @@ FUZZ_TARGETS = \
8991
test/fuzz/psbt_input_deserialize \
9092
test/fuzz/psbt_output_deserialize \
9193
test/fuzz/pub_key_deserialize \
94+
test/fuzz/random \
9295
test/fuzz/rolling_bloom_filter \
9396
test/fuzz/script \
9497
test/fuzz/script_deserialize \
@@ -98,6 +101,7 @@ FUZZ_TARGETS = \
98101
test/fuzz/service_deserialize \
99102
test/fuzz/signature_checker \
100103
test/fuzz/snapshotmetadata_deserialize \
104+
test/fuzz/span \
101105
test/fuzz/spanparsing \
102106
test/fuzz/string \
103107
test/fuzz/strprintf \
@@ -480,6 +484,12 @@ test_fuzz_flat_file_pos_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
480484
test_fuzz_flat_file_pos_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
481485
test_fuzz_flat_file_pos_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
482486

487+
test_fuzz_flatfile_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
488+
test_fuzz_flatfile_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
489+
test_fuzz_flatfile_LDADD = $(FUZZ_SUITE_LD_COMMON)
490+
test_fuzz_flatfile_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
491+
test_fuzz_flatfile_SOURCES = $(FUZZ_SUITE) test/fuzz/flatfile.cpp
492+
483493
test_fuzz_float_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
484494
test_fuzz_float_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
485495
test_fuzz_float_LDADD = $(FUZZ_SUITE_LD_COMMON)
@@ -534,6 +544,12 @@ test_fuzz_merkle_block_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
534544
test_fuzz_merkle_block_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
535545
test_fuzz_merkle_block_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
536546

547+
test_fuzz_merkleblock_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
548+
test_fuzz_merkleblock_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
549+
test_fuzz_merkleblock_LDADD = $(FUZZ_SUITE_LD_COMMON)
550+
test_fuzz_merkleblock_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
551+
test_fuzz_merkleblock_SOURCES = $(FUZZ_SUITE) test/fuzz/merkleblock.cpp
552+
537553
test_fuzz_messageheader_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DMESSAGEHEADER_DESERIALIZE=1
538554
test_fuzz_messageheader_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
539555
test_fuzz_messageheader_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
@@ -804,6 +820,12 @@ test_fuzz_pub_key_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
804820
test_fuzz_pub_key_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
805821
test_fuzz_pub_key_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
806822

823+
test_fuzz_random_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
824+
test_fuzz_random_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
825+
test_fuzz_random_LDADD = $(FUZZ_SUITE_LD_COMMON)
826+
test_fuzz_random_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
827+
test_fuzz_random_SOURCES = $(FUZZ_SUITE) test/fuzz/random.cpp
828+
807829
test_fuzz_rolling_bloom_filter_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
808830
test_fuzz_rolling_bloom_filter_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
809831
test_fuzz_rolling_bloom_filter_LDADD = $(FUZZ_SUITE_LD_COMMON)
@@ -858,6 +880,12 @@ test_fuzz_snapshotmetadata_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
858880
test_fuzz_snapshotmetadata_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
859881
test_fuzz_snapshotmetadata_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
860882

883+
test_fuzz_span_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
884+
test_fuzz_span_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
885+
test_fuzz_span_LDADD = $(FUZZ_SUITE_LD_COMMON)
886+
test_fuzz_span_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
887+
test_fuzz_span_SOURCES = $(FUZZ_SUITE) test/fuzz/span.cpp
888+
861889
test_fuzz_spanparsing_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
862890
test_fuzz_spanparsing_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
863891
test_fuzz_spanparsing_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/random.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ void RandAddEvent(const uint32_t event_info) noexcept;
103103
*
104104
* This class is not thread-safe.
105105
*/
106-
class FastRandomContext {
106+
class FastRandomContext
107+
{
107108
private:
108109
bool requires_seed;
109110
ChaCha20 rng;
@@ -155,7 +156,8 @@ class FastRandomContext {
155156
}
156157

157158
/** Generate a random (bits)-bit integer. */
158-
uint64_t randbits(int bits) noexcept {
159+
uint64_t randbits(int bits) noexcept
160+
{
159161
if (bits == 0) {
160162
return 0;
161163
} else if (bits > 32) {
@@ -169,7 +171,9 @@ class FastRandomContext {
169171
}
170172
}
171173

172-
/** Generate a random integer in the range [0..range). */
174+
/** Generate a random integer in the range [0..range).
175+
* Precondition: range > 0.
176+
*/
173177
uint64_t randrange(uint64_t range) noexcept
174178
{
175179
assert(range);
@@ -210,7 +214,7 @@ class FastRandomContext {
210214
* debug mode detects and panics on. This is a known issue, see
211215
* https://stackoverflow.com/questions/22915325/avoiding-self-assignment-in-stdshuffle
212216
*/
213-
template<typename I, typename R>
217+
template <typename I, typename R>
214218
void Shuffle(I first, I last, R&& rng)
215219
{
216220
while (first != last) {
@@ -233,7 +237,7 @@ static const int NUM_OS_RANDOM_BYTES = 32;
233237
/** Get 32 bytes of system entropy. Do not use this in application code: use
234238
* GetStrongRandBytes instead.
235239
*/
236-
void GetOSRand(unsigned char *ent32);
240+
void GetOSRand(unsigned char* ent32);
237241

238242
/** Check that OS randomness is available and returning the requested number
239243
* of bytes.

src/test/fuzz/flatfile.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2020 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 <flatfile.h>
6+
#include <optional.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <test/fuzz/fuzz.h>
9+
#include <test/fuzz/util.h>
10+
11+
#include <cassert>
12+
#include <cstdint>
13+
#include <string>
14+
#include <vector>
15+
16+
void test_one_input(const std::vector<uint8_t>& buffer)
17+
{
18+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19+
Optional<FlatFilePos> flat_file_pos = ConsumeDeserializable<FlatFilePos>(fuzzed_data_provider);
20+
if (!flat_file_pos) {
21+
return;
22+
}
23+
Optional<FlatFilePos> another_flat_file_pos = ConsumeDeserializable<FlatFilePos>(fuzzed_data_provider);
24+
if (another_flat_file_pos) {
25+
assert((*flat_file_pos == *another_flat_file_pos) != (*flat_file_pos != *another_flat_file_pos));
26+
}
27+
(void)flat_file_pos->ToString();
28+
flat_file_pos->SetNull();
29+
assert(flat_file_pos->IsNull());
30+
}

src/test/fuzz/merkleblock.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2020 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 <merkleblock.h>
6+
#include <optional.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <test/fuzz/fuzz.h>
9+
#include <test/fuzz/util.h>
10+
#include <uint256.h>
11+
12+
#include <cstdint>
13+
#include <string>
14+
#include <vector>
15+
16+
void test_one_input(const std::vector<uint8_t>& buffer)
17+
{
18+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19+
Optional<CPartialMerkleTree> partial_merkle_tree = ConsumeDeserializable<CPartialMerkleTree>(fuzzed_data_provider);
20+
if (!partial_merkle_tree) {
21+
return;
22+
}
23+
(void)partial_merkle_tree->GetNumTransactions();
24+
std::vector<uint256> matches;
25+
std::vector<unsigned int> indices;
26+
(void)partial_merkle_tree->ExtractMatches(matches, indices);
27+
}

src/test/fuzz/random.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2020 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 <random.h>
6+
#include <test/fuzz/FuzzedDataProvider.h>
7+
#include <test/fuzz/fuzz.h>
8+
#include <test/fuzz/util.h>
9+
10+
#include <algorithm>
11+
#include <cstdint>
12+
#include <string>
13+
#include <vector>
14+
15+
void test_one_input(const std::vector<uint8_t>& buffer)
16+
{
17+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
18+
FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
19+
(void)fast_random_context.rand64();
20+
(void)fast_random_context.randbits(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 64));
21+
(void)fast_random_context.randrange(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(FastRandomContext::min() + 1, FastRandomContext::max()));
22+
(void)fast_random_context.randbytes(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 1024));
23+
(void)fast_random_context.rand32();
24+
(void)fast_random_context.rand256();
25+
(void)fast_random_context.randbool();
26+
(void)fast_random_context();
27+
28+
std::vector<int64_t> integrals = ConsumeRandomLengthIntegralVector<int64_t>(fuzzed_data_provider);
29+
Shuffle(integrals.begin(), integrals.end(), fast_random_context);
30+
std::shuffle(integrals.begin(), integrals.end(), fast_random_context);
31+
}

src/test/fuzz/span.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2020 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 <span.h>
6+
#include <test/fuzz/FuzzedDataProvider.h>
7+
#include <test/fuzz/fuzz.h>
8+
#include <test/fuzz/util.h>
9+
10+
#include <cassert>
11+
#include <cstddef>
12+
#include <cstdint>
13+
#include <string>
14+
#include <vector>
15+
16+
void test_one_input(const std::vector<uint8_t>& buffer)
17+
{
18+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19+
20+
std::string str = fuzzed_data_provider.ConsumeBytesAsString(32);
21+
const Span<const char> span = MakeSpan(str);
22+
(void)span.data();
23+
(void)span.begin();
24+
(void)span.end();
25+
if (span.size() > 0) {
26+
const std::ptrdiff_t idx = fuzzed_data_provider.ConsumeIntegralInRange<std::ptrdiff_t>(0U, span.size() - 1U);
27+
(void)span.first(idx);
28+
(void)span.last(idx);
29+
(void)span.subspan(idx);
30+
(void)span.subspan(idx, span.size() - idx);
31+
(void)span[idx];
32+
}
33+
34+
std::string another_str = fuzzed_data_provider.ConsumeBytesAsString(32);
35+
const Span<const char> another_span = MakeSpan(another_str);
36+
assert((span <= another_span) != (span > another_span));
37+
assert((span == another_span) != (span != another_span));
38+
assert((span >= another_span) != (span < another_span));
39+
}

src/test/fuzz/string.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <rpc/server.h>
1313
#include <rpc/util.h>
1414
#include <script/descriptor.h>
15+
#include <serialize.h>
16+
#include <streams.h>
1517
#include <test/fuzz/FuzzedDataProvider.h>
1618
#include <test/fuzz/fuzz.h>
1719
#include <test/fuzz/util.h>
@@ -24,6 +26,7 @@
2426
#include <util/system.h>
2527
#include <util/translation.h>
2628
#include <util/url.h>
29+
#include <version.h>
2730

2831
#include <cstdint>
2932
#include <string>
@@ -86,4 +89,30 @@ void test_one_input(const std::vector<uint8_t>& buffer)
8689
(void)urlDecode(random_string_1);
8790
(void)ValidAsCString(random_string_1);
8891
(void)_(random_string_1.c_str());
92+
93+
{
94+
CDataStream data_stream{SER_NETWORK, INIT_PROTO_VERSION};
95+
std::string s;
96+
LimitedString<10> limited_string = LIMITED_STRING(s, 10);
97+
data_stream << random_string_1;
98+
try {
99+
data_stream >> limited_string;
100+
assert(data_stream.empty());
101+
assert(s.size() <= random_string_1.size());
102+
assert(s.size() <= 10);
103+
if (!random_string_1.empty()) {
104+
assert(!s.empty());
105+
}
106+
} catch (const std::ios_base::failure&) {
107+
}
108+
}
109+
{
110+
CDataStream data_stream{SER_NETWORK, INIT_PROTO_VERSION};
111+
const LimitedString<10> limited_string = LIMITED_STRING(random_string_1, 10);
112+
data_stream << limited_string;
113+
std::string deserialized_string;
114+
data_stream >> deserialized_string;
115+
assert(data_stream.empty());
116+
assert(deserialized_string == random_string_1);
117+
}
89118
}

src/test/fuzz/util.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
#include <string>
2121
#include <vector>
2222

23-
NODISCARD inline std::vector<uint8_t> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, size_t max_length = 4096) noexcept
23+
NODISCARD inline std::vector<uint8_t> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
2424
{
2525
const std::string s = fuzzed_data_provider.ConsumeRandomLengthString(max_length);
2626
return {s.begin(), s.end()};
2727
}
2828

29-
NODISCARD inline std::vector<std::string> ConsumeRandomLengthStringVector(FuzzedDataProvider& fuzzed_data_provider, size_t max_vector_size = 16, size_t max_string_length = 16) noexcept
29+
NODISCARD inline std::vector<std::string> ConsumeRandomLengthStringVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16, const size_t max_string_length = 16) noexcept
3030
{
3131
const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
3232
std::vector<std::string> r;
@@ -37,7 +37,18 @@ NODISCARD inline std::vector<std::string> ConsumeRandomLengthStringVector(Fuzzed
3737
}
3838

3939
template <typename T>
40-
NODISCARD inline Optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, size_t max_length = 4096) noexcept
40+
NODISCARD inline std::vector<T> ConsumeRandomLengthIntegralVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16) noexcept
41+
{
42+
const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
43+
std::vector<T> r;
44+
for (size_t i = 0; i < n_elements; ++i) {
45+
r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
46+
}
47+
return r;
48+
}
49+
50+
template <typename T>
51+
NODISCARD inline Optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
4152
{
4253
const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
4354
CDataStream ds{buffer, SER_NETWORK, INIT_PROTO_VERSION};
@@ -81,7 +92,7 @@ NODISCARD inline uint256 ConsumeUInt256(FuzzedDataProvider& fuzzed_data_provider
8192
}
8293

8394
template <typename T>
84-
bool MultiplicationOverflow(T i, T j)
95+
NODISCARD bool MultiplicationOverflow(const T i, const T j) noexcept
8596
{
8697
static_assert(std::is_integral<T>::value, "Integral required.");
8798
if (std::numeric_limits<T>::is_signed) {

0 commit comments

Comments
 (0)