Skip to content

Commit 4c9f267

Browse files
authored
gh-157247: Block perf trampoline activation when the main interpreter's JIT is enabled (#157258)
1 parent fff3c4f commit 4c9f267

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

Lib/test/test_perf_profiler.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,27 @@ def test_sys_api_with_existing_perf_jit_trampoline(self):
318318
"""
319319
assert_python_ok("-c", code, PYTHON_JIT="0")
320320

321+
@unittest.skipUnless(
322+
"-D_Py_JIT" in (sysconfig.get_config_var("PY_CORE_CFLAGS") or "").split(),
323+
"requires a real JIT (_Py_JIT)",
324+
)
325+
def test_sys_api_perf_jit_backend_in_subinterpreter(self):
326+
# gh-157247: a subinterpreter must not bypass the JIT/perf exclusion.
327+
code = """if 1:
328+
import sys
329+
from contextlib import closing
330+
from concurrent import interpreters
331+
332+
assert sys._jit.is_enabled(), "expected the JIT to be enabled"
333+
334+
with closing(interpreters.create()) as interp:
335+
interp.exec(
336+
"import sys; sys.activate_stack_trampoline('perf_jit')")
337+
"""
338+
rc, out, err = assert_python_failure("-c", code, PYTHON_JIT="1")
339+
self.assertIn(
340+
b"Cannot activate the perf trampoline if the JIT is active", err)
341+
321342

322343
def is_unwinding_reliable_with_frame_pointers():
323344
cflags = sysconfig.get_config_var("PY_CORE_CFLAGS")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`sys.activate_stack_trampoline` allowing activation from a
2+
subinterpreter while the JIT is enabled in the main interpreter.

Python/sysmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2348,7 +2348,9 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend)
23482348
{
23492349
#ifdef PY_HAVE_PERF_TRAMPOLINE
23502350
#ifdef _Py_JIT
2351-
if (_PyInterpreterState_GET()->jit) {
2351+
// Perf state is process-wide, and only the main interpreter can enable
2352+
// the JIT. Check it even when called from a subinterpreter (gh-157247).
2353+
if (_PyInterpreterState_Main()->jit) {
23522354
PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampoline if the JIT is active");
23532355
return NULL;
23542356
}

0 commit comments

Comments
 (0)