Skip to content

Report the region and handler kind for nested endfinally - #136055

Merged
jkotas merged 4 commits into
dotnet:mainfrom
pcshrosbree:fix/ilverify-endfinally-endfault-codes
Nov 25, 2026
Merged

jkotas merged 4 commits into
dotnet:mainfrom
pcshrosbree:fix/ilverify-endfinally-endfault-codes

Conversation

@pcshrosbree

@pcshrosbree pcshrosbree commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: Since #136029, ILVerify rejects endfinally/endfault in some nested blocks, but its error message describes the wrong location. This PR adds four error codes that name the nested region and the handler kind. They go at the end of VerifierError, so existing values stay unchanged. It updates four tests and adds four.

Follow-up to #136029. That PR made ILVerify reject endfinally/endfault when the innermost enclosing block is a try block or filter nested in a finally or fault handler. It reports the existing Endfinally error, whose message, "Endfinally/endfault from outside a finally or fault handler.", has to cover every case. In this case the instruction is inside a finally or fault handler, so the message does not describe the problem.

This change reports four new errors for that case, one for each combination of the nested region and the handler's kind:

Where the instruction is Error
not in any handler, or in a catch or filter handler Endfinally (unchanged)
in a try block nested in a finally handler EndfinallyInNestedTry: "Endfinally inside a try block nested in a finally handler."
in a filter nested in a finally handler EndfinallyInNestedFilter: "Endfinally inside a filter nested in a finally handler."
in a try block nested in a fault handler EndfaultInNestedTry: "Endfault inside a try block nested in a fault handler."
in a filter nested in a fault handler EndfaultInNestedFilter: "Endfault inside a filter nested in a fault handler."

The new error codes are added at the end of VerifierError, so no existing value changes. Only methods that #136029 started rejecting report the new errors; before #136029, ILVerify reported no error for these instructions.

Why four errors

The C# compiler does not use endfinally/endfault to exit a nested try block or filter. But C# code puts try blocks and filters inside finally handlers all the time: a using or lock statement in a finally block compiles to a try block nested in the handler, and a catch (...) when (...) clause there compiles to a nested filter. Tools that rewrite IL, such as profilers, weavers and obfuscators, and code that generates IL with System.Reflection.Emit, work on these shapes. A rewriter can leave the original endfinally in a region it added or rebuilt.

The two regions point to different mistakes, so the error names the region:

  • In a nested try block, a possible cause is a rewriter that wrapped part of a finally body in its own try block and left the original endfinally inside the wrapper. The fix is to leave the try block and run endfinally after it.
  • In a nested filter, the filter's code leaves with endfinally, which can never end a filter. The fix is endfilter.

The handler's kind is named as well, as the leave checks in CheckIsValidLeaveTarget already do with LeaveOutOfFinally and LeaveOutOfFault. endfinally and endfault are the same opcode, so the instruction's name in each message follows the handler, not what the source spelled. The leave checks also have an error for each region, such as LeaveIntoTry and LeaveIntoFilter.

Which region is reported

The filter is checked first. A block in a filter can also be in a try block that encloses the filter's clause. When both are nested in the handler, the filter is the innermost region, because ILVerify already rejects any region nested inside a filter (EHClauseNesting). Checking the try block first would name the wrong region.

Tests

In ExceptionRegionTests.il:

  • The four nested tests from Fix ILVerify endfinally check for nested regions #136029 now expect the error for their region and handler kind.
  • Finally.EndfinallyInFilterInNestedTry_Invalid_EndfinallyInNestedFilter is new. Its endfinally is in a filter whose clause is inside a try block nested in a finally handler, so it pins the requirement to check the filter before the try block.
  • Three new tests pin where Endfinally still applies:
    • Filter.EndfinallyInFilter_Invalid_Endfinally: endfinally in a filter that no handler encloses;
    • Filter.HandlerEndsWithEndfinally_Invalid_Endfinally: endfinally at the end of a filter clause's handler, which is neither a finally nor a fault handler;
    • Fault.EndfaultInCatchInFault_Invalid_Endfinally: endfault in a catch nested in a fault handler, where the innermost handler is the catch.

Validation

I used main at 66368e9 (the merge of #136029) as the base, with linux-x64 and SDK 11.0.100-rc.1.26420.103. The counts are the test run's TRX totals. No test was skipped.

Tree Result (passed/total)
main unchanged 513/513
this change 517/517

After the rebase onto main at b3c0988, the test run passes 572/572. The 517-test results in this section are from before the rebase. On main, #136133 had added two codes at the end of VerifierError, so this PR's four codes now follow them.

To check that the tests can fail, I made temporary edits, one at a time. These edits are not part of this change. Below, "nested-filter tests" are the three tests whose endfinally/endfault is in a nested filter, and "nested-try tests" are the two whose instruction is in a nested try block.

  • Reporting the finally codes for every nested case: 515/517. Only the two nested tests in fault handlers fail, each reporting the finally code. Reporting the fault codes for every nested case: 514/517. Only the three nested tests in finally handlers fail.
  • Swapping the finally and fault codes in the filter branch: 514/517, and only the three nested-filter tests fail. In the try branch: 515/517, and only the two nested-try tests fail.
  • Reporting the try codes from the filter branch: 514/517, and only the three nested-filter tests fail. Reporting the filter codes from the try branch: 515/517, and only the two nested-try tests fail.
  • Checking the try block before the filter: 516/517. Only Finally.EndfinallyInFilterInNestedTry fails, and it reports EndfinallyInNestedTry.
  • Disabling the check for a nested filter: 514/517. The three nested-filter tests fail: the ordering test reports EndfinallyInNestedTry and the other two report no error. Disabling the check for a nested try block: 515/517. The two nested-try tests fail with no error.
  • Removing the handler-kind check, so that catch and filter-clause handlers reach the checks for a nested filter or try block: 514/517. Only Catch.EndsWithEndfinally, Fault.EndfaultInCatchInFault and Filter.HandlerEndsWithEndfinally fail, each with no error.
  • Accepting a filter clause's handler as well as finally and fault handlers: 516/517. Only Filter.HandlerEndsWithEndfinally fails, with no error. Separately, accepting a catch handler as well as finally and fault handlers: 515/517. Only Catch.EndsWithEndfinally and Fault.EndfaultInCatchInFault fail, each with no error.
  • I tested all eight new or renamed tests in separate runs, changing the expected error encoded in one test name per run. Each run passed 516/517 tests; only the test with the changed name failed.

Note

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

@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 19, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jkotas jkotas added area-Tools-ILVerification Issues related to ilverify tool and IL verification in general and removed area-VM-coreclr labels Oct 19, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

When the innermost block enclosing endfinally/endfault is a try block
or filter nested in a finally or fault handler, report one of four new
errors instead of Endfinally: EndfinallyInNestedTry,
EndfinallyInNestedFilter, EndfaultInNestedTry or
EndfaultInNestedFilter. Endfinally's message did not fit that case,
because the instruction is inside the handler.

The region points to different mistakes in IL rewriters: a nested try
needs a leave, a nested filter needs endfilter. The filter is tested
first: nothing can be nested in a filter, so a filter nested in the
handler is always the innermost region.

The four nested tests expect the new errors. A new test uses a filter
inside a nested try to verify that the filter check takes precedence.
Three new tests pin the cases that still report Endfinally: endfinally
in a filter that no handler encloses, endfinally in a filter clause's
handler, and endfault in a catch nested in a fault.
@pcshrosbree
pcshrosbree force-pushed the fix/ilverify-endfinally-endfault-codes branch from 2964d45 to d0e8665 Compare October 21, 2026 07:26
@jkotas
jkotas requested a balanced review from Copilot November 21, 2026 07:02

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 new entries in Strings.resx break the surrounding alphabetical order; Endfault* should precede Endfilter, and NestedFilter should precede NestedTry within each handler kind.

1 open issue
What changed in this PR

This follow-up to #136029 makes ILVerify diagnostics identify the nested region and handler kind for invalid endfinally/endfault instructions, without changing which instructions are rejected.

Changes:

  • Append four error codes, preserving existing enum values, and add matching messages.
  • Report nested filters before nested try blocks so diagnostics name the innermost region.
  • Update four tests and add four covering diagnostic precedence and unchanged Endfinally cases.
File Description
src/​coreclr/​tools/​ILVerification/​VerifierError.cs Append four region- and handler-specific error codes.
src/​coreclr/​tools/​ILVerification/​Strings.resx Add messages for the new error codes.
src/​coreclr/​tools/​ILVerification/​ILImporter.Verify.cs Select diagnostics by nested region and handler kind.
src/​coreclr/​tools/​ILVerification.Tests/​ILTests/​ExceptionRegionTests.il Update expectations and add diagnostic regression cases.

Review effort: Balanced

…al order

Moves the four new entries in Strings.resx so the block stays sorted:
EndfaultInNested* before Endfilter, and NestedFilter before NestedTry.
The VerifierError values are unchanged.
@jkotas

jkotas commented Nov 25, 2026

Copy link
Copy Markdown
Member

/ba-g dead letter

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.

🟢 Approval recommended

No active review issues were identified for the ILVerify nested-region diagnostic update.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@jkotas
jkotas merged commit 010f1ff into dotnet:main Nov 25, 2026
107 of 109 checks passed
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Thank you for the review and merge!

@pcshrosbree
pcshrosbree deleted the fix/ilverify-endfinally-endfault-codes branch November 25, 2026 18:40
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Tools-ILVerification Issues related to ilverify tool and IL verification in general 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.

3 participants