From d4a16e4c854c7f1cc5198c5efdd1e8121ffe9e1e Mon Sep 17 00:00:00 2001 From: Richard Elberger Date: Wed, 17 Jun 2026 13:54:01 -0700 Subject: [PATCH] armv9: fix MTE tagged pointer handling in stack assertions When configARMV9_MTE_HEAP or configARMV9_MTE_STACK is enabled, pvPortMalloc and pxPortInitialiseStack return pointers with non-zero MTE tags in the top byte. The stack depth assertion in tasks.c and the stack overflow checks in stack_macros.h compared tagged pointers against untagged ones, causing spurious assertion failures. Introduce portSTRIP_ADDRESS_TAG() macro: - Default no-op in FreeRTOS.h (zero impact on non-MTE ports) - ARM_AARCH64_ARMV9 portmacro.h defines it to mask bits [63:56] Apply the macro in: - tasks.c: prvInitialiseNewTask stack depth check (both pointers) - stack_macros.h: both stack overflow detection variants --- include/FreeRTOS.h | 4 ++++ include/stack_macros.h | 4 ++-- tasks.c | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/FreeRTOS.h b/include/FreeRTOS.h index 63e2feb51..f28268885 100644 --- a/include/FreeRTOS.h +++ b/include/FreeRTOS.h @@ -2748,6 +2748,10 @@ #define portTASK_USES_FLOATING_POINT() #endif +#ifndef portSTRIP_ADDRESS_TAG + #define portSTRIP_ADDRESS_TAG( pxPointer ) ( pxPointer ) +#endif + #ifndef portALLOCATE_SECURE_CONTEXT #define portALLOCATE_SECURE_CONTEXT( ulSecureStackSize ) #endif diff --git a/include/stack_macros.h b/include/stack_macros.h index 6d0117722..3d66f6f91 100644 --- a/include/stack_macros.h +++ b/include/stack_macros.h @@ -71,7 +71,7 @@ do \ { \ /* Is the currently saved stack pointer within the stack limit? */ \ - if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \ + if( portSTRIP_ADDRESS_TAG( pxCurrentTCB->pxTopOfStack ) <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \ { \ char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \ vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \ @@ -88,7 +88,7 @@ do \ { \ /* Is the currently saved stack pointer within the stack limit? */ \ - if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \ + if( portSTRIP_ADDRESS_TAG( pxCurrentTCB->pxTopOfStack ) >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \ { \ char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \ vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \ diff --git a/tasks.c b/tasks.c index c596c475f..e08a57334 100644 --- a/tasks.c +++ b/tasks.c @@ -2011,11 +2011,11 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, #if ( portSTACK_GROWTH < 0 ) { - configASSERT( ( ( portPOINTER_SIZE_TYPE ) ( pxTopOfStack - pxNewTCB->pxTopOfStack ) ) < ( ( portPOINTER_SIZE_TYPE ) uxStackDepth ) ); + configASSERT( ( ( portPOINTER_SIZE_TYPE ) ( ( StackType_t * ) portSTRIP_ADDRESS_TAG( pxTopOfStack ) - ( StackType_t * ) portSTRIP_ADDRESS_TAG( pxNewTCB->pxTopOfStack ) ) ) < ( ( portPOINTER_SIZE_TYPE ) uxStackDepth ) ); } #else /* portSTACK_GROWTH */ { - configASSERT( ( ( portPOINTER_SIZE_TYPE ) ( pxNewTCB->pxTopOfStack - pxTopOfStack ) ) < ( ( portPOINTER_SIZE_TYPE ) uxStackDepth ) ); + configASSERT( ( ( portPOINTER_SIZE_TYPE ) ( ( StackType_t * ) portSTRIP_ADDRESS_TAG( pxNewTCB->pxTopOfStack ) - ( StackType_t * ) portSTRIP_ADDRESS_TAG( pxTopOfStack ) ) ) < ( ( portPOINTER_SIZE_TYPE ) uxStackDepth ) ); } #endif /* portSTACK_GROWTH */ }