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:
@@ -1,5 +1,8 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { inspectManagedArtifactBytes } from "./ManagedArtifactInspector.js";
|
||||
import { inspectManagedMembersBytes } from "./ManagedMemberInspector.js";
|
||||
import { inspectManagedNativeBoundariesBytes } from "./ManagedNativeBoundaryInspector.js";
|
||||
import {
|
||||
@@ -68,11 +71,74 @@ describe("managed native boundaries", () => {
|
||||
ready_to_run: true,
|
||||
mixed_mode_or_native_header: true,
|
||||
});
|
||||
expect(result.coverage).toMatchObject({ state: "complete", issues: [] });
|
||||
expect(result.limitations).toContain(
|
||||
"P/Invoke rows prove managed import declarations only; this inspection does not verify that a native library, export, thunk, or provider-qualified function exists.",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed native boundary coded indexes", () => {
|
||||
it("retains P/Invoke imports with an invalid MemberForwarded index as partial evidence", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
pinvoke: { memberForwardedRaw: 5 },
|
||||
});
|
||||
const result = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.pinvoke_imports).toMatchObject([
|
||||
{
|
||||
member_token: null,
|
||||
member_kind: "unknown",
|
||||
member_name: null,
|
||||
import_name: "MessageBoxW",
|
||||
},
|
||||
]);
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
expect.objectContaining({
|
||||
code: "invalid-row",
|
||||
scope: "metadata.ImplMap:0x1c000001",
|
||||
detail: expect.stringContaining(
|
||||
"coded index 0x5 is invalid: coded index selects row 2 in table 6, which has 1 rows",
|
||||
),
|
||||
}),
|
||||
],
|
||||
});
|
||||
expect(result.coverage.issues[0]?.offset).toBe(
|
||||
(result.pinvoke_imports[0]?.row_offset ?? 0) + 2,
|
||||
);
|
||||
});
|
||||
|
||||
it("reports null for the required MemberForwarded index", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
pinvoke: { memberForwardedRaw: 0 },
|
||||
});
|
||||
const result = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.pinvoke_imports).toMatchObject([
|
||||
{ member_token: null, member_kind: "unknown" },
|
||||
]);
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
expect.objectContaining({
|
||||
code: "invalid-row",
|
||||
detail:
|
||||
"ImplMap MemberForwarded coded index 0x0 is null, but the column must reference a Field or MethodDef row",
|
||||
}),
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed native boundary inspection", () => {
|
||||
it("returns complete member inventories inline and reports unavailable metadata", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const paged = inspectManagedMembersBytes(
|
||||
@@ -158,3 +224,34 @@ describe("managed native boundaries", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed PE fixture layout", () => {
|
||||
it("keeps ReadyToRun metadata separate from expanded metadata and resources", () => {
|
||||
const resourceData = Buffer.alloc(64 * 1024, 0x5a);
|
||||
const bytes = buildManagedPeFixture({
|
||||
readyToRun: true,
|
||||
references: Array.from(
|
||||
{ length: 2_200 },
|
||||
(_, index) => `Reference.${String(index).padStart(4, "0")}`,
|
||||
),
|
||||
resourceData,
|
||||
});
|
||||
const target = managedPeFixtureTarget(bytes);
|
||||
const artifact = inspectManagedArtifactBytes(bytes, target);
|
||||
const boundaries = inspectManagedNativeBoundariesBytes(bytes, target);
|
||||
|
||||
expect(artifact.coverage).toMatchObject({ state: "complete", issues: [] });
|
||||
expect(artifact.references).toHaveLength(2_200);
|
||||
expect(artifact.resources[0]).toMatchObject({
|
||||
data_length: resourceData.length,
|
||||
data_sha256: createHash("sha256").update(resourceData).digest("hex"),
|
||||
});
|
||||
expect(boundaries.cli_native).toMatchObject({
|
||||
ready_to_run_signature: true,
|
||||
managed_native_header_size: 4,
|
||||
});
|
||||
expect(boundaries.cli_native.managed_native_header_rva).toBeGreaterThan(
|
||||
0x2700,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user