fix(ghidra): preserve source failures and snapshot ownership

This commit is contained in:
morluto
2026-10-09 05:10:36 +08:00
parent 2f59287f1a
commit 704fa7699c
8 changed files with 316 additions and 62 deletions
+1 -1
View File
@@ -75,7 +75,7 @@ export const analysisErrorRemediationAction = (
if (error instanceof AnalysisAccessDeniedError)
return "Check the current process's read access to the selected path. Retry with a readable local file.";
if (error instanceof AnalysisArtifactChangedError)
return "Wait until the selected file is stable, then retry this operation.";
return "Wait until the selected file is stable. For an active binary session, reopen the target with open_binary before retrying so REA acquires its current identity; for a CLI command or target-free tool, rerun the operation.";
if (error instanceof AnalysisInputError)
return "Correct the listed arguments and retry.";
if (error instanceof UnknownRegistryError && error.reason === "not-found")
+16
View File
@@ -11,6 +11,8 @@ import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
import type { BinaryTarget } from "../domain/binaryTarget.js";
import {
AnalysisCancelledError,
AnalysisArtifactChangedError,
AnalysisAccessDeniedError,
AnalysisCapabilityUnavailableError,
AnalysisInputError,
AnalysisResourceConstraintError,
@@ -319,6 +321,20 @@ const projectSessionError = (
operation: AnalysisOperation,
failure: GhidraSessionError,
): AnalysisError => {
if (failure.cause instanceof AnalysisAccessDeniedError)
return new AnalysisAccessDeniedError(
operation,
failure.cause.path,
failure.cause.systemCode,
{ cause: failure },
);
if (failure.cause instanceof AnalysisArtifactChangedError)
return new AnalysisArtifactChangedError(
operation,
failure.cause.path,
failure.cause.reason,
{ cause: failure },
);
if (
operation === "annotate_native_function" &&
failure.kind === "remote" &&
+68 -12
View File
@@ -1,9 +1,15 @@
import { createHash } from "node:crypto";
import { constants, createReadStream } from "node:fs";
import { chmod, copyFile, rm } from "node:fs/promises";
import { constants } from "node:fs";
import { open, rm, type FileHandle } from "node:fs/promises";
import { extname, join } from "node:path";
import { Transform } from "node:stream";
import { pipeline } from "node:stream/promises";
import type { JsonValue } from "../domain/jsonValue.js";
import {
AnalysisArtifactChangedError,
AnalysisAccessDeniedError,
} from "../domain/analysisErrorCore.js";
import { windowsPrivateRuntime } from "../windows/WindowsPrivateRuntime.js";
/** Immutable target copy admitted to one ephemeral Ghidra import. */
@@ -37,7 +43,9 @@ export const createGhidraTargetSnapshot = async (
options.signal,
);
if (observed.sha256 !== expectedSha256)
throw new Error(
throw new AnalysisArtifactChangedError(
"open_binary",
sourcePath,
`Ghidra target snapshot digest mismatch: expected ${expectedSha256}, observed ${observed.sha256}`,
);
return {
@@ -46,23 +54,71 @@ export const createGhidraTargetSnapshot = async (
admission: { source: observed.source, snapshot: observed.snapshot },
};
}
let source: FileHandle | undefined;
let destination: FileHandle | undefined;
try {
await copyFile(sourcePath, snapshotPath, constants.COPYFILE_EXCL);
await chmod(snapshotPath, 0o600);
source = await openSnapshotSource(sourcePath);
if (!(await source.stat()).isFile())
throw new AnalysisArtifactChangedError(
"open_binary",
sourcePath,
"The selected Ghidra source is no longer a regular file",
);
destination = await open(snapshotPath, "wx", 0o600);
await destination.chmod(0o600);
const hash = createHash("sha256");
for await (const chunk of createReadStream(snapshotPath))
hash.update(chunk);
const digest = new Transform({
transform(chunk: unknown, _encoding, done) {
if (!Buffer.isBuffer(chunk)) {
done(new TypeError("Ghidra snapshot stream must contain bytes"));
return;
}
hash.update(chunk);
done(null, chunk);
},
});
await pipeline(
source.createReadStream(),
digest,
destination.createWriteStream(),
options.signal === undefined ? {} : { signal: options.signal },
);
const observedSha256 = hash.digest("hex");
if (observedSha256 !== expectedSha256)
throw new Error(
throw new AnalysisArtifactChangedError(
"open_binary",
sourcePath,
`Ghidra target snapshot digest mismatch: expected ${expectedSha256}, observed ${observedSha256}`,
);
return { path: snapshotPath, sha256: observedSha256 };
} catch (cause: unknown) {
await rm(snapshotPath, { force: true }).catch((cause: unknown) => {
// best-effort cleanup: snapshot removal must not mask the copy failure.
void cause;
});
if (destination !== undefined)
await rm(snapshotPath, { force: true }).catch((cause: unknown) => {
// best-effort cleanup: snapshot removal must not mask the copy failure.
void cause;
});
throw cause;
} finally {
await Promise.all([source?.close(), destination?.close()]);
}
};
const openSnapshotSource = async (path: string): Promise<FileHandle> => {
try {
// A path replaced by a FIFO must not block startup before its type is checked.
return await open(path, constants.O_RDONLY | constants.O_NONBLOCK);
} catch (cause: unknown) {
const code =
cause instanceof Error && "code" in cause ? cause.code : undefined;
if (code === "EACCES" || code === "EPERM")
throw new AnalysisAccessDeniedError("open_binary", path, code, { cause });
if (code === "ENOENT" || code === "ENOTDIR" || code === "ELOOP")
throw new AnalysisArtifactChangedError(
"open_binary",
path,
`The selected Ghidra source could not be opened (${code})`,
{ cause },
);
throw cause;
}
};