public void TestAddDeclarativeSecurityDuplicateAction() { MethodBuilder mb = genClass.DefineMethod( genMethodName(), MethodAttributes.Public, typeof(void), new Type [0]); PermissionSet set = new PermissionSet(PermissionState.Unrestricted); mb.AddDeclarativeSecurity(SecurityAction.Demand, set); mb.AddDeclarativeSecurity(SecurityAction.Demand, set); }
public static void BuildDynMethod(ref ModuleBuilder myModBuilder) { //<Snippet1> // myModBuilder is an instance of ModuleBuilder. // Note that for the use of PermissionSet and SecurityAction, // the namespaces System.Security and System.Security.Permissions // should be included. TypeBuilder myTypeBuilder = myModBuilder.DefineType("MyType", TypeAttributes.Public); MethodBuilder myMethod1 = myTypeBuilder.DefineMethod("MyMethod", MethodAttributes.Public, typeof(int), new Type[] { typeof(int), typeof(int) }); PermissionSet myMethodPermissions = new PermissionSet( PermissionState.Unrestricted); myMethod1.AddDeclarativeSecurity(SecurityAction.Demand, myMethodPermissions); //</Snippet1> }
private static MethodBuilder AddConstructor(TypeBuilder tb, MethodWrapper defaultConstructor, MethodBase serializationConstructor, bool callReadObject) { MethodBuilder ctor = ReflectUtil.DefineConstructor(tb, MethodAttributes.Family, new Type[] { JVM.Import(typeof(SerializationInfo)), JVM.Import(typeof(StreamingContext)) }); AttributeHelper.HideFromJava(ctor); ctor.AddDeclarativeSecurity(SecurityAction.Demand, psetSerializationFormatter); CodeEmitter ilgen = CodeEmitter.Create(ctor); ilgen.Emit(OpCodes.Ldarg_0); if (defaultConstructor != null) { defaultConstructor.EmitCall(ilgen); } else { ilgen.Emit(OpCodes.Ldarg_1); ilgen.Emit(OpCodes.Ldarg_2); ilgen.Emit(OpCodes.Call, serializationConstructor); } if (callReadObject) { ilgen.Emit(OpCodes.Ldarg_0); ilgen.Emit(OpCodes.Ldarg_1); TypeWrapper serializationHelper = ClassLoaderWrapper.LoadClassCritical("ikvm.internal.Serialization"); MethodWrapper mw = serializationHelper.GetMethodWrapper("readObject", "(Ljava.lang.Object;Lcli.System.Runtime.Serialization.SerializationInfo;)V", false); mw.Link(); mw.EmitCall(ilgen); } ilgen.Emit(OpCodes.Ret); ilgen.DoEmit(); return(ctor); }
internal static void AddGetObjectData(TypeBuilder tb) { string name = tb.IsSealed ? "System.Runtime.Serialization.ISerializable.GetObjectData" : "GetObjectData"; MethodAttributes attr = tb.IsSealed ? MethodAttributes.Private | MethodAttributes.Virtual | MethodAttributes.NewSlot | MethodAttributes.Final : MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.NewSlot | MethodAttributes.CheckAccessOnOverride; tb.AddInterfaceImplementation(JVM.Import(typeof(ISerializable))); MethodBuilder getObjectData = tb.DefineMethod(name, attr, null, new Type[] { JVM.Import(typeof(SerializationInfo)), JVM.Import(typeof(StreamingContext)) }); getObjectData.SetCustomAttribute(securityCriticalAttribute); AttributeHelper.HideFromJava(getObjectData); getObjectData.AddDeclarativeSecurity(SecurityAction.Demand, psetSerializationFormatter); tb.DefineMethodOverride(getObjectData, JVM.Import(typeof(ISerializable)).GetMethod("GetObjectData")); CodeEmitter ilgen = CodeEmitter.Create(getObjectData); ilgen.Emit(OpCodes.Ldarg_0); ilgen.Emit(OpCodes.Ldarg_1); TypeWrapper serializationHelper = ClassLoaderWrapper.LoadClassCritical("ikvm.internal.Serialization"); MethodWrapper mw = serializationHelper.GetMethodWrapper("writeObject", "(Ljava.lang.Object;Lcli.System.Runtime.Serialization.SerializationInfo;)V", false); mw.Link(); mw.EmitCall(ilgen); ilgen.Emit(OpCodes.Ret); ilgen.DoEmit(); }
public void TestAddDeclarativeSecurityNullPermissionSet() { MethodBuilder mb = genClass.DefineMethod( genMethodName(), MethodAttributes.Public, typeof(void), new Type [0]); mb.AddDeclarativeSecurity(SecurityAction.Demand, null); }
public void TestAddDeclarativeSecurityAlreadyCreated() { MethodBuilder mb = genClass.DefineMethod( genMethodName(), MethodAttributes.Public, typeof(void), new Type [0]); ILGenerator ilgen = mb.GetILGenerator(); ilgen.Emit(OpCodes.Ret); genClass.CreateType(); PermissionSet set = new PermissionSet(PermissionState.Unrestricted); mb.AddDeclarativeSecurity(SecurityAction.Demand, set); }
public void TestAddDeclarativeSecurityInvalidAction() { MethodBuilder mb = genClass.DefineMethod( genMethodName(), MethodAttributes.Public, typeof(void), new Type [0]); SecurityAction [] actions = new SecurityAction [] { SecurityAction.RequestMinimum, SecurityAction.RequestOptional, SecurityAction.RequestRefuse }; PermissionSet set = new PermissionSet(PermissionState.Unrestricted); foreach (SecurityAction action in actions) { try { mb.AddDeclarativeSecurity(action, set); Assert.Fail(); } catch (ArgumentOutOfRangeException) { } } }
private static MethodInvoker SpitAndInstantiateClassFor(MethodInfo method, bool requiresDemand) { TypeBuilder builder = Runtime.ThunkModuleBuilder.DefineType("invoker" + count++, TypeAttributes.Public, typeof(MethodInvoker)); MethodBuilder builder2 = builder.DefineMethod("Invoke", MethodAttributes.Virtual | MethodAttributes.Public, typeof(object), new Type[] { typeof(object), typeof(object[]) }); if (requiresDemand) { builder2.AddDeclarativeSecurity(SecurityAction.Demand, new NamedPermissionSet("FullTrust")); } builder2.SetCustomAttribute(new CustomAttributeBuilder(Runtime.TypeRefs.debuggerStepThroughAttributeCtor, new object[0])); builder2.SetCustomAttribute(new CustomAttributeBuilder(Runtime.TypeRefs.debuggerHiddenAttributeCtor, new object[0])); ILGenerator iLGenerator = builder2.GetILGenerator(); if (!method.DeclaringType.IsPublic) { method = method.GetBaseDefinition(); } Type declaringType = method.DeclaringType; if (!method.IsStatic) { iLGenerator.Emit(OpCodes.Ldarg_1); if (declaringType.IsValueType) { Microsoft.JScript.Convert.EmitUnbox(iLGenerator, declaringType, Type.GetTypeCode(declaringType)); Microsoft.JScript.Convert.EmitLdloca(iLGenerator, declaringType); } else { iLGenerator.Emit(OpCodes.Castclass, declaringType); } } ParameterInfo[] parameters = method.GetParameters(); LocalBuilder[] builderArray = null; int i = 0; int length = parameters.Length; while (i < length) { iLGenerator.Emit(OpCodes.Ldarg_2); ConstantWrapper.TranslateToILInt(iLGenerator, i); Type parameterType = parameters[i].ParameterType; if (parameterType.IsByRef) { parameterType = parameterType.GetElementType(); if (builderArray == null) { builderArray = new LocalBuilder[length]; } builderArray[i] = iLGenerator.DeclareLocal(parameterType); iLGenerator.Emit(OpCodes.Ldelem_Ref); if (parameterType.IsValueType) { Microsoft.JScript.Convert.EmitUnbox(iLGenerator, parameterType, Type.GetTypeCode(parameterType)); } iLGenerator.Emit(OpCodes.Stloc, builderArray[i]); iLGenerator.Emit(OpCodes.Ldloca, builderArray[i]); } else { iLGenerator.Emit(OpCodes.Ldelem_Ref); if (parameterType.IsValueType) { Microsoft.JScript.Convert.EmitUnbox(iLGenerator, parameterType, Type.GetTypeCode(parameterType)); } } i++; } if (((!method.IsStatic && method.IsVirtual) && !method.IsFinal) && (!declaringType.IsSealed || !declaringType.IsValueType)) { iLGenerator.Emit(OpCodes.Callvirt, method); } else { iLGenerator.Emit(OpCodes.Call, method); } Type returnType = method.ReturnType; if (returnType == typeof(void)) { iLGenerator.Emit(OpCodes.Ldnull); } else if (returnType.IsValueType) { iLGenerator.Emit(OpCodes.Box, returnType); } if (builderArray != null) { int index = 0; int num4 = parameters.Length; while (index < num4) { LocalBuilder local = builderArray[index]; if (local != null) { iLGenerator.Emit(OpCodes.Ldarg_2); ConstantWrapper.TranslateToILInt(iLGenerator, index); iLGenerator.Emit(OpCodes.Ldloc, local); Type elementType = parameters[index].ParameterType.GetElementType(); if (elementType.IsValueType) { iLGenerator.Emit(OpCodes.Box, elementType); } iLGenerator.Emit(OpCodes.Stelem_Ref); } index++; } } iLGenerator.Emit(OpCodes.Ret); return((MethodInvoker)Activator.CreateInstance(builder.CreateType())); }
internal override void Apply(ClassLoaderWrapper loader, MethodBuilder mb, object annotation) { if (type.IsSubclassOf(Types.SecurityAttribute)) { #if STATIC_COMPILER mb.__AddDeclarativeSecurity(MakeCustomAttributeBuilder(loader, annotation)); #elif STUB_GENERATOR #else SecurityAction action; PermissionSet permSet; if (MakeDeclSecurity(type, annotation, out action, out permSet)) { mb.AddDeclarativeSecurity(action, permSet); } #endif } else { mb.SetCustomAttribute(MakeCustomAttributeBuilder(loader, annotation)); } }
private static MethodInvoker SpitAndInstantiateClassFor(MethodInfo method, bool requiresDemand) { TypeBuilder tb = Runtime.ThunkModuleBuilder.DefineType("invoker" + MethodInvoker.count++, TypeAttributes.Public, typeof(MethodInvoker)); MethodBuilder mb = tb.DefineMethod("Invoke", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot, typeof(Object), new Type[] { typeof(Object), typeof(Object[]) }); if (requiresDemand) { mb.AddDeclarativeSecurity(SecurityAction.Demand, new NamedPermissionSet("FullTrust")); } #if !DEBUG mb.SetCustomAttribute(new CustomAttributeBuilder(Runtime.TypeRefs.debuggerStepThroughAttributeCtor, new Object[] {})); mb.SetCustomAttribute(new CustomAttributeBuilder(Runtime.TypeRefs.debuggerHiddenAttributeCtor, new Object[] {})); #endif ILGenerator il = mb.GetILGenerator(); if (!method.DeclaringType.IsPublic) { method = method.GetBaseDefinition(); } Type obT = method.DeclaringType; if (!method.IsStatic) { il.Emit(OpCodes.Ldarg_1); if (obT.IsValueType) { Convert.EmitUnbox(il, obT, Type.GetTypeCode(obT)); Convert.EmitLdloca(il, obT); } else { il.Emit(OpCodes.Castclass, obT); } } ParameterInfo[] parameters = method.GetParameters(); LocalBuilder[] outTemps = null; for (int i = 0, n = parameters.Length; i < n; i++) { il.Emit(OpCodes.Ldarg_2); ConstantWrapper.TranslateToILInt(il, i); Type pt = parameters[i].ParameterType; if (pt.IsByRef) { pt = pt.GetElementType(); if (outTemps == null) { outTemps = new LocalBuilder[n]; } outTemps[i] = il.DeclareLocal(pt); il.Emit(OpCodes.Ldelem_Ref); if (pt.IsValueType) { Convert.EmitUnbox(il, pt, Type.GetTypeCode(pt)); } il.Emit(OpCodes.Stloc, outTemps[i]); il.Emit(OpCodes.Ldloca, outTemps[i]); } else { il.Emit(OpCodes.Ldelem_Ref); if (pt.IsValueType) { Convert.EmitUnbox(il, pt, Type.GetTypeCode(pt)); } } } if (!method.IsStatic && method.IsVirtual && !method.IsFinal && (!obT.IsSealed || !obT.IsValueType)) { il.Emit(OpCodes.Callvirt, method); } else { il.Emit(OpCodes.Call, method); } Type rt = method.ReturnType; if (rt == typeof(void)) { il.Emit(OpCodes.Ldnull); } else if (rt.IsValueType) { il.Emit(OpCodes.Box, rt); } if (outTemps != null) { for (int i = 0, n = parameters.Length; i < n; i++) { LocalBuilder loc = outTemps[i]; if (loc != null) { il.Emit(OpCodes.Ldarg_2); ConstantWrapper.TranslateToILInt(il, i); il.Emit(OpCodes.Ldloc, loc); Type pt = parameters[i].ParameterType.GetElementType(); if (pt.IsValueType) { il.Emit(OpCodes.Box, pt); } il.Emit(OpCodes.Stelem_Ref); } } } il.Emit(OpCodes.Ret); Type t = tb.CreateType(); return((MethodInvoker)Activator.CreateInstance(t)); }