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 warnings #52176

Merged
merged 5 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docs/workflow/trimming/feature-switches.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ configurations but their defaults might vary as any SDK can set the defaults dif
| 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. |
| BuiltInComInteropSupport | System.Runtime.InteropServices.BuiltInComInterop.IsSupported | Built-in COM 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.BuiltInComInterop.IsSupported" featurevalue="false">
<method signature="System.Boolean get_IsBuiltInComSupported()" body="stub" value="false" />
</type>
</assembly>
</linker>
</linker>
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.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace Internal.Runtime.InteropServices
Expand Down Expand Up @@ -39,6 +40,7 @@ public partial struct ComActivationContext
[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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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 @@ -62,7 +63,7 @@ public partial struct ComActivationContext
[CLSCompliant(false)]
public static unsafe ComActivationContext Create(ref ComActivationContextInternal cxtInt)
{
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -92,7 +93,7 @@ public static class ComActivator
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
public static object GetClassFactoryForType(ComActivationContext cxt)
{
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -126,7 +127,7 @@ public static object GetClassFactoryForType(ComActivationContext cxt)
[RequiresUnreferencedCode("Built-in COM support is not trim compatible", Url = "https://aka.ms/dotnet-illink/com")]
public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bool register)
{
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -222,7 +223,7 @@ public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bo
[UnmanagedCallersOnly]
public static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
{
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -265,7 +266,7 @@ public static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInte
[UnmanagedCallersOnly]
public static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
{
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -311,7 +312,7 @@ public static unsafe int RegisterClassForTypeInternal(ComActivationContextIntern
[UnmanagedCallersOnly]
public static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
{
if (!Marshal.IsComSupported)
if (!Marshal.IsBuiltInComSupported)
{
throw new NotSupportedException(SR.NotSupported_COM);
}
Expand Down Expand Up @@ -364,6 +365,7 @@ private static void Log(string fmt, params object[] args)
Debug.WriteLine(fmt, args);
}

[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)
{
try
Expand Down Expand Up @@ -489,6 +491,7 @@ public static object CreateAggregatedObject(object pUnkOuter, object comObject)
}
}

[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 @@ -524,6 +527,7 @@ public LicenseClassFactory(Guid clsid, Type classType)
_classType = classType;
}

[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,7 @@ private static object PtrToStructureHelper(IntPtr ptr, Type structureType)
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool IsPinnable(object? obj);

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

private static bool InitializeIsComSupported() => IsComSupportedInternal();
internal static bool IsBuiltInComSupported { get; } = IsBuiltInComSupportedInternal();

#if TARGET_WINDOWS
/// <summary>
Expand Down Expand Up @@ -296,7 +294,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 +441,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 +455,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 +482,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 +510,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 +534,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 +565,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 +595,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 +650,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 +670,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 +684,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 +695,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 +709,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 +720,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 +734,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 +762,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 +779,19 @@ public static object BindToMoniker(string monikerName)
return obj;
}

// Revist after https://github.com/mono/linker/issues/1989 is fixed
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
Justification = "The calling method is annotated with RequiresUnreferencedCode")]
[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 annotated with RequiresUnreferencedCode")]
[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 annotated with RequiresUnreferencedCode")]
[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.BuiltInComInterop.IsSupported"), 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
Loading