dotnet / dotnet/linker

Linker is not warning when calling CreateDelegate on a Method with Linker annotations applied to it

Open
#1,898 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

We have the following usage of Reflection in `dotnet/runtime`:

In `System.Private.CoreLib`, it is getting a MethodInfo for a method in `System.ComponentModel.TypeConverter`

https://github.com/dotnet/runtime/blob/82ca681cbac89d813a3ce397e0c665e6c051ed67/src/libraries/System.Private.CoreLib/src/System/ComponentModel/DefaultValueAttribute.cs#L69-L71

```C#
Type? typeDescriptorType = Type.GetType("System.ComponentModel.TypeDescriptor, System.ComponentModel.TypeConverter", throwOnError: false);
MethodInfo? mi = typeDescriptorType?.GetMethod("ConvertFromInvariantString", BindingFlags.NonPublic | BindingFlags.Static);
Volatile.Write(ref s_convertFromInvariantString, mi == null ? new object() : mi.CreateDelegate(typeof(Func)));
```

The method has a parameter that is now annotated with `DynamicallyAccessedMembers.All`:

https://github.com/dotnet/runtime/blob/ec5d2eeade1ba4f203b2166c399056e124b4cfb8/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs#L824-L828

```C#
// This is called by System.ComponentModel.DefaultValueAttribute via reflection.
private static object ConvertFromInvariantString([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, string stringValue)
{
return GetConverter(type).ConvertFromInvariantString(stringValue);
}
```

However, the linker isn't warning about this reflection usage. It should be a warning because nothing is ensuring the `Type` being passed into this method is annotated with `DynamicallyAccessedMembers.All`.

cc @MichalStrehovsky @vitek-karas @mateoatr

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.