예제 #1
0
        public BasicBlockStatement(BasicBlock block)
        {
            // *****************************************************************************
            // private fields
            // *****************************************************************************
            // *****************************************************************************
            // constructors
            // *****************************************************************************
            type       = Statement.Type_Basicblock;
            this.block = block;
            id         = block.id;
            CounterContainer coun = DecompilerContext.GetCounterContainer();

            if (id >= coun.GetCounter(CounterContainer.Statement_Counter))
            {
                coun.SetCounter(CounterContainer.Statement_Counter, id + 1);
            }
            Instruction instr = block.GetLastInstruction();

            if (instr != null)
            {
                if (instr.group == ICodeConstants.Group_Jump && instr.opcode != ICodeConstants.opc_goto)
                {
                    lastBasicType = Lastbasictype_If;
                }
                else if (instr.group == ICodeConstants.Group_Switch)
                {
                    lastBasicType = Lastbasictype_Switch;
                }
            }
            // monitorenter and monitorexits
            BuildMonitorFlags();
        }
예제 #2
0
 public Statement()
 {
     {
         id = DecompilerContext.GetCounterContainer().GetCounterAndIncrement(CounterContainer
                                                                             .Statement_Counter);
     }
 }
예제 #3
0
 public Exprent(int type)
 {
     // offsets of bytecode instructions decompiled to this exprent
     this.type = type;
     this.id   = DecompilerContext.GetCounterContainer().GetCounterAndIncrement(CounterContainer
                                                                                .Expression_Counter);
 }
예제 #4
0
 private static void RemoveLastEmptyStatement(DoStatement dostat, Statement stat)
 {
     if (stat == dostat.GetFirst())
     {
         BasicBlockStatement bstat = new BasicBlockStatement(new BasicBlock(DecompilerContext
                                                                            .GetCounterContainer().GetCounterAndIncrement(CounterContainer.Statement_Counter
                                                                                                                          )));
         bstat.SetExprents(new List <Exprent>());
         dostat.ReplaceStatement(stat, bstat);
     }
     else
     {
         foreach (StatEdge edge in stat.GetAllPredecessorEdges())
         {
             edge.GetSource().ChangeEdgeType(Statement.Direction_Forward, edge, StatEdge.Type_Continue
                                             );
             stat.RemovePredecessor(edge);
             edge.GetSource().ChangeEdgeNode(Statement.Direction_Forward, edge, dostat);
             dostat.AddPredecessor(edge);
             dostat.AddLabeledEdge(edge);
         }
         // parent is a sequence statement
         stat.GetParent().GetStats().RemoveWithKey(stat.id);
     }
 }
예제 #5
0
        public override Statement GetSimpleCopy()
        {
            BasicBlock newblock = new BasicBlock(DecompilerContext.GetCounterContainer().GetCounterAndIncrement
                                                     (CounterContainer.Statement_Counter));
            SimpleInstructionSequence seq = new SimpleInstructionSequence();

            for (int i = 0; i < block.GetSeq().Length(); i++)
            {
                seq.AddInstruction(block.GetSeq().GetInstr(i).Clone(), -1);
            }
            newblock.SetSeq(seq);
            return(new BasicBlockStatement(newblock));
        }
예제 #6
0
        public override Statement GetSimpleCopy()
        {
            CatchStatement cs = new CatchStatement();

            foreach (List <string> exc in this.exctstrings)
            {
                cs.exctstrings.Add(new List <string>(exc));
                cs.vars.Add(new VarExprent(DecompilerContext.GetCounterContainer().GetCounterAndIncrement
                                               (CounterContainer.Var_Counter), new VarType(ICodeConstants.Type_Object, 0, exc[0
                                                                                           ]), DecompilerContext.GetVarProcessor()));
            }
            return(cs);
        }
예제 #7
0
        public static void DestroyAndFlattenStatement(Statement stat)
        {
            DestroyStatementContent(stat, false);
            BasicBlockStatement bstat = new BasicBlockStatement(new BasicBlock(DecompilerContext
                                                                               .GetCounterContainer().GetCounterAndIncrement(CounterContainer.Statement_Counter
                                                                                                                             )));

            if (stat.GetExprents() == null)
            {
                bstat.SetExprents(new List <Exprent>());
            }
            else
            {
                bstat.SetExprents(DecHelper.CopyExprentList(stat.GetExprents()));
            }
            stat.GetParent().ReplaceStatement(stat, bstat);
        }
예제 #8
0
        public override Statement GetSimpleCopy()
        {
            CatchAllStatement cas = new CatchAllStatement();

            cas.isFinally__ = this.isFinally__;
            if (this.monitor != null)
            {
                cas.monitor = new VarExprent(DecompilerContext.GetCounterContainer().GetCounterAndIncrement
                                                 (CounterContainer.Var_Counter), VarType.Vartype_Int, DecompilerContext.GetVarProcessor
                                                 ());
            }
            if (!(this.vars.Count == 0))
            {
                cas.vars.Add(new VarExprent(DecompilerContext.GetCounterContainer().GetCounterAndIncrement
                                                (CounterContainer.Var_Counter), new VarType(ICodeConstants.Type_Object, 0, "java/lang/Throwable"
                                                                                            ), DecompilerContext.GetVarProcessor()));
            }
            return(cas);
        }
