Repository: FreeRTOS/FreeRTOS-Kernel
Pull request #1270: FreeRTOS SMP: direct access to current TCB inside stack macros
Author: login: Matth9814
Created: 2025-04-26T10:18:45Z | Merged: 2025-04-30T07:42:09Z

<!--- Title -->
Fix access to current TCB inside the stack macros in SMP

Description
-----------
The problem is already described [here](https://github.com/FreeRTOS/FreeRTOS-Kernel/issues/1269).

Checklist:
----------
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [ ] I have tested my changes. No regression in existing tests.
- [ ] I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue
-----------
[#1269](https://github.com/FreeRTOS/FreeRTOS-Kernel/issues/1269)


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.



Diff:
diff --git a/include/stack_macros.h b/include/stack_macros.h
index 6d0117722af..8bf0720c9e6 100644
--- a/include/stack_macros.h
+++ b/include/stack_macros.h
@@ -66,8 +66,10 @@
  */
 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
+    #if ( configNUMBER_OF_CORES == 1 )
+
 /* Only the current stack state is to be checked. */
-    #define taskCHECK_FOR_STACK_OVERFLOW()                                                      \
+        #define taskCHECK_FOR_STACK_OVERFLOW()                                                  \
     do                                                                                          \
     {                                                                                           \
         /* Is the currently saved stack pointer within the stack limit? */                      \
@@ -78,13 +80,33 @@
         }                                                                                       \
     } while( 0 )
 
+    #else /* if ( configNUMBER_OF_CORES == 1 ) */
+
+/* Only the current stack state is to be checked. */
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                  \
+    do                                                                                   \
+    {                                                                                    \
+        TCB_t * pxTCB = pxCurrentTCBs[ xCoreID ];                                        \
+                                                                                         \
+        /* Is the currently saved stack pointer within the stack limit? */               \
+        if( pxTCB->pxTopOfStack <= pxTCB->pxStack + portSTACK_LIMIT_PADDING )            \
+        {                                                                                \
+            char * pcOverflowTaskName = pxTCB->pcTaskName;                               \
+            vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName ); \
+        }                                                                                \
+    } while( 0 )
+
+    #endif /* if ( configNUMBER_OF_CORES == 1 ) */
+
 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
+    #if ( configNUMBER_OF_CORES == 1 )
+
 /* Only the current stack state is to be checked. */
-    #define taskCHECK_FOR_STACK_OVERFLOW()                                                       \
+        #define taskCHECK_FOR_STACK_OVERFLOW()                                                   \
     do                                                                                           \
     {                                                                                            \
         /* Is the currently saved stack pointer within the stack limit? */                       \
@@ -95,12 +117,32 @@
         }                                                                                        \
     } while( 0 )
 
+    #else /* if ( configNUMBER_OF_CORES == 1 ) */
+
+/* Only the current stack state is to be checked. */
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                  \
+    do                                                                                   \
+    {                                                                                    \
+        TCB_t * pxTCB = pxCurrentTCBs[ xCoreID ];                                        \
+                                                                                         \
+        /* Is the currently saved stack pointer within the stack limit? */               \
+        if( pxTCB->pxTopOfStack >= pxTCB->pxEndOfStack - portSTACK_LIMIT_PADDING )       \
+        {                                                                                \
+            char * pcOverflowTaskName = pxTCB->pcTaskName;                               \
+            vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName ); \
+        }                                                                                \
+    } while( 0 )
+
+    #endif /* if ( configNUMBER_OF_CORES == 1 ) */
+
 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-    #define taskCHECK_FOR_STACK_OVERFLOW()                                                       \
+    #if ( configNUMBER_OF_CORES == 1 )
+
+        #define taskCHECK_FOR_STACK_OVERFLOW()                                                   \
     do                                                                                           \
     {                                                                                            \
         const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                  \
@@ -117,12 +159,36 @@
         }                                                                                        \
     } while( 0 )
 
