18 #include <type_traits>
23 #define ZEROERR_CREATE_FUZZ_TEST_FUNC(function, name) \
24 static void function(zeroerr::TestContext*); \
25 static zeroerr::detail::regTest ZEROERR_NAMEGEN(_zeroerr_reg)( \
26 {name, __FILE__, __LINE__, function}, zeroerr::TestType::fuzz_test); \
27 static void function(ZEROERR_UNUSED(zeroerr::TestContext* _ZEROERR_TEST_CONTEXT))
29 #define FUZZ_TEST_CASE(name) ZEROERR_CREATE_FUZZ_TEST_FUNC(ZEROERR_NAMEGEN(_zeroerr_testcase), name)
31 #define FUZZ_FUNC(func) zeroerr::FuzzFunction(func, _ZEROERR_TEST_CONTEXT)
42 template <
typename... Args>
44 static constexpr
size_t numOfArgs =
sizeof...(Args);
48 template <
typename... Args>
52 template <
typename... Args>
54 std::function<
void(Args...)>);
62 template <
typename Ret,
typename Class,
typename... Args>
82 template <
typename TargetFunction,
86 template <
typename TargetFunction,
typename FuncType,
typename Domain,
91 virtual void Run(
int count = 1000,
int seed = 0) = 0;
92 virtual void RunOneTime(
const uint8_t* data,
size_t size) = 0;
93 virtual std::string
MutateData(
const uint8_t* data,
size_t size,
size_t max_size,
94 unsigned int seed) = 0;
102 virtual const char*
what()
const throw() {
return "Fuzzing finished"; }
106 template <
typename TargetFunction,
typename FuncType>
113 template <
typename... T>
116 AggregateOf<std::tuple<
typename T::ValueType...>, T...>>;
121 template <
typename... T>
123 AggregateOf<std::tuple<typename T::ValueType...>, T...> domain) {
132 template <
typename... T>
138 this->
seeds.insert(this->
seeds.end(), _seeds.begin(), _seeds.end());
143 virtual void Run(
int = 1000,
int = 0) {}
145 virtual std::string
MutateData(
const uint8_t*,
size_t,
size_t,
unsigned int) {
return ""; }
153 int timeout = 1200,
int len_control = 100);
163 template <
typename TargetFunction,
typename FuncType,
typename Domain,
typename Base>
167 virtual void Run(
int _count = 1000,
int _seed = 0)
override {
169 Base::max_count = _count;
178 if (Base::seeds.size() > 0 && rng.
bounded(2) == 0) {
184 typename Domain::CorpusType
TryParse(
const std::string& input) {
194 template <
typename T,
unsigned... I>
196 this->func(std::get<I>(args)...);
199 virtual void RunOneTime(
const uint8_t* data,
size_t size)
override {
201 std::string input = std::string((
const char*)data, size);
202 typename Domain::CorpusType corpus =
TryParse(input);
207 virtual std::string
MutateData(
const uint8_t* data,
size_t size,
size_t max_size,
208 unsigned int seed)
override {
210 std::string input = std::string((
const char*)data, size);
211 typename Domain::CorpusType corpus =
TryParse(input);
222 template <
typename T>
227 template <
typename T>
Definition: aggregate_of.h:17
Domain class for generating random values of a specific type.
Definition: domain.h:22
virtual ValueType GetValue(const CorpusType &v) const =0
virtual void Mutate(Rng &rng, CorpusType &v, bool only_shrink=false) const =0
virtual CorpusType GetRandomCorpus(Rng &rng) const =0
virtual IRObject SerializeCorpus(const CorpusType &v) const
Definition: domain.h:33
virtual CorpusType FromValue(const ValueType &v) const =0
virtual CorpusType ParseCorpus(IRObject v) const
Definition: domain.h:32
Definition: fuzztest.h:100
virtual const char * what() const
Definition: fuzztest.h:102
uint32_t bounded(uint32_t range) noexcept
Definition: rng.h:126
TestContext is a class that holds the test results and reporter context. There are 8 different matric...
Definition: unittest.h:67
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition: config.h:265
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition: config.h:218
decltype(FunctionDecompositionImpl(std::declval< T >())) FunctionDecompositionT
Definition: fuzztest.h:72
FunctionDecomposition< typename std::decay< Args >::type... > FunctionDecompositionImpl(void(*)(Args...))
Definition: benchmark.cpp:17
void RunFuzzTest(IFuzzTest &fuzz_test, int seed, int runs, int max_len, int timeout, int len_control)
Definition: fuzztest.cpp:32
std::vector< T > ReadCorpusFromDir(std::string dir)
@ fuzz_test
Definition: unittest.h:254
AggregateOf< std::tuple< typename Inner::ValueType... >, Inner... > TupleOf(Inner &&... inner)
Definition: aggregate_of.h:83
FuzzTest< T > FuzzFunction(T func, TestContext *context)
Definition: fuzztest.h:223
FuzzTestWithDomain implements the Base class with the domain passed into.
Definition: fuzztest.h:164
Domain m_domain
Definition: fuzztest.h:217
FuzzTestWithDomain(const Base &ft, const Domain &domain)
Definition: fuzztest.h:165
Domain::CorpusType GetRandomCorpus()
Definition: fuzztest.h:176
virtual void RunOneTime(const uint8_t *data, size_t size) override
Definition: fuzztest.h:199
void apply(T args, detail::seq< I... >)
Definition: fuzztest.h:195
virtual std::string MutateData(const uint8_t *data, size_t size, size_t max_size, unsigned int seed) override
Definition: fuzztest.h:207
Rng * m_rng
Definition: fuzztest.h:218
virtual void Run(int _count=1000, int _seed=0) override
Definition: fuzztest.h:167
Domain::CorpusType TryParse(const std::string &input)
Definition: fuzztest.h:184
FuzzTest is a template class to create a fuzz test for the target function.
Definition: fuzztest.h:107
TargetFunction func
Definition: fuzztest.h:147
FuzzTest(TargetFunction func, TestContext *context)
Definition: fuzztest.h:109
FuzzTestWithDomainType< T... > WithDomains(T &&... domains)
Definition: fuzztest.h:133
std::vector< ValueType > seeds
Definition: fuzztest.h:149
TestContext * context
Definition: fuzztest.h:148
virtual void Run(int=1000, int=0)
Definition: fuzztest.h:143
virtual void RunOneTime(const uint8_t *, size_t)
Definition: fuzztest.h:144
typename FuncType::ValueType ValueType
Definition: fuzztest.h:108
FuzzTestWithDomainType< T... > WithDomains(AggregateOf< std::tuple< typename T::ValueType... >, T... > domain)
Definition: fuzztest.h:122
FuzzTest & WithSeeds(std::vector< ValueType > _seeds)
Definition: fuzztest.h:137
FuzzTest(const FuzzTest &other)
Definition: fuzztest.h:110
virtual std::string MutateData(const uint8_t *, size_t, size_t, unsigned int)
Definition: fuzztest.h:145
Definition: fuzztest.h:90
virtual void RunOneTime(const uint8_t *data, size_t size)=0
virtual std::string MutateData(const uint8_t *data, size_t size, size_t max_size, unsigned int seed)=0
virtual void Run(int count=1000, int seed=0)=0
bool should_stop()
Definition: fuzztest.h:97
int max_count
Definition: fuzztest.h:96
int count
Definition: fuzztest.h:96
Definition: serialization.h:17
static std::string ToString(IRObject obj)
Definition: serialization.cpp:133
static IRObject FromString(std::string str)
Definition: serialization.cpp:139
unsigned type
Definition: serialization.h:42
FunctionDecomposition is a meta function to decompose the function type. The ValueType and numOfArgs ...
Definition: fuzztest.h:43
std::tuple< Args... > ValueType
Definition: fuzztest.h:45
static constexpr size_t numOfArgs
Definition: fuzztest.h:44
Definition: typetraits.h:58
Definition: fuzztest.h:58
void type
Definition: fuzztest.h:59
Definition: typetraits.h:55