예제 #9
0
        private CatchAllStatement(Statement head, Statement handler)
            : this()
        {
            first = head;
            stats.AddWithKey(head, head.id);
            this.handler = handler;
            stats.AddWithKey(handler, handler.id);
            List <StatEdge> lstSuccs = head.GetSuccessorEdges(Statedge_Direct_All);

            if (!(lstSuccs.Count == 0))
            {
                StatEdge edge = lstSuccs[0];
                if (edge.GetType() == StatEdge.Type_Regular)
                {
                    post = edge.GetDestination();
                }
            }
            vars.Add(new VarExprent(DecompilerContext.GetCounterContainer().GetCounterAndIncrement
                                        (CounterContainer.Var_Counter), new VarType(ICodeConstants.Type_Object, 0, "java/lang/Throwable"
                                                                                    ), DecompilerContext.GetVarProcessor()));
        }
예제 #10
0
 private CatchStatement(Statement head, Statement next, HashSet <Statement> setHandlers
                        )
     : this()
 {
     first = head;
     stats.AddWithKey(first, first.id);
     foreach (StatEdge edge in head.GetSuccessorEdges(StatEdge.Type_Exception))
     {
         Statement stat = edge.GetDestination();
         if (setHandlers.Contains(stat))
         {
             stats.AddWithKey(stat, stat.id);
             exctstrings.Add(new List <string>(edge.GetExceptions()));
             vars.Add(new VarExprent(DecompilerContext.GetCounterContainer().GetCounterAndIncrement
                                         (CounterContainer.Var_Counter), new VarType(ICodeConstants.Type_Object, 0, edge.
                                                                                     GetExceptions()[0]), DecompilerContext.GetVarProcessor()));
         }
     }
     // FIXME: for now simply the first type. Should get the first common superclass when possible.
     if (next != null)
     {
         post = next;
     }
 }
예제 #11
0
        private static int IntegrateExits(Statement stat)
        {
            int       ret = 0;
            Statement dest;

            if (stat.GetExprents() == null)
            {
                while (true)
                {
                    int changed = 0;
                    foreach (Statement st in stat.GetStats())
                    {
                        changed = IntegrateExits(st);
                        if (changed > 0)
                        {
                            ret = 1;
                            break;
                        }
                    }
                    if (changed == 0)
                    {
                        break;
                    }
                }
                if (stat.type == Statement.Type_If)
                {
                    IfStatement ifst = (IfStatement)stat;
                    if (ifst.GetIfstat() == null)
                    {
                        StatEdge ifedge = ifst.GetIfEdge();
                        dest = IsExitEdge(ifedge);
                        if (dest != null)
                        {
                            BasicBlockStatement bstat = new BasicBlockStatement(new BasicBlock(DecompilerContext
                                                                                               .GetCounterContainer().GetCounterAndIncrement(CounterContainer.Statement_Counter
                                                                                                                                             )));
                            bstat.SetExprents(DecHelper.CopyExprentList(dest.GetExprents()));
                            ifst.GetFirst().RemoveSuccessor(ifedge);
                            StatEdge newedge = new StatEdge(StatEdge.Type_Regular, ifst.GetFirst(), bstat);
                            ifst.GetFirst().AddSuccessor(newedge);
                            ifst.SetIfEdge(newedge);
                            ifst.SetIfstat(bstat);
                            ifst.GetStats().AddWithKey(bstat, bstat.id);
                            bstat.SetParent(ifst);
                            StatEdge oldexitedge = dest.GetAllSuccessorEdges()[0];
                            StatEdge newexitedge = new StatEdge(StatEdge.Type_Break, bstat, oldexitedge.GetDestination
                                                                    ());
                            bstat.AddSuccessor(newexitedge);
                            oldexitedge.closure.AddLabeledEdge(newexitedge);
                            ret = 1;
                        }
                    }
                }
            }
            if (stat.GetAllSuccessorEdges().Count == 1 && stat.GetAllSuccessorEdges()[0].GetType
                    () == StatEdge.Type_Break && (stat.GetLabelEdges().Count == 0))
            {
                Statement parent = stat.GetParent();
                if (stat != parent.GetFirst() || (parent.type != Statement.Type_If && parent.type
                                                  != Statement.Type_Switch))
                {
                    StatEdge destedge = stat.GetAllSuccessorEdges()[0];
                    dest = IsExitEdge(destedge);
                    if (dest != null)
                    {
                        stat.RemoveSuccessor(destedge);
                        BasicBlockStatement bstat = new BasicBlockStatement(new BasicBlock(DecompilerContext
                                                                                           .GetCounterContainer().GetCounterAndIncrement(CounterContainer.Statement_Counter
                                                                                                                                         )));
                        bstat.SetExprents(DecHelper.CopyExprentList(dest.GetExprents()));
                        StatEdge oldexitedge = dest.GetAllSuccessorEdges()[0];
                        StatEdge newexitedge = new StatEdge(StatEdge.Type_Break, bstat, oldexitedge.GetDestination
                                                                ());
                        bstat.AddSuccessor(newexitedge);
                        oldexitedge.closure.AddLabeledEdge(newexitedge);
                        SequenceStatement block = new SequenceStatement(Sharpen.Arrays.AsList(stat, bstat
                                                                                              ));
                        block.SetAllParent();
                        parent.ReplaceStatement(stat, block);
                        // LabelHelper.lowContinueLabels not applicable because of forward continue edges
                        // LabelHelper.lowContinueLabels(block, new HashSet<StatEdge>());
                        // do it by hand
                        foreach (StatEdge prededge in block.GetPredecessorEdges(StatEdge.Type_Continue))
                        {
                            block.RemovePredecessor(prededge);
                            prededge.GetSource().ChangeEdgeNode(Statement.Direction_Forward, prededge, stat);
                            stat.AddPredecessor(prededge);
                            stat.AddLabeledEdge(prededge);
                        }
                        stat.AddSuccessor(new StatEdge(StatEdge.Type_Regular, stat, bstat));
                        foreach (StatEdge edge in dest.GetAllPredecessorEdges())
                        {
                            if (!edge.@explicit && stat.ContainsStatementStrict(edge.GetSource()) && MergeHelper
                                .IsDirectPath(edge.GetSource().GetParent(), bstat))
                            {
                                dest.RemovePredecessor(edge);
                                edge.GetSource().ChangeEdgeNode(Statement.Direction_Forward, edge, bstat);
                                bstat.AddPredecessor(edge);
                                if (!stat.ContainsStatementStrict(edge.closure))
                                {
                                    stat.AddLabeledEdge(edge);
                                }
                            }
                        }
                        ret = 2;
                    }
                }
            }
            return(ret);
        }
