##// END OF EJS Templates
fuzz: new fuzzer for fncache-related functions...
Augie Fackler -
r43422:b37dd269 default
parent child Browse files
Show More
@@ -0,0 +1,78 b''
1 #include <Python.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "pyutil.h"
7
8 #include <iostream>
9 #include <string>
10
11 extern "C" {
12
13 static PyCodeObject *code;
14
15 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
16 {
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
28 try:
29 for fn in (
30 isasciistr,
31 asciilower,
32 asciiupper,
33 encodedir,
34 pathencode,
35 lowerencode,
36 ):
37 try:
38 fn(data)
39 except UnicodeDecodeError:
40 pass # some functions emit this exception
41 except AttributeError:
42 # pathencode needs hashlib, which fails to import because the time
43 # module fails to import. We should try and fix that some day, but
44 # for now we at least get coverage on non-hashencoded codepaths.
45 if fn != pathencode:
46 raise
47 # uncomment this for debugging exceptions
48 # except Exception as e:
49 # raise Exception('%r: %r' % (fn, e))
50 except Exception as e:
51 pass
52 # uncomment this print if you're editing this Python code
53 # to debug failures.
54 # print(e)
55 )py",
56 "fuzzer", Py_file_input);
57 if (!code) {
58 std::cerr << "failed to compile Python code!" << std::endl;
59 }
60 return 0;
61 }
62
63 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
64 {
65 PyObject *mtext =
66 PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
67 PyObject *locals = PyDict_New();
68 PyDict_SetItemString(locals, "data", mtext);
69 PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
70 if (!res) {
71 PyErr_Print();
72 }
73 Py_XDECREF(res);
74 Py_DECREF(locals);
75 Py_DECREF(mtext);
76 return 0; // Non-zero return values are reserved for future use.
77 }
78 }
@@ -113,6 +113,14 b' dirs_fuzzer: dirs.cc manifest.o charenco'
113 -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
113 -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
114 -o $$OUT/dirs_fuzzer
114 -o $$OUT/dirs_fuzzer
115
115
116 fncache_fuzzer: fncache.cc manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o
117 $(CXX) $(CXXFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \
118 -Wno-register -Wno-macro-redefined \
119 -I../../mercurial fncache.cc \
120 manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \
121 -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
122 -o $$OUT/fncache_fuzzer
123
116 manifest_corpus.zip:
124 manifest_corpus.zip:
117 python manifest_corpus.py $$OUT/manifest_fuzzer_seed_corpus.zip
125 python manifest_corpus.py $$OUT/manifest_fuzzer_seed_corpus.zip
118
126
@@ -163,6 +171,6 b' clean:'
163 mpatch \
171 mpatch \
164 xdiff
172 xdiff
165
173
166 oss-fuzz: bdiff_fuzzer mpatch_fuzzer mpatch_corpus.zip xdiff_fuzzer dirs_fuzzer manifest_fuzzer manifest_corpus.zip revlog_fuzzer revlog_corpus.zip dirstate_fuzzer dirstate_corpus.zip fm1readmarkers_fuzzer fm1readmarkers_corpus.zip
174 oss-fuzz: bdiff_fuzzer mpatch_fuzzer mpatch_corpus.zip xdiff_fuzzer dirs_fuzzer fncache_fuzzer manifest_fuzzer manifest_corpus.zip revlog_fuzzer revlog_corpus.zip dirstate_fuzzer dirstate_corpus.zip fm1readmarkers_fuzzer fm1readmarkers_corpus.zip
167
175
168 .PHONY: all clean oss-fuzz
176 .PHONY: all clean oss-fuzz
General Comments 0
You need to be logged in to leave comments. Login now