Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes COM trimmer warnings #52122

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/workflow/trimming/feature-switches.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ configurations but their defaults might vary as any SDK can set the defaults dif

| MSBuild Property Name | AppContext Setting | Description |
|-|-|-|
| BuiltInComSupport | System.Runtime.InteropServices.Marshal.IsBuiltInComSupported | In-built COM support is trimmed when set to false. |
| CustomResourceTypesSupport | System.Resources.ResourceManager.AllowCustomResourceTypes | Use of custom resource types is disabled when set to false. ResourceManager code paths that use reflection for custom types can be trimmed. |
| DebuggerSupport | System.Diagnostics.Debugger.IsSupported | Any dependency that enables better debugging experience to be trimmed when set to false |
| EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization | System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization | BinaryFormatter serialization support is trimmed when set to false. |
| EnableUnsafeUTF7Encoding | System.Text.Encoding.EnableUnsafeUTF7Encoding | Insecure UTF-7 encoding is trimmed when set to false |
| EnableUnsafeBinaryFormatterSerialization | System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization | BinaryFormatter serialization support is trimmed when set to false |
| EventSourceSupport | System.Diagnostics.Tracing.EventSource.IsSupported | Any EventSource related code or logic is trimmed when set to false |
| InvariantGlobalization | System.Globalization.Invariant | All globalization specific code and data is trimmed when set to true |
| UseSystemResourceKeys | System.Resources.UseSystemResourceKeys | Any localizable resources for system assemblies is trimmed when set to true |
| HttpActivityPropagationSupport | System.Net.Http.EnableActivityPropagation | Any dependency related to diagnostics support for System.Net.Http is trimmed when set to false |
| HttpNativeHandler | System.Net.Http.UseNativeHttpHandler | HttpClient uses by default platform native implementation of HttpMessageHandler if set to true. |
| StartupHookSupport | System.StartupHookProvider.IsSupported | Startup hooks are disabled when set to false. Startup hook related functionality can be trimmed. |
| UseSystemResourceKeys | System.Resources.UseSystemResourceKeys | Any localizable resources for system assemblies is trimmed when set to true |
| TBD | System.Threading.ThreadPool.EnableDispatchAutoreleasePool | When set to true, creates an NSAutoreleasePool around each thread pool work item on applicable platforms. |
| CustomResourceTypesSupport | System.Resources.ResourceManager.AllowCustomResourceTypes | Use of custom resource types is disabled when set to false. ResourceManager code paths that use reflection for custom types can be trimmed. |
| EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization | System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization | BinaryFormatter serialization support is trimmed when set to false. |

