xref: /aosp_15_r20/external/pigweed/pw_fuzzer/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_fuzzer:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=========
4*61c4878aSAndroid Build Coastguard Workerpw_fuzzer
5*61c4878aSAndroid Build Coastguard Worker=========
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_fuzzer
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard WorkerUse state of the art tools to automatically find bugs in your C++ code with 5
10*61c4878aSAndroid Build Coastguard Workerlines of code or less!
11*61c4878aSAndroid Build Coastguard Worker
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker   FUZZ_TEST(MyTestSuite, TestMyInterestingFunction)
16*61c4878aSAndroid Build Coastguard Worker     .WithDomains(Arbitrary<size_t>(), AsciiString());
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker----------
19*61c4878aSAndroid Build Coastguard WorkerBackground
20*61c4878aSAndroid Build Coastguard Worker----------
21*61c4878aSAndroid Build Coastguard WorkerYou've written some code. You've written unit tests for that code. The unit
22*61c4878aSAndroid Build Coastguard Workertests pass. But could there be bugs in inputs or code paths the unit tests do
23*61c4878aSAndroid Build Coastguard Workernot cover? `Fuzzing`_ can help!
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard WorkerHowever, fuzzing requires some complex interactions between compiler-added
26*61c4878aSAndroid Build Coastguard Worker`instrumentation`_, `compiler runtimes`_, code under test, and the fuzzer code
27*61c4878aSAndroid Build Coastguard Workeritself. And to be reliably useful, fuzzers need to be part of a continuous
28*61c4878aSAndroid Build Coastguard Workerfuzzing infrastructure, adding even more complexity.
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard WorkerSee :ref:`module-pw_fuzzer-concepts` to learn more about the different
31*61c4878aSAndroid Build Coastguard Workercomponents of a fuzzer and how they work together to discover hard-to-find bugs.
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker------------
34*61c4878aSAndroid Build Coastguard WorkerOur solution
35*61c4878aSAndroid Build Coastguard Worker------------
36*61c4878aSAndroid Build Coastguard Worker``pw_fuzzer`` makes it easier to write, build, run, and deploy fuzzers. It
37*61c4878aSAndroid Build Coastguard Workerprovides convenient integration with two fuzzing `engines`_:
38*61c4878aSAndroid Build Coastguard Worker
39*61c4878aSAndroid Build Coastguard Worker* `libFuzzer`_, allowing integration of legacy fuzzers.
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker* `FuzzTest`_, allowing easy creation of fuzzers from unit tests in around 5
42*61c4878aSAndroid Build Coastguard Worker  lines of code.
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard WorkerAdditionally, it produces artifacts for continuous fuzzing infrastructures such
45*61c4878aSAndroid Build Coastguard Workeras `ClusterFuzz`_ and `OSS-Fuzz`_, and provides the artifacts those systems need.
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard Worker---------------
48*61c4878aSAndroid Build Coastguard WorkerWho this is for
49*61c4878aSAndroid Build Coastguard Worker---------------
50*61c4878aSAndroid Build Coastguard Worker``pw_fuzzer`` is useful for authors of `wide range`_ of C++ code who have
51*61c4878aSAndroid Build Coastguard Workerwritten unit tests and are interested in finding actionable bugs in their code.
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard WorkerIn particular, coverage-guided is effective for testing and finding bugs in code
54*61c4878aSAndroid Build Coastguard Workerthat:
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker* Receives inputs from **untrusted sources** and must be secure.
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Worker* Has **complex algorithms** with some equivalence, e.g. compress and
59*61c4878aSAndroid Build Coastguard Worker  decompress, and must be correct.
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard Worker* Handles **high volumes** of inputs and/or unreliable dependencies and must be
62*61c4878aSAndroid Build Coastguard Worker  stable.
63*61c4878aSAndroid Build Coastguard Worker
64*61c4878aSAndroid Build Coastguard WorkerFuzzing works best when code handles inputs deterministically, that is, given
65*61c4878aSAndroid Build Coastguard Workerthe same input it behaves the same way. Fuzzing will be less effective with code
66*61c4878aSAndroid Build Coastguard Workerthat modifies global state or has some randomness, e.g. depends on how multiple
67*61c4878aSAndroid Build Coastguard Workerthreads are scheduled. Simply put, good fuzzers typically resemble unit tests in
68*61c4878aSAndroid Build Coastguard Workerterms of scope and isolation.
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard Worker--------------------
71*61c4878aSAndroid Build Coastguard WorkerIs it right for you?
72*61c4878aSAndroid Build Coastguard Worker--------------------
73*61c4878aSAndroid Build Coastguard WorkerCurrently, ``pw_fuzzer`` only provides support for fuzzers that:
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard Worker* Run on **host**. Sanitizer runtimes such as `AddressSanitizer`_ add
76*61c4878aSAndroid Build Coastguard Worker  significant memory overhead and are not suitable for running on device.
77*61c4878aSAndroid Build Coastguard Worker  Additionally, the currently supported engines assume the presence of certain
78*61c4878aSAndroid Build Coastguard Worker  POSIX features.
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard Worker* Are built with **Clang**. The `instrumentation`_ used in fuzzing is added by
81*61c4878aSAndroid Build Coastguard Worker  ``clang``.
82*61c4878aSAndroid Build Coastguard Worker
83*61c4878aSAndroid Build Coastguard Worker.. _module-pw_fuzzer-get-started:
84*61c4878aSAndroid Build Coastguard Worker
85*61c4878aSAndroid Build Coastguard Worker---------------
86*61c4878aSAndroid Build Coastguard WorkerGetting started
87*61c4878aSAndroid Build Coastguard Worker---------------
88*61c4878aSAndroid Build Coastguard WorkerThe first step in adding a fuzzer is to determine what fuzzing engine should you
89*61c4878aSAndroid Build Coastguard Workeruse. Pigweed currently supports two fuzzing engines:
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Worker* **FuzzTest** is the recommended engine. It makes it easy to create fuzzers
92*61c4878aSAndroid Build Coastguard Worker  from your existing unit tests, but it does requires additional third party
93*61c4878aSAndroid Build Coastguard Worker  dependencies and at least C++17. See
94*61c4878aSAndroid Build Coastguard Worker  :ref:`module-pw_fuzzer-guides-using_fuzztest` for details on how to set up a
95*61c4878aSAndroid Build Coastguard Worker  project to use FuzzTest and on how to create and run fuzzers with it.
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard Worker* **libFuzzer** is a mature, proven engine. It is a part of LLVM and requires
98*61c4878aSAndroid Build Coastguard Worker  code authors to implement a specific function, ``LLVMFuzzerTestOneInput``. See
99*61c4878aSAndroid Build Coastguard Worker  :ref:`module-pw_fuzzer-guides-using_libfuzzer` for details on how to write
100*61c4878aSAndroid Build Coastguard Worker  fuzzers with it.
101*61c4878aSAndroid Build Coastguard Worker
102*61c4878aSAndroid Build Coastguard Worker-------
103*61c4878aSAndroid Build Coastguard WorkerRoadmap
104*61c4878aSAndroid Build Coastguard Worker-------
105*61c4878aSAndroid Build Coastguard Worker``pw_fuzzer`` is under active development. There are a number of improvements we
106*61c4878aSAndroid Build Coastguard Workerwould like to add, including:
107*61c4878aSAndroid Build Coastguard Worker
108*61c4878aSAndroid Build Coastguard Worker* `b/282560789`_ - Document workflows for analyzing coverage and improving
109*61c4878aSAndroid Build Coastguard Worker  fuzzers.
110*61c4878aSAndroid Build Coastguard Worker
111*61c4878aSAndroid Build Coastguard Worker* `b/280457542`_ - Add CMake support for FuzzTest.
112*61c4878aSAndroid Build Coastguard Worker
113*61c4878aSAndroid Build Coastguard Worker* `b/281138993`_ - Add a ``pw_cli`` plugin for fuzzing.
114*61c4878aSAndroid Build Coastguard Worker
115*61c4878aSAndroid Build Coastguard Worker* `b/281139237`_ - Develop OSS-Fuzz and ClusterFuzz workflow templates for
116*61c4878aSAndroid Build Coastguard Worker  downtream projects.
117*61c4878aSAndroid Build Coastguard Worker
118*61c4878aSAndroid Build Coastguard Worker.. _b/282560789: https://issues.pigweed.dev/issues/282560789
119*61c4878aSAndroid Build Coastguard Worker.. _b/280457542: https://issues.pigweed.dev/issues/280457542
120*61c4878aSAndroid Build Coastguard Worker.. _b/281138993: https://issues.pigweed.dev/issues/281138993
121*61c4878aSAndroid Build Coastguard Worker.. _b/281139237: https://issues.pigweed.dev/issues/281139237
122*61c4878aSAndroid Build Coastguard Worker
123*61c4878aSAndroid Build Coastguard Worker.. toctree::
124*61c4878aSAndroid Build Coastguard Worker   :maxdepth: 1
125*61c4878aSAndroid Build Coastguard Worker   :hidden:
126*61c4878aSAndroid Build Coastguard Worker
127*61c4878aSAndroid Build Coastguard Worker   concepts
128*61c4878aSAndroid Build Coastguard Worker   guides/fuzztest
129*61c4878aSAndroid Build Coastguard Worker   guides/libfuzzer
130*61c4878aSAndroid Build Coastguard Worker   guides/reproducing_oss_fuzz_bugs
131*61c4878aSAndroid Build Coastguard Worker
132*61c4878aSAndroid Build Coastguard Worker.. inclusive-language: disable
133*61c4878aSAndroid Build Coastguard Worker.. _AddressSanitizer: https://github.com/google/sanitizers/wiki/AddressSanitizer
134*61c4878aSAndroid Build Coastguard Worker.. _ClusterFuzz: https://github.com/google/clusterfuzz/
135*61c4878aSAndroid Build Coastguard Worker.. _compiler runtimes: https://compiler-rt.llvm.org/
136*61c4878aSAndroid Build Coastguard Worker.. _engines: https://github.com/google/fuzzing/blob/master/docs/glossary.md#fuzzing-engine
137*61c4878aSAndroid Build Coastguard Worker.. _Fuzzing: https://github.com/google/fuzzing/blob/master/docs/intro-to-fuzzing.md
138*61c4878aSAndroid Build Coastguard Worker.. _FuzzTest: https://github.com/google/fuzztest
139*61c4878aSAndroid Build Coastguard Worker.. _instrumentation: https://clang.llvm.org/docs/SanitizerCoverage.html#instrumentation-points
140*61c4878aSAndroid Build Coastguard Worker.. _libFuzzer: https://llvm.org/docs/LibFuzzer.html
141*61c4878aSAndroid Build Coastguard Worker.. _OSS-Fuzz: https://github.com/google/oss-fuzz
142*61c4878aSAndroid Build Coastguard Worker.. _wide range: https://github.com/google/fuzzing/blob/master/docs/why-fuzz.md
143*61c4878aSAndroid Build Coastguard Worker.. inclusive-language: enable
144