Skip to content

Commit 8ddb452

Browse files
committed
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 <linux/prctl.h> and <sys/prctl.h> (which on glibc imports the definitions from <linux/prctl.h> and on musl includes all the definitions itself). Given that Python is not the Linux kernel, <sys/prctl.h> 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 "<python-input-5>", line 1, in <module> mm.set_name("hello") ~~~~~~~~~~~^^^^^^^^^ OSError: [Errno 22] Invalid argument
1 parent ad7d361 commit 8ddb452

File tree

4 files changed

+12
-44
lines changed

4 files changed

+12
-44
lines changed

Include/internal/pycore_mmap.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ extern "C" {
1111

1212
#include "pycore_pystate.h"
1313

14-
#if defined(HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__)
15-
# include <linux/prctl.h>
16-
# include <sys/prctl.h>
14+
#if defined(__linux__)
15+
16+
#include <sys/prctl.h>
17+
18+
#ifndef PR_SET_VMA
19+
# define PR_SET_VMA 0x53564d41
20+
#endif
21+
#ifndef PR_SET_VMA_ANON_NAME
22+
# define PR_SET_VMA_ANON_NAME 0
1723
#endif
1824

19-
#if defined(HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__)
2025
static inline int
2126
_PyAnnotateMemoryMap(void *addr, size_t size, const char *name)
2227
{
@@ -33,12 +38,15 @@ _PyAnnotateMemoryMap(void *addr, size_t size, const char *name)
3338
}
3439
return 0;
3540
}
41+
3642
#else
43+
3744
static inline int
3845
_PyAnnotateMemoryMap(void *Py_UNUSED(addr), size_t Py_UNUSED(size), const char *Py_UNUSED(name))
3946
{
4047
return 0;
4148
}
49+
4250
#endif
4351

4452
#ifdef __cplusplus

configure

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5728,15 +5728,6 @@ AC_CHECK_DECLS([UT_NAMESIZE],
57285728
[Define if you have the 'HAVE_UT_NAMESIZE' constant.])],
57295729
[],
57305730
[@%:@include <utmp.h>])
5731-
# musl libc redefines struct prctl_mm_map and conflicts with linux/prctl.h
5732-
AS_IF([test "$ac_cv_libc" != musl], [
5733-
AC_CHECK_DECLS([PR_SET_VMA_ANON_NAME],
5734-
[AC_DEFINE([HAVE_PR_SET_VMA_ANON_NAME], [1],
5735-
[Define if you have the 'PR_SET_VMA_ANON_NAME' constant.])],
5736-
[],
5737-
[@%:@include <linux/prctl.h>
5738-
@%:@include <sys/prctl.h>])
5739-
])
57405731
# check for openpty, login_tty, and forkpty
57415732

57425733
AC_CHECK_FUNCS([openpty], [],

pyconfig.h.in

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,6 @@
224224
/* Define to 1 if you have the <db.h> header file. */
225225
#undef HAVE_DB_H
226226

227-
/* Define to 1 if you have the declaration of 'PR_SET_VMA_ANON_NAME', and to 0
228-
if you don't. */
229-
#undef HAVE_DECL_PR_SET_VMA_ANON_NAME
230-
231227
/* Define to 1 if you have the declaration of 'RTLD_DEEPBIND', and to 0 if you
232228
don't. */
233229
#undef HAVE_DECL_RTLD_DEEPBIND
@@ -1003,9 +999,6 @@
1003999
/* Define if your compiler supports function prototype */
10041000
#undef HAVE_PROTOTYPES
10051001

1006-
/* Define if you have the 'PR_SET_VMA_ANON_NAME' constant. */
1007-
#undef HAVE_PR_SET_VMA_ANON_NAME
1008-
10091002
/* Define to 1 if you have the 'pthread_condattr_setclock' function. */
10101003
#undef HAVE_PTHREAD_CONDATTR_SETCLOCK
10111004

0 commit comments

Comments
 (0)