# fkie-cad/snafuzz architecture

> Architecture diagram of the GitHub repository fkie-cad/snafuzz, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fkie-cad/snafuzz
- Repository: https://github.com/fkie-cad/snafuzz

## Overview

1. **Top-level shape**
   SNAFUzz is split into two operational paths: a **snapshot-creation path** and a **snapshot-fuzzing path**. The same binary (`snafuzz.exe`) can boot a live Windows system, capture a snapshot, reload it later, or start directly from an existing `.snapshot` / `.dmp` state for fuzzing.

2. **Main entry and configuration**
   `src/main.c` is the orchestration point. Runtime behavior is controlled by `src/options.c`, and the build is intentionally a **single-translation-unit** model where a selected target file is included into `main.c`. That makes the fuzzing harness and the core engine compile together, while target-specific logic remains swappable.

3. **State ingestion and snapshot formats**
   The input state can come from several sources: a live VM snapshot, a Windows kernel dump (`.dmp`), or a virtual disk image (`.vhdx`, `.qcow2`, raw disk variants). The repository has dedicated loaders/parsers for these formats in `src/snapshot.c`, `src/parse_dmp.c`, `src/vhdx.c`, `src/qcow2.c`, and `src/raw_disk.c`. This is the boundary where “real machine state” becomes emulator state.

4. **Execution backends**
   There are two host-side execution backends: `src/hyperv.c` for Windows Hypervisor Platform and `src/kvm.c` for Linux/KVM. They provide the low-level VM control path used in snapshot mode and during bootstrapping. The README’s Linux path is clearly secondary; the Windows/WHP path is the primary and best-tested deployment.

5. **Guest platform emulation**
   Booting Windows requires a partial platform emulation layer: `src/uefi_bios.c` provides an incomplete UEFI firmware, and `src/vmbus.c` plus the device modules emulate the minimal virtual hardware Windows expects. The device set is intentionally small: framebuffer, SCSI disk, PS/2 keyboard, and HID mouse. This is enough to get Windows into a debuggable, snapshot-able state.

6. **CPU, memory, and JIT core**
   The deterministic emulator is the heart of fuzzing mode. `src/jit.c` lifts guest x64 instructions one basic block at a time and executes them through a host-side JIT. CPU register state is represented explicitly in `src/cpu_state.c`, while `src/memory_unit.c` manages guest physical memory, dirty-page tracking, and guest-virtual translation caching. This is the main performance and determinism boundary.

7. **Instruction semantics and low-level helpers**
   Instruction decoding/execution is split across helpers like `src/instruction_helpers.c`, `src/instruction_table.h`, `src/interrupts.c`, `src/apic.c`, `src/hooks.c`, and `src/core_target_helpers.c`. These modules fill in the architectural details that the JIT delegates to: interrupt delivery, APIC behavior, instruction-specific helper logic, and target hooks.

8. **Debugger and symbolization**
   `src/debugger.c` implements the built-in WinDbg-style debugger used both during boot/snapshot creation and during crash analysis. Breakpoints can be hit from either the hypervisor or the JIT and are funneled into one debugger handler. Symbol resolution is backed by `src/pdb.c`, with module enumeration from `src/loaded_module.c` using `PsLoadedModuleList` and the user-mode loader list. PDBs are fetched on demand from Microsoft’s public symbol server.

9. **Fuzzing loop and feedback**
   Fuzzing mode repeatedly executes target inputs from a snapshot, monitors crashes, records coverage, mutates inputs, and resets back to the snapshot after a configurable number of cases or instructions. `src/coverage.c` collects instruction, branch, and compare coverage; crash artifacts are written to `crashes/`; coverage snapshots go to `coverage/`. The design is explicitly deterministic so crashes can be reproduced and minimized exactly.

10. **Target-specific boundary**
   Target behavior is not hardcoded into the engine. Instead, targets implement three functions described in `src/target_specific_code_api.h`: initialization, input generation, and input execution. The repo includes examples for kernel fuzzing (`hevd.c`, `kernel_snapshot.c`) and user-mode fuzzing (`HeaderParser.c`, `user_snapshot.c`). `src/default_target.c` is the generic harness used for simple kernel IOCTL-style or user-input-style targets.

11. **Bootstrapping and snapshot preparation**
   `src/preboot_initialization.c` and related initialization code prepare the VM before it enters the fuzzable state. In practice, snapshot creation is a workflow: boot Windows, load or prepare the target, break into the debugger, and serialize the resulting state. This is how the system bridges “normal OS boot” and “repeatable fuzzing state.”

12. **Repo outputs and auxiliary tooling**
   The `symbols/`, `coverage/`, and `crashes/` directories are runtime artifacts rather than source modules. `scripts/ColorHitInstructions.java` is an analysis aid for visualizing coverage in Ghidra. The `images/` folder documents the operational flow but does not affect runtime architecture.

13. **Important architectural boundary**
   The main boundary to capture in a diagram is: **host-controlled orchestration** on one side, and **guest Windows state execution** on the other. Host code manages snapshots, JIT, devices, debugging, coverage, and crash capture; target-specific code lives at the edge and only supplies “how to set up input” and “how to drive the target.” That separation is what makes the system reusable across kernel and user-mode targets.

