Skip to content

Commit

Permalink
Fix MISRA_C_2012 rule 13.2 violation (#855)
Browse files Browse the repository at this point in the history
* Assign volatile variables to local non-volatile variables before read

* Fix stack macro overflow check volatile access

* Explicit the read order of volatile variable

* Fix issue : ISO C90 forbids mixed declarations and code



---------

Co-authored-by: Ubuntu <[email protected]>
Co-authored-by: chinglee-iot <[email protected]>
Co-authored-by: Monika Singh <[email protected]>
Co-authored-by: Gaurav-Aggarwal-AWS <[email protected]>
  • Loading branch information
5 people authored Dec 7, 2023
1 parent d1a0202 commit 55094e2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
60 changes: 32 additions & 28 deletions include/stack_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@
#if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) )

/* Only the current stack state is to be checked. */
#define taskCHECK_FOR_STACK_OVERFLOW() \
do { \
/* Is the currently saved stack pointer within the stack limit? */ \
if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \
{ \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
} \
#define taskCHECK_FOR_STACK_OVERFLOW() \
do { \
/* Is the currently saved stack pointer within the stack limit? */ \
if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \
{ \
char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
} \
} while( 0 )

#endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
Expand All @@ -71,33 +72,35 @@
#if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) )

/* Only the current stack state is to be checked. */
#define taskCHECK_FOR_STACK_OVERFLOW() \
do { \
\
/* Is the currently saved stack pointer within the stack limit? */ \
if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \
{ \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
} \
#define taskCHECK_FOR_STACK_OVERFLOW() \
do { \
\
/* Is the currently saved stack pointer within the stack limit? */ \
if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \
{ \
char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
} \
} while( 0 )

#endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
/*-----------------------------------------------------------*/

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

#define taskCHECK_FOR_STACK_OVERFLOW() \
do { \
const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \
\
if( ( pulStack[ 0 ] != ulCheckValue ) || \
( pulStack[ 1 ] != ulCheckValue ) || \
( pulStack[ 2 ] != ulCheckValue ) || \
( pulStack[ 3 ] != ulCheckValue ) ) \
{ \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
} \
#define taskCHECK_FOR_STACK_OVERFLOW() \
do { \
const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5; \
\
if( ( pulStack[ 0 ] != ulCheckValue ) || \
( pulStack[ 1 ] != ulCheckValue ) || \
( pulStack[ 2 ] != ulCheckValue ) || \
( pulStack[ 3 ] != ulCheckValue ) ) \
{ \
char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
} \
} while( 0 )

#endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
Expand All @@ -120,7 +123,8 @@
/* Has the extremity of the task stack ever been written over? */ \
if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \
{ \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
} \
} while( 0 )

Expand Down
20 changes: 13 additions & 7 deletions tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -3916,7 +3916,8 @@ void vTaskSuspendAll( void )
}
else
{
xReturn = xNextTaskUnblockTime - xTickCount;
xReturn = xNextTaskUnblockTime;
xReturn -= xTickCount;
}

return xReturn;
Expand Down Expand Up @@ -4549,14 +4550,17 @@ char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char

void vTaskStepTick( TickType_t xTicksToJump )
{
TickType_t xUpdatedTickCount;

traceENTER_vTaskStepTick( xTicksToJump );

/* Correct the tick count value after a period during which the tick
* was suppressed. Note this does *not* call the tick hook function for
* each stepped tick. */
configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
xUpdatedTickCount = xTickCount + xTicksToJump;
configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );

if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
if( xUpdatedTickCount == xNextTaskUnblockTime )
{
/* Arrange for xTickCount to reach xNextTaskUnblockTime in
* xTaskIncrementTick() when the scheduler resumes. This ensures
Expand Down Expand Up @@ -8450,6 +8454,8 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
{
TickType_t xTimeToWake;
const TickType_t xConstTickCount = xTickCount;
List_t * const pxDelayedList = pxDelayedTaskList;
List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;

#if ( INCLUDE_xTaskAbortDelay == 1 )
{
Expand Down Expand Up @@ -8497,14 +8503,14 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
/* Wake time has overflowed. Place this item in the overflow
* list. */
traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
}
else
{
/* The wake time has not overflowed, so the current block list
* is used. */
traceMOVED_TASK_TO_DELAYED_LIST();
vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );

/* If the task entering the blocked state was placed at the
* head of the list of blocked tasks then xNextTaskUnblockTime
Expand Down Expand Up @@ -8534,13 +8540,13 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
{
traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
/* Wake time has overflowed. Place this item in the overflow list. */
vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
}
else
{
traceMOVED_TASK_TO_DELAYED_LIST();
/* The wake time has not overflowed, so the current block list is used. */
vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );

/* If the task entering the blocked state was placed at the head of the
* list of blocked tasks then xNextTaskUnblockTime needs to be updated
Expand Down

0 comments on commit 55094e2

Please sign in to comment.