-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Exception Propagation Helper
Before converting call sites that rely on exception propagation semantics (that is, Main(), finalizer, thread entrypoints`, we need to implement a helper function akin to:
internal static bool PropagateExceptionToNativeCode(Exception e)
{
// Stack has an assembly's EntryPoint as the last frame in the stack
if (...)
return false;
// Stack has no further managed frames above
if (...)
return false;
return true;
}This helper will determine whether an exception should be returned via the out-parameter (to be re-thrown by the caller) or left to propagate naturally through the UnmanagedCallersOnly boundary.
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)
-
METHOD__LOADERALLOCATOR__CTORinLoaderAllocator::Init
Custom Marshaler (custommarshalerinfo.cpp)
-
GetCustomMarshalermethod lookup inCustomMarshalerInfo::CustomMarshalerInfo
Dynamic Methods / Resolver (dynamicmethod.cpp)
-
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)
-
METHOD__CULTURE_INFO__INT_CTORinGetCultureInfoForLCID -
METHOD__COLORMARSHALER__CONVERT_TO_MANAGEDinConvertOleColorToSystemColor -
METHOD__COLORMARSHALER__CONVERT_TO_NATIVEinConvertSystemColorToOleColor
Threads / Culture (threads.cpp)
-
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)
-
METHOD__EVENT_SOURCE__INITIALIZE_DEFAULT_EVENT_SOURCESinCorHost2::CreateAppDomain
Diagnostics (ds-rt-coreclr.h)
-
METHOD__STARTUP_HOOK_PROVIDER__CALL_STARTUP_HOOKinds_rt_apply_startup_hook
Priority 2: Cross-Platform, Exception Handling
Exception Construction (clrex.cpp)
- Exception constructor in
EEException::CreateThrowable - Exception constructor in
EEMessageException::CreateThrowable - Exception constructor in
EEResourceException::CreateThrowable
Exception Construction (excep.cpp)
-
METHOD__RUNTIME_WRAPPED_EXCEPTION__OBJ_CTORinWrapThrowableInRuntimeWrappedException - Various exception constructors in
CreateTypeInitializationExceptionObject
Exception Utilities (excep.cpp)
-
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)
-
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 -
METHOD__ASYNC_HELPERS__HANDLE_ASYNC_ENTRYPOINTinRunMainInternal -
METHOD__ASYNC_HELPERS__HANDLE_ASYNC_ENTRYPOINT_VOIDinRunMainInternal -
METHOD__STARTUP_HOOK_PROVIDER__MANAGED_STARTUPinAssembly::ExecuteMainMethod
Process Lifecycle (appdomain.cpp)
-
METHOD__APPCONTEXT__ON_UNHANDLED_EXCEPTIONinAppDomain::RaiseUnhandledExceptionEvent -
METHOD__APPCONTEXT__ON_PROCESS_EXITinAppDomain::RaiseExitProcessEvent
Invoke / Reflection (invokeutil.cpp)
- Constructor invocation in
InvokeUtil::CreateObject - Constructor invocation in
InvokeUtil::CreateValueType
Custom Attributes (customattribute.cpp)
- Custom attribute constructor in
COMCustomAttribute::CreateCaObject
Host Callbacks (corhost.cpp)
- Method invocation in
CorHost2::ExecuteAssembly
Func Eval (funceval.cpp)
-
FuncEvalWrapper()andDoNormalFuncEval()
Priority 4: Platform-Specific (ObjC - macOS/iOS)
ObjC Interop (interoplibinterface_objc.cpp)
-
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)
- Delegate constructor in
ConnectionPoint::Advise - Provider method in
ConnectionPoint::Advise
Standard Interfaces (stdinterfaces.cpp)
-
CanReadproperty getter inIDispatchExInfo::GetIDsOfNames -
CanWriteproperty getter inIDispatchExInfo::GetIDsOfNames
CLR to COM Calls (clrtocomcall.cpp)
-
METHOD__COM_OBJECT__GET_EVENT_PROVIDERinComPlusMethodFrame::DoSlowPathComPlusCall - Event provider method in
ComPlusMethodFrame::DoSlowPathComPlusCall -
METHOD__CLASS__FORWARD_CALL_TO_INVOKEinCLRToCOMLateBoundWorker
COM Callable Wrapper (comcallablewrapper.cpp)
- Custom
ICustomQueryInterface::GetInterfaceinComCallWrapper::CallICustomQueryInterface
Custom Marshaler (custommarshalerinfo.cpp)
-
METHOD__STUBHELPERS__GET_IENUMERATOR_TO_ENUM_VARIANT_MARSHALERinCustomMarshalerInfo::GetIEnumeratorToEnumVariantMarshaler
Runtime Callable Wrapper (runtimecallablewrapper.cpp)
-
METHOD__LICENSE_INTEROP_PROXY__CREATEinLicenseInteropHelper::GetCurrentContextInfo -
METHOD__LICENSE_INTEROP_PROXY__GETCURRENTCONTEXTINFOinLicenseInteropHelper::GetCurrentContextInfo -
METHOD__LICENSE_INTEROP_PROXY__SAVEKEYINCURRENTCONTEXTinLicenseInteropHelper::SaveKeyInCurrentContext - Delegate method invocation in
COMToCLRWorkerBody
OLE Variant (olevariant.cpp)
-
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
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
Metadata
Metadata
Assignees
Labels
Type
Projects
Status