Esempio n. 1
0
        public static void ClearEnum(ClassWrapper wrapper)
        {
            StructClass cl = wrapper.GetClassStruct();

            // hide values/valueOf methods and super() invocations
            foreach (MethodWrapper method in wrapper.GetMethods())
            {
                StructMethod mt         = method.methodStruct;
                string       name       = mt.GetName();
                string       descriptor = mt.GetDescriptor();
                if ("values".Equals(name))
                {
                    if (descriptor.Equals("()[L" + cl.qualifiedName + ";"))
                    {
                        wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(name, descriptor));
                    }
                }
                else if ("valueOf".Equals(name))
                {
                    if (descriptor.Equals("(Ljava/lang/String;)L" + cl.qualifiedName + ";"))
                    {
                        wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(name, descriptor));
                    }
                }
                else if (ICodeConstants.Init_Name.Equals(name))
                {
                    Statement firstData = Statements.FindFirstData(method.root);
                    if (firstData != null && !(firstData.GetExprents().Count == 0))
                    {
                        Exprent exprent = firstData.GetExprents()[0];
                        if (exprent.type == Exprent.Exprent_Invocation)
                        {
                            InvocationExprent invExpr = (InvocationExprent)exprent;
                            if (Statements.IsInvocationInitConstructor(invExpr, method, wrapper, false))
                            {
                                firstData.GetExprents().RemoveAtReturningValue(0);
                            }
                        }
                    }
                }
            }
            // hide synthetic fields of enum and it's constants
            foreach (StructField fd in cl.GetFields())
            {
                string descriptor = fd.GetDescriptor();
                if (fd.IsSynthetic() && descriptor.Equals("[L" + cl.qualifiedName + ";"))
                {
                    wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(fd.GetName(), descriptor
                                                                                 ));
                }
            }
        }
        public static void ProcessClassReferences(ClassesProcessor.ClassNode node)
        {
            // find the synthetic method Class class$(String) if present
            Dictionary <ClassWrapper, MethodWrapper> mapClassMeths = new Dictionary <ClassWrapper
                                                                                     , MethodWrapper>();

            MapClassMethods(node, mapClassMeths);
            if ((mapClassMeths.Count == 0))
            {
                return;
            }
            HashSet <ClassWrapper> setFound = new HashSet <ClassWrapper>();

            ProcessClassRec(node, mapClassMeths, setFound);
            if (!(setFound.Count == 0))
            {
                foreach (ClassWrapper wrp in setFound)
                {
                    StructMethod mt = mapClassMeths.GetOrNull(wrp).methodStruct;
                    wrp.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(mt.GetName(), mt.GetDescriptor
                                                                                 ()));
                }
            }
        }
Esempio n. 3
0
        public virtual byte[] LoadBytecode(StructMethod mt, int codeFullLength)
        {
            string className = mt.GetClassStruct().qualifiedName;

            try
            {
                using (DataInputFullStream @in = GetClassStream(className))
                {
                    if (@in != null)
                    {
                        @in.Discard(8);
                        ConstantPool pool = mt.GetClassStruct().GetPool();
                        if (pool == null)
                        {
                            pool = new ConstantPool(@in);
                        }
                        else
                        {
                            ConstantPool.SkipPool(@in);
                        }
                        @in.Discard(6);
                        // interfaces
                        @in.Discard(@in.ReadUnsignedShort() * 2);
                        // fields
                        int size = @in.ReadUnsignedShort();
                        for (int i = 0; i < size; i++)
                        {
                            @in.Discard(6);
                            SkipAttributes(@in);
                        }
                        // methods
                        size = @in.ReadUnsignedShort();
                        for (int i = 0; i < size; i++)
                        {
                            @in.Discard(2);
                            int      nameIndex       = @in.ReadUnsignedShort();
                            int      descriptorIndex = @in.ReadUnsignedShort();
                            string[] values          = pool.GetClassElement(ConstantPool.Method, className, nameIndex,
                                                                            descriptorIndex);
                            if (!mt.GetName().Equals(values[0]) || !mt.GetDescriptor().Equals(values[1]))
                            {
                                SkipAttributes(@in);
                                continue;
                            }
                            int attrSize = @in.ReadUnsignedShort();
                            for (int j = 0; j < attrSize; j++)
                            {
                                int    attrNameIndex = @in.ReadUnsignedShort();
                                string attrName      = pool.GetPrimitiveConstant(attrNameIndex).GetString();
                                if (!StructGeneralAttribute.Attribute_Code.GetName().Equals(attrName))
                                {
                                    @in.Discard(@in.ReadInt());
                                    continue;
                                }
                                @in.Discard(12);
                                return(@in.Read(codeFullLength));
                            }
                            break;
                        }
                    }
                    return(null);
                }
            }
            catch (IOException ex)
            {
                throw;
            }
        }
