##// END OF EJS Templates
fuzz: new fuzzer for dirstate parser...
Augie Fackler -
r41051:b444407f default
parent child Browse files
Show More
@@ -0,0 +1,48
1 #include <Python.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include <string>
7
8 #include "pyutil.h"
9
10 extern "C" {
11
12 static PyCodeObject *code;
13
14 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
15 {
16 contrib::initpy(*argv[0]);
17 code = (PyCodeObject *)Py_CompileString(R"py(
18 from parsers import parse_dirstate
19 try:
20 dmap = {}
21 copymap = {}
22 p = parse_dirstate(dmap, copymap, data)
23 except Exception as e:
24 pass
25 # uncomment this print if you're editing this Python code
26 # to debug failures.
27 # print e
28 )py",
29 "fuzzer", Py_file_input);
30 return 0;
31 }
32
33 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
34 {
35 PyObject *text =
36 PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
37 PyObject *locals = PyDict_New();
38 PyDict_SetItemString(locals, "data", text);
39 PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
40 if (!res) {
41 PyErr_Print();
42 }
43 Py_XDECREF(res);
44 Py_DECREF(locals);
45 Py_DECREF(text);
46 return 0; // Non-zero return values are reserved for future use.
47 }
48 }
@@ -0,0 +1,18
1 from __future__ import absolute_import, print_function
2
3 import argparse
4 import os
5 import zipfile
6
7 ap = argparse.ArgumentParser()
8 ap.add_argument("out", metavar="some.zip", type=str, nargs=1)
9 args = ap.parse_args()
10
11 reporoot = os.path.normpath(os.path.join(os.path.dirname(__file__),
12 '..', '..'))
13 dirstate = os.path.join(reporoot, '.hg', 'dirstate')
14
15 with zipfile.ZipFile(args.out[0], "w", zipfile.ZIP_STORED) as zf:
16 if os.path.exists(dirstate):
17 with open(dirstate) as f:
18 zf.writestr("dirstate", f.read())
@@ -1,146 +1,157
1 CC = clang
1 CC = clang
2 CXX = clang++
2 CXX = clang++
3
3
4 all: bdiff mpatch xdiff
4 all: bdiff mpatch xdiff
5
5
6 fuzzutil.o: fuzzutil.cc fuzzutil.h
6 fuzzutil.o: fuzzutil.cc fuzzutil.h
7 $(CXX) $(CXXFLAGS) -g -O1 \
7 $(CXX) $(CXXFLAGS) -g -O1 \
8 -std=c++17 \
8 -std=c++17 \
9 -I../../mercurial -c -o fuzzutil.o fuzzutil.cc
9 -I../../mercurial -c -o fuzzutil.o fuzzutil.cc
10
10
11 fuzzutil-oss-fuzz.o: fuzzutil.cc fuzzutil.h
11 fuzzutil-oss-fuzz.o: fuzzutil.cc fuzzutil.h
12 $(CXX) $(CXXFLAGS) -std=c++17 \
12 $(CXX) $(CXXFLAGS) -std=c++17 \
13 -I../../mercurial -c -o fuzzutil-oss-fuzz.o fuzzutil.cc
13 -I../../mercurial -c -o fuzzutil-oss-fuzz.o fuzzutil.cc
14
14
15 pyutil.o: pyutil.cc pyutil.h
15 pyutil.o: pyutil.cc pyutil.h
16 $(CXX) $(CXXFLAGS) -g -O1 \
16 $(CXX) $(CXXFLAGS) -g -O1 \
17 `$$OUT/sanpy/bin/python-config --cflags` \
17 `$$OUT/sanpy/bin/python-config --cflags` \
18 -I../../mercurial -c -o pyutil.o pyutil.cc
18 -I../../mercurial -c -o pyutil.o pyutil.cc
19
19
20 bdiff.o: ../../mercurial/bdiff.c
20 bdiff.o: ../../mercurial/bdiff.c
21 $(CC) $(CFLAGS) -fsanitize=fuzzer-no-link,address -c -o bdiff.o \
21 $(CC) $(CFLAGS) -fsanitize=fuzzer-no-link,address -c -o bdiff.o \
22 ../../mercurial/bdiff.c
22 ../../mercurial/bdiff.c
23
23
24 bdiff: bdiff.cc bdiff.o fuzzutil.o
24 bdiff: bdiff.cc bdiff.o fuzzutil.o
25 $(CXX) $(CXXFLAGS) -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
25 $(CXX) $(CXXFLAGS) -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
26 -std=c++17 \
26 -std=c++17 \
27 -I../../mercurial bdiff.cc bdiff.o fuzzutil.o -o bdiff
27 -I../../mercurial bdiff.cc bdiff.o fuzzutil.o -o bdiff
28
28
29 bdiff-oss-fuzz.o: ../../mercurial/bdiff.c
29 bdiff-oss-fuzz.o: ../../mercurial/bdiff.c
30 $(CC) $(CFLAGS) -c -o bdiff-oss-fuzz.o ../../mercurial/bdiff.c
30 $(CC) $(CFLAGS) -c -o bdiff-oss-fuzz.o ../../mercurial/bdiff.c
31
31
32 bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o
32 bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o
33 $(CXX) $(CXXFLAGS) -std=c++17 -I../../mercurial bdiff.cc \
33 $(CXX) $(CXXFLAGS) -std=c++17 -I../../mercurial bdiff.cc \
34 bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o -lFuzzingEngine -o \
34 bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o -lFuzzingEngine -o \
35 $$OUT/bdiff_fuzzer
35 $$OUT/bdiff_fuzzer
36
36
37 mpatch.o: ../../mercurial/mpatch.c
37 mpatch.o: ../../mercurial/mpatch.c
38 $(CC) -g -O1 -fsanitize=fuzzer-no-link,address -c -o mpatch.o \
38 $(CC) -g -O1 -fsanitize=fuzzer-no-link,address -c -o mpatch.o \
39 ../../mercurial/mpatch.c
39 ../../mercurial/mpatch.c
40
40
41 mpatch: CXXFLAGS += -std=c++17
41 mpatch: CXXFLAGS += -std=c++17
42 mpatch: mpatch.cc mpatch.o fuzzutil.o
42 mpatch: mpatch.cc mpatch.o fuzzutil.o
43 $(CXX) $(CXXFLAGS) -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
43 $(CXX) $(CXXFLAGS) -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
44 -I../../mercurial mpatch.cc mpatch.o fuzzutil.o -o mpatch
44 -I../../mercurial mpatch.cc mpatch.o fuzzutil.o -o mpatch
45
45
46 mpatch-oss-fuzz.o: ../../mercurial/mpatch.c
46 mpatch-oss-fuzz.o: ../../mercurial/mpatch.c
47 $(CC) $(CFLAGS) -c -o mpatch-oss-fuzz.o ../../mercurial/mpatch.c
47 $(CC) $(CFLAGS) -c -o mpatch-oss-fuzz.o ../../mercurial/mpatch.c
48
48
49 mpatch_fuzzer: mpatch.cc mpatch-oss-fuzz.o fuzzutil-oss-fuzz.o
49 mpatch_fuzzer: mpatch.cc mpatch-oss-fuzz.o fuzzutil-oss-fuzz.o
50 $(CXX) $(CXXFLAGS) -std=c++17 -I../../mercurial mpatch.cc \
50 $(CXX) $(CXXFLAGS) -std=c++17 -I../../mercurial mpatch.cc \
51 mpatch-oss-fuzz.o fuzzutil-oss-fuzz.o -lFuzzingEngine -o \
51 mpatch-oss-fuzz.o fuzzutil-oss-fuzz.o -lFuzzingEngine -o \
52 $$OUT/mpatch_fuzzer
52 $$OUT/mpatch_fuzzer
53
53
54 mpatch_corpus.zip:
54 mpatch_corpus.zip:
55 python mpatch_corpus.py $$OUT/mpatch_fuzzer_seed_corpus.zip
55 python mpatch_corpus.py $$OUT/mpatch_fuzzer_seed_corpus.zip
56
56
57 x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
57 x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
58 $(CC) -g -O1 -fsanitize=fuzzer-no-link,address -c \
58 $(CC) -g -O1 -fsanitize=fuzzer-no-link,address -c \
59 -o $@ \
59 -o $@ \
60 $<
60 $<
61
61
62 xdiff: CXXFLAGS += -std=c++17
62 xdiff: CXXFLAGS += -std=c++17
63 xdiff: xdiff.cc xdiffi.o xprepare.o xutils.o fuzzutil.o
63 xdiff: xdiff.cc xdiffi.o xprepare.o xutils.o fuzzutil.o
64 $(CXX) $(CXXFLAGS) -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
64 $(CXX) $(CXXFLAGS) -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
65 -I../../mercurial xdiff.cc \
65 -I../../mercurial xdiff.cc \
66 xdiffi.o xprepare.o xutils.o fuzzutil.o -o xdiff
66 xdiffi.o xprepare.o xutils.o fuzzutil.o -o xdiff
67
67
68 fuzz-x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
68 fuzz-x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
69 $(CC) $(CFLAGS) -c \
69 $(CC) $(CFLAGS) -c \
70 -o $@ \
70 -o $@ \
71 $<
71 $<
72
72
73 xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o
73 xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o
74 $(CXX) $(CXXFLAGS) -std=c++17 -I../../mercurial xdiff.cc \
74 $(CXX) $(CXXFLAGS) -std=c++17 -I../../mercurial xdiff.cc \
75 fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o \
75 fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o \
76 -lFuzzingEngine -o $$OUT/xdiff_fuzzer
76 -lFuzzingEngine -o $$OUT/xdiff_fuzzer
77
77
78 # TODO use the $OUT env var instead of hardcoding /out
78 # TODO use the $OUT env var instead of hardcoding /out
79 /out/sanpy/bin/python:
79 /out/sanpy/bin/python:
80 cd /Python-2.7.15/ ; ASAN_OPTIONS=detect_leaks=0 ./configure --without-pymalloc --prefix=$$OUT/sanpy CFLAGS="$(CFLAGS)" LINKCC="$($CXX)" LDFLAGS="$(CXXFLAGS)"
80 cd /Python-2.7.15/ ; ASAN_OPTIONS=detect_leaks=0 ./configure --without-pymalloc --prefix=$$OUT/sanpy CFLAGS="$(CFLAGS)" LINKCC="$($CXX)" LDFLAGS="$(CXXFLAGS)"
81 cd /Python-2.7.15/ ; grep -v HAVE_GETC_UNLOCKED < pyconfig.h > tmp && mv tmp pyconfig.h
81 cd /Python-2.7.15/ ; grep -v HAVE_GETC_UNLOCKED < pyconfig.h > tmp && mv tmp pyconfig.h
82 cd /Python-2.7.15/ ; ASAN_OPTIONS=detect_leaks=0 make && make install
82 cd /Python-2.7.15/ ; ASAN_OPTIONS=detect_leaks=0 make && make install
83
83
84 sanpy: /out/sanpy/bin/python
84 sanpy: /out/sanpy/bin/python
85
85
86 manifest.o: sanpy ../../mercurial/cext/manifest.c
86 manifest.o: sanpy ../../mercurial/cext/manifest.c
87 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
87 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
88 -I../../mercurial \
88 -I../../mercurial \
89 -c -o manifest.o ../../mercurial/cext/manifest.c
89 -c -o manifest.o ../../mercurial/cext/manifest.c
90
90
91 charencode.o: sanpy ../../mercurial/cext/charencode.c
91 charencode.o: sanpy ../../mercurial/cext/charencode.c
92 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
92 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
93 -I../../mercurial \
93 -I../../mercurial \
94 -c -o charencode.o ../../mercurial/cext/charencode.c
94 -c -o charencode.o ../../mercurial/cext/charencode.c
95
95
96 parsers.o: sanpy ../../mercurial/cext/parsers.c
96 parsers.o: sanpy ../../mercurial/cext/parsers.c
97 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
97 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
98 -I../../mercurial \
98 -I../../mercurial \
99 -c -o parsers.o ../../mercurial/cext/parsers.c
99 -c -o parsers.o ../../mercurial/cext/parsers.c
100
100
101 dirs.o: sanpy ../../mercurial/cext/dirs.c
101 dirs.o: sanpy ../../mercurial/cext/dirs.c
102 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
102 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
103 -I../../mercurial \
103 -I../../mercurial \
104 -c -o dirs.o ../../mercurial/cext/dirs.c
104 -c -o dirs.o ../../mercurial/cext/dirs.c
105
105
106 pathencode.o: sanpy ../../mercurial/cext/pathencode.c
106 pathencode.o: sanpy ../../mercurial/cext/pathencode.c
107 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
107 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
108 -I../../mercurial \
108 -I../../mercurial \
109 -c -o pathencode.o ../../mercurial/cext/pathencode.c
109 -c -o pathencode.o ../../mercurial/cext/pathencode.c
110
110
111 revlog.o: sanpy ../../mercurial/cext/revlog.c
111 revlog.o: sanpy ../../mercurial/cext/revlog.c
112 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
112 $(CC) $(CFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
113 -I../../mercurial \
113 -I../../mercurial \
114 -c -o revlog.o ../../mercurial/cext/revlog.c
114 -c -o revlog.o ../../mercurial/cext/revlog.c
115
115
116 manifest_fuzzer: sanpy manifest.cc manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o
116 manifest_fuzzer: sanpy manifest.cc manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o
117 $(CXX) $(CXXFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
117 $(CXX) $(CXXFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
118 -Wno-register -Wno-macro-redefined \
118 -Wno-register -Wno-macro-redefined \
119 -I../../mercurial manifest.cc \
119 -I../../mercurial manifest.cc \
120 manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \
120 manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \
121 -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
121 -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
122 -o $$OUT/manifest_fuzzer
122 -o $$OUT/manifest_fuzzer
123
123
124 manifest_corpus.zip:
124 manifest_corpus.zip:
125 python manifest_corpus.py $$OUT/manifest_fuzzer_seed_corpus.zip
125 python manifest_corpus.py $$OUT/manifest_fuzzer_seed_corpus.zip
126
126
127 revlog_fuzzer: sanpy revlog.cc manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o
127 revlog_fuzzer: sanpy revlog.cc manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o
128 $(CXX) $(CXXFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
128 $(CXX) $(CXXFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
129 -Wno-register -Wno-macro-redefined \
129 -Wno-register -Wno-macro-redefined \
130 -I../../mercurial revlog.cc \
130 -I../../mercurial revlog.cc \
131 manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \
131 manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \
132 -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
132 -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
133 -o $$OUT/revlog_fuzzer
133 -o $$OUT/revlog_fuzzer
134
134
135 revlog_corpus.zip:
135 revlog_corpus.zip:
136 python revlog_corpus.py $$OUT/revlog_fuzzer_seed_corpus.zip
136 python revlog_corpus.py $$OUT/revlog_fuzzer_seed_corpus.zip
137
137
138 dirstate_fuzzer: sanpy dirstate.cc manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o
139 $(CXX) $(CXXFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
140 -Wno-register -Wno-macro-redefined \
141 -I../../mercurial dirstate.cc \
142 manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \
143 -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
144 -o $$OUT/dirstate_fuzzer
145
146 dirstate_corpus.zip:
147 python dirstate_corpus.py $$OUT/dirstate_fuzzer_seed_corpus.zip
148
138 clean:
149 clean:
139 $(RM) *.o *_fuzzer \
150 $(RM) *.o *_fuzzer \
140 bdiff \
151 bdiff \
141 mpatch \
152 mpatch \
142 xdiff
153 xdiff
143
154
144 oss-fuzz: bdiff_fuzzer mpatch_fuzzer mpatch_corpus.zip xdiff_fuzzer manifest_fuzzer manifest_corpus.zip revlog_fuzzer revlog_corpus.zip
155 oss-fuzz: bdiff_fuzzer mpatch_fuzzer mpatch_corpus.zip xdiff_fuzzer manifest_fuzzer manifest_corpus.zip revlog_fuzzer revlog_corpus.zip dirstate_fuzzer dirstate_corpus.zip
145
156
146 .PHONY: all clean oss-fuzz sanpy
157 .PHONY: all clean oss-fuzz sanpy
General Comments 0
You need to be logged in to leave comments. Login now