Branch data Line data Source code
1 : : // Copyright (c) 2009-present 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 : : #ifndef BITCOIN_TEST_FUZZ_UTIL_H
6 : : #define BITCOIN_TEST_FUZZ_UTIL_H
7 : :
8 : : #include <addresstype.h>
9 : : #include <arith_uint256.h>
10 : : #include <coins.h>
11 : : #include <compat/compat.h>
12 : : #include <consensus/amount.h>
13 : : #include <consensus/consensus.h>
14 : : #include <key.h>
15 : : #include <merkleblock.h>
16 : : #include <pow.h>
17 : : #include <primitives/transaction.h>
18 : : #include <script/script.h>
19 : : #include <serialize.h>
20 : : #include <streams.h>
21 : : #include <test/fuzz/FuzzedDataProvider.h>
22 : : #include <test/fuzz/fuzz.h>
23 : : #include <uint256.h>
24 : : #include <util/check.h>
25 : : #include <validation.h>
26 : :
27 : : #include <algorithm>
28 : : #include <array>
29 : : #include <cstdint>
30 : : #include <cstdio>
31 : : #include <optional>
32 : : #include <string>
33 : : #include <vector>
34 : :
35 : : class PeerManager;
36 : :
37 : : template <typename... Callables>
38 : 64845830 : size_t CallOneOf(FuzzedDataProvider& fuzzed_data_provider, Callables... callables)
39 : : {
40 : 64845830 : constexpr size_t call_size{sizeof...(callables)};
41 : : static_assert(call_size >= 1);
42 : 64845830 : const size_t call_index{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, call_size - 1)};
43 : :
44 : : size_t i{0};
45 [ + + + + ]: 65456044 : ((i++ == call_index ? callables() : void()), ...);
[ + + + +
+ + ][ + +
+ + + + +
+ ][ + + +
+ + + + +
+ + ]
[ + + + +
+ + + + +
+ + + ]
[ + + + +
+ + + + +
+ + + +
+ ]
[ + + + +
+ + + + +
+ + + + +
+ + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ]
46 : 64838868 : return call_size;
47 : : }
48 : :
49 : : template <typename Collection>
50 [ - + ]: 19260980 : auto PickIterator(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
51 : : {
52 : 19260980 : const auto sz{col.size()};
53 [ - + ][ - + ]: 19219228 : assert(sz >= 1);
54 : 19260980 : return std::next(col.begin(), fuzzed_data_provider.ConsumeIntegralInRange<decltype(sz)>(0, sz - 1));
55 : : }
56 : :
57 : : template <typename Collection>
58 : 19241484 : auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
59 : : {
60 : 19241484 : return *PickIterator(fuzzed_data_provider, col);
61 : : }
62 : :
63 : : template<typename B = uint8_t>
64 [ + + ]: 4046688 : [[nodiscard]] inline std::vector<B> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
65 : : {
66 : : static_assert(sizeof(B) == 1);
67 [ + + ]: 4046688 : const std::string s = max_length ?
68 : 613674 : fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
69 : : fuzzed_data_provider.ConsumeRandomLengthString();
70 [ - + ]: 4046688 : std::vector<B> ret(s.size());
71 [ - + ]: 4046688 : std::copy(s.begin(), s.end(), reinterpret_cast<char*>(ret.data()));
72 : 4046688 : return ret;
73 : 4046688 : }
74 : :
75 : 7670 : [[nodiscard]] inline DataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
76 : : {
77 [ - + ]: 7670 : return DataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
78 : : }
79 : :
80 : 427873 : [[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
81 : : {
82 : 427873 : const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
83 : 427873 : std::vector<std::string> r;
84 : 427873 : r.reserve(n_elements);
85 [ + + ]: 2535359 : for (size_t i = 0; i < n_elements; ++i) {
86 : 2107486 : r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length));
87 : : }
88 : 427873 : return r;
89 : : }
90 : :
91 : : template <typename T>
92 : 3988 : [[nodiscard]] inline std::vector<T> ConsumeRandomLengthIntegralVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16) noexcept
93 : : {
94 : 3988 : const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
95 : 3988 : std::vector<T> r;
96 : 3988 : r.reserve(n_elements);
97 [ + + ]: 31211 : for (size_t i = 0; i < n_elements; ++i) {
98 : 27223 : r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
99 : : }
100 : 3988 : return r;
101 : : }
102 : :
103 : : template <typename P>
104 : : [[nodiscard]] P ConsumeDeserializationParams(FuzzedDataProvider& fuzzed_data_provider) noexcept;
105 : :
106 : : template <typename T, typename P>
107 : 653529 : [[nodiscard]] std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const P& params, const std::optional<size_t>& max_length = std::nullopt) noexcept
108 : : {
109 [ - + ]: 653529 : const std::vector<uint8_t> buffer{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
110 : 653529 : SpanReader ds{buffer};
111 [ + + ][ + + ]: 653529 : T obj;
112 : : try {
113 [ + + ][ + + ]: 1296571 : ds >> params(obj);
114 [ - + ][ - + ]: 10487 : } catch (const std::ios_base::failure&) {
115 : 10487 : return std::nullopt;
116 : : }
117 : 643042 : return obj;
118 : 653529 : }
119 : :
120 : : template <typename T>
121 : 1176490 : [[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
122 : : {
123 [ - + ]: 1176490 : const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
124 [ + + ][ # # ]: 1176490 : SpanReader ds{buffer};
125 [ + + ][ + + ]: 1176490 : T obj;
[ # # ]
126 : : try {
127 : 854595 : ds >> obj;
128 [ - + ][ - + ]: 321895 : } catch (const std::ios_base::failure&) {
[ - - ]
129 : 321895 : return std::nullopt;
130 : : }
131 : 854595 : return obj;
132 : 1176490 : }
133 : :
134 : : template <typename T>
135 : 56037 : [[nodiscard]] inline std::optional<T> ConsumeDeserializableConstructor(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
136 : : {
137 [ - + ]: 56037 : const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
138 : 56037 : SpanReader ds{buffer};
139 : : try {
140 [ + + ]: 56037 : T obj(deserialize, ds);
141 : 52336 : return obj;
142 [ - + ]: 56037 : } catch (const std::ios_base::failure&) {
143 : 3701 : return std::nullopt;
144 : : }
145 : 56037 : }
146 : :
147 : : template <typename WeakEnumType, size_t size>
148 : 3999650 : [[nodiscard]] WeakEnumType ConsumeWeakEnum(FuzzedDataProvider& fuzzed_data_provider, const WeakEnumType (&all_types)[size]) noexcept
149 : : {
150 [ + + ]: 3999650 : return fuzzed_data_provider.ConsumeBool() ?
151 : 3872526 : fuzzed_data_provider.PickValueInArray<WeakEnumType>(all_types) :
152 : 127124 : WeakEnumType(fuzzed_data_provider.ConsumeIntegral<std::underlying_type_t<WeakEnumType>>());
153 : : }
154 : :
155 : 988001 : [[nodiscard]] inline opcodetype ConsumeOpcodeType(FuzzedDataProvider& fuzzed_data_provider) noexcept
156 : : {
157 : 988001 : return static_cast<opcodetype>(fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, MAX_OPCODE));
158 : : }
159 : :
160 : : [[nodiscard]] CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::optional<CAmount>& max = std::nullopt) noexcept;
161 : :
162 : : [[nodiscard]] NodeSeconds ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional<int64_t>& min = std::nullopt, const std::optional<int64_t>& max = std::nullopt) noexcept;
163 : :
164 : : template <class Dur>
165 : : // Having the compiler infer the template argument from the function argument
166 : : // is dangerous, because the desired return value generally has a different
167 : : // type than the function argument. So std::common_type is used to force the
168 : : // call site to specify the type of the return value.
169 : 3577625 : [[nodiscard]] Dur ConsumeDuration(FuzzedDataProvider& fuzzed_data_provider, std::common_type_t<Dur> min, std::common_type_t<Dur> max) noexcept
170 : : {
171 : 3577625 : return Dur{fuzzed_data_provider.ConsumeIntegralInRange(min.count(), max.count())};
172 : : }
173 : :
174 : : [[nodiscard]] CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider, const std::optional<std::vector<Txid>>& prevout_txids, int max_num_in = 10, int max_num_out = 10) noexcept;
175 : :
176 : : [[nodiscard]] CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, size_t max_stack_elem_size = 32) noexcept;
177 : :
178 : : [[nodiscard]] CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, bool maybe_p2wsh = false) noexcept;
179 : :
180 : : [[nodiscard]] uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept;
181 : :
182 : 537414 : [[nodiscard]] inline CScriptNum ConsumeScriptNum(FuzzedDataProvider& fuzzed_data_provider) noexcept
183 : : {
184 [ + + + + : 537414 : return CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
+ + ]
185 : : }
186 : :
187 : 45726 : [[nodiscard]] inline uint160 ConsumeUInt160(FuzzedDataProvider& fuzzed_data_provider) noexcept
188 : : {
189 : 45726 : const std::vector<uint8_t> v160 = fuzzed_data_provider.ConsumeBytes<uint8_t>(160 / 8);
190 [ - + + + ]: 45726 : if (v160.size() != 160 / 8) {
191 : 9989 : return {};
192 : : }
193 : 35737 : return uint160{v160};
194 : 45726 : }
195 : :
196 : 473146 : [[nodiscard]] inline uint256 ConsumeUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
197 : : {
198 : 473146 : const std::vector<uint8_t> v256 = fuzzed_data_provider.ConsumeBytes<uint8_t>(256 / 8);
199 [ - + + + ]: 473146 : if (v256.size() != 256 / 8) {
200 : 15930 : return {};
201 : : }
202 : 457216 : return uint256{v256};
203 : 473146 : }
204 : :
205 : 78371 : [[nodiscard]] inline arith_uint256 ConsumeArithUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
206 : : {
207 : 78371 : return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
208 : : }
209 : :
210 : 18017 : [[nodiscard]] inline arith_uint256 ConsumeArithUInt256InRange(FuzzedDataProvider& fuzzed_data_provider, const arith_uint256& min, const arith_uint256& max) noexcept
211 : : {
212 [ - + ]: 18017 : assert(min <= max);
213 : 18017 : const arith_uint256 range = max - min;
214 : 18017 : const arith_uint256 value = ConsumeArithUInt256(fuzzed_data_provider);
215 : 18017 : arith_uint256 result = value;
216 : : // Avoid division by 0, in case range + 1 results in overflow.
217 [ + - ]: 36034 : if (range != ~arith_uint256(0)) {
218 : 18017 : const arith_uint256 quotient = value / (range + 1);
219 : 18017 : result = value - (quotient * (range + 1));
220 : : }
221 : 18017 : result += min;
222 [ + - - + ]: 18017 : assert(result >= min && result <= max);
223 : 18017 : return result;
224 : : }
225 : :
226 : : [[nodiscard]] std::map<COutPoint, Coin> ConsumeCoins(FuzzedDataProvider& fuzzed_data_provider) noexcept;
227 : :
228 : : [[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept;
229 : :
230 : : [[nodiscard]] CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional<bool> compressed = std::nullopt) noexcept;
231 : :
232 : : template <typename T>
233 : 1971 : [[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept
234 : : {
235 : : static_assert(std::is_integral_v<T>, "Integral required.");
236 : : if (std::numeric_limits<T>::is_signed) {
237 [ + + ]: 1299 : if (i > 0) {
238 [ + + ]: 254 : if (j > 0) {
239 : 149 : return i > (std::numeric_limits<T>::max() / j);
240 : : } else {
241 : 105 : return j < (std::numeric_limits<T>::min() / i);
242 : : }
243 : : } else {
244 [ + + ]: 1045 : if (j > 0) {
245 : 179 : return i < (std::numeric_limits<T>::min() / j);
246 : : } else {
247 [ + + + + ]: 866 : return i != 0 && (j < (std::numeric_limits<T>::max() / i));
248 : : }
249 : : }
250 : : } else {
251 [ + + + + ]: 672 : return j != 0 && i > std::numeric_limits<T>::max() / j;
252 : : }
253 : : }
254 : :
255 : : [[nodiscard]] bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept;
256 : :
257 : : /**
258 : : * Sets errno to a value selected from the given std::array `errnos`.
259 : : */
260 : : template <typename T, size_t size>
261 : 188222 : void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider, const std::array<T, size>& errnos)
262 : : {
263 : 188222 : errno = fuzzed_data_provider.PickValueInArray(errnos);
264 : 188222 : }
265 : :
266 : : /*
267 : : * Sets a fuzzed errno in the range [0, 133 (EHWPOISON)]. Can be used from functions emulating
268 : : * standard library functions that set errno, or in other contexts where the value of errno
269 : : * might be relevant for the execution path that will be taken.
270 : : */
271 : 59707 : inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept
272 : : {
273 : 59707 : errno = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 133);
274 : 59707 : }
275 : :
276 : : /**
277 : : * Returns a byte vector of specified size regardless of the number of remaining bytes available
278 : : * from the fuzzer. Pads with zero value bytes if needed to achieve the specified size.
279 : : */
280 : : template<typename B = uint8_t>
281 : 235420 : [[nodiscard]] inline std::vector<B> ConsumeFixedLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t length) noexcept
282 : : {
283 : : static_assert(sizeof(B) == 1);
284 : 235420 : auto random_bytes = fuzzed_data_provider.ConsumeBytes<B>(length);
285 : 235420 : random_bytes.resize(length);
286 : 235420 : return random_bytes;
287 : : }
288 : :
289 : : class FuzzedFileProvider
290 : : {
291 : : FuzzedDataProvider& m_fuzzed_data_provider;
292 : : int64_t m_offset = 0;
293 : :
294 : : public:
295 [ + + ][ + - ]: 4316 : FuzzedFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
296 : : {
297 : : }
298 : :
299 : : FILE* open();
300 : :
301 : : static ssize_t read(void* cookie, char* buf, size_t size);
302 : :
303 : : static ssize_t write(void* cookie, const char* buf, size_t size);
304 : :
305 : : static int seek(void* cookie, int64_t* offset, int whence);
306 : :
307 : : static int close(void* cookie);
308 : : };
309 : :
310 : : #define WRITE_TO_STREAM_CASE(type, consume) \
311 : : [&] { \
312 : : type o = consume; \
313 : : stream << o; \
314 : : }
315 : : template <typename Stream>
316 : 3062 : void WriteToStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
317 : : {
318 [ + + ]: 452413 : while (fuzzed_data_provider.ConsumeBool()) {
319 : : try {
320 [ + + ]: 450673 : CallOneOf(
321 : : fuzzed_data_provider,
322 : 3162 : WRITE_TO_STREAM_CASE(bool, fuzzed_data_provider.ConsumeBool()),
323 : 364644 : WRITE_TO_STREAM_CASE(int8_t, fuzzed_data_provider.ConsumeIntegral<int8_t>()),
324 : 5322 : WRITE_TO_STREAM_CASE(uint8_t, fuzzed_data_provider.ConsumeIntegral<uint8_t>()),
325 : 4096 : WRITE_TO_STREAM_CASE(int16_t, fuzzed_data_provider.ConsumeIntegral<int16_t>()),
326 : 1294 : WRITE_TO_STREAM_CASE(uint16_t, fuzzed_data_provider.ConsumeIntegral<uint16_t>()),
327 : 3710 : WRITE_TO_STREAM_CASE(int32_t, fuzzed_data_provider.ConsumeIntegral<int32_t>()),
328 : 1039 : WRITE_TO_STREAM_CASE(uint32_t, fuzzed_data_provider.ConsumeIntegral<uint32_t>()),
329 : 2348 : WRITE_TO_STREAM_CASE(int64_t, fuzzed_data_provider.ConsumeIntegral<int64_t>()),
330 : 10471 : WRITE_TO_STREAM_CASE(uint64_t, fuzzed_data_provider.ConsumeIntegral<uint64_t>()),
331 [ + + ]: 102642 : WRITE_TO_STREAM_CASE(std::string, fuzzed_data_provider.ConsumeRandomLengthString(32)),
332 [ + + ]: 5657 : WRITE_TO_STREAM_CASE(std::vector<uint8_t>, ConsumeRandomLengthIntegralVector<uint8_t>(fuzzed_data_provider)));
333 [ - + ]: 1322 : } catch (const std::ios_base::failure&) {
334 : : break;
335 : : }
336 : : }
337 : 3062 : }
338 : :
339 : : #define READ_FROM_STREAM_CASE(type) \
340 : : [&] { \
341 : : type o; \
342 : : stream >> o; \
343 : : }
344 : : template <typename Stream>
345 : 6689 : void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
346 : : {
347 [ + + ]: 63579 : while (fuzzed_data_provider.ConsumeBool()) {
348 : : try {
349 [ + + ]: 62235 : CallOneOf(
350 : : fuzzed_data_provider,
351 : 3459 : READ_FROM_STREAM_CASE(bool),
352 : 3016 : READ_FROM_STREAM_CASE(int8_t),
353 : 3103 : READ_FROM_STREAM_CASE(uint8_t),
354 : 1846 : READ_FROM_STREAM_CASE(int16_t),
355 : 1482 : READ_FROM_STREAM_CASE(uint16_t),
356 : 6234 : READ_FROM_STREAM_CASE(int32_t),
357 : 5510 : READ_FROM_STREAM_CASE(uint32_t),
358 : 7449 : READ_FROM_STREAM_CASE(int64_t),
359 : 2780 : READ_FROM_STREAM_CASE(uint64_t),
360 [ + + ]: 18975 : READ_FROM_STREAM_CASE(std::string),
361 [ + + ]: 31520 : READ_FROM_STREAM_CASE(std::vector<uint8_t>));
362 [ - + ]: 5345 : } catch (const std::ios_base::failure&) {
363 : : break;
364 : : }
365 : : }
366 : 6689 : }
367 : :
368 : 287595 : inline void FinalizeHeader(CBlockHeader& header, const ChainstateManager& chainman)
369 : : {
370 [ + + ]: 572944 : while (!CheckProofOfWork(header.GetHash(), header.nBits, chainman.GetParams().GetConsensus())) {
371 : 285349 : ++(header.nNonce);
372 : : }
373 : 287595 : }
374 : :
375 : : #endif // BITCOIN_TEST_FUZZ_UTIL_H
|