Skip to content

Commit 81382eb

Browse files
committed
Turn on main thread stack checking
Since the heap and stack are no longer shared, stack checking on the main thread can be turned back on. This allows stack overflows on the main thread to be caught quickly.
1 parent 1d3d01f commit 81382eb

File tree

3 files changed

+0
-47
lines changed

3 files changed

+0
-47
lines changed

rtos/rtx/TARGET_CORTEX_M/HAL_CM.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
9090
/* Task entry point. */
9191
p_TCB->ptask = task_body;
9292

93-
94-
#ifdef __MBED_CMSIS_RTOS_CM
95-
/* Set a magic word for checking of stack overflow.
96-
For the main thread (ID: MAIN_THREAD_ID) the stack is in a memory area shared with the
97-
heap, therefore the last word of the stack is a moving target.
98-
We want to do stack/heap collision detection instead.
99-
Similar applies to stack filling for the magic pattern.
100-
*/
101-
if (p_TCB->task_id != MAIN_THREAD_ID) {
102-
p_TCB->stack[0] = MAGIC_WORD;
103-
104-
/* Initialize stack with magic pattern. */
105-
if (os_stackinfo & 0x10000000U) {
106-
if (size > (16U+1U)) {
107-
for (i = ((size - 16U)/2U) - 1U; i; i--) {
108-
stk -= 2U;
109-
stk[1] = MAGIC_PATTERN;
110-
stk[0] = MAGIC_PATTERN;
111-
}
112-
if (--stk > p_TCB->stack) {
113-
*stk = MAGIC_PATTERN;
114-
}
115-
}
116-
}
117-
}
118-
#else
11993
/* Initialize stack with magic pattern. */
12094
if (os_stackinfo & 0x10000000U) {
12195
if (size > (16U+1U)) {
@@ -132,7 +106,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
132106

133107
/* Set a magic word for checking of stack overflow. */
134108
p_TCB->stack[0] = MAGIC_WORD;
135-
#endif
136109
}
137110

138111

rtos/rtx/TARGET_CORTEX_M/cmsis_os.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,6 @@
8080
#define OS_TIMERS 0
8181
#endif
8282

83-
/* If os timers macro is set to 0, there's no timer thread created, therefore
84-
* main thread has tid 0x01
85-
*/
86-
#if defined(OS_TIMERS) && (OS_TIMERS == 0)
87-
#define MAIN_THREAD_ID 0x01
88-
#else
89-
#define MAIN_THREAD_ID 0x02
90-
#endif
9183
#endif
9284

9385
#define DEFAULT_STACK_SIZE (WORDS_STACK_SIZE*4)

rtos/rtx/TARGET_CORTEX_M/rt_System.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,22 +313,10 @@ void rt_systick (void) {
313313
/*--------------------------- rt_stk_check ----------------------------------*/
314314

315315
__weak void rt_stk_check (void) {
316-
#ifdef __MBED_CMSIS_RTOS_CM
317-
/* Check for stack overflow. */
318-
if (os_tsk.run->task_id == MAIN_THREAD_ID) {
319-
// TODO: For the main thread the check should be done against the main heap pointer
320-
} else {
321-
if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
322-
(os_tsk.run->stack[0] != MAGIC_WORD)) {
323-
os_error (OS_ERR_STK_OVF);
324-
}
325-
}
326-
#else
327316
if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
328317
(os_tsk.run->stack[0] != MAGIC_WORD)) {
329318
os_error (OS_ERR_STK_OVF);
330319
}
331-
#endif
332320
}
333321

334322
/*----------------------------------------------------------------------------

0 commit comments

Comments
 (0)