Skip to content

fix(ghidra): resolve keg-only Homebrew openjdk on macOS (#4556) - #4557

Merged
morluto merged 4 commits into
morluto:mainfrom
vadlachary2002:patch-1
Oct 16, 2026
Merged

morluto merged 4 commits into
morluto:mainfrom
vadlachary2002:patch-1

Conversation

@vadlachary2002

@vadlachary2002 vadlachary2002 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes an issue where rea doctor --provider ghidra fails the ghidra-java check on macOS when OpenJDK is installed via Homebrew. Homebrew's openjdk is keg-only, so it is neither on PATH nor resolved by /usr/libexec/java_home.

Fixes #4556

Problem and expected behavior

Problem: When options.javaHome is undefined, the provider defaulted to a bare "java" command. On a default macOS setup with Homebrew (brew install ghidra openjdk), java is not available on PATH, causing REA to report detail: java with status missing_dependency even though a fully functional JDK exists in Homebrew's keg directory.

Expected behavior: ghidra-java checks standard Homebrew installation paths for a usable JDK before reporting it as missing, allowing rea doctor to pass without requiring manual JAVA_HOME configuration.

Change and scope

When javaHome is unset on macOS, preserve Java on the caller's absolute PATH entries; otherwise check the standard Homebrew OpenJDK paths, preferring /opt/homebrew for arm64 and /usr/local for x64. Retain the bare Java command if no Homebrew executable is found. Explicit JAVA_HOME remains authoritative, and Linux/Windows keep their existing selection.

The merge resolution preserves main's typed Java probe results and its refusal to execute relative JAVA_HOME paths. Discovery checks absolute executable paths rather than interpreting a file named java in the working directory as a PATH match. Added six regression cases for macOS architecture preference, caller PATH, explicit JAVA_HOME, and Linux/Windows selection.

Contract and boundary impact

  • Semantic owner and earliest changed stage: ghidra provider admission / setup check (inspectGhidraInstallation).
  • Contribution scope fit and linked maintainer decision: Not applicable (ordinary fix within existing provider setup boundaries).
  • CLI and MCP/tool-catalog contract: None.
  • Provider, bridge, target-format, or platform compatibility: macOS (darwin/arm64 and darwin/x64) with Homebrew-installed OpenJDK.
  • Evidence, artifact, provenance, or reconstruction contract: None.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): Not applicable.
  • 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: Six installation-discovery regression cases; existing Java probe and doctor boundary tests also passed.
  • Base reproduction or other evidence: On macOS with brew install ghidra openjdk and JAVA_HOME unset, rea doctor --provider ghidra returned ghidra-java: ok: false, detail: java.
  • User-visible CLI/MCP output (if applicable): rea doctor --provider ghidra now passes all checks including ghidra-java.
  • Remaining proof gaps: The merge resolution was not verified against real Homebrew/Ghidra installations. Executable PATH Java remains authoritative even if it is the macOS stub or an unsupported JDK; automatic retry after a failed Java probe is not implemented.

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.

Merge-resolution validation

Merged main at 360626434 in c0accec68. Executed with Node 24.18.0 on macOS:

  • npm run test:focused -- src/ghidra/GhidraInstallation.test.ts src/ghidra/GhidraJavaProbe.test.ts tests/boundary/filesystem/ghidraJavaProbe.test.ts tests/boundary/cli/ghidraJavaDoctor.test.ts: passed, 121 tests across four files. Discovery uses simulated host observations; doctor tests use controlled Java processes.
  • npm run check:fast: passed (typecheck and lint).
  • git diff origin/main...HEAD --check: passed.
  • Real-provider verification was not rerun for this resolution; the author's earlier result below applies to the earlier revision.

Original author validation (before merge resolution)

  • env -u JAVA_HOME GHIDRA_INSTALL_DIR=/opt/homebrew/Cellar/ghidra/12.1.4/libexec PATH="/usr/bin:/bin:/opt/homebrew/bin" rea doctor --provider ghidra — ghidra-java reports ok: true.

Compatibility, safety, and release

  • Breaking changes or migration steps: None.
  • Real Hopper/Ghidra, browser, or OS coverage: Ghidra 12.1.4 on macOS (darwin/arm64).
  • Package or release metadata impact: None.
  • Security, privacy, process, or containment review: Safe; all newly probed paths are absolute standard Homebrew system paths.

Author readiness

Human owner:

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

Fixes an issue where the `ghidra-java` check fails when OpenJDK is installed via Homebrew on macOS (where it is keg-only and not added to `PATH` or resolved by `java_home`).

- **Problem:** When `options.javaHome` is undefined, the provider defaulted to a bare `"java"` command, causing valid Homebrew OpenJDK installations to be reported as missing dependencies.
- **Solution:** Replaced the single fallback with an absolute candidate list (`javaCandidates`) that probes standard Homebrew OpenJDK paths (`/opt/homebrew/opt/openjdk/...` and `/usr/local/opt/openjdk/...`) before falling back to `"java"`. All candidates are absolute, preserving existing constraints and current behavior.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 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-16T22:44:57.950262Z c0accec 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.

@morluto

morluto commented Oct 16, 2026

Copy link
Copy Markdown
Owner

looks good!

@greptile-apps

greptile-apps Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] Changes how the system finds Java on macOS.

The PR should not merge until a failing macOS Java launcher allows Homebrew discovery to continue.

Findings

  1. P1 Java launcher blocks Homebrew fallback ▶
Fix with agent prompt
### Issue 1
src/ghidra/GhidraInstallation.ts:459-464
If `/usr/bin/java` is an executable macOS launcher with no registered JDK, this check returns `"java"` before checking Homebrew. The launcher then fails, and the single `probeJava` call never tries the installed Homebrew JDK. The setup described in the PR still fails `rea doctor`.

Probe the selected command before accepting it, and try Homebrew when it cannot provide a usable JDK. Add a test with an executable but failing `/usr/bin/java`.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds macOS Homebrew OpenJDK discovery when no javaHome is configured.

  • Ghidra checks Homebrew Java on macOS when JAVA_HOME is unset.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[No configured javaHome on macOS] --> B{Executable java on PATH?}
  B -->|Yes| C[Select bare java]
  B -->|No| D[Select first executable Homebrew Java]
  D --> E[Probe selected command]
  C --> E
  E -->|Usable JDK| F[Continue installation checks]
  E -->|Probe fails| G[Report failure without trying another candidate]
Loading

Reviews (4) · Last reviewed commit: "fix(ghidra): resolve Homebrew Java disco..." · Reviewed by Greptile

Comment thread src/ghidra/GhidraInstallation.ts Resolved
Comment thread src/ghidra/GhidraInstallation.ts Resolved
Comment thread src/ghidra/GhidraInstallation.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: 139537c1e5

ℹ️ 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/GhidraInstallation.ts Resolved
…uto#4556)

Fixes a TypeScript compilation error (`TS2322` and `TS2345`) in `src/ghidra/GhidraInstallation.ts` where `options.javaHome` was inferred as `string | undefined` inside `path.join()`.

- **Problem:** The ternary operator structure prevented TypeScript from narrowing `options.javaHome` to `string` in the `path.join()` call when `options.javaHome` was defined, causing CI compile checks to fail.
- **Solution:** Refactored the `javaCandidates` assignment using an immediately invoked function expression (IIFE) with an explicit `if (options.javaHome === undefined)` check. This properly narrows `options.javaHome` to `string` in the non-undefined branch while keeping the same logic and Homebrew OpenJDK paths intact.

@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: 4709e43383

ℹ️ 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/GhidraInstallation.ts Resolved
vadlachary2002 and others added 2 commits October 17, 2026 00:45
…luto#4556)

Updates the Homebrew OpenJDK candidate ordering in `src/ghidra/GhidraInstallation.ts` to be architecture-aware, addressing review feedback regarding cross-architecture runs (e.g., x64 under Rosetta on Apple Silicon).

