1. Today’s topic
Today we examine crashes caused by:
invalid pointer
array bounds violation
stack overflow
use-after-free
double free
heap corruption
incorrect DMA buffer
wrong peripheral address
data race
execution of corrupt codeFor STM32:
HardFault
MemManage
BusFault
UsageFault
CFSR / HFSR
BFAR / MMFAR
MSP / PSP
exception stack frame
PC / LR / xPSRFor ESP32:
Guru Meditation
register dump
backtrace
EXCCAUSE / EXCVADDR
Core Dump
GDB Stub
heap poisoning
hardware watchpointsThe central idea: the instruction where the processor crashed is often merely the first instruction to notice corruption. The actual error may have occurred much earlier—during an out-of-bounds write, object deallocation, or DMA operation.
2. Why this matters in your projects
The EC25 AT parser may corrupt memory through an oversized URC, an incorrect MQTT payload length, a missing null terminator, or a pointer into an overwritten DMA ring buffer. For example:
ADC DMA writes 512 samples
the array was allocated for only 256 samples
DMA corrupts an adjacent FreeRTOS structure
the next task switch causes HardFaultCAN/RS-485 hazards include incorrect DLC/length, CRC access beyond the frame, and a pointer to a stack buffer in a queue. A crash should become an artifact:
firmware version
Git SHA
reset reason
fault registers
PC/LR
task name
stack watermark
recent diagnostic events
exact release ELF3. Theory
3.1. Three moments of an error
1. Corruption occurs.
2. Corruption is detected.
3. The crash occurs.A crash in free() or the scheduler often means memory was corrupted earlier.
3.2. Cortex-M faults
MemManage:
MPU violation, forbidden access, execution from an XN-region.
BusFault:
bus error, missing memory, Flash/RAM/peripheral access error.
UsageFault:
undefined instruction, invalid state, division by zero, unaligned access.HardFault often results from escalation of another fault. If HFSR.FORCED = 1 and the target implements these registers, decode CFSR. The register set below applies to cores such as Cortex-M3/M4/M7, not every STM32.
3.3. Main STM32 fault registers
uint32_t cfsr = SCB->CFSR;
uint32_t hfsr = SCB->HFSR;
uint32_t dfsr = SCB->DFSR;
uint32_t afsr = SCB->AFSR;
uint32_t bfar = SCB->BFAR;
uint32_t mmfar = SCB->MMFAR;
uint32_t shcsr = SCB->SHCSR;CFSR contains:
bits 0-7: MMFSR
bits 8-15: BFSR
bits 16-31: UFSRInterpret BFAR and MMFAR only when BFARVALID and MMARVALID are set. Stacking or unstacking faults can mean the exception frame itself is invalid.
3.4. Exception stack frame
The basic Cortex-M exception frame contains:
R0
R1
R2
R3
R12
LR
PC
xPSRThe stacked PC helps identify the exception context, but may not identify the original failing write: an imprecise BusFault can be reported later. LR helps reconstruct the call, while R0-R3 may hold a pointer or length. Validate the frame before trusting these values, especially after a stacking fault.
3.5. HardFault wrapper
void hardfault_capture(uint32_t *stack_pointer, uint32_t exc_return);
__attribute__((naked))
void HardFault_Handler(void)
{
__asm volatile(
"tst lr, #4 \n"
"ite eq \n"
"mrseq r0, msp \n"
"mrsne r0, psp \n"
"mov r1, lr \n"
"b hardfault_capture \n"
);
}EXC_RETURN bit 2 selects MSP or PSP. This wrapper illustrates GCC on a compatible Thumb-2 Cortex-M; it is not a complete portable handler for every Cortex-M. hardfault_capture must not return as an ordinary function: define a terminal halt or safe reset path. Before reading the frame, validate its pointer, stacking status, and the FPU extended-frame layout using the exact core manual.
3.6. Finding a source line from PC
arm-none-eabi-addr2line \
-e build/firmware.elf \
-f \
-C \
0x08012A9EUse the exact ELF from the build that crashed.
3.7. ESP32 Guru Meditation
Example for the original Xtensa-based ESP32. Other ESP32 families, including RISC-V targets, use different registers and dump formats:
Guru Meditation Error:
Core 1 panic'ed (LoadProhibited)
PC : ...
A0/A1 : ...
EXCCAUSE: ...
EXCVADDR: ...
Backtrace: ...EXCVADDR = 0x00000000 often suggests a NULL dereference; 0x00000010 may indicate a member access through NULL plus an offset. These are hypotheses to verify against the instruction, operands, and exact ELF, not a diagnosis from the address alone.
3.8. ESP32 Core Dump
Core Dump records diagnostic state, including registers and selected task structures and stacks. Its scope, format, and storage depend on ESP-IDF configuration. A dump can contain secrets from RAM and must be access-controlled.
idf.py coredump-info
idf.py coredump-debugIn GDB:
info registers
info threads
thread apply all bt
bt
frame 0
info locals3.9. Heap poisoning and integrity checkpoints
ESP-IDF provides additional heap checks. Head/tail canaries are used in Light Impact mode, while allocation/free fill patterns are added by Comprehensive mode. With the corresponding mode enabled, typical values are:
0xABBA1234: head canary
0xBAAD5678: tail canary
0xCECECECE: uninitialized memory
0xFEFEFEFE: freed memoryChecks:
heap_caps_check_integrity_all(true);These can be placed temporarily before/after suspicious sections.
3.10. Hardware watchpoint
If the corrupted address is known, set a watchpoint in GDB. The following is a debugger command, not C source:
watch *(uint32_t *)0x20001000If the debugger installs a suitable hardware watchpoint, it can stop the core on a matching CPU access. Interpret the stopped instruction using the core’s rules; it need not identify the first cause of corruption. A CPU watchpoint generally does not observe writes by a separate DMA master. Watchpoint count, alignment, and supported sizes are limited.
4. Common mistakes
1. Looking only at the crash line.
2. Using an ELF from another version.
3. Trusting BFAR/MMFAR unconditionally.
4. Ignoring an FPU extended frame.
5. Calling printf() inside HardFault.
6. Writing Flash from a fault handler.
7. Not checking the stack pointer before reading the frame.
8. Ignoring an imprecise BusFault.
9. “Fixing” the problem by adding a delay.
10. Not archiving crash artifacts.5. Practical assignment for 30-60 minutes
Create CRASH_DEBUG_POLICY.md:
# Crash debugging policy
1. Every production build archives ELF, MAP and sdkconfig.
2. Every crash contains firmware version and Git SHA.
3. STM32 faults save CFSR, HFSR, BFAR, MMFAR and stacked PC/LR.
4. ESP32 production builds provide panic output or Core Dump.
5. Fault handlers do not allocate memory or use blocking APIs.
6. Flash is not written directly from an unsafe fault context.
7. Known corrupt addresses are investigated with watchpoints.
8. Heap corruption is narrowed using integrity checkpoints.
9. Crash reproducers become unit or HIL regression tests.
10. A delay is never accepted as a final race-condition fix.For a supported STM32, adapt the wrapper, trigger a controlled fault in an isolated debug build, and resolve PC against the exact ELF using addr2line. For ESP32, configure Core Dump, trigger an intentional assert in debug firmware, and inspect idf.py coredump-info. These experiments have not been run for this lesson.
6. Further reading or experiments
- ESP-IDF Fatal Errors, Core Dump, Heap Memory Debugging, and JTAG Debugging.
- ST materials on HardFault debugging.
- The ARM Cortex-M programming manual for your core.
Brief recap
crash detected
→ preserve firmware identity
→ identify fault class
→ obtain stacked PC/LR
→ check CFSR/HFSR and fault address
→ locate the line using exact ELF
→ check disassembly and operands
→ determine whether the cause is here or memory was corrupted earlier
→ integrity checks / MPU / watchpoint
→ reproducer
→ regression testExercise
A crash occurs in the scheduler after an ADC DMA transfer. Explain how to test the earlier corruption hypothesis using lengths, ownership, and retained build evidence.
Self-check criteria: Do not assume the crash line is the root cause; identify a reproducer and an observable bounds/ownership violation.
Show the supplied answer
Compare the configured transfer count with the allocated destination capacity and its lifetime, inspect nearby memory/integrity checkpoints, and decode the crash using the exact retained ELF. A scheduler crash may be a late symptom of an earlier DMA write.
Exercise
Before adapting the source’s HardFault wrapper to another Cortex-M target, list the compatibility checks required for reading the exception frame safely.
Self-check criteria: Do not claim that one wrapper works on every Cortex-M variant. Keep target-specific assumptions explicit and avoid Flash writes or complex logging in the fault handler.
Show the supplied answer
Check the actual core/exception architecture, MSP/PSP selection through EXC_RETURN, FPU/extended-frame behavior, and stack-pointer validity. Decode only fault registers supported by that target and only valid fault-address fields.