-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[mono] Add support for UnmanagedCallersOnlyAttribute #38728
Merged
lambdageek
merged 27 commits into
dotnet:master
from
lambdageek:hack-unmanaged-callers-only
Jul 10, 2020
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
e6009c9
[marshal] Add mono_method_has_unmanaged_callers_only_attribute
lambdageek 4156eff
[marshal] Allow calls to mono_marshal_get_managed_wrapped without a d…
lambdageek a8d4d57
[aot] Allow decode_method_ref to decode NATIVE_TO_MANAGED wrappers wi…
lambdageek 40662c4
[mini] return ptr to wrapper in mono_ldftn for UnmanagedCallersOnly m…
lambdageek 92ef04c
[interp] ldftn will return a native-to-managed wrapper to UnmanagedCa…
lambdageek 36ebfaa
[tests] Temporarily set UnmanagedCallersOnly tests to Pri0
lambdageek 376db97
fixup build
lambdageek ff12195
fixup null
lambdageek 04b6da5
aot: emit byte when we don't expect a class
lambdageek 3ef4363
jit: move wrapper creation to method-to-ir, not mono_ldftn
lambdageek 1f6d260
fix typo
lambdageek 2579230
don't get callconv from Invoke if there's no invoke
lambdageek 00ed38d
jit: don't create a jump trampoline for ldftn of a native-to-managed …
lambdageek b100d40
interp: transform LDFTN into LDC of a create_method_pointer for Unman…
lambdageek 7b00b07
remove obsolete comment
lambdageek 6340c36
marshal: throw invalid program exception for instance and generic met…
lambdageek a7199ad
Emit IPE throw instead of aborting JIT or interp compilation for bad …
lambdageek d6b9c56
Use domain mempool for IPE error message
lambdageek 1b5422f
disallow delegate constructor calls on UnmanagedCallersOnly methods
lambdageek 6e85742
throw IPE if UnmanagedCallersOnly method has non-blittable args
lambdageek 9d502c5
disallow DllImport and UnmanagedCallersOnly together
lambdageek 70075a9
Change UnamangedCallersOnlyTest.csproj back to Pri1
lambdageek 025dd73
Apply suggestions from code review
lambdageek 0e6f067
System.Char isn't blittable
lambdageek a2f0d94
fixup non-blittable exception message
lambdageek 89c8c28
non-netcore doesn't have UnmanagedCallersOnlyAttribute
lambdageek ee50f3e
remove dead code
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 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 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 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 |
---|---|---|
|
@@ -113,6 +113,7 @@ static GENERATE_TRY_GET_CLASS_WITH_CACHE (unmanaged_function_pointer_attribute, | |
|
||
#ifdef ENABLE_NETCORE | ||
static GENERATE_TRY_GET_CLASS_WITH_CACHE (suppress_gc_transition_attribute, "System.Runtime.InteropServices", "SuppressGCTransitionAttribute") | ||
static GENERATE_TRY_GET_CLASS_WITH_CACHE (unmanaged_callers_only_attribute, "System.Runtime.InteropServices", "UnmanagedCallersOnlyAttribute") | ||
#endif | ||
|
||
static MonoImage* | ||
|
@@ -3661,6 +3662,23 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions, | |
|
||
mb->method->save_lmf = 1; | ||
|
||
if (G_UNLIKELY (pinvoke && mono_method_has_unmanaged_callers_only_attribute (method))) { | ||
/* emit a wrapper that throws a NotSupportedException */ | ||
get_marshal_cb ()->mb_emit_exception (mb, "System", "NotSupportedException", "Method canot be marked with both DllImportAttribute and UnmanagedCallersOnlyAttribute"); | ||
|
||
info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE); | ||
info->d.managed_to_native.method = method; | ||
|
||
csig = mono_metadata_signature_dup_full (get_method_image (method), sig); | ||
csig->pinvoke = 0; | ||
res = mono_mb_create_and_cache_full (cache, method, mb, csig, | ||
csig->param_count + 16, info, NULL); | ||
mono_mb_free (mb); | ||
|
||
return res; | ||
} | ||
|
||
|
||
/* | ||
* In AOT mode and embedding scenarios, it is possible that the icall is not | ||
* registered in the runtime doing the AOT compilation. | ||
|
@@ -3940,10 +3958,54 @@ emit_managed_wrapper_noilgen (MonoMethodBuilder *mb, MonoMethodSignature *invoke | |
} | ||
#endif | ||
|
||
static gboolean | ||
type_is_blittable (MonoType *type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already a class->blittable flag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed this, but it's not the same because of byref types. |
||
{ | ||
if (type->byref) | ||
CoffeeFlux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return FALSE; | ||
type = mono_type_get_underlying_type (type); | ||
switch (type->type) { | ||
case MONO_TYPE_I1: | ||
case MONO_TYPE_U1: | ||
case MONO_TYPE_I2: | ||
case MONO_TYPE_U2: | ||
case MONO_TYPE_I4: | ||
case MONO_TYPE_U4: | ||
case MONO_TYPE_I8: | ||
case MONO_TYPE_U8: | ||
case MONO_TYPE_R4: | ||
case MONO_TYPE_R8: | ||
case MONO_TYPE_I: | ||
case MONO_TYPE_U: | ||
case MONO_TYPE_PTR: | ||
case MONO_TYPE_FNPTR: | ||
return TRUE; | ||
default: | ||
return m_class_is_blittable (mono_class_from_mono_type_internal (type)); | ||
} | ||
} | ||
|
||
static gboolean | ||
method_signature_is_blittable (MonoMethodSignature *sig) | ||
{ | ||
if (!type_is_blittable (sig->ret)) | ||
return FALSE; | ||
|
||
for (int i = 0; i < sig->param_count; ++i) { | ||
MonoType *type = sig->params [i]; | ||
if (!type_is_blittable (type)) | ||
return FALSE; | ||
} | ||
return TRUE; | ||
} | ||
|
||
/** | ||
* mono_marshal_get_managed_wrapper: | ||
* Generates IL code to call managed methods from unmanaged code | ||
* If \p target_handle is \c 0, the wrapper info will be a \c WrapperInfo structure. | ||
* | ||
* If \p delegate_klass is \c NULL, we're creating a wrapper for a function pointer to a method marked with | ||
* UnamangedCallersOnlyAttribute. | ||
*/ | ||
MonoMethod * | ||
mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, MonoGCHandle target_handle, MonoError *error) | ||
|
@@ -3975,11 +4037,33 @@ mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, | |
if (!target_handle && (res = mono_marshal_find_in_cache (cache, method))) | ||
return res; | ||
|
||
invoke = mono_get_delegate_invoke_internal (delegate_klass); | ||
invoke_sig = mono_method_signature_internal (invoke); | ||
|
||
mspecs = g_new0 (MonoMarshalSpec*, mono_method_signature_internal (invoke)->param_count + 1); | ||
mono_method_get_marshal_info (invoke, mspecs); | ||
if (G_UNLIKELY (!delegate_klass)) { | ||
/* creating a wrapper for a function pointer with UnmanagedCallersOnlyAttribute */ | ||
if (mono_method_has_marshal_info (method)) { | ||
mono_error_set_invalid_program (error, "method %s with UnmanadedCallersOnlyAttribute has marshal specs", mono_method_full_name (method, TRUE)); | ||
return NULL; | ||
} | ||
invoke = NULL; | ||
invoke_sig = mono_method_signature_internal (method); | ||
if (invoke_sig->hasthis) { | ||
mono_error_set_invalid_program (error, "method %s with UnamanagedCallersOnlyAttribute is an instance method", mono_method_full_name (method, TRUE)); | ||
return NULL; | ||
} | ||
if (method->is_generic || method->is_inflated || mono_class_is_ginst (method->klass)) { | ||
mono_error_set_invalid_program (error, "method %s with UnamangedCallersOnlyAttribute is generic", mono_method_full_name (method, TRUE)); | ||
return NULL; | ||
} | ||
if (!method_signature_is_blittable (invoke_sig)) { | ||
mono_error_set_invalid_program (error, "method %s with UnmanagedCallersOnlyAttribute has non-blittable parameters or return type", mono_method_full_name (method, TRUE)); | ||
return NULL; | ||
} | ||
mspecs = g_new0 (MonoMarshalSpec*, invoke_sig->param_count + 1); | ||
} else { | ||
invoke = mono_get_delegate_invoke_internal (delegate_klass); | ||
invoke_sig = mono_method_signature_internal (invoke); | ||
mspecs = g_new0 (MonoMarshalSpec*, invoke_sig->param_count + 1); | ||
mono_method_get_marshal_info (invoke, mspecs); | ||
} | ||
|
||
sig = mono_method_signature_internal (method); | ||
|
||
|
@@ -4005,10 +4089,11 @@ mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, | |
m.csig = csig; | ||
m.image = get_method_image (method); | ||
|
||
mono_marshal_set_callconv_from_modopt (invoke, csig, TRUE); | ||
if (invoke) | ||
mono_marshal_set_callconv_from_modopt (invoke, csig, TRUE); | ||
|
||
/* The attribute is only available in Net 2.0 */ | ||
if (mono_class_try_get_unmanaged_function_pointer_attribute_class ()) { | ||
if (delegate_klass && mono_class_try_get_unmanaged_function_pointer_attribute_class ()) { | ||
MonoCustomAttrInfo *cinfo; | ||
MonoCustomAttrEntry *attr; | ||
|
||
|
@@ -4097,7 +4182,7 @@ mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, | |
} | ||
mono_mb_free (mb); | ||
|
||
for (i = mono_method_signature_internal (invoke)->param_count; i >= 0; i--) | ||
for (i = invoke_sig->param_count; i >= 0; i--) | ||
if (mspecs [i]) | ||
mono_metadata_free_marshal_spec (mspecs [i]); | ||
g_free (mspecs); | ||
|
@@ -6755,3 +6840,33 @@ get_marshal_cb (void) | |
} | ||
return &marshal_cb; | ||
} | ||
|
||
/** | ||
* mono_method_has_unmanaged_callers_only_attribute: | ||
* | ||
* Returns \c TRUE if \p method has the \c UnmanagedCallersOnlyAttribute | ||
*/ | ||
gboolean | ||
mono_method_has_unmanaged_callers_only_attribute (MonoMethod *method) | ||
{ | ||
#ifndef ENABLE_NETCORE | ||
return FALSE; | ||
#else | ||
ERROR_DECL (attr_error); | ||
MonoClass *attr_klass = NULL; | ||
attr_klass = mono_class_try_get_unmanaged_callers_only_attribute_class (); | ||
CoffeeFlux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!attr_klass) | ||
return FALSE; | ||
MonoCustomAttrInfo *cinfo; | ||
cinfo = mono_custom_attrs_from_method_checked (method, attr_error); | ||
if (!is_ok (attr_error) || !cinfo) { | ||
mono_error_cleanup (attr_error); | ||
return FALSE; | ||
} | ||
gboolean result; | ||
result = mono_custom_attrs_has_attr (cinfo, attr_klass); | ||
if (!cinfo->cached) | ||
mono_custom_attrs_free (cinfo); | ||
return result; | ||
#endif | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this 16 coming from? It looks like it's used elsewhere, but I'm not familiar enough with this code to understand why.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. I'm cargo-culting the other
mono_mb_create_and_cache_full
calls in this function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, that's what I feared. @vargaz any idea? For my own learning if nothing else :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea, it probably comes from the fact that these are wrappers so the deepest stack will be calling the 'wrapped' method, so param_count + is good for max stack.