contrib/fuzz/pyutil.cc
changeset 43859 8766728dbce6
parent 43807 c78f8f0720cc
child 44961 ee5f27d7b9fb
equal deleted inserted replaced
43858:b0867b7751ba 43859:8766728dbce6
     4 #include <string>
     4 #include <string>
     5 
     5 
     6 namespace contrib
     6 namespace contrib
     7 {
     7 {
     8 
     8 
       
     9 #if PY_MAJOR_VERSION >= 3
       
    10 #define HG_FUZZER_PY3 1
       
    11 PyMODINIT_FUNC PyInit_parsers(void);
       
    12 #else
       
    13 PyMODINIT_FUNC initparsers(void);
       
    14 #endif
       
    15 
     9 static char cpypath[8192] = "\0";
    16 static char cpypath[8192] = "\0";
    10 
    17 
    11 static PyObject *mainmod;
    18 static PyObject *mainmod;
    12 static PyObject *globals;
    19 static PyObject *globals;
    13 
    20 
    14 /* TODO: use Python 3 for this fuzzing? */
       
    15 PyMODINIT_FUNC initparsers(void);
       
    16 
       
    17 void initpy(const char *cselfpath)
    21 void initpy(const char *cselfpath)
    18 {
    22 {
       
    23 #ifdef HG_FUZZER_PY3
       
    24 	const std::string subdir = "/sanpy/lib/python3.7";
       
    25 #else
    19 	const std::string subdir = "/sanpy/lib/python2.7";
    26 	const std::string subdir = "/sanpy/lib/python2.7";
       
    27 #endif
       
    28 
    20 	/* HACK ALERT: we need a full Python installation built without
    29 	/* HACK ALERT: we need a full Python installation built without
    21 	   pymalloc and with ASAN, so we dump one in
    30 	   pymalloc and with ASAN, so we dump one in
    22 	   $OUT/sanpy/lib/python2.7. This helps us wire that up. */
    31 	   $OUT/sanpy/lib/python2.7. This helps us wire that up. */
    23 	std::string selfpath(cselfpath);
    32 	std::string selfpath(cselfpath);
    24 	std::string pypath;
    33 	std::string pypath;
    37 	strncpy(cpypath, pypath.c_str(), pypath.size());
    46 	strncpy(cpypath, pypath.c_str(), pypath.size());
    38 	setenv("PYTHONPATH", cpypath, 1);
    47 	setenv("PYTHONPATH", cpypath, 1);
    39 	setenv("PYTHONNOUSERSITE", "1", 1);
    48 	setenv("PYTHONNOUSERSITE", "1", 1);
    40 	/* prevent Python from looking up users in the fuzz environment */
    49 	/* prevent Python from looking up users in the fuzz environment */
    41 	setenv("PYTHONUSERBASE", cpypath, 1);
    50 	setenv("PYTHONUSERBASE", cpypath, 1);
       
    51 #ifdef HG_FUZZER_PY3
       
    52 	std::wstring wcpypath(pypath.begin(), pypath.end());
       
    53 	Py_SetPythonHome(wcpypath.c_str());
       
    54 #else
    42 	Py_SetPythonHome(cpypath);
    55 	Py_SetPythonHome(cpypath);
       
    56 #endif
    43 	Py_InitializeEx(0);
    57 	Py_InitializeEx(0);
    44 	mainmod = PyImport_AddModule("__main__");
    58 	mainmod = PyImport_AddModule("__main__");
    45 	globals = PyModule_GetDict(mainmod);
    59 	globals = PyModule_GetDict(mainmod);
       
    60 
       
    61 #ifdef HG_FUZZER_PY3
       
    62 	PyObject *mod = PyInit_parsers();
       
    63 #else
    46 	initparsers();
    64 	initparsers();
       
    65 	PyObject *mod = PyImport_ImportModule("parsers");
       
    66 #endif
       
    67 
       
    68 	PyDict_SetItemString(globals, "parsers", mod);
    47 }
    69 }
    48 
    70 
    49 PyObject *pyglobals()
    71 PyObject *pyglobals()
    50 {
    72 {
    51 	return globals;
    73 	return globals;