Skip to content

Commit b0d1cf3

Browse files
committed
Fix TSan: use larger minimum stack size
1 parent 5d157ff commit b0d1cf3

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Include/internal/pycore_pythonrun.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ extern const char* _Py_SourceAsString(
5454
# define _PyOS_STACK_MARGIN_SHIFT (_PyOS_LOG2_STACK_MARGIN + 2)
5555
#endif
5656

57+
#ifdef _Py_THREAD_SANITIZER
58+
# define _PyOS_MIN_STACK_SIZE (_PyOS_STACK_MARGIN_BYTES * 6)
59+
#else
60+
# define _PyOS_MIN_STACK_SIZE (_PyOS_STACK_MARGIN_BYTES * 3)
61+
#endif
62+
5763

5864
#ifdef __cplusplus
5965
}

Modules/_testinternalcapi.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,9 +2425,8 @@ 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 + _PyOS_STACK_MARGIN_BYTES);
24292428
assert(ts->c_stack_top == (uintptr_t)start + size);
2430-
assert(ts->c_stack_soft_limit >= ts->c_stack_hard_limit);
2429+
assert(ts->c_stack_hard_limit <= ts->c_stack_soft_limit);
24312430
assert(ts->c_stack_soft_limit < ts->c_stack_top);
24322431
}
24332432

@@ -2443,12 +2442,13 @@ test_threadstate_set_stack(PyObject *self, PyObject *Py_UNUSED(args))
24432442
size_t init_top = ts->c_stack_init_top;
24442443

24452444
// Test the minimum stack size
2446-
size_t size = _PyOS_STACK_MARGIN_BYTES * 3;
2445+
size_t size = _PyOS_MIN_STACK_SIZE;
24472446
void *start = (void*)(_Py_get_machine_stack_pointer() - size);
24482447
check_threadstate_set_stack(tstate, start, size);
24492448

24502449
// Test a larger size
24512450
size = 7654321;
2451+
assert(size > _PyOS_MIN_STACK_SIZE);
24522452
start = (void*)(_Py_get_machine_stack_pointer() - size);
24532453
check_threadstate_set_stack(tstate, start, size);
24542454

Python/ceval.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ tstate_set_stack(PyThreadState *tstate,
487487
uintptr_t base, uintptr_t top)
488488
{
489489
assert(base < top);
490-
assert((top - base) >= (_PyOS_STACK_MARGIN_BYTES * 3));
490+
assert((top - base) >= _PyOS_MIN_STACK_SIZE);
491491

492492
#ifdef _Py_THREAD_SANITIZER
493493
// Thread sanitizer crashes if we use more than half the stack.
@@ -533,10 +533,10 @@ int
533533
PyUnstable_ThreadState_SetStack(PyThreadState *tstate,
534534
void *stack_start_addr, size_t stack_size)
535535
{
536-
if (stack_size < (_PyOS_STACK_MARGIN_BYTES * 3)) {
536+
if (stack_size < _PyOS_MIN_STACK_SIZE) {
537537
PyErr_Format(PyExc_ValueError,
538538
"stack_size must be at least %zu bytes",
539-
_PyOS_STACK_MARGIN_BYTES * 3);
539+
_PyOS_MIN_STACK_SIZE);
540540
return -1;
541541
}
542542

0 commit comments

Comments
 (0)