Skip to content

Commit 404f291

Browse files
committed
Fix c_stack_hard_limit: add _PyOS_STACK_MARGIN_BYTES
1 parent 4cc256f commit 404f291

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Modules/_testinternalcapi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ check_threadstate_set_stack(PyThreadState *tstate, void *start, size_t size)
24252425
assert(!PyErr_Occurred());
24262426

24272427
_PyThreadStateImpl *ts = (_PyThreadStateImpl *)tstate;
2428-
assert(ts->c_stack_hard_limit == (uintptr_t)start);
2428+
assert(ts->c_stack_hard_limit == (uintptr_t)start + _PyOS_STACK_MARGIN_BYTES);
24292429
assert(ts->c_stack_top == (uintptr_t)start + size);
24302430
assert(ts->c_stack_soft_limit >= ts->c_stack_hard_limit);
24312431
assert(ts->c_stack_soft_limit < ts->c_stack_top);

Python/ceval.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,10 @@ tstate_set_stack(PyThreadState *tstate,
446446
assert(stack_size > 0);
447447
assert(stack_size >= (_PyOS_STACK_MARGIN_BYTES * 3));
448448

449+
uintptr_t start = (uintptr_t)stack_start_addr;
449450
_PyThreadStateImpl *ts = (_PyThreadStateImpl *)tstate;
450-
ts->c_stack_hard_limit = (uintptr_t)stack_start_addr;
451-
ts->c_stack_top = (uintptr_t)stack_start_addr + stack_size;
451+
ts->c_stack_hard_limit = start + _PyOS_STACK_MARGIN_BYTES;
452+
ts->c_stack_top = start + stack_size;
452453

453454
uintptr_t soft_limit = ts->c_stack_hard_limit;
454455
#ifdef _Py_THREAD_SANITIZER
@@ -498,7 +499,7 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
498499
ULONG guarantee = 0;
499500
SetThreadStackGuarantee(&guarantee);
500501

501-
uintptr_t start = (uintptr_t)low + guarantee + _PyOS_STACK_MARGIN_BYTES;
502+
uintptr_t start = (uintptr_t)low + guarantee;
502503
size_t size = (uintptr_t)high - start;
503504
tstate_set_stack(tstate, (void*)start, size);
504505

@@ -525,7 +526,7 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
525526
}
526527
if (err == 0) {
527528
uintptr_t base = ((uintptr_t)stack_addr) + guard_size;
528-
uintptr_t start = base + _PyOS_STACK_MARGIN_BYTES;
529+
uintptr_t start = base;
529530
size_t pystack_size = (base + stack_size) - start;
530531
tstate_set_stack(tstate, (void*)start, pystack_size);
531532
}
@@ -534,7 +535,7 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
534535
{
535536
uintptr_t here_addr = _Py_get_machine_stack_pointer();
536537
uintptr_t top = _Py_SIZE_ROUND_UP(here_addr, 4096);
537-
uintptr_t start = top - (Py_C_STACK_SIZE + _PyOS_STACK_MARGIN_BYTES);
538+
uintptr_t start = top - Py_C_STACK_SIZE;
538539
size_t pystack_size = top - start;
539540
tstate_set_stack(tstate, (void*)start, pystack_size);
540541
}

0 commit comments

Comments
 (0)