From 77b2576e88d3dfb9fb11fff684a65526c68e3ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 20 Mar 2025 20:43:09 +0100 Subject: [PATCH 1/6] fix UBSan failures for `legacy_tracing.c` --- Python/legacy_tracing.c | 47 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/Python/legacy_tracing.c b/Python/legacy_tracing.c index 124b219ea8af97..560b752e3811cb 100644 --- a/Python/legacy_tracing.c +++ b/Python/legacy_tracing.c @@ -16,6 +16,8 @@ typedef struct _PyLegacyEventHandler { int event; } _PyLegacyEventHandler; +#define _PyLegacyEventHandler_CAST(op) ((_PyLegacyEventHandler *)(op)) + #ifdef Py_GIL_DISABLED #define LOCK_SETUP() PyMutex_Lock(&_PyRuntime.ceval.sys_trace_profile_mutex); #define UNLOCK_SETUP() PyMutex_Unlock(&_PyRuntime.ceval.sys_trace_profile_mutex); @@ -54,51 +56,56 @@ call_profile_func(_PyLegacyEventHandler *self, PyObject *arg) static PyObject * sys_profile_start( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 2); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); return call_profile_func(self, Py_None); } static PyObject * sys_profile_throw( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); return call_profile_func(self, Py_None); } static PyObject * sys_profile_return( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); return call_profile_func(self, args[2]); } static PyObject * sys_profile_unwind( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); - return call_profile_func(self, NULL); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); + return call_profile_func(self, NULL); } static PyObject * sys_profile_call_or_return( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 4); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); PyObject *callable = args[2]; if (PyCFunction_Check(callable)) { return call_profile_func(self, callable); @@ -428,38 +435,38 @@ setup_profile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg, PyObject if (!tstate->interp->sys_profile_initialized) { tstate->interp->sys_profile_initialized = true; if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, - (vectorcallfunc)sys_profile_start, PyTrace_CALL, - PY_MONITORING_EVENT_PY_START, PY_MONITORING_EVENT_PY_RESUME)) { + sys_profile_start, PyTrace_CALL, + PY_MONITORING_EVENT_PY_START, PY_MONITORING_EVENT_PY_RESUME)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, - (vectorcallfunc)sys_profile_throw, PyTrace_CALL, - PY_MONITORING_EVENT_PY_THROW, -1)) { + sys_profile_throw, PyTrace_CALL, + PY_MONITORING_EVENT_PY_THROW, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, - (vectorcallfunc)sys_profile_return, PyTrace_RETURN, - PY_MONITORING_EVENT_PY_RETURN, PY_MONITORING_EVENT_PY_YIELD)) { + sys_profile_return, PyTrace_RETURN, + PY_MONITORING_EVENT_PY_RETURN, PY_MONITORING_EVENT_PY_YIELD)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, - (vectorcallfunc)sys_profile_unwind, PyTrace_RETURN, - PY_MONITORING_EVENT_PY_UNWIND, -1)) { + sys_profile_unwind, PyTrace_RETURN, + PY_MONITORING_EVENT_PY_UNWIND, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, - (vectorcallfunc)sys_profile_call_or_return, PyTrace_C_CALL, - PY_MONITORING_EVENT_CALL, -1)) { + sys_profile_call_or_return, PyTrace_C_CALL, + PY_MONITORING_EVENT_CALL, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, - (vectorcallfunc)sys_profile_call_or_return, PyTrace_C_RETURN, - PY_MONITORING_EVENT_C_RETURN, -1)) { + sys_profile_call_or_return, PyTrace_C_RETURN, + PY_MONITORING_EVENT_C_RETURN, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, - (vectorcallfunc)sys_profile_call_or_return, PyTrace_C_EXCEPTION, - PY_MONITORING_EVENT_C_RAISE, -1)) { + sys_profile_call_or_return, PyTrace_C_EXCEPTION, + PY_MONITORING_EVENT_C_RAISE, -1)) { return -1; } } From af9703ad34693e94f665811f3a0e64801776c57e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 20 Mar 2025 21:31:11 +0100 Subject: [PATCH 2/6] fixup --- Python/legacy_tracing.c | 67 +++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/Python/legacy_tracing.c b/Python/legacy_tracing.c index 560b752e3811cb..2afac06fd029b3 100644 --- a/Python/legacy_tracing.c +++ b/Python/legacy_tracing.c @@ -59,9 +59,9 @@ sys_profile_start( PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 2); - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); return call_profile_func(self, Py_None); } @@ -70,9 +70,9 @@ sys_profile_throw( PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); return call_profile_func(self, Py_None); } @@ -81,9 +81,9 @@ sys_profile_return( PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); return call_profile_func(self, args[2]); } @@ -92,9 +92,9 @@ sys_profile_unwind( PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); return call_profile_func(self, NULL); } @@ -103,9 +103,9 @@ sys_profile_call_or_return( PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 4); - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); PyObject *callable = args[2]; if (PyCFunction_Check(callable)) { return call_profile_func(self, callable); @@ -190,9 +190,10 @@ call_trace_func(_PyLegacyEventHandler *self, PyObject *arg) static PyObject * sys_trace_exception_func( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); PyObject *exc = args[2]; @@ -214,9 +215,10 @@ sys_trace_exception_func( static PyObject * sys_trace_start( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 2); return call_trace_func(self, Py_None); @@ -224,9 +226,10 @@ sys_trace_start( static PyObject * sys_trace_throw( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); return call_trace_func(self, Py_None); @@ -234,9 +237,10 @@ sys_trace_throw( static PyObject * sys_trace_unwind( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); return call_trace_func(self, NULL); @@ -244,9 +248,10 @@ sys_trace_unwind( static PyObject * sys_trace_return( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(!PyErr_Occurred()); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); @@ -258,9 +263,10 @@ sys_trace_return( static PyObject * sys_trace_yield( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); return call_trace_func(self, args[2]); @@ -268,9 +274,10 @@ sys_trace_yield( static PyObject * sys_trace_instruction_func( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 2); PyFrameObject *frame = PyEval_GetFrame(); @@ -520,48 +527,48 @@ setup_tracing(PyThreadState *tstate, Py_tracefunc func, PyObject *arg, PyObject if (!tstate->interp->sys_trace_initialized) { tstate->interp->sys_trace_initialized = true; if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_start, PyTrace_CALL, - PY_MONITORING_EVENT_PY_START, PY_MONITORING_EVENT_PY_RESUME)) { + sys_trace_start, PyTrace_CALL, + PY_MONITORING_EVENT_PY_START, PY_MONITORING_EVENT_PY_RESUME)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_throw, PyTrace_CALL, - PY_MONITORING_EVENT_PY_THROW, -1)) { + sys_trace_throw, PyTrace_CALL, + PY_MONITORING_EVENT_PY_THROW, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_return, PyTrace_RETURN, - PY_MONITORING_EVENT_PY_RETURN, -1)) { + sys_trace_return, PyTrace_RETURN, + PY_MONITORING_EVENT_PY_RETURN, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_yield, PyTrace_RETURN, - PY_MONITORING_EVENT_PY_YIELD, -1)) { + sys_trace_yield, PyTrace_RETURN, + PY_MONITORING_EVENT_PY_YIELD, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_exception_func, PyTrace_EXCEPTION, - PY_MONITORING_EVENT_RAISE, PY_MONITORING_EVENT_STOP_ITERATION)) { + sys_trace_exception_func, PyTrace_EXCEPTION, + PY_MONITORING_EVENT_RAISE, PY_MONITORING_EVENT_STOP_ITERATION)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_line_func, PyTrace_LINE, - PY_MONITORING_EVENT_LINE, -1)) { + sys_trace_line_func, PyTrace_LINE, + PY_MONITORING_EVENT_LINE, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_unwind, PyTrace_RETURN, - PY_MONITORING_EVENT_PY_UNWIND, -1)) { + sys_trace_unwind, PyTrace_RETURN, + PY_MONITORING_EVENT_PY_UNWIND, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_jump_func, PyTrace_LINE, - PY_MONITORING_EVENT_JUMP, -1)) { + sys_trace_jump_func, PyTrace_LINE, + PY_MONITORING_EVENT_JUMP, -1)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_TRACE_ID, - (vectorcallfunc)sys_trace_instruction_func, PyTrace_OPCODE, - PY_MONITORING_EVENT_INSTRUCTION, -1)) { + sys_trace_instruction_func, PyTrace_OPCODE, + PY_MONITORING_EVENT_INSTRUCTION, -1)) { return -1; } } From b21ae7392906a47f04f72802e42eaf646c3ef0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 20 Mar 2025 21:32:47 +0100 Subject: [PATCH 3/6] fixup --- Python/legacy_tracing.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Python/legacy_tracing.c b/Python/legacy_tracing.c index 2afac06fd029b3..1f6b514ef5d419 100644 --- a/Python/legacy_tracing.c +++ b/Python/legacy_tracing.c @@ -327,9 +327,10 @@ trace_line( static PyObject * sys_trace_line_func( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); PyThreadState *tstate = _PyThreadState_GET(); if (tstate->c_tracefunc == NULL) { @@ -353,9 +354,10 @@ sys_trace_line_func( * Handle that case here */ static PyObject * sys_trace_jump_func( - _PyLegacyEventHandler *self, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); PyThreadState *tstate = _PyThreadState_GET(); if (tstate->c_tracefunc == NULL) { From e5ab45a12eefecc0b7f2467c1b83095e51c1e411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 23 Mar 2025 11:04:30 +0100 Subject: [PATCH 4/6] Merge remote-tracking branch 'upstream/main' into fix/ubsan/iterobject-111178 # Conflicts: # Objects/iterobject.c --- Python/legacy_tracing.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Python/legacy_tracing.c b/Python/legacy_tracing.c index 153d6343a55be5..d05e01bc4f0ea0 100644 --- a/Python/legacy_tracing.c +++ b/Python/legacy_tracing.c @@ -447,7 +447,8 @@ setup_profile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg, PyObject tstate->interp->sys_profile_initialized = true; if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, sys_profile_start, PyTrace_CALL, - PY_MONITORING_EVENT_PY_START, PY_MONITORING_EVENT_PY_RESUME)) { + PY_MONITORING_EVENT_PY_START, + PY_MONITORING_EVENT_PY_RESUME)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, @@ -457,7 +458,8 @@ setup_profile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg, PyObject } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, sys_profile_return, PyTrace_RETURN, - PY_MONITORING_EVENT_PY_RETURN, PY_MONITORING_EVENT_PY_YIELD)) { + PY_MONITORING_EVENT_PY_RETURN, + PY_MONITORING_EVENT_PY_YIELD)) { return -1; } if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID, From 951f70c9742c7c2fd5ec337b48f7ba60fbc55bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 23 Mar 2025 11:05:57 +0100 Subject: [PATCH 5/6] renaming --- Python/legacy_tracing.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Python/legacy_tracing.c b/Python/legacy_tracing.c index d05e01bc4f0ea0..9ed84cd6274beb 100644 --- a/Python/legacy_tracing.c +++ b/Python/legacy_tracing.c @@ -58,10 +58,10 @@ call_profile_func(_PyLegacyEventHandler *self, PyObject *arg) static PyObject * sys_profile_start( - PyObject *op, PyObject *const *args, + PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 2); return call_profile_func(self, Py_None); @@ -69,10 +69,10 @@ sys_profile_start( static PyObject * sys_profile_throw( - PyObject *op, PyObject *const *args, + PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); return call_profile_func(self, Py_None); @@ -80,10 +80,10 @@ sys_profile_throw( static PyObject * sys_profile_return( - PyObject *op, PyObject *const *args, + PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); return call_profile_func(self, args[2]); @@ -91,10 +91,10 @@ sys_profile_return( static PyObject * sys_profile_unwind( - PyObject *op, PyObject *const *args, + PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 3); return call_profile_func(self, NULL); @@ -102,10 +102,10 @@ sys_profile_unwind( static PyObject * sys_profile_call_or_return( - PyObject *op, PyObject *const *args, + PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 4); PyObject *callable = args[2]; From 256eb574ae8af160625e2d61e947af3639a3d90d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 23 Mar 2025 11:14:38 +0100 Subject: [PATCH 6/6] fix compilation --- Python/legacy_tracing.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python/legacy_tracing.c b/Python/legacy_tracing.c index 9ed84cd6274beb..dbd19d7755c237 100644 --- a/Python/legacy_tracing.c +++ b/Python/legacy_tracing.c @@ -102,10 +102,10 @@ sys_profile_unwind( static PyObject * sys_profile_call_or_return( - PyObject *callable, PyObject *const *args, + PyObject *op, PyObject *const *args, size_t nargsf, PyObject *kwnames ) { - _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable); + _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op); assert(kwnames == NULL); assert(PyVectorcall_NARGS(nargsf) == 4); PyObject *callable = args[2];