contrib/fuzz/jsonescapeu8fast.cc
author Augie Fackler <augie@google.com>
Fri, 06 Dec 2019 15:19:47 -0500
changeset 43813 5a9e2ae9899b
parent 43153 741fb1a95da2
child 43859 8766728dbce6
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
#include <Python.h>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#include <assert.h>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
#include <stdlib.h>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#include <unistd.h>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
#include "pyutil.h"
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
#include <iostream>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
#include <string>
43813
5a9e2ae9899b fuzz: use a more standard approach to allow local builds of fuzzers
Augie Fackler <augie@google.com>
parents: 43153
diff changeset
    10
#include "FuzzedDataProvider.h"
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
extern "C" {
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
static PyCodeObject *code;
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
{
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
	contrib::initpy(*argv[0]);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
	code = (PyCodeObject *)Py_CompileString(R"py(
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
from parsers import jsonescapeu8fast
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
try:
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
    jsonescapeu8fast(data, paranoid)
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
except Exception as e:
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
    pass
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
    # uncomment this print if you're editing this Python code
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
    # to debug failures.
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
    # print(e)
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
)py",
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
	                                        "fuzzer", Py_file_input);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
	if (!code) {
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
		std::cerr << "failed to compile Python code!" << std::endl;
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
	}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
	return 0;
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
{
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
	FuzzedDataProvider provider(Data, Size);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
	bool paranoid = provider.ConsumeBool();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
	std::string remainder = provider.ConsumeRemainingBytesAsString();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
	PyObject *mtext = PyBytes_FromStringAndSize(
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
	    (const char *)remainder.c_str(), remainder.size());
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
	PyObject *locals = PyDict_New();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
	PyDict_SetItemString(locals, "data", mtext);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
	PyDict_SetItemString(locals, "paranoid", paranoid ? Py_True : Py_False);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
	if (!res) {
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
		PyErr_Print();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
	}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
	Py_XDECREF(res);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
	Py_DECREF(locals);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
	Py_DECREF(mtext);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
	return 0; // Non-zero return values are reserved for future use.
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
}