Skip to content

Commit c6c1472

Browse files
ioannisgcarlescufi
authored andcommitted
arch: arm: cortex_m: fix stack overflow error detection
In rare cases when a thread may overflow its stack, the core will not report a Stacking Error. This is the case when a large stack array is created, making the PSP cross beyond the stack guard; in this case a MemManage fault won't cause a stacking error (but only a Data Access Violation error). We fix the fault handling logic so such errors are reported as stack overflows and not as generic CPU exceptions. Signed-off-by: Ioannis Glaropoulos <[email protected]>
1 parent 202c2fd commit c6c1472

File tree

1 file changed

+10
-2
lines changed
  • arch/arm/core/aarch32/cortex_m

1 file changed

+10
-2
lines changed

arch/arm/core/aarch32/cortex_m/fault.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,16 @@ static uint32_t mem_manage_fault(z_arch_esf_t *esf, int from_hard_fault,
256256
*
257257
* By design, being a Stacking MemManage fault is a necessary
258258
* and sufficient condition for a thread stack corruption.
259+
* [Cortex-M process stack pointer is always descending and
260+
* is never modified by code (except for the context-switch
261+
* routine), therefore, a stacking error implies the PSP has
262+
* crossed into an area beyond the thread stack.]
263+
*
264+
* Data Access Violation errors may or may not be caused by
265+
* thread stack overflows.
259266
*/
260-
if (SCB->CFSR & SCB_CFSR_MSTKERR_Msk) {
267+
if ((SCB->CFSR & SCB_CFSR_MSTKERR_Msk) ||
268+
(SCB->CFSR & SCB_CFSR_DACCVIOL_Msk)) {
261269
#if defined(CONFIG_MPU_STACK_GUARD) || defined(CONFIG_USERSPACE)
262270
/* MemManage Faults are always banked between security
263271
* states. Therefore, we can safely assume the fault
@@ -310,7 +318,7 @@ static uint32_t mem_manage_fault(z_arch_esf_t *esf, int from_hard_fault,
310318

311319
reason = K_ERR_STACK_CHK_FAIL;
312320
} else {
313-
__ASSERT(0,
321+
__ASSERT(!(SCB->CFSR & SCB_CFSR_MSTKERR_Msk),
314322
"Stacking error not a stack fail\n");
315323
}
316324
}

0 commit comments

Comments
 (0)