##// END OF EJS Templates
fuzz: move many initialization steps into LLVMFuzzerInitialize...
Augie Fackler -
r40409:c3ab0a89 default
parent child Browse files
Show More
@@ -1,81 +1,83 b''
1 1 #include <Python.h>
2 2 #include <assert.h>
3 3 #include <stdlib.h>
4 4 #include <unistd.h>
5 5
6 6 #include <string>
7 7
8 8 extern "C" {
9 9
10 10 /* TODO: use Python 3 for this fuzzing? */
11 11 PyMODINIT_FUNC initparsers(void);
12 12
13 13 static char cpypath[8192] = "\0";
14 14
15 static PyCodeObject *code;
16 static PyObject *mainmod;
17 static PyObject *globals;
18
15 19 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
16 20 {
17 21 const std::string subdir = "/sanpy/lib/python2.7";
18 22 /* HACK ALERT: we need a full Python installation built without
19 23 pymalloc and with ASAN, so we dump one in
20 24 $OUT/sanpy/lib/python2.7. This helps us wire that up. */
21 25 std::string selfpath(*argv[0]);
22 26 std::string pypath;
23 27 auto pos = selfpath.rfind("/");
24 28 if (pos == std::string::npos) {
25 29 char wd[8192];
26 30 getcwd(wd, 8192);
27 31 pypath = std::string(wd) + subdir;
28 32 } else {
29 33 pypath = selfpath.substr(0, pos) + subdir;
30 34 }
31 35 strncpy(cpypath, pypath.c_str(), pypath.size());
32 36 setenv("PYTHONPATH", cpypath, 1);
33 37 setenv("PYTHONNOUSERSITE", "1", 1);
34 38 /* prevent Python from looking up users in the fuzz environment */
35 39 setenv("PYTHONUSERBASE", cpypath, 1);
36 40 Py_SetPythonHome(cpypath);
37 41 Py_InitializeEx(0);
38 return 0;
39 }
40
41 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
42 {
43 42 initparsers();
44 PyObject *mtext =
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(
43 code = (PyCodeObject *)Py_CompileString(R"py(
52 44 from parsers import lazymanifest
53 45 try:
54 46 lm = lazymanifest(mdata)
55 47 # iterate the whole thing, which causes the code to fully parse
56 48 # every line in the manifest
57 49 list(lm.iterentries())
58 50 lm[b'xyzzy'] = (b'\0' * 20, 'x')
59 51 # do an insert, text should change
60 52 assert lm.text() != mdata, "insert should change text and didn't: %r %r" % (lm.text(), mdata)
61 53 del lm[b'xyzzy']
62 54 # should be back to the same
63 55 assert lm.text() == mdata, "delete should have restored text but didn't: %r %r" % (lm.text(), mdata)
64 56 except Exception as e:
65 57 pass
66 58 # uncomment this print if you're editing this Python code
67 59 # to debug failures.
68 60 # print e
69 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 74 PyObject *res = PyEval_EvalCode(code, globals, locals);
72 75 if (!res) {
73 76 PyErr_Print();
74 77 }
75 78 Py_XDECREF(res);
76 Py_DECREF(code);
77 79 Py_DECREF(locals);
78 80 Py_DECREF(mtext);
79 81 return 0; // Non-zero return values are reserved for future use.
80 82 }
81 83 }
General Comments 0
You need to be logged in to leave comments. Login now