-
-
Notifications
You must be signed in to change notification settings - Fork 33k
gh-139653: Add PyUnstable_ThreadState_SetStack() #139668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vstinner
wants to merge
7
commits into
python:main
Choose a base branch
from
vstinner:setstack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−35
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a0b713
gh-139653: Add PyUnstable_ThreadState_SetStack()
vstinner bfc8619
Fix test_capi
vstinner ba0d3ed
Add What's New / NEWS entry
vstinner 1775754
PyUnstable_ThreadState_SetStack() can fail
vstinner a00a6f0
Move #endif
vstinner bdde91e
Fix make smelly
vstinner 60e2d20
Don't check the stack pointer on WASI
vstinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/C_API/2025-10-06-22-17-47.gh-issue-139653.6-1MOd.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Add :c:func:`PyUnstable_ThreadState_SetStack` and | ||
:c:func:`PyUnstable_ThreadState_ResetStack` functions to set the stack base | ||
address and stack size of a Python thread state. Patch by Victor Stinner. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,7 +351,7 @@ _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count) | |
return 0; | ||
} | ||
if (_tstate->c_stack_hard_limit == 0) { | ||
_Py_InitializeRecursionLimits(tstate); | ||
PyUnstable_ThreadState_ResetStack(tstate); | ||
} | ||
return here_addr <= _tstate->c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES; | ||
} | ||
|
@@ -439,35 +439,83 @@ int pthread_attr_destroy(pthread_attr_t *a) | |
#endif | ||
|
||
|
||
static void | ||
tstate_set_stack(PyThreadState *tstate, | ||
void *stack_start_addr, size_t stack_size) | ||
{ | ||
assert(stack_size > 0); | ||
|
||
_PyThreadStateImpl *ts = (_PyThreadStateImpl *)tstate; | ||
ts->c_stack_hard_limit = (uintptr_t)stack_start_addr; | ||
ts->c_stack_top = (uintptr_t)stack_start_addr + stack_size; | ||
|
||
uintptr_t soft_limit = ts->c_stack_hard_limit; | ||
if (stack_size >= _PyOS_STACK_MARGIN_BYTES) { | ||
#ifdef _Py_THREAD_SANITIZER | ||
// Thread sanitizer crashes if we use a bit more than half the stack. | ||
soft_limit += (stack_size / 2); | ||
#else | ||
soft_limit += _PyOS_STACK_MARGIN_BYTES; | ||
#endif | ||
} | ||
ts->c_stack_soft_limit = soft_limit; | ||
|
||
// Sanity checks | ||
assert(ts->c_stack_hard_limit <= ts->c_stack_soft_limit); | ||
assert(ts->c_stack_soft_limit < ts->c_stack_top); | ||
|
||
// Test the stack pointer | ||
#if !defined(NDEBUG) && !defined(__wasi__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These assertions fail on WASI but I don't have time right now to investigate why. |
||
uintptr_t here_addr = _Py_get_machine_stack_pointer(); | ||
assert(ts->c_stack_soft_limit < here_addr); | ||
assert(here_addr < ts->c_stack_top); | ||
#endif | ||
} | ||
|
||
|
||
int | ||
PyUnstable_ThreadState_SetStack(PyThreadState *tstate, | ||
void *stack_start_addr, size_t stack_size) | ||
{ | ||
if (stack_size == 0) { | ||
PyErr_SetString(PyExc_ValueError, "stack_size must be greater than 0"); | ||
return -1; | ||
} | ||
|
||
tstate_set_stack(tstate, stack_start_addr, stack_size); | ||
return 0; | ||
} | ||
|
||
|
||
void | ||
_Py_InitializeRecursionLimits(PyThreadState *tstate) | ||
PyUnstable_ThreadState_ResetStack(PyThreadState *tstate) | ||
{ | ||
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate; | ||
#ifdef WIN32 | ||
ULONG_PTR low, high; | ||
GetCurrentThreadStackLimits(&low, &high); | ||
_tstate->c_stack_top = (uintptr_t)high; | ||
|
||
ULONG guarantee = 0; | ||
SetThreadStackGuarantee(&guarantee); | ||
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + _PyOS_STACK_MARGIN_BYTES; | ||
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES; | ||
|
||
uintptr_t start = (uintptr_t)low + guarantee + _PyOS_STACK_MARGIN_BYTES; | ||
size_t size = (uintptr_t)high - start; | ||
tstate_set_stack(tstate, (void*)start, size); | ||
|
||
#elif defined(__APPLE__) | ||
pthread_t this_thread = pthread_self(); | ||
void *stack_addr = pthread_get_stackaddr_np(this_thread); // top of the stack | ||
size_t stack_size = pthread_get_stacksize_np(this_thread); | ||
_tstate->c_stack_top = (uintptr_t)stack_addr; | ||
_tstate->c_stack_hard_limit = _tstate->c_stack_top - stack_size; | ||
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES; | ||
void *top = pthread_get_stackaddr_np(this_thread); // top of the stack | ||
size_t size = pthread_get_stacksize_np(this_thread); | ||
tstate_set_stack(tstate, (char*)top - size, size); | ||
|
||
#else | ||
uintptr_t here_addr = _Py_get_machine_stack_pointer(); | ||
/// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size | ||
/// (on alpine at least) is much smaller than expected and imposes undue limits | ||
/// compared to the old stack size estimation. (We assume musl is not glibc.) | ||
// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size | ||
// (on alpine at least) is much smaller than expected and imposes undue limits | ||
// compared to the old stack size estimation. (We assume musl is not glibc.) | ||
# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \ | ||
!defined(__NetBSD__) && (defined(__GLIBC__) || !defined(__linux__)) | ||
size_t stack_size, guard_size; | ||
void *stack_addr; | ||
pthread_attr_t attr; | ||
size_t guard_size, stack_size; | ||
void *stack_addr; | ||
int err = pthread_getattr_np(pthread_self(), &attr); | ||
if (err == 0) { | ||
err = pthread_attr_getguardsize(&attr, &guard_size); | ||
|
@@ -476,25 +524,23 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate) | |
} | ||
if (err == 0) { | ||
uintptr_t base = ((uintptr_t)stack_addr) + guard_size; | ||
_tstate->c_stack_top = base + stack_size; | ||
#ifdef _Py_THREAD_SANITIZER | ||
// Thread sanitizer crashes if we use a bit more than half the stack. | ||
_tstate->c_stack_soft_limit = base + (stack_size / 2); | ||
#else | ||
_tstate->c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2; | ||
#endif | ||
_tstate->c_stack_hard_limit = base + _PyOS_STACK_MARGIN_BYTES; | ||
assert(_tstate->c_stack_soft_limit < here_addr); | ||
assert(here_addr < _tstate->c_stack_top); | ||
return; | ||
uintptr_t start = base + _PyOS_STACK_MARGIN_BYTES; | ||
size_t pystack_size = (base + stack_size) - start; | ||
tstate_set_stack(tstate, (void*)start, pystack_size); | ||
} | ||
else | ||
# endif | ||
_tstate->c_stack_top = _Py_SIZE_ROUND_UP(here_addr, 4096); | ||
_tstate->c_stack_soft_limit = _tstate->c_stack_top - Py_C_STACK_SIZE; | ||
_tstate->c_stack_hard_limit = _tstate->c_stack_top - (Py_C_STACK_SIZE + _PyOS_STACK_MARGIN_BYTES); | ||
{ | ||
uintptr_t here_addr = _Py_get_machine_stack_pointer(); | ||
uintptr_t top = _Py_SIZE_ROUND_UP(here_addr, 4096); | ||
uintptr_t start = top - (Py_C_STACK_SIZE + _PyOS_STACK_MARGIN_BYTES); | ||
size_t pystack_size = top - start; | ||
tstate_set_stack(tstate, (void*)start, pystack_size); | ||
} | ||
#endif | ||
} | ||
|
||
|
||
/* The function _Py_EnterRecursiveCallTstate() only calls _Py_CheckRecursiveCall() | ||
if the recursion_depth reaches recursion_limit. */ | ||
int | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure of this name. Maybe
PyUnstable_ThreadState_InitStack()
would be more explicit?