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 admitted member facts once in the final projection (#3487)

Kaoru0822bddeae5108cb
diff --git a/src/dotnet/ManagedDecodeBudget.test.ts b/src/dotnet/ManagedDecodeBudget.test.ts
index ebab4a99e..b46f6a0a4 100644
--- a/src/dotnet/ManagedDecodeBudget.test.ts
+++ b/src/dotnet/ManagedDecodeBudget.test.ts
@@ -35,6 +35,35 @@ describe("managed projection admission", () => {
ManagedReaderFailure,
);
});
+
+ it("charges an admitted record once inside its containing projection", () => {
+ const budget = new ManagedDecodeBudget();
+ const record = admitManagedProjection(
+ { name: "a".repeat(40 * 1024 * 1024) },
+ budget,
+ );
+
+ expect(() =>
+ admitManagedProjection({ records: [record] }, budget),
+ ).not.toThrow();
+ // The containing projection spent the credit, so later reuse pays again.
+ expect(() =>
+ admitManagedProjection({ records: [record, record] }, budget),
+ ).toThrow(ManagedReaderFailure);
+ });
+
+ it("charges a repeated occurrence of an admitted record again", () => {
+ const budget = new ManagedDecodeBudget();
+ const record = admitManagedProjection(
+ { name: "a".repeat(40 * 1024 * 1024) },
+ budget,
+ );
+
+ // The first occurrence spends the credit; the second serializes again.
+ expect(() =>
+ admitManagedProjection({ records: [record, record] }, budget),
+ ).toThrow(ManagedReaderFailure);
+ });
});
describe("managed decoding bounds", () => {
diff --git a/src/dotnet/ManagedDecodeBudget.ts b/src/dotnet/ManagedDecodeBudget.ts
index d02fce0bc..fba6229a2 100644
--- a/src/dotnet/ManagedDecodeBudget.ts
+++ b/src/dotnet/ManagedDecodeBudget.ts
@@ -18,6 +18,7 @@ export class ManagedDecodeBudget {
readonly #rows = new Map<number, Set<number>>();
readonly #strings = new Map<number, CachedString>();
readonly #digests = new WeakMap<ArrayBufferLike, Map<string, string>>();
+ readonly #prepaid = new WeakSet<object>();
reserve(bytes: number, offset: number | null): void {
this.#admit(bytes, offset);
@@ -82,6 +83,16 @@ export class ManagedDecodeBudget {
return digest;
}
+ /** Record an admitted projection; its next serialized occurrence is already charged. */
+ prepay(value: object): void {
+ this.#prepaid.add(value);
+ }
+
+ /** Spend an admitted projection's charge once; repeated occurrences pay again. */
+ consumePrepaid(value: object): boolean {
+ return this.#prepaid.delete(value);
+ }
+
cachedString(index: number): CachedString | undefined {
return this.#strings.get(index);
}
@@ -141,12 +152,23 @@ const jsonBytes = (item: unknown): number =>
? managedJsonStringBytes(item)
: Buffer.byteLength(JSON.stringify(item) ?? "null");
-/** Admit repeated values too: shared heap strings still expand at the JSON boundary. */
+/**
+ * Admit repeated values too: shared heap strings still expand at the JSON
+ * boundary. An object admitted earlier was charged for one occurrence, so a
+ * containing projection spends that charge instead of counting it again.
+ * Admitted values must stay unmodified.
+ */
export const admitManagedProjection = <Value>(
value: Value,
budget: ManagedDecodeBudget = new ManagedDecodeBudget(),
): Value => {
const visit = (item: unknown): void => {
+ if (
+ item !== null &&
+ typeof item === "object" &&
+ budget.consumePrepaid(item)
+ )
+ return;
if (Array.isArray(item)) {
budget.reserve(item.length + 2, null);
for (const child of item) visit(child);
@@ -161,5 +183,6 @@ export const admitManagedProjection = <Value>(
}
};
visit(value);
+ if (value !== null && typeof value === "object") budget.prepay(value);
return value;
};