https://github.com/python/cpython/commit/2d37c719ed7c54430257028be6b48a1e2d065973
commit: 2d37c719ed7c54430257028be6b48a1e2d065973
branch: main
author: Pablo Galindo Salgado <pablog...@gmail.com>
committer: pablogsal <pablog...@gmail.com>
date: 2024-10-30T00:12:45Z
summary:

gh-124855: Don't allow the JIT and perf support to be active at the same time 
(#124856)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-10-01-17-31-32.gh-issue-124855.sdsv_H.rst
M Doc/library/sys.rst
M Python/pylifecycle.c
M Python/sysmodule.c

diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index 37f1719db607de..d83816ec1502ca 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -1757,6 +1757,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 8f38fbedae9842..2efaa9db7d7d58 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -1310,14 +1310,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 24af4798eeac3b..cbb73977e1aae6 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);

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to