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

pull fanquake wants to merge 1 commits into bitcoin:master from fanquake:macos_libfuzzer_weak_main changing 1 files +6 −3
  1. fanquake commented at 10:43 am on January 27, 2020: member

    This fixes fuzzing using libFuzzer 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.

    Have tested fuzzing using libFuzzer and AFL with this patch.

  2. fanquake added the label macOS on Jan 27, 2020
  3. fanquake added the label Tests on Jan 27, 2020
  4. practicalswift commented at 12:42 pm on January 27, 2020: contributor

    ACK 6b04aff38c06ee9db74132c783fde3e72c117ca0 – diff looks correct

    Thanks for fixing this!

  5. in src/test/fuzz/fuzz.cpp:50 in 6b04aff38c outdated
    45@@ -46,6 +46,9 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
    46 
    47 // Declare main(...) "weak" to allow for libFuzzer linking. libFuzzer provides
    48 // the main(...) function.
    49+#if defined(MAC_OSX) && !defined(__AFL_COMPILER)
    50+extern int main(int argc, char** argv) __attribute__((weak_import));
    


    MarcoFalke commented at 6:50 pm on January 27, 2020:

    What is the point of declaring main here if it is already declared in libFuzzer?

    I guess we could save us all of this __attribute__ wrangling with a simple compile time check:

    0#if defined(__AFL_COMPILER)
    1static bool read_stdin(std::vector<uint8_t>& data) { ... }
    2int main(int argc, char** argv) { ... }
    3#endif 
    

    fanquake commented at 9:26 am on January 28, 2020:
    We could leave out the main declaration entirely, I just thought it might be clearer in regards to it being weak linked. If you prefer I can replace it with a comment as to why we special case libFuzzer on macOS.

    MarcoFalke commented at 2:15 pm on January 28, 2020:

    I can replace it with a comment as to why we special case libFuzzer on macOS

    My point is that we don’t need to special case libFuzzer on macOS. I think, we can have the same simple code (a single compile time check) for all operating systems. There would be no need for _attribute__ and #if defined(MAC_OSX) wrangling.


    MarcoFalke commented at 6:00 pm on January 28, 2020:
    Also, not having a main function should fix the issue where compilation and linking succeeds and the fuzzer just runs “blank”, I think.

    fanquake commented at 9:57 pm on January 28, 2020:

    should fix the issue where compilation and linking succeeds and the fuzzer just runs “blank”, I think.

    That is what is fixed by this PR in its current state. Are you still seeing that issue somewhere else with this change applied?


    MarcoFalke commented at 11:48 pm on January 28, 2020:

    Sorry, I think I might have been unclear. I see that your patch does fix the issue. However, I think we should go even further: A main function that does not exist should be better than a main function that is weak, as it would properly lead to linker issues instead of odd runtime behaviour.

    The following patch (on top of master) should achieve the same result while also removing the main function altogether for fuzzers (except AFL).

     0diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp
     1index a6ab620e21..5a7b96c89b 100644
     2--- a/src/test/fuzz/fuzz.cpp
     3+++ b/src/test/fuzz/fuzz.cpp
     4@@ -44,9 +44,9 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
     5     return 0;
     6 }
     7 
     8-// Declare main(...) "weak" to allow for libFuzzer linking. libFuzzer provides
     9-// the main(...) function.
    10-__attribute__((weak)) int main(int argc, char** argv)
    11+// The fuzzer generally provides the main function, except for AFL
    12+#if defined(__AFL_COMPILER)
    13+int main(int argc, char** argv)
    14 {
    15     initialize();
    16 #ifdef __AFL_INIT
    17@@ -74,3 +74,4 @@ __attribute__((weak)) int main(int argc, char** argv)
    18 #endif
    19     return 0;
    20 }
    21+#endif
    

    fanquake commented at 0:05 am on January 29, 2020:
    Right, we can do that. I’ll update this and drop the __attribute__ usage in favour of an #ifdef.
  6. eriknylund commented at 8:37 pm on January 27, 2020: contributor

    ACK 6b04aff38c06ee9db74132c783fde3e72c117ca0

    Tested both AFL and libFuzzer using documentation from #17942

    Both AFL and libFuzzer build and run without issues.

  7. fjahr commented at 9:11 pm on January 27, 2020: member

    ACK 6b04aff38c06ee9db74132c783fde3e72c117ca0

    Tested compiling fuzzer tests with libFuzzer which had not been working previously.

  8. Sjors commented at 5:04 pm on January 28, 2020: member

    What error am I supposed to be seeing on master (I run into plenty of problems, but don’t know if that’s because not following instructions correctly)?

    On macOS 10.15.2 with llvm/9.0.1 (homebrew) I followed the instructions in #17942, first on master then on this PR. On master I ran into subprocess timed out: Currently only libFuzzer is supported. With this branch it just exits after Fuzz targets selected: ['bech32']. And then I’m able to run src/test/fuzz/bech32 and watch a bunch of cryptic info scroll by.

  9. fjahr commented at 5:11 pm on January 28, 2020: member

    What error am I supposed to be seeing on master (I run into plenty of problems, but don’t know if that’s because not following instructions correctly)?

    For me the fuzzers compile when following the instructions from #17942 but when I start any of the fuzzers, they just hang. There is not output and they don’t stop until interrupted manually. Running them through the python harness gives an error message about only AFL being supported but that’s because the fuzzer hangs and the harness has a timeout built in (error message could be improved though).

  10. fanquake commented at 0:04 am on January 29, 2020: member

    With this branch it just exits after Fuzz targets selected: [‘bech32’] @Sjors It doesn’t just exit, the bech32 fuzzer is run. However the log level defaults to INFO, which doesn’t generate any output. Pass -l DEBUG and you’ll see:

     0Fuzz targets selected: ['bech32']
     1Run bech32 with args ['/Users/michael/github/fanquake-bitcoin/src/test/fuzz/bech32', '-runs=1', '-detect_leaks=0', '../qa-assets/fuzz_seed_corpus/bech32']
     2Output: INFO: Seed: 254137248
     3INFO: Loaded 1 modules   (4751 inline 8-bit counters): 4751 [0x1010c4b58, 0x1010c5de7), 
     4INFO: Loaded 1 PC tables (4751 PCs): 4751 [0x1010c5de8,0x1010d86d8), 
     5INFO:      254 files found in ../qa-assets/fuzz_seed_corpus/bech32
     6INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes
     7INFO: seed corpus: files: 254 min: 1b max: 713b total: 50505b rss: 27Mb
     8[#255](/bitcoin-bitcoin/255/)	INITED cov: 1076 ft: 4020 corp: 151/19Kb exec/s: 0 rss: 33Mb
     9[#255](/bitcoin-bitcoin/255/)	DONE   cov: 1076 ft: 4020 corp: 151/19Kb lim: 713 exec/s: 0 rss: 33Mb
    10Done 255 runs in 0 second(s)
    
  11. MarcoFalke commented at 0:10 am on January 29, 2020: member

    On master I ran into subprocess timed out: Currently only libFuzzer is supported.

    This is exactly the error you are supposed to see without this patch on macos. The fuzzer will run the main function, but not the one provided by libFuzzer.

  12. 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.
    b35567fe0b
  13. fanquake force-pushed on Jan 29, 2020
  14. fanquake renamed this:
    test: fix fuzzing using libFuzzer on macOS
    test: only declare a main() when fuzzing with AFL
    on Jan 29, 2020
  15. fanquake commented at 0:33 am on January 29, 2020: member
    Updated to drop main() deceleration entirely except for when using AFL.
  16. MarcoFalke commented at 0:35 am on January 29, 2020: member

    ACK b35567fe0ba3e6f8d8f9525088eb8ee62065ad01

    Can’t test on mac, sorry :grimacing:

  17. Sjors commented at 8:46 am on January 29, 2020: member
    Tested b35567fe0ba3e6f8d8f9525088eb8ee62065ad01 on macOS 10.15.2 (also checked that AFL still works)
  18. fjahr commented at 12:34 pm on January 29, 2020: member

    ACK b35567f

    Tested libFuzzer on macOS.

  19. fanquake referenced this in commit c434282d2c on Jan 29, 2020
  20. fanquake merged this on Jan 29, 2020
  21. fanquake closed this on Jan 29, 2020

  22. fanquake deleted the branch on Jan 29, 2020
  23. MarcoFalke referenced this in commit 3bbd8225b9 on Jun 26, 2020
  24. Fabcien referenced this in commit 4ffa90d363 on Jan 19, 2021
  25. kittywhiskers referenced this in commit 863502cd17 on Aug 2, 2021
  26. kittywhiskers referenced this in commit de95ae367b on Aug 2, 2021
  27. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-23 21:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me