contrib/fuzz/jsonescapeu8fast.cc
author Martin von Zweigbergk <martinvonz@google.com>
Thu, 31 Mar 2022 22:02:46 -0700
changeset 49061 81d293eb5264
parent 43859 8766728dbce6
permissions -rw-r--r--
rust-requirements: allow loading repos with `bookmarksinstore` requirement `rhg` does support bookmarks, so it can load repos with the `bookmarksinstore` requirement just as well as other repos. Differential Revision: https://phab.mercurial-scm.org/D12425
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
43859
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43813
diff changeset
    14
static PYCODETYPE *code;
43153
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]);
43859
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43813
diff changeset
    19
	code = (PYCODETYPE *)Py_CompileString(R"py(
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
try:
43859
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43813
diff changeset
    21
    parsers.jsonescapeu8fast(data, paranoid)
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
except Exception as e:
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
    pass
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
    # 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
    25
    # to debug failures.
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
    # print(e)
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
)py",
43859
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43813
diff changeset
    28
	                                      "fuzzer", Py_file_input);
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
	if (!code) {
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
		std::cerr << "failed to compile Python code!" << std::endl;
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
	}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
	return 0;
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
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
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
	FuzzedDataProvider provider(Data, Size);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
	bool paranoid = provider.ConsumeBool();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
	std::string remainder = provider.ConsumeRemainingBytesAsString();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
	PyObject *mtext = PyBytes_FromStringAndSize(
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
	    (const char *)remainder.c_str(), remainder.size());
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
	PyObject *locals = PyDict_New();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
	PyDict_SetItemString(locals, "data", mtext);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
	PyDict_SetItemString(locals, "paranoid", paranoid ? Py_True : Py_False);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
	if (!res) {
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
		PyErr_Print();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
	}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
	Py_XDECREF(res);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
	Py_DECREF(locals);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
	Py_DECREF(mtext);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
	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
    54
}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
}