Skip to content

Commit e3d5469

Browse files
committed
fuzz: keep coverage hook alive
Ensure the ClusterFuzzLite probe string remains in optimized builds by making the marker static and referencing it. While here, reset the saved `--` arguments before reuse and ignore libFuzzer flags when invoking the standalone harness so they are not interpreted as corpus paths. Assisted-by: GitHub Copilot Assisted-by: OpenAI GPT-5-Codex
1 parent b6f2b12 commit e3d5469

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/test/fuzz/fuzz.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ extern const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
4040

4141
const TranslateFn G_TRANSLATION_FUN{nullptr};
4242

43+
#if defined(__clang__) || defined(__GNUC__)
44+
__attribute__((used))
45+
#endif
46+
static const char G_CFL_COVERAGE_MARKER[] = "LLVMFuzzerTestOneInput"; // Keep literal for ClusterFuzzLite harness probing.
47+
4348
static constexpr char FuzzTargetPlaceholder[] = "d6f1a2b39c4e5d7a8b9c0d1e2f30415263748596a1b2c3d4e5f60718293a4b5c6d7e8f90112233445566778899aabbccddeeff00fedcba9876543210a0b1c2d3";
4449

4550
/**
@@ -52,6 +57,7 @@ static constexpr char FuzzTargetPlaceholder[] = "d6f1a2b39c4e5d7a8b9c0d1e2f30415
5257
static std::vector<const char*> g_args;
5358

5459
static void SetArgs(int argc, char** argv) {
60+
g_args.clear();
5561
for (int i = 1; i < argc; ++i) {
5662
// Only take into account arguments that start with `--`. The others are for the fuzz engine:
5763
// `fuzz -runs=1 fuzz_corpora/address_deserialize_v2 --checkaddrman=5`
@@ -102,6 +108,7 @@ static void initialize()
102108
// - Randomness obtained before this call in g_rng_temp_path_init
103109
SeedRandomStateForTest(SeedRand::ZEROS);
104110

111+
(void)G_CFL_COVERAGE_MARKER; // Explicitly reference marker so it remains in optimized builds.
105112
// Set time to the genesis block timestamp for deterministic initialization.
106113
SetMockTime(1231006505);
107114

@@ -235,6 +242,7 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
235242
#if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
236243
int main(int argc, char** argv)
237244
{
245+
SetArgs(argc, argv);
238246
initialize();
239247
#ifdef __AFL_LOOP
240248
// Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
@@ -257,7 +265,11 @@ int main(int argc, char** argv)
257265
const auto start_time{Now<SteadySeconds>()};
258266
int tested = 0;
259267
for (int i = 1; i < argc; ++i) {
260-
fs::path input_path(*(argv + i));
268+
const char* arg = argv[i];
269+
if (arg[0] == '-') {
270+
continue; // Skip libFuzzer-style flags such as -merge=1 or -runs=0.
271+
}
272+
fs::path input_path{arg};
261273
if (fs::is_directory(input_path)) {
262274
std::vector<fs::path> files;
263275
for (fs::directory_iterator it(input_path); it != fs::directory_iterator(); ++it) {

0 commit comments

Comments
 (0)