Skip to content

Commit af9d887

Browse files
committed
fix UBSan failures for optimizer.c
1 parent 49234c0 commit af9d887

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

Python/optimizer.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#define MAX_EXECUTORS_SIZE 256
2525

26+
#define _PyExecutorObject_CAST(op) ((_PyExecutorObject *)(op))
27+
2628
static bool
2729
has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
2830
{
@@ -194,11 +196,12 @@ get_oparg(PyObject *self, PyObject *Py_UNUSED(ignored))
194196

195197
///////////////////// Experimental UOp Optimizer /////////////////////
196198

197-
static int executor_clear(_PyExecutorObject *executor);
199+
static int executor_clear(PyObject *executor);
198200
static void unlink_executor(_PyExecutorObject *executor);
199201

200202
static void
201-
uop_dealloc(_PyExecutorObject *self) {
203+
uop_dealloc(PyObject *op) {
204+
_PyExecutorObject *self = _PyExecutorObject_CAST(op);
202205
_PyObject_GC_UNTRACK(self);
203206
assert(self->vm_data.code == NULL);
204207
unlink_executor(self);
@@ -255,15 +258,17 @@ _PyUOpPrint(const _PyUOpInstruction *uop)
255258
#endif
256259

257260
static Py_ssize_t
258-
uop_len(_PyExecutorObject *self)
261+
uop_len(PyObject *op)
259262
{
263+
_PyExecutorObject *self = _PyExecutorObject_CAST(op);
260264
return self->code_size;
261265
}
262266

263267
static PyObject *
264-
uop_item(_PyExecutorObject *self, Py_ssize_t index)
268+
uop_item(PyObject *op, Py_ssize_t index)
265269
{
266-
Py_ssize_t len = uop_len(self);
270+
_PyExecutorObject *self = _PyExecutorObject_CAST(op);
271+
Py_ssize_t len = uop_len(op);
267272
if (index < 0 || index >= len) {
268273
PyErr_SetNone(PyExc_IndexError);
269274
return NULL;
@@ -299,14 +304,14 @@ uop_item(_PyExecutorObject *self, Py_ssize_t index)
299304
}
300305

301306
PySequenceMethods uop_as_sequence = {
302-
.sq_length = (lenfunc)uop_len,
303-
.sq_item = (ssizeargfunc)uop_item,
307+
.sq_length = uop_len,
308+
.sq_item = uop_item,
304309
};
305310

306311
static int
307312
executor_traverse(PyObject *o, visitproc visit, void *arg)
308313
{
309-
_PyExecutorObject *executor = (_PyExecutorObject *)o;
314+
_PyExecutorObject *executor = _PyExecutorObject_CAST(o);
310315
for (uint32_t i = 0; i < executor->exit_count; i++) {
311316
Py_VISIT(executor->exits[i].executor);
312317
}
@@ -320,7 +325,7 @@ get_jit_code(PyObject *self, PyObject *Py_UNUSED(ignored))
320325
PyErr_SetString(PyExc_RuntimeError, "JIT support not enabled.");
321326
return NULL;
322327
#else
323-
_PyExecutorObject *executor = (_PyExecutorObject *)self;
328+
_PyExecutorObject *executor = _PyExecutorObject_CAST(self);
324329
if (executor->jit_code == NULL || executor->jit_size == 0) {
325330
Py_RETURN_NONE;
326331
}
@@ -348,11 +353,11 @@ PyTypeObject _PyUOpExecutor_Type = {
348353
.tp_basicsize = offsetof(_PyExecutorObject, exits),
349354
.tp_itemsize = 1,
350355
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC,
351-
.tp_dealloc = (destructor)uop_dealloc,
356+
.tp_dealloc = uop_dealloc,
352357
.tp_as_sequence = &uop_as_sequence,
353358
.tp_methods = uop_executor_methods,
354359
.tp_traverse = executor_traverse,
355-
.tp_clear = (inquiry)executor_clear,
360+
.tp_clear = executor_clear,
356361
.tp_is_gc = executor_is_gc,
357362
};
358363

@@ -1421,8 +1426,9 @@ _Py_ExecutorDetach(_PyExecutorObject *executor)
14211426
}
14221427

14231428
static int
1424-
executor_clear(_PyExecutorObject *executor)
1429+
executor_clear(PyObject *op)
14251430
{
1431+
_PyExecutorObject *executor = _PyExecutorObject_CAST(op);
14261432
if (!executor->vm_data.valid) {
14271433
return 0;
14281434
}
@@ -1478,7 +1484,7 @@ _Py_Executors_InvalidateDependency(PyInterpreterState *interp, void *obj, int is
14781484
exec = next;
14791485
}
14801486
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(invalidate); i++) {
1481-
_PyExecutorObject *exec = (_PyExecutorObject *)PyList_GET_ITEM(invalidate, i);
1487+
PyObject *exec = PyList_GET_ITEM(invalidate, i);
14821488
executor_clear(exec);
14831489
if (is_invalidation) {
14841490
OPT_STAT_INC(executors_invalidated);
@@ -1505,7 +1511,7 @@ _Py_Executors_InvalidateAll(PyInterpreterState *interp, int is_invalidation)
15051511
_PyCode_Clear_Executors(executor->vm_data.code);
15061512
}
15071513
else {
1508-
executor_clear(executor);
1514+
executor_clear((PyObject *)executor);
15091515
}
15101516
if (is_invalidation) {
15111517
OPT_STAT_INC(executors_invalidated);
@@ -1539,7 +1545,7 @@ _Py_Executors_InvalidateCold(PyInterpreterState *interp)
15391545
exec = next;
15401546
}
15411547
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(invalidate); i++) {
1542-
_PyExecutorObject *exec = (_PyExecutorObject *)PyList_GET_ITEM(invalidate, i);
1548+
PyObject *exec = PyList_GET_ITEM(invalidate, i);
15431549
executor_clear(exec);
15441550
}
15451551
Py_DECREF(invalidate);

0 commit comments

Comments
 (0)