1 #include <Python.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 
6 #include <string>
7 
8 #include "pyutil.h"
9 
10 extern "C" {
11 
12 static PYCODETYPE *code;
13 
LLVMFuzzerInitialize(int * argc,char *** argv)14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 {
16 	contrib::initpy(*argv[0]);
17 	code = (PYCODETYPE *)Py_CompileString(R"py(
18 for inline in (True, False):
19     try:
20         index, cache = parsers.parse_index2(data, inline)
21         index.slicechunktodensity(list(range(len(index))), 0.5, 262144)
22         index.stats()
23         index.findsnapshots({}, 0)
24         10 in index
25         for rev in range(len(index)):
26             index.reachableroots(0, [len(index)-1], [rev])
27             node = index[rev][7]
28             partial = index.shortest(node)
29             index.partialmatch(node[:partial])
30             index.deltachain(rev, None, True)
31     except Exception as e:
32         pass
33         # uncomment this print if you're editing this Python code
34         # to debug failures.
35         # print e
36 )py",
37 	                                      "fuzzer", Py_file_input);
38 	return 0;
39 }
40 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)41 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
42 {
43 	// Don't allow fuzzer inputs larger than 60k, since we'll just bog
44 	// down and not accomplish much.
45 	if (Size > 60000) {
46 		return 0;
47 	}
48 	PyObject *text =
49 	    PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
50 	PyObject *locals = PyDict_New();
51 	PyDict_SetItemString(locals, "data", text);
52 	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
53 	if (!res) {
54 		PyErr_Print();
55 	}
56 	Py_XDECREF(res);
57 	Py_DECREF(locals);
58 	Py_DECREF(text);
59 	return 0; // Non-zero return values are reserved for future use.
60 }
61 }
62