_xxtestfuzz.c 1.1 KB

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