summaryrefslogtreecommitdiff
path: root/common/src/tx_thread_create.c
diff options
context:
space:
mode:
authorFrédéric Desbiens <[email protected]>2026-09-15 16:24:58 -0400
committerGitHub <[email protected]>2026-09-15 16:24:58 -0400
commit30b3d22adceb10221de7ac3b4c4fd7ebcd3117e1 (patch)
tree39bc2cb5f232d6010f7b7f3e8f15e03aa8056390 /common/src/tx_thread_create.c
parent201cc046092a8751cdf88cc8614b7cf4a7595838 (diff)
Restored the random stack fill value cleared during thread creation (#732)
Fixes #723 With `TX_ENABLE_STACK_CHECKING` and `TX_ENABLE_RANDOM_NUMBER_STACK_FILLING` both enabled, `_tx_thread_create` picked a random byte, stored it in `tx_thread_stack_fill_value`, filled the stack with it -- and then cleared the whole control block with `TX_MEMSET`. The stack held the pattern while the control block claimed zero, so `TX_THREAD_STACK_CHECK` and `_tx_thread_stack_analyze` compared against the wrong value for the entire life of the thread. The value is now computed into a local and written back after the clear. The clear stays where it is, because the module manager's error checking walks the created list before the control block may be touched. Fixed in `common/src/tx_thread_create.c`, `common_smp/src/tx_thread_create.c` and `txm_module_manager_thread_create.c`, where it additionally left a user-mode module thread's kernel stack filled with zeros. `threadx_thread_stack_fill_value_test` creates sixteen unstarted threads and checks each control block against the pattern in its stack, tolerating a random zero byte without letting that hide the defect. Added to both suites: `ERROR #3` on `dev` in `stack_checking_rand_fill_build`, green with the fix. Five tx configurations at 104 tests, five SMP at 117, and all three sources clean under `-Wall -Wextra` across every combination of the four stack-filling switches. Assisted-by: Copilot (Opus 5) <[email protected]>
Diffstat (limited to 'common/src/tx_thread_create.c')
-rw-r--r--common/src/tx_thread_create.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/common/src/tx_thread_create.c b/common/src/tx_thread_create.c
index 026b0ada..37b4d3e2 100644
--- a/common/src/tx_thread_create.c
+++ b/common/src/tx_thread_create.c
@@ -9,6 +9,8 @@
* SPDX-License-Identifier: MIT
**************************************************************************/
+// Some portions generated by Copilot (Opus 5).
+
/**************************************************************************/
/**************************************************************************/
@@ -93,18 +95,26 @@ UCHAR *temp_ptr;
ALIGN_TYPE new_stack_start;
ALIGN_TYPE updated_stack_start;
#endif
+#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING) && !defined(TX_DISABLE_STACK_FILLING)
+ULONG stack_fill_value;
+#endif
#ifndef TX_DISABLE_STACK_FILLING
#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING)
/* Initialize the stack fill value to a 8-bit random value. */
- thread_ptr -> tx_thread_stack_fill_value = ((ULONG) TX_RAND()) & 0xFFUL;
+ stack_fill_value = ((ULONG) TX_RAND()) & 0xFFUL;
/* Duplicate the random value in each of the 4 bytes of the stack fill value. */
- thread_ptr -> tx_thread_stack_fill_value = thread_ptr -> tx_thread_stack_fill_value |
- (thread_ptr -> tx_thread_stack_fill_value << 8) |
- (thread_ptr -> tx_thread_stack_fill_value << 16) |
- (thread_ptr -> tx_thread_stack_fill_value << 24);
+ stack_fill_value = stack_fill_value |
+ (stack_fill_value << 8) |
+ (stack_fill_value << 16) |
+ (stack_fill_value << 24);
+
+ /* Store the fill value in the control block so that the stack fill below picks it up
+ through the TX_STACK_FILL macro. The control block is cleared further down in this
+ function, so the value is stored again once that has been done. */
+ thread_ptr -> tx_thread_stack_fill_value = stack_fill_value;
#endif
/* Set the thread stack to a pattern prior to creating the initial
@@ -150,6 +160,14 @@ ALIGN_TYPE updated_stack_start;
/* Initialize thread control block to all zeros. */
TX_MEMSET(thread_ptr, 0, (sizeof(TX_THREAD)));
+#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING) && !defined(TX_DISABLE_STACK_FILLING)
+
+ /* Clearing the control block reset the stack fill value, so store the value that was
+ actually used to fill the stack again. Otherwise the stack checking and stack analyze
+ routines would look for a pattern that is not the one present in the stack. */
+ thread_ptr -> tx_thread_stack_fill_value = stack_fill_value;
+#endif
+
/* Place the supplied parameters into the thread's control block. */
thread_ptr -> tx_thread_name = name_ptr;
thread_ptr -> tx_thread_entry = entry_function;