comparison docs/LibFuzzer.rst @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
73 73
74 Recent versions of Clang (starting from 6.0) include libFuzzer, and no extra installation is necessary. 74 Recent versions of Clang (starting from 6.0) include libFuzzer, and no extra installation is necessary.
75 75
76 In order to build your fuzzer binary, use the `-fsanitize=fuzzer` flag during the 76 In order to build your fuzzer binary, use the `-fsanitize=fuzzer` flag during the
77 compilation and linking. In most cases you may want to combine libFuzzer with 77 compilation and linking. In most cases you may want to combine libFuzzer with
78 AddressSanitizer_ (ASAN), UndefinedBehaviorSanitizer_ (UBSAN), or both:: 78 AddressSanitizer_ (ASAN), UndefinedBehaviorSanitizer_ (UBSAN), or both. You can
79 also build with MemorySanitizer_ (MSAN), but support is experimental::
79 80
80 clang -g -O1 -fsanitize=fuzzer mytarget.c # Builds the fuzz target w/o sanitizers 81 clang -g -O1 -fsanitize=fuzzer mytarget.c # Builds the fuzz target w/o sanitizers
81 clang -g -O1 -fsanitize=fuzzer,address mytarget.c # Builds the fuzz target with ASAN 82 clang -g -O1 -fsanitize=fuzzer,address mytarget.c # Builds the fuzz target with ASAN
82 clang -g -O1 -fsanitize=fuzzer,signed-integer-overflow mytarget.c # Builds the fuzz target with a part of UBSAN 83 clang -g -O1 -fsanitize=fuzzer,signed-integer-overflow mytarget.c # Builds the fuzz target with a part of UBSAN
84 clang -g -O1 -fsanitize=fuzzer,memory mytarget.c # Builds the fuzz target with MSAN
83 85
84 This will perform the necessary instrumentation, as well as linking with the libFuzzer library. 86 This will perform the necessary instrumentation, as well as linking with the libFuzzer library.
85 Note that ``-fsanitize=fuzzer`` links in the libFuzzer's ``main()`` symbol. 87 Note that ``-fsanitize=fuzzer`` links in the libFuzzer's ``main()`` symbol.
86 88
87 If modifying ``CFLAGS`` of a large project, which also compiles executables 89 If modifying ``CFLAGS`` of a large project, which also compiles executables
90 92
91 clang -fsanitize=fuzzer-no-link mytarget.c 93 clang -fsanitize=fuzzer-no-link mytarget.c
92 94
93 Then libFuzzer can be linked to the desired driver by passing in 95 Then libFuzzer can be linked to the desired driver by passing in
94 ``-fsanitize=fuzzer`` during the linking stage. 96 ``-fsanitize=fuzzer`` during the linking stage.
95
96 Using MemorySanitizer_ (MSAN) with libFuzzer is possible too, but tricky.
97 The exact details are out of scope, we expect to simplify this in future
98 versions.
99 97
100 .. _libfuzzer-corpus: 98 .. _libfuzzer-corpus:
101 99
102 Corpus 100 Corpus
103 ------ 101 ------
178 worker processes, by default using half of the available CPU cores; the count of 176 worker processes, by default using half of the available CPU cores; the count of
179 worker processes can be overridden by the ``-workers=N`` option. For example, 177 worker processes can be overridden by the ``-workers=N`` option. For example,
180 running with ``-jobs=30`` on a 12-core machine would run 6 workers by default, 178 running with ``-jobs=30`` on a 12-core machine would run 6 workers by default,
181 with each worker averaging 5 bugs by completion of the entire process. 179 with each worker averaging 5 bugs by completion of the entire process.
182 180
181 Fork mode
182 ---------
183
184 **Experimental** mode ``-fork=N`` (where ``N`` is the number of parallel jobs)
185 enables oom-, timeout-, and crash-resistant
186 fuzzing with separate processes (using ``fork-exec``, not just ``fork``).
187
188 The top libFuzzer process will not do any fuzzing itself, but will
189 spawn up to ``N`` concurrent child processes providing them
190 small random subsets of the corpus. After a child exits, the top process
191 merges the corpus generated by the child back to the main corpus.
192
193 Related flags:
194
195 ``-ignore_ooms``
196 True by default. If an OOM happens during fuzzing in one of the child processes,
197 the reproducer is saved on disk, and fuzzing continues.
198 ``-ignore_timeouts``
199 True by default, same as ``-ignore_ooms``, but for timeouts.
200 ``-ignore_crashes``
201 False by default, same as ``-ignore_ooms``, but for all other crashes.
202
203 The plan is to eventually replace ``-jobs=N`` and ``-workers=N`` with ``-fork=N``.
183 204
184 Resuming merge 205 Resuming merge
185 -------------- 206 --------------
186 207
187 Merging large corpora may be time consuming, and it is often desirable to do it 208 Merging large corpora may be time consuming, and it is often desirable to do it
235 ``-runs`` 256 ``-runs``
236 Number of individual test runs, -1 (the default) to run indefinitely. 257 Number of individual test runs, -1 (the default) to run indefinitely.
237 ``-max_len`` 258 ``-max_len``
238 Maximum length of a test input. If 0 (the default), libFuzzer tries to guess 259 Maximum length of a test input. If 0 (the default), libFuzzer tries to guess
239 a good value based on the corpus (and reports it). 260 a good value based on the corpus (and reports it).
261 ``len_control``
262 Try generating small inputs first, then try larger inputs over time.
263 Specifies the rate at which the length limit is increased (smaller == faster).
264 Default is 100. If 0, immediately try inputs with size up to max_len.
240 ``-timeout`` 265 ``-timeout``
241 Timeout in seconds, default 1200. If an input takes longer than this timeout, 266 Timeout in seconds, default 1200. If an input takes longer than this timeout,
242 the process is treated as a failure case. 267 the process is treated as a failure case.
243 ``-rss_limit_mb`` 268 ``-rss_limit_mb``
244 Memory usage limit in Mb, default 2048. Use 0 to disable the limit. 269 Memory usage limit in Mb, default 2048. Use 0 to disable the limit.
367 fuzzer processes (see `Parallel Fuzzing`_). 392 fuzzer processes (see `Parallel Fuzzing`_).
368 393
369 Each output line also reports the following statistics (when non-zero): 394 Each output line also reports the following statistics (when non-zero):
370 395
371 ``cov:`` 396 ``cov:``
372 Total number of code blocks or edges covered by the executing the current 397 Total number of code blocks or edges covered by executing the current corpus.
373 corpus.
374 ``ft:`` 398 ``ft:``
375 libFuzzer uses different signals to evaluate the code coverage: 399 libFuzzer uses different signals to evaluate the code coverage:
376 edge coverage, edge counters, value profiles, indirect caller/callee pairs, etc. 400 edge coverage, edge counters, value profiles, indirect caller/callee pairs, etc.
377 These signals combined are called *features* (`ft:`). 401 These signals combined are called *features* (`ft:`).
378 ``corp:`` 402 ``corp:``
379 Number of entries in the current in-memory test corpus and its size in bytes. 403 Number of entries in the current in-memory test corpus and its size in bytes.
404 ``lim:``
405 Current limit on the length of new entries in the corpus. Increases over time
406 until the max length (``-max_len``) is reached.
380 ``exec/s:`` 407 ``exec/s:``
381 Number of fuzzer iterations per second. 408 Number of fuzzer iterations per second.
382 ``rss:`` 409 ``rss:``
383 Current memory consumption. 410 Current memory consumption.
384 411
412 if (size > 2 && data[2] == '!') 439 if (size > 2 && data[2] == '!')
413 __builtin_trap(); 440 __builtin_trap();
414 return 0; 441 return 0;
415 } 442 }
416 EOF 443 EOF
417 # Build test_fuzzer.cc with asan and link against libFuzzer.a 444 # Build test_fuzzer.cc with asan and link against libFuzzer.
418 clang++ -fsanitize=address -fsanitize-coverage=trace-pc-guard test_fuzzer.cc libFuzzer.a 445 clang++ -fsanitize=address,fuzzer test_fuzzer.cc
419 # Run the fuzzer with no corpus. 446 # Run the fuzzer with no corpus.
420 ./a.out 447 ./a.out
421 448
422 You should get an error pretty quickly:: 449 You should get an error pretty quickly::
423 450
481 the fuzzing but is very likely to improve the results. 508 the fuzzing but is very likely to improve the results.
482 509
483 Value Profile 510 Value Profile
484 ------------- 511 -------------
485 512
486 With ``-fsanitize-coverage=trace-cmp`` 513 With ``-fsanitize-coverage=trace-cmp`` (default with ``-fsanitize=fuzzer``)
487 and extra run-time flag ``-use_value_profile=1`` the fuzzer will 514 and extra run-time flag ``-use_value_profile=1`` the fuzzer will
488 collect value profiles for the parameters of compare instructions 515 collect value profiles for the parameters of compare instructions
489 and treat some new values as new coverage. 516 and treat some new values as new coverage.
490 517
491 The current imlpementation does roughly the following: 518 The current imlpementation does roughly the following:
542 569
543 Periodically restart both fuzzers so that they can use each other's findings. 570 Periodically restart both fuzzers so that they can use each other's findings.
544 Currently, there is no simple way to run both fuzzing engines in parallel while sharing the same corpus dir. 571 Currently, there is no simple way to run both fuzzing engines in parallel while sharing the same corpus dir.
545 572
546 You may also use AFL on your target function ``LLVMFuzzerTestOneInput``: 573 You may also use AFL on your target function ``LLVMFuzzerTestOneInput``:
547 see an example `here <https://github.com/llvm-mirror/compiler-rt/tree/master/lib/fuzzer/afl>`__. 574 see an example `here <https://github.com/llvm/llvm-project/tree/master/compiler-rt/lib/fuzzer/afl>`__.
548 575
549 How good is my fuzzer? 576 How good is my fuzzer?
550 ---------------------- 577 ----------------------
551 578
552 Once you implement your target function ``LLVMFuzzerTestOneInput`` and fuzz it to death, 579 Once you implement your target function ``LLVMFuzzerTestOneInput`` and fuzz it to death,
560 587
561 588
562 User-supplied mutators 589 User-supplied mutators
563 ---------------------- 590 ----------------------
564 591
565 LibFuzzer allows to use custom (user-supplied) mutators, 592 LibFuzzer allows to use custom (user-supplied) mutators, see
566 see FuzzerInterface.h_ 593 `Structure-Aware Fuzzing <https://github.com/google/fuzzer-test-suite/blob/master/tutorial/structure-aware-fuzzing.md>`_
594 for more details.
567 595
568 Startup initialization 596 Startup initialization
569 ---------------------- 597 ----------------------
570 If the library being tested needs to be initialized, there are several options. 598 If the library being tested needs to be initialized, there are several options.
571 599
643 is enabled, it will also instrument the LLVM support code which will blow up the 671 is enabled, it will also instrument the LLVM support code which will blow up the
644 coverage set of the process (since the fuzzer is in-process). In other words, by 672 coverage set of the process (since the fuzzer is in-process). In other words, by
645 using more external dependencies we will slow down the fuzzer while the main 673 using more external dependencies we will slow down the fuzzer while the main
646 reason for it to exist is extreme speed. 674 reason for it to exist is extreme speed.
647 675
648 Q. What about Windows then? The fuzzer contains code that does not build on Windows. 676 Q. Does libFuzzer Support Windows?
649 ------------------------------------------------------------------------------------ 677 ------------------------------------------------------------------------------------
650 678
651 Volunteers are welcome. 679 Yes, libFuzzer now supports Windows. Initial support was added in r341082.
680 Any build of Clang 9 supports it. You can download a build of Clang for Windows
681 that has libFuzzer from
682 `LLVM Snapshot Builds <https://llvm.org/builds/>`_.
683
684 Using libFuzzer on Windows without ASAN is unsupported. Building fuzzers with the
685 ``/MD`` (dynamic runtime library) compile option is unsupported. Support for these
686 may be added in the future. Linking fuzzers with the ``/INCREMENTAL`` link option
687 (or the ``/DEBUG`` option which implies it) is also unsupported.
688
689 Send any questions or comments to the mailing list: libfuzzer(#)googlegroups.com
652 690
653 Q. When libFuzzer is not a good solution for a problem? 691 Q. When libFuzzer is not a good solution for a problem?
654 --------------------------------------------------------- 692 ---------------------------------------------------------
655 693
656 * If the test inputs are validated by the target library and the validator 694 * If the test inputs are validated by the target library and the validator
678 small inputs, each input takes < 10ms to run, and the library code is not expected 716 small inputs, each input takes < 10ms to run, and the library code is not expected
679 to crash on invalid inputs. 717 to crash on invalid inputs.
680 Examples: regular expression matchers, text or binary format parsers, compression, 718 Examples: regular expression matchers, text or binary format parsers, compression,
681 network, crypto. 719 network, crypto.
682 720
721 Q. LibFuzzer crashes on my complicated fuzz target (but works fine for me on smaller targets).
722 ----------------------------------------------------------------------------------------------
723
724 Check if your fuzz target uses ``dlclose``.
725 Currently, libFuzzer doesn't support targets that call ``dlclose``,
726 this may be fixed in future.
727
683 728
684 Trophies 729 Trophies
685 ======== 730 ========
686 * Thousands of bugs found on OSS-Fuzz: https://opensource.googleblog.com/2017/05/oss-fuzz-five-months-later-and.html 731 * Thousands of bugs found on OSS-Fuzz: https://opensource.googleblog.com/2017/05/oss-fuzz-five-months-later-and.html
687 732
739 .. _SanitizerCoverage: http://clang.llvm.org/docs/SanitizerCoverage.html 784 .. _SanitizerCoverage: http://clang.llvm.org/docs/SanitizerCoverage.html
740 .. _SanitizerCoverageTraceDataFlow: http://clang.llvm.org/docs/SanitizerCoverage.html#tracing-data-flow 785 .. _SanitizerCoverageTraceDataFlow: http://clang.llvm.org/docs/SanitizerCoverage.html#tracing-data-flow
741 .. _AddressSanitizer: http://clang.llvm.org/docs/AddressSanitizer.html 786 .. _AddressSanitizer: http://clang.llvm.org/docs/AddressSanitizer.html
742 .. _LeakSanitizer: http://clang.llvm.org/docs/LeakSanitizer.html 787 .. _LeakSanitizer: http://clang.llvm.org/docs/LeakSanitizer.html
743 .. _Heartbleed: http://en.wikipedia.org/wiki/Heartbleed 788 .. _Heartbleed: http://en.wikipedia.org/wiki/Heartbleed
744 .. _FuzzerInterface.h: https://github.com/llvm-mirror/compiler-rt/blob/master/lib/fuzzer/FuzzerInterface.h 789 .. _FuzzerInterface.h: https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/fuzzer/FuzzerInterface.h
745 .. _3.7.0: http://llvm.org/releases/3.7.0/docs/LibFuzzer.html 790 .. _3.7.0: http://llvm.org/releases/3.7.0/docs/LibFuzzer.html
746 .. _building Clang from trunk: http://clang.llvm.org/get_started.html 791 .. _building Clang from trunk: http://clang.llvm.org/get_started.html
747 .. _MemorySanitizer: http://clang.llvm.org/docs/MemorySanitizer.html 792 .. _MemorySanitizer: http://clang.llvm.org/docs/MemorySanitizer.html
748 .. _UndefinedBehaviorSanitizer: http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html 793 .. _UndefinedBehaviorSanitizer: http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
749 .. _`coverage counters`: http://clang.llvm.org/docs/SanitizerCoverage.html#coverage-counters 794 .. _`coverage counters`: http://clang.llvm.org/docs/SanitizerCoverage.html#coverage-counters