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
Show file tree
Hide file tree
Changes from 4 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
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ static GenericHasCctor()
}
}

// 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
{
[MethodImpl(MethodImplOptions.InternalCall)]
public extern void Func();
}

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

if (PlatformDetection.IsNotWindows)
{
Assert.Throws<PlatformNotSupportedException>(() => RuntimeHelpers.PrepareMethod(typeof(ComClass).GetMethod("Func").MethodHandle));
}
}

[Fact]
Expand Down
Loading