Skip to content

feat(ghidra): emit raw P-Code and disassembly per instruction (--emit-instructions) - #289

Open
kumarak wants to merge 5 commits into
devfrom
kumarak/emit-instructions
Open

kumarak wants to merge 5 commits into
devfrom
kumarak/emit-instructions

Conversation

@kumarak

@kumarak kumarak commented Sep 18, 2026 •

Copy link
Copy Markdown
Collaborator

Add an opt-in --emit-instructions export to the Ghidra P-Code serializer:
the machine-level view of every function next to the high P-Code, meant as
input for LLM-driven decompilation (the LLM writes the C, patchir-decomp -from-c -validate-pcode checks it against this JSON, see #284).

With the switch on, every function carries an instructions map right after
address_ranges, one entry per listing instruction keyed by address:

"instructions": {
  "ram:00100000": {
    "text": "PUSH RBP",
    "length": 1,
    "pcode": ["u_4f900:8 = COPY RBP", "RSP = INT_SUB RSP, 0x8", "STORE ram, RSP, u_4f900:8"]
  },
  ...
}

text is Ghidra's disassembly, length the encoded size in bytes, and
pcode the raw (low) P-Code of the instruction, one string per op, rendered
with register names (Varnode.toString(Language)) instead of
(space, offset, size) tuples. Off by default; patchir-decomp ignores the
key.

Steps

Landed as small verified commits, all pushed:

  1. Serializer switch and emission plus JUnit tests.
  2. LOAD/STORE address spaces printed by name (STORE ram, RSP, ...
    instead of STORE 0x1b1, ...), found while inspecting the first output.
  3. --emit-instructions flag in PatchestryDecompileFunctions, forwarding in
    decompile-headless.sh and decompile-entrypoint.sh, LIT test
    test/ghidra/emit_instructions.c.
  4. Docs: scripts/ghidra/README.md, docs/system_data_flow.md, the
    pcode-serialization rule and AGENTS.md.

Testing

  • All 25 Ghidra script sources compile with JDK 21 against the Ghidra 12.0.4
    jars.
  • Docker image rebuilt with the scripts (gradle build succeeds);
    lit test/ghidra --filter emit_instructions passes end to end through
    decompile-headless.sh.
  • JUnit testEmitInstructionsOffByDefault and
    testEmitInstructionsListsEveryBodyInstruction run in the CI test container.

…witch

Add PcodeSerializer.setEmitInstructions and serializeInstructions: one
entry per listing instruction keyed by address, after address_ranges,
with the disassembly text, the encoded length and the raw P-Code as
strings that name registers (Varnode.toString(Language)) instead of
(space, offset, size) tuples. Off by default; no CLI flag yet.
The first input of LOAD and STORE is the address space id as a
constant; print the space name (STORE ram, RSP, ...) the way Ghidra's
listing formatter does, instead of 0x1b1.
…appers

Parse --emit-instructions, --no-emit-instructions and
--emit-instructions=<bool> in PatchestryDecompileFunctions and pass
the switch to PcodeSerializer; forward the flag through
decompile-headless.sh and decompile-entrypoint.sh. Off by default.
test/ghidra/emit_instructions.c runs the flag end to end through the
headless container and checks the x86-64 prologue's readable P-Code.

This branch has not been deployed

No deployments
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