Skip to content
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

Add AOT support for the EntryPoint property for UnmanagedCallersOnly. #44809

Merged
merged 3 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/mono/mono/metadata/custom-attrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ load_cattr_value (MonoImage *image, MonoType *t, MonoObject **out_obj, const cha
g_error ("generic valutype %s not handled in custom attr value decoding", m_class_get_name (t->data.klass));
break;

case MONO_TYPE_STRING:
case MONO_TYPE_STRING: {
const char *start = p;

if (!bcheck_blob (p, 0, boundp, error))
return NULL;
MONO_DISABLE_WARNING (4310) // cast truncates constant value
Expand All @@ -389,7 +391,7 @@ MONO_RESTORE_WARNING
return NULL;
*end = p + slen;
if (!out_obj)
return (void*)p;
return (void*)start;
// https://bugzilla.xamarin.com/show_bug.cgi?id=60848
// Custom attribute strings are encoded as wtf-8 instead of utf-8.
// If we decode using utf-8 like the spec says, we will silently fail
Expand All @@ -398,6 +400,7 @@ MONO_RESTORE_WARNING
// See https://simonsapin.github.io/wtf-8/ for a description of wtf-8.
*out_obj = (MonoObject*)mono_string_new_wtf8_len_checked (mono_domain_get (), p, slen, error);
return NULL;
}
case MONO_TYPE_CLASS: {
MonoType *type = load_cattr_type (image, t, TRUE, p, boundp, end, error, &slen);
if (out_obj) {
Expand Down Expand Up @@ -1204,7 +1207,9 @@ mono_reflection_create_custom_attr_data_args (MonoImage *image, MonoMethod *meth
* mono_reflection_create_custom_attr_data_args_noalloc:
*
* Same as mono_reflection_create_custom_attr_data_args but allocate no managed objects, return values
* using C arrays. Only usable for cattrs with primitive/type arguments.
* using C arrays. Only usable for cattrs with primitive/type/string arguments.
* For types, a MonoType* is returned.
* For strings, the address in the metadata blob is returned.
* TYPED_ARGS, NAMED_ARGS, and NAMED_ARG_INFO should be freed using g_free ().
*/
void
Expand Down
34 changes: 33 additions & 1 deletion src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -5060,10 +5060,42 @@ MONO_RESTORE_WARNING
if (cattr->attrs [j].ctor && mono_is_corlib_image (m_class_get_image (cattr->attrs [j].ctor->klass)) && !strcmp (m_class_get_name (cattr->attrs [j].ctor->klass), "UnmanagedCallersOnlyAttribute"))
break;
if (j < cattr->num_attrs) {
MonoMethod *wrapper = mono_marshal_get_managed_wrapper (method, NULL, 0, error);
MonoCustomAttrEntry *e = &cattr->attrs [j];
const char *named;
int slen;
char *export_name = NULL;
MonoMethod *wrapper;

if (!(method->flags & METHOD_ATTRIBUTE_STATIC)) {
g_warning ("AOT restriction: Method '%s' must be static since it is decorated with [UnmanagedCallers].",
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this permanent limitation?

Copy link
Member

Choose a reason for hiding this comment

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

It's part of the spec of UnmanagedCallersOnlyAttribute

It is an error to apply the attribute to anything other than an ordinary static method or ordinary static local function. The C# compiler will mark any non-static or static non-ordinary methods imported from metadata with this attribute as unsupported by the language.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we check only for this restriction then and shouldn't this be TLE or something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its just a copy paste from the MonoPInvokeCallback case.

mono_method_full_name (method, TRUE));
exit (1);
}

gpointer *typed_args = NULL;
gpointer *named_args = NULL;
CattrNamedArg *named_arg_info = NULL;
int num_named_args = 0;
mono_reflection_create_custom_attr_data_args_noalloc (acfg->image, e->ctor, e->data, e->data_size, &typed_args, &named_args, &num_named_args, &named_arg_info, error);
mono_error_assert_ok (error);
for (j = 0; j < num_named_args; ++j) {
if (named_arg_info [j].field && !strcmp (named_arg_info [j].field->name, "EntryPoint")) {
named = named_args [j];
slen = mono_metadata_decode_value (named, &named);
export_name = (char *)g_malloc (slen + 1);
memcpy (export_name, named, slen);
export_name [slen] = 0;
}
}
g_free (named_args);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
g_free (named_args);
g_assert (!typed_args);
g_free (named_args);

g_free (named_arg_info);

wrapper = mono_marshal_get_managed_wrapper (method, NULL, 0, error);
mono_error_assert_ok (error);

add_method (acfg, wrapper);
if (export_name)
g_hash_table_insert (acfg->export_names, wrapper, export_name);
}
#endif

Expand Down
5 changes: 3 additions & 2 deletions src/mono/mono/mini/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,9 @@ method_should_be_regression_tested (MonoMethod *method, gboolean interp)
if (!is_ok (error))
continue;

char *utf8_str = (char*)(void*)typed_args[0]; //this points into image memory that is constant
const char *arg = (const char*)typed_args [0];
mono_metadata_decode_value (arg, &arg);
char *utf8_str = (char*)arg; //this points into image memory that is constant
g_free (typed_args);
g_free (named_args);
g_free (arginfo);
Expand Down Expand Up @@ -3352,4 +3354,3 @@ mono_parse_env_options (int *ref_argc, char **ref_argv [])
fprintf (stderr, "%s", ret);
exit (1);
}