diff --git a/src/random.h b/src/random.h --- a/src/random.h +++ b/src/random.h @@ -179,7 +179,10 @@ } } - /** Generate a random integer in the range [0..range). */ + /** + * Generate a random integer in the range [0..range). + * Precondition: range > 0. + */ uint64_t randrange(uint64_t range) noexcept { assert(range); --range; diff --git a/src/test/fuzz/CMakeLists.txt b/src/test/fuzz/CMakeLists.txt --- a/src/test/fuzz/CMakeLists.txt +++ b/src/test/fuzz/CMakeLists.txt @@ -87,12 +87,14 @@ descriptor_parse eval_script fee_rate + flatfile float hex integer key key_io locale + merkleblock multiplication_overflow net_permissions netaddress @@ -106,12 +108,14 @@ process_messages protocol psbt + random rolling_bloom_filter script script_flags script_ops scriptnum_ops signature_checker + span spanparsing string strprintf diff --git a/src/test/fuzz/flatfile.cpp b/src/test/fuzz/flatfile.cpp new file mode 100644 --- /dev/null +++ b/src/test/fuzz/flatfile.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +void test_one_input(const std::vector &buffer) { + FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); + std::optional flat_file_pos = + ConsumeDeserializable(fuzzed_data_provider); + if (!flat_file_pos) { + return; + } + std::optional another_flat_file_pos = + ConsumeDeserializable(fuzzed_data_provider); + if (another_flat_file_pos) { + assert((*flat_file_pos == *another_flat_file_pos) != + (*flat_file_pos != *another_flat_file_pos)); + } + (void)flat_file_pos->ToString(); + flat_file_pos->SetNull(); + assert(flat_file_pos->IsNull()); +} diff --git a/src/test/fuzz/merkleblock.cpp b/src/test/fuzz/merkleblock.cpp new file mode 100644 --- /dev/null +++ b/src/test/fuzz/merkleblock.cpp @@ -0,0 +1,28 @@ +// Copyright (c) 2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +void test_one_input(const std::vector &buffer) { + FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); + std::optional partial_merkle_tree = + ConsumeDeserializable(fuzzed_data_provider); + if (!partial_merkle_tree) { + return; + } + (void)partial_merkle_tree->GetNumTransactions(); + std::vector matches; + std::vector indices; + (void)partial_merkle_tree->ExtractMatches(matches, indices); +} diff --git a/src/test/fuzz/random.cpp b/src/test/fuzz/random.cpp new file mode 100644 --- /dev/null +++ b/src/test/fuzz/random.cpp @@ -0,0 +1,36 @@ +// Copyright (c) 2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include + +#include +#include +#include +#include + +void test_one_input(const std::vector &buffer) { + FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); + FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)}; + (void)fast_random_context.rand64(); + (void)fast_random_context.randbits( + fuzzed_data_provider.ConsumeIntegralInRange(0, 64)); + (void)fast_random_context.randrange( + fuzzed_data_provider.ConsumeIntegralInRange( + FastRandomContext::min() + 1, FastRandomContext::max())); + (void)fast_random_context.randbytes( + fuzzed_data_provider.ConsumeIntegralInRange(0, 1024)); + (void)fast_random_context.rand32(); + (void)fast_random_context.rand256(); + (void)fast_random_context.randbool(); + (void)fast_random_context(); + + std::vector integrals = + ConsumeRandomLengthIntegralVector(fuzzed_data_provider); + Shuffle(integrals.begin(), integrals.end(), fast_random_context); + std::shuffle(integrals.begin(), integrals.end(), fast_random_context); +} diff --git a/src/test/fuzz/span.cpp b/src/test/fuzz/span.cpp new file mode 100644 --- /dev/null +++ b/src/test/fuzz/span.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +void test_one_input(const std::vector &buffer) { + FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); + + std::string str = fuzzed_data_provider.ConsumeBytesAsString(32); + const Span span = MakeSpan(str); + (void)span.data(); + (void)span.begin(); + (void)span.end(); + if (span.size() > 0) { + const std::ptrdiff_t idx = + fuzzed_data_provider.ConsumeIntegralInRange( + 0U, span.size() - 1U); + (void)span.first(idx); + (void)span.last(idx); + (void)span.subspan(idx); + (void)span.subspan(idx, span.size() - idx); + (void)span[idx]; + } + + std::string another_str = fuzzed_data_provider.ConsumeBytesAsString(32); + const Span another_span = MakeSpan(another_str); + assert((span <= another_span) != (span > another_span)); + assert((span == another_span) != (span != another_span)); + assert((span >= another_span) != (span < another_span)); +} diff --git a/src/test/fuzz/string.cpp b/src/test/fuzz/string.cpp --- a/src/test/fuzz/string.cpp +++ b/src/test/fuzz/string.cpp @@ -12,9 +12,8 @@ #include #include #include