From f5469fe3ca5fb5fb7f1c4a57ce6dac7c74e32d8c Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Thu, 21 Jul 2022 07:02:41 -0700 Subject: [PATCH] Warn on trimming with COM hosting (built-in COM support) enabled (#72493) --- .../linker/SupportFiles/Directory.Build.props | 1 - .../ILLink.Suppressions.LibraryBuild.xml | 7 +++++++ .../Runtime/InteropServices/ComActivator.cs | 20 +++++++++++++------ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/eng/testing/linker/SupportFiles/Directory.Build.props b/eng/testing/linker/SupportFiles/Directory.Build.props index bb7beb3a3cc8c..c3be2bf7d265a 100644 --- a/eng/testing/linker/SupportFiles/Directory.Build.props +++ b/eng/testing/linker/SupportFiles/Directory.Build.props @@ -3,7 +3,6 @@ true true true - true link link false diff --git a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml index ae5bf9ff04e32..f04e51ff8bc7b 100644 --- a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml +++ b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Suppressions.LibraryBuild.xml @@ -8,6 +8,13 @@ M:System.StartupHookProvider.ProcessStartupHooks() This warning is left in the product so developers get an ILLink warning when trimming an app with System.StartupHookProvider.IsSupported=true. + + ILLink + IL2026 + member + M:Internal.Runtime.InteropServices.ComActivator.GetClassFactoryForTypeInternal(Internal.Runtime.InteropServices.ComActivationContextInternal*) + This warning is left in the product so developers get an ILLink warning when trimming an app with System.Runtime.InteropServices.BuiltInComInterop.IsSupported.IsSupported=true. + ILLink IL2026 diff --git a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs b/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs index da6ccb945e751..f33a45061ea6d 100644 --- a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs +++ b/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs @@ -59,7 +59,6 @@ void CreateInstanceLic( internal partial struct ComActivationContext { - [RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")] public static unsafe ComActivationContext Create(ref ComActivationContextInternal cxtInt) { if (!Marshal.IsBuiltInComSupported) @@ -217,7 +216,6 @@ private static void ClassRegistrationScenarioForType(ComActivationContext cxt, b /// Internal entry point for unmanaged COM activation API from native code /// /// Pointer to a instance - [RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")] [UnmanagedCallersOnly] private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt) { @@ -243,7 +241,9 @@ private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInt try { var cxt = ComActivationContext.Create(ref cxtInt); +#pragma warning disable IL2026 // suppressed in ILLink.Suppressions.LibraryBuild.xml object cf = GetClassFactoryForType(cxt); +#pragma warning restore IL2026 IntPtr nativeIUnknown = Marshal.GetIUnknownForObject(cf); Marshal.WriteIntPtr(cxtInt.ClassFactoryDest, nativeIUnknown); } @@ -259,7 +259,6 @@ private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInt /// Internal entry point for registering a managed COM server API from native code /// /// Pointer to a instance - [RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")] [UnmanagedCallersOnly] private static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt) { @@ -291,7 +290,7 @@ private static unsafe int RegisterClassForTypeInternal(ComActivationContextInter try { var cxt = ComActivationContext.Create(ref cxtInt); - ClassRegistrationScenarioForType(cxt, register: true); + ClassRegistrationScenarioForTypeLocal(cxt, register: true); } catch (Exception e) { @@ -299,12 +298,16 @@ private static unsafe int RegisterClassForTypeInternal(ComActivationContextInter } return 0; + + // Use a local function for a targeted suppression of the requires unreferenced code warning + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "The same feature switch applies to GetClassFactoryForTypeInternal and this function. We rely on the warning from GetClassFactoryForTypeInternal.")] + static void ClassRegistrationScenarioForTypeLocal(ComActivationContext cxt, bool register) => ClassRegistrationScenarioForType(cxt, register); } /// /// Internal entry point for unregistering a managed COM server API from native code /// - [RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")] [UnmanagedCallersOnly] private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt) { @@ -336,7 +339,7 @@ private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInt try { var cxt = ComActivationContext.Create(ref cxtInt); - ClassRegistrationScenarioForType(cxt, register: false); + ClassRegistrationScenarioForTypeLocal(cxt, register: false); } catch (Exception e) { @@ -344,6 +347,11 @@ private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInt } return 0; + + // Use a local function for a targeted suppression of the requires unreferenced code warning + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "The same feature switch applies to GetClassFactoryForTypeInternal and this function. We rely on the warning from GetClassFactoryForTypeInternal.")] + static void ClassRegistrationScenarioForTypeLocal(ComActivationContext cxt, bool register) => ClassRegistrationScenarioForType(cxt, register); } private static bool IsLoggingEnabled()