-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
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
8
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
14c66bd
gh-139653: Add PyUnstable_ThreadState_SetStack()
vstinner 5d157ff
Update the doc
vstinner b0d1cf3
Fix TSan: use larger minimum stack size
vstinner b8672c8
Update Doc/c-api/init.rst
vstinner 8290679
Update Doc/c-api/init.rst
vstinner b85c8b8
Mention Py_EnterRecursiveCall()
vstinner da8cbf0
Py_EnterRecursiveCall(): add seealso
vstinner 2f7b92e
Update Doc/c-api/init.rst
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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -439,7 +439,7 @@ int pthread_attr_destroy(pthread_attr_t *a) | |
| #endif | ||
|
|
||
| static void | ||
| hardware_stack_limits(uintptr_t *top, uintptr_t *base) | ||
| hardware_stack_limits(uintptr_t *base, uintptr_t *top) | ||
| { | ||
| #ifdef WIN32 | ||
| ULONG_PTR low, high; | ||
|
|
@@ -482,23 +482,86 @@ hardware_stack_limits(uintptr_t *top, uintptr_t *base) | |
| #endif | ||
| } | ||
|
|
||
| void | ||
| _Py_InitializeRecursionLimits(PyThreadState *tstate) | ||
| static void | ||
| tstate_set_stack(PyThreadState *tstate, | ||
| uintptr_t base, uintptr_t top) | ||
| { | ||
| uintptr_t top; | ||
| uintptr_t base; | ||
| hardware_stack_limits(&top, &base); | ||
| assert(base < top); | ||
| assert((top - base) >= _PyOS_MIN_STACK_SIZE); | ||
|
|
||
| #ifdef _Py_THREAD_SANITIZER | ||
| // Thread sanitizer crashes if we use more than half the stack. | ||
| uintptr_t stacksize = top - base; | ||
| base += stacksize/2; | ||
| base += stacksize / 2; | ||
| #endif | ||
| _PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate; | ||
| _tstate->c_stack_top = top; | ||
| _tstate->c_stack_hard_limit = base + _PyOS_STACK_MARGIN_BYTES; | ||
| _tstate->c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2; | ||
|
|
||
| #ifndef NDEBUG | ||
| // Sanity checks | ||
| _PyThreadStateImpl *ts = (_PyThreadStateImpl *)tstate; | ||
| assert(ts->c_stack_hard_limit <= ts->c_stack_soft_limit); | ||
|
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. Be aware that this is going to conflict with #140028 |
||
| assert(ts->c_stack_soft_limit < ts->c_stack_top); | ||
| #endif | ||
| } | ||
|
|
||
|
|
||
| void | ||
| _Py_InitializeRecursionLimits(PyThreadState *tstate) | ||
| { | ||
| uintptr_t base, top; | ||
| hardware_stack_limits(&base, &top); | ||
| assert(top != 0); | ||
|
|
||
| tstate_set_stack(tstate, base, top); | ||
| _PyThreadStateImpl *ts = (_PyThreadStateImpl *)tstate; | ||
| ts->c_stack_init_base = base; | ||
| ts->c_stack_init_top = top; | ||
|
|
||
| // Test the stack pointer | ||
| #if !defined(NDEBUG) && !defined(__wasi__) | ||
| 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 < _PyOS_MIN_STACK_SIZE) { | ||
| PyErr_Format(PyExc_ValueError, | ||
| "stack_size must be at least %zu bytes", | ||
| _PyOS_MIN_STACK_SIZE); | ||
| return -1; | ||
| } | ||
|
|
||
| uintptr_t base = (uintptr_t)stack_start_addr; | ||
| uintptr_t top = base + stack_size; | ||
| tstate_set_stack(tstate, base, top); | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
| void | ||
| PyUnstable_ThreadState_ResetStack(PyThreadState *tstate) | ||
| { | ||
| _PyThreadStateImpl *ts = (_PyThreadStateImpl *)tstate; | ||
| if (ts->c_stack_init_top != 0) { | ||
| tstate_set_stack(tstate, | ||
| ts->c_stack_init_base, | ||
| ts->c_stack_init_top); | ||
| return; | ||
| } | ||
|
|
||
| _Py_InitializeRecursionLimits(tstate); | ||
| } | ||
|
|
||
|
|
||
| /* 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
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.
Uh oh!
There was an error while loading. Please reload this page.