| 1 |
#include <Python.h> |
| 2 |
#include <assert.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "pyutil.h" |
| 7 |
|
| 8 |
#include <string> |
| 9 |
|
| 10 |
extern "C" { |
| 11 |
|
| 12 |
static PYCODETYPE *code; |
| 13 |
|
| 14 |
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) |
| 15 |
{ |
| 16 |
contrib::initpy(*argv[0]); |
| 17 |
code = (PYCODETYPE *)Py_CompileString(R"py( |
| 18 |
try: |
| 19 |
files = mdata.split('\n') |
| 20 |
d = parsers.dirs(files) |
| 21 |
list(d) |
| 22 |
'a' in d |
| 23 |
if files: |
| 24 |
files[0] in d |
| 25 |
except Exception as e: |
| 26 |
pass |
| 27 |
# uncomment this print if you're editing this Python code |
| 28 |
# to debug failures. |
| 29 |
# print e |
| 30 |
)py", |
| 31 |
"fuzzer", Py_file_input); |
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
| 36 |
{ |
| 37 |
// Don't allow fuzzer inputs larger than 100k, since we'll just bog |
| 38 |
// down and not accomplish much. |
| 39 |
if (Size > 100000) { |
| 40 |
return 0; |
| 41 |
} |
| 42 |
PyObject *mtext = |
| 43 |
PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size); |
| 44 |
PyObject *locals = PyDict_New(); |
| 45 |
PyDict_SetItemString(locals, "mdata", mtext); |
| 46 |
PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); |
| 47 |
if (!res) { |
| 48 |
PyErr_Print(); |
| 49 |
} |
| 50 |
Py_XDECREF(res); |
| 51 |
Py_DECREF(locals); |
| 52 |
Py_DECREF(mtext); |
| 53 |
return 0; // Non-zero return values are reserved for future use. |
| 54 |
} |
| 55 |
} |