8000 gh-111178: fix UBSan failures for `Python/legacy_tracing.c` (#131611) · python/cpython@511a844 · GitHub
[go: up one dir, main page]

Skip to content

Commit 511a844

Browse files
authored
gh-111178: fix UBSan failures for Python/legacy_tracing.c (#131611)
1 parent 4519179 commit 511a844

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

Python/legacy_tracing.c

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ typedef struct _PyLegacyEventHandler {
1818
int event;
1919
} _PyLegacyEventHandler;
2020

21+
#define _PyLegacyEventHandler_CAST(op) ((_PyLegacyEventHandler *)(op))
22+
2123
#ifdef Py_GIL_DISABLED
2224
#define LOCK_SETUP() PyMutex_Lock(&_PyRuntime.ceval.sys_trace_profile_mutex);
2325
#define UNLOCK_SETUP() PyMutex_Unlock(&_PyRuntime.ceval.sys_trace_profile_mutex);
@@ -56,49 +58,54 @@ call_profile_func(_PyLegacyEventHandler *self, PyObject *arg)
5658

5759
static PyObject *
5860
sys_profile_start(
59-
_PyLegacyEventHandler *self, PyObject *const *args,
61+
PyObject *callable, PyObject *const *args,
6062
size_t nargsf, PyObject *kwnames
6163
) {
64+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
6265
assert(kwnames == NULL);
6366
assert(PyVectorcall_NARGS(nargsf) == 2);
6467
return call_profile_func(self, Py_None);
6568
}
6669

6770
static PyObject *
6871
sys_profile_throw(
69-
_PyLegacyEventHandler *self, PyObject *const *args,
72+
PyObject *callable, PyObject *const *args,
7073
size_t nargsf, PyObject *kwnames
7174
) {
75+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
7276
assert(kwnames == NULL);
7377
assert(PyVectorcall_NARGS(nargsf) == 3);
7478
return call_profile_func(self, Py_None);
7579
}
7680

7781
static PyObject *
7882
sys_profile_return(
79-
_PyLegacyEventHandler *self, PyObject *const *args,
83+
PyObject *callable, PyObject *const *args,
8084
size_t nargsf, PyObject *kwnames
8185
) {
86+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
8287
assert(kwnames == NULL);
8388
assert(PyVectorcall_NARGS(nargsf) == 3);
8489
return call_profile_func(self, args[2]);
8590
}
8691

8792
static PyObject *
8893
sys_profile_unwind(
89-
_PyLegacyEventHandler *self, PyObject *const *args,
94+
PyObject *callable, PyObject *const *args,
9095
size_t nargsf, PyObject *kwnames
9196
) {
97+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
9298
assert(kwnames == NULL);
9399
assert(PyVectorcall_NARGS(nargsf) == 3);
94-
return call_profile_func(self, NULL);
100+
return call_profile_func(self, NULL);
95101
}
96102

97103
static PyObject *
98104
sys_profile_call_or_return(
99-
_PyLegacyEventHandler *self, PyObject *const *args,
105+
PyObject *op, PyObject *const *args,
100106
size_t nargsf, PyObject *kwnames
101107
) {
108+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op);
102109
assert(kwnames == NULL);
103110
assert(PyVectorcall_NARGS(nargsf) == 4);
104111
PyObject *callable = args[2];
@@ -188,7 +195,7 @@ sys_trace_exception_func(
188195
PyObject *callable, PyObject *const *args,
189196
size_t nargsf, PyObject *kwnames
190197
) {
191-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)callable;
198+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
192199
assert(kwnames == NULL);
193200
assert(PyVectorcall_NARGS(nargsf) == 3);
194201
PyObject *exc = args[2];
@@ -213,7 +220,7 @@ sys_trace_start(
213220
PyObject *callable, PyObject *const *args,
214221
size_t nargsf, PyObject *kwnames
215222
) {
216-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)callable;
223+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
217224
assert(kwnames == NULL);
218225
assert(PyVectorcall_NARGS(nargsf) == 2);
219226
return call_trace_func(self, Py_None);
@@ -224,7 +231,7 @@ sys_trace_throw(
224231
PyObject *callable, PyObject *const *args,
225232
size_t nargsf, PyObject *kwnames
226233
) {
227-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)callable;
234+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
228235
assert(kwnames == NULL);
229236
assert(PyVectorcall_NARGS(nargsf) == 3);
230237
return call_trace_func(self, Py_None);
@@ -235,7 +242,7 @@ sys_trace_unwind(
235242
PyObject *callable, PyObject *const *args,
236243
size_t nargsf, PyObject *kwnames
237244
) {
238-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)< 9E12 /span>callable;
245+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
239246
assert(kwnames == NULL);
240247
assert(PyVectorcall_NARGS(nargsf) == 3);
241248
return call_trace_func(self, NULL);
@@ -246,7 +253,7 @@ sys_trace_return(
246253
PyObject *callable, PyObject *const *args,
247254
size_t nargsf, PyObject *kwnames
248255
) {
249-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)callable;
256+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
250257
assert(!PyErr_Occurred());
251258
assert(kwnames == NULL);
252259
assert(PyVectorcall_NARGS(nargsf) == 3);
@@ -261,7 +268,7 @@ sys_trace_yield(
261268
PyObject *callable, PyObject *const *args,
262269
size_t nargsf, PyObject *kwnames
263270
) {
264-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)callable;
271+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
265272
assert(kwnames == NULL);
266273
assert(PyVectorcall_NARGS(nargsf) == 3);
267274
return call_trace_func(self, args[2]);
@@ -272,7 +279,7 @@ sys_trace_instruction_func(
272279
PyObject *callable, PyObject *const *args,
273280
size_t nargsf, PyObject *kwnames
274281
) {
275-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)callable;
282+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
276283
assert(kwnames == NULL);
277284
assert(PyVectorcall_NARGS(nargsf) == 2);
278285
PyFrameObject *frame = PyEval_GetFrame();
@@ -325,7 +332,7 @@ sys_trace_line_func(
325332
PyObject *callable, PyObject *const *args,
326333
size_t nargsf, PyObject *kwnames
327334
) {
328-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)callable;
335+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
329336
assert(kwnames == NULL);
330337
PyThreadState *tstate = _PyThreadState_GET();
331338
if (tstate->c_tracefunc == NULL) {
@@ -352,7 +359,7 @@ sys_trace_jump_func(
352359
PyObject *callable, PyObject *const *args,
353360
size_t nargsf, PyObject *kwnames
354361
) {
355-
_PyLegacyEventHandler *self = (_PyLegacyEventHandler*)callable;
362+
_PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
356363
assert(kwnames == NULL);
357364
PyThreadState *tstate = _PyThreadState_GET();
358365
if (tstate->c_tracefunc == NULL) {
@@ -439,38 +446,40 @@ setup_profile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg, PyObject
439446
if (!tstate->interp->sys_profile_initialized) {
440447
tstate->interp->sys_profile_initialized = true;
441448
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
442-
(vectorcallfunc)sys_profile_start, PyTrace_CALL,
443-
PY_MONITORING_EVENT_PY_START, PY_MONITORING_EVENT_PY_RESUME)) {
449+
sys_profile_start, PyTrace_CALL,
450+
PY_MONITORING_EVENT_PY_START,
451+
PY_MONITORING_EVENT_PY_RESUME)) {
444452
return -1;
445453
}
446454
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
447-
(vectorcallfunc)sys_profile_throw, PyTrace_CALL,
448-
PY_MONITORING_EVENT_PY_THROW, -1)) {
455+
sys_profile_throw, PyTrace_CALL,
456+
PY_MONITORING_EVENT_PY_THROW, -1)) {
449457
return -1;
450458
}
451459
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
452-
(vectorcallfunc)sys_profile_return, PyTrace_RETURN,
453-
PY_MONITORING_EVENT_PY_RETURN, PY_MONITORING_EVENT_PY_YIELD)) {
460+
sys_profile_return, PyTrace_RETURN,
461+
PY_MONITORING_EVENT_PY_RETURN,
462+
PY_MONITORING_EVENT_PY_YIELD)) {
454463
return -1;
455464
}
456465
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
457-
(vectorcallfunc)sys_profile_unwind, PyTrace_RETURN,
458-
PY_MONITORING_EVENT_PY_UNWIND, -1)) {
466+
sys_profile_unwind, PyTrace_RETURN,
467+
PY_MONITORING_EVENT_PY_UNWIND, -1)) {
459468
return -1;
460469
}
461470
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
462-
(vectorcallfunc)sys_profile_call_or_return, PyTrace_C_CALL,
463-
PY_MONITORING_EVENT_CALL, -1)) {
471+
sys_profile_call_or_return, PyTrace_C_CALL,
472+
PY_MONITORING_EVENT_CALL, -1)) {
464473
return -1;
465474
}
466475
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
467-
(vectorcallfunc)sys_profile_call_or_return, PyTrace_C_RETURN,
468-
PY_MONITORING_EVENT_C_RETURN, -1)) {
476+
sys_profile_call_or_return, PyTrace_C_RETURN,
477+
PY_MONITORING_EVENT_C_RETURN, -1)) {
469478
return -1;
470479
}
471480
if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
472-
(vectorcallfunc)sys_profile_call_or_return, PyTrace_C_EXCEPTION,
473-
PY_MONITORING_EVENT_C_RAISE, -1)) {
481+
sys_profile_call_or_return, PyTrace_C_EXCEPTION,
482+
PY_MONITORING_EVENT_C_RAISE, -1)) {
474483
return -1;
475484
}
476485
}

0 commit comments

Comments
 (0)
0