Mercurial > hg
view contrib/fuzz/jsonescapeu8fast.cc @ 43813:5a9e2ae9899b
fuzz: use a more standard approach to allow local builds of fuzzers
This is taken from the (improved since we started fuzzing) guide on ideal
integrations. Rather than have our own wonky targets for building outside the
fuzzer universe, we have a driver program we carry along and use when we're
not using LibFuzzer. This will let us jettison a fair amount of goo.
contrib/fuzz/standalone_fuzz_target_runner.cc is
https://github.com/google/oss-fuzz/ file
projects/example/my-api-repo/standalone from git revision
c4579d9358a73ea5dbcc99cb985de1f2bf76dcf7, reformatted with out
clang-format settings and a no-check-code comment added. It allows
running a single test input through a fuzzer, rather than performing
ongoing fuzzing as libfuzzer would.
contrib/fuzz/FuzzedDataProvider.h is
https://github.com/llvm/llvm-project/ file
/compiler-rt/include/fuzzer/FuzzedDataProvider.h from git revision
a44ef027ebca1598892ea9b104d6189aeb3bc2f0, reformatted with our
clang-format settings and a no-check-code comment added. We can
discard this if we instead want to add an hghave check for a new
enough llvm that includes FuzzedDataProvder.h in the fuzzer headers.
Differential Revision: https://phab.mercurial-scm.org/D7564
author | Augie Fackler <augie@google.com> |
---|---|
date | Fri, 06 Dec 2019 15:19:47 -0500 |
parents | 741fb1a95da2 |
children | 8766728dbce6 |
line wrap: on
line source
#include <Python.h> #include <assert.h> #include <stdlib.h> #include <unistd.h> #include "pyutil.h" #include <iostream> #include <string> #include "FuzzedDataProvider.h" extern "C" { static PyCodeObject *code; extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { contrib::initpy(*argv[0]); code = (PyCodeObject *)Py_CompileString(R"py( from parsers import jsonescapeu8fast try: jsonescapeu8fast(data, paranoid) except Exception as e: pass # uncomment this print if you're editing this Python code # to debug failures. # print(e) )py", "fuzzer", Py_file_input); if (!code) { std::cerr << "failed to compile Python code!" << std::endl; } return 0; } int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { FuzzedDataProvider provider(Data, Size); bool paranoid = provider.ConsumeBool(); std::string remainder = provider.ConsumeRemainingBytesAsString(); PyObject *mtext = PyBytes_FromStringAndSize( (const char *)remainder.c_str(), remainder.size()); PyObject *locals = PyDict_New(); PyDict_SetItemString(locals, "data", mtext); PyDict_SetItemString(locals, "paranoid", paranoid ? Py_True : Py_False); PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); if (!res) { PyErr_Print(); } Py_XDECREF(res); Py_DECREF(locals); Py_DECREF(mtext); return 0; // Non-zero return values are reserved for future use. } }