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