-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
gh-153400: Resolve newer libc/syscall wrappers at runtime on Linux #153398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daandemeyer
wants to merge
1
commit into
python:main
Choose a base branch
from
daandemeyer:posix-runtime-libc-shims
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2026-07-09-10-19-10.gh-issue-153400.ELIvWE.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| On Linux, :func:`os.copy_file_range`, :func:`os.memfd_create`, | ||
| :func:`os.pidfd_open` and :func:`os.pidfd_getfd` are now resolved at runtime, so | ||
| they stay available on interpreters built against an older glibc when the | ||
| running system provides them. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* posixshims.h: resolve newer libc/syscall wrappers at runtime, not build time. | ||
|
|
||
| CPython gates functions like copy_file_range() on a configure-time | ||
| AC_CHECK_FUNCS probe, compiling them out when the build libc lacks the symbol. | ||
| That permanently disables them in a redistributable built against an old glibc | ||
| even when it later runs on a newer one. Instead, on Linux we always expose | ||
| _Py_<func>(), resolve the libc symbol once at load time via dlsym(RTLD_DEFAULT) | ||
| from a constructor, and fall back to the raw syscall. A constructor (vs. lazy | ||
| resolution) keeps dlsym(), which is not async-signal-safe, out of signal | ||
| handlers and the fork()/exec() window, and lets the cache skip synchronization. | ||
|
|
||
| Each _Py_<func>() is declared explicitly; only the shared body is a macro. | ||
| Off Linux, each function keeps its classic build-time HAVE_* direct call. */ | ||
|
|
||
| #ifndef Py_POSIXSHIMS_H | ||
| #define Py_POSIXSHIMS_H | ||
|
|
||
| /* Needs ELF constructors, dlsym() and Linux syscall numbers. */ | ||
| #if defined(__linux__) && (defined(__GNUC__) || defined(__clang__)) \ | ||
| && defined(HAVE_DLFCN_H) && defined(HAVE_SYS_SYSCALL_H) | ||
| # define _Py_HAVE_POSIX_SHIMS | ||
| #endif | ||
|
|
||
| #ifdef _Py_HAVE_POSIX_SHIMS | ||
|
|
||
| #include <dlfcn.h> // dlsym(), RTLD_DEFAULT | ||
| #include <sys/syscall.h> // __NR_* syscall numbers | ||
| #include <sys/types.h> // off_t, ssize_t | ||
| #include <unistd.h> // syscall() | ||
|
|
||
| /* The raw-syscall fallback needs a syscall number. If a stale build header | ||
| predates one, default it to an invalid number so the shim still builds and the | ||
| dlsym'd libc symbol stays usable; only the syscall fallback is lost, returning | ||
| -1/ENOSYS at runtime. <sys/syscall.h> above is the sole source of these | ||
| numbers in this translation unit, so nothing redefines them afterwards. */ | ||
| #ifndef __NR_copy_file_range | ||
| # define __NR_copy_file_range (-1) | ||
| #endif | ||
| #ifndef __NR_memfd_create | ||
| # define __NR_memfd_create (-1) | ||
| #endif | ||
| #ifndef __NR_pidfd_open | ||
| # define __NR_pidfd_open (-1) | ||
| #endif | ||
| #ifndef __NR_pidfd_getfd | ||
| # define __NR_pidfd_getfd (-1) | ||
| #endif | ||
|
|
||
| /* A per-function cache plus a constructor that resolves the libc symbol once at | ||
| load time. The __asm__ barrier after dlsym() is load-bearing under LTO: | ||
| without it the compiler may tail-call dlsym(), breaking glibc's | ||
| return-address caller lookup and crashing at startup (glibc BZ #34156). */ | ||
| #define _Py_SHIM_RESOLVER(func) \ | ||
| static void *_Py_shim_##func; \ | ||
| __attribute__((constructor)) \ | ||
| static void _Py_shim_##func##_resolve(void) { \ | ||
| void *p = dlsym(RTLD_DEFAULT, #func); \ | ||
| __asm__ volatile("" ::: "memory"); \ | ||
| _Py_shim_##func = p; \ | ||
| } | ||
|
|
||
| /* Body of an explicitly declared _Py_<func>() wrapper: call the resolved libc | ||
| symbol if present, else fall back to syscall(__NR_<func>, ...). On a kernel | ||
| too old for the syscall that returns -1/ENOSYS, which posixmodule.c reports as | ||
| an ordinary OSError. The cast is __typeof__ of _Py_<func> (the wrapper | ||
| itself), not of <func>, which may be undeclared at build time. */ | ||
| #define _Py_SHIM_SYSCALL(func, ...) \ | ||
| if (_Py_shim_##func != NULL) { \ | ||
| return ((__typeof__(&_Py_##func)) _Py_shim_##func)(__VA_ARGS__); \ | ||
| } \ | ||
| return syscall(__NR_##func, __VA_ARGS__) | ||
|
|
||
| #endif /* _Py_HAVE_POSIX_SHIMS */ | ||
|
|
||
|
|
||
| /* ---- copy_file_range() (glibc 2.27) ------------------------------------- */ | ||
|
|
||
| #if defined(_Py_HAVE_POSIX_SHIMS) | ||
| # define _Py_HAVE_COPY_FILE_RANGE | ||
| _Py_SHIM_RESOLVER(copy_file_range) | ||
| /* off_t is 64-bit (_FILE_OFFSET_BITS=64), matching the kernel's loff_t. */ | ||
| static inline ssize_t | ||
| _Py_copy_file_range(int fd_in, off_t *off_in, int fd_out, off_t *off_out, | ||
| size_t len, unsigned int flags) | ||
| { | ||
| _Py_SHIM_SYSCALL(copy_file_range, | ||
| fd_in, off_in, fd_out, off_out, len, flags); | ||
| } | ||
|
|
||
| #elif defined(HAVE_COPY_FILE_RANGE) | ||
| # define _Py_HAVE_COPY_FILE_RANGE | ||
| # define _Py_copy_file_range copy_file_range | ||
| #endif | ||
|
|
||
|
|
||
| /* ---- memfd_create() (glibc 2.27) ---------------------------------------- */ | ||
|
|
||
| #if defined(_Py_HAVE_POSIX_SHIMS) | ||
| # define _Py_HAVE_MEMFD_CREATE | ||
| _Py_SHIM_RESOLVER(memfd_create) | ||
| static inline int | ||
| _Py_memfd_create(const char *name, unsigned int flags) | ||
| { | ||
| _Py_SHIM_SYSCALL(memfd_create, name, flags); | ||
| } | ||
|
|
||
| #elif defined(HAVE_MEMFD_CREATE) | ||
| # define _Py_HAVE_MEMFD_CREATE | ||
| # define _Py_memfd_create memfd_create | ||
| #endif | ||
|
|
||
|
|
||
| /* ---- pidfd_open(), pidfd_getfd() (glibc 2.36) --------------------------- * | ||
|
|
||
| Previously unconditional raw syscalls; the shim now prefers glibc's wrapper. | ||
| Linux-only from the start (no non-Linux branch); the Android API floor from | ||
| the original guards is preserved. */ | ||
|
|
||
| #if defined(_Py_HAVE_POSIX_SHIMS) \ | ||
| && !(defined(__ANDROID__) && __ANDROID_API__ < 31) | ||
| # define _Py_HAVE_PIDFD_OPEN | ||
| _Py_SHIM_RESOLVER(pidfd_open) | ||
| static inline int | ||
| _Py_pidfd_open(pid_t pid, unsigned int flags) | ||
| { | ||
| _Py_SHIM_SYSCALL(pidfd_open, pid, flags); | ||
| } | ||
| #endif | ||
|
|
||
| #if defined(_Py_HAVE_POSIX_SHIMS) \ | ||
| && !(defined(__ANDROID__) && __ANDROID_API__ < 31) | ||
| # define _Py_HAVE_PIDFD_GETFD | ||
| _Py_SHIM_RESOLVER(pidfd_getfd) | ||
| static inline int | ||
| _Py_pidfd_getfd(int pidfd, int targetfd, unsigned int flags) | ||
| { | ||
| _Py_SHIM_SYSCALL(pidfd_getfd, pidfd, targetfd, flags); | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* !Py_POSIXSHIMS_H */ | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the else branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure what you meant here, I've added fallbacks if the syscall numbers are not available.