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 <validation.h>
25 : :
26 : : #include <algorithm>
27 : : #include <array>
28 : : #include <cstdint>
29 : : #include <cstdio>
30 : : #include <optional>
31 : : #include <string>
32 : : #include <vector>
33 : :
34 : : class PeerManager;
35 : :
36 : : template <typename... Callables>
37 : 40684636 : size_t CallOneOf(FuzzedDataProvider& fuzzed_data_provider, Callables... callables)
38 : : {
39 : 40684636 : constexpr size_t call_size{sizeof...(callables)};
40 : : static_assert(call_size >= 1);
41 : 40684636 : const size_t call_index{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, call_size - 1)};
42 : :
43 : : size_t i{0};
44 [ + + + + : 41076062 : ((i++ == call_index ? callables() : void()), ...);
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ][ +
+ + + # #
# # # # #
# # # # #
# # ][ + +
+ + + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ + + + +
+ + + + +
+ + + + +
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + + +
+ + + + +
+ + + + +
+ + + + +
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ ][ + + +
+ + + + +
+ + + + +
+ + + + +
+ + ][ + +
+ + + + +
+ # # # #
# # # # #
# # # #
# ][ + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
[ + + + +
+ + # # #
# # # # #
# # ][ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + ][ + +
+ + + + +
+ + + + +
# # ][ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ][ +
+ + + + +
+ + + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + ]
45 : 40678945 : return call_size;
46 : : }
47 : :
48 : : template <typename Collection>
49 [ - + ]: 13947041 : auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
50 : : {
51 : 13947041 : auto sz{col.size()};
52 [ - + ]: 13929236 : assert(sz >= 1);
53 : 13947041 : auto it = col.begin();
54 : 13947041 : std::advance(it, fuzzed_data_provider.ConsumeIntegralInRange<decltype(sz)>(0, sz - 1));
55 : 13947041 : return *it;
56 : : }
57 : :
58 : : template<typename B = uint8_t>
59 [ + + ]: 2510550 : [[nodiscard]] inline std::vector<B> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
60 : : {
61 : : static_assert(sizeof(B) == 1);
62 [ + + ]: 2510550 : const std::string s = max_length ?
63 : 322395 : fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
64 : : fuzzed_data_provider.ConsumeRandomLengthString();
65 [ - + ]: 2510550 : std::vector<B> ret(s.size());
66 [ - + ]: 2510550 : std::copy(s.begin(), s.end(), reinterpret_cast<char*>(ret.data()));
67 : 2510550 : return ret;
68 : 2510550 : }
69 : :
70 : 6698 : [[nodiscard]] inline DataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
71 : : {
72 [ - + ]: 6698 : return DataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
73 : : }
74 : :
75 : 162919 : [[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
76 : : {
77 : 162919 : const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
78 : 162919 : std::vector<std::string> r;
79 : 162919 : r.reserve(n_elements);
80 [ + + ]: 1190484 : for (size_t i = 0; i < n_elements; ++i) {
81 : 1027565 : r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length));
82 : : }
83 : 162919 : return r;
84 : : }
85 : :
86 : : template <typename T>
87 : 3193 : [[nodiscard]] inline std::vector<T> ConsumeRandomLengthIntegralVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16) noexcept
88 : : {
89 : 3193 : const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
90 : 3193 : std::vector<T> r;
91 : 3193 : r.reserve(n_elements);
92 [ + + ]: 25349 : for (size_t i = 0; i < n_elements; ++i) {
93 : 22156 : r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
94 : : }
95 : 3193 : return r;
96 : : }
97 : :
98 : : template <typename P>
99 : : [[nodiscard]] P ConsumeDeserializationParams(FuzzedDataProvider& fuzzed_data_provider) noexcept;
100 : :
101 : : template <typename T, typename P>
102 : 403732 : [[nodiscard]] std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const P& params, const std::optional<size_t>& max_length = std::nullopt) noexcept
103 : : {
104 [ - + ]: 403732 : const std::vector<uint8_t> buffer{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
105 : 403732 : SpanReader ds{buffer};
106 [ + + ]: 403732 : T obj;
107 : : try {
108 [ + + ]: 801335 : ds >> params(obj);
109 [ - + ]: 6129 : } catch (const std::ios_base::failure&) {
110 : 6129 : return std::nullopt;
111 : : }
112 : 397603 : return obj;
113 : 403732 : }
114 : :
115 : : template <typename T>
116 : 708648 : [[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
117 : : {
118 [ - + ]: 708648 : const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
119 [ + + ]: 708648 : SpanReader ds{buffer};
120 [ + + ]: 708648 : T obj;
121 : : try {
122 : 548681 : ds >> obj;
123 [ - + ]: 159967 : } catch (const std::ios_base::failure&) {
124 : 159967 : return std::nullopt;
125 : : }
126 : 487 : return obj;
127 : 708648 : }
128 : :
129 : : template <typename T>
130 : 32221 : [[nodiscard]] inline std::optional<T> ConsumeDeserializableConstructor(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
131 : : {
132 [ - + ]: 32221 : const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
133 : 32221 : SpanReader ds{buffer};
134 : : try {
135 [ + + ]: 32221 : T obj(deserialize, ds);
136 : 28179 : return obj;
137 [ - + ]: 32221 : } catch (const std::ios_base::failure&) {
138 : 4042 : return std::nullopt;
139 : : }
140 : 32221 : }
141 : :
142 : : template <typename WeakEnumType, size_t size>
143 : 3018449 : [[nodiscard]] WeakEnumType ConsumeWeakEnum(FuzzedDataProvider& fuzzed_data_provider, const WeakEnumType (&all_types)[size]) noexcept
144 : : {
145 [ + + ]: 3018449 : return fuzzed_data_provider.ConsumeBool() ?
146 : 2962967 : fuzzed_data_provider.PickValueInArray<WeakEnumType>(all_types) :
147 : 55482 : WeakEnumType(fuzzed_data_provider.ConsumeIntegral<std::underlying_type_t<WeakEnumType>>());
148 : : }
149 : :
150 : 542341 : [[nodiscard]] inline opcodetype ConsumeOpcodeType(FuzzedDataProvider& fuzzed_data_provider) noexcept
151 : : {
152 : 542341 : return static_cast<opcodetype>(fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, MAX_OPCODE));
153 : : }
154 : :
155 : : [[nodiscard]] CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::optional<CAmount>& max = std::nullopt) noexcept;
156 : :
157 : : [[nodiscard]] NodeSeconds ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional<int64_t>& min = std::nullopt, const std::optional<int64_t>& max = std::nullopt) noexcept;
158 : :
159 : : template <class Dur>
160 : : // Having the compiler infer the template argument from the function argument
161 : : // is dangerous, because the desired return value generally has a different
162 : : // type than the function argument. So std::common_type is used to force the
163 : : // call site to specify the type of the return value.
164 : 1357040 : [[nodiscard]] Dur ConsumeDuration(FuzzedDataProvider& fuzzed_data_provider, std::common_type_t<Dur> min, std::common_type_t<Dur> max) noexcept
165 : : {
166 : 1357040 : return Dur{fuzzed_data_provider.ConsumeIntegralInRange(min.count(), max.count())};
167 : : }
168 : :
169 : : [[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;
170 : :
171 : : [[nodiscard]] CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, size_t max_stack_elem_size = 32) noexcept;
172 : :
173 : : [[nodiscard]] CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, bool maybe_p2wsh = false) noexcept;
174 : :
175 : : [[nodiscard]] uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept;
176 : :
177 : 387968 : [[nodiscard]] inline CScriptNum ConsumeScriptNum(FuzzedDataProvider& fuzzed_data_provider) noexcept
178 : : {
179 [ + + + + : 387968 : return CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
+ + ]
180 : : }
181 : :
182 : 23393 : [[nodiscard]] inline uint160 ConsumeUInt160(FuzzedDataProvider& fuzzed_data_provider) noexcept
183 : : {
184 : 23393 : const std::vector<uint8_t> v160 = fuzzed_data_provider.ConsumeBytes<uint8_t>(160 / 8);
185 [ - + + + ]: 23393 : if (v160.size() != 160 / 8) {
186 : 7546 : return {};
187 : : }
188 : 15847 : return uint160{v160};
189 : 23393 : }
190 : :
191 : 229679 : [[nodiscard]] inline uint256 ConsumeUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
192 : : {
193 : 229679 : const std::vector<uint8_t> v256 = fuzzed_data_provider.ConsumeBytes<uint8_t>(256 / 8);
194 [ - + + + ]: 229679 : if (v256.size() != 256 / 8) {
195 : 7368 : return {};
196 : : }
197 : 222311 : return uint256{v256};
198 : 229679 : }
199 : :
200 : 60721 : [[nodiscard]] inline arith_uint256 ConsumeArithUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
201 : : {
202 : 60721 : return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
203 : : }
204 : :
205 : 12591 : [[nodiscard]] inline arith_uint256 ConsumeArithUInt256InRange(FuzzedDataProvider& fuzzed_data_provider, const arith_uint256& min, const arith_uint256& max) noexcept
206 : : {
207 [ - + ]: 12591 : assert(min <= max);
208 : 12591 : const arith_uint256 range = max - min;
209 : 12591 : const arith_uint256 value = ConsumeArithUInt256(fuzzed_data_provider);
210 : 12591 : arith_uint256 result = value;
211 : : // Avoid division by 0, in case range + 1 results in overflow.
212 [ + - ]: 25182 : if (range != ~arith_uint256(0)) {
213 : 12591 : const arith_uint256 quotient = value / (range + 1);
214 : 12591 : result = value - (quotient * (range + 1));
215 : : }
216 : 12591 : result += min;
217 [ + - - + ]: 12591 : assert(result >= min && result <= max);
218 : 12591 : return result;
219 : : }
220 : :
221 : : [[nodiscard]] std::map<COutPoint, Coin> ConsumeCoins(FuzzedDataProvider& fuzzed_data_provider) noexcept;
222 : :
223 : : [[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept;
224 : :
225 : : [[nodiscard]] CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional<bool> compressed = std::nullopt) noexcept;
226 : :
227 : : template <typename T>
228 : 1442 : [[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept
229 : : {
230 : : static_assert(std::is_integral_v<T>, "Integral required.");
231 : : if (std::numeric_limits<T>::is_signed) {
232 [ + + ]: 974 : if (i > 0) {
233 [ + + ]: 169 : if (j > 0) {
234 : 99 : return i > (std::numeric_limits<T>::max() / j);
235 : : } else {
236 : 70 : return j < (std::numeric_limits<T>::min() / i);
237 : : }
238 : : } else {
239 [ + + ]: 805 : if (j > 0) {
240 : 124 : return i < (std::numeric_limits<T>::min() / j);
241 : : } else {
242 [ + + + + ]: 681 : return i != 0 && (j < (std::numeric_limits<T>::max() / i));
243 : : }
244 : : }
245 : : } else {
246 [ + + + + ]: 468 : return j != 0 && i > std::numeric_limits<T>::max() / j;
247 : : }
248 : : }
249 : :
250 : : [[nodiscard]] bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept;
251 : :
252 : : /**
253 : : * Sets errno to a value selected from the given std::array `errnos`.
254 : : */
255 : : template <typename T, size_t size>
256 : 11849 : void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider, const std::array<T, size>& errnos)
257 : : {
258 : 11849 : errno = fuzzed_data_provider.PickValueInArray(errnos);
259 : 11849 : }
260 : :
261 : : /*
262 : : * Sets a fuzzed errno in the range [0, 133 (EHWPOISON)]. Can be used from functions emulating
263 : : * standard library functions that set errno, or in other contexts where the value of errno
264 : : * might be relevant for the execution path that will be taken.
265 : : */
266 : 50482 : inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept
267 : : {
268 : 50482 : errno = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 133);
269 : 50482 : }
270 : :
271 : : /**
272 : : * Returns a byte vector of specified size regardless of the number of remaining bytes available
273 : : * from the fuzzer. Pads with zero value bytes if needed to achieve the specified size.
274 : : */
275 : : template<typename B = uint8_t>
276 : 220381 : [[nodiscard]] inline std::vector<B> ConsumeFixedLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t length) noexcept
277 : : {
278 : : static_assert(sizeof(B) == 1);
279 : 220381 : auto random_bytes = fuzzed_data_provider.ConsumeBytes<B>(length);
280 : 220381 : random_bytes.resize(length);
281 : 220381 : return random_bytes;
282 : : }
283 : :
284 : : class FuzzedFileProvider
285 : : {
286 : : FuzzedDataProvider& m_fuzzed_data_provider;
287 : : int64_t m_offset = 0;
288 : :
289 : : public:
290 [ + - ]: 3647 : FuzzedFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
291 : : {
292 : : }
293 : :
294 : : FILE* open();
295 : :
296 : : static ssize_t read(void* cookie, char* buf, size_t size);
297 : :
298 : : static ssize_t write(void* cookie, const char* buf, size_t size);
299 : :
300 : : static int seek(void* cookie, int64_t* offset, int whence);
301 : :
302 : : static int close(void* cookie);
303 : : };
304 : :
305 : : #define WRITE_TO_STREAM_CASE(type, consume) \
306 : : [&] { \
307 : : type o = consume; \
308 : : stream << o; \
309 : : }
310 : : template <typename Stream>
311 : 2155 : void WriteToStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
312 : : {
313 [ + + ]: 194868 : while (fuzzed_data_provider.ConsumeBool()) {
314 : : try {
315 [ + + ]: 193694 : CallOneOf(
316 : : fuzzed_data_provider,
317 : 2074 : WRITE_TO_STREAM_CASE(bool, fuzzed_data_provider.ConsumeBool()),
318 : 142018 : WRITE_TO_STREAM_CASE(int8_t, fuzzed_data_provider.ConsumeIntegral<int8_t>()),
319 : 2771 : WRITE_TO_STREAM_CASE(uint8_t, fuzzed_data_provider.ConsumeIntegral<uint8_t>()),
320 : 2366 : WRITE_TO_STREAM_CASE(int16_t, fuzzed_data_provider.ConsumeIntegral<int16_t>()),
321 : 780 : WRITE_TO_STREAM_CASE(uint16_t, fuzzed_data_provider.ConsumeIntegral<uint16_t>()),
322 : 2393 : WRITE_TO_STREAM_CASE(int32_t, fuzzed_data_provider.ConsumeIntegral<int32_t>()),
323 : 625 : WRITE_TO_STREAM_CASE(uint32_t, fuzzed_data_provider.ConsumeIntegral<uint32_t>()),
324 : 1356 : WRITE_TO_STREAM_CASE(int64_t, fuzzed_data_provider.ConsumeIntegral<int64_t>()),
325 : 4538 : WRITE_TO_STREAM_CASE(uint64_t, fuzzed_data_provider.ConsumeIntegral<uint64_t>()),
326 [ + + ]: 64613 : WRITE_TO_STREAM_CASE(std::string, fuzzed_data_provider.ConsumeRandomLengthString(32)),
327 [ + + ]: 4244 : WRITE_TO_STREAM_CASE(std::vector<uint8_t>, ConsumeRandomLengthIntegralVector<uint8_t>(fuzzed_data_provider)));
328 [ - + ]: 981 : } catch (const std::ios_base::failure&) {
329 : : break;
330 : : }
331 : : }
332 : 2155 : }
333 : :
334 : : #define READ_FROM_STREAM_CASE(type) \
335 : : [&] { \
336 : : type o; \
337 : : stream >> o; \
338 : : }
339 : : template <typename Stream>
340 : 5625 : void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
341 : : {
342 [ + + ]: 52593 : while (fuzzed_data_provider.ConsumeBool()) {
343 : : try {
344 [ + + ]: 51419 : CallOneOf(
345 : : fuzzed_data_provider,
346 : 1889 : READ_FROM_STREAM_CASE(bool),
347 : 2725 : READ_FROM_STREAM_CASE(int8_t),
348 : 2568 : READ_FROM_STREAM_CASE(uint8_t),
349 : 1627 : READ_FROM_STREAM_CASE(int16_t),
350 : 1235 : READ_FROM_STREAM_CASE(uint16_t),
351 : 5600 : READ_FROM_STREAM_CASE(int32_t),
352 : 3543 : READ_FROM_STREAM_CASE(uint32_t),
353 : 6556 : READ_FROM_STREAM_CASE(int64_t),
354 : 2663 : READ_FROM_STREAM_CASE(uint64_t),
355 [ + + ]: 15201 : READ_FROM_STREAM_CASE(std::string),
356 [ + + ]: 27277 : READ_FROM_STREAM_CASE(std::vector<uint8_t>));
357 [ - + ]: 4451 : } catch (const std::ios_base::failure&) {
358 : : break;
359 : : }
360 : : }
361 : 5625 : }
362 : :
363 : 174714 : inline void FinalizeHeader(CBlockHeader& header, const ChainstateManager& chainman)
364 : : {
365 [ + + ]: 347561 : while (!CheckProofOfWork(header.GetHash(), header.nBits, chainman.GetParams().GetConsensus())) {
366 : 172847 : ++(header.nNonce);
367 : : }
368 : 174714 : }
369 : :
370 : : #endif // BITCOIN_TEST_FUZZ_UTIL_H
|