Skip to content

Convert exception-related MethodDescCallSite calls to UnmanagedCallersOnly#124834

Open
AaronRobinsonMSFT wants to merge 1 commit intodotnet:mainfrom
AaronRobinsonMSFT:uco-excep-priority2
Open

Convert exception-related MethodDescCallSite calls to UnmanagedCallersOnly#124834
AaronRobinsonMSFT wants to merge 1 commit intodotnet:mainfrom
AaronRobinsonMSFT:uco-excep-priority2

Conversation

@AaronRobinsonMSFT
Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT commented Feb 24, 2026

Summary

Replace MethodDescCallSite/CallDescrWorker calls with more efficient UnmanagedCallersOnly reverse P/Invoke calls in exception handling code paths (priority 2 from #123864).

Changes

Convert the following call sites in excep.cpp to use UnmanagedCallersOnlyCaller:

  • GetExceptionMessage: Object.ToString via UCO (METHOD__OBJECT__GET_TO_STRING)
  • ExceptionPreserveStackTrace: InternalPreserveStackTrace via UCO
  • WrapNonCompliantException: RuntimeWrappedException construction via UCO
  • CreateTypeInitializationExceptionObject: TypeInitializationException construction via UCO
  • GetResourceStringFromManaged: Environment.GetResourceStringLocal via UCO
  • GetEventArgsForNotification: FirstChanceExceptionEventArgs construction via UCO

Pattern

Each conversion follows the established pattern:

  1. Add [UnmanagedCallersOnly] static wrapper in managed code with Exception* out-parameter
  2. Add corresponding metasig in metasig.h and DEFINE_METHOD in corelib.h
  3. Replace MethodDescCallSite + ARG_SLOT with UnmanagedCallersOnlyCaller::InvokeThrowing
  4. Remove now-unused DEFINE_METHOD entries for old constructors

All GC references passed to InvokeThrowing are in GCPROTECT'd locations.

…sOnly

Replace MethodDescCallSite/CallDescrWorker calls with more efficient
UnmanagedCallersOnly reverse P/Invoke calls in exception handling code
paths (priority 2 from dotnet#123864):

- GetExceptionMessage: Object.ToString via UCO
- ExceptionPreserveStackTrace: InternalPreserveStackTrace via UCO
- WrapNonCompliantException: RuntimeWrappedException ctor via UCO
- CreateTypeInitializationExceptionObject: TypeInitializationException via UCO
- GetResourceStringFromManaged: Environment.GetResourceStringLocal via UCO
- GetEventArgsForNotification: FirstChanceExceptionEventArgs ctor via UCO

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request converts six exception-related MethodDescCallSite call sites in excep.cpp to use the more efficient UnmanagedCallersOnly reverse P/Invoke pattern, following the established approach from priority 1 and 2 tasks in issue #123864.

Changes:

  • Replaces MethodDescCallSite/CallDescrWorker with UnmanagedCallersOnlyCaller::InvokeThrowing for six exception-handling methods
  • Adds six new UnmanagedCallersOnly wrapper methods in managed code (Object.CoreCLR.cs, Exception.CoreCLR.cs, Environment.CoreCLR.cs)
  • Updates metasig.h with six new method signatures and corelib.h with updated DEFINE_METHOD entries, removing obsolete constructor definitions

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/coreclr/vm/metasig.h Adds 6 new metasig definitions for UnmanagedCallersOnly wrappers (PtrObj_PtrStr, PtrObj_PtrObj, PtrStr_PtrStr, PtrException_PtrException, PtrChar_PtrException_PtrObj, PtrException_PtrObj, all with PtrException_RetVoid suffix)
src/coreclr/vm/corelib.h Updates DEFINE_METHOD entries for the 6 converted methods and removes 3 obsolete constructor entries (TYPE_INIT_EXCEPTION.STR_EX_CTOR, RUNTIME_WRAPPED_EXCEPTION.OBJ_CTOR, FIRSTCHANCE_EVENTARGS.CTOR)
src/coreclr/vm/excep.cpp Converts 6 call sites to use UnmanagedCallersOnlyCaller: GetExceptionMessage, ExceptionPreserveStackTrace, WrapNonCompliantException, CreateTypeInitializationExceptionObject, GetResourceStringFromManaged, GetEventArgsForNotification
src/coreclr/System.Private.CoreLib/src/System/Object.CoreCLR.cs Adds GetToString UnmanagedCallersOnly wrapper with Exception* out-parameter
src/coreclr/System.Private.CoreLib/src/System/Exception.CoreCLR.cs Adds 4 UnmanagedCallersOnly wrappers: InternalPreserveStackTrace, CreateRuntimeWrappedException, CreateTypeInitializationException, CreateFirstChanceExceptionEventArgs
src/coreclr/System.Private.CoreLib/src/System/Environment.CoreCLR.cs Converts GetResourceStringLocal to UnmanagedCallersOnly pattern with string* in/out parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants