merge: retain main CI planning and artifact checks
This commit is contained in:
@@ -21,8 +21,8 @@ import {
|
||||
parseFunctionDossier,
|
||||
parseListCount,
|
||||
parseRelatedAddresses,
|
||||
parseSegmentImageBase,
|
||||
parseSegments,
|
||||
parseSegmentImageBase,
|
||||
} from "../domain/hopperValues.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import {
|
||||
@@ -46,6 +46,7 @@ import { traceCallPath } from "./CallPathTracing.js";
|
||||
import { resolveProcedureAddress } from "./ProcedureAddressResolution.js";
|
||||
import { traceLiteralFeature } from "./EnhancedLiteralTracing.js";
|
||||
import { projectNativeApiInspection } from "./native/NativeApiInspection.js";
|
||||
|
||||
/**
|
||||
* Composes direct provider operations into bounded reverse-engineering tools.
|
||||
* Direct callers use `execute`; adapters that own validation use
|
||||
@@ -528,24 +529,29 @@ export class EnhancedTools {
|
||||
|
||||
const segments = parseSegments(segmentsResult.value);
|
||||
if (!segments.ok) return segments;
|
||||
const imageBase = parseSegmentImageBase(segmentsResult.value);
|
||||
const procedureCount = parseListCount(proceduresResult.value, "procedures");
|
||||
if (!procedureCount.ok) return procedureCount;
|
||||
const stringCount = parseListCount(stringsResult.value, "strings");
|
||||
if (!stringCount.ok) return stringCount;
|
||||
|
||||
const imageBase = parseSegmentImageBase(segments.value);
|
||||
|
||||
return ok({
|
||||
document: document.value,
|
||||
segments: segments.value.map(({ name, start, end }) => ({
|
||||
name,
|
||||
start,
|
||||
end,
|
||||
length: addressDistance(start, end),
|
||||
})),
|
||||
...(imageBase === undefined ? {} : { image_base: imageBase }),
|
||||
segments: segments.value.map(
|
||||
({ name, start, end, image_base, address_space }) => ({
|
||||
name,
|
||||
start,
|
||||
end,
|
||||
length: addressDistance(start, end),
|
||||
...(image_base === undefined ? {} : { image_base }),
|
||||
...(address_space === undefined ? {} : { address_space }),
|
||||
}),
|
||||
),
|
||||
segment_count: segments.value.length,
|
||||
procedure_count: procedureCount.value,
|
||||
string_count: stringCount.value,
|
||||
...(imageBase === undefined ? {} : { image_base: imageBase }),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -18,11 +18,11 @@ export const workflowAnalysisProfile = (
|
||||
): AnalysisProfileCommitment =>
|
||||
createAnalysisProfile(REA_WORKFLOW_PROVIDER, {
|
||||
upstream_analysis_profile: upstream,
|
||||
...(operation === "binary_overview" ||
|
||||
operation === "batch_decompile" ||
|
||||
operation === "get_call_graph"
|
||||
? { workflow_revision: 2 }
|
||||
: {}),
|
||||
...(operation === "binary_overview"
|
||||
? { workflow_revision: 3 }
|
||||
: operation === "batch_decompile" || operation === "get_call_graph"
|
||||
? { workflow_revision: 2 }
|
||||
: {}),
|
||||
});
|
||||
|
||||
/** Provider identity for deterministic artifact inventories. */
|
||||
|
||||
@@ -8,11 +8,11 @@ import {
|
||||
type NativeCommandRunner,
|
||||
} from "../native/CommandRunner.js";
|
||||
import {
|
||||
lipoSubtypeBits,
|
||||
parseLipoArchitectures,
|
||||
type LipoArchitecture,
|
||||
} from "../native/parsers/lipo.js";
|
||||
import type { MachoSlice } from "../domain/apple/dylibResolution.js";
|
||||
import { machoCpuSubtypeBase } from "../domain/apple/machoCpuIdentity.js";
|
||||
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
|
||||
import { readMachoImage, type ReadAt } from "./apple/MachoLoadCommandReader.js";
|
||||
import {
|
||||
@@ -471,8 +471,10 @@ const lipoSubtypeMatches = (
|
||||
): boolean => {
|
||||
if (reported.cpu_subtype_code !== null)
|
||||
return reported.cpu_subtype_code === observedCpuSubtype;
|
||||
const bits = lipoSubtypeBits(reported.cpu_subtype);
|
||||
return bits === null || bits === (observedCpuSubtype & 0x00ffffff);
|
||||
return (
|
||||
reported.cpu_subtype_base === null ||
|
||||
reported.cpu_subtype_base === machoCpuSubtypeBase(observedCpuSubtype)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -585,6 +585,11 @@ const normalizeDestinationPath = (
|
||||
"path",
|
||||
`Windows destination cannot materialize artifact path as a regular file: ${path}; ':' denotes alternate data stream syntax.`,
|
||||
);
|
||||
if (platform === "win32" && /[\u0001-\u001f]/u.test(path))
|
||||
throw new ArtifactReaderFailure(
|
||||
"path",
|
||||
`Windows destination cannot materialize artifact path: ${JSON.stringify(path)}; Windows names cannot contain U+0001-U+001F.`,
|
||||
);
|
||||
return path;
|
||||
};
|
||||
|
||||
|
||||
@@ -255,10 +255,29 @@ const decodeZipEntryText = (
|
||||
encoding: string,
|
||||
type: "filename" | "comment",
|
||||
): string | undefined => {
|
||||
if (type !== "filename" || encoding !== "utf-8") return undefined;
|
||||
return decodeStrictZipUtf8(bytes, "ZIP filename is not valid UTF-8");
|
||||
if (type !== "filename") return undefined;
|
||||
if (encoding === "utf-8")
|
||||
return decodeStrictZipUtf8(bytes, "ZIP filename is not valid UTF-8");
|
||||
return encoding === "cp437" ? decodeCp437(bytes) : undefined;
|
||||
};
|
||||
|
||||
/** CP437 characters for bytes 0x80-0xFF. */
|
||||
const CP437_HIGH_HALF =
|
||||
"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■\u00A0";
|
||||
|
||||
/**
|
||||
* Decode a legacy ZIP name with the Unicode CP437 mapping, which Python's
|
||||
* zipfile also uses: bytes below 0x80 are ASCII. zip.js shows 0x01-0x1F and
|
||||
* 0x7F as IBM PC display glyphs, which turns the macOS folder icon file
|
||||
* `Icon\r` into `Icon♪`.
|
||||
*/
|
||||
const decodeCp437 = (bytes: Uint8Array): string =>
|
||||
Array.from(bytes, (byte) =>
|
||||
byte < 0x80
|
||||
? String.fromCharCode(byte)
|
||||
: CP437_HIGH_HALF.charAt(byte - 0x80),
|
||||
).join("");
|
||||
|
||||
const zipEntryPath = (entry: Entry): string => {
|
||||
const field = entry.extraFieldUnicodePath;
|
||||
if (field?.valid !== true) return entry.filename;
|
||||
|
||||
@@ -134,7 +134,14 @@ export const decodeNibArchive = (
|
||||
const value = values.get(index);
|
||||
if (value === undefined) continue;
|
||||
const key = keys[value.key_index];
|
||||
if (key !== undefined) fields[key] = value.value;
|
||||
if (key !== undefined)
|
||||
// Archive keys are data, including the legacy __proto__ setter name.
|
||||
Object.defineProperty(fields, key, {
|
||||
value: value.value,
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
return [{ id, class_name: className, values: fields }];
|
||||
});
|
||||
@@ -519,7 +526,8 @@ const decodeUtf8 = (
|
||||
label: string,
|
||||
): string => {
|
||||
try {
|
||||
return new TextDecoder("utf-8", { fatal: true }).decode(
|
||||
// These are length-prefixed names, not standalone text documents.
|
||||
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
|
||||
bytes.subarray(start, end),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
|
||||
@@ -162,6 +162,19 @@ it("rejects identical duplicate core fields before schema validation", () => {
|
||||
expect(() => decodeHarCapture(text, [])).toThrow("duplicate");
|
||||
});
|
||||
|
||||
it("names the violated HAR schema constraint without restating capture values", () => {
|
||||
expect(() =>
|
||||
decodeHarCapture(
|
||||
JSON.stringify({
|
||||
log: { creator: { name: "x", version: "1" }, entries: [] },
|
||||
}),
|
||||
[],
|
||||
),
|
||||
).toThrow(
|
||||
"HAR schema validation failed: required (must have required property 'version').",
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves prototype-named producer members through the capture boundary", () => {
|
||||
const text = JSON.stringify(historicalHar()).replace(
|
||||
'"cache":{}',
|
||||
|
||||
@@ -23,9 +23,13 @@ export const createHarValidator = (): ((value: unknown) => void) => {
|
||||
allowMitmproxyAbsentPostData(value);
|
||||
if (!validate(value)) {
|
||||
const error = validate.errors?.[0];
|
||||
// AJV messages name the violated constraint, such as the missing
|
||||
// property, without restating any captured value.
|
||||
throw new CaptureFormatError(
|
||||
"format",
|
||||
`HAR schema validation failed: ${error?.keyword ?? "unknown constraint"}.`,
|
||||
error === undefined
|
||||
? "HAR schema validation failed: unknown constraint."
|
||||
: `HAR schema validation failed: ${error.keyword} (${error.message ?? "no detail"}).`,
|
||||
error?.instancePath ?? "",
|
||||
);
|
||||
}
|
||||
|
||||
@@ -62,15 +62,17 @@ export const registerApktoolCommands = (
|
||||
.describe(
|
||||
"Project strings from res/values-<locale> instead of the default",
|
||||
),
|
||||
"no-strings": z
|
||||
strings: z
|
||||
.boolean()
|
||||
.default(false)
|
||||
.describe("Skip projecting string resources"),
|
||||
.default(true)
|
||||
.describe(
|
||||
"Include string resources; use --no-strings to skip projecting them",
|
||||
),
|
||||
}),
|
||||
run: ({ args, options }) =>
|
||||
execute(CLI_COMMANDS.decodeAndroidResources, "decode_android_resources", {
|
||||
path: args.path,
|
||||
include_strings: !options["no-strings"],
|
||||
include_strings: options.strings,
|
||||
...(options.locale === undefined ? {} : { locale: options.locale }),
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -137,6 +137,13 @@ const registerDecompileCommand = (
|
||||
.describe(
|
||||
"Procedure name or address; use --procedure=<value> for leading dashes",
|
||||
),
|
||||
"file-offset": z
|
||||
.string()
|
||||
.regex(/^0x[0-9a-fA-F]+$/u)
|
||||
.optional()
|
||||
.describe(
|
||||
"Ghidra source-file byte offset; mapped internally to an analysis address",
|
||||
),
|
||||
snapshot: z
|
||||
.string()
|
||||
.min(1)
|
||||
@@ -152,8 +159,17 @@ const registerDecompileCommand = (
|
||||
{
|
||||
procedure: resolveCliAnalysisSelector(
|
||||
args.address,
|
||||
options.procedure,
|
||||
"procedure",
|
||||
options["file-offset"] === undefined
|
||||
? options.procedure
|
||||
: resolveCliAnalysisSelector(
|
||||
options.procedure,
|
||||
`file-offset:${options["file-offset"]}`,
|
||||
"file-offset",
|
||||
"procedure_pseudo_code",
|
||||
),
|
||||
options["file-offset"] === undefined
|
||||
? "procedure"
|
||||
: "file-offset",
|
||||
"procedure_pseudo_code",
|
||||
),
|
||||
},
|
||||
|
||||
@@ -181,7 +181,7 @@ export const OFFICIAL_TOOL_CONTRACTS = [
|
||||
),
|
||||
official(
|
||||
"procedure_pseudo_code",
|
||||
"Decompile one analyzed procedure by symbol name or provider-normalized address. Returns provider-specific pseudocode, never original source or cross-provider text equivalence, and may return null; request procedure_assembly when instruction precision matters. Ghidra no-return assumptions may cut the analyzed body; Evidence limitations report observed terminal calls and available callee identities.",
|
||||
"Decompile one analyzed procedure by symbol name or provider-normalized address. Ghidra also accepts file-offset:0x... selectors and falls back to a unique source-file mapping when a 0x-prefixed number resolves to no procedure; existing analysis addresses take precedence and ambiguous mappings fail explicitly. Returns provider-specific pseudocode, never original source or cross-provider text equivalence, and may return null; request procedure_assembly when instruction precision matters. Ghidra no-return assumptions may cut the analyzed body; Evidence limitations report observed terminal calls and available callee identities.",
|
||||
z.object({ procedure, document }),
|
||||
),
|
||||
official(
|
||||
|
||||
@@ -357,18 +357,20 @@ export const enhancedOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
|
||||
binary_overview: evidenceResultOf(
|
||||
z.object({
|
||||
document: z.string(),
|
||||
image_base: z.string().min(1).optional(),
|
||||
segments: z.array(
|
||||
z.object({
|
||||
name: z.string(),
|
||||
start: z.string(),
|
||||
end: z.string(),
|
||||
length: z.number().min(0),
|
||||
image_base: z.string().exactOptional(),
|
||||
address_space: z.string().exactOptional(),
|
||||
}),
|
||||
),
|
||||
segment_count: z.number().int().min(0),
|
||||
procedure_count: z.number().int().min(0),
|
||||
string_count: z.number().int().min(0),
|
||||
image_base: z.string().min(1).optional(),
|
||||
}),
|
||||
),
|
||||
analyze_function: functionDossierOutput,
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import {
|
||||
DYLIB_RESOLUTION_LIMITATIONS,
|
||||
deriveFindings,
|
||||
type TracedRootSlice,
|
||||
} from "./dylibResolutionFindings.js";
|
||||
|
||||
/**
|
||||
@@ -568,6 +569,7 @@ export const traceDylibLoading = async (
|
||||
for (const path of request.unclassified ?? [])
|
||||
images.set(path, await view.image(path));
|
||||
const roots: DylibTrace["roots"][number][] = [];
|
||||
const rootSlices: TracedRootSlice[] = [];
|
||||
const edges: Edge[] = [];
|
||||
const withoutArchitecture: string[] = [];
|
||||
const environmentRoots = new Set<string>();
|
||||
@@ -597,6 +599,7 @@ export const traceDylibLoading = async (
|
||||
)
|
||||
environmentRoots.add(root);
|
||||
roots.push({ image: root, architecture: slice.architecture });
|
||||
rootSlices.push({ image: root, slice });
|
||||
edges.push(
|
||||
...(await traceProcess(
|
||||
{
|
||||
@@ -641,7 +644,7 @@ export const traceDylibLoading = async (
|
||||
facts.status === "parsed" ? facts.slices.map(withoutDependencies) : [],
|
||||
})),
|
||||
edges,
|
||||
findings: deriveFindings(edges, roots, images),
|
||||
findings: deriveFindings(edges, rootSlices),
|
||||
coverage: {
|
||||
status:
|
||||
unparsed.length === 0 &&
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import type {
|
||||
DylibEdge as Edge,
|
||||
DylibFinding as Finding,
|
||||
DylibTrace,
|
||||
MachoImageFacts,
|
||||
MachoSlice,
|
||||
} from "./dylibResolution.js";
|
||||
|
||||
/** One traced process root with the exact slice it was traced from. */
|
||||
export interface TracedRootSlice {
|
||||
readonly image: string;
|
||||
readonly slice: MachoSlice;
|
||||
}
|
||||
|
||||
export const deriveFindings = (
|
||||
edges: readonly Edge[],
|
||||
roots: DylibTrace["roots"],
|
||||
images: ReadonlyMap<string, MachoImageFacts>,
|
||||
roots: readonly TracedRootSlice[],
|
||||
): Finding[] => {
|
||||
const findings: Finding[] = [];
|
||||
edges.forEach((edge, index) => {
|
||||
@@ -32,15 +36,11 @@ export const deriveFindings = (
|
||||
explanation: `The modeled dyld search checks ${earlier.join(", ")} before ${edge.resolution.image ?? edge.install_name}. A compatible Mach-O placed at an earlier modeled path could take precedence over that fallback, subject to unmodeled search inputs and code-signing library validation; library validation is not evaluated here (see inspect_signature).${edge.loader_conditional ? " This loader loads only conditionally; if it never loads, this search never occurs." : ""}${edge.resolution.status === "conditional" ? " Unmodeled environment overrides or earlier unknown candidates may take precedence; placing an image at this rpath does not establish which image dyld will load." : ""}`,
|
||||
});
|
||||
});
|
||||
for (const { image, architecture } of roots) {
|
||||
const facts = images.get(image);
|
||||
const slice =
|
||||
facts?.status === "parsed"
|
||||
? facts.slices.find(
|
||||
(candidate) => candidate.architecture === architecture,
|
||||
)
|
||||
: undefined;
|
||||
const environment = slice?.dyld_environment ?? [];
|
||||
// Slices can share an architecture name (arm64e and arm64e.x1), so each
|
||||
// finding uses the slice its root was traced from, not a name lookup.
|
||||
for (const { image, slice } of roots) {
|
||||
const { architecture } = slice;
|
||||
const environment = slice.dyld_environment;
|
||||
if (environment.length > 0)
|
||||
findings.push({
|
||||
kind: "dyld-environment-present",
|
||||
@@ -48,7 +48,7 @@ export const deriveFindings = (
|
||||
image,
|
||||
basis: "derived",
|
||||
explanation:
|
||||
slice?.file_type === "execute"
|
||||
slice.file_type === "execute"
|
||||
? `${image} (${architecture}) sets dyld environment variables through LC_DYLD_ENVIRONMENT (${environment.join(", ")}); any image-selection overrides they request are not modeled. Diagnostic settings alone do not alter resolution.`
|
||||
: `${image} (${architecture}) contains LC_DYLD_ENVIRONMENT (${environment.join(", ")}). Dyld reads these commands only from the main executable; entries in this non-executable root are observations and do not alter resolution.`,
|
||||
});
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
parseNames,
|
||||
parseProcedures,
|
||||
parseRelatedAddresses,
|
||||
parseSegmentImageBase,
|
||||
parseSegments,
|
||||
} from "./hopperValues.js";
|
||||
|
||||
@@ -66,19 +65,6 @@ describe("Hopper boundary values", () => {
|
||||
ok: true,
|
||||
value: ["fixture"],
|
||||
});
|
||||
expect(
|
||||
parseSegmentImageBase([
|
||||
{ image_base: "0x100000" },
|
||||
{ image_base: "0x100000" },
|
||||
]),
|
||||
).toBe("0x100000");
|
||||
expect(
|
||||
parseSegmentImageBase([
|
||||
{ image_base: "0x100000" },
|
||||
{ image_base: "0x200000" },
|
||||
]),
|
||||
).toBeUndefined();
|
||||
expect(parseSegmentImageBase([{ name: "__TEXT" }])).toBeUndefined();
|
||||
});
|
||||
|
||||
it.each([
|
||||
|
||||
+14
-12
@@ -20,6 +20,8 @@ export interface SegmentSummary {
|
||||
readonly readable: boolean | null;
|
||||
readonly writable: boolean | null;
|
||||
readonly executable: boolean | null;
|
||||
readonly image_base?: string;
|
||||
readonly address_space?: string;
|
||||
}
|
||||
|
||||
/** Return a bounded byte distance between canonical addresses in one space. */
|
||||
@@ -58,10 +60,9 @@ const segmentSchema = z.object({
|
||||
readable: z.boolean().nullable(),
|
||||
writable: z.boolean().nullable(),
|
||||
executable: z.boolean().nullable(),
|
||||
image_base: z.string().min(1).exactOptional(),
|
||||
address_space: z.string().min(1).exactOptional(),
|
||||
});
|
||||
const segmentImageBaseSchema = z
|
||||
.object({ image_base: z.string().min(1) })
|
||||
.passthrough();
|
||||
const unavailableAnalysisFactSchema = z
|
||||
.object({ available: z.literal(false), reason: z.string() })
|
||||
.strict();
|
||||
@@ -415,7 +416,7 @@ export const parseRelatedAddresses = (
|
||||
: invalid(`${relation} list`, parsed.error);
|
||||
};
|
||||
|
||||
/** Parse direct or wrapped Hopper segment records. */
|
||||
/** Preserve reported address context in direct or wrapped provider segments. */
|
||||
export const parseSegments = (
|
||||
value: JsonValue,
|
||||
): Result<readonly SegmentSummary[], HopperProtocolError> => {
|
||||
@@ -427,14 +428,15 @@ export const parseSegments = (
|
||||
: invalid("segment list", parsed.error);
|
||||
};
|
||||
|
||||
/** Return a provider-reported image base only when every segment agrees on it. */
|
||||
export const parseSegmentImageBase = (value: JsonValue): string | undefined => {
|
||||
const parsed = z
|
||||
.array(segmentImageBaseSchema)
|
||||
.safeParse(unwrapProperty(value, "segments"));
|
||||
if (!parsed.success) return undefined;
|
||||
const bases = new Set(parsed.data.map(({ image_base }) => image_base));
|
||||
return bases.size === 1 ? parsed.data[0]?.image_base : undefined;
|
||||
/** Return a reported overview base only when every parsed segment agrees. */
|
||||
export const parseSegmentImageBase = (
|
||||
segments: readonly SegmentSummary[],
|
||||
): string | undefined => {
|
||||
const base = segments[0]?.image_base;
|
||||
return base !== undefined &&
|
||||
segments.every((segment) => segment.image_base === base)
|
||||
? base
|
||||
: undefined;
|
||||
};
|
||||
|
||||
/** Parse direct or wrapped Hopper document names. */
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
|
||||
|
||||
const registration = (runtime: string, callback: string, chunk = 1) =>
|
||||
"(globalThis." +
|
||||
runtime +
|
||||
" = globalThis." +
|
||||
runtime +
|
||||
" || []).push([[" +
|
||||
chunk +
|
||||
"], {a: function() {}}, " +
|
||||
callback +
|
||||
"]);";
|
||||
|
||||
it.each(["webpackChunkFixture", "rspackChunkFixture"])(
|
||||
"keeps differing callback facts for %s independent of source order",
|
||||
(runtime) => {
|
||||
const first = registration(
|
||||
runtime,
|
||||
'function(r) { r(7); r.e("chunk-a"); }',
|
||||
);
|
||||
const second = registration(
|
||||
runtime,
|
||||
'function(r) { r(8); r.e("chunk-b"); }',
|
||||
);
|
||||
for (const source of [first + "\n" + second, second + "\n" + first]) {
|
||||
const result = analyzeJavaScriptStaticSource(source);
|
||||
expect(result.parse_status).toBe("complete");
|
||||
expect(result.bundler_registrations).toHaveLength(2);
|
||||
expect(
|
||||
result.bundler_registrations.flatMap((r) => r.entry_module_keys).sort(),
|
||||
).toEqual(["7", "8"]);
|
||||
expect(
|
||||
result.bundler_registrations.flatMap((r) => r.async_chunk_keys).sort(),
|
||||
).toEqual(["chunk-a", "chunk-b"]);
|
||||
}
|
||||
expect(
|
||||
analyzeJavaScriptStaticSource(first + "\n" + first).bundler_registrations,
|
||||
).toHaveLength(1);
|
||||
expect(
|
||||
analyzeJavaScriptStaticSource(first).bundler_registrations[0],
|
||||
).toMatchObject({
|
||||
entry_module_keys: ["7"],
|
||||
async_chunk_keys: ["chunk-a"],
|
||||
});
|
||||
expect(
|
||||
analyzeJavaScriptStaticSource(second).bundler_registrations[0],
|
||||
).toMatchObject({
|
||||
entry_module_keys: ["8"],
|
||||
async_chunk_keys: ["chunk-b"],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
["function(r) { r(7); }", "function(r) { r(8); }"],
|
||||
['function(r) { r.e("a"); }', 'function(r) { r.e("b"); }'],
|
||||
["function(r) {}", "function(s) {}"],
|
||||
["function(r) {}", "function(r) { r(dynamic); }"],
|
||||
["function(r) {}", "function(r) { r.e(dynamic); }"],
|
||||
])("retains callback metadata differences: %s / %s", (left, right) => {
|
||||
const result = analyzeJavaScriptStaticSource(
|
||||
registration("webpackChunkFixture", left) +
|
||||
"\n" +
|
||||
registration("webpackChunkFixture", right),
|
||||
);
|
||||
expect(result.bundler_registrations).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("retains distinct chunk identities with identical callbacks", () => {
|
||||
const callback = "function(r) { r(7); }";
|
||||
const result = analyzeJavaScriptStaticSource(
|
||||
registration("webpackChunkFixture", callback, 1) +
|
||||
"\n" +
|
||||
registration("webpackChunkFixture", callback, 2),
|
||||
);
|
||||
expect(result.bundler_registrations).toHaveLength(2);
|
||||
});
|
||||
@@ -691,6 +691,14 @@ export const registrationKey = (
|
||||
registration.runtime,
|
||||
registration.chunk_keys,
|
||||
String(registration.unknown_chunk_keys),
|
||||
// Equal factory tables can be registered with different runtime callbacks.
|
||||
// Both early collection and finalization must retain those recovered facts.
|
||||
registration.runtime_require_name,
|
||||
registration.runtime_module_cache_status,
|
||||
registration.entry_module_keys,
|
||||
String(registration.unknown_entry_module_keys),
|
||||
registration.async_chunk_keys,
|
||||
String(registration.unknown_async_chunk_keys),
|
||||
registration.modules.map(({ module_key: key, source_sha256: digest }) => [
|
||||
key,
|
||||
digest,
|
||||
|
||||
@@ -265,6 +265,10 @@ type OperandResult = {
|
||||
|
||||
type OperandReader = (il: Buffer, offset: number) => OperandResult;
|
||||
|
||||
// String(-0) loses a CIL constant's sign in both anchors and normalized hashes.
|
||||
const floatingPointOperand = (value: number): string =>
|
||||
Object.is(value, -0) ? "-0" : String(value);
|
||||
|
||||
const FIXED_OPERAND_READERS = new Map<string, OperandReader>([
|
||||
[
|
||||
"none",
|
||||
@@ -315,7 +319,7 @@ const FIXED_OPERAND_READERS = new Map<string, OperandReader>([
|
||||
(il, offset) => ({
|
||||
next: offset + 4,
|
||||
anchorKind: "constant",
|
||||
value: String(il.readFloatLE(offset)),
|
||||
value: floatingPointOperand(il.readFloatLE(offset)),
|
||||
}),
|
||||
],
|
||||
[
|
||||
@@ -323,7 +327,7 @@ const FIXED_OPERAND_READERS = new Map<string, OperandReader>([
|
||||
(il, offset) => ({
|
||||
next: offset + 8,
|
||||
anchorKind: "constant",
|
||||
value: String(il.readDoubleLE(offset)),
|
||||
value: floatingPointOperand(il.readDoubleLE(offset)),
|
||||
}),
|
||||
],
|
||||
[
|
||||
|
||||
@@ -123,6 +123,8 @@ export const resolveGhidraAnalysisProfile = async (
|
||||
call_relationship_coverage: "resolved-call-reference-diagnostics-v1",
|
||||
string_inventory_evidence: "defined-data-coverage-v1",
|
||||
location_resolution: "explicit-address-exact-entry-symbol-first-v3",
|
||||
procedure_file_offset_resolution:
|
||||
"address-first-unique-target-source-mapping-v2",
|
||||
instruction_flow_evidence: "decoded-return-pcode-v1",
|
||||
instruction_references: "exact-address-incoming-v1",
|
||||
function_boundary_observations: "ghidra-terminal-call-limitations-v1",
|
||||
@@ -212,6 +214,8 @@ export const resolveGhidraAnalysisProfile = async (
|
||||
call_relationship_coverage: "resolved-call-reference-diagnostics-v1",
|
||||
string_inventory_evidence: "defined-data-coverage-v1",
|
||||
location_resolution: "explicit-address-exact-entry-symbol-first-v3",
|
||||
procedure_file_offset_resolution:
|
||||
"address-first-unique-target-source-mapping-v2",
|
||||
instruction_flow_evidence: "decoded-return-pcode-v1",
|
||||
instruction_references: "exact-address-incoming-v1",
|
||||
function_boundary_observations: "ghidra-terminal-call-limitations-v1",
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { runDoctor, type DoctorHost } from "../application/Doctor.js";
|
||||
import { createDoctorHostFixture } from "../application/Doctor.fixture.js";
|
||||
import { providerRegistrationEnvironment } from "../application/SetupRegistrationEnvironment.js";
|
||||
import { WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON } from "../process/WindowsAuthority.js";
|
||||
import * as windowsAuthority from "../process/WindowsAuthority.js";
|
||||
import {
|
||||
inspectGhidraInstallation,
|
||||
type GhidraInstallationHost,
|
||||
@@ -173,6 +174,16 @@ describe("Ghidra doctor integration", () => {
|
||||
});
|
||||
|
||||
describe("Windows Ghidra doctor authority", () => {
|
||||
beforeEach(() => {
|
||||
vi.spyOn(windowsAuthority, "hasWindowsNativeAuthority").mockReturnValue(
|
||||
false,
|
||||
);
|
||||
vi.spyOn(
|
||||
windowsAuthority,
|
||||
"windowsNativeAuthorityUnavailableReason",
|
||||
).mockReturnValue(WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON);
|
||||
});
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
it("distinguishes a valid Windows installation from unavailable native authority", async () => {
|
||||
const ghidra = inspection({
|
||||
installDir: "C:\\tools\\ghidra_12.1.4_PUBLIC",
|
||||
|
||||
@@ -42,6 +42,30 @@ const parseFunctionValueFixture = (
|
||||
};
|
||||
|
||||
describe("Ghidra function-analysis result values", () => {
|
||||
it("preserves file-offset resolution in Evidence locations and rejects inexact offsets", () => {
|
||||
const selection = {
|
||||
file_offset: 0x210,
|
||||
mapped_address: "0x140001010",
|
||||
procedure_address: "0x140001010",
|
||||
};
|
||||
const wire = { value: "return 42;", limitations: [], selection };
|
||||
expect(parseGhidraFunctionResult("procedure_assembly", wire)).toMatchObject(
|
||||
{ ok: false, error: { _tag: "AnalysisOutputError" } },
|
||||
);
|
||||
const parsed = parseGhidraFunctionResult("procedure_pseudo_code", wire);
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
expect(parsed.value.locations).toEqual([
|
||||
{ kind: "file-offset", offset: 0x210 },
|
||||
{ kind: "address", address: "0x140001010" },
|
||||
{ kind: "address", address: "0x140001010" },
|
||||
]);
|
||||
expect(
|
||||
parseGhidraFunctionResult("procedure_pseudo_code", {
|
||||
...wire,
|
||||
selection: { ...selection, file_offset: Number.MAX_SAFE_INTEGER + 1 },
|
||||
}),
|
||||
).toMatchObject({ ok: false, error: { _tag: "AnalysisOutputError" } });
|
||||
});
|
||||
it.each([...observationOperations])(
|
||||
"requires the function-observation limitations envelope for %s",
|
||||
(operation) => {
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
} from "../domain/jsonValue.js";
|
||||
import { nativeApiBoundarySchema } from "../domain/native/nativeApiBoundary.js";
|
||||
import { nativeValueFlowSchema } from "../domain/native/nativeValueFlow.js";
|
||||
import type { EvidenceLocation } from "../domain/evidence.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
|
||||
import {
|
||||
@@ -399,11 +400,19 @@ const functionBoundaryObservationOperations: ReadonlySet<GhidraFunctionOperation
|
||||
const functionObservationEnvelope = z.strictObject({
|
||||
value: z.unknown(),
|
||||
limitations: z.array(z.string()),
|
||||
selection: z
|
||||
.strictObject({
|
||||
file_offset: z.number().int().min(0).max(Number.MAX_SAFE_INTEGER),
|
||||
mapped_address: ghidraCanonicalAddressSchema,
|
||||
procedure_address: ghidraCanonicalAddressSchema,
|
||||
})
|
||||
.exactOptional(),
|
||||
});
|
||||
|
||||
export interface ParsedGhidraFunctionResult {
|
||||
readonly value: JsonValue;
|
||||
readonly limitations: readonly string[];
|
||||
readonly locations?: readonly EvidenceLocation[];
|
||||
}
|
||||
|
||||
/** Require exact, bounded Java-bridge function output before creating Evidence. */
|
||||
@@ -414,15 +423,22 @@ export const parseGhidraFunctionResult = (
|
||||
let observation: {
|
||||
readonly value: unknown;
|
||||
readonly limitations: readonly string[];
|
||||
readonly selection?: z.infer<
|
||||
typeof functionObservationEnvelope
|
||||
>["selection"];
|
||||
};
|
||||
if (functionBoundaryObservationOperations.has(operation)) {
|
||||
const envelope = functionObservationEnvelope.safeParse(value);
|
||||
if (!envelope.success)
|
||||
if (
|
||||
!envelope.success ||
|
||||
(operation !== "procedure_pseudo_code" &&
|
||||
envelope.data.selection !== undefined)
|
||||
)
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
operation,
|
||||
"Ghidra bridge omitted the function observation envelope",
|
||||
{ cause: envelope.error },
|
||||
envelope.success ? undefined : { cause: envelope.error },
|
||||
),
|
||||
);
|
||||
observation = envelope.data;
|
||||
@@ -434,6 +450,24 @@ export const parseGhidraFunctionResult = (
|
||||
? ok({
|
||||
value: jsonValueSchema.parse(parsed.data),
|
||||
limitations: observation.limitations,
|
||||
...(observation.selection === undefined
|
||||
? {}
|
||||
: {
|
||||
locations: [
|
||||
{
|
||||
kind: "file-offset" as const,
|
||||
offset: observation.selection.file_offset,
|
||||
},
|
||||
{
|
||||
kind: "address" as const,
|
||||
address: observation.selection.mapped_address,
|
||||
},
|
||||
{
|
||||
kind: "address" as const,
|
||||
address: observation.selection.procedure_address,
|
||||
},
|
||||
],
|
||||
}),
|
||||
})
|
||||
: err(
|
||||
new AnalysisOutputError(
|
||||
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
AnalysisTimeoutError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import type { EvidenceLocation } from "../domain/evidence.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
@@ -678,10 +679,12 @@ export const createGhidraProviderClient = (input: {
|
||||
normalized: JsonValue,
|
||||
rawResult: JsonValue,
|
||||
observationLimitations: readonly string[] = [],
|
||||
locations: readonly EvidenceLocation[] = [],
|
||||
) => {
|
||||
const relationshipCoverage = relationshipCoverageFor(operation);
|
||||
return createAnalysisExecution(normalized, committedProfile.provider, {
|
||||
rawResult,
|
||||
locations,
|
||||
analysisProfile: committedProfile,
|
||||
...(relationshipCoverage === undefined ? {} : { relationshipCoverage }),
|
||||
limitations: [
|
||||
@@ -776,11 +779,13 @@ export const createGhidraProviderClient = (input: {
|
||||
);
|
||||
let normalized: JsonValue;
|
||||
let observationLimitations: readonly string[] = [];
|
||||
let observationLocations: readonly EvidenceLocation[] = [];
|
||||
if (isGhidraFunctionOperation(operation)) {
|
||||
const result = parseGhidraFunctionResult(operation, called.value);
|
||||
if (!result.ok) return result;
|
||||
normalized = result.value.value;
|
||||
observationLimitations = result.value.limitations;
|
||||
observationLocations = result.value.locations ?? [];
|
||||
} else {
|
||||
const result = parseGhidraInventoryResult(operation, called.value);
|
||||
if (!result.ok) return result;
|
||||
@@ -875,6 +880,7 @@ export const createGhidraProviderClient = (input: {
|
||||
normalized,
|
||||
called.value,
|
||||
observationLimitations,
|
||||
observationLocations,
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -7,9 +7,13 @@ import {
|
||||
fatImage,
|
||||
machoImage,
|
||||
} from "../artifacts/apple/MachoImage.fixture.js";
|
||||
import { parseMachoLayout } from "./AppleMachoSelection.js";
|
||||
import { parseMachoLayout, selectMachoSlice } from "./AppleMachoSelection.js";
|
||||
import { decodeAppleDispatchMetadata } from "./AppleDispatchMetadata.js";
|
||||
|
||||
/** macOS 27 system binaries ship CPU_SUBTYPE_ARM64E_X1 (12) with the PAC ABI bit. */
|
||||
const ARM64E_X1 = { type: 0x0100000c, subtype: 0x8000000c } as const;
|
||||
const X86_64H = { type: 0x01000007, subtype: 8 } as const;
|
||||
|
||||
describe("native Mach-O slice selection", () => {
|
||||
it("preserves high-bit section-name bytes instead of ASCII-folding metadata names", () => {
|
||||
const segment = Buffer.alloc(152);
|
||||
@@ -68,6 +72,48 @@ describe("native Mach-O slice selection", () => {
|
||||
/not aligned/u,
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ layout: "arm64e, arm64e.x1", cpus: [CPU.arm64e, ARM64E_X1], index: 0 },
|
||||
{ layout: "arm64e.x1, arm64e", cpus: [ARM64E_X1, CPU.arm64e], index: 1 },
|
||||
{ layout: "arm64, arm64e.x1", cpus: [CPU.arm64, ARM64E_X1], index: 0 },
|
||||
{ layout: "arm64e.x1, arm64", cpus: [ARM64E_X1, CPU.arm64], index: 1 },
|
||||
{
|
||||
layout: "x86_64, arm64e, arm64e.x1",
|
||||
cpus: [CPU.x86_64, CPU.arm64e, ARM64E_X1],
|
||||
index: 1,
|
||||
},
|
||||
{
|
||||
layout: "x86_64h, x86_64",
|
||||
cpus: [X86_64H, CPU.x86_64],
|
||||
index: 1,
|
||||
architecture: "x86_64",
|
||||
},
|
||||
])(
|
||||
"selects one slice when FAT slices $layout share a CPU type",
|
||||
({ cpus, index, architecture = "arm64" }) => {
|
||||
const fat = Buffer.from(
|
||||
fatImage(cpus.map((cpu) => ({ cpu, bytes: machoImage({ cpu }) }))),
|
||||
);
|
||||
expect(selectMachoSlice(fat, architecture).fatDeclaration).toMatchObject({
|
||||
index,
|
||||
cpuSubtype: cpus[index]?.subtype,
|
||||
});
|
||||
expect(parseMachoLayout(fat, architecture).segments).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it("rejects slices of the requested CPU type that share a subtype", () => {
|
||||
const fat = fatImage(
|
||||
[CPU.arm64e, CPU.arm64e].map((cpu) => ({
|
||||
cpu,
|
||||
bytes: machoImage({ cpu }),
|
||||
})),
|
||||
);
|
||||
expect(() => parseMachoLayout(Buffer.from(fat), "arm64")).toThrow(
|
||||
/Ambiguous FAT architecture slice/u,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("native Mach-O fixup load command boundaries", () => {
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import type { FixupCommands } from "./AppleMachoFixups.js";
|
||||
import {
|
||||
MACHO_ARCHITECTURE_POLICIES,
|
||||
machoSubtypeRank,
|
||||
} from "./MachoArchitectureSelection.js";
|
||||
import { machoCpuSubtypeBase } from "../domain/apple/machoCpuIdentity.js";
|
||||
import {
|
||||
readFatSliceDeclarations,
|
||||
validateFatSlice,
|
||||
type FatSliceDeclaration,
|
||||
} from "../domain/apple/machoContainer.js";
|
||||
|
||||
/** One mapped Mach-O segment with file-backed VA range. */
|
||||
@@ -90,32 +96,44 @@ export const selectMachoSlice = (
|
||||
const headerEnd = 8 + count * stride;
|
||||
if (count > 128 || headerEnd > bytes.length)
|
||||
throw malformed("Malformed FAT architecture table");
|
||||
const cpu = MACHO_CPU_TYPE[architecture];
|
||||
if (cpu === undefined) throw invalid("Requested FAT architecture is absent");
|
||||
const policy = MACHO_ARCHITECTURE_POLICIES[architecture];
|
||||
if (policy === undefined)
|
||||
throw invalid("Requested FAT architecture is absent");
|
||||
const declarations = readFatSliceDeclarations(bytes.subarray(8, headerEnd), {
|
||||
count,
|
||||
wide: fat64,
|
||||
littleEndian,
|
||||
});
|
||||
if (declarations.status !== "parsed") throw malformed(declarations.reason);
|
||||
let selected: MachoSlice | undefined;
|
||||
let best: FatSliceDeclaration | undefined;
|
||||
let bestRank = Number.POSITIVE_INFINITY;
|
||||
let ambiguous = false;
|
||||
for (const declaration of declarations.slices) {
|
||||
const issue = validateFatSlice(declaration, bytes.length, headerEnd);
|
||||
if (issue !== null) throw malformed(issue);
|
||||
if (declaration.cpuType !== cpu) continue;
|
||||
if (selected !== undefined)
|
||||
throw invalid("Ambiguous FAT architecture slice");
|
||||
selected = {
|
||||
slice: declaration.offset,
|
||||
sliceEnd: declaration.offset + declaration.size,
|
||||
fatDeclaration: {
|
||||
...declaration,
|
||||
tableEnd: headerEnd,
|
||||
},
|
||||
};
|
||||
if (declaration.cpuType !== policy.cpuType) continue;
|
||||
const rank = machoSubtypeRank(policy, declaration.cpuSubtype);
|
||||
if (best !== undefined && rank >= bestRank) {
|
||||
// Equal ranks keep FAT table order unless the slices share a subtype.
|
||||
if (
|
||||
rank === bestRank &&
|
||||
machoCpuSubtypeBase(declaration.cpuSubtype) ===
|
||||
machoCpuSubtypeBase(best.cpuSubtype)
|
||||
)
|
||||
ambiguous = true;
|
||||
continue;
|
||||
}
|
||||
best = declaration;
|
||||
bestRank = rank;
|
||||
ambiguous = false;
|
||||
}
|
||||
if (selected === undefined)
|
||||
throw invalid("Requested FAT architecture is absent");
|
||||
if (best === undefined) throw invalid("Requested FAT architecture is absent");
|
||||
if (ambiguous) throw invalid("Ambiguous FAT architecture slice");
|
||||
const selected: MachoSlice = {
|
||||
slice: best.offset,
|
||||
sliceEnd: best.offset + best.size,
|
||||
fatDeclaration: { ...best, tableEnd: headerEnd },
|
||||
};
|
||||
if (selected.slice + 4 > bytes.length)
|
||||
throw malformed("Truncated Mach-O header");
|
||||
const sliceMagic = bytes.readUInt32LE(selected.slice);
|
||||
@@ -126,13 +144,6 @@ export const selectMachoSlice = (
|
||||
return selected;
|
||||
};
|
||||
|
||||
/** CPU types admitted by `machArchitecture` in the domain binary target parser. */
|
||||
const MACHO_CPU_TYPE: Readonly<Record<string, number>> = {
|
||||
x86: 7,
|
||||
x86_64: 0x01000007,
|
||||
arm: 12,
|
||||
arm64: 0x0100000c,
|
||||
};
|
||||
const MH_MAGIC = 0xfeedface;
|
||||
const MH_CIGAM = 0xcefaedfe;
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { machoCpuSubtypeBase } from "../domain/apple/machoCpuIdentity.js";
|
||||
import type { LipoArchitecture } from "./parsers/lipo.js";
|
||||
|
||||
interface MachoArchitecturePolicy {
|
||||
readonly cpuType: number;
|
||||
readonly subtypes: readonly {
|
||||
readonly subtype: number;
|
||||
readonly name: string;
|
||||
}[];
|
||||
readonly variantName?: RegExp;
|
||||
}
|
||||
|
||||
/** One preference order for FAT subtype identities and Apple tool slice names. */
|
||||
export const MACHO_ARCHITECTURE_POLICIES: Readonly<
|
||||
Record<string, MachoArchitecturePolicy>
|
||||
> = {
|
||||
x86: { cpuType: 7, subtypes: [{ subtype: 3, name: "i386" }] },
|
||||
x86_64: {
|
||||
cpuType: 0x01000007,
|
||||
subtypes: [{ subtype: 3, name: "x86_64" }],
|
||||
},
|
||||
arm: { cpuType: 12, subtypes: [], variantName: /^arm$/u },
|
||||
arm64: {
|
||||
cpuType: 0x0100000c,
|
||||
subtypes: [
|
||||
{ subtype: 0, name: "arm64" },
|
||||
{ subtype: 2, name: "arm64e" },
|
||||
{ subtype: 1, name: "arm64v8" },
|
||||
],
|
||||
variantName: /^arm64e\.[A-Za-z0-9_]+$/u,
|
||||
},
|
||||
};
|
||||
|
||||
/** Unlisted subtypes follow the preferred slices in FAT table order. */
|
||||
export const machoSubtypeRank = (
|
||||
policy: MachoArchitecturePolicy,
|
||||
subtype: number,
|
||||
): number => {
|
||||
const rank = policy.subtypes.findIndex(
|
||||
(candidate) => candidate.subtype === machoCpuSubtypeBase(subtype),
|
||||
);
|
||||
return rank < 0 ? policy.subtypes.length : rank;
|
||||
};
|
||||
|
||||
/** Select a tool spelling using the same preference order as FAT selection. */
|
||||
export const selectMachoArchitectureName = (
|
||||
policy: MachoArchitecturePolicy,
|
||||
available: readonly LipoArchitecture[],
|
||||
): string | undefined => {
|
||||
let selected: string | undefined;
|
||||
let bestRank = Number.POSITIVE_INFINITY;
|
||||
for (const architecture of available) {
|
||||
if (
|
||||
architecture.cpu_type_code !== null &&
|
||||
architecture.cpu_type_code !== policy.cpuType
|
||||
)
|
||||
continue;
|
||||
const namedSubtype = policy.subtypes.find(
|
||||
({ name }) => name === architecture.name,
|
||||
);
|
||||
const recognizedName =
|
||||
namedSubtype !== undefined || policy.variantName?.test(architecture.name);
|
||||
if (
|
||||
!recognizedName &&
|
||||
(architecture.cpu_type_code === null ||
|
||||
architecture.cpu_subtype_base === null)
|
||||
)
|
||||
continue;
|
||||
// Parsed identity is authoritative. Names support thin/partial lipo records.
|
||||
const subtype = architecture.cpu_subtype_base ?? namedSubtype?.subtype;
|
||||
const rank =
|
||||
subtype === undefined
|
||||
? policy.subtypes.length
|
||||
: machoSubtypeRank(policy, subtype);
|
||||
if (rank >= bestRank) continue;
|
||||
selected = architecture.name;
|
||||
bestRank = rank;
|
||||
}
|
||||
return selected;
|
||||
};
|
||||
@@ -1,10 +1,18 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
CPU,
|
||||
LC,
|
||||
dylibCommand,
|
||||
fatImage,
|
||||
machoImage,
|
||||
} from "../artifacts/apple/MachoImage.fixture.js";
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
AnalysisOutputError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { parseMachoLayout, selectMachoSlice } from "./AppleMachoSelection.js";
|
||||
import type { NativeCommandCapture } from "./CommandRunner.js";
|
||||
import { inspectNativeMacho } from "./NativeMachoInspection.js";
|
||||
import {
|
||||
@@ -78,6 +86,62 @@ describe("Mach-O tool architecture selection", () => {
|
||||
"/fixture/app",
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ names: ["arm64v8", "arm64e"], selected: "arm64e" },
|
||||
{ names: ["arm64e", "arm64v8"], selected: "arm64e" },
|
||||
{ names: ["arm64v8", "arm64e", "arm64"], selected: "arm64" },
|
||||
{ names: ["arm64v8"], selected: "arm64v8" },
|
||||
{ names: ["arm64v8", "arm64e.x1"], selected: "arm64v8" },
|
||||
{ names: ["arm64e.x1", "arm64v8"], selected: "arm64v8" },
|
||||
{ names: ["arm64v8", "arm64e.v1"], selected: "arm64e.v1" },
|
||||
{ names: ["arm64e.v1", "arm64v8"], selected: "arm64e.v1" },
|
||||
] as const)(
|
||||
"keeps inspection and dispatch on $selected for FAT order $names",
|
||||
async ({ names, selected }) => {
|
||||
const cpus = {
|
||||
arm64: CPU.arm64,
|
||||
arm64v8: { type: CPU.arm64.type, subtype: 1 },
|
||||
arm64e: CPU.arm64e,
|
||||
"arm64e.x1": { type: CPU.arm64.type, subtype: 0x8000000c },
|
||||
"arm64e.v1": { type: CPU.arm64.type, subtype: 0x81000002 },
|
||||
};
|
||||
const slices = names.map((name) => {
|
||||
const cpu = cpus[name];
|
||||
return {
|
||||
cpu,
|
||||
bytes: machoImage({
|
||||
cpu,
|
||||
commands: [dylibCommand(LC.LOAD_DYLIB, `/fixture/${name}.dylib`)],
|
||||
}),
|
||||
};
|
||||
});
|
||||
const fat = Buffer.from(fatImage(slices));
|
||||
const lipo = names
|
||||
.map(
|
||||
(name, index) =>
|
||||
`architecture ${name}\n cputype ${cpus[name].type}\n cpusubtype ${cpus[name].subtype}\n offset ${(index + 1) * 4096}\n size ${slices[index]?.bytes.length}\n align 2^12\n`,
|
||||
)
|
||||
.join("");
|
||||
const { commands } = await inspect(lipo, "arm64");
|
||||
expect(commands.find(({ tool }) => tool === "otool")?.args).toEqual([
|
||||
"-h",
|
||||
"-l",
|
||||
"-arch",
|
||||
selected.split(".")[0],
|
||||
"/fixture/app",
|
||||
]);
|
||||
expect(commands.find(({ tool }) => tool === "dyld_info")?.args).toContain(
|
||||
selected,
|
||||
);
|
||||
expect(selectMachoSlice(fat, "arm64").slice).toBe(
|
||||
(names.findIndex((name) => name === selected) + 1) * 4096,
|
||||
);
|
||||
expect(parseMachoLayout(fat, "arm64").fixups.dylibs).toEqual([
|
||||
`/fixture/${selected}.dylib`,
|
||||
]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("Mach-O UUID reconciliation", () => {
|
||||
|
||||
@@ -9,8 +9,15 @@ import {
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import type { NativeCommandCapture } from "./CommandRunner.js";
|
||||
import {
|
||||
MACHO_ARCHITECTURE_POLICIES,
|
||||
selectMachoArchitectureName,
|
||||
} from "./MachoArchitectureSelection.js";
|
||||
import { parseDyldSymbols } from "./parsers/dyldInfo.js";
|
||||
import { parseLipoArchitectures } from "./parsers/lipo.js";
|
||||
import {
|
||||
parseLipoArchitectures,
|
||||
type LipoArchitecture,
|
||||
} from "./parsers/lipo.js";
|
||||
import { withoutEchoedPathHeader } from "./parsers/echoedPath.js";
|
||||
import { parseOtoolLoadCommands } from "./parsers/otool.js";
|
||||
import { reconcileMachoUuid } from "./parsers/machoUuid.js";
|
||||
@@ -68,8 +75,7 @@ export const inspectNativeMacho = async (
|
||||
const lipo = captures.find(({ tool }) => tool === "lipo");
|
||||
if (lipo === undefined) throw new TypeError("Missing lipo capture");
|
||||
const architectures = parseLipoArchitectures(lipo.stdout);
|
||||
const availableArchitectures = architectures.map(({ name }) => name);
|
||||
const selection = selectArchitecture(context.target, availableArchitectures);
|
||||
const selection = selectArchitecture(context.target, architectures);
|
||||
if (selection.status === "absent")
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
@@ -397,25 +403,24 @@ type ArchitectureSelection =
|
||||
|
||||
const selectArchitecture = (
|
||||
target: BinaryTarget,
|
||||
available: readonly string[],
|
||||
available: readonly LipoArchitecture[],
|
||||
): ArchitectureSelection => {
|
||||
if (target.kind !== "executable" || target.availableArchitectures.length < 2)
|
||||
// BinaryTarget lists CPU families and lipo lists slices. A universal file
|
||||
// can hold several slices of one family (arm64e and arm64e.x1), and slice
|
||||
// tools without -arch print every slice.
|
||||
if (
|
||||
target.kind !== "executable" ||
|
||||
(target.availableArchitectures.length < 2 && available.length < 2)
|
||||
)
|
||||
return { status: "unfiltered" };
|
||||
const normalized =
|
||||
target.architecture === "x86" ? "i386" : target.architecture;
|
||||
if (available.includes(normalized))
|
||||
return { status: "selected", architecture: normalized };
|
||||
// BinaryTarget intentionally normalizes ARM64 CPU subtypes. Recover the
|
||||
// concrete arm64e slice name from lipo before invoking slice-aware tools.
|
||||
if (normalized === "arm64") {
|
||||
const arm64e = available.find((architecture) => architecture === "arm64e");
|
||||
if (arm64e !== undefined)
|
||||
return { status: "selected", architecture: arm64e };
|
||||
const variant = available.find((architecture) =>
|
||||
/^arm64e\.[A-Za-z0-9_]+$/u.test(architecture),
|
||||
);
|
||||
if (variant !== undefined)
|
||||
return { status: "selected", architecture: variant };
|
||||
}
|
||||
const policy = MACHO_ARCHITECTURE_POLICIES[target.architecture];
|
||||
const selected =
|
||||
policy === undefined
|
||||
? available.find(({ name }) => name === normalized)?.name
|
||||
: selectMachoArchitectureName(policy, available);
|
||||
if (selected !== undefined)
|
||||
return { status: "selected", architecture: selected };
|
||||
return { status: "absent", architecture: normalized };
|
||||
};
|
||||
|
||||
@@ -16,14 +16,11 @@ export const parseCodeSignature = (
|
||||
unsigned: boolean,
|
||||
executablePaths: readonly string[] = [],
|
||||
): Omit<InspectSignature, "provenance"> => {
|
||||
const { identifier, fields } = splitEchoedValues(
|
||||
output.replaceAll("\r\n", "\n"),
|
||||
executablePaths,
|
||||
);
|
||||
const { identifier, fields } = splitEchoedValues(output, executablePaths);
|
||||
const values = new Map<string, string>();
|
||||
const authorities: string[] = [];
|
||||
const cdhashes: string[] = [];
|
||||
for (const line of fields.split("\n")) {
|
||||
for (const line of fields.split(/\r?\n/u)) {
|
||||
if (line.startsWith("CodeDirectory ")) {
|
||||
values.set("CodeDirectory", line.slice("CodeDirectory ".length));
|
||||
continue;
|
||||
@@ -65,12 +62,20 @@ const splitEchoedValues = (
|
||||
text: string,
|
||||
executablePaths: readonly string[],
|
||||
): { readonly identifier: string | undefined; readonly fields: string } => {
|
||||
const executable = executablePaths
|
||||
.map((path) => `Executable=${path}\n`)
|
||||
.find((line) => text.startsWith(line));
|
||||
let rest = text;
|
||||
if (executable !== undefined) rest = text.slice(executable.length);
|
||||
else if (text.startsWith("Executable=")) {
|
||||
// Frame the exact source path before interpreting record separators. A CRLF
|
||||
// inside that path or the signing identifier is payload, not a line ending.
|
||||
let newline: string | undefined;
|
||||
for (const path of executablePaths) {
|
||||
const prefix = `Executable=${path}`;
|
||||
if (!text.startsWith(prefix)) continue;
|
||||
const separator = /^\r?\n/u.exec(text.slice(prefix.length))?.[0];
|
||||
if (separator === undefined) continue;
|
||||
newline = separator;
|
||||
rest = text.slice(prefix.length + separator.length);
|
||||
break;
|
||||
}
|
||||
if (rest === text && text.startsWith("Executable=")) {
|
||||
const identifierLine = text.indexOf("\nIdentifier=");
|
||||
rest = identifierLine < 0 ? text : text.slice(identifierLine + 1);
|
||||
}
|
||||
@@ -79,8 +84,17 @@ const splitEchoedValues = (
|
||||
// Later fields come from the signature itself, so the last `Format=` line
|
||||
// is the one codesign printed after the identifier.
|
||||
const format = rest.lastIndexOf("\nFormat=");
|
||||
const lineEnd = rest.indexOf("\n");
|
||||
const end = format >= 0 ? format : lineEnd >= 0 ? lineEnd : rest.length;
|
||||
// Without an exact path, use the producer-owned Format record's terminator.
|
||||
// Looking inside Identifier instead would confuse literal CRLF payload with
|
||||
// record framing, or strip a literal trailing CR from an LF-delimited value.
|
||||
if (newline === undefined && format >= 0)
|
||||
newline = /\r?\n/u.exec(rest.slice(format + 1))?.[0];
|
||||
const boundary = format >= 0 ? format : rest.indexOf("\n");
|
||||
const crlf = newline === undefined || newline === "\r\n";
|
||||
const end =
|
||||
boundary < 0
|
||||
? rest.length
|
||||
: boundary - (crlf && rest[boundary - 1] === "\r" ? 1 : 0);
|
||||
return {
|
||||
identifier: rest.slice("Identifier=".length, end),
|
||||
fields: rest.slice(end),
|
||||
|
||||
@@ -59,6 +59,7 @@ architecture arm64e
|
||||
cpu_type_code: null,
|
||||
cpu_subtype: "CPU_SUBTYPE_FUTURE",
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
@@ -85,8 +86,29 @@ architecture arm64e
|
||||
{
|
||||
cpu_subtype: "CPU_SUBTYPE_X86_ALL",
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: 3,
|
||||
capabilities: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ capabilities: "PTR_AUTH_VERSION USERSPACE 1", code: 0x81000002 },
|
||||
{ capabilities: "", code: null },
|
||||
])(
|
||||
"retains an arm64e base subtype with capabilities '$capabilities'",
|
||||
({ capabilities, code }) => {
|
||||
expect(
|
||||
parseLipoArchitectures(
|
||||
`architecture arm64e.v1\n cputype CPU_TYPE_ARM64\n cpusubtype CPU_SUBTYPE_ARM64E\n capabilities ${capabilities}\n`,
|
||||
),
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type_code: 0x0100000c,
|
||||
cpu_subtype_code: code,
|
||||
cpu_subtype_base: 2,
|
||||
},
|
||||
]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { z } from "zod";
|
||||
import { machoCpuSubtypeBase } from "../../domain/apple/machoCpuIdentity.js";
|
||||
|
||||
const architectureSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
@@ -6,6 +7,7 @@ const architectureSchema = z.object({
|
||||
cpu_subtype: z.string().nullable(),
|
||||
cpu_type_code: z.number().int().nonnegative().nullable(),
|
||||
cpu_subtype_code: z.number().int().nonnegative().nullable(),
|
||||
cpu_subtype_base: z.number().int().nonnegative().nullable(),
|
||||
capabilities: z.string().nullable(),
|
||||
file_offset: z.number().int().min(0).nullable(),
|
||||
size: z.number().int().min(0).nullable(),
|
||||
@@ -30,6 +32,7 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
@@ -54,6 +57,7 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
current.cpusubtype,
|
||||
current.capabilities,
|
||||
),
|
||||
cpu_subtype_base: cpuSubtypeBase(current.cpusubtype),
|
||||
capabilities: current.capabilities ?? null,
|
||||
file_offset: integer(current.offset),
|
||||
size: integer(current.size),
|
||||
@@ -158,10 +162,10 @@ const cpuSubtypeCode = (
|
||||
};
|
||||
|
||||
/** The subtype bits a lipo record names, without its capability byte. */
|
||||
export const lipoSubtypeBits = (value: string | null): number | null => {
|
||||
if (value === null) return null;
|
||||
const cpuSubtypeBase = (value: string | undefined): number | null => {
|
||||
if (value === undefined) return null;
|
||||
const subtype = unsigned32(value) ?? CPU_SUBTYPES[value];
|
||||
return subtype === undefined ? null : subtype & 0x00ffffff;
|
||||
return subtype === undefined ? null : machoCpuSubtypeBase(subtype);
|
||||
};
|
||||
|
||||
/** lipo names the LIB64 capability and prints other capability bits in hex. */
|
||||
|
||||
Reference in New Issue
Block a user