2007-08-30 Dick Porter <dick@ximian.com>
[mono.git] / mono / metadata / metadata.c
index 8f400f1bbef87a55daaba15de24f16fed790d8c3..126f8708a4fabb8dc325a2216ca430ec87d02814 100644 (file)
@@ -1341,6 +1341,13 @@ static GHashTable *generic_inst_cache = NULL;
 static GHashTable *generic_class_cache = NULL;
 static int next_generic_inst_id = 0;
 
+/*
+ * Protected by the loader lock.
+ * It has a MonoMethodInflated* as key and value.
+ * The key lookup will just access the declaring and context fields
+ */
+static GHashTable *generic_method_cache = NULL;
+
 static guint mono_generic_class_hash (gconstpointer data);
 
 /*
@@ -1390,7 +1397,7 @@ mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
        const MonoGenericInst *b = (const MonoGenericInst *) kb;
        int i;
 
-       if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc) || (a->is_reference != b->is_reference))
+       if (a->is_open != b->is_open || a->type_argc != b->type_argc)
                return FALSE;
        for (i = 0; i < a->type_argc; ++i) {
                if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
@@ -1406,7 +1413,7 @@ mono_generic_class_hash (gconstpointer data)
        guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
 
        hash *= 13;
-       hash += mono_generic_inst_hash (gclass->inst);
+       hash += mono_metadata_generic_context_hash (&gclass->context);
 
        return hash;
 }
@@ -1451,6 +1458,12 @@ mono_metadata_cleanup (void)
        g_hash_table_destroy (type_cache);
        g_hash_table_destroy (generic_inst_cache);
        g_hash_table_destroy (generic_class_cache);
+       if (generic_method_cache)
+               g_hash_table_destroy (generic_method_cache);
+       type_cache = NULL;
+       generic_inst_cache = NULL;
+       generic_class_cache = NULL;
+       generic_method_cache = NULL;
 }
 
 /**
@@ -1474,7 +1487,7 @@ mono_metadata_cleanup (void)
  * this MonoGenericContainer.
  * This is a Mono runtime internal function.
  *
- * LOCKING: Assumes the loader lock is held.
+ * LOCKING: Acquires the loader lock.
  *
  * Returns: a #MonoType structure representing the decoded type.
  */
@@ -1560,8 +1573,6 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo
        type->pinned = pinned ? 1 : 0;
 
        if (!do_mono_metadata_parse_type (type, m, container, ptr, &ptr)) {
-               if (type != &stype)
-                       g_free (type);
                mono_loader_unlock ();
                return NULL;
        }
@@ -1569,7 +1580,7 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo
        if (rptr)
                *rptr = ptr;
 
-               if (!type->num_mods) {
+       if (!type->num_mods) {
                /* no need to free type here, because it is on the stack */
                if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
                        MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
@@ -1884,61 +1895,222 @@ mono_metadata_free_method_signature (MonoMethodSignature *sig)
        }
 }
 
