From 1dc826f031e2adf1fb2c0307f967c7894d1e8ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 14 Nov 2024 08:52:10 +0100 Subject: [PATCH 1/8] Fix dataflow analysis for GetNestedType https://github.com/dotnet/linker/pull/2133 adjusted rooting logic to keep `.All` on all nested types preserved with PublicNestedTypes/NonPublicNestedTypes. The PR resolved the bug and the analysis work that was identified in the review as necessary never happened. So we just uselessly preserve members. This adds dataflow analysis to propagate this. Since we never had the dataflow analysis, I'm exposing this not as .All, but a subset of .All that doesn't do the worst part - doesn't require reflectability of interface methods. --- .../TestCases/TestSuites.cs | 1 + .../TrimAnalysis/HandleCallAction.cs | 17 ++++++-- .../Reflection/NestedTypeUsedViaReflection.cs | 43 +++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCases/TestSuites.cs b/src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCases/TestSuites.cs index 1adbd655afa5d..0ac9ba0eb1134 100644 --- a/src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCases/TestSuites.cs +++ b/src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCases/TestSuites.cs @@ -75,6 +75,7 @@ public void Reflection (string t) case "UnsafeAccessor": case "TypeUsedViaReflection": case "RunClassConstructor": + case "NestedTypeUsedViaReflection": Run (t); break; default: diff --git a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs index a58bcdd7ea7a0..c6b8e73a93ba8 100644 --- a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs +++ b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs @@ -530,6 +530,15 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers break; } + const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = + DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | + DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | + DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | + DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | + DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | + DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | + DynamicallyAccessedMemberTypes.Interfaces; + BindingFlags? bindingFlags; if (calledMethod.HasParameterOfType ((ParameterIndex) 2, "System.Reflection.BindingFlags")) bindingFlags = GetBindingFlagsFromValue (argumentValues[1]); @@ -554,8 +563,8 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers _requireDynamicallyAccessedMembersAction.Invoke (value, targetValue); // We only applied the annotation based on binding flags, so we will keep the necessary types - // but we will not keep anything on them. So the return value has no known annotations on it - AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypes.None)); + // and we keep the set of implicitly available members on them. + AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, ImplicitNestedTypeAccessLevel)); } } } else if (value is NullValue) { @@ -567,11 +576,11 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers // If the input is an annotated value which has All - we can propagate that to the return value // since All applies recursively to all nested type (see MarkStep.MarkEntireType). - // Otherwise we only mark the nested type itself, nothing on it, so the return value has no annotation on it. + // Otherwise we mark the nested type with implicitly available members on it. if (value is ValueWithDynamicallyAccessedMembers { DynamicallyAccessedMemberTypes: DynamicallyAccessedMemberTypes.All }) AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypes.All)); else - AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypes.None)); + AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, ImplicitNestedTypeAccessLevel)); } } } diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/NestedTypeUsedViaReflection.cs b/src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/NestedTypeUsedViaReflection.cs index e02436f7d1285..4d95bd68bd39c 100644 --- a/src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/NestedTypeUsedViaReflection.cs +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/NestedTypeUsedViaReflection.cs @@ -2,8 +2,10 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Helpers; namespace Mono.Linker.Tests.Cases.Reflection { @@ -28,6 +30,9 @@ public static void Main () UnsupportedBindingFlags.Test (); MemberOnNestedType.Test (); + MemberOnUnknownBindingFlagsAndName.Test (BindingFlags.Public, "DoesntMatter"); + + GetNestedTypeOnAnnotatedLocation.Test (null); } [Kept] @@ -202,6 +207,31 @@ public static void Test () } } + static class MemberOnUnknownBindingFlagsAndName + { + [Kept] + public static class PublicNestedType + { + [Kept] + public static int SomeField; + + [Kept] + private static void SomeMethod() { } + } + + [Kept] + [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresAll))] + public static void Test (BindingFlags bindingFlags, string name) + { + // Since the binding flags and name are not known trimming tools should mark all nested types on the type + // It should also mark the members that are expected and satisfy the requirements + typeof (MemberOnUnknownBindingFlagsAndName).GetNestedType (name, bindingFlags).RequiresPublicFields (); + + // Should warn + typeof (MemberOnUnknownBindingFlagsAndName).GetNestedType (name, bindingFlags).RequiresAll (); + } + } + [Kept] static class IgnoreCaseBindingFlags { @@ -245,5 +275,18 @@ public static void Test () _ = typeof (UnsupportedBindingFlags).GetNestedType ("SuppressChangeTypeNestedType", BindingFlags.SuppressChangeType); } } + + [Kept] + private class GetNestedTypeOnAnnotatedLocation + { + [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresAll))] + [Kept] + public static void Test ([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)] Type t) + { + t.GetNestedType ("DoesntMatter").RequiresPublicConstructors (); + t.GetNestedType ("DoesntMatter").RequiresNonPublicMethodsWithInherited (); + t.GetNestedType ("DoesntMatter").RequiresAll (); + } + } } } From a7d303f8a5725e1ee8225986af995f154c56c7e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 14 Nov 2024 09:21:39 +0100 Subject: [PATCH 2/8] Temp change, this should be separate PR --- .../ref/System.Diagnostics.Tracing.cs | 6 +-- .../System/Diagnostics/Tracing/EventSource.cs | 8 +-- ...iCompatBaseline.NetCoreAppLatestStable.xml | 54 +++++++++++++++++++ 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Diagnostics.Tracing/ref/System.Diagnostics.Tracing.cs b/src/libraries/System.Diagnostics.Tracing/ref/System.Diagnostics.Tracing.cs index f4a06e51691b0..30f85c26f8525 100644 --- a/src/libraries/System.Diagnostics.Tracing/ref/System.Diagnostics.Tracing.cs +++ b/src/libraries/System.Diagnostics.Tracing/ref/System.Diagnostics.Tracing.cs @@ -145,7 +145,7 @@ public enum EventOpcode Send = 9, Receive = 240, } - [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] + [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicNestedTypes)] public partial class EventSource : System.IDisposable { protected EventSource() { } @@ -164,8 +164,8 @@ public event System.EventHandler GetSources() { throw null; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 0b727874ddc5a..73db299b6e018 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -399,7 +399,10 @@ public static string GetName(Type eventSourceType) return GetName(eventSourceType, EventManifestOptions.None); } - private const DynamicallyAccessedMemberTypes ManifestMemberTypes = DynamicallyAccessedMemberTypes.All; + private const DynamicallyAccessedMemberTypes ManifestMemberTypes = + DynamicallyAccessedMemberTypes.PublicMethods + | DynamicallyAccessedMemberTypes.NonPublicMethods + | DynamicallyAccessedMemberTypes.PublicNestedTypes; /// /// Returns a string of the XML manifest associated with the eventSourceType. The scheme for this XML is @@ -2517,9 +2520,6 @@ internal partial struct EventMetadata private TraceLoggingEventTypes _traceLoggingEventTypes; public TraceLoggingEventTypes TraceLoggingEventTypes { - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2112:ReflectionToRequiresUnreferencedCode", - Justification = "EnsureDescriptorsInitialized's use of GetType preserves this method which " + - "requires unreferenced code, but EnsureDescriptorsInitialized does not access this member and is safe to call.")] [RequiresUnreferencedCode(EventSourceRequiresUnreferenceMessage)] get { diff --git a/src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml b/src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml index f74069e67951e..92e83d32f8c46 100644 --- a/src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml +++ b/src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml @@ -277,12 +277,30 @@ net9.0/mscorlib.dll net10.0/mscorlib.dll + + CP0015 + M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String,System.Diagnostics.Tracing.EventManifestOptions)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/mscorlib.dll + net10.0/mscorlib.dll + + + CP0015 + M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/mscorlib.dll + net10.0/mscorlib.dll + CP0015 M:System.MulticastDelegate.#ctor(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] net9.0/mscorlib.dll net10.0/mscorlib.dll + + CP0015 + T:System.Diagnostics.Tracing.EventSource:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/mscorlib.dll + net10.0/mscorlib.dll + CP0015 M:System.Delegate.#ctor(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] @@ -307,18 +325,54 @@ net9.0/netstandard.dll net10.0/netstandard.dll + + CP0015 + M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String,System.Diagnostics.Tracing.EventManifestOptions)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/netstandard.dll + net10.0/netstandard.dll + + + CP0015 + M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/netstandard.dll + net10.0/netstandard.dll + CP0015 M:System.MulticastDelegate.#ctor(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] net9.0/netstandard.dll net10.0/netstandard.dll + + CP0015 + T:System.Diagnostics.Tracing.EventSource:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/netstandard.dll + net10.0/netstandard.dll + CP0015 T:System.Security.Cryptography.AesGcm:[T:System.Runtime.Versioning.SupportedOSPlatformAttribute] net9.0/netstandard.dll net10.0/netstandard.dll + + CP0015 + M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String,System.Diagnostics.Tracing.EventManifestOptions)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/System.Diagnostics.Tracing.dll + net10.0/System.Diagnostics.Tracing.dll + + + CP0015 + M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/System.Diagnostics.Tracing.dll + net10.0/System.Diagnostics.Tracing.dll + + + CP0015 + T:System.Diagnostics.Tracing.EventSource:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] + net9.0/System.Diagnostics.Tracing.dll + net10.0/System.Diagnostics.Tracing.dll + CP0015 M:System.Delegate.#ctor(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] From a8336f9005d7103909a8cf974d743c06f671b704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 14 Nov 2024 10:20:19 +0100 Subject: [PATCH 3/8] Fix test --- .../Reflection/NestedTypeUsedViaReflection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/NestedTypeUsedViaReflection.cs b/src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/NestedTypeUsedViaReflection.cs index 4d95bd68bd39c..64cd5b93c0ac9 100644 --- a/src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/NestedTypeUsedViaReflection.cs +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/NestedTypeUsedViaReflection.cs @@ -281,7 +281,7 @@ private class GetNestedTypeOnAnnotatedLocation { [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresAll))] [Kept] - public static void Test ([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)] Type t) + public static void Test ([KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute)), DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)] Type t) { t.GetNestedType ("DoesntMatter").RequiresPublicConstructors (); t.GetNestedType ("DoesntMatter").RequiresNonPublicMethodsWithInherited (); From e14074a9155c4d42ea001a28d48061a7d67a70a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 14 Nov 2024 12:07:35 +0100 Subject: [PATCH 4/8] Match marking algorithm --- .../DynamicallyAccessedMembersBinder.cs | 10 ++++------ .../DynamicallyAccessedMembersBinder.cs | 9 +++------ .../TrimAnalysis/HandleCallAction.cs | 18 +++++++++--------- .../DynamicallyAccessedMembersBinder.cs | 9 +++------ 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs b/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs index 14372b423f332..2e9d5ba04e844 100644 --- a/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs +++ b/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs @@ -6,6 +6,8 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; +using ILLink.Shared.TrimAnalysis; + using Internal.TypeSystem; using Internal.TypeSystem.Ecma; @@ -84,9 +86,7 @@ public static IEnumerable GetDynamicallyAccessedMembers(this T foreach (var t in typeDefinition.ApplyIncludeInherited(t => t.GetNestedTypesOnType(filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return t; - var members = new List(); - t.GetAllOnType(declaredOnly: false, members); - foreach (var m in members) + foreach (var m in t.GetDynamicallyAccessedMembers(HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -97,9 +97,7 @@ public static IEnumerable GetDynamicallyAccessedMembers(this T foreach (var t in typeDefinition.ApplyIncludeInherited(t => t.GetNestedTypesOnType(filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return t; - var members = new List(); - t.GetAllOnType(declaredOnly: false, members); - foreach (var m in members) + foreach (var m in t.GetDynamicallyAccessedMembers(HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } diff --git a/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs b/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs index 0a341169593f6..4db64cd580f67 100644 --- a/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs +++ b/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Reflection; +using ILLink.Shared.TrimAnalysis; using Microsoft.CodeAnalysis; namespace ILLink.RoslynAnalyzer.DataFlow @@ -72,9 +73,7 @@ public static IEnumerable GetDynamicallyAccessedMembers (this ITypeSymb bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (t => t.GetNestedTypesOnType (filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return nested; - var members = new List (); - nested.GetAllOnType (declaredOnly: false, members); - foreach (var m in members) + foreach (var m in nested.GetDynamicallyAccessedMembers (HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -83,9 +82,7 @@ public static IEnumerable GetDynamicallyAccessedMembers (this ITypeSymb bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (t => t.GetNestedTypesOnType (filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return nested; - var members = new List (); - nested.GetAllOnType (declaredOnly: false, members); - foreach (var m in members) + foreach (var m in nested.GetDynamicallyAccessedMembers (HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } diff --git a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs index c6b8e73a93ba8..a3d1775285303 100644 --- a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs +++ b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs @@ -21,6 +21,15 @@ namespace ILLink.Shared.TrimAnalysis [StructLayout (LayoutKind.Auto)] // A good way to avoid CS0282, we don't really care about field order internal partial struct HandleCallAction { + internal const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = + DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | + DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | + DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | + DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | + DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | + DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | + DynamicallyAccessedMemberTypes.Interfaces; + private static ValueSetLattice MultiValueLattice => default; private readonly DiagnosticContext _diagnosticContext; @@ -530,15 +539,6 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers break; } - const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = - DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | - DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | - DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | - DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | - DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | - DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | - DynamicallyAccessedMemberTypes.Interfaces; - BindingFlags? bindingFlags; if (calledMethod.HasParameterOfType ((ParameterIndex) 2, "System.Reflection.BindingFlags")) bindingFlags = GetBindingFlagsFromValue (argumentValues[1]); diff --git a/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs b/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs index 9c2f276cb0f49..baf8edccf3d69 100644 --- a/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs +++ b/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs @@ -6,6 +6,7 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; using ILLink.Shared; +using ILLink.Shared.TrimAnalysis; using Mono.Cecil; namespace Mono.Linker @@ -73,9 +74,7 @@ public static IEnumerable GetDynamicallyAccessedMembers bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (context, t => t.GetNestedTypesOnType (context, filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return nested; - var members = new List (); - nested.GetAllOnType (context, declaredOnly: false, members); - foreach (var m in members) + foreach (var m in nested.GetDynamicallyAccessedMembers (context, HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -84,9 +83,7 @@ public static IEnumerable GetDynamicallyAccessedMembers bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (context, t => t.GetNestedTypesOnType (context, filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return nested; - var members = new List (); - nested.GetAllOnType (context, declaredOnly: false, members); - foreach (var m in members) + foreach (var m in nested.GetDynamicallyAccessedMembers (context, HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } From 97b9c104934a580165c21e7ef5d74975ffaa5f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 14 Nov 2024 13:05:45 +0100 Subject: [PATCH 5/8] Crossgen build break --- .../Dataflow/DynamicallyAccessedMembersBinder.cs | 6 ++---- .../DataFlow/DynamicallyAccessedMembersBinder.cs | 5 ++--- .../DynamicallyAccessedMemberTypesEx.cs | 9 +++++++++ .../ILLink.Shared/TrimAnalysis/HandleCallAction.cs | 13 ++----------- .../DynamicallyAccessedMembersBinder.cs | 5 ++--- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs b/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs index 2e9d5ba04e844..58eb6874632f7 100644 --- a/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs +++ b/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs @@ -6,8 +6,6 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; -using ILLink.Shared.TrimAnalysis; - using Internal.TypeSystem; using Internal.TypeSystem.Ecma; @@ -86,7 +84,7 @@ public static IEnumerable GetDynamicallyAccessedMembers(this T foreach (var t in typeDefinition.ApplyIncludeInherited(t => t.GetNestedTypesOnType(filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return t; - foreach (var m in t.GetDynamicallyAccessedMembers(HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in t.GetDynamicallyAccessedMembers(DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -97,7 +95,7 @@ public static IEnumerable GetDynamicallyAccessedMembers(this T foreach (var t in typeDefinition.ApplyIncludeInherited(t => t.GetNestedTypesOnType(filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return t; - foreach (var m in t.GetDynamicallyAccessedMembers(HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in t.GetDynamicallyAccessedMembers(DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } diff --git a/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs b/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs index 4db64cd580f67..bf52d969cb28d 100644 --- a/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs +++ b/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Reflection; -using ILLink.Shared.TrimAnalysis; using Microsoft.CodeAnalysis; namespace ILLink.RoslynAnalyzer.DataFlow @@ -73,7 +72,7 @@ public static IEnumerable GetDynamicallyAccessedMembers (this ITypeSymb bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (t => t.GetNestedTypesOnType (filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in nested.GetDynamicallyAccessedMembers (DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -82,7 +81,7 @@ public static IEnumerable GetDynamicallyAccessedMembers (this ITypeSymb bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (t => t.GetNestedTypesOnType (filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in nested.GetDynamicallyAccessedMembers (DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } diff --git a/src/tools/illink/src/ILLink.Shared/DynamicallyAccessedMemberTypesEx.cs b/src/tools/illink/src/ILLink.Shared/DynamicallyAccessedMemberTypesEx.cs index d73a10daeeede..a06b031ce2b32 100644 --- a/src/tools/illink/src/ILLink.Shared/DynamicallyAccessedMemberTypesEx.cs +++ b/src/tools/illink/src/ILLink.Shared/DynamicallyAccessedMemberTypesEx.cs @@ -46,5 +46,14 @@ public static class DynamicallyAccessedMemberTypesEx /// Specifies all public nested types, including those inherited from base classes. /// public const DynamicallyAccessedMemberTypes PublicNestedTypesWithInherited = DynamicallyAccessedMemberTypes.PublicNestedTypes | (DynamicallyAccessedMemberTypes) 0x200000; + + internal const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = + DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | + DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | + DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | + DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | + DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | + DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | + DynamicallyAccessedMemberTypes.Interfaces; } } diff --git a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs index a3d1775285303..042139fa3cc2c 100644 --- a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs +++ b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs @@ -21,15 +21,6 @@ namespace ILLink.Shared.TrimAnalysis [StructLayout (LayoutKind.Auto)] // A good way to avoid CS0282, we don't really care about field order internal partial struct HandleCallAction { - internal const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = - DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | - DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | - DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | - DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | - DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | - DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | - DynamicallyAccessedMemberTypes.Interfaces; - private static ValueSetLattice MultiValueLattice => default; private readonly DiagnosticContext _diagnosticContext; @@ -564,7 +555,7 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers // We only applied the annotation based on binding flags, so we will keep the necessary types // and we keep the set of implicitly available members on them. - AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, ImplicitNestedTypeAccessLevel)); + AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel)); } } } else if (value is NullValue) { @@ -580,7 +571,7 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers if (value is ValueWithDynamicallyAccessedMembers { DynamicallyAccessedMemberTypes: DynamicallyAccessedMemberTypes.All }) AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypes.All)); else - AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, ImplicitNestedTypeAccessLevel)); + AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel)); } } } diff --git a/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs b/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs index baf8edccf3d69..808e0ec8d6555 100644 --- a/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs +++ b/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs @@ -6,7 +6,6 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; using ILLink.Shared; -using ILLink.Shared.TrimAnalysis; using Mono.Cecil; namespace Mono.Linker @@ -74,7 +73,7 @@ public static IEnumerable GetDynamicallyAccessedMembers bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (context, t => t.GetNestedTypesOnType (context, filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (context, HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in nested.GetDynamicallyAccessedMembers (context, DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -83,7 +82,7 @@ public static IEnumerable GetDynamicallyAccessedMembers bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (context, t => t.GetNestedTypesOnType (context, filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (context, HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in nested.GetDynamicallyAccessedMembers (context, DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } From d870c2dfcde8e9d544a4bf5f9b3bd36167ef3522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 14 Nov 2024 23:11:28 +0100 Subject: [PATCH 6/8] Revert "Crossgen build break" This reverts commit 97b9c104934a580165c21e7ef5d74975ffaa5f9b. --- .../Dataflow/DynamicallyAccessedMembersBinder.cs | 6 ++++-- .../DataFlow/DynamicallyAccessedMembersBinder.cs | 5 +++-- .../DynamicallyAccessedMemberTypesEx.cs | 9 --------- .../ILLink.Shared/TrimAnalysis/HandleCallAction.cs | 13 +++++++++++-- .../DynamicallyAccessedMembersBinder.cs | 5 +++-- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs b/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs index 58eb6874632f7..2e9d5ba04e844 100644 --- a/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs +++ b/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs @@ -6,6 +6,8 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; +using ILLink.Shared.TrimAnalysis; + using Internal.TypeSystem; using Internal.TypeSystem.Ecma; @@ -84,7 +86,7 @@ public static IEnumerable GetDynamicallyAccessedMembers(this T foreach (var t in typeDefinition.ApplyIncludeInherited(t => t.GetNestedTypesOnType(filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return t; - foreach (var m in t.GetDynamicallyAccessedMembers(DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in t.GetDynamicallyAccessedMembers(HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -95,7 +97,7 @@ public static IEnumerable GetDynamicallyAccessedMembers(this T foreach (var t in typeDefinition.ApplyIncludeInherited(t => t.GetNestedTypesOnType(filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return t; - foreach (var m in t.GetDynamicallyAccessedMembers(DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in t.GetDynamicallyAccessedMembers(HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } diff --git a/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs b/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs index bf52d969cb28d..4db64cd580f67 100644 --- a/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs +++ b/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Reflection; +using ILLink.Shared.TrimAnalysis; using Microsoft.CodeAnalysis; namespace ILLink.RoslynAnalyzer.DataFlow @@ -72,7 +73,7 @@ public static IEnumerable GetDynamicallyAccessedMembers (this ITypeSymb bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (t => t.GetNestedTypesOnType (filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in nested.GetDynamicallyAccessedMembers (HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -81,7 +82,7 @@ public static IEnumerable GetDynamicallyAccessedMembers (this ITypeSymb bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (t => t.GetNestedTypesOnType (filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in nested.GetDynamicallyAccessedMembers (HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } diff --git a/src/tools/illink/src/ILLink.Shared/DynamicallyAccessedMemberTypesEx.cs b/src/tools/illink/src/ILLink.Shared/DynamicallyAccessedMemberTypesEx.cs index a06b031ce2b32..d73a10daeeede 100644 --- a/src/tools/illink/src/ILLink.Shared/DynamicallyAccessedMemberTypesEx.cs +++ b/src/tools/illink/src/ILLink.Shared/DynamicallyAccessedMemberTypesEx.cs @@ -46,14 +46,5 @@ public static class DynamicallyAccessedMemberTypesEx /// Specifies all public nested types, including those inherited from base classes. /// public const DynamicallyAccessedMemberTypes PublicNestedTypesWithInherited = DynamicallyAccessedMemberTypes.PublicNestedTypes | (DynamicallyAccessedMemberTypes) 0x200000; - - internal const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = - DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | - DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | - DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | - DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | - DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | - DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | - DynamicallyAccessedMemberTypes.Interfaces; } } diff --git a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs index 042139fa3cc2c..a3d1775285303 100644 --- a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs +++ b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs @@ -21,6 +21,15 @@ namespace ILLink.Shared.TrimAnalysis [StructLayout (LayoutKind.Auto)] // A good way to avoid CS0282, we don't really care about field order internal partial struct HandleCallAction { + internal const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = + DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | + DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | + DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | + DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | + DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | + DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | + DynamicallyAccessedMemberTypes.Interfaces; + private static ValueSetLattice MultiValueLattice => default; private readonly DiagnosticContext _diagnosticContext; @@ -555,7 +564,7 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers // We only applied the annotation based on binding flags, so we will keep the necessary types // and we keep the set of implicitly available members on them. - AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel)); + AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, ImplicitNestedTypeAccessLevel)); } } } else if (value is NullValue) { @@ -571,7 +580,7 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers if (value is ValueWithDynamicallyAccessedMembers { DynamicallyAccessedMemberTypes: DynamicallyAccessedMemberTypes.All }) AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypes.All)); else - AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel)); + AddReturnValue (_annotations.GetMethodReturnValue (calledMethod, _isNewObj, ImplicitNestedTypeAccessLevel)); } } } diff --git a/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs b/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs index 808e0ec8d6555..baf8edccf3d69 100644 --- a/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs +++ b/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs @@ -6,6 +6,7 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; using ILLink.Shared; +using ILLink.Shared.TrimAnalysis; using Mono.Cecil; namespace Mono.Linker @@ -73,7 +74,7 @@ public static IEnumerable GetDynamicallyAccessedMembers bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (context, t => t.GetNestedTypesOnType (context, filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (context, DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in nested.GetDynamicallyAccessedMembers (context, HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } @@ -82,7 +83,7 @@ public static IEnumerable GetDynamicallyAccessedMembers bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (context, t => t.GetNestedTypesOnType (context, filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (context, DynamicallyAccessedMemberTypesEx.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + foreach (var m in nested.GetDynamicallyAccessedMembers (context, HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) yield return m; } } From 4097d076ed6947287132b55f7cc9bd95bf085d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 14 Nov 2024 23:11:39 +0100 Subject: [PATCH 7/8] Revert "Match marking algorithm" This reverts commit e14074a9155c4d42ea001a28d48061a7d67a70a9. --- .../DynamicallyAccessedMembersBinder.cs | 10 ++++++---- .../DynamicallyAccessedMembersBinder.cs | 9 ++++++--- .../TrimAnalysis/HandleCallAction.cs | 18 +++++++++--------- .../DynamicallyAccessedMembersBinder.cs | 9 ++++++--- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs b/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs index 2e9d5ba04e844..14372b423f332 100644 --- a/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs +++ b/src/coreclr/tools/Common/Compiler/Dataflow/DynamicallyAccessedMembersBinder.cs @@ -6,8 +6,6 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; -using ILLink.Shared.TrimAnalysis; - using Internal.TypeSystem; using Internal.TypeSystem.Ecma; @@ -86,7 +84,9 @@ public static IEnumerable GetDynamicallyAccessedMembers(this T foreach (var t in typeDefinition.ApplyIncludeInherited(t => t.GetNestedTypesOnType(filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return t; - foreach (var m in t.GetDynamicallyAccessedMembers(HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + var members = new List(); + t.GetAllOnType(declaredOnly: false, members); + foreach (var m in members) yield return m; } } @@ -97,7 +97,9 @@ public static IEnumerable GetDynamicallyAccessedMembers(this T foreach (var t in typeDefinition.ApplyIncludeInherited(t => t.GetNestedTypesOnType(filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return t; - foreach (var m in t.GetDynamicallyAccessedMembers(HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + var members = new List(); + t.GetAllOnType(declaredOnly: false, members); + foreach (var m in members) yield return m; } } diff --git a/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs b/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs index 4db64cd580f67..0a341169593f6 100644 --- a/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs +++ b/src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/DynamicallyAccessedMembersBinder.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Reflection; -using ILLink.Shared.TrimAnalysis; using Microsoft.CodeAnalysis; namespace ILLink.RoslynAnalyzer.DataFlow @@ -73,7 +72,9 @@ public static IEnumerable GetDynamicallyAccessedMembers (this ITypeSymb bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (t => t.GetNestedTypesOnType (filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + var members = new List (); + nested.GetAllOnType (declaredOnly: false, members); + foreach (var m in members) yield return m; } } @@ -82,7 +83,9 @@ public static IEnumerable GetDynamicallyAccessedMembers (this ITypeSymb bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (t => t.GetNestedTypesOnType (filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + var members = new List (); + nested.GetAllOnType (declaredOnly: false, members); + foreach (var m in members) yield return m; } } diff --git a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs index a3d1775285303..c6b8e73a93ba8 100644 --- a/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs +++ b/src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs @@ -21,15 +21,6 @@ namespace ILLink.Shared.TrimAnalysis [StructLayout (LayoutKind.Auto)] // A good way to avoid CS0282, we don't really care about field order internal partial struct HandleCallAction { - internal const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = - DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | - DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | - DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | - DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | - DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | - DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | - DynamicallyAccessedMemberTypes.Interfaces; - private static ValueSetLattice MultiValueLattice => default; private readonly DiagnosticContext _diagnosticContext; @@ -539,6 +530,15 @@ ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers break; } + const DynamicallyAccessedMemberTypes ImplicitNestedTypeAccessLevel = + DynamicallyAccessedMemberTypesEx.PublicConstructorsWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicConstructorsWithInherited | + DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypesEx.NonPublicMethodsWithInherited | + DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypesEx.NonPublicFieldsWithInherited | + DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypesEx.NonPublicPropertiesWithInherited | + DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypesEx.NonPublicEventsWithInherited | + DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited | DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited | + DynamicallyAccessedMemberTypes.Interfaces; + BindingFlags? bindingFlags; if (calledMethod.HasParameterOfType ((ParameterIndex) 2, "System.Reflection.BindingFlags")) bindingFlags = GetBindingFlagsFromValue (argumentValues[1]); diff --git a/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs b/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs index baf8edccf3d69..9c2f276cb0f49 100644 --- a/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs +++ b/src/tools/illink/src/linker/Linker.Dataflow/DynamicallyAccessedMembersBinder.cs @@ -6,7 +6,6 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; using ILLink.Shared; -using ILLink.Shared.TrimAnalysis; using Mono.Cecil; namespace Mono.Linker @@ -74,7 +73,9 @@ public static IEnumerable GetDynamicallyAccessedMembers bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.NonPublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (context, t => t.GetNestedTypesOnType (context, filter: null, bindingFlags: BindingFlags.NonPublic), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (context, HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + var members = new List (); + nested.GetAllOnType (context, declaredOnly: false, members); + foreach (var m in members) yield return m; } } @@ -83,7 +84,9 @@ public static IEnumerable GetDynamicallyAccessedMembers bool withInherited = !declaredOnly && memberTypes.HasFlag (DynamicallyAccessedMemberTypesEx.PublicNestedTypesWithInherited); foreach (var nested in typeDefinition.ApplyIncludeInherited (context, t => t.GetNestedTypesOnType (context, filter: null, bindingFlags: BindingFlags.Public), withInherited)) { yield return nested; - foreach (var m in nested.GetDynamicallyAccessedMembers (context, HandleCallAction.ImplicitNestedTypeAccessLevel, declaredOnly: false)) + var members = new List (); + nested.GetAllOnType (context, declaredOnly: false, members); + foreach (var m in members) yield return m; } } From 15d7dcc877cd347de5b66a22b898a95471c5a7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 14 Nov 2024 23:11:50 +0100 Subject: [PATCH 8/8] Revert "Temp change, this should be separate PR" This reverts commit a7d303f8a5725e1ee8225986af995f154c56c7e4. --- .../ref/System.Diagnostics.Tracing.cs | 6 +-- .../System/Diagnostics/Tracing/EventSource.cs | 8 +-- ...iCompatBaseline.NetCoreAppLatestStable.xml | 54 ------------------- 3 files changed, 7 insertions(+), 61 deletions(-) diff --git a/src/libraries/System.Diagnostics.Tracing/ref/System.Diagnostics.Tracing.cs b/src/libraries/System.Diagnostics.Tracing/ref/System.Diagnostics.Tracing.cs index 30f85c26f8525..f4a06e51691b0 100644 --- a/src/libraries/System.Diagnostics.Tracing/ref/System.Diagnostics.Tracing.cs +++ b/src/libraries/System.Diagnostics.Tracing/ref/System.Diagnostics.Tracing.cs @@ -145,7 +145,7 @@ public enum EventOpcode Send = 9, Receive = 240, } - [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicNestedTypes)] + [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] public partial class EventSource : System.IDisposable { protected EventSource() { } @@ -164,8 +164,8 @@ public event System.EventHandler GetSources() { throw null; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 73db299b6e018..0b727874ddc5a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -399,10 +399,7 @@ public static string GetName(Type eventSourceType) return GetName(eventSourceType, EventManifestOptions.None); } - private const DynamicallyAccessedMemberTypes ManifestMemberTypes = - DynamicallyAccessedMemberTypes.PublicMethods - | DynamicallyAccessedMemberTypes.NonPublicMethods - | DynamicallyAccessedMemberTypes.PublicNestedTypes; + private const DynamicallyAccessedMemberTypes ManifestMemberTypes = DynamicallyAccessedMemberTypes.All; /// /// Returns a string of the XML manifest associated with the eventSourceType. The scheme for this XML is @@ -2520,6 +2517,9 @@ internal partial struct EventMetadata private TraceLoggingEventTypes _traceLoggingEventTypes; public TraceLoggingEventTypes TraceLoggingEventTypes { + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2112:ReflectionToRequiresUnreferencedCode", + Justification = "EnsureDescriptorsInitialized's use of GetType preserves this method which " + + "requires unreferenced code, but EnsureDescriptorsInitialized does not access this member and is safe to call.")] [RequiresUnreferencedCode(EventSourceRequiresUnreferenceMessage)] get { diff --git a/src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml b/src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml index 92e83d32f8c46..f74069e67951e 100644 --- a/src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml +++ b/src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml @@ -277,30 +277,12 @@ net9.0/mscorlib.dll net10.0/mscorlib.dll - - CP0015 - M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String,System.Diagnostics.Tracing.EventManifestOptions)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/mscorlib.dll - net10.0/mscorlib.dll - - - CP0015 - M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/mscorlib.dll - net10.0/mscorlib.dll - CP0015 M:System.MulticastDelegate.#ctor(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] net9.0/mscorlib.dll net10.0/mscorlib.dll - - CP0015 - T:System.Diagnostics.Tracing.EventSource:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/mscorlib.dll - net10.0/mscorlib.dll - CP0015 M:System.Delegate.#ctor(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] @@ -325,54 +307,18 @@ net9.0/netstandard.dll net10.0/netstandard.dll - - CP0015 - M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String,System.Diagnostics.Tracing.EventManifestOptions)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/netstandard.dll - net10.0/netstandard.dll - - - CP0015 - M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/netstandard.dll - net10.0/netstandard.dll - CP0015 M:System.MulticastDelegate.#ctor(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] net9.0/netstandard.dll net10.0/netstandard.dll - - CP0015 - T:System.Diagnostics.Tracing.EventSource:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/netstandard.dll - net10.0/netstandard.dll - CP0015 T:System.Security.Cryptography.AesGcm:[T:System.Runtime.Versioning.SupportedOSPlatformAttribute] net9.0/netstandard.dll net10.0/netstandard.dll - - CP0015 - M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String,System.Diagnostics.Tracing.EventManifestOptions)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/System.Diagnostics.Tracing.dll - net10.0/System.Diagnostics.Tracing.dll - - - CP0015 - M:System.Diagnostics.Tracing.EventSource.GenerateManifest(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/System.Diagnostics.Tracing.dll - net10.0/System.Diagnostics.Tracing.dll - - - CP0015 - T:System.Diagnostics.Tracing.EventSource:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute] - net9.0/System.Diagnostics.Tracing.dll - net10.0/System.Diagnostics.Tracing.dll - CP0015 M:System.Delegate.#ctor(System.Type,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute]