Debian Codegraph @

hypothesis-python/docs/how-to/external-fuzzers.rst

raw
1 Use Hypothesis with an external fuzzer
2 ======================================
3
4 Sometimes you might want to point a traditional fuzzer like `python-afl <https://github.com/jwilk/python-afl>`__ or Google's :pypi:`atheris` at your code, to get coverage-guided exploration of native C extensions. The associated tooling is often much less mature than property-based testing libraries though, so you might want to use Hypothesis strategies to describe your input data, and our world-class shrinking and :ref:`observability <observability>` tools to wrangle the results. That's exactly what this how-to guide is about!
5
6 .. note::
7
8 If you already have Hypothesis tests and want to fuzz them, or are targeting pure Python code, we strongly recommend the purpose-built `HypoFuzz <https://hypofuzz.com/>`_.
9 This page is about writing traditional 'fuzz harnesses' with an external fuzzer, using parts of Hypothesis.
10
11 In order to support this workflow, Hypothesis exposes the |fuzz_one_input| method. |fuzz_one_input| takes a bytestring, parses it into a test case, and executes the corresponding test once. This means you can treat each of your Hypothesis tests as a traditional fuzz target, by pointing the fuzzer at |fuzz_one_input|.
12
13 For example:
14
15 .. code-block:: python
16
17 from hypothesis import given, strategies as st
18
19 @given(st.integers())
20 def test_ints(n):
21 pass
22
23 # this parses the bytestring into a test case using st.integers(),
24 # and then executes `test_ints` once.
25 test_ints.hypothesis.fuzz_one_input(b"\x00" * 50)
26
27 Note that |fuzz_one_input| bypasses the standard test lifecycle. In a standard test run, Hypothesis is responsible for managing the lifecycle of a test, for example by moving between each |Phase|. In contrast, |fuzz_one_input| executes one test case, independent of this lifecycle.
28
29 See the documentation of |fuzz_one_input| for details of how it interacts with other features of Hypothesis, such as |@settings|.
30
31 Worked example: using Atheris
32 -----------------------------
33
34 Here is an example that uses |fuzz_one_input| with the `Atheris <https://github.com/google/atheris>`__ coverage-guided fuzzer (which is built on top of `libFuzzer <https://llvm.org/docs/LibFuzzer.html>`_):
35
36 .. code-block:: python
37
38 import json
39 import sys
40
41 import atheris
42
43 from hypothesis import given, strategies as st
44
45 @given(
46 st.recursive(
47 st.none() | st.booleans() | st.integers() | st.floats() | st.text(),
48 lambda j: st.lists(j) | st.dictionaries(st.text(), j),
49 )
50 )
51 def test_json_dumps_valid_json(value):
52 json.dumps(value)
53
54 atheris.Setup(sys.argv, test_json_dumps_valid_json.hypothesis.fuzz_one_input)
55 atheris.Fuzz()
56
57 Generating valid JSON objects based only on Atheris' ``FuzzDataProvider`` interface would be considerably more difficult.
58
59 You may also want to use ``atheris.instrument_all`` or ``atheris.instrument_imports`` in order to add coverage instrumentation to Atheris. See the `Atheris <https://github.com/google/atheris>`__ documentation for full details.