fix(managed): charge retained member facts instead of decoding bounds (#1768)
* fix(managed): charge retained member facts instead of decoding bounds inspect_managed_members returned no types or methods for ordinary assemblies: Newtonsoft.Json 13.0.3 (netstandard2.0, 705 KB) exceeded the 64 MiB representation budget. Each method body reserved 256 bytes per IL byte and each signature 256 bytes per blob byte before decoding, and those worst-case bounds were never returned, so the budget scaled with total IL size rather than with the retained output. The final projection admission also charged every decoded string at six bytes per character and every scalar at 32 bytes, about eight times its JSON size. Bound IL, exception-clause and signature decoding with a transient admission that does not consume capacity, then reserve the decoded body or signature that is actually retained. Projection admission now charges the exact UTF-8 JSON bytes of decoded strings, keys and scalars; escaped and multibyte text still counts at its encoded size. Newtonsoft.Json now yields 496 types, 4185 methods and 18846 call edges. Assemblies whose retained facts exceed the budget, such as the 2.3 MB System.Private.ServiceModel 4.7.0, remain unavailable. * fix(managed): bound concurrent decoding and count JSON bytes in place Review follow-up. A method's IL bound and its exception-section bounds were each checked against the same remaining capacity although their bookkeeping is live at the same time, so 40 MiB of IL plus 40 MiB of exception clauses passed a 64 MiB budget. Transient admissions now accumulate until the method body or signature is built, and reserve respects bounds that are still live. Projection admission also serialized each decoded string to measure it, which could allocate about six times a near-limit string before the check. It now counts the exact escaped UTF-8 bytes without building the JSON text. * fix(managed): admit derived call and field edges as they are built Review follow-up. Released IL bounds no longer stand in for the call and field-access records that edges() derives from anchors. Each record is now admitted before the graph grows further, so a crafted assembly cannot materialize the whole derived graph before the representation guard fails. * fix(managed): admit derived caller names before building them Review follow-up. The caller name of call and field-access records was interpolated per anchor before any admission saw it. It is now built once per method, after its exact JSON size, counted from the type and member names, has been admitted.
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { inspectManagedMembersBytes } from "./ManagedMemberInspector.js";
|
||||
import { ManagedDecodeBudget } from "./ManagedDecodeBudget.js";
|
||||
import { edges } from "./ManagedMemberRows.js";
|
||||
import type { ManagedMethod } from "./ManagedMemberInspectorCore.js";
|
||||
import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
|
||||
|
||||
import {
|
||||
buildManagedPeFixture,
|
||||
managedPeFixtureTarget,
|
||||
@@ -133,6 +138,128 @@ describe("managed member inspection", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed member decoding budget", () => {
|
||||
it("charges decoded output rather than accumulated decoding bounds", () => {
|
||||
// Each bound fits alone, but 256 bytes per IL and signature byte together
|
||||
// exceed the 64 MiB budget although the retained members are small.
|
||||
const il = Buffer.concat([Buffer.alloc(200_000), Buffer.from([0x2a])]);
|
||||
const header = Buffer.alloc(12);
|
||||
header.writeUInt16LE(0x3003, 0);
|
||||
header.writeUInt16LE(8, 2);
|
||||
header.writeUInt32LE(il.length, 4);
|
||||
const bytes = buildManagedPeFixture({
|
||||
ilBody: Buffer.concat([header, il]),
|
||||
methodSignature: Buffer.concat([
|
||||
Buffer.from([0x00, 0x00, 0x01]),
|
||||
Buffer.alloc(80_000),
|
||||
]),
|
||||
});
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.coverage.state).not.toBe("unavailable");
|
||||
expect(result.methods[0]?.body).toMatchObject({
|
||||
status: "present",
|
||||
il_size: il.length,
|
||||
opcode_counts: { nop: 200_000, ret: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it("bounds a method's IL and exception sections together", () => {
|
||||
// Each bound fits alone; both stay live while one method is decoded.
|
||||
const il = Buffer.concat([Buffer.alloc(159_999), Buffer.from([0x2a])]);
|
||||
const header = Buffer.alloc(12);
|
||||
header.writeUInt16LE(0x300b, 0);
|
||||
header.writeUInt16LE(8, 2);
|
||||
header.writeUInt32LE(il.length, 4);
|
||||
const clauses = 110_000;
|
||||
const section = Buffer.alloc(4 + clauses * 24);
|
||||
section.writeUInt32LE(0x41 | ((4 + clauses * 24) << 8), 0);
|
||||
const bytes = buildManagedPeFixture({
|
||||
ilBody: Buffer.concat([header, il, section]),
|
||||
});
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "unavailable",
|
||||
issues: [{ code: "resource-limit" }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed derived edges", () => {
|
||||
it("admits call edges before the derived graph exceeds the budget", () => {
|
||||
const anchor = {
|
||||
il_offset: 0,
|
||||
opcode: "call",
|
||||
operand_kind: "method",
|
||||
operand: "0x0a000001",
|
||||
};
|
||||
const method = {
|
||||
token: "0x06000001",
|
||||
declaring_type: "T".repeat(2000),
|
||||
name: "m",
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed derived caller names", () => {
|
||||
it("admits a caller name before building it", () => {
|
||||
let built = false;
|
||||
// A string-like type name records whether the caller was interpolated.
|
||||
const declaringType = {
|
||||
length: 70 * 1024 * 1024,
|
||||
charCodeAt: () => 0x54,
|
||||
toString: () => {
|
||||
built = true;
|
||||
return "T";
|
||||
},
|
||||
} as unknown as string;
|
||||
const method = {
|
||||
token: "0x06000001",
|
||||
declaring_type: declaringType,
|
||||
name: "m",
|
||||
body: {
|
||||
anchors: [
|
||||
{
|
||||
il_offset: 0,
|
||||
opcode: "call",
|
||||
operand_kind: "method",
|
||||
operand: "0x0a000001",
|
||||
},
|
||||
],
|
||||
},
|
||||
} as unknown as ManagedMethod;
|
||||
|
||||
expect(() =>
|
||||
edges(
|
||||
[method],
|
||||
new Map(),
|
||||
new Map(),
|
||||
new Map(),
|
||||
new ManagedDecodeBudget(),
|
||||
),
|
||||
).toThrow(ManagedReaderFailure);
|
||||
expect(built).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed member reference bounds", () => {
|
||||
it("retains members and reports an out-of-range TypeDef coded index as partial", () => {
|
||||
const bytes = buildManagedPeFixture({ extendsRaw: 12 });
|
||||
|
||||
Reference in New Issue
Block a user