Skip to content

Conversation

@markshannon
Copy link
Member

@markshannon markshannon commented Nov 18, 2025

Only raises if the stack pointer is both below the limit and above the stack base. This prevents false positives for user-space threads, as the stack pointer will be outside those bounds if the stack has been swapped.

…out to overflow the stack.

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.
# endif
uintptr_t here_addr = _Py_get_machine_stack_pointer();
uintptr_t top_addr = _Py_SIZE_ROUND_UP(here_addr, 4096);
// Add some space for caller function then round to minimum page size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment.
Both “space for caller function” and “minimum page size” are guesses; what happens if they're wrong?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing bad. We only use the upper limit (for stack growing down) for reporting stack use in case of an overflow.
If we are wrong the report value will be off by a few kb, but it is already an estimate. We are assuming that there isn't much stack above the call to create the thread state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to avoid calculation, and use here_addr as is? With the Py_C_STACK_SIZE guesses, 4k indeed doesn't matter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a perfect guess, but I don't see any reason to make it worse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I see is unnecessary complexity: adding 60 bytes of faux precision to a guess that's in the order of megabytes is rather confusing. These look like more numbers to tweak if you get spurious overflow check failures

If this needs to go in, could you add a comment that this guess can be wildly wrong?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 18, 2025
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @encukou for commit 41e9404 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F141711%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 18, 2025
Copy link
Member

@efimov-mikhail efimov-mikhail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed some typos.

Python/ceval.c Outdated

/* The function _Py_EnterRecursiveCallTstate() only calls _Py_CheckRecursiveCall()
if the recursion_depth reaches recursion_limit. */
if the stack poineter is between the stack base and c_stack_hard_limit. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if the stack poineter is between the stack base and c_stack_hard_limit. */
if the stack pointer is between the stack base and c_stack_hard_limit. */

# endif
uintptr_t here_addr = _Py_get_machine_stack_pointer();
uintptr_t top_addr = _Py_SIZE_ROUND_UP(here_addr, 4096);
// Add some space for caller function then round to minimum page size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I see is unnecessary complexity: adding 60 bytes of faux precision to a guess that's in the order of megabytes is rather confusing. These look like more numbers to tweak if you get spurious overflow check failures

If this needs to go in, could you add a comment that this guess can be wildly wrong?

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@markshannon markshannon merged commit c25a070 into python:main Nov 19, 2025
52 checks passed
@miss-islington-app
Copy link

Thanks @markshannon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@markshannon markshannon deleted the conservative-stack-check branch November 19, 2025 10:16
@miss-islington-app
Copy link

Sorry, @markshannon, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker c25a070759952b13f97ecc37ca2991c2669aee47 3.14

hroncok pushed a commit to hroncok/cpython that referenced this pull request Nov 24, 2025
…ack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.

(cherry picked from commit c25a070)
@bedevere-app
Copy link

bedevere-app bot commented Nov 24, 2025

GH-141892 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Nov 24, 2025
encukou pushed a commit to encukou/cpython that referenced this pull request Nov 25, 2025
… the stack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.

Cherry-picked from commit c25a070
encukou pushed a commit to encukou/cpython that referenced this pull request Nov 25, 2025
… the stack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.

Cherry-picked from commit c25a070
@bedevere-app
Copy link

bedevere-app bot commented Nov 25, 2025

GH-141944 is a backport of this pull request to the 3.14 branch.

encukou added a commit that referenced this pull request Nov 26, 2025
…tack pointer is about to overflow the stack. (GH-141711) (GH-141944)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.

Cherry-picked from commit c25a070

Co-authored-by: Mark Shannon <[email protected]>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…ack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.
ashm-dev pushed a commit to ashm-dev/cpython that referenced this pull request Dec 8, 2025
…ack pointer is about to overflow the stack. (pythonGH-141711)

Only raises if the stack pointer is both below the limit *and* above the stack base.
This prevents false positives for user-space threads, as the stack pointer will be outside those bounds
if the stack has been swapped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants