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

Don't depend on marking order in DAM type hierarchy - #2777

Merged
sbomer merged 2 commits into
dotnet:mainfrom
sbomer:damHierarchyFactoring
May 3, 2022
Merged

sbomer merged 2 commits into
dotnet:mainfrom
sbomer:damHierarchyFactoring

Conversation

@sbomer

@sbomer sbomer commented May 2, 2022

Copy link
Copy Markdown
Member

This will allow ApplyDynamicallyAccessedMembersToTypeHierarchy to be called from the scanner when it sees object.GetType to get annotations on the type, without requiring that the type has been marked yet. (It will be marked during the final pass after the flow analysis has converged.)

@sbomer
sbomer requested a review from marek-safar as a code owner May 2, 2022 17:08
@sbomer
sbomer requested review from tlakollo and vitek-karas May 2, 2022 17:08

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

I "think" this is correct - this code still hurts my brain every time I look at it :-)

DynamicallyAccessedMemberTypes annotation = _context.Annotations.FlowAnnotations.GetTypeAnnotation (type);
bool apply = false;

// We'll use the cache also as a way to detect and avoid recursion

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.

This comment should move above the very first thing in the method (which is what sort of checks for recursion). It will need to be updated as it now acts on all types, not just interfaces.

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.

Thanks, I fixed the comment.

One thing I wanted to note is that it will still not put unannotated base types into the cache (I didn't want to introduce another cache of every marked type) - but it means that now we need to walk up the unannotated base types. Whereas before, relying on marking order meant that a type not in the cache could be assumed to have None annotations.

}

void ApplyDynamicallyAccessedMembersToType (ref ReflectionMarker reflectionMarker, in MessageOrigin origin, TypeDefinition type, DynamicallyAccessedMemberTypes annotation)
void ApplyDynamicallyAccessedMembersToType (in ReflectionMarker reflectionMarker, in MessageOrigin origin, TypeDefinition type, DynamicallyAccessedMemberTypes annotation)

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.

Is it correct that we should use the RequireDynamicallyAccessedMembersAction.MarkTypeForDynamicallyAccessedMembers instead of the ReflectionMarker once we share this code with the analyzer?
(Maybe you thought about this... if not, no matter, somebody will look into it once we implement sharing of this).

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.

This gave me some trouble which is actually what led me to make the changes in this PR. :) My understanding is that this can continue using ReflectionMarker - it's ok to always mark and produce warnings, because the warnings are already deduplicated by the type cache, no need to run with marking/warnings disabled during the analysis.

If we wanted ApplyDynamicallyAccessedMembersToTypeHierarchy to work with marking disabled the first time (which might be nice in theory, just for consistency), that can be done too, it just takes a few more changes to this logic. We'd probably just need to pass down a separate bool to indicate whether it should mark or just retrieve annotations.

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 actually didn't even think about that. I was thinking what it would take to share this code with the analyzer, which doesn't have a ReflectionMarker.

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.

Sorry, I misinterpreted the question. I think the analyzer will need a similar cache per type, to avoid duplicate warnings for multiple object.GetType calls on the same type - and the logic for ApplyDynamicallyAccessedMembersToType would need to be duplicated or shared.

Then instead of MarkTypeForDynamicallyAccessedMembers it would probably call ReflectionAccessAnalyzer.GetReflectionAccessDiagnostics which is already pretty similar to the code in ReflectionMarker (or maybe that code could be shared too).

Fix comment
@sbomer

sbomer commented May 3, 2022

Copy link
Copy Markdown
Member Author

this code still hurts my brain every time I look at it :-)

Yes, I spent about a day trying to figure this out... Thanks for the review!

@sbomer
sbomer merged commit 9eb23e4 into dotnet:main May 3, 2022
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
This will allow ApplyDynamicallyAccessedMembersToTypeHierarchy to
be called from the scanner when it sees object.GetType to get
annotations on the type, without requiring that the type has been
marked yet. (It will be marked during the final pass after the
flow analysis has converged.)

This will still not put unannotated base types into the cache (I
didn't want to introduce another cache of every marked type) -
but it means that now we need to walk up the unannotated base
types. Whereas before, relying on marking order meant that a type
not in the cache could be assumed to have None annotations.



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