Skip to content

Commit c7c92f4

Browse files
hugovkencukouYhg1s
authored
[3.15] gh-159320: use pthread_getattr_np for stack limit checks even on musl (GH-161749) (GH-162819)
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 235d138) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Thomas Wouters <thomas@python.org>
1 parent 8547347 commit c7c92f4

5 files changed

Lines changed: 157 additions & 8 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Enable stack size limit detection on musl and other non-glibc Linux
2+
platforms (like Android), instead of relying on a fixed stack size limit.

‎Python/ceval.c‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ int pthread_attr_destroy(pthread_attr_t *a)
142142

143143
#endif
144144

145+
#ifdef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP
146+
#ifdef HAVE_SYS_RESOURCE_H
147+
# include <sys/resource.h>
148+
#endif
149+
#ifdef HAVE_SYS_SYSCALL_H
150+
# include <sys/syscall.h>
151+
#endif
152+
#ifdef HAVE_UNISTD_H
153+
# include <unistd.h>
154+
#endif
155+
#endif
156+
145157
static void
146158
hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
147159
{
@@ -159,14 +171,8 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
159171
*top = (uintptr_t)stack_addr;
160172
*base = ((uintptr_t)stack_addr) - stack_size;
161173
#else
162-
// musl supports HAVE_PTHREAD_GETATTR_NP, but the resulting stack size
163-
// (on alpine at least) is much smaller than expected and imposes undue
164-
// limits compared to the old stack size estimation. musl doesn't give
165-
// us a way to detect it, so we have to assume any non-glibc,
166-
// non-Android Linux is musl and mustn't rely on pthread_getattr_np.
167174
# if (defined(HAVE_PTHREAD_GETATTR_NP) || defined(_Py_HAVE_PTHREAD_ATTR_GET_NP)) \
168-
&& !defined(_AIX) && !defined(__NetBSD__) \
169-
&& (defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__))
175+
&& !defined(_AIX) && !defined(__NetBSD__)
170176
size_t stack_size, guard_size;
171177
void *stack_addr;
172178
pthread_attr_t attr;
@@ -182,8 +188,30 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
182188
err |= pthread_attr_destroy(&attr);
183189
}
184190
if (err == 0) {
185-
*base = ((uintptr_t)stack_addr) + guard_size;
186191
*top = (uintptr_t)stack_addr + stack_size;
192+
# ifdef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP
193+
/* pthread_getattr_np on the main thread of a musl process reports
194+
* only the part of the stack that is currently mapped, rather than
195+
* the size allowed. The reported top is correct.
196+
* New threads are reported correctly even on musl, so this fallback
197+
* only applies to the main thread. */
198+
if ((pid_t)syscall(SYS_gettid) == getpid()) {
199+
struct rlimit rl;
200+
if (getrlimit(RLIMIT_STACK, &rl) == 0) {
201+
/* If there's a reasonable limit set (one that fits in the
202+
* address space below *top), use that.
203+
* Otherwise use the compile-time guess */
204+
size_t limit = Py_C_STACK_SIZE;
205+
if (rl.rlim_cur != RLIM_INFINITY && rl.rlim_cur < *top) {
206+
limit = (size_t)rl.rlim_cur;
207+
}
208+
if (stack_size < limit) {
209+
stack_addr = (void *)(*top - limit);
210+
}
211+
}
212+
}
213+
# endif
214+
*base = ((uintptr_t)stack_addr) + guard_size;
187215
return;
188216
}
189217
# endif

‎configure‎

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

‎configure.ac‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5546,6 +5546,48 @@ PY_CHECK_FUNC_PRIVATE([strerror_r], [@%:@include <string.h>])
55465546
# FreeBSD pthread_attr_get_np()
55475547
PY_CHECK_FUNC_PRIVATE([pthread_attr_get_np], [@%:@include <pthread_np.h>])
55485548

5549+
# Detect musl's behaviour of reporting the current allocated stack size for
5550+
# the main thread, rather than the max size, in pthread_getattr_np.
5551+
AS_IF([test "$ac_cv_func_pthread_getattr_np" = yes],
5552+
[AC_CACHE_CHECK([whether pthread_getattr_np reports the main thread stack unreliably],
5553+
[ac_cv_unreliable_pthread_getattr_np],
5554+
[# Run the test with a finite stack limit (1 MiB),
5555+
# so the result does not depend on the limit configure runs under.
5556+
py_save_stack_limit=`ulimit -S -s 2>/dev/null`
5557+
ulimit -S -s 1024 2>/dev/null
5558+
AC_RUN_IFELSE([AC_LANG_SOURCE([[
5559+
#define _GNU_SOURCE
5560+
#include <pthread.h>
5561+
#include <stdlib.h>
5562+
#include <sys/resource.h>
5563+
5564+
int main(void) {
5565+
pthread_attr_t attr;
5566+
void *stack_addr;
5567+
size_t stack_size, guard_size;
5568+
struct rlimit rl;
5569+
5570+
if (pthread_getattr_np(pthread_self(), &attr) != 0) return 1;
5571+
if (pthread_attr_getstack(&attr, &stack_addr, &stack_size) != 0 ||
5572+
pthread_attr_getguardsize(&attr, &guard_size) != 0) {
5573+
pthread_attr_destroy(&attr);
5574+
return 1;
5575+
}
5576+
pthread_attr_destroy(&attr);
5577+
if (getrlimit(RLIMIT_STACK, &rl) != 0) return 1;
5578+
if (rl.rlim_cur == RLIM_INFINITY) return 0;
5579+
return (stack_size >= rl.rlim_cur / 2) ? 0 : 1;
5580+
}
5581+
]])],
5582+
[ac_cv_unreliable_pthread_getattr_np=no],
5583+
[ac_cv_unreliable_pthread_getattr_np=yes],
5584+
[ac_cv_unreliable_pthread_getattr_np=no])
5585+
AS_IF([test -n "$py_save_stack_limit"],
5586+
[ulimit -S -s "$py_save_stack_limit" 2>/dev/null])])
5587+
AS_VAR_IF([ac_cv_unreliable_pthread_getattr_np], [yes],
5588+
[AC_DEFINE([_Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP], [1],
5589+
[Define if pthread_getattr_np under-reports the main thread's stack size (musl behaviour).])])])
5590+
55495591

55505592
# os.statx uses Linux's statx function. AIX also has a function named statx,
55515593
# but it's unrelated. Check only on Linux (including Android).

‎pyconfig.h.in‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,10 @@
21032103
/* Define if you have the 'strerror_r' function. */
21042104
#undef _Py_HAVE_STRERROR_R
21052105

2106+
/* Define if pthread_getattr_np under-reports the main thread's stack size
2107+
(musl behaviour). */
2108+
#undef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP
2109+
21062110
/* Thread stack size set by the linker (in bytes). */
21072111
#undef _Py_LINKER_THREAD_STACK_SIZE
21082112

0 commit comments

Comments
 (0)