Skip to content

fix(ghidra): report JVM heap exhaustion as a memory constraint - #4275

Closed
RishTheFish21 wants to merge 2 commits into
morluto:mainfrom
RishTheFish21:fix/4241-ghidra-heap
Closed

RishTheFish21 wants to merge 2 commits into
morluto:mainfrom
RishTheFish21:fix/4241-ghidra-heap

Conversation

@RishTheFish21

Copy link
Copy Markdown
Contributor

Summary

When Ghidra's JVM runs out of heap during import or analysis, REA reports a generic execution_failure that tells the caller to run rea doctor. Doctor reports Ghidra as healthy, so the advice leads nowhere. The real cause (java.lang.OutOfMemoryError: Java heap space) is only visible deep inside details.diagnostics.stdout.text.

Now, when a failed Ghidra session's own output shows a JVM OutOfMemoryError, REA reports resource_constraint with resource: "memory", the heap size it launched Ghidra with, and memory-specific remediation.

Fixes #4241

Problem and expected behavior

docs/binary-diagnostics.md says memory allocation failures use resource_constraint, with effective limits (or an explicit unknown) and memory-specific guidance. No Ghidra path recognized OutOfMemoryError, so every heap exhaustion was projected as ProviderAdapterError (execution_failure), or as a timeout if the deadline hit first.

Smallest trigger: any target large enough to exhaust a small heap, e.g. GHIDRA_HEADLESS_MAXMEM=128M rea inspect "$(command -v node)" --provider ghidra --json.

Change and scope

  • src/ghidra/GhidraMemoryExhaustion.ts: looks for java.lang.OutOfMemoryError (with its detail, e.g. "Java heap space") in the retained Ghidra process stdout/stderr. When found, the failure becomes AnalysisResourceConstraintError("memory"):
    • reported_limits.jvm_max_heap is the heap REA launched with, or null if unknown.
    • The remediation is to raise GHIDRA_HEADLESS_MAXMEM above that value or skip heavy analyzers with REA_GHIDRA_DISABLED_ANALYZERS, then reopen. It also notes that rea doctor doesn't check heap size.
    • It applies to start, process, protocol and timeout failures, after cancellation is handled. Bridge-reported (remote) failures and cancellations are unchanged.
  • The launcher records the selected heap on the launch, and diagnostics expose it as jvm_max_heap.
    • On macOS/Linux it's the last -Xmx actually passed to the JVM.
    • On Windows, the headless script picks the heap itself, so only an explicit GHIDRA_HEADLESS_MAXMEM/GHIDRA_MAXMEM is reported. Otherwise it stays unknown.
  • docs/installation.md: documents the new classification under "Ghidra heap and CPU controls".

Not included: the issue's second request (naming REA_GHIDRA_STARTUP_TIMEOUT_MS in startup-timeout remediation) and the optional "Untested Go version" limitation. Both touch different code paths, so I'd do them separately.

Follow the contribution scope.
Unresolved scope, authority, side effects, or required verification block
integration. Link the required maintainer scope decision for new providers or
capabilities involving target execution, injection, mutation, or protection
recovery; ordinary fixes within existing boundaries may state "not applicable".
Draft PRs may leave the scope decision pending and the readiness and merge checklists
unchecked. Maintainers can help resolve open questions.

Contract and boundary impact

  • Semantic owner and earliest changed stage: Ghidra session error projection (projectPrimarySessionError in src/ghidra/GhidraProviderClient.ts); launch diagnostics in GhidraLauncher.ts / GhidraDiagnostics.ts.
  • Contribution scope fit and linked maintainer decision: Bug fix within the existing Ghidra provider boundary. Maintainer decision not applicable.
  • CLI and MCP/tool-catalog contract: Ghidra heap exhaustion now returns the existing resource_constraint code instead of execution_failure/provider_timeout. No schema change.
  • Provider, bridge, target-format, or platform compatibility: Ghidra only. No bridge (Java) change. Windows reports the heap only when it's explicitly configured.
  • Evidence, artifact, provenance, or reconstruction contract: Error-path only. No Evidence or analysis-profile change.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): Not applicable. Cleanup behavior is unchanged.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact (CI generates and validates ignored catalogs and skills/; edit authored instructions in .agents/skills/reverse-engineer-anything/ and never commit build outputs): None.

Expected effects (check known effects; use documentation-only on its own).
Unchecked items do not establish absence; leave uncertainties visible.

  • 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:
    • src/ghidra/GhidraMemoryExhaustion.test.ts: reads OutOfMemoryError with and without detail from stdout and stderr; bounds long details; ignores mentions outside the process streams. Heap selection uses the last POSIX -Xmx, and Windows reports only explicit settings.
    • src/ghidra/GhidraProvider.behavior.test.ts: a process failure with heap exhaustion in stdout projects to resource_constraint with jvm_max_heap: "4G" and the new remediation. A timeout with GC overhead limit exceeded and no recorded heap reports jvm_max_heap: null. Failures without an OOM, and bridge-reported failures, are unchanged.
    • tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts: a real launch records jvmMaxHeap: "1G" and diagnostics expose it.
  • Base reproduction or other evidence: Real Ghidra 12.1.4 on macOS arm64, GHIDRA_HEADLESS_MAXMEM=128M rea inspect "$(command -v node)" --provider ghidra --json:
    • upstream/main: "code":"execution_failure". The only mention of the cause is buried in stdout diagnostics: OutOfMemoryError: Java heap space\n\tat java.base/java.lang.Long.valueOf(...).
    • this branch: "code":"resource_constraint", "jvm_max_heap":"128M", message Ghidra's JVM ran out of memory (java.lang.OutOfMemoryError: Java heap space) with a maximum heap of 128M.
  • User-visible CLI/MCP output (if applicable): see above.
  • Remaining proof gaps: Not run on Linux or Windows. I didn't reproduce the issue's Go/Hugo target or confirm that a larger heap lets it finish. An OutOfMemoryError printed earlier in a long session will classify a later process failure as memory too, since the classification reads the session's retained output.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npm run test:focused -- src/ghidra/GhidraMemoryExhaustion.test.ts src/ghidra/GhidraProvider.behavior.test.ts tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts: passed
  • npm run check:fast: passed
  • npm run lint: passed
  • npm run knip: passed
  • npm run docs:check: passed
  • Real Ghidra 12.1.4 before/after run on macOS arm64 (above): execution_failure → resource_constraint with jvm_max_heap: "128M"

Compatibility, safety, and release

  • Breaking changes or migration steps: Callers that matched execution_failure for Ghidra heap exhaustion now receive resource_constraint (resource: "memory"). Other failures are unchanged.
  • Real Hopper/Ghidra, browser, or OS coverage: Real Ghidra 12.1.4 on macOS arm64. Hopper is unaffected.
  • Package or release metadata impact: None.
  • Security, privacy, process, or containment review: Reads already-retained, already-redacted process output. No new process or target interaction.

Author readiness

Human owner: @RishTheFish21

  • 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.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T00:19:16.844942Z cf884f4 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.

@greptile-apps

greptile-apps Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge; the earlier concern about heap advice is fixed.

Summary

Ghidra failures with a retained JVM OutOfMemoryError now return resource_constraint with memory-specific guidance.

  • Ghidra heap failures now return memory-specific errors.

No new actionable issues were found. Whether another PR supersedes this one could not be checked because GitHub credentials were unavailable.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Failed Ghidra session] --> B{Cancelled or remote?}
  B -->|Yes| C[Keep existing error]
  B -->|No| D{Retained output contains OutOfMemoryError?}
  D -->|No| C
  D -->|Yes| E[Return memory resource_constraint]
  E --> F{Heap failure?}
  F -->|Yes| G[Suggest larger heap within host limits]
  F -->|No| H[Suggest checking host memory and relevant limits]
Loading

Reviews (2) · Last reviewed commit: "fix(ghidra): advise a larger heap only f..." · Reviewed by Greptile

Comment thread src/ghidra/GhidraMemoryExhaustion.ts Resolved

@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: cd2ee3785f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/ghidra/GhidraMemoryExhaustion.ts Resolved
import type { GhidraSessionError } from "./GhidraSessionError.js";

const JAVA_OUT_OF_MEMORY =
/java\.lang\.OutOfMemoryError(?::[ \t]*([^\r\n]*))?/u;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restrict heap guidance to heap-specific OOMs

When Ghidra emits java.lang.OutOfMemoryError: unable to create native thread (which can be caused by OS thread/process limits) or Metaspace, this regex accepts it exactly like Java heap space; ghidraMemoryExhaustionError then labels it a memory constraint and directs the caller to increase GHIDRA_HEADLESS_MAXMEM, although GhidraJavaLaunch.ts uses that setting only for -Xmx. This can misstate the controlling resource and provide ineffective remediation, so apply heap guidance only to heap/GC details or preserve non-heap applicability as unknown.

AGENTS.md reference: AGENTS.md:L21-L21

Useful? React with 👍 / 👎.

morluto added a commit that referenced this pull request Oct 15, 2026
Drain output within caller bounds at asynchronous completion owners and classify JVM exhaustion from the failing operation only. Preserve raw-byte scope, credential redaction, configured heap provenance, cancellation and exact cleanup failures. Validation uses controlled processes and provider fixtures; native Ghidra OOM execution was not repeated.

Refs #4241, #4275

Co-authored-by: Syed Rishad <[email protected]>
Report the JVM memory area (heap, metaspace, native, unknown) and give non-heap out-of-memory errors guidance that does not rely on GHIDRA_HEADLESS_MAXMEM.

@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: cf884f41ef

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

): AnalysisResourceConstraintError | undefined => {
if (failure.kind === "cancelled" || failure.kind === "remote")
return undefined;
const observed = ghidraJavaOutOfMemory(failure.diagnostics);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Detect OOM before truncating process output

When a Ghidra session emits more than GHIDRA_PROCESS_DIAGNOSTIC_BYTES (8 MiB) before the exception, ProviderProcessSupervisor.#capture stops retaining later bytes and only marks the snapshot truncated, so this downstream scan never sees an OOM printed at the end and the failure remains execution_failure or provider_timeout. Capture a structured OOM observation at the Ghidra process-output boundary while draining instead of deriving it only from the bounded diagnostic prefix.

AGENTS.md reference: AGENTS.md:L25-L25

Useful? React with 👍 / 👎.

@kaoru0822-kitauji kaoru0822-kitauji added the ci-fix-wip Claimed by the CI-fix track label Oct 16, 2026
@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator

Conflict check from the CI & conflict-fix track: main's d52a488 ("fix(ghidra): retain scoped failures through process finalization", Refs #4241, #4275, co-authored from this PR) already lands this change. It classifies JVM OutOfMemoryError as resource_constraint, records configured_jvm_max_heap from GhidraLaunch.configuredMaxHeap, and gives heap-specific recovery advice, with provider and client tests. Merging main into this branch conflicts in GhidraLauncher.ts, GhidraProviderClient.ts and GhidraProvider.behavior.test.ts, because the two branches add parallel fields (jvmMaxHeap here, configuredMaxHeap on main) for the same fact. Resolving would remove this PR's changes in favor of main's, so I left the branch untouched. A maintainer may want to close this as superseded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working provider: ghidra Analysis provider / engine: ghidra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Ghidra "Java heap space" during import is reported as execution_failure ("run rea doctor"), not a memory resource constraint

4 participants