From 15cca119a2f3daeb17d36b503c80d964d9578672 Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Wed, 2 Dec 2026 16:16:45 -0800 Subject: [PATCH] JIT: reject filters starting at the end of the method A filter offset equal to the IL code size passes validation, but the block lookup returns null and the JIT dereferences it. Reject the offset and check the lookup result before using it. Add an IL regression test that expects InvalidProgramException. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/coreclr/jit/fgbasic.cpp | 8 ++- src/tests/JIT/Directed/Directed_d.csproj | 1 + .../coverage/importer/filteroffset.il | 50 +++++++++++++++++++ .../coverage/importer/filteroffset.ilproj | 8 +++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/tests/JIT/Directed/coverage/importer/filteroffset.il create mode 100644 src/tests/JIT/Directed/coverage/importer/filteroffset.ilproj diff --git a/src/coreclr/jit/fgbasic.cpp b/src/coreclr/jit/fgbasic.cpp index 1d6ccf1289853f..d606b815d57d3a 100644 --- a/src/coreclr/jit/fgbasic.cpp +++ b/src/coreclr/jit/fgbasic.cpp @@ -3499,9 +3499,9 @@ void Compiler::fgFindBasicBlocks() if (clause.Flags & CORINFO_EH_CLAUSE_FILTER) { - if (clause.FilterOffset > info.compILCodeSize) + if (clause.FilterOffset >= info.compILCodeSize) { - BADCODE("filter offset > codesize"); + BADCODE("filter offset >= codesize"); } jumpTarget->bitVectSet(clause.FilterOffset); } @@ -3723,6 +3723,10 @@ void Compiler::fgFindBasicBlocks() if (clause.Flags & CORINFO_EH_CLAUSE_FILTER) { filtBB = HBtab->ebdFilter = fgLookupBB(clause.FilterOffset); + if (filtBB == nullptr) + { + BADCODE("Filter Clause is invalid"); + } filtBB->SetCatchType(BBCT_FILTER); hndBegBB->SetCatchType(BBCT_FILTER_HANDLER); diff --git a/src/tests/JIT/Directed/Directed_d.csproj b/src/tests/JIT/Directed/Directed_d.csproj index fc2213d756ad44..3bde810ff96ab2 100644 --- a/src/tests/JIT/Directed/Directed_d.csproj +++ b/src/tests/JIT/Directed/Directed_d.csproj @@ -10,6 +10,7 @@ + diff --git a/src/tests/JIT/Directed/coverage/importer/filteroffset.il b/src/tests/JIT/Directed/coverage/importer/filteroffset.il new file mode 100644 index 00000000000000..1146b788da6fbd --- /dev/null +++ b/src/tests/JIT/Directed/coverage/importer/filteroffset.il @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +.assembly filteroffset {} +.assembly extern xunit.core {} +.assembly extern mscorlib {} + +.class public auto ansi Test +{ + .method public static int32 Main() cil managed + { + .custom instance void [xunit.core]Xunit.FactAttribute::.ctor() = ( + 01 00 00 00 + ) + .entrypoint + .try + { + call void Test::BadFilter() + leave.s Fail + } + catch [mscorlib]System.InvalidProgramException + { + pop + leave.s Pass + } + + Pass: + ldc.i4 100 + ret + Fail: + ldc.i4.0 + ret + } + + .method private static void BadFilter() cil managed noinlining + { + .maxstack 1 + + TryStart: + leave.s MethodEnd + HandlerStart: + pop + leave.s MethodEnd + MethodEnd: + ret + FilterStart: + + .try TryStart to HandlerStart filter FilterStart handler HandlerStart to MethodEnd + } +} diff --git a/src/tests/JIT/Directed/coverage/importer/filteroffset.ilproj b/src/tests/JIT/Directed/coverage/importer/filteroffset.ilproj new file mode 100644 index 00000000000000..b9a393b2470e4b --- /dev/null +++ b/src/tests/JIT/Directed/coverage/importer/filteroffset.ilproj @@ -0,0 +1,8 @@ + + + 1 + + + + +