2009-12-16 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / loader.c
index 13efb16d6f48911e9cbc23d5f6fb9cebd85eb2f2..c2eadb7f3da33ac044732f31b637d50d9651a64e 100644 (file)
@@ -63,6 +63,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 ()
 {
@@ -72,6 +78,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);
@@ -86,6 +93,7 @@ void
 mono_loader_cleanup (void)
 {
        TlsFree (loader_error_thread_id);
+       TlsFree (loader_lock_nest_id);
 
        /*DeleteCriticalSection (&loader_mutex);*/
 }
@@ -409,7 +417,7 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
        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);
@@ -421,7 +429,34 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
        fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
 
        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 %08x field name %s token %08x", class, fname, token));
+               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;
+       }
+
+       switch (class) {
+       case MONO_MEMBERREF_PARENT_TYPEDEF:
+               class_table = MONO_TOKEN_TYPE_DEF;
+               klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
+               break;
+       case MONO_MEMBERREF_PARENT_TYPEREF:
+               class_table = MONO_TOKEN_TYPE_REF;
+               klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
+               break;
+       case MONO_MEMBERREF_PARENT_TYPESPEC:
+               class_table = MONO_TOKEN_TYPE_SPEC;
+               klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
+               break;
+       default:
+               /*FIXME this must set a loader error!*/
+               g_warning ("field load from %x", class);
+               return NULL;
+       }
+
+       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;
        }
 
@@ -431,7 +466,7 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
 
        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 (NULL, fname);
+               mono_loader_set_error_field_load (klass, fname);
                return NULL;
        }
        /* FIXME: This needs a cache, especially for generic instances, since
@@ -441,60 +476,16 @@ field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
        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 (NULL, fname);
+                       mono_loader_set_error_field_load (klass, fname);
                        return NULL;
                }
                sig_type = cache_memberref_sig (image, cols [MONO_MEMBERREF_SIGNATURE], sig_type);
        }
 
-       switch (class) {
-       case MONO_MEMBERREF_PARENT_TYPEDEF:
-               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;
-               field = mono_class_get_field_from_name_full (klass, fname, sig_type);
-               break;
-       case MONO_MEMBERREF_PARENT_TYPEREF:
-               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;
-               field = mono_class_get_field_from_name_full (klass, fname, sig_type);
-               break;
-       case MONO_MEMBERREF_PARENT_TYPESPEC: {
-               klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
-               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;
-               field = mono_class_get_field_from_name_full (klass, fname, sig_type);
-               break;
-       }
-       default:
-               g_warning ("field load from %x", class);
-               return NULL;
-       }
+       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);
@@ -518,7 +509,7 @@ mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
                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."));
+                       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;
@@ -594,6 +585,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);
 
@@ -605,7 +597,8 @@ 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;
                }
        }
@@ -613,17 +606,21 @@ find_method_in_class (MonoClass *klass, const char *name, const char *qname, con
        mono_class_setup_methods (klass);
        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;
                }
        }
@@ -798,7 +795,7 @@ mono_method_get_signature_full (MonoMethod *method, MonoImage *image, guint32 to
                        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 %08x field name %s token %08x", class, fname, token));
+                       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;
                }
 
@@ -995,6 +992,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;
@@ -1019,8 +1017,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);
@@ -1250,7 +1253,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) ||
@@ -1259,7 +1262,7 @@ mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char
                        } else
 #endif
                                    continue;
-#ifndef PLATFORM_WIN32
+#ifndef TARGET_WIN32
                        break;
 #endif
                }
@@ -1340,7 +1343,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
 
@@ -1350,7 +1353,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
@@ -1367,7 +1370,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
@@ -1384,7 +1387,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
@@ -1493,6 +1496,8 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *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;
@@ -1533,7 +1538,7 @@ mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
        } 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]);
@@ -1558,14 +1563,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)
@@ -1579,8 +1576,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);
@@ -1599,7 +1596,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));
 
@@ -1609,7 +1606,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);
        }
@@ -1992,6 +1989,8 @@ mono_method_get_last_managed (void)
        return m;
 }
 
+static gboolean loader_lock_track_ownership = FALSE;
+
 /**
  * mono_loader_lock:
  *
@@ -2001,12 +2000,45 @@ void
 mono_loader_lock (void)
 {
        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)
 {
        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;
 }
 
 /**
@@ -2024,7 +2056,6 @@ 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 */ 
@@ -2069,11 +2100,8 @@ 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);
@@ -2102,15 +2130,18 @@ mono_method_signature (MonoMethod *m)
        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 ();
                        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",
+                       g_warning ("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);
+                       mono_loader_unlock ();
                        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 ();
                return NULL;
        }
        if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
@@ -2141,6 +2172,7 @@ mono_method_signature (MonoMethod *m)
                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 ();
                        return NULL;
                }
                signature->call_convention = conv;
@@ -2217,13 +2249,13 @@ mono_method_get_header (MonoMethod *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;
 
+       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 ();