Skip to content

Commit 444bce7

Browse files
committed
Use pthread_getattr_np to determine stack limits on systems supporting _GNU_SOURCE
1 parent 5d66c55 commit 444bce7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Python/ceval.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,21 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
360360
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + PYOS_STACK_MARGIN_BYTES;
361361
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + PYOS_STACK_MARGIN_BYTES;
362362
#else
363+
# if defined(_GNU_SOURCE)
364+
size_t stack_size, guard_size;
365+
void *stack_addr;
366+
pthread_attr_t attr;
367+
int err = pthread_getattr_np(pthread_self(), &attr);
368+
err |= pthread_attr_getguardsize(&attr, &guard_size);
369+
err |= pthread_attr_getstack(&attr, &stack_addr, &stack_size);
370+
if (err == 0) {
371+
uintptr_t base = ((uintptr_t)stack_addr) + guard_size;
372+
_tstate->c_stack_top = base + stack_size;
373+
_tstate->c_stack_soft_limit = base + PYOS_STACK_MARGIN_BYTES * 2;
374+
_tstate->c_stack_hard_limit = base + PYOS_STACK_MARGIN_BYTES;
375+
return;
376+
}
377+
# endif
363378
char here;
364379
uintptr_t here_addr = (uintptr_t)&here;
365380
_tstate->c_stack_top = _Py_SIZE_ROUND_UP(here_addr, 4096);

0 commit comments

Comments
 (0)