Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(ghidra): retain stable Homebrew installation aliases

morluto7c6142efdc19
diff --git a/src/ghidra/GhidraInstallation.test.ts b/src/ghidra/GhidraInstallation.test.ts
index 2118c0e30..50b2b11a8 100644
--- a/src/ghidra/GhidraInstallation.test.ts
+++ b/src/ghidra/GhidraInstallation.test.ts
@@ -9,7 +9,7 @@ import {
parseGhidraJavaOutput,
type GhidraJavaObservation,
} from "./GhidraJavaProbe.js";
-import { chmod, mkdir, symlink, writeFile } from "node:fs/promises";
+import { chmod, mkdir, rm, symlink, unlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -153,6 +153,112 @@ it.skipIf(process.platform === "win32")(
},
);
+it.skipIf(process.platform !== "darwin")(
+ "uses a same-installation Homebrew opt alias for doctor registration and survives keg rotation",
+ async () => {
+ const temporaryRoot = await createTestTempDirectory("rea-ghidra-homebrew-");
+ const prefix = join(temporaryRoot, "brew\n雪");
+ await mkdir(prefix);
+ const pathDirectory = join(prefix, "bin");
+ const optDirectory = join(prefix, "opt");
+ const optAlias = join(optDirectory, "ghidra");
+ const firstKeg = join(prefix, "Cellar", "ghidra", "12.1.4");
+ const secondKeg = join(prefix, "Cellar", "ghidra", "12.1.5");
+ const foreignKeg = join(prefix, "Cellar", "other-ghidra", "12.1.4");
+
+ const createKeg = async (keg: string) => {
+ const libexec = join(keg, "libexec");
+ await mkdir(join(libexec, "Ghidra"), { recursive: true });
+ await mkdir(join(libexec, "support"), { recursive: true });
+ await writeFile(
+ join(libexec, "Ghidra", "application.properties"),
+ `application.version=${SUPPORTED_GHIDRA_VERSION}\napplication.java.min=${String(SUPPORTED_GHIDRA_JAVA_MAJOR)}\n`,
+ );
+ await writeFile(join(libexec, "ghidraRun"), "launcher\n");
+ await writeFile(
+ join(libexec, "support", "analyzeHeadless"),
+ "headless\n",
+ );
+ await chmod(join(libexec, "ghidraRun"), 0o700);
+ await chmod(join(libexec, "support", "analyzeHeadless"), 0o700);
+ return libexec;
+ };
+
+ const firstRoot = await createKeg(firstKeg);
+ const secondRoot = await createKeg(secondKeg);
+ await createKeg(foreignKeg);
+ await mkdir(pathDirectory, { recursive: true });
+ await mkdir(optDirectory, { recursive: true });
+ await symlink(firstKeg, optAlias);
+ await symlink(
+ join(optAlias, "libexec", "ghidraRun"),
+ join(pathDirectory, "ghidra"),
+ );
+
+ const environment = { PATH: pathDirectory };
+ const registeredRoot = join(optAlias, "libexec");
+ expect(
+ discoverGhidraInstallationOnPath(environment, process.platform),
+ ).toBe(registeredRoot);
+ const inspection = await inspectGhidraInstallation(
+ {
+ environment,
+ resourceOwner: inspectionResources.owner,
+ platform: "darwin",
+ architecture: "arm64",
+ },
+ host({
+ discoverInstallDirOnPath: discoverGhidraInstallationOnPath,
+ readText: (path) =>
+ path === join(registeredRoot, "Ghidra", "application.properties")
+ ? `application.version=${SUPPORTED_GHIDRA_VERSION}\napplication.java.min=${String(SUPPORTED_GHIDRA_JAVA_MAJOR)}\n`
+ : undefined,
+ executable: (path) =>
+ path === join(registeredRoot, "support", "analyzeHeadless") ||
+ path ===
+ join(
+ registeredRoot,
+ "Ghidra",
+ "Features",
+ "Decompiler",
+ "os",
+ "mac_arm_64",
+ "decompile",
+ ),
+ probeJava: () =>
+ javaProbeOk({ ...JAVA, osArch: "aarch64", osName: "Mac OS X" }),
+ }),
+ );
+ expect(inspection.status).toBe("available");
+ expect(inspection.installDir).toBe(registeredRoot);
+ expect(
+ projectGhidraDoctorInspection(inspection).registrationEnvironment
+ .GHIDRA_INSTALL_DIR,
+ ).toBe(registeredRoot);
+
+ await unlink(optAlias);
+ await symlink(secondKeg, optAlias);
+ await rm(firstKeg, { recursive: true });
+ expect(
+ discoverGhidraInstallationOnPath(environment, process.platform),
+ ).toBe(registeredRoot);
+ expect(
+ discoverGhidraInstallationOnPath(environment, process.platform),
+ ).not.toBe(firstRoot);
+
+ await unlink(join(pathDirectory, "ghidra"));
+ await symlink(join(secondRoot, "ghidraRun"), join(pathDirectory, "ghidra"));
+ await unlink(optAlias);
+ expect(
+ discoverGhidraInstallationOnPath(environment, process.platform),
+ ).toBe(secondRoot);
+ await symlink(foreignKeg, optAlias);
+ expect(
+ discoverGhidraInstallationOnPath(environment, process.platform),
+ ).toBe(secondRoot);
+ },
+);
+
it("does not fall back to PATH when an explicit Ghidra installation path is malformed", async () => {
const inspection = await inspectGhidraInstallation(
{
diff --git a/src/ghidra/GhidraInstallation.ts b/src/ghidra/GhidraInstallation.ts
index 4f04d3397..a56c5adec 100644
--- a/src/ghidra/GhidraInstallation.ts
+++ b/src/ghidra/GhidraInstallation.ts
@@ -1034,7 +1034,7 @@ export const discoverGhidraInstallationOnPath = (
);
try {
if (statSync(properties).isFile() && executablePath(headless))
- return root;
+ return stableGhidraInstallationRoot(root, platform);
} catch (cause: unknown) {
void cause;
}
@@ -1043,6 +1043,42 @@ export const discoverGhidraInstallationOnPath = (
return undefined;
};
+/**
+ * Keep a Homebrew Ghidra registration stable across keg upgrades when its
+ * `opt` alias resolves to this exact, already validated installation.
+ */
+const stableGhidraInstallationRoot = (
+ root: string,
+ platform: NodeJS.Platform,
+): string => {
+ if (platform !== "darwin") return root;
+ const parsed = posix.parse(root);
+ const segments = root.slice(parsed.root.length).split(posix.sep);
+ const cellarIndex = segments.length - 4;
+ const formula = segments[cellarIndex + 1];
+ const version = segments[cellarIndex + 2];
+ if (
+ cellarIndex < 0 ||
+ segments[cellarIndex] !== "Cellar" ||
+ formula === undefined ||
+ formula.length === 0 ||
+ version === undefined ||
+ version.length === 0 ||
+ segments[cellarIndex + 3] !== "libexec"
+ )
+ return root;
+
+ const prefix = posix.join(parsed.root, ...segments.slice(0, cellarIndex));
+ const candidate = posix.join(prefix, "opt", formula, "libexec");
+ try {
+ return realpathSync(candidate) === root ? candidate : root;
+ } catch (cause: unknown) {
+ // An absent or unresolved alias cannot establish a stable installation identity.
+ void cause;
+ return root;
+ }
+};
+
const executablePath = (path: string): boolean => {
try {
accessSync(path, constants.X_OK);