Skip to content

Commit 6b8ece0

Browse files
Add random number stack filling option. (#257)
Co-authored-by: TiejunZhou <[email protected]>
1 parent 6d9f25f commit 6b8ece0

File tree

13 files changed

+179
-12
lines changed

13 files changed

+179
-12
lines changed

common/inc/tx_api.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/* APPLICATION INTERFACE DEFINITION RELEASE */
2727
/* */
2828
/* tx_api.h PORTABLE C */
29-
/* 6.2.1 */
29+
/* 6.x */
3030
/* AUTHOR */
3131
/* */
3232
/* William E. Lamie, Microsoft Corporation */
@@ -97,6 +97,10 @@
9797
/* 03-08-2023 Tiejun Zhou Modified comment(s), */
9898
/* update patch number, */
9999
/* resulting in version 6.2.1 */
100+
/* xx-xx-xxxx Xiuwen Cai Modified comment(s), */
101+
/* added option for random */
102+
/* number stack filling, */
103+
/* resulting in version 6.x */
100104
/* */
101105
/**************************************************************************/
102106

@@ -171,7 +175,11 @@ extern "C" {
171175
#define TX_NO_MESSAGES ((UINT) 0)
172176
#define TX_EMPTY ((ULONG) 0)
173177
#define TX_CLEAR_ID ((ULONG) 0)
178+
#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING)
179+
#define TX_STACK_FILL (thread_ptr -> tx_thread_stack_fill_value)
180+
#else
174181
#define TX_STACK_FILL ((ULONG) 0xEFEFEFEFUL)
182+
#endif
175183

176184

177185
/* Thread execution state values. */
@@ -618,6 +626,12 @@ typedef struct TX_THREAD_STRUCT
618626
cleanup routine executes. */
619627
ULONG tx_thread_suspension_sequence;
620628

629+
#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING)
630+
631+
/* Define the random stack fill number. This can be used to detect stack overflow. */
632+
ULONG tx_thread_stack_fill_value;
633+
#endif
634+
621635
/* Define the user extension field. This typically is defined
622636
to white space, but some ports of ThreadX may need to have
623637
additional fields in the thread control block. This is
@@ -1892,6 +1906,21 @@ UINT _tx_trace_interrupt_control(UINT new_posture);
18921906
#endif
18931907

18941908

1909+
/* Add a default macro that can be re-defined in tx_port.h to add processing to the initialize random number generator.
1910+
By default, this is simply defined as whitespace. */
1911+
1912+
#ifndef TX_INITIALIZE_RANDOM_GENERATOR_INITIALIZATION
1913+
#define TX_INITIALIZE_RANDOM_GENERATOR_INITIALIZATION
1914+
#endif
1915+
1916+
1917+
/* Define the TX_RAND macro to the standard library function, if not already defined. */
1918+
1919+
#ifndef TX_RAND
1920+
#define TX_RAND() rand()
1921+
#endif
1922+
1923+
18951924
/* Check for MISRA compliance requirements. */
18961925

18971926
#ifdef TX_MISRA_ENABLE

common/inc/tx_user_sample.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/* PORT SPECIFIC C INFORMATION RELEASE */
2727
/* */
2828
/* tx_user.h PORTABLE C */
29-
/* 6.1.11 */
29+
/* 6.x */
3030
/* */
3131
/* AUTHOR */
3232
/* */
@@ -62,6 +62,10 @@
6262
/* optimized the definition of */
6363
/* TX_TIMER_TICKS_PER_SECOND, */
6464
/* resulting in version 6.1.11 */
65+
/* xx-xx-xxxx Xiuwen Cai Modified comment(s), */
66+
/* added option for random */
67+
/* number stack filling, */
68+
/* resulting in version 6.x */
6569
/* */
6670
/**************************************************************************/
6771

@@ -170,6 +174,14 @@
170174
#define TX_ENABLE_STACK_CHECKING
171175
*/
172176

177+
/* Determine if random number is used for stack filling. By default, ThreadX uses a fixed
178+
pattern for stack filling. When the following is defined, ThreadX uses a random number
179+
for stack filling. This is effective only when TX_ENABLE_STACK_CHECKING is defined. */
180+
181+
/*
182+
#define TX_ENABLE_RANDOM_NUMBER_STACK_FILLING
183+
*/
184+
173185
/* Determine if preemption-threshold should be disabled. By default, preemption-threshold is
174186
enabled. If the application does not use preemption-threshold, it may be disabled to reduce
175187
code size and improve performance. */

common/src/tx_initialize_kernel_enter.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TX_SAFETY_CRITICAL_EXCEPTION_HANDLER
4949
/* FUNCTION RELEASE */
5050
/* */
5151
/* _tx_initialize_kernel_enter PORTABLE C */
52-
/* 6.1.11 */
52+
/* 6.x */
5353
/* AUTHOR */
5454
/* */
5555
/* William E. Lamie, Microsoft Corporation */
@@ -93,6 +93,10 @@ TX_SAFETY_CRITICAL_EXCEPTION_HANDLER
9393
/* 04-25-2022 Scott Larson Modified comment(s), */
9494
/* added EPK initialization, */
9595
/* resulting in version 6.1.11 */
96+
/* xx-xx-xxxx Xiuwen Cai Modified comment(s), */
97+
/* added random generator */
98+
/* initialization, */
99+
/* resulting in version 6.x */
96100
/* */
97101
/**************************************************************************/
98102
VOID _tx_initialize_kernel_enter(VOID)
@@ -133,6 +137,9 @@ VOID _tx_initialize_kernel_enter(VOID)
133137
later used to represent interrupt nesting. */
134138
_tx_thread_system_state = TX_INITIALIZE_IN_PROGRESS;
135139

140+
/* Optional random number generator initialization. */
141+
TX_INITIALIZE_RANDOM_GENERATOR_INITIALIZATION
142+
136143
/* Call the application provided initialization function. Pass the
137144
first available memory address to it. */
138145
tx_application_define(_tx_initialize_unused_memory);

common/src/tx_thread_create.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/* FUNCTION RELEASE */
3737
/* */
3838
/* _tx_thread_create PORTABLE C */
39-
/* 6.1.8 */
39+
/* 6.x */
4040
/* AUTHOR */
4141
/* */
4242
/* William E. Lamie, Microsoft Corporation */
@@ -88,6 +88,10 @@
8888
/* supported TX_MISRA_ENABLE, */
8989
/* 08-02-2021 Scott Larson Removed unneeded cast, */
9090
/* resulting in version 6.1.8 */
91+
/* xx-xx-xxxx Xiuwen Cai Modified comment(s), */
92+
/* added option for random */
93+
/* number stack filling, */
94+
/* resulting in version 6.x */
9195
/* */
9296
/**************************************************************************/
9397
UINT _tx_thread_create(TX_THREAD *thread_ptr, CHAR *name_ptr, VOID (*entry_function)(ULONG id), ULONG entry_input,
@@ -109,6 +113,17 @@ ALIGN_TYPE updated_stack_start;
109113
#endif
110114

111115
#ifndef TX_DISABLE_STACK_FILLING
116+
#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING)
117+
118+
/* Initialize the stack fill value to a 8-bit random value. */
119+
thread_ptr -> tx_thread_stack_fill_value = ((ULONG) TX_RAND()) & 0xFFUL;
120+
121+
/* Duplicate the random value in each of the 4 bytes of the stack fill value. */
122+
thread_ptr -> tx_thread_stack_fill_value = thread_ptr -> tx_thread_stack_fill_value |
123+
(thread_ptr -> tx_thread_stack_fill_value << 8) |
124+
(thread_ptr -> tx_thread_stack_fill_value << 16) |
125+
(thread_ptr -> tx_thread_stack_fill_value << 24);
126+
#endif
112127

113128
/* Set the thread stack to a pattern prior to creating the initial
114129
stack frame. This pattern is used by the stack checking routines

common_modules/module_manager/src/txm_module_manager_thread_create.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/* FUNCTION RELEASE */
4040
/* */
4141
/* _txm_module_manager_thread_create PORTABLE C */
42-
/* 6.2.1 */
42+
/* 6.x */
4343
/* AUTHOR */
4444
/* */
4545
/* Scott Larson, Microsoft Corporation */
@@ -94,6 +94,10 @@
9494
/* 03-08-2023 Scott Larson Check module stack for */
9595
/* overlap, */
9696
/* resulting in version 6.2.1 */
97+
/* xx-xx-xxxx Xiuwen Cai Modified comment(s), */
98+
/* added option for random */
99+
/* number stack filling, */
100+
/* resulting in version 6.x */
97101
/* */
98102
/**************************************************************************/
99103
UINT _txm_module_manager_thread_create(TX_THREAD *thread_ptr, CHAR *name_ptr,
@@ -272,6 +276,17 @@ ULONG i;
272276
}
273277

274278
#ifndef TX_DISABLE_STACK_FILLING
279+
#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING)
280+
281+
/* Initialize the stack fill value to a 8-bit random value. */
282+
thread_ptr -> tx_thread_stack_fill_value = ((ULONG) TX_RAND()) & 0xFFUL;
283+
284+
/* Duplicate the random value in each of the 4 bytes of the stack fill value. */
285+
thread_ptr -> tx_thread_stack_fill_value = thread_ptr -> tx_thread_stack_fill_value |
286+
(thread_ptr -> tx_thread_stack_fill_value << 8) |
287+
(thread_ptr -> tx_thread_stack_fill_value << 16) |
288+
(thread_ptr -> tx_thread_stack_fill_value << 24);
289+
#endif
275290

276291
/* Set the thread stack to a pattern prior to creating the initial
277292
stack frame. This pattern is used by the stack checking routines

common_smp/inc/tx_api.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/* APPLICATION INTERFACE DEFINITION RELEASE */
2727
/* */
2828
/* tx_api.h PORTABLE SMP */
29-
/* 6.2.1 */
29+
/* 6.x */
3030
/* AUTHOR */
3131
/* */
3232
/* William E. Lamie, Microsoft Corporation */
@@ -85,6 +85,10 @@
8585
/* 03-08-2023 Tiejun Zhou Modified comment(s), */
8686
/* update patch number, */
8787
/* resulting in version 6.2.1 */
88+
/* xx-xx-xxxx Xiuwen Cai Modified comment(s), */
89+
/* added option for random */
90+
/* number stack filling, */
91+
/* resulting in version 6.x */
8892
/* */
8993
/**************************************************************************/
9094

@@ -172,7 +176,11 @@ extern "C" {
172176
#define TX_NO_MESSAGES ((UINT) 0)
173177
#define TX_EMPTY ((ULONG) 0)
174178
#define TX_CLEAR_ID ((ULONG) 0)
179+
#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING)
180+
#define TX_STACK_FILL (thread_ptr -> tx_thread_stack_fill_value)
181+
#else
175182
#define TX_STACK_FILL ((ULONG) 0xEFEFEFEFUL)
183+
#endif
176184

177185

178186
/* Thread execution state values. */
@@ -639,6 +647,12 @@ typedef struct TX_THREAD_STRUCT
639647
cleanup routine executes. */
640648
ULONG tx_thread_suspension_sequence;
641649

650+
#if defined(TX_ENABLE_RANDOM_NUMBER_STACK_FILLING) && defined(TX_ENABLE_STACK_CHECKING)
651+
652+
/* Define the random stack fill number. This can be used to detect stack overflow. */
653+
ULONG tx_thread_stack_fill_value;
654+
#endif
655+
642656
/* Define the user extension field. This typically is defined
643657
to white space, but some ports of ThreadX may need to have
644658
additional fields in the thread control block. This is
@@ -1886,6 +1900,21 @@ UINT _tx_trace_interrupt_control(UINT new_posture);
18861900
#endif
18871901

18881902

1903+
/* Add a default macro that can be re-defined in tx_port.h to add processing to the initialize random number generator.
1904+
By default, this is simply defined as whitespace. */
1905+
1906+
#ifndef TX_INITIALIZE_RANDOM_GENERATOR_INITIALIZATION
1907+
#define TX_INITIALIZE_RANDOM_GENERATOR_INITIALIZATION
1908+
#endif
1909+
1910+
1911+
/* Define the TX_RAND macro to the standard library function, if not already defined. */
1912+
1913+
#ifndef TX_RAND
1914+
#define TX_RAND() rand()
1915+
#endif
1916+
1917+
18891918
/* Check for MISRA compliance requirements. */
18901919

18911920
#ifdef TX_MISRA_ENABLE

common_smp/inc/tx_user_sample.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/* PORT SPECIFIC C INFORMATION RELEASE */
2727
/* */
2828
/* tx_user.h PORTABLE C */
29-
/* 6.1.11 */
29+
/* 6.x */
3030
/* */
3131
/* AUTHOR */
3232
/* */
@@ -62,6 +62,10 @@
6262
/* optimized the definition of */
6363
/* TX_TIMER_TICKS_PER_SECOND, */
6464
/* resulting in version 6.1.11 */
65+
/* xx-xx-xxxx Xiuwen Cai Modified comment(s), */
66+
/* added option for random */
67+
/* number stack filling, */
68+
/* resulting in version 6.x */
6569
/* */
6670
/**************************************************************************/
6771

@@ -170,6 +174,14 @@
170174
#define TX_ENABLE_STACK_CHECKING
171175
*/
172176

177+
/* Determine if random number is used for stack filling. By default, ThreadX uses a fixed
178+
pattern for stack filling. When the following is defined, ThreadX uses a random number
179+
for stack filling. This is effective only when TX_ENABLE_STACK_CHECKING is defined. */
180+
181+
/*
182+
#define TX_ENABLE_RANDOM_NUMBER_STACK_FILLING
183+
*/
184+
173185
/* Determine if preemption-threshold should be disabled. By default, preemption-threshold is
174186
enabled. If the application does not use preemption-threshold, it may be disabled to reduce
175187
code size and improve performance. */

common_smp/src/tx_initialize_kernel_enter.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ TX_SAFETY_CRITICAL_EXCEPTION_HANDLER
4747
/* FUNCTION RELEASE */
4848
/* */
4949
/* _tx_initialize_kernel_enter PORTABLE SMP */
50-
/* 6.1 */
50+
/* 6.x */
5151
/* AUTHOR */
5252
/* */
5353
/* William E. Lamie, Microsoft Corporation */
@@ -87,7 +87,11 @@ TX_SAFETY_CRITICAL_EXCEPTION_HANDLER
8787
/* */
8888
/* DATE NAME DESCRIPTION */
8989
/* */
90-
/* 09-30-2020 William E. Lamie Initial Version 6.1 */
90+
/* 09-30-2020 William E. Lamie Initial Version 6.1 */
91+
/* xx-xx-xxxx Xiuwen Cai Modified comment(s), */
92+
/* added random generator */
93+
/* initialization, */
94+
/* resulting in version 6.x */
9195
/* */
9296
/**************************************************************************/
9397
VOID _tx_initialize_kernel_enter(VOID)
@@ -134,6 +138,9 @@ ULONG other_core_status, i;
134138
later used to represent interrupt nesting. */
135139
_tx_thread_system_state[0] = TX_INITIALIZE_IN_PROGRESS;
136140

141+
/* Optional random number generator initialization. */
142+
TX_INITIALIZE_RANDOM_GENERATOR_INITIALIZATION
143+
137144
/* Call the application provided initialization function. Pass the
138145
first available memory address to it. */
139146
tx_application_define(_tx_initialize_unused_memory);

0 commit comments

Comments
 (0)