Show More
@@ -1,76 +1,76 | |||||
1 | #include "pyutil.h" |
|
1 | #include "pyutil.h" | |
2 |
|
2 | |||
3 | #include <iostream> |
|
3 | #include <iostream> | |
4 | #include <string> |
|
4 | #include <string> | |
5 |
|
5 | |||
6 | namespace contrib |
|
6 | namespace contrib | |
7 | { |
|
7 | { | |
8 |
|
8 | |||
9 | #if PY_MAJOR_VERSION >= 3 |
|
9 | #if PY_MAJOR_VERSION >= 3 | |
10 | #define HG_FUZZER_PY3 1 |
|
10 | #define HG_FUZZER_PY3 1 | |
11 | PyMODINIT_FUNC PyInit_parsers(void); |
|
11 | PyMODINIT_FUNC PyInit_parsers(void); | |
12 | #else |
|
12 | #else | |
13 | PyMODINIT_FUNC initparsers(void); |
|
13 | PyMODINIT_FUNC initparsers(void); | |
14 | #endif |
|
14 | #endif | |
15 |
|
15 | |||
16 | static char cpypath[8192] = "\0"; |
|
16 | static char cpypath[8192] = "\0"; | |
17 |
|
17 | |||
18 | static PyObject *mainmod; |
|
18 | static PyObject *mainmod; | |
19 | static PyObject *globals; |
|
19 | static PyObject *globals; | |
20 |
|
20 | |||
21 | void initpy(const char *cselfpath) |
|
21 | void initpy(const char *cselfpath) | |
22 | { |
|
22 | { | |
23 | #ifdef HG_FUZZER_PY3 |
|
23 | #ifdef HG_FUZZER_PY3 | |
24 |
const std::string subdir = "/sanpy/lib/python3. |
|
24 | const std::string subdir = "/sanpy/lib/python3.8"; | |
25 | #else |
|
25 | #else | |
26 | const std::string subdir = "/sanpy/lib/python2.7"; |
|
26 | const std::string subdir = "/sanpy/lib/python2.7"; | |
27 | #endif |
|
27 | #endif | |
28 |
|
28 | |||
29 | /* HACK ALERT: we need a full Python installation built without |
|
29 | /* HACK ALERT: we need a full Python installation built without | |
30 | pymalloc and with ASAN, so we dump one in |
|
30 | pymalloc and with ASAN, so we dump one in | |
31 | $OUT/sanpy/lib/python2.7. This helps us wire that up. */ |
|
31 | $OUT/sanpy/lib/python2.7. This helps us wire that up. */ | |
32 | std::string selfpath(cselfpath); |
|
32 | std::string selfpath(cselfpath); | |
33 | std::string pypath; |
|
33 | std::string pypath; | |
34 | auto pos = selfpath.rfind("/"); |
|
34 | auto pos = selfpath.rfind("/"); | |
35 | if (pos == std::string::npos) { |
|
35 | if (pos == std::string::npos) { | |
36 | char wd[8192]; |
|
36 | char wd[8192]; | |
37 | if (!getcwd(wd, 8192)) { |
|
37 | if (!getcwd(wd, 8192)) { | |
38 | std::cerr << "Failed to call getcwd: errno " << errno |
|
38 | std::cerr << "Failed to call getcwd: errno " << errno | |
39 | << std::endl; |
|
39 | << std::endl; | |
40 | exit(1); |
|
40 | exit(1); | |
41 | } |
|
41 | } | |
42 | pypath = std::string(wd) + subdir; |
|
42 | pypath = std::string(wd) + subdir; | |
43 | } else { |
|
43 | } else { | |
44 | pypath = selfpath.substr(0, pos) + subdir; |
|
44 | pypath = selfpath.substr(0, pos) + subdir; | |
45 | } |
|
45 | } | |
46 | strncpy(cpypath, pypath.c_str(), pypath.size()); |
|
46 | strncpy(cpypath, pypath.c_str(), pypath.size()); | |
47 | setenv("PYTHONPATH", cpypath, 1); |
|
47 | setenv("PYTHONPATH", cpypath, 1); | |
48 | setenv("PYTHONNOUSERSITE", "1", 1); |
|
48 | setenv("PYTHONNOUSERSITE", "1", 1); | |
49 | /* prevent Python from looking up users in the fuzz environment */ |
|
49 | /* prevent Python from looking up users in the fuzz environment */ | |
50 | setenv("PYTHONUSERBASE", cpypath, 1); |
|
50 | setenv("PYTHONUSERBASE", cpypath, 1); | |
51 | #ifdef HG_FUZZER_PY3 |
|
51 | #ifdef HG_FUZZER_PY3 | |
52 | std::wstring wcpypath(pypath.begin(), pypath.end()); |
|
52 | std::wstring wcpypath(pypath.begin(), pypath.end()); | |
53 | Py_SetPythonHome(wcpypath.c_str()); |
|
53 | Py_SetPythonHome(wcpypath.c_str()); | |
54 | #else |
|
54 | #else | |
55 | Py_SetPythonHome(cpypath); |
|
55 | Py_SetPythonHome(cpypath); | |
56 | #endif |
|
56 | #endif | |
57 | Py_InitializeEx(0); |
|
57 | Py_InitializeEx(0); | |
58 | mainmod = PyImport_AddModule("__main__"); |
|
58 | mainmod = PyImport_AddModule("__main__"); | |
59 | globals = PyModule_GetDict(mainmod); |
|
59 | globals = PyModule_GetDict(mainmod); | |
60 |
|
60 | |||
61 | #ifdef HG_FUZZER_PY3 |
|
61 | #ifdef HG_FUZZER_PY3 | |
62 | PyObject *mod = PyInit_parsers(); |
|
62 | PyObject *mod = PyInit_parsers(); | |
63 | #else |
|
63 | #else | |
64 | initparsers(); |
|
64 | initparsers(); | |
65 | PyObject *mod = PyImport_ImportModule("parsers"); |
|
65 | PyObject *mod = PyImport_ImportModule("parsers"); | |
66 | #endif |
|
66 | #endif | |
67 |
|
67 | |||
68 | PyDict_SetItemString(globals, "parsers", mod); |
|
68 | PyDict_SetItemString(globals, "parsers", mod); | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | PyObject *pyglobals() |
|
71 | PyObject *pyglobals() | |
72 | { |
|
72 | { | |
73 | return globals; |
|
73 | return globals; | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | } // namespace contrib |
|
76 | } // namespace contrib |
General Comments 0
You need to be logged in to leave comments.
Login now