contrib/fuzz/manifest.cc
changeset 41013 ef103c96ed33
parent 40373 c3ab0a89331d
child 41311 44cd432aed9f
--- a/contrib/fuzz/manifest.cc	Wed Dec 19 23:40:37 2018 -0500
+++ b/contrib/fuzz/manifest.cc	Wed Dec 19 21:57:23 2018 -0500
@@ -3,43 +3,17 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include "pyutil.h"
+
 #include <string>
 
 extern "C" {
 
-/* TODO: use Python 3 for this fuzzing? */
-PyMODINIT_FUNC initparsers(void);
-
-static char cpypath[8192] = "\0";
-
 static PyCodeObject *code;
-static PyObject *mainmod;
-static PyObject *globals;
 
 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
 {
-	const std::string subdir = "/sanpy/lib/python2.7";
-	/* HACK ALERT: we need a full Python installation built without
-	   pymalloc and with ASAN, so we dump one in
-	   $OUT/sanpy/lib/python2.7. This helps us wire that up. */
-	std::string selfpath(*argv[0]);
-	std::string pypath;
-	auto pos = selfpath.rfind("/");
-	if (pos == std::string::npos) {
-		char wd[8192];
-		getcwd(wd, 8192);
-		pypath = std::string(wd) + subdir;
-	} else {
-		pypath = selfpath.substr(0, pos) + subdir;
-	}
-	strncpy(cpypath, pypath.c_str(), pypath.size());
-	setenv("PYTHONPATH", cpypath, 1);
-	setenv("PYTHONNOUSERSITE", "1", 1);
-	/* prevent Python from looking up users in the fuzz environment */
-	setenv("PYTHONUSERBASE", cpypath, 1);
-	Py_SetPythonHome(cpypath);
-	Py_InitializeEx(0);
-	initparsers();
+	contrib::initpy(*argv[0]);
 	code = (PyCodeObject *)Py_CompileString(R"py(
 from parsers import lazymanifest
 try:
@@ -60,8 +34,6 @@
   # print e
 )py",
 	                                        "fuzzer", Py_file_input);
-	mainmod = PyImport_AddModule("__main__");
-	globals = PyModule_GetDict(mainmod);
 	return 0;
 }
 
@@ -71,7 +43,7 @@
 	    PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
 	PyObject *locals = PyDict_New();
 	PyDict_SetItemString(locals, "mdata", mtext);
-	PyObject *res = PyEval_EvalCode(code, globals, locals);
+	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
 	if (!res) {
 		PyErr_Print();
 	}