Mercurial > hg > mercurial-source
changeset 41801:b444407f635b
fuzz: new fuzzer for dirstate parser
Differential Revision: https://phab.mercurial-scm.org/D5463
author | Augie Fackler <augie@google.com> |
---|---|
date | Wed, 19 Dec 2018 23:48:35 -0500 |
parents | c06f0ef9a5ba |
children | 5c68b617ba24 |
files | contrib/fuzz/Makefile contrib/fuzz/dirstate.cc contrib/fuzz/dirstate_corpus.py |
diffstat | 3 files changed, 78 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/contrib/fuzz/Makefile +++ b/contrib/fuzz/Makefile @@ -135,12 +135,23 @@ revlog_corpus.zip: python revlog_corpus.py $$OUT/revlog_fuzzer_seed_corpus.zip +dirstate_fuzzer: sanpy dirstate.cc manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o + $(CXX) $(CXXFLAGS) `$$OUT/sanpy/bin/python-config --cflags` \ + -Wno-register -Wno-macro-redefined \ + -I../../mercurial dirstate.cc \ + manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \ + -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \ + -o $$OUT/dirstate_fuzzer + +dirstate_corpus.zip: + python dirstate_corpus.py $$OUT/dirstate_fuzzer_seed_corpus.zip + clean: $(RM) *.o *_fuzzer \ bdiff \ mpatch \ xdiff -oss-fuzz: bdiff_fuzzer mpatch_fuzzer mpatch_corpus.zip xdiff_fuzzer manifest_fuzzer manifest_corpus.zip revlog_fuzzer revlog_corpus.zip +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 .PHONY: all clean oss-fuzz sanpy
new file mode 100644 --- /dev/null +++ b/contrib/fuzz/dirstate.cc @@ -0,0 +1,48 @@ +#include <Python.h> +#include <assert.h> +#include <stdlib.h> +#include <unistd.h> + +#include <string> + +#include "pyutil.h" + +extern "C" { + +static PyCodeObject *code; + +extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + contrib::initpy(*argv[0]); + code = (PyCodeObject *)Py_CompileString(R"py( +from parsers import parse_dirstate +try: + dmap = {} + copymap = {} + p = parse_dirstate(dmap, copymap, data) +except Exception as e: + pass + # uncomment this print if you're editing this Python code + # to debug failures. + # print e +)py", + "fuzzer", Py_file_input); + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) +{ + PyObject *text = + PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size); + PyObject *locals = PyDict_New(); + PyDict_SetItemString(locals, "data", text); + PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); + if (!res) { + PyErr_Print(); + } + Py_XDECREF(res); + Py_DECREF(locals); + Py_DECREF(text); + return 0; // Non-zero return values are reserved for future use. +} +}
new file mode 100644 --- /dev/null +++ b/contrib/fuzz/dirstate_corpus.py @@ -0,0 +1,18 @@ +from __future__ import absolute_import, print_function + +import argparse +import os +import zipfile + +ap = argparse.ArgumentParser() +ap.add_argument("out", metavar="some.zip", type=str, nargs=1) +args = ap.parse_args() + +reporoot = os.path.normpath(os.path.join(os.path.dirname(__file__), + '..', '..')) +dirstate = os.path.join(reporoot, '.hg', 'dirstate') + +with zipfile.ZipFile(args.out[0], "w", zipfile.ZIP_STORED) as zf: + if os.path.exists(dirstate): + with open(dirstate) as f: + zf.writestr("dirstate", f.read())