Skip to content

Commit bf03fed

Browse files
committed
Merge bitcoin/bitcoin#28065: fuzz: Flatten all FUZZ_TARGET macros into one
fa6dfaa scripted-diff: Use new FUZZ_TARGET macro everywhere (MarcoFalke) fa36ad8 fuzz: Accept options in FUZZ_TARGET macro (MarcoFalke) Pull request description: The `FUZZ_TARGET` macros have many issues: * The developer will have to pick the right macro to pass the wanted option. * Adding a new option requires doubling the number of existing macros in the worst case. Fix all issues by using only a single macro. This refactor does not change behavior. ACKs for top commit: dergoegge: ACK fa6dfaa Tree-SHA512: 49a34553867a1734ce89e616b2d7c29b784a67cd8990db6573f0c7b18957636ef0c81d3d0d444a04c12cdc98bc4c4aa7a2ec94e6232dc363620a746e28416444
2 parents 275b3e4 + fa6dfaa commit bf03fed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+87
-82
lines changed

src/test/fuzz/addrman.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void initialize_addrman()
4646
return NetGroupManager(asmap);
4747
}
4848

49-
FUZZ_TARGET_INIT(data_stream_addr_man, initialize_addrman)
49+
FUZZ_TARGET(data_stream_addr_man, .init = initialize_addrman)
5050
{
5151
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
5252
CDataStream data_stream = ConsumeDataStream(fuzzed_data_provider);
@@ -233,7 +233,7 @@ class AddrManDeterministic : public AddrMan
233233
}
234234
};
235235

236-
FUZZ_TARGET_INIT(addrman, initialize_addrman)
236+
FUZZ_TARGET(addrman, .init = initialize_addrman)
237237
{
238238
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
239239
SetMockTime(ConsumeTime(fuzzed_data_provider));
@@ -319,7 +319,7 @@ FUZZ_TARGET_INIT(addrman, initialize_addrman)
319319
}
320320

321321
// Check that serialize followed by unserialize produces the same addrman.
322-
FUZZ_TARGET_INIT(addrman_serdeser, initialize_addrman)
322+
FUZZ_TARGET(addrman_serdeser, .init = initialize_addrman)
323323
{
324324
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
325325
SetMockTime(ConsumeTime(fuzzed_data_provider));

src/test/fuzz/banman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs)
4040
lhs.nBanUntil == rhs.nBanUntil;
4141
}
4242

43-
FUZZ_TARGET_INIT(banman, initialize_banman)
43+
FUZZ_TARGET(banman, .init = initialize_banman)
4444
{
4545
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
4646
SetMockTime(ConsumeTime(fuzzed_data_provider));

src/test/fuzz/bitdeque.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void InitRandData()
3131

3232
} // namespace
3333

34-
FUZZ_TARGET_INIT(bitdeque, InitRandData)
34+
FUZZ_TARGET(bitdeque, .init = InitRandData)
3535
{
3636
FuzzedDataProvider provider(buffer.data(), buffer.size());
3737
FastRandomContext ctx(true);

src/test/fuzz/block.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void initialize_block()
2323
SelectParams(ChainType::REGTEST);
2424
}
2525

26-
FUZZ_TARGET_INIT(block, initialize_block)
26+
FUZZ_TARGET(block, .init = initialize_block)
2727
{
2828
CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
2929
CBlock block;

src/test/fuzz/coins_view.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void initialize_coins_view()
4141
g_setup = testing_setup.get();
4242
}
4343

44-
FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
44+
FUZZ_TARGET(coins_view, .init = initialize_coins_view)
4545
{
4646
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
4747
CCoinsView backend_coins_view;

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void initialize_connman()
2828
g_setup = testing_setup.get();
2929
}
3030

31-
FUZZ_TARGET_INIT(connman, initialize_connman)
31+
FUZZ_TARGET(connman, .init = initialize_connman)
3232
{
3333
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
3434
SetMockTime(ConsumeTime(fuzzed_data_provider));

src/test/fuzz/descriptor_parse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void initialize_descriptor_parse()
1414
SelectParams(ChainType::MAIN);
1515
}
1616

17-
FUZZ_TARGET_INIT(descriptor_parse, initialize_descriptor_parse)
17+
FUZZ_TARGET(descriptor_parse, .init = initialize_descriptor_parse)
1818
{
1919
const std::string descriptor(buffer.begin(), buffer.end());
2020
FlatSigningProvider signing_provider;

src/test/fuzz/deserialize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void initialize_deserialize()
4949
}
5050

