1 #define PY_SSIZE_T_CLEAN
2 #include <Python.h>
3 #include <stdlib.h>
4 #include <inttypes.h>
5 
6 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
7 
_fuzz_run(PyObject * self,PyObject * args)8 static 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 
27 static PyMethodDef module_methods[] = {
28     {"run", (PyCFunction)_fuzz_run, METH_VARARGS, ""},
29     {NULL},
30 };
31 
32 static 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 
44 PyMODINIT_FUNC
PyInit__xxtestfuzz(void)45 PyInit__xxtestfuzz(void)
46 {
47     PyObject *m = NULL;
48 
49     if ((m = PyModule_Create(&_fuzzmodule)) == NULL) {
50         return NULL;
51     }
52     return m;
53 }
54