diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index b0e40a4ea06946..ab16c36201e6db 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -1727,6 +1727,8 @@ always available. Activate the stack profiler trampoline *backend*. The only supported backend is ``"perf"``. + Stack trampolines cannot be activated if the JIT is active. + .. availability:: Linux. .. versionadded:: 3.12 diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-01-17-31-32.gh-issue-124855.sdsv_H.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-01-17-31-32.gh-issue-124855.sdsv_H.rst new file mode 100644 index 00000000000000..b65a5e6ac11c76 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-01-17-31-32.gh-issue-124855.sdsv_H.rst @@ -0,0 +1,2 @@ +Don't allow the JIT and perf support to be active at the same time. Patch by +Pablo Galindo diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8aebbe5c405ffe..9be1a5dac2fc6e 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1300,14 +1300,21 @@ init_interp_main(PyThreadState *tstate) enabled = *env != '0'; } if (enabled) { - PyObject *opt = _PyOptimizer_NewUOpOptimizer(); - if (opt == NULL) { - return _PyStatus_ERR("can't initialize optimizer"); - } - if (_Py_SetTier2Optimizer((_PyOptimizerObject *)opt)) { - return _PyStatus_ERR("can't install optimizer"); + if (config->perf_profiling > 0) { + (void)PyErr_WarnEx( + PyExc_RuntimeWarning, + "JIT deactivated as perf profiling support is active", + 0); + } else { + PyObject *opt = _PyOptimizer_NewUOpOptimizer(); + if (opt == NULL) { + return _PyStatus_ERR("can't initialize optimizer"); + } + if (_Py_SetTier2Optimizer((_PyOptimizerObject *)opt)) { + return _PyStatus_ERR("can't install optimizer"); + } + Py_DECREF(opt); } - Py_DECREF(opt); } } #endif diff --git a/Python/sysmodule.c b/Python/sysmodule.c index ac343a8048e008..5bdfca549e08d1 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2287,6 +2287,14 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend) /*[clinic end generated code: output=5783cdeb51874b43 input=a12df928758a82b4]*/ { #ifdef PY_HAVE_PERF_TRAMPOLINE +#ifdef _Py_JIT + _PyOptimizerObject* optimizer = _Py_GetOptimizer(); + if (optimizer != NULL) { + PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampoline if the JIT is active"); + return NULL; + } +#endif + if (strcmp(backend, "perf") == 0) { _PyPerf_Callbacks cur_cb; _PyPerfTrampoline_GetCallbacks(&cur_cb);