gh-111178: fix UBSan failures for Python/instrumentation.c (#131608)

This commit is contained in:
Bénédikt Tran 2025-03-24 10:58:33 +01:00 committed by GitHub
parent 27f81e82df
commit 43fde78bef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2916,18 +2916,21 @@ typedef struct _PyLegacyBranchEventHandler {
int tool_id; int tool_id;
} _PyLegacyBranchEventHandler; } _PyLegacyBranchEventHandler;
#define _PyLegacyBranchEventHandler_CAST(op) ((_PyLegacyBranchEventHandler *)(op))
static void static void
dealloc_branch_handler(_PyLegacyBranchEventHandler *self) dealloc_branch_handler(PyObject *op)
{ {
_PyLegacyBranchEventHandler *self = _PyLegacyBranchEventHandler_CAST(op);
Py_CLEAR(self->handler); Py_CLEAR(self->handler);
PyObject_Free((PyObject *)self); PyObject_Free(self);
} }
static PyTypeObject _PyLegacyBranchEventHandler_Type = { static PyTypeObject _PyLegacyBranchEventHandler_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0) PyVarObject_HEAD_INIT(&PyType_Type, 0)
"sys.monitoring.branch_event_handler", "sys.monitoring.branch_event_handler",
sizeof(_PyLegacyBranchEventHandler), sizeof(_PyLegacyBranchEventHandler),
.tp_dealloc = (destructor)dealloc_branch_handler, .tp_dealloc = dealloc_branch_handler,
.tp_vectorcall_offset = offsetof(_PyLegacyBranchEventHandler, vectorcall), .tp_vectorcall_offset = offsetof(_PyLegacyBranchEventHandler, vectorcall),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_DISALLOW_INSTANTIATION,
@ -2936,10 +2939,11 @@ static PyTypeObject _PyLegacyBranchEventHandler_Type = {
static PyObject * static PyObject *
branch_handler( branch_handler_vectorcall(
_PyLegacyBranchEventHandler *self, PyObject *const *args, PyObject *op, PyObject *const *args,
size_t nargsf, PyObject *kwnames size_t nargsf, PyObject *kwnames
) { ) {
_PyLegacyBranchEventHandler *self = _PyLegacyBranchEventHandler_CAST(op);
// Find the other instrumented instruction and remove tool // Find the other instrumented instruction and remove tool
// The spec (PEP 669) allows spurious events after a DISABLE, // The spec (PEP 669) allows spurious events after a DISABLE,
// so a best effort is good enough. // so a best effort is good enough.
@ -3000,7 +3004,7 @@ static PyObject *make_branch_handler(int tool_id, PyObject *handler, bool right)
if (callback == NULL) { if (callback == NULL) {
return NULL; return NULL;
} }
callback->vectorcall = (vectorcallfunc)branch_handler; callback->vectorcall = branch_handler_vectorcall;
callback->handler = Py_NewRef(handler); callback->handler = Py_NewRef(handler);
callback->right = right; callback->right = right;
callback->tool_id = tool_id; callback->tool_id = tool_id;
@ -3062,6 +3066,8 @@ typedef struct {
int bi_offset; int bi_offset;
} branchesiterator; } branchesiterator;
#define branchesiterator_CAST(op) ((branchesiterator *)(op))
static PyObject * static PyObject *
int_triple(int a, int b, int c) { int_triple(int a, int b, int c) {
PyObject *obja = PyLong_FromLong(a); PyObject *obja = PyLong_FromLong(a);
@ -3088,8 +3094,9 @@ error:
} }
static PyObject * static PyObject *
branchesiter_next(branchesiterator *bi) branchesiter_next(PyObject *op)
{ {
branchesiterator *bi = branchesiterator_CAST(op);
int offset = bi->bi_offset; int offset = bi->bi_offset;
int oparg = 0; int oparg = 0;
while (offset < Py_SIZE(bi->bi_code)) { while (offset < Py_SIZE(bi->bi_code)) {
@ -3130,8 +3137,9 @@ branchesiter_next(branchesiterator *bi)
} }
static void static void
branchesiter_dealloc(branchesiterator *bi) branchesiter_dealloc(PyObject *op)
{ {
branchesiterator *bi = branchesiterator_CAST(op);
Py_DECREF(bi->bi_code); Py_DECREF(bi->bi_code);
PyObject_Free(bi); PyObject_Free(bi);
} }
@ -3142,10 +3150,10 @@ static PyTypeObject _PyBranchesIterator = {
sizeof(branchesiterator), /* tp_basicsize */ sizeof(branchesiterator), /* tp_basicsize */
0, /* tp_itemsize */ 0, /* tp_itemsize */
/* methods */ /* methods */
.tp_dealloc = (destructor)branchesiter_dealloc, .tp_dealloc = branchesiter_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_iter = PyObject_SelfIter, .tp_iter = PyObject_SelfIter,
.tp_iternext = (iternextfunc)branchesiter_next, .tp_iternext = branchesiter_next,
.tp_free = PyObject_Del, .tp_free = PyObject_Del,
}; };