Main entry points: [`src/main.c`](https://github.com/fkie-cad/snafuzz/blob/main/src/main.c) (Main), [`src/target_specific_code_api.h`](https://github.com/fkie-cad/snafuzz/blob/main/src/target_specific_code_api.h) (Target API), [`src/jit.c`](https://github.com/fkie-cad/snafuzz/blob/main/src/jit.c) (JIT), [`src/snapshot.c`](https://github.com/fkie-cad/snafuzz/blob/main/src/snapshot.c) (Snapshot)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_orchestration["Orchestration"]
  node_main["Main<br/>entrypoint<br/>[main.c]"]
  node_options["Options<br/>config<br/>[options.c]"]
  node_preboot["Preboot Init<br/>bootstrap"]
end

subgraph group_group_state["State Ingestion"]
  node_snapshot["Snapshot<br/>state bridge<br/>[snapshot.c]"]
  node_parse_dmp["DMP Parser<br/>dump loader<br/>[parse_dmp.c]"]
  node_vhdx["VHDX Loader<br/>disk loader<br/>[vhdx.c]"]
  node_qcow2["QCOW2 Loader<br/>disk loader<br/>[qcow2.c]"]
  node_raw_disk["Raw Disk<br/>disk loader<br/>[raw_disk.c]"]
end

subgraph group_group_backends["Execution Backends"]
  node_hyperv["Hyper-V<br/>whp backend<br/>[hyperv.c]"]
  node_kvm["KVM<br/>linux backend<br/>[kvm.c]"]
end

subgraph group_group_guest["Guest Emulation"]
  node_uefi["UEFI BIOS<br/>firmware emu<br/>[uefi_bios.c]"]
  node_vmbus["VMBus<br/>bus emu<br/>[vmbus.c]"]
  node_disk[("Disk<br/>device emu<br/>[disk.c]")]
  node_display["Display<br/>device emu<br/>[hacky_display.c]"]
end

subgraph group_group_core["Core Emulator"]
  node_jit["JIT<br/>x64 executor<br/>[jit.c]"]
  node_cpu_state["CPU State<br/>register model<br/>[cpu_state.c]"]
  node_memory_unit[("Memory Unit<br/>memory model<br/>[memory_unit.c]")]
  node_instr["Instr Helpers<br/>semantics"]
  node_interrupts["Interrupts<br/>arch logic<br/>[interrupts.c]"]
end

subgraph group_group_debug["Debugging"]
  node_debugger["Debugger<br/>analysis tool<br/>[debugger.c]"]
  node_pdb["PDB<br/>symbol resolver<br/>[pdb.c]"]
  node_loaded_module["Modules<br/>module scan<br/>[loaded_module.c]"]
  node_coverage["Coverage<br/>feedback<br/>[coverage.c]"]
end

subgraph group_group_targets["Targets"]
  node_target_api["Target API<br/>plugin api"]
  node_default_target["Default Target<br/>harness<br/>[default_target.c]"]
  node_hevd["HEVD<br/>kernel target<br/>[hevd.c]"]
  node_kernel_snapshot["Kernel Snapshot<br/>kernel harness<br/>[kernel_snapshot.c]"]
  node_headerparser["Header Parser<br/>user target<br/>[HeaderParser.c]"]
  node_user_snapshot["User Snapshot<br/>user harness<br/>[user_snapshot.c]"]
end

subgraph group_group_artifacts["Artifacts"]
  node_crashes["Crashes<br/>artifact dir"]
  node_coverage_dir["Coverage Dir<br/>artifact dir"]
  node_symbols_dir["Symbols Dir<br/>artifact dir"]
end

node_main -->|"reads"| node_options
node_main -->|"boots"| node_preboot
node_main -->|"orchestrates"| node_snapshot
node_snapshot -->|"loads"| node_parse_dmp
node_snapshot -->|"loads"| node_vhdx
node_snapshot -->|"loads"| node_qcow2
node_snapshot -->|"loads"| node_raw_disk
node_main -->|"uses"| node_hyperv
node_main -->|"uses"| node_kvm
node_hyperv -->|"boots"| node_uefi
node_kvm -->|"boots"| node_uefi
node_uefi -->|"initializes"| node_vmbus
node_vmbus -->|"provisions"| node_disk
node_vmbus -->|"provisions"| node_display
node_main -->|"executes"| node_jit
node_jit -->|"reads/writes"| node_cpu_state
node_jit -->|"uses"| node_memory_unit
node_jit -->|"delegates"| node_instr
node_jit -->|"delegates"| node_interrupts
node_debugger -->|"resolves"| node_pdb
node_debugger -->|"enumerates"| node_loaded_module
node_coverage -->|"writes"| node_coverage_dir
node_debugger -->|"writes"| node_crashes
node_pdb -->|"caches"| node_symbols_dir
node_target_api -->|"implements"| node_default_target
node_target_api -->|"implements"| node_hevd
node_target_api -->|"supports"| node_kernel_snapshot
node_target_api -->|"implements"| node_headerparser
node_target_api -->|"supports"| node_user_snapshot
node_main -->|"loads target"| node_target_api
node_coverage -->|"observes"| node_jit

click node_main "https://github.com/fkie-cad/snafuzz/blob/main/src/main.c"
click node_options "https://github.com/fkie-cad/snafuzz/blob/main/src/options.c"
click node_preboot "https://github.com/fkie-cad/snafuzz/blob/main/src/preboot_initialization.c"
click node_snapshot "https://github.com/fkie-cad/snafuzz/blob/main/src/snapshot.c"
click node_parse_dmp "https://github.com/fkie-cad/snafuzz/blob/main/src/parse_dmp.c"
click node_vhdx "https://github.com/fkie-cad/snafuzz/blob/main/src/vhdx.c"
click node_qcow2 "https://github.com/fkie-cad/snafuzz/blob/main/src/qcow2.c"
click node_raw_disk "https://github.com/fkie-cad/snafuzz/blob/main/src/raw_disk.c"
click node_hyperv "https://github.com/fkie-cad/snafuzz/blob/main/src/hyperv.c"
click node_kvm "https://github.com/fkie-cad/snafuzz/blob/main/src/kvm.c"
click node_uefi "https://github.com/fkie-cad/snafuzz/blob/main/src/uefi_bios.c"
click node_vmbus "https://github.com/fkie-cad/snafuzz/blob/main/src/vmbus.c"
click node_disk "https://github.com/fkie-cad/snafuzz/blob/main/src/disk.c"
click node_display "https://github.com/fkie-cad/snafuzz/blob/main/src/hacky_display.c"
click node_jit "https://github.com/fkie-cad/snafuzz/blob/main/src/jit.c"
click node_cpu_state "https://github.com/fkie-cad/snafuzz/blob/main/src/cpu_state.c"
click node_memory_unit "https://github.com/fkie-cad/snafuzz/blob/main/src/memory_unit.c"
click node_instr "https://github.com/fkie-cad/snafuzz/blob/main/src/instruction_helpers.c"
click node_interrupts "https://github.com/fkie-cad/snafuzz/blob/main/src/interrupts.c"
click node_debugger "https://github.com/fkie-cad/snafuzz/blob/main/src/debugger.c"
click node_pdb "https://github.com/fkie-cad/snafuzz/blob/main/src/pdb.c"
click node_loaded_module "https://github.com/fkie-cad/snafuzz/blob/main/src/loaded_module.c"
click node_coverage "https://github.com/fkie-cad/snafuzz/blob/main/src/coverage.c"
click node_target_api "https://github.com/fkie-cad/snafuzz/blob/main/src/target_specific_code_api.h"
click node_default_target "https://github.com/fkie-cad/snafuzz/blob/main/src/default_target.c"
click node_hevd "https://github.com/fkie-cad/snafuzz/blob/main/hevd.c"
click node_kernel_snapshot "https://github.com/fkie-cad/snafuzz/blob/main/kernel_snapshot.c"
click node_headerparser "https://github.com/fkie-cad/snafuzz/blob/main/HeaderParser.c"
click node_user_snapshot "https://github.com/fkie-cad/snafuzz/blob/main/user_snapshot.c"
click node_crashes "https://github.com/fkie-cad/snafuzz/tree/main/crashes"
click node_coverage_dir "https://github.com/fkie-cad/snafuzz/tree/main/coverage"
click node_symbols_dir "https://github.com/fkie-cad/snafuzz/tree/main/symbols"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_options,node_preboot,node_target_api,node_default_target,node_hevd,node_kernel_snapshot,node_headerparser,node_user_snapshot toneBlue
class node_snapshot,node_parse_dmp,node_vhdx,node_qcow2,node_raw_disk,node_crashes,node_coverage_dir,node_symbols_dir toneAmber
class node_hyperv,node_kvm toneMint
class node_uefi,node_vmbus,node_disk,node_display toneRose
class node_jit,node_cpu_state,node_memory_unit,node_instr,node_interrupts toneIndigo
class node_debugger,node_pdb,node_loaded_module,node_coverage toneTeal
```

## Components

### Orchestration

Top-level entrypoint, options, and snapshot workflow control.

- [Main](https://github.com/fkie-cad/snafuzz/blob/main/src/main.c): entrypoint · Single orchestration entrypoint that selects snapshot creation, reload, or fuzzing mode. · `src/main.c`
- [Options](https://github.com/fkie-cad/snafuzz/blob/main/src/options.c): config · Parses runtime flags and mode selection. · `src/options.c`
- [Preboot Init](https://github.com/fkie-cad/snafuzz/blob/main/src/preboot_initialization.c): bootstrap · Prepares the VM before it enters a debuggable or fuzzable state. · `src/preboot_initialization.c`

### State Ingestion

Loaders and parsers that turn external machine state into emulator state.

- [Snapshot](https://github.com/fkie-cad/snafuzz/blob/main/src/snapshot.c): state bridge · Top-level snapshot workflow that bridges live machine state and replayable emulator state. · `src/snapshot.c`
- [DMP Parser](https://github.com/fkie-cad/snafuzz/blob/main/src/parse_dmp.c): dump loader · Loads Windows kernel dumps as initial state. · `src/parse_dmp.c`
- [VHDX Loader](https://github.com/fkie-cad/snafuzz/blob/main/src/vhdx.c): disk loader · Reads VHDX-backed disk state. · `src/vhdx.c`
- [QCOW2 Loader](https://github.com/fkie-cad/snafuzz/blob/main/src/qcow2.c): disk loader · Reads QCOW2-backed disk state. · `src/qcow2.c`
- [Raw Disk](https://github.com/fkie-cad/snafuzz/blob/main/src/raw_disk.c): disk loader · Handles raw disk variants used as boot media. · `src/raw_disk.c`

### Execution Backends

Host-side virtualization control paths.

- [Hyper-V](https://github.com/fkie-cad/snafuzz/blob/main/src/hyperv.c): whp backend · Primary Windows host backend for VM control. · `src/hyperv.c`
- [KVM](https://github.com/fkie-cad/snafuzz/blob/main/src/kvm.c): linux backend · Secondary Linux/KVM execution backend. · `src/kvm.c`

### Guest Emulation

Partial platform and device emulation needed to boot Windows.

- [UEFI BIOS](https://github.com/fkie-cad/snafuzz/blob/main/src/uefi_bios.c): firmware emu · Incomplete UEFI firmware used to boot Windows. · `src/uefi_bios.c`
- [VMBus](https://github.com/fkie-cad/snafuzz/blob/main/src/vmbus.c): bus emu · Minimal virtual bus support expected by Windows. · `src/vmbus.c`
- [Disk](https://github.com/fkie-cad/snafuzz/blob/main/src/disk.c): device emu · Virtual storage device used during boot and target execution. · `src/disk.c`
- [Display](https://github.com/fkie-cad/snafuzz/blob/main/src/hacky_display.c): device emu · Framebuffer/display path for guest visibility. · `src/hacky_display.c`

### Core Emulator

Deterministic CPU, memory, and instruction execution engine.

- [JIT](https://github.com/fkie-cad/snafuzz/blob/main/src/jit.c): x64 executor · Lifts and executes guest x64 basic blocks deterministically. · `src/jit.c`
- [CPU State](https://github.com/fkie-cad/snafuzz/blob/main/src/cpu_state.c): register model · Explicit guest register and processor state representation. · `src/cpu_state.c`
- [Memory Unit](https://github.com/fkie-cad/snafuzz/blob/main/src/memory_unit.c): memory model · Guest physical memory, dirty tracking, and translation cache. · `src/memory_unit.c`
- [Instr Helpers](https://github.com/fkie-cad/snafuzz/blob/main/src/instruction_helpers.c): semantics · Instruction-specific helper logic and architectural fill-ins. · `src/instruction_helpers.c`
- [Interrupts](https://github.com/fkie-cad/snafuzz/blob/main/src/interrupts.c): arch logic · Interrupt delivery and architectural exception flow. · `src/interrupts.c`

### Debugging

Debugger, symbols, and module discovery.

- [Debugger](https://github.com/fkie-cad/snafuzz/blob/main/src/debugger.c): analysis tool · WinDbg-style debugger used for breakpoints, boot inspection, and crash analysis. · `src/debugger.c`
- [PDB](https://github.com/fkie-cad/snafuzz/blob/main/src/pdb.c): symbol resolver · Resolves symbols via downloaded program database files. · `src/pdb.c`
- [Modules](https://github.com/fkie-cad/snafuzz/blob/main/src/loaded_module.c): module scan · Enumerates loaded kernel and user modules for symbolization. · `src/loaded_module.c`
- [Coverage](https://github.com/fkie-cad/snafuzz/blob/main/src/coverage.c): feedback · Collects instruction, branch, and compare coverage for fuzzing feedback. · `src/coverage.c`

### Targets

Target-specific harnesses and the generic target API.

- [Target API](https://github.com/fkie-cad/snafuzz/blob/main/src/target_specific_code_api.h): plugin api · Defines initialization, input generation, and execution hooks for targets. · `src/target_specific_code_api.h`
- [Default Target](https://github.com/fkie-cad/snafuzz/blob/main/src/default_target.c): harness · Generic harness for simple kernel or user-input style targets. · `src/default_target.c`
- [HEVD](https://github.com/fkie-cad/snafuzz/blob/main/hevd.c): kernel target · Example kernel fuzzing target implementation. · `hevd.c`
- [Kernel Snapshot](https://github.com/fkie-cad/snafuzz/blob/main/kernel_snapshot.c): kernel harness · Kernel-target snapshot preparation and replay harness. · `kernel_snapshot.c`
- [Header Parser](https://github.com/fkie-cad/snafuzz/blob/main/HeaderParser.c): user target · Example user-mode fuzzing target implementation. · `HeaderParser.c`
- [User Snapshot](https://github.com/fkie-cad/snafuzz/blob/main/user_snapshot.c): user harness · User-target snapshot preparation and replay harness. · `user_snapshot.c`

### Artifacts

Runtime output directories and analysis aids.

- [Crashes](https://github.com/fkie-cad/snafuzz/tree/main/crashes): artifact dir · Runtime crash artifacts written by fuzzing runs. · `crashes`
- [Coverage Dir](https://github.com/fkie-cad/snafuzz/tree/main/coverage): artifact dir · Stored coverage snapshots and outputs. · `coverage`
- [Symbols Dir](https://github.com/fkie-cad/snafuzz/tree/main/symbols): artifact dir · Fetched and cached symbol files. · `symbols`

## Connections

- Main → Options: reads · Main selects runtime mode and configuration from parsed options.
- Main → Preboot Init: boots · Main drives VM preparation before snapshotting or fuzzing.
- Main → Snapshot: orchestrates · Main enters snapshot creation or replay through the snapshot boundary.
- Snapshot → DMP Parser: loads · Snapshot state can originate from kernel dumps.
- Snapshot → VHDX Loader: loads · Snapshot state can originate from VHDX disk images.
- Snapshot → QCOW2 Loader: loads · Snapshot state can originate from QCOW2 disk images.
- Snapshot → Raw Disk: loads · Snapshot state can originate from raw disk variants.
- Main → Hyper-V: uses · Primary host execution path on Windows.
- Main → KVM: uses · Secondary host execution path on Linux.
- Hyper-V → UEFI BIOS: boots · Hyper-V path starts Windows through firmware emulation.
- KVM → UEFI BIOS: boots · KVM path also relies on firmware emulation when booting.
- UEFI BIOS → VMBus: initializes · Firmware hands off into the minimal virtual platform.
- VMBus → Disk: provisions · VMBus-mediated devices include the boot disk.
- VMBus → Display: provisions · VMBus-mediated devices include framebuffer/display support.
- Main → JIT: executes · Fuzzing mode runs guest code through the JIT engine.
- JIT → CPU State: reads/writes · JIT operates over explicit guest CPU state.
- JIT → Memory Unit: uses · JIT fetches and stores through the guest memory model.
- JIT → Instr Helpers: delegates · Instruction-specific behavior is delegated to helper logic.
- JIT → Interrupts: delegates · Architectural interrupts and exceptions are handled separately.
- Debugger → PDB: resolves · Debugger uses PDBs for symbolized crash analysis.
- Debugger → Modules: enumerates · Debugger consults loaded modules for symbol lookup.
- Coverage → Coverage Dir: writes · Coverage feedback is persisted to the coverage output directory.
- Debugger → Crashes: writes · Crash findings are materialized as artifacts.
- PDB → Symbols Dir: caches · Downloaded symbols are stored locally for reuse.
- Target API → Default Target: implements · Generic target harness follows the target API contract.
- Target API → HEVD: implements · Kernel target example follows the target API contract.
- Target API → Kernel Snapshot: supports · Kernel snapshot harness sits at the target boundary.
- Target API → Header Parser: implements · User-mode target example follows the target API contract.
- Target API → User Snapshot: supports · User snapshot harness sits at the target boundary.
- Main → Target API: loads target · Single-translation-unit build pulls one selected target into the binary.
- Coverage → JIT: observes · Coverage is collected from executed blocks and comparisons.
