author | Martin von Zweigbergk <martinvonz@google.com> |
Thu, 23 Sep 2021 09:38:45 -0700 | |
changeset 48030 | 7d908ee19b5b |
parent 43859 | 8766728dbce6 |
permissions | -rw-r--r-- |
43150 | 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 |
||
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
43150
diff
changeset
|
12 |
static PYCODETYPE *code; |
43150 | 13 |
|
14 |
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) |
|
15 |
{ |
|
16 |
contrib::initpy(*argv[0]); |
|
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
43150
diff
changeset
|
17 |
code = (PYCODETYPE *)Py_CompileString(R"py( |
43150 | 18 |
try: |
19 |
files = mdata.split('\n') |
|
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
43150
diff
changeset
|
20 |
d = parsers.dirs(files) |
43150 | 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", |
|
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
43150
diff
changeset
|
31 |
"fuzzer", Py_file_input); |
43150 | 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 |
} |