#include "abi-details.h"
#include <mono/utils/mono-error-internals.h>
#include <mono/utils/bsearch.h>
+#include <mono/utils/atomic.h>
/* Auxiliary structure used for caching inflated signatures */
typedef struct {
{
int i;
-#ifndef MONO_SMALL_CONFIG
- if (a->id && b->id) {
+ // An optimization: if the ids of two insts are the same, we know they are the same inst and don't check contents.
+ // Furthermore, because we perform early de-duping, if the ids differ, we know the contents differ.
+#ifndef MONO_SMALL_CONFIG // Optimization does not work in MONO_SMALL_CONFIG: There are no IDs
+ if (a->id && b->id) { // "id 0" means "object has no id"-- de-duping hasn't been performed yet, must check contents.
if (a->id == b->id)
return TRUE;
+ // In signature-comparison mode id equality implies object equality, but this is not true for inequality.
+ // Two separate objects could have signature-equavalent contents.
if (!signature_only)
return FALSE;
}
for (i = 0; i < NBUILTIN_TYPES (); ++i)
g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
- mono_mutex_init_recursive (&image_sets_mutex);
+ mono_os_mutex_init_recursive (&image_sets_mutex);
}
/**
type_cache = NULL;
g_ptr_array_free (image_sets, TRUE);
image_sets = NULL;
- mono_mutex_destroy (&image_sets_mutex);
+ mono_os_mutex_destroy (&image_sets_mutex);
}
/**
gboolean byref = FALSE;
gboolean pinned = FALSE;
const char *tmp_ptr;
- int count = 0;
+ int count = 0; // Number of mod arguments
gboolean found;
/*
}
}
- if (count) {
+ if (count) { // There are mods, so the MonoType will be of nonstandard size.
int size;
size = MONO_SIZEOF_TYPE + ((gint32)count) * sizeof (MonoCustomMod);
type->num_mods = count;
if (count > 64)
g_warning ("got more than 64 modifiers in type");
- } else {
+ } else { // The type is of standard size, so we can allocate it on the stack.
type = &stype;
memset (type, 0, MONO_SIZEOF_TYPE);
}
- /* Parse pinned, byref and custom modifiers */
+ /* Iterate again, but now parse pinned, byref and custom modifiers */
found = TRUE;
count = 0;
while (found) {
if (rptr)
*rptr = ptr;
+ // Possibly we can return an already-allocated type instead of the one we decoded
if (!type->num_mods && !transient) {
/* 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) {
/* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
- if (type == &stype) {
+ if (type == &stype) { // Type was allocated on the stack, so we need to copy it to safety
type = transient ? g_malloc (MONO_SIZEOF_TYPE) : mono_image_alloc (m, MONO_SIZEOF_TYPE);
memcpy (type, &stype, MONO_SIZEOF_TYPE);
}
goto retry;
case MONO_TYPE_FNPTR:
return signature_in_image (type->data.method, image);
- case MONO_TYPE_VAR: {
- MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
- if (container) {
- g_assert (!container->is_method);
- /*
- * FIXME: The following check is here solely
- * for monodis, which uses the internal
- * function
- * mono_metadata_load_generic_params(). The
- * caller of that function needs to fill in
- * owner->klass or owner->method of the
- * returned struct, but monodis doesn't do
- * that. The image unloading depends on that,
- * however, so a crash results without this
- * check.
- */
- if (!container->owner.klass)
- return container->image == image;
- return container->owner.klass->image == image;
- } else {
- return type->data.generic_param->image == image;
- }
- }
- case MONO_TYPE_MVAR: {
- MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
- if (type->data.generic_param->image == image)
- return TRUE;
- if (container) {
- g_assert (container->is_method);
- if (!container->owner.method)
- /* RefEmit created generic param whose method is not finished */
- return container->image == image;
- return container->owner.method->klass->image == image;
- } else {
- return type->data.generic_param->image == image;
- }
- }
+ case MONO_TYPE_VAR:
+ case MONO_TYPE_MVAR:
+ return image == get_image_for_generic_param (type->data.generic_param);
default:
/* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
return image == mono_class_from_mono_type (type)->image;
static inline void
image_sets_lock (void)
{
- mono_mutex_lock (&image_sets_mutex);
+ mono_os_mutex_lock (&image_sets_mutex);
}
static inline void
image_sets_unlock (void)
{
- mono_mutex_unlock (&image_sets_mutex);
+ mono_os_mutex_unlock (&image_sets_mutex);
}
/*
MonoImageSet *set;
GSList *l;
- /* Common case */
+ /* Common case: Image set contains corlib only. If we've seen that case before, we cached the set. */
if (nimages == 1 && images [0] == mono_defaults.corlib && mscorlib_image_set)
return mscorlib_image_set;
/* Happens with empty generic instances */
+ // FIXME: Is corlib the correct thing to return here? If so, why? This may be an artifact of generic instances previously defaulting to allocating from corlib.
if (nimages == 0)
return mscorlib_image_set;
if (!image_sets)
image_sets = g_ptr_array_new ();
+ // Before we go on, we should check to see whether a MonoImageSet with these images already exists.
+ // We can search the referred-by imagesets of any one of our images to do this. Arbitrarily pick one here:
if (images [0] == mono_defaults.corlib && nimages > 1)
- l = images [1]->image_sets;
+ l = images [1]->image_sets; // Prefer not to search the imagesets of corlib-- that will be a long list.
else
l = images [0]->image_sets;
set = NULL;
- for (; l; l = l->next) {
+ while (l) // Iterate over selected list, looking for an imageset with members equal to our target one
+ {
set = l->data;
- if (set->nimages == nimages) {
+ if (set->nimages == nimages) { // Member count differs, this can't be it
+ // Compare all members to all members-- order might be different
for (j = 0; j < nimages; ++j) {
for (k = 0; k < nimages; ++k)
if (set->images [k] == images [j])
- break;
+ break; // Break on match
+
+ // If we iterated all the way through set->images, images[j] was *not* found.
if (k == nimages)
- /* Not found */
- break;
+ break; // Break on "image not found"
}
+
+ // If we iterated all the way through images without breaking, all items in images were found in set->images
if (j == nimages)
- /* Found */
- break;
+ break; // Break on "found a set with equal members"
}
+
+ l = l->next;
}
+ // If we iterated all the way through l without breaking, the imageset does not already exist and we shuold create it
if (!l) {
- /* Not found */
set = g_new0 (MonoImageSet, 1);
set->nimages = nimages;
set->images = g_new0 (MonoImage*, nimages);
- mono_mutex_init_recursive (&set->lock);
+ mono_os_mutex_init_recursive (&set->lock);
for (i = 0; i < nimages; ++i)
set->images [i] = images [i];
set->gclass_cache = g_hash_table_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
set->ginst_cache = g_hash_table_new_full (mono_metadata_generic_inst_hash, mono_metadata_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
set->gmethod_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
set->gsignature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
-
+
for (i = 0; i < nimages; ++i)
set->images [i]->image_sets = g_slist_prepend (set->images [i]->image_sets, set);
if (set->mempool)
mono_mempool_destroy (set->mempool);
g_free (set->images);
- mono_mutex_destroy (&set->lock);
+ mono_os_mutex_destroy (&set->lock);
g_free (set);
}
void
mono_image_set_lock (MonoImageSet *set)
{
- mono_mutex_lock (&set->lock);
+ mono_os_mutex_lock (&set->lock);
}
void
mono_image_set_unlock (MonoImageSet *set)
{
- mono_mutex_unlock (&set->lock);
+ mono_os_mutex_unlock (&set->lock);
}
gpointer
return res;
}
+// Get a descriptive string for a MonoImageSet
+// Callers are obligated to free buffer with g_free after use
+char *
+mono_image_set_description (MonoImageSet *set)
+{
+ GString *result = g_string_new (NULL);
+ int img;
+ g_string_append (result, "[");
+ for (img = 0; img < set->nimages; img++)
+ {
+ if (img > 0)
+ g_string_append (result, ", ");
+ g_string_append (result, set->images[img]->name);
+ }
+ g_string_append (result, "]");
+ return g_string_free (result, FALSE);
+}
+
/*
* Structure used by the collect_..._images functions to store the image list.
*/
case MONO_TYPE_FNPTR:
//return signature_in_image (type->data.method, image);
g_assert_not_reached ();
- case MONO_TYPE_VAR: {
- MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
- if (container) {
- g_assert (!container->is_method);
- /*
- * FIXME: The following check is here solely
- * for monodis, which uses the internal
- * function
- * mono_metadata_load_generic_params(). The
- * caller of that function needs to fill in
- * owner->klass or owner->method of the
- * returned struct, but monodis doesn't do
- * that. The image unloading depends on that,
- * however, so a crash results without this
- * check.
- */
- if (!container->owner.klass)
- add_image (container->image, data);
- else
- add_image (container->owner.klass->image, data);
- } else {
- add_image (type->data.generic_param->image, data);
- }
- }
+ case MONO_TYPE_VAR:
+ case MONO_TYPE_MVAR:
+ {
+ MonoImage *image = get_image_for_generic_param (type->data.generic_param);
+ add_image (image, data);
break;
- case MONO_TYPE_MVAR: {
- MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
- if (type->data.generic_param->image)
- add_image (type->data.generic_param->image, data);
- if (container) {
- if (!container->owner.method) {
- /* RefEmit created generic param whose method is not finished */
- add_image (container->image, data);
- } else {
- g_assert (container->is_method);
- add_image (container->owner.method->klass->image, data);
- }
- } else {
- add_image (type->data.generic_param->image, data);
- }
}
- break;
case MONO_TYPE_CLASS:
case MONO_TYPE_VALUETYPE:
add_image (mono_class_from_mono_type (type)->image, data);
MonoImageSet *set;
CollectData data;
+ memset (&helper, 0, sizeof(helper)); // act like g_new0
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.is_tb_open = is_tb_open;
- helper.cached_class = NULL;
collect_data_init (&data);
return gc;
}
-/*
+MonoGenericContainer *
+get_anonymous_container_for_image (MonoImage *image, gboolean is_mvar)
+{
+ MonoGenericContainer **container_pointer;
+ if (is_mvar)
+ container_pointer = &image->anonymous_generic_method_container;
+ else
+ container_pointer = &image->anonymous_generic_class_container;
+ MonoGenericContainer *result = *container_pointer;
+
+ // This container has never been created; make it now.
+ if (!result)
+ {
+ // Note this is never deallocated anywhere-- it exists for the lifetime of the image it's allocated from
+ result = mono_image_alloc0 (image, sizeof (MonoGenericContainer));
+ result->owner.image = image;
+ result->is_anonymous = TRUE;
+ result->is_small_param = TRUE;
+ result->is_method = is_mvar;
+
+ // If another thread already made a container, use that and leak this new one.
+ // (Technically it would currently be safe to just assign instead of CASing.)
+ MonoGenericContainer *exchange = InterlockedCompareExchangePointer ((volatile gpointer *)container_pointer, result, NULL);
+ if (exchange)
+ result = exchange;
+ }
+ return result;
+}
+
+/*
* mono_metadata_parse_generic_param:
* @generic_container: Our MonoClass's or MonoMethod's MonoGenericContainer;
* see mono_metadata_parse_type_full() for details.
generic_container = select_container (generic_container, type);
if (!generic_container) {
+ gboolean is_mvar = FALSE;
+ switch (type)
+ {
+ case MONO_TYPE_VAR:
+ break;
+ case MONO_TYPE_MVAR:
+ is_mvar = TRUE;
+ break;
+ default:
+ g_error ("Cerating generic param object with invalid MonoType"); // This is not a generic param
+ }
+
/* Create dummy MonoGenericParam */
MonoGenericParam *param;
param = mono_image_alloc0 (m, sizeof (MonoGenericParam));
param->num = index;
- param->image = m;
+ param->owner = get_anonymous_container_for_image (m, is_mvar);
return param;
}
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_CLASS: {
guint32 token;
- MonoClass *class;
+ MonoClass *klass;
token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
- class = mono_class_get_checked (m, token, error);
- type->data.klass = class;
- if (!class)
+ klass = mono_class_get_checked (m, token, error);
+ type->data.klass = klass;
+ if (!klass)
return FALSE;
- if (!compare_type_literals (m, class->byval_arg.type, type->type, error))
+ if (!compare_type_literals (m, klass->byval_arg.type, type->type, error))
return FALSE;
break;
}
if (t->byref) {
*align = MONO_ABI_ALIGNOF (gpointer);
- return sizeof (gpointer);
+ return MONO_ABI_SIZEOF (gpointer);
}
simple_type = t->type;
case MONO_TYPE_I:
case MONO_TYPE_U:
*align = MONO_ABI_ALIGNOF (gpointer);
- return sizeof (gpointer);
- case MONO_TYPE_STRING:
- *align = MONO_ABI_ALIGNOF (gpointer);
- return sizeof (gpointer);
- case MONO_TYPE_OBJECT:
- *align = MONO_ABI_ALIGNOF (gpointer);
- return sizeof (gpointer);
+ return MONO_ABI_SIZEOF (gpointer);
case MONO_TYPE_VALUETYPE: {
if (t->data.klass->enumtype)
return mono_type_size (mono_class_enum_basetype (t->data.klass), align);
else
return mono_class_value_size (t->data.klass, (guint32*)align);
}
+ case MONO_TYPE_STRING:
+ case MONO_TYPE_OBJECT:
case MONO_TYPE_CLASS:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
case MONO_TYPE_ARRAY:
*align = MONO_ABI_ALIGNOF (gpointer);
- return sizeof (gpointer);
+ return MONO_ABI_SIZEOF (gpointer);
case MONO_TYPE_TYPEDBYREF:
return mono_class_value_size (mono_defaults.typed_reference_class, (guint32*)align);
case MONO_TYPE_GENERICINST: {
return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
} else {
*align = MONO_ABI_ALIGNOF (gpointer);
- return sizeof (gpointer);
+ return MONO_ABI_SIZEOF (gpointer);
}
}
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
if (!t->data.generic_param->gshared_constraint || t->data.generic_param->gshared_constraint->type == MONO_TYPE_VALUETYPE) {
*align = MONO_ABI_ALIGNOF (gpointer);
- return sizeof (gpointer);
+ return MONO_ABI_SIZEOF (gpointer);
} else {
/* The gparam can only match types given by gshared_constraint */
return mono_type_size (t->data.generic_param->gshared_constraint, align);
int tmp;
MonoTypeEnum simple_type;
#if SIZEOF_VOID_P == SIZEOF_REGISTER
- int stack_slot_size = sizeof (gpointer);
+ int stack_slot_size = MONO_ABI_SIZEOF (gpointer);
int stack_slot_align = MONO_ABI_ALIGNOF (gpointer);
#elif SIZEOF_VOID_P < SIZEOF_REGISTER
int stack_slot_size = SIZEOF_REGISTER;
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_CLASS:
case MONO_TYPE_SZARRAY: {
- MonoClass *class = t1->data.klass;
+ MonoClass *klass = t1->data.klass;
/*
* Dynamic classes must not be hashed on their type since it can change
* during runtime. For example, if we hash a reference type that is
* This is specially problematic with generic instances since they are
* inserted in a bunch of hash tables before been finished.
*/
- if (image_is_dynamic (class->image))
- return (t1->byref << 6) | mono_metadata_str_hash (class->name);
- return ((hash << 5) - hash) ^ mono_metadata_str_hash (class->name);
+ if (image_is_dynamic (klass->image))
+ return (t1->byref << 6) | mono_metadata_str_hash (klass->name);
+ return ((hash << 5) - hash) ^ mono_metadata_str_hash (klass->name);
}
case MONO_TYPE_PTR:
return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
* image B gets that generic inst from the cache, image A is
* unloaded, so the inst is deleted, but image B still retains
* a pointer to it.
- *
- * The AOT runtime doesn't set the image when it's decoding
- * types, so we only compare it when the owner is NULL.
*/
- if (mono_generic_param_owner (p1) == mono_generic_param_owner (p2) &&
- (mono_generic_param_owner (p1) || p1->image == p2->image))
+ if (mono_generic_param_owner (p1) == mono_generic_param_owner (p2))
return TRUE;
/*
* mono_metadata_type_equal:
* @t1: a type
* @t2: another type
+ * @signature_only: If true, treat ginsts as equal which are instantiated separately but have equal positional value
*
* Determine if @t1 and @t2 represent the same type.
* Returns: #TRUE if @t1 and @t2 are equal.
params = NULL;
n = 0;
container = mono_image_alloc0 (image, sizeof (MonoGenericContainer));
- container->image = image;
+ container->owner.image = image; // Temporarily mark as anonymous, but this will be overriden by caller
+ container->is_anonymous = TRUE;
do {
n++;
params = g_realloc (params, sizeof (MonoGenericParamFull) * n);
return &method->klass->image->wrapper_caches;
}
}
+
+// This is support for the mempool reference tracking feature in checked-build, but lives in metadata.c due to use of static variables of this file.
+
+/**
+ * mono_find_image_set_owner:
+ *
+ * Find the imageset, if any, which a given pointer is located in the memory of.
+ */
+MonoImageSet *
+mono_find_image_set_owner (void *ptr)
+{
+ MonoImageSet *owner = NULL;
+ int i;
+
+ image_sets_lock ();
+
+ if (image_sets)
+ {
+ for (i = 0; !owner && i < image_sets->len; ++i) {
+ MonoImageSet *set = g_ptr_array_index (image_sets, i);
+ if (mono_mempool_contains_addr (set->mempool, ptr))
+ owner = set;
+ }
+ }
+
+ image_sets_unlock ();
+
+ return owner;
+}