Skip to content

Disable ctor calls on classes marked with ComImport when COM interop feature isn't enabled. #115009

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

Merged
merged 6 commits into from
Apr 26, 2025
Merged
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
13 changes: 8 additions & 5 deletions src/coreclr/vm/ecall.cpp
Original file line number Diff line number Diff line change
@@ -336,12 +336,9 @@ PCODE ECall::GetFCallImpl(MethodDesc * pMD, BOOL * pfSharedOrDynamicFCallImpl /*
return CoreLibBinder::GetMethod(METHOD__DELEGATE__CONSTRUCT_DELEGATE)->GetMultiCallableAddrOfCode();
}

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

if (!pMD->GetModule()->IsSystem())
COMPlusThrow(kSecurityException, BFA_ECALLS_MUST_BE_IN_SYS_MOD);
Original file line number Diff line number Diff line change
@@ -127,6 +127,22 @@ static GenericHasCctor()
}
}

[ComImport]
[Guid("00000000-0000-0000-0000-000000000000")]
interface ComInterface
{
void Func();
}

// This class is used to test the PrepareMethod API with COM interop on non-Windows platforms.
[ComImport]
[Guid("00000000-0000-0000-0000-000000000000")]
class ComClass : ComInterface
{
[MethodImpl(MethodImplOptions.InternalCall)]
public extern void Func();
}

[Fact]
public static void PrepareMethod()
{
@@ -139,6 +155,16 @@ public static void PrepareMethod()
{
Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(IList).GetMethod("Add").MethodHandle));
}

try
{
// This is expected to either succeed or throw PlatformNotSupportedException depending on the platform
// and runtime flavor
RuntimeHelpers.PrepareMethod(typeof(ComClass).GetMethod("Func").MethodHandle);
}
catch (PlatformNotSupportedException)
{
}
}

[Fact]
Loading
Oops, something went wrong.