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();
        }
Beispiel #2
0
            /// <summary>
            /// Adds a value to counter, and  returns result value.
            /// </summary>
            /// <param name="incrementValue">Value to be added.</param>
            /// <returns></returns>
            private static int Counter(int incrementValue)
            {
                CounterContainer counter = SuppressEventScopeCounter;

                counter.Counter += incrementValue;

                return(counter.Counter);
            }
Beispiel #3
0
 public MethodWrapper(RootStatement root, VarProcessor varproc, StructMethod methodStruct
                      , CounterContainer counter)
 {
     this.root         = root;
     this.varproc      = varproc;
     this.methodStruct = methodStruct;
     this.counter      = counter;
 }
 public DecompilerContext(Dictionary <string, object> properties, IFernflowerLogger
                          logger, StructContext structContext, ClassesProcessor classProcessor, PoolInterceptor
                          interceptor)
 {
     this.properties       = properties;
     this.logger           = logger;
     this.structContext    = structContext;
     this.classProcessor   = classProcessor;
     this.poolInterceptor  = interceptor;
     this.counterContainer = new CounterContainer();
 }
 public void DoTest()
 {
     _firstTestCounter = new CounterContainer();
     _errorTestCounter = new CounterContainer();
     var tt = new TestThreads(false, FirstTest, 1);
     tt.RunParallel(100);
     while (_firstTestCounter.Counter < 100)
     {
         Thread.Sleep(10);
     }
     Assert.AreEqual(100, _firstTestCounter.Counter);
     tt.Terminate();
 }
 public void DoTestMultiWithPar()
 {
     _firstTestCounter = new CounterContainer();
     _errorTestCounter = new CounterContainer();
     var tt = new TestThreads(false, SecondTest, 10);
     tt.RunParallel(1000, 2);
     while (_firstTestCounter.Counter < 1000 && _errorTestCounter.Counter==0)
     {
         Thread.Sleep(10);
     }
     Assert.AreEqual(0, _errorTestCounter.Counter);
     Assert.AreEqual(1000, _firstTestCounter.Counter);
     tt.Terminate();
 }
 public void DoTestMulti()
 {
     const int iterations = 10000;
     _firstTestCounter = new CounterContainer();
     _errorTestCounter = new CounterContainer();
     var tt = new TestThreads(false, FirstTest, 100);
     tt.RunParallel(iterations);
     while (_firstTestCounter.Counter < iterations)
     {
         Thread.Sleep(10);
     }
     Assert.AreEqual(iterations, _firstTestCounter.Counter);
     tt.Terminate();
 }
        public void DoTestMultiWithParAndReadingThread()
        {
            const int iterations = 10000;
            var lfq = new LockFreeQueue<int>();
            _sendCounter = new CounterContainer();
            _firstTestCounter = new CounterContainer();
            _errorTestCounter = new CounterContainer();
            var ttr = new TestThreads(false, ThirdTestReader, 5);
            ttr.RunParallel(iterations, lfq, true);

            var tts = new TestThreads(false, ThirdTestSender, 5);
            tts.RunParallel(iterations, lfq);
            while (_firstTestCounter.Counter < (iterations-1) && _errorTestCounter.Counter == 0)
            {
                Thread.Sleep(10);
            }
            Assert.AreEqual(0, _errorTestCounter.Counter);
            Assert.AreEqual(iterations, _sendCounter.Counter);
            Assert.AreEqual(iterations-1, _firstTestCounter.Counter);
            tts.Terminate();
            ttr.Terminate();
        }
Beispiel #9
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();
        }
Beispiel #10
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;
            }
        }