refactor(python): report pyc parser limits from LimitRef (#12213)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 05:32:28 +00:00
1 parent 954a2511b7
commit 84551888ec
7 files changed
+182 -118

No files matched your search

+52 -73
View File
@@ -27,10 +27,7 @@ import {
import { Budget } from "../domain/budget.js";
import { malformedInput, unsupportedInput } from "../domain/parseFailure.js";
import { BinaryReader } from "../domain/binaryContainer.js";
import {
PYTHON_PYC_FORMAT,
PythonPycResourceFailure,
} from "./PythonPyc.errors.js";
import { PYTHON_PYC_FORMAT, pythonPycLimit } from "./PythonPyc.errors.js";
import { pythonPycBytecodeIdentity } from "./PythonPycBytecodeEquality.js";
import { sha256Hex } from "../kernel/sha256Hex.js";
@@ -421,28 +418,28 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
const { max_objects, max_depth, max_string_bytes } = this.input;
switch (exceeded.limit) {
case "objects":
throw new PythonPycResourceFailure(
"objects",
{ max_objects },
throw pythonPycLimit(
"max_objects",
max_objects,
`Marshal object count exceeds max_objects ${max_objects}.`,
);
case "depth":
throw new PythonPycResourceFailure(
"depth",
{ max_depth },
throw pythonPycLimit(
"max_depth",
max_depth,
`Marshal nesting exceeds max_depth ${max_depth}.`,
);
case "string-bytes":
throw new PythonPycResourceFailure(
"string",
{ max_string_bytes },
throw pythonPycLimit(
"max_string_bytes",
max_string_bytes,
`Marshal string of ${exceeded.amount} bytes exceeds max_string_bytes ${max_string_bytes}.`,
);
default:
// Long digits; this reader sets no sequence or digit-count bound.
throw new PythonPycResourceFailure(
"string",
{ max_string_bytes },
throw pythonPycLimit(
"max_string_bytes",
max_string_bytes,
`Marshal long of ${exceeded.amount} digit bytes exceeds max_string_bytes ${max_string_bytes}.`,
);
}
@@ -717,14 +714,12 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
#reportStrings(values: readonly string[]): void {
let bytes = 0;
for (const value of values) bytes += Buffer.byteLength(value, "utf8");
this.#reportedStringBytes.charge(
bytes,
() =>
new PythonPycResourceFailure(
"string",
{ max_file_bytes: this.input.max_file_bytes },
`Reported code-object strings exceed max_file_bytes ${this.input.max_file_bytes}.`,
),
this.#reportedStringBytes.charge(bytes, () =>
pythonPycLimit(
"reported_string_bytes",
this.#reportedStringBytes.maximum,
`Reported code-object strings exceed max_file_bytes ${this.#reportedStringBytes.maximum}.`,
),
);
}
@@ -763,14 +758,12 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
count: number,
what: "constants" | "names" | "nested code relationships",
): void {
this.#reportedCopies.charge(
count,
() =>
new PythonPycResourceFailure(
"objects",
{ max_objects: this.input.max_objects },
`Reported ${what} exceed max_objects ${this.input.max_objects}.`,
),
this.#reportedCopies.charge(count, () =>
pythonPycLimit(
"reported_rows",
this.#reportedCopies.maximum,
`Reported ${what} exceed max_objects ${this.#reportedCopies.maximum}.`,
),
);
}
@@ -934,17 +927,12 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
}
#takeNestedStep(): void {
this.#nestedSteps.charge(
1,
() =>
new PythonPycResourceFailure(
"objects",
{
max_objects: this.input.max_objects,
max_depth: this.input.max_depth,
},
`Nested code walk exceeds ${String(this.#nestedSteps.maximum)} steps (max_objects * max_depth).`,
),
this.#nestedSteps.charge(1, () =>
pythonPycLimit(
"nested_code_steps",
this.#nestedSteps.maximum,
`Nested code walk exceeds ${String(this.#nestedSteps.maximum)} steps (max_objects * max_depth).`,
),
);
this.work.nestedSteps += 1;
}
@@ -956,17 +944,12 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
}
#retainNested(codes: readonly CodeNode[]): void {
this.#nestedRetained.charge(
codes.length,
() =>
new PythonPycResourceFailure(
"objects",
{
max_objects: this.input.max_objects,
max_depth: this.input.max_depth,
},
`Nested code walk retains more than ${String(this.#nestedRetained.maximum)} code references.`,
),
this.#nestedRetained.charge(codes.length, () =>
pythonPycLimit(
"nested_code_references",
this.#nestedRetained.maximum,
`Nested code walk retains more than ${String(this.#nestedRetained.maximum)} code references.`,
),
);
}
@@ -1190,14 +1173,12 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
* not `max_depth`, because a reference chain is shallow in the stream.
*/
#takeKeyStep(): void {
this.#keySteps.charge(
1,
() =>
new PythonPycResourceFailure(
"objects",
{ max_objects: this.input.max_objects },
`Marshal key walk exceeds ${this.#keySteps.maximum} steps (max_objects * 8).`,
),
this.#keySteps.charge(1, () =>
pythonPycLimit(
"marshal_key_steps",
this.#keySteps.maximum,
`Marshal key walk exceeds ${this.#keySteps.maximum} steps (max_objects * 8).`,
),
);
this.work.keySteps += 1;
}
@@ -1508,22 +1489,20 @@ class PycMarshalReader extends MarshalReader<MarshalValue> {
#reserveKey(size: number): void {
if (size > this.input.max_string_bytes) this.#keyTooLarge(size);
this.#keyBytes.charge(
size,
() =>
new PythonPycResourceFailure(
"string",
{ max_file_bytes: this.input.max_file_bytes },
`Marshal key expansion exceeds max_file_bytes ${this.input.max_file_bytes}.`,
),
this.#keyBytes.charge(size, () =>
pythonPycLimit(
"marshal_key_bytes",
this.#keyBytes.maximum,
`Marshal key expansion exceeds max_file_bytes ${this.#keyBytes.maximum}.`,
),
);
this.work.keyBytes += size;
}
#keyTooLarge(size: number): never {
throw new PythonPycResourceFailure(
"string",
{ max_string_bytes: this.input.max_string_bytes },
throw pythonPycLimit(
"max_string_bytes",
this.input.max_string_bytes,
`Marshal key of ${size} bytes exceeds max_string_bytes ${this.input.max_string_bytes}.`,
);
}