Skip to content

Commit 4cc256f

Browse files
committed
Set the minimum size to _PyOS_STACK_MARGIN_BYTES*3
1 parent 60e2d20 commit 4cc256f

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

Modules/_testinternalcapi.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,9 +2438,8 @@ test_threadstate_set_stack(PyObject *self, PyObject *Py_UNUSED(args))
24382438
PyThreadState *tstate = PyThreadState_GET();
24392439
assert(!PyErr_Occurred());
24402440

2441-
// Test a size smaller than _PyOS_STACK_MARGIN_BYTES
2442-
size_t size = 4096;
2443-
assert(size < _PyOS_STACK_MARGIN_BYTES);
2441+
// Test the minimum stack size
2442+
size_t size = _PyOS_STACK_MARGIN_BYTES * 3;
24442443
void *start = (void*)(_Py_get_machine_stack_pointer() - size);
24452444
check_threadstate_set_stack(tstate, start, size);
24462445

@@ -2449,6 +2448,13 @@ test_threadstate_set_stack(PyObject *self, PyObject *Py_UNUSED(args))
24492448
start = (void*)(_Py_get_machine_stack_pointer() - size);
24502449
check_threadstate_set_stack(tstate, start, size);
24512450

2451+
// Test invalid size (too small)
2452+
size = 5;
2453+
start = (void*)(_Py_get_machine_stack_pointer() - size);
2454+
assert(PyUnstable_ThreadState_SetStack(tstate, start, size) == -1);
2455+
assert(PyErr_ExceptionMatches(PyExc_ValueError));
2456+
PyErr_Clear();
2457+
24522458
PyUnstable_ThreadState_ResetStack(tstate);
24532459
Py_RETURN_NONE;
24542460
}

Python/ceval.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,19 @@ tstate_set_stack(PyThreadState *tstate,
444444
void *stack_start_addr, size_t stack_size)
445445
{
446446
assert(stack_size > 0);
447+
assert(stack_size >= (_PyOS_STACK_MARGIN_BYTES * 3));
447448

448449
_PyThreadStateImpl *ts = (_PyThreadStateImpl *)tstate;
449450
ts->c_stack_hard_limit = (uintptr_t)stack_start_addr;
450451
ts->c_stack_top = (uintptr_t)stack_start_addr + stack_size;
451452

452453
uintptr_t soft_limit = ts->c_stack_hard_limit;
453-
if (stack_size >= _PyOS_STACK_MARGIN_BYTES) {
454454
#ifdef _Py_THREAD_SANITIZER
455-
// Thread sanitizer crashes if we use a bit more than half the stack.
456-
soft_limit += (stack_size / 2);
455+
// Thread sanitizer crashes if we use a bit more than half the stack.
456+
soft_limit += (stack_size / 2);
457457
#else
458-
soft_limit += _PyOS_STACK_MARGIN_BYTES;
458+
soft_limit += _PyOS_STACK_MARGIN_BYTES;
459459
#endif
460-
}
461460
ts->c_stack_soft_limit = soft_limit;
462461

463462
// Sanity checks
@@ -477,8 +476,10 @@ int
477476
PyUnstable_ThreadState_SetStack(PyThreadState *tstate,
478477
void *stack_start_addr, size_t stack_size)
479478
{
480-
if (stack_size == 0) {
481-
PyErr_SetString(PyExc_ValueError, "stack_size must be greater than 0");
479+
if (stack_size < (_PyOS_STACK_MARGIN_BYTES * 3)) {
480+
PyErr_Format(PyExc_ValueError,
481+
"stack_size must be at least %zu bytes",
482+
_PyOS_STACK_MARGIN_BYTES * 3);
482483
return -1;
483484
}
484485

0 commit comments

Comments
 (0)