fix(managed): bound transient CIL by decoded instruction count (#1822)

fix(managed): charge transient CIL by decoded instructions
This commit is contained in:
jhuang-tw
2026-10-11 22:51:41 +08:00
committed by GitHub
parent ca11162d02
commit 554cb45f7f
3 changed files with 72 additions and 5 deletions
@@ -1,4 +1,5 @@
import type { ManagedDecodeBudget } from "./ManagedDecodeBudget.js";
import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
import type {
ManagedExceptionRegion,
ManagedInstructionAnchor,
@@ -381,6 +382,7 @@ const readOperand = (
export const decodeInstructions = (
il: Buffer,
admitInstruction?: (offset: number) => void,
): {
readonly parsed: readonly ParsedInstruction[];
readonly instructionStarts: readonly number[];
@@ -409,6 +411,9 @@ export const decodeInstructions = (
issue = `Instruction ${descriptor.name} at IL offset ${String(start)} leaves method body`;
break;
}
// Charge each actual instruction before growing the decoded arrays.
// A multi-byte operand must not consume a full record per raw IL byte.
admitInstruction?.(start);
offset = operand.next;
if (PREFIX_OPCODES.has(descriptor.name)) {
prefixStart ??= start;
@@ -424,6 +429,7 @@ export const decodeInstructions = (
});
}
} catch (cause: unknown) {
if (cause instanceof ManagedReaderFailure) throw cause;
issue =
cause instanceof Error ? cause.message : "Instruction decode failed";
}