Line data Source code
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 : /** 15 : * Can be used to limit a theoretically unbounded loop. This caps the runtime 16 : * to avoid timeouts or OOMs. 17 : */ 18 : #define LIMITED_WHILE(condition, limit) \ 19 : for (unsigned _count{limit}; (condition) && _count; --_count) 20 : 21 : using FuzzBufferType = Span<const uint8_t>; 22 : 23 : using TypeTestOneInput = std::function<void(FuzzBufferType)>; 24 : struct FuzzTargetOptions { 25 75 : std::function<void()> init{[] {}}; 26 : bool hidden{false}; 27 : }; 28 : 29 : void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, FuzzTargetOptions opts); 30 : 31 : #if defined(__clang__) 32 : #define FUZZ_TARGET(...) _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wgnu-zero-variadic-macro-arguments\"") DETAIL_FUZZ(__VA_ARGS__) _Pragma("clang diagnostic pop") 33 : #else 34 : #define FUZZ_TARGET(...) DETAIL_FUZZ(__VA_ARGS__) 35 : #endif 36 : 37 : #define DETAIL_FUZZ(name, ...) \ 38 : void name##_fuzz_target(FuzzBufferType); \ 39 : struct name##_Before_Main { \ 40 : name##_Before_Main() \ 41 : { \ 42 : FuzzFrameworkRegisterTarget(#name, name##_fuzz_target, {__VA_ARGS__}); \ 43 : } \ 44 : } const static g_##name##_before_main; \ 45 : void name##_fuzz_target(FuzzBufferType buffer) 46 : 47 : #endif // BITCOIN_TEST_FUZZ_FUZZ_H