* 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.
153 lines
5.1 KiB
TypeScript
153 lines
5.1 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
|
|
import type { ManagedMetadataLayout } from "./ManagedMetadataLayout.js";
|
|
import { managedFailure } from "./ManagedReaderFailure.js";
|
|
|
|
/** Per-inspection capacity for eagerly retained metadata and its JSON projection. */
|
|
const MAX_METADATA_REPRESENTATION_BYTES = 64 * 1024 * 1024;
|
|
|
|
interface CachedString {
|
|
readonly value: string;
|
|
readonly encodedBytes: number;
|
|
}
|
|
|
|
/** 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 {
|
|
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,
|
|
);
|
|
}
|
|
|
|
/** UTF-8 cannot decode to more UTF-16 units than bytes; JSON uses at most six bytes per unit. */
|
|
reserveString(encodedBytes: number, offset: number | null): void {
|
|
this.reserve(encodedBytes * 6 + 2, offset);
|
|
}
|
|
|
|
/** Snapshot buffers are immutable within a layout; cache exact backing-buffer ranges. */
|
|
digest(bytes: Buffer, algorithm: "sha256" | "sha1"): string {
|
|
let ranges = this.#digests.get(bytes.buffer);
|
|
if (ranges === undefined) {
|
|
ranges = new Map();
|
|
this.#digests.set(bytes.buffer, ranges);
|
|
}
|
|
const key = `${algorithm}:${String(bytes.byteOffset)}:${String(bytes.byteLength)}`;
|
|
const cached = ranges.get(key);
|
|
if (cached !== undefined) return cached;
|
|
this.reserve(256, null);
|
|
const digest = createHash(algorithm).update(bytes).digest("hex");
|
|
ranges.set(key, digest);
|
|
return digest;
|
|
}
|
|
|
|
cachedString(index: number): CachedString | undefined {
|
|
return this.#strings.get(index);
|
|
}
|
|
|
|
cacheString(index: number, value: string, encodedBytes: number): void {
|
|
this.#strings.set(index, { value, encodedBytes });
|
|
}
|
|
}
|
|
|
|
const budgets = new WeakMap<ManagedMetadataLayout, ManagedDecodeBudget>();
|
|
|
|
/** Transfer metadata-root admission capacity to its completed immutable layout. */
|
|
export const attachManagedDecodeBudget = (
|
|
layout: ManagedMetadataLayout,
|
|
budget: ManagedDecodeBudget,
|
|
): void => {
|
|
budgets.set(layout, budget);
|
|
};
|
|
|
|
/** Share one owner for the lifetime of the admitted layout, including all inspection facets. */
|
|
export const managedDecodeBudget = (
|
|
layout: ManagedMetadataLayout,
|
|
): ManagedDecodeBudget => {
|
|
let budget = budgets.get(layout);
|
|
if (budget === undefined) {
|
|
budget = new ManagedDecodeBudget();
|
|
budgets.set(layout, budget);
|
|
}
|
|
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 (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(jsonBytes(key) + 2, null);
|
|
visit(Reflect.get(item, key));
|
|
}
|
|
} else {
|
|
budget.reserve(jsonBytes(item), null);
|
|
}
|
|
};
|
|
visit(value);
|
|
return value;
|
|
};
|