author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Mon, 03 May 2021 12:22:36 +0200 | |
changeset 47150 | 8d3c2f9d4af7 |
parent 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 |
|
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
41024
diff
changeset
|
12 |
static PYCODETYPE *code; |
41024
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]); |
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
41024
diff
changeset
|
17 |
code = (PYCODETYPE *)Py_CompileString(R"py( |
41024
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
18 |
def maybeint(s, default): |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
19 |
try: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
20 |
return int(s) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
21 |
except ValueError: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
22 |
return default |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
23 |
try: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
24 |
parts = data.split('\0', 2) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
25 |
if len(parts) == 3: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
26 |
offset, stop, data = parts |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
27 |
elif len(parts) == 2: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
28 |
stop, data = parts |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
29 |
offset = 0 |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
30 |
else: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
31 |
offset = stop = 0 |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
32 |
offset, stop = maybeint(offset, 0), maybeint(stop, len(data)) |
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
41024
diff
changeset
|
33 |
parsers.fm1readmarkers(data, offset, stop) |
41024
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
34 |
except Exception as e: |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
35 |
pass |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
36 |
# 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
|
37 |
# to debug failures. |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
38 |
# print e |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
39 |
)py", |
43859
8766728dbce6
fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents:
41024
diff
changeset
|
40 |
"fuzzer", Py_file_input); |
41024
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
41 |
return 0; |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
42 |
} |
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 |
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
45 |
{ |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
46 |
PyObject *text = |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
47 |
PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
48 |
PyObject *locals = PyDict_New(); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
49 |
PyDict_SetItemString(locals, "data", text); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
50 |
PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
51 |
if (!res) { |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
52 |
PyErr_Print(); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
53 |
} |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
54 |
Py_XDECREF(res); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
55 |
Py_DECREF(locals); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
56 |
Py_DECREF(text); |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
57 |
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
|
58 |
} |
6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
59 |
} |