Пример #1
0
        private bool InstrumentMethodCallInstruction(MethodDefinition method, Instruction instruction)
        {
            var methodName = $"{method.DeclaringType.FullName}::{method.Name}";
            var processor  = method.Body.GetILProcessor();

            var calleeRef  = (MethodReference)instruction.Operand;
            var calleeName = $"{calleeRef.DeclaringType.FullName}::{calleeRef.Name}";

            if (Constants.MethodPrefixBlackList.Any(x => calleeName.StartsWith(x)))
            {
                return(false);
            }

            if (calleeRef.DeclaringType.IsValueType)
            {
                return(false); // method in struct
            }

            var patchTarget = instruction;
            var isValueType = false;

            if (patchTarget.Previous != null && patchTarget.Previous.OpCode == OpCodes.Constrained)
            {
                patchTarget = patchTarget.Previous;
                if (patchTarget.Operand is GenericInstanceType)
                {
                    isValueType |= ((GenericInstanceType)patchTarget.Operand).IsValueType;
                }
                else if (patchTarget.Operand is TypeReference)
                {
                    var typeRef = (TypeReference)patchTarget.Operand;
                    isValueType |= typeRef.IsValueType;
                }
            }

            if (isValueType)
            {
                return(false); // method in struct
            }

            var                signature      = calleeRef.GetResolvedMethodSignature();
            var                isNewObj       = instruction.OpCode == OpCodes.Newobj;
            Instruction        patchStart     = null;
            VariableDefinition instanceVarDef = null;

            if (!isNewObj && calleeRef.HasThis)
            {
                instanceVarDef = new VariableDefinition(method.Module.TypeSystem.Object);
                method.Body.Variables.Add(instanceVarDef);

                var loadThisInstruction = MSILHelper.LocateLoadThisInstruction(processor, instruction);
                if (loadThisInstruction == null)
                {
                    return(false);
                }

                var patch1 = new List <Instruction>()
                {
                    processor.Create(OpCodes.Dup),
                    processor.Create(OpCodes.Stloc, instanceVarDef),
                };
                processor.InsertAfter(loadThisInstruction, patch1);
                patchStart = patch1[0];
            }

            VariableDefinition contextVarDef    = null;
            var afterMethodCallbackWillBeCalled = calleeRef.Name.Equals("Dispose");

            if (afterMethodCallbackWillBeCalled)
            {
                contextVarDef = new VariableDefinition(method.Module.TypeSystem.Object);
                method.Body.Variables.Add(contextVarDef);
            }

            var patch = this.GetPatchForBeforeMethodCall(
                processor,
                instanceVarDef,
                methodName,
                calleeRef,
                instruction.Offset,
                isValueType,
                contextVarDef);

            if (patchStart == null)
            {
                patchStart = patch[0];
            }

            processor.InsertBefore(patchTarget, patch);
            method.UpdateInstructionReferences(patchTarget, patchStart, true);

            /*
             * if (contextVarDef != null)
             * {
             *  patch = this.GetPatchForAfterMethodCall(processor, contextVarDef);
             *  processor.InsertAfter(instruction, patch);
             * }
             */
            return(true);
        }