Skip to content

JIT: index filter blocks after EH table sorting - #135601

Merged
jkotas merged 6 commits into
dotnet:mainfrom
pcshrosbree:fix/jit-sort-filter-hndindex
Nov 25, 2026
Merged

jkotas merged 6 commits into
dotnet:mainfrom
pcshrosbree:fix/jit-sort-filter-hndindex

Conversation

@pcshrosbree

Copy link
Copy Markdown
Contributor

Fixes #135592.

fgFindBasicBlocks stamped the blocks of a filter with the clause's index before fgSortEHTable ran, and the post-sort loop that indexes handler blocks never revisited them. When the sort moved a filter clause, its filter blocks kept the pre-sort index, which after the sort names a different clause:

  • In FilterInEarlierFinally, the stale index names a finally clause. fgCheckBasicBlockControlFlow reports BADCODE("Unexpected endfilter"), and the method fails with InvalidProgramException.
  • In FilterInEarlierFilterHandler, the two filters retain each other's clause index. The main Checked JIT asserts block->bbHndIndex == blockHndIndex[block->bbNum] in fgVerifyHandlerTab; the main Release JIT rejects the method with InvalidProgramException.

ECMA-335 I.12.4.2.5 orders nested try blocks only; clauses nested in a handler may be listed in either order, which is what the sort exists to accept (its comment spells out the try { } catch { try { } catch { } } case).

Change

The filter stamping moves from the pre-sort scan into the post-sort loop, next to the handler stamping. The pre-sort scan still finds the endfilter block and checks that the filter immediately precedes its handler. Valid filters cannot contain nested EH regions, so the new loop assigns each filter block its clause's sorted index unconditionally.

For valid methods whose filter indices are already correct, this preserves the final handler indices. It corrects stale indices when sorting moves a filter clause.

Test

JIT/Regression/JitBlue/Runtime_135592 is an IL test that exercises explicit EH clause ordering. Its four scenario methods throw inside a nested try and return 1 when the nested handler catches. Three scenarios use filters; CatchInEarlierFinally is a catch-only control:

method EH table before the fix
FilterInEarlierFinally finally clause listed before the filter clause nested in its handler InvalidProgramException
FilterInEarlierFilterHandler outer filter clause listed before the filter clause nested in its handler InvalidProgramException (release); checked JIT asserts in fgVerifyHandlerTab
FilterInEarlierFinally_InnerFirst control: the compiler's order passes
CatchInEarlierFinally control: catch instead of filter passes

Validation

Checked JIT at main 8630d89, baseline vs this change, each test run under corerun:

