Skip to content

fix(ghidra): reuse completed decompiles and decompile code outside functions - #14843

Merged
morluto merged 6 commits into
mainfrom
fix/ghidra-bridge-decompile-reuse-orphan-code
Nov 27, 2026
Merged

morluto merged 6 commits into
mainfrom
fix/ghidra-bridge-decompile-reuse-orphan-code

Conversation

@morluto

@morluto morluto commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes #14826 and addresses the dead end in #14829. The Java bridge keeps completed decompilations of the current Program state, and procedure_pseudo_code can decompile disassembled code that Ghidra left outside every function.

Problem and expected behavior

  1. DecompInterface keeps no results, so every bridge request re-ran the native decompiler. Four calls on one large function took about 15 s. analyze_function is declared live on the stated assumption that Ghidra retains its own decompiler cache, which it does not.

  2. Ghidra's exception-handler analysis disassembles each .eh_frame range without creating a function. main in a stripped ELF is only passed to __libc_start_main as a pointer, so it has code and no function. On clang- and GCC-built stripped x86_64 ELF (including Debian ls, sort, true) every REA route to it failed, and the default error described a missing file-offset mapping.

Change and scope

Decompile reuse: decompile(Function) consults a bounded least-recently-used map of soft references keyed by entry address, whether the function is undefined, Program.getModificationNumber() and the payload limit. Only completed results are stored. The existing invalidation hook and the payload-limit path clear it; the modification number makes a missed invalidation a miss rather than a stale hit. The entry bound is a fixed ceiling with its reason stated in the source.

Code outside functions: when a decompile selector resolves to no procedure but names an analysis address holding disassembled code outside every function, the bridge builds ghidra.util.UndefinedFunction as Ghidra's decompiler panel does and decompiles that. The Program is unchanged. The result's limitations state that the entry was inferred from control flow and is absent from the inventory. This reading precedes the file-offset fallback. procedure_not_found and file_offset_unmapped messages now say when the address holds code without a function or is a mapped address with no code. A file offset names the same location as its mapped analysis address, so an offset that maps to such code is converted once and takes the same UndefinedFunction path, with the same limitation; its selection reports the file offset, the mapped address and the inferred entry as procedure_address. The refusal that sent the caller back to the address form is removed.

Not included: making the procedure inventory complete (on Debian ls, 139 .eh_frame starts hold code with no function), parallel decompilation, and any change to other procedure operations, which still require a function.

Contribution scope: not applicable; a fix within existing boundaries.

Contract and boundary impact

  • Semantic owner and earliest changed stage: The bridge owns Program state, the decompiler and selector resolution. The TypeScript result contract is unchanged; the undefined-function facts travel in limitations.
  • Contribution scope fit and linked maintainer decision: ordinary fix within existing boundaries; not applicable
  • CLI and MCP/tool-catalog contract: No schema change. procedure_pseudo_code and CLI decompile succeed for an address, or a file offset mapping to it, that they previously refused. Error text for two failure codes is extended.
  • Provider, bridge, target-format, or platform compatibility: Ghidra bridge only. Uses ghidra.util.UndefinedFunction and Program.getModificationNumber, both present in Ghidra 12.2; older supported releases were not checked.
  • Evidence, artifact, provenance, or reconstruction contract: A decompile of code outside a function is marked as inferred through its limitation. Inventory results are unchanged, so absence from list_procedures remains visible.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): none
  • Generated metadata, package, or installation impact: none

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: None added. The bridge's Program-level behavior is covered by the real-Ghidra verifiers, which were not extended in this PR.
  • Base reproduction or other evidence: Executed, real Ghidra. Reuse: repeat procedure_pseudo_code 11 ms; repeat analyze_function 325–481 ms, previously 3.6–4.4 s. After annotate_native_function renamed a callee, the caller's next decompile showed the new name. Code outside functions: procedure_pseudo_code at main of a stripped ELF returned pseudocode with the limitation; an interior address resolved to the same entry; list_procedures was unchanged. procedure_pseudo_code with file-offset:0x860, which maps to that main at 0x1001860, returned the same UndefinedFunction_01001860 pseudocode and limitation with selection {file_offset: 2144, mapped_address: 0x1001860, procedure_address: 0x1001860}; the address request that followed was answered from the cache in 3 ms.
  • User-visible CLI/MCP output (if applicable): Limitation text: "No Ghidra function contains 0x1001860: Ghidra disassembled this code without creating a function, so it is absent from the procedure inventory. This pseudocode is Ghidra's on-the-fly decompilation from 0x1001860, an entry inferred by following control flow back from the requested address. The entry and extent are not an analyzed function boundary, and the Program is unchanged."
  • Remaining proof gaps: No automated regression for either behavior. Not run: npm run verify:ghidra. Heap behavior of 32 retained results on very large functions was not measured; soft references are the mitigation. Only x86_64 ELF and one Mach-O were exercised.

