From 18898e3256a1e6ae8612e8a4de797989ab01e8c9 Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Wed, 2 Dec 2026 11:23:53 -0800 Subject: [PATCH 1/2] JIT: validate IL arg/local numbers before use Malformed IL can use out-of-range arg or local numbers. When inlining, the prescan and importer could index past the fixed-size InlineInfo arrays and corrupt the JIT's stack. Fail the inline (bad argument/local number) before any such access. For non-inline methods, starg and stloc in the prescan now BADCODE instead of relying on a stale "verifier" comment, and impLoadArg checks against compILargsCount rather than compArgsCount, so shared generic code with a hidden context arg no longer reaches the compMapILargNum assert. Adds an IL regression test. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c2f0cfcd-4917-4a8a-8e65-a771f4a12789 --- src/coreclr/jit/fgbasic.cpp | 84 ++++-- src/coreclr/jit/importer.cpp | 45 ++- .../inlinee-il-bounds/inlinee-il-bounds.il | 281 ++++++++++++++++++ .../inlinee-il-bounds.ilproj | 10 + 4 files changed, 388 insertions(+), 32 deletions(-) create mode 100644 src/tests/JIT/opt/Inline/regression/inlinee-il-bounds/inlinee-il-bounds.il create mode 100644 src/tests/JIT/opt/Inline/regression/inlinee-il-bounds/inlinee-il-bounds.ilproj diff --git a/src/coreclr/jit/fgbasic.cpp b/src/coreclr/jit/fgbasic.cpp index 1d6ccf1289853f..bcc47865b8f77f 100644 --- a/src/coreclr/jit/fgbasic.cpp +++ b/src/coreclr/jit/fgbasic.cpp @@ -2081,23 +2081,26 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed if (isInlining) { - if (varNum < impInlineInfo->argCnt) + if (varNum >= impInlineInfo->argCnt) { - impInlineInfo->inlArgInfo[varNum].argHasStargOp = true; + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; } + + impInlineInfo->inlArgInfo[varNum].argHasStargOp = true; } else { + if (varNum >= info.compILargsCount) + { + BADCODE("bad argument number"); + } + // account for possible hidden param varNum = compMapILargNum(varNum); - // This check is only intended to prevent an AV. Bad varNum values will later - // be handled properly by the verifier. - if (varNum < lvaTableCnt) - { - // In non-inline cases, note written-to arguments. - lvaTable[varNum].lvHasILStoreOp = 1; - } + // In non-inline cases, note written-to arguments. + lvaTable[varNum].lvHasILStoreOp = 1; } } break; @@ -2124,6 +2127,12 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed STLOC: if (isInlining) { + if (varNum >= info.compMethodInfo->locals.numArgs) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_LOCAL_NUMBER); + return; + } + InlLclVarInfo& lclInfo = impInlineInfo->lclVarInfo[varNum + impInlineInfo->argCnt]; if (lclInfo.lclHasStlocOp) @@ -2137,21 +2146,21 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed } else { + if (varNum >= info.compMethodInfo->locals.numArgs) + { + BADCODE("bad local number"); + } + varNum += info.compArgsCount; - // This check is only intended to prevent an AV. Bad varNum values will later - // be handled properly by the verifier. - if (varNum < lvaTableCnt) + // In non-inline cases, note written-to locals. + if (lvaTable[varNum].lvHasILStoreOp) { - // In non-inline cases, note written-to locals. - if (lvaTable[varNum].lvHasILStoreOp) - { - lvaTable[varNum].lvHasMultipleILStoreOp = 1; - } - else - { - lvaTable[varNum].lvHasILStoreOp = 1; - } + lvaTable[varNum].lvHasMultipleILStoreOp = 1; + } + else + { + lvaTable[varNum].lvHasILStoreOp = 1; } } } @@ -2189,6 +2198,12 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed { if (opcode == CEE_LDLOCA || opcode == CEE_LDLOCA_S) { + if (varNum >= info.compMethodInfo->locals.numArgs) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_LOCAL_NUMBER); + return; + } + varType = impInlineInfo->lclVarInfo[varNum + impInlineInfo->argCnt].lclTypeInfo; impInlineInfo->lclVarInfo[varNum + impInlineInfo->argCnt].lclHasLdlocaOp = true; @@ -2197,6 +2212,12 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed { noway_assert(opcode == CEE_LDARGA || opcode == CEE_LDARGA_S); + if (varNum >= impInlineInfo->argCnt) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; + } + varType = impInlineInfo->lclVarInfo[varNum].lclTypeInfo; impInlineInfo->inlArgInfo[varNum].argHasLdargaOp = true; @@ -2330,7 +2351,14 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed case CEE_LDARG_3: if (makeInlineObservations) { - pushedStack.PushArgument(opcode - CEE_LDARG_0); + unsigned argNum = opcode - CEE_LDARG_0; + if (isInlining && (argNum >= impInlineInfo->argCnt)) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; + } + + pushedStack.PushArgument(argNum); handled = true; } break; @@ -2347,6 +2375,12 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed if (makeInlineObservations) { + if (isInlining && (varNum >= impInlineInfo->argCnt)) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; + } + pushedStack.PushArgument(varNum); handled = true; } @@ -2610,7 +2644,7 @@ void Compiler::fgObserveInlineConstants(OPCODE opcode, const FgStack& stack, boo // Check for the double whammy of an incoming constant argument // feeding a constant test. unsigned varNum = FgStack::SlotTypeToArgNum(slot0); - if (impInlineInfo->inlArgInfo[varNum].argIsInvariant) + if ((varNum < impInlineInfo->argCnt) && impInlineInfo->inlArgInfo[varNum].argIsInvariant) { compInlineResult->Note(InlineObservation::CALLSITE_CONSTANT_ARG_FEEDS_TEST); } @@ -2652,7 +2686,7 @@ void Compiler::fgObserveInlineConstants(OPCODE opcode, const FgStack& stack, boo compInlineResult->Note(InlineObservation::CALLEE_ARG_FEEDS_TEST); unsigned varNum = FgStack::SlotTypeToArgNum(slot0); - if (impInlineInfo->inlArgInfo[varNum].argIsInvariant) + if ((varNum < impInlineInfo->argCnt) && impInlineInfo->inlArgInfo[varNum].argIsInvariant) { compInlineResult->Note(InlineObservation::CALLSITE_CONSTANT_ARG_FEEDS_TEST); } @@ -2663,7 +2697,7 @@ void Compiler::fgObserveInlineConstants(OPCODE opcode, const FgStack& stack, boo compInlineResult->Note(InlineObservation::CALLEE_ARG_FEEDS_TEST); unsigned varNum = FgStack::SlotTypeToArgNum(slot1); - if (impInlineInfo->inlArgInfo[varNum].argIsInvariant) + if ((varNum < impInlineInfo->argCnt) && impInlineInfo->inlArgInfo[varNum].argIsInvariant) { compInlineResult->Note(InlineObservation::CALLSITE_CONSTANT_ARG_FEEDS_TEST); } diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 925a3d7b77570a..a3441275d89a53 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -6926,6 +6926,12 @@ void Compiler::impImportBlockCode(BasicBlock* block) if (compIsForInlining()) { + if (lclNum >= impInlineInfo->argCnt) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; + } + op1 = impInlineFetchArg(impInlineInfo->inlArgInfo[lclNum], impInlineInfo->lclVarInfo[lclNum]); noway_assert(op1->OperIs(GT_LCL_VAR)); lclNum = op1->AsLclVar()->GetLclNum(); @@ -6933,6 +6939,11 @@ void Compiler::impImportBlockCode(BasicBlock* block) goto VAR_ST_VALID; } + if (lclNum >= info.compILargsCount) + { + BADCODE("Bad IL"); + } + lclNum = compMapILargNum(lclNum); // account for possible hidden param assertImp(lclNum < numArgs); @@ -6969,6 +6980,12 @@ void Compiler::impImportBlockCode(BasicBlock* block) LOC_ST: if (compIsForInlining()) { + if (lclNum >= info.compMethodInfo->locals.numArgs) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_LOCAL_NUMBER); + return; + } + lclTyp = impInlineInfo->lclVarInfo[lclNum + impInlineInfo->argCnt].lclTypeInfo; /* Have we allocated a temp for this local? */ @@ -7140,6 +7157,12 @@ void Compiler::impImportBlockCode(BasicBlock* block) if (compIsForInlining()) { + if (lclNum >= info.compMethodInfo->locals.numArgs) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_LOCAL_NUMBER); + return; + } + // Get the local type lclTyp = impInlineInfo->lclVarInfo[lclNum + impInlineInfo->argCnt].lclTypeInfo; @@ -7165,13 +7188,14 @@ void Compiler::impImportBlockCode(BasicBlock* block) LDARGA: JITDUMP(" %u", lclNum); - if (lclNum >= info.compILargsCount) - { - BADCODE("Bad IL"); - } - if (compIsForInlining()) { + if (lclNum >= impInlineInfo->argCnt) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; + } + // In IL, LDARGA(_S) is used to load the byref managed pointer of struct argument, // followed by a ldfld to load the field. @@ -7186,6 +7210,11 @@ void Compiler::impImportBlockCode(BasicBlock* block) goto _PUSH_ADRVAR; } + if (lclNum >= info.compILargsCount) + { + BADCODE("Bad IL"); + } + lclNum = compMapILargNum(lclNum); // account for possible hidden param assertImp(lclNum < numArgs); @@ -11531,7 +11560,7 @@ void Compiler::impLoadArg(unsigned ilArgNum, IL_OFFSET offset) { if (compIsForInlining()) { - if (ilArgNum >= info.compArgsCount) + if (ilArgNum >= impInlineInfo->argCnt) { compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); return; @@ -11553,7 +11582,7 @@ void Compiler::impLoadArg(unsigned ilArgNum, IL_OFFSET offset) } else { - if (ilArgNum >= info.compArgsCount) + if (ilArgNum >= info.compILargsCount) { BADCODE("Bad IL"); } @@ -14571,6 +14600,8 @@ void Compiler::impInlineInitVars(InlineInfo* pInlineInfo) unsigned Compiler::impInlineFetchLocal(unsigned lclNum DEBUGARG(const char* reason)) { assert(compIsForInlining()); + assert(lclNum < info.compMethodInfo->locals.numArgs); + assert(lclNum < MAX_INL_LCLS); unsigned tmpNum = impInlineInfo->lclTmpNum[lclNum]; diff --git a/src/tests/JIT/opt/Inline/regression/inlinee-il-bounds/inlinee-il-bounds.il b/src/tests/JIT/opt/Inline/regression/inlinee-il-bounds/inlinee-il-bounds.il new file mode 100644 index 00000000000000..aaeae9f2f76ca8 --- /dev/null +++ b/src/tests/JIT/opt/Inline/regression/inlinee-il-bounds/inlinee-il-bounds.il @@ -0,0 +1,281 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +.assembly extern System.Runtime {} +.assembly extern xunit.core {} + +.assembly inlinee_il_bounds {} + +.class public auto ansi beforefieldinit InlineeILBounds + extends [System.Runtime]System.Object +{ + .method public hidebysig static int32 Main() cil managed + { + .custom instance void [xunit.core]Xunit.FactAttribute::.ctor() = ( + 01 00 00 00 + ) + .entrypoint + .maxstack 2 + + call int32 InlineeILBounds::RunBadLdarg() + ldc.i4.s 100 + bne.un.s Failure + + call int32 InlineeILBounds::RunBadLdarg0() + ldc.i4.s 100 + bne.un.s Failure + + call int32 InlineeILBounds::RunBadLdarga() + ldc.i4.s 100 + bne.un.s Failure + + call int32 InlineeILBounds::RunBadStarg() + ldc.i4.s 100 + bne.un.s Failure + + call int32 InlineeILBounds::RunBadLdloca() + ldc.i4.s 100 + bne.un.s Failure + + call int32 InlineeILBounds::RunBadStloc() + ldc.i4.s 100 + bne.un.s Failure + + call int32 InlineeILBounds::RunBadGenericLdarg() + ldc.i4.s 100 + bne.un.s Failure + + ldc.i4.s 100 + ret + + Failure: + ldc.i4.0 + ret + } + + .method private hidebysig static int32 RunBadGenericLdarg() cil managed noinlining + { + .maxstack 1 + .try + { + call void InlineeILBounds::BadGenericLdarg() + leave.s Failed + } + catch [System.Runtime]System.InvalidProgramException + { + pop + leave.s Passed + } + + Passed: + ldc.i4.s 100 + ret + + Failed: + ldc.i4.0 + ret + } + + // Not inlined: shared generic code has a hidden generic context argument, so the + // IL argument index must be validated against the IL-visible argument count. + .method private hidebysig static void BadGenericLdarg() cil managed noinlining + { + .maxstack 1 + ldarg.0 + pop + ret + } + + .method private hidebysig static int32 RunBadLdarg() cil managed noinlining + { + .maxstack 1 + .try + { + ldc.i4.0 + call void InlineeILBounds::BadLdarg(int32) + leave.s Failed + } + catch [System.Runtime]System.InvalidProgramException + { + pop + leave.s Passed + } + + Passed: + ldc.i4.s 100 + ret + + Failed: + ldc.i4.0 + ret + } + + .method private hidebysig static int32 RunBadLdarga() cil managed noinlining + { + .maxstack 1 + .try + { + ldc.i4.0 + call void InlineeILBounds::BadLdarga(int32) + leave.s Failed + } + catch [System.Runtime]System.InvalidProgramException + { + pop + leave.s Passed + } + + Passed: + ldc.i4.s 100 + ret + + Failed: + ldc.i4.0 + ret + } + + .method private hidebysig static int32 RunBadLdarg0() cil managed noinlining + { + .maxstack 1 + .try + { + call void InlineeILBounds::BadLdarg0() + leave.s Failed + } + catch [System.Runtime]System.InvalidProgramException + { + pop + leave.s Passed + } + + Passed: + ldc.i4.s 100 + ret + + Failed: + ldc.i4.0 + ret + } + + .method private hidebysig static int32 RunBadStarg() cil managed noinlining + { + .maxstack 1 + .try + { + ldc.i4.0 + call void InlineeILBounds::BadStarg(int32) + leave.s Failed + } + catch [System.Runtime]System.InvalidProgramException + { + pop + leave.s Passed + } + + Passed: + ldc.i4.s 100 + ret + + Failed: + ldc.i4.0 + ret + } + + .method private hidebysig static int32 RunBadLdloca() cil managed noinlining + { + .maxstack 1 + .try + { + call void InlineeILBounds::BadLdloca() + leave.s Failed + } + catch [System.Runtime]System.InvalidProgramException + { + pop + leave.s Passed + } + + Passed: + ldc.i4.s 100 + ret + + Failed: + ldc.i4.0 + ret + } + + .method private hidebysig static int32 RunBadStloc() cil managed noinlining + { + .maxstack 1 + .try + { + call void InlineeILBounds::BadStloc() + leave.s Failed + } + catch [System.Runtime]System.InvalidProgramException + { + pop + leave.s Passed + } + + Passed: + ldc.i4.s 100 + ret + + Failed: + ldc.i4.0 + ret + } + + .method private hidebysig static void BadLdarg(int32 arg0) cil managed aggressiveinlining + { + .maxstack 1 + ldarg 65535 + brtrue.s Done + Done: + ret + } + + .method private hidebysig static void BadLdarga(int32 arg0) cil managed aggressiveinlining + { + .maxstack 1 + ldarga 65535 + pop + ret + } + + .method private hidebysig static void BadLdarg0() cil managed aggressiveinlining + { + .maxstack 1 + ldarg.0 + brtrue.s Done + Done: + ret + } + + .method private hidebysig static void BadStarg(int32 arg0) cil managed aggressiveinlining + { + .maxstack 1 + ldc.i4.0 + starg 65535 + ret + } + + .method private hidebysig static void BadLdloca() cil managed aggressiveinlining + { + .maxstack 1 + .locals init (int32 V_0) + ldloca 65535 + pop + ret + } + + .method private hidebysig static void BadStloc() cil managed aggressiveinlining + { + .maxstack 1 + .locals init (int32 V_0) + ldc.i4.0 + stloc 65535 + ret + } +} diff --git a/src/tests/JIT/opt/Inline/regression/inlinee-il-bounds/inlinee-il-bounds.ilproj b/src/tests/JIT/opt/Inline/regression/inlinee-il-bounds/inlinee-il-bounds.ilproj new file mode 100644 index 00000000000000..7d5898a5071dd9 --- /dev/null +++ b/src/tests/JIT/opt/Inline/regression/inlinee-il-bounds/inlinee-il-bounds.ilproj @@ -0,0 +1,10 @@ + + + 1 + + true + + + + + From 33b83f59c290666a29f91eebd9be7bb83d110dbb Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Wed, 2 Dec 2026 17:08:22 -0800 Subject: [PATCH 2/2] JIT: fail inlines for invalid argument observations Fail the inline rather than skipping the observation when an argument index is out of range. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c2f0cfcd-4917-4a8a-8e65-a771f4a12789 --- src/coreclr/jit/fgbasic.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/coreclr/jit/fgbasic.cpp b/src/coreclr/jit/fgbasic.cpp index bcc47865b8f77f..3d10e816a5ace7 100644 --- a/src/coreclr/jit/fgbasic.cpp +++ b/src/coreclr/jit/fgbasic.cpp @@ -2644,7 +2644,13 @@ void Compiler::fgObserveInlineConstants(OPCODE opcode, const FgStack& stack, boo // Check for the double whammy of an incoming constant argument // feeding a constant test. unsigned varNum = FgStack::SlotTypeToArgNum(slot0); - if ((varNum < impInlineInfo->argCnt) && impInlineInfo->inlArgInfo[varNum].argIsInvariant) + if (varNum >= impInlineInfo->argCnt) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; + } + + if (impInlineInfo->inlArgInfo[varNum].argIsInvariant) { compInlineResult->Note(InlineObservation::CALLSITE_CONSTANT_ARG_FEEDS_TEST); } @@ -2686,7 +2692,13 @@ void Compiler::fgObserveInlineConstants(OPCODE opcode, const FgStack& stack, boo compInlineResult->Note(InlineObservation::CALLEE_ARG_FEEDS_TEST); unsigned varNum = FgStack::SlotTypeToArgNum(slot0); - if ((varNum < impInlineInfo->argCnt) && impInlineInfo->inlArgInfo[varNum].argIsInvariant) + if (varNum >= impInlineInfo->argCnt) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; + } + + if (impInlineInfo->inlArgInfo[varNum].argIsInvariant) { compInlineResult->Note(InlineObservation::CALLSITE_CONSTANT_ARG_FEEDS_TEST); } @@ -2697,7 +2709,13 @@ void Compiler::fgObserveInlineConstants(OPCODE opcode, const FgStack& stack, boo compInlineResult->Note(InlineObservation::CALLEE_ARG_FEEDS_TEST); unsigned varNum = FgStack::SlotTypeToArgNum(slot1); - if ((varNum < impInlineInfo->argCnt) && impInlineInfo->inlArgInfo[varNum].argIsInvariant) + if (varNum >= impInlineInfo->argCnt) + { + compInlineResult->NoteFatal(InlineObservation::CALLEE_BAD_ARGUMENT_NUMBER); + return; + } + + if (impInlineInfo->inlArgInfo[varNum].argIsInvariant) { compInlineResult->Note(InlineObservation::CALLSITE_CONSTANT_ARG_FEEDS_TEST); }