gh-143632: Detect availability of PR_SET_VMA at runtime (fixes build with musl libc)#148771
Open
sideeffect42 wants to merge 1 commit intopython:mainfrom
Open
gh-143632: Detect availability of PR_SET_VMA at runtime (fixes build with musl libc)#148771sideeffect42 wants to merge 1 commit intopython:mainfrom
PR_SET_VMA at runtime (fixes build with musl libc)#148771sideeffect42 wants to merge 1 commit intopython:mainfrom
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
248dbf9 to
4dcdecf
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
…ibc)
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
4dcdecf to
8ddb452
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Building latest Python 3.15.0a8+ on musl-based Linux fails with the following error:
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 thePR_SET_VMA_ANON_NAMEmacro.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_NAMEis not enabled,mmap.mmap.set_name()will raise anOSError, e.g.: