Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ILC: missing warning for DAM on type when used through derived type #104740

Closed
sbomer opened this issue Jul 11, 2024 · 1 comment · Fixed by #110655
Closed

ILC: missing warning for DAM on type when used through derived type #104740

sbomer opened this issue Jul 11, 2024 · 1 comment · Fixed by #110655
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@sbomer
Copy link
Member

sbomer commented Jul 11, 2024

DAM on type is supposed to ensure that GetType called on that type or any derived type will satisfy the DAM annotations. In this example, d.GetType() should keep public methods on the base type and produce a warning for the RUC method.

using System.Diagnostics.CodeAnalysis;

class Program {
    public static void Main() {
        RequirePublicMethods(d.GetType());
    }

    static D d = new();

    static void RequirePublicMethods([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type t) {}
}


[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
class B {
    [RequiresUnreferencedCode("M")]
    public virtual void M() {} // There should be a warning about reflection access (through DAM-on-type) to M
}

class D : B {}

ProcessTypeGetTypeDataflow gets called for the derived type D, but nothing applies the annotations on B. This logic doesn't do it because it assumes B's annotations were already applied, and so doesn't keep B's members while processing D:

if (type.HasBaseType)
{
var baseAnnotation = flowAnnotations.GetTypeAnnotation(type.BaseType);
var annotationToApplyToBase = Annotations.GetMissingMemberTypes(annotation, baseAnnotation);
// Apply any annotations that didn't exist on the base type to the base type.
// This may produce redundant warnings when the annotation is DAMT.All or DAMT.PublicConstructors and the base already has a
// subset of those annotations.
reflectionMarker.MarkTypeForDynamicallyAccessedMembers(origin, type.BaseType, annotationToApplyToBase, type.GetDisplayName(), declaredOnly: false);
}

Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@agocke agocke added this to the 10.0.0 milestone Jul 29, 2024
@agocke agocke removed the untriaged New issue has not been triaged by the area owner label Jul 29, 2024
MichalStrehovsky added a commit to MichalStrehovsky/runtime that referenced this issue Dec 12, 2024
Fixes dotnet#104740, also implements dotnet#110563 for ILC.

The annotation on DAM annotated classes gets triggered by calling GetType on a location typed as something DAM annotated (or deriving/implementing from it).

The marking is done in layers so that warning suppressions can be properly applied. The bug was that we didn't walk down the hierarchy and assumed someone else will do it. Nobody did. I'm adding a new node type to do the marking.

Previously, we only used one node for tracking. The node got dropped into the dependency graph when we saw GetType being called and it ensured the proper marking for that one type (not the bases). We also added conditional dependencies into MethodTables so that `Derived` can depend on this node of the `Base`. This ensured that if GetType was called on Base, we'd treat it same as GetType on Derived. This was resulting in marking too much and too little (we'd mark right away when we saw GetType call, irrespective of the MethodTable existence, and we wouldn't walk down to Base if the GetType was called on Derived.

The fix is to use two node types. One simply tracks "GetType was called on something". It doesn't bring any other dependencies with it. We only use it to condition other nodes. The other node represents "the dependencies from the annotations".

The way this works is:
* We see GetType called on Base, so we add ObjectGetTypeCalledNode for Base.
* We generate MethodTable for Derived, which adds a conditional dependency on ObjectGetTypeCalledNode of Derived if ObjectGetTypeCalledNode of Base is in the graph. (This ensures "walking up the hierarchy".)
* MethodTable of Derived also adds a conditional dependency on ObjectGetTypeFlowDependenciesNode of Derived if ObjectGetTypeCalledNode of Derived is part of the graph. This will do the actual marking but only if the MethodTable and GetType call was seen.
* ObjectGetTypeFlowDependenciesNode also "walks down the hierarchy" and makes sure ObjectGetTypeFlowDependenciesNode of all the bases and interfaces are present in the graph.

This happens to also address dotnet#110563. Because of that, I had to update tests since ILC started trimming more stuff without seeing the type as constructed.
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Dec 12, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jan 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants