예제 #1
0
        static void restoreConstrainedPrefix(MethodDefinition method)
        {
            if (method == null || method.Body == null)
            {
                return;
            }

            var instrs = method.Body.Instructions;

            for (int i = 0; i < instrs.Count; i++)
            {
                var instr = instrs[i];
                if (instr.OpCode.Code != Code.Callvirt)
                {
                    continue;
                }

                var calledMethod = instr.Operand as MethodReference;
                if (calledMethod == null || !calledMethod.HasThis)
                {
                    continue;
                }
                var thisType = MethodStack.getLoadedType(method, instrs, i, calledMethod.Parameters.Count) as ByReferenceType;
                if (thisType == null)
                {
                    continue;
                }
                if (hasPrefix(instrs, i, Code.Constrained))
                {
                    continue;
                }
                instrs.Insert(i, Instruction.Create(OpCodes.Constrained, thisType.ElementType));
                i++;
            }
        }
예제 #2
0
        public bool restore(MethodDef method)
        {
            this.method = method;
            bool atLeastOneFailed = false;

            if (method == null || method.Body == null)
            {
                return(!atLeastOneFailed);
            }

            var instrs = method.Body.Instructions;

            for (int i = 0; i < instrs.Count; i++)
            {
                var instr = instrs[i];
                if (instr.Operand != null)
                {
                    continue;
                }

                TypeSig operandType = null;
                switch (instr.OpCode.Code)
                {
                case Code.Ldelema:
                    var arrayType = MethodStack.getLoadedType(method, instrs, i, 1) as SZArraySig;
                    if (arrayType == null)
                    {
                        break;
                    }
                    operandType = arrayType.Next;
                    break;

                case Code.Ldobj:
                    operandType = getPtrElementType(MethodStack.getLoadedType(method, instrs, i, 0));
                    break;

                case Code.Stobj:
                    operandType = MethodStack.getLoadedType(method, instrs, i, 0);
                    if (!isValidType(operandType))
                    {
                        operandType = getPtrElementType(MethodStack.getLoadedType(method, instrs, i, 1));
                    }
                    break;

                default:
                    continue;
                }
                if (!isValidType(operandType))
                {
                    atLeastOneFailed = true;
                    continue;
                }

                instr.Operand = operandType.ToTypeDefOrRef();
            }

            return(!atLeastOneFailed);
        }
        static void restoreConstrainedPrefix(MethodDef method)
        {
            if (method == null || method.Body == null)
            {
                return;
            }

            var instrs = method.Body.Instructions;

            for (int i = 0; i < instrs.Count; i++)
            {
                var instr = instrs[i];
                if (instr.OpCode.Code != Code.Callvirt)
                {
                    continue;
                }

                var calledMethod = instr.Operand as IMethod;
                if (calledMethod == null)
                {
                    continue;
                }
                var sig = calledMethod.MethodSig;
                if (sig == null || !sig.HasThis)
                {
                    continue;
                }
                var thisType = MethodStack.getLoadedType(method, instrs, i, sig.Params.Count) as ByRefSig;
                if (thisType == null)
                {
                    continue;
                }
                if (hasPrefix(instrs, i, Code.Constrained))
                {
                    continue;
                }
                instrs.Insert(i, OpCodes.Constrained.ToInstruction(thisType.Next.ToTypeDefOrRef()));
                i++;
            }
        }