From d8c6b68cbc8a177e36162df52a9a945a330ace31 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Fri, 27 Nov 2026 12:00:18 +0100 Subject: [PATCH 1/2] [3.15] gh-159320: use pthread_getattr_np for stack limit checks even on musl (GH-161749) Rewrite the pthread_getattr_np usage to avoid the problem on musl of it reporting the stack limit of the main thread incorrectly. On platforms where pthread_getattr_np underreports the main thread's stack size, use RLIMIT_STACK for the main thread instead. Add HAVE_UNRELIABLE_PTHREAD_GETATTR_NP to the long, long list of ignored symbols for the Py check. Authored mainly by: Thomas Wouters (cherry picked from commit 235d1383333bd36d15290de03a98c25f1a2fd109) Co-authored-by: Petr Viktorin Co-authored-by: Thomas Wouters --- ...-10-13-23-08-01.gh-issue-159320._Yhg1s.rst | 2 + Python/ceval.c | 44 +++++++++-- configure | 74 +++++++++++++++++++ configure.ac | 42 +++++++++++ pyconfig.h.in | 4 + 5 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-23-08-01.gh-issue-159320._Yhg1s.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-23-08-01.gh-issue-159320._Yhg1s.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-23-08-01.gh-issue-159320._Yhg1s.rst new file mode 100644 index 000000000000000..e9a4609a8a4f2d1 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-23-08-01.gh-issue-159320._Yhg1s.rst @@ -0,0 +1,2 @@ +Enable stack size limit detection on musl and other non-glibc Linux +platforms (like Android), instead of relying on a fixed stack size limit. diff --git a/Python/ceval.c b/Python/ceval.c index c9c8d1f1a7d1163..f7a23eb4f737d8b 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -142,6 +142,18 @@ int pthread_attr_destroy(pthread_attr_t *a) #endif +#ifdef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP +#ifdef HAVE_SYS_RESOURCE_H +# include +#endif +#ifdef HAVE_SYS_SYSCALL_H +# include +#endif +#ifdef HAVE_UNISTD_H +# include +#endif +#endif + static void hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp) { @@ -159,14 +171,8 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp) *top = (uintptr_t)stack_addr; *base = ((uintptr_t)stack_addr) - stack_size; #else - // musl supports HAVE_PTHREAD_GETATTR_NP, but the resulting stack size - // (on alpine at least) is much smaller than expected and imposes undue - // limits compared to the old stack size estimation. musl doesn't give - // us a way to detect it, so we have to assume any non-glibc, - // non-Android Linux is musl and mustn't rely on pthread_getattr_np. # if (defined(HAVE_PTHREAD_GETATTR_NP) || defined(_Py_HAVE_PTHREAD_ATTR_GET_NP)) \ - && !defined(_AIX) && !defined(__NetBSD__) \ - && (defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__)) + && !defined(_AIX) && !defined(__NetBSD__) size_t stack_size, guard_size; void *stack_addr; pthread_attr_t attr; @@ -182,8 +188,30 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp) err |= pthread_attr_destroy(&attr); } if (err == 0) { - *base = ((uintptr_t)stack_addr) + guard_size; *top = (uintptr_t)stack_addr + stack_size; +# ifdef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP + /* pthread_getattr_np on the main thread of a musl process reports + * only the part of the stack that is currently mapped, rather than + * the size allowed. The reported top is correct. + * New threads are reported correctly even on musl, so this fallback + * only applies to the main thread. */ + if ((pid_t)syscall(SYS_gettid) == getpid()) { + struct rlimit rl; + if (getrlimit(RLIMIT_STACK, &rl) == 0) { + /* If there's a reasonable limit set (one that fits in the + * address space below *top), use that. + * Otherwise use the compile-time guess */ + size_t limit = Py_C_STACK_SIZE; + if (rl.rlim_cur != RLIM_INFINITY && rl.rlim_cur < *top) { + limit = (size_t)rl.rlim_cur; + } + if (stack_size < limit) { + stack_addr = (void *)(*top - limit); + } + } + } +# endif + *base = ((uintptr_t)stack_addr) + guard_size; return; } # endif diff --git a/configure b/configure index 8f430e6d8c07b6e..69524fe02b32794 100755 --- a/configure +++ b/configure @@ -21312,6 +21312,80 @@ fi +# Detect musl's behaviour of reporting the current allocated stack size for +# the main thread, rather than the max size, in pthread_getattr_np. +if test "$ac_cv_func_pthread_getattr_np" = yes +then : + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether pthread_getattr_np reports the main thread stack unreliably" >&5 +printf %s "checking whether pthread_getattr_np reports the main thread stack unreliably... " >&6; } +if test ${ac_cv_unreliable_pthread_getattr_np+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) # Run the test with a finite stack limit (1 MiB), + # so the result does not depend on the limit configure runs under. + py_save_stack_limit=`ulimit -S -s 2>/dev/null` + ulimit -S -s 1024 2>/dev/null + if test "$cross_compiling" = yes +then : + ac_cv_unreliable_pthread_getattr_np=no +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +#define _GNU_SOURCE +#include +#include +#include + +int main(void) { + pthread_attr_t attr; + void *stack_addr; + size_t stack_size, guard_size; + struct rlimit rl; + + if (pthread_getattr_np(pthread_self(), &attr) != 0) return 1; + if (pthread_attr_getstack(&attr, &stack_addr, &stack_size) != 0 || + pthread_attr_getguardsize(&attr, &guard_size) != 0) { + pthread_attr_destroy(&attr); + return 1; + } + pthread_attr_destroy(&attr); + if (getrlimit(RLIMIT_STACK, &rl) != 0) return 1; + if (rl.rlim_cur == RLIM_INFINITY) return 0; + return (stack_size >= rl.rlim_cur / 2) ? 0 : 1; +} + +_ACEOF +if ac_fn_c_try_run "$LINENO" +then : + ac_cv_unreliable_pthread_getattr_np=no +else case e in #( + e) ac_cv_unreliable_pthread_getattr_np=yes ;; +esac +fi +rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \ + conftest.$ac_objext conftest.beam conftest.$ac_ext ;; +esac +fi + + if test -n "$py_save_stack_limit" +then : + ulimit -S -s "$py_save_stack_limit" 2>/dev/null +fi ;; +esac +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_unreliable_pthread_getattr_np" >&5 +printf "%s\n" "$ac_cv_unreliable_pthread_getattr_np" >&6; } + if test "x$ac_cv_unreliable_pthread_getattr_np" = xyes +then : + +printf "%s\n" "#define _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP 1" >>confdefs.h + +fi +fi + + # os.statx uses Linux's statx function. AIX also has a function named statx, # but it's unrelated. Check only on Linux (including Android). diff --git a/configure.ac b/configure.ac index 80461e596f379cf..fd4882be760942a 100644 --- a/configure.ac +++ b/configure.ac @@ -5546,6 +5546,48 @@ PY_CHECK_FUNC_PRIVATE([strerror_r], [@%:@include ]) # FreeBSD pthread_attr_get_np() PY_CHECK_FUNC_PRIVATE([pthread_attr_get_np], [@%:@include ]) +# Detect musl's behaviour of reporting the current allocated stack size for +# the main thread, rather than the max size, in pthread_getattr_np. +AS_IF([test "$ac_cv_func_pthread_getattr_np" = yes], + [AC_CACHE_CHECK([whether pthread_getattr_np reports the main thread stack unreliably], + [ac_cv_unreliable_pthread_getattr_np], + [# Run the test with a finite stack limit (1 MiB), + # so the result does not depend on the limit configure runs under. + py_save_stack_limit=`ulimit -S -s 2>/dev/null` + ulimit -S -s 1024 2>/dev/null + AC_RUN_IFELSE([AC_LANG_SOURCE([[ +#define _GNU_SOURCE +#include +#include +#include + +int main(void) { + pthread_attr_t attr; + void *stack_addr; + size_t stack_size, guard_size; + struct rlimit rl; + + if (pthread_getattr_np(pthread_self(), &attr) != 0) return 1; + if (pthread_attr_getstack(&attr, &stack_addr, &stack_size) != 0 || + pthread_attr_getguardsize(&attr, &guard_size) != 0) { + pthread_attr_destroy(&attr); + return 1; + } + pthread_attr_destroy(&attr); + if (getrlimit(RLIMIT_STACK, &rl) != 0) return 1; + if (rl.rlim_cur == RLIM_INFINITY) return 0; + return (stack_size >= rl.rlim_cur / 2) ? 0 : 1; +} + ]])], + [ac_cv_unreliable_pthread_getattr_np=no], + [ac_cv_unreliable_pthread_getattr_np=yes], + [ac_cv_unreliable_pthread_getattr_np=no]) + AS_IF([test -n "$py_save_stack_limit"], + [ulimit -S -s "$py_save_stack_limit" 2>/dev/null])]) + AS_VAR_IF([ac_cv_unreliable_pthread_getattr_np], [yes], + [AC_DEFINE([_Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP], [1], + [Define if pthread_getattr_np under-reports the main thread's stack size (musl behaviour).])])]) + # os.statx uses Linux's statx function. AIX also has a function named statx, # but it's unrelated. Check only on Linux (including Android). diff --git a/pyconfig.h.in b/pyconfig.h.in index dfc28a51944f52a..e3356a5e643ef4f 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -2103,6 +2103,10 @@ /* Define if you have the 'strerror_r' function. */ #undef _Py_HAVE_STRERROR_R +/* Define if pthread_getattr_np under-reports the main thread's stack size + (musl behaviour). */ +#undef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP + /* Thread stack size set by the linker (in bytes). */ #undef _Py_LINKER_THREAD_STACK_SIZE From 7ea827e81e2e72ad69799573bb37daaa50e23b18 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 28 Nov 2026 19:57:24 +0200 Subject: [PATCH 2/2] Fix autoconf whitespace --- configure | 1 - 1 file changed, 1 deletion(-) diff --git a/configure b/configure index 69524fe02b32794..98207437d24cdc3 100755 --- a/configure +++ b/configure @@ -21386,7 +21386,6 @@ fi fi - # os.statx uses Linux's statx function. AIX also has a function named statx, # but it's unrelated. Check only on Linux (including Android). case $ac_sys_system in #(