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();
59 const std::string s = max_length ?
60 fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
61 fuzzed_data_provider.ConsumeRandomLengthString();
62 return {s.begin(), s.end()};
78 std::vector<std::string> r;
79 for (
size_t i = 0; i < n_elements; ++i) {
90 for (
size_t i = 0; i < n_elements; ++i) {
104 }
catch (
const std::ios_base::failure&) {
110 template <
typename WeakEnumType,
size_t size>
115 WeakEnumType(fuzzed_data_provider.
ConsumeIntegral<
typename std::underlying_type<WeakEnumType>::type>());
125 [[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;
142 const std::vector<uint8_t> v160 = fuzzed_data_provider.
ConsumeBytes<uint8_t>(160 / 8);
143 if (v160.size() != 160 / 8) {
151 const std::vector<uint8_t> v256 = fuzzed_data_provider.
ConsumeBytes<uint8_t>(256 / 8);
152 if (v256.size() != 256 / 8) {
165 template <
typename T>
168 static_assert(std::is_integral<T>::value,
"Integral required.");
169 if (std::numeric_limits<T>::is_signed) {
172 return i > (std::numeric_limits<T>::max() / j);
174 return j < (std::numeric_limits<T>::min() / i);
178 return i < (std::numeric_limits<T>::min() / j);
180 return i != 0 && (j < (std::numeric_limits<T>::max() / i));
184 return j != 0 && i > std::numeric_limits<T>::max() / j;
193 template <
typename T,
size_t size>
215 std::vector<uint8_t> result(length);
216 const std::vector<uint8_t> random_bytes = fuzzed_data_provider.
ConsumeBytes<uint8_t>(length);
217 if (!random_bytes.empty()) {
218 std::memcpy(result.data(), random_bytes.data(), random_bytes.size());
235 static ssize_t
read(
void* cookie,
char* buf,
size_t size);
237 static ssize_t
write(
void* cookie,
const char* buf,
size_t size);
239 static int seek(
void* cookie, int64_t* offset,
int whence);
241 static int close(
void* cookie);
246 return {fuzzed_data_provider};
266 return {fuzzed_data_provider};
269 #define WRITE_TO_STREAM_CASE(type, consume) \
274 template <
typename Stream>
280 fuzzed_data_provider,
291 WRITE_TO_STREAM_CASE(std::vector<uint8_t>, ConsumeRandomLengthIntegralVector<uint8_t>(fuzzed_data_provider)));
292 }
catch (
const std::ios_base::failure&) {
298 #define READ_FROM_STREAM_CASE(type) \
303 template <
typename Stream>
309 fuzzed_data_provider,
321 }
catch (
const std::ios_base::failure&) {
int64_t CAmount
Amount in satoshis (Can be negative)
arith_uint256 UintToArith256(const uint256 &a)
Non-refcounted RAII wrapper for FILE*.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
FuzzedFileProvider m_fuzzed_file_provider
FuzzedAutoFileProvider(FuzzedDataProvider &fuzzed_data_provider)
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.
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
A mutable version of CTransaction.
CScriptNum ConsumeScriptNum(FuzzedDataProvider &fuzzed_data_provider) noexcept
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
std::vector< uint8_t > 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...
void ReadFromStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
CScriptWitness ConsumeScriptWitness(FuzzedDataProvider &fuzzed_data_provider, const size_t max_stack_elem_size=32) noexcept
CDataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
std::vector< uint8_t > 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
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
bool ContainsSpentInput(const CTransaction &tx, const CCoinsViewCache &inputs) noexcept
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) 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
#define READ_FROM_STREAM_CASE(type)
arith_uint256 ConsumeArithUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh=false) noexcept
void WriteToStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) 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
bool MultiplicationOverflow(const T i, const T j) noexcept
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
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
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max=std::nullopt) noexcept
std::vector< bool > ConsumeRandomLengthBitVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
FuzzedFileProvider ConsumeFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation