contrib/fuzz/revlog.cc
changeset 41014 c06f0ef9a5ba
child 41309 afc33a5705b9
equal deleted inserted replaced
41013:ef103c96ed33 41014:c06f0ef9a5ba
       
     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 PyCodeObject *code;
       
    13 
       
    14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
       
    15 {
       
    16 	contrib::initpy(*argv[0]);
       
    17 	code = (PyCodeObject *)Py_CompileString(R"py(
       
    18 from parsers import parse_index2
       
    19 for inline in (True, False):
       
    20     try:
       
    21         index, cache = parse_index2(data, inline)
       
    22     except Exception as e:
       
    23         pass
       
    24         # uncomment this print if you're editing this Python code
       
    25         # to debug failures.
       
    26         # print e
       
    27 )py",
       
    28 	                                        "fuzzer", Py_file_input);
       
    29 	return 0;
       
    30 }
       
    31 
       
    32 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
       
    33 {
       
    34 	PyObject *text =
       
    35 	    PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
       
    36 	PyObject *locals = PyDict_New();
       
    37 	PyDict_SetItemString(locals, "data", text);
       
    38 	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
       
    39 	if (!res) {
       
    40 		PyErr_Print();
       
    41 	}
       
    42 	Py_XDECREF(res);
       
    43 	Py_DECREF(locals);
       
    44 	Py_DECREF(text);
       
    45 	return 0; // Non-zero return values are reserved for future use.
       
    46 }
       
    47 }