2010-05-29 Robert Jordan <robertj@gmx.net>
[mono.git] / mono / metadata / loader.c
index bb1c2ab1e87d12783256ddd2a4a518c264d0ad30..ea3eada007156034d16264f5a35d915eb29ede57 100644 (file)
 #include <mono/metadata/profiler-private.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/marshal.h>
-#include <mono/utils/mono-logger.h>
+#include <mono/metadata/lock-tracer.h>
+#include <mono/metadata/verify-internals.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;
 
@@ -61,6 +64,12 @@ static guint32 memberref_sig_cache_size;
  */
 guint32 loader_error_thread_id;
 
+/*
+ * This TLS variable holds how many times the current thread has acquired the loader 
+ * lock.
+ */
+guint32 loader_lock_nest_id;
+
 void
 mono_loader_init ()
 {
@@ -70,6 +79,7 @@ mono_loader_init ()
                InitializeCriticalSection (&loader_mutex);
 
                loader_error_thread_id = TlsAlloc ();
+               loader_lock_nest_id = TlsAlloc ();
 
                mono_counters_register ("Inflated signatures size",
                                                                MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_signatures_size);
@@ -84,6 +94,7 @@ void
 mono_loader_cleanup (void)
 {
        TlsFree (loader_error_thread_id);
+       TlsFree (loader_lock_nest_id);
 
        /*DeleteCriticalSection (&loader_mutex);*/
 }
@@ -160,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);
 }
@@ -304,8 +315,8 @@ mono_loader_error_prepare_exception (MonoLoaderError *error)
        }
                
        case MONO_EXCEPTION_MISSING_FIELD: {
-               char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
-               char *cname = g_strdup (error->klass->name);
+               char *cnspace = g_strdup ((error->klass && *error->klass->name_space) ? error->klass->name_space : "");
+               char *cname = g_strdup (error->klass ? error->klass->name : "");
                char *cmembername = g_strdup (error->member_name);
                 char *class_name;
 
@@ -403,11 +414,11 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
                      MonoGenericContext *context)
 {
        MonoClass *klass;
-       MonoClassField *field, *sig_field = NULL;
+       MonoClassField *field;
        MonoTableInfo *tables = image->tables;
        MonoType *sig_type;
        guint32 cols[6];
-       guint32 nindex, class;
+       guint32 nindex, class, class_table;
        const char *fname;
        const char *ptr;
        guint32 idx = mono_metadata_token_index (token);
@@ -418,74 +429,67 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
 
        fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
 
-       ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
-       mono_metadata_decode_blob_size (ptr, &ptr);
-       /* we may want to check the signature here... */
-
-       if (*ptr++ != 0x6) {
-               mono_loader_set_error_bad_image (g_strdup_printf ("Bad field signature class token %08x field name %s token %08x", class, fname, token));
+       if (!mono_verifier_verify_memberref_signature (image, cols [MONO_MEMBERREF_SIGNATURE], NULL)) {
+               mono_loader_set_error_bad_image (g_strdup_printf ("Bad field signature class token 0x%08x field name %s token 0x%08x on image %s", class, fname, token, image->name));
                return NULL;
        }
-       /* FIXME: This needs a cache, especially for generic instances, since
-        * mono_metadata_parse_type () allocates everything from a mempool.
-        */
-       sig_type = find_cached_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE]);
-       if (!sig_type) {
-               sig_type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
-               sig_type = cache_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE], sig_type);
-       }
 
        switch (class) {
        case MONO_MEMBERREF_PARENT_TYPEDEF:
+               class_table = MONO_TOKEN_TYPE_DEF;
                klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
-               if (!klass) {
-                       char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
-                       g_warning ("Missing field %s in class %s (typedef index %d)", fname, name, nindex);
-                       mono_loader_set_error_type_load (name, image->assembly_name);
-                       g_free (name);
-                       return NULL;
-               }
-               mono_class_init (klass);
-               if (retklass)
-                       *retklass = klass;
-               sig_field = field = mono_class_get_field_from_name (klass, fname);
                break;
        case MONO_MEMBERREF_PARENT_TYPEREF:
+               class_table = MONO_TOKEN_TYPE_REF;
                klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
-               if (!klass) {
-                       char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
-                       g_warning ("missing field %s in class %s (typeref index %d)", fname, name, nindex);
-                       mono_loader_set_error_type_load (name, image->assembly_name);
-                       g_free (name);
-                       return NULL;
-               }
-               mono_class_init (klass);
-               if (retklass)
-                       *retklass = klass;
-               sig_field = field = mono_class_get_field_from_name (klass, fname);
                break;
-       case MONO_MEMBERREF_PARENT_TYPESPEC: {
+       case MONO_MEMBERREF_PARENT_TYPESPEC:
+               class_table = MONO_TOKEN_TYPE_SPEC;
                klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
-               //FIXME can't klass be null?
-               mono_class_init (klass);
-               if (retklass)
-                       *retklass = klass;
-               field = mono_class_get_field_from_name (klass, fname);
-               if (field)
-                       sig_field = mono_metadata_get_corresponding_field_from_generic_type_definition (field);
                break;
-       }
        default:
+               /*FIXME this must set a loader error!*/
                g_warning ("field load from %x", class);
                return NULL;
        }
 
