5#ifndef BITCOIN_TEST_FUZZ_UTIL_H
6#define BITCOIN_TEST_FUZZ_UTIL_H
34template <
typename... Callables>
37 constexpr size_t call_size{
sizeof...(callables)};
38 static_assert(call_size >= 1);
42 ((i++ == call_index ? callables() : void()), ...);
46template <
typename Collection>
51 auto it = col.begin();
56template<
typename B = u
int8_t>
59 static_assert(
sizeof(
B) == 1);
60 const std::string
s = max_length ?
63 std::vector<B>
ret(
s.size());
64 std::copy(
s.begin(),
s.end(),
reinterpret_cast<char*
>(
ret.data()));
81 std::vector<std::string> r;
82 r.reserve(n_elements);
83 for (
size_t i = 0; i < n_elements; ++i) {
94 r.reserve(n_elements);
95 for (
size_t i = 0; i < n_elements; ++i) {
104template <
typename T,
typename P>
112 }
catch (
const std::ios_base::failure&) {
126 }
catch (
const std::ios_base::failure&) {
132template <
typename WeakEnumType,
size_t size>
166 if (v160.size() != 160 / 8) {
175 if (v256.size() != 256 / 8) {
195 result = value - (quotient * (range + 1));
198 assert(result >= min && result <= max);
211 static_assert(std::is_integral_v<T>,
"Integral required.");
212 if (std::numeric_limits<T>::is_signed) {
215 return i > (std::numeric_limits<T>::max() / j);
217 return j < (std::numeric_limits<T>::min() / i);
221 return i < (std::numeric_limits<T>::min() / j);
223 return i != 0 && (j < (std::numeric_limits<T>::max() / i));
227 return j != 0 && i > std::numeric_limits<T>::max() / j;
236template <
typename T,
size_t size>
256template<
typename B = u
int8_t>
259 static_assert(
sizeof(
B) == 1);
261 random_bytes.resize(length);
277 static ssize_t
read(
void* cookie,
char* buf,
size_t size);
279 static ssize_t
write(
void* cookie,
const char* buf,
size_t size);
281 static int seek(
void* cookie, int64_t* offset,
int whence);
283 static int close(
void* cookie);
286#define WRITE_TO_STREAM_CASE(type, consume) \
291template <
typename Stream>
309 }
catch (
const std::ios_base::failure&) {
315#define READ_FROM_STREAM_CASE(type) \
320template <
typename Stream>
338 }
catch (
const std::ios_base::failure&) {
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
int64_t CAmount
Amount in satoshis (Can be negative)
arith_uint256 UintToArith256(const uint256 &a)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
An encapsulated private key.
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
Double ended buffer combining vector and stream-like interfaces.
std::string ConsumeRandomLengthString(size_t max_length)
std::vector< T > ConsumeBytes(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
static ssize_t write(void *cookie, const char *buf, size_t size)
FuzzedDataProvider & m_fuzzed_data_provider
static int seek(void *cookie, int64_t *offset, int whence)
static int close(void *cookie)
FuzzedFileProvider(FuzzedDataProvider &fuzzed_data_provider)
static ssize_t read(void *cookie, char *buf, size_t size)
256-bit unsigned big integer.
std::vector< bool > BytesToBits(const std::vector< unsigned char > &bytes)
static const unsigned int MAX_OPCODE
opcodetype
Script opcodes.
A mutable version of CTransaction.
CScriptNum ConsumeScriptNum(FuzzedDataProvider &fuzzed_data_provider) noexcept
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
DataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min=std::nullopt, const std::optional< int64_t > &max=std::nullopt) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
void ReadFromStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
std::vector< bool > ConsumeRandomLengthBitVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
P ConsumeDeserializationParams(FuzzedDataProvider &fuzzed_data_provider) noexcept
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...
bool ContainsSpentInput(const CTransaction &tx, const CCoinsViewCache &inputs) noexcept
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
#define READ_FROM_STREAM_CASE(type)
arith_uint256 ConsumeArithUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
std::vector< T > ConsumeRandomLengthIntegralVector(FuzzedDataProvider &fuzzed_data_provider, const size_t max_vector_size=16) noexcept
CScriptWitness ConsumeScriptWitness(FuzzedDataProvider &fuzzed_data_provider, size_t max_stack_elem_size=32) noexcept
void WriteToStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, bool maybe_p2wsh=false) noexcept
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P ¶ms, const std::optional< size_t > &max_length=std::nullopt) noexcept
std::vector< std::string > ConsumeRandomLengthStringVector(FuzzedDataProvider &fuzzed_data_provider, const size_t max_vector_size=16, const size_t max_string_length=16) noexcept
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
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed=std::nullopt) noexcept
bool MultiplicationOverflow(const T i, const T j) noexcept
opcodetype ConsumeOpcodeType(FuzzedDataProvider &fuzzed_data_provider) noexcept
void SetFuzzedErrNo(FuzzedDataProvider &fuzzed_data_provider, const std::array< T, size > &errnos)
Sets errno to a value selected from the given std::array errnos.
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
#define WRITE_TO_STREAM_CASE(type, consume)
CTxDestination ConsumeTxDestination(FuzzedDataProvider &fuzzed_data_provider) noexcept
arith_uint256 ConsumeArithUInt256InRange(FuzzedDataProvider &fuzzed_data_provider, const arith_uint256 &min, const arith_uint256 &max) noexcept
std::map< COutPoint, Coin > ConsumeCoins(FuzzedDataProvider &fuzzed_data_provider) noexcept
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max=std::nullopt) noexcept
std::chrono::seconds ConsumeDuration(FuzzedDataProvider &fuzzed_data_provider, std::chrono::seconds min, std::chrono::seconds max) noexcept
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
FuzzedDataProvider & fuzzed_data_provider