5 #ifndef BITCOIN_TEST_FUZZ_UTIL_H 6 #define BITCOIN_TEST_FUZZ_UTIL_H 35 template <
typename... Callables>
38 constexpr
size_t call_size{
sizeof...(callables)};
39 static_assert(call_size >= 1);
43 ((i++ == call_index ? callables() : void()), ...);
47 template <
typename Collection>
52 auto it = col.begin();
57 template<
typename B = u
int8_t>
60 static_assert(
sizeof(
B) == 1);
61 const std::string s = max_length ?
62 fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
63 fuzzed_data_provider.ConsumeRandomLengthString();
64 std::vector<B>
ret(s.size());
65 std::copy(s.begin(), s.end(),
reinterpret_cast<char*
>(
ret.data()));
82 std::vector<std::string> r;
83 for (
size_t i = 0; i < n_elements; ++i) {
94 for (
size_t i = 0; i < n_elements; ++i) {
100 template <
typename P>
103 template <
typename T,
typename P>
111 }
catch (
const std::ios_base::failure&) {
117 template <
typename T>
125 }
catch (
const std::ios_base::failure&) {
131 template <
typename WeakEnumType,
size_t size>
136 WeakEnumType(fuzzed_data_provider.
ConsumeIntegral<
typename std::underlying_type<WeakEnumType>::type>());
146 [[nodiscard]] int64_t
ConsumeTime(
FuzzedDataProvider& fuzzed_data_provider,
const std::optional<int64_t>& min = std::nullopt,
const std::optional<int64_t>& max = std::nullopt) noexcept;
158 return CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
163 const std::vector<uint8_t> v160 = fuzzed_data_provider.
ConsumeBytes<uint8_t>(160 / 8);
164 if (v160.size() != 160 / 8) {
172 const std::vector<uint8_t> v256 = fuzzed_data_provider.
ConsumeBytes<uint8_t>(256 / 8);
173 if (v256.size() != 256 / 8) {
188 template <
typename T>
191 static_assert(std::is_integral<T>::value,
"Integral required.");
192 if (std::numeric_limits<T>::is_signed) {
195 return i > (std::numeric_limits<T>::max() / j);
197 return j < (std::numeric_limits<T>::min() / i);
201 return i < (std::numeric_limits<T>::min() / j);
203 return i != 0 && (j < (std::numeric_limits<T>::max() / i));
207 return j != 0 && i > std::numeric_limits<T>::max() / j;
216 template <
typename T,
size_t size>
236 template<
typename B = u
int8_t>
239 static_assert(
sizeof(
B) == 1);
240 auto random_bytes = fuzzed_data_provider.
ConsumeBytes<
B>(length);
241 random_bytes.resize(length);
257 static ssize_t
read(
void* cookie,
char* buf,
size_t size);
259 static ssize_t
write(
void* cookie,
const char* buf,
size_t size);
261 static int seek(
void* cookie, int64_t* offset,
int whence);
263 static int close(
void* cookie);
268 return {fuzzed_data_provider};
288 return {fuzzed_data_provider};
291 #define WRITE_TO_STREAM_CASE(type, consume) \ 296 template <
typename Stream>
302 fuzzed_data_provider,
313 WRITE_TO_STREAM_CASE(std::vector<uint8_t>, ConsumeRandomLengthIntegralVector<uint8_t>(fuzzed_data_provider)));
314 }
catch (
const std::ios_base::failure&) {
320 #define READ_FROM_STREAM_CASE(type) \ 325 template <
typename Stream>
331 fuzzed_data_provider,
343 }
catch (
const std::ios_base::failure&) {
349 #endif // BITCOIN_TEST_FUZZ_UTIL_H CMutableTransaction ConsumeTransaction(FuzzedDataProvider &fuzzed_data_provider, const std::optional< std::vector< uint256 >> &prevout_txids, const int max_num_in=10, const int max_num_out=10) noexcept
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min=std::nullopt, const std::optional< int64_t > &max=std::nullopt) noexcept
FuzzedFileProvider(FuzzedDataProvider &fuzzed_data_provider)
std::vector< B > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
std::vector< std::string > ConsumeRandomLengthStringVector(FuzzedDataProvider &fuzzed_data_provider, const size_t max_vector_size=16, const size_t max_string_length=16) noexcept
CTxDestination ConsumeTxDestination(FuzzedDataProvider &fuzzed_data_provider) noexcept
void ReadFromStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
static int seek(void *cookie, int64_t *offset, int whence)
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P ¶ms, const std::optional< size_t > &max_length=std::nullopt) noexcept
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
bool ContainsSpentInput(const CTransaction &tx, const CCoinsViewCache &inputs) noexcept
opcodetype ConsumeOpcodeType(FuzzedDataProvider &fuzzed_data_provider) noexcept
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
FuzzedDataProvider & m_fuzzed_data_provider
P ConsumeDeserializationParams(FuzzedDataProvider &fuzzed_data_provider) noexcept
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed=std::nullopt) noexcept
Non-refcounted RAII wrapper for FILE*.
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
arith_uint256 UintToArith256(const uint256 &a)
static int close(void *cookie)
std::vector< T > ConsumeRandomLengthIntegralVector(FuzzedDataProvider &fuzzed_data_provider, const size_t max_vector_size=16) noexcept
int64_t CAmount
Amount in satoshis (Can be negative)
bool MultiplicationOverflow(const T i, const T j) noexcept
FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
DataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
FuzzedFileProvider ConsumeFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Double ended buffer combining vector and stream-like interfaces.
void WriteToStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
std::string ConsumeRandomLengthString(size_t max_length)
opcodetype
Script opcodes.
CScriptWitness ConsumeScriptWitness(FuzzedDataProvider &fuzzed_data_provider, const size_t max_stack_elem_size=32) noexcept
std::vector< bool > ConsumeRandomLengthBitVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh=false) noexcept
static auto WithParams(const Params ¶ms, T &&t)
Return a wrapper around t that (de)serializes it with specified parameter params. ...
std::vector< bool > BytesToBits(const std::vector< unsigned char > &bytes)
256-bit unsigned big integer.
#define READ_FROM_STREAM_CASE(type)
std::vector< T > ConsumeBytes(size_t num_bytes)
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
static ssize_t write(void *cookie, const char *buf, size_t size)
static ssize_t read(void *cookie, char *buf, size_t size)
Serialized script, used inside transaction inputs and outputs.
FuzzedAutoFileProvider(FuzzedDataProvider &fuzzed_data_provider)
CScriptNum ConsumeScriptNum(FuzzedDataProvider &fuzzed_data_provider) noexcept
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max=std::nullopt) noexcept
#define WRITE_TO_STREAM_CASE(type, consume)
void SetFuzzedErrNo(FuzzedDataProvider &fuzzed_data_provider, const std::array< T, size > &errnos)
Sets errno to a value selected from the given std::array errnos.
A mutable version of CTransaction.
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
An encapsulated private key.
The basic transaction that is broadcasted on the network and contained in blocks. ...
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
T ConsumeIntegralInRange(T min, T max)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
static const unsigned int MAX_OPCODE
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
T PickValueInArray(const T(&array)[size])
arith_uint256 ConsumeArithUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
FuzzedFileProvider m_fuzzed_file_provider