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

Validation of annotations across virtual method hierarchies - #1315

Merged
vitek-karas merged 5 commits into
dotnet:masterfrom
vitek-karas:ValidateMethodHierarchies
Jul 8, 2020
Merged

vitek-karas merged 5 commits into
dotnet:masterfrom
vitek-karas:ValidateMethodHierarchies

Conversation

@vitek-karas

@vitek-karas vitek-karas commented Jul 6, 2020 •

Copy link
Copy Markdown
Member

For the linker to work correctly all correctness annotations must be exactly the same on all methods in a single virtual method override hierarchy.

Currently this applies to DynamicallyAccessedMemberAttribute and RequiresUnreferencedCodeAttribute.

The validation is postponed after the MarkStep since it requires full knowledge of virtual method hierarchies (and interface method hierarchies). With TypeMapStep these are available during MarkStep as well, but we're trying to move away from that requirement, so the validation is done after MarkStep, this only adds a very small amount of memory (the list of methods to validate is currently cca 200 for the entire corelibe).

Refactorings to help with this change:

  • Change how we store generic parameter annotations - it now copies the method parameter annotation storage - array indexed by the index of the generic parameter on the type/method. This makes it easy to map the annotation to a generic parameter on any method in the virtual hierarchy (as they all share the same index).
  • FlowAnnotations are now stored directly on the Annotations class (since the new step needs them after MarkStep)
  • Move some helpers into DataFlowUtilities - mostly around getting description of members for error message purposes
  • Synchronize behavior of LogContains and LogDoesNotContain test attributes - both now support "contains" and "regex" versions
  • Correctly detect persense of LogContains and LogDoesNotContain attributes on all members of a test, so that test logging is turned on when they're used
  • Added a TestSystemTypeBase class - which is a class derived from System.Type and implements all abstract members. This is used to then write test classes which need to derive from System.Type (to test the "this" parameter annotations).

This implements #1028 and #1271 - the very strict version. We can dial it back for the case of implementation->interface if we think that's important, but that's relatively tricky (for example it's possibly OK to not annotate implementation's in parameters even if interface has it, but it's not OK to omit annotation on the return value in this case - so we would have to correctly track in/out annotations for the purposes of validation).

@vitek-karas
vitek-karas force-pushed the ValidateMethodHierarchies branch from e43ef5b to e95455c Compare July 7, 2020 09:42
if (results.Count == 0)
return DynamicallyAccessedMemberTypes.None.ToString ();

return string.Join (" | ", results.Select (r => r.ToString ()));

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.

Do we actually want such output? It can produce almost unreadable text

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It should recreate the text used in the attribute in source code pretty well. By far the most common case is annotations with one or two flags from the enum, so that should be fine.

I must admit I don't understand the scenario when this would produce a mess. The only potential is "I want to include everything", but for that we have the All value which is handled explicitly.

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.

We used this to dump to user two sets of enums and let him compare. This is very hard as most of the options are very similar. I don't know if the common path will use 2, 3 or more values but I think it'd be better to re-word the error message to allow us to report only single enum value instead.

(ignoring the aspect that this is C#-ish in IL-based tool)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd like to address this in a separate PR as it's mostly unrelated to this one (the validation introduced in this PR doesn't actually use this method, I just moved it out of the already large ReflectionMethodBodyScanner).

I added note about this to #1157 which should address the warning message where this is used from.

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.

ok

Comment thread src/linker/Linker/Annotations.cs Outdated
if (!method.IsVirtual)
return;

if (context.Annotations.FlowAnnotations.RequiresDataFlowAnalysis (method) ||

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.

Suggested change
if (context.Annotations.FlowAnnotations.RequiresDataFlowAnalysis (method) ||
if (FlowAnnotations.RequiresDataFlowAnalysis (method) || HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (method))

protected override void Process ()
{
Context.VirtualMethodAnnotationValidator.Validate ();
foreach (var method in Context.Annotations.VirtualMethodsWithAnnotationsToValidate) {

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.

You could store Context.Annotations in local variable to have fewer prefixes here

For the linker to work correctly all correctness annotations must be exactly the same on all methods in a single virtual method override hierarchy.

Currently this applies to DynamicallyAccessedMemberAttribute and RequiresUnreferencedCodeAttribute.

The validation is postponed after the MarkStep since it requires full knowledge of virtual method hierarchies (and interface method hierarchies). With TypeMapStep these are available during MarkStep as well, but we're trying to move away from that requirement, so the validation is done after MarkStep, this only adds a very small amount of memory (the list of methods to validate is currently cca 200 for the entire corelibe).

Refactorings to help with this change:
* Change how we store generic parameter annotations - it now copies the method parameter annotation storage - array indexed by the index of the generic parameter on the type/method. This makes it easy to map the annotation to a generic parameter on any method in the virtual hierarchy (as they all share the same index).
* FlowAnnotations are now stored directly on the Annotations class (since the new step needs them after MarkStep)
* Move some helpers into DataFlowUtilities - mostly around getting description of members for error message purposes
* Synchronize behavior of LogContains and LogDoesNotContain test attributes - both now support "contains" and "regex" versions
* Correctly detect persense of LogContains and LogDoesNotContain attributes on all members of a test, so that test logging is turned on when they're used
* Added a TestSystemTypeBase class - which is a class derived from System.Type and implements all abstract members. This is used to then write test classes which need to derive from System.Type (to test the "this" parameter annotations).
Remove left-over debugging write
Update comments
@vitek-karas
vitek-karas force-pushed the ValidateMethodHierarchies branch from dfffc96 to b47ed47 Compare July 8, 2020 08:27
@vitek-karas
vitek-karas merged commit cf697fc into dotnet:master Jul 8, 2020
@vitek-karas
vitek-karas deleted the ValidateMethodHierarchies branch July 8, 2020 11:08
tkapin pushed a commit to tkapin/runtime that referenced this pull request Jan 31, 2023
…inker#1315)

For the linker to work correctly all correctness annotations must be exactly the same on all methods in a single virtual method override hierarchy.

Currently this applies to DynamicallyAccessedMemberAttribute and RequiresUnreferencedCodeAttribute.

The validation is postponed after the MarkStep since it requires full knowledge of virtual method hierarchies (and interface method hierarchies). With TypeMapStep these are available during MarkStep as well, but we're trying to move away from that requirement, so the validation is done after MarkStep, this only adds a very small amount of memory (the list of methods to validate is currently cca 200 for the entire corelibe).

Refactorings to help with this change:
* Change how we store generic parameter annotations - it now copies the method parameter annotation storage - array indexed by the index of the generic parameter on the type/method. This makes it easy to map the annotation to a generic parameter on any method in the virtual hierarchy (as they all share the same index).
* FlowAnnotations are now stored directly on the Annotations class (since the new step needs them after MarkStep)
* Move some helpers into DataFlowUtilities - mostly around getting description of members for error message purposes
* Synchronize behavior of LogContains and LogDoesNotContain test attributes - both now support "contains" and "regex" versions
* Correctly detect persense of LogContains and LogDoesNotContain attributes on all members of a test, so that test logging is turned on when they're used
* Added a TestSystemTypeBase class - which is a class derived from System.Type and implements all abstract members. This is used to then write test classes which need to derive from System.Type (to test the "this" parameter annotations).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants