##// END OF EJS Templates
fuzz: add support for fuzzing under either Python 2 or 3...
Augie Fackler -
r44311:8766728d default
parent child Browse files
Show More
@@ -9,16 +9,15 b''
9 9
10 10 extern "C" {
11 11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13 13
14 14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 15 {
16 16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
18 from parsers import dirs
17 code = (PYCODETYPE *)Py_CompileString(R"py(
19 18 try:
20 19 files = mdata.split('\n')
21 d = dirs(files)
20 d = parsers.dirs(files)
22 21 list(d)
23 22 'a' in d
24 23 if files:
@@ -29,7 +28,7 b' except Exception as e:'
29 28 # to debug failures.
30 29 # print e
31 30 )py",
32 "fuzzer", Py_file_input);
31 "fuzzer", Py_file_input);
33 32 return 0;
34 33 }
35 34
@@ -9,24 +9,23 b''
9 9
10 10 extern "C" {
11 11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13 13
14 14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 15 {
16 16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
18 from parsers import parse_dirstate
17 code = (PYCODETYPE *)Py_CompileString(R"py(
19 18 try:
20 19 dmap = {}
21 20 copymap = {}
22 p = parse_dirstate(dmap, copymap, data)
21 p = parsers.parse_dirstate(dmap, copymap, data)
23 22 except Exception as e:
24 23 pass
25 24 # uncomment this print if you're editing this Python code
26 25 # to debug failures.
27 26 # print e
28 27 )py",
29 "fuzzer", Py_file_input);
28 "fuzzer", Py_file_input);
30 29 return 0;
31 30 }
32 31
@@ -9,13 +9,12 b''
9 9
10 10 extern "C" {
11 11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13 13
14 14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 15 {
16 16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
18 from parsers import fm1readmarkers
17 code = (PYCODETYPE *)Py_CompileString(R"py(
19 18 def maybeint(s, default):
20 19 try:
21 20 return int(s)
@@ -31,14 +30,14 b' try:'
31 30 else:
32 31 offset = stop = 0
33 32 offset, stop = maybeint(offset, 0), maybeint(stop, len(data))
34 fm1readmarkers(data, offset, stop)
33 parsers.fm1readmarkers(data, offset, stop)
35 34 except Exception as e:
36 35 pass
37 36 # uncomment this print if you're editing this Python code
38 37 # to debug failures.
39 38 # print e
40 39 )py",
41 "fuzzer", Py_file_input);
40 "fuzzer", Py_file_input);
42 41 return 0;
43 42 }
44 43
@@ -10,29 +10,20 b''
10 10
11 11 extern "C" {
12 12
13 static PyCodeObject *code;
13 static PYCODETYPE *code;
14 14
15 15 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
16 16 {
17 17 contrib::initpy(*argv[0]);
18 code = (PyCodeObject *)Py_CompileString(R"py(
19 from parsers import (
20 isasciistr,
21 asciilower,
22 asciiupper,
23 encodedir,
24 pathencode,
25 lowerencode,
26 )
27
18 code = (PYCODETYPE *)Py_CompileString(R"py(
28 19 try:
29 20 for fn in (
30 isasciistr,
31 asciilower,
32 asciiupper,
33 encodedir,
34 pathencode,
35 lowerencode,
21 parsers.isasciistr,
22 parsers.asciilower,
23 parsers.asciiupper,
24 parsers.encodedir,
25 parsers.pathencode,
26 parsers.lowerencode,
36 27 ):
37 28 try:
38 29 fn(data)
@@ -53,7 +44,7 b' except Exception as e:'
53 44 # to debug failures.
54 45 # print(e)
55 46 )py",
56 "fuzzer", Py_file_input);
47 "fuzzer", Py_file_input);
57 48 if (!code) {
58 49 std::cerr << "failed to compile Python code!" << std::endl;
59 50 }
@@ -11,23 +11,21 b''
11 11
12 12 extern "C" {
13 13
14 static PyCodeObject *code;
14 static PYCODETYPE *code;
15 15
16 16 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
17 17 {
18 18 contrib::initpy(*argv[0]);
19 code = (PyCodeObject *)Py_CompileString(R"py(
20 from parsers import jsonescapeu8fast
21
19 code = (PYCODETYPE *)Py_CompileString(R"py(
22 20 try:
23 jsonescapeu8fast(data, paranoid)
21 parsers.jsonescapeu8fast(data, paranoid)
24 22 except Exception as e:
25 23 pass
26 24 # uncomment this print if you're editing this Python code
27 25 # to debug failures.
28 26 # print(e)
29 27 )py",
30 "fuzzer", Py_file_input);
28 "fuzzer", Py_file_input);
31 29 if (!code) {
32 30 std::cerr << "failed to compile Python code!" << std::endl;
33 31 }
@@ -9,15 +9,14 b''
9 9
10 10 extern "C" {
11 11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13 13
14 14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 15 {
16 16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
18 from parsers import lazymanifest
17 code = (PYCODETYPE *)Py_CompileString(R"py(
19 18 try:
20 lm = lazymanifest(mdata)
19 lm = parsers.lazymanifest(mdata)
21 20 # iterate the whole thing, which causes the code to fully parse
22 21 # every line in the manifest
23 22 for e, _, _ in lm.iterentries():
@@ -41,7 +40,7 b' except Exception as e:'
41 40 # to debug failures.
42 41 # print e
43 42 )py",
44 "fuzzer", Py_file_input);
43 "fuzzer", Py_file_input);
45 44 return 0;
46 45 }
47 46
@@ -6,17 +6,26 b''
6 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 16 static char cpypath[8192] = "\0";
10 17
11 18 static PyObject *mainmod;
12 19 static PyObject *globals;
13 20
14 /* TODO: use Python 3 for this fuzzing? */
15 PyMODINIT_FUNC initparsers(void);
16
17 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 26 const std::string subdir = "/sanpy/lib/python2.7";
27 #endif
28
20 29 /* HACK ALERT: we need a full Python installation built without
21 30 pymalloc and with ASAN, so we dump one in
22 31 $OUT/sanpy/lib/python2.7. This helps us wire that up. */
@@ -39,11 +48,24 b' void initpy(const char *cselfpath)'
39 48 setenv("PYTHONNOUSERSITE", "1", 1);
40 49 /* prevent Python from looking up users in the fuzz environment */
41 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 55 Py_SetPythonHome(cpypath);
56 #endif
43 57 Py_InitializeEx(0);
44 58 mainmod = PyImport_AddModule("__main__");
45 59 globals = PyModule_GetDict(mainmod);
60
61 #ifdef HG_FUZZER_PY3
62 PyObject *mod = PyInit_parsers();
63 #else
46 64 initparsers();
65 PyObject *mod = PyImport_ImportModule("parsers");
66 #endif
67
68 PyDict_SetItemString(globals, "parsers", mod);
47 69 }
48 70
49 71 PyObject *pyglobals()
@@ -1,5 +1,11 b''
1 1 #include <Python.h>
2 2
3 #if PY_MAJOR_VERSION >= 3
4 #define PYCODETYPE PyObject
5 #else
6 #define PYCODETYPE PyCodeObject
7 #endif
8
3 9 namespace contrib
4 10 {
5 11
@@ -9,16 +9,15 b''
9 9
10 10 extern "C" {
11 11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13 13
14 14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 15 {
16 16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
18 from parsers import parse_index2
17 code = (PYCODETYPE *)Py_CompileString(R"py(
19 18 for inline in (True, False):
20 19 try:
21 index, cache = parse_index2(data, inline)
20 index, cache = parsers.parse_index2(data, inline)
22 21 index.slicechunktodensity(list(range(len(index))), 0.5, 262144)
23 22 index.stats()
24 23 index.findsnapshots({}, 0)
@@ -35,7 +34,7 b' for inline in (True, False):'
35 34 # to debug failures.
36 35 # print e
37 36 )py",
38 "fuzzer", Py_file_input);
37 "fuzzer", Py_file_input);
39 38 return 0;
40 39 }
41 40
General Comments 0
You need to be logged in to leave comments. Login now