1#define PY_SSIZE_T_CLEAN
2#include <Python.h>
3#include <stdlib.h>
4#include <inttypes.h>
5
6int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
7
8static PyObject* _fuzz_run(PyObject* self, PyObject* args) {
9    const char* buf;
10    Py_ssize_t size;
11    if (!PyArg_ParseTuple(args, "s#", &buf, &size)) {
12        return NULL;
13    }
14    int rv = LLVMFuzzerTestOneInput((const uint8_t*)buf, size);
15    if (PyErr_Occurred()) {
16        return NULL;
17    }
18    if (rv != 0) {
19        // Nonzero return codes are reserved for future use.
20        PyErr_Format(
21            PyExc_RuntimeError, "Nonzero return code from fuzzer: %d", rv);
22        return NULL;
23    }
24    Py_RETURN_NONE;
25}
26
27static PyMethodDef module_methods[] = {
28    {"run", (PyCFunction)_fuzz_run, METH_VARARGS, ""},
29    {NULL},
30};
31
32static struct PyModuleDef _fuzzmodule = {
33        PyModuleDef_HEAD_INIT,
34        "_fuzz",
35        NULL,
36        0,
37        module_methods,
38        NULL,
39        NULL,
40        NULL,
41        NULL
42};
43
44PyMODINIT_FUNC
45PyInit__xxtestfuzz(void)
46{
47    return PyModule_Create(&_fuzzmodule);
48}
49