Skip to content

Commit c162d64

Browse files
committed
More musl detection & workarounding
1 parent a44a16c commit c162d64

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

‎Python/ceval.c‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,29 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
119119
err |= pthread_attr_destroy(&attr);
120120
}
121121
if (err == 0) {
122+
*top = (uintptr_t)stack_addr + stack_size;
122123
# ifdef _Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP
123-
/* pthread_getattr_np on the main thread of a musl process reports a
124-
* stack size based on the current stack rather than RLIMIT_STACK.
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 top is correct. Extend the stack downwards
127+
* from it, by the current RLIMIT_STACK or the compile-time guess,
128+
* Py_C_STACK_SIZE.
125129
* New threads are reported correctly even on musl, so this fallback
126130
* only applies to the main thread. */
127131
if ((pid_t)syscall(SYS_gettid) == getpid()) {
128132
struct rlimit rl;
129-
if (getrlimit(RLIMIT_STACK, &rl) == 0 &&
130-
rl.rlim_cur != RLIM_INFINITY &&
131-
stack_size < rl.rlim_cur)
132-
{
133-
stack_size = rl.rlim_cur;
133+
if (getrlimit(RLIMIT_STACK, &rl) == 0) {
134+
size_t limit = Py_C_STACK_SIZE;
135+
if (rl.rlim_cur != RLIM_INFINITY && rl.rlim_cur < *top) {
136+
limit = (size_t)rl.rlim_cur;
137+
}
138+
if (stack_size < limit) {
139+
stack_addr = (void *)(*top - limit);
140+
}
134141
}
135142
}
136143
# endif
137144
*base = ((uintptr_t)stack_addr) + guard_size;
138-
*top = (uintptr_t)stack_addr + stack_size;
139145
return;
140146
}
141147
# endif

‎configure‎

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

‎configure.ac‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5827,7 +5827,11 @@ PY_CHECK_FUNC_PRIVATE([pthread_attr_get_np], [@%:@include <pthread_np.h>])
58275827
AS_IF([test "$ac_cv_func_pthread_getattr_np" = yes],
58285828
[AC_CACHE_CHECK([whether pthread_getattr_np reports the main thread stack reliably],
58295829
[ac_cv_unreliable_pthread_getattr_np],
5830-
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
5830+
[# Run the test with a finite stack limit (1 MiB),
5831+
# so the result does not depend on the limit configure runs under.
5832+
py_save_stack_limit=`ulimit -S -s 2>/dev/null`
5833+
ulimit -S -s 1024 2>/dev/null
5834+
AC_RUN_IFELSE([AC_LANG_SOURCE([[
58315835
#define _GNU_SOURCE
58325836
#include <pthread.h>
58335837
#include <stdlib.h>
@@ -5853,7 +5857,9 @@ int main(void) {
58535857
]])],
58545858
[ac_cv_unreliable_pthread_getattr_np=no],
58555859
[ac_cv_unreliable_pthread_getattr_np=yes],
5856-
[ac_cv_unreliable_pthread_getattr_np=no])])
5860+
[ac_cv_unreliable_pthread_getattr_np=no])
5861+
AS_IF([test -n "$py_save_stack_limit"],
5862+
[ulimit -S -s "$py_save_stack_limit" 2>/dev/null])])
58575863
AS_VAR_IF([ac_cv_unreliable_pthread_getattr_np], [yes],
58585864
[AC_DEFINE([_Py_HAVE_UNRELIABLE_PTHREAD_GETATTR_NP], [1],
58595865
[Define if pthread_getattr_np under-reports the main thread's stack size (musl behaviour).])])])

0 commit comments

Comments
 (0)