+static gboolean
+inflated_method_equal (gconstpointer a, gconstpointer b)
+{
+       const MonoMethodInflated *ma = a;
+       const MonoMethodInflated *mb = b;
+       if (ma->declaring != mb->declaring)
+               return FALSE;
+       return mono_metadata_generic_context_equal (&ma->context, &mb->context);
+}
+
+static guint
+inflated_method_hash (gconstpointer a)
+{
+       const MonoMethodInflated *ma = a;
+       return mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring);
+}
+
+/*static void
+dump_ginst (MonoGenericInst *ginst)
+{
+       int i;
+       char *name;
+
+       g_print ("Ginst: <");
+       for (i = 0; i < ginst->type_argc; ++i) {
+               if (i != 0)
+                       g_print (", ");
+               name = mono_type_get_name (ginst->type_argv [i]);
+               g_print ("%s", name);
+               g_free (name);
+       }
+       g_print (">");
+}*/
+
+static gboolean gclass_in_image (gpointer key, gpointer value, gpointer data);
+
+static gboolean
+ginst_in_image (gpointer key, gpointer value, gpointer data)
+{
+       MonoImage *image = data;
+       MonoGenericInst *ginst = key;
+       MonoClass *klass;
+       int i;
+       for (i = 0; i < ginst->type_argc; ++i) {
+               MonoType *type = ginst->type_argv [i];
+
+               /* FIXME: Avoid a possible mono_class_inst inside mono_class_from_mono_type */
+               if (type->type == MONO_TYPE_GENERICINST) {
+                       if (gclass_in_image (type->data.generic_class, NULL, image))
+                               return TRUE;
+                       continue;
+               }
+
+               klass = mono_class_from_mono_type (ginst->type_argv [i]);
+               if (klass->image == image) {
+                       /*dump_ginst (ginst);
+                         g_print (" removed\n");*/
+                       return TRUE;
+               }
+       }
+       return FALSE;
+}
+
+static gboolean
+gclass_in_image (gpointer key, gpointer value, gpointer data)
+{
+       MonoImage *image = data;
+       MonoGenericClass *gclass = key;
+
+       if (ginst_in_image (gclass->context.class_inst, NULL, image))
+               return TRUE;
+       if (gclass->container_class->image == image)
+               return TRUE;
+       return FALSE;
+}
+
+static gboolean
+inflated_method_in_image (gpointer key, gpointer value, gpointer data)
+{
+       MonoImage *image = data;
+       MonoMethodInflated *method = key;
+
+       if (method->declaring->klass->image == image)
+               return TRUE;
+       if (method->context.class_inst && ginst_in_image (method->context.class_inst, NULL, image))
+               return TRUE;
+       if (method->context.method_inst && ginst_in_image (method->context.method_inst, NULL, image))
+               return TRUE;
+       return FALSE;
+}
+
 /*
- * mono_metadata_lookup_generic_inst:
- *
- * Check whether the newly created generic instantiation @ginst already exists
- * in the cache and return the cached value in this case.  Otherwise insert
- * it into the cache.
+ * LOCKING: assumes the loader lock is held.
+ */
+MonoMethodInflated*
+mono_method_inflated_lookup (MonoMethodInflated* method, gboolean cache)
+{
+       if (cache) {
+               if (!generic_method_cache)
+                       generic_method_cache = g_hash_table_new (inflated_method_hash, inflated_method_equal);
+               g_hash_table_insert (generic_method_cache, method, method);
+               return method;
+       } else {
+               if (generic_method_cache)
+                       return g_hash_table_lookup (generic_method_cache, method);
+               return NULL;
+       }
+}
+
+void
+mono_metadata_clean_for_image (MonoImage *image)
+{
+       mono_loader_lock ();
+       g_hash_table_foreach_remove (generic_inst_cache, ginst_in_image, image);
+       g_hash_table_foreach_remove (generic_class_cache, gclass_in_image, image);
+       if (generic_method_cache)
+               g_hash_table_foreach_remove (generic_method_cache, inflated_method_in_image, image);
+       mono_loader_unlock ();
+}
+
+/*
+ * mono_metadata_get_generic_inst:
  *
- * Use this method each time you create a new `MonoGenericInst' to ensure
- * proper caching.  Only use the returned value as the argument passed to this
- * method may be freed.
+ * Given a list of types, return a MonoGenericInst that represents that list.
+ * The returned MonoGenericInst has its own copy of the list of types.  The list
+ * passed in the argument can be freed, modified or disposed of.
  *
  */
 MonoGenericInst *
-mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
+mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
 {
-       MonoGenericInst *cached;
+       MonoGenericInst *ginst;
+       MonoGenericInst helper;
        int i;
 
-       cached = g_hash_table_lookup (generic_inst_cache, ginst);
-       if (cached) {
-               for (i = 0; i < ginst->type_argc; i++)
-                       mono_metadata_free_type (ginst->type_argv [i]);
-               g_free (ginst->type_argv);
-               g_free (ginst);
-               return cached;
+       helper.type_argc = type_argc;
+       helper.type_argv = type_argv;
+       helper.id = 0;
+
+       for (i = 0; i < type_argc; ++i)
+               if (mono_class_is_open_constructed_type (type_argv [i]))
+                       break;
+       helper.is_open = (i < type_argc);
+
+       /*dump_ginst (&helper);*/
+       mono_loader_lock ();
+       ginst = g_hash_table_lookup (generic_inst_cache, &helper);
+       if (ginst) {
+               mono_loader_unlock ();
+               /*g_print (" found cached\n");*/
+               return ginst;
        }
 
+       ginst = g_new0 (MonoGenericInst, 1);
+       ginst->type_argc = type_argc;
+       ginst->type_argv = g_new (MonoType*, type_argc);
        ginst->id = ++next_generic_inst_id;
+       ginst->is_open = helper.is_open;
+
+       for (i = 0; i < type_argc; ++i)
+               ginst->type_argv [i] = mono_metadata_type_dup (NULL, type_argv [i]);
+
        g_hash_table_insert (generic_inst_cache, ginst, ginst);
 
+       mono_loader_unlock ();
+       /*g_print (" inserted\n");*/
        return ginst;
 }
 
 /*
  * mono_metadata_lookup_generic_class:
  *
- * Check whether the newly created generic class @gclass already exists
- * in the cache and return the cached value in this case.  Otherwise insert
- * it into the cache and return NULL.
- *
- * Returns: the previosly cached generic class or NULL if it has been newly
- *          inserted into the cache.
+ * Returns a MonoGenericClass with the given properties.
  *
  */
 MonoGenericClass *
-mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
+mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
 {
-       MonoGenericClass *cached;
+       MonoGenericClass *gclass;
 
-       cached = g_hash_table_lookup (generic_class_cache, gclass);
-       if (cached)
-               return cached;
+       MonoGenericClass helper;
+       helper.container_class = container_class;
+       helper.context.class_inst = inst;
+       helper.context.method_inst = NULL;
+       helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
+       helper.cached_class = NULL;
+
+       mono_loader_lock ();
+
+       gclass = g_hash_table_lookup (generic_class_cache, &helper);
+
+       /* A tripwire just to keep us honest */
+       g_assert (!helper.cached_class);
+
+       if (gclass) {
+               mono_loader_unlock ();
+               return gclass;
+       }
+
+       if (is_dynamic) {
+               MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
+               gclass = &dgclass->generic_class;
+               gclass->is_dynamic = 1;
+       } else {
+               gclass = g_new0 (MonoGenericClass, 1);
+       }
+
+       gclass->container_class = container_class;
+       gclass->context.class_inst = inst;
+       gclass->context.method_inst = NULL;
 
        g_hash_table_insert (generic_class_cache, gclass, gclass);
-       return NULL;
+
+       mono_loader_unlock ();
+
+       return gclass;
 }
 
 /*
@@ -1950,128 +2122,80 @@ mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
 MonoGenericInst *
 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
 {
+       MonoType **type_argv;
        MonoGenericInst *nginst;
        int i;
 
        if (!ginst->is_open)
                return ginst;
 
-       nginst = g_new0 (MonoGenericInst, 1);
-       nginst->type_argc = ginst->type_argc;
-       nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
-       nginst->is_reference = 1;
+       type_argv = g_new0 (MonoType*, ginst->type_argc);
 
-       for (i = 0; i < nginst->type_argc; i++) {
-               MonoType *t = mono_class_inflate_generic_type (ginst->type_argv [i], context);
+       for (i = 0; i < ginst->type_argc; i++)
+               type_argv [i] = mono_class_inflate_generic_type (ginst->type_argv [i], context);
 
-               if (!nginst->is_open)
-                       nginst->is_open = mono_class_is_open_constructed_type (t);
-               if (nginst->is_reference)
-                       nginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
+       nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
 
-               nginst->type_argv [i] = t;
-       }
+       g_free (type_argv);
 
-       return mono_metadata_lookup_generic_inst (nginst);
+       return nginst;
 }
 
 MonoGenericInst *
 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
                                  int count, const char *ptr, const char **rptr)
 {
+       MonoType **type_argv;
        MonoGenericInst *ginst;
        int i;
 
-       ginst = g_new0 (MonoGenericInst, 1);
-       ginst->type_argc = count;
-       ginst->type_argv = g_new0 (MonoType*, count);
-       ginst->is_reference = 1;
+       type_argv = g_new0 (MonoType*, count);
 
-       for (i = 0; i < ginst->type_argc; i++) {
+       for (i = 0; i < count; i++) {
                MonoType *t = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
-
                if (!t) {
-                       g_free (ginst->type_argv);
-                       g_free (ginst);
+                       g_free (type_argv);
                        return NULL;
                }
-               ginst->type_argv [i] = t;
-               if (!ginst->is_open)
-                       ginst->is_open = mono_class_is_open_constructed_type (t);
-               if (ginst->is_reference)
-                       ginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
+               type_argv [i] = t;
        }
 
        if (rptr)
                *rptr = ptr;
 
-       return mono_metadata_lookup_generic_inst (ginst);
+       ginst = mono_metadata_get_generic_inst (count, type_argv);
+
+       g_free (type_argv);
+
+       return ginst;
 }
 
 static gboolean
 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
                                      const char *ptr, const char **rptr)
 {
-       MonoGenericClass *gclass, *cached;
+       MonoGenericInst *inst;
        MonoClass *gklass;
        MonoType *gtype;
        int count;
 
-       gclass = g_new0 (MonoGenericClass, 1);
-
-       type->data.generic_class = gclass;
-
        gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
        if (gtype == NULL)
                return FALSE;
-       gclass->container_class = gklass = mono_class_from_mono_type (gtype);
 
-       g_assert (gklass->generic_container);
+       gklass = mono_class_from_mono_type (gtype);
+       if (!gklass->generic_container)
+               return FALSE;
 
        count = mono_metadata_decode_value (ptr, &ptr);
-
-       gclass->inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
-       /* FIXME: this hack is needed to handle the incestuous relationship between metadata parsing and MonoClass. */
-       if (gclass->cached_context) {
-               g_free (gclass->cached_context);
-               gclass->cached_context = NULL;
-       }
+       inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
+       if (inst == NULL)
+               return FALSE;
 
        if (rptr)
                *rptr = ptr;
 
-       /* If we failed to parse, return, the error has been flagged. */
-       if (gclass->inst == NULL)
-               return FALSE;
-       
-       /*
-        * We may be called multiple times on different metadata to create the same
-        * instantiated type.  This happens for instance if we're part of a method or
-        * local variable signature.
-        *
-        * It's important to return the same MonoGenericClass * for each particualar
-        * instantiation of a generic type (ie "Stack<Int32>") to make static fields
-        * work.
-        *
-        * According to the spec ($26.1.5), a static variable in a generic class
-        * declaration is shared amongst all instances of the same closed constructed
-        * type.
-        */
-
-       cached = g_hash_table_lookup (generic_class_cache, gclass);
-       if (cached) {
-               g_free (gclass);
-
-               type->data.generic_class = cached;
-               return TRUE;
-       } else {
-               g_hash_table_insert (generic_class_cache, gclass, gclass);
-
-               mono_stats.generic_instance_count++;
-               mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
-                       sizeof (MonoGenericContext) +
-                       gclass->inst->type_argc * sizeof (MonoType);
-       }
+       type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
        return TRUE;
 }
 
@@ -3330,7 +3454,7 @@ mono_type_stack_size (MonoType *t, int *align)
                MonoGenericClass *gclass = t->data.generic_class;
                MonoClass *container_class = gclass->container_class;
 
-               g_assert (!gclass->inst->is_open);
+               g_assert (!gclass->context.class_inst->is_open);
 
                if (container_class->valuetype) {
                        if (container_class->enumtype)
@@ -3374,33 +3498,38 @@ static gboolean
 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
 {
        int i;
+       MonoGenericInst *i1 = g1->context.class_inst;
+       MonoGenericInst *i2 = g2->context.class_inst;
 
-       if ((g1->inst->type_argc != g2->inst->type_argc) || (g1->is_dynamic != g2->is_dynamic) ||
-           (g1->inst->is_reference != g2->inst->is_reference))
+       if (i1->type_argc != i2->type_argc || g1->is_dynamic != g2->is_dynamic)
                return FALSE;
        if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
                return FALSE;
-       for (i = 0; i < g1->inst->type_argc; ++i) {
-               if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
+       for (i = 0; i < i1->type_argc; ++i) {
+               if (!do_mono_metadata_type_equal (i1->type_argv [i], i2->type_argv [i], signature_only))
                        return FALSE;
        }
        return TRUE;
 }
 
 guint
-mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
-{
-       return gmethod->inst->id;
+mono_metadata_generic_context_hash (const MonoGenericContext *context)
+{
+       /* FIXME: check if this seed is good enough */
+       guint hash = 0xc01dfee7;
+       if (context->class_inst)
+               hash = ((hash << 5) - hash) ^ context->class_inst->id;
+       if (context->method_inst)
+               hash = ((hash << 5) - hash) ^ context->method_inst->id;
+       return hash;
 }
 
 gboolean
-mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
+mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGenericContext *g2)
 {
-       return (g1->container == g2->container) && (g1->class_inst == g2->class_inst) &&
-               (g1->inst == g2->inst);
+       return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
 }
 
-
 /*
  * mono_metadata_type_hash:
  * @t1: a type
@@ -3610,27 +3739,37 @@ mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *s
 }
 
 /**
- * mono_metadata_type_dup_mp:
- * @image: image type is defined in
+ * mono_metadata_type_dup:
+ * @mp: mempool to use
  * @original: type to duplicate
  *
- * Returns: copy of type allocated from mempool.
+ * Returns: copy of type allocated from mempool (or from the heap, if @mp is null).
  */
 MonoType *
-mono_metadata_type_dup_mp (MonoImage *image, const MonoType *original)
+mono_metadata_type_dup (MonoMemPool *mp, const MonoType *o)
 {
        MonoType *r = NULL;
+       int sizeof_o = sizeof (MonoType);
+       if (o->num_mods)
+               sizeof_o += (o->num_mods - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod);
+
        mono_loader_lock ();
-       r = mono_mempool_alloc0 (image->mempool, sizeof(MonoType));
+       r = mp ? mono_mempool_alloc0 (mp, sizeof_o) : g_malloc (sizeof_o);
        mono_loader_unlock ();
-       *r = *original;
-       /* FIXME: we don't handle these yet because they need to duplicate memory
-        * but the current routines used are not using the mempools
-        */
-       if (original->type == MONO_TYPE_PTR || 
-               original->type == MONO_TYPE_ARRAY || 
-               original->type == MONO_TYPE_FNPTR)
-               g_assert_not_reached ();
+
+       memcpy (r, o, sizeof_o);
+
+       if (o->type == MONO_TYPE_PTR) {
+               r->data.type = mono_metadata_type_dup (mp, o->data.type);
+       } else if (o->type == MONO_TYPE_ARRAY) {
+               /* FIXME: should mono_dup_array_type() use mempools? */
+               g_assert (!mp);
+               r->data.array = mono_dup_array_type (o->data.array);
+       } else if (o->type == MONO_TYPE_FNPTR) {
+               /* FIXME: should mono_metadata_signature_deep_dup() use mempools? */
+               g_assert (!mp);
+               r->data.method = mono_metadata_signature_deep_dup (o->data.method);
+       }
        return r;
 }
 
@@ -4007,7 +4146,7 @@ mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
        guint32 cols [MONO_TYPESPEC_SIZE];
        const char *ptr;
        guint32 len;
-       MonoType *type;
+       MonoType *type, *type2;
 
        mono_loader_lock ();
 
@@ -4025,20 +4164,27 @@ mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
 
        type = g_new0 (MonoType, 1);
 
-       g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
-
        if (*ptr == MONO_TYPE_BYREF) {
                type->byref = 1;
                ptr++;
        }
 
        if (!do_mono_metadata_parse_type (type, image, NULL, ptr, &ptr)) {
-               g_hash_table_remove (image->typespec_cache, GUINT_TO_POINTER (type_spec));
                g_free (type);
                mono_loader_unlock ();
                return NULL;
        }
 
+       type2 = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
+
+       if (type2) {
+               g_free (type);
+               mono_loader_unlock ();
+               return type2;
+       }
+
+       g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
+
        mono_loader_unlock ();
 
        return type;
@@ -4217,8 +4363,14 @@ handle_enum:
                                g_error ("Can not marshal string to native type '%02x': Invalid managed/unmanaged type combination (String fields must be paired with LPStr, LPWStr, BStr or ByValTStr).", mspec->native);
                        }
                }       
-               *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
-               return MONO_NATIVE_LPTSTR; 
+               if (unicode) {
+                       *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
+                       return MONO_NATIVE_LPWSTR; 
+               }
+               else {
+                       *conv = MONO_MARSHAL_CONV_STR_LPSTR;
+                       return MONO_NATIVE_LPSTR; 
+               }
        case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
        case MONO_TYPE_VALUETYPE: /*FIXME*/
                if (type->data.klass->enumtype) {
@@ -4262,6 +4414,8 @@ handle_enum:
                        switch (mspec->native) {
                        case MONO_NATIVE_STRUCT:
                                return MONO_NATIVE_STRUCT;
+                       case MONO_NATIVE_CUSTOM:
+                               return MONO_NATIVE_CUSTOM;
                        case MONO_NATIVE_INTERFACE:
                                *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
                                return MONO_NATIVE_INTERFACE;
@@ -4559,6 +4713,7 @@ mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericC
        guint32 i, owner = 0, n;
        MonoGenericContainer *container;
        MonoGenericParam *params;
+       MonoGenericContext *context;
 
        if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
                return NULL;
@@ -4589,9 +4744,45 @@ mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericC
 
        g_assert (container->parent == NULL || container->is_method);
 
+       context = &container->context;
+       if (container->is_method) {
+               context->class_inst = container->parent ? container->parent->context.class_inst : NULL;
+               context->method_inst = mono_get_shared_generic_inst (container);
+       } else {
+               context->class_inst = mono_get_shared_generic_inst (container);
+       }
+
        return container;
 }
 
+MonoGenericInst *
+mono_get_shared_generic_inst (MonoGenericContainer *container)
+{
+       MonoType **type_argv;
+       MonoType *helper;
+       MonoGenericInst *nginst;
+       int i;
+
+       type_argv = g_new0 (MonoType *, container->type_argc);
+       helper = g_new0 (MonoType, container->type_argc);
+
+       for (i = 0; i < container->type_argc; i++) {
+               MonoType *t = &helper [i];
+
+               t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
+               t->data.generic_param = &container->type_params [i];
+
+               type_argv [i] = t;
+       }
+
+       nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
+
+       g_free (type_argv);
+       g_free (helper);
+
+       return nginst;
+}
+
 gboolean
 mono_type_is_byref (MonoType *type)
 {