Exemplo n.º 1
0
        public virtual void RefreshVarNames(VarNamesCollector vc)
        {
            Dictionary <VarVersionPair, string> tempVarNames = new Dictionary <VarVersionPair,
                                                                               string>(mapVarNames);

            foreach (KeyValuePair <VarVersionPair, string> ent in tempVarNames)
            {
                Sharpen.Collections.Put(mapVarNames, ent.Key, vc.GetFreeName(ent.Value));
            }
        }
Exemplo n.º 2
0
        public VarDefinitionHelper(Statement root, StructMethod mt, VarProcessor varproc)
        {
            // statement.id, defined vars
            mapVarDefStatements = new Dictionary <int, Statement>();
            mapStatementVars    = new Dictionary <int, HashSet <int> >();
            implDefVars         = new HashSet <int>();
            this.varproc        = varproc;
            VarNamesCollector vc     = varproc.GetVarNamesCollector();
            bool             thisvar = !mt.HasModifier(ICodeConstants.Acc_Static);
            MethodDescriptor md      = MethodDescriptor.ParseDescriptor(mt.GetDescriptor());
            int paramcount           = 0;

            if (thisvar)
            {
                paramcount = 1;
            }
            paramcount += [email protected];
            // method parameters are implicitly defined
            int varindex = 0;

            for (int i = 0; i < paramcount; i++)
            {
                implDefVars.Add(varindex);
                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;
                }
            }
            if (thisvar)
            {
                StructClass current_class = (StructClass)DecompilerContext.GetProperty(DecompilerContext
                                                                                       .Current_Class);
                Sharpen.Collections.Put(varproc.GetThisVars(), new VarVersionPair(0, 0), current_class
                                        .qualifiedName);
                varproc.SetVarName(new VarVersionPair(0, 0), "this");
                vc.AddName("this");
            }
            // catch variables are implicitly defined
            LinkedList <Statement> stack = new LinkedList <Statement>();

            stack.AddLast(root);
            while (!(stack.Count == 0))
            {
                Statement         st      = Sharpen.Collections.RemoveFirst(stack);
                List <VarExprent> lstVars = null;
                if (st.type == Statement.Type_Catchall)
                {
                    lstVars = ((CatchAllStatement)st).GetVars();
                }
                else if (st.type == Statement.Type_Trycatch)
                {
                    lstVars = ((CatchStatement)st).GetVars();
                }
                if (lstVars != null)
                {
                    foreach (VarExprent var in lstVars)
                    {
                        implDefVars.Add(var.GetIndex());
                        varproc.SetVarName(new VarVersionPair(var), vc.GetFreeName(var.GetIndex()));
                        var.SetDefinition(true);
                    }
                }
                Sharpen.Collections.AddAll(stack, st.GetStats());
            }
            InitStatement(root);
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
0
        public virtual void SetVarDefinitions()
        {
            VarNamesCollector vc = varproc.GetVarNamesCollector();

            foreach (KeyValuePair <int, Statement> en in mapVarDefStatements)
            {
                Statement stat  = en.Value;
                int       index = en.Key;
                if (implDefVars.Contains(index))
                {
                    // already implicitly defined
                    continue;
                }
                varproc.SetVarName(new VarVersionPair(index, 0), vc.GetFreeName(index));
                // special case for
                if (stat.type == Statement.Type_Do)
                {
                    DoStatement dstat = (DoStatement)stat;
                    if (dstat.GetLooptype() == DoStatement.Loop_For)
                    {
                        if (dstat.GetInitExprent() != null && SetDefinition(dstat.GetInitExprent(), index
                                                                            ))
                        {
                            continue;
                        }
                        else
                        {
                            List <Exprent> lstSpecial = Sharpen.Arrays.AsList(dstat.GetConditionExprent(), dstat
                                                                              .GetIncExprent());
                            foreach (VarExprent var in GetAllVars(lstSpecial))
                            {
                                if (var.GetIndex() == index)
                                {
                                    stat = stat.GetParent();
                                    break;
                                }
                            }
                        }
                    }
                }
                Statement      first = FindFirstBlock(stat, index);
                List <Exprent> lst;
                if (first == null)
                {
                    lst = stat.GetVarDefinitions();
                }
                else if (first.GetExprents() == null)
                {
                    lst = first.GetVarDefinitions();
                }
                else
                {
                    lst = first.GetExprents();
                }
                bool defset = false;
                // search for the first assignment to var [index]
                int addindex = 0;
                foreach (Exprent expr in lst)
                {
                    if (SetDefinition(expr, index))
                    {
                        defset = true;
                        break;
                    }
                    else
                    {
                        bool foundvar = false;
                        foreach (Exprent exp in expr.GetAllExprents(true))
                        {
                            if (exp.type == Exprent.Exprent_Var && ((VarExprent)exp).GetIndex() == index)
                            {
                                foundvar = true;
                                break;
                            }
                        }
                        if (foundvar)
                        {
                            break;
                        }
                    }
                    addindex++;
                }
                if (!defset)
                {
                    VarExprent var = new VarExprent(index, varproc.GetVarType(new VarVersionPair(index
                                                                                                 , 0)), varproc);
                    var.SetDefinition(true);
                    lst.Add(addindex, var);
                }
            }
        }