Skip to content

Commit a2b5aae

Browse files
author
MarcoFalke
committed
Merge #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
2 parents d7134b3 + 9ff41f6 commit a2b5aae

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
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 \
@@ -405,6 +406,12 @@ test_fuzz_flat_file_pos_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
405406
test_fuzz_flat_file_pos_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
406407
test_fuzz_flat_file_pos_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
407408

409+
test_fuzz_float_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
410+
test_fuzz_float_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
411+
test_fuzz_float_LDADD = $(FUZZ_SUITE_LD_COMMON)
412+
test_fuzz_float_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
413+
test_fuzz_float_SOURCES = $(FUZZ_SUITE) test/fuzz/float.cpp
414+
408415
test_fuzz_hex_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
409416
test_fuzz_hex_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
410417
test_fuzz_hex_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/indirectmap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_INDIRECTMAP_H
66
#define BITCOIN_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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#define BITCOIN_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

Lines changed: 42 additions & 0 deletions
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+
}

src/test/fuzz/integer.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#include <script/signingprovider.h>
2020
#include <script/standard.h>
2121
#include <serialize.h>
22+
#include <streams.h>
2223
#include <test/fuzz/FuzzedDataProvider.h>
2324
#include <test/fuzz/fuzz.h>
2425
#include <uint256.h>
2526
#include <util/strencodings.h>
2627
#include <util/system.h>
2728
#include <util/time.h>
29+
#include <version.h>
2830

2931
#include <cassert>
3032
#include <limits>
@@ -54,6 +56,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
5456
// We cannot assume a specific value of std::is_signed<char>::value:
5557
// ConsumeIntegral<char>() instead of casting from {u,}int8_t.
5658
const char ch = fuzzed_data_provider.ConsumeIntegral<char>();
59+
const bool b = fuzzed_data_provider.ConsumeBool();
5760

5861
const Consensus::Params& consensus_params = Params().GetConsensus();
5962
(void)CheckProofOfWork(u256, u32, consensus_params);
@@ -132,4 +135,68 @@ void test_one_input(const std::vector<uint8_t>& buffer)
132135
(void)GetScriptForDestination(destination);
133136
(void)IsValidDestination(destination);
134137
}
138+
139+
{
140+
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
141+
142+
uint256 deserialized_u256;
143+
stream << u256;
144+
stream >> deserialized_u256;
145+
assert(u256 == deserialized_u256 && stream.empty());
146+
147+
uint160 deserialized_u160;
148+
stream << u160;
149+
stream >> deserialized_u160;
150+
assert(u160 == deserialized_u160 && stream.empty());
151+
152+
uint64_t deserialized_u64;
153+
stream << u64;
154+
stream >> deserialized_u64;
155+
assert(u64 == deserialized_u64 && stream.empty());
156+
157+
int64_t deserialized_i64;
158+
stream << i64;
159+
stream >> deserialized_i64;
160+
assert(i64 == deserialized_i64 && stream.empty());
161+
162+
uint32_t deserialized_u32;
163+
stream << u32;
164+
stream >> deserialized_u32;
165+
assert(u32 == deserialized_u32 && stream.empty());
166+
167+
int32_t deserialized_i32;
168+
stream << i32;
169+
stream >> deserialized_i32;
170+
assert(i32 == deserialized_i32 && stream.empty());
171+
172+
uint16_t deserialized_u16;
173+
stream << u16;
174+
stream >> deserialized_u16;
175+
assert(u16 == deserialized_u16 && stream.empty());
176+
177+
int16_t deserialized_i16;
178+
stream << i16;
179+
stream >> deserialized_i16;
180+
assert(i16 == deserialized_i16 && stream.empty());
181+
182+
uint8_t deserialized_u8;
183+
stream << u8;
184+
stream >> deserialized_u8;
185+
assert(u8 == deserialized_u8 && stream.empty());
186+
187+
int8_t deserialized_i8;
188+
stream << i8;
189+
stream >> deserialized_i8;
190+
assert(i8 == deserialized_i8 && stream.empty());
191+
192+
char deserialized_ch;
193+
stream << ch;
194+
stream >> deserialized_ch;
195+
assert(ch == deserialized_ch && stream.empty());
196+
197+
bool deserialized_b;
198+
stream << b;
199+
stream >> deserialized_b;
200+
assert(b == deserialized_b && stream.empty());
201+
}
135202
}

test/fuzz/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"decode_tx",
2626
"fee_rate_deserialize",
2727
"flat_file_pos_deserialize",
28+
"float",
2829
"hex",
2930
"integer",
3031
"key_origin_info_deserialize",

0 commit comments

Comments
 (0)