From ac3115dd64d438fd1226340d654458fe3ee44d55 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 19 Apr 2026 17:11:57 +0000 Subject: [PATCH] Detect availability of PR_SET_VMA at runtime (fixes build with musl libc) The build would fail on musl-based systems with the following error: gcc -c -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O3 -Wall -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I./Include/internal/mimalloc -I. -I./Include -DPy_BUILD_CORE -o Objects/obmalloc.o Objects/obmalloc.c In file included from ./Include/internal/pycore_mmap.h:16, from Objects/obmalloc.c:5: /usr/include/sys/prctl.h:88:8: error: redefinition of 'struct prctl_mm_map' 88 | struct prctl_mm_map { | ^~~~~~~~~~~~ In file included from ./Include/internal/pycore_mmap.h:15: /usr/include/linux/prctl.h:134:8: note: originally defined here 134 | struct prctl_mm_map { | ^~~~~~~~~~~~ make: *** [Makefile:3388: Objects/obmalloc.o] Error 1 This is due to Python including both and (which on glibc imports the definitions from and on musl includes all the definitions itself). Given that Python is not the Linux kernel, should be included. Another issue of the previous implementation and the configure check was that the `_PyAnnotateMemoryMap()` function would only be enabled if the linux-headers on the build host are recent enough to contain the `PR_SET_VMA_ANON_NAME` macro. However the resulting binaries might be run on older kernels lacking the functionality or be built on older systems but run on newer kernels. With this patch, the function is always enabled and the magic numbers are included with Python, so that the `prctl()` is always executed and feature detection is essentially done at runtime. When run on a kernel too old to support `PR_SET_VMA_ANON_NAME`, `mmap.mmap.set_name()` will silently do nothing. When the kernel is new enough but `CONFIG_ANON_VMA_NAME` is not enabled, `mmap.mmap.set_name()` will raise an `OSError`, e.g.: >>> mm.set_name("hello") Traceback (most recent call last): File "", line 1, in mm.set_name("hello") ~~~~~~~~~~~^^^^^^^^^ OSError: [Errno 22] Invalid argument --- Include/internal/pycore_mmap.h | 16 ++++++++++---- Tools/build/check_capi_macros_ignored.txt | 1 - configure | 26 ----------------------- configure.ac | 11 ---------- pyconfig.h.in | 7 ------ 5 files changed, 12 insertions(+), 49 deletions(-) diff --git a/Include/internal/pycore_mmap.h b/Include/internal/pycore_mmap.h index c117cbd16283da..63033e30c5cbee 100644 --- a/Include/internal/pycore_mmap.h +++ b/Include/internal/pycore_mmap.h @@ -11,12 +11,17 @@ extern "C" { #include "pycore_pystate.h" -#if defined(_Py_HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__) -# include -# include +#if defined(__linux__) + +#include + +#ifndef PR_SET_VMA +# define PR_SET_VMA 0x53564d41 +#endif +#ifndef PR_SET_VMA_ANON_NAME +# define PR_SET_VMA_ANON_NAME 0 #endif -#if defined(_Py_HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__) static inline int _PyAnnotateMemoryMap(void *addr, size_t size, const char *name) { @@ -33,12 +38,15 @@ _PyAnnotateMemoryMap(void *addr, size_t size, const char *name) } return 0; } + #else + static inline int _PyAnnotateMemoryMap(void *Py_UNUSED(addr), size_t Py_UNUSED(size), const char *Py_UNUSED(name)) { return 0; } + #endif #ifdef __cplusplus diff --git a/Tools/build/check_capi_macros_ignored.txt b/Tools/build/check_capi_macros_ignored.txt index 154f1dbfca039c..b6cde027a77586 100644 --- a/Tools/build/check_capi_macros_ignored.txt +++ b/Tools/build/check_capi_macros_ignored.txt @@ -135,7 +135,6 @@ HAVE_CURSES_USE_ENV HAVE_CURSES_WCHGAT HAVE_DB_H HAVE_DECLSPEC_DLL -HAVE_DECL_PR_SET_VMA_ANON_NAME HAVE_DECL_RTLD_DEEPBIND HAVE_DECL_RTLD_GLOBAL HAVE_DECL_RTLD_LAZY diff --git a/configure b/configure index da44380d27a1ab..3bfd1fe903cdf7 100755 --- a/configure +++ b/configure @@ -26104,32 +26104,6 @@ printf "%s\n" "#define HAVE_UT_NAMESIZE 1" >>confdefs.h fi -# musl libc redefines struct prctl_mm_map and conflicts with linux/prctl.h. -# The conftest does not reliably catch this, so skip the check on musl. -case $host in #( - *-musl*) : - ;; #( - *) : - ac_fn_check_decl "$LINENO" "PR_SET_VMA_ANON_NAME" "ac_cv_have_decl_PR_SET_VMA_ANON_NAME" "#include - #include -" "$ac_c_undeclared_builtin_options" "CFLAGS" -if test "x$ac_cv_have_decl_PR_SET_VMA_ANON_NAME" = xyes -then : - ac_have_decl=1 -else case e in #( - e) ac_have_decl=0 ;; -esac -fi -printf "%s\n" "#define HAVE_DECL_PR_SET_VMA_ANON_NAME $ac_have_decl" >>confdefs.h -if test $ac_have_decl = 1 -then : - -printf "%s\n" "#define _Py_HAVE_PR_SET_VMA_ANON_NAME 1" >>confdefs.h - -fi - - ;; -esac # check for openpty, login_tty, and forkpty # tvOS/watchOS have functions for tty, but can't use them if test "$ac_sys_system" != "tvOS" -a "$ac_sys_system" != "watchOS" ; then diff --git a/configure.ac b/configure.ac index c73f8f2fd28a1c..e399abdf29df62 100644 --- a/configure.ac +++ b/configure.ac @@ -6232,17 +6232,6 @@ AC_CHECK_DECLS([UT_NAMESIZE], [Define if you have the 'HAVE_UT_NAMESIZE' constant.])], [], [@%:@include ]) -# musl libc redefines struct prctl_mm_map and conflicts with linux/prctl.h. -# The conftest does not reliably catch this, so skip the check on musl. -AS_CASE([$host], - [*-musl*], [], - [AC_CHECK_DECLS([PR_SET_VMA_ANON_NAME], - [AC_DEFINE([_Py_HAVE_PR_SET_VMA_ANON_NAME], [1], - [Define if you have the 'PR_SET_VMA_ANON_NAME' constant.])], - [], - [@%:@include - @%:@include ])] -) # check for openpty, login_tty, and forkpty # tvOS/watchOS have functions for tty, but can't use them if test "$ac_sys_system" != "tvOS" -a "$ac_sys_system" != "watchOS" ; then diff --git a/pyconfig.h.in b/pyconfig.h.in index 1d4f445a8b1fda..ab1281eea94cf3 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -239,10 +239,6 @@ /* Define to 1 if you have the header file. */ #undef HAVE_DB_H -/* Define to 1 if you have the declaration of 'PR_SET_VMA_ANON_NAME', and to 0 - if you don't. */ -#undef HAVE_DECL_PR_SET_VMA_ANON_NAME - /* Define to 1 if you have the declaration of 'RTLD_DEEPBIND', and to 0 if you don't. */ #undef HAVE_DECL_RTLD_DEEPBIND @@ -2155,9 +2151,6 @@ /* Define if you have the 'pidfd_send_signal' function. */ #undef _Py_HAVE_PIDFD_SEND_SIGNAL -/* Define if you have the 'PR_SET_VMA_ANON_NAME' constant. */ -#undef _Py_HAVE_PR_SET_VMA_ANON_NAME - /* Define if you have the 'pthread_attr_get_np' function. */ #undef _Py_HAVE_PTHREAD_ATTR_GET_NP