Skip to content

Commit fd01372

Browse files
authored
gh-145064: Fix JIT assertion failure during CALL_ALLOC_AND_ENTER_INIT side exit (GH-145100)
1 parent 819ea3c commit fd01372

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4065,6 +4065,55 @@ def hot_loop():
40654065
self.assertNotIn('_PyJit_TryInitializeTracing', stderr,
40664066
f"JIT tracer memory leak detected:\n{stderr}")
40674067

4068+
def test_cold_exit_on_init_cleanup_frame(self):
4069+
4070+
result = script_helper.run_python_until_end('-c', textwrap.dedent("""
4071+
class A:
4072+
__slots__ = ('x', 'y', 'z', 'w')
4073+
def __init__(self):
4074+
self.x = self.y = -1
4075+
self.z = self.w = None
4076+
4077+
class B(A):
4078+
__slots__ = ('a', 'b', 'c', 'd', 'e')
4079+
def __init__(self):
4080+
super().__init__()
4081+
self.a = self.b = None
4082+
self.c = ""
4083+
self.d = self.e = False
4084+
4085+
class C(B):
4086+
__slots__ = ('name', 'flag')
4087+
def __init__(self, name):
4088+
super().__init__()
4089+
self.name = name
4090+
self.flag = False
4091+
4092+
funcs = []
4093+
for n in range(20, 80):
4094+
lines = [f"def f{n}(names, info):"]
4095+
for j in range(n):
4096+
lines.append(f" v{j} = names[{j % 3}]")
4097+
if j % 3 == 0:
4098+
lines.append(f" if v{j} in info:")
4099+
lines.append(f" v{j} = info[v{j}]")
4100+
elif j % 5 == 0:
4101+
lines.append(f" v{j} = len(v{j}) if isinstance(v{j}, str) else 0")
4102+
lines.append(" return C(names[0])")
4103+
ns = {'C': C}
4104+
exec("\\n".join(lines), ns)
4105+
funcs.append(ns[f"f{n}"])
4106+
4107+
names = ['alpha', 'beta', 'gamma']
4108+
info = {'alpha': 'x', 'beta': 'y', 'gamma': 'z'}
4109+
4110+
for f in funcs:
4111+
for _ in range(10):
4112+
f(names, info)
4113+
"""), PYTHON_JIT="1", PYTHON_JIT_STRESS="1",
4114+
PYTHON_JIT_SIDE_EXIT_INITIAL_VALUE="1")
4115+
self.assertEqual(result[0].rc, 0, result)
4116+
40684117
def global_identity(x):
40694118
return x
40704119

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix JIT optimizer assertion failure during ``CALL_ALLOC_AND_ENTER_INIT`` side exit.

Python/optimizer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ _PyJit_TryInitializeTracing(
995995
return 0;
996996
}
997997
PyObject *func = PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
998-
if (func == NULL) {
998+
if (func == NULL || !PyFunction_Check(func)) {
999999
return 0;
10001000
}
10011001
PyCodeObject *code = _PyFrame_GetCode(frame);

0 commit comments

Comments
 (0)