Skip to content

Commit 5edfe55

Browse files
authored
gh-137838: Fix JIT trace buffer overrun by increasing possible exit stubs (gh-138177)
1 parent 03ee060 commit 5edfe55

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ PyAPI_FUNC(void) _Py_Executors_InvalidateCold(PyInterpreterState *interp);
119119
#define JIT_CLEANUP_THRESHOLD 100000
120120

121121
// This is the length of the trace we project initially.
122-
#define UOP_MAX_TRACE_LENGTH 800
122+
#define UOP_MAX_TRACE_LENGTH 1200
123123

124124
#define TRACE_STACK_SIZE 5
125125

Lib/test/test_sys_settrace.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ class TraceTestCase(unittest.TestCase):
360360
# Disable gc collection when tracing, otherwise the
361361
# deallocators may be traced as well.
362362
def setUp(self):
363+
if os.environ.get('PYTHON_UOPS_OPTIMIZE') == '0':
364+
self.skipTest("Line tracing behavior differs when JIT optimizer is disabled")
363365
self.using_gc = gc.isenabled()
364366
gc.disable()
365367
self.addCleanup(sys.settrace, sys.gettrace())

Lib/test/test_trace.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ def test_traced_func_linear(self):
142142

143143
self.assertEqual(self.tracer.results().counts, expected)
144144

145+
@unittest.skipIf(os.environ.get('PYTHON_UOPS_OPTIMIZE') == '0',
146+
"Line counts differ when JIT optimizer is disabled")
145147
def test_traced_func_loop(self):
146148
self.tracer.runfunc(traced_func_loop, 2, 3)
147149

@@ -166,6 +168,8 @@ def test_traced_func_importing(self):
166168

167169
self.assertEqual(self.tracer.results().counts, expected)
168170

171+
@unittest.skipIf(os.environ.get('PYTHON_UOPS_OPTIMIZE') == '0',
172+
"Line counts differ when JIT optimizer is disabled")
169173
def test_trace_func_generator(self):
170174
self.tracer.runfunc(traced_func_calling_generator)
171175

@@ -236,6 +240,8 @@ def setUp(self):
236240
self.my_py_filename = fix_ext_py(__file__)
237241
self.addCleanup(sys.settrace, sys.gettrace())
238242

243+
@unittest.skipIf(os.environ.get('PYTHON_UOPS_OPTIMIZE') == '0',
244+
"Line counts differ when JIT optimizer is disabled")
239245
def test_exec_counts(self):
240246
self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
241247
code = r'''traced_func_loop(2, 5)'''
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix JIT trace buffer overrun by increasing possible exit stubs.
2+
Patch by Donghee Na.

Python/optimizer.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,8 @@ translate_bytecode_to_trace(
591591

592592
for (;;) {
593593
target = INSTR_IP(instr, code);
594-
// Need space for _DEOPT
595-
max_length--;
596-
594+
// One for possible _DEOPT, one because _CHECK_VALIDITY itself might _DEOPT
595+
max_length-=2;
597596
uint32_t opcode = instr->op.code;
598597
uint32_t oparg = instr->op.arg;
599598

@@ -1283,15 +1282,19 @@ uop_optimize(
12831282
_Py_BloomFilter_Init(&dependencies);
12841283
_PyUOpInstruction buffer[UOP_MAX_TRACE_LENGTH];
12851284
OPT_STAT_INC(attempts);
1285+
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
1286+
bool is_noopt = true;
1287+
if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
1288+
is_noopt = false;
1289+
}
12861290
int length = translate_bytecode_to_trace(frame, instr, buffer, UOP_MAX_TRACE_LENGTH, &dependencies, progress_needed);
12871291
if (length <= 0) {
12881292
// Error or nothing translated
12891293
return length;
12901294
}
12911295
assert(length < UOP_MAX_TRACE_LENGTH);
12921296
OPT_STAT_INC(traces_created);
1293-
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
1294-
if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
1297+
if (!is_noopt) {
12951298
length = _Py_uop_analyze_and_optimize(frame, buffer,
12961299
length,
12971300
curr_stackentries, &dependencies);

0 commit comments

Comments
 (0)