Пример #1
0
        private TripleObject BuildFromTriple(Instruction fromInstruction,
                                             string fromInstanceOwnerKey,
                                             MethodDefinition parentMethod)
        {
            var tripleObj = new TripleObject();

            tripleObj.Instruction = fromInstruction;

            var fromObjectType = InstructionKeyService.GetFromObjectType(fromInstruction);

            tripleObj.ObjectType = fromObjectType;

            if (fromObjectType == ObjectType.Argument)
            {
                int fromArgumentIndex = (int)DecompilerService.GetPosition(fromInstruction);
                tripleObj.ObjectKey = InstructionKeyService.GetObjectKey(fromInstruction, fromObjectType, parentMethod, fromArgumentIndex);
            }
            else
            {
                tripleObj.ObjectKey = InstructionKeyService.GetObjectKey(fromInstruction, fromObjectType, parentMethod);
            }

            tripleObj.InstructionKey            = InstructionKeyService.GetInstructionKey(fromInstruction, parentMethod);
            tripleObj.OwnerTypeCategory         = InstructionKeyService.GetTypeCategory(fromInstruction);
            tripleObj.OwnerTypeKey              = InstructionKeyService.GetTypeKey(fromInstruction);
            tripleObj.InheritsFromConcreteClass = InstructionKeyService.GetConcreteInheritance(fromInstruction);
            tripleObj.InstanceOwnerKey          = fromInstanceOwnerKey;

            return(tripleObj);
        }
Пример #2
0
        private Triple GetNilArgumentTriple(Instruction instruction, MethodDefinition parentMethod, string objectInitializerInstanceKey = null)
        {
            var parseResult       = new ParseResult();
            var nilArgumentTriple = new Triple();

            nilArgumentTriple.ParentAssembly = parentMethod.Module.Assembly.Name.Name;
            nilArgumentTriple.ParentMethod   = parentMethod;

            string toInstanceOwnerKey = string.Empty;

            if (objectInitializerInstanceKey != null)
            {
                toInstanceOwnerKey = objectInitializerInstanceKey;
            }
            //else if (MethodArgumentInstructionParser.IsChainedToPreviousInstruction(instruction))
            //    toInstanceOwnerKey = null;
            else
            {
                toInstanceOwnerKey = InstructionKeyService.GetToInstanceOwnerKey(instruction, ObjectType.NilArgument, parentMethod);
            }

            nilArgumentTriple.To              = BuildToTriple(instruction, toInstanceOwnerKey, parentMethod);
            nilArgumentTriple.To.ObjectType   = ObjectType.NilArgument;
            nilArgumentTriple.From            = new TripleObject();
            nilArgumentTriple.From.ObjectType = ObjectType.None;
            return(nilArgumentTriple);
        }
Пример #3
0
        private TripleObject BuildToTriple(Instruction toInstruction,
                                           string toInstanceOwnerKey,
                                           MethodDefinition parentMethod,
                                           int?toArgumentCounter = null)
        {
            var tripleObj = new TripleObject();

            tripleObj.Instruction = toInstruction;

            var toObjectType = InstructionKeyService.GetToObjectType(toInstruction);

            tripleObj.ObjectType       = toObjectType;
            tripleObj.InstanceOwnerKey = toInstanceOwnerKey;

            if (toObjectType == ObjectType.Argument)
            {
                var methodCallReference = (MethodReference)toInstruction.Operand;
                tripleObj.ObjectKey = InstructionKeyService.GetObjectKey(toInstruction, toObjectType, methodCallReference, toArgumentCounter.Value);
            }
            else
            {
                tripleObj.ObjectKey = InstructionKeyService.GetObjectKey(toInstruction, toObjectType, parentMethod);
            }

            tripleObj.InstructionKey            = InstructionKeyService.GetInstructionKey(toInstruction, parentMethod);
            tripleObj.OwnerTypeCategory         = InstructionKeyService.GetTypeCategory(toInstruction);
            tripleObj.OwnerTypeKey              = InstructionKeyService.GetTypeKey(toInstruction);
            tripleObj.InheritsFromConcreteClass = InstructionKeyService.GetConcreteInheritance(toInstruction);

            return(tripleObj);
        }
