[3.14] gh-140104: Set next_instr properly in the JIT during exceptions (GH-140233) (GH-141495)

This commit is contained in:
Mikhail Efimov 2025-11-13 21:47:25 +03:00 committed by GitHub
parent c5e0ce73f9
commit af20b880fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 2 deletions

View file

@ -1992,6 +1992,27 @@ class TestUopsOptimization(unittest.TestCase):
next(g) next(g)
""" % _testinternalcapi.SPECIALIZATION_THRESHOLD)) """ % _testinternalcapi.SPECIALIZATION_THRESHOLD))
def test_next_instr_for_exception_handler_set(self):
# gh-140104: We just want the exception to be caught properly.
def f():
for i in range(TIER2_THRESHOLD + 3):
try:
undefined_variable(i)
except Exception:
pass
f()
def test_next_instr_for_exception_handler_set_lasts_instr(self):
# gh-140104: We just want the exception to be caught properly.
def f():
a_list = []
for _ in range(TIER2_THRESHOLD + 3):
try:
a_list[""] = 0
except Exception:
pass
def global_identity(x): def global_identity(x):
return x return x

View file

@ -0,0 +1,2 @@
Fix a bug with exception handling in the JIT. Patch by Ken Jin. Bug reported
by Daniel Diniz.

View file

@ -376,7 +376,9 @@ do { \
frame = tstate->current_frame; \ frame = tstate->current_frame; \
stack_pointer = _PyFrame_GetStackPointer(frame); \ stack_pointer = _PyFrame_GetStackPointer(frame); \
if (next_instr == NULL) { \ if (next_instr == NULL) { \
next_instr = frame->instr_ptr; \ /* gh-140104: The exception handler expects frame->instr_ptr
to after this_instr, not this_instr! */ \
next_instr = frame->instr_ptr + 1; \
JUMP_TO_LABEL(error); \ JUMP_TO_LABEL(error); \
} \ } \
DISPATCH(); \ DISPATCH(); \
@ -404,7 +406,9 @@ do { \
stack_pointer = _PyFrame_GetStackPointer(frame); \ stack_pointer = _PyFrame_GetStackPointer(frame); \
if (next_instr == NULL) \ if (next_instr == NULL) \
{ \ { \
next_instr = frame->instr_ptr; \ /* gh-140104: The exception handler expects frame->instr_ptr
to after this_instr, not this_instr! */ \
next_instr = frame->instr_ptr + 1; \
goto error; \ goto error; \
} \ } \
DISPATCH(); \ DISPATCH(); \