Skip to content

Commit dbc5d9a

Browse files
committed
Revert "gh-137838: Fix JIT trace buffer overrun by pre-reserving exit stub space"
This reverts commit 0776a59.
1 parent eff78b4 commit dbc5d9a

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import textwrap
1414
import subprocess
1515
import warnings
16-
1716
try:
1817
import _testinternalcapi
1918
except ImportError:
@@ -361,8 +360,6 @@ class TraceTestCase(unittest.TestCase):
361360
# Disable gc collection when tracing, otherwise the
362361
# deallocators may be traced as well.
363362
def setUp(self):
364-
if os.environ.get('PYTHON_UOPS_OPTIMIZE') == '0':
365-
self.skipTest("Line tracing behavior differs when JIT optimizer is disabled")
366363
self.using_gc = gc.isenabled()
367364
gc.disable()
368365
self.addCleanup(sys.settrace, sys.gettrace())

Lib/test/test_trace.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ 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")
147145
def test_traced_func_loop(self):
148146
self.tracer.runfunc(traced_func_loop, 2, 3)
149147

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

169167
self.assertEqual(self.tracer.results().counts, expected)
170168

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

@@ -240,8 +236,6 @@ def setUp(self):
240236
self.my_py_filename = fix_ext_py(__file__)
241237
self.addCleanup(sys.settrace, sys.gettrace())
242238

243-
@unittest.skipIf(os.environ.get('PYTHON_UOPS_OPTIMIZE') == '0',
244-
"Line counts differ when JIT optimizer is disabled")
245239
def test_exec_counts(self):
246240
self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
247241
code = r'''traced_func_loop(2, 5)'''

Python/optimizer.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,6 @@ add_to_trace(
540540
assert(func == NULL || func->func_code == (PyObject *)code); \
541541
instr = trace_stack[trace_stack_depth].instr;
542542

543-
544543
/* Returns the length of the trace on success,
545544
* 0 if it failed to produce a worthwhile trace,
546545
* and -1 on an error.
@@ -561,10 +560,8 @@ translate_bytecode_to_trace(
561560
_Py_BloomFilter_Add(dependencies, initial_code);
562561
_Py_CODEUNIT *initial_instr = instr;
563562
int trace_length = 0;
564-
// Leave space for possible trailing _EXIT_TRACE and estimated exit stubs
565-
// Reserve 20% of buffer space for exit stubs (empirically sufficient)
566-
int max_exit_stubs = (buffer_size * 20) / 100; // 20% for exit stubs
567-
int max_length = buffer_size - 2 - max_exit_stubs;
563+
// Leave space for possible trailing _EXIT_TRACE
564+
int max_length = buffer_size-2;
568565
struct {
569566
PyFunctionObject *func;
570567
PyCodeObject *code;
@@ -650,7 +647,16 @@ translate_bytecode_to_trace(
650647
assert(!OPCODE_HAS_DEOPT(opcode));
651648
}
652649

653-
// Note: Exit stub space is pre-reserved in max_length calculation above
650+
if (OPCODE_HAS_EXIT(opcode)) {
651+
// Make space for side exit and final _EXIT_TRACE:
652+
RESERVE_RAW(2, "_EXIT_TRACE");
653+
max_length--;
654+
}
655+
if (OPCODE_HAS_ERROR(opcode)) {
656+
// Make space for error stub and final _EXIT_TRACE:
657+
RESERVE_RAW(2, "_ERROR_POP_N");
658+
max_length--;
659+
}
654660
switch (opcode) {
655661
case POP_JUMP_IF_NONE:
656662
case POP_JUMP_IF_NOT_NONE:
@@ -725,11 +731,9 @@ translate_bytecode_to_trace(
725731
{
726732
const struct opcode_macro_expansion *expansion = &_PyOpcode_macro_expansion[opcode];
727733
if (expansion->nuops > 0) {
728-
// Reserve space for nuops
734+
// Reserve space for nuops (+ _SET_IP + _EXIT_TRACE)
729735
int nuops = expansion->nuops;
730-
731-
// Reserve space for nuops (exit stub space already pre-reserved)
732-
RESERVE(nuops);
736+
RESERVE(nuops + 1); /* One extra for exit */
733737
int16_t last_op = expansion->uops[nuops-1].uop;
734738
if (last_op == _RETURN_VALUE || last_op == _RETURN_GENERATOR || last_op == _YIELD_VALUE) {
735739
// Check for trace stack underflow now:

0 commit comments

Comments
 (0)