예제 #12
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;
        }
예제 #13
0
        public virtual void Init()
        {
            DecompilerContext.SetProperty(DecompilerContext.Current_Class, classStruct);
            DecompilerContext.SetProperty(DecompilerContext.Current_Class_Wrapper, this);
            DecompilerContext.GetLogger().StartClass(classStruct.qualifiedName);
            int maxSec = System.Convert.ToInt32(DecompilerContext.GetProperty(IFernflowerPreferences
                                                                              .Max_Processing_Method).ToString());
            bool testMode = DecompilerContext.GetOption(IFernflowerPreferences.Unit_Test_Mode
                                                        );

            foreach (StructMethod mt in classStruct.GetMethods())
            {
                DecompilerContext.GetLogger().StartMethod(mt.GetName() + " " + mt.GetDescriptor()
                                                          );
                MethodDescriptor md      = MethodDescriptor.ParseDescriptor(mt.GetDescriptor());
                VarProcessor     varProc = new VarProcessor(mt, md);
                DecompilerContext.StartMethod(varProc);
                VarNamesCollector vc      = varProc.GetVarNamesCollector();
                CounterContainer  counter = DecompilerContext.GetCounterContainer();
                RootStatement     root    = null;
                bool isError = false;
                try
                {
                    if (mt.ContainsCode())
                    {
                        if (maxSec == 0 || testMode)
                        {
                            root = MethodProcessorRunnable.CodeToJava(mt, md, varProc);
                        }
                        else
                        {
                            MethodProcessorRunnable mtProc = new MethodProcessorRunnable(mt, md, varProc, DecompilerContext
                                                                                         .GetCurrentContext());
                            Thread mtThread = new Thread(o => mtProc.Run())
                            {
                                Name = "Java decompiler"
                            };
                            long stopAt = Runtime.CurrentTimeMillis() + maxSec * 1000L;
                            mtThread.Start();
                            while (!mtProc.IsFinished())
                            {
                                try
                                {
                                    lock (mtProc.Lock)
                                    {
                                        Thread.Sleep(200);
                                    }
                                }
                                catch (Exception e)
                                {
                                    KillThread(mtThread);
                                    throw;
                                }
                                if (Runtime.CurrentTimeMillis() >= stopAt)
                                {
                                    string message = "Processing time limit exceeded for method " + mt.GetName() + ", execution interrupted.";
                                    DecompilerContext.GetLogger().WriteMessage(message, IFernflowerLogger.Severity.Error
                                                                               );
                                    KillThread(mtThread);
                                    isError = true;
                                    break;
                                }
                            }
                            if (!isError)
                            {
                                root = mtProc.GetResult();
                            }
                        }
                    }
                    else
                    {
                        bool thisVar    = !mt.HasModifier(ICodeConstants.Acc_Static);
                        int  paramCount = 0;
                        if (thisVar)
                        {
                            Sharpen.Collections.Put(varProc.GetThisVars(), new VarVersionPair(0, 0), classStruct
                                                    .qualifiedName);
                            paramCount = 1;
                        }
                        paramCount += [email protected];
                        int varIndex = 0;
                        for (int i = 0; i < paramCount; i++)
                        {
                            varProc.SetVarName(new VarVersionPair(varIndex, 0), vc.GetFreeName(varIndex));
                            if (thisVar)
                            {
                                if (i == 0)
                                {
                                    varIndex++;
                                }
                                else
                                {
                                    varIndex += md.@params[i - 1].stackSize;
                                }
                            }
                            else
                            {
                                varIndex += md.@params[i].stackSize;
                            }
                        }
                    }
                }
                catch (Exception t)
                {
                    string message = "Method " + mt.GetName() + " " + mt.GetDescriptor() + " couldn't be decompiled.";
                    DecompilerContext.GetLogger().WriteMessage(message, IFernflowerLogger.Severity.Warn
                                                               , t);
                    isError = true;
                }
                MethodWrapper methodWrapper = new MethodWrapper(root, varProc, mt, counter);
                methodWrapper.decompiledWithErrors = isError;
                methods.AddWithKey(methodWrapper, InterpreterUtil.MakeUniqueKey(mt.GetName(), mt.
                                                                                GetDescriptor()));
                if (!isError)
                {
                    // rename vars so that no one has the same name as a field
                    VarNamesCollector namesCollector = new VarNamesCollector();
                    classStruct.GetFields().ForEach((StructField f) => namesCollector.AddName(f.GetName
                                                                                                  ()));
                    varProc.RefreshVarNames(namesCollector);
                    // if debug information present and should be used
                    if (DecompilerContext.GetOption(IFernflowerPreferences.Use_Debug_Var_Names))
                    {
                        StructLocalVariableTableAttribute attr = mt.GetLocalVariableAttr();
                        if (attr != null)
                        {
                            // only param names here
                            varProc.SetDebugVarNames(attr.GetMapParamNames());
                            // the rest is here
                            methodWrapper.GetOrBuildGraph().IterateExprents((Exprent exprent) => {
                                List <Exprent> lst = exprent.GetAllExprents(true);
                                lst.Add(exprent);
                                lst.Where(e => e.type == Exprent.Exprent_Var).ToList().ForEach((Exprent
                                                                                                e) => {
                                    VarExprent varExprent = (VarExprent)e;
                                    string name           = varExprent.GetDebugName(mt);
                                    if (name != null)
                                    {
                                        varProc.SetVarName(varExprent.GetVarVersionPair(), name);
                                    }
                                }
                                                                                               );
                                return(0);
                            }
                                                                            );
                        }
                    }
                }
                DecompilerContext.GetLogger().EndMethod();
            }
            DecompilerContext.GetLogger().EndClass();
        }
예제 #14
0
        private bool ProcessStatementEx(StructMethod mt, RootStatement root, ControlFlowGraph
                                        graph)
        {
            int bytecode_version         = mt.GetClassStruct().GetBytecodeVersion();
            LinkedList <Statement> stack = new LinkedList <Statement>();

            stack.AddLast(root);
            while (!(stack.Count == 0))
            {
                Statement stat   = Sharpen.Collections.RemoveLast(stack);
                Statement parent = stat.GetParent();
                if (parent != null && parent.type == Statement.Type_Catchall && stat == parent.GetFirst
                        () && !parent.IsCopied())
                {
                    CatchAllStatement fin     = (CatchAllStatement)parent;
                    BasicBlock        head    = fin.GetBasichead().GetBlock();
                    BasicBlock        handler = fin.GetHandler().GetBasichead().GetBlock();
                    if (catchallBlockIDs.ContainsKey(handler.id))
                    {
                    }
                    else if (finallyBlockIDs.ContainsKey(handler.id))
                    {
                        // do nothing
                        fin.SetFinally(true);
                        int?var = finallyBlockIDs.GetOrNullable(handler.id);
                        fin.SetMonitor(var == null ? null : new VarExprent(var.Value, VarType.Vartype_Int
                                                                           , varProcessor));
                    }
                    else
                    {
                        FinallyProcessor.Record inf = GetFinallyInformation(mt, root, fin);
                        if (inf == null)
                        {
                            // inconsistent finally
                            Sharpen.Collections.Put(catchallBlockIDs, handler.id, null);
                        }
                        else
                        {
                            if (DecompilerContext.GetOption(IFernflowerPreferences.Finally_Deinline) && VerifyFinallyEx
                                    (graph, fin, inf))
                            {
                                Sharpen.Collections.Put(finallyBlockIDs, handler.id, null);
                            }
                            else
                            {
                                int varindex = DecompilerContext.GetCounterContainer().GetCounterAndIncrement(CounterContainer
                                                                                                              .Var_Counter);
                                InsertSemaphore(graph, GetAllBasicBlocks(fin.GetFirst()), head, handler, varindex
                                                , inf, bytecode_version);
                                Sharpen.Collections.Put(finallyBlockIDs, handler.id, varindex);
                            }
                            DeadCodeHelper.RemoveDeadBlocks(graph);
                            // e.g. multiple return blocks after a nested finally
                            DeadCodeHelper.RemoveEmptyBlocks(graph);
                            DeadCodeHelper.MergeBasicBlocks(graph);
                        }
                        return(true);
                    }
                }
                Sharpen.Collections.AddAll(stack, stat.GetStats());
            }
            return(false);
        }