lane (Checked JIT, main 8630d89) Runtime_135592 before after Runtime_135593 (PR for #135593)
linux-x64 InvalidProgramException from FilterInEarlierFinally returns 100 unchanged (fails before and after, as expected)
osx-arm64 InvalidProgramException from FilterInEarlierFinally returns 100 unchanged (fails before and after, as expected)
windows-x64 InvalidProgramException from FilterInEarlierFinally returns 100 unchanged (fails before and after, as expected)
windows-arm64 InvalidProgramException from FilterInEarlierFinally returns 100 unchanged (fails before and after, as expected)
linux-arm64 InvalidProgramException from FilterInEarlierFinally returns 100 unchanged (fails before and after, as expected)

The regression test for #135593 (separate PR) was run in the same lanes and is unaffected by this change, as expected.

With this change, JIT/Methodical/eh and JIT/Directed/ExcepFilters pass on linux-x64 Checked (168 tests, standalone). The standalone FilterInEarlierFinally repro is rejected on the shipped .NET 10.0.12 runtime on linux-x64 and osx-arm64, on 10.0.11 on windows-x64 and linux-arm64, and on the explicitly selected 10.0.9 and 10.0.11 runtimes on windows-arm64.

Note

This PR was prepared with AI assistance (Anthropic Claude and OpenAI Codex), under my direction. I reviewed it before posting.

fgFindBasicBlocks stamped filter blocks with their clause's index before
fgSortEHTable ran. The post-sort handler loop did not update those blocks,
so moving a filter clause left its blocks with a stale handler index.

In FilterInEarlierFinally, the stale index names a finally clause and the
method is rejected with InvalidProgramException. In
FilterInEarlierFilterHandler, the two filters retain each other's index;
the main Checked JIT asserts in fgVerifyHandlerTab and the main Release
JIT rejects the method with InvalidProgramException.

ECMA-335 I.12.4.2.5 orders nested try blocks; fgSortEHTable also orders
clauses nested in handlers. Move filter indexing into the post-sort loop
next to handler indexing. Valid filters cannot contain nested EH regions,
so each filter block receives its clause's sorted index unconditionally.

Fixes dotnet#135592
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 11, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 11, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Mono's JIT can leave the blocks of a filter or catch handler nested in an
earlier-listed handler in the enclosing handler's region. In CI the
merged Regression_5 work item crashed in Runtime_135592 on mono
windows-x64 (0xC0000005) and hung in it on mono linux-arm64. Shipped
Mono 8.0.28 reproduces both with FilterInEarlierFinally. The Mono LLVM
AOT compiler asserts on CatchInEarlierFinally (mini-llvm.c, `lhs' not
met), which fails the "AOT compile CoreCLR tests" step of the llvmaot
leg.

Mark the test ActiveIssue on Mono and MonoAotIncompatible. The Mono bug
is tracked in dotnet#135641.
… header

The project file's MonoAotIncompatible and the ActiveIssue attribute
already say why the test is disabled on Mono, so the header comment
repeated them.
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

/azp run runtime

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 135601 in repo dotnet/runtime

The Mono runtime-test legs build and run only Priority 0 tests, so
the attribute has had no effect since the test became Priority 1.
The project file keeps MonoAotIncompatible and the dotnet#135641 link.
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

@jkotas, a gentle ping: your suggestion to remove the Main ActiveIssue is in 15195fc. Is there anything else you would like changed before this merges?

@jkotas
jkotas requested a balanced review from Copilot November 25, 2026 02:26
@jkotas

jkotas commented Nov 25, 2026

Copy link
Copy Markdown
Member

LGTM @dotnet/jit-contrib PTLA

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The regression test remains enabled on Mono JIT despite known crashes and hangs, requiring a Mono-scoped ActiveIssue exclusion for #135641 alongside the existing AOT exclusion.

1 open issue
What changed in this PR

This PR fixes stale filter-block handler indices in the CoreCLR JIT when exception-handling clauses are reordered, addressing #135592.

Changes:

  • Assigns filter-block indices after exception-handling table sorting while preserving existing filter validation.
  • Adds four IL regression scenarios covering reordered filter clauses and control cases.
  • Configures the regression test as priority 1 with a Mono AOT exclusion.
File Description
src/​tests/​JIT/​Regression/​JitBlue/​Runtime_135592/​Runtime_135592.ilproj Registers the IL test and configures priority and Mono AOT exclusion.
src/​tests/​JIT/​Regression/​JitBlue/​Runtime_135592/​Runtime_135592.il Exercises nested filters and a catch-only control with explicit clause ordering.
src/​coreclr/​jit/​fgbasic.cpp Moves filter-block handler indexing to the post-sort pass.

Review effort: Balanced

Apply jkotas's suggestion to remove the obsolete Mono AOT exclusion.
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

@jkotas I've applied your suggestion and removed MonoAotIncompatible from the test. Could you approve again?

@jkotas
jkotas merged commit 8876fb4 into dotnet:main Nov 25, 2026
126 of 128 checks passed
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Thank you for the review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: a filter clause nested in an earlier-listed handler fails with "Unexpected endfilter" (stale handler index after fgSortEHTable)

4 participants