5151
#define FUZZ_TARGET_DESERIALIZE(name, code) \
52-
FUZZ_TARGET_INIT(name, initialize_deserialize) \
52+
FUZZ_TARGET(name, .init = initialize_deserialize) \
5353
{ \
5454
try { \
5555
code \

src/test/fuzz/fuzz.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,25 @@ const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS =
5454
return g_args;
5555
};
5656

57-
std::map<std::string_view, std::tuple<TypeTestOneInput, TypeInitialize, TypeHidden>>& FuzzTargets()
57+
struct FuzzTarget {
58+
const TypeTestOneInput test_one_input;
59+
const FuzzTargetOptions opts;
60+
};
61+
62+
auto& FuzzTargets()
5863
{
59-
static std::map<std::string_view, std::tuple<TypeTestOneInput, TypeInitialize, TypeHidden>> g_fuzz_targets;
64+
static std::map<std::string_view, FuzzTarget> g_fuzz_targets;
6065
return g_fuzz_targets;
6166
}
6267

63-
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, TypeInitialize init, TypeHidden hidden)
68+
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, FuzzTargetOptions opts)
6469
{
65-
const auto it_ins = FuzzTargets().try_emplace(name, std::move(target), std::move(init), hidden);
70+
const auto it_ins{FuzzTargets().try_emplace(name, FuzzTarget /* temporary can be dropped in C++20 */ {std::move(target), std::move(opts)})};
6671
Assert(it_ins.second);
6772
}
6873

6974
static std::string_view g_fuzz_target;
70-
static TypeTestOneInput* g_test_one_input{nullptr};
75+
static const TypeTestOneInput* g_test_one_input{nullptr};
7176

7277
void initialize()
7378
{
@@ -84,22 +89,22 @@ void initialize()
8489

8590
bool should_exit{false};
8691
if (std::getenv("PRINT_ALL_FUZZ_TARGETS_AND_ABORT")) {
87-
for (const auto& t : FuzzTargets()) {
88-
if (std::get<2>(t.second)) continue;
89-
std::cout << t.first << std::endl;
92+
for (const auto& [name, t] : FuzzTargets()) {
93+
if (t.opts.hidden) continue;
94+
std::cout << name << std::endl;
9095
}
9196
should_exit = true;
9297
}
9398
if (const char* out_path = std::getenv("WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) {
9499
std::cout << "Writing all fuzz target names to '" << out_path << "'." << std::endl;
95100
std::ofstream out_stream{out_path, std::ios::binary};
96-
for (const auto& t : FuzzTargets()) {
97-
if (std::get<2>(t.second)) continue;
98-
out_stream << t.first << std::endl;
101+
for (const auto& [name, t] : FuzzTargets()) {
102+
if (t.opts.hidden) continue;
103+
out_stream << name << std::endl;
99104
}
100-
should_exit= true;
105+
should_exit = true;
101106
}
102-
if (should_exit){
107+
if (should_exit) {
103108
std::exit(EXIT_SUCCESS);
104109
}
105110
if (const auto* env_fuzz{std::getenv("FUZZ")}) {
@@ -117,8 +122,8 @@ void initialize()
117122
std::exit(EXIT_FAILURE);
118123
}
119124
Assert(!g_test_one_input);
120-
g_test_one_input = &std::get<0>(it->second);
121-
std::get<1>(it->second)();
125+
g_test_one_input = &it->second.test_one_input;
126+
it->second.opts.init();
122127
}
123128

124129
#if defined(PROVIDE_FUZZ_MAIN_FUNCTION)

src/test/fuzz/fuzz.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@
2121
using FuzzBufferType = Span<const uint8_t>;
2222

2323
using TypeTestOneInput = std::function<void(FuzzBufferType)>;
24-
using TypeInitialize = std::function<void()>;
25-
using TypeHidden = bool;
24+
struct FuzzTargetOptions {
25+
std::function<void()> init{[] {}};
26+
bool hidden{false};
27+
};
2628

27-
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, TypeInitialize init, TypeHidden hidden);
29+
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, FuzzTargetOptions opts);
2830

29-
inline void FuzzFrameworkEmptyInitFun() {}
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
3036

31-
#define FUZZ_TARGET(name) \
32-
FUZZ_TARGET_INIT(name, FuzzFrameworkEmptyInitFun)
33-
34-
#define FUZZ_TARGET_INIT(name, init_fun) \
35-
FUZZ_TARGET_INIT_HIDDEN(name, init_fun, false)
36-
37-
#define FUZZ_TARGET_INIT_HIDDEN(name, init_fun, hidden) \
37+
#define DETAIL_FUZZ(name, ...) \
3838
void name##_fuzz_target(FuzzBufferType); \
3939
struct name##_Before_Main { \
4040
name##_Before_Main() \
4141
{ \
42-
FuzzFrameworkRegisterTarget(#name, name##_fuzz_target, init_fun, hidden); \
42+
FuzzFrameworkRegisterTarget(#name, name##_fuzz_target, {__VA_ARGS__}); \
4343
} \
4444
} const static g_##name##_before_main; \
4545
void name##_fuzz_target(FuzzBufferType buffer)

0 commit comments

Comments
 (0)