VerificationException: Operation could destabilize the runtime. Troubles with EmitCall(OpCodes.Call)
NickName:Tristan C Ask DateTime:2016-12-01T09:36:59

VerificationException: Operation could destabilize the runtime. Troubles with EmitCall(OpCodes.Call)

I am creating a system to store value types(int, byte, structs) on the heap and in order to prevent boxing and unboxing of said value types. This is because all of the constant boxing and unboxing in the Unity 3D engine is creating large GC CPU spikes in our large code base.

VerificationException: Operation could destabilize the runtime.

The above exception is thrown when I try to invoke the dynamic method. The stack trace ends just before it goes into the dynamic method and it's not possible to break point the execution. More information is given in the example below.

void Main()
{
    var fieldInfo = typeof(MyClass).GetMember("Number")[0] as FieldInfo;
    var pointerSetFunc = CreatePointerFieldSetMethod(fieldInfo);
    object myClass = new MyClass();
    // The exception occurs when invoking the dynamic method.
    pointerSetFunc(myClass, 0);
}

public class MyClass
{
    public byte Number;
}

public static Action<object, int> CreatePointerFieldSetMethod(FieldInfo field)
{
    var setMethod = new DynamicMethod("SetFieldFromPointer", typeof(void), new[] { typeof(object), typeof(int) }, true);
    ILGenerator generator = setMethod.GetILGenerator();

    // This returns the correct value. byte CustomBox<byte>.Unbox(Int32 index);
    var unboxFunc = typeof(CustomBox<>).MakeGenericType(field.FieldType).GetMethod("Unbox", BindingFlags.Static | BindingFlags.Public);

    // Somewhere in the below code the exception occurs.
    generator.Emit(OpCodes.Ldarg_1); // This should be the index or "pointer" to pass into the CustomBox.Unbox function.
    generator.EmitCall(OpCodes.Call, unboxFunc, null);
    generator.Emit(OpCodes.Stloc_0); // This should be the result of unboxing.

    // This code does not get called.
    generator.Emit(OpCodes.Ldarg_0); // This should be the object MyClass.
    generator.Emit(OpCodes.Ldloc_0); // This should be the value received from the CustomBox.Unbox function.
    generator.Emit(OpCodes.Stfld, field); // Set the MyClass.Number field.

    generator.Emit(OpCodes.Ret);
    return (Action<object, int>)setMethod.CreateDelegate(typeof(Action<object, int>));
}

// The point of this class is to store values types (int, byte, struct, etc..) in an array already on the heap to avoid boxing.
// Boxing has become an issue on our application.
public struct CustomBox<T> where T : struct
{
    public static T Unbox(int index)
    {
        // TODO: Actually make the unbox code.
        return default(T);
    }
}

Edit: Heres the method I'm trying to create and it's generated IL:

private static void SetFieldUsingIndex(object myClass, int index)
{
    byte number = Values<byte>.Unbox(index);
    ((MyClass)myClass).Number = number;
}

/* Generated IL for above method.
    IL_0000: nop
    IL_0001: ldarg.1
    IL_0002: call !0 class CPURaceTest.Values`1<uint8>::Unbox(int32)
    IL_0007: stloc.0
    IL_0008: ldarg.0
    IL_0009: castclass CPURaceTest.MyClass
    IL_000e: ldloc.0
    IL_000f: stfld uint8 CPURaceTest.MyClass::Number
    IL_0014: ret
*/

Copyright Notice:Content Author:「Tristan C」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40901221/verificationexception-operation-could-destabilize-the-runtime-troubles-with-em

More about “VerificationException: Operation could destabilize the runtime. Troubles with EmitCall(OpCodes.Call)” related questions

VerificationException: Operation could destabilize the runtime. Troubles with EmitCall(OpCodes.Call)

I am creating a system to store value types(int, byte, structs) on the heap and in order to prevent boxing and unboxing of said value types. This is because all of the constant boxing and unboxing ...

Show Detail

Operation could destabilize the runtime. MVC App

I am getting an Operation could destabilize the runtime error message on a testing machine but doesn't happen locally or on a production server. The Error I am getting is unspecific and gives me no...

Show Detail

VerificationException "Operation could destabilize runtime" Error with MsgPack and .net Error

Encountered VerificationException "Operation could destabilize runtime" error using MsgPack to serialize some F# types. The compiler also suggests that conflicting class libraries may be loaded, bu...

Show Detail

C# MVC: Operation could destabilize the runtime

I'm getting an error when I try to run my project with IIS: Operation could destabilize the runtime. System.Security.VerificationException: Operation could destabilize the runtime. My project is

Show Detail

VerificationException Operation could destabilize the runtime on Simple LINQ Query

Here's the problem. The code below works fine on my development PC, but when I deployed the app, it crashes. Here is the lines of code that are relvant Private TdsTypesList As List(Of TDS_Type) ...

Show Detail

VerificationException was unhandled : Operation could destabilize the runtime

I am trying to calculate normal map and bump map of some images. In order to do this, I am using Craig's Utility Library. However when I try to create bump map I am getting an exception saying "

Show Detail

nhibernate Operation could destabilize the runtime

Locally my site works, but at host I am getting the error: "Operation could destabilize the runtime." I am using nhibernate. I am using the repository pattern. [VerificationException: Operation ...

Show Detail

JSON.Net throwing System.Security.VerificationException: Operation could destabilize the runtime

I have a web application which uses JSON.Net to write out an array of data from a .Net Array(). When run in the VS2010 environment, it works fine. When run under IIS6 and .Net 3.5, it works fine....

Show Detail

Ninject: System.Security.VerificationException : Operation could destabilize the runtime

I have the following code which works well, when tests are executed. But then i try to run these tests + code coverage calculation (SharpDevelop 4) it throws the exception. Can somebody describe ...

Show Detail

"Operation could destabilize the runtime" when using ANTS profiler on a StructureMap application

We are recently upgraded a web/mvc application to use StrucutreMap 3.0.4 Now, when attempting to profile the application using RedGate Ant's profiler at the "Line Level Timings, All Methods with S...

Show Detail