Skip to content

Guarded the stack analyze binary search against a highest stack pointer that is not above the start of the stack, so a stack overflow or a corrupted control block no longer hangs or crashes the caller - #727

Open
fdesbiens wants to merge 1 commit into
eclipse-threadx:devfrom
fdesbiens:fix/issue-460

Conversation

@fdesbiens

Copy link
Copy Markdown
Contributor

Fixes #460

_tx_thread_stack_analyze computes the midpoint of the remaining stack with TX_ULONG_POINTER_DIF, which casts the pointer difference to ULONG. When tx_thread_stack_highest_ptr has already moved below tx_thread_stack_start, that difference is negative and wraps to a huge unsigned value. The probe pointer then lands far outside the stack, and the search never converges: the caller either hangs or faults, depending on what the out-of-range address maps to.

PR #464 stopped TX_THREAD_STACK_CHECK from calling into the analysis in that state, which covers the normal path. This change adds the safeguard inside _tx_thread_stack_analyze itself, as suggested by @billlamiework in the issue, so the function is also safe when it is called directly.

The highest stack pointer is now required to be strictly above the start of the stack before the binary search begins. The final scan for the first used word is also bounded by the initial highest stack pointer, so it cannot run past the end of the region if the fill pattern is intact all the way up.

Regression coverage was added for an inverted pair of stack pointers and for an equal pair. Without the source change the new test case crashes the suite, so it genuinely reproduces the defect.

This does not address why the pointers end up inconsistent in the first place. That is either a real stack overflow or memory corruption of the TX_THREAD control block, and it remains the application's problem to diagnose. What changes is that ThreadX now reports it through the stack error handler instead of hanging.

PR checklist

  • Updated function header with a short description and version number
  • Added test case for bug fix or new feature
  • Validated on real hardware

…er that is not above the start of the stack, so a stack overflow or a corrupted control block no longer hangs or crashes the caller

_tx_thread_stack_analyze computed the midpoint of the remaining stack with
TX_ULONG_POINTER_DIF, which casts the pointer difference to ULONG. When
tx_thread_stack_highest_ptr had already moved below tx_thread_stack_start,
the difference was negative and wrapped to a huge unsigned value, so the
binary search probed far outside the stack and never converged.

The highest stack pointer is now required to be above the start of the
stack before the search begins, and the final scan for the first used word
is bounded by the initial highest stack pointer so it cannot run past the
end of the region either.

Added regression coverage for an inverted and for an equal pair of stack
pointers. Without the fix the new test case crashes the suite.

Assisted-by: Copilot (Opus 5) <noreply@github.com>
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.

1 participant