author | Gregory Szorc <gregory.szorc@gmail.com> |
Tue, 22 Jan 2019 18:16:53 -0800 | |
changeset 41321 | 724b4606528e |
parent 41024 | 6a951f535fee |
child 43859 | 8766728dbce6 |
permissions | -rw-r--r-- |
41024
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
1 |
#include <Python.h> |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
2 |
#include <assert.h> |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
3 |
#include <stdlib.h> |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
4 |
#include <unistd.h> |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
5 |
|
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
6 |
#include <string> |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
7 |
|
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
8 |
#include "pyutil.h" |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
9 |
|
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
10 |
extern "C" { |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
11 |
|
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
12 |
static PyCodeObject *code; |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
13 |
|
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
14 |
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
15 |
{ |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
16 |
contrib::initpy(*argv[0]); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
17 |
code = (PyCodeObject *)Py_CompileString(R"py( |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
18 |
from parsers import fm1readmarkers |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
19 |
def maybeint(s, default): |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
20 |
try: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
21 |
return int(s) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
22 |
except ValueError: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
23 |
return default |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
24 |
try: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
25 |
parts = data.split('\0', 2) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
26 |
if len(parts) == 3: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
27 |
offset, stop, data = parts |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
28 |
elif len(parts) == 2: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
29 |
stop, data = parts |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
30 |
offset = 0 |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
31 |
else: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
32 |
offset = stop = 0 |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
33 |
offset, stop = maybeint(offset, 0), maybeint(stop, len(data)) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
34 |
fm1readmarkers(data, offset, stop) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
35 |
except Exception as e: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
36 |
pass |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
37 |
# uncomment this print if you're editing this Python code |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
38 |
# to debug failures. |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
39 |
# print e |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
40 |
)py", |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
41 |
"fuzzer", Py_file_input); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
42 |
return 0; |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
43 |
} |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
44 |
|
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
45 |
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
46 |
{ |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
47 |
PyObject *text = |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
48 |
PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
49 |
PyObject *locals = PyDict_New(); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
50 |
PyDict_SetItemString(locals, "data", text); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
51 |
PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
52 |
if (!res) { |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
53 |
PyErr_Print(); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
54 |
} |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
55 |
Py_XDECREF(res); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
56 |
Py_DECREF(locals); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
57 |
Py_DECREF(text); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
58 |
return 0; // Non-zero return values are reserved for future use. |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
59 |
} |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
60 |
} |