Skip to content

Commit b42e939

Browse files
Fix some bugs
1. We cannot exit after unpacking indices, as the stack contains tagged ints now which may lead to a crash. We must insert a guard for the type before unpacking indices. 2. It is possible for an indice to not fit in a tagged int, in which case we must deopt. 3. Recorded values do not always mean that that's the actual type we will see at runtime. So we must guard on that as well.
1 parent b47e3fc commit b42e939

12 files changed

Lines changed: 1304 additions & 1143 deletions

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ extern void _Py_uop_sym_set_recorded_type(JitOptContext *ctx, JitOptRef sym, PyT
303303
extern void _Py_uop_sym_set_recorded_gen_func(JitOptContext *ctx, JitOptRef ref, PyFunctionObject *value);
304304
extern PyCodeObject *_Py_uop_sym_get_probable_func_code(JitOptRef sym);
305305
extern PyObject *_Py_uop_sym_get_probable_value(JitOptRef sym);
306+
extern PyTypeObject * _Py_uop_sym_get_probable_type(JitOptRef ref);
306307

307308
extern void _Py_uop_abstractcontext_init(JitOptContext *ctx);
308309
extern void _Py_uop_abstractcontext_fini(JitOptContext *ctx);

Include/internal/pycore_stackref.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,17 @@ PyStackRef_IsTaggedInt(_PyStackRef i)
427427
return (i.bits & Py_TAG_BITS) == Py_INT_TAG;
428428
}
429429

430+
static inline bool
431+
PyStackRef_CanTagInt(intptr_t i)
432+
{
433+
return Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, (intptr_t)(((uintptr_t)i) << Py_TAGGED_SHIFT),
434+
Py_TAGGED_SHIFT) == i;
435+
}
436+
430437
static inline _PyStackRef
431438
PyStackRef_TagInt(intptr_t i)
432439
{
433-
assert(Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, (intptr_t)(((uintptr_t)i) << Py_TAGGED_SHIFT),
434-
Py_TAGGED_SHIFT) == i);
440+
assert(PyStackRef_CanTagInt(i));
435441
return (_PyStackRef){ .bits = ((((uintptr_t)i) << Py_TAGGED_SHIFT) | Py_INT_TAG) };
436442
}
437443

0 commit comments

Comments
 (0)