Skip to content

Commit 68a1b4a

Browse files
Disable ctor calls on classes marked with ComImport when COM interop feature isn't enabled. (#115009)
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
1 parent 2dc9392 commit 68a1b4a

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/coreclr/vm/ecall.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,9 @@ PCODE ECall::GetFCallImpl(MethodDesc * pMD, BOOL * pfSharedOrDynamicFCallImpl /*
336336
return CoreLibBinder::GetMethod(METHOD__DELEGATE__CONSTRUCT_DELEGATE)->GetMultiCallableAddrOfCode();
337337
}
338338

339-
// COM imported classes have special constructors
340-
if (pMT->IsComObjectType()
341339
#ifdef FEATURE_COMINTEROP
342-
&& (g_pBaseCOMObject == NULL || pMT != g_pBaseCOMObject)
343-
#endif // FEATURE_COMINTEROP
344-
)
340+
// COM imported classes have special constructors
341+
if (pMT->IsComObjectType() && pMT != g_pBaseCOMObject)
345342
{
346343
if (pfSharedOrDynamicFCallImpl)
347344
*pfSharedOrDynamicFCallImpl = TRUE;
@@ -352,6 +349,12 @@ PCODE ECall::GetFCallImpl(MethodDesc * pMD, BOOL * pfSharedOrDynamicFCallImpl /*
352349
// FCComCtor does not need to be in the fcall hashtable since it does not erect frame.
353350
return GetEEFuncEntryPoint(FCComCtor);
354351
}
352+
#else // !FEATURE_COMINTEROP
353+
// This code path is taken when a class marked with ComImport is being created.
354+
// If we get here and COM interop isn't suppported, throw.
355+
if (pMT->IsComObjectType())
356+
COMPlusThrow(kPlatformNotSupportedException, IDS_EE_ERROR_COM);
357+
#endif // FEATURE_COMINTEROP
355358

356359
if (!pMD->GetModule()->IsSystem())
357360
COMPlusThrow(kSecurityException, BFA_ECALLS_MUST_BE_IN_SYS_MOD);

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ static GenericHasCctor()
127127
}
128128
}
129129

130+
[ComImport]
131+
[Guid("00000000-0000-0000-0000-000000000000")]
132+
interface ComInterface
133+
{
134+
void Func();
135+
}
136+
137+
// This class is used to test the PrepareMethod API with COM interop on non-Windows platforms.
138+
[ComImport]
139+
[Guid("00000000-0000-0000-0000-000000000000")]
140+
class ComClass : ComInterface
141+
{
142+
[MethodImpl(MethodImplOptions.InternalCall)]
143+
public extern void Func();
144+
}
145+
130146
[Fact]
131147
public static void PrepareMethod()
132148
{
@@ -139,6 +155,16 @@ public static void PrepareMethod()
139155
{
140156
Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(IList).GetMethod("Add").MethodHandle));
141157
}
158+
159+
try
160+
{
161+
// This is expected to either succeed or throw PlatformNotSupportedException depending on the platform
162+
// and runtime flavor
163+
RuntimeHelpers.PrepareMethod(typeof(ComClass).GetMethod("Func").MethodHandle);
164+
}
165+
catch (PlatformNotSupportedException)
166+
{
167+
}
142168
}
143169

144170
[Fact]

0 commit comments

Comments
 (0)