Fault Injection & Debugging

# Cortex-M HardFault triage in CI and reverse GDB

Stop chasing intermittent HardFaults over SWD: reproduce the crash deterministically on a board twin and step backward out of HardFault\_Handler.

A HardFault handler that prints CFSR/HFSR/BFAR and halts gives you the crime scene, not the sequence that corrupted the stack or pointer 400 instructions earlier. On physical silicon, reproducing a timing-sensitive crash under a probe can burn days.

Under CircuitTwin, every run with the same inputs and seed executes identically. When CI catches a HardFault, launch ct debug locally with the exact suite inputs, break on HardFault\_Handler and execute reverse-stepi or reverse-continue to a watchpoint.

## Fastest CFSR triage table

| CFSR hex   | Flag                  | First check in GDB                                               |
| ---------- | --------------------- | ---------------------------------------------------------------- |
| 0x00008200 | PRECISERR + BFARVALID | Read BFAR (0xE000ED38): unclocked peripheral or bad MMIO pointer |
| 0x00000400 | IMPRECISERR           | Set ACTLR.DISDEFWBUF=1 to force precise BusFault on store        |
| 0x00010000 | UNDEFINSTR            | Disassemble stacked PC: erased flash, bad branch, or wrong -mcpu |
| 0x00020000 | INVSTATE              | Stacked PC bit 0 is 0 (ARM mode jump on Thumb-only Cortex-M)     |
| 0x00080000 | NOCP                  | FPU instruction executed before SCB->CPACR CP10/CP11 enabled     |
| 0x02000000 | DIVBYZERO             | SDIV/UDIV zero divisor with CCR.DIV\_0\_TRP=1                    |

## Local CLI workflow

Shell — reproduce & reverse-debug HardFault

```
ct debug --board nucleo-f767zi --gdb-port 3333 build/app.elf

# In arm-none-eabi-gdb:
# (gdb) target remote :3333
# (gdb) b HardFault_Handler
# (gdb) continue
# (gdb) x/6wx 0xE000ED28
# (gdb) reverse-stepi 20
```

## Suite & fault configuration

tests/hardfault-gate.suite.yaml

```
version: 1
board: nucleo-f767zi
firmware: build/app.elf
tests:
  - name: stress-without-hardfault
    timeout_ms: 4000
    expect:
      console_not_contains: "HARDFAULT"
      exit_code: 0
```

## CI pipeline integration

.github/workflows/hardfault-gate.yml

```
name: HardFault Regression Gate
on: [push, pull_request]
jobs:
  fault-gate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: cmake --build build
      - uses: circuittwin/action@v1
        with:
          suite: tests/hardfault-gate.suite.yaml
```

## Catching Cortex-M stack overflows, MemManage and STKOF faults

Stop chasing corrupted globals days after a stack overflow. Trap the exact overflowing push instruction in CI.

Traditional stack-canary checks only notice an overflow at the next context switch, long after the stack overwrote neighbor structs. Deep nested interrupts make worst-case stack depth hard to trigger by hand.

CircuitTwin models Cortex-M MPU guard regions and ARMv8-M MSPLIM/PSPLIM hardware stack limits, then lets you attach ct debug with GDB reverse-step right back to the offending function call.

### Stack protection mechanisms

| Architecture       | Hardware guard                   | Fault bit set                           |
| ------------------ | -------------------------------- | --------------------------------------- |
| ARMv8-M (M33)      | MSPLIM / PSPLIM registers        | UFSR.STKOF (CFSR bit 20)                |
| ARMv7-M (M3/M4/M7) | 32-byte MPU no-access guard page | MMFSR.DACCVIOL (CFSR bit 1)             |
| All Cortex-M       | Reverse-step in ct debug         | Exact call frame & SP before corruption |

Shell - inspect stack overflow in GDB reverse mode

