Core Principles
1. Static Analysis as Source of Truth
Ghidra decompilation provides the authoritative view of program structure:- Function boundaries and call graphs
- Data structure layouts and access patterns
- Control flow and branching logic
analysis/ghidra/maps/name_map.json- Function and global namesanalysis/ghidra/maps/data_map.json- Data structure definitions
2. Runtime Validation for Ambiguity
When static analysis is unclear (polymorphic calls, magic constants, undocumented algorithms), use runtime instrumentation:- Frida for high-frequency capture (RNG traces, state snapshots)
- WinDbg for deep inspection (memory dumps, conditional breakpoints)
3. Differential Testing for Verification
Every behavioral claim must be verified by running original and rewrite with identical inputs and comparing state:- Deterministic replay system with seeded RNG
- Tick-aligned state checkpoints
- Field-by-field comparison (player position, creature health, projectile count)
The Five-Stage Workflow
Stage 1: Initial Decompilation
Load Binary into Ghidra
- Import
crimsonland.exeandgrim.dll - Apply auto-analysis (function discovery, string search)
- Import third-party headers (Windows API, DirectX, standard library)
- Export raw decompiled C to
analysis/ghidra/raw/
FUN_00401234, DAT_00480348)Stage 2: Symbol Recovery
Identify Key Functions
Start with high-level entry points and work inward:- WinMain - Program entry point
- Game loop - Main update/render cycle
- State machine - Menu navigation, mode transitions
- Core systems - Player update, creature AI, projectile physics
Naming Strategy
Use consistent prefixes:player_*- Player state and actionscreature_*- Enemy pool and AIprojectile_*- Bullet/beam physicsweapon_*- Weapon tables and fire logicperk_*- Perk effects and countsbonus_*- Pickup spawn and applicationconfig_*- Configuration blob fieldsgrim_*- Grim2D engine calls
Store in Maps
Stage 3: Runtime Evidence Collection
Frida Instrumentation
Hook key functions to capture actual runtime behavior:- RNG call sequences and return values
- Damage calculations (input/output pairs)
- State transitions (menu IDs, game mode)
- Struct field values at known offsets
WinDbg Deep Inspection
For one-off questions:analysis/frida/raw/*.jsonlStage 4: Struct Layout Recovery
Cross-Reference Static and Runtime
Combine decompiled memory access patterns with captured runtime values:Example: Player structStatic analysis shows:| Offset | Field | Evidence |
|---|---|---|
| 0x00 | health | player_take_damage writes; < 0 triggers death |
| -0x10 | pos_x | Camera centering, distance checks |
docs/structs/Stage 5: Differential Verification
Implement and Test
- Port behavior to Python rewrite using recovered names/structs
- Record a replay in the original game with Frida capture
- Play back same inputs in the rewrite
- Compare state checkpoints tick-by-tick
Hotspot Extraction
For complex functions (1000+ lines), extract focused slices:- Isolated context for renaming and annotation
- Includes direct callees for complete logic flow
- Work files separate from immutable baseline
Parity Workflow Example
Let’s trace how we recovered the Fire Bullets bonus behavior:1. Static: Find the hook
2. Runtime: Confirm the behavior
3. Implement in rewrite
4. Verify with differential test
Tools and Scripts
Key automation inscripts/:
ghidra_analyze.py- Apply name maps, regenerate decompilesfrida_reduce.py- Normalize Frida logs into evidence factsghidra_hotspot_extract.py- Extract function subgraphsreplay_verify.py- Differential checkpoint comparison
Evidence Promotion
After reviewing runtime captures, promote findings to authoritative maps:- Review
analysis/frida/name_map_candidates.json - Manually merge high-confidence entries into
analysis/ghidra/maps/name_map.json - Rerun Ghidra scripts to apply names:
just ghidra-exe - Update documentation in
docs/
Success Metrics
- 500+ functions named and documented
- 200+ globals identified with struct layouts
- Full parity in Survival mode (1000+ tick replays match)
- Quest completion verified across all 90 levels
- Deterministic replays with frame-perfect accuracy
Related Pages
Ghidra Workflow
Detailed Ghidra setup and scripting
Frida Capture
Runtime instrumentation guide
Differential Testing
Verification and replay system