1 // Copyright (c) 2009-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_TEST_FUZZ_FUZZ_H
6 #define BITCOIN_TEST_FUZZ_FUZZ_H
7 
8 #include <span.h>
9 
10 #include <cstdint>
11 #include <functional>
12 #include <string_view>
13 
14 using FuzzBufferType = Span<const uint8_t>;
15 
16 using TypeTestOneInput = std::function<void(FuzzBufferType)>;
17 using TypeInitialize = std::function<void()>;
18 using TypeHidden = bool;
19 
20 void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, TypeInitialize init, TypeHidden hidden);
21 
FuzzFrameworkEmptyInitFun()22 inline void FuzzFrameworkEmptyInitFun() {}
23 
24 #define FUZZ_TARGET(name) \
25     FUZZ_TARGET_INIT(name, FuzzFrameworkEmptyInitFun)
26 
27 #define FUZZ_TARGET_INIT(name, init_fun) \
28     FUZZ_TARGET_INIT_HIDDEN(name, init_fun, false)
29 
30 #define FUZZ_TARGET_INIT_HIDDEN(name, init_fun, hidden)                               \
31     void name##_fuzz_target(FuzzBufferType);                                          \
32     struct name##_Before_Main {                                                       \
33         name##_Before_Main()                                                          \
34         {                                                                             \
35             FuzzFrameworkRegisterTarget(#name, name##_fuzz_target, init_fun, hidden); \
36         }                                                                             \
37     } const static g_##name##_before_main;                                            \
38     void name##_fuzz_target(FuzzBufferType buffer)
39 
40 #endif // BITCOIN_TEST_FUZZ_FUZZ_H
41