]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-112367: Only free perf trampoline arenas at shutdown (GH-112368) (#112590)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Fri, 1 Dec 2023 16:05:31 +0000 (16:05 +0000)
committerGitHub <noreply@github.com>
Fri, 1 Dec 2023 16:05:31 +0000 (16:05 +0000)
(cherry picked from commit a73aa48e6bec900be7edd3431deaa5fc1d809e6f)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Include/internal/pycore_ceval.h
Misc/NEWS.d/next/Core and Builtins/2023-11-24-14-10-57.gh-issue-112367.9z1IDp.rst [new file with mode: 0644]
Python/perf_trampoline.c
Python/pylifecycle.c

index fc0f72efdae48efe44291da6f3f29ccd5e3a1f27..921b1cfcd3ac4dc4af485fc7b856f9721c8ff760 100644 (file)
@@ -74,6 +74,7 @@ extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
 extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
 extern int _PyPerfTrampoline_Init(int activate);
 extern int _PyPerfTrampoline_Fini(void);
+extern void _PyPerfTrampoline_FreeArenas(void);
 extern int _PyIsPerfTrampolineActive(void);
 extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);
 #ifdef PY_HAVE_PERF_TRAMPOLINE
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-24-14-10-57.gh-issue-112367.9z1IDp.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-24-14-10-57.gh-issue-112367.9z1IDp.rst
new file mode 100644 (file)
index 0000000..991e45a
--- /dev/null
@@ -0,0 +1,2 @@
+Avoid undefined behaviour when using the perf trampolines by not freeing the
+code arenas until shutdown. Patch by Pablo Galindo
index 6a6f223d095d3b1488386ac7d001eaff917c83de..a91f4f82877b3a5adaa9dbb02131d5a452efbd5b 100644 (file)
@@ -216,10 +216,24 @@ perf_map_write_entry(void *state, const void *code_addr,
     PyMem_RawFree(perf_map_entry);
 }
 
+static void*
+perf_map_init_state(void)
+{
+    PyUnstable_PerfMapState_Init();
+    return NULL;
+}
+
+static int
+perf_map_free_state(void *state)
+{
+    PyUnstable_PerfMapState_Fini();
+    return 0;
+}
+
 _PyPerf_Callbacks _Py_perfmap_callbacks = {
-    NULL,
+    &perf_map_init_state,
     &perf_map_write_entry,
-    NULL,
+    &perf_map_free_state,
 };
 
 static int
@@ -398,11 +412,17 @@ _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *callbacks)
     trampoline_api.write_state = callbacks->write_state;
     trampoline_api.free_state = callbacks->free_state;
     trampoline_api.state = NULL;
-    perf_status = PERF_STATUS_OK;
 #endif
     return 0;
 }
 
+void _PyPerfTrampoline_FreeArenas(void) {
+#ifdef PY_HAVE_PERF_TRAMPOLINE
+    free_code_arenas();
+#endif
+    return;
+}
+
 int
 _PyPerfTrampoline_Init(int activate)
 {
@@ -417,6 +437,7 @@ _PyPerfTrampoline_Init(int activate)
     }
     if (!activate) {
         tstate->interp->eval_frame = NULL;
+        perf_status = PERF_STATUS_NO_INIT;
     }
     else {
         tstate->interp->eval_frame = py_trampoline_evaluator;
@@ -427,6 +448,9 @@ _PyPerfTrampoline_Init(int activate)
         if (extra_code_index == -1) {
             return -1;
         }
+        if (trampoline_api.state == NULL && trampoline_api.init_state != NULL) {
+            trampoline_api.state = trampoline_api.init_state();
+        }
         perf_status = PERF_STATUS_OK;
     }
 #endif
@@ -437,12 +461,18 @@ int
 _PyPerfTrampoline_Fini(void)
 {
 #ifdef PY_HAVE_PERF_TRAMPOLINE
+    if (perf_status != PERF_STATUS_OK) {
+        return 0;
+    }
     PyThreadState *tstate = _PyThreadState_GET();
     if (tstate->interp->eval_frame == py_trampoline_evaluator) {
         tstate->interp->eval_frame = NULL;
     }
-    free_code_arenas();
+    if (perf_status == PERF_STATUS_OK) {
+        trampoline_api.free_state(trampoline_api.state);
+    }
     extra_code_index = -1;
+    perf_status = PERF_STATUS_NO_INIT;
 #endif
     return 0;
 }
index ee0eb79e0151ee5aa97ff43dcbf4a0e417a40955..a0130fde15d574e7bb339df679b05751ef6f0903 100644 (file)
@@ -1755,6 +1755,7 @@ finalize_interp_clear(PyThreadState *tstate)
         _Py_ClearFileSystemEncoding();
         _Py_Deepfreeze_Fini();
         _PyPerfTrampoline_Fini();
+        _PyPerfTrampoline_FreeArenas();
     }
 
     finalize_interp_types(tstate->interp);
@@ -1812,7 +1813,6 @@ Py_FinalizeEx(void)
      */
 
     _PyAtExit_Call(tstate->interp);
-    PyUnstable_PerfMapState_Fini();
 
     /* Copy the core config, PyInterpreterState_Delete() free
        the core config memory */