chore: sync the latest main before CI audit verification
Pin upstream baseline 811bd9f63 for the final local and hosted checks.
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
@@ -76,6 +76,8 @@ export const parseBinaryTarget = async (
|
||||
await access(candidate, constants.R_OK);
|
||||
throwIfTargetResolutionCancelled(signal);
|
||||
const canonical = await realpath(candidate);
|
||||
// Windows native admission must inspect the caller-selected reparse components.
|
||||
const sourcePath = process.platform === "win32" ? candidate : canonical;
|
||||
throwIfTargetResolutionCancelled(signal);
|
||||
const resolved = await resolveAppBundle(canonical);
|
||||
throwIfTargetResolutionCancelled(signal);
|
||||
@@ -113,7 +115,7 @@ export const parseBinaryTarget = async (
|
||||
);
|
||||
return ok({
|
||||
path,
|
||||
sourcePath: canonical,
|
||||
sourcePath,
|
||||
sha256: await sha256Handle(handle, path, metadata, signal),
|
||||
kind: "executable",
|
||||
format: "raw-binary",
|
||||
@@ -152,7 +154,7 @@ export const parseBinaryTarget = async (
|
||||
);
|
||||
return ok({
|
||||
path,
|
||||
sourcePath: canonical,
|
||||
sourcePath,
|
||||
sha256: await sha256Handle(handle, path, metadata, signal),
|
||||
kind: "executable",
|
||||
format: "dos-com",
|
||||
@@ -166,7 +168,7 @@ export const parseBinaryTarget = async (
|
||||
)
|
||||
return ok({
|
||||
path,
|
||||
sourcePath: process.platform === "win32" ? candidate : canonical,
|
||||
sourcePath,
|
||||
sha256: await sha256Handle(handle, path, metadata, signal),
|
||||
kind: "database",
|
||||
format: "analysis-database",
|
||||
@@ -175,7 +177,7 @@ export const parseBinaryTarget = async (
|
||||
if (artifactFormat !== undefined) {
|
||||
const identity = {
|
||||
path,
|
||||
sourcePath: process.platform === "win32" ? candidate : canonical,
|
||||
sourcePath,
|
||||
sha256: await sha256Handle(handle, path, metadata, signal),
|
||||
};
|
||||
return isArchiveFormat(artifactFormat)
|
||||
@@ -195,7 +197,7 @@ export const parseBinaryTarget = async (
|
||||
);
|
||||
return ok({
|
||||
path,
|
||||
sourcePath: process.platform === "win32" ? candidate : canonical,
|
||||
sourcePath,
|
||||
...bundle,
|
||||
sha256: await sha256Handle(handle, path, metadata, signal),
|
||||
kind: "executable",
|
||||
|
||||
@@ -141,6 +141,7 @@ const FRIDA_OPERATION_NAMES: ReadonlySet<string> = new Set(
|
||||
|
||||
/** Native contracts defined only for an active Mach-O executable. */
|
||||
const MACHO_TARGET_OPERATIONS: ReadonlySet<string> = new Set([
|
||||
"inspect_signature",
|
||||
"inspect_macho",
|
||||
"list_architectures",
|
||||
"observe_native_calls",
|
||||
|
||||
@@ -23,6 +23,7 @@ import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import { access } from "node:fs/promises";
|
||||
import { readAnalysisSnapshot } from "./binary/AnalysisSnapshotFiles.js";
|
||||
import { requiresNativeSourceAdmissionBeforeReplay } from "./binary/AnalysisSnapshotCache.js";
|
||||
import { parseBinaryTarget } from "./BinaryTargetResolver.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
||||
import {
|
||||
@@ -254,9 +255,14 @@ const runAnalysis = async (
|
||||
const route = preview.value.route;
|
||||
const bindingProfile = route.profile ?? undefined;
|
||||
const evidenceProfile = analysisProfileForRoute(route, tool);
|
||||
// A parsed Windows path does not prove Ghidra's native snapshot owner
|
||||
// admitted the selected source; CLI replay must wait for that client.
|
||||
if (
|
||||
bindingProfile !== undefined &&
|
||||
evidenceProfile !== undefined &&
|
||||
!requiresNativeSourceAdmissionBeforeReplay(
|
||||
route.binding?.identity.id,
|
||||
) &&
|
||||
allowsSnapshotReplay(route, tool)
|
||||
) {
|
||||
const cached = snapshotEvidenceForQuery(snapshot, {
|
||||
|
||||
@@ -135,6 +135,9 @@ const codexDirectory = ({ home, env }: ClientPathContext): string =>
|
||||
const qoderDirectory = ({ home, env }: ClientPathContext): string =>
|
||||
env.QODER_CONFIG_DIR || join(home, ".qoder");
|
||||
|
||||
const factoryDirectory = ({ home }: ClientPathContext): string =>
|
||||
join(home, ".factory");
|
||||
|
||||
/** Match Qwen Code's home override, including tilde and cwd-relative paths. */
|
||||
const qwenCodeDirectory = ({ home, env }: ClientPathContext): string => {
|
||||
const override = env.QWEN_HOME;
|
||||
@@ -558,6 +561,16 @@ export const SUPPORTED_CLIENT_DEFINITIONS = [
|
||||
markerPath: qoderDirectory,
|
||||
format: "json",
|
||||
},
|
||||
{
|
||||
name: "droid",
|
||||
displayName: "Factory Droid",
|
||||
skillPath: (context: ClientPathContext) =>
|
||||
join(factoryDirectory(context), "skills"),
|
||||
configPath: (context: ClientPathContext) =>
|
||||
join(factoryDirectory(context), "mcp.json"),
|
||||
markerPath: factoryDirectory,
|
||||
format: "json",
|
||||
},
|
||||
{
|
||||
name: "zcode",
|
||||
zcodeFallbackPath: ({ home }: ClientPathContext) =>
|
||||
|
||||
@@ -84,6 +84,11 @@ export const isSnapshotCacheable = (
|
||||
descriptor.effects.mayWriteFilesystem === false) &&
|
||||
descriptor.effects.changesPermissions === false;
|
||||
|
||||
/** Windows Ghidra must admit the selected source through its native snapshot owner. */
|
||||
export const requiresNativeSourceAdmissionBeforeReplay = (
|
||||
providerId: string | undefined,
|
||||
): boolean => process.platform === "win32" && providerId === "ghidra";
|
||||
|
||||
/** Bounded in-memory cache for one immutable binary identity. */
|
||||
export class AnalysisSnapshotCache {
|
||||
readonly #entries = new Map<string, AnalysisSnapshotEntry>();
|
||||
|
||||
@@ -58,6 +58,7 @@ interface SessionBinding {
|
||||
readonly profile: AnalysisProfileCommitment | null;
|
||||
readonly route: SessionProviderRoute;
|
||||
readonly runId: string;
|
||||
nativeSourceAdmitted: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -323,6 +324,7 @@ export class BinarySession
|
||||
profile,
|
||||
route,
|
||||
runId,
|
||||
nativeSourceAdmitted: false,
|
||||
};
|
||||
this.#clearRuntimeAvailability();
|
||||
this.resetSnapshotInvalidation();
|
||||
@@ -609,6 +611,7 @@ export class BinarySession
|
||||
active: this.#active,
|
||||
operation: name,
|
||||
parameters: arguments_,
|
||||
nativeSourceAdmitted: this.#active?.nativeSourceAdmitted === true,
|
||||
unboundOperationError: (operation, route) =>
|
||||
this.#providerRouter.unboundOperationError(operation, route),
|
||||
lookupSnapshot: (target, profile, operation, parameters) =>
|
||||
@@ -645,6 +648,14 @@ export class BinarySession
|
||||
},
|
||||
})
|
||||
: profiled;
|
||||
// A successful Ghidra operation proves this client's native source snapshot
|
||||
// was admitted; a composite health response and parsed path do not.
|
||||
if (
|
||||
execution.ok &&
|
||||
capability.provider.id === "ghidra" &&
|
||||
this.#active === active
|
||||
)
|
||||
this.#active.nativeSourceAdmitted = true;
|
||||
this.#observeRuntimeAvailability(name, execution);
|
||||
if (
|
||||
execution.ok &&
|
||||
@@ -838,6 +849,7 @@ export class BinarySession
|
||||
profile: previous.profile,
|
||||
route: previous.route,
|
||||
runId,
|
||||
nativeSourceAdmitted: false,
|
||||
};
|
||||
return ok(null);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,10 @@ import type {
|
||||
AnalysisOperation,
|
||||
CapabilityDescriptor,
|
||||
} from "../AnalysisProvider.js";
|
||||
import { isSnapshotCacheable } from "./AnalysisSnapshotCache.js";
|
||||
import {
|
||||
isSnapshotCacheable,
|
||||
requiresNativeSourceAdmissionBeforeReplay,
|
||||
} from "./AnalysisSnapshotCache.js";
|
||||
import type { SessionProviderRoute } from "./SessionProviderRouter.js";
|
||||
|
||||
interface ActiveExecutionBinding {
|
||||
@@ -35,6 +38,7 @@ interface PrepareSessionExecutionInput {
|
||||
readonly active: ActiveExecutionBinding | undefined;
|
||||
readonly operation: AnalysisOperation;
|
||||
readonly parameters: Readonly<Record<string, JsonValue>>;
|
||||
readonly nativeSourceAdmitted: boolean;
|
||||
readonly unboundOperationError: (
|
||||
operation: AnalysisOperation,
|
||||
route: SessionProviderRoute,
|
||||
@@ -55,6 +59,7 @@ export const prepareSessionExecution = (
|
||||
active,
|
||||
operation,
|
||||
parameters,
|
||||
nativeSourceAdmitted,
|
||||
unboundOperationError,
|
||||
lookupSnapshot,
|
||||
} = input;
|
||||
@@ -79,9 +84,12 @@ export const prepareSessionExecution = (
|
||||
const cacheable =
|
||||
profile !== undefined &&
|
||||
isSnapshotCacheable(operation, capability, parameters);
|
||||
const cached = cacheable
|
||||
? lookupSnapshot(active.target, profile, operation, parameters)
|
||||
: undefined;
|
||||
const cached =
|
||||
cacheable &&
|
||||
(!requiresNativeSourceAdmissionBeforeReplay(capability.provider.id) ||
|
||||
nativeSourceAdmitted)
|
||||
? lookupSnapshot(active.target, profile, operation, parameters)
|
||||
: undefined;
|
||||
return ok({ active, capability, profile, cacheable, cached });
|
||||
};
|
||||
|
||||
|
||||
@@ -92,9 +92,7 @@ export const resolveSessionTarget = async (
|
||||
input: ResolveSessionTargetInput,
|
||||
): Promise<Result<ResolvedSessionOpen, AnalysisError>> => {
|
||||
const { router, current, target, options } = input;
|
||||
const sameTarget =
|
||||
current?.target.path === target.path &&
|
||||
snapshotMatchesTarget(snapshotTarget(current.target), target);
|
||||
const sameTarget = sameSessionTarget(current?.target, target);
|
||||
const resolvedRoute =
|
||||
sameTarget && options.providerId === undefined && current !== undefined
|
||||
? ({ ok: true, value: current.route } as const)
|
||||
@@ -113,9 +111,7 @@ export const validateResolvedSessionTarget = (
|
||||
},
|
||||
): Result<ResolvedSessionOpen, AnalysisError> => {
|
||||
const { current, target, route, options, stagedSnapshotMatches } = input;
|
||||
const sameTarget =
|
||||
current?.target.path === target.path &&
|
||||
snapshotMatchesTarget(snapshotTarget(current.target), target);
|
||||
const sameTarget = sameSessionTarget(current?.target, target);
|
||||
const snapshotError = validateSnapshot({
|
||||
snapshot: options.snapshot,
|
||||
target,
|
||||
@@ -127,6 +123,16 @@ export const validateResolvedSessionTarget = (
|
||||
return { ok: true, value: { target, route, sameTarget } };
|
||||
};
|
||||
|
||||
const sameSessionTarget = (
|
||||
current: BinaryTarget | undefined,
|
||||
target: BinaryTarget,
|
||||
): boolean =>
|
||||
current !== undefined &&
|
||||
current.path === target.path &&
|
||||
// Native admission is bound to the requested path, not just its realpath.
|
||||
(current.sourcePath ?? current.path) === (target.sourcePath ?? target.path) &&
|
||||
snapshotMatchesTarget(snapshotTarget(current), target);
|
||||
|
||||
interface ValidateSnapshotInput {
|
||||
readonly snapshot: AnalysisSnapshot | undefined;
|
||||
readonly target: BinaryTarget;
|
||||
|
||||
@@ -40,6 +40,7 @@ import { ArtifactResourceScope } from "./ArtifactResourceScope.js";
|
||||
import { artifactCapabilities } from "./ArtifactProviderMetadata.js";
|
||||
import { ARTIFACT_GRAPH_PROVIDER } from "../application/InvestigationProviders.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
import { freezeJsonSnapshot } from "../domain/immutableJson.js";
|
||||
import { createArtifactInspection } from "../domain/artifactInspection.js";
|
||||
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
|
||||
@@ -341,7 +342,11 @@ class ArtifactClient implements AnalysisClient {
|
||||
total: 1,
|
||||
message: "inventory substep started",
|
||||
});
|
||||
const inventory = await this.inventory(inventoryParameters, options);
|
||||
// The freshly parsed inventory is exclusively owned here. Sealing it lets
|
||||
// the nested Evidence share it instead of cloning and re-digesting it.
|
||||
const inventory = freezeJsonSnapshot(
|
||||
await this.inventory(inventoryParameters, options),
|
||||
);
|
||||
const subject = subjectFor(
|
||||
this.target.sourcePath ?? this.target.path,
|
||||
inventory.manifest,
|
||||
@@ -355,7 +360,10 @@ class ArtifactClient implements AnalysisClient {
|
||||
// The nested inventory already names every occurrence's logical path.
|
||||
locations: [],
|
||||
});
|
||||
const result = createArtifactInspection(inventoryEvidence);
|
||||
// The returned execution and its Evidence then reuse this owned report.
|
||||
const result = freezeJsonSnapshot(
|
||||
createArtifactInspection(inventoryEvidence),
|
||||
);
|
||||
await options?.progress?.report({
|
||||
phase: "inspect_artifact.inventory",
|
||||
completed: 1,
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
type NativeCommandRunner,
|
||||
} from "../native/CommandRunner.js";
|
||||
import {
|
||||
parseLipoArchitectures,
|
||||
parseLipoOutput,
|
||||
type LipoArchitecture,
|
||||
} from "../native/parsers/lipo.js";
|
||||
import type { MachoSlice } from "../domain/apple/dylibResolution.js";
|
||||
@@ -114,7 +114,7 @@ export class MachOSliceArtifactReader implements ArtifactReader {
|
||||
};
|
||||
await this.#verifySource(source);
|
||||
const fileSize = source.initial.size;
|
||||
const architectures = parseLipoArchitectures(captured.value.stdout);
|
||||
const { architectures } = parseLipoOutput(captured.value.stdout, this.path);
|
||||
const structural = await readStructuralSlices(source.handle, fileSize);
|
||||
await this.#verifySource(source);
|
||||
if (structural.status === "malformed")
|
||||
|
||||
@@ -56,6 +56,7 @@ export const analyzeInterfaceBuilderBundle = async (input: {
|
||||
const omittedArchives: Array<{ path: string; reason: string }> = [];
|
||||
let unreportedOmittedArchives = 0;
|
||||
const incompleteHierarchies = new Map<string, number>();
|
||||
const unresolvedConnections = new Map<string, number>();
|
||||
const prototypeKeyOmissions = new Map<string, number | null>();
|
||||
let omitted = 0;
|
||||
let attempted = 0;
|
||||
@@ -136,6 +137,8 @@ export const analyzeInterfaceBuilderBundle = async (input: {
|
||||
: { value: nib.raw, omittedPrototypeKeys: 0 };
|
||||
if (nib !== null && nib.omitted > 0)
|
||||
incompleteHierarchies.set(entry.path, nib.omitted);
|
||||
if (nib !== null && nib.unresolvedConnections > 0)
|
||||
unresolvedConnections.set(entry.path, nib.unresolvedConnections);
|
||||
if (omittedPrototypeKeys !== 0)
|
||||
prototypeKeyOmissions.set(entry.path, omittedPrototypeKeys);
|
||||
const documentHash = createHash("sha256").update(bytes).digest("hex");
|
||||
@@ -170,6 +173,7 @@ export const analyzeInterfaceBuilderBundle = async (input: {
|
||||
});
|
||||
return finalizeHierarchyCoverage(result, {
|
||||
incompleteHierarchies,
|
||||
unresolvedConnections,
|
||||
prototypeKeyOmissions,
|
||||
invalid,
|
||||
omittedArchives,
|
||||
@@ -237,6 +241,7 @@ const finalizeHierarchyCoverage = (
|
||||
result: ReturnType<typeof buildInterfaceBuilderAnalysis>,
|
||||
counts: {
|
||||
incompleteHierarchies: ReadonlyMap<string, number>;
|
||||
unresolvedConnections: ReadonlyMap<string, number>;
|
||||
prototypeKeyOmissions: ReadonlyMap<string, number | null>;
|
||||
invalid: readonly string[];
|
||||
omittedArchives: readonly { path: string; reason: string }[];
|
||||
@@ -247,6 +252,7 @@ const finalizeHierarchyCoverage = (
|
||||
) => {
|
||||
const {
|
||||
incompleteHierarchies,
|
||||
unresolvedConnections,
|
||||
prototypeKeyOmissions,
|
||||
invalid,
|
||||
omittedArchives,
|
||||
@@ -260,6 +266,13 @@ const finalizeHierarchyCoverage = (
|
||||
...result,
|
||||
documents: result.documents.map((document) => ({
|
||||
...document,
|
||||
// The dictionary parser only sees connectors that were projected, so the
|
||||
// omission recorded where an ownerless connector was dropped has to be
|
||||
// added here for this producer to report the same document count as the
|
||||
// keyed-archive producer for the same recognized connector.
|
||||
connection_count:
|
||||
document.connection_count +
|
||||
(unresolvedConnections.get(document.relative_path) ?? 0),
|
||||
hierarchy_complete:
|
||||
document.hierarchy_complete &&
|
||||
!incompleteHierarchies.has(document.relative_path),
|
||||
@@ -268,15 +281,32 @@ const finalizeHierarchyCoverage = (
|
||||
...result.graph,
|
||||
coverage: [
|
||||
...result.graph.coverage.map((facet) => {
|
||||
const omitted = incompleteHierarchies.get(
|
||||
const omittedHierarchy = incompleteHierarchies.get(
|
||||
facet.facet.replace(/^hierarchy:/u, ""),
|
||||
);
|
||||
return facet.facet.startsWith("hierarchy:") && omitted !== undefined
|
||||
if (
|
||||
facet.facet.startsWith("hierarchy:") &&
|
||||
omittedHierarchy !== undefined
|
||||
)
|
||||
return {
|
||||
...facet,
|
||||
status: "partial" as const,
|
||||
reason: "serialized_view_hierarchy_incomplete",
|
||||
omitted: facet.omitted + omittedHierarchy,
|
||||
};
|
||||
const omittedConnection = unresolvedConnections.get(
|
||||
facet.facet.replace(/^connections:/u, ""),
|
||||
);
|
||||
return facet.facet.startsWith("connections:") &&
|
||||
omittedConnection !== undefined
|
||||
? {
|
||||
...facet,
|
||||
status: "partial" as const,
|
||||
reason: "serialized_view_hierarchy_incomplete",
|
||||
omitted: facet.omitted + omitted,
|
||||
// A connection dropped by the limit is recoverable by raising
|
||||
// it, so that cause keeps the reason the graph already named.
|
||||
// Only a facet with no other cause reports the omission.
|
||||
reason: facet.reason ?? "connection_unresolved",
|
||||
omitted: facet.omitted + omittedConnection,
|
||||
}
|
||||
: facet;
|
||||
}),
|
||||
@@ -306,7 +336,8 @@ const finalizeHierarchyCoverage = (
|
||||
result.graph.truncated ||
|
||||
counts.omitted > 0 ||
|
||||
archiveDecodePartial ||
|
||||
incompleteHierarchies.size > 0,
|
||||
incompleteHierarchies.size > 0 ||
|
||||
unresolvedConnections.size > 0,
|
||||
},
|
||||
limitations: [
|
||||
...result.limitations,
|
||||
@@ -316,6 +347,11 @@ const finalizeHierarchyCoverage = (
|
||||
: [
|
||||
"Some serialized hierarchy links could not be projected within the bounded hierarchy; their parentage remains unknown.",
|
||||
]),
|
||||
...(unresolvedConnections.size === 0
|
||||
? []
|
||||
: [
|
||||
"Some recognized NIBArchive connectors declare no resolvable owner; they carry no connection evidence and are reported as omissions.",
|
||||
]),
|
||||
...(invalid.length === 0
|
||||
? []
|
||||
: [
|
||||
@@ -496,6 +532,10 @@ const projectNibArchive = (
|
||||
);
|
||||
const connections: Record<string, JsonValue[]> = {};
|
||||
const hierarchy: NibHierarchyNode[] = [];
|
||||
// A recognized connector whose owner reference cannot be resolved produces no
|
||||
// connection entry, so the omission has to be counted here: the projected
|
||||
// dictionary carries no trace of it for a downstream consumer to find.
|
||||
let unresolvedConnections = 0;
|
||||
const reference = (value: JsonValue | undefined): number | null => {
|
||||
if (typeof value !== "object" || value === null || Array.isArray(value))
|
||||
return null;
|
||||
@@ -512,7 +552,10 @@ const projectNibArchive = (
|
||||
const values = object.values;
|
||||
const connectionFields = interfaceBuilderConnectionFieldNames(className);
|
||||
const source = reference(values[connectionFields.source]);
|
||||
if (source === null) continue;
|
||||
if (source === null) {
|
||||
unresolvedConnections += 1;
|
||||
continue;
|
||||
}
|
||||
const destination = reference(values[connectionFields.destination]);
|
||||
const label = referencedString(values[connectionFields.label]);
|
||||
const type = className.includes("Outlet")
|
||||
@@ -608,6 +651,7 @@ const projectNibArchive = (
|
||||
);
|
||||
return {
|
||||
omitted: viewHierarchy.omitted + hierarchyOmitted + merged.omitted,
|
||||
unresolvedConnections,
|
||||
raw: jsonValueSchema.parse({
|
||||
"com.apple.ibtool.document.objects": objects,
|
||||
"com.apple.ibtool.document.connections": connections,
|
||||
|
||||
@@ -37,6 +37,7 @@ interface ExpectedFile {
|
||||
|
||||
interface ExpectedContainer {
|
||||
readonly hash_status: ArtifactOccurrence["hash_status"];
|
||||
readonly format: ArtifactOccurrence["artifact_format"];
|
||||
readonly inventory: JavaScriptArtifactContainer | null;
|
||||
}
|
||||
|
||||
@@ -219,6 +220,9 @@ const visitReader = async (
|
||||
"integrity",
|
||||
`Nested ASAR was not integrity-verified: ${path}`,
|
||||
);
|
||||
// Inventory expands an ASAR-named file only when its bytes are an
|
||||
// ASAR. One it recorded as ordinary bytes has no members to read.
|
||||
if (container.format !== "asar") continue;
|
||||
const inventory = container.inventory;
|
||||
if (inventory === null)
|
||||
throw new ArtifactReaderFailure(
|
||||
@@ -395,6 +399,7 @@ const expectedInventory = (
|
||||
if (isNestedAsar)
|
||||
containers.set(occurrence.logical_path, {
|
||||
hash_status: occurrence.hash_status,
|
||||
format: occurrence.artifact_format,
|
||||
inventory: null,
|
||||
});
|
||||
continue;
|
||||
@@ -412,6 +417,7 @@ const expectedInventory = (
|
||||
if (isNestedAsar)
|
||||
containers.set(occurrence.logical_path, {
|
||||
hash_status: occurrence.hash_status,
|
||||
format: occurrence.artifact_format,
|
||||
inventory: {
|
||||
path: occurrence.logical_path,
|
||||
sha256: node.sha256,
|
||||
|
||||
@@ -2,6 +2,8 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
|
||||
import { captureDom } from "./CdpCaptureDocuments.js";
|
||||
import { capturedSvgNodeKind } from "./CdpCaptureDomSvg.js";
|
||||
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
|
||||
|
||||
const origin = "https://app.example.test";
|
||||
const documentUrl = `${origin}/screens/app`;
|
||||
@@ -50,8 +52,11 @@ const snapshot = (
|
||||
],
|
||||
});
|
||||
|
||||
const capture = (value: ReturnType<typeof snapshot>) =>
|
||||
captureDom(value, new Set([origin]), input);
|
||||
const capture = (value: ReturnType<typeof snapshot>) => {
|
||||
const step = captureDom(value, new Set([origin]), input).next();
|
||||
if (!step.done) throw new Error("Unexpected SVG identity query");
|
||||
return step.value;
|
||||
};
|
||||
|
||||
describe("DOM metadata document base URLs", () => {
|
||||
it("resolves links, actions and agent hints against the captured document base", () => {
|
||||
@@ -282,6 +287,7 @@ interface SvgSnapshotNode {
|
||||
readonly name: string;
|
||||
readonly parent: number;
|
||||
readonly attributes?: readonly (readonly [string, string])[];
|
||||
readonly svg?: boolean;
|
||||
}
|
||||
|
||||
const svgDocument = (
|
||||
@@ -300,7 +306,13 @@ const svgDocument = (
|
||||
const nodeValue = [intern("")];
|
||||
const parentIndex = [-1];
|
||||
const attributes: number[][] = [[]];
|
||||
const backendNodeId = [1];
|
||||
const svgNodeKinds = new Map<string, boolean>();
|
||||
for (const element of elements) {
|
||||
const id = backendNodeId.length + 1;
|
||||
backendNodeId.push(id);
|
||||
if (element.svg !== undefined)
|
||||
svgNodeKinds.set(`${String(id)}:${element.name}`, element.svg);
|
||||
nodeType.push(1);
|
||||
nodeName.push(intern(element.name));
|
||||
nodeValue.push(intern(""));
|
||||
@@ -316,14 +328,35 @@ const svgDocument = (
|
||||
{
|
||||
documentURL: 0,
|
||||
baseURL: 1,
|
||||
nodes: { nodeType, nodeName, nodeValue, parentIndex, attributes },
|
||||
nodes: {
|
||||
nodeType,
|
||||
nodeName,
|
||||
nodeValue,
|
||||
parentIndex,
|
||||
attributes,
|
||||
backendNodeId,
|
||||
},
|
||||
svgNodeKinds,
|
||||
},
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
const captureSvg = (value: ReturnType<typeof svgDocument>) =>
|
||||
captureDom(value, new Set([origin]), input);
|
||||
const captureSvg = (value: ReturnType<typeof svgDocument>) => {
|
||||
const kinds = new Map(
|
||||
value.documents.flatMap((document) => [...document.svgNodeKinds]),
|
||||
);
|
||||
const capture = captureDom(value, new Set([origin]), input);
|
||||
let step = capture.next();
|
||||
while (!step.done) {
|
||||
step = capture.next(
|
||||
step.value.map((query) =>
|
||||
kinds.get(`${String(query.backendNodeId)}:${query.nodeName}`),
|
||||
),
|
||||
);
|
||||
}
|
||||
return step.value;
|
||||
};
|
||||
|
||||
const urlFor = (
|
||||
result: ReturnType<typeof captureSvg>,
|
||||
@@ -331,6 +364,136 @@ const urlFor = (
|
||||
): (typeof result.urls)[number] | undefined =>
|
||||
result.urls.find((url) => url.node_index === nodeIndex);
|
||||
|
||||
describe("browser SVG identity", () => {
|
||||
const query = { backendNodeId: 42, nodeName: "a" };
|
||||
const node = { ...query, nodeType: 1 };
|
||||
|
||||
it("uses only a matching described element's SVG flag", () => {
|
||||
expect(capturedSvgNodeKind(query, { node: { ...node, isSVG: true } })).toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
capturedSvgNodeKind(query, { node: { ...node, isSVG: false } }),
|
||||
).toBe(false);
|
||||
expect(capturedSvgNodeKind(query, { node })).toBe(false);
|
||||
for (const invalid of [
|
||||
{},
|
||||
{ node: { ...node, backendNodeId: 43, isSVG: true } },
|
||||
{ node: { ...node, nodeName: "A", isSVG: true } },
|
||||
{ node: { ...node, nodeType: 3, isSVG: true } },
|
||||
{ node: { ...node, isSVG: "true" } },
|
||||
])
|
||||
expect(capturedSvgNodeKind(query, invalid)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("distinguishes identically named SVG and MathML elements regardless of parent", () => {
|
||||
const result = captureSvg(
|
||||
svgDocument([
|
||||
{ name: "svg", parent: 0 },
|
||||
{ name: "desc", parent: 1 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 2,
|
||||
attributes: [["xlink:href", "/svg"]],
|
||||
},
|
||||
{
|
||||
name: "a",
|
||||
svg: false,
|
||||
parent: 2,
|
||||
attributes: [["xlink:href", "/math"]],
|
||||
},
|
||||
{
|
||||
name: "A",
|
||||
svg: false,
|
||||
parent: 1,
|
||||
attributes: [["xlink:href", "/html"]],
|
||||
},
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 0,
|
||||
attributes: [["xlink:href", "/orphan"]],
|
||||
},
|
||||
]),
|
||||
);
|
||||
expect(result.urls.map(({ url }) => url)).toEqual([
|
||||
`${origin}/svg`,
|
||||
`${origin}/orphan`,
|
||||
]);
|
||||
});
|
||||
|
||||
it("records unresolved identity without inventing a destination or hiding attributes", () => {
|
||||
const value = svgDocument([
|
||||
{ name: "a", parent: 0, attributes: [["xlink:href", "/unknown"]] },
|
||||
]);
|
||||
const completeness = new CdpCaptureCompleteness();
|
||||
const capture = captureDom(value, new Set([origin]), input, completeness);
|
||||
expect(capture.next()).toEqual({
|
||||
done: false,
|
||||
value: [{ backendNodeId: 2, nodeName: "a" }],
|
||||
});
|
||||
const step = capture.next([undefined]);
|
||||
expect(step.done).toBe(true);
|
||||
if (!step.done) throw new Error("Unexpected second query");
|
||||
expect(step.value.unresolvedSvgNodes).toBe(1);
|
||||
expect(step.value.urls).toEqual([]);
|
||||
expect(step.value.nodes[1]?.attribute_names).toEqual(["xlink:href"]);
|
||||
expect(completeness.snapshot().excluded).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
section: "metadata",
|
||||
reason: "provider_unavailable",
|
||||
count: 1,
|
||||
}),
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps batched SVG identities aligned around missing IDs and ordinary links", () => {
|
||||
const value = svgDocument([
|
||||
{ name: "a", parent: 0, attributes: [["xlink:href", "/svg"]] },
|
||||
{ name: "A", parent: 0, attributes: [["href", "/ordinary"]] },
|
||||
{ name: "a", parent: 0, attributes: [["xlink:href", "/missing-id"]] },
|
||||
{ name: "a", parent: 0, attributes: [["xlink:href", "/non-svg"]] },
|
||||
]);
|
||||
const completeness = new CdpCaptureCompleteness();
|
||||
const capture = captureDom(
|
||||
{
|
||||
...value,
|
||||
documents: value.documents.map((document) => ({
|
||||
...document,
|
||||
nodes: { ...document.nodes, backendNodeId: [1, 2, 3, null, 5] },
|
||||
})),
|
||||
},
|
||||
new Set([origin]),
|
||||
input,
|
||||
completeness,
|
||||
);
|
||||
expect(capture.next()).toEqual({
|
||||
done: false,
|
||||
value: [
|
||||
{ backendNodeId: 2, nodeName: "a" },
|
||||
{ backendNodeId: 5, nodeName: "a" },
|
||||
],
|
||||
});
|
||||
const step = capture.next([true, false]);
|
||||
if (!step.done) throw new Error("Unexpected second batch");
|
||||
expect(
|
||||
step.value.urls.map(({ node_index, url }) => [node_index, url]),
|
||||
).toEqual([
|
||||
[1, `${origin}/svg`],
|
||||
[2, `${origin}/ordinary`],
|
||||
]);
|
||||
expect(step.value.unresolvedSvgNodes).toBe(1);
|
||||
expect(completeness.snapshot().excluded).toContainEqual({
|
||||
section: "metadata",
|
||||
reason: "provider_unavailable",
|
||||
count: 1,
|
||||
});
|
||||
});
|
||||
|
||||
describe("SVG xlink:href destinations", () => {
|
||||
it("reports an SVG anchor that only has xlink:href, keeping the first value", () => {
|
||||
const result = captureSvg(
|
||||
@@ -339,6 +502,7 @@ describe("SVG xlink:href destinations", () => {
|
||||
{ name: "g", parent: 1 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 2,
|
||||
attributes: [
|
||||
["xlink:href", "/xl-s2h"],
|
||||
@@ -371,6 +535,7 @@ describe("SVG xlink:href destinations", () => {
|
||||
{ name: "svg", parent: 0 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 1,
|
||||
attributes: [
|
||||
[first, valueFor(first)],
|
||||
@@ -395,18 +560,21 @@ describe("SVG xlink:href destinations", () => {
|
||||
svgDocument([
|
||||
{
|
||||
name: "A",
|
||||
svg: false,
|
||||
parent: 0,
|
||||
attributes: [["xlink:href", "/xl-html"]],
|
||||
},
|
||||
{ name: "math", parent: 0 },
|
||||
{
|
||||
name: "mrow",
|
||||
svg: false,
|
||||
parent: 2,
|
||||
attributes: [["xlink:href", "/xl-math"]],
|
||||
},
|
||||
{ name: "svg", parent: 0 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 4,
|
||||
attributes: [["xlink:href", "/xl-s2h"]],
|
||||
},
|
||||
@@ -425,6 +593,7 @@ describe("SVG xlink:href destinations", () => {
|
||||
{ name: "svg", parent: 0 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 1,
|
||||
attributes: [["xlink:href", "https://cdn.example.test/out"]],
|
||||
},
|
||||
@@ -444,8 +613,18 @@ describe("SVG xlink:href destinations", () => {
|
||||
const result = captureSvg(
|
||||
svgDocument([
|
||||
{ name: "svg", parent: 0 },
|
||||
{ name: "image", parent: 1, attributes: [["xlink:href", "guide"]] },
|
||||
{ name: "use", parent: 1, attributes: [["xlink:href", "icon.svg"]] },
|
||||
{
|
||||
name: "image",
|
||||
svg: true,
|
||||
parent: 1,
|
||||
attributes: [["xlink:href", "guide"]],
|
||||
},
|
||||
{
|
||||
name: "use",
|
||||
svg: true,
|
||||
parent: 1,
|
||||
attributes: [["xlink:href", "icon.svg"]],
|
||||
},
|
||||
]),
|
||||
);
|
||||
expect(result.urls).toEqual([
|
||||
@@ -466,14 +645,24 @@ describe("SVG xlink:href destinations", () => {
|
||||
});
|
||||
|
||||
describe("SVG xlink:href context", () => {
|
||||
it("keeps forward ancestor context local to each document", () => {
|
||||
it("binds SVG identity to the selected element across documents", () => {
|
||||
const svg = svgDocument([
|
||||
{ name: "a", parent: 2, attributes: [["xlink:href", "/svg-link"]] },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 2,
|
||||
attributes: [["xlink:href", "/svg-link"]],
|
||||
},
|
||||
{ name: "g", parent: 3 },
|
||||
{ name: "svg", parent: 0 },
|
||||
]);
|
||||
const html = svgDocument([
|
||||
{ name: "A", parent: 2, attributes: [["xlink:href", "/html-link"]] },
|
||||
{
|
||||
name: "A",
|
||||
svg: false,
|
||||
parent: 2,
|
||||
attributes: [["xlink:href", "/html-link"]],
|
||||
},
|
||||
{ name: "DIV", parent: 3 },
|
||||
{ name: "BODY", parent: 0 },
|
||||
]);
|
||||
@@ -507,32 +696,39 @@ describe("SVG xlink:href context", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("stops SVG context at foreignObject and resumes it for a nested svg", () => {
|
||||
const result = captureSvg(
|
||||
svgDocument([
|
||||
{ name: "svg", parent: 0 },
|
||||
{ name: "foreignObject", parent: 1 },
|
||||
{
|
||||
name: "A",
|
||||
parent: 2,
|
||||
attributes: [["xlink:href", "/xl-html-fo"]],
|
||||
},
|
||||
{ name: "svg", parent: 2 },
|
||||
{
|
||||
name: "a",
|
||||
parent: 4,
|
||||
attributes: [["xlink:href", "/xl-nested"]],
|
||||
},
|
||||
]),
|
||||
);
|
||||
expect(urlFor(result, 3)).toBeUndefined();
|
||||
expect(urlFor(result, 5)).toMatchObject({
|
||||
attribute: "href",
|
||||
url: `${origin}/xl-nested`,
|
||||
destination_scope: "approved",
|
||||
});
|
||||
});
|
||||
it.each(["foreignObject", "desc", "title"])(
|
||||
"preserves browser SVG identity across %s HTML integration points",
|
||||
(integrationPoint) => {
|
||||
const result = captureSvg(
|
||||
svgDocument([
|
||||
{ name: "svg", parent: 0 },
|
||||
{ name: integrationPoint, parent: 1 },
|
||||
{
|
||||
name: "A",
|
||||
svg: false,
|
||||
parent: 2,
|
||||
attributes: [["xlink:href", "/xl-html-fo"]],
|
||||
},
|
||||
{ name: "svg", parent: 2 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 4,
|
||||
attributes: [["xlink:href", "/xl-nested"]],
|
||||
},
|
||||
]),
|
||||
);
|
||||
expect(urlFor(result, 3)).toBeUndefined();
|
||||
expect(urlFor(result, 5)).toMatchObject({
|
||||
attribute: "href",
|
||||
url: `${origin}/xl-nested`,
|
||||
destination_scope: "approved",
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("SVG XLink precedence and unknown identity", () => {
|
||||
it("keeps an empty href ahead of xlink:href", () => {
|
||||
const baseUrl = `${origin}/screens/assets/`;
|
||||
const result = captureSvg(
|
||||
@@ -541,6 +737,7 @@ describe("SVG xlink:href context", () => {
|
||||
{ name: "svg", parent: 0 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 1,
|
||||
attributes: [
|
||||
["href", ""],
|
||||
@@ -567,6 +764,7 @@ describe("SVG xlink:href context", () => {
|
||||
{ name: "svg", parent: 0 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 1,
|
||||
attributes: [["xlink:href", "javascript:void(0)"]],
|
||||
},
|
||||
@@ -582,7 +780,7 @@ describe("SVG xlink:href context", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("does not treat a cyclic or dangling parent chain as SVG", () => {
|
||||
it("keeps an unavailable SVG identity unknown despite ancestor names", () => {
|
||||
const cyclic = captureSvg(
|
||||
svgDocument([
|
||||
{ name: "g", parent: 2 },
|
||||
@@ -652,7 +850,12 @@ describe("XML SVG roots and namespace scope restoration", () => {
|
||||
["xmlns:p", XLINK_NAMESPACE],
|
||||
],
|
||||
},
|
||||
{ name: "a", parent: 1, attributes: [["p:href", "/linked"]] },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 1,
|
||||
attributes: [["p:href", "/linked"]],
|
||||
},
|
||||
]),
|
||||
);
|
||||
expect(result.urls).toEqual([approvedHref(2, `${origin}/linked`)]);
|
||||
@@ -764,6 +967,7 @@ describe("XHTML and XML SVG namespace prefixes", () => {
|
||||
{ name: "svg:foreignObject", parent: 2 },
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 3,
|
||||
attributes: [["xlink:href", "/xl-html-fo"]],
|
||||
},
|
||||
@@ -799,6 +1003,7 @@ describe("XHTML and XML SVG namespace prefixes", () => {
|
||||
},
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 2,
|
||||
attributes: [
|
||||
["xlink:href", "/not-xlink"],
|
||||
@@ -918,11 +1123,13 @@ describe("XHTML SVG exclusions, HTML documents, and prefix scope", () => {
|
||||
},
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 2,
|
||||
attributes: [["xlink:href", "/xl-literal"]],
|
||||
},
|
||||
{
|
||||
name: "a",
|
||||
svg: true,
|
||||
parent: 2,
|
||||
attributes: [["x:href", "/xl-custom-html"]],
|
||||
},
|
||||
@@ -937,6 +1144,7 @@ describe("XHTML SVG exclusions, HTML documents, and prefix scope", () => {
|
||||
},
|
||||
{
|
||||
name: "A",
|
||||
svg: false,
|
||||
parent: 1,
|
||||
attributes: [["xlink:href", "/xl-html-anchor"]],
|
||||
},
|
||||
@@ -944,7 +1152,9 @@ describe("XHTML SVG exclusions, HTML documents, and prefix scope", () => {
|
||||
);
|
||||
expect(result.urls).toEqual([approvedHref(3, `${origin}/xl-literal`)]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("XML namespace resolution ordering and capacity", () => {
|
||||
it("applies an element's own xmlns declarations to its name and href", () => {
|
||||
const result = captureSvg(
|
||||
svgDocument([
|
||||
|
||||
@@ -11,7 +11,11 @@ import {
|
||||
} from "./CdpCaptureValues.js";
|
||||
import { exclusionReasonForUrl } from "./CdpCaptureEventHelpers.js";
|
||||
import type { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
|
||||
import { createDocumentSvgAncestry, domSvgLink } from "./CdpCaptureDomSvg.js";
|
||||
import {
|
||||
createDocumentSvgAncestry,
|
||||
domSvgLink,
|
||||
type DomSvgNodeQuery,
|
||||
} from "./CdpCaptureDomSvg.js";
|
||||
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js";
|
||||
|
||||
export type CapturedResource = Omit<
|
||||
@@ -135,18 +139,39 @@ export const walkFrameTrees = function* (
|
||||
}
|
||||
};
|
||||
|
||||
export const captureDom = (
|
||||
interface DomMetadataCapture {
|
||||
readonly urls: WebPageInspection["metadata"]["dom_urls"];
|
||||
readonly agentHints: WebPageInspection["metadata"]["agent_hints"];
|
||||
readonly excludedUrls: number;
|
||||
readonly unresolvedSvgNodes: number;
|
||||
}
|
||||
|
||||
export interface DomCapture extends DomMetadataCapture {
|
||||
readonly total: number;
|
||||
readonly nodes: WebPageInspection["dom"]["nodes"];
|
||||
}
|
||||
|
||||
// Bound retained projections and outstanding CDP reads independently of page size.
|
||||
// Eight leaves room for the session's other cancellation listeners.
|
||||
const DOM_METADATA_BATCH_SIZE = 8;
|
||||
|
||||
interface PendingDomMetadata {
|
||||
readonly options: DomMetadataOptions;
|
||||
// Undefined means no query is needed; null means the candidate has no valid ID.
|
||||
readonly svgQuery: DomSvgNodeQuery | null | undefined;
|
||||
}
|
||||
|
||||
/** Project one snapshot, requesting bounded batches of SVG identity facts. */
|
||||
export function* captureDom(
|
||||
result: unknown,
|
||||
allowedOrigins: ReadonlySet<string>,
|
||||
input: InspectWebPageInput,
|
||||
completeness?: CdpCaptureCompleteness,
|
||||
): {
|
||||
readonly total: number;
|
||||
readonly nodes: WebPageInspection["dom"]["nodes"];
|
||||
readonly urls: WebPageInspection["metadata"]["dom_urls"];
|
||||
readonly agentHints: WebPageInspection["metadata"]["agent_hints"];
|
||||
readonly excludedUrls: number;
|
||||
} => {
|
||||
): Generator<
|
||||
readonly DomSvgNodeQuery[],
|
||||
DomCapture,
|
||||
readonly (boolean | undefined)[]
|
||||
> {
|
||||
const root = requiredRecord(result);
|
||||
const strings = Array.isArray(root.strings)
|
||||
? root.strings.map((value) => cdpStringValue(value) ?? "")
|
||||
@@ -156,6 +181,7 @@ export const captureDom = (
|
||||
const agentHints: WebPageInspection["metadata"]["agent_hints"] = [];
|
||||
let excludedUrls = 0;
|
||||
let total = 0;
|
||||
let unresolvedSvgNodes = 0;
|
||||
for (const document of recordsValue(root.documents)) {
|
||||
const documentUrl = indexedString(strings, document.documentURL);
|
||||
if (allowedSanitizedUrl(documentUrl, allowedOrigins) === undefined) {
|
||||
@@ -172,6 +198,9 @@ export const captureDom = (
|
||||
const attributes = Array.isArray(documentNodes.attributes)
|
||||
? documentNodes.attributes
|
||||
: [];
|
||||
const backendNodeIds = Array.isArray(documentNodes.backendNodeId)
|
||||
? documentNodes.backendNodeId
|
||||
: [];
|
||||
const baseIndex = nodes.length;
|
||||
const ancestry = createDocumentSvgAncestry({
|
||||
nodeTypes,
|
||||
@@ -181,12 +210,29 @@ export const captureDom = (
|
||||
attributes,
|
||||
});
|
||||
total += nodeTypes.length;
|
||||
const pendingMetadata: PendingDomMetadata[] = [];
|
||||
for (let index = 0; index < nodeTypes.length; index += 1) {
|
||||
const attributeIndexes = numberArray(attributes[index]);
|
||||
const parent = Math.trunc(parents[index] ?? -1);
|
||||
const nodeIndex = nodes.length;
|
||||
const nodeName = indexedString(strings, nodeNames[index]);
|
||||
const attributeNames = uniqueAttributeNames(attributeIndexes, strings);
|
||||
const svgLink = domSvgLink(ancestry, index);
|
||||
let svgQuery: DomSvgNodeQuery | null | undefined;
|
||||
if (
|
||||
!svgLink.xmlDocument &&
|
||||
nodeTypes[index] === 1 &&
|
||||
attributeNames.includes("xlink:href") &&
|
||||
!attributeNames.includes("href")
|
||||
) {
|
||||
const backendNodeId = numberValue(backendNodeIds[index]);
|
||||
svgQuery =
|
||||
backendNodeId !== undefined &&
|
||||
Number.isSafeInteger(backendNodeId) &&
|
||||
backendNodeId > 0
|
||||
? { backendNodeId, nodeName }
|
||||
: null;
|
||||
}
|
||||
nodes.push({
|
||||
index: nodeIndex,
|
||||
parent_index: parent < 0 ? -1 : baseIndex + parent,
|
||||
@@ -195,30 +241,34 @@ export const captureDom = (
|
||||
node_value_length: indexedString(strings, nodeValues[index]).length,
|
||||
attribute_names: attributeNames,
|
||||
});
|
||||
const metadata = domMetadata({
|
||||
strings,
|
||||
attributes: attributeIndexes,
|
||||
// The BASE element itself resolves against the document fallback URL.
|
||||
baseUrl: nodeName.toLowerCase() === "base" ? documentUrl : baseUrl,
|
||||
documentUrl,
|
||||
nodeIndex,
|
||||
nodeName,
|
||||
allowedOrigins,
|
||||
...domSvgLink(ancestry, index),
|
||||
pendingMetadata.push({
|
||||
svgQuery,
|
||||
options: {
|
||||
strings,
|
||||
attributes: attributeIndexes,
|
||||
// The BASE element itself resolves against the document fallback URL.
|
||||
baseUrl: nodeName.toLowerCase() === "base" ? documentUrl : baseUrl,
|
||||
documentUrl,
|
||||
nodeIndex,
|
||||
nodeName,
|
||||
allowedOrigins,
|
||||
...svgLink,
|
||||
},
|
||||
});
|
||||
for (const url of metadata.urls) {
|
||||
urls.push(url);
|
||||
if (url.destination_scope !== "approved") {
|
||||
excludedUrls += 1;
|
||||
completeness?.exclude(
|
||||
"metadata",
|
||||
url.destination_scope === "outside_policy"
|
||||
? "disallowed_origin"
|
||||
: "unsupported_url",
|
||||
);
|
||||
}
|
||||
if (
|
||||
pendingMetadata.length === DOM_METADATA_BATCH_SIZE ||
|
||||
index === nodeTypes.length - 1
|
||||
) {
|
||||
const metadata = yield* captureDomMetadata(
|
||||
pendingMetadata,
|
||||
completeness,
|
||||
);
|
||||
urls.push(...metadata.urls);
|
||||
agentHints.push(...metadata.agentHints);
|
||||
excludedUrls += metadata.excludedUrls;
|
||||
unresolvedSvgNodes += metadata.unresolvedSvgNodes;
|
||||
pendingMetadata.length = 0;
|
||||
}
|
||||
agentHints.push(...metadata.agentHints);
|
||||
}
|
||||
}
|
||||
return {
|
||||
@@ -227,8 +277,54 @@ export const captureDom = (
|
||||
urls,
|
||||
agentHints,
|
||||
excludedUrls,
|
||||
unresolvedSvgNodes,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function* captureDomMetadata(
|
||||
pending: readonly PendingDomMetadata[],
|
||||
completeness: CdpCaptureCompleteness | undefined,
|
||||
): Generator<
|
||||
readonly DomSvgNodeQuery[],
|
||||
DomMetadataCapture,
|
||||
readonly (boolean | undefined)[]
|
||||
> {
|
||||
const queries = pending.flatMap(({ svgQuery }) =>
|
||||
svgQuery == null ? [] : [svgQuery],
|
||||
);
|
||||
const kinds = queries.length > 0 ? yield queries : [];
|
||||
const urls: DomMetadataCapture["urls"] = [];
|
||||
const agentHints: DomMetadataCapture["agentHints"] = [];
|
||||
let queryIndex = 0;
|
||||
let unresolvedSvgNodes = 0;
|
||||
let excludedUrls = 0;
|
||||
for (const { options, svgQuery } of pending) {
|
||||
let svgElement = options.svgElement;
|
||||
if (svgQuery !== undefined) {
|
||||
const kind = svgQuery === null ? undefined : kinds[queryIndex++];
|
||||
svgElement = kind === true;
|
||||
if (kind === undefined) {
|
||||
unresolvedSvgNodes += 1;
|
||||
completeness?.exclude("metadata", "provider_unavailable");
|
||||
}
|
||||
}
|
||||
const metadata = domMetadata({ ...options, svgElement });
|
||||
for (const url of metadata.urls) {
|
||||
urls.push(url);
|
||||
if (url.destination_scope !== "approved") {
|
||||
excludedUrls += 1;
|
||||
completeness?.exclude(
|
||||
"metadata",
|
||||
url.destination_scope === "outside_policy"
|
||||
? "disallowed_origin"
|
||||
: "unsupported_url",
|
||||
);
|
||||
}
|
||||
}
|
||||
agentHints.push(...metadata.agentHints);
|
||||
}
|
||||
return { urls, agentHints, unresolvedSvgNodes, excludedUrls };
|
||||
}
|
||||
|
||||
export const captureAccessibility = (
|
||||
results: readonly unknown[],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { numberValue } from "./CdpCaptureValues.js";
|
||||
import { numberValue, recordValue } from "./CdpCaptureValues.js";
|
||||
|
||||
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
||||
const XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
|
||||
@@ -18,11 +18,34 @@ interface XmlSvgLink {
|
||||
|
||||
interface DocumentSvgAncestry {
|
||||
readonly xml: boolean;
|
||||
readonly nodes: DomSnapshotNodes;
|
||||
readonly svgContexts: Map<number, boolean>;
|
||||
readonly xmlLinks: Map<number, XmlSvgLink>;
|
||||
}
|
||||
|
||||
export interface DomSvgNodeQuery {
|
||||
readonly backendNodeId: number;
|
||||
readonly nodeName: string;
|
||||
}
|
||||
|
||||
/** Bind the browser's SVG flag to the exact snapshotted element. */
|
||||
export const capturedSvgNodeKind = (
|
||||
query: DomSvgNodeQuery,
|
||||
result: unknown,
|
||||
): boolean | undefined => {
|
||||
const node = recordValue(recordValue(result)?.node);
|
||||
if (
|
||||
node?.backendNodeId !== query.backendNodeId ||
|
||||
node.nodeName !== query.nodeName ||
|
||||
node.nodeType !== 1
|
||||
)
|
||||
return undefined;
|
||||
// CDP omits isSVG for non-SVG nodes. Other values are malformed, not false.
|
||||
return node.isSVG === undefined
|
||||
? false
|
||||
: typeof node.isSVG === "boolean"
|
||||
? node.isSVG
|
||||
: undefined;
|
||||
};
|
||||
|
||||
interface DomSvgLink {
|
||||
readonly svgElement: boolean;
|
||||
readonly xmlDocument: boolean;
|
||||
@@ -79,8 +102,6 @@ export const createDocumentSvgAncestry = (
|
||||
const xml = xmlDocument(nodes);
|
||||
return {
|
||||
xml,
|
||||
nodes,
|
||||
svgContexts: new Map(),
|
||||
xmlLinks: xml ? resolveXmlElements(nodes) : new Map(),
|
||||
};
|
||||
};
|
||||
@@ -91,11 +112,7 @@ export const domSvgLink = (
|
||||
): DomSvgLink => {
|
||||
if (!ancestry.xml) {
|
||||
return {
|
||||
svgElement: elementInSvgContext(
|
||||
index,
|
||||
ancestry.nodes,
|
||||
ancestry.svgContexts,
|
||||
),
|
||||
svgElement: false,
|
||||
xmlDocument: false,
|
||||
xlinkHref: undefined,
|
||||
};
|
||||
@@ -108,43 +125,6 @@ export const domSvgLink = (
|
||||
};
|
||||
};
|
||||
|
||||
// Chrome's HTML parser records SVG local names (`svg`, `foreignObject`).
|
||||
// Descendants of foreignObject are HTML until a nested `svg`.
|
||||
const elementInSvgContext = (
|
||||
index: number,
|
||||
nodes: DomSnapshotNodes,
|
||||
contexts: Map<number, boolean>,
|
||||
): boolean => {
|
||||
let current = index;
|
||||
const seen = new Set<number>();
|
||||
let inSvg = false;
|
||||
while (
|
||||
current >= 0 &&
|
||||
current < nodes.nodeNames.length &&
|
||||
!seen.has(current)
|
||||
) {
|
||||
const cached = contexts.get(current);
|
||||
if (cached !== undefined) {
|
||||
inSvg = cached;
|
||||
break;
|
||||
}
|
||||
seen.add(current);
|
||||
if (stringAt(nodes.strings, nodes.nodeNames[current]) === "svg") {
|
||||
inSvg = true;
|
||||
break;
|
||||
}
|
||||
const parent = Math.trunc(nodes.parents[current] ?? -1);
|
||||
if (parent < 0 || parent === current) break;
|
||||
if (stringAt(nodes.strings, nodes.nodeNames[parent]) === "foreignObject")
|
||||
break;
|
||||
current = parent;
|
||||
}
|
||||
// Cache the entire examined chain, including forward references and cycles,
|
||||
// so shared ancestors are examined once per document.
|
||||
for (const node of seen) contexts.set(node, inSvg);
|
||||
return inSvg;
|
||||
};
|
||||
|
||||
interface NamespaceRestore {
|
||||
readonly prefix: string;
|
||||
readonly previous: string | undefined;
|
||||
|
||||
@@ -17,8 +17,10 @@ import {
|
||||
captureFrames,
|
||||
captureResources,
|
||||
type CapturedResource,
|
||||
type DomCapture,
|
||||
mainFrameUrl,
|
||||
} from "./CdpCaptureDocuments.js";
|
||||
import { capturedSvgNodeKind } from "./CdpCaptureDomSvg.js";
|
||||
import {
|
||||
allowedSanitizedUrl,
|
||||
delayWithCancellation,
|
||||
@@ -52,7 +54,7 @@ interface CaptureState {
|
||||
interface CapturedSections {
|
||||
readonly attachedUrl: string;
|
||||
readonly frames: WebPageInspection["frames"];
|
||||
readonly dom: ReturnType<typeof captureDom>;
|
||||
readonly dom: DomCapture;
|
||||
readonly accessibility: ReturnType<typeof captureAccessibility>;
|
||||
readonly scripts: WebPageInspection["scripts"];
|
||||
readonly resources: WebPageInspection["resources"];
|
||||
@@ -260,9 +262,7 @@ const authorizeObservationWindow = async (
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
};
|
||||
|
||||
const capturePageDom = async (
|
||||
state: CaptureState,
|
||||
): Promise<ReturnType<typeof captureDom>> => {
|
||||
const capturePageDom = async (state: CaptureState): Promise<DomCapture> => {
|
||||
const { context, allowedOrigins } = state;
|
||||
const result = await context.connection.send(
|
||||
"DOMSnapshot.captureSnapshot",
|
||||
@@ -274,12 +274,56 @@ const capturePageDom = async (
|
||||
context.sessionId,
|
||||
context.signal,
|
||||
);
|
||||
return captureDom(
|
||||
const capture = captureDom(
|
||||
result,
|
||||
allowedOrigins,
|
||||
context.input,
|
||||
state.events.completeness,
|
||||
);
|
||||
let step = capture.next();
|
||||
let descriptionUnavailable = false;
|
||||
const pendingController = new AbortController();
|
||||
const signal =
|
||||
context.signal === undefined
|
||||
? pendingController.signal
|
||||
: AbortSignal.any([context.signal, pendingController.signal]);
|
||||
while (!step.done) {
|
||||
const pending = step.value.map(async (query) => {
|
||||
if (descriptionUnavailable) return undefined;
|
||||
try {
|
||||
return capturedSvgNodeKind(
|
||||
query,
|
||||
await context.connection.send(
|
||||
"DOM.describeNode",
|
||||
{ backendNodeId: query.backendNodeId, depth: 0 },
|
||||
context.sessionId,
|
||||
signal,
|
||||
),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
// A detached node can disappear between snapshot and description.
|
||||
// Preserve partial metadata, but propagate transport/cancellation errors.
|
||||
if (!(cause instanceof CdpCommandRejection)) throw cause;
|
||||
if (cause.code === -32_601) descriptionUnavailable = true;
|
||||
else if (cause.code !== -32_000) throw cause;
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
try {
|
||||
// Promise.all preserves snapshot order even when CDP replies out of order.
|
||||
step = capture.next(await Promise.all(pending));
|
||||
} catch (cause: unknown) {
|
||||
// Stop and settle this bounded batch before the session can be detached.
|
||||
pendingController.abort();
|
||||
await Promise.allSettled(pending);
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
if (step.value.unresolvedSvgNodes > 0)
|
||||
state.limitations.push(
|
||||
`SVG identity was unavailable for ${String(step.value.unresolvedSvgNodes)} snapshotted XLink candidate nodes; their URL destinations remain unknown.`,
|
||||
);
|
||||
return step.value;
|
||||
};
|
||||
|
||||
const normalizedInspection = (
|
||||
|
||||
@@ -107,7 +107,7 @@ export const NATIVE_TOOL_CONTRACTS = [
|
||||
),
|
||||
native(
|
||||
"inspect_signature",
|
||||
"Inspect the active artifact's code-signing identity, hashes, authorities, requirements, entitlements, hardened-runtime state, and exact command provenance.",
|
||||
"Inspect the active Mach-O executable's code-signing identity, hashes, authorities, requirements, entitlements, hardened-runtime state, and exact command provenance.",
|
||||
z.object({}),
|
||||
),
|
||||
native(
|
||||
|
||||
@@ -149,12 +149,12 @@ export const OFFICIAL_TOOL_CONTRACTS = [
|
||||
),
|
||||
official(
|
||||
"procedure_callees",
|
||||
"Return the provider's resolved direct callees for one procedure identified by symbol or address. Unresolved indirect calls may be absent; use analyze_function and typed references to preserve available edge uncertainty.",
|
||||
"Return the provider's resolved direct callees for one procedure identified by symbol or address. Unresolved indirect calls may be absent; use analyze_function and typed references to preserve available edge uncertainty. Ghidra terminal-call assumptions that may cut the analyzed body are reported in Evidence limitations.",
|
||||
z.object({ procedure, document }),
|
||||
),
|
||||
official(
|
||||
"procedure_callers",
|
||||
"Return the provider's resolved direct callers for one procedure identified by symbol or address. Results reflect completed static analysis and may omit unresolved indirect references.",
|
||||
"Return the provider's resolved direct callers for one procedure identified by symbol or address. Results reflect completed static analysis and may omit unresolved indirect references. Ghidra terminal-call assumptions that may cut the analyzed body are reported in Evidence limitations.",
|
||||
z.object({ procedure, document }),
|
||||
),
|
||||
official(
|
||||
|
||||
@@ -6,6 +6,34 @@ import { z } from "zod";
|
||||
|
||||
import { toolContract } from "./toolContracts.js";
|
||||
import { toolOutputSchemaWithMetadata } from "./toolSchemaMetadata.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
|
||||
it.each(["draft-2020-12", "draft-07"] as const)(
|
||||
"advertises the raw-only 6502 Evidence subject constraint in %s",
|
||||
(target) => {
|
||||
const valid = createEvidence(
|
||||
{
|
||||
path: "/fixture/image.bin",
|
||||
sha256: "a".repeat(64),
|
||||
format: "raw-binary",
|
||||
architecture: "6502",
|
||||
},
|
||||
{ id: "fixture", name: "Fixture", version: "1" },
|
||||
{ operation: "address_name", parameters: {}, result: "entry" },
|
||||
);
|
||||
const advertised = toolOutputSchemaWithMetadata(
|
||||
toolContract("address_name"),
|
||||
)["~standard"].jsonSchema.output({ target });
|
||||
const validate = new AjvJsonSchemaValidator().getValidator(advertised);
|
||||
expect(validate(valid).valid).toBe(true);
|
||||
expect(
|
||||
validate({
|
||||
...valid,
|
||||
subject: { ...valid.subject, format: "pe" },
|
||||
}).valid,
|
||||
).toBe(false);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps dialect-specific validators distinct after JSON schema serialization", () => {
|
||||
const contract = {
|
||||
|
||||
@@ -444,11 +444,11 @@ const lifecycleTargetFields = {
|
||||
path: z.string(),
|
||||
kind: targetKindSchema,
|
||||
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
||||
architecture: z.enum(BINARY_ARCHITECTURES).nullable(),
|
||||
};
|
||||
const detectedLifecycleTargetSchema = z.object({
|
||||
...lifecycleTargetFields,
|
||||
format: targetFormatSchema.exclude(["raw-binary"]),
|
||||
architecture: z.enum(BINARY_ARCHITECTURES).exclude(["6502"]).nullable(),
|
||||
});
|
||||
const rawLifecycleTargetSchema = z.object({
|
||||
...lifecycleTargetFields,
|
||||
|
||||
@@ -52,7 +52,7 @@ const snapshotWithHistoricalEvidence = () => {
|
||||
};
|
||||
return {
|
||||
current,
|
||||
snapshot: {
|
||||
snapshot: analysisSnapshotSchema.parse({
|
||||
target,
|
||||
binding,
|
||||
entries: [entry],
|
||||
@@ -66,11 +66,32 @@ const snapshotWithHistoricalEvidence = () => {
|
||||
result: { summary: "unprofiled" },
|
||||
}),
|
||||
]),
|
||||
} satisfies AnalysisSnapshot,
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
describe("analysis snapshot target identity", () => {
|
||||
it("keeps 6502 confined to executable raw snapshots", () => {
|
||||
const target = {
|
||||
sha256: ANALYSIS_SNAPSHOT_TARGET.sha256,
|
||||
kind: "executable",
|
||||
architecture: "6502",
|
||||
};
|
||||
expect(
|
||||
analysisSnapshotSchema.shape.target.safeParse({
|
||||
...target,
|
||||
format: "pe",
|
||||
}).success,
|
||||
).toBe(false);
|
||||
expect(
|
||||
analysisSnapshotSchema.shape.target.safeParse({
|
||||
...target,
|
||||
format: "raw-binary",
|
||||
entry_point: "0x8001",
|
||||
}).success,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("partitions raw snapshots and query identities by the canonical entry", () => {
|
||||
const target: BinaryTarget = {
|
||||
path: "/fixture/image.bin",
|
||||
@@ -120,6 +141,48 @@ describe("analysis snapshot target identity", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("analysis snapshot nested subject contract", () => {
|
||||
it("rejects raw-only 6502 in nested detected execution subjects", () => {
|
||||
const { snapshot } = snapshotWithHistoricalEvidence();
|
||||
const entry = snapshot.entries[0];
|
||||
if (entry === undefined) throw new Error("Fixture has no snapshot entry");
|
||||
const malformedSubject = {
|
||||
...entry.execution.subject,
|
||||
format: "pe",
|
||||
architecture: "6502",
|
||||
};
|
||||
expect(
|
||||
analysisSnapshotSchema.safeParse({
|
||||
...snapshot,
|
||||
entries: [
|
||||
{
|
||||
...entry,
|
||||
execution: {
|
||||
...entry.execution,
|
||||
subject: malformedSubject,
|
||||
},
|
||||
},
|
||||
],
|
||||
}).success,
|
||||
).toBe(false);
|
||||
expect(
|
||||
analysisSnapshotSchema.safeParse({
|
||||
...snapshot,
|
||||
workflow_entries: [
|
||||
{
|
||||
...entry,
|
||||
execution: {
|
||||
...entry.execution,
|
||||
analysis_profile: snapshot.binding.analysis_profile,
|
||||
subject: malformedSubject,
|
||||
},
|
||||
},
|
||||
],
|
||||
}).success,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("analysis snapshot contract", () => {
|
||||
it.each([
|
||||
["dos-mz", "x86"],
|
||||
|
||||
@@ -41,7 +41,10 @@ import { digestSchema } from "./../domain/digests.js";
|
||||
import { prefixedDigestSchema } from "./../domain/digests.js";
|
||||
import { canonicalJsonDigestSteps } from "./canonicalJsonDigestSteps.js";
|
||||
import { freezeJsonSnapshot } from "./immutableJson.js";
|
||||
import { rawBinaryEntryPointSchema } from "./rawBinary.js";
|
||||
import {
|
||||
binaryArchitectureSchema,
|
||||
rawBinaryEntryPointSchema,
|
||||
} from "./rawBinary.js";
|
||||
import {
|
||||
relationshipCoverageSchema,
|
||||
type RelationshipCoverage,
|
||||
@@ -73,20 +76,30 @@ const formatSchema = z.enum([
|
||||
"source-map",
|
||||
]);
|
||||
const kindSchema = z.enum(["executable", "database", "archive", "artifact"]);
|
||||
const subjectSchema = z.object({
|
||||
const subjectBaseSchema = z.object({
|
||||
path: localPathStringSchema,
|
||||
sha256: digestSchema,
|
||||
format: z.enum([
|
||||
...formatSchema.options,
|
||||
"hopper",
|
||||
"directory",
|
||||
"file",
|
||||
"unknown",
|
||||
"javascript-bundle",
|
||||
"entitlements",
|
||||
]),
|
||||
architecture: architectureSchema.nullable(),
|
||||
});
|
||||
const subjectFormatSchema = z.enum([
|
||||
...formatSchema.options,
|
||||
"hopper",
|
||||
"directory",
|
||||
"file",
|
||||
"unknown",
|
||||
"javascript-bundle",
|
||||
"entitlements",
|
||||
]);
|
||||
// Snapshot entries retain the same raw-only architecture boundary as targets.
|
||||
const subjectSchema = z.discriminatedUnion("format", [
|
||||
subjectBaseSchema.extend({
|
||||
format: subjectFormatSchema.exclude(["raw-binary"]),
|
||||
architecture: architectureSchema.exclude(["6502"]).nullable(),
|
||||
}),
|
||||
subjectBaseSchema.extend({
|
||||
format: z.literal("raw-binary"),
|
||||
architecture: architectureSchema.nullable(),
|
||||
}),
|
||||
]);
|
||||
const targetIdentitySchema = z.object({
|
||||
sha256: digestSchema,
|
||||
kind: kindSchema,
|
||||
@@ -97,9 +110,12 @@ const targetSchema = z.discriminatedUnion("format", [
|
||||
format: z.enum(
|
||||
formatSchema.options.filter((format) => format !== "raw-binary"),
|
||||
),
|
||||
architecture: architectureSchema.exclude(["6502"]).nullable(),
|
||||
}),
|
||||
targetIdentitySchema.extend({
|
||||
format: z.literal("raw-binary"),
|
||||
kind: z.literal("executable"),
|
||||
architecture: binaryArchitectureSchema,
|
||||
entry_point: rawBinaryEntryPointSchema,
|
||||
}),
|
||||
]);
|
||||
@@ -332,18 +348,21 @@ const canonicalJson = (value: unknown): string => {
|
||||
};
|
||||
|
||||
/** Project a binary into the path-free identity used for cache invalidation. */
|
||||
export const snapshotTarget = (
|
||||
target: BinaryTarget,
|
||||
): AnalysisSnapshotTarget => {
|
||||
const identity = {
|
||||
sha256: target.sha256,
|
||||
kind: target.kind,
|
||||
architecture: target.architecture ?? null,
|
||||
};
|
||||
return target.format === "raw-binary"
|
||||
? { ...identity, format: target.format, entry_point: target.entryPoint }
|
||||
: { ...identity, format: target.format };
|
||||
};
|
||||
export const snapshotTarget = (target: BinaryTarget): AnalysisSnapshotTarget =>
|
||||
target.format === "raw-binary"
|
||||
? {
|
||||
sha256: target.sha256,
|
||||
kind: target.kind,
|
||||
architecture: target.architecture,
|
||||
format: target.format,
|
||||
entry_point: target.entryPoint,
|
||||
}
|
||||
: {
|
||||
sha256: target.sha256,
|
||||
kind: target.kind,
|
||||
architecture: target.architecture ?? null,
|
||||
format: target.format,
|
||||
};
|
||||
|
||||
/** Build the immutable provider/profile binding used by a cached snapshot. */
|
||||
export const snapshotBinding = (
|
||||
@@ -475,10 +494,10 @@ export const createAnalysisSnapshotWorkflowEntry = (input: {
|
||||
subject:
|
||||
execution.subject === null
|
||||
? null
|
||||
: {
|
||||
: subjectSchema.parse({
|
||||
...execution.subject,
|
||||
architecture: execution.subject.architecture ?? null,
|
||||
},
|
||||
}),
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -538,10 +557,10 @@ export const createAnalysisSnapshotEntry = (input: {
|
||||
subject:
|
||||
execution.subject === null
|
||||
? null
|
||||
: {
|
||||
: subjectSchema.parse({
|
||||
...execution.subject,
|
||||
architecture: execution.subject.architecture ?? null,
|
||||
},
|
||||
}),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -510,6 +510,106 @@ describe("AppKit keyed archive connectors", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("keyed archive unresolved connectors", () => {
|
||||
// A rooted UIKit archive whose only connector is UID 3, with UID 1 as the
|
||||
// owner, UID 2 as the destination and UID 7 as the "view" label.
|
||||
const rootedOutletArchive = (connection: Record<string, unknown>) => ({
|
||||
$archiver: "NSKeyedArchiver",
|
||||
$version: 100000,
|
||||
$objects: [
|
||||
"$null",
|
||||
{ $class: { CF$UID: 4 } },
|
||||
{ $class: { CF$UID: 5 } },
|
||||
{ $class: { CF$UID: 6 }, ...connection },
|
||||
{
|
||||
$classname: "UIViewController",
|
||||
$classes: ["UIViewController", "UIResponder", "NSObject"],
|
||||
},
|
||||
{ $classname: "UIView", $classes: ["UIView", "UIResponder", "NSObject"] },
|
||||
{
|
||||
$classname: "UIRuntimeOutletConnection",
|
||||
$classes: [
|
||||
"UIRuntimeOutletConnection",
|
||||
"UIRuntimeConnection",
|
||||
"NSObject",
|
||||
],
|
||||
},
|
||||
"view",
|
||||
{
|
||||
$class: { CF$UID: 11 },
|
||||
"NS.objects": [{ CF$UID: 1 }, { CF$UID: 2 }, { CF$UID: 3 }],
|
||||
},
|
||||
{ $class: { CF$UID: 11 }, "NS.objects": [{ CF$UID: 3 }] },
|
||||
{
|
||||
$class: { CF$UID: 11 },
|
||||
"NS.objects": [{ CF$UID: 1 }, { CF$UID: 2 }],
|
||||
},
|
||||
{ $classname: "NSArray", $classes: ["NSArray", "NSObject"] },
|
||||
],
|
||||
$top: {
|
||||
root: { CF$UID: 8 },
|
||||
UINibConnectionsKey: { CF$UID: 9 },
|
||||
UINibObjectsKey: { CF$UID: 10 },
|
||||
UINibTopLevelObjectsKey: { CF$UID: 10 },
|
||||
},
|
||||
});
|
||||
|
||||
const project = (raw: unknown) =>
|
||||
buildInterfaceBuilderAnalysis({
|
||||
targetSha256: hash,
|
||||
toolVersion: "test",
|
||||
documents: [
|
||||
{
|
||||
relativePath: "Main.nib",
|
||||
archiveSha256: hash,
|
||||
documentKind: "nib",
|
||||
raw,
|
||||
},
|
||||
],
|
||||
limits: interfaceBuilderLimitsSchema.parse({}),
|
||||
});
|
||||
|
||||
it("covers a resolvable connector as a complete examined connection", () => {
|
||||
const raw = rootedOutletArchive({
|
||||
UISource: { CF$UID: 1 },
|
||||
UIDestination: { CF$UID: 2 },
|
||||
UILabel: { CF$UID: 7 },
|
||||
});
|
||||
expect(parseInterfaceBuilderRecords(raw).unresolvedConnections).toBe(0);
|
||||
expect(project(raw).graph.coverage).toContainEqual(
|
||||
expect.objectContaining({
|
||||
facet: "connections:Main.nib",
|
||||
status: "complete",
|
||||
examined: 1,
|
||||
omitted: 0,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("reports a recognized connector with no resolvable owner as unresolved", () => {
|
||||
const raw = rootedOutletArchive({
|
||||
UIDestination: { CF$UID: 2 },
|
||||
UILabel: { CF$UID: 7 },
|
||||
});
|
||||
expect(parseInterfaceBuilderRecords(raw).unresolvedConnections).toBe(1);
|
||||
|
||||
const analysis = project(raw);
|
||||
expect(analysis.graph.nodes.some(({ kind }) => kind === "outlet")).toBe(
|
||||
false,
|
||||
);
|
||||
expect(analysis.graph.coverage).toContainEqual(
|
||||
expect.objectContaining({
|
||||
facet: "connections:Main.nib",
|
||||
status: "partial",
|
||||
reason: "connection_unresolved",
|
||||
examined: 0,
|
||||
omitted: 1,
|
||||
}),
|
||||
);
|
||||
expect(analysis.graph.truncated).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("names the archive once per evidence record", () => {
|
||||
const relativePath = "Views/Main.storyboardc/scene.nib/objects.nib";
|
||||
const result = buildInterfaceBuilderAnalysis({
|
||||
|
||||
@@ -410,18 +410,26 @@ export const buildInterfaceBuilderAnalysis = (input: {
|
||||
const connectionsTruncated =
|
||||
parserOmittedConnections > 0 ||
|
||||
parsed.connections.length > input.limits.max_connections;
|
||||
// A recognized connector the parser could not attach to an owner produces
|
||||
// no connection record, so its coverage is partial with its own reason
|
||||
// rather than absent or truncated.
|
||||
const connectionsUnresolved = parsed.unresolvedConnections > 0;
|
||||
omittedObjects +=
|
||||
parsed.omittedObjects +
|
||||
Math.max(0, parsed.objects.length - documentObjectCount);
|
||||
omittedConnections +=
|
||||
Math.max(0, parsed.connections.length - input.limits.max_connections) +
|
||||
parserOmittedConnections;
|
||||
parserOmittedConnections +
|
||||
parsed.unresolvedConnections;
|
||||
summaries.push({
|
||||
relative_path: document.relativePath,
|
||||
archive_sha256: document.archiveSha256,
|
||||
document_kind: document.documentKind,
|
||||
object_count: parsed.objectCount,
|
||||
connection_count: parsed.connections.length + parserOmittedConnections,
|
||||
connection_count:
|
||||
parsed.connections.length +
|
||||
parserOmittedConnections +
|
||||
parsed.unresolvedConnections,
|
||||
hierarchy_complete:
|
||||
parsed.hierarchy.length > 0 &&
|
||||
!truncated &&
|
||||
@@ -440,13 +448,17 @@ export const buildInterfaceBuilderAnalysis = (input: {
|
||||
{
|
||||
facet: `connections:${document.relativePath}`,
|
||||
status:
|
||||
connectionsTruncated || documentOmittedConnections > 0
|
||||
connectionsTruncated ||
|
||||
documentOmittedConnections > 0 ||
|
||||
connectionsUnresolved
|
||||
? "partial"
|
||||
: "complete",
|
||||
reason:
|
||||
connectionsTruncated || documentOmittedConnections > 0
|
||||
? "connection_limit_reached"
|
||||
: null,
|
||||
: connectionsUnresolved
|
||||
? "connection_unresolved"
|
||||
: null,
|
||||
examined: Math.min(
|
||||
parsed.connections.length,
|
||||
input.limits.max_connections,
|
||||
@@ -457,7 +469,8 @@ export const buildInterfaceBuilderAnalysis = (input: {
|
||||
parsed.connections.length - input.limits.max_connections,
|
||||
) +
|
||||
documentOmittedConnections +
|
||||
parserOmittedConnections,
|
||||
parserOmittedConnections +
|
||||
parsed.unresolvedConnections,
|
||||
},
|
||||
{
|
||||
facet: `hierarchy:${document.relativePath}`,
|
||||
|
||||
@@ -55,6 +55,7 @@ export const parseInterfaceBuilderRecords = (
|
||||
readonly objectCount: number;
|
||||
readonly omittedObjects: number;
|
||||
readonly omittedConnections: number;
|
||||
readonly unresolvedConnections: number;
|
||||
readonly omittedHierarchyReferences: number;
|
||||
} => {
|
||||
const root = record(value);
|
||||
@@ -135,6 +136,7 @@ export const parseInterfaceBuilderRecords = (
|
||||
objectCount: objectEntries.length,
|
||||
omittedObjects: Math.max(0, objectEntries.length - 20_000),
|
||||
omittedConnections,
|
||||
unresolvedConnections: 0,
|
||||
omittedHierarchyReferences: 0,
|
||||
};
|
||||
};
|
||||
@@ -320,6 +322,7 @@ const parseKeyedArchive = (
|
||||
const connections: InterfaceBuilderConnection[] = [];
|
||||
let objectCount = 0;
|
||||
let omittedConnections = 0;
|
||||
let unresolvedConnections = 0;
|
||||
for (const [index, raw] of objectTable.entries()) {
|
||||
if (index === 0) continue;
|
||||
const resolved = record(resolve(raw));
|
||||
@@ -371,7 +374,13 @@ const parseKeyedArchive = (
|
||||
controlAction ? fields.to : fields.from,
|
||||
controlAction ? fields.target : fields.owner,
|
||||
);
|
||||
if (source === null) continue;
|
||||
if (source === null) {
|
||||
// A recognized connector that cannot name its owner is an unresolved
|
||||
// connection, not an absent one, and cannot be represented as a
|
||||
// connection because connection identity requires a source.
|
||||
unresolvedConnections += 1;
|
||||
continue;
|
||||
}
|
||||
const kind = controlAction ? "action" : classifyConnection(className);
|
||||
const parsedConnection = connection.parse({
|
||||
id: `archive:${objectId}`,
|
||||
@@ -426,6 +435,7 @@ const parseKeyedArchive = (
|
||||
objectCount,
|
||||
omittedObjects: Math.max(0, objectCount - 20_000),
|
||||
omittedConnections,
|
||||
unresolvedConnections,
|
||||
omittedHierarchyReferences: hierarchy.omittedReferences,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -351,7 +351,9 @@ const machArchitecture = (cpu: number): HeaderArchitecture | undefined => {
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const elfArchitecture = (machine: number): BinaryArchitecture | undefined => {
|
||||
const elfArchitecture = (
|
||||
machine: number,
|
||||
): Exclude<BinaryArchitecture, "6502"> | undefined => {
|
||||
switch (machine) {
|
||||
case 3:
|
||||
return "x86";
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import type { ZipPackageFormat } from "./zipPackageFormat.js";
|
||||
|
||||
/** Provider-neutral CPU families detected from supported executable headers. */
|
||||
/** Provider-neutral CPU families detected from headers or explicitly selected raw loads. */
|
||||
export const BINARY_ARCHITECTURES = [
|
||||
"x86",
|
||||
"x86_64",
|
||||
"arm",
|
||||
"arm64",
|
||||
"mips",
|
||||
"6502",
|
||||
"riscv",
|
||||
] as const;
|
||||
export type BinaryArchitecture = (typeof BINARY_ARCHITECTURES)[number];
|
||||
@@ -40,7 +41,7 @@ export const isElfMetadataArchitecture = (
|
||||
/** Recognized family with no ELF metadata member; the header alone is the fact. */
|
||||
export type HeaderArchitecture = Exclude<
|
||||
BinaryArchitecture,
|
||||
ElfMetadataArchitecture
|
||||
ElfMetadataArchitecture | "6502"
|
||||
>;
|
||||
|
||||
/** Raw ELF facts; family recognition does not establish ISA or ABI support. */
|
||||
@@ -144,8 +145,17 @@ export type BinaryTarget =
|
||||
readonly executableRole?: never;
|
||||
readonly managed?: never;
|
||||
})
|
||||
| (ExecutableTarget & {
|
||||
| (BinaryTargetIdentity & {
|
||||
readonly kind: "executable";
|
||||
readonly format: "raw-binary";
|
||||
readonly architecture: Exclude<
|
||||
BinaryArchitecture,
|
||||
ElfMetadataArchitecture
|
||||
>;
|
||||
readonly availableArchitectures: readonly Exclude<
|
||||
BinaryArchitecture,
|
||||
ElfMetadataArchitecture
|
||||
>[];
|
||||
readonly entryPoint: string;
|
||||
readonly entryPointSource: string;
|
||||
readonly executableRole?: never;
|
||||
|
||||
@@ -19,6 +19,33 @@ const TARGET: BinaryTarget = {
|
||||
const PROVIDER = { id: "fixture", name: "Fixture provider", version: "1" };
|
||||
const PROFILE = createAnalysisProfile(PROVIDER, { loader: "default" });
|
||||
|
||||
it("rejects a detected Evidence subject with the raw-only 6502 architecture", () => {
|
||||
const selected = {
|
||||
path: TARGET.path,
|
||||
sha256: TARGET.sha256,
|
||||
format: "raw-binary" as const,
|
||||
architecture: "6502" as const,
|
||||
};
|
||||
expect(
|
||||
createEvidence(selected, PROVIDER, {
|
||||
operation: "inspect",
|
||||
parameters: {},
|
||||
result: null,
|
||||
}).subject,
|
||||
).toMatchObject({ format: "raw-binary", architecture: "6502" });
|
||||
const malformed = {
|
||||
...selected,
|
||||
format: "pe" as const,
|
||||
};
|
||||
expect(() =>
|
||||
createEvidence(malformed, PROVIDER, {
|
||||
operation: "inspect",
|
||||
parameters: {},
|
||||
result: null,
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("preserves omitted relationship diagnostics and authenticates reported diagnostics", () => {
|
||||
const observation = {
|
||||
operation: "procedure_callees",
|
||||
|
||||
+43
-33
@@ -38,42 +38,52 @@ export const providerSchema = z.strictObject({
|
||||
name: z.string().min(1),
|
||||
version: z.string().nullable(),
|
||||
});
|
||||
const subjectSchema = z.strictObject({
|
||||
const subjectFormatSchema = z.enum([
|
||||
"hopper",
|
||||
"analysis-database",
|
||||
"mach-o",
|
||||
"elf",
|
||||
"pe",
|
||||
"dos-mz",
|
||||
"dos-com",
|
||||
"raw-binary",
|
||||
"zip",
|
||||
"ipa",
|
||||
"apk",
|
||||
"msix",
|
||||
"appx",
|
||||
"hap",
|
||||
"hsp",
|
||||
"app-pack",
|
||||
"asar",
|
||||
"dmg",
|
||||
"pkg",
|
||||
"plist",
|
||||
"javascript",
|
||||
"source-map",
|
||||
"directory",
|
||||
"file",
|
||||
"unknown",
|
||||
"mach-o-universal",
|
||||
"javascript-bundle",
|
||||
"entitlements",
|
||||
]);
|
||||
const subjectBaseSchema = z.strictObject({
|
||||
name: z.string().min(1),
|
||||
digest: z.strictObject({ sha256: digestSchema }),
|
||||
format: z.enum([
|
||||
"hopper",
|
||||
"analysis-database",
|
||||
"mach-o",
|
||||
"elf",
|
||||
"pe",
|
||||
"dos-mz",
|
||||
"dos-com",
|
||||
"raw-binary",
|
||||
"zip",
|
||||
"ipa",
|
||||
"apk",
|
||||
"msix",
|
||||
"appx",
|
||||
"hap",
|
||||
"hsp",
|
||||
"app-pack",
|
||||
"asar",
|
||||
"dmg",
|
||||
"pkg",
|
||||
"plist",
|
||||
"javascript",
|
||||
"source-map",
|
||||
"directory",
|
||||
"file",
|
||||
"unknown",
|
||||
"mach-o-universal",
|
||||
"javascript-bundle",
|
||||
"entitlements",
|
||||
]),
|
||||
architecture: z.enum(BINARY_ARCHITECTURES).nullable(),
|
||||
local_path: nulFreeStringSchema,
|
||||
});
|
||||
// The format owns the architecture claim, including nested Evidence subjects.
|
||||
const subjectSchema = z.discriminatedUnion("format", [
|
||||
subjectBaseSchema.extend({
|
||||
format: subjectFormatSchema.exclude(["raw-binary"]),
|
||||
architecture: z.enum(BINARY_ARCHITECTURES).exclude(["6502"]).nullable(),
|
||||
}),
|
||||
subjectBaseSchema.extend({
|
||||
format: z.literal("raw-binary"),
|
||||
architecture: z.enum(BINARY_ARCHITECTURES).nullable(),
|
||||
}),
|
||||
]);
|
||||
/** Source location attached to an evidence observation. */
|
||||
/** Source-location schema shared by evidence-bearing persistence formats. */
|
||||
export const evidenceLocationSchema = z.discriminatedUnion("kind", [
|
||||
@@ -186,7 +196,7 @@ const immutableResultSchema = z
|
||||
});
|
||||
export type EvidenceLocation = z.infer<typeof evidenceLocationSchema>;
|
||||
|
||||
/** Minimal immutable local artifact identity accepted by Evidence. */
|
||||
/** Candidate local artifact identity validated by the Evidence subject schema. */
|
||||
export interface EvidenceSubjectTarget {
|
||||
readonly path: string;
|
||||
readonly sha256: string;
|
||||
|
||||
@@ -16,14 +16,17 @@ import {
|
||||
} from "./residualUnknown.js";
|
||||
import { freezeJsonSnapshot } from "./immutableJson.js";
|
||||
|
||||
const artifactManifestSchema = evidenceSchema.shape.subject
|
||||
.unwrap()
|
||||
.pick({
|
||||
digest: true,
|
||||
format: true,
|
||||
architecture: true,
|
||||
})
|
||||
.strict();
|
||||
const [detectedSubjectSchema, rawSubjectSchema] =
|
||||
evidenceSchema.shape.subject.unwrap().options;
|
||||
const manifestFields = {
|
||||
digest: true,
|
||||
format: true,
|
||||
architecture: true,
|
||||
} as const;
|
||||
const artifactManifestSchema = z.discriminatedUnion("format", [
|
||||
detectedSubjectSchema.pick(manifestFields),
|
||||
rawSubjectSchema.pick(manifestFields),
|
||||
]);
|
||||
const providerManifestSchema = providerSchema;
|
||||
const environmentManifestSchema = evidenceSchema.shape.environment.unwrap();
|
||||
const scenarioManifestSchema = z.strictObject({
|
||||
@@ -85,11 +88,11 @@ export const createEvidenceBundle = (
|
||||
subject === null
|
||||
? []
|
||||
: [
|
||||
{
|
||||
artifactManifestSchema.parse({
|
||||
digest: subject.digest,
|
||||
format: subject.format,
|
||||
architecture: subject.architecture,
|
||||
},
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -0,0 +1,351 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
import { onlyCallable } from "./javascriptSemanticAnalysis.fixture.js";
|
||||
|
||||
const returnedValue = (getter: string, write = "box.v.x = 2;") =>
|
||||
onlyCallable(
|
||||
analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1, y: 1 };
|
||||
const box = { s: shared, get v() { ${getter} } };
|
||||
${write}
|
||||
return shared.x;
|
||||
}`),
|
||||
"result",
|
||||
).returnSites[0]?.value;
|
||||
|
||||
describe("projected private getter receivers", () => {
|
||||
it.each([
|
||||
[
|
||||
"destructured self field",
|
||||
"shared",
|
||||
"peer = this;",
|
||||
"const { peer: self } = this; return self.#s;",
|
||||
true,
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"unrelated private child",
|
||||
"{ peer: shared }",
|
||||
"peer = this;",
|
||||
"const { peer: self } = this; return self.#s;",
|
||||
true,
|
||||
"literal",
|
||||
],
|
||||
[
|
||||
"read without write",
|
||||
"shared",
|
||||
"peer = this;",
|
||||
"const { peer: self } = this; return self.#s;",
|
||||
false,
|
||||
"literal",
|
||||
],
|
||||
[
|
||||
"two self fields",
|
||||
"shared",
|
||||
"peer = this; next = this.peer;",
|
||||
"const { next: self } = this; return self.#s;",
|
||||
true,
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"nested receiver projection",
|
||||
"shared",
|
||||
"peer = this;",
|
||||
"const { peer: { peer: self } } = this; return self.#s;",
|
||||
true,
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"member receiver alias",
|
||||
"shared",
|
||||
"peer = this;",
|
||||
"const self = this.peer.peer; return self.#s;",
|
||||
true,
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"projected result suffix",
|
||||
"{ child: shared }",
|
||||
"peer = this;",
|
||||
"const { peer: self } = this; return self.#s.child;",
|
||||
true,
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"external receiver",
|
||||
"shared",
|
||||
"peer = external;",
|
||||
"const { peer: self } = this; return self.#s;",
|
||||
true,
|
||||
"literal",
|
||||
],
|
||||
[
|
||||
"field receiver cycle",
|
||||
"shared",
|
||||
"peer = this.other; other = this.peer;",
|
||||
"const { peer: self } = this; return self.#s;",
|
||||
true,
|
||||
"literal",
|
||||
],
|
||||
] as const)(
|
||||
"keeps receiver and result paths separate for %s",
|
||||
(_name, value, fields, getter, write, status) => {
|
||||
const ir = analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1 };
|
||||
class Box {
|
||||
#s = ${value}; ${fields}
|
||||
get v() { ${getter} }
|
||||
}
|
||||
const box = new Box();
|
||||
${write ? "box.v.x = 2;" : "const saved = box.v;"}
|
||||
return shared.x;
|
||||
}`);
|
||||
expect(onlyCallable(ir, "result").returnSites[0]?.value).toMatchObject(
|
||||
status === "literal" ? { status, value: 1 } : { status },
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("private receiver field ownership", () => {
|
||||
it("bounds growing receiver projections under cyclic class inheritance", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1 };
|
||||
class A extends B {
|
||||
#s = shared;
|
||||
peer = this.other.other;
|
||||
get v() { const { peer: self } = this; return self.#s; }
|
||||
}
|
||||
class B extends A { other = this.peer.peer; }
|
||||
new A().v.x = 2;
|
||||
return shared.x;
|
||||
}`);
|
||||
expect(onlyCallable(ir, "result").returnSites[0]?.value).toMatchObject({
|
||||
status: "literal",
|
||||
value: 1,
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
it.each([
|
||||
[
|
||||
"own static receiver",
|
||||
"class Box { static #s=shared; static peer=this; static get v(){const {peer:self}=this;return self.#s;} } Box.v.x=2;",
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"inherited instance receiver",
|
||||
"class Base {peer=this;} class Box extends Base {#s=shared;get v(){const {peer:self}=this;return self.#s;}} new Box().v.x=2;",
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"inherited static field retains its declaring class",
|
||||
"class Base {static peer=this;} class Box extends Base {static #s=shared;static get v(){const {peer:self}=this;return self.#s;}} Box.v.x=2;",
|
||||
"literal",
|
||||
],
|
||||
[
|
||||
"inherited static getter's own private field",
|
||||
"class Base {static #s=shared;static peer=this;static get v(){const {peer:self}=this;return self.#s;}} class Box extends Base {} Box.v.x=2;",
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"a field that is not initialized yet",
|
||||
"class Box {#s=shared;peer=this.later;later=this;get v(){const {peer:self}=this;return self.#s;}} new Box().v.x=2;",
|
||||
"literal",
|
||||
],
|
||||
[
|
||||
"remaining getter projection after a captured field",
|
||||
"class Box {#s=shared;peer=this;next=this.peer;later=this;get v(){const {next:{later:self}}=this;return self.#s;}} new Box().v.x=2;",
|
||||
"unknown",
|
||||
],
|
||||
[
|
||||
"a replaced self field",
|
||||
"class Box {#s=shared;peer=this;peer=external;get v(){const {peer:self}=this;return self.#s;}} new Box().v.x=2;",
|
||||
"literal",
|
||||
],
|
||||
] as const)("respects %s", (_name, body, status) => {
|
||||
const ir = analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1 };
|
||||
${body}
|
||||
return shared.x;
|
||||
}`);
|
||||
expect(onlyCallable(ir, "result").returnSites[0]?.value).toMatchObject(
|
||||
status === "literal" ? { status, value: 1 } : { status },
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getter-local receiver aliases (#2653)", () => {
|
||||
it.each([
|
||||
["initialized alias", "const self = this; return self.s;"],
|
||||
["assigned alias", "let self; self = this; return self.s;"],
|
||||
[
|
||||
"chained assignment",
|
||||
"let self; let alias; self = alias = this; return self.s;",
|
||||
],
|
||||
["destructured receiver", "const { s: value } = this; return value;"],
|
||||
["alias chain", "const self = this; const saved = self; return saved.s;"],
|
||||
[
|
||||
"capture before reassignment",
|
||||
"let self = this; const saved = self; self = { s: { x: 1 } }; return saved.s;",
|
||||
],
|
||||
[
|
||||
"conditional receiver alias",
|
||||
"const self = flag ? this : { s: { x: 1 } }; return self.s;",
|
||||
],
|
||||
])("invalidates shared values through %s", (_name, getter) => {
|
||||
expect(returnedValue(getter)?.status).toBe("unknown");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["no write", "const self = this; return self.s;", "const saved = box.v;"],
|
||||
[
|
||||
"an unrelated property write",
|
||||
"const self = this; return self.s;",
|
||||
"box.v.y = 2;",
|
||||
],
|
||||
[
|
||||
"a cycle that does not grow the selected path",
|
||||
"let self = this; while (flag) self = self; return self.s;",
|
||||
"box.v.y = 2;",
|
||||
],
|
||||
[
|
||||
"an alias replaced before return",
|
||||
"let self = this; self = { s: { x: 1 } }; return self.s;",
|
||||
"box.v.x = 2;",
|
||||
],
|
||||
[
|
||||
"a shadowing block binding",
|
||||
"const self = this; { const self = { s: { x: 1 } }; return self.s; }",
|
||||
"box.v.x = 2;",
|
||||
],
|
||||
[
|
||||
"a nested function's receiver",
|
||||
"function other() { const self = this; return self.s; } return other();",
|
||||
"box.v.x = 2;",
|
||||
],
|
||||
[
|
||||
"an alias assigned in a different receiver scope",
|
||||
"let self; function assign() { self = this; } assign(); return self.s;",
|
||||
"box.v.x = 2;",
|
||||
],
|
||||
[
|
||||
"an excluded destructuring default",
|
||||
"const { value = shared } = { value: { x: 1 } }; return value;",
|
||||
"box.v.x = 2;",
|
||||
],
|
||||
])("preserves known values with %s", (_name, getter, write) => {
|
||||
expect(returnedValue(getter, write)).toMatchObject({
|
||||
status: "literal",
|
||||
value: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("getter receiver alias boundaries", () => {
|
||||
it("does not reinterpret an outer this binding as the getter receiver", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1 };
|
||||
const outside = this;
|
||||
const box = { s: shared, get v() { return outside.s; } };
|
||||
box.v.x = 2;
|
||||
return shared.x;
|
||||
}`);
|
||||
expect(onlyCallable(ir, "result").returnSites[0]?.value).toMatchObject({
|
||||
status: "literal",
|
||||
value: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("retains a private field's lexical owner through a receiver alias", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1 };
|
||||
class Box {
|
||||
#s = shared;
|
||||
get v() { const self = this; return self.#s; }
|
||||
}
|
||||
new Box().v.x = 2;
|
||||
return shared.x;
|
||||
}`);
|
||||
expect(onlyCallable(ir, "result").returnSites[0]?.value.status).toBe(
|
||||
"unknown",
|
||||
);
|
||||
});
|
||||
|
||||
it("does not append a receiver projection to a private field's result path", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1 };
|
||||
class Box {
|
||||
#s = { peer: shared };
|
||||
peer = this;
|
||||
get v() { const { peer: self } = this; return self.#s; }
|
||||
}
|
||||
new Box().v.x = 2;
|
||||
return shared.x;
|
||||
}`);
|
||||
expect(onlyCallable(ir, "result").returnSites[0]?.value).toMatchObject({
|
||||
status: "literal",
|
||||
value: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves a long local alias chain without recursive expansion", () => {
|
||||
const aliases = ["const a0 = this;"];
|
||||
for (let index = 1; index <= 2000; index++)
|
||||
aliases.push(`const a${String(index)} = a${String(index - 1)};`);
|
||||
expect(returnedValue(`${aliases.join("\n")} return a2000.s;`)?.status).toBe(
|
||||
"unknown",
|
||||
);
|
||||
});
|
||||
|
||||
it("bounds a growing alias cycle without losing the original receiver", () => {
|
||||
expect(
|
||||
returnedValue(
|
||||
"let self = this; while (flag) self = self.s; return self.s;",
|
||||
)?.status,
|
||||
).toBe("unknown");
|
||||
});
|
||||
|
||||
it.each([true, false])(
|
||||
"retains deeper shared references through an alias cycle (write=%s)",
|
||||
(write) => {
|
||||
const ir = analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1 };
|
||||
const box = {
|
||||
start: { child: { child: shared } },
|
||||
get v() {
|
||||
let self = this.start;
|
||||
while (flag) self = self.child;
|
||||
return self;
|
||||
}
|
||||
};
|
||||
${write ? "box.v.x = 2;" : "const saved = box.v;"}
|
||||
return shared.x;
|
||||
}`);
|
||||
expect(onlyCallable(ir, "result").returnSites[0]?.value).toMatchObject(
|
||||
write ? { status: "unknown" } : { status: "literal", value: 1 },
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it("does not widen a growing cycle to unrelated receiver properties", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`export function result() {
|
||||
const shared = { x: 1 };
|
||||
const unrelated = { x: 1 };
|
||||
const box = {
|
||||
start: { child: { child: shared } },
|
||||
unrelated,
|
||||
get v() {
|
||||
let self = this.start;
|
||||
while (flag) self = self.child;
|
||||
return self;
|
||||
}
|
||||
};
|
||||
box.v.x = 2;
|
||||
return unrelated.x;
|
||||
}`);
|
||||
expect(onlyCallable(ir, "result").returnSites[0]?.value).toMatchObject({
|
||||
status: "literal",
|
||||
value: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -10,10 +10,7 @@ import {
|
||||
evaluateSemanticBinding,
|
||||
evaluateSemanticExpression,
|
||||
} from "./javascriptSemanticValues.js";
|
||||
import {
|
||||
semanticContainer,
|
||||
semanticSlotAtPath,
|
||||
} from "./javascriptSemanticSlots.js";
|
||||
import { semanticContainer } from "./javascriptSemanticSlots.js";
|
||||
import {
|
||||
semanticClassCandidates,
|
||||
semanticClassPropertyReferences,
|
||||
@@ -24,6 +21,10 @@ import { semanticIterationSources } from "./javascriptSemanticIterationSources.j
|
||||
import { semanticCallableResultExpressions } from "./javascriptSemanticCallableResults.js";
|
||||
import { createSemanticReturnedReferences } from "./javascriptSemanticReturnedReferences.js";
|
||||
import { semanticMutationInitializers } from "./javascriptSemanticMutationInitializers.js";
|
||||
import {
|
||||
semanticReferenceSourcesInactive,
|
||||
semanticSelectedReferenceSlot,
|
||||
} from "./javascriptSemanticReferenceSources.js";
|
||||
import {
|
||||
semanticArrayIndex,
|
||||
SemanticPropertyPathCoverage,
|
||||
@@ -329,7 +330,17 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
for (const value of effect === "escape"
|
||||
? escapedReferenceLeaves(expression, current.path)
|
||||
: expandedValues(expression, current.path, effect)) {
|
||||
if (value.iterableFallbackPath !== undefined)
|
||||
if (value.escapes && effect !== "escape")
|
||||
markValue(
|
||||
value.node,
|
||||
value.path,
|
||||
new Set(),
|
||||
"escape",
|
||||
mutation,
|
||||
current.originAt,
|
||||
value.ownerInitializer,
|
||||
);
|
||||
else if (value.iterableFallbackPath !== undefined)
|
||||
deferIterable({
|
||||
node: value.node,
|
||||
projection: [],
|
||||
@@ -504,7 +515,7 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
for (const [index, reference] of pendingReferences.splice(0).entries()) {
|
||||
if (index % 64 === 0) yield;
|
||||
const { initializer } = reference;
|
||||
if (referenceSourcesInactive(initializer, state))
|
||||
if (semanticReferenceSourcesInactive(initializer, state))
|
||||
pendingReferences.push(reference);
|
||||
else if (t.isForOfStatement(initializer.node))
|
||||
deferIterable({
|
||||
@@ -549,7 +560,7 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
}
|
||||
for (const [index, reference] of [...iterableReferences].entries()) {
|
||||
if (index % 64 === 0) yield;
|
||||
if (referenceSourcesInactive(reference, state)) continue;
|
||||
if (semanticReferenceSourcesInactive(reference, state)) continue;
|
||||
if (reference.iterationOnly) {
|
||||
const expanded = expandedIterationReferences(reference, state);
|
||||
if (expanded !== null) {
|
||||
@@ -557,7 +568,7 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const slot = selectedReferenceSlot(reference, state);
|
||||
const slot = semanticSelectedReferenceSlot(reference, state);
|
||||
// Custom iteration can yield the iterable itself as well as any child.
|
||||
// Treat its root as escaped, including writes through yielded references.
|
||||
const knownArray =
|
||||
@@ -610,27 +621,6 @@ interface IterableReference extends ReferencedValue {
|
||||
}[];
|
||||
}
|
||||
|
||||
const referenceSourcesInactive = (
|
||||
reference: Pick<IterableReference, "requiredSources" | "fallbackSources">,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): boolean =>
|
||||
reference.requiredSources?.some(
|
||||
(source) =>
|
||||
selectedReferenceSlot(source, state, source.node)?.presence === "absent",
|
||||
) === true ||
|
||||
reference.fallbackSources?.some((source) => {
|
||||
// Selection precedes effects through the chosen reference. A later escape
|
||||
// cannot make a fallback that was excluded at destructuring reachable.
|
||||
const slot = selectedReferenceSlot(source, state, source.node);
|
||||
return (
|
||||
slot?.presence === "present" &&
|
||||
(slot.value.status === "literal" ||
|
||||
slot.value.status === "union" ||
|
||||
slot.value.status === "object" ||
|
||||
slot.value.status === "array")
|
||||
);
|
||||
}) === true;
|
||||
|
||||
const expandedIterationReferences = (
|
||||
reference: IterableReference,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
@@ -645,7 +635,10 @@ const expandedIterationReferences = (
|
||||
...(reference.fallbackSources ?? []),
|
||||
].find((source) => source.node === iterable && source.projection[0] === null);
|
||||
if (source === undefined) return null;
|
||||
const slot = selectedReferenceSlot({ node: iterable, projection: [] }, state);
|
||||
const slot = semanticSelectedReferenceSlot(
|
||||
{ node: iterable, projection: [] },
|
||||
state,
|
||||
);
|
||||
const container = slot === undefined ? null : semanticContainer(slot.value);
|
||||
if (container?.coverage.status !== "complete") return null;
|
||||
const select = (
|
||||
@@ -1141,19 +1134,3 @@ const copiedReferencePath = (
|
||||
}
|
||||
return [...initializer.projection.slice(0, offset), key, ...remaining];
|
||||
};
|
||||
|
||||
const selectedReferenceSlot = (
|
||||
source: { readonly node: t.Node; readonly projection: PropertyPath },
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
capturePoint?: t.Node,
|
||||
) => {
|
||||
const path: string[] = [];
|
||||
for (const key of source.projection) {
|
||||
if (key === null || typeof key === "object") return undefined;
|
||||
path.push(String(key));
|
||||
}
|
||||
return semanticSlotAtPath(
|
||||
evaluateSemanticExpression(source.node, state, capturePoint),
|
||||
path,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -149,7 +149,50 @@ export const semanticMutationInitializers = (
|
||||
const mutationPosition =
|
||||
directMutationPosition(mutation, parents) ??
|
||||
referenceInitializerPosition(mutation, parents);
|
||||
if (mutationPosition === null) return binding;
|
||||
return selectInitializers(binding, mutationPosition, parents);
|
||||
};
|
||||
|
||||
/** Select a local alias at its read, before any later straight-line reassignment. */
|
||||
export const semanticReadInitializers = (
|
||||
binding: JavaScriptSemanticBindingState,
|
||||
read: t.Node,
|
||||
parents: WeakMap<t.Node, t.Node>,
|
||||
): Pick<
|
||||
JavaScriptSemanticBindingState,
|
||||
"initializers" | "referenceInitializers"
|
||||
> => {
|
||||
let position: StatementPosition | null = null;
|
||||
let current = read;
|
||||
for (
|
||||
let parent = parents.get(current);
|
||||
parent;
|
||||
parent = parents.get(current)
|
||||
) {
|
||||
if (t.isLoop(parent)) return binding;
|
||||
if (t.isFunction(parent)) break;
|
||||
if (position === null) {
|
||||
if (t.isVariableDeclarator(parent)) {
|
||||
const declaration = parents.get(parent);
|
||||
if (t.isVariableDeclaration(declaration))
|
||||
position = statementPosition(declaration, parents, parent);
|
||||
} else if (t.isStatement(parent))
|
||||
position = statementPosition(parent, parents);
|
||||
}
|
||||
current = parent;
|
||||
}
|
||||
return selectInitializers(binding, position, parents, true);
|
||||
};
|
||||
|
||||
const selectInitializers = (
|
||||
binding: JavaScriptSemanticBindingState,
|
||||
position: StatementPosition | null,
|
||||
parents: WeakMap<t.Node, t.Node>,
|
||||
atRead = false,
|
||||
): Pick<
|
||||
JavaScriptSemanticBindingState,
|
||||
"initializers" | "referenceInitializers"
|
||||
> => {
|
||||
if (position === null) return binding;
|
||||
const origins = [
|
||||
...binding.initializers.map((initializer) => ({
|
||||
initializer,
|
||||
@@ -169,12 +212,20 @@ export const semanticMutationInitializers = (
|
||||
];
|
||||
if (
|
||||
origins.some(
|
||||
({ position }) =>
|
||||
position === null || !positionPrecedes(position, mutationPosition),
|
||||
({ position: origin }) =>
|
||||
origin === null ||
|
||||
origin.body !== position.body ||
|
||||
(!atRead && !positionPrecedes(origin, position)),
|
||||
)
|
||||
)
|
||||
return binding;
|
||||
const latest = origins.reduce<StatementPosition | null>(
|
||||
const preceding = atRead
|
||||
? origins.filter(
|
||||
({ position: origin }) =>
|
||||
origin !== null && positionPrecedes(origin, position),
|
||||
)
|
||||
: origins;
|
||||
const latest = preceding.reduce<StatementPosition | null>(
|
||||
(latest, { position }) =>
|
||||
position !== null &&
|
||||
(latest === null || positionPrecedes(latest, position))
|
||||
@@ -183,7 +234,7 @@ export const semanticMutationInitializers = (
|
||||
null,
|
||||
);
|
||||
const retained = new Set(
|
||||
origins
|
||||
preceding
|
||||
.filter(
|
||||
({ position }) =>
|
||||
position?.index === latest?.index &&
|
||||
|
||||
@@ -4,6 +4,8 @@ import {
|
||||
type JavaScriptSemanticAnalysisState,
|
||||
} from "./javascriptSemanticState.js";
|
||||
import { semanticCallableResultExpressions } from "./javascriptSemanticCallableResults.js";
|
||||
import { semanticReadInitializers } from "./javascriptSemanticMutationInitializers.js";
|
||||
import { semanticReferenceSourcesInactive } from "./javascriptSemanticReferenceSources.js";
|
||||
import {
|
||||
semanticStaticPropertyKey,
|
||||
unwrapJavaScriptExpression,
|
||||
@@ -17,6 +19,7 @@ import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
|
||||
export interface JavaScriptSemanticObjectReference {
|
||||
readonly node: t.Node;
|
||||
readonly path: PropertyPath;
|
||||
readonly escapes?: true;
|
||||
/** The allocation owned by this binding, rather than a read of its prior value. */
|
||||
readonly ownerInitializer?: t.Node;
|
||||
}
|
||||
@@ -73,6 +76,7 @@ const instanceFieldReferences = (
|
||||
propertyName: string | null,
|
||||
path: PropertyPath,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
initializedBefore?: t.ClassProperty,
|
||||
): {
|
||||
readonly references: readonly JavaScriptSemanticObjectReference[];
|
||||
readonly definiteNames: ReadonlySet<string>;
|
||||
@@ -94,7 +98,15 @@ const instanceFieldReferences = (
|
||||
if (previous !== undefined && previous.size === excluded.size) continue;
|
||||
selections.set(current.klass, excluded);
|
||||
const shadowed = new Set(excluded);
|
||||
for (const member of [...current.klass.body.body].reverse()) {
|
||||
const before =
|
||||
initializedBefore === undefined
|
||||
? -1
|
||||
: current.klass.body.body.indexOf(initializedBefore);
|
||||
const members =
|
||||
before < 0
|
||||
? current.klass.body.body
|
||||
: current.klass.body.body.slice(0, before);
|
||||
for (const member of [...members].reverse()) {
|
||||
if (!t.isClassProperty(member) || member.static) continue;
|
||||
const key = semanticStaticPropertyKey(member.key, member.computed);
|
||||
if (key !== null && shadowed.has(key)) continue;
|
||||
@@ -123,12 +135,15 @@ interface ClassPropertyLookup {
|
||||
readonly owner: t.ClassDeclaration | t.ClassExpression;
|
||||
readonly receiver?: t.ClassDeclaration | t.ClassExpression;
|
||||
readonly kind: "instance" | "static" | "prototype";
|
||||
readonly fieldsOnly?: true;
|
||||
readonly initializedBefore?: t.ClassProperty;
|
||||
}
|
||||
|
||||
const classPropertyDescriptors = (
|
||||
klass: t.ClassDeclaration | t.ClassExpression,
|
||||
propertyName: string | null,
|
||||
kind: ClassPropertyLookup["kind"],
|
||||
initializedBefore?: t.ClassProperty,
|
||||
) => {
|
||||
const isStatic = kind === "static";
|
||||
const selectedGetters: t.ClassMethod[] = [];
|
||||
@@ -138,7 +153,12 @@ const classPropertyDescriptors = (
|
||||
readonly index: number;
|
||||
readonly key: string | null;
|
||||
}[] = [];
|
||||
const before =
|
||||
initializedBefore === undefined
|
||||
? -1
|
||||
: klass.body.body.indexOf(initializedBefore);
|
||||
klass.body.body.forEach((member, index) => {
|
||||
if (before >= 0 && index >= before && t.isClassProperty(member)) return;
|
||||
if (
|
||||
(t.isClassMethod(member) ||
|
||||
(kind === "static" && t.isClassProperty(member))) &&
|
||||
@@ -223,7 +243,13 @@ export const semanticClassPropertyReferences = (
|
||||
// constructors. They take precedence over every prototype descriptor.
|
||||
let instanceFields: ReadonlySet<string> = new Set();
|
||||
if (lookup.kind === "instance") {
|
||||
const fields = instanceFieldReferences(root, propertyName, path, state);
|
||||
const fields = instanceFieldReferences(
|
||||
root,
|
||||
propertyName,
|
||||
path,
|
||||
state,
|
||||
lookup.initializedBefore,
|
||||
);
|
||||
possibleResults.push(...fields.references);
|
||||
instanceFields = fields.definiteNames;
|
||||
if (propertyName !== null && instanceFields.has(propertyName))
|
||||
@@ -237,11 +263,12 @@ export const semanticClassPropertyReferences = (
|
||||
klass,
|
||||
propertyName,
|
||||
lookup.kind,
|
||||
lookup.initializedBefore,
|
||||
);
|
||||
for (const field of descriptors.fields)
|
||||
if (field.value != null)
|
||||
possibleResults.push({ node: field.value, path });
|
||||
for (const getter of descriptors.getters) {
|
||||
for (const getter of lookup.fieldsOnly ? [] : descriptors.getters) {
|
||||
const key = semanticStaticPropertyKey(getter.key, getter.computed);
|
||||
if (key !== null && instanceFields.has(key)) continue;
|
||||
possibleResults.push(
|
||||
@@ -345,6 +372,102 @@ interface GetterInvocation {
|
||||
| t.ClassExpression;
|
||||
}
|
||||
|
||||
/** Follow only data fields whose initializers retain a class receiver. */
|
||||
const privateReceiverProjection = (
|
||||
receiver: t.ClassDeclaration | t.ClassExpression,
|
||||
owner: t.ClassDeclaration | t.ClassExpression,
|
||||
path: PropertyPath,
|
||||
isStatic: boolean,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): boolean => {
|
||||
if (path.some((key) => key === null || typeof key === "object")) return false;
|
||||
type Selection = { readonly key: string; readonly before?: t.ClassProperty };
|
||||
const pending: {
|
||||
receiver: typeof receiver;
|
||||
path: readonly Selection[];
|
||||
visits: ReadonlyMap<string, number>;
|
||||
}[] = [
|
||||
{
|
||||
receiver,
|
||||
path: path.map((key) => ({ key: String(key) })),
|
||||
visits: new Map(),
|
||||
},
|
||||
];
|
||||
const seen = new Set<string>();
|
||||
const declaringClass = (field: t.ClassProperty) => {
|
||||
const body = state.parentsByNode.get(field);
|
||||
return body === undefined ? undefined : state.parentsByNode.get(body);
|
||||
};
|
||||
for (let cursor = 0; cursor < pending.length; cursor++) {
|
||||
const current = pending[cursor];
|
||||
if (current === undefined) break;
|
||||
const [selection, ...remaining] = current.path;
|
||||
if (selection === undefined) {
|
||||
if (!isStatic || current.receiver === owner) return true;
|
||||
continue;
|
||||
}
|
||||
const origin = JSON.stringify([
|
||||
current.receiver.start,
|
||||
selection.key,
|
||||
selection.before?.start,
|
||||
]);
|
||||
const previous = current.visits.get(origin);
|
||||
// Repeated fields may consume a finite getter path. An initializer cycle
|
||||
// that preserves or grows it cannot establish another receiver identity.
|
||||
if (previous !== undefined && previous <= current.path.length) continue;
|
||||
const visits = new Map(current.visits).set(origin, current.path.length);
|
||||
const identity = JSON.stringify([
|
||||
current.receiver.start,
|
||||
current.path.map(({ key, before }) => [key, before?.start]),
|
||||
]);
|
||||
if (seen.has(identity)) continue;
|
||||
seen.add(identity);
|
||||
const lookupOwner =
|
||||
selection.before === undefined
|
||||
? current.receiver
|
||||
: declaringClass(selection.before);
|
||||
if (!t.isClass(lookupOwner)) continue;
|
||||
for (const reference of semanticClassPropertyReferences(
|
||||
{
|
||||
owner: lookupOwner,
|
||||
receiver: current.receiver,
|
||||
kind: isStatic ? "static" : "instance",
|
||||
fieldsOnly: true,
|
||||
...(selection.before === undefined
|
||||
? {}
|
||||
: { initializedBefore: selection.before }),
|
||||
},
|
||||
[selection.key],
|
||||
state,
|
||||
)) {
|
||||
const field = state.parentsByNode.get(reference.node);
|
||||
if (!t.isClassProperty(field)) continue;
|
||||
const fieldOwner = declaringClass(field);
|
||||
if (!t.isClass(fieldOwner)) continue;
|
||||
const projection: Selection[] = [];
|
||||
let value = unwrapJavaScriptExpression(reference.node).node;
|
||||
while (
|
||||
t.isMemberExpression(value) ||
|
||||
t.isOptionalMemberExpression(value)
|
||||
) {
|
||||
const key = semanticStaticPropertyKey(value.property, value.computed);
|
||||
if (key === null || t.isPrivateName(value.property)) break;
|
||||
projection.unshift({ key, before: field });
|
||||
value = unwrapJavaScriptExpression(value.object).node;
|
||||
}
|
||||
if (!t.isThisExpression(value)) continue;
|
||||
// Instance fields retain the actual instance. Static field `this` belongs
|
||||
// to its declaring class, even when a subclass inherits the field.
|
||||
pending.push({
|
||||
receiver: isStatic ? fieldOwner : current.receiver,
|
||||
path: [...projection, ...remaining],
|
||||
visits,
|
||||
});
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const getterResultReferences = (
|
||||
invocation: GetterInvocation,
|
||||
path: PropertyPath,
|
||||
@@ -358,40 +481,206 @@ const getterResultReferences = (
|
||||
// path; recursive getters that preserve or grow that path cannot progress.
|
||||
const active = new Map(activeGetters).set(getter, path.length);
|
||||
const isStatic = t.isClassMethod(getter) && getter.static;
|
||||
const resolve = (
|
||||
node: t.Node,
|
||||
remaining: PropertyPath,
|
||||
): readonly JavaScriptSemanticObjectReference[] => {
|
||||
const expression = unwrapJavaScriptExpression(node).node;
|
||||
if (t.isThisExpression(expression)) {
|
||||
if (t.isClass(receiver)) {
|
||||
return semanticClassPropertyReferences(
|
||||
{ owner: receiver, kind: isStatic ? "static" : "instance" },
|
||||
remaining,
|
||||
state,
|
||||
active,
|
||||
);
|
||||
interface AliasVisit {
|
||||
readonly identity: string;
|
||||
readonly pathLength: number;
|
||||
readonly parent: AliasVisit | undefined;
|
||||
}
|
||||
const pending: {
|
||||
readonly node: t.Node;
|
||||
readonly path: PropertyPath;
|
||||
readonly aliases: AliasVisit | undefined;
|
||||
readonly escapes?: true;
|
||||
readonly privateRead?: {
|
||||
readonly node: t.MemberExpression | t.OptionalMemberExpression;
|
||||
readonly name: string;
|
||||
readonly path: PropertyPath;
|
||||
};
|
||||
}[] = semanticCallableResultExpressions(getter).flatMap(({ node }) =>
|
||||
node === null ? [] : [{ node, path, aliases: undefined }],
|
||||
);
|
||||
const references: JavaScriptSemanticObjectReference[] = [];
|
||||
const expandedAliases = new Map<string, Set<string>>();
|
||||
const getterScope = state.scopeByNode.get(getter)?.scopeId;
|
||||
for (let cursor = 0; cursor < pending.length; cursor++) {
|
||||
const current = pending[cursor];
|
||||
if (current === undefined) break;
|
||||
const { path: remaining } = current;
|
||||
const expression = unwrapJavaScriptExpression(current.node).node;
|
||||
const retain = (reference: JavaScriptSemanticObjectReference): void => {
|
||||
references.push(
|
||||
current.escapes ? { ...reference, escapes: true } : reference,
|
||||
);
|
||||
};
|
||||
const retainPrivateRead = (): void => {
|
||||
if (current.privateRead !== undefined)
|
||||
retain({
|
||||
node: current.privateRead.node,
|
||||
path: current.escapes ? [] : current.privateRead.path,
|
||||
});
|
||||
};
|
||||
if (t.isIdentifier(expression)) {
|
||||
const binding = resolveSemanticBindingState(
|
||||
state,
|
||||
expression,
|
||||
expression.name,
|
||||
);
|
||||
let scope =
|
||||
binding === undefined
|
||||
? undefined
|
||||
: state.scopesById.get(binding.scopeId);
|
||||
while (scope !== undefined && scope.scopeId !== getterScope) {
|
||||
if (scope.kind === "function" || scope.parentScopeId === null) {
|
||||
scope = undefined;
|
||||
break;
|
||||
}
|
||||
scope = state.scopesById.get(scope.parentScopeId);
|
||||
}
|
||||
return objectReceiverReference(receiver, remaining, state);
|
||||
if (
|
||||
binding !== undefined &&
|
||||
scope !== undefined &&
|
||||
binding.referenceInitializers.every(
|
||||
(initializer) => initializer.copyKind === undefined,
|
||||
)
|
||||
) {
|
||||
const identity = JSON.stringify([
|
||||
binding.bindingId,
|
||||
expression.start,
|
||||
expression.end,
|
||||
]);
|
||||
const known = expandedAliases.get(identity);
|
||||
const key = JSON.stringify([
|
||||
remaining,
|
||||
current.privateRead?.name,
|
||||
current.privateRead?.path,
|
||||
current.escapes,
|
||||
]);
|
||||
let cyclic = false;
|
||||
let growing = false;
|
||||
if (known !== undefined)
|
||||
for (
|
||||
let visit = current.aliases;
|
||||
visit !== undefined;
|
||||
visit = visit.parent
|
||||
)
|
||||
if (visit.identity === identity) {
|
||||
cyclic = true;
|
||||
growing = remaining.length > visit.pathLength;
|
||||
break;
|
||||
}
|
||||
if (cyclic) {
|
||||
// A repeated alias can select arbitrarily deep children. Revisit its
|
||||
// roots as escapes rather than enumerating iterations or dropping them.
|
||||
if (growing && !current.escapes)
|
||||
pending.push({
|
||||
...current,
|
||||
path: [],
|
||||
aliases: undefined,
|
||||
escapes: true,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (known?.has(key)) continue;
|
||||
const selections = known ?? new Set<string>();
|
||||
selections.add(key);
|
||||
expandedAliases.set(identity, selections);
|
||||
const aliases = {
|
||||
identity,
|
||||
pathLength: remaining.length,
|
||||
parent: current.aliases,
|
||||
};
|
||||
const origins = semanticReadInitializers(
|
||||
binding,
|
||||
expression,
|
||||
state.parentsByNode,
|
||||
);
|
||||
for (const initializer of [
|
||||
...origins.initializers,
|
||||
...origins.referenceInitializers.filter(
|
||||
(initializer) =>
|
||||
!semanticReferenceSourcesInactive(initializer, state),
|
||||
),
|
||||
])
|
||||
pending.push({
|
||||
...current,
|
||||
node: initializer.node,
|
||||
path: [...initializer.projection, ...remaining],
|
||||
aliases,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (t.isThisExpression(expression)) {
|
||||
// An outer binding or a nested ordinary function has a different receiver.
|
||||
let ownerNode = state.parentsByNode.get(expression);
|
||||
while (
|
||||
ownerNode !== undefined &&
|
||||
!t.isClass(ownerNode) &&
|
||||
!(t.isFunction(ownerNode) && !t.isArrowFunctionExpression(ownerNode))
|
||||
)
|
||||
ownerNode = state.parentsByNode.get(ownerNode);
|
||||
if (ownerNode !== getter) continue;
|
||||
if (current.privateRead !== undefined && t.isClass(owner)) {
|
||||
if (
|
||||
!t.isClass(receiver) ||
|
||||
!privateReceiverProjection(
|
||||
receiver,
|
||||
owner,
|
||||
remaining,
|
||||
isStatic,
|
||||
state,
|
||||
)
|
||||
) {
|
||||
retainPrivateRead();
|
||||
continue;
|
||||
}
|
||||
for (const member of owner.body.body)
|
||||
if (
|
||||
t.isClassPrivateProperty(member) &&
|
||||
member.static === isStatic &&
|
||||
member.key.id.name === current.privateRead.name &&
|
||||
member.value != null
|
||||
)
|
||||
retain({
|
||||
node: member.value,
|
||||
path: current.escapes ? [] : current.privateRead.path,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
for (const reference of t.isClass(receiver)
|
||||
? semanticClassPropertyReferences(
|
||||
{ owner: receiver, kind: isStatic ? "static" : "instance" },
|
||||
remaining,
|
||||
state,
|
||||
active,
|
||||
)
|
||||
: objectReceiverReference(receiver, remaining, state))
|
||||
retain(reference);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
t.isMemberExpression(expression) ||
|
||||
t.isOptionalMemberExpression(expression)
|
||||
) {
|
||||
if (
|
||||
t.isPrivateName(expression.property) &&
|
||||
t.isThisExpression(expression.object) &&
|
||||
t.isClass(owner)
|
||||
current.privateRead !== undefined &&
|
||||
t.isPrivateName(expression.property)
|
||||
) {
|
||||
const privateName = expression.property.id.name;
|
||||
return owner.body.body.flatMap((member) =>
|
||||
t.isClassPrivateProperty(member) &&
|
||||
member.static === isStatic &&
|
||||
member.key.id.name === privateName &&
|
||||
member.value != null
|
||||
? [{ node: member.value, path: remaining }]
|
||||
: [],
|
||||
);
|
||||
retainPrivateRead();
|
||||
continue;
|
||||
}
|
||||
if (t.isPrivateName(expression.property) && t.isClass(owner)) {
|
||||
pending.push({
|
||||
...current,
|
||||
node: expression.object,
|
||||
path: [],
|
||||
privateRead: {
|
||||
node: expression,
|
||||
name: expression.property.id.name,
|
||||
path: remaining,
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const key = semanticStaticPropertyKey(
|
||||
expression.property,
|
||||
@@ -402,41 +691,52 @@ const getterResultReferences = (
|
||||
t.isClass(owner) &&
|
||||
owner.superClass != null
|
||||
) {
|
||||
return semanticClassCandidates(owner.superClass, state).flatMap(
|
||||
(base) =>
|
||||
semanticClassPropertyReferences(
|
||||
{
|
||||
owner: base,
|
||||
receiver: t.isClass(receiver) ? receiver : owner,
|
||||
kind: isStatic ? "static" : "prototype",
|
||||
},
|
||||
[key, ...remaining],
|
||||
state,
|
||||
active,
|
||||
),
|
||||
);
|
||||
for (const base of semanticClassCandidates(owner.superClass, state))
|
||||
for (const reference of semanticClassPropertyReferences(
|
||||
{
|
||||
owner: base,
|
||||
receiver: t.isClass(receiver) ? receiver : owner,
|
||||
kind: isStatic ? "static" : "prototype",
|
||||
},
|
||||
[key, ...remaining],
|
||||
state,
|
||||
active,
|
||||
))
|
||||
retain(reference);
|
||||
continue;
|
||||
}
|
||||
return resolve(expression.object, [key, ...remaining]);
|
||||
}
|
||||
if (t.isConditionalExpression(expression))
|
||||
return [
|
||||
...resolve(expression.consequent, remaining),
|
||||
...resolve(expression.alternate, remaining),
|
||||
];
|
||||
if (t.isLogicalExpression(expression))
|
||||
return [
|
||||
...resolve(expression.left, remaining),
|
||||
...resolve(expression.right, remaining),
|
||||
];
|
||||
if (t.isSequenceExpression(expression)) {
|
||||
pending.push({
|
||||
...current,
|
||||
node: expression.object,
|
||||
path: [key, ...remaining],
|
||||
});
|
||||
} else if (t.isConditionalExpression(expression))
|
||||
pending.push(
|
||||
{ ...current, node: expression.consequent },
|
||||
{ ...current, node: expression.alternate },
|
||||
);
|
||||
else if (t.isLogicalExpression(expression))
|
||||
pending.push(
|
||||
{ ...current, node: expression.left },
|
||||
{ ...current, node: expression.right },
|
||||
);
|
||||
else if (t.isSequenceExpression(expression)) {
|
||||
const last = expression.expressions.at(-1);
|
||||
return last === undefined ? [] : resolve(last, remaining);
|
||||
}
|
||||
return [{ node: expression, path: remaining }];
|
||||
};
|
||||
return semanticCallableResultExpressions(getter).flatMap(({ node }) =>
|
||||
node === null ? [] : resolve(node, path),
|
||||
);
|
||||
if (last !== undefined) pending.push({ ...current, node: last });
|
||||
} else if (t.isAssignmentExpression(expression, { operator: "=" }))
|
||||
pending.push({ ...current, node: expression.right });
|
||||
else if (
|
||||
t.isAssignmentExpression(expression) &&
|
||||
["&&=", "||=", "??="].includes(expression.operator)
|
||||
)
|
||||
pending.push(
|
||||
{ ...current, node: expression.left },
|
||||
{ ...current, node: expression.right },
|
||||
);
|
||||
else if (current.privateRead !== undefined) retainPrivateRead();
|
||||
else retain({ node: expression, path: remaining });
|
||||
}
|
||||
return references;
|
||||
};
|
||||
|
||||
export const semanticObjectPropertyReferences = (
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import type * as t from "@babel/types";
|
||||
import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js";
|
||||
import type { JavaScriptSemanticPropertyPath as PropertyPath } from "./javascriptSemanticPropertyPaths.js";
|
||||
import { evaluateSemanticExpression } from "./javascriptSemanticValues.js";
|
||||
import { semanticSlotAtPath } from "./javascriptSemanticSlots.js";
|
||||
|
||||
interface ReferenceSource {
|
||||
readonly node: t.Node;
|
||||
readonly projection: PropertyPath;
|
||||
}
|
||||
|
||||
/** Exclude references whose source slot or destructuring fallback is absent. */
|
||||
export const semanticReferenceSourcesInactive = (
|
||||
reference: {
|
||||
readonly requiredSources?: readonly ReferenceSource[];
|
||||
readonly fallbackSources?: readonly ReferenceSource[];
|
||||
},
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): boolean =>
|
||||
reference.requiredSources?.some(
|
||||
(source) =>
|
||||
semanticSelectedReferenceSlot(source, state, source.node)?.presence ===
|
||||
"absent",
|
||||
) === true ||
|
||||
reference.fallbackSources?.some((source) => {
|
||||
// Selection precedes effects through the chosen reference. A later escape
|
||||
// cannot make a fallback that was excluded at destructuring reachable.
|
||||
const slot = semanticSelectedReferenceSlot(source, state, source.node);
|
||||
return (
|
||||
slot?.presence === "present" &&
|
||||
(slot.value.status === "literal" ||
|
||||
slot.value.status === "union" ||
|
||||
slot.value.status === "object" ||
|
||||
slot.value.status === "array")
|
||||
);
|
||||
}) === true;
|
||||
|
||||
/** Select a retained source slot at its capture, before later effects. */
|
||||
export const semanticSelectedReferenceSlot = (
|
||||
source: ReferenceSource,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
capturePoint?: t.Node,
|
||||
) => {
|
||||
const path: string[] = [];
|
||||
for (const key of source.projection) {
|
||||
if (key === null || typeof key === "object") return undefined;
|
||||
path.push(String(key));
|
||||
}
|
||||
return semanticSlotAtPath(
|
||||
evaluateSemanticExpression(source.node, state, capturePoint),
|
||||
path,
|
||||
);
|
||||
};
|
||||
@@ -203,10 +203,18 @@ const managedMethodSchema = z.object({
|
||||
body: managedMethodBodySchema,
|
||||
});
|
||||
|
||||
// Declaring types are namespace-qualified like TypeDef `full_name`, nested
|
||||
// types keep bare names, and a generic instantiation names its generic type.
|
||||
// Evidence recorded before declaring types were named reads them as null.
|
||||
const declaringTypeNameSchema = z.string().nullable().default(null);
|
||||
|
||||
const managedMemberReferenceSchema = z.object({
|
||||
token: tokenSchema,
|
||||
row_offset: offsetSchema,
|
||||
parent_token: tokenSchema.nullable(),
|
||||
parent_name: declaringTypeNameSchema.describe(
|
||||
"Name of a TypeDef, TypeRef, or generic-instantiation TypeSpec parent; null for other parents or when unknown",
|
||||
),
|
||||
name: z.string(),
|
||||
signature: managedSignatureSchema,
|
||||
});
|
||||
@@ -281,7 +289,13 @@ const managedCallEdgeSchema = z.object({
|
||||
opcode: z.string(),
|
||||
target_token: tokenSchema,
|
||||
target_kind: z.enum(["method-def", "member-ref", "method-spec", "unknown"]),
|
||||
target_name: z.string().nullable(),
|
||||
target_name: z
|
||||
.string()
|
||||
.nullable()
|
||||
.describe("Method name; a method-spec target reports its generic method"),
|
||||
target_declaring_type: declaringTypeNameSchema.describe(
|
||||
"Declaring type of the called method; null when unknown",
|
||||
),
|
||||
});
|
||||
|
||||
const managedFieldAccessSchema = z.object({
|
||||
@@ -290,6 +304,9 @@ const managedFieldAccessSchema = z.object({
|
||||
opcode: z.string(),
|
||||
field_token: tokenSchema,
|
||||
field_name: z.string().nullable(),
|
||||
field_declaring_type: declaringTypeNameSchema.describe(
|
||||
"Declaring type of the accessed field; null when unknown",
|
||||
),
|
||||
});
|
||||
|
||||
const managedParseIssueSchema = z.object({
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { z } from "zod";
|
||||
import { nativeMetadataRecoverySummarySchema } from "./nativeMetadataRecovery.js";
|
||||
import { jsonValueSchema } from "../jsonValue.js";
|
||||
import { binaryArchitectureSchema } from "../rawBinary.js";
|
||||
|
||||
const integer = z.number().int().min(0).max(Number.MAX_SAFE_INTEGER);
|
||||
const digest = z.string().regex(/^[a-f0-9]{64}$/u);
|
||||
@@ -112,7 +113,7 @@ export const nativeLoadImageSchema = z.union([
|
||||
status: z.enum(["verified", "mismatch"]),
|
||||
format: z.literal("raw-binary"),
|
||||
target_sha256: digest,
|
||||
architecture: z.enum(["x86", "x86_64", "arm", "arm64"]),
|
||||
architecture: binaryArchitectureSchema,
|
||||
language_id: z.string().min(1),
|
||||
compiler_spec_id: z.string().min(1),
|
||||
base_address: address,
|
||||
|
||||
@@ -39,6 +39,44 @@ describe("raw-binary entry addresses", () => {
|
||||
});
|
||||
|
||||
describe("raw-binary admission", () => {
|
||||
it("admits byte-aligned 6502 images up to the 16-bit address boundary", () => {
|
||||
for (const [entryPoint, fileBytes] of [
|
||||
["0x8001", 8],
|
||||
["0xffff", 1],
|
||||
["0x0", 65536],
|
||||
] as const) {
|
||||
const result = admitRawBinary({
|
||||
architecture: "6502",
|
||||
entryPoint,
|
||||
fileBytes,
|
||||
});
|
||||
if (!result.ok) throw new Error(result.error.reason);
|
||||
expect(result.value).toEqual({
|
||||
architecture: "6502",
|
||||
entryPoint,
|
||||
entryPointSource: entryPoint,
|
||||
});
|
||||
}
|
||||
});
|
||||
it.each([
|
||||
["0x10000", 1],
|
||||
["0xffff", 2],
|
||||
["0x0", 65537],
|
||||
])(
|
||||
"rejects a 6502 mapping at %s with %i bytes that exceeds 16 bits",
|
||||
(entryPoint, fileBytes) => {
|
||||
const result = admitRawBinary({
|
||||
architecture: "6502",
|
||||
entryPoint,
|
||||
fileBytes,
|
||||
});
|
||||
if (result.ok) throw new Error("expected 16-bit overflow rejection");
|
||||
expect(result.error).toEqual({
|
||||
reason: `raw-binary mapping from ${entryPoint} must fit in the 6502 address width`,
|
||||
constraint: "load_selection_rejected",
|
||||
});
|
||||
},
|
||||
);
|
||||
it("admits a little-endian ARM image mapped at the caller entry", () => {
|
||||
expect(
|
||||
admitRawBinary({
|
||||
|
||||
@@ -13,6 +13,7 @@ export const BINARY_ARCHITECTURES = [
|
||||
"x86_64",
|
||||
"arm",
|
||||
"arm64",
|
||||
"6502",
|
||||
] as const satisfies readonly BinaryArchitecture[];
|
||||
|
||||
/** Provider-neutral CPU family for a caller-supplied raw-binary load. */
|
||||
@@ -58,7 +59,7 @@ export const validateRawBinaryLength = (
|
||||
|
||||
/** Admitted headerless load identity used by target resolution and profiles. */
|
||||
export interface RawBinaryAdmission {
|
||||
readonly architecture: HeaderArchitecture;
|
||||
readonly architecture: HeaderArchitecture | "6502";
|
||||
readonly entryPoint: string;
|
||||
readonly entryPointSource: string;
|
||||
}
|
||||
@@ -74,9 +75,11 @@ export interface RawBinaryAdmissionRejection {
|
||||
}
|
||||
|
||||
const addressWidth = (architecture: BinaryArchitecture): bigint =>
|
||||
architecture === "x86" || architecture === "arm"
|
||||
? 0xffffffffn
|
||||
: 0xffffffffffffffffn;
|
||||
architecture === "6502"
|
||||
? 0xffffn
|
||||
: architecture === "x86" || architecture === "arm"
|
||||
? 0xffffffffn
|
||||
: 0xffffffffffffffffn;
|
||||
|
||||
/** Validate architecture, entry, and that the mapped image fits the CPU width. */
|
||||
export const admitRawBinary = (input: {
|
||||
@@ -88,7 +91,7 @@ export const admitRawBinary = (input: {
|
||||
if (!architecture.success)
|
||||
return err({
|
||||
reason:
|
||||
"raw-binary format requires architecture (x86, x86_64, arm, or arm64)",
|
||||
"raw-binary format requires architecture (x86, x86_64, arm, arm64, or 6502)",
|
||||
constraint: "load_selection_rejected",
|
||||
});
|
||||
if (typeof input.entryPoint !== "string")
|
||||
|
||||
@@ -547,6 +547,63 @@ describe("managed fixture data layout", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed implementation classification", () => {
|
||||
it("classifies crossgen2 ReadyToRun output without ILONLY as modern .NET", () => {
|
||||
// crossgen2 writes IL_LIBRARY | STRONGNAMESIGNED and clears ILONLY.
|
||||
const bytes = buildManagedPeFixture({ readyToRun: true, cliFlags: 0xc });
|
||||
const result = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.classification).toMatchObject({
|
||||
runtime_family: "modern-dotnet",
|
||||
implementation: "cil-and-ready-to-run",
|
||||
});
|
||||
expect(result.classification.evidence).toContainEqual(
|
||||
expect.objectContaining({ code: "ready-to-run-signature-observed" }),
|
||||
);
|
||||
expect(result.classification.evidence).not.toContainEqual(
|
||||
expect.objectContaining({ code: "cli-il-only-clear" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves ReadyToRun runtime identity after metadata capacity is exhausted", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
readyToRun: true,
|
||||
cliFlags: 0xc,
|
||||
references: ["x".repeat(12 * 1024 * 1024)],
|
||||
});
|
||||
const result = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
expect(result.classification).toMatchObject({
|
||||
status: "managed",
|
||||
runtime_family: "modern-dotnet",
|
||||
});
|
||||
expect(result.coverage.issues).toContainEqual(
|
||||
expect.objectContaining({ code: "resource-limit" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps a clear ILONLY flag without ReadyToRun as mixed CLR/native", () => {
|
||||
const bytes = buildManagedPeFixture({ cliFlags: 0 });
|
||||
const result = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.classification).toMatchObject({
|
||||
runtime_family: "mixed-clr-native",
|
||||
implementation: "cpp-cli-mixed",
|
||||
});
|
||||
expect(result.classification.evidence).toContainEqual(
|
||||
expect.objectContaining({ code: "cli-il-only-clear" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed artifact metadata identity", () => {
|
||||
it("accepts CLI metadata GUIDs without RFC UUID version or variant bits", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
|
||||
@@ -158,6 +158,15 @@ interface RuntimeClassificationInput {
|
||||
readonly inventory: ManagedMetadataInventory;
|
||||
}
|
||||
|
||||
const runtimeFamilyFromCliHeader = (
|
||||
cli: ManagedCliHeader | null,
|
||||
): ManagedArtifactInspection["classification"]["runtime_family"] =>
|
||||
cli?.readyToRunSignature === true
|
||||
? "modern-dotnet"
|
||||
: cli !== null && (cli.flags & 1) === 0
|
||||
? "mixed-clr-native"
|
||||
: "unknown";
|
||||
|
||||
const runtimeFamilyFor = (
|
||||
cli: ManagedCliHeader,
|
||||
inventory: ManagedMetadataInventory,
|
||||
@@ -180,7 +189,9 @@ const runtimeFamilyFor = (
|
||||
);
|
||||
let runtimeFamily: ManagedArtifactInspection["classification"]["runtime_family"] =
|
||||
"unknown";
|
||||
if ((cli.flags & 1) === 0) {
|
||||
// crossgen2 clears ILONLY on ReadyToRun output, so only a clear flag
|
||||
// without the ReadyToRun signature indicates mixed CLR/native code.
|
||||
if (runtimeFamilyFromCliHeader(cli) === "mixed-clr-native") {
|
||||
runtimeFamily = "mixed-clr-native";
|
||||
evidence.push({
|
||||
code: "cli-il-only-clear",
|
||||
@@ -239,8 +250,8 @@ const implementationFor = (
|
||||
cli: ManagedCliHeader,
|
||||
metadata: ManagedMetadataLayout,
|
||||
): ManagedArtifactInspection["classification"]["implementation"] => {
|
||||
if ((cli.flags & 1) === 0) return "cpp-cli-mixed";
|
||||
if (cli.readyToRunSignature) return "cil-and-ready-to-run";
|
||||
if ((cli.flags & 1) === 0) return "cpp-cli-mixed";
|
||||
return (metadata.rowCounts[6] ?? 0) === 0 ? "metadata-only" : "cil";
|
||||
};
|
||||
|
||||
@@ -303,8 +314,7 @@ const partialMetadataResult = ({
|
||||
classification: {
|
||||
status: issue.code === "resource-limit" ? "managed" : "malformed",
|
||||
container: "pe",
|
||||
runtime_family:
|
||||
(layout.cli?.flags ?? 1) & 1 ? "unknown" : "mixed-clr-native",
|
||||
runtime_family: runtimeFamilyFromCliHeader(layout.cli),
|
||||
implementation: "unknown",
|
||||
managed_architecture: "unknown",
|
||||
evidence: [
|
||||
|
||||
@@ -345,6 +345,31 @@ describe("managed exception region prefix boundaries", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("decodes the no. prefix and keeps it with its opcode", () => {
|
||||
// ldarg.0; no. nullcheck; ldfld 0x04000001; pop; ret
|
||||
const il = Buffer.from([2, 0xfe, 0x19, 4, 0x7b, 1, 0, 0, 4, 0x26, 0x2a]);
|
||||
expect(
|
||||
managedBodyWithRegion({ il, tryLength: 10, handlerOffset: 10 }),
|
||||
).toMatchObject({
|
||||
status: "present",
|
||||
issue: null,
|
||||
decoded_instruction_count: 5,
|
||||
opcode_counts: { "ldarg.0": 1, "no.": 1, ldfld: 1, pop: 1, ret: 1 },
|
||||
});
|
||||
expect(
|
||||
managedBodyWithRegion({
|
||||
il,
|
||||
tryOffset: 4,
|
||||
tryLength: 5,
|
||||
handlerOffset: 10,
|
||||
}),
|
||||
).toMatchObject({
|
||||
status: "malformed",
|
||||
issue:
|
||||
"Exception clause try range does not align with CIL instruction boundaries",
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["start", { tryOffset: 5, tryLength: 1, handlerOffset: 6 }],
|
||||
[
|
||||
@@ -510,3 +535,41 @@ describe("managed exception section decoding", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed CIL truncated operands", () => {
|
||||
const tinyBody = (il: readonly number[]) => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
ilBody: Buffer.from([(il.length << 2) | 0x02, ...il]),
|
||||
});
|
||||
const inspection = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
return { body: inspection.methods[0]?.body, coverage: inspection.coverage };
|
||||
};
|
||||
|
||||
it.each([
|
||||
[[0x20], 0, "Instruction ldc.i4 at IL offset 0 leaves method body"],
|
||||
[
|
||||
[0x00, 0x20, 0x01],
|
||||
1,
|
||||
"Instruction ldc.i4 at IL offset 1 leaves method body",
|
||||
],
|
||||
[[0x2b], 0, "Instruction br.s at IL offset 0 leaves method body"],
|
||||
[[0x45, 0x02], 0, "Instruction switch at IL offset 0 leaves method body"],
|
||||
[
|
||||
[0xfe, 0x09, 0x00],
|
||||
0,
|
||||
"Instruction ldarg at IL offset 0 leaves method body",
|
||||
],
|
||||
[[0x00, 0xfe], 1, "Two-byte CIL opcode at IL offset 1 leaves method body"],
|
||||
])("names the instruction and IL offset for IL %j", (il, decoded, issue) => {
|
||||
const { body, coverage } = tinyBody(il);
|
||||
expect(body).toMatchObject({
|
||||
status: "malformed",
|
||||
decoded_instruction_count: decoded,
|
||||
issue,
|
||||
});
|
||||
expect(coverage.state).toBe("partial");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -110,6 +110,8 @@ describe("managed member inspection", () => {
|
||||
expect(result.member_refs).toEqual([
|
||||
expect.objectContaining({
|
||||
token: "0x0a000001",
|
||||
parent_token: "0x01000001",
|
||||
parent_name: "System.Runtime.Versioning.TargetFrameworkAttribute",
|
||||
name: ".ctor",
|
||||
signature: expect.objectContaining({
|
||||
kind: "method",
|
||||
@@ -126,6 +128,8 @@ describe("managed member inspection", () => {
|
||||
target_token: "0x0a000001",
|
||||
target_kind: "member-ref",
|
||||
target_name: ".ctor",
|
||||
target_declaring_type:
|
||||
"System.Runtime.Versioning.TargetFrameworkAttribute",
|
||||
},
|
||||
]);
|
||||
expect(result.field_accesses).toEqual([
|
||||
@@ -135,12 +139,366 @@ describe("managed member inspection", () => {
|
||||
opcode: "ldfld",
|
||||
field_token: "0x04000001",
|
||||
field_name: "counter",
|
||||
field_declaring_type: "Fixture.Program",
|
||||
},
|
||||
]);
|
||||
expect(result.coverage).toMatchObject({ state: "complete", issues: [] });
|
||||
});
|
||||
});
|
||||
|
||||
/** A tiny-header body of token-operand instructions followed by `ret`. */
|
||||
const tokenBody = (
|
||||
instructions: readonly (readonly [opcode: number, token: number])[],
|
||||
): Buffer => {
|
||||
const il = Buffer.concat([
|
||||
...instructions.map(([opcode, token]) => {
|
||||
const instruction = Buffer.alloc(5);
|
||||
instruction.writeUInt8(opcode, 0);
|
||||
instruction.writeUInt32LE(token, 1);
|
||||
return instruction;
|
||||
}),
|
||||
Buffer.from([0x2a]),
|
||||
]);
|
||||
return Buffer.concat([Buffer.from([(il.length << 2) | 2]), il]);
|
||||
};
|
||||
|
||||
const attribute = "System.Runtime.Versioning.TargetFrameworkAttribute";
|
||||
const inspect = (options: Parameters<typeof buildManagedPeFixture>[0]) => {
|
||||
const bytes = buildManagedPeFixture(options);
|
||||
return inspectManagedMembersBytes(bytes, managedPeFixtureTarget(bytes));
|
||||
};
|
||||
const call = 0x28;
|
||||
const ldsfld = 0x7e;
|
||||
const memberRefOnTypeSpec = (signature: Buffer) =>
|
||||
inspect({
|
||||
typeSpecSignatures: [signature],
|
||||
memberRefParentRaw: (1 << 3) | 4,
|
||||
});
|
||||
|
||||
// HASTHIS | GENERIC, one generic parameter, no parameters, void.
|
||||
const genericRef = Buffer.from([0x30, 0x01, 0x00, 0x01]);
|
||||
// GENERIC, one generic parameter, no parameters, void.
|
||||
const genericDef = Buffer.from([0x10, 0x01, 0x00, 0x01]);
|
||||
|
||||
describe("managed edge target declaring types", () => {
|
||||
it("names a generic TypeSpec parent by its generic type and leaves other TypeSpecs unknown", () => {
|
||||
// GENERICINST CLASS TypeRef(1) <string>
|
||||
const generic = memberRefOnTypeSpec(
|
||||
Buffer.from([0x15, 0x12, (1 << 2) | 1, 0x01, 0x0e]),
|
||||
);
|
||||
expect(generic.member_refs[0]?.parent_name).toBe(attribute);
|
||||
expect(generic.call_edges[0]?.target_declaring_type).toBe(attribute);
|
||||
|
||||
// SZARRAY i4
|
||||
const array = memberRefOnTypeSpec(Buffer.from([0x1d, 0x08]));
|
||||
expect(array.member_refs[0]?.parent_name).toBeNull();
|
||||
expect(array.coverage).toEqual({ state: "complete", issues: [] });
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
"names a TypeSpec as its generic type",
|
||||
[0x15, 0x12, (1 << 2) | 2, 0x01, 0x0e],
|
||||
"invalid-row",
|
||||
"metadata.TypeSpec:0x1b000001",
|
||||
],
|
||||
[
|
||||
"has no type arguments",
|
||||
[0x15, 0x12, (1 << 2) | 1],
|
||||
"invalid-blob",
|
||||
"metadata.TypeSpec:0x1b000001",
|
||||
],
|
||||
[
|
||||
"names a missing TypeRef",
|
||||
[0x15, 0x12, (9 << 2) | 1, 0x01, 0x0e],
|
||||
"invalid-row",
|
||||
"metadata.TypeSpec:0x1b000001",
|
||||
],
|
||||
[
|
||||
"names a missing TypeDef",
|
||||
[0x15, 0x12, (9 << 2) | 0, 0x01, 0x0e],
|
||||
"invalid-row",
|
||||
"metadata.TypeSpec:0x1b000001",
|
||||
],
|
||||
[
|
||||
"has a type argument naming a missing TypeRef",
|
||||
[0x15, 0x12, (1 << 2) | 1, 0x01, 0x12, (9 << 2) | 1],
|
||||
"invalid-row",
|
||||
"metadata.TypeSpec:0x1b000001",
|
||||
],
|
||||
])(
|
||||
"reports a generic TypeSpec parent that %s and leaves it unknown",
|
||||
(_case, signature, code, scope) => {
|
||||
const result = memberRefOnTypeSpec(Buffer.from(signature));
|
||||
|
||||
expect(result.member_refs[0]?.parent_name).toBeNull();
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [{ code, scope }],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("reports unsupported TypeSpec and MethodSpec forms instead of treating them as unknown", () => {
|
||||
const typeSpec = memberRefOnTypeSpec(Buffer.from([0xff]));
|
||||
const methodSpec = inspect({
|
||||
memberRefSignature: genericRef,
|
||||
methodSpecMethods: [(1 << 1) | 1],
|
||||
methodSpecInstantiation: Buffer.from([0x0a, 0x01, 0xff]),
|
||||
ilBody: tokenBody([[call, 0x2b00_0001]]),
|
||||
});
|
||||
|
||||
expect(typeSpec.member_refs[0]?.parent_name).toBeNull();
|
||||
expect(typeSpec.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
{
|
||||
code: "unsupported-signature",
|
||||
scope: "metadata.TypeSpec:0x1b000001",
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(methodSpec.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
{
|
||||
code: "unsupported-signature",
|
||||
scope: "metadata.MethodSpec:0x2b000001",
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("names field MemberRefs as fields and leaves mismatched member kinds unnamed", () => {
|
||||
const result = inspect({
|
||||
memberRefName: "Empty",
|
||||
// FIELD string
|
||||
memberRefSignature: Buffer.from([0x06, 0x0e]),
|
||||
ilBody: tokenBody([
|
||||
[ldsfld, 0x0a00_0001],
|
||||
[call, 0x0400_0001],
|
||||
]),
|
||||
});
|
||||
const methodRef = inspect({ ilBody: tokenBody([[ldsfld, 0x0a00_0001]]) });
|
||||
const flaggedField = inspect({
|
||||
memberRefName: "Empty",
|
||||
// GENERIC | FIELD string: FieldSig allows no flags.
|
||||
memberRefSignature: Buffer.from([0x16, 0x0e]),
|
||||
ilBody: tokenBody([[ldsfld, 0x0a00_0001]]),
|
||||
});
|
||||
|
||||
expect(result.field_accesses).toMatchObject([
|
||||
{ field_name: "Empty", field_declaring_type: attribute },
|
||||
]);
|
||||
expect(methodRef.field_accesses).toMatchObject([
|
||||
{ field_name: null, field_declaring_type: null },
|
||||
]);
|
||||
expect(flaggedField.field_accesses).toMatchObject([
|
||||
{ field_name: null, field_declaring_type: null },
|
||||
]);
|
||||
expect(flaggedField.coverage.state).toBe("partial");
|
||||
expect(result.call_edges).toMatchObject([
|
||||
{
|
||||
target_kind: "unknown",
|
||||
target_name: null,
|
||||
target_declaring_type: null,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("reads Evidence recorded before declaring types were named as unknown", () => {
|
||||
const current = inspect({});
|
||||
const parsed = managedMemberInspectionSchema.parse({
|
||||
...current,
|
||||
member_refs: current.member_refs.map(
|
||||
({ parent_name: _omitted, ...legacy }) => legacy,
|
||||
),
|
||||
call_edges: current.call_edges.map(
|
||||
({ target_declaring_type: _omitted, ...legacy }) => legacy,
|
||||
),
|
||||
field_accesses: current.field_accesses.map(
|
||||
({ field_declaring_type: _omitted, ...legacy }) => legacy,
|
||||
),
|
||||
});
|
||||
|
||||
expect(parsed.member_refs[0]?.parent_name).toBeNull();
|
||||
expect(parsed.call_edges[0]?.target_declaring_type).toBeNull();
|
||||
expect(parsed.field_accesses[0]?.field_declaring_type).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed TypeRef resolution scopes", () => {
|
||||
it.each([
|
||||
// AssemblyRef 9 does not exist.
|
||||
["an invalid", (9 << 2) | 2, "is invalid"],
|
||||
// The TypeRef is its own enclosing type.
|
||||
["a cyclic", (1 << 2) | 3, "contains a cycle"],
|
||||
])(
|
||||
"keeps a TypeRef name beside %s resolution scope",
|
||||
(_case, typeRefScopeRaw, detail) => {
|
||||
const result = inspect({ typeRefScopeRaw });
|
||||
|
||||
expect(result.member_refs[0]?.parent_name).toBe(attribute);
|
||||
expect(result.call_edges[0]?.target_declaring_type).toBe(attribute);
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
{
|
||||
code: "invalid-row",
|
||||
scope: "metadata.TypeRef:0x01000001",
|
||||
detail: expect.stringContaining(detail),
|
||||
},
|
||||
],
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("managed MethodSpec call targets", () => {
|
||||
it("names generic method calls after the method they instantiate", () => {
|
||||
const result = inspect({
|
||||
memberRefSignature: genericRef,
|
||||
methodSignature: genericDef,
|
||||
methodSpecMethods: [(1 << 1) | 1, (1 << 1) | 0],
|
||||
ilBody: tokenBody([
|
||||
[call, 0x2b00_0001],
|
||||
[call, 0x2b00_0002],
|
||||
]),
|
||||
});
|
||||
|
||||
expect(
|
||||
result.call_edges.map((edge) => [
|
||||
edge.target_kind,
|
||||
edge.target_name,
|
||||
edge.target_declaring_type,
|
||||
]),
|
||||
).toEqual([
|
||||
["method-spec", ".ctor", attribute],
|
||||
["method-spec", "Main", "Fixture.Program"],
|
||||
]);
|
||||
expect(result.coverage).toEqual({ state: "complete", issues: [] });
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
"a non-generic method",
|
||||
{},
|
||||
Buffer.from([0x0a, 0x01, 0x0e]),
|
||||
"but its method declares 0 generic parameter(s)",
|
||||
],
|
||||
[
|
||||
"the wrong number of type arguments",
|
||||
{ memberRefSignature: genericRef },
|
||||
Buffer.from([0x0a, 0x02, 0x0e, 0x08]),
|
||||
"supplies 2 type argument(s), but its method declares 1",
|
||||
],
|
||||
[
|
||||
"a field reference",
|
||||
{ memberRefSignature: Buffer.from([0x06, 0x0e]) },
|
||||
Buffer.from([0x0a, 0x01, 0x0e]),
|
||||
"does not have a decodable method signature",
|
||||
],
|
||||
[
|
||||
"a property signature",
|
||||
// PROPERTY | GENERIC, one generic parameter, no parameters, void.
|
||||
{ memberRefSignature: Buffer.from([0x18, 0x01, 0x00, 0x01]) },
|
||||
Buffer.from([0x0a, 0x01, 0x0e]),
|
||||
"does not have a decodable method signature",
|
||||
],
|
||||
[
|
||||
"a truncated signature",
|
||||
// HASTHIS | GENERIC, one generic parameter, no parameters, no return type.
|
||||
{ memberRefSignature: Buffer.from([0x30, 0x01, 0x00]) },
|
||||
Buffer.from([0x0a, 0x01, 0x0e]),
|
||||
"does not have a decodable method signature",
|
||||
],
|
||||
[
|
||||
"a type argument naming a missing TypeRef",
|
||||
{ memberRefSignature: genericRef },
|
||||
Buffer.from([0x0a, 0x01, 0x12, (9 << 2) | 1]),
|
||||
"TypeDefOrRefOrSpec operand 0x25 is invalid",
|
||||
],
|
||||
[
|
||||
"an unmanaged-convention signature",
|
||||
// C | GENERIC: a function pointer convention, not a MethodRefSig one.
|
||||
{ memberRefSignature: Buffer.from([0x11, 0x01, 0x00, 0x01]) },
|
||||
Buffer.from([0x0a, 0x01, 0x0e]),
|
||||
"does not have a decodable method signature",
|
||||
],
|
||||
])(
|
||||
"reports a MethodSpec that instantiates %s",
|
||||
(_case, options, methodSpecInstantiation, detail) => {
|
||||
const result = inspect({
|
||||
...options,
|
||||
methodSpecMethods: [(1 << 1) | 1],
|
||||
methodSpecInstantiation,
|
||||
ilBody: tokenBody([[call, 0x2b00_0001]]),
|
||||
});
|
||||
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
{
|
||||
code: "invalid-row",
|
||||
scope: "metadata.MethodSpec:0x2b000001",
|
||||
detail: expect.stringContaining(detail),
|
||||
},
|
||||
],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("never names a field through a MethodSpec", () => {
|
||||
const result = inspect({
|
||||
memberRefName: "Empty",
|
||||
// FIELD string: a MethodSpec on it is malformed.
|
||||
memberRefSignature: Buffer.from([0x06, 0x0e]),
|
||||
methodSpecMethods: [(1 << 1) | 1],
|
||||
ilBody: tokenBody([
|
||||
[ldsfld, 0x2b00_0001],
|
||||
[call, 0x2b00_0001],
|
||||
]),
|
||||
});
|
||||
|
||||
expect(result.field_accesses).toMatchObject([
|
||||
{ field_name: null, field_declaring_type: null },
|
||||
]);
|
||||
expect(result.call_edges).toMatchObject([
|
||||
{ target_name: null, target_declaring_type: null },
|
||||
]);
|
||||
expect(result.coverage.state).toBe("partial");
|
||||
});
|
||||
|
||||
it("keeps a readable MethodSpec method beside a malformed instantiation", () => {
|
||||
const result = inspect({
|
||||
methodSpecMethods: [(1 << 1) | 1, (9 << 1) | 1],
|
||||
methodSpecInstantiation: Buffer.from([0x0a, 0x00]),
|
||||
ilBody: tokenBody([
|
||||
[call, 0x2b00_0001],
|
||||
[call, 0x2b00_0002],
|
||||
]),
|
||||
});
|
||||
|
||||
expect(
|
||||
result.call_edges.map(({ target_name, target_declaring_type }) => [
|
||||
target_name,
|
||||
target_declaring_type,
|
||||
]),
|
||||
).toEqual([
|
||||
[".ctor", attribute],
|
||||
[null, null],
|
||||
]);
|
||||
expect(result.coverage.state).toBe("partial");
|
||||
expect(
|
||||
result.coverage.issues.map(({ code, scope }) => [code, scope]),
|
||||
).toEqual([
|
||||
["invalid-blob", "metadata.MethodSpec:0x2b000001"],
|
||||
["invalid-row", "metadata.MethodSpec:0x2b000002"],
|
||||
["invalid-blob", "metadata.MethodSpec:0x2b000002"],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed exported types", () => {
|
||||
it("reports type forwarders and nested exported types with their implementation scope", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
@@ -373,15 +731,9 @@ describe("managed derived edges", () => {
|
||||
body: { anchors: Array.from({ length: 40_000 }, () => anchor) },
|
||||
} as unknown as ManagedMethod;
|
||||
|
||||
expect(() =>
|
||||
edges(
|
||||
[method],
|
||||
new Map(),
|
||||
new Map(),
|
||||
new Map(),
|
||||
new ManagedDecodeBudget(),
|
||||
),
|
||||
).toThrow(ManagedReaderFailure);
|
||||
expect(() => edges([method], new Map(), new ManagedDecodeBudget())).toThrow(
|
||||
ManagedReaderFailure,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -413,15 +765,9 @@ describe("managed derived caller names", () => {
|
||||
},
|
||||
} as unknown as ManagedMethod;
|
||||
|
||||
expect(() =>
|
||||
edges(
|
||||
[method],
|
||||
new Map(),
|
||||
new Map(),
|
||||
new Map(),
|
||||
new ManagedDecodeBudget(),
|
||||
),
|
||||
).toThrow(ManagedReaderFailure);
|
||||
expect(() => edges([method], new Map(), new ManagedDecodeBudget())).toThrow(
|
||||
ManagedReaderFailure,
|
||||
);
|
||||
expect(built).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -671,6 +1017,18 @@ it.each([
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["a property header", [0x08, 0x00, 0x08]],
|
||||
["an explicit-this header without has-this", [0x40, 0x00, 0x01]],
|
||||
["a C calling convention header", [0x01, 0x00, 0x01]],
|
||||
["a generic vararg header", [0x15, 0x01, 0x00, 0x01]],
|
||||
])("reports %s as a malformed member signature", (_name, blob) => {
|
||||
expect(signature(Buffer.from(blob))).toMatchObject({
|
||||
kind: "unknown",
|
||||
parse_status: "malformed",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects array ranks beyond the CLR limit before rendering dimensions", () => {
|
||||
expect(
|
||||
signature(
|
||||
|
||||
@@ -27,6 +27,8 @@ import {
|
||||
typeRanges,
|
||||
type TypeRange,
|
||||
} from "./ManagedMemberInspectorCore.js";
|
||||
import { edgeTargets } from "./ManagedMethodSpecs.js";
|
||||
import { typeNameLookup } from "./ManagedTypeNames.js";
|
||||
import {
|
||||
edges,
|
||||
parseExportedTypes,
|
||||
@@ -171,7 +173,11 @@ export const inspectManagedMembersBytes = (
|
||||
const exportedTypes = parseExportedTypes(bytes, layout);
|
||||
issues.push(...exportedTypes.issues);
|
||||
const fields = parseFields(bytes, layout, ranges, selected);
|
||||
const memberRefs = parseMemberRefs(bytes, layout);
|
||||
const memberRefs = parseMemberRefs(
|
||||
bytes,
|
||||
layout,
|
||||
typeNameLookup(bytes, layout, ranges, issues),
|
||||
);
|
||||
issues.push(...memberRefs.issues);
|
||||
const methods = parseMethods({
|
||||
bytes,
|
||||
@@ -181,6 +187,12 @@ export const inspectManagedMembersBytes = (
|
||||
selected,
|
||||
});
|
||||
issues.push(...fields.issues, ...methods.issues);
|
||||
const targets = edgeTargets(bytes, layout, methods.methods, [
|
||||
fields.core,
|
||||
memberRefs.core,
|
||||
methods.core,
|
||||
]);
|
||||
issues.push(...targets.issues);
|
||||
const coverageIssues = uniqueManagedParseIssues(issues);
|
||||
const incompleteMembers =
|
||||
[...fields.fields, ...memberRefs.refs, ...methods.methods].some(
|
||||
@@ -193,9 +205,7 @@ export const inspectManagedMembersBytes = (
|
||||
);
|
||||
const related = edges(
|
||||
methods.methods,
|
||||
methods.core,
|
||||
fields.core,
|
||||
memberRefs.core,
|
||||
targets.members,
|
||||
managedDecodeBudget(layout),
|
||||
);
|
||||
return managedMemberInspectionSchema.parse(
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
ManagedMemberInspection,
|
||||
ManagedParseIssue,
|
||||
} from "../domain/managed/managedArtifact.js";
|
||||
import { managedDecodeBudget } from "./ManagedDecodeBudget.js";
|
||||
import type {
|
||||
ManagedMetadataLayout,
|
||||
MetadataTableLayout,
|
||||
@@ -49,20 +50,22 @@ export interface TypeRange {
|
||||
readonly methodEnd: number;
|
||||
}
|
||||
|
||||
export interface MethodCore {
|
||||
/**
|
||||
* Name and declaring type of a field, method, member reference, or method
|
||||
* specification, keyed by its token so edges resolve any target uniformly.
|
||||
*/
|
||||
export interface MemberCore {
|
||||
readonly token: string;
|
||||
readonly name: string;
|
||||
readonly declaringType: string | null;
|
||||
}
|
||||
|
||||
export interface FieldCore {
|
||||
readonly token: string;
|
||||
readonly name: string;
|
||||
}
|
||||
|
||||
export interface MemberRefCore {
|
||||
readonly token: string;
|
||||
readonly name: string;
|
||||
/** Fields and methods by table; member references by decoded signature. */
|
||||
readonly kind: ManagedSignature["kind"];
|
||||
/**
|
||||
* Generic arity from the owner's decoded signature (see
|
||||
* `signatureGenericArity`); absent when no owner decoded it, as for a
|
||||
* MethodDef outside the type selection.
|
||||
*/
|
||||
readonly genericArity?: number | null;
|
||||
}
|
||||
|
||||
export interface ParsedInstruction {
|
||||
@@ -88,7 +91,7 @@ const rowRange = (
|
||||
return { first: start, last: end - 1, count: end - start };
|
||||
};
|
||||
|
||||
const fullName = (namespace: string, name: string): string =>
|
||||
export const fullName = (namespace: string, name: string): string =>
|
||||
namespace.length === 0 ? name : `${namespace}.${name}`;
|
||||
|
||||
const readTypeRange = (
|
||||
@@ -242,7 +245,7 @@ export const parseTypes = (
|
||||
return { types: items, issues };
|
||||
};
|
||||
|
||||
const readCompressed = (
|
||||
export const readCompressed = (
|
||||
bytes: Buffer,
|
||||
offset: number,
|
||||
): { readonly value: number; readonly next: number } => {
|
||||
@@ -341,13 +344,38 @@ interface MethodSignatureHeader {
|
||||
readonly next: number;
|
||||
}
|
||||
|
||||
// MethodDefSig and MethodRefSig headers (ECMA-335 II.23.2.1-2) without
|
||||
// HASTHIS and EXPLICITTHIS: DEFAULT, VARARG, or GENERIC, which implies DEFAULT.
|
||||
const MEMBER_METHOD_HEADERS = new Set([0x00, 0x05, 0x10]);
|
||||
|
||||
// FNPTR also carries the unmanaged C, STDCALL, THISCALL, FASTCALL, and
|
||||
// UNMANAGED conventions that compilers emit for unmanaged function pointers.
|
||||
const FUNCTION_POINTER_CONVENTIONS = new Set([0, 1, 2, 3, 4, 5, 9]);
|
||||
|
||||
/**
|
||||
* Read a method signature's calling convention and counts. A header that is
|
||||
* not a method signature in its context (a MethodDef or MemberRef row, or an
|
||||
* FNPTR type), sets the reserved bit, or sets EXPLICITTHIS without HASTHIS is
|
||||
* malformed, so no caller consumes its counts. FIELD, LOCAL_SIG, PROPERTY,
|
||||
* and GENERICINST headers are never method signatures.
|
||||
*/
|
||||
const readMethodSignatureHeader = (
|
||||
blob: Buffer,
|
||||
offset: number,
|
||||
context: "member" | "function-pointer",
|
||||
): MethodSignatureHeader => {
|
||||
const first = blob[offset];
|
||||
if (first === undefined)
|
||||
return malformedSignature("truncated method signature", offset);
|
||||
const allowed =
|
||||
context === "member"
|
||||
? MEMBER_METHOD_HEADERS.has(first & 0x9f)
|
||||
: (first & 0x80) === 0 && FUNCTION_POINTER_CONVENTIONS.has(first & 0x0f);
|
||||
if (!allowed || ((first & 0x40) !== 0 && (first & 0x20) === 0))
|
||||
return malformedSignature(
|
||||
`calling convention 0x${first.toString(16)} does not head a ${context} method signature`,
|
||||
offset,
|
||||
);
|
||||
let next = offset + 1;
|
||||
let genericParameterCount: number | null = null;
|
||||
if ((first & 0x10) !== 0) {
|
||||
@@ -366,12 +394,6 @@ const readMethodSignatureHeader = (
|
||||
|
||||
const MAX_ARRAY_RANK = 32;
|
||||
|
||||
// FNPTR carries a method signature: managed DEFAULT/VARARG, or the unmanaged
|
||||
// C, STDCALL, THISCALL, FASTCALL, and UNMANAGED conventions that compilers
|
||||
// emit for unmanaged function pointers. FIELD, LOCAL_SIG, PROPERTY, and
|
||||
// GENERICINST headers are not method signatures.
|
||||
const FUNCTION_POINTER_CONVENTIONS = new Set([0, 1, 2, 3, 4, 5, 9]);
|
||||
|
||||
const arrayShape = (
|
||||
blob: Buffer,
|
||||
offset: number,
|
||||
@@ -449,10 +471,40 @@ const POSITION_ELEMENTS: ReadonlyMap<number, readonly SignaturePosition[]> =
|
||||
[0x10, ["return", "parameter", "field"]],
|
||||
]);
|
||||
|
||||
/** Decode a type operand once; spec callers also validate its metadata coordinate. */
|
||||
const readTypeOperand = (
|
||||
blob: Buffer,
|
||||
offset: number,
|
||||
layout?: ManagedMetadataLayout,
|
||||
tables: readonly number[] = [2, 1, 27],
|
||||
): ReturnType<typeof readCompressed> => {
|
||||
const token = readCompressed(blob, offset);
|
||||
if (layout !== undefined) {
|
||||
const reason =
|
||||
token.value === 0
|
||||
? "type operand is null"
|
||||
: metadataCodedTokenInvalidReason(
|
||||
token.value,
|
||||
2,
|
||||
tables,
|
||||
layout.rowCounts,
|
||||
);
|
||||
if (reason !== null)
|
||||
throw managedFailure(
|
||||
"invalid-row",
|
||||
"signature",
|
||||
`TypeDefOrRefOrSpec operand 0x${token.value.toString(16)} is invalid: ${reason}`,
|
||||
offset,
|
||||
);
|
||||
}
|
||||
return token;
|
||||
};
|
||||
|
||||
const readTypeSignature = (
|
||||
blob: Buffer,
|
||||
offset: number,
|
||||
base: SignaturePosition,
|
||||
layout?: ManagedMetadataLayout,
|
||||
): { readonly value: string; readonly next: number } => {
|
||||
const stack: TypeContinuation[] = [];
|
||||
let cursor = offset;
|
||||
@@ -479,7 +531,7 @@ const readTypeSignature = (
|
||||
stack.push({ kind: "suffix", suffix });
|
||||
cursor += 1;
|
||||
} else if (kind === 0x1f || kind === 0x20) {
|
||||
const token = readCompressed(blob, cursor + 1);
|
||||
const token = readTypeOperand(blob, cursor + 1, layout);
|
||||
const modifier = `${kind === 0x1f ? "modreq" : "modopt"}:${String(token.value)}`;
|
||||
// A modifier frame on top was pushed by the directly preceding byte;
|
||||
// keep an adjacent run together and in source order.
|
||||
@@ -488,7 +540,7 @@ const readTypeSignature = (
|
||||
else stack.push({ kind: "modifiers", modifiers: [modifier] });
|
||||
cursor = token.next;
|
||||
} else if (kind === 0x11 || kind === 0x12) {
|
||||
const token = readCompressed(blob, cursor + 1);
|
||||
const token = readTypeOperand(blob, cursor + 1, layout);
|
||||
value = `${kind === 0x11 ? "valuetype" : "class"}:${String(token.value)}`;
|
||||
cursor = token.next;
|
||||
} else if (kind === 0x1e || kind === 0x13) {
|
||||
@@ -508,7 +560,7 @@ const readTypeSignature = (
|
||||
"generic instantiation does not name a class or value type",
|
||||
cursor + 1,
|
||||
);
|
||||
const token = readCompressed(blob, cursor + 2);
|
||||
const token = readTypeOperand(blob, cursor + 2, layout, [2, 1]);
|
||||
const argumentCount = readCompressed(blob, token.next);
|
||||
if (argumentCount.value === 0)
|
||||
return malformedSignature(
|
||||
@@ -525,16 +577,11 @@ const readTypeSignature = (
|
||||
});
|
||||
cursor = argumentCount.next;
|
||||
} else if (kind === 0x1b) {
|
||||
const header = readMethodSignatureHeader(blob, cursor + 1);
|
||||
if (
|
||||
!FUNCTION_POINTER_CONVENTIONS.has(header.first & 0x0f) ||
|
||||
(header.first & 0x80) !== 0 ||
|
||||
((header.first & 0x40) !== 0 && (header.first & 0x20) === 0)
|
||||
)
|
||||
return malformedSignature(
|
||||
`function pointer calling convention 0x${header.first.toString(16)} does not head a method signature`,
|
||||
cursor + 1,
|
||||
);
|
||||
const header = readMethodSignatureHeader(
|
||||
blob,
|
||||
cursor + 1,
|
||||
"function-pointer",
|
||||
);
|
||||
const convention = callingConvention(header.first);
|
||||
const head =
|
||||
header.genericParameterCount === null
|
||||
@@ -602,22 +649,81 @@ const callingConvention = (value: number): string => {
|
||||
return flags.length === 0 ? name : `${name} ${flags.join(" ")}`;
|
||||
};
|
||||
|
||||
export const signature = (
|
||||
/**
|
||||
* Decode a whole TypeSpec blob, or a MethodSpec instantiation (GENERICINST,
|
||||
* argument count, Types), with the member-signature grammar and return the
|
||||
* rendered types. A malformed or unsupported blob throws invalid-blob or
|
||||
* unsupported-signature under the owning row, so coverage records it.
|
||||
*/
|
||||
export const specSignature = (
|
||||
layout: ManagedMetadataLayout,
|
||||
blob: Buffer,
|
||||
rawSha256?: string,
|
||||
): ManagedSignature => {
|
||||
const raw = {
|
||||
raw_length: blob.length,
|
||||
raw_sha256: rawSha256 ?? sha256Bytes(blob),
|
||||
};
|
||||
owner: {
|
||||
readonly form: "type" | "instantiation";
|
||||
readonly scope: string;
|
||||
readonly offset: number;
|
||||
},
|
||||
): readonly string[] => {
|
||||
const budget = managedDecodeBudget(layout);
|
||||
// Rendering is transient, so charge it as member signatures are charged.
|
||||
budget.admitTransient(blob.length * 256, owner.offset);
|
||||
try {
|
||||
let cursor = 0;
|
||||
let count = 1;
|
||||
if (owner.form === "instantiation") {
|
||||
if (blob[0] !== 0x0a)
|
||||
malformedSignature("MethodSpec instantiation is not GENERICINST", 0);
|
||||
const header = readCompressed(blob, 1);
|
||||
if (header.value === 0)
|
||||
malformedSignature("MethodSpec instantiation has no type arguments", 1);
|
||||
count = header.value;
|
||||
cursor = header.next;
|
||||
}
|
||||
const types: string[] = [];
|
||||
// Each Type consumes at least one byte, so a false count fails on truncation.
|
||||
for (let index = 0; index < count; index += 1) {
|
||||
const type = readTypeSignature(blob, cursor, "type", layout);
|
||||
types.push(type.value);
|
||||
cursor = type.next;
|
||||
}
|
||||
if (cursor !== blob.length)
|
||||
malformedSignature("Trailing signature data", cursor);
|
||||
return types;
|
||||
} catch (cause: unknown) {
|
||||
if (
|
||||
!(cause instanceof ManagedReaderFailure) ||
|
||||
(cause.issue.code !== "invalid-blob" &&
|
||||
cause.issue.code !== "invalid-row" &&
|
||||
cause.issue.code !== "unsupported-signature")
|
||||
)
|
||||
throw cause;
|
||||
throw managedFailure(
|
||||
cause.issue.code,
|
||||
owner.scope,
|
||||
cause.issue.detail,
|
||||
owner.offset,
|
||||
);
|
||||
} finally {
|
||||
budget.releaseTransient();
|
||||
}
|
||||
};
|
||||
|
||||
type DecodedSignature = Omit<ManagedSignature, "raw_length" | "raw_sha256">;
|
||||
|
||||
/**
|
||||
* Decode a field or member method signature. A malformed or unsupported blob
|
||||
* decodes as kind `unknown` with its reason.
|
||||
*/
|
||||
const decodeSignature = (blob: Buffer): DecodedSignature => {
|
||||
try {
|
||||
if (blob.length === 0)
|
||||
throw managedFailure("invalid-blob", "signature", "empty signature", 0);
|
||||
const first = blob[0] ?? 0;
|
||||
if ((first & 0x0f) === 6) {
|
||||
// FieldSig (II.23.2.4) is exactly FIELD; a flagged FIELD header is not a
|
||||
// method header either, so it is malformed below.
|
||||
if (first === 0x06) {
|
||||
const fieldType = readTypeSignature(blob, 1, "field");
|
||||
return {
|
||||
...raw,
|
||||
kind: "field",
|
||||
parse_status: fieldType.next === blob.length ? "decoded" : "partial",
|
||||
calling_convention: callingConvention(first),
|
||||
@@ -630,7 +736,7 @@ export const signature = (
|
||||
fieldType.next === blob.length ? null : "Trailing signature data",
|
||||
};
|
||||
}
|
||||
const header = readMethodSignatureHeader(blob, 0);
|
||||
const header = readMethodSignatureHeader(blob, 0, "member");
|
||||
const returnType = readTypeSignature(blob, header.next, "return");
|
||||
let offset = returnType.next;
|
||||
const parameters: string[] = [];
|
||||
@@ -640,7 +746,6 @@ export const signature = (
|
||||
offset = parameter.next;
|
||||
}
|
||||
return {
|
||||
...raw,
|
||||
kind: "method",
|
||||
parse_status: offset === blob.length ? "decoded" : "partial",
|
||||
calling_convention: callingConvention(first),
|
||||
@@ -659,7 +764,6 @@ export const signature = (
|
||||
)
|
||||
throw cause;
|
||||
return {
|
||||
...raw,
|
||||
kind: "unknown",
|
||||
parse_status:
|
||||
cause.issue.code === "unsupported-signature"
|
||||
@@ -675,3 +779,41 @@ export const signature = (
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Generic parameter count of a method signature that decodes completely, 0
|
||||
* when it is not generic, or null when the blob is not a decodable method
|
||||
* signature. The rendered decode is transient.
|
||||
*/
|
||||
export const methodGenericArity = (
|
||||
layout: ManagedMetadataLayout,
|
||||
blob: Buffer,
|
||||
): number | null => {
|
||||
const budget = managedDecodeBudget(layout);
|
||||
budget.admitTransient(blob.length * 256, layout.blob.offset);
|
||||
try {
|
||||
return signatureGenericArity(decodeSignature(blob));
|
||||
} finally {
|
||||
budget.releaseTransient();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Generic parameter count of a completely decoded method signature, 0 when it
|
||||
* is not generic, or null for any other signature.
|
||||
*/
|
||||
export const signatureGenericArity = (
|
||||
decoded: DecodedSignature,
|
||||
): number | null =>
|
||||
decoded.kind === "method" && decoded.parse_status === "decoded"
|
||||
? (decoded.generic_parameter_count ?? 0)
|
||||
: null;
|
||||
|
||||
export const signature = (
|
||||
blob: Buffer,
|
||||
rawSha256?: string,
|
||||
): ManagedSignature => ({
|
||||
raw_length: blob.length,
|
||||
raw_sha256: rawSha256 ?? sha256Bytes(blob),
|
||||
...decodeSignature(blob),
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@ const PREFIX_OPCODES = new Set([
|
||||
"tail.",
|
||||
"constrained.",
|
||||
"readonly.",
|
||||
"no.",
|
||||
]);
|
||||
|
||||
const OPCODES = new Map<
|
||||
@@ -232,6 +233,7 @@ const OPCODES = new Map<
|
||||
[0xfe16, { name: "constrained.", operand: "type" }],
|
||||
[0xfe17, { name: "cpblk", operand: "none" }],
|
||||
[0xfe18, { name: "initblk", operand: "none" }],
|
||||
[0xfe19, { name: "no.", operand: "short-i" }],
|
||||
[0xfe1a, { name: "rethrow", operand: "none" }],
|
||||
[0xfe1c, { name: "sizeof", operand: "type" }],
|
||||
[0xfe1d, { name: "refanytype", operand: "none" }],
|
||||
@@ -384,6 +386,22 @@ const readOperand = (
|
||||
return { next: offset, anchorKind: "unknown", value: null };
|
||||
};
|
||||
|
||||
/** An operand cut off by the end of the IL body has no decodable value. */
|
||||
const readOperandWithin = (
|
||||
il: Buffer,
|
||||
offset: number,
|
||||
kind: string,
|
||||
): OperandResult | undefined => {
|
||||
try {
|
||||
return readOperand(il, offset, kind);
|
||||
} catch (cause: unknown) {
|
||||
// Buffer reads past the IL body throw RangeError; report it as the
|
||||
// instruction leaving the method body rather than as a Buffer message.
|
||||
if (cause instanceof RangeError) return undefined;
|
||||
throw cause;
|
||||
}
|
||||
};
|
||||
|
||||
export const decodeInstructions = (
|
||||
il: Buffer,
|
||||
admitInstruction?: (offset: number) => void,
|
||||
@@ -404,14 +422,22 @@ export const decodeInstructions = (
|
||||
const start = offset;
|
||||
const first = il.readUInt8(offset);
|
||||
offset += 1;
|
||||
if (first === 0xfe && offset >= il.length) {
|
||||
issue = `Two-byte CIL opcode at IL offset ${String(start)} leaves method body`;
|
||||
break;
|
||||
}
|
||||
const code = first === 0xfe ? 0xfe00 + il.readUInt8(offset++) : first;
|
||||
const descriptor = OPCODES.get(code);
|
||||
if (descriptor === undefined) {
|
||||
issue = `Unsupported CIL opcode 0x${code.toString(16)} at IL offset ${String(start)}`;
|
||||
break;
|
||||
}
|
||||
const operand = readOperand(il, offset, descriptor.operand);
|
||||
if (operand.next < offset || operand.next > il.length) {
|
||||
const operand = readOperandWithin(il, offset, descriptor.operand);
|
||||
if (
|
||||
operand === undefined ||
|
||||
operand.next < offset ||
|
||||
operand.next > il.length
|
||||
) {
|
||||
issue = `Instruction ${descriptor.name} at IL offset ${String(start)} leaves method body`;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -9,15 +9,14 @@ import type { ManagedParseIssue } from "../domain/managed/managedArtifact.js";
|
||||
import type { ManagedMetadataLayout } from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
signature,
|
||||
type FieldCore,
|
||||
signatureGenericArity,
|
||||
type ManagedCallEdge,
|
||||
type ManagedExportedType,
|
||||
type ManagedField,
|
||||
type ManagedFieldAccess,
|
||||
type ManagedMemberRef,
|
||||
type ManagedMethod,
|
||||
type MemberRefCore,
|
||||
type MethodCore,
|
||||
type MemberCore,
|
||||
type TypeRange,
|
||||
} from "./ManagedMemberInspectorCore.js";
|
||||
import {
|
||||
@@ -51,11 +50,11 @@ export const parseFields = (
|
||||
selected: SelectedTypeTokens = null,
|
||||
): {
|
||||
readonly fields: readonly ManagedField[];
|
||||
readonly core: ReadonlyMap<string, FieldCore>;
|
||||
readonly core: ReadonlyMap<string, MemberCore>;
|
||||
readonly issues: readonly ManagedParseIssue[];
|
||||
} => {
|
||||
const fields: ManagedField[] = [];
|
||||
const core = new Map<string, FieldCore>();
|
||||
const core = new Map<string, MemberCore>();
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
const table = layout.table(4);
|
||||
const ownerRow = readManagedValue(
|
||||
@@ -78,7 +77,12 @@ export const parseFields = (
|
||||
: (readManagedValue(() => ownerRow(row), issues) ?? 0);
|
||||
const declared = ranges[owner - 1];
|
||||
const token = metadataToken(4, row);
|
||||
core.set(token, { token, name });
|
||||
core.set(token, {
|
||||
token,
|
||||
name,
|
||||
declaringType: declared?.fullName ?? null,
|
||||
kind: "field",
|
||||
});
|
||||
if (!isSelected(selected, declared)) return;
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
@@ -174,13 +178,14 @@ export const parseExportedTypes = (
|
||||
export const parseMemberRefs = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
typeName: (token: string | null) => string | null,
|
||||
): {
|
||||
readonly refs: readonly ManagedMemberRef[];
|
||||
readonly core: ReadonlyMap<string, MemberRefCore>;
|
||||
readonly core: ReadonlyMap<string, MemberCore>;
|
||||
readonly issues: readonly ManagedParseIssue[];
|
||||
} => {
|
||||
const refs: ManagedMemberRef[] = [];
|
||||
const core = new Map<string, MemberRefCore>();
|
||||
const core = new Map<string, MemberCore>();
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
const table = layout.table(10);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
@@ -218,19 +223,29 @@ export const parseMemberRefs = (
|
||||
? "MemberRef parent coded index 0x0 is null, but the Class column must reference a row"
|
||||
: `MemberRef parent coded index 0x${parentRaw.toString(16)} is invalid: ${parentReason}`,
|
||||
});
|
||||
const parentToken = metadataCodedToken(
|
||||
parentRaw,
|
||||
3,
|
||||
[2, 1, 26, 6, 27],
|
||||
layout.rowCounts,
|
||||
);
|
||||
const parentName = typeName(parentToken);
|
||||
const decoded = budgetedSignature(sig, layout);
|
||||
refs.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
parent_token: metadataCodedToken(
|
||||
parentRaw,
|
||||
3,
|
||||
[2, 1, 26, 6, 27],
|
||||
layout.rowCounts,
|
||||
),
|
||||
parent_token: parentToken,
|
||||
parent_name: parentName,
|
||||
name,
|
||||
signature: budgetedSignature(sig, layout),
|
||||
signature: decoded,
|
||||
});
|
||||
core.set(token, {
|
||||
token,
|
||||
name,
|
||||
declaringType: parentName,
|
||||
kind: decoded.kind,
|
||||
genericArity: signatureGenericArity(decoded),
|
||||
});
|
||||
core.set(token, { token, name });
|
||||
}, issues);
|
||||
}
|
||||
return { refs, core, issues };
|
||||
@@ -252,11 +267,11 @@ export const parseMethods = ({
|
||||
selected = null,
|
||||
}: ParseMethodsInput): {
|
||||
readonly methods: readonly ManagedMethod[];
|
||||
readonly core: ReadonlyMap<string, MethodCore>;
|
||||
readonly core: ReadonlyMap<string, MemberCore>;
|
||||
readonly issues: readonly ManagedParseIssue[];
|
||||
} => {
|
||||
const methods: ManagedMethod[] = [];
|
||||
const core = new Map<string, MethodCore>();
|
||||
const core = new Map<string, MemberCore>();
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
const table = layout.table(6);
|
||||
const ownerRow = readManagedValue(
|
||||
@@ -281,11 +296,13 @@ export const parseMethods = ({
|
||||
: (readManagedValue(() => ownerRow(row), issues) ?? 0);
|
||||
const declared = ranges[owner - 1];
|
||||
const token = metadataToken(6, row);
|
||||
core.set(token, {
|
||||
const member = {
|
||||
token,
|
||||
name,
|
||||
declaringType: declared?.fullName ?? null,
|
||||
});
|
||||
kind: "method",
|
||||
} as const;
|
||||
core.set(token, member);
|
||||
if (!isSelected(selected, declared)) return;
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
@@ -294,6 +311,11 @@ export const parseMethods = ({
|
||||
layout.blob.size,
|
||||
);
|
||||
cursor.readIndex(layout.tableIndexSize(8));
|
||||
const decoded = budgetedSignature(sig, layout);
|
||||
core.set(token, {
|
||||
...member,
|
||||
genericArity: signatureGenericArity(decoded),
|
||||
});
|
||||
methods.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
@@ -303,7 +325,7 @@ export const parseMethods = ({
|
||||
rva,
|
||||
impl_flags: implFlags,
|
||||
flags,
|
||||
signature: budgetedSignature(sig, layout),
|
||||
signature: decoded,
|
||||
body: methodBody(
|
||||
bytes,
|
||||
pe,
|
||||
@@ -317,6 +339,15 @@ export const parseMethods = ({
|
||||
return { methods, core, issues };
|
||||
};
|
||||
|
||||
/**
|
||||
* Member kinds each operand kind may name. A MemberRef whose signature does
|
||||
* not decode keeps its name on calls, as before, but is never named a field.
|
||||
*/
|
||||
const NAMEABLE_KINDS = new Map<string, ReadonlySet<MemberCore["kind"]>>([
|
||||
["method", new Set(["method", "unknown"])],
|
||||
["field", new Set(["field"])],
|
||||
]);
|
||||
|
||||
const targetKind = (token: string): ManagedCallEdge["target_kind"] =>
|
||||
token.startsWith("0x06")
|
||||
? "method-def"
|
||||
@@ -326,11 +357,22 @@ const targetKind = (token: string): ManagedCallEdge["target_kind"] =>
|
||||
? "method-spec"
|
||||
: "unknown";
|
||||
|
||||
/** The member an operand names, if the operand kind can name that member's kind. */
|
||||
const namedTarget = (
|
||||
members: ReadonlyMap<string, MemberCore>,
|
||||
operandKind: string,
|
||||
operand: string,
|
||||
): MemberCore | undefined => {
|
||||
const member = members.get(operand);
|
||||
return member !== undefined &&
|
||||
NAMEABLE_KINDS.get(operandKind)?.has(member.kind) === true
|
||||
? member
|
||||
: undefined;
|
||||
};
|
||||
|
||||
export const edges = (
|
||||
methods: readonly ManagedMethod[],
|
||||
methodCore: ReadonlyMap<string, MethodCore>,
|
||||
fieldCore: ReadonlyMap<string, FieldCore>,
|
||||
refCore: ReadonlyMap<string, MemberRefCore>,
|
||||
members: ReadonlyMap<string, MemberCore>,
|
||||
budget: ManagedDecodeBudget,
|
||||
): {
|
||||
readonly callEdges: readonly ManagedCallEdge[];
|
||||
@@ -350,11 +392,10 @@ export const edges = (
|
||||
const caller = derives ? callerName(method) : method.name;
|
||||
for (const anchor of method.body.anchors) {
|
||||
if (anchor.opcode === "ldtoken") continue;
|
||||
if (anchor.operand_kind === "method" && anchor.operand !== null) {
|
||||
const named =
|
||||
methodCore.get(anchor.operand)?.name ??
|
||||
refCore.get(anchor.operand)?.name ??
|
||||
null;
|
||||
const { operand } = anchor;
|
||||
if (operand === null) continue;
|
||||
const target = namedTarget(members, anchor.operand_kind, operand);
|
||||
if (anchor.operand_kind === "method") {
|
||||
// Each derived record is admitted before the graph grows further.
|
||||
callEdges.push(
|
||||
admitManagedProjection(
|
||||
@@ -362,23 +403,25 @@ export const edges = (
|
||||
caller_token: method.token,
|
||||
caller,
|
||||
opcode: anchor.opcode,
|
||||
target_token: anchor.operand,
|
||||
target_kind: targetKind(anchor.operand),
|
||||
target_name: named,
|
||||
target_token: operand,
|
||||
target_kind: targetKind(operand),
|
||||
target_name: target?.name ?? null,
|
||||
target_declaring_type: target?.declaringType ?? null,
|
||||
},
|
||||
budget,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (anchor.operand_kind === "field" && anchor.operand !== null) {
|
||||
if (anchor.operand_kind === "field") {
|
||||
fieldAccesses.push(
|
||||
admitManagedProjection(
|
||||
{
|
||||
method_token: method.token,
|
||||
method: caller,
|
||||
opcode: anchor.opcode,
|
||||
field_token: anchor.operand,
|
||||
field_name: fieldCore.get(anchor.operand)?.name ?? null,
|
||||
field_token: operand,
|
||||
field_name: target?.name ?? null,
|
||||
field_declaring_type: target?.declaringType ?? null,
|
||||
},
|
||||
budget,
|
||||
),
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
import type { ManagedParseIssue } from "../domain/managed/managedArtifact.js";
|
||||
import {
|
||||
methodGenericArity,
|
||||
specSignature,
|
||||
type ManagedMethod,
|
||||
type MemberCore,
|
||||
} from "./ManagedMemberInspectorCore.js";
|
||||
import type { ManagedMetadataLayout } from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
metadataCodedToken,
|
||||
metadataCodedTokenInvalidReason,
|
||||
metadataRowCursor,
|
||||
readMetadataBlob,
|
||||
} from "./ManagedMetadataHeaps.js";
|
||||
import { managedFailure, readManagedValue } from "./ManagedReaderFailure.js";
|
||||
|
||||
/**
|
||||
* Index fields, member references, and methods by token, then name each
|
||||
* MethodSpec an inspected body calls after the generic method it instantiates,
|
||||
* so every edge target resolves through one lookup.
|
||||
*/
|
||||
export const edgeTargets = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
methods: readonly ManagedMethod[],
|
||||
cores: readonly ReadonlyMap<string, MemberCore>[],
|
||||
): {
|
||||
readonly members: ReadonlyMap<string, MemberCore>;
|
||||
readonly issues: readonly ManagedParseIssue[];
|
||||
} => {
|
||||
const members = new Map(cores.flatMap((core) => [...core]));
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
const seen = new Set<string>();
|
||||
// Generic arity by method token, so instantiations of one method decode it once.
|
||||
const arities = new Map<string, number | null>();
|
||||
for (const method of methods)
|
||||
for (const { operand } of method.body.anchors) {
|
||||
if (operand?.startsWith("0x2b") !== true || seen.has(operand)) continue;
|
||||
seen.add(operand);
|
||||
readManagedValue(
|
||||
() =>
|
||||
readMethodSpec({ bytes, layout, members, issues, arities }, operand),
|
||||
issues,
|
||||
);
|
||||
}
|
||||
return { members, issues };
|
||||
};
|
||||
|
||||
/**
|
||||
* Map one MethodSpec to its generic method, then check its instantiation
|
||||
* decodes and matches that method's generic arity. A readable Method column
|
||||
* keeps its name when the rest is malformed; the failure makes coverage partial.
|
||||
*/
|
||||
const readMethodSpec = (
|
||||
{
|
||||
bytes,
|
||||
layout,
|
||||
members,
|
||||
issues,
|
||||
arities,
|
||||
}: {
|
||||
readonly bytes: Buffer;
|
||||
readonly layout: ManagedMetadataLayout;
|
||||
readonly members: Map<string, MemberCore>;
|
||||
readonly issues: ManagedParseIssue[];
|
||||
readonly arities: Map<string, number | null>;
|
||||
},
|
||||
token: string,
|
||||
): void => {
|
||||
const scope = `metadata.MethodSpec:${token}`;
|
||||
const cursor = metadataRowCursor(
|
||||
bytes,
|
||||
layout,
|
||||
0x2b,
|
||||
Number.parseInt(token.slice(4), 16),
|
||||
);
|
||||
const methodRaw = cursor.readIndex(layout.codedIndexSize("MethodDefOrRef"));
|
||||
const instantiation = cursor.readIndex(layout.blobIndexSize);
|
||||
const methodReason = metadataCodedTokenInvalidReason(
|
||||
methodRaw,
|
||||
1,
|
||||
[6, 10],
|
||||
layout.rowCounts,
|
||||
);
|
||||
if (methodReason !== null || methodRaw === 0)
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope,
|
||||
offset: cursor.start,
|
||||
detail:
|
||||
methodReason === null
|
||||
? "MethodSpec method coded index 0x0 is null, but the Method column must reference a row"
|
||||
: `MethodSpec method coded index 0x${methodRaw.toString(16)} is invalid: ${methodReason}`,
|
||||
});
|
||||
const generic = metadataCodedToken(methodRaw, 1, [6, 10], layout.rowCounts);
|
||||
const target = generic === null ? undefined : members.get(generic);
|
||||
// A MethodSpec instantiates a method, so a field target never lends it a
|
||||
// name, even to a field operand.
|
||||
if (target !== undefined && target.kind !== "field")
|
||||
members.set(token, { ...target, token });
|
||||
const types = specSignature(
|
||||
layout,
|
||||
readMetadataBlob(bytes, layout, instantiation, layout.blob.size),
|
||||
{ form: "instantiation", scope, offset: cursor.start },
|
||||
);
|
||||
if (generic === null) return;
|
||||
// Prefer the arity the target's owner decoded; read the raw signature only
|
||||
// for a target nothing decoded, such as a MethodDef outside the selection.
|
||||
if (target?.genericArity === undefined && !arities.has(generic))
|
||||
arities.set(
|
||||
generic,
|
||||
methodGenericArity(layout, methodSignatureBlob(bytes, layout, generic)),
|
||||
);
|
||||
const arity =
|
||||
target?.genericArity === undefined
|
||||
? (arities.get(generic) ?? null)
|
||||
: target.genericArity;
|
||||
if (arity !== types.length)
|
||||
throw managedFailure(
|
||||
"invalid-row",
|
||||
scope,
|
||||
arity === null
|
||||
? "MethodSpec method does not have a decodable method signature"
|
||||
: `MethodSpec supplies ${String(types.length)} type argument(s), but its method declares ${String(arity)} generic parameter(s)`,
|
||||
cursor.start,
|
||||
);
|
||||
};
|
||||
|
||||
/** The Signature column of a MethodDef or MemberRef row. */
|
||||
const methodSignatureBlob = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
token: string,
|
||||
): Buffer => {
|
||||
const table = Number.parseInt(token.slice(2, 4), 16);
|
||||
const cursor = metadataRowCursor(
|
||||
bytes,
|
||||
layout,
|
||||
table,
|
||||
Number.parseInt(token.slice(4), 16),
|
||||
);
|
||||
if (table === 6) {
|
||||
cursor.readUInt32();
|
||||
cursor.readUInt16();
|
||||
cursor.readUInt16();
|
||||
} else cursor.readIndex(layout.codedIndexSize("MemberRefParent"));
|
||||
cursor.readIndex(layout.stringIndexSize);
|
||||
return readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
};
|
||||
@@ -29,6 +29,16 @@ interface ManagedPeFixtureOptions {
|
||||
readonly moduleRowCount?: number;
|
||||
readonly assemblyRowCount?: number;
|
||||
readonly memberRefParentRaw?: number;
|
||||
readonly memberRefName?: string;
|
||||
readonly memberRefSignature?: Buffer;
|
||||
/** Raw MethodDefOrRef coded values, one MethodSpec row each. */
|
||||
readonly methodSpecMethods?: readonly number[];
|
||||
/** Instantiation blob shared by every MethodSpec row. */
|
||||
readonly methodSpecInstantiation?: Buffer;
|
||||
/** TypeSpec signature blobs, one TypeSpec row each. */
|
||||
readonly typeSpecSignatures?: readonly Buffer[];
|
||||
/** Raw ResolutionScope coded value of the TypeRef row; null by default. */
|
||||
readonly typeRefScopeRaw?: number;
|
||||
readonly mvid?: Buffer;
|
||||
readonly pinvoke?: {
|
||||
readonly importName?: string;
|
||||
@@ -174,10 +184,10 @@ const typeRefRow = (
|
||||
name: number,
|
||||
namespace: number,
|
||||
stringSize: MetadataIndexSize,
|
||||
resolutionScopeSize: MetadataIndexSize,
|
||||
resolutionScope: Buffer,
|
||||
): Buffer =>
|
||||
Buffer.concat([
|
||||
metadataIndex(0, resolutionScopeSize),
|
||||
resolutionScope,
|
||||
metadataIndex(name, stringSize),
|
||||
metadataIndex(namespace, stringSize),
|
||||
]);
|
||||
@@ -524,6 +534,8 @@ interface FixtureMetadataRowContext {
|
||||
readonly methodSignature: number;
|
||||
readonly constructorSignature: number;
|
||||
readonly attributeBlob: number;
|
||||
readonly typeSpecSignatures: readonly number[];
|
||||
readonly methodSpecInstantiation: number;
|
||||
};
|
||||
readonly heapSizes: {
|
||||
readonly strings: MetadataIndexSize;
|
||||
@@ -595,6 +607,37 @@ const exportedTypeTable = (
|
||||
],
|
||||
];
|
||||
|
||||
const specTables = (
|
||||
options: ManagedPeFixtureOptions,
|
||||
indexes: FixtureMetadataRowContext["indexes"],
|
||||
heapSizes: FixtureMetadataRowContext["heapSizes"],
|
||||
widths: FixtureIndexWidths,
|
||||
): readonly (readonly [number, readonly Buffer[]])[] => [
|
||||
...(indexes.typeSpecSignatures.length === 0
|
||||
? []
|
||||
: ([
|
||||
[
|
||||
27,
|
||||
indexes.typeSpecSignatures.map((signature) =>
|
||||
metadataIndex(signature, heapSizes.blobs),
|
||||
),
|
||||
],
|
||||
] as const)),
|
||||
...(options.methodSpecMethods === undefined
|
||||
? []
|
||||
: ([
|
||||
[
|
||||
43,
|
||||
options.methodSpecMethods.map((method) =>
|
||||
Buffer.concat([
|
||||
metadataIndex(method, widths.coded([6, 10], 1)),
|
||||
metadataIndex(indexes.methodSpecInstantiation, heapSizes.blobs),
|
||||
]),
|
||||
),
|
||||
],
|
||||
] as const)),
|
||||
];
|
||||
|
||||
const fixtureIndexWidths = ({
|
||||
options,
|
||||
referenceStringIndexes,
|
||||
@@ -616,10 +659,12 @@ const fixtureIndexWidths = ({
|
||||
],
|
||||
...(indexes.pinvokeModuleName === null ? [] : ([[26, 1]] as const)),
|
||||
...(indexes.pinvokeImportName === null ? [] : ([[28, 1]] as const)),
|
||||
[27, indexes.typeSpecSignatures.length],
|
||||
[32, options.assemblyRowCount ?? 1],
|
||||
[35, referenceStringIndexes.length],
|
||||
[39, indexes.exportedTypes.length],
|
||||
[40, 1],
|
||||
[43, options.methodSpecMethods?.length ?? 0],
|
||||
]);
|
||||
const count = (table: number): number => rowCounts.get(table) ?? 0;
|
||||
const table = (index: number): MetadataIndexSize => indexSize(count(index));
|
||||
@@ -673,7 +718,7 @@ const fixtureMetadataRows = ({
|
||||
indexes.attributeName,
|
||||
indexes.attributeNamespace,
|
||||
heapSizes.strings,
|
||||
widths.resolutionScope,
|
||||
metadataIndex(options.typeRefScopeRaw ?? 0, widths.resolutionScope),
|
||||
),
|
||||
],
|
||||
],
|
||||
@@ -780,6 +825,7 @@ const fixtureMetadataRows = ({
|
||||
),
|
||||
],
|
||||
...exportedTypeTable(options, indexes, heapSizes, widths),
|
||||
...specTables(options, indexes, heapSizes, widths),
|
||||
[
|
||||
40,
|
||||
[
|
||||
@@ -795,6 +841,29 @@ const fixtureMetadataRows = ({
|
||||
return rows;
|
||||
};
|
||||
|
||||
/** Blob indexes for the MemberRef and optional spec rows; defaults leave the heap unchanged. */
|
||||
const referenceBlobIndexes = (
|
||||
blobs: BlobHeap,
|
||||
options: ManagedPeFixtureOptions,
|
||||
): Pick<
|
||||
FixtureMetadataRowContext["indexes"],
|
||||
"constructorSignature" | "typeSpecSignatures" | "methodSpecInstantiation"
|
||||
> => ({
|
||||
constructorSignature: blobs.add(
|
||||
options.memberRefSignature ?? Buffer.from([0x20, 0x01, 0x01, 0x0e]),
|
||||
),
|
||||
typeSpecSignatures: (options.typeSpecSignatures ?? []).map((signature) =>
|
||||
blobs.add(signature),
|
||||
),
|
||||
// Defaults to GENERICINST with one string type argument.
|
||||
methodSpecInstantiation:
|
||||
options.methodSpecMethods === undefined
|
||||
? 0
|
||||
: blobs.add(
|
||||
options.methodSpecInstantiation ?? Buffer.from([0x0a, 0x01, 0x0e]),
|
||||
),
|
||||
});
|
||||
|
||||
const buildManagedFixtureMetadata = (
|
||||
options: ManagedPeFixtureOptions,
|
||||
methodRva: number,
|
||||
@@ -805,7 +874,7 @@ const buildManagedFixtureMetadata = (
|
||||
const assemblyName = strings.add("Fixture.Managed");
|
||||
const attributeName = strings.add("TargetFrameworkAttribute");
|
||||
const attributeNamespace = strings.add("System.Runtime.Versioning");
|
||||
const constructorName = strings.add(".ctor");
|
||||
const constructorName = strings.add(options.memberRefName ?? ".ctor");
|
||||
const typeName = strings.add(options.typeName ?? "Program");
|
||||
const additionalTypeNames = Array.from(
|
||||
{ length: options.additionalEmptyTypes ?? 0 },
|
||||
@@ -840,7 +909,7 @@ const buildManagedFixtureMetadata = (
|
||||
const methodSignature = blobs.add(
|
||||
options.methodSignature ?? Buffer.from([0x00, 0x00, 0x01]),
|
||||
);
|
||||
const constructorSignature = blobs.add(Buffer.from([0x20, 0x01, 0x01, 0x0e]));
|
||||
const referenceBlobs = referenceBlobIndexes(blobs, options);
|
||||
const attributeBlob = blobs.add(
|
||||
fixedStringAttributeBlob(
|
||||
options.targetFramework === null
|
||||
@@ -873,8 +942,8 @@ const buildManagedFixtureMetadata = (
|
||||
tokenBlob,
|
||||
fieldSignature,
|
||||
methodSignature,
|
||||
constructorSignature,
|
||||
attributeBlob,
|
||||
...referenceBlobs,
|
||||
},
|
||||
heapSizes: { strings: stringSize, blobs: blobSize, guids: guidSize },
|
||||
});
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import { managedDecodeBudget } from "./ManagedDecodeBudget.js";
|
||||
import type { ManagedParseIssue } from "../domain/managed/managedArtifact.js";
|
||||
import {
|
||||
fullName,
|
||||
readCompressed,
|
||||
specSignature,
|
||||
type TypeRange,
|
||||
} from "./ManagedMemberInspectorCore.js";
|
||||
import {
|
||||
metadataRowOffset,
|
||||
type ManagedMetadataLayout,
|
||||
} from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
metadataCodedTokenInvalidReason,
|
||||
metadataRowCursor,
|
||||
metadataToken,
|
||||
readMetadataBlob,
|
||||
readMetadataString,
|
||||
} from "./ManagedMetadataHeaps.js";
|
||||
import { managedFailure, readManagedValue } from "./ManagedReaderFailure.js";
|
||||
|
||||
/**
|
||||
* Name the type a TypeDef, TypeRef, or generic-instantiation TypeSpec token
|
||||
* denotes, in the namespace-qualified form TypeDef `full_name` uses, once per
|
||||
* token. Other decoded TypeSpecs stay null: the type is unknown. Malformed or
|
||||
* unsupported rows and blobs are reported to `issues` and also stay null,
|
||||
* except that a TypeRef with an invalid ResolutionScope keeps its readable name.
|
||||
*/
|
||||
export const typeNameLookup = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
ranges: readonly TypeRange[],
|
||||
issues: ManagedParseIssue[],
|
||||
): ((token: string | null) => string | null) => {
|
||||
const names = new Map<string, string | null>();
|
||||
const checkedScopes = new Set<number>();
|
||||
const validateScopeChain = (row: number): void => {
|
||||
const path = new Set<number>();
|
||||
let current = row;
|
||||
while (!checkedScopes.has(current)) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 1, current);
|
||||
if (path.has(current)) {
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.TypeRef:${metadataToken(1, current)}`,
|
||||
offset: cursor.start,
|
||||
detail: "TypeRef ResolutionScope chain contains a cycle",
|
||||
});
|
||||
break;
|
||||
}
|
||||
// Admit the chain's visited entry and retained validation-cache entry.
|
||||
managedDecodeBudget(layout).reserve(128, cursor.start);
|
||||
path.add(current);
|
||||
const raw = cursor.readIndex(layout.codedIndexSize("ResolutionScope"));
|
||||
const reason = metadataCodedTokenInvalidReason(
|
||||
raw,
|
||||
2,
|
||||
[0, 26, 35, 1],
|
||||
layout.rowCounts,
|
||||
);
|
||||
if (reason !== null) {
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.TypeRef:${metadataToken(1, current)}`,
|
||||
offset: cursor.start,
|
||||
detail: `TypeRef ResolutionScope coded index 0x${raw.toString(16)} is invalid: ${reason}`,
|
||||
});
|
||||
break;
|
||||
}
|
||||
// Null scopes are legal; non-TypeRef scopes terminate local validation.
|
||||
if (raw === 0 || (raw & 3) !== 3) break;
|
||||
current = Math.floor(raw / 4);
|
||||
}
|
||||
for (const visited of path) checkedScopes.add(visited);
|
||||
};
|
||||
const definitionName = (table: 1 | 2, row: number): string | null => {
|
||||
if (table === 2) {
|
||||
metadataRowOffset(layout, 2, row);
|
||||
return ranges[row - 1]?.fullName ?? null;
|
||||
}
|
||||
const cursor = metadataRowCursor(bytes, layout, 1, row);
|
||||
cursor.readIndex(layout.codedIndexSize("ResolutionScope"));
|
||||
readManagedValue(() => validateScopeChain(row), issues);
|
||||
const name = cursor.readIndex(layout.stringIndexSize);
|
||||
const namespace = cursor.readIndex(layout.stringIndexSize);
|
||||
return fullName(
|
||||
readMetadataString(bytes, layout, namespace, layout.strings.size),
|
||||
readMetadataString(bytes, layout, name, layout.strings.size),
|
||||
);
|
||||
};
|
||||
const specName = (row: number): string | null => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 27, row);
|
||||
const spec = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
const owner = {
|
||||
form: "type",
|
||||
scope: `metadata.TypeSpec:${metadataToken(27, row)}`,
|
||||
offset: cursor.start,
|
||||
} as const;
|
||||
specSignature(layout, spec, owner);
|
||||
if (spec[0] !== 0x15) return null;
|
||||
// GENERICINST (CLASS | VALUETYPE) TypeDefOrRefEncoded: never a TypeSpec,
|
||||
// so naming never recurses.
|
||||
const encoded = readCompressed(spec, 2).value;
|
||||
const table = ([2, 1] as const)[encoded & 3];
|
||||
if (table === undefined)
|
||||
throw managedFailure(
|
||||
"invalid-blob",
|
||||
owner.scope,
|
||||
"Generic instantiation does not name a TypeDef or TypeRef",
|
||||
owner.offset,
|
||||
);
|
||||
return definitionName(table, Math.floor(encoded / 4));
|
||||
};
|
||||
return (token) => {
|
||||
if (token === null) return null;
|
||||
if (names.has(token)) return names.get(token) ?? null;
|
||||
const table = Number.parseInt(token.slice(2, 4), 16);
|
||||
const row = Number.parseInt(token.slice(4), 16);
|
||||
const name =
|
||||
readManagedValue(
|
||||
() =>
|
||||
table === 27
|
||||
? specName(row)
|
||||
: table === 1 || table === 2
|
||||
? definitionName(table, row)
|
||||
: null,
|
||||
issues,
|
||||
) ?? null;
|
||||
names.set(token, name);
|
||||
return name;
|
||||
};
|
||||
};
|
||||
@@ -127,7 +127,7 @@ export const resolveGhidraAnalysisProfile = async (
|
||||
"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",
|
||||
function_boundary_observations: "ghidra-terminal-call-limitations-v2",
|
||||
no_return_repair:
|
||||
installation.platform === "win32"
|
||||
? "disabled-windows-p0"
|
||||
@@ -136,7 +136,7 @@ export const resolveGhidraAnalysisProfile = async (
|
||||
installation.platform === "win32"
|
||||
? "official-headless-script-v1"
|
||||
: "inspected-jvm-launch-support-v1",
|
||||
load_image_evidence: "independent-raw-mapping-context-v2",
|
||||
load_image_evidence: "independent-raw-mapping-context-v4",
|
||||
jump_table_evidence: "typed-case-default-blocks-v1",
|
||||
decompiler_jump_loads: true,
|
||||
loader: "BinaryLoader",
|
||||
@@ -218,7 +218,7 @@ export const resolveGhidraAnalysisProfile = async (
|
||||
"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",
|
||||
function_boundary_observations: "ghidra-terminal-call-limitations-v2",
|
||||
no_return_repair:
|
||||
installation.platform === "win32"
|
||||
? "disabled-windows-p0"
|
||||
|
||||
@@ -23,6 +23,8 @@ import {
|
||||
const observationOperations: ReadonlySet<GhidraFunctionOperation> = new Set([
|
||||
"analyze_function",
|
||||
"procedure_assembly",
|
||||
"procedure_callees",
|
||||
"procedure_callers",
|
||||
"procedure_info",
|
||||
"procedure_pseudo_code",
|
||||
"read_function_instructions",
|
||||
@@ -93,6 +95,29 @@ describe("Ghidra function-analysis result values", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["procedure_callees", "procedure_callers"] as const)(
|
||||
"requires the function-observation envelope to carry terminal-call limitations for %s",
|
||||
(operation) => {
|
||||
// A bare address list is the pre-fix bridge shape; without the envelope
|
||||
// a truncated function body reports no warning, so it must be rejected.
|
||||
expect(parseGhidraFunctionResult(operation, ["0x2000"])).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisOutputError" },
|
||||
});
|
||||
const limitation =
|
||||
"Ghidra Listing reports a terminal call at 0x401000 to 0x402000 abort; fallthrough is excluded by Ghidra's flow model.";
|
||||
expect(
|
||||
parseGhidraFunctionResult(operation, {
|
||||
value: ["0x2000"],
|
||||
limitations: [limitation],
|
||||
}),
|
||||
).toEqual({
|
||||
ok: true,
|
||||
value: { value: ["0x2000"], limitations: [limitation] },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["reference_kinds_available", "unresolved_calls"])(
|
||||
"rejects omitted %s instead of inventing complete reference observations",
|
||||
(field) => {
|
||||
|
||||
@@ -409,6 +409,8 @@ const functionBoundaryObservationOperations: ReadonlySet<GhidraFunctionOperation
|
||||
new Set([
|
||||
"analyze_function",
|
||||
"procedure_assembly",
|
||||
"procedure_callees",
|
||||
"procedure_callers",
|
||||
"procedure_info",
|
||||
"procedure_pseudo_code",
|
||||
"read_function_instructions",
|
||||
|
||||
@@ -361,7 +361,10 @@ export const attestGhidraRawBinaryLoadImage = (
|
||||
});
|
||||
};
|
||||
const source = observations.source_files[0];
|
||||
const mapping = observations.mappings[0];
|
||||
const fileMappings = observations.mappings.filter(
|
||||
(item) => item.source_file_index !== null,
|
||||
);
|
||||
const mapping = fileMappings[0];
|
||||
check("executable_format", "Raw Binary", observations.executable_format);
|
||||
check("language_id", language.languageId, observations.language_id);
|
||||
check(
|
||||
@@ -371,11 +374,34 @@ export const attestGhidraRawBinaryLoadImage = (
|
||||
);
|
||||
// BinaryLoader's block base is independent of the Program image base.
|
||||
check("image_base", "0x0", observations.image_base);
|
||||
check(
|
||||
"default_address_space",
|
||||
language.defaultAddressSpace,
|
||||
observations.default_address_space,
|
||||
);
|
||||
check("source_file_count", 1, observations.source_files.length);
|
||||
check("source_file_size", snapshot.bytes, source?.size ?? null);
|
||||
check("original_file_sha256", target.sha256, source?.original_sha256 ?? null);
|
||||
check("modified_file_sha256", target.sha256, source?.modified_sha256 ?? null);
|
||||
check("mapping_count", 1, observations.mappings.length);
|
||||
// Language-defined uninitialized zero page/stack blocks are observations, not file bytes.
|
||||
check("mapping_count", 1, fileMappings.length);
|
||||
check(
|
||||
"initialized_unbacked_mappings",
|
||||
0,
|
||||
observations.mappings.filter(
|
||||
(item) => item.initialized && item.source_file_index === null,
|
||||
).length,
|
||||
);
|
||||
check("mapping.source_file_index", 0, mapping?.source_file_index ?? null);
|
||||
// Matching bytes alone do not establish the admitted default-space load.
|
||||
check("mapping.initialized", true, mapping?.initialized ?? null);
|
||||
check("mapping.loaded", true, mapping?.loaded ?? null);
|
||||
check("mapping.overlay", false, mapping?.overlay ?? null);
|
||||
check(
|
||||
"mapping.address_space",
|
||||
observations.default_address_space,
|
||||
mapping?.address_space ?? null,
|
||||
);
|
||||
check(
|
||||
"mapping.file_offset",
|
||||
0,
|
||||
@@ -427,8 +453,9 @@ export const attestGhidraRawBinaryLoadImage = (
|
||||
checks,
|
||||
limitations: [
|
||||
"Raw binary is a headerless interpretation explicitly selected by the caller, not format identification from file bytes.",
|
||||
"Verification covers complete original/modified source identity, mapped file bytes, the caller-supplied entry, and the bound little-endian Ghidra language. File offset 0 is mapped at the entry address.",
|
||||
"Verification covers complete original/modified source identity, initialized and loaded non-overlay file bytes in the default address space, the caller-supplied entry, and the bound little-endian Ghidra language. File offset 0 is mapped at the entry address.",
|
||||
"Ghidra may report additional language-defined external entries outside the mapped image. They are preserved as observations; verification requires the caller-supplied entry and does not attest other entries.",
|
||||
"Language-defined uninitialized memory, such as 6502 zero page and stack, is preserved as observations but is not attested as file-backed bytes.",
|
||||
"Reset vectors, memory-mapped I/O, big-endian and Thumb-only variants, and runtime boot behavior are not modeled or verified. No original executable bytes are changed.",
|
||||
"Mapping ends are inclusive; a mismatching digest identifies its source range, not the first differing byte.",
|
||||
],
|
||||
|
||||
@@ -8,7 +8,7 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, onTestFinished, vi } from "vitest";
|
||||
import { fixtureDosLoadImage } from "./GhidraLoadImage.fixture.js";
|
||||
import { fixtureRawBinaryLoadImage } from "./GhidraRawBinaryLoadImage.fixture.js";
|
||||
import {
|
||||
@@ -30,6 +30,8 @@ import { err, ok } from "../domain/result.js";
|
||||
import type { Result } from "../domain/result.js";
|
||||
import { GhidraSessionError } from "./GhidraSessionError.js";
|
||||
import { silentLogger } from "../logger.js";
|
||||
import * as WindowsNativeLoader from "../windows/WindowsNativeLoader.js";
|
||||
import { WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON } from "../process/WindowsAuthority.js";
|
||||
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { ProviderStartupDeadline } from "../process/ProviderDeadline.js";
|
||||
@@ -190,6 +192,36 @@ describe("Ghidra provider", () => {
|
||||
});
|
||||
|
||||
describe("Ghidra raw-binary profile", () => {
|
||||
it("admits raw 6502 on Windows and binds its 16-bit language without mutation authority", async () => {
|
||||
const ghidra = provider({ ...installationHost(), platform: "win32" });
|
||||
const target: BinaryTarget = {
|
||||
path: "/tmp/6502.bin",
|
||||
sha256: "a".repeat(64),
|
||||
kind: "executable",
|
||||
format: "raw-binary",
|
||||
architecture: "6502",
|
||||
availableArchitectures: ["6502"],
|
||||
entryPoint: "0x8001",
|
||||
entryPointSource: "0x008001",
|
||||
};
|
||||
expect(ghidra.inspectTargetSupport(target).status).toBe("supported");
|
||||
const resolved = await ghidra.resolveAnalysisProfile(target);
|
||||
if (!resolved.ok) throw resolved.error;
|
||||
expect(resolved.value.profile?.parameters).toMatchObject({
|
||||
architecture: "6502",
|
||||
loader: "BinaryLoader",
|
||||
language_id: "6502:LE:16:default",
|
||||
compiler_spec_id: "default",
|
||||
entry_point: "0x8001",
|
||||
language_selection: "caller-supplied-v1",
|
||||
load_image_evidence: "independent-raw-mapping-context-v4",
|
||||
});
|
||||
expect(
|
||||
ghidra
|
||||
.capabilities()
|
||||
.some(({ available, effects }) => available && effects.mutatesArtifact),
|
||||
).toBe(false);
|
||||
});
|
||||
it("commits BinaryLoader language and caller-supplied entry", async () => {
|
||||
const ghidra = provider();
|
||||
const target: BinaryTarget = {
|
||||
@@ -324,6 +356,13 @@ describe("Ghidra platform support", () => {
|
||||
});
|
||||
|
||||
it("keeps Windows P0 unavailable until native isolation authority exists", () => {
|
||||
const native = vi
|
||||
.spyOn(WindowsNativeLoader, "systemWindowsNativeAuthority")
|
||||
.mockReturnValue({
|
||||
available: false,
|
||||
reason: WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON,
|
||||
});
|
||||
onTestFinished(() => native.mockRestore());
|
||||
const ghidra = provider({ ...installationHost(), platform: "win32" });
|
||||
const nativeApplication = peTarget("x86_64");
|
||||
|
||||
@@ -789,7 +828,11 @@ describe("Ghidra relationship coverage", () => {
|
||||
start: () => Promise.resolve(ok(sessionInfo())),
|
||||
callTool: (operation) =>
|
||||
Promise.resolve(
|
||||
ok(operation === "procedure_callees" ? ["0x2000"] : []),
|
||||
ok(
|
||||
operation === "procedure_callees"
|
||||
? { value: ["0x2000"], limitations: [] }
|
||||
: { value: [], limitations: [] },
|
||||
),
|
||||
),
|
||||
close: () => Promise.resolve(ok(null)),
|
||||
}));
|
||||
@@ -812,11 +855,22 @@ describe("Ghidra relationship coverage", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Ghidra measured load-image projection", () => {
|
||||
it.each(["verified", "changed-snapshot", "cancelled"] as const)(
|
||||
describe("Ghidra measured raw load-image projection", () => {
|
||||
it.each([
|
||||
"verified",
|
||||
"mapping-mismatch",
|
||||
"changed-snapshot",
|
||||
"cancelled",
|
||||
] as const)(
|
||||
"streams raw snapshot identity and preserves %s outcomes",
|
||||
async (state) => {
|
||||
const fixture = fixtureRawBinaryLoadImage();
|
||||
if (state === "mapping-mismatch") {
|
||||
const mapping = fixture.observation.mappings[0];
|
||||
if (mapping === undefined)
|
||||
throw new Error("Missing source mapping fixture");
|
||||
mapping.overlay = true;
|
||||
}
|
||||
const controller = new AbortController();
|
||||
const signals: (AbortSignal | undefined)[] = [];
|
||||
const ghidra = provider(installationHost(), () => ({
|
||||
@@ -872,12 +926,23 @@ describe("Ghidra measured load-image projection", () => {
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
expect(signals).toEqual([controller.signal]);
|
||||
if (state === "verified") {
|
||||
if (state === "verified" || state === "mapping-mismatch") {
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.result).toMatchObject({
|
||||
status: "verified",
|
||||
status: state === "verified" ? "verified" : "mismatch",
|
||||
observations: fixture.observation,
|
||||
});
|
||||
if (state === "mapping-mismatch") {
|
||||
expect(result.value.result).toMatchObject({
|
||||
checks: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "mapping.overlay",
|
||||
observed: true,
|
||||
matched: false,
|
||||
}),
|
||||
]),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
@@ -894,7 +959,9 @@ describe("Ghidra measured load-image projection", () => {
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("Ghidra measured DOS load-image projection", () => {
|
||||
it.each([
|
||||
"verified",
|
||||
"mismatch",
|
||||
|
||||
@@ -66,6 +66,7 @@ const SUPPORTED_ARCHITECTURES = new Set([
|
||||
"arm",
|
||||
"arm64",
|
||||
"mips",
|
||||
"6502",
|
||||
"riscv",
|
||||
]);
|
||||
|
||||
@@ -184,7 +185,7 @@ export class GhidraProvider implements AnalysisProviderCandidate {
|
||||
status: "unsupported",
|
||||
code: "architecture_unsupported",
|
||||
reason:
|
||||
"Ghidra v1 requires a supported x86, x86_64, arm, arm64, or MIPS target profile.",
|
||||
"Ghidra v1 requires a supported x86, x86_64, arm, arm64, MIPS, RISC-V, or raw 6502 target profile.",
|
||||
diagnostics,
|
||||
};
|
||||
return {
|
||||
@@ -304,11 +305,15 @@ const inspectWindowsP0TargetSupport = (
|
||||
target: BinaryTarget,
|
||||
diagnostics: Readonly<Record<string, JsonValue>>,
|
||||
): ProviderTargetSupport => {
|
||||
// The raw 6502 lane uses the same immutable snapshot and native ownership boundary.
|
||||
if (target.format === "raw-binary" && target.architecture === "6502")
|
||||
return { status: "supported", code: null, reason: null, diagnostics };
|
||||
if (target.format !== "pe")
|
||||
return {
|
||||
status: "unsupported",
|
||||
code: "target_format_unsupported",
|
||||
reason: "Windows Ghidra P0 accepts PE targets only.",
|
||||
reason:
|
||||
"Windows Ghidra P0 accepts native PE and explicitly selected raw 6502 targets only.",
|
||||
diagnostics,
|
||||
};
|
||||
if (target.architecture !== "x86" && target.architecture !== "x86_64")
|
||||
|
||||
@@ -33,7 +33,7 @@ export const healthLimitations = Object.freeze([
|
||||
|
||||
/** Additional limitations applied to the experimental Windows x64 P0 boundary. */
|
||||
export const windowsP0Limitations = Object.freeze([
|
||||
"Windows Ghidra P0 accepts approved native x86 and x86-64 PE applications and DLLs; managed, hostile, sensitive, and mutable-path targets are unsupported.",
|
||||
"Windows Ghidra P0 accepts native x86 and x86-64 PE applications and DLLs, and explicitly selected raw 6502 images; managed, hostile, sensitive, and mutable-path targets are unsupported.",
|
||||
"The Windows bridge uses authenticated IPv4 loopback because Node path-based IPC does not expose Java AF_UNIX sockets; the endpoint file contains no bearer token.",
|
||||
"Windows sessions require the matching packaged Windows x64 native addon, local NTFS targets and runtimes, and Windows 10 or later. Native handles enforce path admission, private DACLs, and Job Object ownership automatically.",
|
||||
]);
|
||||
|
||||
@@ -516,7 +516,7 @@ export const createGhidraProviderClient = (input: {
|
||||
...(target.format === "raw-binary"
|
||||
? [
|
||||
"Headerless raw-binary analysis uses the caller-supplied architecture and entry address; file offset 0 is mapped at that address. Headers are not discovered from file bytes.",
|
||||
"Little-endian generic Ghidra languages are used for x86, x86_64, arm, and arm64. Big-endian, Thumb-only, Cortex-M, and other processor variants are unsupported in this increment.",
|
||||
"Little-endian generic Ghidra languages are used for x86, x86_64, arm, arm64, and NMOS 6502. Big-endian, Thumb-only, Cortex-M, 65C02, 65816, and other processor variants are unsupported in this increment.",
|
||||
"Static analysis does not emulate reset vectors, memory-mapped I/O, or firmware boot sequences.",
|
||||
]
|
||||
: []),
|
||||
|
||||
@@ -1,28 +1,51 @@
|
||||
import type { HeaderArchitecture } from "../domain/binaryTargetTypes.js";
|
||||
import type { RawBinaryAdmission } from "../domain/rawBinary.js";
|
||||
|
||||
/** Bound Ghidra language for a caller-supplied raw-binary architecture. */
|
||||
export interface GhidraRawBinaryLanguage {
|
||||
readonly languageId: string;
|
||||
readonly compilerSpecId: "default" | "gcc";
|
||||
readonly defaultAddressSpace: "ram" | "RAM";
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the four caller-visible CPU families to little-endian generic Ghidra
|
||||
* Map caller-visible CPU families to little-endian generic Ghidra
|
||||
* languages with a supported generic compiler specification. These are
|
||||
* analysis conventions, not evidence of the target's original compiler.
|
||||
* Big-endian, Thumb-only, and Cortex-M variants are out of scope.
|
||||
*/
|
||||
export const ghidraRawBinaryLanguage = (
|
||||
architecture: HeaderArchitecture,
|
||||
architecture: RawBinaryAdmission["architecture"],
|
||||
): GhidraRawBinaryLanguage => {
|
||||
switch (architecture) {
|
||||
case "6502":
|
||||
return {
|
||||
languageId: "6502:LE:16:default",
|
||||
compilerSpecId: "default",
|
||||
defaultAddressSpace: "RAM",
|
||||
};
|
||||
case "x86":
|
||||
return { languageId: "x86:LE:32:default", compilerSpecId: "gcc" };
|
||||
return {
|
||||
languageId: "x86:LE:32:default",
|
||||
compilerSpecId: "gcc",
|
||||
defaultAddressSpace: "ram",
|
||||
};
|
||||
case "x86_64":
|
||||
return { languageId: "x86:LE:64:default", compilerSpecId: "gcc" };
|
||||
return {
|
||||
languageId: "x86:LE:64:default",
|
||||
compilerSpecId: "gcc",
|
||||
defaultAddressSpace: "ram",
|
||||
};
|
||||
case "arm":
|
||||
return { languageId: "ARM:LE:32:v7", compilerSpecId: "default" };
|
||||
return {
|
||||
languageId: "ARM:LE:32:v7",
|
||||
compilerSpecId: "default",
|
||||
defaultAddressSpace: "ram",
|
||||
};
|
||||
case "arm64":
|
||||
return { languageId: "AARCH64:LE:64:v8A", compilerSpecId: "default" };
|
||||
return {
|
||||
languageId: "AARCH64:LE:64:v8A",
|
||||
compilerSpecId: "default",
|
||||
defaultAddressSpace: "ram",
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,23 +9,161 @@ describe("raw-binary Ghidra language binding", () => {
|
||||
expect(ghidraRawBinaryLanguage("x86")).toEqual({
|
||||
languageId: "x86:LE:32:default",
|
||||
compilerSpecId: "gcc",
|
||||
defaultAddressSpace: "ram",
|
||||
});
|
||||
expect(ghidraRawBinaryLanguage("x86_64")).toEqual({
|
||||
languageId: "x86:LE:64:default",
|
||||
compilerSpecId: "gcc",
|
||||
defaultAddressSpace: "ram",
|
||||
});
|
||||
expect(ghidraRawBinaryLanguage("arm")).toEqual({
|
||||
languageId: "ARM:LE:32:v7",
|
||||
compilerSpecId: "default",
|
||||
defaultAddressSpace: "ram",
|
||||
});
|
||||
expect(ghidraRawBinaryLanguage("arm64")).toEqual({
|
||||
languageId: "AARCH64:LE:64:v8A",
|
||||
compilerSpecId: "default",
|
||||
defaultAddressSpace: "ram",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("raw-binary mapped-block state", () => {
|
||||
it.each(["RAM", "ram"])(
|
||||
"requires the bound 6502 address space for %s observations",
|
||||
(space) => {
|
||||
const f = fixtureRawBinaryLoadImage();
|
||||
const target = {
|
||||
...f.target,
|
||||
architecture: "6502" as const,
|
||||
availableArchitectures: ["6502" as const],
|
||||
entryPoint: "0x8001",
|
||||
entryPointSource: "0x8001",
|
||||
};
|
||||
f.observation.language_id = "6502:LE:16:default";
|
||||
f.observation.default_address_space = space;
|
||||
f.observation.entry_points = [target.entryPoint];
|
||||
const mapping = f.observation.mappings[0];
|
||||
if (mapping === undefined)
|
||||
throw new Error("Missing source mapping fixture");
|
||||
mapping.start = target.entryPoint;
|
||||
mapping.end = "0x8004";
|
||||
mapping.address_space = space;
|
||||
expect(
|
||||
attestGhidraRawBinaryLoadImage(f.snapshot, target, f.observation),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
status: space === "RAM" ? "verified" : "mismatch",
|
||||
checks: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "default_address_space",
|
||||
expected: "RAM",
|
||||
observed: space,
|
||||
matched: space === "RAM",
|
||||
}),
|
||||
]),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
it.each([
|
||||
["initialized", false],
|
||||
["loaded", false],
|
||||
["overlay", true],
|
||||
["address_space", "overlay"],
|
||||
] as const)(
|
||||
"retains a mismatch when the file mapping declares %s=%s",
|
||||
(property, value) => {
|
||||
const f = fixtureRawBinaryLoadImage();
|
||||
const mapping = f.observation.mappings[0];
|
||||
if (mapping === undefined)
|
||||
throw new Error("Missing source mapping fixture");
|
||||
Object.assign(mapping, { [property]: value });
|
||||
expect(
|
||||
attestGhidraRawBinaryLoadImage(f.snapshot, f.target, f.observation),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
status: "mismatch",
|
||||
observations: f.observation,
|
||||
checks: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: `mapping.${property}`,
|
||||
matched: false,
|
||||
observed: value,
|
||||
}),
|
||||
]),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
it("does not verify a foreign default address space even when its mapping agrees", () => {
|
||||
const f = fixtureRawBinaryLoadImage();
|
||||
f.observation.default_address_space = "foreign";
|
||||
const mapping = f.observation.mappings[0];
|
||||
if (mapping === undefined)
|
||||
throw new Error("Missing source mapping fixture");
|
||||
mapping.address_space = "foreign";
|
||||
expect(
|
||||
attestGhidraRawBinaryLoadImage(f.snapshot, f.target, f.observation),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
status: "mismatch",
|
||||
observations: f.observation,
|
||||
checks: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "default_address_space",
|
||||
matched: false,
|
||||
observed: "foreign",
|
||||
}),
|
||||
]),
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("raw-binary independent load-image checks", () => {
|
||||
it("retains uninitialized language memory without treating it as file-backed bytes", () => {
|
||||
const f = fixtureRawBinaryLoadImage();
|
||||
const zeroPage = {
|
||||
block: "ZERO_PAGE",
|
||||
start: "0x0",
|
||||
end: "0xff",
|
||||
address_space: "ram",
|
||||
initialized: false,
|
||||
loaded: true,
|
||||
overlay: false,
|
||||
length: 256,
|
||||
source_file_index: null,
|
||||
file_offset: null,
|
||||
sha256: null,
|
||||
};
|
||||
f.observation.mappings.unshift(zeroPage);
|
||||
expect(
|
||||
attestGhidraRawBinaryLoadImage(f.snapshot, f.target, f.observation),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
value: { status: "verified", observations: f.observation },
|
||||
});
|
||||
zeroPage.initialized = true;
|
||||
expect(
|
||||
attestGhidraRawBinaryLoadImage(f.snapshot, f.target, f.observation),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
status: "mismatch",
|
||||
checks: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "initialized_unbacked_mappings",
|
||||
matched: false,
|
||||
}),
|
||||
]),
|
||||
},
|
||||
});
|
||||
});
|
||||
it("verifies mapped bytes at the caller-supplied entry", () => {
|
||||
const f = fixtureRawBinaryLoadImage();
|
||||
const result = attestGhidraRawBinaryLoadImage(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { join, parse, relative } from "node:path";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { parseConfig } from "../config/parseConfig.js";
|
||||
import type {
|
||||
@@ -92,7 +92,14 @@ const target = (change: Partial<RiscvElfMetadata> = {}): BinaryTarget => ({
|
||||
|
||||
let installDir = "";
|
||||
beforeAll(async () => {
|
||||
installDir = await mkdtemp(join(tmpdir(), "rea-ghidra-riscv-languages-"));
|
||||
const root =
|
||||
process.platform === "win32" ? join(process.cwd(), "build") : tmpdir();
|
||||
await mkdir(root, { recursive: true });
|
||||
installDir = await mkdtemp(join(root, "rea-ghidra-riscv-languages-"));
|
||||
// The simulated Linux host needs a POSIX absolute path. A current-drive root
|
||||
// path also reaches this real fixture on Windows without changing admission.
|
||||
if (process.platform === "win32")
|
||||
installDir = `/${relative(parse(installDir).root, installDir).replaceAll("\\", "/")}`;
|
||||
const languages = join(installDir, "Ghidra/Processors/RISCV/data/languages");
|
||||
await mkdir(languages, { recursive: true });
|
||||
await writeFile(join(languages, "riscv.ldefs"), RISCV_LDEFS);
|
||||
|
||||
+25
-5
@@ -8,6 +8,10 @@ import {
|
||||
type GoBinaryContainer,
|
||||
type GoFileMapping,
|
||||
} from "./GoBinaryContainer.js";
|
||||
import {
|
||||
classifyFatContainerIdentity,
|
||||
readFatContainerHeader,
|
||||
} from "../domain/apple/machoContainer.js";
|
||||
import { readGoElfImage } from "./GoElfImage.js";
|
||||
import { readGoMachoImage } from "./GoMachoImage.js";
|
||||
import { readGoPeImage } from "./GoPeImage.js";
|
||||
@@ -187,11 +191,27 @@ const readContainer = (bytes: Buffer): GoBinaryContainer => {
|
||||
return readGoPeImage(bytes);
|
||||
if (bytes.length >= 4) {
|
||||
const magic = bytes.readUInt32BE();
|
||||
if ([0xcafebabe, 0xcafebabf, 0xbebafeca, 0xbfbafeca].includes(magic))
|
||||
throw new GoBinaryFormatFailure(
|
||||
"unsupported",
|
||||
"Universal/fat Mach-O containers require selecting a thin slice",
|
||||
);
|
||||
if ([0xcafebabe, 0xcafebabf, 0xbebafeca, 0xbfbafeca].includes(magic)) {
|
||||
const fatHeader = readFatContainerHeader(bytes, bytes.length);
|
||||
if (fatHeader.state === "malformed")
|
||||
throw new GoBinaryFormatFailure("malformed", fatHeader.reason);
|
||||
const identity =
|
||||
fatHeader.state === "parsed"
|
||||
? classifyFatContainerIdentity(fatHeader, (offset) => {
|
||||
if (offset >= bytes.length) return undefined;
|
||||
if (offset + 4 > bytes.length) return null;
|
||||
return bytes.readUInt32BE(offset);
|
||||
})
|
||||
: undefined;
|
||||
// An invalid FAT-shaped table is not proof of a Mach-O container: JVM
|
||||
// constant-pool bytes can share both the magic and a known CPU value.
|
||||
const confirmedFatMachO = identity?.state === "mach-o";
|
||||
if (confirmedFatMachO)
|
||||
throw new GoBinaryFormatFailure(
|
||||
"unsupported",
|
||||
"Universal/fat Mach-O containers require selecting a thin slice",
|
||||
);
|
||||
}
|
||||
if ([0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe].includes(magic))
|
||||
return readGoMachoImage(bytes);
|
||||
}
|
||||
|
||||
@@ -123,6 +123,8 @@ export const hopperLoaderArgsForTarget = (
|
||||
if (target.kind === "database") return ok([]);
|
||||
if (target.kind !== "executable")
|
||||
return err(new ProviderAdapterError("hopper", "resolve_analysis_profile"));
|
||||
if (target.format === "raw-binary")
|
||||
return err(new ProviderAdapterError("hopper", "resolve_analysis_profile"));
|
||||
const architecture = target.architecture;
|
||||
if (isElfMetadataArchitecture(architecture))
|
||||
return err(
|
||||
@@ -155,7 +157,6 @@ export const hopperLoaderArgsForTarget = (
|
||||
return ok(["-l", "WinPE", flag]);
|
||||
case "dos-mz":
|
||||
case "dos-com":
|
||||
case "raw-binary":
|
||||
return err(
|
||||
new ProviderAdapterError("hopper", "resolve_analysis_profile"),
|
||||
);
|
||||
|
||||
@@ -2,11 +2,7 @@ import { createHash } from "node:crypto";
|
||||
import { open, type FileHandle } from "node:fs/promises";
|
||||
import { z } from "zod";
|
||||
|
||||
import {
|
||||
isMachOFormat,
|
||||
isElfMetadataArchitecture,
|
||||
type BinaryTarget,
|
||||
} from "../domain/binaryTargetTypes.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
||||
import { BinaryTargetError } from "../domain/configurationErrors.js";
|
||||
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
||||
import {
|
||||
@@ -48,10 +44,7 @@ export const resolveHopperMachOImage = async (
|
||||
),
|
||||
);
|
||||
if (signal?.aborted) return err(new AnalysisCancelledError("open_binary"));
|
||||
if (
|
||||
!isMachOFormat(target.format) ||
|
||||
isElfMetadataArchitecture(target.architecture)
|
||||
)
|
||||
if (target.format !== "mach-o" && target.format !== "mach-o-universal")
|
||||
return invalid("Hopper FAT64 image preparation requires a Mach-O target");
|
||||
let file: FileHandle;
|
||||
try {
|
||||
|
||||
@@ -73,7 +73,7 @@ import {
|
||||
} from "./SignatureTargetBinding.js";
|
||||
import { parseCodeSignature } from "./parsers/codesign.js";
|
||||
import { parseDemangledSymbols } from "./parsers/demangle.js";
|
||||
import { parseLipoArchitectures } from "./parsers/lipo.js";
|
||||
import { parseLipoOutput } from "./parsers/lipo.js";
|
||||
import {
|
||||
parsePlistJson,
|
||||
parsePlistXml,
|
||||
@@ -507,7 +507,10 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
);
|
||||
if (!capture.ok) return capture;
|
||||
const provenance = [invocation(capture.value, this.target.path)];
|
||||
const architectures = parseLipoArchitectures(capture.value.stdout);
|
||||
const { architectures } = parseLipoOutput(
|
||||
capture.value.stdout,
|
||||
this.target.path,
|
||||
);
|
||||
const result = listArchitecturesSchema.parse({
|
||||
architectures: {
|
||||
items: architectures,
|
||||
@@ -561,6 +564,19 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
async #inspectSignature(
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<NativeObservation, AnalysisError>> {
|
||||
// This provider's signature inspection covers Mach-O code signatures
|
||||
// only. On another container, codesign's "not signed" verdict cannot
|
||||
// see a format-level signature such as a xar pkg signature, so
|
||||
// reporting it would claim unsigned for a layer that was never
|
||||
// evaluated.
|
||||
if (this.target.kind !== "executable" || !isMachOFormat(this.target.format))
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
IDENTITY.id,
|
||||
"inspect_signature",
|
||||
"Active artifact is not Mach-O; its container-level signature layer is not evaluated by codesign.",
|
||||
),
|
||||
);
|
||||
return withSignatureTarget(
|
||||
this.target,
|
||||
this.#signatureResources,
|
||||
|
||||
@@ -14,10 +14,7 @@ import {
|
||||
selectMachoArchitectureName,
|
||||
} from "./MachoArchitectureSelection.js";
|
||||
import { parseDyldSymbols } from "./parsers/dyldInfo.js";
|
||||
import {
|
||||
parseLipoArchitectures,
|
||||
type LipoArchitecture,
|
||||
} from "./parsers/lipo.js";
|
||||
import { parseLipoOutput, type LipoArchitecture } from "./parsers/lipo.js";
|
||||
import { withoutEchoedPathHeader } from "./parsers/echoedPath.js";
|
||||
import { parseOtoolLoadCommands } from "./parsers/otool.js";
|
||||
import { reconcileMachoUuid } from "./parsers/machoUuid.js";
|
||||
@@ -74,7 +71,10 @@ 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 { format, architectures } = parseLipoOutput(
|
||||
lipo.stdout,
|
||||
context.target.path,
|
||||
);
|
||||
const selection = selectArchitecture(context.target, architectures);
|
||||
if (selection.status === "absent")
|
||||
return err(
|
||||
@@ -145,7 +145,7 @@ export const inspectNativeMacho = async (
|
||||
const containerOffset =
|
||||
selectedArchitectureName === null
|
||||
? undefined
|
||||
: /^Non-fat file:/mu.test(lipo.stdout)
|
||||
: format === "thin"
|
||||
? 0
|
||||
: (architectures.find(({ name }) => name === selectedArchitectureName)
|
||||
?.file_offset ?? undefined);
|
||||
@@ -185,7 +185,7 @@ const normalizeMacho = (
|
||||
toInvocation: (capture: NativeCommandCapture) => NativeCommandInvocation,
|
||||
additionalLimitations: readonly string[],
|
||||
load: ReturnType<typeof parseOtoolLoadCommands>,
|
||||
architectures: ReturnType<typeof parseLipoArchitectures>,
|
||||
architectures: readonly LipoArchitecture[],
|
||||
artifactPath: string,
|
||||
selectedArchitecture: {
|
||||
readonly name: string | null;
|
||||
|
||||
+105
-10
@@ -1,10 +1,28 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseLipoArchitectures } from "./lipo.js";
|
||||
import { parseLipoOutput } from "./lipo.js";
|
||||
|
||||
const fatRecords = `fat_magic 0xcafebabe
|
||||
nfat_arch 2
|
||||
architecture arm64
|
||||
cputype CPU_TYPE_ARM64
|
||||
cpusubtype CPU_SUBTYPE_ARM64_ALL
|
||||
capabilities 0x0
|
||||
offset 4096
|
||||
size 32
|
||||
align 2^12 (4096)
|
||||
architecture x86_64
|
||||
cputype CPU_TYPE_X86_64
|
||||
cpusubtype CPU_SUBTYPE_X86_64_ALL
|
||||
capabilities 0x0
|
||||
offset 8192
|
||||
size 32
|
||||
align 2^12 (4096)
|
||||
`;
|
||||
|
||||
describe("lipo architecture facts", () => {
|
||||
it("normalizes symbolic CPU fields and arm64e capability bits", () => {
|
||||
const architectures = parseLipoArchitectures(`Fat header in: /bin/ls
|
||||
const { format, architectures } = parseLipoOutput(`Fat header in: /bin/ls
|
||||
fat_magic 0xcafebabe
|
||||
nfat_arch 2
|
||||
architecture x86_64
|
||||
@@ -22,6 +40,7 @@ architecture arm64e
|
||||
size 89088
|
||||
align 2^14 (16384)
|
||||
`);
|
||||
expect(format).toBe("fat");
|
||||
expect(architectures).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "x86_64",
|
||||
@@ -50,9 +69,9 @@ architecture arm64e
|
||||
|
||||
it("keeps unknown symbolic CPU values as source text without a code", () => {
|
||||
expect(
|
||||
parseLipoArchitectures(
|
||||
parseLipoOutput(
|
||||
"architecture future\n cputype CPU_TYPE_FUTURE\n cpusubtype CPU_SUBTYPE_FUTURE\n offset 4096\n size 64\n align 2^12\n",
|
||||
),
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type: "CPU_TYPE_FUTURE",
|
||||
@@ -70,18 +89,18 @@ architecture arm64e
|
||||
["0x0", 3],
|
||||
])("applies printed x86_64 capabilities %s", (capabilities, code) => {
|
||||
expect(
|
||||
parseLipoArchitectures(
|
||||
parseLipoOutput(
|
||||
`architecture x86_64\n cpusubtype CPU_SUBTYPE_X86_64_ALL\n capabilities ${capabilities}\n`,
|
||||
)[0]?.cpu_subtype_code,
|
||||
).architectures[0]?.cpu_subtype_code,
|
||||
).toBe(code);
|
||||
});
|
||||
|
||||
it("leaves the subtype code unknown when lipo prints no capabilities", () => {
|
||||
// macOS 26 lipo prints this blank line for LIB64 and plain x86_64 slices.
|
||||
expect(
|
||||
parseLipoArchitectures(
|
||||
parseLipoOutput(
|
||||
"architecture x86_64\n cputype CPU_TYPE_X86_64\n cpusubtype CPU_SUBTYPE_X86_ALL\n capabilities \n offset 16384\n",
|
||||
),
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_subtype: "CPU_SUBTYPE_X86_ALL",
|
||||
@@ -99,9 +118,9 @@ architecture arm64e
|
||||
"retains an arm64e base subtype with capabilities '$capabilities'",
|
||||
({ capabilities, code }) => {
|
||||
expect(
|
||||
parseLipoArchitectures(
|
||||
parseLipoOutput(
|
||||
`architecture arm64e.v1\n cputype CPU_TYPE_ARM64\n cpusubtype CPU_SUBTYPE_ARM64E\n capabilities ${capabilities}\n`,
|
||||
),
|
||||
).architectures,
|
||||
).toMatchObject([
|
||||
{
|
||||
cpu_type_code: 0x0100000c,
|
||||
@@ -112,3 +131,79 @@ architecture arm64e
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("lipo FAT operand framing", () => {
|
||||
it.each([
|
||||
"/fixture/\narchitecture forged\n offset 123\n size 64",
|
||||
"/fixture/\nfat_magic 0xcafebabe\nnfat_arch 1\narchitecture forged",
|
||||
"/fixture/\nNon-fat file: fake is architecture: forged",
|
||||
"/fixture/first\nsecond\nthird",
|
||||
"/fixture/first\r\narchitecture forged\r\n offset 123",
|
||||
"/fixture/[app](arm64).*+$^?\\name",
|
||||
])(
|
||||
"keeps the complete echoed operand out of FAT slice records: %j",
|
||||
(path) => {
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
expect(parseLipoOutput(output, path)).toEqual(
|
||||
parseLipoOutput(fatRecords),
|
||||
);
|
||||
expect(
|
||||
parseLipoOutput(output, path).architectures.map(({ name }) => name),
|
||||
).toEqual(["arm64", "x86_64"]);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps a CRLF pathname separate from a CRLF record terminator", () => {
|
||||
const path = "/fixture/first\r\narchitecture forged\r\n size 64";
|
||||
const output = `Fat header in: ${path}\r\n${fatRecords.replaceAll("\n", "\r\n")}`;
|
||||
|
||||
expect(parseLipoOutput(output, path)).toEqual(parseLipoOutput(fatRecords));
|
||||
});
|
||||
|
||||
it("preserves ordinary FAT metadata with and without an operand", () => {
|
||||
const path = "/fixture/app";
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
expect(parseLipoOutput(output, path)).toEqual(parseLipoOutput(output));
|
||||
});
|
||||
|
||||
it("does not require a FAT path header in captured fixtures", () => {
|
||||
expect(parseLipoOutput(fatRecords, "/fixture/app")).toEqual(
|
||||
parseLipoOutput(fatRecords),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not strip a header for a different operand that shares a prefix", () => {
|
||||
const path = "/fixture/app-extra\narchitecture forged";
|
||||
const output = `Fat header in: ${path}\n${fatRecords}`;
|
||||
|
||||
expect(parseLipoOutput(output, "/fixture/app")).toEqual(
|
||||
parseLipoOutput(output),
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves thin-file parsing when the operand contains FAT-like lines", () => {
|
||||
const path = "/fixture/\nFat header in: fake\narchitecture forged";
|
||||
const output = `Non-fat file: ${path} is architecture: arm64\n`;
|
||||
|
||||
expect(parseLipoOutput(output, path)).toEqual({
|
||||
format: "thin",
|
||||
architectures: [
|
||||
{
|
||||
name: "arm64",
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(parseLipoOutput(output, path)).toEqual(parseLipoOutput(output));
|
||||
});
|
||||
});
|
||||
|
||||
+38
-17
@@ -16,8 +16,26 @@ const architectureSchema = z.object({
|
||||
|
||||
export type LipoArchitecture = z.infer<typeof architectureSchema>;
|
||||
|
||||
/** Parse `lipo -detailed_info` into deterministic slice metadata. */
|
||||
export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
interface LipoOutput {
|
||||
readonly format: "thin" | "fat";
|
||||
readonly architectures: LipoArchitecture[];
|
||||
}
|
||||
|
||||
/** Parse `lipo -detailed_info` framing and deterministic slice metadata. */
|
||||
export const parseLipoOutput = (
|
||||
output: string,
|
||||
operandPath?: string,
|
||||
): LipoOutput => {
|
||||
// The echoed FAT operand is one value, even when its pathname contains lines.
|
||||
if (operandPath !== undefined) {
|
||||
const header = `Fat header in: ${operandPath}`;
|
||||
if (output.startsWith(header)) {
|
||||
if (output.startsWith("\r\n", header.length))
|
||||
output = output.slice(header.length + 2);
|
||||
else if (output.startsWith("\n", header.length))
|
||||
output = output.slice(header.length + 1);
|
||||
}
|
||||
}
|
||||
// lipo echoes a thin file's pathname verbatim, including embedded newlines.
|
||||
// Match its final architecture field before splitting records into lines.
|
||||
const thinArchitecture =
|
||||
@@ -25,20 +43,23 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
output,
|
||||
)?.[1];
|
||||
if (thinArchitecture !== undefined)
|
||||
return [
|
||||
architectureSchema.parse({
|
||||
name: thinArchitecture,
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
}),
|
||||
];
|
||||
return {
|
||||
format: "thin",
|
||||
architectures: [
|
||||
architectureSchema.parse({
|
||||
name: thinArchitecture,
|
||||
cpu_type: null,
|
||||
cpu_subtype: null,
|
||||
cpu_type_code: null,
|
||||
cpu_subtype_code: null,
|
||||
cpu_subtype_base: null,
|
||||
capabilities: null,
|
||||
file_offset: null,
|
||||
size: null,
|
||||
alignment: null,
|
||||
}),
|
||||
],
|
||||
};
|
||||
const architectures: LipoArchitecture[] = [];
|
||||
let current: Record<string, string> | undefined;
|
||||
const flush = (): void => {
|
||||
@@ -90,7 +111,7 @@ export const parseLipoArchitectures = (output: string): LipoArchitecture[] => {
|
||||
flush();
|
||||
if (architectures.length === 0)
|
||||
throw new TypeError("lipo output contained no architectures");
|
||||
return architectures;
|
||||
return { format: "fat", architectures };
|
||||
};
|
||||
|
||||
const integer = (value: string | undefined): number | null => {
|
||||
|
||||
@@ -51,6 +51,7 @@ import {
|
||||
PWNTOOLS_PROVIDER_IDENTITY,
|
||||
PWNTOOLS_LIMITS,
|
||||
} from "./PwntoolsRelease.js";
|
||||
import { pwntoolsInputBytes } from "./PwntoolsInputLimit.js";
|
||||
|
||||
const HOST_REQUIREMENT =
|
||||
"The initial real-verified pwntools artifact profile supports Linux x64 only.";
|
||||
@@ -245,11 +246,23 @@ export class PwntoolsDecoder<
|
||||
undefined,
|
||||
context,
|
||||
);
|
||||
const inputLimit = pwntoolsInputBytes(this.environment);
|
||||
if (!inputLimit.configured)
|
||||
throw pwntoolsUnavailable(
|
||||
inputLimit.error,
|
||||
selectedPath,
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
...context,
|
||||
configurationKey: "REA_PWNTOOLS_MAX_INPUT_BYTES",
|
||||
},
|
||||
);
|
||||
phase = "artifact-read";
|
||||
selectedPath = input.path;
|
||||
const snapshot = await readStableArtifact(
|
||||
input.path,
|
||||
PWNTOOLS_LIMITS.inputBytes,
|
||||
inputLimit.bytes,
|
||||
options?.signal,
|
||||
undefined,
|
||||
this.#resources,
|
||||
|
||||
@@ -30,6 +30,7 @@ export interface PwntoolsFailureContext {
|
||||
readonly operation: string;
|
||||
readonly providerId: string;
|
||||
readonly precedingOutput?: AnalysisCapturedOutput;
|
||||
readonly configurationKey?: string;
|
||||
}
|
||||
const DEFAULT_CONTEXT = {
|
||||
operation: "inspect_binary_layout",
|
||||
@@ -274,7 +275,7 @@ export const pwntoolsUnavailable = (
|
||||
code: "provider_unavailable",
|
||||
reason,
|
||||
diagnostics: {
|
||||
configuration_key: "REA_PWNTOOLS_PYTHON",
|
||||
configuration_key: context.configurationKey ?? "REA_PWNTOOLS_PYTHON",
|
||||
executable_path: path,
|
||||
...(systemCode === undefined ? {} : { system_code: systemCode }),
|
||||
},
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { pwntoolsInputBytes } from "./PwntoolsInputLimit.js";
|
||||
import { pwntoolsUnavailable } from "./PwntoolsFailures.js";
|
||||
import {
|
||||
PWNTOOLS_LIMITS,
|
||||
PWNTOOLS_PROVIDER_IDENTITY,
|
||||
} from "./PwntoolsRelease.js";
|
||||
|
||||
const CONSTRAINT =
|
||||
"REA_PWNTOOLS_MAX_INPUT_BYTES must be a decimal integer from 1 to " +
|
||||
"1073741824 bytes, and REA never installs Python or packages.";
|
||||
|
||||
describe("pwntools input bound", () => {
|
||||
it("keeps the shipped default when the override is absent", () => {
|
||||
expect(pwntoolsInputBytes({})).toEqual({
|
||||
configured: true,
|
||||
bytes: PWNTOOLS_LIMITS.inputBytes,
|
||||
});
|
||||
expect(PWNTOOLS_LIMITS.inputBytes).toBe(32 * 1024 * 1024);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["1", 1],
|
||||
["67108864", 64 * 1024 * 1024],
|
||||
["172879872", 172_879_872],
|
||||
["1073741824", 1024 * 1024 * 1024],
|
||||
] as const)("accepts %s as %s bytes", (value, expected) => {
|
||||
expect(pwntoolsInputBytes({ REA_PWNTOOLS_MAX_INPUT_BYTES: value })).toEqual(
|
||||
{ configured: true, bytes: expected },
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"",
|
||||
"0",
|
||||
"-1",
|
||||
"33554432.0",
|
||||
"1e8",
|
||||
" 67108864",
|
||||
"67_108_864",
|
||||
"0x2000000",
|
||||
"1073741825",
|
||||
"99999999999999999999",
|
||||
"not-a-number",
|
||||
])("rejects %s instead of widening the bound silently", (value) => {
|
||||
expect(pwntoolsInputBytes({ REA_PWNTOOLS_MAX_INPUT_BYTES: value })).toEqual(
|
||||
{
|
||||
configured: false,
|
||||
error: CONSTRAINT,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects a value above the ceiling that a float would round", () => {
|
||||
const ceiling = pwntoolsInputBytes({
|
||||
REA_PWNTOOLS_MAX_INPUT_BYTES: "1073741824",
|
||||
});
|
||||
const above = pwntoolsInputBytes({
|
||||
REA_PWNTOOLS_MAX_INPUT_BYTES: "10737418241",
|
||||
});
|
||||
expect(ceiling.configured).toBe(true);
|
||||
expect(above).toEqual({ configured: false, error: CONSTRAINT });
|
||||
});
|
||||
|
||||
it("never narrows the bound below the shipped default implicitly", () => {
|
||||
const accepted = [1, 1024, PWNTOOLS_LIMITS.inputBytes].map((bytes) =>
|
||||
pwntoolsInputBytes({ REA_PWNTOOLS_MAX_INPUT_BYTES: String(bytes) }),
|
||||
);
|
||||
expect(accepted.every((limit) => limit.configured)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("pwntools unavailability configuration key", () => {
|
||||
const context = {
|
||||
operation: "inspect_binary_layout",
|
||||
providerId: PWNTOOLS_PROVIDER_IDENTITY.id,
|
||||
};
|
||||
const reportedKey = (configurationKey?: string) =>
|
||||
pwntoolsUnavailable(
|
||||
CONSTRAINT,
|
||||
"/caller-selected/python",
|
||||
undefined,
|
||||
undefined,
|
||||
configurationKey === undefined
|
||||
? context
|
||||
: { ...context, configurationKey },
|
||||
).rejections[0]?.diagnostics["configuration_key"];
|
||||
|
||||
it("keeps the engine key unless the failure is about the bound", () => {
|
||||
expect(reportedKey()).toBe("REA_PWNTOOLS_PYTHON");
|
||||
expect(reportedKey("REA_PWNTOOLS_MAX_INPUT_BYTES")).toBe(
|
||||
"REA_PWNTOOLS_MAX_INPUT_BYTES",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { PWNTOOLS_LIMITS } from "./PwntoolsRelease.js";
|
||||
|
||||
// The artifact is read into one in-memory buffer and staged again as a private
|
||||
// snapshot before the owned bridge parses it under a 3 GiB address-space
|
||||
// reservation, so an override stays within a third of that reservation.
|
||||
const MAXIMUM_INPUT_BYTES = 1024 * 1024 * 1024;
|
||||
|
||||
/** Optional caller-supplied input bound, in bytes, without a decimal point. */
|
||||
export const pwntoolsInputBytesSchema = z
|
||||
.string()
|
||||
.regex(/^[1-9]\d*$/u, "must be a positive decimal integer")
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().safe().max(MAXIMUM_INPUT_BYTES));
|
||||
|
||||
const CONSTRAINT =
|
||||
`REA_PWNTOOLS_MAX_INPUT_BYTES must be a decimal integer from 1 to ` +
|
||||
`${String(MAXIMUM_INPUT_BYTES)} bytes, and REA never installs Python or packages.`;
|
||||
|
||||
/** Resolved input bound, or the reason the caller's override cannot be used. */
|
||||
export type PwntoolsInputBytes =
|
||||
| { readonly configured: true; readonly bytes: number }
|
||||
| { readonly configured: false; readonly error: string };
|
||||
|
||||
/**
|
||||
* Resolve the admitted artifact size for the pwntools adapters. An unset value
|
||||
* keeps the shipped default, so the bound is only ever widened deliberately.
|
||||
*/
|
||||
export const pwntoolsInputBytes = (
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
): PwntoolsInputBytes => {
|
||||
const value = environment.REA_PWNTOOLS_MAX_INPUT_BYTES;
|
||||
if (value === undefined)
|
||||
return { configured: true, bytes: PWNTOOLS_LIMITS.inputBytes };
|
||||
const parsed = pwntoolsInputBytesSchema.safeParse(value);
|
||||
return parsed.success
|
||||
? { configured: true, bytes: parsed.data }
|
||||
: { configured: false, error: CONSTRAINT };
|
||||
};
|
||||
Reference in New Issue
Block a user