# HG changeset patch # User Augie Fackler # Date 1545269213 18000 # Node ID c06f0ef9a5ba166d57bb85aa5cc421f30d6b2c0c # Parent ef103c96ed330a0deba60094b93d6b8c0a27ea30 fuzz: new fuzzer for revlog's parse_index2 method Differential Revision: https://phab.mercurial-scm.org/D5462 diff -r ef103c96ed33 -r c06f0ef9a5ba contrib/fuzz/Makefile --- a/contrib/fuzz/Makefile Wed Dec 19 21:57:23 2018 -0500 +++ b/contrib/fuzz/Makefile Wed Dec 19 20:26:53 2018 -0500 @@ -124,12 +124,23 @@ manifest_corpus.zip: python manifest_corpus.py $$OUT/manifest_fuzzer_seed_corpus.zip +revlog_fuzzer: sanpy revlog.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 revlog.cc \ + manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \ + -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \ + -o $$OUT/revlog_fuzzer + +revlog_corpus.zip: + python revlog_corpus.py $$OUT/revlog_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 +oss-fuzz: bdiff_fuzzer mpatch_fuzzer mpatch_corpus.zip xdiff_fuzzer manifest_fuzzer manifest_corpus.zip revlog_fuzzer revlog_corpus.zip .PHONY: all clean oss-fuzz sanpy diff -r ef103c96ed33 -r c06f0ef9a5ba contrib/fuzz/revlog.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contrib/fuzz/revlog.cc Wed Dec 19 20:26:53 2018 -0500 @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +#include + +#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_index2 +for inline in (True, False): + try: + index, cache = parse_index2(data, inline) + 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. +} +} diff -r ef103c96ed33 -r c06f0ef9a5ba contrib/fuzz/revlog_corpus.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contrib/fuzz/revlog_corpus.py Wed Dec 19 20:26:53 2018 -0500 @@ -0,0 +1,28 @@ +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__), + '..', '..')) +# typically a standalone index +changelog = os.path.join(reporoot, '.hg', 'store', '00changelog.i') +# an inline revlog with only a few revisions +contributing = os.path.join( + reporoot, '.hg', 'store', 'data', 'contrib', 'fuzz', 'mpatch.cc.i') + +print(changelog, os.path.exists(changelog)) +print(contributing, os.path.exists(contributing)) + +with zipfile.ZipFile(args.out[0], "w", zipfile.ZIP_STORED) as zf: + if os.path.exists(changelog): + with open(changelog) as f: + zf.writestr("00changelog.i", f.read()) + if os.path.exists(contributing): + with open(contributing) as f: + zf.writestr("contributing.i", f.read())