- **Problem:** Previously, `/opt/homebrew` was always checked before `/usr/local`. When running REA as an x64 process under Rosetta on Apple Silicon, this could incorrectly select an ARM64 JDK over an Intel/x64 one, triggering a `nativeDecompilerCheck` failure and missing the valid fallback.
- **Solution:** Dynamically orders `javaCandidates` based on the runtime `architecture`. On `arm64`, `/opt/homebrew` is prioritized; on `x64`, `/usr/local` is prioritized. Bare `"java"` remains first in both cases to preserve standard PATH resolution.
Preserve typed Java probe failures and rejection of relative JAVA_HOME from main. Resolve PATH candidates against the caller environment, limit Homebrew discovery to macOS, and cover architecture preference and explicit selection.
@morluto
morluto merged commit 8267edc into morluto:main Oct 16, 2026
1 check passed
Comment on lines +459 to +464
const javaOnPath = options.environment.PATH?.split(":").some(
(directory) =>
path.isAbsolute(directory) &&
host.executable(path.join(directory, "java")),
);
if (javaOnPath) return "java";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Java launcher blocks Homebrew fallback

If /usr/bin/java is an executable macOS launcher with no registered JDK, this check returns "java" before checking Homebrew. The launcher then fails, and the single probeJava call never tries the installed Homebrew JDK. The setup described in the PR still fails rea doctor.

Probe the selected command before accepting it, and try Homebrew when it cannot provide a usable JDK. Add a test with an executable but failing /usr/bin/java.

Knowledge Base Used: Ghidra integration

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/ghidra/GhidraInstallation.ts
Line: 459-464

Comment:
**Java launcher blocks Homebrew fallback**

If `/usr/bin/java` is an executable macOS launcher with no registered JDK, this check returns `"java"` before checking Homebrew. The launcher then fails, and the single `probeJava` call never tries the installed Homebrew JDK. The setup described in the PR still fails `rea doctor`.

Probe the selected command before accepting it, and try Homebrew when it cannot provide a usable JDK. Add a test with an executable but failing `/usr/bin/java`.

**Knowledge Base Used:** [Ghidra integration](https://app.greptile.com/pre/-/custom-context/knowledge-base/morluto/rea/-/docs/ghidra-integration.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

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

ℹ️ 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".

path.isAbsolute(directory) &&
host.executable(path.join(directory, "java")),
);
if (javaOnPath) return "java";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep probing after the macOS Java stub fails

When macOS has only an unregistered keg-only JDK and PATH includes /usr/bin (the normal PATH=/usr/bin:/bin:... case), /usr/bin/java is still executable even though it exits with the system-stub error; the repository exercises that state in scripts/verify/android/failure-recovery.mjs:120-129. javaOnPath is therefore true, this branch probes bare java, and the single probe fails without ever trying either Homebrew keg, so the change still reports the exact installation it is meant to discover. Continue to the architecture-matching keg when the system wrapper is unavailable; the fake-host test's executable-only PATH model does not cover this real provider state.

AGENTS.md reference: AGENTS.md:L79-L79

Useful? React with 👍 / 👎.

Comment on lines +459 to +462
const javaOnPath = options.environment.PATH?.split(":").some(
(directory) =>
path.isAbsolute(directory) &&
host.executable(path.join(directory, "java")),

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 Return the absolute PATH candidate that was checked

When macOS PATH has a relative or empty entry before a valid absolute one (for example PATH=.:/usr/bin) and the working directory contains an executable java, this scan ignores the relative entry, observes /usr/bin/java, and then returns bare java; probeGhidraJava passes the unchanged PATH to spawnSync (src/ghidra/GhidraJavaProbe.ts:93-108), so it actually executes ./java first. This defeats the stated working-directory exclusion and can record and launch a different JDK than the absolute candidate that was checked; retain and return the matched absolute path instead.

AGENTS.md reference: AGENTS.md:L13-L13

Useful? React with 👍 / 👎.

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 check fails with Homebrew keg-only openjdk on macOS

2 participants