Skip to content

Support TX_ENABLE_STACK_CHECKING for module threads - #820

Open
Tisya-Gupta wants to merge 4 commits into
eclipse-threadx:devfrom
Tisya-Gupta:feature/stack-checking-module-threads
Open

Tisya-Gupta wants to merge 4 commits into
eclipse-threadx:devfrom
Tisya-Gupta:feature/stack-checking-module-threads

Conversation

@Tisya-Gupta

@Tisya-Gupta Tisya-Gupta commented Oct 22, 2026 •

Copy link
Copy Markdown

Fixes #702

In this PR, I added stack checking support for module threads when TX_ENABLE_STACK_CHECKING is turned on. Module threads switch between a user stack and a kernel stack, but previously there was no separate pointer tracking the kernel stack and no guard word reserved at the end of the kernel stack buffer, which caused false stack overflow errors when switching between modes.

To solve this, I reserved space for the guard pattern at the end of the kernel stack buffer during thread creation and initialized its highest usage pointer. I used the existing tx_thread_module_reserved field in the thread control block to track the kernel stack usage, avoiding any need to touch assembly code. I also updated TX_THREAD_STACK_CHECK and _tx_thread_stack_analyze to check the right stack depending on whether the thread is currently running in kernel mode or user mode, and added unit tests to verify both user and kernel stack checks.

@Dev10-sys

Copy link
Copy Markdown

Hey @Tisya-Gupta, I reviewed and noticed a few quick things that need fixing.

Mainly, checking stack_start == kernel_stack_start in the macro breaks when TXM_MODULE_KERNEL_STACK_MAINTENANCE_DISABLE is defined, which causes false stack overflow errors. There's also a boundary issue where reading before stack_start happens when the stack is full, and the kernel guard word placement doesn't match the user stack convention. Also, per #702, we really need this tested on actual Cortex-M/A hardware rather than just the host mock test.

Lastly, please update the commit title to past tense (Added support...), use the Cause:, Fix:, Test result: format in the commit body, and drop the 2024 Microsoft copyright from the new test file.

Could you please push these updates? Thanks!

@fdesbiens

Copy link
Copy Markdown
Contributor

Hi @Tisya-Gupta.

Thank you for this contribution. @Dev10-sys's comments look accurate to me, but I will provide a comprehensive review next week.

@Tisya-Gupta

Copy link
Copy Markdown
Author

Thank you @fdesbiens and @Dev10-sys for the reviews. I will work on the requested changes....

Fixes eclipse-threadx#702

Cause:
User-mode module threads operate with two separate stacks: an unprivileged user stack
and a privileged kernel stack allocated by the module manager. When a module thread enters
the kernel, the scheduler swaps execution over to the kernel stack. Previously,
TX_ENABLE_STACK_CHECKING only tracked tx_thread_stack_highest_ptr against the active stack
without distinguishing between user and kernel mode. This caused cross-stack pointer
corruption, false positive stack overflow detections upon returning to user mode, and
potential out-of-bounds reads when highest_ptr equaled stack_start. Additionally, kernel
stack creation did not reserve a guard word at the end outside the usable stack boundary,
and MISRA-C mode stack check wrappers bypassed kernel stack verification entirely.

Fix:
- Added tx_thread_module_kernel_stack_highest_ptr alias mapping to tx_thread_module_reserved
  to track kernel stack usage independently without increasing control block size.
- Reserved an aligned guard word at tx_thread_module_kernel_stack_end outside usable stack
  space in txm_module_manager_thread_create.
- Updated TX_THREAD_STACK_CHECK (both standard and MISRA-C implementations) to select
  between kernel and user stacks based on current execution mode, supporting targets with
  TXM_MODULE_KERNEL_STACK_MAINTENANCE_DISABLE, and guarded against buffer under-reads when
  highest_ptr equals stack_start.
- Updated _tx_thread_stack_analyze to track execution stack state across interrupt
  restores and prevent cross-stack address comparisons during binary search.
- Added host unit test suite covering user mode, kernel mode, stack switching, guard
  corruption detection, overflow detection, maintenance-disabled mode, and boundary
  conditions.

Test result:
Module manager test suite passed all 10 unit tests (including 18 assertions in
threadx_module_manager_thread_stack_checking_test) cleanly with zero errors and zero warnings.

Signed-off-by: Tisya-Gupta <tisyagupta793@gmail.com>
@Tisya-Gupta
Tisya-Gupta force-pushed the feature/stack-checking-module-threads branch from b9d81c4 to ea37813 Compare October 24, 2026 14:52
The scheduler can retain a saved pointer for one stack while executing
on the other. Selecting the watermark from the current privilege mode
reported overflow during ordinary module service calls.

Matched saved pointers to the allocated ranges and checked each stack's
guards and watermark independently. Reused the bounded stack scan for
both stacks and used the same checker in MISRA builds.

Fourteen host tests and four Linux stack regressions passed. Cortex-M3/M4
passed 18 QEMU checks, including maintenance disabled, random fill, and
MISRA. GCC built Cortex-A7, Cortex-A35 UP/SMP, and Cortex-R52 managers.
Host coverage reached 95/95 changed lines and 76/78 branches. The two
untaken branches belong to the host interrupt shim. Silicon validation
is pending.

Assisted-by: Codex (GPT-6.2-Sol) <noreply@openai.com>
Signed-off-by: Frédéric Desbiens <frederic.desbiens@eclipse-foundation.org>
The Linux trace build enables thread creation trace hooks. The standalone
Module Manager stack test did not include their trace state or registration.

Linked the existing trace initialization and object registration sources
into the host test target.

GCC 14 built the Linux trace configuration and passed 127/127 tests. The
Module Manager stack test also passed in the default configuration.

Assisted-by: Codex (GPT-6.2-Sol) <noreply@openai.com>
@fdesbiens

Copy link
Copy Markdown
Contributor

Hi @Tisya-Gupta, I ran 37 QEMU tests on Cortex-M3, Cortex-M4, and Cortex-A7. They covered normal user/kernel stack switches and checked each guard on both stacks independently.

Could you test 283168b6 on a Cortex-M board with TX_ENABLE_STACK_CHECKING enabled? Please verify that normal module calls raise no false stack errors and that the stack-error callback reports a guard failure on each stack. Please share the board, toolchain, kernel stack size, and results. Which Cortex-A board and ThreadX port could you test for the follow-up?

The stack-checking PR and the Cortex-A35 regression added host tests
at the same location in the Module Manager CMake file.

Kept both test registrations and retained the trace sources that the
stack-checking test links in the trace build.

GCC 14 passed 136/136 Linux tests in the default build and 131/131 in
the trace build. Cortex-M3 and Cortex-M4 passed 16/16 QEMU tests with
stack checking and pointer context enabled, with and without module
kernel stack maintenance.

Assisted-by: Codex (GPT-6.2-Sol) <noreply@openai.com>
@fdesbiens

Copy link
Copy Markdown
Contributor

Hi @Tisya-Gupta, I updated #820 against dev and all 16 Cortex-M3 and M4 QEMU tests passed. Could you test 126cee25 on a Cortex-M board with TX_ENABLE_STACK_CHECKING enabled? Please check that normal module calls run without false stack errors and that a guard failure on each stack invokes the stack-error callback.

Please share the board, toolchain, and kernel stack size. Which Cortex-A board and ThreadX port could you test for the follow-up?

This branch has not been deployed

No deployments
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.

3 participants