fix(dotnet): validate managed metadata and CIL boundaries (#938)

* perf(dotnet): reuse decoded metadata rows

* test(dotnet): preserve expanded fixture metadata

* style(dotnet): format metadata inventory

* fix(dotnet): bound method bodies to PE section data

* fix(dotnet): derive fixture metadata index widths

* fix(dotnet): validate managed metadata bounds

* test(dotnet): cover large metadata ReadyToRun layout

* fix(dotnet): validate exception instruction boundaries

* fix(dotnet): report invalid metadata coded indexes

* fix(dotnet): clarify MemberForwarded null diagnostics

* fix(dotnet): honor complete CIL and EH boundaries

* fix(dotnet): validate metadata identity rows and offsets
This commit is contained in:
morluto
2026-10-08 00:17:26 +08:00
committed by GitHub
parent 35a4e18d30
commit bda4ada7fb
18 changed files with 1825 additions and 223 deletions
+109
View File
@@ -111,6 +111,115 @@ describe("managed member inspection", () => {
});
});
describe("managed member reference bounds", () => {
it("retains members and reports an out-of-range TypeDef coded index as partial", () => {
const bytes = buildManagedPeFixture({ extendsRaw: 12 });
const result = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes),
);
expect(result.types[0]).toMatchObject({
token: "0x02000001",
extends_token: null,
});
expect(result.fields).toHaveLength(1);
expect(result.methods).toHaveLength(1);
expect(result.metadata.status).toBe("partial");
expect(result.coverage).toMatchObject({
state: "partial",
issues: [
{
code: "invalid-row",
scope: "metadata.TypeDef:0x02000001",
detail: expect.stringContaining(
"TypeDef Extends coded index 0xc is invalid: coded index selects row 3 in table 2, which has 1 rows",
),
},
],
});
expect(result.coverage.issues[0]?.offset).toBe(
(result.types[0]?.row_offset ?? 0) + 8,
);
});
it("retains a MemberRef with an out-of-range parent as an unknown partial reference", () => {
const bytes = buildManagedPeFixture({ memberRefParentRaw: 40 });
const result = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes),
);
expect(result.member_refs[0]).toMatchObject({
token: "0x0a000001",
parent_token: null,
name: ".ctor",
});
expect(result.coverage).toMatchObject({
state: "partial",
issues: [
{
code: "invalid-row",
scope: "metadata.MemberRef:0x0a000001",
detail: expect.stringContaining(
"MemberRef parent coded index 0x28 is invalid: coded index selects row 5 in table 2, which has 1 rows",
),
},
],
});
expect(result.coverage.issues[0]?.offset).toBe(
result.member_refs[0]?.row_offset,
);
});
it("reports a null required MemberRef parent without dropping the reference", () => {
const bytes = buildManagedPeFixture({ memberRefParentRaw: 0 });
const result = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes),
);
expect(result.member_refs[0]).toMatchObject({
token: "0x0a000001",
parent_token: null,
});
expect(result.coverage).toMatchObject({
state: "partial",
issues: [
expect.objectContaining({
code: "invalid-row",
detail:
"MemberRef parent coded index 0x0 is null, but the Class column must reference a row",
}),
],
});
});
it("decodes deeply nested supported pointer signatures without recursion", () => {
const depth = 8_000;
const bytes = buildManagedPeFixture({
fieldSignature: Buffer.concat([
Buffer.from([0x06]),
Buffer.alloc(depth, 0x0f),
Buffer.from([0x08]),
]),
});
const result = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes),
);
expect(result.fields[0]?.signature).toMatchObject({
kind: "field",
parse_status: "decoded",
field_type: `${"i4"}${"*".repeat(depth)}`,
issue: null,
});
expect(result.methods[0]?.body.status).toBe("present");
expect(result.coverage).toMatchObject({ state: "complete", issues: [] });
});
});
it.each([
Buffer.alloc(0),
Buffer.from([0]),