dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.03k stars 4.68k forks source link

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

Open sbomer opened 2 months ago

sbomer commented 2 months ago

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: https://github.com/dotnet/runtime/blob/4dd997dc1344f64dce53d7233e7ab6d09ff186b2/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/ReflectionMethodBodyScanner.cs#L147-L156

dotnet-policy-service[bot] commented 2 months ago

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