Merge pull request #1404 from woodsb02/mono-route
[mono.git] / mono / metadata / icall.c
index ca2781451e0549ea01d99c9f1e57fe0b60909ab2..48eb780b944c1ff25a581301940af5ac116ff2d6 100644 (file)
@@ -45,6 +45,7 @@
 #include <mono/metadata/exception.h>
 #include <mono/metadata/file-io.h>
 #include <mono/metadata/console-io.h>
+#include <mono/metadata/mono-route.h>
 #include <mono/metadata/socket-io.h>
 #include <mono/metadata/mono-endian.h>
 #include <mono/metadata/tokentype.h>
@@ -129,9 +130,9 @@ mono_double_ParseImpl (char *ptr, double *result)
 
        if (*ptr){
                /* mono_strtod () is not thread-safe */
-               EnterCriticalSection (&mono_strtod_mutex);
+               mono_mutex_lock (&mono_strtod_mutex);
                *result = mono_strtod (ptr, &endptr);
-               LeaveCriticalSection (&mono_strtod_mutex);
+               mono_mutex_unlock (&mono_strtod_mutex);
        }
 
        if (!*ptr || (endptr && *endptr))
@@ -907,11 +908,12 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (Mo
 ICALL_EXPORT void
 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunModuleConstructor (MonoImage *image)
 {
-       MONO_ARCH_SAVE_REGS;
+       MonoError error;
 
        mono_image_check_for_module_cctor (image);
        if (image->has_module_cctor) {
-               MonoClass *module_klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | 1);
+               MonoClass *module_klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | 1, &error);
+               mono_error_raise_exception (&error);
                /*It's fine to raise the exception here*/
                mono_runtime_class_init (mono_class_vtable_full (mono_domain_get (), module_klass, TRUE));
        }
@@ -1613,12 +1615,17 @@ ves_icall_System_Reflection_FieldInfo_get_marshal_info (MonoReflectionField *fie
 {
        MonoClass *klass = field->field->parent;
        MonoMarshalType *info;
+       MonoType *ftype;
        int i;
 
        if (klass->generic_container ||
            (klass->generic_class && klass->generic_class->context.class_inst->is_open))
                return NULL;
 
+       ftype = mono_field_get_type (field->field);
+       if (ftype && !(ftype->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL))
+               return NULL;
+
        info = mono_marshal_load_type_info (klass);
 
        for (i = 0; i < info->num_fields; ++i) {
@@ -1668,8 +1675,7 @@ ves_icall_System_Reflection_FieldInfo_GetTypeModifiers (MonoReflectionField *fie
 {
        MonoError error;
        MonoType *type = mono_field_get_type_checked (field->field, &error);
-       if (!mono_error_ok (&error))
-               mono_error_raise_exception (&error);
+       mono_error_raise_exception (&error);
 
        return type_array_from_modifiers (field->field->parent->image, type, optional);
 }
@@ -1744,6 +1750,7 @@ ves_icall_MonoField_GetFieldOffset (MonoReflectionField *field)
        MonoClass *parent = field->field->parent;
        if (!parent->size_inited)
                mono_class_init (parent);
+       mono_class_setup_fields_locking (parent);
 
        return field->field->offset - sizeof (MonoObject);
 }
@@ -1891,15 +1898,24 @@ ves_icall_MonoField_GetRawConstantValue (MonoReflectionField *this)
        if (!(t->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT))
                mono_raise_exception (mono_get_exception_invalid_operation (NULL));
 
-       if (field->parent->image->dynamic) {
-               /* FIXME: */
-               g_assert_not_reached ();
+       if (image_is_dynamic (field->parent->image)) {
+               MonoClass *klass = field->parent;
+               int fidx = field - klass->fields;
+
+               g_assert (fidx >= 0 && fidx < klass->field.count);
+               g_assert (klass->ext);
+               g_assert (klass->ext->field_def_values);
+               def_type = klass->ext->field_def_values [fidx].def_type;
+               def_value = klass->ext->field_def_values [fidx].data;
+               if (def_type == MONO_TYPE_END)
+                       mono_raise_exception (mono_get_exception_invalid_operation (NULL));
+       } else {
+               def_value = mono_class_get_field_default_value (field, &def_type);
+               /* FIXME, maybe we should try to raise TLE if field->parent is broken */
+               if (!def_value)
+                       mono_raise_exception (mono_get_exception_invalid_operation (NULL));
        }
 
-       def_value = mono_class_get_field_default_value (field, &def_type);
-       if (!def_value) /*FIXME, maybe we should try to raise TLE if field->parent is broken */
-               mono_raise_exception (mono_get_exception_invalid_operation (NULL));
-
        /*FIXME unify this with reflection.c:mono_get_object_from_blob*/
        switch (def_type) {
        case MONO_TYPE_U1:
@@ -2171,7 +2187,7 @@ ves_icall_Type_GetPacking (MonoReflectionType *type, guint32 *packing, guint32 *
        MonoClass *klass = mono_class_from_mono_type (type->type);
        mono_class_init_or_throw (klass);
 
-       if (klass->image->dynamic) {
+       if (image_is_dynamic (klass->image)) {
                MonoReflectionTypeBuilder *tb = (MonoReflectionTypeBuilder*)type;
                *packing = tb->packing_size;
                *size = tb->class_size;
@@ -2270,12 +2286,15 @@ ves_icall_MonoType_get_DeclaringType (MonoReflectionType *type)
 
        if (type->type->byref)
                return NULL;
-       if (type->type->type == MONO_TYPE_VAR)
-               class = mono_type_get_generic_param_owner (type->type)->owner.klass;
-       else if (type->type->type == MONO_TYPE_MVAR)
-               class = mono_type_get_generic_param_owner (type->type)->owner.method->klass;
-       else
+       if (type->type->type == MONO_TYPE_VAR) {
+               MonoGenericContainer *param = mono_type_get_generic_param_owner (type->type);
+               class = param ? param->owner.klass : NULL;
+       } else if (type->type->type == MONO_TYPE_MVAR) {
+               MonoGenericContainer *param = mono_type_get_generic_param_owner (type->type);
+               class = param ? param->owner.method->klass : NULL;
+       } else {
                class = mono_class_from_mono_type (type->type)->nested_in;
+       }
 
        return class ? mono_type_get_object (domain, &class->byval_arg) : NULL;
 }
@@ -2604,7 +2623,7 @@ ves_icall_MonoMethod_GetDllImportAttribute (MonoMethod *method)
                g_assert (DllImportAttributeClass);
        }
                                                                                                                
-       if (method->klass->image->dynamic) {
+       if (image_is_dynamic (method->klass->image)) {
                MonoReflectionMethodAux *method_aux = 
                        g_hash_table_lookup (
                                                                          ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
@@ -2668,7 +2687,7 @@ ves_icall_MonoMethod_GetGenericMethodDefinition (MonoReflectionMethod *method)
        if (!result->is_generic)
                return NULL;
 
-       if (method->method->klass->image->dynamic) {
+       if (image_is_dynamic (method->method->klass->image)) {
                MonoDynamicImage *image = (MonoDynamicImage*)method->method->klass->image;
                MonoReflectionMethod *res;
 
@@ -2819,7 +2838,7 @@ ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoAr
                return NULL;
        }
 
-       if (image->dynamic && !((MonoDynamicImage*)image)->run) {
+       if (image_is_dynamic (image) && !((MonoDynamicImage*)image)->run) {
                mono_gc_wbarrier_generic_store (exc, (MonoObject*) mono_get_exception_not_supported ("Cannot invoke a method in a dynamic assembly without run access."));
                return NULL;
        }
@@ -2834,6 +2853,18 @@ ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoAr
                for (i = 0; i < pcount; ++i)
                        lengths [i] = *(int32_t*) ((char*)mono_array_get (params, gpointer, i) + sizeof (MonoObject));
 
+               if (m->klass->rank == 1 && sig->param_count == 2 && m->klass->element_class->rank) {
+                       /* This is a ctor for jagged arrays. MS creates an array of arrays. */
+                       MonoArray *arr = mono_array_new_full (mono_object_domain (params), m->klass, lengths, NULL);
+
+                       for (i = 0; i < mono_array_length (arr); ++i) {
+                               MonoArray *subarray = mono_array_new_full (mono_object_domain (params), m->klass->element_class, &lengths [1], NULL);
+
+                               mono_array_setref_fast (arr, i, subarray);
+                       }
+                       return (MonoObject*)arr;
+               }
+
                if (m->klass->rank == pcount) {
                        /* Only lengths provided. */
                        lower_bounds = NULL;
@@ -2983,10 +3014,12 @@ static guint64
 read_enum_value (char *mem, int type)
 {
        switch (type) {
+       case MONO_TYPE_BOOLEAN:
        case MONO_TYPE_U1:
                return *(guint8*)mem;
        case MONO_TYPE_I1:
                return *(gint8*)mem;
+       case MONO_TYPE_CHAR:
        case MONO_TYPE_U2:
                return *(guint16*)mem;
        case MONO_TYPE_I2:
@@ -3062,7 +3095,7 @@ ves_icall_System_Enum_ToObject (MonoReflectionType *enumType, MonoObject *value)
 
        if (!enumc->enumtype)
                mono_raise_exception (mono_get_exception_argument ("enumType", "Type provided must be an Enum."));
-       if (!((objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 && objc->byval_arg.type <= MONO_TYPE_U8)))
+       if (!((objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_BOOLEAN && objc->byval_arg.type <= MONO_TYPE_U8)))
                mono_raise_exception (mono_get_exception_argument ("value", "The value passed in must be an enum base or an underlying type for an enum, such as an Int32."));
 
        etype = mono_class_enum_basetype (enumc);
@@ -4165,7 +4198,7 @@ ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssembly *as
                        type = NULL;
        }
        else
-               if (assembly->assembly->dynamic) {
+               if (assembly_is_dynamic (assembly->assembly)) {
                        /* Enumerate all modules */
                        MonoReflectionAssemblyBuilder *abuilder = (MonoReflectionAssemblyBuilder*)assembly;
                        int i;
@@ -4715,7 +4748,7 @@ ves_icall_System_Reflection_Assembly_GetModulesInternal (MonoReflectionAssembly
        MonoImage *image = assembly->assembly->image;
 
        g_assert (image != NULL);
-       g_assert (!assembly->assembly->dynamic);
+       g_assert (!assembly_is_dynamic (assembly->assembly));
 
        table = &image->tables [MONO_TABLE_FILE];
        file_count = table->rows;
@@ -5152,21 +5185,16 @@ mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArray **excepti
                attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
                visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
                if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
-                       klass = mono_class_get (image, (i + 1) | MONO_TOKEN_TYPE_DEF);
+                       MonoError error;
+                       klass = mono_class_get_checked (image, (i + 1) | MONO_TOKEN_TYPE_DEF, &error);
+                       g_assert (!mono_loader_get_last_error ()); /* Plug any leaks */
+                       
                        if (klass) {
                                mono_array_setref (res, count, mono_type_get_object (domain, &klass->byval_arg));
                        } else {
-                               MonoLoaderError *error;
-                               MonoException *ex;
-                               
-                               error = mono_loader_get_last_error ();
-                               g_assert (error != NULL);
-       
-                               ex = mono_loader_error_prepare_exception (error);
+                               MonoException *ex = mono_error_convert_to_exception (&error);
                                mono_array_setref (*exceptions, count, ex);
                        }
-                       if (mono_loader_get_last_error ())
-                               mono_loader_clear_error ();
                        count++;
                }
        }
@@ -5189,7 +5217,7 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
 
        domain = mono_object_domain (assembly);
 
-       g_assert (!assembly->assembly->dynamic);
+       g_assert (!assembly_is_dynamic (assembly->assembly));
        image = assembly->assembly->image;
        table = &image->tables [MONO_TABLE_FILE];
        res = mono_module_get_types (domain, image, &exceptions, exportedOnly);
@@ -5257,7 +5285,7 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
                mono_loader_clear_error ();
 
                exl = mono_array_new (domain, mono_defaults.exception_class, length);
-               /* Types for which mono_class_get () succeeded */
+               /* Types for which mono_class_get_checked () succeeded */
                for (i = 0, tmp = list; tmp; i++, tmp = tmp->next) {
                        MonoException *exc = mono_class_get_exception_for_failure (tmp->data);
                        mono_array_setref (exl, i, exc);
@@ -5312,6 +5340,7 @@ ves_icall_System_Reflection_AssemblyName_ParseName (MonoReflectionAssemblyName *
 ICALL_EXPORT MonoReflectionType*
 ves_icall_System_Reflection_Module_GetGlobalType (MonoReflectionModule *module)
 {
+       MonoError error;
        MonoDomain *domain = mono_object_domain (module); 
        MonoClass *klass;
 
@@ -5319,11 +5348,12 @@ ves_icall_System_Reflection_Module_GetGlobalType (MonoReflectionModule *module)
 
        g_assert (module->image);
 
-       if (module->image->dynamic && ((MonoDynamicImage*)(module->image))->initial_image)
+       if (image_is_dynamic (module->image) && ((MonoDynamicImage*)(module->image))->initial_image)
                /* These images do not have a global type */
                return NULL;
 
-       klass = mono_class_get (module->image, 1 | MONO_TOKEN_TYPE_DEF);
+       klass = mono_class_get_checked (module->image, 1 | MONO_TOKEN_TYPE_DEF, &error);
+       mono_error_raise_exception (&error);
        return mono_type_get_object (domain, &klass->byval_arg);
 }
 
@@ -5359,7 +5389,7 @@ ves_icall_System_Reflection_Module_GetHINSTANCE (MonoReflectionModule *module)
 ICALL_EXPORT void
 ves_icall_System_Reflection_Module_GetPEKind (MonoImage *image, gint32 *pe_kind, gint32 *machine)
 {
-       if (image->dynamic) {
+       if (image_is_dynamic (image)) {
                MonoDynamicImage *dyn = (MonoDynamicImage*)image;
                *pe_kind = dyn->pe_kind;
                *machine = dyn->machine;
@@ -5400,7 +5430,7 @@ ves_icall_System_Reflection_Module_InternalGetTypes (MonoReflectionModule *modul
 static gboolean
 mono_memberref_is_method (MonoImage *image, guint32 token)
 {
-       if (!image->dynamic) {
+       if (!image_is_dynamic (image)) {
                guint32 cols [MONO_MEMBERREF_SIZE];
                const char *sig;
                mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
@@ -5433,23 +5463,24 @@ init_generic_context_from_args (MonoGenericContext *context, MonoArray *type_arg
 }
 
 ICALL_EXPORT MonoType*
-ves_icall_System_Reflection_Module_ResolveTypeToken (MonoImage *image, guint32 token, MonoArray *type_args, MonoArray *method_args, MonoResolveTokenError *error)
+ves_icall_System_Reflection_Module_ResolveTypeToken (MonoImage *image, guint32 token, MonoArray *type_args, MonoArray *method_args, MonoResolveTokenError *resolve_error)
 {
        MonoClass *klass;
        int table = mono_metadata_token_table (token);
        int index = mono_metadata_token_index (token);
        MonoGenericContext context;
+       MonoError error;
 
-       *error = ResolveTokenError_Other;
+       *resolve_error = ResolveTokenError_Other;
 
        /* Validate token */
        if ((table != MONO_TABLE_TYPEDEF) && (table != MONO_TABLE_TYPEREF) && 
                (table != MONO_TABLE_TYPESPEC)) {
-               *error = ResolveTokenError_BadTable;
+               *resolve_error = ResolveTokenError_BadTable;
                return NULL;
        }
 
-       if (image->dynamic) {
+       if (image_is_dynamic (image)) {
                if ((table == MONO_TABLE_TYPEDEF) || (table == MONO_TABLE_TYPEREF)) {
                        klass = mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL);
                        return klass ? &klass->byval_arg : NULL;
@@ -5461,15 +5492,15 @@ ves_icall_System_Reflection_Module_ResolveTypeToken (MonoImage *image, guint32 t
        }
 
        if ((index <= 0) || (index > image->tables [table].rows)) {
-               *error = ResolveTokenError_OutOfRange;
+               *resolve_error = ResolveTokenError_OutOfRange;
                return NULL;
        }
 
        init_generic_context_from_args (&context, type_args, method_args);
-       klass = mono_class_get_full (image, token, &context);
-
-       if (mono_loader_get_last_error ())
-               mono_raise_exception (mono_loader_error_prepare_exception (mono_loader_get_last_error ()));
+       klass = mono_class_get_checked (image, token, &error);
+       if (klass)
+               klass = mono_class_inflate_generic_class_checked (klass, &context, &error);
+       mono_error_raise_exception (&error);
 
        if (klass)
                return &klass->byval_arg;
@@ -5494,7 +5525,7 @@ ves_icall_System_Reflection_Module_ResolveMethodToken (MonoImage *image, guint32
                return NULL;
        }
 
-       if (image->dynamic) {
+       if (image_is_dynamic (image)) {
                if (table == MONO_TABLE_METHOD)
                        return mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL);
 
@@ -5538,7 +5569,7 @@ ves_icall_System_Reflection_Module_ResolveStringToken (MonoImage *image, guint32
                return NULL;
        }
 
-       if (image->dynamic)
+       if (image_is_dynamic (image))
                return mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL);
 
        if ((index <= 0) || (index >= image->heap_us.size)) {
@@ -5552,28 +5583,29 @@ ves_icall_System_Reflection_Module_ResolveStringToken (MonoImage *image, guint32
 }
 
 ICALL_EXPORT MonoClassField*
-ves_icall_System_Reflection_Module_ResolveFieldToken (MonoImage *image, guint32 token, MonoArray *type_args, MonoArray *method_args, MonoResolveTokenError *error)
+ves_icall_System_Reflection_Module_ResolveFieldToken (MonoImage *image, guint32 token, MonoArray *type_args, MonoArray *method_args, MonoResolveTokenError *resolve_error)
 {
+       MonoError error;
        MonoClass *klass;
        int table = mono_metadata_token_table (token);
        int index = mono_metadata_token_index (token);
        MonoGenericContext context;
        MonoClassField *field;
 
-       *error = ResolveTokenError_Other;
+       *resolve_error = ResolveTokenError_Other;
 
        /* Validate token */
        if ((table != MONO_TABLE_FIELD) && (table != MONO_TABLE_MEMBERREF)) {
-               *error = ResolveTokenError_BadTable;
+               *resolve_error = ResolveTokenError_BadTable;
                return NULL;
        }
 
-       if (image->dynamic) {
+       if (image_is_dynamic (image)) {
                if (table == MONO_TABLE_FIELD)
                        return mono_lookup_dynamic_token_class (image, token, FALSE, NULL, NULL);
 
                if (mono_memberref_is_method (image, token)) {
-                       *error = ResolveTokenError_BadTable;
+                       *resolve_error = ResolveTokenError_BadTable;
                        return NULL;
                }
 
@@ -5582,19 +5614,17 @@ ves_icall_System_Reflection_Module_ResolveFieldToken (MonoImage *image, guint32
        }
 
        if ((index <= 0) || (index > image->tables [table].rows)) {
-               *error = ResolveTokenError_OutOfRange;
+               *resolve_error = ResolveTokenError_OutOfRange;
                return NULL;
        }
        if ((table == MONO_TABLE_MEMBERREF) && (mono_memberref_is_method (image, token))) {
-               *error = ResolveTokenError_BadTable;
+               *resolve_error = ResolveTokenError_BadTable;
                return NULL;
        }
 
        init_generic_context_from_args (&context, type_args, method_args);
-       field = mono_field_from_token (image, token, &klass, &context);
-
-       if (mono_loader_get_last_error ())
-               mono_raise_exception (mono_loader_error_prepare_exception (mono_loader_get_last_error ()));
+       field = mono_field_from_token_checked (image, token, &klass, &context, &error);
+       mono_error_raise_exception (&error);
        
        return field;
 }
@@ -5672,7 +5702,7 @@ ves_icall_System_Reflection_Module_ResolveSignature (MonoImage *image, guint32 t
        if (table != MONO_TABLE_STANDALONESIG)
                return NULL;
 
-       if (image->dynamic)
+       if (image_is_dynamic (image))
                return NULL;
 
        if ((idx == 0) || (idx > tables [MONO_TABLE_STANDALONESIG].rows))
@@ -5844,7 +5874,7 @@ ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, Mon
 
        delegate = mono_object_new (mono_object_domain (type), delegate_class);
 
-       if (method->dynamic) {
+       if (method_is_dynamic (method)) {
                /* Creating a trampoline would leak memory */
                func = mono_compile_method (method);
        } else {
@@ -7403,15 +7433,11 @@ ves_icall_System_Char_GetDataTablePointers (int category_data_version,
        *to_upper_data_high = ToUpperDataHigh;
 }
 
-ICALL_EXPORT gint32
-ves_icall_MonoDebugger_GetMethodToken (MonoReflectionMethod *method)
-{
-       return method->method->token;
-}
-
 /*
  * We return NULL for no modifiers so the corlib code can return Type.EmptyTypes
  * and avoid useless allocations.
+ * 
+ * MAY THROW
  */
 static MonoArray*
 type_array_from_modifiers (MonoImage *image, MonoType *type, int optional)
@@ -7428,7 +7454,9 @@ type_array_from_modifiers (MonoImage *image, MonoType *type, int optional)
        count = 0;
        for (i = 0; i < type->num_mods; ++i) {
                if ((optional && !type->modifiers [i].required) || (!optional && type->modifiers [i].required)) {
-                       MonoClass *klass = mono_class_get (image, type->modifiers [i].token);
+                       MonoError error;
+                       MonoClass *klass = mono_class_get_checked (image, type->modifiers [i].token, &error);
+                       mono_error_raise_exception (&error); /* this is safe, no cleanup needed on callers */ 
                        mono_array_setref (res, count, mono_type_get_object (mono_domain_get (), &klass->byval_arg));
                        count++;
                }