#include <mono/metadata/verify-internals.h>
#include <mono/metadata/mono-debug.h>
#include <mono/utils/mono-counters.h>
+#include <mono/utils/mono-string.h>
MonoStats mono_stats;
gboolean explicit_size;
MonoClassField *field;
MonoGenericContainer *container = NULL;
- MonoClass *gklass = NULL;
+ MonoClass *gtd = class->generic_class ? mono_class_get_generic_type_definition (class) : NULL;
if (class->size_inited)
return;
return;
}
- if (class->generic_class) {
- MonoClass *gklass = class->generic_class->container_class;
- mono_class_setup_fields (gklass);
- top = gklass->field.count;
- class->field.first = gklass->field.first;
- class->field.count = gklass->field.count;
+ if (gtd) {
+ mono_class_setup_fields (gtd);
+ if (gtd->exception_type) {
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
+ return;
+ }
+
+ top = gtd->field.count;
+ class->field.first = gtd->field.first;
+ class->field.count = gtd->field.count;
}
class->instance_size = 0;
if (class->generic_container) {
container = class->generic_container;
- } else if (class->generic_class) {
- gklass = class->generic_class->container_class;
- container = gklass->generic_container;
+ } else if (gtd) {
+ container = gtd->generic_container;
g_assert (container);
-
- mono_class_setup_fields (gklass);
-
- if (gklass->exception_type) {
- mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
- return;
- }
}
/*
field->parent = class;
- if (class->generic_class) {
- MonoClassField *gfield = &gklass->fields [i];
+ if (gtd) {
+ MonoClassField *gfield = >d->fields [i];
field->name = mono_field_get_name (gfield);
/*This memory must come from the image mempool as we don't have a chance to free it.*/
return -1;
}
+/*
+ * Return the number of virtual methods.
+ * Even for interfaces we can't simply return the number of methods as all CLR types are allowed to have static methods.
+ * FIXME It would be nice if this information could be cached somewhere.
+ */
+static int
+count_virtual_methods (MonoClass *class)
+{
+ int i, count = 0;
+ guint32 flags;
+ class = mono_class_get_generic_type_definition (class); /*We can find this information by looking at the GTD*/
+
+ if (class->methods || !MONO_CLASS_HAS_STATIC_METADATA (class)) {
+ mono_class_setup_methods (class);
+
+ for (i = 0; i < class->method.count; ++i) {
+ flags = class->methods [i]->flags;
+ if (flags & METHOD_ATTRIBUTE_VIRTUAL)
+ ++count;
+ }
+ } else {
+ for (i = 0; i < class->method.count; ++i) {
+ flags = mono_metadata_decode_table_row_col (class->image, MONO_TABLE_METHOD, class->method.first + i, MONO_METHOD_FLAGS);
+
+ if (flags & METHOD_ATTRIBUTE_VIRTUAL)
+ ++count;
+ }
+ }
+ return count;
+}
+
/*
* LOCKING: this is supposed to be called with the loader lock held.
*/
interface_offsets_full [i] = -1;
}
- ifaces = mono_class_get_implemented_interfaces (class);
- if (ifaces) {
- for (i = 0; i < ifaces->len; ++i) {
- ic = g_ptr_array_index (ifaces, i);
- interfaces_full [ic->interface_id] = ic;
- interface_offsets_full [ic->interface_id] = cur_slot;
- cur_slot += ic->method.count;
- }
- g_ptr_array_free (ifaces, TRUE);
- }
-
for (k = class->parent; k ; k = k->parent) {
ifaces = mono_class_get_implemented_interfaces (k);
if (ifaces) {
for (i = 0; i < ifaces->len; ++i) {
+ int io;
ic = g_ptr_array_index (ifaces, i);
-
- if (interface_offsets_full [ic->interface_id] == -1) {
- int io = mono_class_interface_offset (k, ic);
-
- g_assert (io >= 0);
-
- interfaces_full [ic->interface_id] = ic;
- interface_offsets_full [ic->interface_id] = io;
- }
+
+ /*Force the sharing of interface offsets between parent and subtypes.*/
+ io = mono_class_interface_offset (k, ic);
+ g_assert (io >= 0);
+ interfaces_full [ic->interface_id] = ic;
+ interface_offsets_full [ic->interface_id] = io;
}
g_ptr_array_free (ifaces, TRUE);
}
}
+
+ ifaces = mono_class_get_implemented_interfaces (class);
+ if (ifaces) {
+ for (i = 0; i < ifaces->len; ++i) {
+ ic = g_ptr_array_index (ifaces, i);
+ if (interfaces_full [ic->interface_id] != NULL)
+ continue;
+ interfaces_full [ic->interface_id] = ic;
+ interface_offsets_full [ic->interface_id] = cur_slot;
+ cur_slot += count_virtual_methods (ic);
+ }
+ g_ptr_array_free (ifaces, TRUE);
+ }
+
if (MONO_CLASS_IS_INTERFACE (class)) {
interfaces_full [class->interface_id] = class;
interface_offsets_full [class->interface_id] = cur_slot;
#define DEBUG_INTERFACE_VTABLE_CODE 0
#define TRACE_INTERFACE_VTABLE_CODE 0
#define VERIFY_INTERFACE_VTABLE_CODE 0
+#define VTABLE_SELECTOR (1)
#if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
#define DEBUG_INTERFACE_VTABLE(stmt) do {\
+ if (!(VTABLE_SELECTOR)) break; \
stmt;\
} while (0)
#else
#if TRACE_INTERFACE_VTABLE_CODE
#define TRACE_INTERFACE_VTABLE(stmt) do {\
+ if (!(VTABLE_SELECTOR)) break; \
stmt;\
} while (0)
#else
#if VERIFY_INTERFACE_VTABLE_CODE
#define VERIFY_INTERFACE_VTABLE(stmt) do {\
+ if (!(VTABLE_SELECTOR)) break; \
stmt;\
} while (0)
#else
}
}
+static gboolean
+verify_class_overrides (MonoClass *class, MonoMethod **overrides, int onum)
+{
+ int i;
+
+ for (i = 0; i < onum; ++i) {
+ MonoMethod *decl = overrides [i * 2];
+ MonoMethod *body = overrides [i * 2 + 1];
+
+ if (mono_class_get_generic_type_definition (body->klass) != mono_class_get_generic_type_definition (class)) {
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method belongs to a different class than the declared one"));
+ return FALSE;
+ }
+
+ if (!(body->flags & METHOD_ATTRIBUTE_VIRTUAL) || (body->flags & METHOD_ATTRIBUTE_STATIC)) {
+ if (body->flags & METHOD_ATTRIBUTE_STATIC)
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method must not be static to override a base type"));
+ else
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method must be virtual to override a base type"));
+ return FALSE;
+ }
+
+ if (!(decl->flags & METHOD_ATTRIBUTE_VIRTUAL) || (decl->flags & METHOD_ATTRIBUTE_STATIC)) {
+ if (body->flags & METHOD_ATTRIBUTE_STATIC)
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Cannot override a static method in a base type"));
+ else
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Cannot override a non virtual method in a base type"));
+ return FALSE;
+ }
+
+ if (!mono_class_is_assignable_from_slow (decl->klass, class)) {
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method overrides a class or interface that extended or implemented by this type"));
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
/*
* LOCKING: this is supposed to be called with the loader lock held.
*/
if (class->vtable)
return;
+ if (overrides && !verify_class_overrides (class, overrides, onum))
+ return;
+
ifaces = mono_class_get_implemented_interfaces (class);
+
if (ifaces) {
for (i = 0; i < ifaces->len; i++) {
MonoClass *ic = g_ptr_array_index (ifaces, i);
cm->slot = slot;
}
+ /*Non final newslot methods must be given a non-interface vtable slot*/
+ if ((cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && !(cm->flags & METHOD_ATTRIBUTE_FINAL) && cm->slot >= 0)
+ cm->slot = -1;
+
if (cm->slot < 0)
cm->slot = cur_slot++;
g_hash_table_destroy (override_map);
}
+ /* Ensure that all vtable slots are filled with concrete instance methods */
+ if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
+ for (i = 0; i < cur_slot; ++i) {
+ if (vtable [i] == NULL || (vtable [i]->flags & (METHOD_ATTRIBUTE_ABSTRACT | METHOD_ATTRIBUTE_STATIC))) {
+ char *type_name = mono_type_get_full_name (class);
+ char *method_name = vtable [i] ? mono_method_full_name (vtable [i], TRUE) : g_strdup ("none");
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Type %s has invalid vtable method slot %d with method %s", type_name, i, method_name));
+ g_free (type_name);
+ g_free (method_name);
+ return;
+ }
+ }
+ }
+
if (class->generic_class) {
MonoClass *gklass = class->generic_class->container_class;
class->init_pending = 1;
+ if (class->byval_arg.type == MONO_TYPE_ARRAY || class->byval_arg.type == MONO_TYPE_SZARRAY) {
+ MonoClass *element_class = class->element_class;
+ if (!element_class->inited)
+ mono_class_init (element_class);
+ if (element_class->exception_type != MONO_EXCEPTION_NONE) {
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
+ goto fail;
+ }
+ }
+
/* CAS - SecurityAction.InheritanceDemand */
if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
mono_secman_inheritancedemand_class (class, class->parent);
class->has_cctor = gklass->has_cctor;
mono_class_setup_vtable (gklass);
- if (gklass->exception_type)
+ if (gklass->exception_type) {
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
goto fail;
+ }
class->vtable_size = gklass->vtable_size;
} else {
if (class->parent) {
/* This will compute class->parent->vtable_size for some classes */
mono_class_init (class->parent);
- if (class->parent->exception_type || mono_loader_get_last_error ())
+ if (class->parent->exception_type) {
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
+ goto fail;
+ }
+ if (mono_loader_get_last_error ())
goto fail;
if (!class->parent->vtable_size) {
/* FIXME: Get rid of this somehow */
mono_class_setup_vtable (class->parent);
- if (class->parent->exception_type || mono_loader_get_last_error ())
+ if (class->parent->exception_type) {
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
+ goto fail;
+ }
+ if (mono_loader_get_last_error ())
goto fail;
}
setup_interface_offsets (class, class->parent->vtable_size);
if (!enum_basetype) {
mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
mono_loader_unlock ();
+ mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
return NULL;
}
class->cast_class = class->element_class = mono_class_from_mono_type (enum_basetype);
* We must do this after the class has been constructed to make certain recursive scenarios
* work.
*/
- if (class->generic_container)
- mono_metadata_load_generic_param_constraints (
- image, type_token, class->generic_container);
+ if (class->generic_container && !mono_metadata_load_generic_param_constraints_full (image, type_token, class->generic_container)){
+ char *class_name = g_strdup_printf("%s.%s", class->name_space, class->name);
+ char *error = concat_two_strings_with_zero (class->image, class_name, class->image->assembly_name);
+ mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, error);
+ g_free (class_name);
+ mono_loader_unlock ();
+ mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
+ return NULL;
+ }
if (class->image->assembly_name && !strcmp (class->image->assembly_name, "Mono.Simd") && !strcmp (nspace, "Mono.Simd")) {
if (!strncmp (name, "Vector", 6))
if (!type) {
char *name = mono_class_name_from_token (image, type_token);
char *assembly = mono_assembly_name_from_token (image, type_token);
- if (inflated)
- mono_metadata_free_type (type);
mono_loader_set_error_type_load (name, assembly);
+ return NULL;
}
if (inflated) {
char *name = (char*)key;
FindUserData *data = (FindUserData*)user_data;
- if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
+ if (!data->value && (mono_utf8_strcasecmp (name, (char*)data->key) == 0))
data->value = value;
}
continue;
n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
- if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
+ if (mono_utf8_strcasecmp (n, name) == 0 && mono_utf8_strcasecmp (nspace, name_space) == 0)
return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
}
return NULL;
return mono_class_has_parent (oklass, klass);
}
+/*Check if @candidate implements the interface @target*/
+static gboolean
+mono_class_implement_interface_slow (MonoClass *target, MonoClass *candidate)
+{
+ int i;
+
+ do {
+ if (candidate == target)
+ return TRUE;
+
+ /*A TypeBuilder can have more interfaces on tb->interfaces than on candidate->interfaces*/
+ if (candidate->image->dynamic && !candidate->wastypebuilder) {
+ MonoReflectionTypeBuilder *tb = candidate->reflection_info;
+ int j;
+ if (tb->interfaces) {
+ for (j = mono_array_length (tb->interfaces) - 1; j >= 0; --j) {
+ MonoReflectionType *iface = mono_array_get (tb->interfaces, MonoReflectionType*, j);
+ MonoClass *iface_class = mono_class_from_mono_type (iface->type);
+ if (iface_class == target || mono_class_implement_interface_slow (target, iface_class))
+ return TRUE;
+ }
+ }
+ } else {
+ /*setup_interfaces don't mono_class_init anything*/
+ mono_class_setup_interfaces (candidate);
+ for (i = 0; i < candidate->interface_count; ++i) {
+ if (candidate->interfaces [i] == target || mono_class_implement_interface_slow (target, candidate->interfaces [i]))
+ return TRUE;
+ }
+ }
+ candidate = candidate->parent;
+ } while (candidate);
+
+ return FALSE;
+}
+
+/*
+ * Check if @oklass can be assigned to @klass.
+ * This function does the same as mono_class_is_assignable_from but is safe to be used from mono_class_init context.
+ */
+gboolean
+mono_class_is_assignable_from_slow (MonoClass *target, MonoClass *candidate)
+{
+ if (candidate == target)
+ return TRUE;
+ if (target == mono_defaults.object_class)
+ return TRUE;
+
+ /*setup_supertypes don't mono_class_init anything */
+ mono_class_setup_supertypes (candidate);
+ mono_class_setup_supertypes (target);
+
+ if (mono_class_has_parent (candidate, target))
+ return TRUE;
+
+ /*If target is not an interface there is no need to check them.*/
+ if (!MONO_CLASS_IS_INTERFACE (target))
+ return FALSE;
+ return mono_class_implement_interface_slow (target, candidate);
+}
+
/**
* mono_class_get_cctor:
* @klass: A MonoClass pointer
if (member_klass->nested_in && !can_access_type (access_klass, member_klass->nested_in))
return FALSE;
+ /*Non nested type with nested visibility. We just fail it.*/
+ if (access_level >= TYPE_ATTRIBUTE_NESTED_PRIVATE && access_level <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM && member_klass->nested_in == NULL)
+ return FALSE;
+
switch (access_level) {
case TYPE_ATTRIBUTE_NOT_PUBLIC:
return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);