Patchwork D5465: fuzz: new fuzzer for parsers.fm1readmarkers

login
register
mail settings
Submitter phabricator
Date Dec. 20, 2018, 12:34 p.m.
Message ID <05d36a7b4eb31207ebdc597af8ef8e13@localhost.localdomain>
Download mbox | patch
Permalink /patch/37275/
State Not Applicable
Headers show

Comments

phabricator - Dec. 20, 2018, 12:34 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGb4a0062b1bac: fuzz: new fuzzer for parsers.fm1readmarkers (authored by durin42, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D5465?vs=12914&id=12926

REVISION DETAIL
  https://phab.mercurial-scm.org/D5465

AFFECTED FILES
  contrib/fuzz/Makefile
  contrib/fuzz/fm1readmarkers.cc
  contrib/fuzz/fm1readmarkers_corpus.py

CHANGE DETAILS




To: durin42, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/contrib/fuzz/fm1readmarkers_corpus.py b/contrib/fuzz/fm1readmarkers_corpus.py
new file mode 100644
--- /dev/null
+++ b/contrib/fuzz/fm1readmarkers_corpus.py
@@ -0,0 +1,37 @@ 
+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()
+
+with zipfile.ZipFile(args.out[0], "w", zipfile.ZIP_STORED) as zf:
+    zf.writestr(
+        'smallish_obsstore',
+        (
+            # header: fm1readmarkers should start at offset 1, and
+            # read until byte 597.
+            '1\x00597\x00'
+            # body of obsstore file
+            '\x01\x00\x00\x00vA\xd7\x02+C\x1a<)\x01,\x00\x00\x01\x03\x03\xe6'
+            '\x92\xde)x\x16\xd1Xph\xc7\xa7[\xe5\xe2\x1a\xab\x1e6e\xaf\xc2\xae'
+            '\xe7\xbc\x83\xe1\x88\xa5\xda\xce>O\xbd\x04\xe9\x03\xc4o\xeb\x03'
+            '\x01\t\x05\x04\x1fef18operationamenduserAugie Fackler <raf@duri'
+            'n42.com>\x00\x00\x00vA\xd7\x02-\x8aD\xaf-\x01,\x00\x00\x01\x03\x03'
+            '\x17*\xca\x8f\x9e}i\xe0i\xbb\xdf\x9fb\x03\xd2XG?\xd3h\x98\x89\x1a'
+            '=2\xeb\xc3\xc5<\xb3\x9e\xcc\x0e;#\xee\xc3\x10ux\x03\x01\t\x05\x04'
+            '\x1fef18operationamenduserAugie Fackler <raf@durin42.com>\x00\x00'
+            '\x00vA\xd7\x02Mn\xd9%\xea\x01,\x00\x00\x01\x03\x03\x98\x89\x1a='
+            '2\xeb\xc3\xc5<\xb3\x9e\xcc\x0e;#\xee\xc3\x10ux\xe0*\xcaT\x86Z8J'
+            '\x85)\x97\xff7\xcc)\xc1\x7f\x19\x0c\x01\x03\x01\t\x05\x04\x1fef'
+            '18operationamenduserAugie Fackler <raf@durin42.com>\x00\x00\x00'
+            'yA\xd7\x02MtA\xbfj\x01,\x00\x00\x01\x03\x03\xe0*\xcaT\x86Z8J\x85'
+            ')\x97\xff7\xcc)\xc1\x7f\x19\x0c\x01\x00\x94\x01\xa9\n\xf80\x92\xa3'
+            'j\xc5X\xb1\xc9:\xd51\xb8*\xa9\x03\x01\t\x08\x04\x1fef11operatio'
+            'nhistedituserAugie Fackler <raf@durin42.com>\x00\x00\x00yA\xd7\x02'
+            'MtA\xd4\xe1\x01,\x00\x00\x01\x03\x03"\xa5\xcb\x86\xb6\xf4\xbaO\xa0'
+            'sH\xe7?\xcb\x9b\xc2n\xcfI\x9e\x14\xf0D\xf0!\x18DN\xcd\x97\x016\xa5'
+            '\xef\xa06\xcb\x884\x8a\x03\x01\t\x08\x04\x1fef14operationhisted'))
diff --git a/contrib/fuzz/fm1readmarkers.cc b/contrib/fuzz/fm1readmarkers.cc
new file mode 100644
--- /dev/null
+++ b/contrib/fuzz/fm1readmarkers.cc
@@ -0,0 +1,60 @@ 
+#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 fm1readmarkers
+def maybeint(s, default):
+    try:
+        return int(s)
+    except ValueError:
+        return default
+try:
+    parts = data.split('\0', 2)
+    if len(parts) == 3:
+        offset, stop, data = parts
+    elif len(parts) == 2:
+        stop, data = parts
+        offset = 0
+    else:
+        offset = stop = 0
+    offset, stop = maybeint(offset, 0), maybeint(stop, len(data))
+    fm1readmarkers(data, offset, stop)
+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 --git a/contrib/fuzz/Makefile b/contrib/fuzz/Makefile
--- a/contrib/fuzz/Makefile
+++ b/contrib/fuzz/Makefile
@@ -146,12 +146,23 @@ 
 dirstate_corpus.zip:
 	python dirstate_corpus.py $$OUT/dirstate_fuzzer_seed_corpus.zip
 
+fm1readmarkers_fuzzer: sanpy fm1readmarkers.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 fm1readmarkers.cc \
+	  manifest.o charencode.o parsers.o dirs.o pathencode.o revlog.o pyutil.o \
+	  -lFuzzingEngine `$$OUT/sanpy/bin/python-config --ldflags` \
+	  -o $$OUT/fm1readmarkers_fuzzer
+
+fm1readmarkers_corpus.zip:
+	python fm1readmarkers_corpus.py $$OUT/fm1readmarkers_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 dirstate_fuzzer dirstate_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 fm1readmarkers_fuzzer fm1readmarkers_corpus.zip
 
 .PHONY: all clean oss-fuzz sanpy