2007-10-04 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / metadata / icall.c
index 0f8223ff17fd5c2c93e1f3f65ae2eaa2a6d9ef5e..c3b45edd959bdb8213eeba6a010212413813e956 100644 (file)
@@ -58,6 +58,7 @@
 #include <mono/metadata/mono-config.h>
 #include <mono/metadata/cil-coff.h>
 #include <mono/metadata/security-manager.h>
+#include <mono/metadata/security-core-clr.h>
 #include <mono/io-layer/io-layer.h>
 #include <mono/utils/strtod.h>
 #include <mono/utils/monobitset.h>
@@ -95,7 +96,7 @@ mono_double_ParseImpl (char *ptr, double *result)
                *result = strtod (ptr, &endptr);
 #else
        if (*ptr)
-               *result = bsd_strtod (ptr, &endptr);
+               *result = mono_strtod (ptr, &endptr);
 #endif
 
        if (!*ptr || (endptr && *endptr))
@@ -1682,11 +1683,11 @@ ves_icall_get_property_info (MonoReflectionProperty *property, MonoPropertyInfo
 
        if ((req_info & PInfo_GetMethod) != 0)
                info->get = property->property->get ?
-                           mono_method_get_object (domain, property->property->get, NULL): NULL;
+                           mono_method_get_object (domain, property->property->get, property->klass): NULL;
        
        if ((req_info & PInfo_SetMethod) != 0)
                info->set = property->property->set ?
-                           mono_method_get_object (domain, property->property->set, NULL): NULL;
+                           mono_method_get_object (domain, property->property->set, property->klass): NULL;
        /* 
         * There may be other methods defined for properties, though, it seems they are not exposed 
         * in the reflection API 
@@ -2005,18 +2006,18 @@ ves_icall_MonoType_GetGenericArguments (MonoReflectionType *type)
 
        if (klass->generic_container) {
                MonoGenericContainer *container = klass->generic_container;
-               res = mono_array_new (mono_object_domain (type), mono_defaults.monotype_class, container->type_argc);
+               res = mono_array_new (mono_object_domain (type), mono_defaults.systemtype_class, container->type_argc);
                for (i = 0; i < container->type_argc; ++i) {
                        pklass = mono_class_from_generic_parameter (&container->type_params [i], klass->image, FALSE);
                        mono_array_setref (res, i, mono_type_get_object (mono_object_domain (type), &pklass->byval_arg));
                }
        } else if (klass->generic_class) {
                MonoGenericInst *inst = klass->generic_class->context.class_inst;
-               res = mono_array_new (mono_object_domain (type), mono_defaults.monotype_class, inst->type_argc);
+               res = mono_array_new (mono_object_domain (type), mono_defaults.systemtype_class, inst->type_argc);
                for (i = 0; i < inst->type_argc; ++i)
                        mono_array_setref (res, i, mono_type_get_object (mono_object_domain (type), inst->type_argv [i]));
        } else {
-               res = mono_array_new (mono_object_domain (type), mono_defaults.monotype_class, 0);
+               res = mono_array_new (mono_object_domain (type), mono_defaults.systemtype_class, 0);
        }
        return res;
 }
@@ -2689,6 +2690,35 @@ ves_icall_MonoMethod_GetGenericArguments (MonoReflectionMethod *method)
        return res;
 }
 
+static void
+ensure_reflection_security (void)
+{
+       MonoMethod *m = mono_method_get_last_managed ();
+
+       while (m) {
+               /*
+               g_print ("method %s.%s.%s in image %s\n",
+                       m->klass->name_space, m->klass->name, m->name, m->klass->image->name);
+               */
+
+               /* We stop at the first method which is not in
+                  System.Reflection or which is not in a platform
+                  image. */
+               if (strcmp (m->klass->name_space, "System.Reflection") != 0 ||
+                               !mono_security_core_clr_is_platform_image (m->klass->image)) {
+                       /* If the method is transparent we throw an exception. */
+                       if (mono_security_core_clr_method_level (m, TRUE) == MONO_SECURITY_CORE_CLR_TRANSPARENT ) {
+                               MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MethodAccessException", "Reflection called from transparent code");
+
+                               mono_raise_exception (ex);
+                       }
+                       return;
+               }
+
+               mono_stack_walk_no_il (get_caller, &m);
+       }
+}
+
 static MonoObject *
 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
 {
@@ -2703,6 +2733,10 @@ ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoAr
 
        MONO_ARCH_SAVE_REGS;
 
+       if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR &&
+                       mono_security_core_clr_method_level (m, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL)
+               ensure_reflection_security ();
+
        if (!(m->flags & METHOD_ATTRIBUTE_STATIC)) {
                if (this) {
                        if (!mono_object_isinst (this, m->klass))
@@ -4024,7 +4058,7 @@ ves_icall_System_Reflection_Assembly_get_EntryPoint (MonoReflectionAssembly *ass
 }
 
 static MonoReflectionModule*
-ves_icall_System_Reflection_Assembly_get_ManifestModule (MonoReflectionAssembly *assembly) 
+ves_icall_System_Reflection_Assembly_GetManifestModuleInternal (MonoReflectionAssembly *assembly) 
 {
        return mono_module_get_object (mono_object_domain (assembly), assembly->assembly->image);
 }
@@ -4991,7 +5025,7 @@ ves_icall_System_Reflection_Module_ResolveTypeToken (MonoImage *image, guint32 t
        if (image->dynamic) {
                if (type_args || method_args)
                        mono_raise_exception (mono_get_exception_not_implemented (NULL));
-               return mono_lookup_dynamic_token (image, token);
+               return mono_lookup_dynamic_token (image, token, NULL);
        }
 
        if ((index <= 0) || (index > image->tables [table].rows)) {
@@ -5029,7 +5063,7 @@ ves_icall_System_Reflection_Module_ResolveMethodToken (MonoImage *image, guint32
                if (type_args || method_args)
                        mono_raise_exception (mono_get_exception_not_implemented (NULL));
                /* FIXME: validate memberref token type */
-               return mono_lookup_dynamic_token (image, token);
+               return mono_lookup_dynamic_token (image, token, NULL);
        }
 
        if ((index <= 0) || (index > image->tables [table].rows)) {
@@ -5061,7 +5095,7 @@ ves_icall_System_Reflection_Module_ResolveStringToken (MonoImage *image, guint32
        }
 
        if (image->dynamic)
-               return mono_lookup_dynamic_token (image, token);
+               return mono_lookup_dynamic_token (image, token, NULL);
 
        if ((index <= 0) || (index >= image->heap_us.size)) {
                *error = ResolveTokenError_OutOfRange;
@@ -5094,7 +5128,7 @@ ves_icall_System_Reflection_Module_ResolveFieldToken (MonoImage *image, guint32
                if (type_args || method_args)
                        mono_raise_exception (mono_get_exception_not_implemented (NULL));
                /* FIXME: validate memberref token type */
-               return mono_lookup_dynamic_token (image, token);
+               return mono_lookup_dynamic_token (image, token, NULL);
        }
 
        if ((index <= 0) || (index > image->tables [table].rows)) {
@@ -6223,7 +6257,7 @@ ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (void)
 }
 
 static MonoString *
-ves_icall_System_Configuration_DefaultConfig_get_bundled_machine_config (void)
+get_bundled_machine_config (void)
 {
        const gchar *machine_config;