Skip to content

Commit c434282

Browse files
committed
Merge #18008: test: only declare a main() when fuzzing with AFL
b35567f test: only declare a main() when fuzzing with AFL (fanquake) Pull request description: This fixes fuzzing using [libFuzzer](https://llvm.org/docs/LibFuzzer.html) on macOS, which caused a few issues during the recent review club. macOS users could only fuzz using afl, or inside a VM. It seems that the `__attribute__((weak))` marking is not quite enough to properly mark `main()` as weak on macOS. See Apples docs on [Frameworks and Weak Linking](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html#//apple_ref/doc/uid/20002378-107262-CJBJAEID). Have tested fuzzing using libFuzzer and AFL with this patch. ACKs for top commit: MarcoFalke: ACK b35567f fjahr: ACK b35567f Tree-SHA512: b881fdd98c7e1587fcf44debd31f5e7a52df938059ab91c41d0785077b3329b793e051a2bf2eee64488b9f6029d9288c911052ec23ab3ab8c0561a2be1682dae
2 parents e061b8d + b35567f commit c434282

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)