Merge pull request #1150 from ludovic-henry/pr17c-delegate-trampoline
[mono.git] / mono / mini / declsec.c
index e64286ff4c7e511d70005ae0554a8d27c196de71..1ece56bdc46111365dcbbd9ddf15afe630da3168 100644 (file)
@@ -6,10 +6,13 @@
  *
  * Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  */
+#include <config.h>
 
 #include "declsec.h"
 #include "mini.h"
 
+#ifndef DISABLE_SECURITY
+
 /*
  * Does the methods (or it's class) as any declarative security attribute ?
  * Is so are they applicable ? (e.g. static class constructor)
@@ -17,7 +20,7 @@
 MonoBoolean
 mono_method_has_declsec (MonoMethod *method)
 {
-       mono_jit_stats.cas_declsec_check++;
+       InterlockedIncrement (&mono_jit_stats.cas_declsec_check);
 
        if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
                method = mono_marshal_method_from_wrapper (method);
@@ -43,17 +46,25 @@ mono_method_has_declsec (MonoMethod *method)
 void
 mono_declsec_cache_stack_modifiers (MonoJitInfo *jinfo)
 {
+       MonoMethodCasInfo *info = mono_jit_info_get_cas_info (jinfo);
+       MonoMethod *method;
+       guint32 flags;
+
+       if (!info)
+               return;
+
+       method = jinfo_get_method (jinfo);
        /* first find the stack modifiers applied to the method */
-       guint32 flags = mono_declsec_flags_from_method (jinfo->method);
-       jinfo->cas_method_assert = (flags & MONO_DECLSEC_FLAG_ASSERT) != 0;
-       jinfo->cas_method_deny = (flags & MONO_DECLSEC_FLAG_DENY) != 0;
-       jinfo->cas_method_permitonly = (flags & MONO_DECLSEC_FLAG_PERMITONLY) != 0;
+       flags = mono_declsec_flags_from_method (method);
+       info->cas_method_assert = (flags & MONO_DECLSEC_FLAG_ASSERT) != 0;
+       info->cas_method_deny = (flags & MONO_DECLSEC_FLAG_DENY) != 0;
+       info->cas_method_permitonly = (flags & MONO_DECLSEC_FLAG_PERMITONLY) != 0;
 
        /* then find the stack modifiers applied to the class */
-       flags = mono_declsec_flags_from_class (jinfo->method->klass);
-       jinfo->cas_class_assert = (flags & MONO_DECLSEC_FLAG_ASSERT) != 0;
-       jinfo->cas_class_deny = (flags & MONO_DECLSEC_FLAG_DENY) != 0;
-       jinfo->cas_class_permitonly = (flags & MONO_DECLSEC_FLAG_PERMITONLY) != 0;
+       flags = mono_declsec_flags_from_class (method->klass);
+       info->cas_class_assert = (flags & MONO_DECLSEC_FLAG_ASSERT) != 0;
+       info->cas_class_deny = (flags & MONO_DECLSEC_FLAG_DENY) != 0;
+       info->cas_class_permitonly = (flags & MONO_DECLSEC_FLAG_PERMITONLY) != 0;
 }
 
 
@@ -61,40 +72,44 @@ MonoSecurityFrame*
 mono_declsec_create_frame (MonoDomain *domain, MonoJitInfo *jinfo)
 {
        MonoSecurityFrame *frame = (MonoSecurityFrame*) mono_object_new (domain, mono_defaults.runtimesecurityframe_class);
+       MonoMethodCasInfo *info;
+       MonoMethod *method;
 
-       if (!jinfo->cas_inited) {
-               if (mono_method_has_declsec (jinfo->method)) {
+       method = jinfo_get_method (jinfo);
+       info = mono_jit_info_get_cas_info (jinfo);
+       if (info && !info->cas_inited) {
+               if (mono_method_has_declsec (method)) {
                        /* Cache the stack modifiers into the MonoJitInfo structure to speed up future stack walks */
                        mono_declsec_cache_stack_modifiers (jinfo);
                }
-               jinfo->cas_inited = TRUE;
+               info->cas_inited = TRUE;
        }
 
-       MONO_OBJECT_SETREF (frame, method, mono_method_get_object (domain, jinfo->method, NULL));
+       MONO_OBJECT_SETREF (frame, method, mono_method_get_object (domain, method, NULL));
        MONO_OBJECT_SETREF (frame, domain, domain->domain);
 
        /* stack modifiers on methods have priority on (i.e. replaces) modifiers on class */
 
-       if (jinfo->cas_method_assert) {
-               mono_declsec_get_method_action (jinfo->method, SECURITY_ACTION_ASSERT, &frame->assert);
-       } else if (jinfo->cas_class_assert) {
-               mono_declsec_get_class_action (jinfo->method->klass, SECURITY_ACTION_ASSERT, &frame->assert);
+       if (info && info->cas_method_assert) {
+               mono_declsec_get_method_action (method, SECURITY_ACTION_ASSERT, &frame->assert);
+       } else if (info && info->cas_class_assert) {
+               mono_declsec_get_class_action (method->klass, SECURITY_ACTION_ASSERT, &frame->assert);
        }
 
-       if (jinfo->cas_method_deny) {
-               mono_declsec_get_method_action (jinfo->method, SECURITY_ACTION_DENY, &frame->deny);
-       } else if (jinfo->cas_class_deny) {
-               mono_declsec_get_class_action (jinfo->method->klass, SECURITY_ACTION_DENY, &frame->deny);
+       if (info && info->cas_method_deny) {
+               mono_declsec_get_method_action (method, SECURITY_ACTION_DENY, &frame->deny);
+       } else if (info && info->cas_class_deny) {
+               mono_declsec_get_class_action (method->klass, SECURITY_ACTION_DENY, &frame->deny);
        }
 
-       if (jinfo->cas_method_permitonly) {
-               mono_declsec_get_method_action (jinfo->method, SECURITY_ACTION_PERMITONLY, &frame->permitonly);
-       } else if (jinfo->cas_class_permitonly) {
-               mono_declsec_get_class_action (jinfo->method->klass, SECURITY_ACTION_PERMITONLY, &frame->permitonly);
+       if (info && info->cas_method_permitonly) {
+               mono_declsec_get_method_action (method, SECURITY_ACTION_PERMITONLY, &frame->permitonly);
+       } else if (info && info->cas_class_permitonly) {
+               mono_declsec_get_class_action (method->klass, SECURITY_ACTION_PERMITONLY, &frame->permitonly);
        }
 
        /* g_warning ("FRAME %s A(%p,%d) D(%p,%d) PO(%p,%d)", 
-       jinfo->method->name, frame->assert.blob, frame->assert.size, frame->deny.blob, frame->deny.size, frame->permitonly.blob,frame->permitonly.size); */
+       method->name, frame->assert.blob, frame->assert.size, frame->deny.blob, frame->deny.size, frame->permitonly.blob,frame->permitonly.size); */
 
        return frame;
 }
@@ -116,7 +131,7 @@ mono_declsec_linkdemand_standard (MonoDomain *domain, MonoMethod *caller, MonoMe
 {
        MonoDeclSecurityActions linkclass, linkmethod;
 
-       mono_jit_stats.cas_linkdemand++;
+       InterlockedIncrement (&mono_jit_stats.cas_linkdemand);
 
        if (mono_declsec_get_linkdemands (callee, &linkclass, &linkmethod)) {
                MonoAssembly *assembly = mono_image_get_assembly (caller->klass->image);
@@ -190,7 +205,7 @@ mono_declsec_linkdemand_aptc (MonoDomain *domain, MonoMethod *caller, MonoMethod
        MonoAssembly *assembly;
        guint32 size = 0;
 
-       mono_jit_stats.cas_linkdemand_aptc++;
+       InterlockedIncrement (&mono_jit_stats.cas_linkdemand_aptc);
 
        /* A - Applicable only if we're calling into *another* assembly */
        if (caller->klass->image == callee->klass->image)
@@ -214,6 +229,8 @@ mono_declsec_linkdemand_aptc (MonoDomain *domain, MonoMethod *caller, MonoMethod
                        /* look for AllowPartiallyTrustedCallersAttribute */
                        result = mono_custom_attrs_has_attr (cinfo, secman->allowpartiallytrustedcallers);
                }
+               if (cinfo)
+                       mono_custom_attrs_free (cinfo);
                MONO_SECMAN_FLAG_SET_VALUE (assembly->aptc, result);
        }
 
@@ -260,7 +277,7 @@ mono_declsec_linkdemand_pinvoke (MonoDomain *domain, MonoMethod *caller, MonoMet
 {
        MonoAssembly *assembly = mono_image_get_assembly (caller->klass->image);
 
-       mono_jit_stats.cas_linkdemand_pinvoke++;
+       InterlockedIncrement (&mono_jit_stats.cas_linkdemand_pinvoke);
 
        /* Check for P/Invoke flag for the assembly */
        if (!MONO_SECMAN_FLAG_INIT (assembly->unmanaged)) {
@@ -319,7 +336,7 @@ mono_declsec_linkdemand_icall (MonoDomain *domain, MonoMethod *caller, MonoMetho
 {
        MonoAssembly *assembly;
 
-       mono_jit_stats.cas_linkdemand_icall++;
+       InterlockedIncrement (&mono_jit_stats.cas_linkdemand_icall);
 
        /* check if the _icall_ is defined inside an ECMA signed assembly */
        assembly = mono_image_get_assembly (icall->klass->image);
@@ -393,3 +410,30 @@ mono_declsec_linkdemand (MonoDomain *domain, MonoMethod *caller, MonoMethod *cal
        /* if (violation) g_warning ("mono_declsec_linkdemand violation reported %d", violation); */
        return violation;
 }
+
+#else /* DISABLE_SECURITY */
+
+void
+mono_declsec_cache_stack_modifiers (MonoJitInfo *jinfo)
+{
+}
+
+MonoSecurityFrame*
+mono_declsec_create_frame (MonoDomain *domain, MonoJitInfo *jinfo)
+{
+       return NULL;
+}
+
+guint32
+mono_declsec_linkdemand (MonoDomain *domain, MonoMethod *caller, MonoMethod *callee)
+{
+       return MONO_JIT_SECURITY_OK;
+}
+
+MonoBoolean
+mono_method_has_declsec (MonoMethod *method)
+{
+       return FALSE;
+}
+
+#endif