+    #else /* if ( configNUMBER_OF_CORES == 1 ) */
+
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                  \
+    do                                                                                   \
+    {                                                                                    \
+        TCB_t * pxTCB = pxCurrentTCBs[ xCoreID ];                                        \
+        const uint32_t * const pulStack = ( uint32_t * ) pxTCB->pxStack;                 \
+        const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                          \
+                                                                                         \
+        if( ( pxTCB->pxTopOfStack <= pxTCB->pxStack + portSTACK_LIMIT_PADDING ) ||       \
+            ( pulStack[ 0 ] != ulCheckValue ) ||                                         \
+            ( pulStack[ 1 ] != ulCheckValue ) ||                                         \
+            ( pulStack[ 2 ] != ulCheckValue ) ||                                         \
+            ( pulStack[ 3 ] != ulCheckValue ) )                                          \
+        {                                                                                \
+            char * pcOverflowTaskName = pxTCB->pcTaskName;                               \
+            vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName ); \
+        }                                                                                \
+    } while( 0 )
+
+    #endif /* if ( configNUMBER_OF_CORES == 1 ) */
+
 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-    #define taskCHECK_FOR_STACK_OVERFLOW()                                                                                                \
+    #if ( configNUMBER_OF_CORES == 1 )
+
+        #define taskCHECK_FOR_STACK_OVERFLOW()                                                                                            \
     do                                                                                                                                    \
     {                                                                                                                                     \
         int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack;                                                                  \
@@ -142,12 +208,41 @@
         }                                                                                                                                 \
     } while( 0 )
 
+    #else /* if ( configNUMBER_OF_CORES == 1 ) */
+
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                                                                   \
+    do                                                                                                                                    \
+    {                                                                                                                                     \
+        TCB_t * pxTCB = pxCurrentTCBs[ xCoreID ];                                                                                         \
+        int8_t * pcEndOfStack = ( int8_t * ) pxTCB->pxEndOfStack;                                                                         \
+        static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                        tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                        tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                        tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                        tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
+                                                                                                                                          \
+        pcEndOfStack -= sizeof( ucExpectedStackBytes );                                                                                   \
+                                                                                                                                          \
+        if( ( pxTCB->pxTopOfStack >= pxTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) ||                                                   \
+            ( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) )                 \
+        {                                                                                                                                 \
+            char * pcOverflowTaskName = pxTCB->pcTaskName;                                                                                \
+            vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName );                                                  \
+        }                                                                                                                                 \
+    } while( 0 )
+
+    #endif /* if ( configNUMBER_OF_CORES == 1 ) */
+
 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
 /*-----------------------------------------------------------*/
 
 /* Remove stack overflow macro if not being used. */
 #ifndef taskCHECK_FOR_STACK_OVERFLOW
-    #define taskCHECK_FOR_STACK_OVERFLOW()
+    #if ( configNUMBER_OF_CORES == 1 )
+        #define taskCHECK_FOR_STACK_OVERFLOW()
+    #else
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )
+    #endif
 #endif
 
 
diff --git a/tasks.c b/tasks.c
index 24cfb2620a2..e5d2c97c95f 100644
--- a/tasks.c
+++ b/tasks.c
@@ -5251,7 +5251,7 @@ BaseType_t xTaskIncrementTick( void )
                 #endif /* configGENERATE_RUN_TIME_STATS */
 
                 /* Check for stack overflow, if configured. */
-                taskCHECK_FOR_STACK_OVERFLOW();
+                taskCHECK_FOR_STACK_OVERFLOW( xCoreID );
 
                 /* Before the currently running task is switched out, save its errno. */
                 #if ( configUSE_POSIX_ERRNO == 1 )


Reviews:
user: login: jefftenney
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-26T23:22:29Z
user: login: jasonpcarroll
body: Looks good to me. Thank you!
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-04-28T21:08:14Z
user: login: jasonpcarroll
body: Could you fix failing CI checks?
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-04-28T21:09:07Z
user: login: aggarg
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-30T06:06:46Z
user: login: aggarg
state: DISMISSED
author_association: MEMBER
submitted_at: 2025-04-30T06:07:52Z
user: login: aggarg
state: DISMISSED
author_association: MEMBER
submitted_at: 2025-04-30T06:41:40Z
user: login: kar-rahul-aws
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-04-30T06:42:28Z
user: login: aggarg
state: APPROVED
author_association: MEMBER
submitted_at: 2025-04-30T07:30:52Z
user: login: kar-rahul-aws
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-04-30T07:32:01Z

