Skip to content

Trimmer doesn't warn on reflection access to RUC override method #86008

@vitek-karas

Description

@vitek-karas

Found by @MichalStrehovsky

interface IBase
{
    [RequriesUnreferencedCode("")]
    void DoSomethingDangerous();
}
class Implementation : IBase
{
    [RequiresUnreferencedCode("")]
    public void DoSomethingDangerous() { Console.WriteLine("!!!DANGER!!!"); }
}
void CallMethodOnInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] T>(T instance)
{
    typeof(T).GetMethod("DoSomethingDangerous").Invoke(instance, Array.Empty<object>());
}
static void Main()
{
    CallMethodOnInstance(new Implementation);
}

The above will not produce any warning when trimmed, but when executed it will print out !!!DANGER!!!.
The bug is that ILLink will not warn if a method is an override and is in RUC scope and is marked due to annotation (so broader marking like PublicMethods).
This was done assuming that the same broad marking will also mark the base method which will warn. But that's not always true. Reflection doesn't return base interface methods when usual GetMethod is called. ILLink doesn't enumerate the base interface methods in this case either.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILLink.NET linker development as well as trimming analyzers

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions