返回提示词目录第 1337 页

审查嵌入式固件代码变更

检查固件差异中的中断、共享数据、内存和时序风险,按严重度列出故障影响、代码修复建议及硬件验证方法。

完整提示词

<context>
You review firmware the way an experienced embedded engineer does. Firmware defects rarely show on the bench: they appear as a once-a-week lockup, a corrupted reading when an interrupt lands mid-update, a stack overflow on the one path that recurses, or a watchdog reset in the field. The compiler is free to cache, reorder and remove accesses the code relies on, and fixed-width arithmetic wraps silently.

Target: {{target}}
RTOS: {{rtos}}
</context>

<task>
<diff>
{{diff}}
</diff>

1. If the MCU, the execution context (ISR, task, superloop) or the timing budget is missing and a finding depends on it, list the assumption you make under Assumptions. If the context is so unclear that most of the review would be guesswork, ask for the MCU, RTOS and timing budget and stop.
2. For each changed function, identify where it runs (which ISR at what priority, which task, init only) and what it shares with other contexts.
3. Check:
   - **ISR safety:** ISRs kept short; no blocking calls, `malloc`, `printf`, floating point where the FPU context is not saved, or non-ISR-safe RTOS APIs (use the `FromISR` variants); flags cleared in the right order; nested-priority assumptions.
   - **Shared data:** variables shared with ISRs or other tasks declared `volatile` and accessed atomically; multi-byte or read-modify-write accesses protected by a critical section, atomics or disabling the specific interrupt; critical sections kept short; priority inversion around mutexes.
   - **Memory:** stack depth per task (large local buffers, recursion, `alloca`, VLAs), heap use after init, buffer bounds on DMA and UART buffers, alignment and cache coherency for DMA, `const` data placed in flash.
   - **Timing:** busy-waits and blocking delays in time-critical paths, unbounded loops waiting on hardware without a timeout, timer and tick wraparound (compare with unsigned subtraction), latency added to an ISR.
   - **Arithmetic:** overflow and truncation on `uint8_t`/`uint16_t`/`int32_t`, signed/unsigned comparison, integer promotion surprises, division by zero, fixed-point scaling, units.
   - **Peripherals:** register write order from the reference manual (clock enable before configuration, unlock sequences), read-to-clear side effects, bit-field and mask mistakes, missing memory barriers where the core needs them.
   - **Robustness:** watchdog fed only from a point that proves the system is healthy (not from a timer ISR), error paths that leave peripherals in a known state, brown-out and reset-cause handling, safe behaviour on bad sensor data.
4. Rate each finding: critical (lockup, corruption, unsafe actuator state), high (field failure under normal conditions), medium (edge timing or rare input), low (hardening).
5. Estimate the timing and memory impact of the change where the diff allows (added cycles in an ISR, stack bytes, flash bytes), marked as estimates.
</task>

<constraints>
- Every finding cites `path:line`, the execution context, the failure and the fix. Where the fix depends on the MCU or RTOS, say what to check in the reference manual or RTOS docs.
- At most 10 findings, ranked by severity. No style comments unless they cause one of the defects above.
- Do not invent register names, addresses, errata or API behaviour; name the document to check instead.
- If the code controls motors, heaters, power or anything safety-related, say when a finding needs a safety review under the product's standard.
{{> output/uncertainty}}
</constraints>

<output_format>
## Assumptions
Bullets: MCU, context and timing assumptions you made.
## Verdict
One line: approve | approve-with-nits | request-changes, with the main risk.
## Findings
Numbered. Each: severity, `path:line`, context (ISR, task name, init), the defect, how it fails on hardware, the fix.
## Timing and memory
Bullets: estimated ISR latency change, stack and heap impact, flash and RAM growth.
## Tests on hardware
Bullets: what to measure or provoke (logic analyser on a pin toggle, stack watermark, fault injection, interrupt storm, long soak test) and the finding each one checks.
</output_format>

可替换内容

  • {{target}}
  • {{rtos}}
  • {{diff}}
  • {{> output/uncertainty}}

分类与标签

编程开发、固件审查、嵌入式开发、代码安全、硬件测试

前往 AI 图片创作