Mercurial > hg
comparison contrib/fuzz/fm1readmarkers.cc @ 41024:6a951f535fee
fuzz: new fuzzer for parsers.fm1readmarkers
Differential Revision: https://phab.mercurial-scm.org/D5465
author | Augie Fackler <augie@google.com> |
---|---|
date | Thu, 20 Dec 2018 01:22:58 -0500 |
parents | |
children | 8766728dbce6 |
comparison
equal
deleted
inserted
replaced
41016:5c68b617ba24 | 41024:6a951f535fee |
---|---|
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 fm1readmarkers | |
19 def maybeint(s, default): | |
20 try: | |
21 return int(s) | |
22 except ValueError: | |
23 return default | |
24 try: | |
25 parts = data.split('\0', 2) | |
26 if len(parts) == 3: | |
27 offset, stop, data = parts | |
28 elif len(parts) == 2: | |
29 stop, data = parts | |
30 offset = 0 | |
31 else: | |
32 offset = stop = 0 | |
33 offset, stop = maybeint(offset, 0), maybeint(stop, len(data)) | |
34 fm1readmarkers(data, offset, stop) | |
35 except Exception as e: | |
36 pass | |
37 # uncomment this print if you're editing this Python code | |
38 # to debug failures. | |
39 # print e | |
40 )py", | |
41 "fuzzer", Py_file_input); | |
42 return 0; | |
43 } | |
44 | |
45 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) | |
46 { | |
47 PyObject *text = | |
48 PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size); | |
49 PyObject *locals = PyDict_New(); | |
50 PyDict_SetItemString(locals, "data", text); | |
51 PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); | |
52 if (!res) { | |
53 PyErr_Print(); | |
54 } | |
55 Py_XDECREF(res); | |
56 Py_DECREF(locals); | |
57 Py_DECREF(text); | |
58 return 0; // Non-zero return values are reserved for future use. | |
59 } | |
60 } |