Skip to content

Commit 11a520f

Browse files
tests: Add fuzzing harness for functions/classes in random.h
1 parent 64d277b commit 11a520f

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ FUZZ_TARGETS = \
9191
test/fuzz/psbt_input_deserialize \
9292
test/fuzz/psbt_output_deserialize \
9393
test/fuzz/pub_key_deserialize \
94+
test/fuzz/random \
9495
test/fuzz/rolling_bloom_filter \
9596
test/fuzz/script \
9697
test/fuzz/script_deserialize \
@@ -819,6 +820,12 @@ test_fuzz_pub_key_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
819820
test_fuzz_pub_key_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
820821
test_fuzz_pub_key_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
821822

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+
822829
test_fuzz_rolling_bloom_filter_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
823830
test_fuzz_rolling_bloom_filter_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
824831
test_fuzz_rolling_bloom_filter_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/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/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)