Skip to content

gh-153400: Resolve newer libc/syscall wrappers at runtime on Linux - #154816

Open
daandemeyer wants to merge 1 commit into
python:mainfrom
daandemeyer:posix-runtime-libc-shims
Open

gh-153400: Resolve newer libc/syscall wrappers at runtime on Linux#154816
daandemeyer wants to merge 1 commit into
python:mainfrom
daandemeyer:posix-runtime-libc-shims

Conversation

@daandemeyer

@daandemeyer daandemeyer commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

os functions like copy_file_range() are gated on a configure-time AC_CHECK_FUNCS probe and compiled out when the build libc lacks the symbol. A redistributable built against an old glibc (the python-build-standalone builds target glibc 2.17) therefore never exposes them, even when run on a newer glibc or a capable kernel.

Add Modules/posixshims.h, which on Linux always exposes Py(), resolves the libc symbol once at load time via dlsym(RTLD_DEFAULT) from a constructor, and falls back to the raw syscall when the running libc lacks the wrapper. Resolving in a constructor keeps dlsym(), which is not async-signal-safe, out of signal handlers and the fork()/exec() window. Off Linux the classic build-time HAVE_* direct calls are kept.

Limit the shims to wrappers newer than the glibc 2.17 baseline, since anything at or below it is always present in the build libc:

copy_file_range (glibc 2.27)
memfd_create (glibc 2.27)
pidfd_open (glibc 2.36)
pidfd_getfd (glibc 2.36)

pidfd_open() and pidfd_getfd() previously issued raw syscalls unconditionally; they now prefer the glibc wrapper when present.

os functions like copy_file_range() are gated on a configure-time
AC_CHECK_FUNCS probe and compiled out when the build libc lacks the
symbol. A redistributable built against an old glibc (the
python-build-standalone builds target glibc 2.17) therefore never
exposes them, even when run on a newer glibc or a capable kernel.

Add Modules/posixshims.h, which on Linux always exposes _Py_<func>(),
resolves the libc symbol once at load time via dlsym(RTLD_DEFAULT) from a
constructor, and falls back to the raw syscall when the running libc
lacks the wrapper. Resolving in a constructor keeps dlsym(), which is not
async-signal-safe, out of signal handlers and the fork()/exec() window.
Off Linux the classic build-time HAVE_* direct calls are kept.

Limit the shims to wrappers newer than the glibc 2.17 baseline, since
anything at or below it is always present in the build libc:

  copy_file_range (glibc 2.27)
  memfd_create    (glibc 2.27)
  pidfd_open      (glibc 2.36)
  pidfd_getfd     (glibc 2.36)

pidfd_open() and pidfd_getfd() previously issued raw syscalls
unconditionally; they now prefer the glibc wrapper when present.

Signed-off-by: Daan De Meyer <daan@amutable.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant