Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(managed): charge retained member facts instead of decoding bounds (#1768)

Martin Klein30787297d49b
diff --git a/src/dotnet/ManagedDecodeBudget.test.ts b/src/dotnet/ManagedDecodeBudget.test.ts
new file mode 100644
index 000000000..48ef12123
--- /dev/null
+++ b/src/dotnet/ManagedDecodeBudget.test.ts
@@ -0,0 +1,53 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ admitManagedProjection,
+ ManagedDecodeBudget,
+} from "./ManagedDecodeBudget.js";
+import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
+
+describe("managed projection admission", () => {
+ it("charges decoded strings by their JSON bytes", () => {
+ const name = "a".repeat(12 * 1024 * 1024);
+
+ expect(admitManagedProjection({ name })).toEqual({ name });
+ });
+
+ it("charges escaped and multibyte characters at their encoded size", () => {
+ expect(() =>
+ admitManagedProjection({ name: "\u0001".repeat(12 * 1024 * 1024) }),
+ ).toThrow(ManagedReaderFailure);
+ expect(() =>
+ admitManagedProjection({ name: "€".repeat(23 * 1024 * 1024) }),
+ ).toThrow(ManagedReaderFailure);
+ });
+
+ it("counts escaped JSON bytes exactly at the budget boundary", () => {
+ // Control escapes, quotes, three-byte text, surrogate pairs and lone
+ // surrogates each have a distinct JSON size; fill the rest with ASCII.
+ const mixed = "\u0001".repeat(4096) + '"\\' + "€😀\ud800\udc00\ud800";
+ const used = Buffer.byteLength(JSON.stringify(mixed));
+ const filled = (extra: number) =>
+ mixed + "a".repeat(64 * 1024 * 1024 - used + extra);
+
+ expect(() => admitManagedProjection(filled(0))).not.toThrow();
+ expect(() => admitManagedProjection(filled(1))).toThrow(
+ ManagedReaderFailure,
+ );
+ });
+});
+
+describe("managed decoding bounds", () => {
+ it("keeps concurrent decoding bounds within one budget until released", () => {
+ const budget = new ManagedDecodeBudget();
+ const bound = 40 * 1024 * 1024;
+
+ budget.admitTransient(bound, null);
+ expect(() => budget.admitTransient(bound, null)).toThrow(
+ ManagedReaderFailure,
+ );
+ expect(() => budget.reserve(bound, null)).toThrow(ManagedReaderFailure);
+ budget.releaseTransient();
+ expect(() => budget.admitTransient(bound, null)).not.toThrow();
+ });
+});
diff --git a/src/dotnet/ManagedDecodeBudget.ts b/src/dotnet/ManagedDecodeBudget.ts
index 351565e45..14b691566 100644
--- a/src/dotnet/ManagedDecodeBudget.ts
+++ b/src/dotnet/ManagedDecodeBudget.ts
@@ -14,18 +14,38 @@ interface CachedString {
/** The layout owns decoding capacity and heap identity for all of its consumers. */
export class ManagedDecodeBudget {
#remaining = MAX_METADATA_REPRESENTATION_BYTES;
+ #transient = 0;
readonly #strings = new Map<number, CachedString>();
readonly #digests = new WeakMap<ArrayBufferLike, Map<string, string>>();
reserve(bytes: number, offset: number | null): void {
- if (!Number.isSafeInteger(bytes) || bytes < 0 || bytes > this.#remaining)
+ this.#admit(bytes, offset);
+ this.#remaining -= bytes;
+ }
+
+ /** Bound a decoding allocation that stays live until `releaseTransient`; only retained output is reserved. */
+ admitTransient(bytes: number, offset: number | null): void {
+ this.#admit(bytes, offset);
+ this.#transient += bytes;
+ }
+
+ /** End the current item's decoding; its bookkeeping is unreachable and only reserved output remains. */
+ releaseTransient(): void {
+ this.#transient = 0;
+ }
+
+ #admit(bytes: number, offset: number | null): void {
+ if (
+ !Number.isSafeInteger(bytes) ||
+ bytes < 0 ||
+ bytes > this.#remaining - this.#transient
+ )
throw managedFailure(
"resource-limit",
"metadata.representation",
`Managed metadata exceeds the ${String(MAX_METADATA_REPRESENTATION_BYTES)} byte representation budget`,
offset,
);
- this.#remaining -= bytes;
}
/** UTF-8 cannot decode to more UTF-16 units than bytes; JSON uses at most six bytes per unit. */
@@ -80,25 +100,51 @@ export const managedDecodeBudget = (
return budget;
};
+/** Exact UTF-8 bytes of a JSON string literal, counted without serializing it. */
+export const managedJsonStringBytes = (value: string): number => {
+ let bytes = 2;
+ for (let index = 0; index < value.length; index++) {
+ const unit = value.charCodeAt(index);
+ if (unit === 0x22 || unit === 0x5c) bytes += 2;
+ else if (unit < 0x20)
+ bytes += [0x08, 0x09, 0x0a, 0x0c, 0x0d].includes(unit) ? 2 : 6;
+ else if (unit < 0x80) bytes += 1;
+ else if (unit < 0x800) bytes += 2;
+ else if (unit >= 0xd800 && unit <= 0xdbff) {
+ const next = value.charCodeAt(index + 1);
+ if (next >= 0xdc00 && next <= 0xdfff) {
+ bytes += 4;
+ index++;
+ } else bytes += 6;
+ } else if (unit >= 0xdc00 && unit <= 0xdfff) bytes += 6;
+ else bytes += 3;
+ }
+ return bytes;
+};
+
+/** Exact UTF-8 JSON bytes of a decoded scalar or key, including escapes. */
+const jsonBytes = (item: unknown): number =>
+ typeof item === "string"
+ ? managedJsonStringBytes(item)
+ : Buffer.byteLength(JSON.stringify(item) ?? "null");
+
/** Admit repeated values too: shared heap strings still expand at the JSON boundary. */
export const admitManagedProjection = <Value>(
value: Value,
budget: ManagedDecodeBudget = new ManagedDecodeBudget(),
): Value => {
const visit = (item: unknown): void => {
- if (typeof item === "string") {
- budget.reserveString(item.length, null);
- } else if (Array.isArray(item)) {
+ if (Array.isArray(item)) {
budget.reserve(item.length + 2, null);
for (const child of item) visit(child);
} else if (item !== null && typeof item === "object") {
budget.reserve(2, null);
for (const key of Object.keys(item)) {
- budget.reserve(key.length * 6 + 4, null);
+ budget.reserve(jsonBytes(key) + 2, null);
visit(Reflect.get(item, key));
}
} else {
- budget.reserve(32, null);
+ budget.reserve(jsonBytes(item), null);
}
};
visit(value);
diff --git a/src/dotnet/ManagedMemberInspector.test.ts b/src/dotnet/ManagedMemberInspector.test.ts
index 2def3272e..9440a6bf7 100644
--- a/src/dotnet/ManagedMemberInspector.test.ts
+++ b/src/dotnet/ManagedMemberInspector.test.ts
@@ -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 });
diff --git a/src/dotnet/ManagedMemberInspector.ts b/src/dotnet/ManagedMemberInspector.ts
index e9862d3fa..95cae8fae 100644
--- a/src/dotnet/ManagedMemberInspector.ts
+++ b/src/dotnet/ManagedMemberInspector.ts
@@ -142,6 +142,7 @@ export const inspectManagedMembersBytes = (
methods.core,
fields.core,
memberRefs.core,
+ managedDecodeBudget(layout),
);
return managedMemberInspectionSchema.parse(
admitManagedProjection(
diff --git a/src/dotnet/ManagedMemberInstructionDecoder.ts b/src/dotnet/ManagedMemberInstructionDecoder.ts
index 43a678ded..464a5c01b 100644
--- a/src/dotnet/ManagedMemberInstructionDecoder.ts
+++ b/src/dotnet/ManagedMemberInstructionDecoder.ts
@@ -489,7 +489,8 @@ export const parseExceptionRegions = (
);
const count = (size - 4) / clauseSize;
- budget?.reserve(count * 256, sectionOffset);
+ // Accumulates with the method IL bound; the body reserves what it retains.
+ budget?.admitTransient(count * 256, sectionOffset);
for (const region of readExceptionClauses(
bytes,
sectionOffset + 4,
diff --git a/src/dotnet/ManagedMemberRows.ts b/src/dotnet/ManagedMemberRows.ts
index 18a67dbaf..ab8b40724 100644
--- a/src/dotnet/ManagedMemberRows.ts
+++ b/src/dotnet/ManagedMemberRows.ts
@@ -1,4 +1,9 @@
-import { managedDecodeBudget } from "./ManagedDecodeBudget.js";
+import {
+ admitManagedProjection,
+ managedDecodeBudget,
+ managedJsonStringBytes,
+ type ManagedDecodeBudget,
+} from "./ManagedDecodeBudget.js";
import type { ManagedPeLayout } from "./ManagedPeReader.js";
import type { ManagedParseIssue } from "../domain/managed/managedArtifact.js";
import type { ManagedMetadataLayout } from "./ManagedMetadataLayout.js";
@@ -223,6 +228,7 @@ export const edges = (
methodCore: ReadonlyMap<string, MethodCore>,
fieldCore: ReadonlyMap<string, FieldCore>,
refCore: ReadonlyMap<string, MemberRefCore>,
+ budget: ManagedDecodeBudget,
): {
readonly callEdges: readonly ManagedCallEdge[];
readonly fieldAccesses: readonly ManagedFieldAccess[];
@@ -230,6 +236,15 @@ export const edges = (
const callEdges: ManagedCallEdge[] = [];
const fieldAccesses: ManagedFieldAccess[] = [];
for (const method of methods) {
+ const derives = method.body.anchors.some(
+ (anchor) =>
+ anchor.opcode !== "ldtoken" &&
+ anchor.operand !== null &&
+ (anchor.operand_kind === "method" || anchor.operand_kind === "field"),
+ );
+ if (derives) budget.admitTransient(callerNameBytes(method), null);
+ // Built once per method, only after its exact size was admitted.
+ const caller = derives ? callerName(method) : method.name;
for (const anchor of method.body.anchors) {
if (anchor.opcode === "ldtoken") continue;
if (anchor.operand_kind === "method" && anchor.operand !== null) {
@@ -237,39 +252,67 @@ export const edges = (
methodCore.get(anchor.operand)?.name ??
refCore.get(anchor.operand)?.name ??
null;
- callEdges.push({
- caller_token: method.token,
- caller:
- method.declaring_type === null
- ? method.name
- : `${method.declaring_type}.${method.name}`,
- opcode: anchor.opcode,
- target_token: anchor.operand,
- target_kind: targetKind(anchor.operand),
- target_name: named,
- });
+ // Each derived record is admitted before the graph grows further.
+ callEdges.push(
+ admitManagedProjection(
+ {
+ caller_token: method.token,
+ caller,
+ opcode: anchor.opcode,
+ target_token: anchor.operand,
+ target_kind: targetKind(anchor.operand),
+ target_name: named,
+ },
+ budget,
+ ),
+ );
}
if (anchor.operand_kind === "field" && anchor.operand !== null) {
- fieldAccesses.push({
- method_token: method.token,
- method:
- method.declaring_type === null
- ? method.name
- : `${method.declaring_type}.${method.name}`,
- opcode: anchor.opcode,
- field_token: anchor.operand,
- field_name: fieldCore.get(anchor.operand)?.name ?? null,
- });
+ fieldAccesses.push(
+ admitManagedProjection(
+ {
+ method_token: method.token,
+ method: caller,
+ opcode: anchor.opcode,
+ field_token: anchor.operand,
+ field_name: fieldCore.get(anchor.operand)?.name ?? null,
+ },
+ budget,
+ ),
+ );
}
}
+ // Every record now reserves the name it carries.
+ if (derives) budget.releaseTransient();
}
return { callEdges, fieldAccesses };
};
+const callerName = (method: ManagedMethod): string =>
+ method.declaring_type === null
+ ? method.name
+ : `${method.declaring_type}.${method.name}`;
+
+/** JSON bytes of `callerName`, from its parts: the joining dot adds one byte. */
+const callerNameBytes = (method: ManagedMethod): number =>
+ method.declaring_type === null
+ ? managedJsonStringBytes(method.name)
+ : managedJsonStringBytes(method.declaring_type) +
+ managedJsonStringBytes(method.name) -
+ 1;
+
const budgetedSignature = (
bytes: Buffer,
layout: ManagedMetadataLayout,
): ReturnType<typeof signature> => {
- managedDecodeBudget(layout).reserve(bytes.length * 256, layout.blob.offset);
- return signature(bytes, managedDecodeBudget(layout).digest(bytes, "sha256"));
+ const budget = managedDecodeBudget(layout);
+ budget.admitTransient(bytes.length * 256, layout.blob.offset);
+ try {
+ return admitManagedProjection(
+ signature(bytes, budget.digest(bytes, "sha256")),
+ budget,
+ );
+ } finally {
+ budget.releaseTransient();
+ }
};
diff --git a/src/dotnet/ManagedMethodBodyReader.ts b/src/dotnet/ManagedMethodBodyReader.ts
index 9985b718b..047c2846a 100644
--- a/src/dotnet/ManagedMethodBodyReader.ts
+++ b/src/dotnet/ManagedMethodBodyReader.ts
@@ -1,4 +1,7 @@
-import type { ManagedDecodeBudget } from "./ManagedDecodeBudget.js";
+import {
+ admitManagedProjection,
+ type ManagedDecodeBudget,
+} from "./ManagedDecodeBudget.js";
import type { ManagedPeLayout } from "./ManagedPeReader.js";
import type { ManagedMethodBody } from "./ManagedMemberInspectorCore.js";
import {
@@ -98,8 +101,9 @@ export const methodBody = (
throw new RangeError("Method IL bytes leave artifact");
const il = bytes.subarray(ilOffset, ilOffset + header.ilSize);
// One byte can produce an instruction record, start index, normalized tuple,
- // and anchor. Reserve conservative bookkeeping capacity before decoding.
- budget?.reserve(il.length * 256, ilOffset);
+ // and anchor. Bound that bookkeeping before decoding; the retained body is
+ // reserved once it is known.
+ budget?.admitTransient(il.length * 256, ilOffset);
const decoded = decodeInstructions(il);
const opcodeCounts: Record<string, number> = {};
for (const instruction of decoded.parsed)
@@ -143,7 +147,7 @@ export const methodBody = (
decoded.truncated,
exceptionRegions,
);
- return {
+ const body: ManagedMethodBody = {
status,
header_format: header.format,
rva,
@@ -173,6 +177,7 @@ export const methodBody = (
? "Instruction decoding stopped before the end of the method body"
: null),
};
+ return budget === undefined ? body : admitManagedProjection(body, budget);
} catch (cause: unknown) {
if (
cause instanceof ManagedReaderFailure &&
@@ -189,6 +194,9 @@ export const methodBody = (
"malformed",
cause instanceof Error ? cause.message : "Method body parse failed",
);
+ } finally {
+ // IL and exception-section bookkeeping coexist until the body is built.
+ budget?.releaseTransient();
}
};