Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b95e8b5

Browse files
committed
gh-112367: Only free perf trampoline arenas at shutdown
Signed-off-by: Pablo Galindo <[email protected]>
1 parent e9d1360 commit b95e8b5

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

Include/internal/pycore_ceval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
101101
extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
102102
extern int _PyPerfTrampoline_Init(int activate);
103103
extern int _PyPerfTrampoline_Fini(void);
104+
extern void _PyPerfTrampoline_FreeArenas(void);
104105
extern int _PyIsPerfTrampolineActive(void);
105106
extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);
106107
#ifdef PY_HAVE_PERF_TRAMPOLINE
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid undefined behaviour when using the perf trampolines by not freeing the
2+
code arenas until shutdown. Patch by Pablo Galindo

Python/perf_trampoline.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,24 @@ perf_map_write_entry(void *state, const void *code_addr,
216216
PyMem_RawFree(perf_map_entry);
217217
}
218218

219+
static void*
220+
perf_map_init_state(void)
221+
{
222+
PyUnstable_PerfMapState_Init();
223+
return NULL;
224+
}
225+
226+
static int
227+
perf_map_free_state(void *state)
228+
{
229+
PyUnstable_PerfMapState_Fini();
230+
return 0;
231+
}
232+
219233
_PyPerf_Callbacks _Py_perfmap_callbacks = {
220-
NULL,
234+
&perf_map_init_state,
221235
&perf_map_write_entry,
222-
NULL,
236+
&perf_map_free_state,
223237
};
224238

225239
static int
@@ -444,6 +458,9 @@ _PyPerfTrampoline_Init(int activate)
444458
if (extra_code_index == -1) {
445459
return -1;
446460
}
461+
if (trampoline_api.state == NULL && trampoline_api.init_state != NULL) {
462+
trampoline_api.state = trampoline_api.init_state();
463+
}
447464
perf_status = PERF_STATUS_OK;
448465
}
449466
#endif
@@ -458,12 +475,21 @@ _PyPerfTrampoline_Fini(void)
458475
if (tstate->interp->eval_frame == py_trampoline_evaluator) {
459476
tstate->interp->eval_frame = NULL;
460477
}
461-
free_code_arenas();
478+
if (perf_status == PERF_STATUS_OK) {
479+
trampoline_api.free_state(trampoline_api.state);
480+
}
462481
extra_code_index = -1;
463482
#endif
464483
return 0;
465484
}
466485

486+
void _PyPerfTrampoline_FreeArenas(void) {
487+
#ifdef PY_HAVE_PERF_TRAMPOLINE
488+
free_code_arenas();
489+
#endif
490+
return;
491+
}
492+
467493
int
468494
PyUnstable_PerfTrampoline_SetPersistAfterFork(int enable){
469495
#ifdef PY_HAVE_PERF_TRAMPOLINE
@@ -477,7 +503,7 @@ PyStatus
477503
_PyPerfTrampoline_AfterFork_Child(void)
478504
{
479505
#ifdef PY_HAVE_PERF_TRAMPOLINE
480-
PyUnstable_PerfMapState_Fini();
506+
trampoline_api.free_state(trampoline_api.state);
481507
if (persist_after_fork) {
482508
char filename[256];
483509
pid_t parent_pid = getppid();
@@ -488,8 +514,8 @@ _PyPerfTrampoline_AfterFork_Child(void)
488514
} else {
489515
// Restart trampoline in file in child.
490516
int was_active = _PyIsPerfTrampolineActive();
491-
_PyPerfTrampoline_Fini();
492517
if (was_active) {
518+
_PyPerfTrampoline_Fini();
493519
_PyPerfTrampoline_Init(1);
494520
}
495521
}

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,7 @@ finalize_interp_clear(PyThreadState *tstate)
17971797
_PyArg_Fini();
17981798
_Py_ClearFileSystemEncoding();
17991799
_PyPerfTrampoline_Fini();
1800+
_PyPerfTrampoline_FreeArenas();
18001801
}
18011802

18021803
finalize_interp_types(tstate->interp);
@@ -1854,7 +1855,6 @@ Py_FinalizeEx(void)
18541855
*/
18551856

18561857
_PyAtExit_Call(tstate->interp);
1857-
PyUnstable_PerfMapState_Fini();
18581858

18591859
/* Copy the core config, PyInterpreterState_Delete() free
18601860
the core config memory */

0 commit comments

Comments
 (0)