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
+
+
+
+
+