Skip to content

Commit

Permalink
Merge bitcoin#19366: tests: Provide main(...) function in fuzzer. All…
Browse files Browse the repository at this point in the history
…ow 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 bitcoin#18008. bitcoin#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
  • Loading branch information
MarcoFalke committed Jun 26, 2020
2 parents f32f7e9 + 1087807 commit 3bbd822
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/test/fuzz/fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@

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

#if defined(__AFL_COMPILER)
// Decide if main(...) should be provided:
// * AFL needs main(...) regardless of platform.
// * macOS handles __attribute__((weak)) main(...) poorly when linking
// against libFuzzer. See https://github.com/bitcoin/bitcoin/pull/18008
// for details.
#if defined(__AFL_COMPILER) || !defined(MAC_OSX)
#define PROVIDE_MAIN_FUNCTION
#endif

#if defined(PROVIDE_MAIN_FUNCTION)
static bool read_stdin(std::vector<uint8_t>& data)
{
uint8_t buffer[1024];
Expand Down Expand Up @@ -44,9 +53,8 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
return 0;
}

// Generally, the fuzzer will provide main(), except for AFL
#if defined(__AFL_COMPILER)
int main(int argc, char** argv)
#if defined(PROVIDE_MAIN_FUNCTION)
__attribute__((weak)) int main(int argc, char** argv)
{
initialize();
#ifdef __AFL_INIT
Expand Down

0 comments on commit 3bbd822

Please sign in to comment.