Пример #1
0
        /// <summary>
        /// This method starts from an API call instruction and goes backward to locate the instruction that
        /// pushes the "this" object into stack. We insert out instrumentation patch at that point so that
        /// the interception method can access the "this" object. After the interception method ends, we
        /// push the "this" object to stack again so that the original API call can continue.
        /// </summary>
        /// <param name="callInstruction">Instruction that calls the API.</param>
        /// <returns>The instruction that pushes the "this" object into stack.</returns>
        private Instruction LocateLoadThisInstruction(Instruction callInstruction)
        {
            Tuple <int, int> poppedPushed = MSILHelper.GetStackTransition(callInstruction);
            int toPop = poppedPushed.Item1;

            Instruction current = callInstruction.Previous;

            while (toPop > 1)
            {
                poppedPushed = MSILHelper.GetStackTransition(current);
                toPop       += poppedPushed.Item1;
                toPop       -= poppedPushed.Item2;
                current      = current.Previous;
            }

            return(current);
        }
Пример #2
0
        public static Instruction LocateLoadThisInstruction(ILProcessor ilProcessor, Instruction callInstruction)
        {
            Dictionary <Instruction, int> stackSize = new Dictionary <Instruction, int>();

            Tuple <int, int> poppedPushed = MSILHelper.GetStackTransition(callInstruction);
            int toPop = poppedPushed.Item1;

            stackSize[callInstruction] = toPop;

            if (callInstruction.Previous == null)
            {
                ilProcessor.InsertBefore(callInstruction, ilProcessor.Create(OpCodes.Nop));
            }

            Instruction current = callInstruction.Previous;

            // fail if there current is a branch instruction
            if (current.OpCode.FlowControl != FlowControl.Next)
            {
                return(null);
            }

            while (toPop > 1)
            {
                if (IsStackEmptyingInstruction(current))
                {
                    Instruction branchSrc;
                    Instruction branchDst;
                    GetNextBranchDestinationInstruction(current, callInstruction, out branchSrc, out branchDst);
                    if (branchDst != null)
                    {
                        current = branchSrc;
                        toPop   = stackSize[branchDst];
                    }
                }

                poppedPushed       = MSILHelper.GetStackTransition(current);
                toPop             += poppedPushed.Item1;
                toPop             -= poppedPushed.Item2;
                stackSize[current] = toPop;

                if (current.Previous == null)
                {
                    ilProcessor.InsertBefore(current, ilProcessor.Create(OpCodes.Nop));
                }

                current = current.Previous;

                // fail if there current is a branch instruction
                if (current.OpCode.FlowControl != FlowControl.Next)
                {
                    return(null);
                }
            }

            if (current.OpCode == OpCodes.Constrained)
            {
                current = current.Previous;

                // fail if there current is a branch instruction
                if (current.OpCode.FlowControl != FlowControl.Next)
                {
                    return(null);
                }
            }

            return(current);
        }