Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

[DAM analyzer] Analyze exception filters - #2593

Merged
vitek-karas merged 6 commits into
dotnet:feature/damAnalyzerfrom
sbomer:exceptionFilters
Feb 16, 2022
Merged

vitek-karas merged 6 commits into
dotnet:feature/damAnalyzerfrom
sbomer:exceptionFilters

Conversation

@sbomer

@sbomer sbomer commented Feb 8, 2022 •

Copy link
Copy Markdown
Member

This adds support for exception filters in the analyzer. Now the exception state will flow from from try blocks to any catch blocks without filters, or to the filter regions of any catch blocks that do have filters. Additionally, the dataflow state flows from a filter region to the next filter or catch. We don't check whether the exception type of the filter regions match, but conservatively assume that control can flow out of any filter to the next filter or catch.

This also assumes that control leaves the filter region from its last block - I don't know if there are cases where a filter region can contain more than one basic block, so if this is possible the logic might need to be updated.

I added a few testcases with dataflow and side effects in filter regions. This fixes #2415 and allows enabling two more of the previously failing tests.

Also add a catch clause to testcase to test propagation from filter to catch.
To a later catch or filter region. This should check all previous filter
clauses, not just the immediately preceding one.
Also update the testcase to show that both previous
filter states reach a subsequent filter state.
@MichalStrehovsky

Copy link
Copy Markdown
Member

This also assumes that control leaves the filter region from its last block - I don't know if there are cases where a filter region can contain more than one basic block, so if this is possible the logic might need to be updated.

From the IL perspective (not sure how much that is relevant to the analyzer), a ​valid filter region will have exactly one endfilter instruction and it will be the last instruction of the filter block.

There can be multiple basic blocks within the filter but they all have to lead to the single endfilter:

Type academicalSituation = null;
try { throw new Exception(); }
catch (Exception ex) when ((ex is NullReferenceException ? (academicalSituation = typeof(object)) : (academicalSituation = typeof(string))) == null) { }
``

@sbomer

sbomer commented Feb 9, 2022

Copy link
Copy Markdown
Member Author

Thanks for the example @MichalStrehovsky, I added a test similar to it. It looks like Roslyn does insert a final basic block that merges the two branches and is the source for the fall-through case (where control doesn't enter the corresponding handler), so the assumption holds at least for this case.

@sbomer sbomer self-assigned this Feb 9, 2022
@sbomer

sbomer commented Feb 10, 2022 •

Copy link
Copy Markdown
Member Author

@tlakollo I addressed the case you pointed out where a filter may throw an exception that is caught by a later catch, PTAL. Thanks for noticing this!

@vitek-karas vitek-karas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good

@vitek-karas
vitek-karas merged commit 60bce42 into dotnet:feature/damAnalyzer Feb 16, 2022
@sbomer
sbomer deleted the exceptionFilters branch February 22, 2022 18:13
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
This adds support for exception filters in the analyzer. Now the exception state will flow from from try blocks to any catch blocks without filters, or to the filter regions of any catch blocks that do have filters. Additionally, the dataflow state flows from a filter region to the next filter or catch. We don't check whether the exception type of the filter regions match, but conservatively assume that control can flow out of any filter to the next filter or catch.

This also assumes that control leaves the filter region from its last block - I don't know if there are cases where a filter region can contain more than one basic block, so if this is possible the logic might need to be updated.

I added a few testcases with dataflow and side effects in filter regions. This fixes dotnet/linker#2415 and allows enabling two more of the previously failing tests.

Commit migrated from dotnet/linker@60bce42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants