Skip to content

Commit 43fde78

Browse files
authored
gh-111178: fix UBSan failures for Python/instrumentation.c (#131608)
1 parent 27f81e8 commit 43fde78

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

Python/instrumentation.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,18 +2916,21 @@ typedef struct _PyLegacyBranchEventHandler {
29162916
int tool_id;
29172917
} _PyLegacyBranchEventHandler;
29182918

2919+
#define _PyLegacyBranchEventHandler_CAST(op) ((_PyLegacyBranchEventHandler *)(op))
2920+
29192921
static void
2920-
dealloc_branch_handler(_PyLegacyBranchEventHandler *self)
2922+
dealloc_branch_handler(PyObject *op)
29212923
{
2924+
_PyLegacyBranchEventHandler *self = _PyLegacyBranchEventHandler_CAST(op);
29222925
Py_CLEAR(self->handler);
2923-
PyObject_Free((PyObject *)self);
2926+
PyObject_Free(self);
29242927
}
29252928

29262929
static PyTypeObject _PyLegacyBranchEventHandler_Type = {
29272930
PyVarObject_HEAD_INIT(&PyType_Type, 0)
29282931
"sys.monitoring.branch_event_handler",
29292932
sizeof(_PyLegacyBranchEventHandler),
2930-
.tp_dealloc = (destructor)dealloc_branch_handler,
2933+
.tp_dealloc = dealloc_branch_handler,
29312934
.tp_vectorcall_offset = offsetof(_PyLegacyBranchEventHandler, vectorcall),
29322935
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
29332936
Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_DISALLOW_INSTANTIATION,
@@ -2936,10 +2939,11 @@ static PyTypeObject _PyLegacyBranchEventHandler_Type = {
29362939

29372940

29382941
static PyObject *
2939-
branch_handler(
2940-
_PyLegacyBranchEventHandler *self, PyObject *const *args,
2942+
branch_handler_vectorcall(
2943+
PyObject *op, PyObject *const *args,
29412944
size_t nargsf, PyObject *kwnames
29422945
) {
2946+
_PyLegacyBranchEventHandler *self = _PyLegacyBranchEventHandler_CAST(op);
29432947
// Find the other instrumented instruction and remove tool
29442948
// The spec (PEP 669) allows spurious events after a DISABLE,
29452949
// so a best effort is good enough.
@@ -3000,7 +3004,7 @@ static PyObject *make_branch_handler(int tool_id, PyObject *handler, bool right)
30003004
if (callback == NULL) {
30013005
return NULL;
30023006
}
3003-
callback->vectorcall = (vectorcallfunc)branch_handler;
3007+
callback->vectorcall = branch_handler_vectorcall;
30043008
callback->handler = Py_NewRef(handler);
30053009
callback->right = right;
30063010
callback->tool_id = tool_id;
@@ -3062,6 +3066,8 @@ typedef struct {
30623066
int bi_offset;
30633067
} branchesiterator;
30643068

3069+
#define branchesiterator_CAST(op) ((branchesiterator *)(op))
3070+
30653071
static PyObject *
30663072
int_triple(int a, int b, int c) {
30673073
PyObject *obja = PyLong_FromLong(a);
@@ -3088,8 +3094,9 @@ int_triple(int a, int b, int c) {
30883094
}
30893095

30903096
static PyObject *
3091-
branchesiter_next(branchesiterator *bi)
3097+
branchesiter_next(PyObject *op)
30923098
{
3099+
branchesiterator *bi = branchesiterator_CAST(op);
30933100
int offset = bi->bi_offset;
30943101
int oparg = 0;
30953102
while (offset < Py_SIZE(bi->bi_code)) {
@@ -3130,8 +3137,9 @@ branchesiter_next(branchesiterator *bi)
31303137
}
31313138

31323139
static void
3133-
branchesiter_dealloc(branchesiterator *bi)
3140+
branchesiter_dealloc(PyObject *op)
31343141
{
3142+
branchesiterator *bi = branchesiterator_CAST(op);
31353143
Py_DECREF(bi->bi_code);
31363144
PyObject_Free(bi);
31373145
}
@@ -3142,10 +3150,10 @@ static PyTypeObject _PyBranchesIterator = {
31423150
sizeof(branchesiterator), /* tp_basicsize */
31433151
0, /* tp_itemsize */
31443152
/* methods */
3145-
.tp_dealloc = (destructor)branchesiter_dealloc,
3153+
.tp_dealloc = branchesiter_dealloc,
31463154
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
31473155
.tp_iter = PyObject_SelfIter,
3148-
.tp_iternext = (iternextfunc)branchesiter_next,
3156+
.tp_iternext = branchesiter_next,
31493157
.tp_free = PyObject_Del,
31503158
};
31513159

0 commit comments

Comments
 (0)