Review comments:
diff_hunk: @@ -66,81 +66,172 @@
  */
 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-/* 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 )     \
-        {                                                                                       \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
-        }                                                                                       \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        /* 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 )     \
+            {                                                                                       \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
+            }                                                                                       \
+        } while( 0 )
+
+    #else
+
+            /* Only the current stack state is to be checked. */
+            #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+            do                                                                                      \
+            {                                                                                       \
+                const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
+                                                                                                    \
+                /* Is the currently saved stack pointer within the stack limit? */                  \
+                if( pxTCB->pxTopOfStack <= pxTCB->pxStack + portSTACK_LIMIT_PADDING )               \
+                {                                                                                   \
+                    char * pcOverflowTaskName = pxTCB->pcTaskName;                                  \
+                    vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName );    \
+                }                                                                                   \
+            } while( 0 )
+
+    #endif
 
 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-/* 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 ) \
-        {                                                                                        \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
-        }                                                                                        \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        /* 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 ) \
+            {                                                                                        \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
+            }                                                                                        \
+        } while( 0 )
+
+    #else
+
+        /* Only the current stack state is to be checked. */
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+        do                                                                                      \
+        {                                                                                       \
+            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
+                                                                                                \
+            /* Is the currently saved stack pointer within the stack limit? */                  \
+            if( pxTCB->pxTopOfStack >= pxTCB->pxEndOfStack - portSTACK_LIMIT_PADDING )          \
+            {                                                                                   \
+                char * pcOverflowTaskName = pxTCB->pcTaskName;                                  \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName );    \
+            }                                                                                   \
+        } while( 0 )
+
+    #endif
 
 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-    #define taskCHECK_FOR_STACK_OVERFLOW()                                                       \
-    do                                                                                           \
-    {                                                                                            \
-        const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                  \
-        const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                                  \
-                                                                                                 \
-        if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
-            ( pulStack[ 0 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 1 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 2 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 3 ] != ulCheckValue ) )                                                  \
-        {                                                                                        \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
-        }                                                                                        \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        #define taskCHECK_FOR_STACK_OVERFLOW()                                                       \
+        do                                                                                           \
+        {                                                                                            \
+            const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                  \
+            const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                                  \
+                                                                                                     \
+            if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
+                ( pulStack[ 0 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 1 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 2 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 3 ] != ulCheckValue ) )                                                  \
+            {                                                                                        \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
+            }                                                                                        \
+        } while( 0 )
+
+    #else
+
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+        do                                                                                      \
+        {                                                                                       \
+            const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;             \
+            const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                             \
+            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
path: include/stack_macros.h
user: login: jefftenney
body: Should we set `pulStack` from `pxTCB` instead of `pxCurrentTCB`?

```suggestion
            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
            const uint32_t * const pulStack = ( uint32_t * ) pxTCB->pxStack;                    \
            const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                             \
```
created_at: 2025-04-26T23:22:05Z
author_association: CONTRIBUTOR
diff_hunk: @@ -66,81 +66,172 @@
  */
 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-/* 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 )     \
-        {                                                                                       \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
-        }                                                                                       \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        /* 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 )     \
+            {                                                                                       \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
+            }                                                                                       \
+        } while( 0 )
+
+    #else
+
+            /* Only the current stack state is to be checked. */
+            #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+            do                                                                                      \
+            {                                                                                       \
+                const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
+                                                                                                    \
+                /* Is the currently saved stack pointer within the stack limit? */                  \
+                if( pxTCB->pxTopOfStack <= pxTCB->pxStack + portSTACK_LIMIT_PADDING )               \
+                {                                                                                   \
+                    char * pcOverflowTaskName = pxTCB->pcTaskName;                                  \
+                    vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName );    \
+                }                                                                                   \
+            } while( 0 )
+
+    #endif
 
 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-/* 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 ) \
-        {                                                                                        \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
-        }                                                                                        \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        /* 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 ) \
+            {                                                                                        \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
+            }                                                                                        \
+        } while( 0 )
+
+    #else
+
+        /* Only the current stack state is to be checked. */
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+        do                                                                                      \
+        {                                                                                       \
+            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
+                                                                                                \
+            /* Is the currently saved stack pointer within the stack limit? */                  \
+            if( pxTCB->pxTopOfStack >= pxTCB->pxEndOfStack - portSTACK_LIMIT_PADDING )          \
+            {                                                                                   \
+                char * pcOverflowTaskName = pxTCB->pcTaskName;                                  \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName );    \
+            }                                                                                   \
+        } while( 0 )
+
+    #endif
 
 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-    #define taskCHECK_FOR_STACK_OVERFLOW()                                                       \