Пример #4
0
        private Triple GetGenericTriple(Instruction instruction,
                                        MethodDefinition parentMethod,
                                        ObjectType toObjectType,
                                        string constructorInstructionKey = null)
        {
            var fromInstruction      = instruction.Previous;
            var fromObjectType       = InstructionKeyService.GetFromObjectType(fromInstruction);
            var fromInstanceOwnerKey = InstructionKeyService.GetFromInstanceOwnerKey(fromInstruction, fromObjectType, parentMethod);

            string toInstanceOwnerKey = string.Empty;

            if (constructorInstructionKey != null)
            {
                toInstanceOwnerKey = null;
            }
            else
            {
                toInstanceOwnerKey = InstructionKeyService.GetToInstanceOwnerKey(instruction, toObjectType, parentMethod);
            }

            var triple = BuildTriple(instruction, toInstanceOwnerKey, fromInstruction, fromInstanceOwnerKey, parentMethod);

            if (constructorInstructionKey != null)
            {
                triple.SetsObjectInitializerMember = true;
                triple.ConstructorInstructionKey   = constructorInstructionKey;
            }

            return(triple);
        }
Пример #5
0
        private List <Triple> GetMethodCallArgumentTriples(MethodDefinition parentMethod, Instruction instruction, string constructorInstructionKey = null)
        {
            var triples      = new List <Triple>();
            var calledMethod = (MethodReference)instruction.Operand;
            var rootNode     = GetInstructionTree(parentMethod, instruction, calledMethod);

            if (rootNode == null)
            {
                return(triples);
            }

            string instanceOwnerKey = string.Empty;

            if (constructorInstructionKey != null)
            {
                instanceOwnerKey = null;
                triples          = TraverseTree(parentMethod, rootNode, instanceOwnerKey, true, constructorInstructionKey);
            }
            else
            {
                instanceOwnerKey = InstructionKeyService.GetToInstanceOwnerKey(instruction, ObjectType.Method, parentMethod);
                triples          = TraverseTree(parentMethod, rootNode, instanceOwnerKey, false);
            }

            return(triples);
        }
Пример #6
0
        private bool IsSupportedFromObject(Instruction instruction)
        {
            var fromObjectType = InstructionKeyService.GetFromObjectType(instruction);

            if (!InstructionKeyService.IsSupportedObject(fromObjectType))
            {
                return(false);
            }

            return(true);
        }
Пример #7
0
        private List <Triple> TraverseTree(MethodDefinition parentMethod,
                                           InstructionTreeNode parentNode,
                                           string instanceOwnerKey,
                                           bool isObjectInitializerMember,
                                           string constructorInstructionKey = null)
        {
            var triples = new List <Triple>();

            var toArgumentCounter = parentNode.ChildInstructions.Count;

            foreach (var argumentInstruction in parentNode.ChildInstructions)
            {
                var fromObjectType       = InstructionKeyService.GetFromObjectType(argumentInstruction.Instruction);
                var fromInstanceOwnerKey = InstructionKeyService.GetFromInstanceOwnerKey(argumentInstruction.Instruction, fromObjectType, parentMethod);

                var triple = BuildTriple(parentNode.Instruction, instanceOwnerKey, argumentInstruction.Instruction, fromInstanceOwnerKey, parentMethod, toArgumentCounter);
                if (triple == null)
                {
                    continue;
                }

                if (isObjectInitializerMember)
                {
                    triple.SetsObjectInitializerMember = true;
                    triple.ConstructorInstructionKey   = constructorInstructionKey;
                }

                triples.Add(triple);
                toArgumentCounter--;

                if (argumentInstruction.ChildInstructions.Any())
                {
                    var subTriples = TraverseTree(parentMethod, argumentInstruction, triple.From.InstanceOwnerKey, false);
                    triples.AddRange(subTriples);
                }
            }

            return(triples);
        }
Пример #8
0
        public IList <AssignmentTree> PerformBacktrackingSearch(Instruction instruction, MethodDefinition methodOfInstruction,
                                                                ISourceDetector nameSourceDetector,
                                                                List <GoToInstancePattern> permittedToGoInstancePatterns,
                                                                List <string> searchBaseConstructorPatterns)
        {
            _goToInstancePatterns          = permittedToGoInstancePatterns;
            _searchBaseConstructorPatterns = searchBaseConstructorPatterns;

            var assignmentTrees = new List <AssignmentTree>();

            CreateStartTraceSection(methodOfInstruction.FullName + " : " + instruction);

            var instructionKey = InstructionKeyService.GetInstructionKey(instruction, methodOfInstruction);
            var startingKeys   = _tripleStore.GetToViaInstructionKey(instructionKey);

            foreach (var start in startingKeys)
            {
                Print(instructionKey, 0);

                var exploreTreeNode = new ExploreTreeNode()
                {
                    FullSignature = start.ToString()
                };
                var rootNode       = new AssignmentTreeNode(start);
                var assignmentTree = new AssignmentTree();
                assignmentTree.RootNode = rootNode;
                var counter = new BacktrackCounter();

                _instructionKeyStack = new Stack <string>();
                BacktrackingSearch(start, start, nameSourceDetector, counter, assignmentTree, rootNode, false, exploreTreeNode, 0);

                assignmentTrees.Add(assignmentTree);
            }



            return(assignmentTrees);
        }
