From a507d4eabf9803db82c949c8e214894934b9be5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Fri, 26 Apr 2024 08:27:59 +0200 Subject: [PATCH] Start running System.Runtime trimming tests --- .../TrimmingTests/InvariantGlobalizationTrue.cs | 17 +++++++++++++++++ .../System.Runtime.TrimmingTests.proj | 6 ++++++ src/libraries/tests.proj | 1 - 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/InvariantGlobalizationTrue.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/InvariantGlobalizationTrue.cs index fe878e3a7459d8..27ab1b136b5a4f 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/InvariantGlobalizationTrue.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/InvariantGlobalizationTrue.cs @@ -4,6 +4,7 @@ using System; using System.Globalization; using System.Reflection; +using System.Runtime.CompilerServices; /// /// Ensures setting InvariantGlobalization = true still works in a trimmed app. @@ -27,6 +28,18 @@ static int Main(string[] args) { return -3; } + + // The rest of this code depends on a property of IL level trimming that keeps reflection + // metadata for anything that is statically reachable. It's not applicable if we're not doing that + // kind of trimming. Approximate what kind of trimming are we doing. + if (GetMethodSecretly(typeof(Program), nameof(GetCoreLibType)) == null) + { + // Sanity check: we only expect this for native AOT; IsDynamicCodeSupported approximates that. + if (RuntimeFeature.IsDynamicCodeSupported) + throw new Exception(); + + return 100; + } // Ensure the internal GlobalizationMode class is trimmed correctly. Type globalizationMode = GetCoreLibType("System.Globalization.GlobalizationMode"); @@ -65,4 +78,8 @@ static int Main(string[] args) // The intention of this method is to ensure the trimmer doesn't preserve the Type. private static Type GetCoreLibType(string name) => typeof(object).Assembly.GetType(name, throwOnError: false); + + // The intention is to look for a method on a type in a way that trimming cannot detect. + private static MethodBase GetMethodSecretly(Type type, string name) => + type.GetMethod(name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/System.Runtime.TrimmingTests.proj b/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/System.Runtime.TrimmingTests.proj index c9c42b2c20234e..6cfdea28f5e0bd 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/System.Runtime.TrimmingTests.proj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/System.Runtime.TrimmingTests.proj @@ -43,6 +43,8 @@ and tested with this test. Issue https://github.com/dotnet/runtime/issues/48849 is tracking investigation on why this test is failing if not skipped. --> browser-wasm + + true + true NullabilityInfoContextSupport @@ -62,6 +66,8 @@ System.Threading.ThreadPool.UseWindowsThreadPool + + true System.Threading.ThreadPool.UseWindowsThreadPool diff --git a/src/libraries/tests.proj b/src/libraries/tests.proj index 7106a44c89e967..87614b7836eaf2 100644 --- a/src/libraries/tests.proj +++ b/src/libraries/tests.proj @@ -652,7 +652,6 @@ -