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

Com trimming related work #52940

Merged
merged 2 commits into from
May 21, 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
4 changes: 2 additions & 2 deletions docs/workflow/trimming/feature-switches.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ configurations but their defaults might vary as any SDK can set the defaults dif
| 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. |
| EnableCPlusPlusCLIHostActivation | Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported | C++/CLI host activation code is disabled when set to false and related functionality can be trimmed. |
| _EnableCallingManagedFunctionFromNativeHosting | Internal.Runtime.InteropServices.ComponentActivator.IsSupported | Getting a managed function from native hosting is disabled when set to false and related functionality can be trimmed. |
| EnableCppCLIHostActivation | System.Runtime.InteropServices.EnableCppCLIHostActivation | C++/CLI host activation code is disabled when set to false and related functionality can be trimmed. |
| _EnableConsumingManagedCodeFromNativeHosting | System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting | Getting a managed function from native hosting is disabled when set to false and related functionality can be trimmed. |

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,10 +3,10 @@
<type fullname="System.StartupHookProvider" feature="System.StartupHookProvider.IsSupported" featurevalue="false">
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
</type>
<type fullname="Internal.Runtime.InteropServices.ComponentActivator" feature="Internal.Runtime.InteropServices.ComponentActivator.IsSupported" featurevalue="false">
<type fullname="Internal.Runtime.InteropServices.ComponentActivator" feature="System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting" featurevalue="false">
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
</type>
<type fullname="Internal.Runtime.InteropServices.InMemoryAssemblyLoader" feature="Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported" featurevalue="false">
<type fullname="Internal.Runtime.InteropServices.InMemoryAssemblyLoader" feature="System.Runtime.InteropServices.EnableCppCLIHostActivation" 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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class ComponentActivator

private static bool IsSupported { get; } = InitializeIsSupported();

private static bool InitializeIsSupported() => AppContext.TryGetSwitch("Internal.Runtime.InteropServices.ComponentActivator.IsSupported", out bool isSupported) ? isSupported : true;
private static bool InitializeIsSupported() => AppContext.TryGetSwitch("System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting", out bool isSupported) ? isSupported : true;

