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

[DAM analyzer] Support exceptional control flow - #2481

Merged
sbomer merged 12 commits into
dotnet:feature/damAnalyzerfrom
sbomer:exceptionalControlFlow
Jan 13, 2022
Merged

sbomer merged 12 commits into
dotnet:feature/damAnalyzerfrom
sbomer:exceptionalControlFlow

Conversation

@sbomer

@sbomer sbomer commented Jan 6, 2022 •

Copy link
Copy Markdown
Member

This includes a change to make the analyzer tests fail when an exception is thrown.

This models exceptional control flow by maintaining an exception state for try/catch regions, which is updated to include all possible states encountered within that region. This exceptional state is propagated from try regions to the beginning of catch regions, and from try or catch regions to the beginning of finally regions.

Finally regions are represented as additional information for each predecessor edge in the control flow graph, following the data model given by Roslyn's CFG. Each edge includes a list of finally regions through which control flows as part of the edge before reaching its destination. For now, this approach inefficiently propagates the old finally state along these chains of finally regions when it encounters them. This has the downside that long finally regions will cause re-analysis until each finally block sees a new state.

The finally state is also unified along all such paths, so the warning state at the exit of a finally is a conservative approximation that accounts for all normal control flow leading to the finally region.

Additionally, the finally regions are analyzed separately for the case when an exception causes a finally block to be reached. Inside the finally region, this will produce a superset of the warnings for normal control flow inside the same region. However this exceptional finally state does not flow out of the finally, to avoid spurious warnings in code that is unreachable when executing finally blocks due to exceptions.

This doesn't yet include any handling for filter regions. I'll work on that next, possibly in a follow-up change.

@sbomer
sbomer force-pushed the exceptionalControlFlow branch from a85eb7a to bb26622 Compare January 6, 2022 17:54
@sbomer sbomer changed the title [DAM analyzer] [WIP] Support exceptional control flow [DAM analyzer] Support exceptional control flow Jan 11, 2022
@sbomer
sbomer marked this pull request as ready for review January 11, 2022 19:14
@sbomer
sbomer requested a review from marek-safar as a code owner January 11, 2022 19:14
@sbomer
sbomer requested review from agocke and vitek-karas January 11, 2022 19:14

@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!

switch (operation.Target) {
case ILocalReferenceOperation localRef:
state.Set (new LocalKey (localRef.Local), value);
state.Current.Set (new LocalKey (localRef.Local), value);

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.

In both places the Set should affect both the current and exception values. Why not add a Set method onto the state itself which does this automatically.

public override TValue VisitFlowCaptureReference (IFlowCaptureReferenceOperation operation, DataFlowState<LocalState<TValue>> state)
{
return state.Get (new LocalKey (operation.Id));
return state.Current.Get (new LocalKey (operation.Id));

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.

Similarly for the get - there should be no place where we ask for a specific local value on the exception state. So let's just add a Get method onto the state which returns the current.

Share code in ControlFlowGraphProxy

Implement wrapper type where Set automatically meets the exception state.
The Set operation is specific to the local dataflow analysis, so this introduces
a state interface to track the current/exception state. Only the state type for
the local dataflow analysis (LocalDataFlowState) implements this specialized
behavior.
@sbomer

sbomer commented Jan 13, 2022

Copy link
Copy Markdown
Member Author

I'm going to go ahead and merge this to unblock changes that depend on the analyzer not throwing exceptions on existing tests - if there's any more feedback, feel free to leave it here and I will address it in a follow-up.

@sbomer
sbomer merged commit b3c58ad into dotnet:feature/damAnalyzer Jan 13, 2022
@sbomer
sbomer deleted the exceptionalControlFlow branch February 22, 2022 18:13
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
This includes a change to make the analyzer tests fail when an
exception is thrown.

This models exceptional control flow by maintaining an exception state
for try/catch regions, which is updated to include all possible states
encountered within that region. This exceptional state is propagated
from try regions to the beginning of catch regions, and from try or
catch regions to the beginning of finally regions.

Finally regions are represented as additional information for each
predecessor edge in the control flow graph, following the data model
given by Roslyn's CFG. Each edge includes a list of finally regions
through which control flows as part of the edge before reaching its
destination. For now, this approach inefficiently propagates the old
finally state along these chains of finally regions when it encounters
them. This has the downside that long finally regions will cause
re-analysis until each finally block sees a new state.

The finally state is also unified along all such paths, so the warning
state at the exit of a finally is a conservative approximation that
accounts for all normal control flow leading to the finally region.

Additionally, the finally regions are analyzed separately for the case
when an exception causes a finally block to be reached. Inside the
finally region, this will produce a superset of the warnings for
normal control flow inside the same region. However this exceptional
finally state does not flow out of the finally, to avoid spurious
warnings in code that is unreachable when executing finally blocks due
to exceptions.

Also, implement a wrapper type where Set automatically meets the
exception state.  The Set operation is specific to the local dataflow
analysis, so this introduces a state interface to track the
current/exception state. Only the state type for the local dataflow
analysis (LocalDataFlowState) implements this specialized behavior.

Commit migrated from dotnet/linker@b3c58ad
sbomer added a commit to dotnet/runtime that referenced this pull request Nov 1, 2023
…94101)

Fixes a bug I encountered while adding some new tests for feature
checks.

While analyzing finally regions in the mode where they will not
exit normally, we weren't correctly propagating dataflow states
through nested finally regions. See
dotnet/linker#2481 for original context
around the analysis done here.
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.

2 participants