contrib/fuzz/fm1readmarkers.cc
author Raphaël Gomès <rgomes@octobus.net>
Tue, 02 Jul 2019 17:15:03 +0200
changeset 42609 326fdce22fb2
parent 41024 6a951f535fee
child 43859 8766728dbce6
permissions -rw-r--r--
rust: switch hg-core and hg-cpython to rust 2018 edition Many interesting changes have happened in Rust since the Oxidation Plan was introduced, like the 2018 edition and procedural macros: - Opting in to the 2018 edition is a clear benefit in terms of future proofing, new (nice to have) syntactical sugar notwithstanding. It also has a new non-lexical, non-AST based borrow checker that has fewer bugs(!) and allows us to write correct code that in some cases would have been rejected by the old one. - Procedural macros allow us to use the PyO3 crate which maintainers have expressed the clear goal of compiling on stable, which would help in code maintainability compared to rust-cpython. In this patch are the following changes: - Removing most `extern crate` uses - Updating `use` clauses (`crate` keyword, nested `use`) - Removing `mod.rs` in favor of an aptly named module file Like discussed in the mailing list ( https://www.mercurial-scm.org/pipermail/mercurial-devel/2019-July/132316.html ), until Rust integration in Mercurial is considered to be out of the experimental phase, the maximum version of Rust allowed is whatever the latest version Debian packages. Differential Revision: https://phab.mercurial-scm.org/D6597
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41024
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
#include <Python.h>
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#include <assert.h>
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
#include <stdlib.h>
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#include <unistd.h>
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
#include <string>
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
#include "pyutil.h"
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
extern "C" {
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
static PyCodeObject *code;
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
{
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
	contrib::initpy(*argv[0]);
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
	code = (PyCodeObject *)Py_CompileString(R"py(
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
from parsers import fm1readmarkers
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
def maybeint(s, default):
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
    try:
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
        return int(s)
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
    except ValueError:
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
        return default
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
try:
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
    parts = data.split('\0', 2)
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
    if len(parts) == 3:
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
        offset, stop, data = parts
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
    elif len(parts) == 2:
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
        stop, data = parts
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
        offset = 0
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
    else:
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
        offset = stop = 0
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
    offset, stop = maybeint(offset, 0), maybeint(stop, len(data))
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
    fm1readmarkers(data, offset, stop)
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
except Exception as e:
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
    pass
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
    # uncomment this print if you're editing this Python code
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
    # to debug failures.
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
    # print e
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
)py",
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
	                                        "fuzzer", Py_file_input);
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
	return 0;
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
}
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
{
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
	PyObject *text =
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
	    PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
	PyObject *locals = PyDict_New();
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
	PyDict_SetItemString(locals, "data", text);
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
	if (!res) {
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
		PyErr_Print();
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
	}
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
	Py_XDECREF(res);
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
	Py_DECREF(locals);
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
	Py_DECREF(text);
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    58
	return 0; // Non-zero return values are reserved for future use.
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
}
6a951f535fee fuzz: new fuzzer for parsers.fm1readmarkers
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
}