Validation performed

  • npm run test:focused -- tests/boundary/filesystem/ghidraBridgeSources.test.ts tests/boundary/providers/ghidra/ghidraBridgeHelpers.test.ts tests/boundary/providers/ghidra/ghidraClient.test.ts tests/boundary/providers/ghidra/ghidraCheckpointLifecycle.test.ts — 4 files, 143 tests passed
  • npm run docs:check — passed
  • node scripts/verify-ghidra-bridge-sources.mjs against Ghidra 12.2 — bridge compiles
  • npm run test:focused -- tests/boundary/mcp/ghidraAmbiguousProcedureMcp.test.ts tests/boundary/mcp/pseudocodeDeliveryMcp.test.ts — 11 tests passed
  • Manual MCP sessions against Ghidra 12.2 — results above
  • npm run check:fast — typecheck, lint and architecture guards passed
  • npx oxfmt --check on the changed files — passed

Compatibility, safety, and release

  • Breaking changes or migration steps: none
  • Real Hopper/Ghidra, browser, or OS coverage: Ghidra 12.2 on macOS arm64, Node 24.22.1, Ghidra 12.2, JDK 25, by hand.
  • Package or release metadata impact: none
  • Security, privacy, process, or containment review: no new authority, process or file effects

Author readiness

Human owner: @morluto

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Review follow-up

  • A request that carries no decompiler options, such as procedure_info or an instruction read, now leaves the session's payload limit and completed decompiles unchanged. An explicit null still selects the installed Ghidra default.
  • A reused decompile of code outside every function reports boundary limitations for the body Ghidra widened during decompilation, not for a fresh entry-only lookup.

@morluto
morluto marked this pull request as ready for review November 27, 2026 03:08
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T08:31:27.106728Z 62b33ed New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2036a313c3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread bridge/ghidra/ReaGhidraBridge.java Resolved
@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no blocking issues remain.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking issues remain.

Summary

The bridge reuses completed decompilations and can decompile disassembled code outside known functions without changing the Program.

  • Pseudocode requests can read disassembled code outside functions.
  • Ghidra reuses completed decompiles for unchanged Program state.

Reviews (6) · Last reviewed commit: "docs(ghidra): state what an omitted deco..." · Reviewed by Greptile

Comment thread bridge/ghidra/ReaGhidraBridge.java Resolved
@morluto

morluto commented Nov 27, 2026

Copy link
Copy Markdown
Owner Author

Added one commit: the list_procedures / procedure_address capability limitation now states that the inventory lists Ghidra functions only, that code disassembled without a function (such as main in a stripped ELF) is absent, and that list_names reports it as a label while procedure_pseudo_code decompiles it by address.

Checked on the stripped test ELF: list_names already returns LAB_01001860 (type label, dynamic: true) at main, and xrefs to it works. So candidates are enumerable through existing contracts and no function is created. This keeps the inventory as Ghidra observed it and makes the gap visible, instead of asserting function boundaries Ghidra declined to assert.

Validation: npm run check:fast passed; npm run test:focused -- tests/boundary/mcp/ghidraInventoryMcp.test.ts tests/boundary/providers/ghidra/ghidraClient.test.ts — 123 tests passed; npm run docs:check passed. The limitation is static text; it does not report how much code is outside functions.

A file-offset selector that mapped to disassembled code without a function
failed with advice to choose an inventory procedure, which cannot name that
code. The failure now states the fact and that procedure_pseudo_code can
decompile it by the mapped analysis address.

One predicate now decides that an address holds code outside every function
for the address, file-offset and procedure_not_found paths, and the bridge
imports UndefinedFunction and SoftReference like its other dependencies.
@morluto

morluto commented Nov 27, 2026

Copy link
Copy Markdown
Owner Author

Review summary

Reviewed the diff and the surrounding bridge code against Ghidra 12.2 sources and real runs (macOS arm64, JDK 25). One commit pushed: ac0a705.