Пример #9
0
        private void ParseInstruction(Instruction instruction, MethodDefinition parentMethod, List <Triple> triples, string constructorInstructionKey = null)
        {
            var toObjectType = InstructionKeyService.GetToObjectType(instruction);

            if (!InstructionKeyService.IsSupportedObject(toObjectType))
            {
                return;
            }

            if (toObjectType == ObjectType.Argument)
            {
                var argumentTriples = GetMethodCallArgumentTriples(parentMethod, instruction, constructorInstructionKey);
                foreach (var argumentTriple in argumentTriples)
                {
                    triples.Add(argumentTriple);
                }
            }
            else if (toObjectType == ObjectType.NilArgument)
            {
                var triple = GetNilArgumentTriple(instruction, parentMethod);
                if (triple != null)
                {
                    triples.Add(triple);
                }
            }
            else if (toObjectType == ObjectType.ReturnValue && parentMethod.FullName.StartsWith("System.Void"))
            {
            }
            else
            {
                var triple = GetGenericTriple(instruction, parentMethod, toObjectType);
                if (triple != null)
                {
                    triples.Add(triple);
                }
            }
        }
Пример #10
0
        public static ObjectInitializerScope BuildObjectInitializer(Instruction contructorInstruction, MethodDefinition parentMethod)
        {
            var objectInitializerScope = new ObjectInitializerScope();

            objectInitializerScope.ParentMethod              = parentMethod;
            objectInitializerScope.ConstructorInstruction    = contructorInstruction;
            objectInitializerScope.ConstructorInstructionKey = InstructionKeyService.GetInstructionKey(contructorInstruction, parentMethod);
            objectInitializerScope.ConstructorOwnerKey       = Guid.NewGuid().ToString();

            var currentMemberScope = new MemberScope();

            currentMemberScope.Parent = objectInitializerScope;
            objectInitializerScope.MemberScopes.Add(currentMemberScope);

            try
            {
                var instructionCursor = contructorInstruction.Next;

                while (instructionCursor != null && instructionCursor.Next != null)
                {
                    if (instructionCursor.OpCode.Code == Mono.Cecil.Cil.Code.Nop &&
                        instructionCursor.Next.OpCode.Code == Mono.Cecil.Cil.Code.Dup)
                    {
                        currentMemberScope.Instructions.Add(instructionCursor);
                        currentMemberScope        = new MemberScope();
                        currentMemberScope.Parent = objectInitializerScope;
                        objectInitializerScope.MemberScopes.Add(currentMemberScope);
                    }
                    else if (instructionCursor.OpCode.Code == Mono.Cecil.Cil.Code.Nop &&
                             instructionCursor.Next.OpCode.Code != Mono.Cecil.Cil.Code.Dup)
                    {
                        currentMemberScope.Instructions.Add(instructionCursor);
                        break;
                    }
                    else if (IsNewObjectInitializerScope(instructionCursor))
                    {
                        currentMemberScope.Instructions.Add(instructionCursor);
                        currentMemberScope.NestedScope = BuildObjectInitializer(instructionCursor, parentMethod);

                        var nestedScopeEndOffset = currentMemberScope.NestedScope.GetEndOffset();
                        instructionCursor = JumpToInstructionAfterOffset(parentMethod, nestedScopeEndOffset);
                        if (instructionCursor == null)
                        {
                            break;
                        }
                        currentMemberScope.Instructions.Add(instructionCursor);
                    }
                    else
                    {
                        currentMemberScope.Instructions.Add(instructionCursor);
                    }

                    instructionCursor = instructionCursor.Next;
                }
            }
            catch (Exception ex)
            {
            }

            foreach (var memberScope in objectInitializerScope.MemberScopes)
            {
                ReorderInstructions(memberScope.Instructions);
            }

            return(objectInitializerScope);
        }