1

Topic: Whether it is possible to generate Unsafe the code in DynamicMethod?

Two days googled - nothing . We take primitive test code: public static void Test () {unsafe {fixed (int* p = new int [1]) {*p =-1;} }} All works. We look in ILDASM:.method/*06000013*/public hidebysig static void Test () cil managed {.maxstack 2.locals/*1100000D*/init ([0] int32* p, [1] int32 [] pinned V_1) IL_0000: nop IL_0001: nop IL_0002: ldc.i4.1 IL_0003: newarr [mscorlib/*23000001*/] System. Int32/*0100003F*/IL_0008: dup IL_0009: stloc.1//V_1 IL_000a: brfalse.s IL_0011 IL_000c: ldloc.1//V_1 IL_000d: ldlen IL_000e: conv.i4 IL_000f: brtrue.s IL_0016 IL_0011: ldc.i4.0 IL_0012: conv.u IL_0013: stloc.0//p IL_0014: br.s IL_001f IL_0016: ldloc.1//V_1 IL_0017: ldc.i4.0 IL_0018: ldelema [mscorlib/*23000001*/] System. Int32/*0100003F*/IL_001d: conv.u IL_001e: stloc.0//p IL_001f: nop IL_0020: ldloc.0//p IL_0021: ldc.i4.m1 IL_0022: stind.i4 IL_0023: nop IL_0024: ldnull IL_0025: stloc.1//V_1 IL_0026: nop IL_0027: ret}//end of method Program:: : Test Anything military - the main knee-bends round array and check pinning that it not null and the length is more than zero. We try to play back: private static void GenerateTestMSIL (ILGenerator ilg) {var p = ilg. DeclareLocal (typeof (int *)); var t = ilg. DeclareLocal (typeof (int []), true); var zeroPtr = ilg. DefineLabel (); var nonZeroPtr = ilg. DefineLabel (); var main = ilg. DefineLabel (); ilg. Emit (OpCodes. Ldc_I4_1); ilg. Emit (OpCodes. Newarr, typeof (int));//ilg. Emit (OpCodes. Dup); ilg. Emit (OpCodes. Stloc, t);//ilg. Emit (OpCodes. Brfalse, zeroPtr);////ilg. Emit (OpCodes. Ldloc, t);//ilg. Emit (OpCodes. Ldlen);//ilg. Emit (OpCodes. Conv_I4);//ilg. Emit (OpCodes. Brtrue, nonZeroPtr);//ilg. MarkLabel (zeroPtr);//ilg. Emit (OpCodes. Ldc_I4_0);//ilg. Emit (OpCodes. Stloc, p);//ilg. Emit (OpCodes. Br_S, main); ilg. MarkLabel (nonZeroPtr); ilg. Emit (OpCodes. Ldloc, t); ilg. Emit (OpCodes. Ldc_I4_0); ilg. Emit (OpCodes. Ldelema, typeof (int)); ilg. Emit (OpCodes. Conv_U); ilg. Emit (OpCodes. Stloc, p); ilg. MarkLabel (main); ilg. Emit (OpCodes. Ldloc, p); ilg. Emit (OpCodes. Ldc_I4_M1); ilg. Emit (OpCodes. Stind_I4);//ilg. Emit (OpCodes. Ldnull);//ilg. Emit (OpCodes. Stloc, t); ilg. Emit (OpCodes. Ret);} (Superfluous operations which C# interposes from a paranoia - their presence are commented out, unfortunately, serviceability does not influence). Here now strangenesses: 1. If we generate this code in a method of the class created within the limits of the dynamic assembly all works, predictably: public static Action GenerateTestAssembly () {var ab = AssemblyBuilder. DefineDynamicAssembly (new AssemblyName ("FixedTest"), AssemblyBuilderAccess. Run); var tb = ab. DefineDynamicModule ("FixedTest", "FixedTest.dll").DefineType ("FixedTest", TypeAttributes. Class | TypeAttributes. Public); var mtb = tb. DefineMethod ("Test", MethodAttributes. HideBySig | MethodAttributes. Public | MethodAttributes. Static); GenerateTestMSIL (mtb. GetILGenerator ()); Type type = tb. CreateType (); return (Action) type. GetMethod ("Test").CreateDelegate (typeof (Action));} And here if we try same MSIL to generate within the limits of DynamicMethod attempt to cause it leads InvalidOperationException "This operation could destabilize the runtime". In what there can be a business? Clear business that PEVerify swears on the code generated thus, in the same way, as well as on original method Test (). Therefore to find, what exactly there could destabilize the runtime, is not obviously possible.

2

Re: Whether it is possible to generate Unsafe the code in DynamicMethod?

Hello, Sinclair, you wrote: S> And here if we try same MSIL to generate within the limits of DynamicMethod attempt to cause it leads InvalidOperationException "This operation could destabilize the runtime". S> In what there can be a business? The code looks normally, ilg. Emit (OpCodes. Ldc_I4_1);//1 1 ilg. Emit (OpCodes. Newarr, typeof (int));//1 int [1] ilg. Emit (OpCodes. Stloc, t);//0 ilg. Emit (OpCodes. Ldloc, t);//1 int [1] ilg. Emit (OpCodes. Ldc_I4_0);//2 int [1], 0 ilg. Emit (OpCodes. Ldelema, typeof (int));//1  [0] ilg. Emit (OpCodes. Conv_U);//1  [0] ilg. Emit (OpCodes. Stloc, p);//0 ilg. Emit (OpCodes. Ldloc, p);//1  [0] ilg. Emit (OpCodes. Ldc_I4_M1);//2  [0],-1 ilg. Emit (OpCodes. Stind_I4);//0 ilg. Emit (OpCodes. Ret);} It seems that business in permission. A long explanation - https://docs.microsoft.com/en-us/dotnet … ction-emit I do not remember precisely, whether there is a method to chop off all checks, but restrictedSkipVisibility + a binding to the unit the parameter can help with designer DynamicMethod. Also sm https://www.codeproject.com/Answers/494 … ic#answer2 https://stackoverflow.com/a/13432022 https://docs.microsoft.com/en-us/dotnet … work-4.7.2

3

Re: Whether it is possible to generate Unsafe the code in DynamicMethod?

Hello, Sinix, you wrote: S> Hello, Sinclair, you wrote: S>> And here if we try same MSIL to generate within the limits of DynamicMethod attempt to cause it leads InvalidOperationException "This operation could destabilize the runtime". S>> In what there can be a business? S> the code looks normally, Moreover - it works. Within the limits of normal type in Dynamic Assembly. S> It seems that business in permission. A long explanation - https://docs.microsoft.com/en-us/dotnet … ction-emit S> I do not remember precisely, whether there is a method to chop off all checks, but restrictedSkipVisibility + a binding to the unit the parameter can help with designer DynamicMethod. We try. S> also sm S> https://www.codeproject.com/Answers/494 … ic#answer2 S> https://stackoverflow.com/a/13432022 S> https://docs.microsoft.com/en-us/dotnet … work-4.7.2 Thanks.

4

Re: Whether it is possible to generate Unsafe the code in DynamicMethod?

Hello, Sinclair, you wrote: S> we Try. Helped?

5

Re: Whether it is possible to generate Unsafe the code in DynamicMethod?

Hello, Sinix, you wrote: S> Helped? Yes. The binding to the unit is necessary. skipVisibility it is not necessary - as it was expected, I in any  I do not climb.