예제 #15
0
        private void SetNewVarIndices(VarTypeProcessor typeProcessor, DirectGraph graph,
                                      VarVersionsProcessor previousVersionsProcessor)
        {
            Dictionary <VarVersionPair, VarType> mapExprentMaxTypes = typeProcessor.GetMapExprentMaxTypes
                                                                          ();
            Dictionary <VarVersionPair, VarType> mapExprentMinTypes = typeProcessor.GetMapExprentMinTypes
                                                                          ();
            Dictionary <VarVersionPair, int> mapFinalVars = typeProcessor.GetMapFinalVars();
            CounterContainer counters = DecompilerContext.GetCounterContainer();
            Dictionary <VarVersionPair, int> mapVarPaar = new Dictionary <VarVersionPair, int>
                                                              ();
            Dictionary <int, int> mapOriginalVarIndices = new Dictionary <int, int>();

            // map var-version pairs on new var indexes
            foreach (VarVersionPair pair in new List <VarVersionPair>(mapExprentMinTypes.Keys))
            {
                if (pair.version >= 0)
                {
                    int newIndex = pair.version == 1 ? pair.var : counters.GetCounterAndIncrement(CounterContainer
                                                                                                  .Var_Counter);
                    VarVersionPair newVar = new VarVersionPair(newIndex, 0);
                    Sharpen.Collections.Put(mapExprentMinTypes, newVar, mapExprentMinTypes.GetOrNull(
                                                pair));
                    Sharpen.Collections.Put(mapExprentMaxTypes, newVar, mapExprentMaxTypes.GetOrNull(
                                                pair));
                    if (mapFinalVars.ContainsKey(pair))
                    {
                        Sharpen.Collections.Put(mapFinalVars, newVar, Sharpen.Collections.Remove(mapFinalVars
                                                                                                 , pair));
                    }
                    Sharpen.Collections.Put(mapVarPaar, pair, newIndex);
                    Sharpen.Collections.Put(mapOriginalVarIndices, newIndex, pair.var);
                }
            }
            // set new vars
            graph.IterateExprents((Exprent exprent) => {
                List <Exprent> lst = exprent.GetAllExprents(true);
                lst.Add(exprent);
                foreach (Exprent expr in lst)
                {
                    if (expr.type == Exprent.Exprent_Var)
                    {
                        VarExprent newVar = (VarExprent)expr;
                        int?newVarIndex   = mapVarPaar.GetOrNullable(new VarVersionPair(newVar));
                        if (newVarIndex != null)
                        {
                            newVar.SetIndex(newVarIndex.Value);
                            newVar.SetVersion(0);
                        }
                    }
                    else if (expr.type == Exprent.Exprent_Const)
                    {
                        VarType maxType = mapExprentMaxTypes.GetOrNull(new VarVersionPair(expr.id, -1));
                        if (maxType != null && maxType.Equals(VarType.Vartype_Char))
                        {
                            ((ConstExprent)expr).SetConstType(maxType);
                        }
                    }
                }
                return(0);
            }
                                  );
            if (previousVersionsProcessor != null)
            {
                Dictionary <int, int> oldIndices = previousVersionsProcessor.GetMapOriginalVarIndices
                                                       ();
                this.mapOriginalVarIndices = new Dictionary <int, int>(mapOriginalVarIndices.Count
                                                                       );
                foreach (KeyValuePair <int, int> entry in mapOriginalVarIndices)
                {
                    int value    = entry.Value;
                    int?oldValue = oldIndices.GetOrNullable(value);
                    value = oldValue != null ? oldValue.Value : value;
                    Sharpen.Collections.Put(this.mapOriginalVarIndices, entry.Key, value);
                }
            }
            else
            {
                this.mapOriginalVarIndices = mapOriginalVarIndices;
            }
        }
        /// <exception cref="System.IO.IOException"/>
        public static RootStatement CodeToJava(StructMethod mt, MethodDescriptor md, VarProcessor
                                               varProc)
        {
            StructClass cl            = mt.GetClassStruct();
            bool        isInitializer = ICodeConstants.Clinit_Name.Equals(mt.GetName());

            // for now static initializer only
            mt.ExpandData();
            InstructionSequence seq   = mt.GetInstructionSequence();
            ControlFlowGraph    graph = new ControlFlowGraph(seq);

            DeadCodeHelper.RemoveDeadBlocks(graph);
            graph.InlineJsr(mt);
            // TODO: move to the start, before jsr inlining
            DeadCodeHelper.ConnectDummyExitBlock(graph);
            DeadCodeHelper.RemoveGotos(graph);
            ExceptionDeobfuscator.RemoveCircularRanges(graph);
            ExceptionDeobfuscator.RestorePopRanges(graph);
            if (DecompilerContext.GetOption(IFernflowerPreferences.Remove_Empty_Ranges))
            {
                ExceptionDeobfuscator.RemoveEmptyRanges(graph);
            }
            if (DecompilerContext.GetOption(IFernflowerPreferences.Ensure_Synchronized_Monitor
                                            ))
            {
                // special case: search for 'synchronized' ranges w/o monitorexit instruction (as generated by Kotlin and Scala)
                DeadCodeHelper.ExtendSynchronizedRangeToMonitorexit(graph);
            }
            if (DecompilerContext.GetOption(IFernflowerPreferences.No_Exceptions_Return))
            {
                // special case: single return instruction outside of a protected range
                DeadCodeHelper.IncorporateValueReturns(graph);
            }
            //		ExceptionDeobfuscator.restorePopRanges(graph);
            ExceptionDeobfuscator.InsertEmptyExceptionHandlerBlocks(graph);
            DeadCodeHelper.MergeBasicBlocks(graph);
            DecompilerContext.GetCounterContainer().SetCounter(CounterContainer.Var_Counter,
                                                               mt.GetLocalVariables());
            if (ExceptionDeobfuscator.HasObfuscatedExceptions(graph))
            {
                DecompilerContext.GetLogger().WriteMessage("Heavily obfuscated exception ranges found!"
                                                           , IFernflowerLogger.Severity.Warn);
                if (!ExceptionDeobfuscator.HandleMultipleEntryExceptionRanges(graph))
                {
                    DecompilerContext.GetLogger().WriteMessage("Found multiple entry exception ranges which could not be splitted"
                                                               , IFernflowerLogger.Severity.Warn);
                }
                ExceptionDeobfuscator.InsertDummyExceptionHandlerBlocks(graph, cl.GetBytecodeVersion
                                                                            ());
            }
            RootStatement    root  = DomHelper.ParseGraph(graph);
            FinallyProcessor fProc = new FinallyProcessor(md, varProc);

            while (fProc.IterateGraph(mt, root, graph))
            {
                root = DomHelper.ParseGraph(graph);
            }
            // remove synchronized exception handler
            // not until now because of comparison between synchronized statements in the finally cycle
            DomHelper.RemoveSynchronizedHandler(root);
            //		LabelHelper.lowContinueLabels(root, new HashSet<StatEdge>());
            SequenceHelper.CondenseSequences(root);
            ClearStructHelper.ClearStatements(root);
            ExprProcessor proc = new ExprProcessor(md, varProc);

            proc.ProcessStatement(root, cl);
            SequenceHelper.CondenseSequences(root);
            StackVarsProcessor stackProc = new StackVarsProcessor();

            do
            {
                stackProc.SimplifyStackVars(root, mt, cl);
                varProc.SetVarVersions(root);
            }while (new PPandMMHelper().FindPPandMM(root));
            while (true)
            {
                LabelHelper.CleanUpEdges(root);
                do
                {
                    MergeHelper.EnhanceLoops(root);
                }while (LoopExtractHelper.ExtractLoops(root) || IfHelper.MergeAllIfs(root));
                if (DecompilerContext.GetOption(IFernflowerPreferences.Idea_Not_Null_Annotation))
                {
                    if (IdeaNotNullHelper.RemoveHardcodedChecks(root, mt))
                    {
                        SequenceHelper.CondenseSequences(root);
                        stackProc.SimplifyStackVars(root, mt, cl);
                        varProc.SetVarVersions(root);
                    }
                }
                LabelHelper.IdentifyLabels(root);
                if (InlineSingleBlockHelper.InlineSingleBlocks(root))
                {
                    continue;
                }
                // initializer may have at most one return point, so no transformation of method exits permitted
                if (isInitializer || !ExitHelper.CondenseExits(root))
                {
                    break;
                }
            }
            // FIXME: !!
            //if(!EliminateLoopsHelper.eliminateLoops(root)) {
            //  break;
            //}
            ExitHelper.RemoveRedundantReturns(root);
            SecondaryFunctionsHelper.IdentifySecondaryFunctions(root, varProc);
            varProc.SetVarDefinitions(root);
            // must be the last invocation, because it makes the statement structure inconsistent
            // FIXME: new edge type needed
            LabelHelper.ReplaceContinueWithBreak(root);
            mt.ReleaseResources();
            return(root);
        }
예제 #17
0
        private static Exprent IdentifySecondaryFunctions(Exprent exprent, bool statement_level
                                                          , VarProcessor varProc)
        {
            if (exprent.type == Exprent.Exprent_Function)
            {
                FunctionExprent fexpr = (FunctionExprent)exprent;
                switch (fexpr.GetFuncType())
                {
                case FunctionExprent.Function_Bool_Not:
                {
                    Exprent retparam = PropagateBoolNot(fexpr);
                    if (retparam != null)
                    {
                        return(retparam);
                    }
                    break;
                }

                case FunctionExprent.Function_Eq:
                case FunctionExprent.Function_Ne:
                case FunctionExprent.Function_Gt:
                case FunctionExprent.Function_Ge:
                case FunctionExprent.Function_Lt:
                case FunctionExprent.Function_Le:
                {
                    Exprent expr1 = fexpr.GetLstOperands()[0];
                    Exprent expr2 = fexpr.GetLstOperands()[1];
                    if (expr1.type == Exprent.Exprent_Const)
                    {
                        expr2 = expr1;
                        expr1 = fexpr.GetLstOperands()[1];
                    }
                    if (expr1.type == Exprent.Exprent_Function && expr2.type == Exprent.Exprent_Const)
                    {
                        FunctionExprent funcexpr = (FunctionExprent)expr1;
                        ConstExprent    cexpr    = (ConstExprent)expr2;
                        int             functype = funcexpr.GetFuncType();
                        if (functype == FunctionExprent.Function_Lcmp || functype == FunctionExprent.Function_Fcmpg ||
                            functype == FunctionExprent.Function_Fcmpl || functype == FunctionExprent.Function_Dcmpg ||
                            functype == FunctionExprent.Function_Dcmpl)
                        {
                            int    desttype = -1;
                            int?[] destcons = mapNumComparisons.GetOrNull(fexpr.GetFuncType());
                            if (destcons != null)
                            {
                                int index = cexpr.GetIntValue() + 1;
                                if (index >= 0 && index <= 2)
                                {
                                    int?destcon = destcons[index];
                                    if (destcon != null)
                                    {
                                        desttype = destcon.Value;
                                    }
                                }
                            }
                            if (desttype >= 0)
                            {
                                return(new FunctionExprent(desttype, funcexpr.GetLstOperands(), funcexpr.bytecode
                                                           ));
                            }
                        }
                    }
                    break;
                }
                }
            }
            bool replaced = true;

            while (replaced)
            {
                replaced = false;
                foreach (Exprent expr in exprent.GetAllExprents())
                {
                    Exprent retexpr = IdentifySecondaryFunctions(expr, false, varProc);
                    if (retexpr != null)
                    {
                        exprent.ReplaceExprent(expr, retexpr);
                        replaced = true;
                        break;
                    }
                }
            }
            switch (exprent.type)
            {
            case Exprent.Exprent_Function:
            {
                FunctionExprent fexpr_1     = (FunctionExprent)exprent;
                List <Exprent>  lstOperands = fexpr_1.GetLstOperands();
                switch (fexpr_1.GetFuncType())
                {
                case FunctionExprent.Function_Xor:
                {
                    for (int i = 0; i < 2; i++)
                    {
                        Exprent operand     = lstOperands[i];
                        VarType operandtype = operand.GetExprType();
                        if (operand.type == Exprent.Exprent_Const && operandtype.type != ICodeConstants.Type_Boolean)
                        {
                            ConstExprent cexpr = (ConstExprent)operand;
                            long         val;
                            if (operandtype.type == ICodeConstants.Type_Long)
                            {
                                val = (long)cexpr.GetValue();
                            }
                            else
                            {
                                val = (int)cexpr.GetValue();
                            }
                            if (val == -1)
                            {
                                List <Exprent> lstBitNotOperand = new List <Exprent>();
                                lstBitNotOperand.Add(lstOperands[1 - i]);
                                return(new FunctionExprent(FunctionExprent.Function_Bit_Not, lstBitNotOperand, fexpr_1
                                                           .bytecode));
                            }
                        }
                    }
                    break;
                }

                case FunctionExprent.Function_Eq:
                case FunctionExprent.Function_Ne:
                {
                    if (lstOperands[0].GetExprType().type == ICodeConstants.Type_Boolean && lstOperands
                        [1].GetExprType().type == ICodeConstants.Type_Boolean)
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            if (lstOperands[i].type == Exprent.Exprent_Const)
                            {
                                ConstExprent cexpr = (ConstExprent)lstOperands[i];
                                int          val   = (int)cexpr.GetValue();
                                if ((fexpr_1.GetFuncType() == FunctionExprent.Function_Eq && val == 1) || (fexpr_1
                                                                                                           .GetFuncType() == FunctionExprent.Function_Ne && val == 0))
                                {
                                    return(lstOperands[1 - i]);
                                }
                                else
                                {
                                    List <Exprent> lstNotOperand = new List <Exprent>();
                                    lstNotOperand.Add(lstOperands[1 - i]);
                                    return(new FunctionExprent(FunctionExprent.Function_Bool_Not, lstNotOperand, fexpr_1
                                                               .bytecode));
                                }
                            }
                        }
                    }
                    break;
                }

                case FunctionExprent.Function_Bool_Not:
                {
                    if (lstOperands[0].type == Exprent.Exprent_Const)
                    {
                        int val = ((ConstExprent)lstOperands[0]).GetIntValue();
                        if (val == 0)
                        {
                            return(new ConstExprent(VarType.Vartype_Boolean, 1, fexpr_1.bytecode));
                        }
                        else
                        {
                            return(new ConstExprent(VarType.Vartype_Boolean, 0, fexpr_1.bytecode));
                        }
                    }
                    break;
                }

                case FunctionExprent.Function_Iif:
                {
                    Exprent expr1_1 = lstOperands[1];
                    Exprent expr2_1 = lstOperands[2];
                    if (expr1_1.type == Exprent.Exprent_Const && expr2_1.type == Exprent.Exprent_Const)
                    {
                        ConstExprent cexpr1 = (ConstExprent)expr1_1;
                        ConstExprent cexpr2 = (ConstExprent)expr2_1;
                        if (cexpr1.GetExprType().type == ICodeConstants.Type_Boolean && cexpr2.GetExprType
                                ().type == ICodeConstants.Type_Boolean)
                        {
                            if (cexpr1.GetIntValue() == 0 && cexpr2.GetIntValue() != 0)
                            {
                                return(new FunctionExprent(FunctionExprent.Function_Bool_Not, lstOperands[0], fexpr_1
                                                           .bytecode));
                            }
                            else if (cexpr1.GetIntValue() != 0 && cexpr2.GetIntValue() == 0)
                            {
                                return(lstOperands[0]);
                            }
                        }
                    }
                    break;
                }

                case FunctionExprent.Function_Lcmp:
                case FunctionExprent.Function_Fcmpl:
                case FunctionExprent.Function_Fcmpg:
                case FunctionExprent.Function_Dcmpl:
                case FunctionExprent.Function_Dcmpg:
                {
                    int var = DecompilerContext.GetCounterContainer().GetCounterAndIncrement(CounterContainer
                                                                                             .Var_Counter);
                    VarType         type = lstOperands[0].GetExprType();
                    FunctionExprent iff  = new FunctionExprent(FunctionExprent.Function_Iif, Sharpen.Arrays.AsList <Exprent>
                                                                   (new FunctionExprent(FunctionExprent.Function_Lt, Sharpen.Arrays.AsList <Exprent>(new VarExprent
                                                                                                                                                         (var, type, varProc), ConstExprent.GetZeroConstant(type.type)), null), new ConstExprent
                                                                       (VarType.Vartype_Int, -1, null), new ConstExprent(VarType.Vartype_Int, 1, null))
                                                               , null);
                    FunctionExprent head = new FunctionExprent(FunctionExprent.Function_Eq, Sharpen.Arrays.AsList <Exprent>
                                                                   (new AssignmentExprent(new VarExprent(var, type, varProc), new FunctionExprent(FunctionExprent
                                                                                                                                                  .Function_Sub, Sharpen.Arrays.AsList(lstOperands[0], lstOperands[1]), null), null
                                                                                          ), ConstExprent.GetZeroConstant(type.type)), null);
                    varProc.SetVarType(new VarVersionPair(var, 0), type);
                    return(new FunctionExprent(FunctionExprent.Function_Iif, Sharpen.Arrays.AsList <Exprent>(head
                                                                                                             , new ConstExprent(VarType.Vartype_Int, 0, null), iff), fexpr_1.bytecode));
                }
                }
                break;
            }

            case Exprent.Exprent_Assignment:
            {
                // check for conditional assignment
                AssignmentExprent asexpr = (AssignmentExprent)exprent;
                Exprent           right  = asexpr.GetRight();
                Exprent           left   = asexpr.GetLeft();
                if (right.type == Exprent.Exprent_Function)
                {
                    FunctionExprent func     = (FunctionExprent)right;
                    VarType         midlayer = null;
                    if (func.GetFuncType() >= FunctionExprent.Function_I2l && func.GetFuncType() <= FunctionExprent
                        .Function_I2s)
                    {
                        right    = func.GetLstOperands()[0];
                        midlayer = func.GetSimpleCastType();
                        if (right.type == Exprent.Exprent_Function)
                        {
                            func = (FunctionExprent)right;
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    List <Exprent> lstFuncOperands = func.GetLstOperands();
                    Exprent        cond            = null;
                    switch (func.GetFuncType())
                    {
                    case FunctionExprent.Function_Add:
                    case FunctionExprent.Function_And:
                    case FunctionExprent.Function_Or:
                    case FunctionExprent.Function_Xor:
                    {
                        if (left.Equals(lstFuncOperands[1]))
                        {
                            cond = lstFuncOperands[0];
                            break;
                        }
                        goto case FunctionExprent.Function_Sub;
                    }

                    case FunctionExprent.Function_Sub:
                    case FunctionExprent.Function_Mul:
                    case FunctionExprent.Function_Div:
                    case FunctionExprent.Function_Rem:
                    case FunctionExprent.Function_Shl:
                    case FunctionExprent.Function_Shr:
                    case FunctionExprent.Function_Ushr:
                    {
                        if (left.Equals(lstFuncOperands[0]))
                        {
                            cond = lstFuncOperands[1];
                        }
                        break;
                    }
                    }
                    if (cond != null && (midlayer == null || midlayer.Equals(cond.GetExprType())))
                    {
                        asexpr.SetRight(cond);
                        asexpr.SetCondType(func.GetFuncType());
                    }
                }
                break;
            }

            case Exprent.Exprent_Invocation:
            {
                if (!statement_level)
                {
                    // simplify if exprent is a real expression. The opposite case is pretty absurd, can still happen however (and happened at least once).
                    Exprent retexpr = ConcatenationHelper.ContractStringConcat(exprent);
                    if (!exprent.Equals(retexpr))
                    {
                        return(retexpr);
                    }
                }
                break;
            }
            }
            return(null);
        }
