Replace MethodDescCallSite with UnmanagedCallersOnly in clrex.cpp#126061
Open
AaronRobinsonMSFT wants to merge 2 commits intodotnet:mainfrom
Open
Replace MethodDescCallSite with UnmanagedCallersOnly in clrex.cpp#126061AaronRobinsonMSFT wants to merge 2 commits intodotnet:mainfrom
AaronRobinsonMSFT wants to merge 2 commits intodotnet:mainfrom
Conversation
Convert EEArgumentException, EETypeLoadException, and EEFileLoadException CreateThrowable methods to use UnmanagedCallersOnlyCaller instead of MethodDescCallSite/CallDescrWorker for invoking managed exception constructors. Add corresponding [UnmanagedCallersOnly] factory methods in managed code and remove unused metasig entries. Contributes to dotnet#123864 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
66 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR migrates several VM-to-managed exception construction paths in src/coreclr/vm/clrex.cpp from MethodDescCallSite / CallDescrWorker to the UnmanagedCallersOnlyCaller reverse P/Invoke pattern, adding the corresponding [UnmanagedCallersOnly] managed factory entrypoints and binder registrations in CoreLib.
Changes:
- Add
[UnmanagedCallersOnly]managed factory methods forArgumentException,TypeLoadException, andFileLoadExceptionconstruction. - Update
clrex.cppto invoke these factories viaUnmanagedCallersOnlyCallerinstead ofMethodDescCallSite. - Register new CoreLib binder method IDs and remove now-unused metasig entries.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/vm/metasig.h | Removes metasig entries no longer needed after dropping MethodDescCallSite ctor invocations. |
| src/coreclr/vm/corelib.h | Adds binder registrations for the new managed [UnmanagedCallersOnly] factory methods. |
| src/coreclr/vm/clrex.cpp | Switches three CreateThrowable() implementations to UnmanagedCallersOnlyCaller. |
| src/coreclr/System.Private.CoreLib/src/System/TypeLoadException.CoreCLR.cs | Adds [UnmanagedCallersOnly] TypeLoadException.Create factory. |
| src/coreclr/System.Private.CoreLib/src/System/IO/FileLoadException.CoreCLR.cs | Adds [UnmanagedCallersOnly] FileLoadException.Create factory. |
| src/coreclr/System.Private.CoreLib/src/System/Exception.CoreCLR.cs | Adds [UnmanagedCallersOnly] Exception.CreateArgumentException helper for ctor invocation. |
jkotas
reviewed
Mar 24, 2026
src/coreclr/System.Private.CoreLib/src/System/TypeLoadException.CoreCLR.cs
Show resolved
Hide resolved
jkotas
reviewed
Mar 24, 2026
jkotas
reviewed
Mar 24, 2026
src/coreclr/System.Private.CoreLib/src/System/Exception.CoreCLR.cs
Outdated
Show resolved
Hide resolved
Refactor EEFileLoadException::GetFileLoadKind to use a switch statement. Add ArgumentExceptionKind and FileLoadExceptionKind enums for managed mapping. Update CreateThrowable methods to use UnmanagedCallersOnlyCaller. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jkotas
approved these changes
Mar 25, 2026
Comment on lines
+59
to
+60
| FileLoadExceptionKind.FileLoad => new FileLoadException(fileName, hresult), | ||
| _ => throw new InvalidOperationException() |
Member
There was a problem hiding this comment.
Suggested change
| FileLoadExceptionKind.FileLoad => new FileLoadException(fileName, hresult), | |
| _ => throw new InvalidOperationException() | |
| _ /* FileLoadExceptionKind.FileLoad */ => new FileLoadException(fileName, hresult), |
| try | ||
| { | ||
| string? fileName = pFileName is not null ? new string(pFileName) : null; | ||
| *pThrowable = kind switch |
Member
There was a problem hiding this comment.
Suggested change
| *pThrowable = kind switch | |
| Debug.Assert(Enum.IsDefined(kind)); | |
| *pThrowable = kind switch |
|
|
||
| // Note that ArgumentException takes arguments to its constructor in a different order, | ||
| // for usability reasons. However it is inconsistent with our other exceptions. | ||
| *pThrowable = kind switch |
Member
There was a problem hiding this comment.
Suggested change
| *pThrowable = kind switch | |
| Debug.Assert(Enum.IsDefined(kind)); | |
| *pThrowable = kind switch |
Comment on lines
+420
to
+423
| ArgumentExceptionKind.Argument => new ArgumentException(message, paramName), | ||
| ArgumentExceptionKind.ArgumentNull => new ArgumentNullException(paramName, message), | ||
| ArgumentExceptionKind.ArgumentOutOfRange => new ArgumentOutOfRangeException(paramName, message), | ||
| _ => throw new InvalidOperationException() |
Member
There was a problem hiding this comment.
Suggested change
| ArgumentExceptionKind.Argument => new ArgumentException(message, paramName), | |
| ArgumentExceptionKind.ArgumentNull => new ArgumentNullException(paramName, message), | |
| ArgumentExceptionKind.ArgumentOutOfRange => new ArgumentOutOfRangeException(paramName, message), | |
| _ => throw new InvalidOperationException() | |
| ArgumentExceptionKind.ArgumentNull => new ArgumentNullException(paramName, message), | |
| ArgumentExceptionKind.ArgumentOutOfRange => new ArgumentOutOfRangeException(paramName, message), | |
| _ /* ArgumentExceptionKind.Argument */ => new ArgumentException(message, paramName) |
Comment on lines
+416
to
+417
| // Note that ArgumentException takes arguments to its constructor in a different order, | ||
| // for usability reasons. However it is inconsistent with our other exceptions. |
Member
There was a problem hiding this comment.
Suggested change
| // Note that ArgumentException takes arguments to its constructor in a different order, | |
| // for usability reasons. However it is inconsistent with our other exceptions. |
I do not think we need this comment here
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This PR description was generated with the assistance of GitHub Copilot.
Convert exception construction in
clrex.cppfromMethodDescCallSite/CallDescrWorkerto theUnmanagedCallersOnlyCallerpattern forEEArgumentException,EETypeLoadException, andEEFileLoadException.Changes
Exception.CoreCLR.cs- Add[UnmanagedCallersOnly] CreateArgumentExceptionfactory methodFileLoadException.CoreCLR.cs- Add[UnmanagedCallersOnly] Createfactory methodTypeLoadException.CoreCLR.cs- Add[UnmanagedCallersOnly] Createfactory methodclrex.cpp- ReplaceMethodDescCallSiteusage withUnmanagedCallersOnlyCallerin threeCreateThrowablemethodscorelib.h- Register new managed methods (METHOD__EXCEPTION__CREATE_ARGUMENT_EXCEPTION,METHOD__TYPE_LOAD_EXCEPTION__CREATE,METHOD__FILE_LOAD_EXCEPTION__CREATE)metasig.h- Remove unused metasig entries (IM(Str_Int_RetVoid),IM(Str_Str_Str_Int_RetVoid))Contributes to #123864