Skip to content

Commit 4678db1

Browse files
committed
merge bitcoin#17996: Add fuzzing harness for serialization/deserialization of floating-points and integrals
1 parent 55abb1e commit 4678db1

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

src/Makefile.test.include

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ FUZZ_TARGETS = \
3232
test/fuzz/eval_script \
3333
test/fuzz/fee_rate_deserialize \
3434
test/fuzz/flat_file_pos_deserialize \
35+
test/fuzz/float \
3536
test/fuzz/hex \
3637
test/fuzz/integer \
3738
test/fuzz/inv_deserialize \
@@ -497,6 +498,12 @@ test_fuzz_flat_file_pos_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
497498
test_fuzz_flat_file_pos_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) $(LDFLAGS_WRAP_EXCEPTIONS)
498499
test_fuzz_flat_file_pos_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
499500

501+
test_fuzz_float_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
502+
test_fuzz_float_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
503+
test_fuzz_float_LDADD = $(FUZZ_SUITE_LD_COMMON)
504+
test_fuzz_float_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) $(LDFLAGS_WRAP_EXCEPTIONS)
505+
test_fuzz_float_SOURCES = $(FUZZ_SUITE) test/fuzz/float.cpp
506+
500507
test_fuzz_key_origin_info_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
501508
test_fuzz_key_origin_info_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DKEY_ORIGIN_INFO_DESERIALIZE=1
502509
test_fuzz_key_origin_info_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

src/indirectmap.h

+2
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

+2
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

+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+
}

src/test/fuzz/integer.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#include <script/sign.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);
@@ -119,4 +122,68 @@ void test_one_input(const std::vector<uint8_t>& buffer)
119122
(void)GetScriptForDestination(destination);
120123
(void)IsValidDestination(destination);
121124
}
125+
126+
{
127+
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
128+
129+
uint256 deserialized_u256;
130+
stream << u256;
131+
stream >> deserialized_u256;
132+
assert(u256 == deserialized_u256 && stream.empty());
133+
134+
uint160 deserialized_u160;
135+
stream << u160;
136+
stream >> deserialized_u160;
137+
assert(u160 == deserialized_u160 && stream.empty());
138+
139+
uint64_t deserialized_u64;
140+
stream << u64;
141+
stream >> deserialized_u64;
142+
assert(u64 == deserialized_u64 && stream.empty());
143+
144+
int64_t deserialized_i64;
145+
stream << i64;
146+
stream >> deserialized_i64;
147+
assert(i64 == deserialized_i64 && stream.empty());
148+
149+
uint32_t deserialized_u32;
150+
stream << u32;
151+
stream >> deserialized_u32;
152+
assert(u32 == deserialized_u32 && stream.empty());
153+
154+
int32_t deserialized_i32;
155+
stream << i32;
156+
stream >> deserialized_i32;
157+
assert(i32 == deserialized_i32 && stream.empty());
158+
159+
uint16_t deserialized_u16;
160+
stream << u16;
161+
stream >> deserialized_u16;
162+
assert(u16 == deserialized_u16 && stream.empty());
163+
164+
int16_t deserialized_i16;
165+
stream << i16;
166+
stream >> deserialized_i16;
167+
assert(i16 == deserialized_i16 && stream.empty());
168+
169+
uint8_t deserialized_u8;
170+
stream << u8;
171+
stream >> deserialized_u8;
172+
assert(u8 == deserialized_u8 && stream.empty());
173+
174+
int8_t deserialized_i8;
175+
stream << i8;
176+
stream >> deserialized_i8;
177+
assert(i8 == deserialized_i8 && stream.empty());
178+
179+
char deserialized_ch;
180+
stream << ch;
181+
stream >> deserialized_ch;
182+
assert(ch == deserialized_ch && stream.empty());
183+
184+
bool deserialized_b;
185+
stream << b;
186+
stream >> deserialized_b;
187+
assert(b == deserialized_b && stream.empty());
188+
}
122189
}

test/fuzz/test_runner.py

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"decode_tx",
2525
"fee_rate_deserialize",
2626
"flat_file_pos_deserialize",
27+
"float",
2728
"hex",
2829
"integer",
2930
"key_origin_info_deserialize",

0 commit comments

Comments
 (0)