Merge #18008: test: only declare a main() when fuzzing with AFL

b35567fe0b 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 b35567fe0b
  fjahr:
    ACK b35567f

Tree-SHA512: b881fdd98c7e1587fcf44debd31f5e7a52df938059ab91c41d0785077b3329b793e051a2bf2eee64488b9f6029d9288c911052ec23ab3ab8c0561a2be1682dae
This commit is contained in:
fanquake 2020-01-29 20:37:13 +08:00
commit c434282d2c
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

View File

@ -12,6 +12,7 @@
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
#if defined(__AFL_COMPILER)
static bool read_stdin(std::vector<uint8_t>& data)
{
uint8_t buffer[1024];
@ -23,6 +24,7 @@ static bool read_stdin(std::vector<uint8_t>& data)
}
return length == 0;
}
#endif
// Default initialization: Override using a non-weak initialize().
__attribute__((weak)) void initialize()
@ -44,9 +46,9 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
return 0;
}
// Declare main(...) "weak" to allow for libFuzzer linking. libFuzzer provides
// the main(...) function.
__attribute__((weak)) int main(int argc, char** argv)
// Generally, the fuzzer will provide main(), except for AFL
#if defined(__AFL_COMPILER)
int main(int argc, char** argv)
{
initialize();
#ifdef __AFL_INIT
@ -74,3 +76,4 @@ __attribute__((weak)) int main(int argc, char** argv)
#endif
return 0;
}
#endif