-    do                                                                                           \
-    {                                                                                            \
-        const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                  \
-        const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                                  \
-                                                                                                 \
-        if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
-            ( pulStack[ 0 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 1 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 2 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 3 ] != ulCheckValue ) )                                                  \
-        {                                                                                        \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
-        }                                                                                        \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        #define taskCHECK_FOR_STACK_OVERFLOW()                                                       \
+        do                                                                                           \
+        {                                                                                            \
+            const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                  \
+            const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                                  \
+                                                                                                     \
+            if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
+                ( pulStack[ 0 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 1 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 2 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 3 ] != ulCheckValue ) )                                                  \
+            {                                                                                        \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
+            }                                                                                        \
+        } while( 0 )
+
+    #else
+
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+        do                                                                                      \
+        {                                                                                       \
+            const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;             \
+            const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                             \
+            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
+                                                                                                \
+            if( ( pxTCB->pxTopOfStack <= pxTCB->pxStack + portSTACK_LIMIT_PADDING ) ||          \
+                ( pulStack[ 0 ] != ulCheckValue ) ||                                            \
+                ( pulStack[ 1 ] != ulCheckValue ) ||                                            \
+                ( pulStack[ 2 ] != ulCheckValue ) ||                                            \
+                ( pulStack[ 3 ] != ulCheckValue ) )                                             \
+            {                                                                                   \
+                char * pcOverflowTaskName = pxTCB->pcTaskName;                                  \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName );    \
+            }                                                                                   \
+        } while( 0 )
+
+    #endif
 
 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-    #define taskCHECK_FOR_STACK_OVERFLOW()                                                                                                \