Confirmed and fixed

  • A file offset that maps to code outside every function was still a dead end. On demo_stripped.elf, procedure_pseudo_code with file-offset:0x860 (and bare 0x860) failed with "maps to analysis address 0x1001860, but no procedure starts at or contains that address ... Choose an observed procedure with ... list_procedures", although 0x1001860 now decompiles on the fly and no inventory entry can name it. The failure code is unchanged; the message now states that Ghidra disassembled code there without a function and that procedure_pseudo_code can decompile it by that analysis address. Confirmed before and after with a real MCP session; 0x40 (maps to a header, no code) keeps the old advice. The file-offset path does not decompile on the fly itself: that would change the selection contract and is left for a maintainer decision.
  • Cleanup in the same commit. One predicate (isCodeOutsideFunctions) and one sentence constant now serve the address path, the file-offset path and the procedure_not_found message, which each made the decision separately. UndefinedFunction and SoftReference are imported.

Checked, no defect found

  • Cache staleness. getModificationNumber() is incremented in DomainObjectAdapter.fireEvent for every change record, including undo/rollback, never reset on save, and is per Program instance in one JVM. decompileOptions builds fresh DecompileOptions and never reads Program options, so the payload limit is the only option input. The timeout only decides whether a result completes, and only completed results are stored. Value-flow limits apply to the projection, not the decompile. Real run: after set_address_name on a callee, both the real function and the on-the-fly decompile of main showed the new name.
  • Sharing results. DecompileResults decodes eagerly in its constructor, so a retained result does not read the interface's shared decoder later. analyze_function complete dossiers for three functions and the summary for entry had identical digests fresh and from the cache (entry: about 4.0 s, then about 0.3 s).
  • Undefined-function path. An entry, an interior address, a mid-instruction address, bare hex and ram:0x... all resolve to the same entry with the limitation naming the requested address. A mapped address with no code, an unmapped address, a thunk in the external block and a name are unchanged or report the new text accurately. An UndefinedFunction key carries its own flag, so it is never served for a real function. resolveDecompileProcedure has one consumer.
  • Limits. rea-numeric-limits and fixed-ceilings pass; the 32-entry bound is a retention size with its reason in the source, not a refusal.

Left open, with reasons

  • UndefinedFunction.findFunction returns null when every block in the region has a non-call predecessor (a loop with no entry) or on cancellation. The request then falls through to the file-offset reading, whose message says the address "holds no disassembled code". Not reproduced; needs a crafted target. Rare, and fixing it means a third failure wording.
  • Walking back from code outside functions can end at a real function's entry when a jump from that function reaches the code. The result is then an on-the-fly decompile from that entry without the function's signature. Ghidra's own panel behaves the same way. Not reproduced.
  • A label on such code (set_address_name at 0x1001860, then procedure: "my_main") is not a decompile selector: "Unknown Ghidra procedure name or address". The limitation text says "by address", so it is accurate; whether names should resolve is a product decision.
  • No automated regression for either behavior or for the new message. The real-Ghidra verifiers have no fixture with code outside functions, and adding one is beyond a targeted run.

Commands

  • Real MCP sessions against Ghidra 12.2 on demo_stripped.elf and ssh_x64: results above.
  • npm run test:focused -- tests/boundary/filesystem/ghidraBridgeSources.test.ts tests/boundary/providers/ghidra/ghidraBridgeHelpers.test.ts: 2 files, 20 tests passed.
  • GHIDRA_INSTALL_DIR=... node scripts/verify-ghidra-bridge-sources.mjs: bridge sources compile against Ghidra 12.2.
  • npm run docs:check: passed. node scripts/verify/rea-numeric-limits.mjs and node scripts/verify/fixed-ceilings.mjs: passed.
  • npx oxfmt --check on the changed doc: passed.
  • Not run: npm run verify:ghidra, npm run check:pr.

A file offset names the same location as its mapped analysis address, but
an offset that mapped to code Ghidra disassembled without a function was
refused while the address decompiled. The offset now resolves through the
same on-the-fly UndefinedFunction path and limitation, and its selection
reports the inferred entry as procedure_address. The refusal that sent the
caller back to the address form is removed.
…ptions

A request that omits decompile_max_payload_mib now leaves the session's
payload limit unchanged instead of resetting it to the Ghidra default,
so an instruction or metadata read no longer discards completed
decompiles. An explicit null still selects the installed default.

Boundary limitations for a reused decompile of code outside every
function now use the function the decompile widened, not a fresh
entry-only UndefinedFunction.
@morluto
morluto merged commit a3a491c into main Nov 27, 2026
44 checks passed
@morluto
morluto deleted the fix/ghidra-bridge-decompile-reuse-orphan-code branch November 27, 2026 09:35
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.

[Feature] Cache completed decompile results in the Ghidra bridge so analyze_function and repeats do not re-decompile

1 participant