Fix issue with return / goto from catch inside catch that exits the outer catch#120821
Merged
janvorli merged 3 commits intodotnet:mainfrom Oct 17, 2025
Merged
Fix issue with return / goto from catch inside catch that exits the outer catch#120821janvorli merged 3 commits intodotnet:mainfrom
janvorli merged 3 commits intodotnet:mainfrom
Conversation
When there is a try/catch inside a catch handler and there is a return in the inner catch or goto going out of the outer catch handler, we were resuming after the inner catch at the end of the method / goto target, so we were not returning from the call to the outer catch. We need to inject a catch leave island that the inner catch returns as its resume after catch location and that island can then return from the outer catch. This mechanism needs to be part of the finally call islands chain so that returns from catches / calls to finallys happen correctly and in the right order. This change extends the finally call islands chain to implement that. This change also fixes a problem with ordering of emission of the code for these islands and the basic blocks linking in the GenerateCode method. It also fixes a bug in GetNativeRangeForClause that was in some edge cases getting incorrectly shorter range. To make reviewing a bit easier, I've split this PR into two commits. The first adds the actual functionality and the second just renames things to match the fact that the islands are no longer finally call islands only.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in the interpreter where returns or gotos from an inner catch handler that exit an outer catch were not properly resuming execution after the outer catch. The fix introduces a "catch leave island" mechanism (similar to existing "finally call islands") to ensure proper control flow when leaving nested catch handlers.
Key changes:
- Introduced catch leave islands to handle leaves from catch handlers that exit outer catch regions
- Extended the finally call island chain to include catch leave islands, creating a unified "leave chain island" mechanism
- Fixed the ordering of code emission for islands and basic block linking in
GenerateCode - Fixed a bug in
GetNativeRangeForClausethat was returning incorrect ranges in edge cases
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/coreclr/interpreter/compiler.h | Renamed pFinallyCallIslandBB to pLeaveChainIslandBB to reflect that it handles both finally calls and catch leaves; added isFinallyCallIsland flag to distinguish island types |
| src/coreclr/interpreter/compiler.cpp | Implemented catch leave island creation, unified finally call and catch leave island handling, fixed GetNativeRangeForClause clause type check, and reordered island code generation before basic block linking |
Contributor
|
Tagging subscribers to this area: @BrzVlad, @janvorli, @kg |
davidwrighton
approved these changes
Oct 16, 2025
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
When there is a try/catch inside a catch handler and there is a return
in the inner catch or goto going out of the outer catch handler, we were
resuming after the inner catch at the end of the method / goto target,
so we were not returning from the call to the outer catch.
We need to inject a catch leave island that the inner catch returns as
its resume after catch location and that island can then return from the
outer catch.
This mechanism needs to be part of the finally call islands chain so that
returns from catches / calls to finallys happen correctly and in the
right order.
This change extends the finally call islands chain to implement that.
This change also fixes a problem with ordering of emission of the code
for these islands and the basic blocks linking in the
GenerateCodemethod.
It also fixes a bug in
GetNativeRangeForClausethat was in some edgecases getting incorrectly shorter range.
To make reviewing a bit easier, I've split this PR into two commits. The
first adds the actual functionality and the second just renames things
to match the fact that the islands are no longer finally call islands
only.