Пример #1
0
 /// <summary>
 /// Insert instructions to check on the size of an array before it is created.
 /// Assumes the item on top of the stack at the moment is an int for the length of the array we're creating.
 /// </summary>
 private void CheckArrayCreationSize(Instruction instruction, ILProcessor il, ObserverRewriterContext context)
 {
     il.Body.SimplifyMacros();
     il.InsertBefore(instruction, il.CreateLdlocBest(context.ObserverVariable));
     il.InsertBefore(instruction, il.Create(OpCodes.Call, context.Observer.FlowThroughMemoryInt32Method));
     il.Body.OptimizeMacros();
 }
Пример #2
0
        private static void AddSpendGasMethodBeforeInstruction(MethodDefinition methodDefinition, ObserverReferences observer, VariableDefinition variable, Instruction instruction, Gas opcodeCount)
        {
            ILProcessor il = methodDefinition.Body.GetILProcessor();

            il.InsertBefore(instruction, il.CreateLdlocBest(variable));                       // load observer
            il.InsertBefore(instruction, il.Create(OpCodes.Ldc_I8, (long)opcodeCount.Value)); // load gas amount
            il.InsertBefore(instruction, il.Create(OpCodes.Call, observer.SpendGasMethod));   // trigger method
        }
 /// <summary>
 /// Insert instructions to check on the size of an array that has just been pushed onto the top of the stack.
 /// </summary>
 private void CheckArrayReturnSize(Instruction instruction, ILProcessor il, ObserverRewriterContext context)
 {
     // TODO: We could do away with the pop on the end and not return anything from the observer method but we would need to cast to long correctly.
     il.InsertAfter(instruction,
                    il.Create(OpCodes.Dup),
                    il.Create(OpCodes.Ldlen),
                    il.CreateLdlocBest(context.ObserverVariable),
                    il.Create(OpCodes.Call, context.Observer.FlowThroughMemoryInt32Method),
                    il.Create(OpCodes.Pop)
                    );
 }
Пример #4
0
        /// <summary>
        /// Adds a call to SpendGas from the RuntimeObserver before the given instruction.
        /// </summary>
        private static void AddSpendGasMethodBeforeInstruction(ILProcessor il, ObserverReferences observer, VariableDefinition variable, CodeSegment codeSegment)
        {
            Instruction first       = codeSegment.Instructions.First();
            Instruction newFirst    = il.CreateLdlocBest(variable);
            long        segmentCost = (long)codeSegment.CalculateGasCost().Value;

            il.Body.SimplifyMacros();
            il.InsertBefore(first, newFirst);                                         // load observer
            il.InsertBefore(first, il.Create(OpCodes.Ldc_I8, (long)segmentCost));     // load gas amount
            il.InsertBefore(first, il.Create(OpCodes.Call, observer.SpendGasMethod)); // trigger method
            il.Body.OptimizeMacros();
        }