fix: consolidate boundary ownership and cleanup failure paths (#477)
This commit is contained in:
@@ -1,16 +1,15 @@
|
||||
import type { ManagedMemberInspection } from "../domain/managedArtifact.js";
|
||||
import {
|
||||
metadataRowOffset,
|
||||
type ManagedMetadataLayout,
|
||||
type MetadataTableLayout,
|
||||
} from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
MetadataRowCursor,
|
||||
metadataRowCursor,
|
||||
metadataCodedToken,
|
||||
metadataToken,
|
||||
readMetadataString,
|
||||
sha256Bytes,
|
||||
} from "./ManagedMetadataHeaps.js";
|
||||
import { managedFailure } from "./ManagedReaderFailure.js";
|
||||
|
||||
export type ManagedPage<Item> = {
|
||||
readonly items: readonly Item[];
|
||||
@@ -117,41 +116,6 @@ export const page = <Item>(
|
||||
};
|
||||
};
|
||||
|
||||
export const rowCursor = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
table: number,
|
||||
row: number,
|
||||
): MetadataRowCursor => {
|
||||
const descriptor = layout.table(table);
|
||||
const start = metadataRowOffset(layout, table, row);
|
||||
if (descriptor === undefined)
|
||||
throw managedFailure(
|
||||
"invalid-row",
|
||||
`metadata.${String(table)}`,
|
||||
"Metadata table is absent",
|
||||
start,
|
||||
);
|
||||
return new MetadataRowCursor(
|
||||
bytes,
|
||||
start,
|
||||
start + descriptor.rowSize,
|
||||
`metadata.${descriptor.name}`,
|
||||
);
|
||||
};
|
||||
|
||||
export const codedToken = (
|
||||
raw: number,
|
||||
bits: number,
|
||||
tables: readonly (number | undefined)[],
|
||||
): string | null => {
|
||||
if (raw === 0) return null;
|
||||
const tag = raw & (2 ** bits - 1);
|
||||
const row = Math.floor(raw / 2 ** bits);
|
||||
const table = tables[tag];
|
||||
return table === undefined || row === 0 ? null : metadataToken(table, row);
|
||||
};
|
||||
|
||||
const rowRange = (
|
||||
table: MetadataTableLayout | undefined,
|
||||
start: number,
|
||||
@@ -179,7 +143,7 @@ const readTypeRange = (
|
||||
): TypeRange => {
|
||||
const methods = layout.table(6);
|
||||
const fields = layout.table(4);
|
||||
const cursor = rowCursor(bytes, layout, 2, row);
|
||||
const cursor = metadataRowCursor(bytes, layout, 2, row);
|
||||
cursor.readUInt32();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
@@ -200,7 +164,7 @@ const readTypeRange = (
|
||||
let nextMethod = (methods?.rowCount ?? 0) + 1;
|
||||
const typeTable = layout.table(2);
|
||||
if (typeTable !== undefined && row < typeTable.rowCount) {
|
||||
const next = rowCursor(bytes, layout, 2, row + 1);
|
||||
const next = metadataRowCursor(bytes, layout, 2, row + 1);
|
||||
next.readUInt32();
|
||||
next.readIndex(layout.stringIndexSize);
|
||||
next.readIndex(layout.stringIndexSize);
|
||||
@@ -255,7 +219,7 @@ export const parseTypes = (
|
||||
const methods = layout.table(6);
|
||||
const items: ManagedType[] = [];
|
||||
for (let row = 1; row <= (typeTable?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor(bytes, layout, 2, row);
|
||||
const cursor = metadataRowCursor(bytes, layout, 2, row);
|
||||
const flags = cursor.readUInt32();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
@@ -286,7 +250,7 @@ export const parseTypes = (
|
||||
name,
|
||||
full_name: fullName(namespace, name),
|
||||
flags,
|
||||
extends_token: codedToken(extendsRaw, 2, [2, 1, 27]),
|
||||
extends_token: metadataCodedToken(extendsRaw, 2, [2, 1, 27]),
|
||||
field_list: {
|
||||
first_row: fieldRange.first,
|
||||
last_row: fieldRange.last,
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import type { ManagedPeLayout } from "./ManagedPeReader.js";
|
||||
import type { ManagedMetadataLayout } from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
codedToken,
|
||||
declaringType,
|
||||
rowCursor,
|
||||
signature,
|
||||
type FieldCore,
|
||||
type ManagedCallEdge,
|
||||
@@ -18,6 +16,8 @@ import {
|
||||
} from "./ManagedMemberInspectorCore.js";
|
||||
import {
|
||||
metadataToken,
|
||||
metadataRowCursor,
|
||||
metadataCodedToken,
|
||||
readMetadataBlob,
|
||||
readMetadataString,
|
||||
} from "./ManagedMetadataHeaps.js";
|
||||
@@ -36,7 +36,7 @@ export const parseFields = (
|
||||
const core = new Map<string, FieldCore>();
|
||||
const table = layout.table(4);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor(bytes, layout, 4, row);
|
||||
const cursor = metadataRowCursor(bytes, layout, 4, row);
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
@@ -78,7 +78,7 @@ export const parseMemberRefs = (
|
||||
const core = new Map<string, MemberRefCore>();
|
||||
const table = layout.table(10);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor(bytes, layout, 10, row);
|
||||
const cursor = metadataRowCursor(bytes, layout, 10, row);
|
||||
const parentRaw = cursor.readIndex(
|
||||
layout.codedIndexSize("MemberRefParent"),
|
||||
);
|
||||
@@ -98,7 +98,7 @@ export const parseMemberRefs = (
|
||||
refs.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
parent_token: codedToken(parentRaw, 3, [2, 1, 26, 6, 27]),
|
||||
parent_token: metadataCodedToken(parentRaw, 3, [2, 1, 26, 6, 27]),
|
||||
name,
|
||||
signature: signature(sig),
|
||||
});
|
||||
@@ -131,7 +131,7 @@ export const parseMethods = ({
|
||||
const core = new Map<string, MethodCore>();
|
||||
const table = layout.table(6);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor(bytes, layout, 6, row);
|
||||
const cursor = metadataRowCursor(bytes, layout, 6, row);
|
||||
const rva = cursor.readUInt32();
|
||||
const implFlags = cursor.readUInt16();
|
||||
const flags = cursor.readUInt16();
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import type { ManagedMetadataLayout } from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
metadataRowOffset,
|
||||
type ManagedMetadataLayout,
|
||||
} from "./ManagedMetadataLayout.js";
|
||||
import { managedFailure } from "./ManagedReaderFailure.js";
|
||||
|
||||
const requireRange = (
|
||||
@@ -219,3 +222,40 @@ export const strongNameToken = (publicKey: Buffer): string =>
|
||||
Buffer.from(createHash("sha1").update(publicKey).digest().subarray(-8))
|
||||
.reverse()
|
||||
.toString("hex");
|
||||
|
||||
/** Create a cursor bounded to one admitted metadata table row. */
|
||||
export const metadataRowCursor = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
table: number,
|
||||
row: number,
|
||||
): MetadataRowCursor => {
|
||||
const descriptor = layout.table(table);
|
||||
const start = metadataRowOffset(layout, table, row);
|
||||
if (descriptor === undefined)
|
||||
throw managedFailure(
|
||||
"invalid-row",
|
||||
`metadata.${String(table)}`,
|
||||
"Metadata table is absent",
|
||||
start,
|
||||
);
|
||||
return new MetadataRowCursor(
|
||||
bytes,
|
||||
start,
|
||||
start + descriptor.rowSize,
|
||||
`metadata.${descriptor.name}`,
|
||||
);
|
||||
};
|
||||
|
||||
/** Decode a metadata coded index without signed 32-bit coercion. */
|
||||
export const metadataCodedToken = (
|
||||
raw: number,
|
||||
bits: number,
|
||||
tables: readonly (number | undefined)[],
|
||||
): string | null => {
|
||||
if (raw === 0) return null;
|
||||
const tag = raw & (2 ** bits - 1);
|
||||
const row = Math.floor(raw / 2 ** bits);
|
||||
const table = tables[tag];
|
||||
return table === undefined || row === 0 ? null : metadataToken(table, row);
|
||||
};
|
||||
|
||||
@@ -7,7 +7,8 @@ import {
|
||||
type ManagedMetadataLayout,
|
||||
} from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
MetadataRowCursor,
|
||||
metadataRowCursor,
|
||||
metadataCodedToken,
|
||||
metadataToken,
|
||||
readMetadataBlob,
|
||||
readMetadataGuid,
|
||||
@@ -25,36 +26,6 @@ type AssemblyReference =
|
||||
type ManagedResource = ManagedArtifactInspection["resources"]["items"][number];
|
||||
type CustomAttribute = ManagedArtifactInspection["attributes"]["items"][number];
|
||||
|
||||
interface RowCursorContext {
|
||||
readonly bytes: Buffer;
|
||||
readonly layout: ManagedMetadataLayout;
|
||||
readonly table: number;
|
||||
readonly row: number;
|
||||
}
|
||||
|
||||
const rowCursor = ({
|
||||
bytes,
|
||||
layout,
|
||||
table,
|
||||
row,
|
||||
}: RowCursorContext): MetadataRowCursor => {
|
||||
const descriptor = layout.table(table);
|
||||
const start = metadataRowOffset(layout, table, row);
|
||||
if (descriptor === undefined)
|
||||
throw managedFailure(
|
||||
"invalid-row",
|
||||
`metadata.${String(table)}`,
|
||||
"Metadata table is absent",
|
||||
start,
|
||||
);
|
||||
return new MetadataRowCursor(
|
||||
bytes,
|
||||
start,
|
||||
start + descriptor.rowSize,
|
||||
`metadata.${descriptor.name}`,
|
||||
);
|
||||
};
|
||||
|
||||
const publicKeyIdentity = (
|
||||
bytes: Buffer,
|
||||
kind: "public-key" | "public-key-token",
|
||||
@@ -79,7 +50,7 @@ export const readModule = (
|
||||
): ModuleIdentity | null => {
|
||||
const table = layout.table(0);
|
||||
if (table === undefined || table.rowCount === 0) return null;
|
||||
const cursor = rowCursor({ bytes, layout, table: 0, row: 1 });
|
||||
const cursor = metadataRowCursor(bytes, layout, 0, 1);
|
||||
const generation = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
@@ -120,7 +91,7 @@ export const readAssembly = (
|
||||
): AssemblyIdentity | null => {
|
||||
const table = layout.table(32);
|
||||
if (table === undefined || table.rowCount === 0) return null;
|
||||
const cursor = rowCursor({ bytes, layout, table: 32, row: 1 });
|
||||
const cursor = metadataRowCursor(bytes, layout, 32, 1);
|
||||
const hashAlgorithm = cursor.readUInt32();
|
||||
const version = [
|
||||
cursor.readUInt16(),
|
||||
@@ -168,7 +139,7 @@ export const readAssemblyReference = (
|
||||
row: number,
|
||||
maxBytes: number,
|
||||
): AssemblyReference => {
|
||||
const cursor = rowCursor({ bytes, layout, table: 35, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 35, row);
|
||||
const version = [
|
||||
cursor.readUInt16(),
|
||||
cursor.readUInt16(),
|
||||
@@ -216,18 +187,6 @@ export const readAssemblyReference = (
|
||||
};
|
||||
};
|
||||
|
||||
const codedToken = (
|
||||
raw: number,
|
||||
bits: number,
|
||||
tables: readonly (number | undefined)[],
|
||||
): string | null => {
|
||||
if (raw === 0) return null;
|
||||
const tag = raw & (2 ** bits - 1);
|
||||
const row = Math.floor(raw / 2 ** bits);
|
||||
const table = tables[tag];
|
||||
return table === undefined || row === 0 ? null : metadataToken(table, row);
|
||||
};
|
||||
|
||||
interface ReadTypeNameContext {
|
||||
readonly bytes: Buffer;
|
||||
readonly layout: ManagedMetadataLayout;
|
||||
@@ -246,7 +205,7 @@ const readTypeName = ({
|
||||
const descriptor = layout.table(table);
|
||||
if (descriptor === undefined || row < 1 || row > descriptor.rowCount)
|
||||
return null;
|
||||
const cursor = rowCursor({ bytes, layout, table, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, table, row);
|
||||
if (table === 1) cursor.readIndex(layout.codedIndexSize("ResolutionScope"));
|
||||
else cursor.readUInt32();
|
||||
const name = readMetadataString(
|
||||
@@ -274,7 +233,7 @@ const declaringTypeForMethod = (
|
||||
const methods = layout.table(6);
|
||||
if (types === undefined || methods === undefined) return null;
|
||||
for (let row = 1; row <= types.rowCount; row += 1) {
|
||||
const cursor = rowCursor({ bytes, layout, table: 2, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 2, row);
|
||||
cursor.readUInt32();
|
||||
const nameIndex = cursor.readIndex(layout.stringIndexSize);
|
||||
const namespaceIndex = cursor.readIndex(layout.stringIndexSize);
|
||||
@@ -303,7 +262,7 @@ const methodListForType = (
|
||||
layout: ManagedMetadataLayout,
|
||||
row: number,
|
||||
): number => {
|
||||
const cursor = rowCursor({ bytes, layout, table: 2, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 2, row);
|
||||
cursor.readUInt32();
|
||||
cursor.readIndex(layout.stringIndexSize);
|
||||
cursor.readIndex(layout.stringIndexSize);
|
||||
@@ -325,7 +284,7 @@ const attributeTypeName = (
|
||||
if (tag !== 3) return null;
|
||||
const memberRefs = layout.table(10);
|
||||
if (memberRefs === undefined || row > memberRefs.rowCount) return null;
|
||||
const cursor = rowCursor({ bytes, layout, table: 10, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 10, row);
|
||||
const parent = cursor.readIndex(layout.codedIndexSize("MemberRefParent"));
|
||||
const parentTag = parent & 7;
|
||||
const parentRow = Math.floor(parent / 8);
|
||||
@@ -389,7 +348,7 @@ export const readCustomAttribute = (
|
||||
row: number,
|
||||
maxBytes: number,
|
||||
): CustomAttribute => {
|
||||
const cursor = rowCursor({ bytes, layout, table: 12, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 12, row);
|
||||
const parentRaw = cursor.readIndex(
|
||||
layout.codedIndexSize("HasCustomAttribute"),
|
||||
);
|
||||
@@ -402,7 +361,7 @@ export const readCustomAttribute = (
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
maxBytes,
|
||||
);
|
||||
const parent = codedToken(
|
||||
const parent = metadataCodedToken(
|
||||
parentRaw,
|
||||
5,
|
||||
[
|
||||
@@ -420,7 +379,12 @@ export const readCustomAttribute = (
|
||||
const typeName = attributeTypeName(bytes, layout, typeRaw, maxBytes);
|
||||
return {
|
||||
parent_token: parent,
|
||||
constructor_token: codedToken(typeRaw, 3, [undefined, undefined, 6, 10]),
|
||||
constructor_token: metadataCodedToken(typeRaw, 3, [
|
||||
undefined,
|
||||
undefined,
|
||||
6,
|
||||
10,
|
||||
]),
|
||||
type_name: typeName,
|
||||
value_length: value.length,
|
||||
value_sha256: sha256Bytes(value),
|
||||
@@ -450,7 +414,7 @@ export const readResource = ({
|
||||
directory,
|
||||
issues,
|
||||
}: ReadResourceContext): ManagedResource => {
|
||||
const cursor = rowCursor({ bytes, layout, table: 40, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 40, row);
|
||||
const declaredOffset = cursor.readUInt32();
|
||||
const flags = cursor.readUInt32();
|
||||
const name = readMetadataString(
|
||||
@@ -462,7 +426,11 @@ export const readResource = ({
|
||||
const implementationRaw = cursor.readIndex(
|
||||
layout.codedIndexSize("Implementation"),
|
||||
);
|
||||
const implementationToken = codedToken(implementationRaw, 2, [38, 35, 39]);
|
||||
const implementationToken = metadataCodedToken(
|
||||
implementationRaw,
|
||||
2,
|
||||
[38, 35, 39],
|
||||
);
|
||||
let dataLength: number | null = null;
|
||||
let dataSha256: string | null = null;
|
||||
if (implementationRaw === 0) {
|
||||
|
||||
@@ -4,18 +4,15 @@ import {
|
||||
type ManagedNativeBoundaryInspection,
|
||||
type ManagedParseIssue,
|
||||
} from "../domain/managedArtifact.js";
|
||||
import { type ManagedMetadataLayout } from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
metadataRowOffset,
|
||||
type ManagedMetadataLayout,
|
||||
} from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
MetadataRowCursor,
|
||||
metadataRowCursor,
|
||||
metadataCodedToken,
|
||||
metadataToken,
|
||||
readMetadataString,
|
||||
} from "./ManagedMetadataHeaps.js";
|
||||
import { managedTableRowCounts } from "./ManagedMetadataInventory.js";
|
||||
import type { ManagedMetadataInventory } from "./ManagedMetadataInventory.js";
|
||||
import { managedFailure } from "./ManagedReaderFailure.js";
|
||||
import type { ManagedNativeBoundaryInspectionLimits } from "./ManagedNativeBoundaryInspector.js";
|
||||
import type { ManagedPeLayout } from "./ManagedPeReader.js";
|
||||
|
||||
@@ -46,48 +43,6 @@ interface MemberCore {
|
||||
readonly rva: number | null;
|
||||
}
|
||||
|
||||
interface RowCursorContext {
|
||||
readonly bytes: Buffer;
|
||||
readonly layout: ManagedMetadataLayout;
|
||||
readonly table: number;
|
||||
readonly row: number;
|
||||
}
|
||||
|
||||
const rowCursor = ({
|
||||
bytes,
|
||||
layout,
|
||||
table,
|
||||
row,
|
||||
}: RowCursorContext): MetadataRowCursor => {
|
||||
const descriptor = layout.table(table);
|
||||
const start = metadataRowOffset(layout, table, row);
|
||||
if (descriptor === undefined)
|
||||
throw managedFailure(
|
||||
"invalid-row",
|
||||
`metadata.${String(table)}`,
|
||||
"Metadata table is absent",
|
||||
start,
|
||||
);
|
||||
return new MetadataRowCursor(
|
||||
bytes,
|
||||
start,
|
||||
start + descriptor.rowSize,
|
||||
`metadata.${descriptor.name}`,
|
||||
);
|
||||
};
|
||||
|
||||
const codedToken = (
|
||||
raw: number,
|
||||
tagBits: number,
|
||||
tables: readonly number[],
|
||||
): string | null => {
|
||||
const tagMask = (1 << tagBits) - 1;
|
||||
const table = tables[raw & tagMask];
|
||||
const row = raw >> tagBits;
|
||||
if (table === undefined || row === 0) return null;
|
||||
return metadataToken(table, row);
|
||||
};
|
||||
|
||||
const flagsHex = (value: number): string =>
|
||||
`0x${value.toString(16).padStart(4, "0")}`;
|
||||
|
||||
@@ -117,7 +72,7 @@ export const parseModuleRefs = (
|
||||
const refs: ModuleRef[] = [];
|
||||
const table = layout.table(26);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor({ bytes, layout, table: 26, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 26, row);
|
||||
refs.push({
|
||||
token: metadataToken(26, row),
|
||||
row_offset: cursor.start,
|
||||
@@ -140,7 +95,7 @@ export const parseFields = (
|
||||
const fields = new Map<string, MemberCore>();
|
||||
const table = layout.table(4);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor({ bytes, layout, table: 4, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 4, row);
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
@@ -171,7 +126,7 @@ export const parseMethods = (
|
||||
const methods = new Map<string, MemberCore>();
|
||||
const table = layout.table(6);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor({ bytes, layout, table: 6, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 6, row);
|
||||
const rva = cursor.readUInt32();
|
||||
const implFlags = cursor.readUInt16();
|
||||
const flags = cursor.readUInt16();
|
||||
@@ -216,9 +171,9 @@ export const parseImplMaps = ({
|
||||
const imports: NativeImport[] = [];
|
||||
const table = layout.table(28);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor({ bytes, layout, table: 28, row });
|
||||
const cursor = metadataRowCursor(bytes, layout, 28, row);
|
||||
const mappingFlags = cursor.readUInt16();
|
||||
const memberToken = codedToken(
|
||||
const memberToken = metadataCodedToken(
|
||||
cursor.readIndex(layout.codedIndexSize("MemberForwarded")),
|
||||
1,
|
||||
[4, 6],
|
||||
|
||||
@@ -234,7 +234,7 @@ const limitationsFor = (operation: ManagedToolName): readonly string[] =>
|
||||
"It never loads the target assembly, resolves dependencies through a CLR, decompiles C#, or executes target code.",
|
||||
]
|
||||
: [
|
||||
"This capability decodes bounded PE/CLI metadata members, signatures, and file-backed method bodies; decompiled C# and cross-build matching are separate future contracts.",
|
||||
"This capability decodes bounded PE/CLI metadata members, signatures, and file-backed method bodies; decompiled C# reconstruction and cross-build member comparison are separate operations.",
|
||||
"It never loads the target assembly, resolves dependencies through a CLR, decompiles C#, or executes target code.",
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user