Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_perf_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,27 @@ def test_sys_api_with_existing_perf_jit_trampoline(self):
"""
assert_python_ok("-c", code, PYTHON_JIT="0")

@unittest.skipUnless(
"-D_Py_JIT" in (sysconfig.get_config_var("PY_CORE_CFLAGS") or "").split(),
"requires a real JIT (_Py_JIT)",
)
def test_sys_api_perf_jit_backend_in_subinterpreter(self):
# gh-157247: a subinterpreter must not bypass the JIT/perf exclusion.
code = """if 1:
import sys
from contextlib import closing
from concurrent import interpreters

assert sys._jit.is_enabled(), "expected the JIT to be enabled"

with closing(interpreters.create()) as interp:
interp.exec(
"import sys; sys.activate_stack_trampoline('perf_jit')")
"""
rc, out, err = assert_python_failure("-c", code, PYTHON_JIT="1")
self.assertIn(
b"Cannot activate the perf trampoline if the JIT is active", err)


def is_unwinding_reliable_with_frame_pointers():
cflags = sysconfig.get_config_var("PY_CORE_CFLAGS")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`sys.activate_stack_trampoline` allowing activation from a
subinterpreter while the JIT is enabled in the main interpreter.
4 changes: 3 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,9 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend)
{
#ifdef PY_HAVE_PERF_TRAMPOLINE
#ifdef _Py_JIT
if (_PyInterpreterState_GET()->jit) {
// Perf state is process-wide, and only the main interpreter can enable
// the JIT. Check it even when called from a subinterpreter (gh-157247).
if (_PyInterpreterState_Main()->jit) {
PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampoline if the JIT is active");
return NULL;
}
Expand Down
Loading