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

Produce diagnostics for KnownValueTypes that have annotations in its members - #2418

Merged
tlakollo merged 20 commits into
dotnet:feature/damAnalyzerfrom
tlakollo:KnownTypeInDAMAnalyzer
Feb 2, 2022
Merged

tlakollo merged 20 commits into
dotnet:feature/damAnalyzerfrom
tlakollo:KnownTypeInDAMAnalyzer

Conversation

@tlakollo

@tlakollo tlakollo commented Dec 3, 2021 •

Copy link
Copy Markdown
Contributor

Add KnownTypeValue as a subclass of ValueWithDynamicallyAccessedMembers
Add IL2110 and IL2111 to shared Diagnostic ids
Add test in analyzer for now since intrinsics are still not handled
Delete the use of context in the binder since the context is never used
Produce diagnostics for KnownValueTypes that have annotations in its members

Fixes #2410
Fixes #2339

Add IL2110 and IL2111 to shared Diagnostic ids
Add test in analyzer for now since intrinsics are still not handled
Delete the use of context in the binder since the context is never used
Produce diagnostics for KnownValueTypes that have annotations in its members
@tlakollo
tlakollo requested a review from sbomer December 3, 2021 07:19
@tlakollo tlakollo self-assigned this Dec 3, 2021

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

Looking good! Would it be reasonable to turn on the test AnnotatedMembersAccessedViaReflection and use ProducedBy to skip the ones that rely on intrinsics? It would be good to get the extra coverage.

Comment thread src/ILLink.Shared/DiagnosticId.cs Resolved
Remove simple test in DynamicallyAccessedMembersAnalyzerTests
Add code to handle properties
Add RequiresUnreferencedCode changes to be able to produce RUC from DAMAnalyzer
@tlakollo
tlakollo marked this pull request as ready for review December 23, 2021 05:12
Only return one diagnostic per method
Disable intrinsics GetMethod, GetProperty and GetField
Fix tests
@tlakollo

Copy link
Copy Markdown
Contributor Author

I will change this to draft, I realized that there is several tests having problems with interfaces and implementations. Need to spend more time on it.

@tlakollo
tlakollo marked this pull request as draft January 20, 2022 07:02
…was previously being built

Changes tests to include Analyzer warnings
Merge branch 'feature/damAnalyzer' of https://github.com/mono/linker into KnownTypeInDAMAnalyzer
@tlakollo
tlakollo marked this pull request as ready for review January 28, 2022 02:20
Comment on lines +28 to +35
/* Skip Type and InterfaceImplementation marking since doesnt seem relevant for diagnostic generation
case ITypeSymbol nestedType:
MarkType (diagnosticContext, nestedType);
break;
case InterfaceImplementation interfaceImplementation:
MarkInterfaceImplementation (analysisContext, interfaceImplementation, dependencyKind);
break;
*/

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.

What about Type level DAM/RUC?
I think it's OK for now.. but going forward... maybe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So I think is kind of handled, each member looks for RUC on the member and on the type based on the conditions (being static or constructor and the ContainingType has RUC), so at least type-level RUC seems fine to me. Maybe once more intrinsics and we enable more tests like ObjectGetType we can have enable the DAM on type level

case IPropertySymbol property:
GetDiagnosticsForProperty (diagnosticContext, property);
break;
/* Skip Type and InterfaceImplementation marking since doesnt seem relevant for diagnostic generation

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.

I think OK for now - I don't remember if this is important for interfaces when DAM on type is enabled.... but we'll get there eventually.

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.

I think

class AnnotatedInterfaces : RequiredInterface
will cover this once we get there.

Use FlowAnnotations helpers to cover the same usecases as in linker and rest of the dataflow
@tlakollo

tlakollo commented Feb 2, 2022

Copy link
Copy Markdown
Contributor Author

Added a bug for most of the properties inconsistencies #2575
The problem is naming so I'm not sure if it's linker logic or test infra. The system to present warnings in those cases is wrong in many ways. For example, makes a non virtual getter to warn

.WithArguments ("System.Type.GetMethod(String)",
"type",
"C.M(Type)",
"'DynamicallyAccessedMemberTypes.PublicMethods'")*/);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure why lint decided on this format but is on purpose

Comment on lines +55 to +56
if (methodSymbol.GetDynamicallyAccessedMemberTypes () != DynamicallyAccessedMemberTypes.None)
diagnosticContext.AddDiagnostic (DiagnosticId.DynamicallyAccessedMembersMethodAccessedViaReflection, methodSymbol.GetDisplayName ());

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.

Ideally this should be only done on instance method (non-static) and only when the declaring type derives from System.Type. The latter part is tracked by either #2409 or a line item in #2273.

So probably OK to leave it as is for now - as we will likely handle this as a separate part of the analyzer. It might produce noise if there's such a case in the code though, but not a big deal.

Comment on lines +540 to +543
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotationOnMembers) + ".get", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotationOnMembers) + ".get", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotationOnMembers) + ".set", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotationOnMembers) + ".set", ProducedBy = ProducedBy.Analyzer)]

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.

The reason for the weird warnings from Linker is that the tests are matching substrings and linker doesn't produce the warnings in the same order as analyzer.

So for example the expected warning which is for PropertyWithAnnotationOnMembers.get will actually match the produced warning for VirtualPropertyWithAnnotationOnMembers.get. This leads to:

  • The expected warning for the Virtual... is not recognized and reported as missing
  • The produced warning for Property... is not expected and reported as such -> leading to seemingly duplicate warnings.

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.

I pushed a change which fixes it for the property test - maybe we should do the same for the other tests in this file as well.

The fix is more of a workaround, I simply renamed the properties so that the substrings don't match.
As far as I can tell the correct warnings are generated. And the linker and analyzer behavior matches exactly now (nice).

That said there are still duplicate warnings, but that's a different matter.

@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 - just a few minor things

Add Struct Layout in the SharedProject

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

:shipit:

@tlakollo
tlakollo merged commit 924ccec into dotnet:feature/damAnalyzer Feb 2, 2022
@tlakollo
tlakollo deleted the KnownTypeInDAMAnalyzer branch February 2, 2022 20:47
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…members (dotnet/linker#2418)

Add IL2110 and IL2111 to shared Diagnostic ids
Delete the use of context in the binder since the context is never used
Add ReflectionAccessAnalyzer which makes the work of ReflectionMethodBodyScanner without marking and only producing diagnostics
Produce diagnostics for SystemTypeValue that have annotations in its members
Enable the AnnotatedMembersAccessedViaReflection test
Add RequiresUnreferencedCode changes to be able to produce RUC from DAMAnalyzer
Disable intrinsics GetMethod, GetEvent, GetProperty, GetConstructor and GetField
Add diagnosticContext as part of the ReflectionAction
Add Struct Layout in ReflectionAction

Co-authored-by: vitek-karas <[email protected]>

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