-       if (!field)
-               mono_loader_set_error_field_load (klass, fname);
-       else if (sig_field && !mono_metadata_type_equal_full (sig_type, sig_field->type, TRUE)) {
+       if (!klass) {
+               char *name = mono_class_name_from_token (image, class_table | nindex);
+               g_warning ("Missing field %s in class %s (type token %d)", fname, name, class_table | nindex);
+               mono_loader_set_error_type_load (name, image->assembly_name);
+               g_free (name);
+               return NULL;
+       }
+
+       ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
+       mono_metadata_decode_blob_size (ptr, &ptr);
+       /* we may want to check the signature here... */
+
+       if (*ptr++ != 0x6) {
+               g_warning ("Bad field signature class token %08x field name %s token %08x", class, fname, token);
                mono_loader_set_error_field_load (klass, fname);
                return NULL;
        }
+       /* FIXME: This needs a cache, especially for generic instances, since
+        * mono_metadata_parse_type () allocates everything from a mempool.
+        */
+       sig_type = find_cached_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE]);
+       if (!sig_type) {
+               sig_type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
+               if (sig_type == NULL) {
+                       mono_loader_set_error_field_load (klass, fname);
+                       return NULL;
+               }
+               sig_type = cache_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE], sig_type);
+       }
+
+       mono_class_init (klass); /*FIXME is this really necessary?*/
+       if (retklass)
+               *retklass = klass;
+       field = mono_class_get_field_from_name_full (klass, fname, sig_type);
+
+       if (!field)
+               mono_loader_set_error_field_load (klass, fname);
 
        return field;
 }
@@ -505,8 +509,8 @@ mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
                *retklass = NULL;
                result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
                // This checks the memberref type as well
-               if (result && handle_class != mono_defaults.fieldhandle_class) {
-                       mono_loader_set_error_bad_image (g_strdup ("Bad field token."));
+               if (!result || handle_class != mono_defaults.fieldhandle_class) {
+                       mono_loader_set_error_bad_image (g_strdup_printf ("Bad field token 0x%08x on image %s.", token, image->name));
                        return NULL;
                }
                *retklass = result->parent;
@@ -537,7 +541,7 @@ mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
        }
 
        mono_loader_lock ();
-       if (field && !field->parent->generic_class && !field->parent->generic_container)
+       if (field && field->parent && !field->parent->generic_class && !field->parent->generic_container)
                g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
        mono_loader_unlock ();
        return field;
@@ -582,6 +586,7 @@ find_method_in_class (MonoClass *klass, const char *name, const char *qname, con
                        guint32 cols [MONO_METHOD_SIZE];
                        MonoMethod *method;
                        const char *m_name;
+                       MonoMethodSignature *other_sig;
 
                        mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
 
@@ -593,25 +598,37 @@ find_method_in_class (MonoClass *klass, const char *name, const char *qname, con
                                continue;
 
                        method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
-                       if (method && (sig->call_convention != MONO_CALL_VARARG) && mono_metadata_signature_equal (sig, mono_method_signature (method)))
+                       other_sig = mono_method_signature (method);
+                       if (method && other_sig && (sig->call_convention != MONO_CALL_VARARG) && mono_metadata_signature_equal (sig, other_sig))
                                return method;
                }
        }
 
        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;
 
                if (!((fqname && !strcmp (m->name, fqname)) ||
                      (qname && !strcmp (m->name, qname)) ||
                      (name && !strcmp (m->name, name))))
                        continue;
+               msig = mono_method_signature (m);
+               if (!msig)
+                       continue;
 
                if (sig->call_convention == MONO_CALL_VARARG) {
-                       if (mono_metadata_signature_vararg_match (sig, mono_method_signature (m)))
+                       if (mono_metadata_signature_vararg_match (sig, msig))
                                break;
                } else {
-                       if (mono_metadata_signature_equal (sig, mono_method_signature (m)))
+                       if (mono_metadata_signature_equal (sig, msig))
                                break;
                }
        }
@@ -688,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 (sizeof (MonoMethodSignature) + ((gint32)sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
+       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]);
        }
@@ -716,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*
@@ -723,7 +765,7 @@ inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
 {
        MonoMethodHeader *res;
        int i;
-       res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
+       res = g_malloc0 (MONO_SIZEOF_METHOD_HEADER + sizeof (gpointer) * header->num_locals);
        res->code = header->code;
        res->code_size = header->code_size;
        res->max_stack = header->max_stack;
@@ -746,7 +788,7 @@ inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
 }
 
 /*
- * token is the method_ref or method_def token used in a call IL instruction.
+ * token is the method_ref/def/spec token used in a call IL instruction.
  */
 MonoMethodSignature*
 mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context)
@@ -763,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);
        }
@@ -782,18 +824,42 @@ mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 to
 
        sig = find_cached_memberref_sig (image, sig_idx);
        if (!sig) {
+               if (!mono_verifier_verify_memberref_signature (image, sig_idx, NULL)) {
+                       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 ("Bad method signature class token 0x%08x field name %s token 0x%08x on image %s", class, fname, token, image->name));
+                       return NULL;
+               }
+
                ptr = mono_metadata_blob_heap (image, sig_idx);
                mono_metadata_decode_blob_size (ptr, &ptr);
                sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
-
+               if (!sig)
+                       return NULL;
                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);
@@ -818,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)
@@ -907,6 +973,11 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
 
        sig_idx = cols [MONO_MEMBERREF_SIGNATURE];
 
+       if (!mono_verifier_verify_memberref_signature (image, sig_idx, NULL)) {
+               mono_loader_set_error_method_load (klass->name, mname);
+               return NULL;
+       }
+
        ptr = mono_metadata_blob_heap (image, sig_idx);
        mono_metadata_decode_blob_size (ptr, &ptr);
 
@@ -927,7 +998,6 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
 
        case MONO_MEMBERREF_PARENT_TYPESPEC: {
                MonoType *type;
-               MonoMethod *result;
 
                type = &klass->byval_arg;
 
@@ -938,11 +1008,7 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
                }
 
                /* we're an array and we created these methods already in klass in mono_class_init () */
-               result = mono_method_search_in_array_class (klass, mname, sig);
-               if (result)
-                       return result;
-
-               g_assert_not_reached ();
+               method = mono_method_search_in_array_class (klass, mname, sig);
                break;
        }
        default:
@@ -974,6 +1040,7 @@ method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *typesp
 static MonoMethod *
 method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
 {
+       MonoError error;
        MonoMethod *method;
        MonoClass *klass;
        MonoTableInfo *tables = image->tables;
@@ -987,6 +1054,9 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
        token = cols [MONO_METHODSPEC_METHOD];
        nindex = token >> MONO_METHODDEFORREF_BITS;
 
+       if (!mono_verifier_verify_methodspec_signature (image, cols [MONO_METHODSPEC_SIGNATURE], NULL))
+               return NULL;
+
        ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
 
        mono_metadata_decode_value (ptr, &ptr);
@@ -995,8 +1065,13 @@ method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 i
        g_assert (param_count);
 
        inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
-       if (context && inst->is_open)
-               inst = mono_metadata_inflate_generic_inst (inst, context);
+       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.*/
+                       return NULL;
+               }
+       }
 
        if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
                method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
@@ -1125,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 ();
@@ -1226,7 +1303,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                                continue;
                        break;
                default:
-#ifndef PLATFORM_WIN32
+#ifndef TARGET_WIN32
                        if (!g_ascii_strcasecmp ("user32.dll", new_scope) ||
                            !g_ascii_strcasecmp ("kernel32.dll", new_scope) ||
                            !g_ascii_strcasecmp ("user32", new_scope) ||
@@ -1235,7 +1312,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                        } else
 #endif
                                    continue;
-#ifndef PLATFORM_WIN32
+#ifndef TARGET_WIN32
                        break;
 #endif
                }
@@ -1316,7 +1393,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                int mangle_charset;
                int mangle_stdcall;
                int mangle_param_count;
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
                int param_count;
 #endif
 
@@ -1326,7 +1403,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                for (mangle_charset = 0; mangle_charset <= 1; mangle_charset ++) {
                        for (mangle_stdcall = 0; mangle_stdcall <= 1; mangle_stdcall ++) {
                                gboolean need_param_count = FALSE;
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
                                if (mangle_stdcall > 0)
                                        need_param_count = TRUE;
 #endif
@@ -1343,7 +1420,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                                                        mangled_name = g_strconcat (import, "W", NULL);
                                                break;
                                        case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
                                                if (mangle_charset == 0)
                                                        mangled_name = g_strconcat (import, "W", NULL);
 #else
@@ -1360,7 +1437,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                                                break;
                                        }
 
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
                                        if (mangle_param_count == 0)
                                                param_count = mono_method_signature (method)->param_count * sizeof (gpointer);
                                        else
@@ -1431,7 +1508,7 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
                result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
                // This checks the memberref type as well
                if (result && handle_class != mono_defaults.methodhandle_class) {
-                       mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
+                       mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
                        return NULL;
                }
                return result;
@@ -1442,16 +1519,18 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
                        if (used_context) *used_context = TRUE;
                        return method_from_methodspec (image, context, idx);
                }
-               if (table != MONO_TABLE_MEMBERREF)
-                       g_print("got wrong token: 0x%08x\n", token);
-               g_assert (table == MONO_TABLE_MEMBERREF);
+               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));
+                       return NULL;
+               }
                return method_from_memberref (image, idx, context, used_context);
        }
 
        if (used_context) *used_context = FALSE;
 
        if (idx > image->tables [MONO_TABLE_METHOD].rows) {
-               mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
+               mono_loader_set_error_bad_image (g_strdup_printf ("Bad method token 0x%08x on image %s.", token, image->name));
                return NULL;
        }
 
@@ -1461,12 +1540,14 @@ 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 ++;
 
-       if (!klass) {
+       if (!klass) { /*FIXME put this before the image alloc*/
                guint32 type = mono_metadata_typedef_from_method (image, token);
+               if (!type)
+                       return NULL;
                klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
                if (klass == NULL)
                        return NULL;
@@ -1479,29 +1560,35 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
        result->token = token;
        result->name = mono_metadata_string_heap (image, cols [3]);
 
+       if (!sig) /* already taken from the methodref */
+               sig = mono_metadata_blob_heap (image, cols [4]);
+       size = mono_metadata_decode_blob_size (sig, &sig);
+
        container = klass->generic_container;
-       generic_container = mono_metadata_load_generic_params (image, token, container);
+
+       /* 
+        * load_generic_params does a binary search so only call it if the method 
+        * is generic.
+        */
+       if (*sig & 0x10)
+               generic_container = mono_metadata_load_generic_params (image, token, container);
        if (generic_container) {
                result->is_generic = TRUE;
                generic_container->owner.method = result;
-
-               mono_metadata_load_generic_param_constraints (image, token, generic_container);
+               /*FIXME put this before the image alloc*/
+               if (!mono_metadata_load_generic_param_constraints_full (image, token, generic_container))
+                       return NULL;
 
                container = generic_container;
        }
 
-
-       if (!sig) /* already taken from the methodref */
-               sig = mono_metadata_blob_heap (image, cols [4]);
-       size = mono_metadata_decode_blob_size (sig, &sig);
-
        if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
                if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
                        result->string_ctor = 1;
        } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
                MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
 
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
                /* IJW is P/Invoke with a predefined function pointer. */
                if (image->is_module_handle && (cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
                        piinfo->addr = mono_image_rva_map (image, cols [0]);
@@ -1526,14 +1613,6 @@ mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
        return mono_get_method_full (image, token, klass, NULL);
 }
 
-static gpointer
-get_method_token (gpointer value)
-{
-       MonoMethod *m = (MonoMethod*)value;
-
-       return GUINT_TO_POINTER (m->token);
-}
-
 MonoMethod *
 mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
                      MonoGenericContext *context)
@@ -1547,8 +1626,8 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
 
        if (mono_metadata_token_table (token) == MONO_TABLE_METHOD) {
                if (!image->method_cache)
-                       image->method_cache = mono_value_hash_table_new (NULL, NULL, get_method_token);
-               result = mono_value_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token));
+                       image->method_cache = g_hash_table_new (NULL, NULL);
+               result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)));
        } else {
                if (!image->methodref_cache)
                        image->methodref_cache = g_hash_table_new (NULL, NULL);
@@ -1567,7 +1646,7 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
        if (!used_context && !result->is_inflated) {
                MonoMethod *result2;
                if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
-                       result2 = mono_value_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token));
+                       result2 = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)));
                else
                        result2 = g_hash_table_lookup (image->methodref_cache, GINT_TO_POINTER (token));
 
@@ -1577,7 +1656,7 @@ mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
                }
 
                if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
-                       mono_value_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
+                       g_hash_table_insert (image->method_cache, GINT_TO_POINTER (mono_metadata_token_index (token)), result);
                else
                        g_hash_table_insert (image->methodref_cache, GINT_TO_POINTER (token), result);
        }
@@ -1616,17 +1695,19 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
        mono_class_init (constrained_class);
        method = *cil_method;
        original_sig = sig = mono_method_signature (method);
+       if (sig == NULL)
+               return NULL;
 
        if (method->is_inflated && sig->generic_param_count) {
                MonoMethodInflated *imethod = (MonoMethodInflated *) method;
-               sig = mono_method_signature (imethod->declaring);
+               sig = mono_method_signature (imethod->declaring); /*We assume that if the inflated method signature is valid, the declaring method is too*/
                method_context = mono_method_get_context (method);
 
                original_sig = sig;
                /*
                 * We must inflate the signature with the class instantiation to work on
                 * cases where a class inherit from a generic type and the override replaces
-                * and type argument which a concrete type. See #325283.
+                * any type argument which a concrete type. See #325283.
                 */
                if (method_context->class_inst) {
                        MonoGenericContext ctx;
@@ -1634,6 +1715,8 @@ mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constra
                        ctx.class_inst = method_context->class_inst;
                
                        sig = inflate_generic_signature (method->klass->image, sig, &ctx);
+                       if (sig == NULL)
+                               return NULL;
                }
        }
 
@@ -1686,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);
@@ -1706,14 +1789,22 @@ mono_method_get_param_names (MonoMethod *method, const char **names)
        MonoClass *klass;
        MonoTableInfo *methodt;
        MonoTableInfo *paramt;
+       MonoMethodSignature *signature;
        guint32 idx;
 
        if (method->is_inflated)
                method = ((MonoMethodInflated *) method)->declaring;
 
-       if (!mono_method_signature (method)->param_count)
+       signature = mono_method_signature (method);
+       /*FIXME this check is somewhat redundant since the caller usally will have to get the signature to figure out the
+         number of arguments and allocate a properly sized array. */
+       if (signature == NULL)
                return;
-       for (i = 0; i < mono_method_signature (method)->param_count; ++i)
+
+       if (!signature->param_count)
+               return;
+
+       for (i = 0; i < signature->param_count; ++i)
                names [i] = "";
 
        klass = method->klass;
@@ -1749,10 +1840,9 @@ mono_method_get_param_names (MonoMethod *method, const char **names)
                        lastp = paramt->rows + 1;
                for (i = param_index; i < lastp; ++i) {
                        mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
-                       if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
+                       if (cols [MONO_PARAM_SEQUENCE] && cols [MONO_PARAM_SEQUENCE] <= signature->param_count) /* skip return param spec and bounds check*/
                                names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
                }
-               return;
        }
 }
 
@@ -1791,9 +1881,13 @@ mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
        MonoClass *klass = method->klass;
        MonoTableInfo *methodt;
        MonoTableInfo *paramt;
+       MonoMethodSignature *signature;
        guint32 idx;
 
-       for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
+       signature = mono_method_signature (method);
+       g_assert (signature); /*FIXME there is no way to signal error from this function*/
+
+       for (i = 0; i < signature->param_count + 1; ++i)
                mspecs [i] = NULL;
 
        if (method->klass->image->dynamic) {
@@ -1802,10 +1896,12 @@ mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
                                ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
                if (method_aux && method_aux->param_marshall) {
                        MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
-                       for (i = 0; i < mono_method_signature (method)->param_count + 1; ++i)
+                       for (i = 0; i < signature->param_count + 1; ++i)
                                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;
@@ -1828,7 +1924,7 @@ mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
                for (i = param_index; i < lastp; ++i) {
                        mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
 
-                       if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
+                       if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL && cols [MONO_PARAM_SEQUENCE] <= signature->param_count) {
                                const char *tp;
                                tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
                                g_assert (tp);
@@ -1945,6 +2041,8 @@ mono_method_get_last_managed (void)
        return m;
 }
 
+static gboolean loader_lock_track_ownership = FALSE;
+
 /**
  * mono_loader_lock:
  *
@@ -1953,22 +2051,55 @@ mono_method_get_last_managed (void)
 void
 mono_loader_lock (void)
 {
-       EnterCriticalSection (&loader_mutex);
+       mono_locks_acquire (&loader_mutex, LoaderLock);
+       if (G_UNLIKELY (loader_lock_track_ownership)) {
+               TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) + 1));
+       }
 }
 
 void
 mono_loader_unlock (void)
 {
-       LeaveCriticalSection (&loader_mutex);
+       mono_locks_release (&loader_mutex, LoaderLock);
+       if (G_UNLIKELY (loader_lock_track_ownership)) {
+               TlsSetValue (loader_lock_nest_id, GUINT_TO_POINTER (GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) - 1));
+       }
+}
+
+/*
+ * mono_loader_lock_track_ownership:
+ *
+ *   Set whenever the runtime should track ownership of the loader lock. If set to TRUE,
+ * the mono_loader_lock_is_owned_by_self () can be called to query whenever the current
+ * thread owns the loader lock. 
+ */
+void
+mono_loader_lock_track_ownership (gboolean track)
+{
+       loader_lock_track_ownership = track;
+}
+
+/*
+ * mono_loader_lock_is_owned_by_self:
+ *
+ *   Return whenever the current thread owns the loader lock.
+ * This is useful to avoid blocking operations while holding the loader lock.
+ */
+gboolean
+mono_loader_lock_is_owned_by_self (void)
+{
+       g_assert (loader_lock_track_ownership);
+
+       return GPOINTER_TO_UINT (TlsGetValue (loader_lock_nest_id)) > 0;
 }
 
 /**
  * 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;
@@ -1977,10 +2108,12 @@ mono_method_signature (MonoMethod *m)
        gboolean can_cache_signature;
        MonoGenericContainer *container;
        MonoMethodSignature *signature = NULL;
-       int *pattrs;
+       guint32 sig_offset;
 
        /* We need memory barriers below because of the double-checked locking pattern */ 
 
+       mono_error_init (error);
+
        if (m->signature)
                return m->signature;
 
@@ -2009,7 +2142,7 @@ mono_method_signature (MonoMethod *m)
        idx = mono_metadata_token_index (m->token);
        img = m->klass->image;
 
-       sig = mono_metadata_blob_heap (img, mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
+       sig = mono_metadata_blob_heap (img, sig_offset = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_SIGNATURE));
 
        g_assert (!m->klass->generic_class);
        container = mono_method_get_generic_container (m);
@@ -2021,23 +2154,27 @@ mono_method_signature (MonoMethod *m)
        can_cache_signature = !(m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && !(m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && !container;
 
        /* If the method has parameter attributes, that can modify the signature */
-       pattrs = mono_metadata_get_param_attrs (img, idx);
-       if (pattrs) {
+       if (mono_metadata_method_has_param_attrs (img, idx))
                can_cache_signature = FALSE;
-               g_free (pattrs);
-       }
 
        if (can_cache_signature)
                signature = g_hash_table_lookup (img->method_signatures, sig);
 
        if (!signature) {
                const char *sig_body;
+               /*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;
+               }
 
                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_loader_unlock ();
+                       mono_error_set_method_load (error, m->klass, m->name, "");
                        return NULL;
                }
 
@@ -2047,14 +2184,21 @@ mono_method_signature (MonoMethod *m)
 
        /* Verify metadata consistency */
        if (signature->generic_param_count) {
-               if (!container || !container->is_method)
-                       g_error ("Signature claims method has generic parameters, but generic_params table says it doesn't");
-               if (container->type_argc != signature->generic_param_count)
-                       g_error ("Inconsistent generic parameter count.  Signature says %d, generic_params table says %d",
-                                signature->generic_param_count, container->type_argc);
-       } else if (container && container->is_method && container->type_argc)
-               g_error ("generic_params table claims method has generic parameters, but signature says it doesn't");
-
+               if (!container || !container->is_method) {
+                       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) {
+                       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) {
+               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)
                signature->pinvoke = 1;
        else if (m->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
@@ -2082,8 +2226,9 @@ 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", piinfo->piflags);
-                       g_assert_not_reached ();
+                       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;
        }
@@ -2095,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)
 {
@@ -2120,65 +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);
 
-       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;
-       }
+       if (!mono_verifier_verify_method_header (img, rva, NULL))
+               return NULL;
 
-       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
@@ -2201,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)