##// END OF EJS Templates
fuzz: move many initialization steps into LLVMFuzzerInitialize...
Augie Fackler -
r40409:c3ab0a89 default
parent child Browse files
Show More
@@ -12,6 +12,10 b' PyMODINIT_FUNC initparsers(void);'
12
12
13 static char cpypath[8192] = "\0";
13 static char cpypath[8192] = "\0";
14
14
15 static PyCodeObject *code;
16 static PyObject *mainmod;
17 static PyObject *globals;
18
15 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
19 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
16 {
20 {
17 const std::string subdir = "/sanpy/lib/python2.7";
21 const std::string subdir = "/sanpy/lib/python2.7";
@@ -35,20 +39,8 b' extern "C" int LLVMFuzzerInitialize(int '
35 setenv("PYTHONUSERBASE", cpypath, 1);
39 setenv("PYTHONUSERBASE", cpypath, 1);
36 Py_SetPythonHome(cpypath);
40 Py_SetPythonHome(cpypath);
37 Py_InitializeEx(0);
41 Py_InitializeEx(0);
38 return 0;
39 }
40
41 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
42 {
43 initparsers();
42 initparsers();
44 PyObject *mtext =
43 code = (PyCodeObject *)Py_CompileString(R"py(
45 PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
46 PyObject *mainmod = PyImport_AddModule("__main__");
47 PyObject *globals = PyModule_GetDict(mainmod);
48 PyObject *locals = PyDict_New();
49 PyDict_SetItemString(locals, "mdata", mtext);
50 PyCodeObject *code =
51 (PyCodeObject *)Py_CompileString(R"py(
52 from parsers import lazymanifest
44 from parsers import lazymanifest
53 try:
45 try:
54 lm = lazymanifest(mdata)
46 lm = lazymanifest(mdata)
@@ -67,13 +59,23 b' except Exception as e:'
67 # to debug failures.
59 # to debug failures.
68 # print e
60 # print e
69 )py",
61 )py",
70 "fuzzer", Py_file_input);
62 "fuzzer", Py_file_input);
63 mainmod = PyImport_AddModule("__main__");
64 globals = PyModule_GetDict(mainmod);
65 return 0;
66 }
67
68 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
69 {
70 PyObject *mtext =
71 PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
72 PyObject *locals = PyDict_New();
73 PyDict_SetItemString(locals, "mdata", mtext);
71 PyObject *res = PyEval_EvalCode(code, globals, locals);
74 PyObject *res = PyEval_EvalCode(code, globals, locals);
72 if (!res) {
75 if (!res) {
73 PyErr_Print();
76 PyErr_Print();
74 }
77 }
75 Py_XDECREF(res);
78 Py_XDECREF(res);
76 Py_DECREF(code);
77 Py_DECREF(locals);
79 Py_DECREF(locals);
78 Py_DECREF(mtext);
80 Py_DECREF(mtext);
79 return 0; // Non-zero return values are reserved for future use.
81 return 0; // Non-zero return values are reserved for future use.
General Comments 0
You need to be logged in to leave comments. Login now