Skip to content

feat(ghidra): Phase 1 — kuna as Ghidra's decompiler core (protocol + extension + docs) - #134

Merged
mahaloz merged 1 commit into
mainfrom
feat/ghidra-mode
Jul 5, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/ghidra-mode

Conversation

@mahaloz

@mahaloz mahaloz commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

What this is

Phase 1 of running kuna as the decompiler core inside the stock Ghidra GUI, over Ghidra's existing decompiler-process protocol, with zero changes to Ghidra's Java side.

Ghidra's GUI drives its decompiler as a spawned child process (decompile) over a burst-framed stdin/stdout protocol. Upstream that process is the C++ ghidra_opt build — the same DECCORE engine kuna ported, plus a GHIDRA glue group (ghidra_process.cc/ghidra_arch.cc/ghidra_translate.cc/…) kuna deliberately excluded from the port (LOSS-002). This PR reimplements that glue group in Rust. The wire protocol is complete; the engine bridge is stubbed (that's Phase 2).

Contents

New crate decompiler/crates/kuna-ghidra (binary kuna_ghidra):

  • protocol.rs — the burst framing (port of ghidra_arch.cc:50-248): {0,0,1,code} markers, string/bool/byte streams, the getBytes/getStringData codecs, the exception channel → KunaError::Java.
  • process.rs — the command loop (port of ghidra_process.cc): the exact doit() lifecycle (response-open before params, archid bind, catch classification, the always-present 16/17 warnings frame), the archlist session model, and the seven decomp commands. registerProgram parses the tspec <sleigh> into a real AddrSpaceManager (so decompileAt decodes real <addr> params); decompileAt returns the incomplete-function shape (empty payload + a clear warning) so the GUI shows a clean per-function error, not a hang. structureGraph and the four signature commands are unregistered — the "Bad command" path is exactly the graceful-degradation contract Java expects (BSim / nested-graph-layout only).
  • client.rs — the 19 typed callback queries, byte-exact request framing.
  • ids.rs — protocol element ids at their upstream numbers (229, 239–257, 261/262/264/268).
  • 24 tests incl. a byte-exact MockJava full-session e2e.

Ghidra extension integrations/ghidra/KunaDecompiler/: a plugin that reflectively points DecompileProcessFactory.exepath at the kuna binary (the only viable substitution seam — flat classpath, no --add-opens), a Tools-menu toggle, and a status script. Compiles against a real Ghidra install; refcounts live plugin instances so the JVM-wide restore is multi-tool-safe.

Docs: docs/ghidra-integration.md (kuna-side design + the Phase 1–4 plan) and docs/decompiler-core-interface.md (what Ghidra requires from any replacement core — the durable, core-agnostic protocol record).

Deliberate divergences (for drop-in robustness)

  • setOptions tolerates unknown option elements (upstream fails the whole program-open on one stale option — the live baddatacount skew is the motivating case).
  • A malformed archid answers a clean JavaError instead of wedging + exit(1)ing the process.

Both are documented in docs/ghidra-integration.md §8; the setOptions one gets a docs/divergences.md DIV entry when setOptions stops being a stub (Phase 2).

Verification

  • make test — 675/675 PARITY OK
  • make test-stages — PARITY OK
  • make rust-test — green
  • cargo test -p kuna-ghidra — 24/24; extension javac clean against a Ghidra 12.1 install
  • Adversarially reviewed (3 skeptics vs. the upstream C++/Java sources); findings fixed.

Additive — no existing crate is touched.

Follow-up

Phase 2 (the engine bridge → first real C in the GUI) is proposed in a stacked draft PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_017t3eYSdxwUsPjN2s2oLGof

…extension + docs)

Add ghidra-mode: kuna can drive the stock Ghidra GUI as its native decompiler
core, over Ghidra's existing decompiler-process protocol, with zero changes to
Ghidra's Java side.

kuna never ported the C++ "ghidra mode" glue (ghidra_process.cc/ghidra_arch.cc/
ghidra_translate.cc, excluded as LOSS-002). This is Phase 1 of restoring it in
Rust: the wire protocol is complete; the engine bridge is stubbed.

New crate `decompiler/crates/kuna-ghidra` (binary `kuna_ghidra`):
- protocol.rs: the burst framing (a port of ghidra_arch.cc:50-248) — the
  {0,0,1,code} markers, string/bool/byte streams, the getBytes nibble and
  getStringData length codecs, the exception channel (-> KunaError::Java).
- process.rs: the command loop (port of ghidra_process.cc) — the exact doit()
  lifecycle (response-open before params; archid bind; catch classification;
  the always-present 16/17 warnings frame), the archlist session model, and the
  seven decomp commands. registerProgram parses the tspec <sleigh> into a real
  AddrSpaceManager, so decompileAt decodes real <addr> params; decompileAt
  answers the incomplete-function shape (empty payload + a clear warning) so the
  GUI shows a clean per-function error, not a hang. structureGraph and the four
  signature commands are unregistered — the "Bad command" path is exactly the
  graceful-degradation contract Java expects (BSim / nested-graph-layout only).
- client.rs: the 19 typed callback queries (getPcode/getBytes/getMappedSymbols/
  ...), byte-exact request framing; payloads returned raw for the Phase-2 bridge.
- ids.rs: the protocol element ids at their upstream numbers (229, 239-257,
  261/262/264/268), resolved against the C++ source.
- 24 tests incl. a byte-exact MockJava full-session e2e.

Two deliberate, documented divergences from upstream for drop-in robustness:
setOptions tolerates unknown option elements (upstream fails the program-open on
one stale option); a malformed archid answers a clean JavaError instead of
wedging + exiting the process.

Ghidra extension `integrations/ghidra/KunaDecompiler/`: a plugin that reflectively
points DecompileProcessFactory.exepath at the kuna_ghidra binary (the only
substitution seam; flat classpath, no --add-opens), a Tools-menu toggle, and a
status script. Refcounts live plugin instances so restore is JVM-wide-safe.

Docs: `docs/ghidra-integration.md` (the kuna-side design + Phase 1-4 plan) and
`docs/decompiler-core-interface.md` (what Ghidra requires from ANY replacement
core — the durable, core-agnostic protocol record).

Gates: make test (675/675 PARITY OK), make test-stages (PARITY OK), make
rust-test — all green; additive, no existing crate touched.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017t3eYSdxwUsPjN2s2oLGof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant