2010-05-29 Robert Jordan <robertj@gmx.net>
[mono.git] / mono / metadata / loader.c
index 5f278a23e63983df4c07813677102b8f5a7ab14f..ea3eada007156034d16264f5a35d915eb29ede57 100644 (file)
 #include <mono/metadata/marshal.h>
 #include <mono/metadata/lock-tracer.h>
 #include <mono/metadata/verify-internals.h>
-#include <mono/utils/mono-logger.h>
+#include <mono/utils/mono-logger-internal.h>
 #include <mono/utils/mono-dl.h>
 #include <mono/utils/mono-membar.h>
 #include <mono/utils/mono-counters.h>
+#include <mono/utils/mono-error-internals.h>
 
 MonoDefaults mono_defaults;
 
@@ -170,7 +171,7 @@ mono_loader_set_error_type_load (const char *class_name, const char *assembly_na
         * can't deal with load errors, and this message is more helpful than an
         * assert.
         */
-       g_warning ("The class %s could not be loaded, used in %s", class_name, assembly_name);
+       mono_trace_warning (MONO_TRACE_TYPE, "The class %s could not be loaded, used in %s", class_name, assembly_name);
 
        set_loader_error (error);
 }
@@ -604,6 +605,13 @@ find_method_in_class (MonoClass *klass, const char *name, const char *qname, con
        }
 
        mono_class_setup_methods (klass);
+       /*
+       We can't fail lookup of methods otherwise the runtime will fail with MissingMethodException instead of TypeLoadException.
+       See mono/tests/generic-type-load-exception.2.il
+       FIXME we should better report this error to the caller
+        */
+       if (!klass->methods)
+               return NULL;
        for (i = 0; i < klass->method.count; ++i) {
                MonoMethod *m = klass->methods [i];
                MonoMethodSignature *msig;
@@ -697,22 +705,28 @@ find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSig
 }
 
 static MonoMethodSignature*
-inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
+inflate_generic_signature_checked (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context, MonoError *error)
 {
        MonoMethodSignature *res;
        gboolean is_open;
        int i;
 
+       mono_error_init (error);
        if (!context)
                return sig;
 
        res = g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE + ((gint32)sig->param_count) * sizeof (MonoType*));
        res->param_count = sig->param_count;
        res->sentinelpos = -1;
-       res->ret = mono_class_inflate_generic_type (sig->ret, context);
+       res->ret = mono_class_inflate_generic_type_checked (sig->ret, context, error);
+       if (!mono_error_ok (error))
+               goto fail;
        is_open = mono_class_is_open_constructed_type (res->ret);
        for (i = 0; i < sig->param_count; ++i) {
-               res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
+               res->params [i] = mono_class_inflate_generic_type_checked (sig->params [i], context, error);
+               if (!mono_error_ok (error))
+                       goto fail;
+
                if (!is_open)
                        is_open = mono_class_is_open_constructed_type (res->params [i]);
        }
@@ -725,6 +739,25 @@ inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGener
        res->has_type_parameters = is_open;
        res->is_inflated = 1;
        return res;
+
+fail:
+       if (res->ret)
+               mono_metadata_free_type (res->ret);
+       for (i = 0; i < sig->param_count; ++i) {
+               if (res->params [i])
+                       mono_metadata_free_type (res->params [i]);
+       }
+       g_free (res);
+       return NULL;
+}
+
+static MonoMethodSignature*
+inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
+{
+       MonoError error;
+       MonoMethodSignature *res = inflate_generic_signature_checked (image, sig, context, &error);
+       g_assert (mono_error_ok (&error)); /*FIXME move callers to use _checked version*/
+       return res;
 }
 
 static MonoMethodHeader*
@@ -772,9 +805,9 @@ mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 to
                return mono_method_signature (method);
 
        if (table == MONO_TABLE_METHODSPEC) {
-               g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
-                         mono_method_signature (method));
-               g_assert (method->is_inflated);
+               /* the verifier (do_invoke_method) will turn the NULL into a verifier error */
+               if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || !method->is_inflated)
+                       return NULL;
 
                return mono_method_signature (method);
        }
@@ -807,11 +840,26 @@ mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 to
                sig = cache_memberref_sig (image, sig_idx, sig);
        }
 
+       if (!mono_verifier_is_sig_compatible (image, method, sig)) {
+               guint32 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
+               const char *fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
+
+               mono_loader_set_error_bad_image (g_strdup_printf ("Incompatible method signature class token 0x%08x field name %s token 0x%08x on image %s", class, fname, token, image->name));
+               return NULL;
+       }
+
        if (context) {
+               MonoError error;
                MonoMethodSignature *cached;
 
                /* This signature is not owned by a MonoMethod, so need to cache */
-               sig = inflate_generic_signature (image, sig, context);
+               sig = inflate_generic_signature_checked (image, sig, context, &error);
+               if (!mono_error_ok (&error)) {/*XXX bubble up this and kill one use of loader errors */
+                       mono_loader_set_error_bad_image (g_strdup_printf ("Could not inflate signature %s", mono_error_get_message (&error)));
+                       mono_error_cleanup (&error);
+                       return NULL;
+               }
+
                cached = mono_metadata_get_inflated_signature (sig, context);
                if (cached != sig)
                        mono_metadata_free_inflated_signature (sig);
@@ -836,7 +884,7 @@ mono_method_search_in_array_class (MonoClass *klass, const char *name, MonoMetho
        int i;
 
        mono_class_setup_methods (klass);
-
+       g_assert (!klass->exception_type); /*FIXME this should not fail, right?*/
        for (i = 0; i < klass->method.count; ++i) {
                MonoMethod *method = klass->methods [i];
                if (strcmp (method->name, name) == 0 && sig->param_count == method->signature->param_count)
@@ -1152,16 +1200,18 @@ static MonoDl*
 cached_module_load (const char *name, int flags, char **err)
 {
        MonoDl *res;
+
+       if (err)
+               *err = NULL;
        mono_loader_lock ();
        if (!global_module_map)
                global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
        res = g_hash_table_lookup (global_module_map, name);
        if (res) {
-               *err = NULL;
                mono_loader_unlock ();
                return res;
        }
-       res = mono_dl_open (name, flags, err);
+       res = mono_dl_open (name, flags, NULL);
        if (res)
                g_hash_table_insert (global_module_map, g_strdup (name), res);
        mono_loader_unlock ();
@@ -1490,7 +1540,7 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
            (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
                result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodPInvoke));
        else
-               result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodNormal));
+               result = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethod));
 
        mono_stats.method_count ++;
 
@@ -1719,12 +1769,12 @@ mono_free_method  (MonoMethod *method)
                mono_image_property_remove (method->klass->image, method);
 
                g_free ((char*)method->name);
-               if (mw->method.header) {
-                       g_free ((char*)mw->method.header->code);
-                       for (i = 0; i < mw->method.header->num_locals; ++i)
-                               g_free (mw->method.header->locals [i]);
-                       g_free (mw->method.header->clauses);
-                       g_free (mw->method.header);
+               if (mw->header) {
+                       g_free ((char*)mw->header->code);
+                       for (i = 0; i < mw->header->num_locals; ++i)
+                               g_free (mw->header->locals [i]);
+                       g_free (mw->header->clauses);
+                       g_free (mw->header);
                }
                g_free (mw->method_data);
                g_free (method->signature);
@@ -1850,6 +1900,8 @@ mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
                                if (dyn_specs [i]) {
                                        mspecs [i] = g_new0 (MonoMarshalSpec, 1);
                                        memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
+                                       mspecs [i]->data.custom_data.custom_name = g_strdup (dyn_specs [i]->data.custom_data.custom_name);
+                                       mspecs [i]->data.custom_data.cookie = g_strdup (dyn_specs [i]->data.custom_data.cookie);
                                }
                }
                return;
@@ -2044,10 +2096,10 @@ mono_loader_lock_is_owned_by_self (void)
 /**
  * mono_method_signature:
  *
- * Return the signature of the method M. On failure, returns NULL.
+ * Return the signature of the method M. On failure, returns NULL, and ERR is set.
  */
 MonoMethodSignature*
-mono_method_signature (MonoMethod *m)
+mono_method_signature_checked (MonoMethod *m, MonoError *error)
 {
        int idx;
        int size;
@@ -2060,6 +2112,8 @@ mono_method_signature (MonoMethod *m)
 
        /* We need memory barriers below because of the double-checked locking pattern */ 
 
+       mono_error_init (error);
+
        if (m->signature)
                return m->signature;
 
@@ -2111,6 +2165,7 @@ mono_method_signature (MonoMethod *m)
                /*TODO we should cache the failure result somewhere*/
                if (!mono_verifier_verify_method_signature (img, sig_offset, NULL)) {
                        mono_loader_unlock ();
+                       mono_error_set_method_load (error, m->klass, m->name, "");
                        return NULL;
                }
 
@@ -2119,6 +2174,7 @@ mono_method_signature (MonoMethod *m)
                signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
                if (!signature) {
                        mono_loader_unlock ();
+                       mono_error_set_method_load (error, m->klass, m->name, "");
                        return NULL;
                }
 
@@ -2129,16 +2185,18 @@ mono_method_signature (MonoMethod *m)
        /* Verify metadata consistency */
        if (signature->generic_param_count) {
                if (!container || !container->is_method) {
-                       g_warning ("Signature claims method has generic parameters, but generic_params table says it doesn't for method 0x%08x from image %s", idx, img->name);
+                       mono_loader_unlock ();
+                       mono_error_set_method_load (error, m->klass, m->name, "Signature claims method has generic parameters, but generic_params table says it doesn't for method 0x%08x from image %s", idx, img->name);
                        return NULL;
                }
                if (container->type_argc != signature->generic_param_count) {
-                       g_warning ("Inconsistent generic parameter count.  Signature says %d, generic_params table says %dfor method 0x%08x from image %s",
-                                signature->generic_param_count, container->type_argc, idx, img->name);
+                       mono_loader_unlock ();
+                       mono_error_set_method_load (error, m->klass, m->name, "Inconsistent generic parameter count.  Signature says %d, generic_params table says %d for method 0x%08x from image %s", signature->generic_param_count, container->type_argc, idx, img->name);
                        return NULL;
                }
        } else if (container && container->is_method && container->type_argc) {
-               g_warning ("generic_params table claims method has generic parameters, but signature says it doesn't for method 0x%08x from image %s", idx, img->name);
+               mono_loader_unlock ();
+               mono_error_set_method_load (error, m->klass, m->name, "generic_params table claims method has generic parameters, but signature says it doesn't for method 0x%08x from image %s", idx, img->name);
                return NULL;
        }
        if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
@@ -2168,7 +2226,8 @@ mono_method_signature (MonoMethod *m)
                case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
                case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
                default:
-                       g_warning ("unsupported calling convention : 0x%04x for method 0x%08x from image %s", piinfo->piflags, idx, img->name);
+                       mono_loader_unlock ();
+                       mono_error_set_method_load (error, m->klass, m->name, "unsupported calling convention : 0x%04x for method 0x%08x from image %s", piinfo->piflags, idx, img->name);
                        return NULL;
                }
                signature->call_convention = conv;
@@ -2181,6 +2240,26 @@ mono_method_signature (MonoMethod *m)
        return m->signature;
 }
 
+/**
+ * mono_method_signature:
+ *
+ * Return the signature of the method M. On failure, returns NULL.
+ */
+MonoMethodSignature*
+mono_method_signature (MonoMethod *m)
+{
+       MonoError error;
+       MonoMethodSignature *sig;
+
+       sig = mono_method_signature_checked (m, &error);
+       if (!sig) {
+               g_warning ("Could not load signature due to: %s", mono_error_get_message (&error));
+               mono_error_cleanup (&error);
+       }
+
+       return sig;
+}
+
 const char*
 mono_method_get_name (MonoMethod *method)
 {
@@ -2206,68 +2285,55 @@ mono_method_get_header (MonoMethod *method)
        guint32 rva;
        MonoImage* img;
        gpointer loc;
-       MonoMethodNormal* mn = (MonoMethodNormal*) method;
        MonoMethodHeader *header;
 
        if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
                return NULL;
 
-#ifdef G_LIKELY
-       if (G_LIKELY (mn->header))
-#else
-       if (mn->header)
-#endif
-               return mn->header;
-
        if (method->is_inflated) {
                MonoMethodInflated *imethod = (MonoMethodInflated *) method;
                MonoMethodHeader *header;
 
-               header = mono_method_get_header (imethod->declaring);
-
                mono_loader_lock ();
 
-               if (mn->header) {
+               if (imethod->header) {
                        mono_loader_unlock ();
-                       return mn->header;
+                       return imethod->header;
                }
 
-               mn->header = inflate_generic_header (header, mono_method_get_context (method));
+               header = mono_method_get_header (imethod->declaring);
+
+               imethod->header = inflate_generic_header (header, mono_method_get_context (method));
                mono_loader_unlock ();
-               return mn->header;
+               mono_metadata_free_mh (header);
+               return imethod->header;
+       }
+
+       if (method->wrapper_type != MONO_WRAPPER_NONE || method->sre_method) {
+               MonoMethodWrapper *mw = (MonoMethodWrapper *)method;
+               g_assert (mw->header);
+               return mw->header;
        }
 
        /* 
-        * Do most of the work outside the loader lock, to avoid assembly loader hook
-        * deadlocks.
+        * We don't need locks here: the new header is allocated from malloc memory
+        * and is not stored anywhere in the runtime, the user needs to free it.
         */
        g_assert (mono_metadata_token_table (method->token) == MONO_TABLE_METHOD);
        idx = mono_metadata_token_index (method->token);
        img = method->klass->image;
        rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
-       loc = mono_image_rva_map (img, rva);
-
-       g_assert (loc);
 
        if (!mono_verifier_verify_method_header (img, rva, NULL))
                return NULL;
 
-       header = mono_metadata_parse_mh_full (img, mono_method_get_generic_container (method), loc);
-
-       mono_loader_lock ();
-
-       if (mn->header) {
-               /* header is allocated from the image mempool, no need to free it */
-               mono_loader_unlock ();
-               return mn->header;
-       }
-
-       mono_memory_barrier ();
+       loc = mono_image_rva_map (img, rva);
+       if (!loc)
+               return NULL;
 
-       mn->header = header;
+       header = mono_metadata_parse_mh_full (img, mono_method_get_generic_container (method), loc);
 
-       mono_loader_unlock ();
-       return mn->header;
+       return header;
 }
 
 guint32
@@ -2290,6 +2356,8 @@ mono_method_get_index (MonoMethod *method) {
                return mono_metadata_token_index (method->token);
 
        mono_class_setup_methods (klass);
+       if (klass->exception_type)
+               return 0;
        for (i = 0; i < klass->method.count; ++i) {
                if (method == klass->methods [i]) {
                        if (klass->image->uncompressed_metadata)