Esempio n. 4
0
 public override string ToString()
 {
     return(methodStruct.GetName());
 }
Esempio n. 5
0
        private void RenameClassIdentifiers(StructClass cl, Dictionary <string, string> names
                                            )
        {
            // all classes are already renamed
            string classOldFullName = cl.qualifiedName;
            string classNewFullName = interceptor.GetName(classOldFullName);

            if (classNewFullName == null)
            {
                classNewFullName = classOldFullName;
            }
            // methods
            HashSet <string> setMethodNames = new HashSet <string>();

            foreach (StructMethod md in cl.GetMethods())
            {
                setMethodNames.Add(md.GetName());
            }
            VBStyleCollection <StructMethod, string> methods = cl.GetMethods();

            for (int i = 0; i < methods.Count; i++)
            {
                StructMethod mt        = methods[i];
                string       key       = methods.GetKey(i);
                bool         isPrivate = mt.HasModifier(ICodeConstants.Acc_Private);
                string       name      = mt.GetName();
                if (!cl.IsOwn() || mt.HasModifier(ICodeConstants.Acc_Native))
                {
                    // external and native methods must not be renamed
                    if (!isPrivate)
                    {
                        Sharpen.Collections.Put(names, key, name);
                    }
                }
                else if (helper.ToBeRenamed(IIdentifierRenamer.Type.Element_Method, classOldFullName
                                            , name, mt.GetDescriptor()))
                {
                    if (isPrivate || !names.ContainsKey(key))
                    {
                        do
                        {
                            name = helper.GetNextMethodName(classOldFullName, name, mt.GetDescriptor());
                        }while (setMethodNames.Contains(name));
                        if (!isPrivate)
                        {
                            Sharpen.Collections.Put(names, key, name);
                        }
                    }
                    else
                    {
                        name = names.GetOrNull(key);
                    }
                    interceptor.AddName(classOldFullName + " " + mt.GetName() + " " + mt.GetDescriptor
                                            (), classNewFullName + " " + name + " " + BuildNewDescriptor(false, mt.GetDescriptor
                                                                                                             ()));
                }
            }
            // external fields are not being renamed
            if (!cl.IsOwn())
            {
                return;
            }
            // fields
            // FIXME: should overloaded fields become the same name?
            HashSet <string> setFieldNames = new HashSet <string>();

            foreach (StructField fd in cl.GetFields())
            {
                setFieldNames.Add(fd.GetName());
            }
            foreach (StructField fd in cl.GetFields())
            {
                if (helper.ToBeRenamed(IIdentifierRenamer.Type.Element_Field, classOldFullName, fd
                                       .GetName(), fd.GetDescriptor()))
                {
                    string newName;
                    do
                    {
                        newName = helper.GetNextFieldName(classOldFullName, fd.GetName(), fd.GetDescriptor
                                                              ());
                    }while (setFieldNames.Contains(newName));
                    interceptor.AddName(classOldFullName + " " + fd.GetName() + " " + fd.GetDescriptor
                                            (), classNewFullName + " " + newName + " " + BuildNewDescriptor(true, fd.GetDescriptor
                                                                                                                ()));
                }
            }
        }
        /// <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);
        }