Skip to content

Commit af20b88

Browse files
[3.14] gh-140104: Set next_instr properly in the JIT during exceptions (GH-140233) (GH-141495)
1 parent c5e0ce7 commit af20b88

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,27 @@ def gen():
19921992
next(g)
19931993
""" % _testinternalcapi.SPECIALIZATION_THRESHOLD))
19941994

1995+
def test_next_instr_for_exception_handler_set(self):
1996+
# gh-140104: We just want the exception to be caught properly.
1997+
def f():
1998+
for i in range(TIER2_THRESHOLD + 3):
1999+
try:
2000+
undefined_variable(i)
2001+
except Exception:
2002+
pass
2003+
2004+
f()
2005+
2006+
def test_next_instr_for_exception_handler_set_lasts_instr(self):
2007+
# gh-140104: We just want the exception to be caught properly.
2008+
def f():
2009+
a_list = []
2010+
for _ in range(TIER2_THRESHOLD + 3):
2011+
try:
2012+
a_list[""] = 0
2013+
except Exception:
2014+
pass
2015+
19952016

19962017
def global_identity(x):
19972018
return x
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug with exception handling in the JIT. Patch by Ken Jin. Bug reported
2+
by Daniel Diniz.

Python/ceval_macros.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,9 @@ do { \
376376
frame = tstate->current_frame; \
377377
stack_pointer = _PyFrame_GetStackPointer(frame); \
378378
if (next_instr == NULL) { \
379-
next_instr = frame->instr_ptr; \
379+
/* gh-140104: The exception handler expects frame->instr_ptr
380+
to after this_instr, not this_instr! */ \
381+
next_instr = frame->instr_ptr + 1; \
380382
JUMP_TO_LABEL(error); \
381383
} \
382384
DISPATCH(); \
@@ -404,7 +406,9 @@ do { \
404406
stack_pointer = _PyFrame_GetStackPointer(frame); \
405407
if (next_instr == NULL) \
406408
{ \
407-
next_instr = frame->instr_ptr; \
409+
/* gh-140104: The exception handler expects frame->instr_ptr
410+
to after this_instr, not this_instr! */ \
411+
next_instr = frame->instr_ptr + 1; \
408412
goto error; \
409413
} \
410414
DISPATCH(); \

0 commit comments

Comments
 (0)