-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[mono] Implement coreclr_create_delegate and move ComponentActivator to shared CoreLib #59962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lambdageek
merged 4 commits into
dotnet:main
from
lambdageek:component-activator-in-mono
Oct 6, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
878682a
[CoreLib] Move ComponentActivator to the shared area
lambdageek 5c30a82
[mono] Implement coreclr_create_delegate hosting API
lambdageek f35e517
Mark IsolatedComponentLoadContext as unsupported on mobile and browser
lambdageek 0ee0d0a
Also unsupported on MacCatalyst
lambdageek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #include <mono/metadata/environment.h> | ||
| #include <mono/metadata/loader-internals.h> | ||
| #include <mono/metadata/native-library.h> | ||
| #include <mono/metadata/reflection-internals.h> | ||
| #include <mono/mini/mini-runtime.h> | ||
| #include <mono/mini/mini.h> | ||
| #include <mono/utils/mono-logger-internals.h> | ||
|
|
@@ -276,3 +277,89 @@ monovm_shutdown (int *latchedExitCode) | |
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int | ||
| monovm_create_delegate_impl (const char* assemblyName, const char* typeName, const char *methodName, void **delegate); | ||
|
|
||
|
|
||
| int | ||
| monovm_create_delegate (const char *assemblyName, const char *typeName, const char *methodName, void **delegate) | ||
| { | ||
| int result; | ||
| /* monovm_create_delegate may be called instead of monovm_execute_assembly. Initialize the | ||
| * runtime if it isn't already. */ | ||
| if (!mono_get_root_domain()) | ||
| mini_init (assemblyName, "v4.0.30319"); | ||
| MONO_ENTER_GC_UNSAFE; | ||
| result = monovm_create_delegate_impl (assemblyName, typeName, methodName, delegate); | ||
| MONO_EXIT_GC_UNSAFE; | ||
| return result; | ||
| } | ||
|
|
||
| int | ||
| monovm_create_delegate_impl (const char* assemblyName, const char* typeName, const char *methodName, void **delegate) | ||
| { | ||
| // Load an assembly and a type and a method from the type, then return a pointer to a native | ||
| // callable version of the method. See CorHost2::CreateDelegate | ||
| // | ||
| // We have to do this in general, but the CoreCLR hostpolicy only calls this with a handful | ||
| // of methods from the [CoreLib]Internal.Runtime.InteropServices.ComponentActivator | ||
| // class. See hostpolicy.cpp get_delegate() | ||
|
|
||
| ERROR_DECL (error); | ||
|
|
||
| const int failure = 0x80004005; /* E_FAIL */ | ||
|
|
||
| if (!delegate) | ||
| return failure; | ||
| *delegate = NULL; | ||
|
|
||
| MonoAssemblyLoadContext *alc = mono_alc_get_default (); | ||
|
|
||
| MonoImage *image; | ||
| if (!strcmp (MONO_ASSEMBLY_CORLIB_NAME, assemblyName)) { | ||
| image = mono_defaults.corlib; | ||
| } else { | ||
| MonoAssemblyName aname = {0}; | ||
| aname.name = assemblyName; | ||
| MonoAssemblyByNameRequest req; | ||
| mono_assembly_request_prepare_byname (&req, alc); | ||
| MonoImageOpenStatus status = MONO_IMAGE_OK; | ||
| MonoAssembly *assm = mono_assembly_request_byname (&aname, &req, &status); | ||
|
|
||
| if (!assm || status != MONO_IMAGE_OK) | ||
| return failure; | ||
| image = assm->image; | ||
| } | ||
|
|
||
| MonoType *t = mono_reflection_type_from_name_checked ((char*)typeName, alc, image, error); | ||
| goto_if_nok (error, fail); | ||
|
|
||
| g_assert (t); | ||
| MonoClass *klass = mono_class_from_mono_type_internal (t); | ||
|
|
||
|
|
||
| MonoMethod *method = mono_class_get_method_from_name_checked (klass, methodName, -1, 0, error); | ||
| goto_if_nok (error, fail); | ||
|
|
||
| if (!mono_method_has_unmanaged_callers_only_attribute (method)) { | ||
AaronRobinsonMSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mono_error_set_not_supported (error, "MonoVM only supports UnmanagedCallersOnly implementations of hostfxr_get_runtime_delegate delegate types"); | ||
| goto fail; | ||
| } | ||
|
|
||
| MonoClass *delegate_klass = NULL; | ||
| MonoGCHandle target_handle = 0; | ||
| MonoMethod *wrapper = mono_marshal_get_managed_wrapper (method, delegate_klass, target_handle, error); | ||
| goto_if_nok (error, fail); | ||
|
|
||
| gpointer addr = mono_compile_method_checked (wrapper, error); | ||
| goto_if_nok (error, fail); | ||
|
|
||
| *delegate = addr; | ||
|
||
| return 0; | ||
|
|
||
| fail: | ||
| g_warning ("coreclr_create_delegate: failed due to %s", mono_error_get_message (error)); | ||
| mono_error_cleanup (error); | ||
| return failure; | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.