@@ -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
0 commit comments