예제 #18
0
        private static bool MatchWhile(DoStatement stat)
        {
            // search for an if condition at the entrance of the loop
            Statement first = stat.GetFirst();

            while (first.type == Statement.Type_Sequence)
            {
                first = first.GetFirst();
            }
            // found an if statement
            if (first.type == Statement.Type_If)
            {
                IfStatement firstif = (IfStatement)first;
                if ((firstif.GetFirst().GetExprents().Count == 0))
                {
                    if (firstif.iftype == IfStatement.Iftype_If)
                    {
                        if (firstif.GetIfstat() == null)
                        {
                            StatEdge ifedge = firstif.GetIfEdge();
                            if (IsDirectPath(stat, ifedge.GetDestination()))
                            {
                                // exit condition identified
                                stat.SetLooptype(DoStatement.Loop_While);
                                // negate condition (while header)
                                IfExprent ifexpr = (IfExprent)firstif.GetHeadexprent().Copy();
                                ifexpr.NegateIf();
                                stat.SetConditionExprent(ifexpr.GetCondition());
                                // remove edges
                                firstif.GetFirst().RemoveSuccessor(ifedge);
                                firstif.RemoveSuccessor(firstif.GetAllSuccessorEdges()[0]);
                                if ((stat.GetAllSuccessorEdges().Count == 0))
                                {
                                    ifedge.SetSource(stat);
                                    if (ifedge.closure == stat)
                                    {
                                        ifedge.closure = stat.GetParent();
                                    }
                                    stat.AddSuccessor(ifedge);
                                }
                                // remove empty if statement as it is now part of the loop
                                if (firstif == stat.GetFirst())
                                {
                                    BasicBlockStatement bstat = new BasicBlockStatement(new BasicBlock(DecompilerContext
                                                                                                       .GetCounterContainer().GetCounterAndIncrement(CounterContainer.Statement_Counter
                                                                                                                                                     )));
                                    bstat.SetExprents(new List <Exprent>());
                                    stat.ReplaceStatement(firstif, bstat);
                                }
                                else
                                {
                                    // precondition: sequence must contain more than one statement!
                                    Statement sequence = firstif.GetParent();
                                    sequence.GetStats().RemoveWithKey(firstif.id);
                                    sequence.SetFirst(sequence.GetStats()[0]);
                                }
                                return(true);
                            }
                        }
                        else
                        {
                            StatEdge elseedge = firstif.GetAllSuccessorEdges()[0];
                            if (IsDirectPath(stat, elseedge.GetDestination()))
                            {
                                // exit condition identified
                                stat.SetLooptype(DoStatement.Loop_While);
                                // no need to negate the while condition
                                stat.SetConditionExprent(((IfExprent)firstif.GetHeadexprent().Copy()).GetCondition
                                                             ());
                                // remove edges
                                StatEdge ifedge = firstif.GetIfEdge();
                                firstif.GetFirst().RemoveSuccessor(ifedge);
                                firstif.RemoveSuccessor(elseedge);
                                if ((stat.GetAllSuccessorEdges().Count == 0))
                                {
                                    elseedge.SetSource(stat);
                                    if (elseedge.closure == stat)
                                    {
                                        elseedge.closure = stat.GetParent();
                                    }
                                    stat.AddSuccessor(elseedge);
                                }
                                if (firstif.GetIfstat() == null)
                                {
                                    BasicBlockStatement bstat = new BasicBlockStatement(new BasicBlock(DecompilerContext
                                                                                                       .GetCounterContainer().GetCounterAndIncrement(CounterContainer.Statement_Counter
                                                                                                                                                     )));
                                    bstat.SetExprents(new List <Exprent>());
                                    ifedge.SetSource(bstat);
                                    bstat.AddSuccessor(ifedge);
                                    stat.ReplaceStatement(firstif, bstat);
                                }
                                else
                                {
                                    // replace the if statement with its content
                                    first.GetParent().ReplaceStatement(first, firstif.GetIfstat());
                                    // lift closures
                                    foreach (StatEdge prededge in elseedge.GetDestination().GetPredecessorEdges(StatEdge
                                                                                                                .Type_Break))
                                    {
                                        if (stat.ContainsStatementStrict(prededge.closure))
                                        {
                                            stat.AddLabeledEdge(prededge);
                                        }
                                    }
                                    LabelHelper.LowClosures(stat);
                                }
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }