Repository navigation
Description
Crash report
PyUnstable_WritePerfMapEntry() is documented as callable from any thread, without a thread state. Py_FinalizeEx() tears the perf map state down (_PyPerfTrampoline_Fini() → PyUnstable_PerfMapState_Fini()) without waiting for threads that are inside PyUnstable_WritePerfMapEntry() or about to take the lock: it closes the file, releases the lock and then frees the lock. A worker thread that is parked on the lock, or that has just read the lock pointer, keeps using it after the free.
On 3.15+ that is a heap-use-after-free on the 1-byte lock. On 3.12 to 3.14 the worker gets past the lock and calls fprintf() with the FILE * that PyUnstable_PerfMapState_Fini() has already closed and reset to NULL: a NULL dereference.
Reproducer
C program, to be linked against an ASan build of Python:
/* Py_FinalizeEx() tears the perf map state down while native threads are
* still inside PyUnstable_WritePerfMapEntry().
*
* Build against an ASan build of Python:
* cc -fsanitize=address -I<build> -I<src>/Include -I<src>/Include/internal \
* perf_map_finalize_race.c -L<build> -lpython3.15 -lpthread -ldl -lm
* (3.12-3.14 declare code_size as unsigned int: add
* -Wno-incompatible-function-pointer-types)
*/
#include <Python.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
static atomic_int stop = 0;
static atomic_int finalized = 0;
static void *
worker(void *arg)
{
char name[32];
snprintf(name, sizeof(name), "worker-%d", (int)(intptr_t)arg);
while (!atomic_load(&stop)) {
/* Documented as callable from any thread, without a thread state. */
PyUnstable_WritePerfMapEntry(
(const void *)(uintptr_t)(0x100000 + (intptr_t)arg * 0x10), 16, name);
if (atomic_load(&finalized)) {
usleep(1000); /* slow down, so that a sanitizer can finish its report */
}
}
return NULL;
}
int
main(void)
{
PyConfig config;
PyConfig_InitPythonConfig(&config);
config.perf_profiling = 1; /* same as -X perf */
PyStatus status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
return 2;
}
PyRun_SimpleString("x = 1\n");
for (intptr_t i = 1; i <= 16; i++) {
pthread_t tid;
pthread_create(&tid, NULL, worker, (void *)i);
pthread_detach(tid);
}
usleep(50000); /* let the workers get going */
Py_FinalizeEx(); /* races with the workers */
atomic_store(&finalized, 1);
usleep(60000000); /* keep the process alive (a sanitizer
may need a while to print its report) */
atomic_store(&stop, 1);
usleep(100000);
return 0;
}The program is killed by the sanitizer as soon as the report is printed, so the 60-second wait only matters in the runs that do not hit the race.
Output
main (4b66b87), ASan build, 3 of 3 runs:
ERROR: AddressSanitizer: heap-use-after-free
READ of size 1 at 0x502000001090 thread T1
#0 _Py_atomic_load_uint8_relaxed Include/cpython/pyatomic_gcc.h:359:10
#1 _PyMutex_LockTimed Python/lock.c:156:13
#2 PyUnstable_WritePerfMapEntry Python/sysmodule.c:2705:5
...
0x502000001090 is located 0 bytes inside of 1-byte region [0x502000001090,0x502000001091)
freed by thread T0 here:
#0 free
#1 PyUnstable_PerfMapState_Fini Python/sysmodule.c:2722:9
#2 perf_map_free_state Python/perf_trampoline.c:293:5
#3 _PyPerfTrampoline_Fini Python/perf_trampoline.c:589:9
#4 finalize_interp_clear Python/pylifecycle.c:2146:9
#5 _Py_Finalize Python/pylifecycle.c:2584:5
#6 main perf_map_finalize_race.c:56:5
previously allocated by thread T0 here:
#0 malloc
#1 PyThread_allocate_lock Python/thread.c:86:32
#2 PyUnstable_PerfMapState_Init Python/sysmodule.c:2684:31
...
On 3.15.2 the access is at Python/sysmodule.c:2726 and the free at line 2743 (3 of 3 runs there and on 3.16.0a2 as well).
3.14.10, same program, 3 of 3 runs (3.12.16 and 3.13.18 behave the same):
ERROR: AddressSanitizer: SEGV on unknown address 0x0000000000c0 ... T9
The signal is caused by a READ memory access.
#0 __vfprintf_internal stdio-common/vfprintf-internal.c:1525:3
#1 fprintf
#2 PyUnstable_WritePerfMapEntry Python/sysmodule.c:2755:5
#3 worker perf_map_finalize_race.c:28:9
Under gdb the crashing call is fprintf(s = 0x0, "%lx %x %s\n", ...) from PyUnstable_WritePerfMapEntry(), and at that moment the main thread is in the usleep() after Py_FinalizeEx() has returned, i.e. the FILE * was closed and nulled by finalization while this worker was still in the function.
CPython versions tested on:
3.12, 3.13, 3.14, 3.15, 3.16, CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.15.2 (main, Nov 25 2026, 20:06:37) [Clang 18.1.3 (1ubuntu1)]
Activity