[reflection] Use MonoError in mono_custom_attrs_from_{index, method} instead of loade...
authorAleksey Kliger <aleksey@xamarin.com>
Thu, 25 Feb 2016 19:14:25 +0000 (14:14 -0500)
committerAleksey Kliger <aleksey@xamarin.com>
Mon, 29 Feb 2016 16:09:24 +0000 (11:09 -0500)
Also mark both as external only.  Runtime should use
mono_custom_attrs_from_index_checked and mono_custom_attrs_from_method_checked.

mono/metadata/cominterop.c
mono/metadata/object.c
mono/metadata/reflection-internals.h
mono/metadata/reflection.c
mono/metadata/reflection.h
mono/mini/aot-compiler.c
mono/mini/debugger-agent.c
mono/mini/mini-generic-sharing.c

index 0abeaf24a9e1d5f2cc36fc82cbc897fbe9aa049c..0060d90f2e89ebfac6cc2eff56bd4405d9524f22 100644 (file)
@@ -2444,6 +2444,7 @@ cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
                                                                                         guint32 lcid, gint32 *rgDispId)
 {
        static MonoClass *ComDispIdAttribute = NULL;
+       MonoError error;
        MonoCustomAttrInfo *cinfo = NULL;
        int i,ret = MONO_S_OK;
        MonoMethod* method;
@@ -2467,9 +2468,9 @@ cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
 
                method = mono_class_get_method_from_name(klass, methodname, -1);
                if (method) {
-                       cinfo = mono_custom_attrs_from_method (method);
+                       cinfo = mono_custom_attrs_from_method_checked (method, &error);
+                       mono_error_assert_ok (&error); /* FIXME what's reasonable to do here */
                        if (cinfo) {
-                               MonoError error;
                                MonoObject *result = mono_custom_attrs_get_attr_checked (cinfo, ComDispIdAttribute, &error);
                                g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/;
 
index 7a4a56290f2570cc49c1e7b19955183607826669..eef91fc09fa35f7d48227021e3ec9300ba89dd53 100644 (file)
@@ -4383,7 +4383,8 @@ mono_runtime_exec_main (MonoMethod *method, MonoArray *args, MonoObject **exc)
                }
        }
 
-       cinfo = mono_custom_attrs_from_method (method);
+       cinfo = mono_custom_attrs_from_method_checked (method, &error);
+       mono_error_cleanup (&error); /* FIXME warn here? */
        if (cinfo) {
                has_stathread_attribute = mono_custom_attrs_has_attr (cinfo, mono_class_get_sta_thread_attribute_class ());
                if (!cinfo->cached)
index f678e8e1875f9897e77ae668e4380e5aae63ffc2..94dd3c7baf6e24f78b6e334324ba7d04799e3779 100644 (file)
@@ -17,6 +17,12 @@ mono_reflection_get_custom_attrs_info_checked (MonoObject *obj, MonoError *error
 MonoArray*
 mono_reflection_get_custom_attrs_data_checked (MonoObject *obj, MonoError *error);
 
+MonoCustomAttrInfo*
+mono_custom_attrs_from_index_checked    (MonoImage *image, uint32_t idx, MonoError *error);
+MonoCustomAttrInfo*
+mono_custom_attrs_from_method_checked   (MonoMethod *method, MonoError *error);
+
+
 char*
 mono_identifier_unescape_type_name_chars (char* identifier);
 
index 7962f938864c2c3eb2ca65c617a188271151f677..6fe10540cc5d1d739b3fc24c2550b557bf8c5016 100644 (file)
@@ -1468,7 +1468,7 @@ mono_custom_attrs_free (MonoCustomAttrInfo *ainfo)
 {
        MONO_REQ_GC_NEUTRAL_MODE;
 
-       if (!ainfo->cached)
+       if (ainfo && !ainfo->cached)
                g_free (ainfo);
 }
 
@@ -9497,6 +9497,19 @@ mono_custom_attrs_data_construct (MonoCustomAttrInfo *cinfo, MonoError *error)
  */
 MonoCustomAttrInfo*
 mono_custom_attrs_from_index (MonoImage *image, guint32 idx)
+{
+       MonoError error;
+       MonoCustomAttrInfo *result = mono_custom_attrs_from_index_checked (image, idx, &error);
+       mono_error_cleanup (&error); /* FIXME a better public API that doesn't swallow the error. */
+       return result;
+}
+/**
+ * mono_custom_attrs_from_index_checked:
+ *
+ * Returns: NULL if no attributes are found.  On error returns NULL and sets @error.
+ */
+MonoCustomAttrInfo*
+mono_custom_attrs_from_index_checked (MonoImage *image, guint32 idx, MonoError *error)
 {
        guint32 mtoken, i, len;
        guint32 cols [MONO_CUSTOM_ATTR_SIZE];
@@ -9506,6 +9519,8 @@ mono_custom_attrs_from_index (MonoImage *image, guint32 idx)
        const char *data;
        MonoCustomAttrEntry* attr;
 
+       mono_error_init (error);
+
        ca = &image->tables [MONO_TABLE_CUSTOMATTRIBUTE];
 
        i = mono_metadata_custom_attrs_from_index (image, idx);
@@ -9525,7 +9540,6 @@ mono_custom_attrs_from_index (MonoImage *image, guint32 idx)
        ainfo->num_attrs = len;
        ainfo->image = image;
        for (i = len, tmp = list; i != 0; --i, tmp = tmp->next) {
-               MonoError error;
                mono_metadata_decode_row (ca, GPOINTER_TO_UINT (tmp->data), cols, MONO_CUSTOM_ATTR_SIZE);
                mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> MONO_CUSTOM_ATTR_TYPE_BITS;
                switch (cols [MONO_CUSTOM_ATTR_TYPE] & MONO_CUSTOM_ATTR_TYPE_MASK) {
@@ -9540,10 +9554,9 @@ mono_custom_attrs_from_index (MonoImage *image, guint32 idx)
                        break;
                }
                attr = &ainfo->attrs [i - 1];
-               attr->ctor = mono_get_method_checked (image, mtoken, NULL, NULL, &error);
+               attr->ctor = mono_get_method_checked (image, mtoken, NULL, NULL, error);
                if (!attr->ctor) {
-                       g_warning ("Can't find custom attr constructor image: %s mtoken: 0x%08x due to %s", image->name, mtoken, mono_error_get_message (&error));
-                       mono_loader_set_error_from_mono_error (&error);
+                       g_warning ("Can't find custom attr constructor image: %s mtoken: 0x%08x due to %s", image->name, mtoken, mono_error_get_message (error));
                        g_list_free (list);
                        g_free (ainfo);
                        return NULL;
@@ -9567,9 +9580,20 @@ mono_custom_attrs_from_index (MonoImage *image, guint32 idx)
 
 MonoCustomAttrInfo*
 mono_custom_attrs_from_method (MonoMethod *method)
+{
+       MonoError error;
+       MonoCustomAttrInfo* result = mono_custom_attrs_from_method_checked  (method, &error);
+       mono_error_cleanup (&error); /* FIXME want a better API that doesn't swallow the error */
+       return result;
+}
+
+MonoCustomAttrInfo*
+mono_custom_attrs_from_method_checked (MonoMethod *method, MonoError *error)
 {
        guint32 idx;
 
+       mono_error_init (error);
+
        /*
         * An instantiated method has the same cattrs as the generic method definition.
         *
@@ -9589,12 +9613,13 @@ mono_custom_attrs_from_method (MonoMethod *method)
        idx = mono_method_get_index (method);
        idx <<= MONO_CUSTOM_ATTR_BITS;
        idx |= MONO_CUSTOM_ATTR_METHODDEF;
-       return mono_custom_attrs_from_index (method->klass->image, idx);
+       return mono_custom_attrs_from_index_checked (method->klass->image, idx, error);
 }
 
 MonoCustomAttrInfo*
 mono_custom_attrs_from_class (MonoClass *klass)
 {
+       MonoError error;
        guint32 idx;
 
        if (klass->generic_class)
@@ -9612,12 +9637,18 @@ mono_custom_attrs_from_class (MonoClass *klass)
                idx <<= MONO_CUSTOM_ATTR_BITS;
                idx |= MONO_CUSTOM_ATTR_TYPEDEF;
        }
-       return mono_custom_attrs_from_index (klass->image, idx);
+       MonoCustomAttrInfo *result = mono_custom_attrs_from_index_checked (klass->image, idx, &error);
+       if (!is_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error); /* FIXME don't set loader error here */
+               return NULL;
+       }
+       return result;
 }
 
 MonoCustomAttrInfo*
 mono_custom_attrs_from_assembly (MonoAssembly *assembly)
 {
+       MonoError error;
        guint32 idx;
        
        if (image_is_dynamic (assembly->image))
@@ -9625,12 +9656,18 @@ mono_custom_attrs_from_assembly (MonoAssembly *assembly)
        idx = 1; /* there is only one assembly */
        idx <<= MONO_CUSTOM_ATTR_BITS;
        idx |= MONO_CUSTOM_ATTR_ASSEMBLY;
-       return mono_custom_attrs_from_index (assembly->image, idx);
+       MonoCustomAttrInfo * result = mono_custom_attrs_from_index_checked (assembly->image, idx, &error);
+       if (!is_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error); /* FIXME don't set loader error here */
+               return NULL;
+       }
+       return result;
 }
 
 static MonoCustomAttrInfo*
 mono_custom_attrs_from_module (MonoImage *image)
 {
+       MonoError error;
        guint32 idx;
        
        if (image_is_dynamic (image))
@@ -9638,12 +9675,18 @@ mono_custom_attrs_from_module (MonoImage *image)
        idx = 1; /* there is only one module */
        idx <<= MONO_CUSTOM_ATTR_BITS;
        idx |= MONO_CUSTOM_ATTR_MODULE;
-       return mono_custom_attrs_from_index (image, idx);
+       MonoCustomAttrInfo *result = mono_custom_attrs_from_index_checked (image, idx, &error);
+       if (!is_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error); /* FIXME don't set loader error here */
+               return NULL;
+       }
+       return result;
 }
 
 MonoCustomAttrInfo*
 mono_custom_attrs_from_property (MonoClass *klass, MonoProperty *property)
 {
+       MonoError error;
        guint32 idx;
        
        if (image_is_dynamic (klass->image)) {
@@ -9653,12 +9696,18 @@ mono_custom_attrs_from_property (MonoClass *klass, MonoProperty *property)
        idx = find_property_index (klass, property);
        idx <<= MONO_CUSTOM_ATTR_BITS;
        idx |= MONO_CUSTOM_ATTR_PROPERTY;
-       return mono_custom_attrs_from_index (klass->image, idx);
+       MonoCustomAttrInfo * result = mono_custom_attrs_from_index_checked (klass->image, idx, &error);
+       if (!is_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error); /* FIXME don't set loader error here */
+               return NULL;
+       }
+       return result;
 }
 
 MonoCustomAttrInfo*
 mono_custom_attrs_from_event (MonoClass *klass, MonoEvent *event)
 {
+       MonoError error;
        guint32 idx;
        
        if (image_is_dynamic (klass->image)) {
@@ -9668,12 +9717,18 @@ mono_custom_attrs_from_event (MonoClass *klass, MonoEvent *event)
        idx = find_event_index (klass, event);
        idx <<= MONO_CUSTOM_ATTR_BITS;
        idx |= MONO_CUSTOM_ATTR_EVENT;
-       return mono_custom_attrs_from_index (klass->image, idx);
+       MonoCustomAttrInfo * result = mono_custom_attrs_from_index_checked (klass->image, idx, &error);
+       if (!is_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error); /* FIXME don't set loader error here */
+               return NULL;
+       }
+       return result;
 }
 
 MonoCustomAttrInfo*
 mono_custom_attrs_from_field (MonoClass *klass, MonoClassField *field)
 {
+       MonoError error;
        guint32 idx;
        if (image_is_dynamic (klass->image)) {
                field = mono_metadata_get_corresponding_field_from_generic_type_definition (field);
@@ -9682,7 +9737,12 @@ mono_custom_attrs_from_field (MonoClass *klass, MonoClassField *field)
        idx = find_field_index (klass, field);
        idx <<= MONO_CUSTOM_ATTR_BITS;
        idx |= MONO_CUSTOM_ATTR_FIELDDEF;
-       return mono_custom_attrs_from_index (klass->image, idx);
+       MonoCustomAttrInfo * result = mono_custom_attrs_from_index_checked (klass->image, idx, &error);
+       if (!is_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error); /* FIXME don't set loader error here */
+               return NULL;
+       }
+       return result;
 }
 
 /**
@@ -9697,6 +9757,7 @@ mono_custom_attrs_from_field (MonoClass *klass, MonoClassField *field)
 MonoCustomAttrInfo*
 mono_custom_attrs_from_param (MonoMethod *method, guint32 param)
 {
+       MonoError error;
        MonoTableInfo *ca;
        guint32 i, idx, method_index;
        guint32 param_list, param_last, param_pos, found;
@@ -9757,7 +9818,12 @@ mono_custom_attrs_from_param (MonoMethod *method, guint32 param)
        idx = i;
        idx <<= MONO_CUSTOM_ATTR_BITS;
        idx |= MONO_CUSTOM_ATTR_PARAMDEF;
-       return mono_custom_attrs_from_index (image, idx);
+       MonoCustomAttrInfo *result = mono_custom_attrs_from_index_checked (image, idx, &error);
+       if (!is_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error);
+               return NULL;
+       }
+       return result;
 }
 
 gboolean
@@ -9869,10 +9935,12 @@ mono_reflection_get_custom_attrs_info_checked (MonoObject *obj, MonoError *error
                cinfo = mono_custom_attrs_from_field (rfield->field->parent, rfield->field);
        } else if ((strcmp ("MonoMethod", klass->name) == 0) || (strcmp ("MonoCMethod", klass->name) == 0)) {
                MonoReflectionMethod *rmethod = (MonoReflectionMethod*)obj;
-               cinfo = mono_custom_attrs_from_method (rmethod->method);
+               cinfo = mono_custom_attrs_from_method_checked (rmethod->method, error);
+               return_val_if_nok (error, NULL);
        } else if ((strcmp ("MonoGenericMethod", klass->name) == 0) || (strcmp ("MonoGenericCMethod", klass->name) == 0)) {
                MonoReflectionMethod *rmethod = (MonoReflectionMethod*)obj;
-               cinfo = mono_custom_attrs_from_method (rmethod->method);
+               cinfo = mono_custom_attrs_from_method_checked (rmethod->method, error);
+               return_val_if_nok (error, NULL);
        } else if (strcmp ("ParameterInfo", klass->name) == 0 || strcmp ("MonoParameterInfo", klass->name) == 0) {
                MonoReflectionParameter *param = (MonoReflectionParameter*)obj;
                MonoClass *member_class = mono_object_class (param->MemberImpl);
index ac4dc33908dee7e51dbee03f5ce2e7f8da0177bd..a73c816b7627a9358daf872313d599fe315f3f66 100644 (file)
@@ -78,7 +78,9 @@ MONO_RT_EXTERNAL_ONLY
 MONO_API MonoCustomAttrInfo* mono_reflection_get_custom_attrs_info (MonoObject *obj);
 MONO_RT_EXTERNAL_ONLY
 MONO_API MonoArray*  mono_custom_attrs_construct (MonoCustomAttrInfo *cinfo);
+MONO_RT_EXTERNAL_ONLY
 MONO_API MonoCustomAttrInfo* mono_custom_attrs_from_index    (MonoImage *image, uint32_t idx);
+MONO_RT_EXTERNAL_ONLY
 MONO_API MonoCustomAttrInfo* mono_custom_attrs_from_method   (MonoMethod *method);
 MONO_API MonoCustomAttrInfo* mono_custom_attrs_from_class    (MonoClass *klass);
 MONO_API MonoCustomAttrInfo* mono_custom_attrs_from_assembly (MonoAssembly *assembly);
index 8227033a1f3ea539d3949eeef0d0f8f0ac1feaa2..7e47c9b93b9ef3c7ad2de566571bb5e46c8ed046 100644 (file)
@@ -40,6 +40,7 @@
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/metadata-internals.h>
+#include <mono/metadata/reflection-internals.h>
 #include <mono/metadata/marshal.h>
 #include <mono/metadata/gc-internals.h>
 #include <mono/metadata/mempool-internals.h>
@@ -3910,7 +3911,12 @@ add_wrappers (MonoAotCompile *acfg)
                 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
                 * name to avoid defining a new assembly to contain it.
                 */
-               cattr = mono_custom_attrs_from_method (method);
+               cattr = mono_custom_attrs_from_method_checked (method, &error);
+               if (!is_ok (&error)) {
+                       char *name = mono_method_get_full_name (method);
+                       report_loader_error (acfg, &error, "Failed to load custom attributes from method %s due to %s\n", name, mono_error_get_message (&error));
+                       g_free (name);
+               }
 
                if (cattr) {
                        for (j = 0; j < cattr->num_attrs; ++j)
index 718cec686fcee855e4d72af8a87755a4b11537a1..e230a5fca340c2eea0646010b63d41a89323ee2b 100644 (file)
@@ -3323,6 +3323,7 @@ static void
 init_jit_info_dbg_attrs (MonoJitInfo *ji)
 {
        static MonoClass *hidden_klass, *step_through_klass, *non_user_klass;
+       MonoError error;
        MonoCustomAttrInfo *ainfo;
 
        if (ji->dbg_attrs_inited)
@@ -3337,7 +3338,8 @@ init_jit_info_dbg_attrs (MonoJitInfo *ji)
        if (!non_user_klass)
                non_user_klass = mono_class_load_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggerNonUserCodeAttribute");
 
-       ainfo = mono_custom_attrs_from_method (jinfo_get_method (ji));
+       ainfo = mono_custom_attrs_from_method_checked (jinfo_get_method (ji), &error);
+       mono_error_cleanup (&error); /* FIXME don't swallow the error? */
        if (ainfo) {
                if (mono_custom_attrs_has_attr (ainfo, hidden_klass))
                        ji->dbg_hidden = TRUE;
@@ -8684,6 +8686,7 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                break;
        }
        case CMD_METHOD_GET_CATTRS: {
+               MonoError error;
                MonoClass *attr_klass;
                MonoCustomAttrInfo *cinfo;
 
@@ -8692,7 +8695,11 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                if (err != ERR_NONE)
                        return err;
 
-               cinfo = mono_custom_attrs_from_method (method);
+               cinfo = mono_custom_attrs_from_method_checked (method, &error);
+               if (!is_ok (&error)) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error message */
+                       return ERR_LOADER_ERROR;
+               }
 
                err = buffer_add_cattrs (buf, domain, method->klass->image, attr_klass, cinfo);
                if (err != ERR_NONE)
index 4f54bd69d95ddadbfcca4ad56e8e4fddeb7665b8..b8d706a205e506da82420d3062ba1ed2af74f86c 100644 (file)
@@ -2652,6 +2652,7 @@ is_async_state_machine_class (MonoClass *klass)
 static G_GNUC_UNUSED gboolean
 is_async_method (MonoMethod *method)
 {
+       MonoError error;
        MonoCustomAttrInfo *cattr;
        MonoMethodSignature *sig;
        gboolean res = FALSE;
@@ -2667,7 +2668,11 @@ is_async_method (MonoMethod *method)
                                (sig->ret->type == MONO_TYPE_CLASS && !strcmp (sig->ret->data.generic_class->container_class->name, "Task")) ||
                                (sig->ret->type == MONO_TYPE_GENERICINST && !strcmp (sig->ret->data.generic_class->container_class->name, "Task`1")))) {
                //printf ("X: %s\n", mono_method_full_name (method, TRUE));
-               cattr = mono_custom_attrs_from_method (method);
+               cattr = mono_custom_attrs_from_method_checked (method, &error);
+               if (!is_ok (&error)) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error? */
+                       return FALSE;
+               }
                if (cattr) {
                        if (mono_custom_attrs_has_attr (cattr, attr_class))
                                res = TRUE;