```
ct debug --board nucleo-l552ze --gdb-port 3333 build/app.elf
# In arm-none-eabi-gdb:
# (gdb) target remote :3333
# (gdb) break UsageFault_Handler
# (gdb) continue
# (gdb) reverse-stepi
```

tests/stack-guard.suite.yaml

```
version: 1
board: nucleo-l552ze
firmware: build/app.elf
tests:
  - name: stress-nested-isr-zero-stkof
    timeout_ms: 2500
    expect:
      console_contains: "STRESS_PASS"
      exit_code: 0
```

## Troubleshooting checklist

| Symptom                                       | Fix                                                                                                       |
| --------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| IMPRECISERR hides the exact store instruction | Temporarily set ACTLR bit 1 (DISDEFWBUF) or use reverse-continue on a data watchpoint in CircuitTwin GDB. |
| Stack trace shows corrupt frames              | Check EXC\_RETURN bit 2 in LR (0=MSP, 1=PSP) and inspect the 8-word hardware frame at that SP.            |
| HardFault only on Cortex-M33                  | Check UFSR bit 20 (STKOF) for MSPLIM/PSPLIM hardware stack-limit overflow.                                |
| Need instant bit decode from CI log           | Paste CFSR/HFSR/MMFAR/BFAR directly into the Cortex-M HardFault Decoder.                                  |
| CFSR reads 0x00100000 on Cortex-M33           | Bit 20 is STKOF (Stack Overflow); increase task stack size or trim large local stack arrays.              |
| HardFault occurs inside printf()              | Newlib printf uses 1-2 KB of stack; switch to nano specs (--specs=nano.specs) or lightweight snprintf.    |

## Frequently asked questions

| Question                                                     | Answer                                                                                                                                        |
| ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------- |
| How do I decode a CFSR hex dump from a stack overflow crash? | Paste the CFSR, HFSR, MMFAR and BFAR hex words into our free Cortex-M HardFault Decoder to inspect every bit flag and GDB inspection command. |

## Related boards & component twins

[Board: nucleo-f767zi](https://circuittwin.ai/boards/nucleo-f767zi) [Board: stm32f4-discovery](https://circuittwin.ai/boards/stm32f4-discovery) [Board: nucleo-l552ze](https://circuittwin.ai/boards/nucleo-l552ze) [Board: teensy-4-1](https://circuittwin.ai/boards/teensy-4-1) [Board: b-u585i-iot02a](https://circuittwin.ai/boards/b-u585i-iot02a) [Board: lpcxpresso55s69](https://circuittwin.ai/boards/lpcxpresso55s69) [Twin: BMI088](https://circuittwin.ai/twins/bmi088) [Twin: LSM6DSO](https://circuittwin.ai/twins/lsm6dso) [Twin: MPU-6050](https://circuittwin.ai/twins/mpu-6050) [Twin: BME280](https://circuittwin.ai/twins/bme280) [GDB reverse execution docs](https://circuittwin.ai/docs/ide) [Seeded bug benchmark](https://circuittwin.ai/docs/agent-benchmark) [HardFault CI triage guide](https://circuittwin.ai/guides/cortex-m-hardfault-ci-triage)

## More in Fault Injection & Debugging

[I2C NACK and stuck-bus fault injection in CI](https://circuittwin.ai/guides/i2c-nack-fault-injection) [SPI bus fault testing and chip-select verification](https://circuittwin.ai/guides/spi-bus-fault-testing) [Debugging UART baud mismatches and framing errors](https://circuittwin.ai/guides/uart-baud-mismatch-debugging) [Detecting ISR race conditions deterministically](https://circuittwin.ai/guides/isr-race-condition-detection)

> ## Run this workflow on your own ELF
>
> Start free on CircuitTwin to run local board twins, fault matrices and CI checks with zero per-run credits.
>
> [Start now](https://app.circuittwin.ai/try) [Read the quickstart](https://circuittwin.ai/docs/quickstart) [Connect a coding agent](https://circuittwin.ai/docs/agents)
