Fix nested exception handler reachability in ILLink#131236
Merged
Merged
Conversation
Rescan exception handlers until no newly reachable handler bodies expose additional nested protected regions. Add an async nested-finally regression test for the trimming failure. Assisted-by: GitHub Copilot:gpt-5.6-sol Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2cee0e07-8e06-4275-b1de-508fe931bf95
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/illink |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes ILLink’s unreachable-block analysis for exception handlers by repeatedly discovering newly reachable handler bodies until reaching a fixed point, preventing nested handlers (e.g., nested finally blocks in async state machines) from being misclassified as unreachable and incorrectly removed.
Changes:
- Update
UnreachableBlocksOptimizerreachability analysis to iteratively enqueue newly reachable exception handler (and filter) blocks until no additional handlers become reachable. - Add a regression test case exercising an async method with nested
finallyblocks underipconstprop. - Add a dependency library used by the new test to reproduce the nested-handler shape.
Show a summary per file
| File | Description |
|---|---|
| src/tools/illink/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs | Reworks exception-handler reachability to rescan handlers and traverse newly reachable handler bodies, enabling correct discovery of nested protected regions. |
| src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBlock/NestedFinallyInAsyncMethod.cs | New test driver enabling ipconstprop and asserting nested-cleanup executes (via a counter). |
| src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBlock/Dependencies/NestedFinallyInAsyncMethod_Lib.cs | New dependency library containing an async method with nested finally blocks to reproduce the scenario. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 0
jtschuster
reviewed
Jul 22, 2026
jtschuster
reviewed
Jul 22, 2026
jtschuster
reviewed
Jul 22, 2026
jtschuster
approved these changes
Jul 22, 2026
jtschuster
left a comment
Member
There was a problem hiding this comment.
Source changes look good to me, just wanted some clarifications on why tests are the way they are.
Replace the async dependency-based repro with a synchronous nested-finally case that exercises the same handler reachability bug. Track the test as an expected ILTrim limitation because ILTrim does not implement ipconstprop. Assisted-by: GitHub Copilot:gpt-5.6-sol Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2cee0e07-8e06-4275-b1de-508fe931bf95
Contributor
There was a problem hiding this comment.
Copilot's findings
Comments suppressed due to low confidence (1)
src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBlock/NestedFinallyInFinallyHandler.cs:52
- After removing
CleanupCount,Cleanupshould no longer reference it. KeepingCleanupas a no-op still allows the test to validate that the nested finally body remains reachable (via the existing[Kept]expectation onCleanup).
static void Cleanup() => CleanupCount++;
- Files reviewed: 3/3 changed files
- Comments generated: 2
hydraxman
reviewed
Jul 23, 2026
Avoid repeating linear instruction lookups during fixed-point handler discovery. Strengthen the regression with a third nested finally and rely on linker expectations instead of an unexecuted runtime counter. Assisted-by: GitHub Copilot:gpt-5.6-sol Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2cee0e07-8e06-4275-b1de-508fe931bf95
sbomer
enabled auto-merge (squash)
July 23, 2026 17:40
jtschuster
approved these changes
Jul 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix exception-handler reachability analysis in the unreachable blocks optimizer by rescanning handlers until no newly reachable handler bodies expose additional nested protected regions.
Previously, handlers were scanned only once. A nested handler could therefore be classified as unreachable before its enclosing handler body was traversed, causing required cleanup code to be removed.
Add a regression test covering an
asyncmethod with nestedfinallyblocks.Validation:
UnreachableBlocktests: 22 passedipconstpropenabled and disabledFixes #131088
Note
This content was created with assistance from AI (GitHub Copilot).