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 <primitives/transaction.h>
17 : : #include <script/script.h>
18 : : #include <serialize.h>
19 : : #include <streams.h>
20 : : #include <test/fuzz/FuzzedDataProvider.h>
21 : : #include <test/fuzz/fuzz.h>
22 : : #include <uint256.h>
23 : :
24 : : #include <algorithm>
25 : : #include <array>
26 : : #include <cstdint>
27 : : #include <cstdio>
28 : : #include <optional>
29 : : #include <string>
30 : : #include <vector>
31 : :
32 : : class PeerManager;
33 : :
34 : : template <typename... Callables>
35 : 66007370 : size_t CallOneOf(FuzzedDataProvider& fuzzed_data_provider, Callables... callables)
36 : : {
37 : 66007370 : constexpr size_t call_size{sizeof...(callables)};
38 : : static_assert(call_size >= 1);
39 : 66007370 : const size_t call_index{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, call_size - 1)};
40 : :
41 : : size_t i{0};
42 [ + + + + : 66616683 : ((i++ == call_index ? callables() : void()), ...);
+ + + + +
+ + + + +
+ + + + ]
[ + + + +
+ + + + +
+ ][ + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + # # #
# # # # #
# # ][ + +
+ + + + +
+ + + + +
+ + + + #
# # # # #
# # # # #
# # # # #
# # ][ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ][ + + +
+ # # # #
# # # # #
# # # # #
# # ][ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + + +
+ + + + +
+ + + + +
+ + + + +
+ + # # ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + ][ +
+ + + + +
+ + + + +
+ # # # #
# # # # #
# # # ][ +
+ + + + +
+ + ][ + +
+ + + + #
# # # # #
# # ][ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + ][ + +
+ + + + +
+ + + + +
+ + # # ]
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
# # # # #
# ]
43 : 65995391 : return call_size;
44 : : }
45 : :
46 : : template <typename Collection>
47 [ - + ]: 21263413 : auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
48 : : {
49 : 21263413 : auto sz{col.size()};
50 [ - + ]: 21233931 : assert(sz >= 1);
51 : 21263413 : auto it = col.begin();
52 : 21263413 : std::advance(it, fuzzed_data_provider.ConsumeIntegralInRange<decltype(sz)>(0, sz - 1));
53 : 21263413 : return *it;
54 : : }
55 : :
56 : : template<typename B = uint8_t>
57 [ + + ]: 4192802 : [[nodiscard]] inline std::vector<B> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
58 : : {
59 : : static_assert(sizeof(B) == 1);
60 [ + + ]: 4192802 : const std::string s = max_length ?
61 : 587401 : fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
62 : : fuzzed_data_provider.ConsumeRandomLengthString();
63 [ - + ]: 4192802 : std::vector<B> ret(s.size());
64 [ - + ]: 4192802 : std::copy(s.begin(), s.end(), reinterpret_cast<char*>(ret.data()));
65 : 4192802 : return ret;
66 : 4192802 : }
67 : :
68 : 9760 : [[nodiscard]] inline DataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
69 : : {
70 [ - + ]: 9760 : return DataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
71 : : }
72 : :
73 : 509336 : [[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
74 : : {
75 : 509336 : const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
76 : 509336 : std::vector<std::string> r;
77 : 509336 : r.reserve(n_elements);
78 [ + + ]: 3289018 : for (size_t i = 0; i < n_elements; ++i) {
79 : 2779682 : r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length));
80 : : }
81 : 509336 : return r;
82 : : }
83 : :
84 : : template <typename T>
85 : 4866 : [[nodiscard]] inline std::vector<T> ConsumeRandomLengthIntegralVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16) noexcept
86 : : {
87 : 4866 : const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
88 : 4866 : std::vector<T> r;
89 : 4866 : r.reserve(n_elements);
90 [ + + ]: 36356 : for (size_t i = 0; i < n_elements; ++i) {
91 : 31490 : r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
92 : : }
93 : 4866 : return r;
94 : : }
95 : :
96 : : template <typename P>
97 : : [[nodiscard]] P ConsumeDeserializationParams(FuzzedDataProvider& fuzzed_data_provider) noexcept;
98 : :
99 : : template <typename T, typename P>
100 : 482830 : [[nodiscard]] std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const P& params, const std::optional<size_t>& max_length = std::nullopt) noexcept
101 : : {
102 [ - + ]: 482830 : const std::vector<uint8_t> buffer{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
103 : 482830 : SpanReader ds{buffer};
104 [ + + ]: 482830 : T obj;
105 : : try {
106 [ + + ]: 956392 : ds >> params(obj);
107 [ - + ]: 9268 : } catch (const std::ios_base::failure&) {
108 : 9268 : return std::nullopt;
109 : : }
110 : 473562 : return obj;
111 : 482830 : }
112 : :
113 : : template <typename T>
114 : 1496559 : [[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
115 : : {
116 [ - + ]: 1496559 : const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
117 [ + + ]: 1496559 : SpanReader ds{buffer};
118 [ + + ]: 1496559 : T obj;
119 : : try {
120 : 1136454 : ds >> obj;
121 [ - + ]: 360105 : } catch (const std::ios_base::failure&) {
122 : 360105 : return std::nullopt;
123 : : }
124 : 1136454 : return obj;
125 : 1496559 : }
126 : :
127 : : template <typename WeakEnumType, size_t size>
128 : 4977418 : [[nodiscard]] WeakEnumType ConsumeWeakEnum(FuzzedDataProvider& fuzzed_data_provider, const WeakEnumType (&all_types)[size]) noexcept
129 : : {
130 [ + + ]: 4977418 : return fuzzed_data_provider.ConsumeBool() ?
131 : 4876131 : fuzzed_data_provider.PickValueInArray<WeakEnumType>(all_types) :
132 : 101287 : WeakEnumType(fuzzed_data_provider.ConsumeIntegral<std::underlying_type_t<WeakEnumType>>());
133 : : }
134 : :
135 : 1032572 : [[nodiscard]] inline opcodetype ConsumeOpcodeType(FuzzedDataProvider& fuzzed_data_provider) noexcept
136 : : {
137 : 1032572 : return static_cast<opcodetype>(fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, MAX_OPCODE));
138 : : }
139 : :
140 : : [[nodiscard]] CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::optional<CAmount>& max = std::nullopt) noexcept;
141 : :
142 : : [[nodiscard]] NodeSeconds ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional<int64_t>& min = std::nullopt, const std::optional<int64_t>& max = std::nullopt) noexcept;
143 : : [[nodiscard]] std::chrono::seconds ConsumeDuration(FuzzedDataProvider& fuzzed_data_provider, std::chrono::seconds min, std::chrono::seconds max) noexcept;
144 : :
145 : : [[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;
146 : :
147 : : [[nodiscard]] CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, size_t max_stack_elem_size = 32) noexcept;
148 : :
149 : : [[nodiscard]] CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, bool maybe_p2wsh = false) noexcept;
150 : :
151 : : [[nodiscard]] uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept;
152 : :
153 : 620298 : [[nodiscard]] inline CScriptNum ConsumeScriptNum(FuzzedDataProvider& fuzzed_data_provider) noexcept
154 : : {
155 [ + + + + : 620298 : return CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
+ + ]
156 : : }
157 : :
158 : 37349 : [[nodiscard]] inline uint160 ConsumeUInt160(FuzzedDataProvider& fuzzed_data_provider) noexcept
159 : : {
160 : 37349 : const std::vector<uint8_t> v160 = fuzzed_data_provider.ConsumeBytes<uint8_t>(160 / 8);
161 [ - + + + ]: 37349 : if (v160.size() != 160 / 8) {
162 : 10208 : return {};
163 : : }
164 : 27141 : return uint160{v160};
165 : 37349 : }
166 : :
167 : 388904 : [[nodiscard]] inline uint256 ConsumeUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
168 : : {
169 : 388904 : const std::vector<uint8_t> v256 = fuzzed_data_provider.ConsumeBytes<uint8_t>(256 / 8);
170 [ - + + + ]: 388904 : if (v256.size() != 256 / 8) {
171 : 11322 : return {};
172 : : }
173 : 377582 : return uint256{v256};
174 : 388904 : }
175 : :
176 : 137531 : [[nodiscard]] inline arith_uint256 ConsumeArithUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
177 : : {
178 : 137531 : return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
179 : : }
180 : :
181 : 20871 : [[nodiscard]] inline arith_uint256 ConsumeArithUInt256InRange(FuzzedDataProvider& fuzzed_data_provider, const arith_uint256& min, const arith_uint256& max) noexcept
182 : : {
183 [ - + ]: 20871 : assert(min <= max);
184 : 20871 : const arith_uint256 range = max - min;
185 : 20871 : const arith_uint256 value = ConsumeArithUInt256(fuzzed_data_provider);
186 : 20871 : arith_uint256 result = value;
187 : : // Avoid division by 0, in case range + 1 results in overflow.
188 [ + - ]: 41742 : if (range != ~arith_uint256(0)) {
189 : 20871 : const arith_uint256 quotient = value / (range + 1);
190 : 20871 : result = value - (quotient * (range + 1));
191 : : }
192 : 20871 : result += min;
193 [ + - - + ]: 20871 : assert(result >= min && result <= max);
194 : 20871 : return result;
195 : : }
196 : :
197 : : [[nodiscard]] std::map<COutPoint, Coin> ConsumeCoins(FuzzedDataProvider& fuzzed_data_provider) noexcept;
198 : :
199 : : [[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept;
200 : :
201 : : [[nodiscard]] CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional<bool> compressed = std::nullopt) noexcept;
202 : :
203 : : template <typename T>
204 : 2766 : [[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept
205 : : {
206 : : static_assert(std::is_integral_v<T>, "Integral required.");
207 : : if (std::numeric_limits<T>::is_signed) {
208 [ + + ]: 1766 : if (i > 0) {
209 [ + + ]: 317 : if (j > 0) {
210 : 180 : return i > (std::numeric_limits<T>::max() / j);
211 : : } else {
212 : 137 : return j < (std::numeric_limits<T>::min() / i);
213 : : }
214 : : } else {
215 [ + + ]: 1449 : if (j > 0) {
216 : 234 : return i < (std::numeric_limits<T>::min() / j);
217 : : } else {
218 [ + + + + ]: 1215 : return i != 0 && (j < (std::numeric_limits<T>::max() / i));
219 : : }
220 : : }
221 : : } else {
222 [ + + + + ]: 1000 : return j != 0 && i > std::numeric_limits<T>::max() / j;
223 : : }
224 : : }
225 : :
226 : : [[nodiscard]] bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept;
227 : :
228 : : /**
229 : : * Sets errno to a value selected from the given std::array `errnos`.
230 : : */
231 : : template <typename T, size_t size>
232 : 113330 : void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider, const std::array<T, size>& errnos)
233 : : {
234 : 113330 : errno = fuzzed_data_provider.PickValueInArray(errnos);
235 : 113330 : }
236 : :
237 : : /*
238 : : * Sets a fuzzed errno in the range [0, 133 (EHWPOISON)]. Can be used from functions emulating
239 : : * standard library functions that set errno, or in other contexts where the value of errno
240 : : * might be relevant for the execution path that will be taken.
241 : : */
242 : 80340 : inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept
243 : : {
244 : 80340 : errno = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 133);
245 : 80340 : }
246 : :
247 : : /**
248 : : * Returns a byte vector of specified size regardless of the number of remaining bytes available
249 : : * from the fuzzer. Pads with zero value bytes if needed to achieve the specified size.
250 : : */
251 : : template<typename B = uint8_t>
252 : 251546 : [[nodiscard]] inline std::vector<B> ConsumeFixedLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t length) noexcept
253 : : {
254 : : static_assert(sizeof(B) == 1);
255 : 251546 : auto random_bytes = fuzzed_data_provider.ConsumeBytes<B>(length);
256 : 251546 : random_bytes.resize(length);
257 : 251546 : return random_bytes;
258 : : }
259 : :
260 : : class FuzzedFileProvider
261 : : {
262 : : FuzzedDataProvider& m_fuzzed_data_provider;
263 : : int64_t m_offset = 0;
264 : :
265 : : public:
266 [ + - ]: 5893 : FuzzedFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
267 : : {
268 : : }
269 : :
270 : : FILE* open();
271 : :
272 : : static ssize_t read(void* cookie, char* buf, size_t size);
273 : :
274 : : static ssize_t write(void* cookie, const char* buf, size_t size);
275 : :
276 : : static int seek(void* cookie, int64_t* offset, int whence);
277 : :
278 : : static int close(void* cookie);
279 : : };
280 : :
281 : : #define WRITE_TO_STREAM_CASE(type, consume) \
282 : : [&] { \
283 : : type o = consume; \
284 : : stream << o; \
285 : : }
286 : : template <typename Stream>
287 : 4433 : void WriteToStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
288 : : {
289 [ + + ]: 584131 : while (fuzzed_data_provider.ConsumeBool()) {
290 : : try {
291 [ + + ]: 581734 : CallOneOf(
292 : : fuzzed_data_provider,
293 : 5156 : WRITE_TO_STREAM_CASE(bool, fuzzed_data_provider.ConsumeBool()),
294 : 406682 : WRITE_TO_STREAM_CASE(int8_t, fuzzed_data_provider.ConsumeIntegral<int8_t>()),
295 : 7206 : WRITE_TO_STREAM_CASE(uint8_t, fuzzed_data_provider.ConsumeIntegral<uint8_t>()),
296 : 5791 : WRITE_TO_STREAM_CASE(int16_t, fuzzed_data_provider.ConsumeIntegral<int16_t>()),
297 : 1590 : WRITE_TO_STREAM_CASE(uint16_t, fuzzed_data_provider.ConsumeIntegral<uint16_t>()),
298 : 6955 : WRITE_TO_STREAM_CASE(int32_t, fuzzed_data_provider.ConsumeIntegral<int32_t>()),
299 : 992 : WRITE_TO_STREAM_CASE(uint32_t, fuzzed_data_provider.ConsumeIntegral<uint32_t>()),
300 : 2822 : WRITE_TO_STREAM_CASE(int64_t, fuzzed_data_provider.ConsumeIntegral<int64_t>()),
301 : 12844 : WRITE_TO_STREAM_CASE(uint64_t, fuzzed_data_provider.ConsumeIntegral<uint64_t>()),
302 [ + + ]: 254662 : WRITE_TO_STREAM_CASE(std::string, fuzzed_data_provider.ConsumeRandomLengthString(32)),
303 [ + + ]: 7282 : WRITE_TO_STREAM_CASE(std::vector<uint8_t>, ConsumeRandomLengthIntegralVector<uint8_t>(fuzzed_data_provider)));
304 [ - + ]: 2036 : } catch (const std::ios_base::failure&) {
305 : : break;
306 : : }
307 : : }
308 : 4433 : }
309 : :
310 : : #define READ_FROM_STREAM_CASE(type) \
311 : : [&] { \
312 : : type o; \
313 : : stream >> o; \
314 : : }
315 : : template <typename Stream>
316 : 12445 : void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
317 : : {
318 [ + + ]: 111403 : while (fuzzed_data_provider.ConsumeBool()) {
319 : : try {
320 [ + + ]: 108532 : CallOneOf(
321 : : fuzzed_data_provider,
322 : 5454 : READ_FROM_STREAM_CASE(bool),
323 : 3338 : READ_FROM_STREAM_CASE(int8_t),
324 : 5316 : READ_FROM_STREAM_CASE(uint8_t),
325 : 3599 : READ_FROM_STREAM_CASE(int16_t),
326 : 2586 : READ_FROM_STREAM_CASE(uint16_t),
327 : 11888 : READ_FROM_STREAM_CASE(int32_t),
328 : 7061 : READ_FROM_STREAM_CASE(uint32_t),
329 : 14489 : READ_FROM_STREAM_CASE(int64_t),
330 : 5491 : READ_FROM_STREAM_CASE(uint64_t),
331 [ + + ]: 34396 : READ_FROM_STREAM_CASE(std::string),
332 [ + + ]: 56603 : READ_FROM_STREAM_CASE(std::vector<uint8_t>));
333 [ - + ]: 9574 : } catch (const std::ios_base::failure&) {
334 : : break;
335 : : }
336 : : }
337 : 12445 : }
338 : :
339 : : #endif // BITCOIN_TEST_FUZZ_UTIL_H
|