AgentSkillsCN

check-isr

验证回调函数的中断安全性。适用于实现或编辑 MacTCP ASR、Open Transport 通知器,或 AppleTalk 回调代码时使用。此外,在提交 Mac 平台代码之前,也应先进行验证,以确保不存在 ISR 违规。

SKILL.md
--- frontmatter
name: check-isr
description: Validate callback functions for interrupt-time safety. Use when implementing or editing MacTCP ASR, Open Transport notifier, or AppleTalk callback code. Also use before committing Mac platform code to verify no ISR violations exist.
argument-hint: [file-or-directory]

ISR Safety Validator

Scans MacTCP ASR, Open Transport notifier, and AppleTalk callback functions for interrupt-time safety violations.

Usage

code
/check-isr src/mactcp/
/check-isr src/mactcp/tcp_mactcp.c
/check-isr

Without arguments, scans all Mac-specific source directories.

Process

  1. Verify prerequisites:

    bash
    # Check validator exists
    if [[ ! -f tools/validators/isr_safety.py ]]; then
        echo "❌ ISR validator not found at tools/validators/isr_safety.py"
        exit 1
    fi
    
    # Run validator with Python (available in Docker or host)
    # The validator is a standalone Python script with no dependencies
    echo "✓ Prerequisites OK"
    
  2. Run the validator:

    bash
    python tools/validators/isr_safety.py ${ARGUMENTS:-src/mactcp src/opentransport src/appletalk}
    
  3. For each violation found:

    • Show the file, line number, and callback function name
    • Show the forbidden function call
    • Explain why it's forbidden (with documentation reference)
    • Suggest the correct pattern
  4. If no violations: Confirm the code is ISR-safe

What It Checks

The validator detects callback functions by matching:

  • Function names ending in _asr, _notifier, _completion, _callback
  • Functions with pascal keyword and MacTCP/OT callback signatures
  • Functions matching known ASR/notifier parameter patterns

Within those functions, it checks for calls to:

CategoryExamplesWhy Forbidden
Memory allocationmalloc, NewPtr, NewHandleHeap operations at interrupt time
Memory operationsmemcpy, BlockMoveMay use unsafe implementations
TimingTickCountNot in Table B-3 interrupt-safe list
I/Oprintf, FSRead, FSWriteFile/device operations forbidden
Sync networkTCPSend, OTConnectSynchronous calls in async context
ToolboxGetResource, DrawStringMost Toolbox not interrupt-safe

Common Fixes

ViolationCorrect Pattern
memcpy in callbackUse pt_memcpy_isr()
TickCount in callbackSet timestamp = 0, let main loop timestamp
malloc in callbackUse pre-allocated buffers from context struct
Sync network callUse async version with completion callback
printf/loggingSet a flag, print from main loop

Example Output

code
src/mactcp/tcp_mactcp.c
Line  | Callback        | Forbidden Call | Reason
------|-----------------|----------------|----------------------------------
142   | tcp_asr         | memcpy         | May use BlockMove internally
156   | tcp_asr         | TickCount      | Not in Table B-3 interrupt-safe
189   | udp_asr         | malloc         | Dynamic allocation forbidden

Integration

The ISR Safety Validator is also integrated as a pre-edit hook. When editing files in src/mactcp/, src/opentransport/, or src/appletalk/, the hook will block edits that introduce violations.


Example Workflows

After Implementing Mac Code

bash
# Implemented MacTCP TCP driver
/check-isr src/mactcp/tcp_mactcp.c
# → Shows violations if any

# Fix violations, then verify all Mac code
/check-isr
# → Checks all src/mactcp/, src/opentransport/, src/appletalk/

During Development

bash
# Write code with callbacks...
# Edit tool auto-runs isr-safety-check.sh hook
# → Hook BLOCKS edit if violation detected

# Fix the violation
# Re-attempt edit
# → Hook allows edit through ✓

Pre-Commit Check

bash
/check-isr
# → Verify all Mac code is ISR-safe before committing

Debugging a Violation

When a hook blocks an edit with an ISR violation:

  1. Understand why: Check .claude/rules/isr-safety.md for the forbidden function
  2. Find all instances: /check-isr src/mactcp/ shows violations with line numbers
  3. Fix using safe pattern: Replace with ISR-safe alternative (see Common Fixes table)
  4. Verify fix: /check-isr should report no violations

Example: TickCount() → Set flag in callback, timestamp in main loop


Reference

See CLAUDE.md sections:

  • "MacTCP ASR Rules"
  • "Open Transport Notifier Rules"
  • "AppleTalk ADSP Callback Rules"

Source: Inside Macintosh Volume VI, Table B-3 - "Routines That May Be Called at Interrupt Time"