contrib/fuzz/revlog.cc
author Martin von Zweigbergk <martinvonz@google.com>
Mon, 24 Sep 2018 14:46:37 -0700
changeset 41386 0132221c25cd
parent 41335 ebe51a2e75be
child 43151 36e386dbbd30
permissions -rw-r--r--
localrepo: use context manager for lock and transaction in commitctx() Differential Revision: https://phab.mercurial-scm.org/D5624
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41024
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
#include <Python.h>
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#include <assert.h>
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
#include <stdlib.h>
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#include <unistd.h>
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
#include <string>
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
#include "pyutil.h"
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
extern "C" {
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
static PyCodeObject *code;
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
{
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
	contrib::initpy(*argv[0]);
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
	code = (PyCodeObject *)Py_CompileString(R"py(
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
from parsers import parse_index2
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
for inline in (True, False):
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
    try:
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
        index, cache = parse_index2(data, inline)
41335
ebe51a2e75be fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents: 41334
diff changeset
    22
        index.slicechunktodensity(list(range(len(index))), 0.5, 262144)
ebe51a2e75be fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents: 41334
diff changeset
    23
        for rev in range(len(index)):
ebe51a2e75be fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents: 41334
diff changeset
    24
            node = index[rev][7]
ebe51a2e75be fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents: 41334
diff changeset
    25
            partial = index.shortest(node)
ebe51a2e75be fuzz: exercise more of the revlog API
Augie Fackler <raf@durin42.com>
parents: 41334
diff changeset
    26
            index.partialmatch(node[:partial])
41024
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
    except Exception as e:
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
        pass
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
        # uncomment this print if you're editing this Python code
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
        # to debug failures.
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
        # print e
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
)py",
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
	                                        "fuzzer", Py_file_input);
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
	return 0;
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
}
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
{
41334
afc33a5705b9 fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents: 41024
diff changeset
    39
	// Don't allow fuzzer inputs larger than 60k, since we'll just bog
afc33a5705b9 fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents: 41024
diff changeset
    40
	// down and not accomplish much.
afc33a5705b9 fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents: 41024
diff changeset
    41
	if (Size > 60000) {
afc33a5705b9 fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents: 41024
diff changeset
    42
		return 0;
afc33a5705b9 fuzz: don't allow enormous revlog inputs either
Augie Fackler <raf@durin42.com>
parents: 41024
diff changeset
    43
	}
41024
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
	PyObject *text =
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
	    PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
	PyObject *locals = PyDict_New();
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
	PyDict_SetItemString(locals, "data", text);
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
	if (!res) {
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
		PyErr_Print();
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
	}
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
	Py_XDECREF(res);
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
	Py_DECREF(locals);
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
	Py_DECREF(text);
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
	return 0; // Non-zero return values are reserved for future use.
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
}
c06f0ef9a5ba fuzz: new fuzzer for revlog's parse_index2 method
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
}