Skip to content

Commit 941061d

Browse files
MarcoFalkesidhujag
MarcoFalke
authored and
sidhujag
committed
Merge bitcoin#17996: tests: Add fuzzing harness for serialization/deserialization of floating-points and integrals
9ff41f6 tests: Add float to FUZZERS_MISSING_CORPORA (temporarily) (practicalswift) 8f6fb0a tests: Add serialization/deserialization fuzzing for integral types (practicalswift) 3c82b92 tests: Add fuzzing harness for functions taking floating-point types as input (practicalswift) c2bd588 Add missing includes (practicalswift) Pull request description: Add simple fuzzing harness for functions with floating-point parameters (such as `ser_double_to_uint64(double)`, etc.). Add serialization/deserialization fuzzing for integral types. Add missing includes. To test this PR: ``` $ make distclean $ ./autogen.sh $ CC=clang CXX=clang++ ./configure --enable-fuzz \ --with-sanitizers=address,fuzzer,undefined $ make $ src/test/fuzz/float … ``` Top commit has no ACKs. Tree-SHA512: 9b5a0c4838ad18d715c7398e557d2a6d0fcc03aa842f76d7a8ed716170a28f17f249eaede4256998aa3417afe2935e0ffdfaa883727d71ae2d2d18a41ced24b5
1 parent 61e2c76 commit 941061d

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

src/Makefile.test.include

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ FUZZ_TARGETS = \
3131
test/fuzz/eval_script \
3232
test/fuzz/fee_rate_deserialize \
3333
test/fuzz/flat_file_pos_deserialize \
34+
test/fuzz/float \
3435
test/fuzz/hex \
3536
test/fuzz/integer \
3637
test/fuzz/inv_deserialize \
@@ -418,6 +419,12 @@ test_fuzz_flat_file_pos_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
418419
test_fuzz_flat_file_pos_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
419420
test_fuzz_flat_file_pos_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
420421

422+
test_fuzz_float_CPPFLAGS = $(AM_CPPFLAGS) $(SYSCOIN_INCLUDES)
423+
test_fuzz_float_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
424+
test_fuzz_float_LDADD = $(FUZZ_SUITE_LD_COMMON)
425+
test_fuzz_float_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
426+
test_fuzz_float_SOURCES = $(FUZZ_SUITE) test/fuzz/float.cpp
427+
421428
test_fuzz_hex_CPPFLAGS = $(AM_CPPFLAGS) $(SYSCOIN_INCLUDES)
422429
test_fuzz_hex_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
423430
test_fuzz_hex_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/indirectmap.h

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef SYSCOIN_INDIRECTMAP_H
66
#define SYSCOIN_INDIRECTMAP_H
77

8+
#include <map>
9+
810
template <class T>
911
struct DereferencingComparator { bool operator()(const T a, const T b) const { return *a < *b; } };
1012

src/memusage.h

+2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#define SYSCOIN_MEMUSAGE_H
77

88
#include <indirectmap.h>
9+
#include <prevector.h>
910

1011
#include <stdlib.h>
1112

13+
#include <cassert>
1214
#include <map>
1315
#include <memory>
1416
#include <set>

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)