Skip to content

Commit

Permalink
Warn on trimming with COM hosting (built-in COM support) enabled (#72493
Browse files Browse the repository at this point in the history
)
  • Loading branch information
elinor-fung authored Jul 21, 2022
1 parent 778137b commit f5469fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 0 additions & 1 deletion eng/testing/linker/SupportFiles/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<SkipConfigureTrimming>true</SkipConfigureTrimming>
<PublishTrimmed>true</PublishTrimmed>
<SkipImportRepoLinkerTargets>true</SkipImportRepoLinkerTargets>
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
<TrimmerDefaultAction>link</TrimmerDefaultAction>
<TrimMode>link</TrimMode>
<TrimmerRemoveSymbols>false</TrimmerRemoveSymbols>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
<property name="Target">M:System.StartupHookProvider.ProcessStartupHooks()</property>
<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app with System.StartupHookProvider.IsSupported=true.</property>
</attribute>
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
<argument>ILLink</argument>
<argument>IL2026</argument>
<property name="Scope">member</property>
<property name="Target">M:Internal.Runtime.InteropServices.ComActivator.GetClassFactoryForTypeInternal(Internal.Runtime.InteropServices.ComActivationContextInternal*)</property>
<property name="Justification">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.</property>
</attribute>
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
<argument>ILLink</argument>
<argument>IL2026</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -217,7 +216,6 @@ private static void ClassRegistrationScenarioForType(ComActivationContext cxt, b
/// Internal entry point for unmanaged COM activation API from native code
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
[UnmanagedCallersOnly]
private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
{
Expand All @@ -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);
}
Expand All @@ -259,7 +259,6 @@ private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInt
/// Internal entry point for registering a managed COM server API from native code
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
[UnmanagedCallersOnly]
private static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
{
Expand Down Expand Up @@ -291,20 +290,24 @@ private static unsafe int RegisterClassForTypeInternal(ComActivationContextInter
try
{
var cxt = ComActivationContext.Create(ref cxtInt);
ClassRegistrationScenarioForType(cxt, register: true);
ClassRegistrationScenarioForTypeLocal(cxt, register: true);
}
catch (Exception e)
{
return e.HResult;
}

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);
}

/// <summary>
/// Internal entry point for unregistering a managed COM server API from native code
/// </summary>
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
[UnmanagedCallersOnly]
private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
{
Expand Down Expand Up @@ -336,14 +339,19 @@ private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInt
try
{
var cxt = ComActivationContext.Create(ref cxtInt);
ClassRegistrationScenarioForType(cxt, register: false);
ClassRegistrationScenarioForTypeLocal(cxt, register: false);
}
catch (Exception e)
{
return e.HResult;
}

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()
Expand Down

0 comments on commit f5469fe

Please sign in to comment.