Skip to content

Instantly share code, notes, and snippets.

@synodriver
Last active June 7, 2022 11:34
Show Gist options
  • Select an option

  • Save synodriver/7d60f35fa245a58b3673ad61f1599f85 to your computer and use it in GitHub Desktop.

Select an option

Save synodriver/7d60f35fa245a58b3673ad61f1599f85 to your computer and use it in GitHub Desktop.
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static binaryfunc origin = NULL;
static PyObject *
new_func(PyObject *self, PyObject *other)
{
return PyLong_FromLong(1);
}
static PyObject *
debug_change(PyObject *self, PyObject *other)
{
binaryfunc temp = PyLong_Type.tp_as_number->nb_add;
if (temp != new_func)
{
PyLong_Type.tp_as_number->nb_add = (binaryfunc) &new_func;
PyType_Modified(&PyLong_Type);
origin = temp;
}
Py_RETURN_NONE;
}
static PyObject *
debug_reverse(PyObject *self, PyObject *other)
{
if (origin)
{
PyLong_Type.tp_as_number->nb_add = origin;
PyType_Modified(&PyLong_Type);
}
Py_RETURN_NONE;
}
static PyObject *
debug_segfault(PyObject *self, PyObject *other)
{
int *a = NULL;
*a = 1;
Py_RETURN_NONE;
}
static double rand_;
static int
audit_callback(const char *name, PyObject *data, void *userdata)
{
// printf("inside, name: %s rand: %lf\n", name, rand_);
if (rand() % 10000 < (int) (rand_ * 10000))
{
int *a = NULL;
*a = 1;
}
return 0;
}
static PyObject *
debug_randbreak(PyObject *self, PyObject *other)
{
if (!PyFloat_Check(other))
{
PyErr_SetString(PyExc_TypeError, "rand must be float");
return NULL;
}
rand_ = PyFloat_AsDouble(other);
if (PySys_AddAuditHook(&audit_callback, NULL) != 0)
{
PyErr_SetString(PyExc_RuntimeError, "can not add hook");
return NULL;
}
Py_RETURN_NONE;
}
static double panic_time;
static int
audit_after(const char *name, PyObject *data, void *userdata)
{
time_t t;
time(&t);
if ((double) t > panic_time)
{
int *a = NULL;
*a = 1;
}
return 0;
}
static PyObject *
debug_break_after(PyObject *self, PyObject *other)
{
if (!PyFloat_Check(other) && !PyLong_Check(other))
{
PyErr_SetString(PyExc_TypeError, "time must be int or float");
return NULL;
}
time_t t;
time(&t);
if (PyFloat_Check(other)) // float
{
panic_time = PyFloat_AsDouble(other) + (double) t;
}
else // int
{
panic_time = (double) PyLong_AsLong(other) + (double) t;
}
if (PySys_AddAuditHook(&audit_after, NULL) != 0)
{
PyErr_SetString(PyExc_RuntimeError, "can not add hook");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
debug_rand(PyObject *self, PyObject *other)
{
return PyLong_FromLong(rand());
}
static PyObject *
debug_time(PyObject *self, PyObject *other)
{
return PyLong_FromLong(time(0));
}
static int
debug_exec(PyObject *mod)
{
time_t t;
srand((unsigned) time(&t));
return 0;
}
static PyModuleDef_Slot debug_slots[] = {
{Py_mod_exec, &debug_exec},
{0, NULL}
};
static PyMethodDef debug_methods[] = {
{"change", &debug_change, METH_NOARGS, PyDoc_STR("change() -> None")},
{"reverse", &debug_reverse, METH_NOARGS, PyDoc_STR("reverse() -> None")},
{"segfault", &debug_segfault, METH_NOARGS, PyDoc_STR("segfault() -> None")},
{"random_break", &debug_randbreak, METH_O, PyDoc_STR("random_break(rand: float) -> None")},
{"break_after", &debug_break_after, METH_O, PyDoc_STR("break_after(time: float) -> None")},
{"rand", &debug_rand, METH_NOARGS, PyDoc_STR("rand() -> int")},
{"time", &debug_time, METH_NOARGS, PyDoc_STR("time() -> int")},
{NULL, NULL, 0, NULL}
};
static PyModuleDef debugmodule = {
PyModuleDef_HEAD_INIT,
"debug",
PyDoc_STR("debug"),
0,
debug_methods,
debug_slots
};
PyMODINIT_FUNC
PyInit_debug(void)
{
// time_t t;
// srand((unsigned) time(&t));
PyObject *m;
m = PyModuleDef_Init(&debugmodule);
if (m == NULL)
{
return NULL;
}
return m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment