[runtime] Use condvars instead of events for notifying the finalizer thread.
[mono.git] / mono / metadata / loader.c
index c3c9a5288e6d1f9b3b404767e67fe54237ea2495..05cc3f7f83358d9caa035fb1985d7caf4a648938 100644 (file)
@@ -103,6 +103,7 @@ mono_loader_init ()
                mono_native_tls_alloc (&loader_error_thread_id, NULL);
                mono_native_tls_alloc (&loader_lock_nest_id, NULL);
 
+               mono_counters_init ();
                mono_counters_register ("Inflated signatures size",
                                                                MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_signatures_size);
                mono_counters_register ("Memberref signature cache size",
@@ -447,7 +448,7 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
        MonoTableInfo *tables = image->tables;
        MonoType *sig_type;
        guint32 cols[6];
-       guint32 nindex, class, class_table;
+       guint32 nindex, class;
        const char *fname;
        const char *ptr;
        guint32 idx = mono_metadata_token_index (token);
@@ -467,15 +468,12 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
 
        switch (class) {
        case MONO_MEMBERREF_PARENT_TYPEDEF:
-               class_table = MONO_TOKEN_TYPE_DEF;
                klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | nindex, error);
                break;
        case MONO_MEMBERREF_PARENT_TYPEREF:
-               class_table = MONO_TOKEN_TYPE_REF;
                klass = mono_class_from_typeref_checked (image, MONO_TOKEN_TYPE_REF | nindex, error);
                break;
        case MONO_MEMBERREF_PARENT_TYPESPEC:
-               class_table = MONO_TOKEN_TYPE_SPEC;
                klass = mono_class_get_and_inflate_typespec_checked (image, MONO_TOKEN_TYPE_SPEC | nindex, context, error);
                break;
        default:
@@ -585,7 +583,7 @@ mono_field_from_token_checked (MonoImage *image, guint32 token, MonoClass **retk
                field = mono_class_get_field (k, token);
                if (!field) {
                        if (mono_loader_get_last_error ())
-                               mono_loader_set_error_from_mono_error (error);
+                               mono_error_set_from_loader_error (error);
                        else
                                mono_error_set_bad_image (error, image, "Could not resolve field token 0x%08x", token);
                }
@@ -625,11 +623,12 @@ mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSigna
 
 static MonoMethod *
 find_method_in_class (MonoClass *klass, const char *name, const char *qname, const char *fqname,
-                     MonoMethodSignature *sig, MonoClass *from_class)
+                     MonoMethodSignature *sig, MonoClass *from_class, MonoError *error)
 {
        int i;
 
        /* Search directly in the metadata to avoid calling setup_methods () */
+       mono_error_init (error);
 
        /* FIXME: !from_class->generic_class condition causes test failures. */
        if (klass->type_token && !image_is_dynamic (klass->image) && !klass->methods && !klass->rank && klass == from_class && !from_class->generic_class) {
@@ -648,23 +647,30 @@ find_method_in_class (MonoClass *klass, const char *name, const char *qname, con
                                  (name && !strcmp (m_name, name))))
                                continue;
 
-                       method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
+                       method = mono_get_method_checked (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass, NULL, error);
+                       if (!mono_error_ok (error)) //bail out if we hit a loader error
+                               return NULL;
                        if (method) {
-                               other_sig = mono_method_signature (method);
+                               other_sig = mono_method_signature_checked (method, error);
+                               if (!mono_error_ok (error)) //bail out if we hit a loader error
+                                       return NULL;                            
                                if (other_sig && (sig->call_convention != MONO_CALL_VARARG) && mono_metadata_signature_equal (sig, other_sig))
                                        return method;
                        }
                }
        }
 
-       mono_class_setup_methods (klass);
+       mono_class_setup_methods (klass); /* FIXME don't swallow the error here. */
        /*
        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)
+       if (!klass->methods || klass->exception_type) {
+               mono_error_set_type_load_class (error, klass, "Could not find method due to a type load error"); //FIXME get the error from the class 
+
                return NULL;
+       }
        for (i = 0; i < klass->method.count; ++i) {
                MonoMethod *m = klass->methods [i];
                MonoMethodSignature *msig;
@@ -677,7 +683,10 @@ find_method_in_class (MonoClass *klass, const char *name, const char *qname, con
                      (qname && !strcmp (m->name, qname)) ||
                      (name && !strcmp (m->name, name))))
                        continue;
-               msig = mono_method_signature (m);
+               msig = mono_method_signature_checked (m, error);
+               if (!mono_error_ok (error)) //bail out if we hit a loader error
+                       return NULL;
+
                if (!msig)
                        continue;
 
@@ -696,13 +705,15 @@ find_method_in_class (MonoClass *klass, const char *name, const char *qname, con
 }
 
 static MonoMethod *
-find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class)
+find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSignature *sig, MonoClass *from_class, MonoError *error)
 {
        int i;
        char *qname, *fqname, *class_name;
        gboolean is_interface;
        MonoMethod *result = NULL;
+       MonoClass *initial_class = in_class;
 
+       mono_error_init (error);
        is_interface = MONO_CLASS_IS_INTERFACE (in_class);
 
        if (ic) {
@@ -718,8 +729,8 @@ find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSig
 
        while (in_class) {
                g_assert (from_class);
-               result = find_method_in_class (in_class, name, qname, fqname, sig, from_class);
-               if (result)
+               result = find_method_in_class (in_class, name, qname, fqname, sig, from_class, error);
+               if (result || !mono_error_ok (error))
                        goto out;
 
                if (name [0] == '.' && (!strcmp (name, ".ctor") || !strcmp (name, ".cctor")))
@@ -747,11 +758,11 @@ find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSig
                        else
                                ic_fqname = NULL;
 
-                       result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic);
+                       result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic, error);
                        g_free (ic_class_name);
                        g_free (ic_fqname);
                        g_free (ic_qname);
-                       if (result)
+                       if (result || !mono_error_ok (error))
                                goto out;
                }
 
@@ -761,8 +772,15 @@ find_method (MonoClass *in_class, MonoClass *ic, const char* name, MonoMethodSig
        g_assert (!in_class == !from_class);
 
        if (is_interface)
-               result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class);
+               result = find_method_in_class (mono_defaults.object_class, name, qname, fqname, sig, mono_defaults.object_class, error);
 
+       //we did not find the method
+       if (!result && mono_error_ok (error)) {
+               char *desc = mono_signature_get_desc (sig, FALSE);
+               mono_error_set_method_load (error, initial_class, name, "Could not find method with signature %s", desc);
+               g_free (desc);
+       }
+               
  out:
        g_free (class_name);
        g_free (fqname);
@@ -930,17 +948,11 @@ mono_method_get_signature_checked (MonoMethod *method, MonoImage *image, guint32
 
                        ptr = mono_metadata_blob_heap (image, sig_idx);
                        mono_metadata_decode_blob_size (ptr, &ptr);
-                       /* FIXME make type/signature parsing not produce loader errors */
-                       sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
-                       g_assert (!mono_loader_get_last_error ());
 
-                       if (!sig) {
-                               guint32 class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
-                               const char *fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
-                               //FIXME include the decoding error
-                               mono_error_set_bad_image (error, image, "Bad method signature class token 0x%08x field name %s token 0x%08x", class, fname, token);
+                       sig = mono_metadata_parse_method_signature_full (image, NULL, 0, ptr, NULL, error);
+                       if (!sig)
                                return NULL;
-                       }
+
                        sig = cache_memberref_sig (image, sig_idx, sig);
                }
                /* FIXME: we probably should verify signature compat in the dynamic case too*/
@@ -1007,7 +1019,7 @@ mono_method_search_in_array_class (MonoClass *klass, const char *name, MonoMetho
 
 static MonoMethod *
 method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typespec_context,
-                      gboolean *used_context)
+                      gboolean *used_context, MonoError *error)
 {
        MonoClass *klass = NULL;
        MonoMethod *method = NULL;
@@ -1017,7 +1029,8 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
        const char *mname;
        MonoMethodSignature *sig;
        const char *ptr;
-       MonoError error;
+
+       mono_error_init (error);
 
        mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
        nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
@@ -1037,52 +1050,42 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
 
        switch (class) {
        case MONO_MEMBERREF_PARENT_TYPEREF:
-               klass = mono_class_from_typeref_checked (image, MONO_TOKEN_TYPE_REF | nindex, &error);
-               if (!klass) {
-                       mono_loader_set_error_from_mono_error (&error);
-                       mono_error_cleanup (&error); /* FIXME Don't swallow the error */
-                       return NULL;
-               }
+               klass = mono_class_from_typeref_checked (image, MONO_TOKEN_TYPE_REF | nindex, error);
+               if (!klass)
+                       goto fail;
                break;
        case MONO_MEMBERREF_PARENT_TYPESPEC:
                /*
                 * Parse the TYPESPEC in the parent's context.
                 */
-               klass = mono_class_get_and_inflate_typespec_checked (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context, &error);
-               if (!klass) {
-                       mono_loader_set_error_from_mono_error (&error);
-                       mono_error_cleanup (&error); /*FIXME don't swallow the error message*/
-                       return NULL;
-               }
+               klass = mono_class_get_and_inflate_typespec_checked (image, MONO_TOKEN_TYPE_SPEC | nindex, typespec_context, error);
+               if (!klass)
+                       goto fail;
                break;
        case MONO_MEMBERREF_PARENT_TYPEDEF:
-               klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | nindex, &error);
-               if (!klass) {
-                       mono_loader_set_error_from_mono_error (&error);
-                       mono_error_cleanup (&error); /*FIXME don't swallow the error message*/
-                       return NULL;
-               }
+               klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | nindex, error);
+               if (!klass)
+                       goto fail;
                break;
-       case MONO_MEMBERREF_PARENT_METHODDEF:
-               return mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
-               
+       case MONO_MEMBERREF_PARENT_METHODDEF: {
+               method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, NULL, error);
+               if (!method)
+                       goto fail;
+               return method;
+       }
        default:
-               {
-                       /* This message leaks */
-                       char *message = g_strdup_printf ("Memberref parent unknown: class: %d, index %d", class, nindex);
-                       mono_loader_set_error_method_load ("", message);
-                       return NULL;
-               }
-
+               mono_error_set_bad_image (error, image, "Memberref parent unknown: class: %d, index %d", class, nindex);
+               goto fail;
        }
+
        g_assert (klass);
        mono_class_init (klass);
 
        sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
 
        if (!mono_verifier_verify_memberref_method_signature (image, sig_idx, NULL)) {
-               mono_loader_set_error_method_load (klass->name, mname);
-               return NULL;
+               mono_error_set_method_load (error, klass, mname, "Verifier rejected method signature");
+               goto fail;
        }
 
        ptr = mono_metadata_blob_heap (image, sig_idx);
@@ -1090,9 +1093,9 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
 
        sig = find_cached_memberref_sig (image, sig_idx);
        if (!sig) {
-               sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
+               sig = mono_metadata_parse_method_signature_full (image, NULL, 0, ptr, NULL, error);
                if (sig == NULL)
-                       return NULL;
+                       goto fail;
 
                sig = cache_memberref_sig (image, sig_idx, sig);
        }
@@ -1100,7 +1103,7 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
        switch (class) {
        case MONO_MEMBERREF_PARENT_TYPEREF:
        case MONO_MEMBERREF_PARENT_TYPEDEF:
-               method = find_method (klass, NULL, mname, sig, klass);
+               method = find_method (klass, NULL, mname, sig, klass, error);
                break;
 
        case MONO_MEMBERREF_PARENT_TYPESPEC: {
@@ -1110,7 +1113,7 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
 
                if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
                        MonoClass *in_class = klass->generic_class ? klass->generic_class->container_class : klass;
-                       method = find_method (in_class, NULL, mname, sig, klass);
+                       method = find_method (in_class, NULL, mname, sig, klass, error);
                        break;
                }
 
@@ -1119,13 +1122,12 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
                break;
        }
        default:
-               g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
-               g_assert_not_reached ();
+               mono_error_set_bad_image (error, image,"Memberref parent unknown: class: %d, index %d", class, nindex);
+               goto fail;
        }
 
-       if (!method) {
+       if (!method && mono_error_ok (error)) {
                char *msig = mono_signature_get_desc (sig, FALSE);
-               char * class_name = mono_type_get_name (&klass->byval_arg);
                GString *s = g_string_new (mname);
                if (sig->generic_param_count)
                        g_string_append_printf (s, "<[%d]>", sig->generic_param_count);
@@ -1133,21 +1135,26 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
                g_free (msig);
                msig = g_string_free (s, FALSE);
 
-               g_warning (
-                       "Missing method %s::%s in assembly %s, referenced in assembly %s",
-                       class_name, msig, klass->image->name, image->name);
-               mono_loader_set_error_method_load (class_name, mname);
+               if (mono_loader_get_last_error ()) /* FIXME find_method and mono_method_search_in_array_class can leak a loader error */
+                       mono_error_set_from_loader_error (error);
+               else
+                       mono_error_set_method_load (error, klass, mname, "Could not find method %s", msig);
+
                g_free (msig);
-               g_free (class_name);
        }
 
+       g_assert (!mono_loader_get_last_error ());
        return method;
+
+fail:
+       g_assert (!mono_loader_get_last_error ());
+       g_assert (!mono_error_ok (error));
+       return NULL;
 }
 
 static MonoMethod *
-method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
+method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx, MonoError *error)
 {
-       MonoError error;
        MonoMethod *method;
        MonoClass *klass;
        MonoTableInfo *tables = image->tables;
@@ -1157,12 +1164,16 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
        guint32 cols [MONO_METHODSPEC_SIZE];
        guint32 token, nindex, param_count;
 
+       mono_error_init (error);
+
        mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
        token = cols [MONO_METHODSPEC_METHOD];
        nindex = token >> MONO_METHODDEFORREF_BITS;
 
-       if (!mono_verifier_verify_methodspec_signature (image, cols [MONO_METHODSPEC_SIGNATURE], NULL))
+       if (!mono_verifier_verify_methodspec_signature (image, cols [MONO_METHODSPEC_SIGNATURE], NULL)) {
+               mono_error_set_bad_image (error, image, "Bad method signals signature 0x%08x", idx);
                return NULL;
+       }
 
        ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
 
@@ -1171,21 +1182,25 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
        param_count = mono_metadata_decode_value (ptr, &ptr);
 
        inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
-       if (!inst)
+       if (!inst) {
+               g_assert (!mono_loader_get_last_error ());
+               mono_error_set_bad_image (error, image, "Cannot parse generic instance for methodspec 0x%08x", idx);
                return NULL;
+       }
 
        if (context && inst->is_open) {
-               inst = mono_metadata_inflate_generic_inst (inst, context, &error);
-               if (!mono_error_ok (&error)) {
-                       mono_error_cleanup (&error); /*FIXME don't swallow error message.*/
+               inst = mono_metadata_inflate_generic_inst (inst, context, error);
+               if (!mono_error_ok (error))
                        return NULL;
-               }
        }
 
-       if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
-               method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
-       else
-               method = method_from_memberref (image, nindex, context, NULL);
+       if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF) {
+               method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context, error);
+               if (!method)
+                       return NULL;
+       } else {
+               method = method_from_memberref (image, nindex, context, NULL, error);
+       }
 
        if (!method)
                return NULL;
@@ -1200,7 +1215,9 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
        new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
        new_context.method_inst = inst;
 
-       return mono_class_inflate_generic_method_full (method, klass, &new_context);
+       method = mono_class_inflate_generic_method_full_checked (method, klass, &new_context, error);
+       g_assert (!mono_loader_get_last_error ());
+       return method;
 }
 
 struct _MonoDllMap {
@@ -1715,12 +1732,16 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                                                                "Probing '%s'.", mangled_name2);
 
                                        error_msg = mono_dl_symbol (module, mangled_name2, &piinfo->addr);
-                                       g_free (error_msg);
-                                       error_msg = NULL;
 
                                        if (piinfo->addr)
                                                mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
                                                                        "Found as '%s'.", mangled_name2);
+                                       else
+                                               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+                                                                       "Could not find '%s' due to '%s'.", mangled_name2, error_msg);
+
+                                       g_free (error_msg);
+                                       error_msg = NULL;
 
                                        if (mangled_name != mangled_name2)
                                                g_free (mangled_name2);
@@ -1747,25 +1768,27 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
  */
 static MonoMethod *
 mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
-                           MonoGenericContext *context, gboolean *used_context)
+                           MonoGenericContext *context, gboolean *used_context, MonoError *error)
 {
-       MonoError error;
        MonoMethod *result;
        int table = mono_metadata_token_table (token);
        int idx = mono_metadata_token_index (token);
        MonoTableInfo *tables = image->tables;
        MonoGenericContainer *generic_container = NULL, *container = NULL;
        const char *sig = NULL;
-       int size;
        guint32 cols [MONO_TYPEDEF_SIZE];
 
+       mono_error_init (error);
+
        if (image_is_dynamic (image)) {
                MonoClass *handle_class;
 
                result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
+               g_assert (!mono_loader_get_last_error ());
+
                // This checks the memberref type as well
                if (result && handle_class != mono_defaults.methodhandle_class) {
-                       mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
+                       mono_error_set_bad_image (error, image, "Bad method token 0x%08x on dynamic image", token);
                        return NULL;
                }
                return result;
@@ -1774,33 +1797,31 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
        if (table != MONO_TABLE_METHOD) {
                if (table == MONO_TABLE_METHODSPEC) {
                        if (used_context) *used_context = TRUE;
-                       return method_from_methodspec (image, context, idx);
+                       return method_from_methodspec (image, context, idx, error);
                }
                if (table != MONO_TABLE_MEMBERREF) {
-                       g_warning ("got wrong token: 0x%08x\n", token);
-                       mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
+                       mono_error_set_bad_image (error, image, "Bad method token 0x%08x.", token);
                        return NULL;
                }
-               return method_from_memberref (image, idx, context, used_context);
+               return method_from_memberref (image, idx, context, used_context, error);
        }
 
        if (used_context) *used_context = FALSE;
 
        if (idx > image->tables [MONO_TABLE_METHOD].rows) {
-               mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
+               mono_error_set_bad_image (error, image, "Bad method token 0x%08x (out of bounds).", token);
                return NULL;
        }
 
        if (!klass) {
                guint32 type = mono_metadata_typedef_from_method (image, token);
-               if (!type)
-                       return NULL;
-               klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | type, &error);
-               if (klass == NULL) {
-                       mono_loader_set_error_from_mono_error (&error);
-                       mono_error_cleanup (&error); /*FIXME don't swallow the error message*/
+               if (!type) {
+                       mono_error_set_bad_image (error, image, "Bad method token 0x%08x (could not find corresponding typedef).", token);
                        return NULL;
                }
+               klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | type, error);
+               if (klass == NULL)
+                       return NULL;
        }
 
        mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
@@ -1824,7 +1845,7 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
 
        if (!sig) /* already taken from the methodref */
                sig = mono_metadata_blob_heap (image, cols [4]);
-       size = mono_metadata_decode_blob_size (sig, &sig);
+       /* size = */ mono_metadata_decode_blob_size (sig, &sig);
 
        container = klass->generic_container;
 
@@ -1832,18 +1853,16 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
         * load_generic_params does a binary search so only call it if the method 
         * is generic.
         */
-       if (*sig & 0x10)
+       if (*sig & 0x10) {
                generic_container = mono_metadata_load_generic_params (image, token, container);
+               g_assert (!mono_loader_get_last_error ()); /* FIXME don't swallow this error. */
+       }
        if (generic_container) {
-               MonoError error;
                result->is_generic = TRUE;
                generic_container->owner.method = result;
                /*FIXME put this before the image alloc*/
-               if (!mono_metadata_load_generic_param_constraints_checked (image, token, generic_container, &error)) {
-                       mono_loader_set_error_from_mono_error (&error);
-                       mono_error_cleanup (&error); /*FIXME don't swallow the error message*/
+               if (!mono_metadata_load_generic_param_constraints_checked (image, token, generic_container, error))
                        return NULL;
-               }
 
                container = generic_container;
        }
@@ -1870,6 +1889,7 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
        if (generic_container)
                mono_method_set_generic_container (result, generic_container);
 
+       g_assert (!mono_loader_get_last_error ());
        return result;
 }
 
@@ -1882,12 +1902,27 @@ mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
 MonoMethod *
 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
                      MonoGenericContext *context)
+{
+       MonoError error;
+       MonoMethod *result = mono_get_method_checked (image, token, klass, context, &error);
+       g_assert (!mono_loader_get_last_error ());
+       if (!mono_error_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error);
+               mono_error_cleanup (&error);
+       }
+       return result;
+}
+
+MonoMethod *
+mono_get_method_checked (MonoImage *image, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
 {
        MonoMethod *result = NULL;
        gboolean used_context = FALSE;
 
        /* We do everything inside the lock to prevent creation races */
 
+       mono_error_init (error);
+
        mono_image_lock (image);
 
        if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
@@ -1905,7 +1940,7 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
                return result;
 
 
-       result = mono_get_method_from_token (image, token, klass, context, &used_context);
+       result = mono_get_method_from_token (image, token, klass, context, &used_context, error);
        if (!result)
                return NULL;
 
@@ -1935,22 +1970,26 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
 }
 
 static MonoMethod *
-get_method_constrained (MonoImage *image, MonoMethod *method, MonoClass *constrained_class, MonoGenericContext *context)
+get_method_constrained (MonoImage *image, MonoMethod *method, MonoClass *constrained_class, MonoGenericContext *context, MonoError *error)
 {
        MonoMethod *result;
        MonoClass *ic = NULL;
        MonoGenericContext *method_context = NULL;
        MonoMethodSignature *sig, *original_sig;
 
+       mono_error_init (error);
+
        mono_class_init (constrained_class);
-       original_sig = sig = mono_method_signature (method);
+       original_sig = sig = mono_method_signature_checked (method, error);
        if (sig == NULL) {
                return NULL;
        }
 
        if (method->is_inflated && sig->generic_param_count) {
                MonoMethodInflated *imethod = (MonoMethodInflated *) method;
-               sig = mono_method_signature (imethod->declaring); /*We assume that if the inflated method signature is valid, the declaring method is too*/
+               sig = mono_method_signature_checked (imethod->declaring, error); /*We assume that if the inflated method signature is valid, the declaring method is too*/
+               if (!sig)
+                       return NULL;
                method_context = mono_method_get_context (method);
 
                original_sig = sig;
@@ -1960,47 +1999,42 @@ get_method_constrained (MonoImage *image, MonoMethod *method, MonoClass *constra
                 * any type argument which a concrete type. See #325283.
                 */
                if (method_context->class_inst) {
-                       MonoError error;
                        MonoGenericContext ctx;
                        ctx.method_inst = NULL;
                        ctx.class_inst = method_context->class_inst;
                        /*Fixme, property propagate this error*/
-                       sig = inflate_generic_signature_checked (method->klass->image, sig, &ctx, &error);
-                       if (!mono_error_ok (&error)) {
-                               mono_error_cleanup (&error);
+                       sig = inflate_generic_signature_checked (method->klass->image, sig, &ctx, error);
+                       if (!sig)
                                return NULL;
-                       }
                }
        }
 
        if ((constrained_class != method->klass) && (MONO_CLASS_IS_INTERFACE (method->klass)))
                ic = method->klass;
 
-       result = find_method (constrained_class, ic, method->name, sig, constrained_class);
+       result = find_method (constrained_class, ic, method->name, sig, constrained_class, error);
        if (sig != original_sig)
                mono_metadata_free_inflated_signature (sig);
 
-       if (!result) {
-               char *m = mono_method_full_name (method, 1);
-               g_warning ("Missing method %s.%s.%s in assembly %s method %s", method->klass->name_space,
-                          method->klass->name, method->name, image->name, m);
-               g_free (m);
+       if (!result)
                return NULL;
-       }
 
-       if (method_context)
-               result = mono_class_inflate_generic_method (result, method_context);
+       if (method_context) {
+               result = mono_class_inflate_generic_method_checked (result, method_context, error);
+               if (!result)
+                       return NULL;
+       }
 
        return result;
 }
 
 MonoMethod *
 mono_get_method_constrained_with_method (MonoImage *image, MonoMethod *method, MonoClass *constrained_class,
-                            MonoGenericContext *context)
+                            MonoGenericContext *context, MonoError *error)
 {
        g_assert (method);
 
-       return get_method_constrained (image, method, constrained_class, context);
+       return get_method_constrained (image, method, constrained_class, context, error);
 }
 
 /**
@@ -2016,15 +2050,27 @@ MonoMethod *
 mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
                             MonoGenericContext *context, MonoMethod **cil_method)
 {
-       MonoMethod *result;
+       MonoError error;
+       MonoMethod *result = mono_get_method_constrained_checked (image, token, constrained_class, context, cil_method, &error);
 
-       *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL);
+       g_assert (!mono_loader_get_last_error ());
+       if (!mono_error_ok (&error)) {
+               mono_loader_set_error_from_mono_error (&error);
+               mono_error_cleanup (&error);
+       }
+       return result;
+}
+
+MonoMethod *
+mono_get_method_constrained_checked (MonoImage *image, guint32 token, MonoClass *constrained_class, MonoGenericContext *context, MonoMethod **cil_method, MonoError *error)
+{
+       mono_error_init (error);
+
+       *cil_method = mono_get_method_from_token (image, token, NULL, context, NULL, error);
        if (!*cil_method)
                return NULL;
 
-       result = get_method_constrained (image, *cil_method, constrained_class, context);
-
-       return result;
+       return get_method_constrained (image, *cil_method, constrained_class, context, error);
 }
 
 void
@@ -2409,7 +2455,10 @@ static gboolean loader_lock_track_ownership = FALSE;
 void
 mono_loader_lock (void)
 {
+       MONO_TRY_BLOCKING
        mono_locks_acquire (&loader_mutex, LoaderLock);
+       MONO_FINISH_TRY_BLOCKING
+               
        if (G_UNLIKELY (loader_lock_track_ownership)) {
                mono_native_tls_set_value (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (mono_native_tls_get_value (loader_lock_nest_id)) + 1));
        }
@@ -2480,7 +2529,6 @@ MonoMethodSignature*
 mono_method_signature_checked (MonoMethod *m, MonoError *error)
 {
        int idx;
-       int size;
        MonoImage* img;
        const char *sig;
        gboolean can_cache_signature;
@@ -2548,13 +2596,11 @@ mono_method_signature_checked (MonoMethod *m, MonoError *error)
                if (!mono_verifier_verify_method_signature (img, sig_offset, error))
                        return NULL;
 
-               size = mono_metadata_decode_blob_size (sig, &sig_body);
+               /* size = */ mono_metadata_decode_blob_size (sig, &sig_body);
 
-               signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL);
-               if (!signature) {
-                       mono_error_set_from_loader_error (error);
+               signature = mono_metadata_parse_method_signature_full (img, container, idx, sig_body, NULL, error);
+               if (!signature)
                        return NULL;
-               }
 
                if (can_cache_signature) {
                        mono_image_lock (img);