Skip to content

JIT: reject method entry inside handlers or filters - #135602

Merged
jkotas merged 5 commits into
dotnet:mainfrom
pcshrosbree:fix/jit-method-entry-in-handler
Oct 15, 2026
Merged

jkotas merged 5 commits into
dotnet:mainfrom
pcshrosbree:fix/jit-method-entry-in-handler

Conversation

@pcshrosbree

@pcshrosbree pcshrosbree commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

IL offset 0 must not lie inside a handler or filter: entry into these regions is controlled by the exception-handling mechanism (ECMA-335 I.12.4.2.8.1). The JIT had no explicit check for this. On linux-x64, the tested EntryInFinally, EntryInFault and EntryInCatch methods compile and return on the main Release JIT, while the main Checked JIT asserts block->bbRefs == blockRefs in fgDebugCheckBBlist. On the shipped .NET 10.0.12 runtime on linux-x64, those methods compile and invocation overflows the stack. The tested catch and filter variants beginning with pop are already rejected by the importer's stack-underflow check.

Change

fgFindBasicBlocks now rejects the method while reading the EH clauses: a handler that starts at offset 0, or a filter that starts at offset 0, contains the method entry. The check sits next to the existing "end of try/handler beyond end of method" checks and reports InvalidProgramException through BADCODE3 like they do.

Test

There is no test: it was dropped in review, as on #135827.

Validation

Checked JIT at main 8630d89, baseline vs this change, under corerun. The measurement used an IL program, since dropped from the PR, with four methods whose first instruction is inside a finally, a fault, a catch (one that does not pop, so the entry point is the only defect) and a filter. The program attempted to compile each method through RuntimeHelpers.PrepareMethod, never calling it, and returned 100 only if all four were rejected. The last column is the test from the PR for #135592:

lane (Checked JIT, main 8630d89) before after Runtime_135592 (PR for #135592)
linux-x64 assert block->bbRefs == blockRefs (fgdiagnostic.cpp) on EntryInFinally returns 100 unchanged (fails before and after, as expected)
osx-arm64 assert block->bbRefs == blockRefs (fgdiagnostic.cpp) on EntryInFinally returns 100 unchanged (fails before and after, as expected)
windows-x64 assert block->bbRefs == blockRefs (fgdiagnostic.cpp) on EntryInFinally returns 100 unchanged (fails before and after, as expected)
windows-arm64 assert block->bbRefs == blockRefs (fgdiagnostic.cpp) on EntryInFinally returns 100 unchanged (fails before and after, as expected)
linux-arm64 assert block->bbRefs == blockRefs (fgdiagnostic.cpp) on EntryInFinally returns 100 unchanged (fails before and after, as expected)

Behaviour of the standalone EntryInFinally, EntryInFault and EntryInCatchNoPop repros from #135593, using RuntimeHelpers.PrepareMethod followed by a call if compilation succeeds, one method per process, on an explicitly selected shipped runtime:

platform .NET runtime EntryInFinally EntryInFault EntryInCatchNoPop
linux-x64 10.0.12 compiles; the call overflows the stack compiles; the call overflows the stack compiles; the call overflows the stack
osx-arm64 10.0.12 compiles; the call crashes (SIGTRAP) compiles; the call crashes (SIGTRAP) compiles; the call crashes (SIGTRAP)
windows-x64 10.0.11 compiles; the call fails fast (0xC0000409) compiles; the call fails fast (0xC0000409) compiles; the call fails fast (0xC0000409)
windows-arm64 10.0.9, 10.0.11 InvalidProgramException InvalidProgramException InvalidProgramException
linux-arm64 10.0.11 InvalidProgramException InvalidProgramException InvalidProgramException

On windows-arm64, 11.0.0-preview.5.26302.115 fails all three with OutOfMemoryException from PrepareMethod.

With the fix, every lane's checked JIT rejects all four methods with InvalidProgramException, and the release JIT on linux-x64 does too.

Resolves #135593

Note

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

IL offset 0 must not lie inside a handler or filter. Entry into these
regions is controlled by the exception-handling mechanism
(ECMA-335 I.12.4.2.8.1).

The tested EntryInFinally, EntryInFault and EntryInCatch methods compile
and return on the main Release JIT on linux-x64, while the main Checked
JIT asserts 'block->bbRefs == blockRefs' in fgDebugCheckBBlist. On the
shipped .NET 10.0.12 runtime on linux-x64, those methods compile and
invocation overflows the stack. The tested filter beginning with pop is
already rejected by the importer.

Reject method entry inside a handler or filter in fgFindBasicBlocks while
reading the EH clauses. A handler beginning at offset 0, or a filter
beginning at offset 0, contains the method entry.

The IL regression test uses RuntimeHelpers.PrepareMethod and requires
InvalidProgramException for all four cases without invoking them.

Fixes dotnet#135593
@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
@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
@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 RuntimeHelpers.PrepareMethod compiles nothing, so this test
cannot observe a rejection on Mono: every scenario reports "compiled"
and the test returns 101. Mono's handling of these methods is tracked
in dotnet#135639: its JIT rejects only EntryInFilter, and its LLVM AOT
compiler asserts on EntryInFinally (mini-llvm.c, `handler_bb`), which
fails the "AOT compile CoreCLR tests" step of the llvmaot leg.

Mark the test ActiveIssue on Mono and MonoAotIncompatible, the same pair
that jit64/localloc/ehverify/eh07_large uses for its invalid IL.
… 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

Applied the same two changes as on #135601 in ae1af9d: the Mono note is out of the .il header and the test is Priority 1.

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#135639 link.
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Also removed the ActiveIssue on Main in ee89b28, as on #135601: the Mono runtime test legs run only Priority 0 tests, so it had no effect since the switch to Priority 1.

Note

This reply was prepared with AI assistance (Anthropic Claude, checked by OpenAI Codex) at my request. I reviewed it before posting.

@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Following the review on #135827, where the invalid-IL test was dropped: I'm happy to do the same here and keep only the fgbasic.cpp change. One difference: #135827 moved an existing check, while this PR adds a handler-offset check for each EH clause and a filter-offset check for filter clauses in fgFindBasicBlocks. Let me know whether you'd like the test dropped, or would rather not take the change.

Note

This comment was prepared with AI assistance (Anthropic Claude, checked by OpenAI Codex) at my request. I reviewed it before posting.

@jkotas

jkotas commented Oct 15, 2026

Copy link
Copy Markdown
Member

I think it is ok to take this (for consistency with what's there already), without the tests.

Drop Runtime_135593 as requested in review. The PR now contains only
the new check in fgFindBasicBlocks.
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Done in 20a08e2.

@jkotas
jkotas merged commit 281925e into dotnet:main Oct 15, 2026
128 of 130 checks passed
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Thank you for the review and merge!

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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 method whose first instruction is inside a finally or fault handler compiles and overflows the stack when called

2 participants