Skip to content

Commit e55bde2

Browse files
authored
Add a stack pointer bounds check when configCHECK_FOR_STACK_OVERFLOW is set to 2. (#1216)
Add a stack pointer bounds check when configCHECK_FOR_STACK_OVERFLOW is set to 2.
1 parent f05244a commit e55bde2

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

include/stack_macros.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,20 @@
8888

8989
#if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )
9090

91-
#define taskCHECK_FOR_STACK_OVERFLOW() \
92-
do { \
93-
const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
94-
const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \
95-
\
96-
if( ( pulStack[ 0 ] != ulCheckValue ) || \
97-
( pulStack[ 1 ] != ulCheckValue ) || \
98-
( pulStack[ 2 ] != ulCheckValue ) || \
99-
( pulStack[ 3 ] != ulCheckValue ) ) \
100-
{ \
101-
char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
102-
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
103-
} \
91+
#define taskCHECK_FOR_STACK_OVERFLOW() \
92+
do { \
93+
const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
94+
const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \
95+
\
96+
if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
97+
( pulStack[ 0 ] != ulCheckValue ) || \
98+
( pulStack[ 1 ] != ulCheckValue ) || \
99+
( pulStack[ 2 ] != ulCheckValue ) || \
100+
( pulStack[ 3 ] != ulCheckValue ) ) \
101+
{ \
102+
char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
103+
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
104+
} \
104105
} while( 0 )
105106

106107
#endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
@@ -120,8 +121,8 @@
120121
\
121122
pcEndOfStack -= sizeof( ucExpectedStackBytes ); \
122123
\
123-
/* Has the extremity of the task stack ever been written over? */ \
124-
if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \
124+
if( ( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) || \
125+
( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) ) \
125126
{ \
126127
char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
127128
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \

0 commit comments

Comments
 (0)