fix(hopper): resolve FAT loaders and original file coordinates
This commit is contained in:
@@ -10,7 +10,11 @@ import type {
|
||||
BinaryArchitecture,
|
||||
BinaryTarget,
|
||||
} from "../domain/binaryTarget.js";
|
||||
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
AnalysisCapabilityUnavailableError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { BinaryTargetError } from "../domain/configurationErrors.js";
|
||||
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
||||
import { type AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
@@ -29,7 +33,17 @@ export const resolveHopperAnalysisProfile = async (
|
||||
): Promise<Result<AnalysisProfileResolution, AnalysisError>> => {
|
||||
if (target.kind !== "executable" && target.kind !== "database")
|
||||
return ok({ profile: null, compatibility: {} });
|
||||
const derived = hopperLoaderArgsForTarget(target);
|
||||
let machoContainer: "thin" | "fat32" | "fat64" | undefined;
|
||||
if (target.kind === "executable" && target.format === "mach-o") {
|
||||
const header = await readMachoMagic(target.path, options.signal);
|
||||
if (!header.ok) return header;
|
||||
machoContainer = [0xcafebabf, 0xbfbafeca].includes(header.value)
|
||||
? "fat64"
|
||||
: [0xcafebabe, 0xbebafeca].includes(header.value)
|
||||
? "fat32"
|
||||
: "thin";
|
||||
}
|
||||
const derived = hopperLoaderArgsForTarget(target, machoContainer);
|
||||
if (!derived.ok) return derived;
|
||||
const loaderArgs =
|
||||
options.loaderArgsOverride.length === 0
|
||||
@@ -53,6 +67,9 @@ export const resolveHopperAnalysisProfile = async (
|
||||
available_architectures: [
|
||||
...(target.availableArchitectures ?? []),
|
||||
].sort(),
|
||||
...(machoContainer === undefined
|
||||
? {}
|
||||
: { macho_container: machoContainer }),
|
||||
loader: {
|
||||
source:
|
||||
options.loaderArgsOverride.length === 0
|
||||
@@ -71,7 +88,11 @@ export const resolveHopperAnalysisProfile = async (
|
||||
/** Derive Hopper's complete non-interactive CLI loader selection. */
|
||||
export const hopperLoaderArgsForTarget = (
|
||||
target: BinaryTarget,
|
||||
): Result<readonly string[], ProviderAdapterError> => {
|
||||
machoContainer?: "thin" | "fat32" | "fat64",
|
||||
): Result<
|
||||
readonly string[],
|
||||
ProviderAdapterError | AnalysisCapabilityUnavailableError
|
||||
> => {
|
||||
if (target.kind === "database") return ok([]);
|
||||
if (target.kind !== "executable")
|
||||
return err(new ProviderAdapterError("hopper", "resolve_analysis_profile"));
|
||||
@@ -79,8 +100,16 @@ export const hopperLoaderArgsForTarget = (
|
||||
const flag = hopperArchitectureFlag(architecture);
|
||||
switch (target.format) {
|
||||
case "mach-o":
|
||||
if (machoContainer === undefined)
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
"hopper",
|
||||
"resolve_analysis_profile",
|
||||
"Mach-O container kind is unknown; resolve the Hopper analysis profile before starting its client.",
|
||||
),
|
||||
);
|
||||
return ok(
|
||||
(target.availableArchitectures?.length ?? 0) > 1
|
||||
machoContainer === "fat32" || machoContainer === "fat64"
|
||||
? ["-l", "FAT", flag, "-l", "Mach-O"]
|
||||
: ["-l", "Mach-O", flag],
|
||||
);
|
||||
@@ -109,6 +138,46 @@ const hopperArchitectureFlag = (architecture: BinaryArchitecture): string => {
|
||||
}
|
||||
};
|
||||
|
||||
const readMachoMagic = async (
|
||||
path: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<number, AnalysisError>> => {
|
||||
if (signalIsAborted(signal))
|
||||
return err(new AnalysisCancelledError("open_binary"));
|
||||
const stream = createReadStream(path, { start: 0, end: 3 });
|
||||
const onAbort = (): void => {
|
||||
stream.destroy();
|
||||
};
|
||||
signal?.addEventListener("abort", onAbort, { once: true });
|
||||
try {
|
||||
const chunks: Buffer[] = [];
|
||||
for await (const chunk of stream) chunks.push(Buffer.from(chunk));
|
||||
if (signalIsAborted(signal))
|
||||
return err(new AnalysisCancelledError("open_binary"));
|
||||
const bytes = Buffer.concat(chunks);
|
||||
return bytes.length === 4
|
||||
? ok(bytes.readUInt32BE(0))
|
||||
: err(
|
||||
new BinaryTargetError(
|
||||
path,
|
||||
"Mach-O header became truncated before Hopper loader selection",
|
||||
),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return signalIsAborted(signal)
|
||||
? err(new AnalysisCancelledError("open_binary"))
|
||||
: err(
|
||||
new BinaryTargetError(
|
||||
path,
|
||||
`Cannot read the Mach-O container header: ${cause instanceof Error ? cause.message : String(cause)}`,
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
signal?.removeEventListener("abort", onAbort);
|
||||
}
|
||||
};
|
||||
|
||||
const sha256File = async (
|
||||
path: string,
|
||||
signal: AbortSignal | undefined,
|
||||
|
||||
@@ -158,7 +158,13 @@ export class HopperProvider implements AnalysisProviderCandidate {
|
||||
),
|
||||
close: () => Promise.resolve(),
|
||||
};
|
||||
const derivedLoaderArgs = hopperLoaderArgsForTarget(target);
|
||||
const container = profile?.parameters.macho_container;
|
||||
const derivedLoaderArgs = hopperLoaderArgsForTarget(
|
||||
target,
|
||||
container === "thin" || container === "fat32" || container === "fat64"
|
||||
? container
|
||||
: undefined,
|
||||
);
|
||||
if (!derivedLoaderArgs.ok)
|
||||
return {
|
||||
execute: () => Promise.resolve(err(derivedLoaderArgs.error)),
|
||||
@@ -200,7 +206,9 @@ export class HopperProvider implements AnalysisProviderCandidate {
|
||||
value: createAnalysisExecution(result.value, executionProvider, {
|
||||
...(profile === undefined ? {} : { analysisProfile: profile }),
|
||||
limitations: [
|
||||
"Results depend on Hopper's completed static analysis.",
|
||||
...(CAPABILITIES.find(
|
||||
(descriptor) => descriptor.operation === operation,
|
||||
)?.limitations ?? []),
|
||||
...(operation === "read_bytes"
|
||||
? [
|
||||
"Hopper byte reads stop at the containing segment's exclusive end; a readable prefix is returned with complete=false when the requested range crosses it.",
|
||||
|
||||
Reference in New Issue
Block a user