From 4aacf930ecdccfb41435e913042662cf97e99b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Mon, 22 Jun 2020 10:20:25 +0200 Subject: [PATCH] Add more trimming annotations to Mono CoreLib (#38148) I've also annotated CoreCLR's `Type.GetType` in the same spot as Mono. Mono's implementation was triggering warnings, which is what promted me to add it there, but we should annotate this nonetheless because absent intrinsic treatment in Linker, this would be unsafe. --- .../src/System.Private.CoreLib/src/System/Type.CoreCLR.cs | 7 +++++++ src/libraries/System.Runtime/ref/System.Runtime.cs | 6 ++++++ .../System.Private.CoreLib/src/System/Delegate.Mono.cs | 2 ++ .../src/System/Runtime/InteropServices/Marshal.Mono.cs | 7 ++++++- .../System.Private.CoreLib/src/System/RuntimeType.Mono.cs | 2 ++ .../System.Private.CoreLib/src/System/Type.Mono.cs | 8 ++++++++ 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Type.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Type.CoreCLR.cs index 22325c2cb29..294c7907c3a 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Type.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Type.CoreCLR.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; using StackCrawlMark = System.Threading.StackCrawlMark; @@ -20,6 +21,7 @@ namespace System } } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName, bool throwOnError, bool ignoreCase) { @@ -27,6 +29,7 @@ namespace System return RuntimeType.GetType(typeName, throwOnError, ignoreCase, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName, bool throwOnError) { @@ -34,6 +37,7 @@ namespace System return RuntimeType.GetType(typeName, throwOnError, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName) { @@ -41,6 +45,7 @@ namespace System return RuntimeType.GetType(typeName, false, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType( string typeName, @@ -51,6 +56,7 @@ namespace System return TypeNameParser.GetType(typeName, assemblyResolver, typeResolver, false, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType( string typeName, @@ -62,6 +68,7 @@ namespace System return TypeNameParser.GetType(typeName, assemblyResolver, typeResolver, throwOnError, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType( string typeName, diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 853e1ef2b50..7fa3dac5f21 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -4222,11 +4222,17 @@ namespace System [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] protected abstract System.Reflection.PropertyInfo? GetPropertyImpl(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Type? returnType, System.Type[]? types, System.Reflection.ParameterModifier[]? modifiers); public new System.Type GetType() { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The type might be removed")] public static System.Type? GetType(string typeName) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The type might be removed")] public static System.Type? GetType(string typeName, bool throwOnError) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The type might be removed")] public static System.Type? GetType(string typeName, bool throwOnError, bool ignoreCase) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The type might be removed")] public static System.Type? GetType(string typeName, System.Func? assemblyResolver, System.Func? typeResolver) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The type might be removed")] public static System.Type? GetType(string typeName, System.Func? assemblyResolver, System.Func? typeResolver, bool throwOnError) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The type might be removed")] public static System.Type? GetType(string typeName, System.Func? assemblyResolver, System.Func? typeResolver, bool throwOnError, bool ignoreCase) { throw null; } public static System.Type[] GetTypeArray(object[] args) { throw null; } public static System.TypeCode GetTypeCode(System.Type? type) { throw null; } diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs index 2aaeb0c4d17..7e99b00a2ba 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Delegate.Mono.cs @@ -248,6 +248,8 @@ namespace System return null; } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Invoke method is never removed from delegates")] private static bool IsMatchingCandidate(Type type, object? target, MethodInfo method, bool allowClosed, out DelegateData? delegateData) { MethodInfo? invoke = type.GetMethod("Invoke"); diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Mono.cs index 185ddbff429..ef84bce7b01 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Mono.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; using System.Threading; @@ -316,7 +317,9 @@ namespace System.Runtime.InteropServices PtrToStructureInternal(ptr, structure, allowValueClasses); } - private static object PtrToStructureHelper(IntPtr ptr, Type structureType) + private static object PtrToStructureHelper(IntPtr ptr, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] + Type structureType) { object obj = Activator.CreateInstance(structureType)!; PtrToStructureHelper(ptr, obj, true); @@ -371,6 +374,8 @@ namespace System.Runtime.InteropServices private static Dictionary<(Type, string), ICustomMarshaler>? MarshalerInstanceCache; + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Implementation detail of MarshalAs.CustomMarshaler")] internal static ICustomMarshaler? GetCustomMarshalerInstance(Type type, string cookie) { var key = (type, cookie); diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs index 592845228cf..06a117f9d04 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs @@ -1685,6 +1685,8 @@ namespace System throw new NotSupportedException(Environment.GetResourceString("Acc_CreateVoid")); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Implementation detail of Activator that linker intrinsically recognizes")] internal object? CreateInstanceImpl( BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture) { diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Type.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Type.Mono.cs index 1b234544f7d..88cd0570c6b 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Type.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Type.Mono.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Reflection; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading; @@ -34,6 +35,7 @@ namespace System } } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName, bool throwOnError, bool ignoreCase) { @@ -41,6 +43,7 @@ namespace System return RuntimeType.GetType(typeName, throwOnError, ignoreCase, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName, bool throwOnError) { @@ -48,6 +51,7 @@ namespace System return RuntimeType.GetType(typeName, throwOnError, false, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName) { @@ -55,6 +59,7 @@ namespace System return RuntimeType.GetType(typeName, false, false, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName, Func? assemblyResolver, Func? typeResolver) { @@ -62,6 +67,7 @@ namespace System return GetType(typeName, assemblyResolver, typeResolver, false, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName, Func? assemblyResolver, Func? typeResolver, bool throwOnError) { @@ -69,6 +75,7 @@ namespace System return GetType(typeName, assemblyResolver, typeResolver, throwOnError, false, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Type? GetType(string typeName, Func? assemblyResolver, Func? typeResolver, bool throwOnError, bool ignoreCase) { @@ -76,6 +83,7 @@ namespace System return GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, ref stackMark); } + [RequiresUnreferencedCode("The type might be removed")] private static Type? GetType(string typeName, Func? assemblyResolver, Func? typeResolver, bool throwOnError, bool ignoreCase, ref StackCrawlMark stackMark) { return TypeNameParser.GetType(typeName, assemblyResolver, typeResolver, throwOnError, ignoreCase, ref stackMark);