refactor!: enforce canonical evidence and provider boundaries
Deliver complete Evidence directly through MCP with required nullable analysis profiles and immutable per-server delivery settings. Bind selected environments at construction and require typed cleanup outcomes with retained retry ownership. Remove test-only routing, duplicate session and snapshot metadata, producer-field repairs, and ambiguous provider arguments. Separate artifact byte identity from occurrence classification and retain typed source-map and partial native facts across CLI, MCP, and verifier consumers. BREAKING CHANGE: obsolete Evidence envelopes, session aliases, incomplete snapshots, and missing normalized producer facts are no longer accepted.
This commit is contained in:
@@ -13,7 +13,7 @@ import {
|
||||
managedTableRowCounts,
|
||||
readManagedMetadataInventory,
|
||||
type ManagedMetadataInventory,
|
||||
type ManagedResourceDirectory,
|
||||
readManagedResourceDirectory,
|
||||
} from "./ManagedMetadataInventory.js";
|
||||
import {
|
||||
readManagedMetadataLayout,
|
||||
@@ -320,30 +320,6 @@ const partialMetadataResult = ({
|
||||
],
|
||||
});
|
||||
|
||||
const mapResources = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedPeLayout,
|
||||
issues: ManagedParseIssue[],
|
||||
): ManagedResourceDirectory | null => {
|
||||
const resources = layout.cli?.resources;
|
||||
if (resources === undefined || (resources.rva === 0 && resources.size === 0))
|
||||
return null;
|
||||
try {
|
||||
return {
|
||||
offset: layout.rvaToOffset(
|
||||
resources.rva,
|
||||
resources.size,
|
||||
"cli.resources",
|
||||
),
|
||||
size: resources.size,
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||
issues.push(cause.issue);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/** Inspect PE/CLI identity directly from local bytes without CLR loading. */
|
||||
export const inspectManagedArtifactBytes = (
|
||||
bytes: Buffer,
|
||||
@@ -376,7 +352,10 @@ export const inspectManagedArtifactBytes = (
|
||||
);
|
||||
}
|
||||
const resourceIssues: ManagedParseIssue[] = [];
|
||||
const resourceDirectory = mapResources(bytes, layout, resourceIssues);
|
||||
const resourceDirectory = readManagedResourceDirectory(
|
||||
layout,
|
||||
resourceIssues,
|
||||
);
|
||||
const inventory = readManagedMetadataInventory(
|
||||
bytes,
|
||||
metadata,
|
||||
|
||||
@@ -328,7 +328,62 @@ it.each([
|
||||
anchors: [],
|
||||
});
|
||||
expect(result.methods[0]?.body.issue).toMatch(/CIL/u);
|
||||
expect(result.coverage.state).toBe("partial");
|
||||
expect(result.call_edges).toEqual([]);
|
||||
expect(result.field_accesses).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ facet: "fields", nameOffset: 2 },
|
||||
{ facet: "methods", nameOffset: 8 },
|
||||
{ facet: "member_refs", nameOffset: 2 },
|
||||
{ facet: "types", nameOffset: 4 },
|
||||
] as const)(
|
||||
"preserves independent metadata when a $facet name is unreadable",
|
||||
({ facet, nameOffset }) => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const original = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
const row = original[facet][0]?.row_offset;
|
||||
if (row === undefined) throw new Error(`Fixture must contain ${facet}`);
|
||||
bytes.writeUInt16LE(0xffff, row + nameOffset);
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.module?.mvid).toBe(original.module?.mvid);
|
||||
expect(result.metadata.status).toBe("partial");
|
||||
expect(result[facet]).toEqual([]);
|
||||
for (const remaining of [
|
||||
"fields",
|
||||
"methods",
|
||||
"member_refs",
|
||||
"types",
|
||||
] as const)
|
||||
if (remaining !== facet)
|
||||
expect(result[remaining]).toHaveLength(original[remaining].length);
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
expect.objectContaining({
|
||||
code: "invalid-heap-index",
|
||||
scope: "metadata.#Strings",
|
||||
}),
|
||||
],
|
||||
});
|
||||
if (facet === "types") {
|
||||
expect(result.fields[0]).toMatchObject({
|
||||
declaring_type_token: "0x02000001",
|
||||
declaring_type: null,
|
||||
});
|
||||
expect(result.methods[0]).toMatchObject({
|
||||
declaring_type_token: "0x02000001",
|
||||
declaring_type: null,
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
@@ -9,12 +9,15 @@ import {
|
||||
type ManagedPeLayout,
|
||||
} from "./ManagedPeReader.js";
|
||||
import {
|
||||
type ManagedResourceDirectory,
|
||||
readManagedResourceDirectory,
|
||||
managedTableRowCounts,
|
||||
readManagedMetadataInventory,
|
||||
} from "./ManagedMetadataInventory.js";
|
||||
import { readManagedMetadataLayout } from "./ManagedMetadataLayout.js";
|
||||
import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
|
||||
import {
|
||||
ManagedReaderFailure,
|
||||
uniqueManagedParseIssues,
|
||||
} from "./ManagedReaderFailure.js";
|
||||
import { parseTypes, typeRanges } from "./ManagedMemberInspectorCore.js";
|
||||
import {
|
||||
edges,
|
||||
@@ -63,26 +66,11 @@ const unavailable = (
|
||||
],
|
||||
});
|
||||
|
||||
const resourceDirectory = (
|
||||
const readMemberInventory = (
|
||||
bytes: Buffer,
|
||||
pe: ManagedPeLayout,
|
||||
): ManagedResourceDirectory | null => {
|
||||
if (
|
||||
pe.cli === null ||
|
||||
pe.cli.resources.rva === 0 ||
|
||||
pe.cli.resources.size === 0
|
||||
)
|
||||
return null;
|
||||
return {
|
||||
offset: pe.rvaToOffset(
|
||||
pe.cli.resources.rva,
|
||||
pe.cli.resources.size,
|
||||
"cli.resources",
|
||||
),
|
||||
size: pe.cli.resources.size,
|
||||
};
|
||||
};
|
||||
|
||||
const readMemberInventory = (bytes: Buffer, pe: ManagedPeLayout) => {
|
||||
issues: ManagedParseIssue[],
|
||||
) => {
|
||||
const cli = pe.cli;
|
||||
if (cli === null)
|
||||
throw new TypeError("Managed inventory requires CLI metadata");
|
||||
@@ -99,7 +87,7 @@ const readMemberInventory = (bytes: Buffer, pe: ManagedPeLayout) => {
|
||||
const inventory = readManagedMetadataInventory(
|
||||
bytes,
|
||||
layout,
|
||||
resourceDirectory(pe),
|
||||
readManagedResourceDirectory(pe, issues),
|
||||
);
|
||||
return { layout, inventory };
|
||||
};
|
||||
@@ -113,9 +101,9 @@ export const inspectManagedMembersBytes = (
|
||||
if (pe.cli === null) return unavailable(target, bytes, pe.cliIssue);
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
try {
|
||||
const { layout, inventory } = readMemberInventory(bytes, pe);
|
||||
const { layout, inventory } = readMemberInventory(bytes, pe, issues);
|
||||
issues.push(...inventory.issues);
|
||||
const ranges = typeRanges(bytes, layout);
|
||||
const ranges = typeRanges(bytes, layout, issues);
|
||||
const parsedTypes = parseTypes(bytes, layout, ranges);
|
||||
issues.push(...parsedTypes.issues);
|
||||
const fields = parseFields(bytes, layout, ranges);
|
||||
@@ -127,7 +115,17 @@ export const inspectManagedMembersBytes = (
|
||||
pe,
|
||||
ranges,
|
||||
});
|
||||
const coverageIssues = issues;
|
||||
issues.push(...fields.issues, ...methods.issues);
|
||||
const coverageIssues = uniqueManagedParseIssues(issues);
|
||||
const incompleteMembers =
|
||||
[...fields.fields, ...memberRefs.refs, ...methods.methods].some(
|
||||
(member) => member.signature.parse_status !== "decoded",
|
||||
) ||
|
||||
methods.methods.some(
|
||||
(method) =>
|
||||
method.body.status === "partial" ||
|
||||
method.body.status === "malformed",
|
||||
);
|
||||
const related = edges(
|
||||
methods.methods,
|
||||
methods.core,
|
||||
@@ -159,7 +157,8 @@ export const inspectManagedMembersBytes = (
|
||||
call_edges: related.callEdges,
|
||||
field_accesses: related.fieldAccesses,
|
||||
coverage: {
|
||||
state: coverageIssues.length === 0 ? "complete" : "partial",
|
||||
state:
|
||||
issues.length === 0 && !incompleteMembers ? "complete" : "partial",
|
||||
issues: coverageIssues,
|
||||
},
|
||||
limitations: [
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
managedFailure,
|
||||
ManagedReaderFailure,
|
||||
readManagedValue,
|
||||
} from "./ManagedReaderFailure.js";
|
||||
import type {
|
||||
ManagedMemberInspection,
|
||||
@@ -34,7 +35,7 @@ export type ManagedExceptionRegion =
|
||||
|
||||
export interface TypeRange {
|
||||
readonly token: string;
|
||||
readonly fullName: string;
|
||||
readonly fullName: string | null;
|
||||
readonly fieldStart: number;
|
||||
readonly fieldEnd: number;
|
||||
readonly methodStart: number;
|
||||
@@ -87,23 +88,28 @@ const readTypeRange = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
row: number,
|
||||
issues: ManagedParseIssue[],
|
||||
): TypeRange => {
|
||||
const methods = layout.table(6);
|
||||
const fields = layout.table(4);
|
||||
const cursor = metadataRowCursor(bytes, layout, 2, row);
|
||||
cursor.readUInt32();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const namespace = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const nameIndex = cursor.readIndex(layout.stringIndexSize);
|
||||
const namespaceIndex = cursor.readIndex(layout.stringIndexSize);
|
||||
const declaredName =
|
||||
readManagedValue(
|
||||
() =>
|
||||
fullName(
|
||||
readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
namespaceIndex,
|
||||
layout.strings.size,
|
||||
),
|
||||
readMetadataString(bytes, layout, nameIndex, layout.strings.size),
|
||||
),
|
||||
issues,
|
||||
) ?? null;
|
||||
cursor.readIndex(layout.codedIndexSize("TypeDefOrRef"));
|
||||
const fieldStart = cursor.readIndex(layout.tableIndexSize(4));
|
||||
const methodStart = cursor.readIndex(layout.tableIndexSize(6));
|
||||
@@ -121,7 +127,7 @@ const readTypeRange = (
|
||||
}
|
||||
return {
|
||||
token: metadataToken(2, row),
|
||||
fullName: fullName(namespace, name),
|
||||
fullName: declaredName,
|
||||
fieldStart,
|
||||
fieldEnd: nextField,
|
||||
methodStart,
|
||||
@@ -132,11 +138,12 @@ const readTypeRange = (
|
||||
export const typeRanges = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
issues: ManagedParseIssue[],
|
||||
): readonly TypeRange[] => {
|
||||
const typeTable = layout.table(2);
|
||||
const ranges: TypeRange[] = [];
|
||||
for (let row = 1; row <= (typeTable?.rowCount ?? 0); row += 1)
|
||||
ranges.push(readTypeRange(bytes, layout, row));
|
||||
ranges.push(readTypeRange(bytes, layout, row, issues));
|
||||
return ranges;
|
||||
};
|
||||
|
||||
@@ -144,7 +151,7 @@ export const declaringType = (
|
||||
ranges: readonly TypeRange[],
|
||||
table: "field" | "method",
|
||||
row: number,
|
||||
): { readonly token: string; readonly fullName: string } | null => {
|
||||
): { readonly token: string; readonly fullName: string | null } | null => {
|
||||
for (const range of ranges) {
|
||||
const start = table === "field" ? range.fieldStart : range.methodStart;
|
||||
const end = table === "field" ? range.fieldEnd : range.methodEnd;
|
||||
@@ -204,68 +211,76 @@ export const parseTypes = (
|
||||
const items: ManagedType[] = [];
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
for (let row = 1; row <= (typeTable?.rowCount ?? 0); row += 1) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 2, row);
|
||||
const flags = cursor.readUInt32();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const namespace = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const extendsOffset = cursor.offset;
|
||||
const extendsRaw = cursor.readIndex(layout.codedIndexSize("TypeDefOrRef"));
|
||||
const fieldStart = cursor.readIndex(layout.tableIndexSize(4));
|
||||
const methodStart = cursor.readIndex(layout.tableIndexSize(6));
|
||||
const nextRange = ranges[row] ?? null;
|
||||
const fieldRange = rowRange(fields, fieldStart, nextRange?.fieldStart ?? 0);
|
||||
const methodRange = rowRange(
|
||||
methods,
|
||||
methodStart,
|
||||
nextRange?.methodStart ?? 0,
|
||||
);
|
||||
const extendsReason = metadataCodedTokenInvalidReason(
|
||||
extendsRaw,
|
||||
2,
|
||||
[2, 1, 27],
|
||||
layout.rowCounts,
|
||||
);
|
||||
if (extendsReason !== null)
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.TypeDef:${metadataToken(2, row)}`,
|
||||
offset: extendsOffset,
|
||||
detail: `TypeDef Extends coded index 0x${extendsRaw.toString(16)} is invalid: ${extendsReason}`,
|
||||
});
|
||||
items.push({
|
||||
token: metadataToken(2, row),
|
||||
row_offset: cursor.start,
|
||||
namespace,
|
||||
name,
|
||||
full_name: fullName(namespace, name),
|
||||
flags,
|
||||
extends_token: metadataCodedToken(
|
||||
readManagedValue(() => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 2, row);
|
||||
const flags = cursor.readUInt32();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const namespace = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const extendsOffset = cursor.offset;
|
||||
const extendsRaw = cursor.readIndex(
|
||||
layout.codedIndexSize("TypeDefOrRef"),
|
||||
);
|
||||
const fieldStart = cursor.readIndex(layout.tableIndexSize(4));
|
||||
const methodStart = cursor.readIndex(layout.tableIndexSize(6));
|
||||
const nextRange = ranges[row] ?? null;
|
||||
const fieldRange = rowRange(
|
||||
fields,
|
||||
fieldStart,
|
||||
nextRange?.fieldStart ?? 0,
|
||||
);
|
||||
const methodRange = rowRange(
|
||||
methods,
|
||||
methodStart,
|
||||
nextRange?.methodStart ?? 0,
|
||||
);
|
||||
const extendsReason = metadataCodedTokenInvalidReason(
|
||||
extendsRaw,
|
||||
2,
|
||||
[2, 1, 27],
|
||||
layout.rowCounts,
|
||||
),
|
||||
field_list: {
|
||||
first_row: fieldRange.first,
|
||||
last_row: fieldRange.last,
|
||||
count: fieldRange.count,
|
||||
},
|
||||
method_list: {
|
||||
first_row: methodRange.first,
|
||||
last_row: methodRange.last,
|
||||
count: methodRange.count,
|
||||
},
|
||||
});
|
||||
);
|
||||
if (extendsReason !== null)
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.TypeDef:${metadataToken(2, row)}`,
|
||||
offset: extendsOffset,
|
||||
detail: `TypeDef Extends coded index 0x${extendsRaw.toString(16)} is invalid: ${extendsReason}`,
|
||||
});
|
||||
items.push({
|
||||
token: metadataToken(2, row),
|
||||
row_offset: cursor.start,
|
||||
namespace,
|
||||
name,
|
||||
full_name: fullName(namespace, name),
|
||||
flags,
|
||||
extends_token: metadataCodedToken(
|
||||
extendsRaw,
|
||||
2,
|
||||
[2, 1, 27],
|
||||
layout.rowCounts,
|
||||
),
|
||||
field_list: {
|
||||
first_row: fieldRange.first,
|
||||
last_row: fieldRange.last,
|
||||
count: fieldRange.count,
|
||||
},
|
||||
method_list: {
|
||||
first_row: methodRange.first,
|
||||
last_row: methodRange.last,
|
||||
count: methodRange.count,
|
||||
},
|
||||
});
|
||||
}, issues);
|
||||
}
|
||||
return { types: items, issues };
|
||||
};
|
||||
|
||||
+117
-102
@@ -22,6 +22,7 @@ import {
|
||||
readMetadataBlob,
|
||||
readMetadataString,
|
||||
} from "./ManagedMetadataHeaps.js";
|
||||
import { readManagedValue } from "./ManagedReaderFailure.js";
|
||||
import { methodBody } from "./ManagedMethodBodyReader.js";
|
||||
|
||||
export const parseFields = (
|
||||
@@ -31,40 +32,44 @@ export const parseFields = (
|
||||
): {
|
||||
readonly fields: readonly ManagedField[];
|
||||
readonly core: ReadonlyMap<string, FieldCore>;
|
||||
readonly issues: readonly ManagedParseIssue[];
|
||||
} => {
|
||||
const fields: ManagedField[] = [];
|
||||
const core = new Map<string, FieldCore>();
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
const table = layout.table(4);
|
||||
const declaringType = createDeclaringTypeLookup(ranges, "field");
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 4, row);
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
const declared = declaringType(row);
|
||||
const token = metadataToken(4, row);
|
||||
fields.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
declaring_type_token: declared?.token ?? null,
|
||||
declaring_type: declared?.fullName ?? null,
|
||||
name,
|
||||
flags,
|
||||
signature: signature(sig),
|
||||
});
|
||||
core.set(token, { token, name });
|
||||
readManagedValue(() => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 4, row);
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
const declared = declaringType(row);
|
||||
const token = metadataToken(4, row);
|
||||
fields.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
declaring_type_token: declared?.token ?? null,
|
||||
declaring_type: declared?.fullName ?? null,
|
||||
name,
|
||||
flags,
|
||||
signature: signature(sig),
|
||||
});
|
||||
core.set(token, { token, name });
|
||||
}, issues);
|
||||
}
|
||||
return { fields, core };
|
||||
return { fields, core, issues };
|
||||
};
|
||||
|
||||
export const parseMemberRefs = (
|
||||
@@ -80,52 +85,54 @@ export const parseMemberRefs = (
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
const table = layout.table(10);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 10, row);
|
||||
const parentRaw = cursor.readIndex(
|
||||
layout.codedIndexSize("MemberRefParent"),
|
||||
);
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
const token = metadataToken(10, row);
|
||||
const parentReason = metadataCodedTokenInvalidReason(
|
||||
parentRaw,
|
||||
3,
|
||||
[2, 1, 26, 6, 27],
|
||||
layout.rowCounts,
|
||||
);
|
||||
if (parentReason !== null || parentRaw === 0)
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.MemberRef:${token}`,
|
||||
offset: cursor.start,
|
||||
detail:
|
||||
parentReason === null
|
||||
? "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}`,
|
||||
});
|
||||
refs.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
parent_token: metadataCodedToken(
|
||||
readManagedValue(() => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 10, row);
|
||||
const parentRaw = cursor.readIndex(
|
||||
layout.codedIndexSize("MemberRefParent"),
|
||||
);
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
const token = metadataToken(10, row);
|
||||
const parentReason = metadataCodedTokenInvalidReason(
|
||||
parentRaw,
|
||||
3,
|
||||
[2, 1, 26, 6, 27],
|
||||
layout.rowCounts,
|
||||
),
|
||||
name,
|
||||
signature: signature(sig),
|
||||
});
|
||||
core.set(token, { token, name });
|
||||
);
|
||||
if (parentReason !== null || parentRaw === 0)
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.MemberRef:${token}`,
|
||||
offset: cursor.start,
|
||||
detail:
|
||||
parentReason === null
|
||||
? "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}`,
|
||||
});
|
||||
refs.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
parent_token: metadataCodedToken(
|
||||
parentRaw,
|
||||
3,
|
||||
[2, 1, 26, 6, 27],
|
||||
layout.rowCounts,
|
||||
),
|
||||
name,
|
||||
signature: signature(sig),
|
||||
});
|
||||
core.set(token, { token, name });
|
||||
}, issues);
|
||||
}
|
||||
return { refs, core, issues };
|
||||
};
|
||||
@@ -145,46 +152,54 @@ export const parseMethods = ({
|
||||
}: ParseMethodsInput): {
|
||||
readonly methods: readonly ManagedMethod[];
|
||||
readonly core: ReadonlyMap<string, MethodCore>;
|
||||
readonly issues: readonly ManagedParseIssue[];
|
||||
} => {
|
||||
const methods: ManagedMethod[] = [];
|
||||
const core = new Map<string, MethodCore>();
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
const table = layout.table(6);
|
||||
const declaringType = createDeclaringTypeLookup(ranges, "method");
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 6, row);
|
||||
const rva = cursor.readUInt32();
|
||||
const implFlags = cursor.readUInt16();
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
cursor.readIndex(layout.tableIndexSize(8));
|
||||
const declared = declaringType(row);
|
||||
const token = metadataToken(6, row);
|
||||
methods.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
declaring_type_token: declared?.token ?? null,
|
||||
declaring_type: declared?.fullName ?? null,
|
||||
name,
|
||||
rva,
|
||||
impl_flags: implFlags,
|
||||
flags,
|
||||
signature: signature(sig),
|
||||
body: methodBody(bytes, pe, rva, { implFlags, flags }),
|
||||
});
|
||||
core.set(token, { token, name, declaringType: declared?.fullName ?? null });
|
||||
readManagedValue(() => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 6, row);
|
||||
const rva = cursor.readUInt32();
|
||||
const implFlags = cursor.readUInt16();
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
cursor.readIndex(layout.tableIndexSize(8));
|
||||
const declared = declaringType(row);
|
||||
const token = metadataToken(6, row);
|
||||
methods.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
declaring_type_token: declared?.token ?? null,
|
||||
declaring_type: declared?.fullName ?? null,
|
||||
name,
|
||||
rva,
|
||||
impl_flags: implFlags,
|
||||
flags,
|
||||
signature: signature(sig),
|
||||
body: methodBody(bytes, pe, rva, { implFlags, flags }),
|
||||
});
|
||||
core.set(token, {
|
||||
token,
|
||||
name,
|
||||
declaringType: declared?.fullName ?? null,
|
||||
});
|
||||
}, issues);
|
||||
}
|
||||
return { methods, core };
|
||||
return { methods, core, issues };
|
||||
};
|
||||
|
||||
const targetKind = (token: string): ManagedCallEdge["target_kind"] =>
|
||||
|
||||
@@ -15,7 +15,11 @@ import {
|
||||
readResource,
|
||||
} from "./ManagedMetadataInventoryRows.js";
|
||||
import { metadataToken } from "./ManagedMetadataHeaps.js";
|
||||
import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
|
||||
import {
|
||||
readManagedValue,
|
||||
uniqueManagedParseIssues,
|
||||
} from "./ManagedReaderFailure.js";
|
||||
import type { ManagedPeLayout } from "./ManagedPeReader.js";
|
||||
|
||||
type ModuleIdentity = NonNullable<ManagedArtifactInspection["module"]>;
|
||||
type AssemblyIdentity = NonNullable<ManagedArtifactInspection["assembly"]>;
|
||||
@@ -27,6 +31,25 @@ export interface ManagedResourceDirectory {
|
||||
readonly size: number;
|
||||
}
|
||||
|
||||
/** Map optional embedded resources without discarding admitted CLI metadata. */
|
||||
export const readManagedResourceDirectory = (
|
||||
pe: ManagedPeLayout,
|
||||
issues: ManagedParseIssue[],
|
||||
): ManagedResourceDirectory | null => {
|
||||
const resources = pe.cli?.resources;
|
||||
if (resources === undefined || (resources.rva === 0 && resources.size === 0))
|
||||
return null;
|
||||
return (
|
||||
readManagedValue(
|
||||
() => ({
|
||||
offset: pe.rvaToOffset(resources.rva, resources.size, "cli.resources"),
|
||||
size: resources.size,
|
||||
}),
|
||||
issues,
|
||||
) ?? null
|
||||
);
|
||||
};
|
||||
|
||||
export interface ManagedMetadataInventory {
|
||||
readonly module: ModuleIdentity | null;
|
||||
readonly assembly: AssemblyIdentity | null;
|
||||
@@ -38,19 +61,6 @@ export interface ManagedMetadataInventory {
|
||||
readonly issues: readonly ManagedParseIssue[];
|
||||
}
|
||||
|
||||
const safeRead = <Value>(
|
||||
operation: () => Value,
|
||||
issues: ManagedParseIssue[],
|
||||
): Value | undefined => {
|
||||
try {
|
||||
return operation();
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||
issues.push(cause.issue);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const readRows = <Item>(
|
||||
descriptor: MetadataTableLayout | undefined,
|
||||
read: (row: number) => Item | undefined,
|
||||
@@ -64,12 +74,6 @@ const readRows = <Item>(
|
||||
return items;
|
||||
};
|
||||
|
||||
const uniqueIssues = (
|
||||
issues: readonly ManagedParseIssue[],
|
||||
): readonly ManagedParseIssue[] => [
|
||||
...new Map(issues.map((issue) => [JSON.stringify(issue), issue])).values(),
|
||||
];
|
||||
|
||||
const heapExtent = (layout: ManagedMetadataLayout): number =>
|
||||
Math.max(layout.strings.size, layout.blob.size);
|
||||
|
||||
@@ -105,7 +109,7 @@ const readReferences = (
|
||||
readonly referenceNames: readonly string[];
|
||||
} => {
|
||||
const references = readRows(layout.table(35), (row) =>
|
||||
safeRead(
|
||||
readManagedValue(
|
||||
() => readAssemblyReference(bytes, layout, row, heapExtent(layout)),
|
||||
issues,
|
||||
),
|
||||
@@ -128,7 +132,7 @@ const readResources = ({
|
||||
readonly issues: ManagedParseIssue[];
|
||||
}): readonly ManagedResource[] =>
|
||||
readRows(layout.table(40), (row) =>
|
||||
safeRead(
|
||||
readManagedValue(
|
||||
() =>
|
||||
readResource({
|
||||
bytes,
|
||||
@@ -150,7 +154,7 @@ const readAttributes = (
|
||||
readonly targetFrameworks: readonly string[];
|
||||
} => {
|
||||
const attributes = readRows(layout.table(12), (row) =>
|
||||
safeRead(
|
||||
readManagedValue(
|
||||
() => readCustomAttribute(bytes, layout, row, heapExtent(layout)),
|
||||
issues,
|
||||
),
|
||||
@@ -177,11 +181,15 @@ export const readManagedMetadataInventory = (
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
validateIdentityTableCounts(layout, issues);
|
||||
const module =
|
||||
safeRead(() => readModule(bytes, layout, heapExtent(layout)), issues) ??
|
||||
null;
|
||||
readManagedValue(
|
||||
() => readModule(bytes, layout, heapExtent(layout)),
|
||||
issues,
|
||||
) ?? null;
|
||||
const assembly =
|
||||
safeRead(() => readAssembly(bytes, layout, heapExtent(layout)), issues) ??
|
||||
null;
|
||||
readManagedValue(
|
||||
() => readAssembly(bytes, layout, heapExtent(layout)),
|
||||
issues,
|
||||
) ?? null;
|
||||
const { references, referenceNames } = readReferences(bytes, layout, issues);
|
||||
const resources = readResources({
|
||||
bytes,
|
||||
@@ -202,7 +210,7 @@ export const readManagedMetadataInventory = (
|
||||
references,
|
||||
resources,
|
||||
attributes,
|
||||
issues: uniqueIssues(issues),
|
||||
issues: uniqueManagedParseIssues(issues),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "./ManagedMemberInstructionDecoder.js";
|
||||
import { validateExceptionRegionRanges } from "./ManagedExceptionRegionValidation.js";
|
||||
import { sha256Bytes } from "./ManagedMetadataHeaps.js";
|
||||
import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
|
||||
|
||||
const bodyStatus = (
|
||||
decodedIssue: string | null,
|
||||
@@ -178,6 +179,11 @@ export const methodBody = (
|
||||
: null),
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
if (
|
||||
!(cause instanceof ManagedReaderFailure) &&
|
||||
!(cause instanceof RangeError)
|
||||
)
|
||||
throw cause;
|
||||
return emptyMethodBody(
|
||||
rva,
|
||||
"malformed",
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
import { managedTableRowCounts } from "./ManagedMetadataInventory.js";
|
||||
import type { ManagedMetadataInventory } from "./ManagedMetadataInventory.js";
|
||||
import type { ManagedPeLayout } from "./ManagedPeReader.js";
|
||||
import { readManagedValue } from "./ManagedReaderFailure.js";
|
||||
|
||||
type ModuleRef = ManagedNativeBoundaryInspection["module_refs"][number];
|
||||
type NativeImport = ManagedNativeBoundaryInspection["pinvoke_imports"][number];
|
||||
@@ -38,21 +39,24 @@ export const parseModuleRefs = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
heapExtent: number,
|
||||
issues: ManagedParseIssue[],
|
||||
): readonly ModuleRef[] => {
|
||||
const refs: ModuleRef[] = [];
|
||||
const table = layout.table(26);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 26, row);
|
||||
refs.push({
|
||||
token: metadataToken(26, row),
|
||||
row_offset: cursor.start,
|
||||
name: readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
),
|
||||
});
|
||||
readManagedValue(() => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 26, row);
|
||||
refs.push({
|
||||
token: metadataToken(26, row),
|
||||
row_offset: cursor.start,
|
||||
name: readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
),
|
||||
});
|
||||
}, issues);
|
||||
}
|
||||
return refs;
|
||||
};
|
||||
@@ -61,29 +65,32 @@ export const parseFields = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
heapExtent: number,
|
||||
issues: ManagedParseIssue[],
|
||||
): ReadonlyMap<string, MemberCore> => {
|
||||
const fields = new Map<string, MemberCore>();
|
||||
const table = layout.table(4);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 4, row);
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
cursor.readIndex(layout.blobIndexSize);
|
||||
const token = metadataToken(4, row);
|
||||
fields.set(token, {
|
||||
token,
|
||||
rowOffset: cursor.start,
|
||||
kind: "field",
|
||||
name,
|
||||
flags,
|
||||
implFlags: null,
|
||||
rva: null,
|
||||
});
|
||||
readManagedValue(() => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 4, row);
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
cursor.readIndex(layout.blobIndexSize);
|
||||
const token = metadataToken(4, row);
|
||||
fields.set(token, {
|
||||
token,
|
||||
rowOffset: cursor.start,
|
||||
kind: "field",
|
||||
name,
|
||||
flags,
|
||||
implFlags: null,
|
||||
rva: null,
|
||||
});
|
||||
}, issues);
|
||||
}
|
||||
return fields;
|
||||
};
|
||||
@@ -92,32 +99,35 @@ export const parseMethods = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
heapExtent: number,
|
||||
issues: ManagedParseIssue[],
|
||||
): ReadonlyMap<string, MemberCore> => {
|
||||
const methods = new Map<string, MemberCore>();
|
||||
const table = layout.table(6);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 6, row);
|
||||
const rva = cursor.readUInt32();
|
||||
const implFlags = cursor.readUInt16();
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
cursor.readIndex(layout.blobIndexSize);
|
||||
cursor.readIndex(layout.tableIndexSize(8));
|
||||
const token = metadataToken(6, row);
|
||||
methods.set(token, {
|
||||
token,
|
||||
rowOffset: cursor.start,
|
||||
kind: "method",
|
||||
name,
|
||||
flags,
|
||||
implFlags,
|
||||
rva,
|
||||
});
|
||||
readManagedValue(() => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 6, row);
|
||||
const rva = cursor.readUInt32();
|
||||
const implFlags = cursor.readUInt16();
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
cursor.readIndex(layout.blobIndexSize);
|
||||
cursor.readIndex(layout.tableIndexSize(8));
|
||||
const token = metadataToken(6, row);
|
||||
methods.set(token, {
|
||||
token,
|
||||
rowOffset: cursor.start,
|
||||
kind: "method",
|
||||
name,
|
||||
flags,
|
||||
implFlags,
|
||||
rva,
|
||||
});
|
||||
}, issues);
|
||||
}
|
||||
return methods;
|
||||
};
|
||||
@@ -143,66 +153,81 @@ export const parseImplMaps = ({
|
||||
const imports: NativeImport[] = [];
|
||||
const table = layout.table(28);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = metadataRowCursor(bytes, layout, 28, row);
|
||||
const mappingFlags = cursor.readUInt16();
|
||||
const memberForwardedOffset = cursor.offset;
|
||||
const memberForwardedRaw = cursor.readIndex(
|
||||
layout.codedIndexSize("MemberForwarded"),
|
||||
);
|
||||
const memberForwardedReason = metadataCodedTokenInvalidReason(
|
||||
memberForwardedRaw,
|
||||
1,
|
||||
[4, 6],
|
||||
layout.rowCounts,
|
||||
);
|
||||
if (memberForwardedReason !== null || memberForwardedRaw === 0)
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.ImplMap:${metadataToken(28, row)}`,
|
||||
offset: memberForwardedOffset,
|
||||
detail:
|
||||
memberForwardedReason === null
|
||||
? "ImplMap MemberForwarded coded index 0x0 is null, but the column must reference a Field or MethodDef row"
|
||||
: `ImplMap MemberForwarded coded index 0x${memberForwardedRaw.toString(16)} is invalid: ${memberForwardedReason}`,
|
||||
readManagedValue(() => {
|
||||
const cursor = metadataRowCursor(bytes, layout, 28, row);
|
||||
const mappingFlags = cursor.readUInt16();
|
||||
const memberForwardedOffset = cursor.offset;
|
||||
const memberForwardedRaw = cursor.readIndex(
|
||||
layout.codedIndexSize("MemberForwarded"),
|
||||
);
|
||||
const memberForwardedReason = metadataCodedTokenInvalidReason(
|
||||
memberForwardedRaw,
|
||||
1,
|
||||
[4, 6],
|
||||
layout.rowCounts,
|
||||
);
|
||||
if (memberForwardedReason !== null || memberForwardedRaw === 0)
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.ImplMap:${metadataToken(28, row)}`,
|
||||
offset: memberForwardedOffset,
|
||||
detail:
|
||||
memberForwardedReason === null
|
||||
? "ImplMap MemberForwarded coded index 0x0 is null, but the column must reference a Field or MethodDef row"
|
||||
: `ImplMap MemberForwarded coded index 0x${memberForwardedRaw.toString(16)} is invalid: ${memberForwardedReason}`,
|
||||
});
|
||||
const memberToken = metadataCodedToken(
|
||||
memberForwardedRaw,
|
||||
1,
|
||||
[4, 6],
|
||||
layout.rowCounts,
|
||||
);
|
||||
const importName = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
const importScopeOffset = cursor.offset;
|
||||
const importScopeRow = cursor.readIndex(layout.tableIndexSize(26));
|
||||
const moduleCount = layout.table(26)?.rowCount ?? 0;
|
||||
const validImportScope =
|
||||
importScopeRow > 0 && importScopeRow <= moduleCount;
|
||||
if (!validImportScope)
|
||||
issues.push({
|
||||
code: "invalid-row",
|
||||
scope: `metadata.ImplMap:${metadataToken(28, row)}`,
|
||||
offset: importScopeOffset,
|
||||
detail: `ImplMap ImportScope row ${String(importScopeRow)} must reference a ModuleRef row between 1 and ${String(moduleCount)}`,
|
||||
});
|
||||
const importScopeToken = validImportScope
|
||||
? metadataToken(26, importScopeRow)
|
||||
: null;
|
||||
const member =
|
||||
memberToken === null ? undefined : members.get(memberToken);
|
||||
imports.push({
|
||||
token: metadataToken(28, row),
|
||||
row_offset: cursor.start,
|
||||
mapping_flags: mappingFlags,
|
||||
mapping_flags_hex: flagsHex(mappingFlags),
|
||||
member_token: memberToken,
|
||||
member_kind: member?.kind ?? "unknown",
|
||||
member_name: member?.name ?? null,
|
||||
import_name: importName,
|
||||
import_scope_token: importScopeToken,
|
||||
import_scope_name:
|
||||
importScopeToken === null
|
||||
? null
|
||||
: (moduleNames.get(importScopeToken)?.name ?? null),
|
||||
no_mangle: (mappingFlags & 0x0001) !== 0,
|
||||
char_set: charSet(mappingFlags),
|
||||
call_convention: callConvention(mappingFlags),
|
||||
supports_last_error: (mappingFlags & 0x0040) !== 0,
|
||||
best_fit: bestFit(mappingFlags),
|
||||
throw_on_unmappable_char: throwOnUnmappable(mappingFlags),
|
||||
verification: "managed-declaration-only",
|
||||
});
|
||||
const memberToken = metadataCodedToken(
|
||||
memberForwardedRaw,
|
||||
1,
|
||||
[4, 6],
|
||||
layout.rowCounts,
|
||||
);
|
||||
const importName = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
const importScopeRow = cursor.readIndex(layout.tableIndexSize(26));
|
||||
const importScopeToken =
|
||||
importScopeRow === 0 ? null : metadataToken(26, importScopeRow);
|
||||
const member = memberToken === null ? undefined : members.get(memberToken);
|
||||
imports.push({
|
||||
token: metadataToken(28, row),
|
||||
row_offset: cursor.start,
|
||||
mapping_flags: mappingFlags,
|
||||
mapping_flags_hex: flagsHex(mappingFlags),
|
||||
member_token: memberToken,
|
||||
member_kind: member?.kind ?? "unknown",
|
||||
member_name: member?.name ?? null,
|
||||
import_name: importName,
|
||||
import_scope_token: importScopeToken,
|
||||
import_scope_name:
|
||||
importScopeToken === null
|
||||
? null
|
||||
: (moduleNames.get(importScopeToken)?.name ?? null),
|
||||
no_mangle: (mappingFlags & 0x0001) !== 0,
|
||||
char_set: charSet(mappingFlags),
|
||||
call_convention: callConvention(mappingFlags),
|
||||
supports_last_error: (mappingFlags & 0x0040) !== 0,
|
||||
best_fit: bestFit(mappingFlags),
|
||||
throw_on_unmappable_char: throwOnUnmappable(mappingFlags),
|
||||
verification: "managed-declaration-only",
|
||||
});
|
||||
}, issues);
|
||||
}
|
||||
return imports;
|
||||
};
|
||||
@@ -275,6 +300,7 @@ export const nativeImplementations = (
|
||||
): readonly NativeImplementation[] => {
|
||||
const implementations: NativeImplementation[] = [];
|
||||
for (const method of methods) {
|
||||
if (method.kind !== "method") continue;
|
||||
const implFlags = method.implFlags ?? 0;
|
||||
const codeType = codeTypeFor(implFlags);
|
||||
const managedKind = managedKindFor(implFlags);
|
||||
@@ -344,21 +370,10 @@ const boundaryKind = (
|
||||
return "mixed-or-unknown";
|
||||
};
|
||||
|
||||
/** CLI header facets of a PE without an admitted CLI header. */
|
||||
export const NO_CLI_NATIVE: ManagedNativeBoundaryInspection["cli_native"] = {
|
||||
il_only: false,
|
||||
requires_32bit: false,
|
||||
strong_name_signed: false,
|
||||
native_entry_point: false,
|
||||
ready_to_run_signature: false,
|
||||
managed_native_header_rva: 0,
|
||||
managed_native_header_size: 0,
|
||||
};
|
||||
|
||||
export const cliNative = (
|
||||
pe: ManagedPeLayout,
|
||||
): ManagedNativeBoundaryInspection["cli_native"] => {
|
||||
if (pe.cli === null) return NO_CLI_NATIVE;
|
||||
if (pe.cli === null) return null;
|
||||
return {
|
||||
il_only: (pe.cli.flags & 0x0000_0001) !== 0,
|
||||
requires_32bit: (pe.cli.flags & 0x0000_0002) !== 0,
|
||||
@@ -379,11 +394,13 @@ export const nativeBoundarySummary = (
|
||||
>,
|
||||
): ManagedNativeBoundaryInspection["summary"] => ({
|
||||
...counts,
|
||||
ready_to_run: native.ready_to_run_signature,
|
||||
ready_to_run: native?.ready_to_run_signature ?? null,
|
||||
mixed_mode_or_native_header:
|
||||
native.managed_native_header_rva !== 0 ||
|
||||
native.managed_native_header_size !== 0 ||
|
||||
native.native_entry_point,
|
||||
native === null
|
||||
? null
|
||||
: native.managed_native_header_rva !== 0 ||
|
||||
native.managed_native_header_size !== 0 ||
|
||||
native.native_entry_point,
|
||||
});
|
||||
|
||||
interface BoundaryInspectionContext {
|
||||
|
||||
@@ -138,6 +138,61 @@ describe("managed native boundary coded indexes", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([0, 2])(
|
||||
"retains imports and reports invalid ModuleRef row %i as partial",
|
||||
(scopeRow) => {
|
||||
const bytes = buildManagedPeFixture({ pinvoke: {} });
|
||||
const original = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
const row = original.pinvoke_imports[0]?.row_offset;
|
||||
if (row === undefined) throw new Error("Fixture must contain an ImplMap");
|
||||
bytes.writeUInt16LE(scopeRow, row + 6);
|
||||
const result = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
expect(result.pinvoke_imports).toMatchObject([
|
||||
{
|
||||
import_name: "MessageBoxW",
|
||||
import_scope_token: null,
|
||||
import_scope_name: null,
|
||||
},
|
||||
]);
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
expect.objectContaining({
|
||||
scope: "metadata.ImplMap:0x1c000001",
|
||||
offset: row + 6,
|
||||
detail: expect.stringContaining("ImportScope"),
|
||||
}),
|
||||
],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("reports field imports as declarations without inventing method implementations", () => {
|
||||
const bytes = buildManagedPeFixture({ pinvoke: { memberForwardedRaw: 2 } });
|
||||
const result = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
expect(result.pinvoke_imports).toMatchObject([
|
||||
{
|
||||
member_kind: "field",
|
||||
member_token: "0x04000001",
|
||||
member_name: "counter",
|
||||
},
|
||||
]);
|
||||
expect(
|
||||
result.native_implementations.every((item) =>
|
||||
item.token.startsWith("0x06"),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
describe("managed native boundary inspection", () => {
|
||||
it("returns complete member inventories inline and reports unavailable metadata", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
@@ -207,7 +262,7 @@ describe("managed native boundary inspection", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("marks CLI header facets as defaults when the CLI header is unreadable", () => {
|
||||
it("reports unknown CLI header facts when the CLI header is unreadable", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
bytes.writeUInt32LE(0x7fff_0000, 0x84 + 20 + 96 + 14 * 8);
|
||||
const result = inspectManagedNativeBoundariesBytes(
|
||||
@@ -216,12 +271,69 @@ describe("managed native boundary inspection", () => {
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
metadata: { status: "malformed" },
|
||||
cli_native: { il_only: false },
|
||||
cli_native: null,
|
||||
summary: { ready_to_run: null, mixed_mode_or_native_header: null },
|
||||
coverage: { state: "unavailable", issues: [expect.anything()] },
|
||||
});
|
||||
expect(result.limitations).toContainEqual(
|
||||
expect.stringContaining("CLI header was not admitted"),
|
||||
});
|
||||
});
|
||||
|
||||
describe("independent managed metadata facets", () => {
|
||||
it("preserves identity, members, and imports when embedded resources are malformed", () => {
|
||||
const bytes = buildManagedPeFixture({ pinvoke: {}, readyToRun: true });
|
||||
bytes.writeUInt32LE(0x7fff_0000, 0x0200 + 24);
|
||||
const target = managedPeFixtureTarget(bytes);
|
||||
const artifact = inspectManagedArtifactBytes(bytes, target);
|
||||
const members = inspectManagedMembersBytes(bytes, target);
|
||||
const native = inspectManagedNativeBoundariesBytes(bytes, target);
|
||||
|
||||
for (const result of [artifact, members, native]) {
|
||||
expect(result.module?.mvid).toBeTruthy();
|
||||
expect(result.metadata.status).toBe("partial");
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
scope: "cli.resources",
|
||||
code: "invalid-directory",
|
||||
}),
|
||||
]),
|
||||
});
|
||||
}
|
||||
expect(members.methods[0]?.name).toBe("Main");
|
||||
expect(native.pinvoke_imports[0]?.import_name).toBe("MessageBoxW");
|
||||
expect(native.cli_native?.ready_to_run_signature).toBe(true);
|
||||
});
|
||||
|
||||
it("preserves native declarations when a module name cannot be decoded", () => {
|
||||
const bytes = buildManagedPeFixture({ pinvoke: {} });
|
||||
const original = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
const row = original.module_refs[0]?.row_offset;
|
||||
if (row === undefined) throw new Error("Fixture must contain a ModuleRef");
|
||||
bytes.writeUInt16LE(0xffff, row);
|
||||
const result = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.module_refs).toEqual([]);
|
||||
expect(result.pinvoke_imports).toMatchObject([
|
||||
{
|
||||
import_name: "MessageBoxW",
|
||||
import_scope_token: "0x1a000001",
|
||||
import_scope_name: null,
|
||||
},
|
||||
]);
|
||||
expect(result.native_implementations[0]?.name).toBe("Main");
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: expect.arrayContaining([
|
||||
expect.objectContaining({ code: "invalid-heap-index" }),
|
||||
]),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -250,7 +362,7 @@ describe("managed PE fixture layout", () => {
|
||||
ready_to_run_signature: true,
|
||||
managed_native_header_size: 4,
|
||||
});
|
||||
expect(boundaries.cli_native.managed_native_header_rva).toBeGreaterThan(
|
||||
expect(boundaries.cli_native?.managed_native_header_rva).toBeGreaterThan(
|
||||
0x2700,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
} from "../domain/managed/managedArtifact.js";
|
||||
import {
|
||||
readManagedMetadataInventory,
|
||||
type ManagedResourceDirectory,
|
||||
readManagedResourceDirectory,
|
||||
} from "./ManagedMetadataInventory.js";
|
||||
import {
|
||||
readManagedMetadataLayout,
|
||||
@@ -20,7 +20,6 @@ import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
|
||||
import {
|
||||
buildNativeBoundaryInspection,
|
||||
cliNative,
|
||||
NO_CLI_NATIVE,
|
||||
nativeBoundarySummary,
|
||||
nativeImplementations,
|
||||
parseFields,
|
||||
@@ -48,7 +47,6 @@ const emptyInspection = (
|
||||
readonly issues?: readonly ManagedParseIssue[];
|
||||
} = {},
|
||||
): ManagedNativeBoundaryInspection => {
|
||||
const header = native ?? NO_CLI_NATIVE;
|
||||
return managedNativeBoundaryInspectionSchema.parse({
|
||||
artifact: {
|
||||
path: target.path,
|
||||
@@ -67,11 +65,11 @@ const emptyInspection = (
|
||||
requires_artifact_sha256: target.sha256,
|
||||
requires_mvid: null,
|
||||
},
|
||||
cli_native: header,
|
||||
cli_native: native,
|
||||
module_refs: [],
|
||||
pinvoke_imports: [],
|
||||
native_implementations: [],
|
||||
summary: nativeBoundarySummary(header, {
|
||||
summary: nativeBoundarySummary(native, {
|
||||
module_ref_count: 0,
|
||||
pinvoke_import_count: 0,
|
||||
native_implementation_count: 0,
|
||||
@@ -79,11 +77,6 @@ const emptyInspection = (
|
||||
coverage: { state: "unavailable", issues },
|
||||
limitations: [
|
||||
"No CLI metadata was admitted; native boundary declarations are unavailable.",
|
||||
...(native === null && classification === "malformed"
|
||||
? [
|
||||
"The CLI header was not admitted, so cli_native and the summary's ready_to_run and mixed_mode_or_native_header values are defaults, not observations.",
|
||||
]
|
||||
: []),
|
||||
"Static inspection does not load or execute target code, so native export resolution is not performed.",
|
||||
],
|
||||
});
|
||||
@@ -93,6 +86,7 @@ const readBoundaryInventory = (
|
||||
bytes: Buffer,
|
||||
pe: ManagedPeLayout,
|
||||
cli: NonNullable<ManagedPeLayout["cli"]>,
|
||||
issues: ManagedParseIssue[],
|
||||
): {
|
||||
readonly layout: ManagedMetadataLayout;
|
||||
readonly inventory: Inventory;
|
||||
@@ -107,18 +101,10 @@ const readBoundaryInventory = (
|
||||
metadataOffset,
|
||||
cli.metadata.size,
|
||||
);
|
||||
const resourceDirectory: ManagedResourceDirectory = {
|
||||
offset: pe.rvaToOffset(
|
||||
cli.resources.rva,
|
||||
cli.resources.size,
|
||||
"cli.resources",
|
||||
),
|
||||
size: cli.resources.size,
|
||||
};
|
||||
const inventory = readManagedMetadataInventory(
|
||||
bytes,
|
||||
layout,
|
||||
resourceDirectory,
|
||||
readManagedResourceDirectory(pe, issues),
|
||||
);
|
||||
return { layout, inventory };
|
||||
};
|
||||
@@ -144,10 +130,11 @@ export const inspectManagedNativeBoundariesBytes = (
|
||||
pe.cliDirectoryPresent ? "malformed" : "not-managed",
|
||||
{ issues: pe.cliIssue === null ? [] : [pe.cliIssue] },
|
||||
);
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
let layout: ManagedMetadataLayout;
|
||||
let inventory: Inventory;
|
||||
try {
|
||||
({ layout, inventory } = readBoundaryInventory(bytes, pe, pe.cli));
|
||||
({ layout, inventory } = readBoundaryInventory(bytes, pe, pe.cli, issues));
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||
return emptyInspection(target, bytes, "malformed", {
|
||||
@@ -156,11 +143,11 @@ export const inspectManagedNativeBoundariesBytes = (
|
||||
});
|
||||
}
|
||||
const heapExtent = Math.max(layout.strings.size, layout.blob.size);
|
||||
const issues = [...inventory.issues];
|
||||
const moduleRefs = parseModuleRefs(bytes, layout, heapExtent);
|
||||
issues.push(...inventory.issues);
|
||||
const moduleRefs = parseModuleRefs(bytes, layout, heapExtent, issues);
|
||||
const members = new Map([
|
||||
...parseFields(bytes, layout, heapExtent),
|
||||
...parseMethods(bytes, layout, heapExtent),
|
||||
...parseFields(bytes, layout, heapExtent, issues),
|
||||
...parseMethods(bytes, layout, heapExtent, issues),
|
||||
]);
|
||||
const imports = parseImplMaps({
|
||||
bytes,
|
||||
|
||||
@@ -18,3 +18,24 @@ export const managedFailure = (
|
||||
offset: number | null = null,
|
||||
): ManagedReaderFailure =>
|
||||
new ManagedReaderFailure({ code, scope, offset, detail });
|
||||
|
||||
/** Retain one malformed item's location while allowing independent items to be read. */
|
||||
export const readManagedValue = <Value>(
|
||||
operation: () => Value,
|
||||
issues: ManagedParseIssue[],
|
||||
): Value | undefined => {
|
||||
try {
|
||||
return operation();
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||
issues.push(cause.issue);
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
/** Preserve issue order while collapsing the same malformed producer fact across facets. */
|
||||
export const uniqueManagedParseIssues = (
|
||||
issues: readonly ManagedParseIssue[],
|
||||
): readonly ManagedParseIssue[] => [
|
||||
...new Map(issues.map((issue) => [JSON.stringify(issue), issue])).values(),
|
||||
];
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
managedNativeBoundaryInputSchema,
|
||||
type ManagedToolName,
|
||||
} from "../contracts/managed/managedToolContracts.js";
|
||||
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
@@ -36,7 +37,7 @@ import type {
|
||||
ManagedMemberInspection,
|
||||
ManagedNativeBoundaryInspection,
|
||||
} from "../domain/managed/managedArtifact.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { inspectManagedArtifactBytes } from "./ManagedArtifactInspector.js";
|
||||
import { inspectManagedMembersBytes } from "./ManagedMemberInspector.js";
|
||||
import { inspectManagedNativeBoundariesBytes } from "./ManagedNativeBoundaryInspector.js";
|
||||
@@ -126,9 +127,9 @@ class ManagedStaticClient implements AnalysisClient {
|
||||
}
|
||||
}
|
||||
|
||||
close(): Promise<void> {
|
||||
close(): Promise<Result<null, AnalysisError>> {
|
||||
this.#snapshotBytes = undefined;
|
||||
return Promise.resolve();
|
||||
return Promise.resolve(ok(null));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user