##// 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 extern "C" {
10 extern "C" {
11
11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13
13
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 {
15 {
16 contrib::initpy(*argv[0]);
16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
17 code = (PYCODETYPE *)Py_CompileString(R"py(
18 from parsers import dirs
19 try:
18 try:
20 files = mdata.split('\n')
19 files = mdata.split('\n')
21 d = dirs(files)
20 d = parsers.dirs(files)
22 list(d)
21 list(d)
23 'a' in d
22 'a' in d
24 if files:
23 if files:
@@ -29,7 +28,7 b' except Exception as e:'
29 # to debug failures.
28 # to debug failures.
30 # print e
29 # print e
31 )py",
30 )py",
32 "fuzzer", Py_file_input);
31 "fuzzer", Py_file_input);
33 return 0;
32 return 0;
34 }
33 }
35
34
@@ -9,24 +9,23 b''
9
9
10 extern "C" {
10 extern "C" {
11
11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13
13
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 {
15 {
16 contrib::initpy(*argv[0]);
16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
17 code = (PYCODETYPE *)Py_CompileString(R"py(
18 from parsers import parse_dirstate
19 try:
18 try:
20 dmap = {}
19 dmap = {}
21 copymap = {}
20 copymap = {}
22 p = parse_dirstate(dmap, copymap, data)
21 p = parsers.parse_dirstate(dmap, copymap, data)
23 except Exception as e:
22 except Exception as e:
24 pass
23 pass
25 # uncomment this print if you're editing this Python code
24 # uncomment this print if you're editing this Python code
26 # to debug failures.
25 # to debug failures.
27 # print e
26 # print e
28 )py",
27 )py",
29 "fuzzer", Py_file_input);
28 "fuzzer", Py_file_input);
30 return 0;
29 return 0;
31 }
30 }
32
31
@@ -9,13 +9,12 b''
9
9
10 extern "C" {
10 extern "C" {
11
11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13
13
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 {
15 {
16 contrib::initpy(*argv[0]);
16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
17 code = (PYCODETYPE *)Py_CompileString(R"py(
18 from parsers import fm1readmarkers
19 def maybeint(s, default):
18 def maybeint(s, default):
20 try:
19 try:
21 return int(s)
20 return int(s)
@@ -31,14 +30,14 b' try:'
31 else:
30 else:
32 offset = stop = 0
31 offset = stop = 0
33 offset, stop = maybeint(offset, 0), maybeint(stop, len(data))
32 offset, stop = maybeint(offset, 0), maybeint(stop, len(data))
34 fm1readmarkers(data, offset, stop)
33 parsers.fm1readmarkers(data, offset, stop)
35 except Exception as e:
34 except Exception as e:
36 pass
35 pass
37 # uncomment this print if you're editing this Python code
36 # uncomment this print if you're editing this Python code
38 # to debug failures.
37 # to debug failures.
39 # print e
38 # print e
40 )py",
39 )py",
41 "fuzzer", Py_file_input);
40 "fuzzer", Py_file_input);
42 return 0;
41 return 0;
43 }
42 }
44
43
@@ -10,29 +10,20 b''
10
10
11 extern "C" {
11 extern "C" {
12
12
13 static PyCodeObject *code;
13 static PYCODETYPE *code;
14
14
15 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
16 {
16 {
17 contrib::initpy(*argv[0]);
17 contrib::initpy(*argv[0]);
18 code = (PyCodeObject *)Py_CompileString(R"py(
18 code = (PYCODETYPE *)Py_CompileString(R"py(
19 from parsers import (
20 isasciistr,
21 asciilower,
22 asciiupper,
23 encodedir,
24 pathencode,
25 lowerencode,
26 )
27
28 try:
19 try:
29 for fn in (
20 for fn in (
30 isasciistr,
21 parsers.isasciistr,
31 asciilower,
22 parsers.asciilower,
32 asciiupper,
23 parsers.asciiupper,
33 encodedir,
24 parsers.encodedir,
34 pathencode,
25 parsers.pathencode,
35 lowerencode,
26 parsers.lowerencode,
36 ):
27 ):
37 try:
28 try:
38 fn(data)
29 fn(data)
@@ -53,7 +44,7 b' except Exception as e:'
53 # to debug failures.
44 # to debug failures.
54 # print(e)
45 # print(e)
55 )py",
46 )py",
56 "fuzzer", Py_file_input);
47 "fuzzer", Py_file_input);
57 if (!code) {
48 if (!code) {
58 std::cerr << "failed to compile Python code!" << std::endl;
49 std::cerr << "failed to compile Python code!" << std::endl;
59 }
50 }
@@ -11,23 +11,21 b''
11
11
12 extern "C" {
12 extern "C" {
13
13
14 static PyCodeObject *code;
14 static PYCODETYPE *code;
15
15
16 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
16 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
17 {
17 {
18 contrib::initpy(*argv[0]);
18 contrib::initpy(*argv[0]);
19 code = (PyCodeObject *)Py_CompileString(R"py(
19 code = (PYCODETYPE *)Py_CompileString(R"py(
20 from parsers import jsonescapeu8fast
21
22 try:
20 try:
23 jsonescapeu8fast(data, paranoid)
21 parsers.jsonescapeu8fast(data, paranoid)
24 except Exception as e:
22 except Exception as e:
25 pass
23 pass
26 # uncomment this print if you're editing this Python code
24 # uncomment this print if you're editing this Python code
27 # to debug failures.
25 # to debug failures.
28 # print(e)
26 # print(e)
29 )py",
27 )py",
30 "fuzzer", Py_file_input);
28 "fuzzer", Py_file_input);
31 if (!code) {
29 if (!code) {
32 std::cerr << "failed to compile Python code!" << std::endl;
30 std::cerr << "failed to compile Python code!" << std::endl;
33 }
31 }
@@ -9,15 +9,14 b''
9
9
10 extern "C" {
10 extern "C" {
11
11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13
13
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 {
15 {
16 contrib::initpy(*argv[0]);
16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
17 code = (PYCODETYPE *)Py_CompileString(R"py(
18 from parsers import lazymanifest
19 try:
18 try:
20 lm = lazymanifest(mdata)
19 lm = parsers.lazymanifest(mdata)
21 # iterate the whole thing, which causes the code to fully parse
20 # iterate the whole thing, which causes the code to fully parse
22 # every line in the manifest
21 # every line in the manifest
23 for e, _, _ in lm.iterentries():
22 for e, _, _ in lm.iterentries():
@@ -41,7 +40,7 b' except Exception as e:'
41 # to debug failures.
40 # to debug failures.
42 # print e
41 # print e
43 )py",
42 )py",
44 "fuzzer", Py_file_input);
43 "fuzzer", Py_file_input);
45 return 0;
44 return 0;
46 }
45 }
47
46
@@ -6,17 +6,26 b''
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. */
@@ -39,11 +48,24 b' void initpy(const char *cselfpath)'
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()
@@ -1,5 +1,11 b''
1 #include <Python.h>
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 namespace contrib
9 namespace contrib
4 {
10 {
5
11
@@ -9,16 +9,15 b''
9
9
10 extern "C" {
10 extern "C" {
11
11
12 static PyCodeObject *code;
12 static PYCODETYPE *code;
13
13
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 {
15 {
16 contrib::initpy(*argv[0]);
16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
17 code = (PYCODETYPE *)Py_CompileString(R"py(
18 from parsers import parse_index2
19 for inline in (True, False):
18 for inline in (True, False):
20 try:
19 try:
21 index, cache = parse_index2(data, inline)
20 index, cache = parsers.parse_index2(data, inline)
22 index.slicechunktodensity(list(range(len(index))), 0.5, 262144)
21 index.slicechunktodensity(list(range(len(index))), 0.5, 262144)
23 index.stats()
22 index.stats()
24 index.findsnapshots({}, 0)
23 index.findsnapshots({}, 0)
@@ -35,7 +34,7 b' for inline in (True, False):'
35 # to debug failures.
34 # to debug failures.
36 # print e
35 # print e
37 )py",
36 )py",
38 "fuzzer", Py_file_input);
37 "fuzzer", Py_file_input);
39 return 0;
38 return 0;
40 }
39 }
41
40
General Comments 0
You need to be logged in to leave comments. Login now