2021-01-25 12:23:45 +01:00
|
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
2019-01-25 18:35:36 -05:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_TEST_FUZZ_FUZZ_H
|
|
|
|
#define BITCOIN_TEST_FUZZ_FUZZ_H
|
|
|
|
|
2021-01-02 19:29:36 +01:00
|
|
|
#include <span.h>
|
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <string_view>
|
2019-01-25 18:35:36 -05:00
|
|
|
|
2021-08-21 19:34:13 +02:00
|
|
|
/**
|
|
|
|
* Can be used to limit a theoretically unbounded loop. This caps the runtime
|
|
|
|
* to avoid timeouts or OOMs.
|
2023-11-07 17:46:41 +01:00
|
|
|
*
|
|
|
|
* This can be used in combination with a check in the condition to confirm
|
|
|
|
* whether the fuzz engine provided "good" data. If the fuzz input contains
|
|
|
|
* invalid data, the loop aborts early. This will teach the fuzz engine to look
|
|
|
|
* for useful data and avoids bloating the fuzz input folder with useless data.
|
2021-08-21 19:34:13 +02:00
|
|
|
*/
|
2021-08-06 12:39:11 +02:00
|
|
|
#define LIMITED_WHILE(condition, limit) \
|
|
|
|
for (unsigned _count{limit}; (condition) && _count; --_count)
|
|
|
|
|
2021-01-02 19:29:36 +01:00
|
|
|
using FuzzBufferType = Span<const uint8_t>;
|
|
|
|
|
|
|
|
using TypeTestOneInput = std::function<void(FuzzBufferType)>;
|
2023-07-11 14:00:51 +02:00
|
|
|
struct FuzzTargetOptions {
|
|
|
|
std::function<void()> init{[] {}};
|
|
|
|
bool hidden{false};
|
|
|
|
};
|
2020-12-03 16:42:49 +01:00
|
|
|
|
2023-07-11 14:00:51 +02:00
|
|
|
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, FuzzTargetOptions opts);
|
2020-12-03 16:42:49 +01:00
|
|
|
|
2023-07-11 14:00:51 +02:00
|
|
|
#define FUZZ_TARGET(...) DETAIL_FUZZ(__VA_ARGS__)
|
2021-02-08 10:13:08 +01:00
|
|
|
|
2023-07-11 14:00:51 +02:00
|
|
|
#define DETAIL_FUZZ(name, ...) \
|
2021-02-08 10:13:08 +01:00
|
|
|
void name##_fuzz_target(FuzzBufferType); \
|
|
|
|
struct name##_Before_Main { \
|
|
|
|
name##_Before_Main() \
|
|
|
|
{ \
|
2023-07-11 14:00:51 +02:00
|
|
|
FuzzFrameworkRegisterTarget(#name, name##_fuzz_target, {__VA_ARGS__}); \
|
2021-02-08 10:13:08 +01:00
|
|
|
} \
|
|
|
|
} const static g_##name##_before_main; \
|
2021-01-02 19:29:36 +01:00
|
|
|
void name##_fuzz_target(FuzzBufferType buffer)
|
2019-01-25 18:35:36 -05:00
|
|
|
|
|
|
|
#endif // BITCOIN_TEST_FUZZ_FUZZ_H
|