public delegate int ComponentEntryPoint(IntPtr args, int sizeBytes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class InMemoryAssemblyLoader
{
private static bool IsSupported { get; } = InitializeIsSupported();

private static bool InitializeIsSupported() => AppContext.TryGetSwitch("Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported", out bool isSupported) ? isSupported : true;
private static bool InitializeIsSupported() => AppContext.TryGetSwitch("System.Runtime.InteropServices.EnableCppCLIHostActivation", out bool isSupported) ? isSupported : true;

/// <summary>
/// Loads into an isolated AssemblyLoadContext an assembly that has already been loaded into memory by the OS loader as a native module.
Expand Down
39 changes: 31 additions & 8 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@
#define DEFINE_FIELD_U(stringName, unmanagedContainingType, unmanagedOffset)
#endif

//
// BEGIN_ILLINK_FEATURE_SWITCH and END_ILLINK_FEATURE_SWITCH allow IL linker to guard types behind a feature switch.
// Current support is only around class scope and not for standalone members of classes.
// See usage in this file itself and on the link (the assembly name for feature switch in this file will be System.Private.CoreLib),
// https://github.com/dotnet/designs/blob/main/accepted/2020/feature-switch.md#generate-the-right-input-for-the-linker-in-sdk
//
#ifndef BEGIN_ILLINK_FEATURE_SWITCH
#define BEGIN_ILLINK_FEATURE_SWITCH(featureName, featureValue, featureDefault)
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
#endif

#ifndef END_ILLINK_FEATURE_SWITCH
#define END_ILLINK_FEATURE_SWITCH()
#endif


// NOTE: Make this window really wide if you want to read the table...

DEFINE_CLASS(ACTIVATOR, System, Activator)
Expand Down Expand Up @@ -176,6 +191,7 @@ DEFINE_METHOD(CLASS, GET_PROPERTY_INFO, GetPropertyInfo,
DEFINE_METHOD(CLASS, FORWARD_CALL_TO_INVOKE, ForwardCallToInvokeMember, IM_Str_BindingFlags_Obj_ArrObj_ArrBool_ArrInt_ArrType_Type_RetObj)
#endif // FEATURE_COMINTEROP

BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
#ifdef FEATURE_COMINTEROP
DEFINE_CLASS(BSTR_WRAPPER, Interop, BStrWrapper)
DEFINE_CLASS(CURRENCY_WRAPPER, Interop, CurrencyWrapper)
Expand All @@ -184,7 +200,9 @@ DEFINE_CLASS(ERROR_WRAPPER, Interop, ErrorWrapper)
DEFINE_CLASS(UNKNOWN_WRAPPER, Interop, UnknownWrapper)
DEFINE_CLASS(VARIANT_WRAPPER, Interop, VariantWrapper)
#endif // FEATURE_COMINTEROP
END_ILLINK_FEATURE_SWITCH()

BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
#ifdef FEATURE_COMINTEROP
DEFINE_CLASS_U(System, __ComObject, ComObject)
DEFINE_FIELD_U(m_ObjectToDataMap, ComObject, m_ObjectToDataMap)
Expand All @@ -201,6 +219,7 @@ DEFINE_METHOD(LICENSE_INTEROP_PROXY, GETCURRENTCONTEXTINFO, GetCurrentContextI
DEFINE_METHOD(LICENSE_INTEROP_PROXY, SAVEKEYINCURRENTCONTEXT, SaveKeyInCurrentContext, IM_IntPtr_RetVoid)

#endif // FEATURE_COMINTEROP
END_ILLINK_FEATURE_SWITCH()

DEFINE_CLASS_U(Interop, CriticalHandle, CriticalHandle)
DEFINE_FIELD_U(handle, CriticalHandle, m_handle)
Expand Down Expand Up @@ -285,10 +304,6 @@ DEFINE_METHOD(CURRENCY, DECIMAL_CTOR, .ctor,
DEFINE_CLASS(DATE_TIME, System, DateTime)
DEFINE_METHOD(DATE_TIME, LONG_CTOR, .ctor, IM_Long_RetVoid)

#ifdef FEATURE_COMINTEROP
DEFINE_CLASS(DATE_TIME_OFFSET, System, DateTimeOffset)
#endif // FEATURE_COMINTEROP

DEFINE_CLASS(DECIMAL, System, Decimal)
DEFINE_METHOD(DECIMAL, CURRENCY_CTOR, .ctor, IM_Currency_RetVoid)

Expand Down Expand Up @@ -402,6 +417,7 @@ DEFINE_METHOD(FIELD_INFO, GET_VALUE, GetValue,

DEFINE_CLASS(GUID, System, Guid)

BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
#ifdef FEATURE_COMINTEROP
DEFINE_CLASS(VARIANT, System, Variant)
DEFINE_METHOD(VARIANT, CONVERT_OBJECT_TO_VARIANT,MarshalHelperConvertObjectToVariant,SM_Obj_RefVariant_RetVoid)
Expand All @@ -413,6 +429,7 @@ DEFINE_FIELD_U(_objref, VariantData, m_objref)
DEFINE_FIELD_U(_data, VariantData, m_data)
DEFINE_FIELD_U(_flags, VariantData, m_flags)
#endif // FEATURE_COMINTEROP
END_ILLINK_FEATURE_SWITCH()

DEFINE_CLASS(IASYNCRESULT, System, IAsyncResult)

Expand All @@ -431,11 +448,13 @@ DEFINE_CLASS(DYNAMICINTERFACECASTABLEHELPERS, Interop, DynamicInterfaceCasta
DEFINE_METHOD(DYNAMICINTERFACECASTABLEHELPERS, IS_INTERFACE_IMPLEMENTED, IsInterfaceImplemented, SM_IDynamicInterfaceCastable_RuntimeType_Bool_RetBool)
DEFINE_METHOD(DYNAMICINTERFACECASTABLEHELPERS, GET_INTERFACE_IMPLEMENTATION, GetInterfaceImplementation, SM_IDynamicInterfaceCastable_RuntimeType_RetRtType)

BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
#ifdef FEATURE_COMINTEROP
DEFINE_CLASS(ICUSTOM_QUERYINTERFACE, Interop, ICustomQueryInterface)
DEFINE_METHOD(ICUSTOM_QUERYINTERFACE, GET_INTERFACE, GetInterface, IM_RefGuid_OutIntPtr_RetCustomQueryInterfaceResult)
DEFINE_CLASS(CUSTOMQUERYINTERFACERESULT, Interop, CustomQueryInterfaceResult)
#endif //FEATURE_COMINTEROP
END_ILLINK_FEATURE_SWITCH()

#ifdef FEATURE_COMWRAPPERS
DEFINE_CLASS(COMWRAPPERS, Interop, ComWrappers)
Expand Down Expand Up @@ -467,11 +486,11 @@ DEFINE_METHOD(IREFLECT, GET_FIELDS, GetFields,
DEFINE_METHOD(IREFLECT, GET_METHODS, GetMethods, IM_BindingFlags_RetArrMethodInfo)
DEFINE_METHOD(IREFLECT, INVOKE_MEMBER, InvokeMember, IM_Str_BindingFlags_Binder_Obj_ArrObj_ArrParameterModifier_CultureInfo_ArrStr_RetObj)


BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
#ifdef FEATURE_COMINTEROP
DEFINE_CLASS(LCID_CONVERSION_TYPE, Interop, LCIDConversionAttribute)
#endif // FEATURE_COMINTEROP

END_ILLINK_FEATURE_SWITCH()

DEFINE_CLASS(MARSHAL, Interop, Marshal)
#ifdef FEATURE_COMINTEROP
Expand Down Expand Up @@ -618,10 +637,11 @@ DEFINE_METHOD(SINGLE, GET_HASH_CODE, GetHashCode, IM_RetI

DEFINE_CLASS(__CANON, System, __Canon)


BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
#ifdef FEATURE_COMINTEROP
DEFINE_CLASS(OLE_AUT_BINDER, System, OleAutBinder)
#endif // FEATURE_COMINTEROP
END_ILLINK_FEATURE_SWITCH()

DEFINE_CLASS(MONITOR, Threading, Monitor)
DEFINE_METHOD(MONITOR, ENTER, Enter, SM_Obj_RetVoid)
Expand Down Expand Up @@ -1060,6 +1080,7 @@ DEFINE_METHOD(ANSIBSTRMARSHALER, CONVERT_TO_NATIVE, ConvertToNative,
DEFINE_METHOD(ANSIBSTRMARSHALER, CONVERT_TO_MANAGED, ConvertToManaged, SM_IntPtr_RetStr)
DEFINE_METHOD(ANSIBSTRMARSHALER, CLEAR_NATIVE, ClearNative, SM_IntPtr_RetVoid)

BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
#ifdef FEATURE_COMINTEROP
DEFINE_CLASS(OBJECTMARSHALER, StubHelpers, ObjectMarshaler)
DEFINE_METHOD(OBJECTMARSHALER, CONVERT_TO_NATIVE, ConvertToNative, SM_ObjIntPtr_RetVoid)
Expand All @@ -1080,6 +1101,7 @@ DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CONVERT_SPACE_TO_MANAGED, ConvertSpa
DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CONVERT_CONTENTS_TO_MANAGED, ConvertContentsToManaged, SM_IntPtr_RefObj_IntPtr_RetVoid)
DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CLEAR_NATIVE, ClearNative, SM_IntPtr_RefObj_IntPtr_RetVoid)
#endif // FEATURE_COMINTEROP
END_ILLINK_FEATURE_SWITCH()

DEFINE_CLASS(DATEMARSHALER, StubHelpers, DateMarshaler)
DEFINE_METHOD(DATEMARSHALER, CONVERT_TO_NATIVE, ConvertToNative, SM_DateTime_RetDbl)
Expand Down Expand Up @@ -1274,9 +1296,10 @@ DEFINE_FIELD_U(_generationInfo2, GCMemoryInfoData, generationInfo2)
DEFINE_FIELD_U(_generationInfo3, GCMemoryInfoData, generationInfo3)
DEFINE_FIELD_U(_generationInfo4, GCMemoryInfoData, generationInfo4)


#undef DEFINE_CLASS
#undef DEFINE_METHOD
#undef DEFINE_FIELD
#undef DEFINE_CLASS_U
#undef DEFINE_FIELD_U
#undef BEGIN_ILLINK_FEATURE_SWITCH
#undef END_ILLINK_FEATURE_SWITCH
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="../Common.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../MockReferenceTrackerRuntime/CMakeLists.txt" />
</ItemGroup>
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.BuiltInComInterop.IsSupported" Value="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,37 @@ static int Main(string[] doNotUse)
{
try
{
bool builtInComDisabled=false;
var comConfig = AppContext.GetData("System.Runtime.InteropServices.BuiltInComInterop.IsSupported");
if(comConfig != null && !bool.Parse(comConfig.ToString()))
{
builtInComDisabled=true;
}
Console.WriteLine($"Built-in COM Disabled?: {builtInComDisabled}");


// The first test registers a global ComWrappers instance for marshalling
// Subsequents tests assume the global instance has already been registered.
ValidateRegisterForMarshalling();

ValidateMarshalAPIs(validateUseRegistered: true);
ValidateMarshalAPIs(validateUseRegistered: false);
if(!builtInComDisabled)
{
ValidateMarshalAPIs(validateUseRegistered: false);
}

ValidatePInvokes(validateUseRegistered: true);
ValidatePInvokes(validateUseRegistered: false);
if(!builtInComDisabled)
{
ValidatePInvokes(validateUseRegistered: false);
}

ValidateComActivation(validateUseRegistered: true);
ValidateComActivation(validateUseRegistered: false);
if(!builtInComDisabled)
{
// This calls ValidateNativeServerActivation which calls Marshal.GetTypeFromCLSID that is not supported
ValidateComActivation(validateUseRegistered: true);
ValidateComActivation(validateUseRegistered: false);
}

ValidateNotRegisteredForTrackerSupport();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<ApplicationManifest>App.manifest</ApplicationManifest>
<UseManagedCOMServer>true</UseManagedCOMServer>
<IsManagedCOMClient>true</IsManagedCOMClient>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="GlobalInstance.cs" />
<Compile Include="GlobalInstance.Marshalling.cs" />
<Compile Include="../Common.cs" />
<Compile Include="../../ServerContracts/Server.Contracts.cs" />
<Compile Include="../../ServerContracts/ServerGuids.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../MockReferenceTrackerRuntime/CMakeLists.txt" />
<ProjectReference Include="../../NativeServer/CMakeLists.txt" />
<ProjectReference Include="../../NETServer/NETServer.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Content</OutputItemType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="CoreShim.X.manifest">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.BuiltInComInterop.IsSupported" Value="false" />
</ItemGroup>
</Project>