fix(dotnet): name the truncated instruction when an operand leaves the method body (#2704)

* fix(dotnet): name the truncated instruction when an operand leaves the method body

The extent guard ran after the operand read, so a truncated operand threw a raw Buffer RangeError that replaced the intended diagnostic with the runtime message and lost the instruction identity.

Bound the operand extent before reading its value and carry the width beside each fixed reader so the two facts cannot drift.

Fixes #2677

* test(dotnet): consolidate truncated operand coverage

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Yunzhou Li
2026-10-14 02:52:47 +08:00
committed by GitHub
co-authored by morluto
parent 388b907c92
commit 97268f40ea
2 changed files with 288 additions and 88 deletions
+83 -50
View File
@@ -271,82 +271,101 @@ type OperandReader = (il: Buffer, offset: number) => OperandResult;
const floatingPointOperand = (value: number): string =>
Object.is(value, -0) ? "-0" : String(value);
const FIXED_OPERAND_READERS = new Map<string, OperandReader>([
/**
* A fixed-width operand declares its byte width next to its reader so the
* extent check can bound the read before any value is consumed. Keeping both
* facts in one entry is what stops the width and the reader from drifting.
*/
type FixedOperand = {
readonly width: number;
readonly read: OperandReader;
};
const fixed = (width: number, read: OperandReader): FixedOperand => ({
width,
read,
});
const FIXED_OPERAND_READERS = new Map<string, FixedOperand>([
[
"none",
(_il, offset) => ({ next: offset, anchorKind: "none", value: null }),
fixed(0, (_il, offset) => ({
next: offset,
anchorKind: "none",
value: null,
})),
],
[
"short-var",
(il, offset) => ({
fixed(1, (il, offset) => ({
next: offset + 1,
anchorKind: "variable",
value: String(il.readUInt8(offset)),
}),
})),
],
[
"var",
(il, offset) => ({
fixed(2, (il, offset) => ({
next: offset + 2,
anchorKind: "variable",
value: String(il.readUInt16LE(offset)),
}),
})),
],
[
"short-i",
(il, offset) => ({
fixed(1, (il, offset) => ({
next: offset + 1,
anchorKind: "constant",
value: String(il.readInt8(offset)),
}),
})),
],
[
"i",
(il, offset) => ({
fixed(4, (il, offset) => ({
next: offset + 4,
anchorKind: "constant",
value: String(il.readInt32LE(offset)),
}),
})),
],
[
"i8",
(il, offset) => ({
fixed(8, (il, offset) => ({
next: offset + 8,
anchorKind: "constant",
value: il.readBigInt64LE(offset).toString(),
}),
})),
],
[
"r4",
(il, offset) => ({
fixed(4, (il, offset) => ({
next: offset + 4,
anchorKind: "constant",
value: floatingPointOperand(il.readFloatLE(offset)),
}),
})),
],
[
"r8",
(il, offset) => ({
fixed(8, (il, offset) => ({
next: offset + 8,
anchorKind: "constant",
value: floatingPointOperand(il.readDoubleLE(offset)),
}),
})),
],
[
"short-branch",
(il, offset) => ({
fixed(1, (il, offset) => ({
next: offset + 1,
anchorKind: "branch",
value: String(offset + 1 + il.readInt8(offset)),
}),
})),
],
[
"branch",
(il, offset) => ({
fixed(4, (il, offset) => ({
next: offset + 4,
anchorKind: "branch",
value: String(offset + 4 + il.readInt32LE(offset)),
}),
})),
],
]);
@@ -361,13 +380,42 @@ const TOKEN_OPERAND_KINDS = new Map<
["signature", "signature"],
]);
/** Every token operand is a 4-byte metadata token. */
const TOKEN_OPERAND_WIDTH = 4;
/**
* Bytes the operand at `offset` occupies, or `null` when the extent itself
* cannot be read from `il`. This is the single extent authority: `readOperand`
* must never be called for an operand whose extent it cannot bound, so no
* operand read can leave the method body.
*/
const operandExtent = (
il: Buffer,
offset: number,
kind: string,
): number | null => {
const fixedOperand = FIXED_OPERAND_READERS.get(kind);
if (fixedOperand !== undefined) return fixedOperand.width;
if (kind === "switch") {
if (offset > il.length - 4) return null;
return 4 + il.readUInt32LE(offset) * 4;
}
return TOKEN_OPERAND_KINDS.has(kind) || kind === "token"
? TOKEN_OPERAND_WIDTH
: 0;
};
/**
* Read an operand whose extent is already known to fit. `next` is always
* `offset + operandExtent(il, offset, kind)`; callers bound the read first.
*/
const readOperand = (
il: Buffer,
offset: number,
kind: string,
): OperandResult => {
const fixed = FIXED_OPERAND_READERS.get(kind);
if (fixed !== undefined) return fixed(il, offset);
const fixedOperand = FIXED_OPERAND_READERS.get(kind);
if (fixedOperand !== undefined) return fixedOperand.read(il, offset);
if (kind === "switch") {
const count = il.readUInt32LE(offset);
return {
@@ -376,30 +424,15 @@ const readOperand = (
value: String(count),
};
}
if (!TOKEN_OPERAND_KINDS.has(kind) && kind !== "token")
return { next: offset, anchorKind: "unknown", value: null };
const raw = il.readUInt32LE(offset);
const token = `0x${raw.toString(16).padStart(8, "0")}`;
const tokenOperandKind = TOKEN_OPERAND_KINDS.get(kind);
if (tokenOperandKind !== undefined)
return { next: offset + 4, anchorKind: tokenOperandKind, value: token };
if (kind === "token")
return { next: offset + 4, anchorKind: tokenKind(token), value: token };
return { next: offset, anchorKind: "unknown", value: null };
};
/** An operand cut off by the end of the IL body has no decodable value. */
const readOperandWithin = (
il: Buffer,
offset: number,
kind: string,
): OperandResult | undefined => {
try {
return readOperand(il, offset, kind);
} catch (cause: unknown) {
// Buffer reads past the IL body throw RangeError; report it as the
// instruction leaving the method body rather than as a Buffer message.
if (cause instanceof RangeError) return undefined;
throw cause;
}
return {
next: offset + TOKEN_OPERAND_WIDTH,
anchorKind: TOKEN_OPERAND_KINDS.get(kind) ?? tokenKind(token),
value: token,
};
};
export const decodeInstructions = (
@@ -432,15 +465,15 @@ export const decodeInstructions = (
issue = `Unsupported CIL opcode 0x${code.toString(16)} at IL offset ${String(start)}`;
break;
}
const operand = readOperandWithin(il, offset, descriptor.operand);
if (
operand === undefined ||
operand.next < offset ||
operand.next > il.length
) {
// Bound the operand extent before reading its value. Reading first would
// throw a raw Buffer RangeError out of a truncated body and replace this
// diagnostic with the runtime message, losing the instruction identity.
const extent = operandExtent(il, offset, descriptor.operand);
if (extent === null || offset + extent > il.length) {
issue = `Instruction ${descriptor.name} at IL offset ${String(start)} leaves method body`;
break;
}
const operand = readOperand(il, offset, descriptor.operand);
// 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);