Repository navigation
Closed
Labels
Milestone
Description
This issue tracks the work to replace MethodDescCallSite / CallDescrWorker infrastructure with more efficient UnmanagedCallersOnly reverse P/Invoke calls for invoking managed code from native code.
Benefits
- Significantly reduced overhead for VM-to-managed calls
- Uses existing reverse P/Invoke infrastructure instead of the expensive
CallDescrWorkermechanism - Better alignment with modern interop patterns
Pattern
The conversion pattern involves:
- Adding
[UnmanagedCallersOnly]wrappers to managed methods with anException*out-parameter - Using the
UnmanagedCallersOnlyCallertemplate class in C++ to invoke these methods - Updating
metasig.handcorelib.hwith appropriate signatures
Notes
- The
UnmanagedCallersOnlyCallertemplate class is defined incallhelpers.hand can be used to invoke the target. - WASM builds require regenerating
callhelpers-reverse.cppwhen adding new UCO methods.
Example PR to follow: #123832
Conversion Tasks
Priority 1: Cross-Platform, Simple Conversions
These are straightforward conversions on code paths that run on all platforms.
AppDomain / Assembly Loading (appdomain.cpp) - #123967
-
METHOD__ASSEMBLYLOADCONTEXT__ON_ASSEMBLY_LOADinAppDomain::RaiseAssemblyLoadEvent -
METHOD__ASSEMBLYLOADCONTEXT__ON_TYPE_RESOLVEinAppDomain::RaiseTypeResolveEventThrowing -
METHOD__ASSEMBLYLOADCONTEXT__ON_RESOURCE_RESOLVEinAppDomain::RaiseResourceResolveEvent -
METHOD__ASSEMBLYLOADCONTEXT__ON_ASSEMBLY_RESOLVEinAppDomain::RaiseAssemblyResolveEvent -
METHOD__ASSEMBLYLOADCONTEXT__RESOLVEinAppDomain::BindAssemblySpec -
METHOD__ASSEMBLYLOADCONTEXT__RESOLVESATELLITEASSEMBLYinAppDomain::BindSatelliteResourceByResourceRoots -
METHOD__ASSEMBLYLOADCONTEXT__RESOLVEUSINGEVENTinAppDomain::RaiseLoadFileEvent
Loader Allocator (loaderallocator.cpp) - #124303
-
METHOD__LOADERALLOCATOR__CTORinLoaderAllocator::Init
Custom Marshaler (custommarshalerinfo.cpp) - #124440
-
GetCustomMarshalermethod lookup inCustomMarshalerInfo::CustomMarshalerInfo
Dynamic Methods / Resolver (dynamicmethod.cpp) - #124303
-
METHOD__RESOLVER__GET_JIT_CONTEXTinLCGMethodResolver::GetJitContext -
METHOD__RESOLVER__GET_CODE_INFOinLCGMethodResolver::GetCodeInfo -
METHOD__RESOLVER__GET_LOCALS_SIGNATUREinLCGMethodResolver::GetLocalSig -
METHOD__RESOLVER__GET_STRING_LITERALinLCGMethodResolver::GetStringLiteral
Interop Utilities (interoputil.cpp) - #124303
-
METHOD__CULTURE_INFO__INT_CTORinGetCultureInfoForLCID -
METHOD__COLORMARSHALER__CONVERT_TO_MANAGEDinConvertOleColorToSystemColor -
METHOD__COLORMARSHALER__CONVERT_TO_NATIVEinConvertSystemColorToOleColor
Threads / Culture (threads.cpp) - #124303
-
METHOD__CULTURE_INFO__GET_CURRENT_CULTURE/METHOD__CULTURE_INFO__GET_CURRENT_UI_CULTUREinThread::GetCultureInfo -
METHOD__CULTURE_INFO__SET_CURRENT_CULTURE/METHOD__CULTURE_INFO__SET_CURRENT_UI_CULTUREinThread::SetCultureInfo
Event Sources (corhost.cpp) - #124303
-
METHOD__EVENT_SOURCE__INITIALIZE_DEFAULT_EVENT_SOURCESinCorHost2::CreateAppDomain
Diagnostics (ds-rt-coreclr.h) - #124303
-
METHOD__STARTUP_HOOK_PROVIDER__CALL_STARTUP_HOOKinds_rt_apply_startup_hook
Priority 2: Cross-Platform, Exception Handling
Exception Construction (clrex.cpp) - #126061
- Exception constructor in
EEException::CreateThrowable - Exception constructor in
EEMessageException::CreateThrowable - Exception constructor in
EEResourceException::CreateThrowable
Exception Construction (excep.cpp) - #124834
-
METHOD__RUNTIME_WRAPPED_EXCEPTION__OBJ_CTORinWrapThrowableInRuntimeWrappedException - Various exception constructors in
CreateTypeInitializationExceptionObject
Exception Utilities (excep.cpp) - #124834
-
METHOD__OBJECT__TO_STRINGinGetExceptionMessage -
METHOD__EXCEPTION__INTERNAL_PRESERVE_STACK_TRACEinRaiseTheExceptionInternalOnly -
METHOD__ENVIRONMENT__GET_RESOURCE_STRING_LOCALinGetResourceStringFromManaged - Event args constructor in
ExceptionNotifications::DeliverExceptionNotification
Exception Info Retrieval (comutilnative.cpp) - #123986
-
METHOD__EXCEPTION__GET_MESSAGEinExceptionNative::GetMessageFromException -
METHOD__EXCEPTION__GET_CLASS_NAMEinExceptionNative::GetMessageFromException -
METHOD__EXCEPTION__GET_SOURCEinExceptionNative::GetSource -
METHOD__EXCEPTION__GET_HELP_CONTEXTinExceptionNative::GetHelpContext
Priority 3: Cross-Platform, Entry Points (Complex)
These are entry points or thread-related and require careful consideration.
Application Entry Points (assembly.cpp)
- Thread start / Main entry point in
RunMainInternal- Convert MethodDescCallSite to UCO in entrypoints #126222 -
METHOD__STARTUP_HOOK_PROVIDER__MANAGED_STARTUPinAssembly::ExecuteMainMethod- Convert priority 3 MethodDescCallSite calls to UnmanagedCallersOnly #124854
Process Lifecycle (appdomain.cpp) - #124854
-
METHOD__APPCONTEXT__ON_UNHANDLED_EXCEPTIONinAppDomain::RaiseUnhandledExceptionEvent -
METHOD__APPCONTEXT__ON_PROCESS_EXITinAppDomain::RaiseExitProcessEvent
Invoke / Reflection (invokeutil.cpp) - #124854
- Constructor invocation in
InvokeUtil::CreateObject - Constructor invocation in
InvokeUtil::CreateValueType
Host Callbacks (corhost.cpp) - #126222
- Method invocation in
CorHost2::ExecuteAssembly
Exception handling
- ThrowHwEx` in HandleManagedFault and HandleHardwareException
-
ThrowExin DispatchManagedException -
RethrowinDispatchRethrownManagedException
Runtime threads
-
FinalizerThread::FinalizeAllObjects - Thread.Start -
KickOffThread_Worker
Reflection Invoke and similar
- MethodInfo.Invoke -
RuntimeMethodHandle_InvokeMethod - Custom attribute constructor in
COMCustomAttribute::CreateCaObject -
FuncEvalWrapper()andDoNormalFuncEval()
Priority 4: Platform-Specific (ObjC - macOS/iOS)
ObjC Interop (interoplibinterface_objc.cpp) - #124446
-
METHOD__OBJCMARSHAL__AVAILABLEUNHANDLEDEXCEPTIONPROPAGATIONinObjCMarshalNative::GetPropagatingExceptionCallback
Priority 5: Windows-Only (COM Interop)
These files are only compiled on Windows and are lowest priority.
COM Connection Points (comconnectionpoints.cpp) - #123864
- Delegate constructor in
ConnectionPoint::Advise - Provider method in
ConnectionPoint::Advise
Standard Interfaces (stdinterfaces.cpp) - #123864
-
CanReadproperty getter inIDispatchExInfo::GetIDsOfNames -
CanWriteproperty getter inIDispatchExInfo::GetIDsOfNames
CLR to COM Calls (clrtocomcall.cpp)
-
METHOD__COM_OBJECT__GET_EVENT_PROVIDERinComPlusMethodFrame::DoSlowPathComPlusCall- Convert some COM interop to UCO #125326 -
METHOD__CLASS__FORWARD_CALL_TO_INVOKEinCLRToCOMLateBoundWorker- Convert some COM interop to UCO #125326 - Event implmentation dispatch in
CLRToCOMEventCallWorker
COM Callable Wrapper (comcallablewrapper.cpp) - #123864
- Custom
ICustomQueryInterface::GetInterfaceinComCallWrapper::CallICustomQueryInterface
Custom Marshaler (custommarshalerinfo.cpp) - #125326
-
METHOD__STUBHELPERS__GET_IENUMERATOR_TO_ENUM_VARIANT_MARSHALERinCustomMarshalerInfo::GetIEnumeratorToEnumVariantMarshaler
Runtime Callable Wrapper (runtimecallablewrapper.cpp)
-
METHOD__LICENSE_INTEROP_PROXY__CREATEinLicenseInteropHelper::GetCurrentContextInfo- Revert "Partially revert PR #125326: revert ComActivator/LicenseInter… #126141 -
METHOD__LICENSE_INTEROP_PROXY__GETCURRENTCONTEXTINFOinLicenseInteropHelper::GetCurrentContextInfo- Revert "Partially revert PR #125326: revert ComActivator/LicenseInter… #126141 -
METHOD__LICENSE_INTEROP_PROXY__SAVEKEYINCURRENTCONTEXTinLicenseInteropHelper::SaveKeyInCurrentContext- Revert "Partially revert PR #125326: revert ComActivator/LicenseInter… #126141
OLE Variant (olevariant.cpp) - #125326
-
METHOD__VARIANT__CAST_VARIANTinOleVariant::MarshalOleVariantForObject -
METHOD__VARIANT__CONVERT_VARIANT_TO_OBJECTinOleVariant::MarshalObjectForOleVariant -
METHOD__VARIANT__CONVERT_OBJECT_TO_VARIANTinOleVariant::MarshalOleVariantForObject
Dispatch Info (dispatchinfo.cpp) - ~30 call sites - #126074
Various reflection-based calls in:
-
DispatchMemberInfo::GetParamInfo- parameter name retrieval -
DispatchMemberInfo::SetUpMethodMarshalerInfo- method handle retrieval -
DispatchMemberInfo::GetMemberInfoValue- property getters -
DispatchMemberInfo::SetMemberInfoValue- property setters -
DispatchExInfo::InvokeMember- member invocation -
DispatchExInfo::SynchWithManagedView-GetProperties,GetFields,GetMethods -
DispatchInfo::GetExceptionDescription- exception property getter
Follow up cleanup
- Delete CallDescrWorker
Reacted by Radek Doulik, Paulus Pärssinen and Pent Ploompuu
Reacted by Filip Navara
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Feb 2, 2026
Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.
AaronRobinsonMSFT commentedon Feb 2, 2026
/cc @jkotas @janvorli @davidwrighton
Replace MethodDescCallSite with UnmanagedCallersOnly in appdomain.cpp
AaronRobinsonMSFT commentedon Feb 4, 2026
@huoyaoyuan If you'd like to help on this one, feel free to grab any of the remaining issues in the Priority 1 bucket. All of them should be straight forward to address. I'd like to get through Priority 4 in .NET 11 as it would simplify some of the WASM work.
huoyaoyuan commentedon Feb 4, 2026
Note: for the IDispatch related ones, I actually had an attempt to move larger code to managed and reduce the number of interop boundaries. If not emergent, I think we can explore on this later.
Remove `MethodDescCallSite` in `appdomain.cpp` (#123967)
jkoritzinsky commentedon Feb 5, 2026
@AaronRobinsonMSFT What about the places in the runtime that use the
PREPARE_(NON)VIRTUAL_CALLSITE*withDECLARE_ARGHOLDER_ARRAYandCALL_MANAGED_METHOD_*macros? These also use MethodDescCallSite under the hood. I assume that we want to move these over to the UnmanagedCallersOnly pattern as well?AaronRobinsonMSFT commentedon Feb 5, 2026
No they don't. They do use some of the same machinery, but not
MethodDescCallSiteand the signature parsing removal which is my first goal.We do need to inspect those macros, but most of them should also go. The tricky bit is they are cheaper than
MethodDescCallSiteso we need to be careful about ensuring we don't regress.jkoritzinsky commentedon Feb 5, 2026
Would it be worthwhile moving some of the MethodDescCallSite usages to use those macros instead of UCO for cases where we can? Or is it more worthwhile to just move straight to UCO?
jkotas commentedon Feb 5, 2026
Ideally, we would get rid of CallDescrWorker.
PREPARE_(NON)VIRTUAL_CALLSITE*use CallDescrWorker, so we would not be able to get rid of it if we keep these macros around.33 remaining items
Revert "Partially revert PR #125326: revert ComActivator/LicenseInter… (
Replace MethodDescCallSite with UnmanagedCallersOnly in clrex.cpp (#1…
Convert MethodDescCallSite to UCO in entrypoints (#126222)
Convert throw and thread helpers to UCO (#126582)
Invoke debugger function evaluations through managed UCO
Invoke debugger function evaluations through managed UCO
Invoke debugger function evaluations through managed UCO
Use shared managed thunks for reflection invocation
Use shared managed thunks for reflection invocation (#133662)
Move custom attribute materialization and invocation to managed code (#…
Invoke debugger function evaluations through managed UCO
Invoke debugger function evaluations through managed UCO (#133659)
Remove obsolete native managed-invocation machinery (#133660)
Remnant cleanups after MethodDescCallSite removal (dotnet#136925)