##// 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:
@@ -9,17 +9,16 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
@@ -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,7 +30,7 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
@@ -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)
@@ -11,16 +11,14 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
@@ -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():
@@ -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)
General Comments 0
You need to be logged in to leave comments. Login now