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

Remove AnalysisContext and ScopeStack from method body scanner - #2747

Merged
sbomer merged 12 commits into
dotnet:mainfrom
sbomer:scope
Apr 21, 2022
Merged

sbomer merged 12 commits into
dotnet:mainfrom
sbomer:scope

Conversation

@sbomer

@sbomer sbomer commented Apr 19, 2022

Copy link
Copy Markdown
Member

Motivation:

For compiler-generated code dataflow, we may need to analyze a compiler-generated method multiple times as part of the closure of methods reached from user code. This is necessary because as we analyze this closure of methods we might find new captured state that flows into a previously analyzed method in the same closure.

To avoid spurious warnings, we need a deduplication mechanism similar to what we do in the analyzer. The eventual goal is for the linker to have something similar to the analyzer's TrimAnalysisPatternStore that deduplicates warnings produced from multiple scans of the same method body.

We also might need the ability to run the method body scanner without producing any warnings, when analyzing compiler-generated methods reachable by reflection. (The linker will warn about reflection over compiler-generated code, and won't produce the full set of diagnostics for these methods, but still needs to scan them to ensure dependencies are kept).

The first step is to remove some levels of indirection around the message origins, to make it easier to cache messages by origin in the future.

This change:

This is an initial factoring of ReflectionMethodBodyScanner that just gets rid of AnalysisContext and ScopeStack usages in that class. No functional changes are intended.

Some of the ScopeStack logic is a bit subtle: the shared global ScopeStack meant that it was difficult to tell which scope might be used when reporting a warning - it was path-dependent. I pushed all of the ScopeStack.CurrentScope.Origin calls up into callsites outside of ReflectionMethodBodyScanner, so that the scanner logic could avoid concerning itself with pushing and popping scopes. Instead, it passes message origins along explicitly when required.

@sbomer
sbomer requested a review from marek-safar as a code owner April 19, 2022 02:46
@sbomer
sbomer requested a review from vitek-karas April 19, 2022 02:46
Don't copy origin into local
@sbomer
sbomer requested a review from tlakollo April 19, 2022 17:34
Use 'in' for new MessageOrigin parameters
@sbomer
sbomer requested a review from tlakollo April 19, 2022 18:10
Comment thread src/linker/Linker.Steps/MarkStep.cs Resolved

@tlakollo tlakollo 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.

Looks good to me

@sbomer
sbomer merged commit 137b7b9 into dotnet:main Apr 21, 2022
vitek-karas pushed a commit that referenced this pull request Apr 26, 2022
)

Follow-up to #2747. This is a factoring change only and doesn't introduce functional changes.

This does two things to move the method body scanner to be more similar to the trim analysis visitor:
- Uses `DiagnosticContext` to record diagnostics. Right now, this still happens inline as we run the scanner, but the idea will be to move this out to a driver that runs the analysis, and then records diagnostics from the recorded patterns (like how it is done in the analyzer)
- Moves reflection-marking helpers out of `ReflectionMethodBodyScanner` to a separate class. This prevents us from having to pass around the scanner to other helpers, and makes it more obvious which parts of the code to scanning only vs other things. The new `ReflectionMarker` is conceptually similar to `ReflectionAccessAnalyzer` in the Roslyn analyzer and could be a candidate for future code sharing.
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…t/linker#2747)

* Motivation:

For compiler-generated code dataflow, we may need to analyze a
compiler-generated method multiple times as part of the closure of
methods reached from user code. This is necessary because as we
analyze this closure of methods we might find new captured state that
flows into a previously analyzed method in the same closure.

To avoid spurious warnings, we need a deduplication mechanism similar
to what we do in the analyzer. The eventual goal is for the linker to
have something similar to the analyzer's TrimAnalysisPatternStore that
deduplicates warnings produced from multiple scans of the same method
body.

We also might need the ability to run the method body scanner without
producing any warnings, when analyzing compiler-generated methods
reachable by reflection. (The linker will warn about reflection over
compiler-generated code, and won't produce the full set of diagnostics
for these methods, but still needs to scan them to ensure dependencies
are kept).

The first step is to remove some levels of indirection around the
message origins, to make it easier to cache messages by origin in the
future.

* This change:

This is an initial factoring of ReflectionMethodBodyScanner that just
gets rid of AnalysisContext and ScopeStack usages in that class. No
functional changes are intended.

Some of the ScopeStack logic is a bit subtle: the shared global
ScopeStack meant that it was difficult to tell which scope might be
used when reporting a warning - it was path-dependent. I pushed all of
the ScopeStack.CurrentScope.Origin calls up into callsites outside of
ReflectionMethodBodyScanner, so that the scanner logic could avoid
concerning itself with pushing and popping scopes. Instead, it passes
message origins along explicitly when required.


Commit migrated from dotnet/linker@137b7b9
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…tnet/linker#2761)

Follow-up to dotnet/linker#2747. This is a factoring change only and doesn't introduce functional changes.

This does two things to move the method body scanner to be more similar to the trim analysis visitor:
- Uses `DiagnosticContext` to record diagnostics. Right now, this still happens inline as we run the scanner, but the idea will be to move this out to a driver that runs the analysis, and then records diagnostics from the recorded patterns (like how it is done in the analyzer)
- Moves reflection-marking helpers out of `ReflectionMethodBodyScanner` to a separate class. This prevents us from having to pass around the scanner to other helpers, and makes it more obvious which parts of the code to scanning only vs other things. The new `ReflectionMarker` is conceptually similar to `ReflectionAccessAnalyzer` in the Roslyn analyzer and could be a candidate for future code sharing.

Commit migrated from dotnet/linker@f26189b
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