Skip to content

Commit 235d138

Browse files
encukouYhg1s
andauthored
pythongh-159320: use pthread_getattr_np for stack limit checks even on musl (pythonGH-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 Co-authored-by: Thomas Wouters <thomas@python.org>
1 parent cc00dde commit 235d138

5 files changed

Lines changed: 159 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
@@ -70,6 +70,18 @@ _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count)
7070
# define Py_C_STACK_SIZE 4000000
7171
#endif
7272

73+
#ifdef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP
74+
#ifdef HAVE_SYS_RESOURCE_H
75+
# include <sys/resource.h>
76+
#endif
77+
#ifdef HAVE_SYS_SYSCALL_H
78+
# include <sys/syscall.h>
79+
#endif
80+
#ifdef HAVE_UNISTD_H
81+
# include <unistd.h>
82+
#endif
83+
#endif
84+
7385
static void
7486
hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
7587
{
@@ -89,14 +101,8 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
89101
*top = (uintptr_t)stack_addr;
90102
*base = ((uintptr_t)stack_addr) - stack_size;
91103
#else
92-
// musl supports HAVE_PTHREAD_GETATTR_NP, but the resulting stack size
93-
// (on alpine at least) is much smaller than expected and imposes undue
94-
// limits compared to the old stack size estimation. musl doesn't give
95-
// us a way to detect it, so we have to assume any non-glibc,
96-
// non-Android Linux is musl and mustn't rely on pthread_getattr_np.
97104
# if (defined(HAVE_PTHREAD_GETATTR_NP) || defined(_Py_HAVE_PTHREAD_ATTR_GET_NP)) \
98-
&& !defined(_AIX) && !defined(__NetBSD__) \
99-
&& (defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__))
105+
&& !defined(_AIX) && !defined(__NetBSD__)
100106
size_t stack_size, guard_size;
101107
void *stack_addr;
102108
pthread_attr_t attr;
@@ -113,8 +119,30 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
113119
err |= pthread_attr_destroy(&attr);
114120
}
115121
if (err == 0) {
116-
*base = ((uintptr_t)stack_addr) + guard_size;
117122
*top = (uintptr_t)stack_addr + stack_size;
123+
# ifdef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP
124+
/* pthread_getattr_np on the main thread of a musl process reports
125+
* only the part of the stack that is currently mapped, rather than
126+
* the size allowed. The reported top is correct.
127+
* New threads are reported correctly even on musl, so this fallback
128+
* only applies to the main thread. */
129+
if ((pid_t)syscall(SYS_gettid) == getpid()) {
130+
struct rlimit rl;
131+
if (getrlimit(RLIMIT_STACK, &rl) == 0) {
132+
/* If there's a reasonable limit set (one that fits in the
133+
* address space below *top), use that.
134+
* Otherwise use the compile-time guess */
135+
size_t limit = Py_C_STACK_SIZE;
136+
if (rl.rlim_cur != RLIM_INFINITY && rl.rlim_cur < *top) {
137+
limit = (size_t)rl.rlim_cur;
138+
}
139+
if (stack_size < limit) {
140+
stack_addr = (void *)(*top - limit);
141+
}
142+
}
143+
}
144+
# endif
145+
*base = ((uintptr_t)stack_addr) + guard_size;
118146
return;
119147
}
120148
# endif

‎configure‎

Lines changed: 74 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5811,6 +5811,49 @@ PY_CHECK_FUNC_PRIVATE([freadlink], [@%:@include <unistd.h>])
58115811
PY_CHECK_FUNC_PRIVATE([strerror_r], [@%:@include <string.h>])
58125812
# FreeBSD pthread_attr_get_np()
58135813
PY_CHECK_FUNC_PRIVATE([pthread_attr_get_np], [@%:@include <pthread_np.h>])
5814+
5815+
# Detect musl's behaviour of reporting the current allocated stack size for
5816+
# the main thread, rather than the max size, in pthread_getattr_np.
5817+
AS_IF([test "$ac_cv_func_pthread_getattr_np" = yes],
5818+
[AC_CACHE_CHECK([whether pthread_getattr_np reports the main thread stack unreliably],
5819+
[ac_cv_unreliable_pthread_getattr_np],
5820+
[# Run the test with a finite stack limit (1 MiB),
5821+
# so the result does not depend on the limit configure runs under.
5822+
py_save_stack_limit=`ulimit -S -s 2>/dev/null`
5823+
ulimit -S -s 1024 2>/dev/null
5824+
AC_RUN_IFELSE([AC_LANG_SOURCE([[
5825+
#define _GNU_SOURCE
5826+
#include <pthread.h>
5827+
#include <stdlib.h>
5828+
#include <sys/resource.h>
5829+
5830+
int main(void) {
5831+
pthread_attr_t attr;
5832+
void *stack_addr;
5833+
size_t stack_size, guard_size;
5834+
struct rlimit rl;
5835+
5836+
if (pthread_getattr_np(pthread_self(), &attr) != 0) return 1;
5837+
if (pthread_attr_getstack(&attr, &stack_addr, &stack_size) != 0 ||
5838+
pthread_attr_getguardsize(&attr, &guard_size) != 0) {
5839+
pthread_attr_destroy(&attr);
5840+
return 1;
5841+
}
5842+
pthread_attr_destroy(&attr);
5843+
if (getrlimit(RLIMIT_STACK, &rl) != 0) return 1;
5844+
if (rl.rlim_cur == RLIM_INFINITY) return 0;
5845+
return (stack_size >= rl.rlim_cur / 2) ? 0 : 1;
5846+
}
5847+
]])],
5848+
[ac_cv_unreliable_pthread_getattr_np=no],
5849+
[ac_cv_unreliable_pthread_getattr_np=yes],
5850+
[ac_cv_unreliable_pthread_getattr_np=no])
5851+
AS_IF([test -n "$py_save_stack_limit"],
5852+
[ulimit -S -s "$py_save_stack_limit" 2>/dev/null])])
5853+
AS_VAR_IF([ac_cv_unreliable_pthread_getattr_np], [yes],
5854+
[AC_DEFINE([_Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP], [1],
5855+
[Define if pthread_getattr_np under-reports the main thread's stack size (musl behaviour).])])])
5856+
58145857
PY_CHECK_FUNC_PRIVATE([getdents64], [@%:@include <dirent.h>])
58155858
PY_CHECK_FUNC_PRIVATE([gettid], [@%:@include <unistd.h>])
58165859
PY_CHECK_FUNC_PRIVATE([if_indextoname], [@%:@include <net/if.h>])

‎pyconfig.h.in‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,10 @@
21732173
/* Define if you have the 'tanpi' function. */
21742174
#undef _Py_HAVE_TANPI
21752175

2176+
/* Define if pthread_getattr_np under-reports the main thread's stack size
2177+
(musl behaviour). */
2178+
#undef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP
2179+
21762180
/* Thread stack size set by the linker (in bytes). */
21772181
#undef _Py_LINKER_THREAD_STACK_SIZE
21782182

0 commit comments

Comments
 (0)