Skip to content

Commit 6854225

Browse files
authored
Com trimming related work (dotnet#52940)
* COM related work and native host name changes * FB
1 parent 8bf306c commit 6854225

File tree

8 files changed

+110
-18
lines changed

8 files changed

+110
-18
lines changed

docs/workflow/trimming/feature-switches.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ configurations but their defaults might vary as any SDK can set the defaults dif
2121
| 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. |
2222
| EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization | System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization | BinaryFormatter serialization support is trimmed when set to false. |
2323
| BuiltInComInteropSupport | System.Runtime.InteropServices.BuiltInComInterop.IsSupported | Built-in COM support is trimmed when set to false. |
24-
| EnableCPlusPlusCLIHostActivation | Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported | C++/CLI host activation code is disabled when set to false and related functionality can be trimmed. |
25-
| _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. |
24+
| EnableCppCLIHostActivation | System.Runtime.InteropServices.EnableCppCLIHostActivation | C++/CLI host activation code is disabled when set to false and related functionality can be trimmed. |
25+
| _EnableConsumingManagedCodeFromNativeHosting | System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting | Getting a managed function from native hosting is disabled when set to false and related functionality can be trimmed. |
2626

2727
Any feature-switch which defines property can be set in csproj file or
2828
on the command line as any other MSBuild property. Those without predefined property name

src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<type fullname="System.StartupHookProvider" feature="System.StartupHookProvider.IsSupported" featurevalue="false">
44
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
55
</type>
6-
<type fullname="Internal.Runtime.InteropServices.ComponentActivator" feature="Internal.Runtime.InteropServices.ComponentActivator.IsSupported" featurevalue="false">
6+
<type fullname="Internal.Runtime.InteropServices.ComponentActivator" feature="System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting" featurevalue="false">
77
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
88
</type>
9-
<type fullname="Internal.Runtime.InteropServices.InMemoryAssemblyLoader" feature="Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported" featurevalue="false">
9+
<type fullname="Internal.Runtime.InteropServices.InMemoryAssemblyLoader" feature="System.Runtime.InteropServices.EnableCppCLIHostActivation" featurevalue="false">
1010
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
1111
</type>
1212
<type fullname="System.Runtime.InteropServices.Marshal" feature="System.Runtime.InteropServices.BuiltInComInterop.IsSupported" featurevalue="false">

src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static class ComponentActivator
2323

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

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

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

src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static class InMemoryAssemblyLoader
1414
{
1515
private static bool IsSupported { get; } = InitializeIsSupported();
1616

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

1919
/// <summary>
2020
/// Loads into an isolated AssemblyLoadContext an assembly that has already been loaded into memory by the OS loader as a native module.

src/coreclr/vm/corelib.h

+31-8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@
6868
#define DEFINE_FIELD_U(stringName, unmanagedContainingType, unmanagedOffset)
6969
#endif
7070

71+
//
72+
// BEGIN_ILLINK_FEATURE_SWITCH and END_ILLINK_FEATURE_SWITCH allow IL linker to guard types behind a feature switch.
73+
// Current support is only around class scope and not for standalone members of classes.
74+
// See usage in this file itself and on the link (the assembly name for feature switch in this file will be System.Private.CoreLib),
75+
// https://github.com/dotnet/designs/blob/main/accepted/2020/feature-switch.md#generate-the-right-input-for-the-linker-in-sdk
76+
//
77+
#ifndef BEGIN_ILLINK_FEATURE_SWITCH
78+
#define BEGIN_ILLINK_FEATURE_SWITCH(featureName, featureValue, featureDefault)
79+
#endif
80+
81+
#ifndef END_ILLINK_FEATURE_SWITCH
82+
#define END_ILLINK_FEATURE_SWITCH()
83+
#endif
84+
85+
7186
// NOTE: Make this window really wide if you want to read the table...
7287

7388
DEFINE_CLASS(ACTIVATOR, System, Activator)
@@ -176,6 +191,7 @@ DEFINE_METHOD(CLASS, GET_PROPERTY_INFO, GetPropertyInfo,
176191
DEFINE_METHOD(CLASS, FORWARD_CALL_TO_INVOKE, ForwardCallToInvokeMember, IM_Str_BindingFlags_Obj_ArrObj_ArrBool_ArrInt_ArrType_Type_RetObj)
177192
#endif // FEATURE_COMINTEROP
178193

194+
BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
179195
#ifdef FEATURE_COMINTEROP
180196
DEFINE_CLASS(BSTR_WRAPPER, Interop, BStrWrapper)
181197
DEFINE_CLASS(CURRENCY_WRAPPER, Interop, CurrencyWrapper)
@@ -184,7 +200,9 @@ DEFINE_CLASS(ERROR_WRAPPER, Interop, ErrorWrapper)
184200
DEFINE_CLASS(UNKNOWN_WRAPPER, Interop, UnknownWrapper)
185201
DEFINE_CLASS(VARIANT_WRAPPER, Interop, VariantWrapper)
186202
#endif // FEATURE_COMINTEROP
203+
END_ILLINK_FEATURE_SWITCH()
187204

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

203221
#endif // FEATURE_COMINTEROP
222+
END_ILLINK_FEATURE_SWITCH()
204223

205224
DEFINE_CLASS_U(Interop, CriticalHandle, CriticalHandle)
206225
DEFINE_FIELD_U(handle, CriticalHandle, m_handle)
@@ -285,10 +304,6 @@ DEFINE_METHOD(CURRENCY, DECIMAL_CTOR, .ctor,
285304
DEFINE_CLASS(DATE_TIME, System, DateTime)
286305
DEFINE_METHOD(DATE_TIME, LONG_CTOR, .ctor, IM_Long_RetVoid)
287306

288-
#ifdef FEATURE_COMINTEROP
289-
DEFINE_CLASS(DATE_TIME_OFFSET, System, DateTimeOffset)
290-
#endif // FEATURE_COMINTEROP
291-
292307
DEFINE_CLASS(DECIMAL, System, Decimal)
293308
DEFINE_METHOD(DECIMAL, CURRENCY_CTOR, .ctor, IM_Currency_RetVoid)
294309

@@ -402,6 +417,7 @@ DEFINE_METHOD(FIELD_INFO, GET_VALUE, GetValue,
402417

403418
DEFINE_CLASS(GUID, System, Guid)
404419

420+
BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
405421
#ifdef FEATURE_COMINTEROP
406422
DEFINE_CLASS(VARIANT, System, Variant)
407423
DEFINE_METHOD(VARIANT, CONVERT_OBJECT_TO_VARIANT,MarshalHelperConvertObjectToVariant,SM_Obj_RefVariant_RetVoid)
@@ -413,6 +429,7 @@ DEFINE_FIELD_U(_objref, VariantData, m_objref)
413429
DEFINE_FIELD_U(_data, VariantData, m_data)
414430
DEFINE_FIELD_U(_flags, VariantData, m_flags)
415431
#endif // FEATURE_COMINTEROP
432+
END_ILLINK_FEATURE_SWITCH()
416433

417434
DEFINE_CLASS(IASYNCRESULT, System, IAsyncResult)
418435

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

451+
BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
434452
#ifdef FEATURE_COMINTEROP
435453
DEFINE_CLASS(ICUSTOM_QUERYINTERFACE, Interop, ICustomQueryInterface)
436454
DEFINE_METHOD(ICUSTOM_QUERYINTERFACE, GET_INTERFACE, GetInterface, IM_RefGuid_OutIntPtr_RetCustomQueryInterfaceResult)
437455
DEFINE_CLASS(CUSTOMQUERYINTERFACERESULT, Interop, CustomQueryInterfaceResult)
438456
#endif //FEATURE_COMINTEROP
457+
END_ILLINK_FEATURE_SWITCH()
439458

440459
#ifdef FEATURE_COMWRAPPERS
441460
DEFINE_CLASS(COMWRAPPERS, Interop, ComWrappers)
@@ -467,11 +486,11 @@ DEFINE_METHOD(IREFLECT, GET_FIELDS, GetFields,
467486
DEFINE_METHOD(IREFLECT, GET_METHODS, GetMethods, IM_BindingFlags_RetArrMethodInfo)
468487
DEFINE_METHOD(IREFLECT, INVOKE_MEMBER, InvokeMember, IM_Str_BindingFlags_Binder_Obj_ArrObj_ArrParameterModifier_CultureInfo_ArrStr_RetObj)
469488

470-
489+
BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
471490
#ifdef FEATURE_COMINTEROP
472491
DEFINE_CLASS(LCID_CONVERSION_TYPE, Interop, LCIDConversionAttribute)
473492
#endif // FEATURE_COMINTEROP
474-
493+
END_ILLINK_FEATURE_SWITCH()
475494

476495
DEFINE_CLASS(MARSHAL, Interop, Marshal)
477496
#ifdef FEATURE_COMINTEROP
@@ -618,10 +637,11 @@ DEFINE_METHOD(SINGLE, GET_HASH_CODE, GetHashCode, IM_RetI
618637

619638
DEFINE_CLASS(__CANON, System, __Canon)
620639

621-
640+
BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
622641
#ifdef FEATURE_COMINTEROP
623642
DEFINE_CLASS(OLE_AUT_BINDER, System, OleAutBinder)
624643
#endif // FEATURE_COMINTEROP
644+
END_ILLINK_FEATURE_SWITCH()
625645

626646
DEFINE_CLASS(MONITOR, Threading, Monitor)
627647
DEFINE_METHOD(MONITOR, ENTER, Enter, SM_Obj_RetVoid)
@@ -1060,6 +1080,7 @@ DEFINE_METHOD(ANSIBSTRMARSHALER, CONVERT_TO_NATIVE, ConvertToNative,
10601080
DEFINE_METHOD(ANSIBSTRMARSHALER, CONVERT_TO_MANAGED, ConvertToManaged, SM_IntPtr_RetStr)
10611081
DEFINE_METHOD(ANSIBSTRMARSHALER, CLEAR_NATIVE, ClearNative, SM_IntPtr_RetVoid)
10621082

1083+
BEGIN_ILLINK_FEATURE_SWITCH(System.Runtime.InteropServices.BuiltInComInterop.IsSupported, true, true)
10631084
#ifdef FEATURE_COMINTEROP
10641085
DEFINE_CLASS(OBJECTMARSHALER, StubHelpers, ObjectMarshaler)
10651086
DEFINE_METHOD(OBJECTMARSHALER, CONVERT_TO_NATIVE, ConvertToNative, SM_ObjIntPtr_RetVoid)
@@ -1080,6 +1101,7 @@ DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CONVERT_SPACE_TO_MANAGED, ConvertSpa
10801101
DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CONVERT_CONTENTS_TO_MANAGED, ConvertContentsToManaged, SM_IntPtr_RefObj_IntPtr_RetVoid)
10811102
DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CLEAR_NATIVE, ClearNative, SM_IntPtr_RefObj_IntPtr_RetVoid)
10821103
#endif // FEATURE_COMINTEROP
1104+
END_ILLINK_FEATURE_SWITCH()
10831105

10841106
DEFINE_CLASS(DATEMARSHALER, StubHelpers, DateMarshaler)
10851107
DEFINE_METHOD(DATEMARSHALER, CONVERT_TO_NATIVE, ConvertToNative, SM_DateTime_RetDbl)
@@ -1274,9 +1296,10 @@ DEFINE_FIELD_U(_generationInfo2, GCMemoryInfoData, generationInfo2)
12741296
DEFINE_FIELD_U(_generationInfo3, GCMemoryInfoData, generationInfo3)
12751297
DEFINE_FIELD_U(_generationInfo4, GCMemoryInfoData, generationInfo4)
12761298

1277-
12781299
#undef DEFINE_CLASS
12791300
#undef DEFINE_METHOD
12801301
#undef DEFINE_FIELD
12811302
#undef DEFINE_CLASS_U
12821303
#undef DEFINE_FIELD_U
1304+
#undef BEGIN_ILLINK_FEATURE_SWITCH
1305+
#undef END_ILLINK_FEATURE_SWITCH
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Program.cs" />
8+
<Compile Include="../Common.cs" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<ProjectReference Include="../MockReferenceTrackerRuntime/CMakeLists.txt" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.BuiltInComInterop.IsSupported" Value="false" />
15+
</ItemGroup>
16+
17+
</Project>

src/tests/Interop/COM/ComWrappers/GlobalInstance/GlobalInstance.Marshalling.cs

+23-4
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,37 @@ static int Main(string[] doNotUse)
2222
{
2323
try
2424
{
25+
bool builtInComDisabled=false;
26+
var comConfig = AppContext.GetData("System.Runtime.InteropServices.BuiltInComInterop.IsSupported");
27+
if(comConfig != null && !bool.Parse(comConfig.ToString()))
28+
{
29+
builtInComDisabled=true;
30+
}
31+
Console.WriteLine($"Built-in COM Disabled?: {builtInComDisabled}");
32+
33+
2534
// The first test registers a global ComWrappers instance for marshalling
2635
// Subsequents tests assume the global instance has already been registered.
2736
ValidateRegisterForMarshalling();
2837

2938
ValidateMarshalAPIs(validateUseRegistered: true);
30-
ValidateMarshalAPIs(validateUseRegistered: false);
39+
if(!builtInComDisabled)
40+
{
41+
ValidateMarshalAPIs(validateUseRegistered: false);
42+
}
3143

3244
ValidatePInvokes(validateUseRegistered: true);
33-
ValidatePInvokes(validateUseRegistered: false);
45+
if(!builtInComDisabled)
46+
{
47+
ValidatePInvokes(validateUseRegistered: false);
48+
}
3449

35-
ValidateComActivation(validateUseRegistered: true);
36-
ValidateComActivation(validateUseRegistered: false);
50+
if(!builtInComDisabled)
51+
{
52+
// This calls ValidateNativeServerActivation which calls Marshal.GetTypeFromCLSID that is not supported
53+
ValidateComActivation(validateUseRegistered: true);
54+
ValidateComActivation(validateUseRegistered: false);
55+
}
3756

3857
ValidateNotRegisteredForTrackerSupport();
3958

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<ApplicationManifest>App.manifest</ApplicationManifest>
5+
<UseManagedCOMServer>true</UseManagedCOMServer>
6+
<IsManagedCOMClient>true</IsManagedCOMClient>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<Compile Include="GlobalInstance.cs" />
11+
<Compile Include="GlobalInstance.Marshalling.cs" />
12+
<Compile Include="../Common.cs" />
13+
<Compile Include="../../ServerContracts/Server.Contracts.cs" />
14+
<Compile Include="../../ServerContracts/ServerGuids.cs" />
15+
</ItemGroup>
16+
<ItemGroup>
17+
<ProjectReference Include="../MockReferenceTrackerRuntime/CMakeLists.txt" />
18+
<ProjectReference Include="../../NativeServer/CMakeLists.txt" />
19+
<ProjectReference Include="../../NETServer/NETServer.csproj">
20+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
21+
<OutputItemType>Content</OutputItemType>
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</ProjectReference>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<None Include="CoreShim.X.manifest">
27+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
28+
</None>
29+
</ItemGroup>
30+
<ItemGroup>
31+
<RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.BuiltInComInterop.IsSupported" Value="false" />
32+
</ItemGroup>
33+
</Project>

0 commit comments

Comments
 (0)