Exemplo n.º 1
0
        public virtual void ReplaceStatement(Statement oldstat, Statement newstat)
        {
            foreach (StatEdge edge in oldstat.GetAllPredecessorEdges())
            {
                oldstat.RemovePredecessor(edge);
                edge.GetSource().ChangeEdgeNode(Direction_Forward, edge, newstat);
                newstat.AddPredecessor(edge);
            }
            foreach (StatEdge edge in oldstat.GetAllSuccessorEdges())
            {
                oldstat.RemoveSuccessor(edge);
                edge.SetSource(newstat);
                newstat.AddSuccessor(edge);
            }
            int statindex = stats.GetIndexByKey(oldstat.id);

            stats.RemoveWithKey(oldstat.id);
            stats.AddWithKeyAndIndex(statindex, newstat, newstat.id);
            newstat.SetParent(this);
            newstat.post = oldstat.post;
            if (first == oldstat)
            {
                first = newstat;
            }
            List <StatEdge> lst = new List <StatEdge>(oldstat.GetLabelEdges());

            for (int i = lst.Count - 1; i >= 0; i--)
            {
                StatEdge edge = lst[i];
                if (edge.GetSource() != newstat)
                {
                    newstat.AddLabeledEdge(edge);
                }
                else if (this == edge.GetDestination() || this.ContainsStatementStrict(edge.GetDestination
                                                                                           ()))
                {
                    edge.closure = null;
                }
                else
                {
                    this.AddLabeledEdge(edge);
                }
            }
            oldstat.GetLabelEdges().Clear();
        }
Exemplo n.º 2
0
        public static bool SplitIrreducibleNode(Statement statement)
        {
            Statement splitnode = GetCandidateForSplitting(statement);

            if (splitnode == null)
            {
                return(false);
            }
            StatEdge enteredge = splitnode.GetPredecessorEdges(StatEdge.Type_Regular).GetEnumerator
                                     ().Current;
            // copy the smallest statement
            Statement splitcopy = CopyStatement(splitnode, null, new Dictionary <Statement, Statement
                                                                                 >());

            InitCopiedStatement(splitcopy);
            // insert the copy
            splitcopy.SetParent(statement);
            statement.GetStats().AddWithKey(splitcopy, splitcopy.id);
            // switch input edges
            foreach (StatEdge prededge in splitnode.GetPredecessorEdges(Statement.Statedge_Direct_All
                                                                        ))
            {
                if (prededge.GetSource() == enteredge.GetSource() || prededge.closure == enteredge
                    .GetSource())
                {
                    splitnode.RemovePredecessor(prededge);
                    prededge.GetSource().ChangeEdgeNode(Statement.Direction_Forward, prededge, splitcopy
                                                        );
                    splitcopy.AddPredecessor(prededge);
                }
            }
            // connect successors
            foreach (StatEdge succ in splitnode.GetSuccessorEdges(Statement.Statedge_Direct_All
                                                                  ))
            {
                splitcopy.AddSuccessor(new StatEdge(succ.GetType(), splitcopy, succ.GetDestination
                                                        (), succ.closure));
            }
            return(true);
        }
Exemplo n.º 3
0
        private void AddEdgeDirectInternal(int direction, StatEdge edge, int edgetype)
        {
            Dictionary <int, List <StatEdge> > mapEdges = direction == Direction_Backward ? mapPredEdges
                                 : mapSuccEdges;
            Dictionary <int, List <Statement> > mapStates = direction == Direction_Backward ?
                                                            mapPredStates : mapSuccStates;

            mapEdges.ComputeIfAbsent(edgetype, (int k) => new List <StatEdge>()).Add(edge);
            mapStates.ComputeIfAbsent(edgetype, (int k) => new List <Statement>()).Add(direction
                                                                                       == Direction_Backward ? edge.GetSource() : edge.GetDestination());
        }
Exemplo n.º 4
0
        // *****************************************************************************
        // private methods
        // *****************************************************************************
        public virtual void SortEdgesAndNodes()
        {
            Dictionary <StatEdge, int> mapEdgeIndex  = new Dictionary <StatEdge, int>();
            List <StatEdge>            lstFirstSuccs = first.GetSuccessorEdges(Statedge_Direct_All);

            for (int i = 0; i < lstFirstSuccs.Count; i++)
            {
                Sharpen.Collections.Put(mapEdgeIndex, lstFirstSuccs[i], i == 0 ? lstFirstSuccs.Count
                                         : i);
            }
            // case values
            BasicBlockStatement bbstat = (BasicBlockStatement)first;

            int[] values = ((SwitchInstruction)bbstat.GetBlock().GetLastInstruction()).GetValues
                               ();
            List <Statement>    nodes = new List <Statement>(stats.Count - 1);
            List <List <int?> > edges = new List <List <int?> >(stats.Count - 1);

            // collect regular edges
            for (int i = 1; i < stats.Count; i++)
            {
                Statement   stat = stats[i];
                List <int?> lst  = new List <int?>();
                foreach (StatEdge edge in stat.GetPredecessorEdges(StatEdge.Type_Regular))
                {
                    if (edge.GetSource() == first)
                    {
                        lst.Add(mapEdgeIndex.GetOrNullable(edge));
                    }
                }
                lst.Sort();
                nodes.Add(stat);
                edges.Add(lst);
            }
            // collect exit edges
            List <StatEdge> lstExitEdges = first.GetSuccessorEdges(StatEdge.Type_Break | StatEdge
                                                                   .Type_Continue);

            while (!(lstExitEdges.Count == 0))
            {
                StatEdge    edge = lstExitEdges[0];
                List <int?> lst  = new List <int?>();
                for (int i = lstExitEdges.Count - 1; i >= 0; i--)
                {
                    StatEdge edgeTemp = lstExitEdges[i];
                    if (edgeTemp.GetDestination() == edge.GetDestination() && edgeTemp.GetType() == edge
                        .GetType())
                    {
                        lst.Add(mapEdgeIndex.GetOrNullable(edgeTemp));
                        lstExitEdges.RemoveAtReturningValue(i);
                    }
                }
                lst.Sort();
                nodes.Add(null);
                edges.Add(lst);
            }
            // sort edges (bubblesort)
            for (int i = 0; i < edges.Count - 1; i++)
            {
                for (int j = edges.Count - 1; j > i; j--)
                {
                    if (edges[j - 1][0] > edges[j][0])
                    {
                        edges[j] = edges[j - 1] = edges[j];
                        nodes[j] = nodes[j - 1] = nodes[j];
                    }
                }
            }
            // sort statement cliques
            for (int index = 0; index < nodes.Count; index++)
            {
                Statement stat = nodes[index];
                if (stat != null)
                {
                    HashSet <Statement> setPreds = new HashSet <Statement>(stat.GetNeighbours(StatEdge.
                                                                                              Type_Regular, Direction_Backward));
                    setPreds.Remove(first);
                    if (!(setPreds.Count == 0))
                    {
                        Statement pred = new Sharpen.EnumeratorAdapter <Statement>(setPreds.GetEnumerator()).Next();
                        // assumption: at most one predecessor node besides the head. May not hold true for obfuscated code.
                        for (int j = 0; j < nodes.Count; j++)
                        {
                            if (j != (index - 1) && nodes[j] == pred)
                            {
                                nodes.Add(j + 1, stat);
                                edges.Add(j + 1, edges[index]);
                                if (j > index)
                                {
                                    nodes.RemoveAtReturningValue(index);
                                    edges.RemoveAtReturningValue(index);
                                    index--;
                                }
                                else
                                {
                                    nodes.RemoveAtReturningValue(index + 1);
                                    edges.RemoveAtReturningValue(index + 1);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            // translate indices back into edges
            List <List <StatEdge> > lstEdges  = new List <List <StatEdge> >(edges.Count);
            List <List <Exprent> >  lstValues = new List <List <Exprent> >(edges.Count);

            foreach (List <int?> lst in edges)
            {
                List <StatEdge> lste     = new List <StatEdge>(lst.Count);
                List <Exprent>  lstv     = new List <Exprent>(lst.Count);
                List <StatEdge> lstSuccs = first.GetSuccessorEdges(Statedge_Direct_All);
                foreach (int? @in in lst)
                {
                    int?index = @in == lstSuccs.Count ? 0 : @in;
                    if (!index.HasValue)
                    {
                        continue;
                    }
                    lste.Add(lstSuccs[index.Value]);
                    lstv.Add(index == 0 ? null : new ConstExprent(values[index.Value - 1], false, null));
                }
                lstEdges.Add(lste);
                lstValues.Add(lstv);
            }
            // replace null statements with dummy basic blocks
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i] == null)
                {
                    BasicBlockStatement bstat = new BasicBlockStatement(new BasicBlock(DecompilerContext
                                                                                       .GetCounterContainer().GetCounterAndIncrement(CounterContainer.Statement_Counter
                                                                                                                                     )));
                    StatEdge sample_edge = lstEdges[i][0];
                    bstat.AddSuccessor(new StatEdge(sample_edge.GetType(), bstat, sample_edge.GetDestination
                                                        (), sample_edge.closure));
                    foreach (StatEdge edge in lstEdges[i])
                    {
                        edge.GetSource().ChangeEdgeType(Direction_Forward, edge, StatEdge.Type_Regular);
                        edge.closure.GetLabelEdges().Remove(edge);
                        edge.GetDestination().RemovePredecessor(edge);
                        edge.GetSource().ChangeEdgeNode(Direction_Forward, edge, bstat);
                        bstat.AddPredecessor(edge);
                    }
                    nodes[i] = bstat;
                    stats.AddWithKey(bstat, bstat.id);
                    bstat.SetParent(this);
                }
            }
            caseStatements = nodes;
            caseEdges      = lstEdges;
            caseValues     = lstValues;
        }