Any feature-switch which defines property can be set in csproj file or
on the command line as any other MSBuild property. Those without predefined property name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
<type fullname="System.StartupHookProvider" feature="System.StartupHookProvider.IsSupported" featurevalue="false">
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
</type>
<type fullname="System.Runtime.InteropServices.Marshal" feature="System.Runtime.InteropServices.Marshal.IsBuiltInComSupported" featurevalue="false">
<method signature="System.Boolean get_IsBuiltInComSupported()" body="stub" value="false" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Internal.Runtime.InteropServices
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IClassFactory
{
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
void CreateInstance(
[MarshalAs(UnmanagedType.Interface)] object? pUnkOuter,
ref Guid riid,
Expand All @@ -49,6 +50,7 @@ internal struct LICINFO
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IClassFactory2 : IClassFactory
{
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
new void CreateInstance(
[MarshalAs(UnmanagedType.Interface)] object? pUnkOuter,
ref Guid riid,
Expand Down Expand Up @@ -96,7 +98,7 @@ public struct ComActivationContext
public static unsafe ComActivationContext Create(ref ComActivationContextInternal cxtInt)
{
#if FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -132,7 +134,7 @@ public static class ComActivator
public static object GetClassFactoryForType(ComActivationContext cxt)
{
#if FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -170,7 +172,7 @@ public static object GetClassFactoryForType(ComActivationContext cxt)
public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bool register)
{
#if FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -270,7 +272,7 @@ public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bo
public static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
{
#if FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -317,7 +319,7 @@ public static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInte
public static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
{
#if FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -367,7 +369,7 @@ public static unsafe int RegisterClassForTypeInternal(ComActivationContextIntern
public static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
{
#if FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -432,6 +434,7 @@ private static void Log(string fmt, params object[] args)
#endif
}

[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
private static Type FindClassType(Guid clsid, string assemblyPath, string assemblyName, string typeName)
{
#if FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
Expand Down Expand Up @@ -583,6 +586,7 @@ public static object CreateAggregatedObject(object pUnkOuter, object comObject)
#endif
}

[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
public void CreateInstance(
[MarshalAs(UnmanagedType.Interface)] object? pUnkOuter,
ref Guid riid,
Expand Down Expand Up @@ -632,6 +636,7 @@ public LicenseClassFactory(Guid clsid, Type classType)
#endif
}

[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
public void CreateInstance(
[MarshalAs(UnmanagedType.Interface)] object? pUnkOuter,
ref Guid riid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private static void PrelinkCore(MethodInfo m)
private static extern void InternalPrelink(RuntimeMethodHandleInternal m);

[DllImport(RuntimeHelpers.QCall)]
private static extern bool IsComSupportedInternal();
private static extern bool IsBuiltInComSupportedInternal();

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern /* struct _EXCEPTION_POINTERS* */ IntPtr GetExceptionPointers();
Expand Down Expand Up @@ -236,9 +236,9 @@ private static object PtrToStructureHelper(IntPtr ptr, Type structureType)
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool IsPinnable(object? obj);

internal static bool IsComSupported { get; } = InitializeIsComSupported();
internal static bool IsBuiltInComSupported { get; } = InitializeIsBuiltInComSupported();

private static bool InitializeIsComSupported() => IsComSupportedInternal();
private static bool InitializeIsBuiltInComSupported() => IsBuiltInComSupportedInternal();

#if TARGET_WINDOWS
/// <summary>
Expand Down Expand Up @@ -296,7 +296,7 @@ public static string GetTypeInfoName(ITypeInfo typeInfo)
// on Marshal for more consistent API surface.
internal static Type? GetTypeFromCLSID(Guid clsid, string? server, bool throwOnError)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -443,7 +443,7 @@ public static object GetUniqueObjectForIUnknown(IntPtr unknown)
[SupportedOSPlatform("windows")]
public static IntPtr CreateAggregatedObject(IntPtr pOuter, object o)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -457,7 +457,7 @@ public static IntPtr CreateAggregatedObject(IntPtr pOuter, object o)
[SupportedOSPlatform("windows")]
public static IntPtr CreateAggregatedObject<T>(IntPtr pOuter, T o) where T : notnull
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -484,7 +484,7 @@ public static IntPtr CreateAggregatedObject<T>(IntPtr pOuter, T o) where T : not
[SupportedOSPlatform("windows")]
public static int ReleaseComObject(object o)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -512,7 +512,7 @@ public static int ReleaseComObject(object o)
[SupportedOSPlatform("windows")]
public static int FinalReleaseComObject(object o)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -536,7 +536,7 @@ public static int FinalReleaseComObject(object o)
[SupportedOSPlatform("windows")]
public static object? GetComObjectData(object obj, object key)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -567,7 +567,7 @@ public static int FinalReleaseComObject(object o)
[SupportedOSPlatform("windows")]
public static bool SetComObjectData(object obj, object key, object? data)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -597,7 +597,7 @@ public static bool SetComObjectData(object obj, object key, object? data)
[return: NotNullIfNotNull("o")]
public static object? CreateWrapperOfType(object? o, Type t)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -652,7 +652,7 @@ public static bool SetComObjectData(object obj, object key, object? data)
[SupportedOSPlatform("windows")]
public static TWrapper CreateWrapperOfType<T, TWrapper>(T? o)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -672,7 +672,7 @@ public static TWrapper CreateWrapperOfType<T, TWrapper>(T? o)
[SupportedOSPlatform("windows")]
public static void GetNativeVariantForObject(object? obj, /* VARIANT * */ IntPtr pDstNativeVariant)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -686,7 +686,7 @@ public static void GetNativeVariantForObject(object? obj, /* VARIANT * */ IntPtr
[SupportedOSPlatform("windows")]
public static void GetNativeVariantForObject<T>(T? obj, IntPtr pDstNativeVariant)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -697,7 +697,7 @@ public static void GetNativeVariantForObject<T>(T? obj, IntPtr pDstNativeVariant
[SupportedOSPlatform("windows")]
public static object? GetObjectForNativeVariant(/* VARIANT * */ IntPtr pSrcNativeVariant)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -711,7 +711,7 @@ public static void GetNativeVariantForObject<T>(T? obj, IntPtr pDstNativeVariant
[SupportedOSPlatform("windows")]
public static T? GetObjectForNativeVariant<T>(IntPtr pSrcNativeVariant)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -722,7 +722,7 @@ public static void GetNativeVariantForObject<T>(T? obj, IntPtr pDstNativeVariant
[SupportedOSPlatform("windows")]
public static object?[] GetObjectsForNativeVariants(/* VARIANT * */ IntPtr aSrcNativeVariant, int cVars)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -736,7 +736,7 @@ public static void GetNativeVariantForObject<T>(T? obj, IntPtr pDstNativeVariant
[SupportedOSPlatform("windows")]
public static T[] GetObjectsForNativeVariants<T>(IntPtr aSrcNativeVariant, int cVars)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -764,10 +764,11 @@ public static T[] GetObjectsForNativeVariants<T>(IntPtr aSrcNativeVariant, int c
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int GetEndComSlot(Type t);

[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
[SupportedOSPlatform("windows")]
public static object BindToMoniker(string monikerName)
{
if (!IsComSupported)
if (!IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand All @@ -780,12 +781,18 @@ public static object BindToMoniker(string monikerName)
return obj;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
Justification = "The calling method is guarded")]
[DllImport(Interop.Libraries.Ole32, PreserveSig = false)]
private static extern void CreateBindCtx(uint reserved, out IBindCtx ppbc);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
Justification = "The calling method is guarded")]
[DllImport(Interop.Libraries.Ole32, PreserveSig = false)]
private static extern void MkParseDisplayName(IBindCtx pbc, [MarshalAs(UnmanagedType.LPWStr)] string szUserName, out uint pchEaten, out IMoniker ppmk);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
Justification = "The calling method is guarded")]
[DllImport(Interop.Libraries.Ole32, PreserveSig = false)]
private static extern void BindMoniker(IMoniker pmk, uint grfOpt, ref Guid iidResult, [MarshalAs(UnmanagedType.Interface)] out object ppvResult);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3680,7 +3680,7 @@ private extern object InvokeDispMethod(
Type[] aArgsTypes,
Type retType)
{
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ FCFuncStart(gInteropMarshalFuncs)
FCFuncElement("OffsetOfHelper", MarshalNative::OffsetOfHelper)

QCFuncElement("InternalPrelink", MarshalNative::Prelink)
QCFuncElement("IsComSupportedInternal", MarshalNative::IsComSupported)
QCFuncElement("IsBuiltInComSupportedInternal", MarshalNative::IsBuiltInComSupported)
FCFuncElement("GetExceptionForHRInternal", MarshalNative::GetExceptionForHR)
FCFuncElement("GetDelegateForFunctionPointerInternal", MarshalNative::GetDelegateForFunctionPointerInternal)
FCFuncElement("GetFunctionPointerForDelegateInternal", MarshalNative::GetFunctionPointerForDelegateInternal)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/eeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ HRESULT EEConfig::sync()
bLogCCWRefCountChange = true;

fEnableRCWCleanupOnSTAShutdown = (CLRConfig::GetConfigValue(CLRConfig::INTERNAL_EnableRCWCleanupOnSTAShutdown) != 0);
m_fBuiltInCOMInteropSupported = Configuration::GetKnobBooleanValue(W("System.Runtime.InteropServices.Marshal.IsComSupported"), true);
m_fBuiltInCOMInteropSupported = Configuration::GetKnobBooleanValue(W("System.Runtime.InteropServices.Marshal.IsBuiltInComSupported"), true);
#endif // FEATURE_COMINTEROP

#ifdef _DEBUG
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/marshalnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ VOID QCALLTYPE MarshalNative::Prelink(MethodDesc * pMD)
END_QCALL;
}

// IsComSupported
// IsBuiltInComSupported
// Built-in COM support is only checked from the native side to ensure the runtime
// is in a consistent state
BOOL QCALLTYPE MarshalNative::IsComSupported()
BOOL QCALLTYPE MarshalNative::IsBuiltInComSupported()
{
QCALL_CONTRACT;

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/marshalnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MarshalNative
{
public:
static VOID QCALLTYPE Prelink(MethodDesc * pMD);
static BOOL QCALLTYPE IsComSupported();
static BOOL QCALLTYPE IsBuiltInComSupported();

//====================================================================
// These methods convert between an HR and and a managed exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
' The .NET Foundation licenses this file to you under the MIT license.

Imports System
Imports System.Diagnostics.CodeAnalysis
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Runtime.Versioning
Expand Down Expand Up @@ -539,6 +540,7 @@ Namespace Microsoft.VisualBasic
End Try
End Function

<RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url:= "https://aka.ms/dotnet-illink/com")>
<SupportedOSPlatform("windows")>
Public Function GetObject(Optional ByVal PathName As String = Nothing, Optional ByVal [Class] As String = Nothing) As Object
'Only works for Com2 objects, not for COM+ objects.
Expand Down
Loading