Skip to content

Commit 3c82b92

Browse files
tests: Add fuzzing harness for functions taking floating-point types as input
1 parent c2bd588 commit 3c82b92

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Makefile.test.include

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ FUZZ_TARGETS = \
2929
test/fuzz/eval_script \
3030
test/fuzz/fee_rate_deserialize \
3131
test/fuzz/flat_file_pos_deserialize \
32+
test/fuzz/float \
3233
test/fuzz/hex \
3334
test/fuzz/integer \
3435
test/fuzz/inv_deserialize \
@@ -387,6 +388,12 @@ test_fuzz_flat_file_pos_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
387388
test_fuzz_flat_file_pos_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
388389
test_fuzz_flat_file_pos_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
389390

391+
test_fuzz_float_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
392+
test_fuzz_float_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
393+
test_fuzz_float_LDADD = $(FUZZ_SUITE_LD_COMMON)
394+
test_fuzz_float_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
395+
test_fuzz_float_SOURCES = $(FUZZ_SUITE) test/fuzz/float.cpp
396+
390397
test_fuzz_hex_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
391398
test_fuzz_hex_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
392399
test_fuzz_hex_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/float.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 <memusage.h>
6+
#include <serialize.h>
7+
#include <streams.h>
8+
#include <test/fuzz/FuzzedDataProvider.h>
9+
#include <test/fuzz/fuzz.h>
10+
#include <version.h>
11+
12+
#include <cassert>
13+
#include <cstdint>
14+
15+
void test_one_input(const std::vector<uint8_t>& buffer)
16+
{
17+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
18+
19+
{
20+
const double d = fuzzed_data_provider.ConsumeFloatingPoint<double>();
21+
(void)memusage::DynamicUsage(d);
22+
assert(ser_uint64_to_double(ser_double_to_uint64(d)) == d);
23+
24+
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
25+
stream << d;
26+
double d_deserialized;
27+
stream >> d_deserialized;
28+
assert(d == d_deserialized);
29+
}
30+
31+
{
32+
const float f = fuzzed_data_provider.ConsumeFloatingPoint<float>();
33+
(void)memusage::DynamicUsage(f);
34+
assert(ser_uint32_to_float(ser_float_to_uint32(f)) == f);
35+
36+
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
37+
stream << f;
38+
float f_deserialized;
39+
stream >> f_deserialized;
40+
assert(f == f_deserialized);
41+
}
42+
}

0 commit comments

Comments
 (0)