From b0760dc5094cdd9418944241fe3565c826550029 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 18 Mar 2026 03:13:03 +0000 Subject: [PATCH 1/2] Initial plan From 69f623caf753fd6b5f6d17e9f7f8f3f1eb503ca8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 18 Mar 2026 03:23:20 +0000 Subject: [PATCH 2/2] Partially revert PR #125326: revert ComActivator/LicenseInteropProxy changes The [UnmanagedCallersOnly] approach for LicenseInteropProxy was causing heap corruption (STATUS_HEAP_CORRUPTION) under GC stress scenarios. Revert to the original MethodDescCallSite-based approach. Changes: - ComActivator.cs: Restore public static object Create() and remove the unsafe [UnmanagedCallersOnly] static methods for GetCurrentContextInfo and SaveKeyInCurrentContext - corelib.h: Restore original LICENSE_INTEROP_PROXY method signatures (SM_RetObj, IM_RuntimeTypeHandle_RefBool_RefIntPtr_RetVoid, IM_IntPtr_RetVoid) - runtimecallablewrapper.cpp: Restore MethodDescCallSite calls for license interop - metasig.h: Remove added PtrLicenseInteropProxy_* metasig definitions Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- .../Runtime/InteropServices/ComActivator.cs | 39 ++----------------- src/coreclr/vm/corelib.h | 6 +-- src/coreclr/vm/metasig.h | 2 - src/coreclr/vm/runtimecallablewrapper.cpp | 24 +++++++----- 4 files changed, 21 insertions(+), 50 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs b/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs index 33b82d2fbb07e6..5ac78f35cdcee0 100644 --- a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs +++ b/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs @@ -762,17 +762,10 @@ private static extern bool Contains( [UnsafeAccessorType(LicInfoHelperLicenseContextTypeName)] object? licInfoHelperContext, string assemblyName); - [UnmanagedCallersOnly] - private static unsafe void Create(object* pResult, Exception* pException) + // Helper function to create an object from the native side + public static object Create() { - try - { - *pResult = new LicenseInteropProxy(); - } - catch (Exception ex) - { - *pException = ex; - } + return new LicenseInteropProxy(); } // Determine if the type supports licensing @@ -884,19 +877,6 @@ public void GetCurrentContextInfo(RuntimeTypeHandle rth, out bool isDesignTime, bstrKey = Marshal.StringToBSTR((string)key!); } - [UnmanagedCallersOnly] - private static unsafe void GetCurrentContextInfo(LicenseInteropProxy* pProxy, Type* pType, bool* pIsDesignTime, IntPtr* pBstrKey, Exception* pException) - { - try - { - pProxy->GetCurrentContextInfo(pType->TypeHandle, out *pIsDesignTime, out *pBstrKey); - } - catch (Exception ex) - { - *pException = ex; - } - } - // The CLR invokes this when instantiating a licensed COM // object inside a designtime license context. // It's purpose is to save away the license key that the CLR @@ -912,18 +892,5 @@ public void SaveKeyInCurrentContext(IntPtr bstrKey) SetSavedLicenseKey(_licContext!, _targetRcwType!, key); } - - [UnmanagedCallersOnly] - private static unsafe void SaveKeyInCurrentContext(LicenseInteropProxy* pProxy, IntPtr bstrKey, Exception* pException) - { - try - { - pProxy->SaveKeyInCurrentContext(bstrKey); - } - catch (Exception ex) - { - *pException = ex; - } - } } } diff --git a/src/coreclr/vm/corelib.h b/src/coreclr/vm/corelib.h index 4c865c7aea2b41..bec5d1dc5cee2a 100644 --- a/src/coreclr/vm/corelib.h +++ b/src/coreclr/vm/corelib.h @@ -185,9 +185,9 @@ DEFINE_METHOD(COM_OBJECT, CTOR, .ctor, #endif // FOR_ILLINK DEFINE_CLASS(LICENSE_INTEROP_PROXY, InternalInteropServices, LicenseInteropProxy) -DEFINE_METHOD(LICENSE_INTEROP_PROXY, CREATE, Create, SM_PtrObj_PtrException_RetVoid) -DEFINE_METHOD(LICENSE_INTEROP_PROXY, GETCURRENTCONTEXTINFO, GetCurrentContextInfo, SM_PtrLicenseInteropProxy_PtrType_PtrBool_PtrIntPtr_PtrException_RetVoid) -DEFINE_METHOD(LICENSE_INTEROP_PROXY, SAVEKEYINCURRENTCONTEXT, SaveKeyInCurrentContext, SM_PtrLicenseInteropProxy_IntPtr_PtrException_RetVoid) +DEFINE_METHOD(LICENSE_INTEROP_PROXY, CREATE, Create, SM_RetObj) +DEFINE_METHOD(LICENSE_INTEROP_PROXY, GETCURRENTCONTEXTINFO, GetCurrentContextInfo, IM_RuntimeTypeHandle_RefBool_RefIntPtr_RetVoid) +DEFINE_METHOD(LICENSE_INTEROP_PROXY, SAVEKEYINCURRENTCONTEXT, SaveKeyInCurrentContext, IM_IntPtr_RetVoid) #endif // FEATURE_COMINTEROP END_ILLINK_FEATURE_SWITCH() diff --git a/src/coreclr/vm/metasig.h b/src/coreclr/vm/metasig.h index 1f7c410c9d083b..0ff156f0883adc 100644 --- a/src/coreclr/vm/metasig.h +++ b/src/coreclr/vm/metasig.h @@ -431,8 +431,6 @@ DEFINE_METASIG_T(SM(PtrResolver_Int_PtrStr_PtrException_RetVoid, P(C(RESOLVER)) #ifdef FEATURE_COMINTEROP DEFINE_METASIG_T(SM(PtrClass_PtrStr_Int_PtrObj_PtrArrObj_PtrArrBool_PtrArrInt_PtrArrType_PtrType_PtrObj_PtrException_RetVoid, P(C(CLASS)) P(s) i P(j) P(a(j)) P(a(F)) P(a(i)) P(a(C(TYPE))) P(C(TYPE)) P(j) P(C(EXCEPTION)), v)) DEFINE_METASIG_T(SM(PtrComObject_PtrClass_PtrObj_PtrException_RetVoid, P(C(COM_OBJECT)) P(C(CLASS)) P(j) P(C(EXCEPTION)), v)) -DEFINE_METASIG_T(SM(PtrLicenseInteropProxy_IntPtr_PtrException_RetVoid, P(C(LICENSE_INTEROP_PROXY)) I P(C(EXCEPTION)), v)) -DEFINE_METASIG_T(SM(PtrLicenseInteropProxy_PtrType_PtrBool_PtrIntPtr_PtrException_RetVoid, P(C(LICENSE_INTEROP_PROXY)) P(C(TYPE)) P(F) P(I) P(C(EXCEPTION)), v)) #endif // FEATURE_COMINTEROP DEFINE_METASIG_T(SM(PtrObj_PtrObj_PtrException_RetVoid, P(j) P(j) P(C(EXCEPTION)), v)) DEFINE_METASIG_T(SM(PtrObj_PtrObj_PtrObj_PtrException_RetVoid, P(j) P(j) P(j) P(C(EXCEPTION)), v)) diff --git a/src/coreclr/vm/runtimecallablewrapper.cpp b/src/coreclr/vm/runtimecallablewrapper.cpp index 076a4a043fa7af..db58879480b80a 100644 --- a/src/coreclr/vm/runtimecallablewrapper.cpp +++ b/src/coreclr/vm/runtimecallablewrapper.cpp @@ -137,16 +137,20 @@ IUnknown *ComClassFactory::CreateInstanceFromClassFactory(IClassFactory *pClassF GCPROTECT_BEGIN(gc); // Create an instance of the object - UnmanagedCallersOnlyCaller createObj(METHOD__LICENSE_INTEROP_PROXY__CREATE); - createObj.InvokeThrowing(&gc.pProxy); + MethodDescCallSite createObj(METHOD__LICENSE_INTEROP_PROXY__CREATE); + gc.pProxy = createObj.Call_RetOBJECTREF(NULL); gc.pType = rth.GetManagedClassObject(); // Query the current licensing context - UnmanagedCallersOnlyCaller getCurrentContextInfo(METHOD__LICENSE_INTEROP_PROXY__GETCURRENTCONTEXTINFO); + MethodDescCallSite getCurrentContextInfo(METHOD__LICENSE_INTEROP_PROXY__GETCURRENTCONTEXTINFO, &gc.pProxy); CLR_BOOL fDesignTime = FALSE; - INT_PTR bstrKeyRaw = NULL; - getCurrentContextInfo.InvokeThrowing(&gc.pProxy, &gc.pType, &fDesignTime, &bstrKeyRaw); - bstrKey = (BSTR)bstrKeyRaw; + ARG_SLOT args[4]; + args[0] = ObjToArgSlot(gc.pProxy); + args[1] = ObjToArgSlot(gc.pType); + args[2] = (ARG_SLOT)&fDesignTime; + args[3] = (ARG_SLOT)(BSTR*)&bstrKey; + + getCurrentContextInfo.Call(args); if (fDesignTime) { @@ -177,9 +181,11 @@ IUnknown *ComClassFactory::CreateInstanceFromClassFactory(IClassFactory *pClassF // Store the requested license key if (SUCCEEDED(hr)) { - UnmanagedCallersOnlyCaller saveKeyInCurrentContext(METHOD__LICENSE_INTEROP_PROXY__SAVEKEYINCURRENTCONTEXT); - BSTR bstrKeyValue = (BSTR)bstrKey; - saveKeyInCurrentContext.InvokeThrowing(&gc.pProxy, reinterpret_cast(bstrKeyValue)); + MethodDescCallSite saveKeyInCurrentContext(METHOD__LICENSE_INTEROP_PROXY__SAVEKEYINCURRENTCONTEXT, &gc.pProxy); + + args[0] = ObjToArgSlot(gc.pProxy); + args[1] = (ARG_SLOT)(BSTR)bstrKey; + saveKeyInCurrentContext.Call(args); } }