Skip to content

Audit reflection usage in our assemblies #10405

@rolfbjarne

Description

@rolfbjarne

The ILlink is now doing reflection analysis resulting in reported warnings. Context: https://github.com/mono/linker/blob/master/docs/design/reflection-flow.md

We should audit our reflection usage by checking the reported warnings and look into how to solve these. (by updating the code where needed or suppressing the warnings)

Example output:

/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(172,27): Trim analysis warning IL2080: Registrar.DynamicRegistrar.<FindMethods>d__14.MoveNext(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The field 'System.Type Registrar.DynamicRegistrar/<FindMethods>d__14::type' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Blocks.cs(307,4): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.GetBlockForDelegate(MethodInfo,Object,UInt32,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetField(String,BindingFlags)'. The return value of method 'ObjCRuntime.BlockLiteral.GetDelegateProxyType(MethodInfo,UInt32,MethodInfo&)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Blocks.cs(185,5): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.SetupBlock(Delegate,Delegate): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.MonoPInvokeCallbackAttribute.DelegateType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Blocks.cs(105,5): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.SetupBlock(Delegate,Delegate,Boolean): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.UserDelegateTypeAttribute.UserDelegateType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Class.cs(457,5): Trim analysis warning IL2026: ObjCRuntime.Class.ResolveToken(Module,UInt32): Calling 'System.Reflection.Module.ResolveMethod(Int32)' which has `RequiresUnreferencedCodeAttribute` can break functionality when trimming application code. Trimming changes metadata tokens.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Class.cs(451,5): Trim analysis warning IL2026: ObjCRuntime.Class.ResolveToken(Module,UInt32): Calling 'System.Reflection.Module.ResolveType(Int32)' which has `RequiresUnreferencedCodeAttribute` can break functionality when trimming application code. Trimming changes metadata tokens.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(198,4): Trim analysis warning IL2070: Registrar.DynamicRegistrar.CollectConstructors(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectConstructors(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(203,4): Trim analysis warning IL2070: Registrar.DynamicRegistrar.CollectMethods(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectMethods(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(208,4): Trim analysis warning IL2070: Registrar.DynamicRegistrar.CollectProperties(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Type.GetProperties(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.CollectProperties(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(214,4): Trim analysis warning IL2026: Registrar.DynamicRegistrar.CollectTypes(Assembly): Calling 'System.Reflection.Assembly.GetTypes()' which has `RequiresUnreferencedCodeAttribute` can break functionality when trimming application code. Types might be removed.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(115,23): Trim analysis warning IL2026: Registrar.DynamicRegistrar.ContainsPlatformReference(Assembly): Calling 'System.Reflection.Assembly.GetReferencedAssemblies()' which has `RequiresUnreferencedCodeAttribute` can break functionality when trimming application code. Assembly references might be removed.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(179,4): Trim analysis warning IL2070: Registrar.DynamicRegistrar.FindProperty(Type,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Type.GetProperty(String,BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.FindProperty(Type,String)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(184,25): Trim analysis warning IL2026: Registrar.DynamicRegistrar.FindType(Type,String,String): Calling 'System.Reflection.Assembly.GetTypes()' which has `RequiresUnreferencedCodeAttribute` can break functionality when trimming application code. Types might be removed.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(395,4): Trim analysis warning IL2070: Registrar.DynamicRegistrar.GetFields(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.GetFields(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(537,4): Trim analysis warning IL2045: Registrar.DynamicRegistrar.HasThisAttributeImpl(MethodBase): Attribute 'System.Runtime.CompilerServices.ExtensionAttribute' is being referenced in code but the linker was instructed to remove all instances of this attribute. If the attribute instances are necessary make sure to either remove the linker attribute XML portion which removes the attribute instances, or override the removal by using the linker XML descriptor to keep the attribute type (which in turn keeps all of its instances).
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(1053,30): Trim analysis warning IL2070: Registrar.DynamicRegistrar.TryMatchProperty(Type,PropertyInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Type.GetProperties(BindingFlags)'. The parameter 'type' of method 'Registrar.DynamicRegistrar.TryMatchProperty(Type,PropertyInfo)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/DynamicRegistrar.cs(497,5): Trim analysis warning IL2055: Registrar.DynamicRegistrar.VerifyIsConstrainedToNSObject(Type,Type&): Call to 'System.Type.MakeGenericType(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(529,23): Trim analysis warning IL2026: ObjCRuntime.Runtime.CollectReferencedAssemblies(List<Assembly>,Assembly): Calling 'System.Reflection.Assembly.GetReferencedAssemblies()' which has `RequiresUnreferencedCodeAttribute` can break functionality when trimming application code. Assembly references might be removed.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(1711,23): Trim analysis warning IL2070: ObjCRuntime.Runtime.FindClosedMethod(Type,MethodBase): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'closed_type' of method 'ObjCRuntime.Runtime.FindClosedMethod(Type,MethodBase)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(1031,23): Trim analysis warning IL2075: ObjCRuntime.Runtime.FindPropertyInfo(MethodInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Reflection.MemberInfo.DeclaringType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(776,6): Trim analysis warning IL2075: ObjCRuntime.Runtime.GetBlockProxyAttributeMethod(MethodInfo,Int32): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.BlockProxyAttribute.Type.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(882,5): Trim analysis warning IL2026: ObjCRuntime.Runtime.GetBlockWrapperCreator(MethodInfo,Int32): Calling 'System.Reflection.Assembly.GetType(String,Boolean)' which has `RequiresUnreferencedCodeAttribute` can break functionality when trimming application code. Types might be removed.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(891,6): Trim analysis warning IL2075: ObjCRuntime.Runtime.GetBlockWrapperCreator(MethodInfo,Int32): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String,BindingFlags,Binder,Type[],ParameterModifier[])'. The return value of method 'System.Reflection.Assembly.GetType(String,Boolean)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(873,7): Trim analysis warning IL2065: ObjCRuntime.Runtime.GetBlockWrapperCreator(MethodInfo,Int32): Value passed to implicit 'this' parameter of method 'System.Type.GetMethod(String)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(1165,4): Trim analysis warning IL2070: ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Runtime.cs(1147,4): Trim analysis warning IL2070: ObjCRuntime.Runtime.GetIntPtrConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Runtime.GetIntPtrConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Stret.cs(192,27): Trim analysis warning IL2070: ObjCRuntime.Stret.GetValueTypeSize(Type,List<Type>,Boolean,Object): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Stret.GetValueTypeSize(Type,List<Type>,Boolean,Object)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
/Users/rolf/work/maccore/onedotnet/xamarin-macios/src/ObjCRuntime/Stret.cs(283,26): Trim analysis warning IL2070: ObjCRuntime.Stret.GetValueTypeSize(Type,Type,List<Type>,Boolean,Int32&,Int32&,Object): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Stret.GetValueTypeSize(Type,Type,List<Type>,Boolean,Int32&,Int32&,Object)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Note: warnings are only reported when SuppressTrimAnalysisWarnings=false

Metadata

Metadata

Assignees

Labels

dotnetAn issue or pull request related to .NET (6)enhancementThe issue or pull request is an enhancementestimate-1wiOSIssues affecting iOSmacOSIssues affecting macOS

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions