2009-06-04 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / verify.c
index e6b039696d526677eedb82c7fe452ac2fbbbc5e4..dd71ec610f07f1e3bd142449a4198ad3766d2ecb 100644 (file)
@@ -7,6 +7,7 @@
  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
+#include <config.h>
 
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/verify.h>
 #include <ctype.h>
 
 
+static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
+static gboolean verify_all = FALSE;
+
+/*
+ * Set the desired level of checks for the verfier.
+ * 
+ */
+void
+mono_verifier_set_mode (MiniVerifierMode mode)
+{
+       verifier_mode = mode;
+}
+
+void
+mono_verifier_enable_verify_all ()
+{
+       verify_all = TRUE;
+}
+
+#ifndef DISABLE_VERIFIER
 /*
  * Pull the list of opcodes
  */
@@ -2574,14 +2595,18 @@ handle_enum:
        case MONO_TYPE_TYPEDBYREF:
                return candidate->type == MONO_TYPE_TYPEDBYREF;
 
-       case MONO_TYPE_VALUETYPE:
-               if (candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass)
+       case MONO_TYPE_VALUETYPE: {
+               MonoClass *target_klass  = mono_class_from_mono_type (target);
+               MonoClass *candidate_klass = mono_class_from_mono_type (candidate);
+
+               if (target_klass == candidate_klass)
                        return TRUE;
                if (mono_type_is_enum_type (target)) {
                        target = mono_type_get_underlying_type_any (target);
                        goto handle_enum;
                }
                return FALSE;
+       }
 
        case MONO_TYPE_VAR:
                if (candidate->type != MONO_TYPE_VAR)
@@ -2758,7 +2783,7 @@ mono_delegate_type_equal (MonoType *target, MonoType *candidate)
 
        case MONO_TYPE_VALUETYPE:
                /*FIXME handle nullables and enum*/
-               return candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass;
+               return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
 
        case MONO_TYPE_VAR:
                return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
@@ -3309,7 +3334,7 @@ do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
                if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
                        char *stack_name = stack_slot_full_name (value);
                        char *sig_name = mono_type_full_name (sig->params [i]);
-                       CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature: %s X %s at 0x%04x", sig_name, stack_name, ctx->ip_offset));
+                       CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter with function signature: Calling method with signature (%s) but for argument %d there is a (%s) on stack at 0x%04x", sig_name, i, stack_name, ctx->ip_offset));
                        g_free (stack_name);
                        g_free (sig_name);
                }
@@ -3372,7 +3397,7 @@ do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
                if (!verify_stack_type_compatibility (ctx, type, &copy))
                        CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
 
-               if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, value->type->data.klass)) {
+               if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
                        char *name = mono_method_full_name (method, TRUE);
                        CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
                        g_free (name);
@@ -3869,8 +3894,13 @@ do_newobj (VerifyContext *ctx, int token)
        if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
                CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
 
-       if (!mono_method_can_access_method_full (ctx->method, method, NULL))
-               CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor not visible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
+       if (!mono_method_can_access_method_full (ctx->method, method, NULL)) {
+               char *from = mono_method_full_name (ctx->method, TRUE);
+               char *to = mono_method_full_name (method, TRUE);
+               CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor %s not visible from %s at 0x%04x", to, from, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
+               g_free (from);
+               g_free (to);
+       }
 
        //FIXME use mono_method_get_signature_full
        sig = mono_method_signature (method);
@@ -4743,10 +4773,8 @@ static void
 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
 {
        /*clause is nested*/
-       if (is_clause_nested (to_test, clause)) {
-               if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
-                       ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
-               }
+       if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
+               ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
                return;
        }
 
@@ -4773,8 +4801,8 @@ verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, Mon
        }
 
        /*not completelly disjoint*/
-       if (is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
-               is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len))
+       if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
+               is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
                ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
 }
 
@@ -5852,25 +5880,6 @@ mono_verify_corlib ()
        return NULL;
 }
 
-static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
-static gboolean verify_all = FALSE;
-
-/*
- * Set the desired level of checks for the verfier.
- * 
- */
-void
-mono_verifier_set_mode (MiniVerifierMode mode)
-{
-       verifier_mode = mode;
-}
-
-void
-mono_verifier_enable_verify_all ()
-{
-       verify_all = TRUE;
-}
-
 /*
  * Returns true if @method needs to be verified.
  * 
@@ -5995,3 +6004,75 @@ mono_verifier_verify_class (MonoClass *class)
                return FALSE;
        return TRUE;
 }
+#else
+
+gboolean
+mono_verifier_verify_class (MonoClass *class)
+{
+       /* The verifier was disabled at compile time */
+       return TRUE;
+}
+
+GSList*
+mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
+{
+       /* The verifier was disabled at compile time */
+       return NULL;
+}
+
+gboolean
+mono_verifier_is_class_full_trust (MonoClass *klass)
+{
+       /* The verifier was disabled at compile time */
+       return TRUE;
+}
+
+gboolean
+mono_verifier_is_method_full_trust (MonoMethod *method)
+{
+       /* The verifier was disabled at compile time */
+       return TRUE;
+}
+
+gboolean
+mono_verifier_is_enabled_for_image (MonoImage *image)
+{
+       /* The verifier was disabled at compile time */
+       return FALSE;
+}
+
+gboolean
+mono_verifier_is_enabled_for_class (MonoClass *klass)
+{
+       /* The verifier was disabled at compile time */
+       return FALSE;
+}
+
+gboolean
+mono_verifier_is_enabled_for_method (MonoMethod *method)
+{
+       /* The verifier was disabled at compile time */
+       return FALSE;
+}
+
+GSList*
+mono_method_verify (MonoMethod *method, int level)
+{
+       /* The verifier was disabled at compile time */
+       return NULL;
+}
+
+void
+mono_free_verify_list (GSList *list)
+{
+       /* The verifier was disabled at compile time */
+       /* will always be null if verifier is disabled */
+}
+
+GSList*
+mono_image_verify_tables (MonoImage *image, int level)
+{
+       /* The verifier was disabled at compile time */
+       return NULL;
+}      
+#endif