-    do                                                                                                                                    \
-    {                                                                                                                                     \
-        int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack;                                                                  \
-        static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
-                                                        tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
-                                                        tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
-                                                        tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
-                                                        tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
-                                                                                                                                          \
-        pcEndOfStack -= sizeof( ucExpectedStackBytes );                                                                                   \
-                                                                                                                                          \
-        if( ( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) ||                                     \
-            ( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) )                 \
-        {                                                                                                                                 \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                                                         \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );                                           \
-        }                                                                                                                                 \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        #define taskCHECK_FOR_STACK_OVERFLOW()                                                                                                \
+        do                                                                                                                                    \
+        {                                                                                                                                     \
+            int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack;                                                                  \
+            static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
+                                                                                                                                              \
+            pcEndOfStack -= sizeof( ucExpectedStackBytes );                                                                                   \
+                                                                                                                                              \
+            if( ( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) ||                                     \
+                ( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) )                 \
+            {                                                                                                                                 \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                                                         \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );                                           \
+            }                                                                                                                                 \
+        } while( 0 )
+
+    #else
+
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                                                                       \
+        do                                                                                                                                    \
+        {                                                                                                                                     \
+            int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack;                                                                  \
+            static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
+                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
+            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                                                                             \
path: include/stack_macros.h
user: login: jefftenney
body: Should we set `pcEndOfStack` from `pxTCB` instead of `pxCurrentTCB`?

```suggestion
            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                                                                             \
            int8_t * pcEndOfStack = ( int8_t * ) pxTCB->pxEndOfStack;                                                                         \
            static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \
                                                            tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
```
created_at: 2025-04-26T23:22:11Z
author_association: CONTRIBUTOR
diff_hunk: @@ -66,81 +66,172 @@
  */
 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-/* 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 )     \
-        {                                                                                       \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
-        }                                                                                       \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        /* 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 )     \
+            {                                                                                       \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                               \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
+            }                                                                                       \
+        } while( 0 )
+
+    #else
+
+            /* Only the current stack state is to be checked. */
+            #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+            do                                                                                      \
+            {                                                                                       \
+                const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
+                                                                                                    \
+                /* Is the currently saved stack pointer within the stack limit? */                  \
+                if( pxTCB->pxTopOfStack <= pxTCB->pxStack + portSTACK_LIMIT_PADDING )               \
+                {                                                                                   \
+                    char * pcOverflowTaskName = pxTCB->pcTaskName;                                  \
+                    vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName );    \
+                }                                                                                   \
+            } while( 0 )
+
+    #endif
 
 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-/* 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 ) \
-        {                                                                                        \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
-        }                                                                                        \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        /* 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 ) \
+            {                                                                                        \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
+            }                                                                                        \
+        } while( 0 )
+
+    #else
+
+        /* Only the current stack state is to be checked. */
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+        do                                                                                      \
+        {                                                                                       \
+            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
+                                                                                                \
+            /* Is the currently saved stack pointer within the stack limit? */                  \
+            if( pxTCB->pxTopOfStack >= pxTCB->pxEndOfStack - portSTACK_LIMIT_PADDING )          \
+            {                                                                                   \
+                char * pcOverflowTaskName = pxTCB->pcTaskName;                                  \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName );    \
+            }                                                                                   \
+        } while( 0 )
+
+    #endif
 
 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
 /*-----------------------------------------------------------*/
 
 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
 
-    #define taskCHECK_FOR_STACK_OVERFLOW()                                                       \
-    do                                                                                           \
-    {                                                                                            \
-        const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                  \
-        const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                                  \
-                                                                                                 \
-        if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
-            ( pulStack[ 0 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 1 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 2 ] != ulCheckValue ) ||                                                 \
-            ( pulStack[ 3 ] != ulCheckValue ) )                                                  \
-        {                                                                                        \
-            char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
-            vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
-        }                                                                                        \
-    } while( 0 )
+    #if( configNUMBER_OF_CORES == 1 )
+
+        #define taskCHECK_FOR_STACK_OVERFLOW()                                                       \
+        do                                                                                           \
+        {                                                                                            \
+            const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                  \
+            const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                                  \
+                                                                                                     \
+            if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
+                ( pulStack[ 0 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 1 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 2 ] != ulCheckValue ) ||                                                 \
+                ( pulStack[ 3 ] != ulCheckValue ) )                                                  \
+            {                                                                                        \
+                char * pcOverflowTaskName = pxCurrentTCB->pcTaskName;                                \
+                vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName );  \
+            }                                                                                        \
+        } while( 0 )
+
+    #else
+
+        #define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )                                         \
+        do                                                                                      \
+        {                                                                                       \
+            const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;             \
+            const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U;                             \
+            const TCB_t * const pxTCB = pxCurrentTCBs[ xCoreID ];                               \
path: include/stack_macros.h
user: login: aggarg
body: Thank you for catching this @jefftenney !
created_at: 2025-04-30T06:06:46Z
author_association: MEMBER

PR comments:
user: login: sonarqubecloud[bot]
created_at: 2025-04-30T07:22:25Z
body: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=FreeRTOS_FreeRTOS-Kernel&pullRequest=1270) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=FreeRTOS_FreeRTOS-Kernel&pullRequest=1270&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=FreeRTOS_FreeRTOS-Kernel&pullRequest=1270&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=FreeRTOS_FreeRTOS-Kernel&pullRequest=1270&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=FreeRTOS_FreeRTOS-Kernel&pullRequest=1270&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=FreeRTOS_FreeRTOS-Kernel&pullRequest=1270&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=FreeRTOS_FreeRTOS-Kernel&pullRequest=1270)


author_association: NONE