Skip to content

Commit b35567f

Browse files
committed
test: only declare a main() when fuzzing with AFL
libFuzzer will provide a main(). This also fixes a weak linking issue when fuzzing with libFuzzer on macOS.
1 parent fe48ac8 commit b35567f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/test/fuzz/fuzz.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
1414

15+
#if defined(__AFL_COMPILER)
1516
static bool read_stdin(std::vector<uint8_t>& data)
1617
{
1718
uint8_t buffer[1024];
@@ -23,6 +24,7 @@ static bool read_stdin(std::vector<uint8_t>& data)
2324
}
2425
return length == 0;
2526
}
27+
#endif
2628

2729
// Default initialization: Override using a non-weak initialize().
2830
__attribute__((weak)) void initialize()
@@ -44,9 +46,9 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
4446
return 0;
4547
}
4648

47-
// Declare main(...) "weak" to allow for libFuzzer linking. libFuzzer provides
48-
// the main(...) function.
49-
__attribute__((weak)) int main(int argc, char** argv)
49+
// Generally, the fuzzer will provide main(), except for AFL
50+
#if defined(__AFL_COMPILER)
51+
int main(int argc, char** argv)
5052
{
5153
initialize();
5254
#ifdef __AFL_INIT
@@ -74,3 +76,4 @@ __attribute__((weak)) int main(int argc, char** argv)
7476
#endif
7577
return 0;
7678
}
79+
#endif

0 commit comments

Comments
 (0)