README.rst 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. Fuzz Tests for CPython
  2. ======================
  3. These fuzz tests are designed to be included in Google's `oss-fuzz`_ project.
  4. oss-fuzz works against a library exposing a function of the form
  5. ``int LLVMFuzzerTestOneInput(const uint8_t* data, size_t length)``. We provide
  6. that library (``fuzzer.c``), and include a ``_fuzz`` module for testing with
  7. some toy values -- no fuzzing occurs in Python's test suite.
  8. oss-fuzz will regularly pull from CPython, discover all the tests in
  9. ``fuzz_tests.txt``, and run them -- so adding a new test here means it will
  10. automatically be run in oss-fuzz, while also being smoke-tested as part of
  11. CPython's test suite.
  12. Adding a new fuzz test
  13. ----------------------
  14. Add the test name on a new line in ``fuzz_tests.txt``.
  15. In ``fuzzer.c``, add a function to be run::
  16. static int $fuzz_test_name(const char* data, size_t size) {
  17. ...
  18. return 0;
  19. }
  20. And invoke it from ``LLVMFuzzerTestOneInput``::
  21. #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_$fuzz_test_name)
  22. rv |= _run_fuzz(data, size, $fuzz_test_name);
  23. #endif
  24. Don't forget to replace ``$fuzz_test_name`` with your actual test name.
  25. ``LLVMFuzzerTestOneInput`` will run in oss-fuzz, with each test in
  26. ``fuzz_tests.txt`` run separately.
  27. Seed data (corpus) for the test can be provided in a subfolder called
  28. ``<test_name>_corpus`` such as ``fuzz_json_loads_corpus``. A wide variety
  29. of good input samples allows the fuzzer to more easily explore a diverse
  30. set of paths and provides a better base to find buggy input from.
  31. Dictionaries of tokens (see oss-fuzz documentation for more details) can
  32. be placed in the ``dictionaries`` folder with the name of the test.
  33. For example, ``dictionaries/fuzz_json_loads.dict`` contains JSON tokens
  34. to guide the fuzzer.
  35. What makes a good fuzz test
  36. ---------------------------
  37. Libraries written in C that might handle untrusted data are worthwhile. The
  38. more complex the logic (e.g. parsing), the more likely this is to be a useful
  39. fuzz test. See the existing examples for reference, and refer to the
  40. `oss-fuzz`_ docs.
  41. .. _oss-fuzz: https://github.com/google/oss-fuzz