comparison contrib/fuzz/dirs.cc @ 43150:7ff40418c6bf

fuzz: new fuzzer for dirs.c This found a six-year-old bug immediately, and then I put it through a few CPU-days of time before sending it. Differential Revision: https://phab.mercurial-scm.org/D7031
author Augie Fackler <augie@google.com>
date Wed, 09 Oct 2019 20:48:12 -0700
parents
children 8766728dbce6
comparison
equal deleted inserted replaced
43149:2a0774e9d2a8 43150:7ff40418c6bf
1 #include <Python.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "pyutil.h"
7
8 #include <string>
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 dirs
19 try:
20 files = mdata.split('\n')
21 d = dirs(files)
22 list(d)
23 'a' in d
24 if files:
25 files[0] in d
26 except Exception as e:
27 pass
28 # uncomment this print if you're editing this Python code
29 # to debug failures.
30 # print e
31 )py",
32 "fuzzer", Py_file_input);
33 return 0;
34 }
35
36 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
37 {
38 // Don't allow fuzzer inputs larger than 100k, since we'll just bog
39 // down and not accomplish much.
40 if (Size > 100000) {
41 return 0;
42 }
43 PyObject *mtext =
44 PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
45 PyObject *locals = PyDict_New();
46 PyDict_SetItemString(locals, "mdata", mtext);
47 PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
48 if (!res) {
49 PyErr_Print();
50 }
51 Py_XDECREF(res);
52 Py_DECREF(locals);
53 Py_DECREF(mtext);
54 return 0; // Non-zero return values are reserved for future use.
55 }
56 }