Skip to content

Commit 3bbd822

Browse files
author
MarcoFalke
committed
Merge #19366: tests: Provide main(...) function in fuzzer. Allow building uninstrumented harnesses with --enable-fuzz.
1087807 tests: Provide main(...) function in fuzzer (practicalswift) Pull request description: Provide `main(...)` function in fuzzer. Allow building uninstrumented harnesses with only `--enable-fuzz`. This PR restores the behaviour to how things worked prior to #18008. #18008 worked around an macOS specific issue but did it in a way which unnecessarily affected platforms not in need of the workaround :) Before this patch: ``` # Build uninstrumented fuzzing harness (no libFuzzer/AFL/other-fuzzer-instrumentation) $ ./configure --enable-fuzz $ make CXXLD test/fuzz/span /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status Makefile:7244: recipe for target 'test/fuzz/span' failed make[2]: *** [test/fuzz/span] Error 1 make[2]: *** Waiting for unfinished jobs.... $ ``` After this patch: ``` # Build uninstrumented fuzzing harness (no libFuzzer/AFL/other-fuzzer-instrumentation) $ ./configure --enable-fuzz $ make $ echo foo | src/test/fuzz/span $ ``` The examples above show the change in non-macOS functionality. macOS functionality is unaffected by this patch. ACKs for top commit: MarcoFalke: ACK 1087807 Tree-SHA512: 9c16ea32ffd378057c4fae9d9124636d11e3769374d340f68a1b761b9e3e3b8a33579e60425293c96b8911405d8b96ac3ed378e669ea4c47836af06892aca73d
2 parents f32f7e9 + 1087807 commit 3bbd822

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/test/fuzz/fuzz.cpp

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

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

15-
#if defined(__AFL_COMPILER)
15+
// Decide if main(...) should be provided:
16+
// * AFL needs main(...) regardless of platform.
17+
// * macOS handles __attribute__((weak)) main(...) poorly when linking
18+
// against libFuzzer. See https://github.com/bitcoin/bitcoin/pull/18008
19+
// for details.
20+
#if defined(__AFL_COMPILER) || !defined(MAC_OSX)
21+
#define PROVIDE_MAIN_FUNCTION
22+
#endif
23+
24+
#if defined(PROVIDE_MAIN_FUNCTION)
1625
static bool read_stdin(std::vector<uint8_t>& data)
1726
{
1827
uint8_t buffer[1024];
@@ -44,9 +53,8 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
4453
return 0;
4554
}
4655

47-
// Generally, the fuzzer will provide main(), except for AFL
48-
#if defined(__AFL_COMPILER)
49-
int main(int argc, char** argv)
56+
#if defined(PROVIDE_MAIN_FUNCTION)
57+
__attribute__((weak)) int main(int argc, char** argv)
5058
{
5159
initialize();
5260
#ifdef __AFL_INIT

0 commit comments

Comments
 (0)