private SFormsFastMapDirect CreateFirstMap(StructMethod mt, RootStatement root) { bool thisvar = !mt.HasModifier(ICodeConstants.Acc_Static); MethodDescriptor md = MethodDescriptor.ParseDescriptor(mt.GetDescriptor()); int paramcount = [email protected] + (thisvar ? 1 : 0); int varindex = 0; SFormsFastMapDirect map = new SFormsFastMapDirect(); for (int i = 0; i < paramcount; i++) { int version = GetNextFreeVersion(varindex, root); // == 1 FastSparseSetFactory <int> .FastSparseSet <int> set = factory.SpawnEmptySet(); set.Add(version); map.Put(varindex, set); ssuversions.CreateNode(new VarVersionPair(varindex, version)); if (thisvar) { if (i == 0) { varindex++; } else { varindex += md.@params[i - 1].stackSize; } } else { varindex += md.@params[i].stackSize; } } return(map); }
private void SetInitVars(RootStatement root) { bool thisVar = !method.HasModifier(ICodeConstants.Acc_Static); MethodDescriptor md = methodDescriptor; if (thisVar) { StructClass cl = (StructClass)DecompilerContext.GetProperty(DecompilerContext.Current_Class ); VarType clType = new VarType(ICodeConstants.Type_Object, 0, cl.qualifiedName); Sharpen.Collections.Put(mapExprentMinTypes, new VarVersionPair(0, 1), clType); Sharpen.Collections.Put(mapExprentMaxTypes, new VarVersionPair(0, 1), clType); } int varIndex = 0; for (int i = 0; i < [email protected]; i++) { Sharpen.Collections.Put(mapExprentMinTypes, new VarVersionPair(varIndex + (thisVar ? 1 : 0), 1), md.@params[i]); Sharpen.Collections.Put(mapExprentMaxTypes, new VarVersionPair(varIndex + (thisVar ? 1 : 0), 1), md.@params[i]); varIndex += md.@params[i].stackSize; } // catch variables LinkedList <Statement> stack = new LinkedList <Statement>(); stack.AddLast(root); while (!(stack.Count == 0)) { Statement stat = Sharpen.Collections.RemoveFirst(stack); List <VarExprent> lstVars = null; if (stat.type == Statement.Type_Catchall) { lstVars = ((CatchAllStatement)stat).GetVars(); } else if (stat.type == Statement.Type_Trycatch) { lstVars = ((CatchStatement)stat).GetVars(); } if (lstVars != null) { foreach (VarExprent var in lstVars) { Sharpen.Collections.Put(mapExprentMinTypes, new VarVersionPair(var.GetIndex(), 1) , var.GetVarType()); Sharpen.Collections.Put(mapExprentMaxTypes, new VarVersionPair(var.GetIndex(), 1) , var.GetVarType()); } } Sharpen.Collections.AddAll(stack, stat.GetStats()); } }
public static DataPoint GetInitialDataPoint(StructMethod mt) { DataPoint point = new DataPoint(); MethodDescriptor md = MethodDescriptor.ParseDescriptor(mt.GetDescriptor()); int k = 0; if (!mt.HasModifier(ICodeConstants.Acc_Static)) { point.SetVariable(k++, new VarType(ICodeConstants.Type_Object, 0, null)); } for (int i = 0; i < [email protected]; i++) { VarType var = md.@params[i]; point.SetVariable(k++, var); if (var.stackSize == 2) { point.SetVariable(k++, new VarType(ICodeConstants.Type_Group2empty)); } } return(point); }
private bool IsVarArgCall() { StructClass cl = DecompilerContext.GetStructContext().GetClass(classname); if (cl != null) { StructMethod mt = cl.GetMethod(InterpreterUtil.MakeUniqueKey(name, stringDescriptor )); if (mt != null) { return(mt.HasModifier(ICodeConstants.Acc_Varargs)); } } else { // TODO: tap into IDEA indices to access libraries methods details // try to check the class on the classpath MethodInfo mtd = ClasspathHelper.FindMethod(classname, name, descriptor); return(mtd != null && mtd.GetParameters() .Any(param => Attribute.IsDefined(param, typeof(ParamArrayAttribute)))); } return(false); }
private static void MapClassMethods(ClassesProcessor.ClassNode node, Dictionary < ClassWrapper, MethodWrapper> map) { bool noSynthFlag = DecompilerContext.GetOption(IFernflowerPreferences.Synthetic_Not_Set ); ClassWrapper wrapper = node.GetWrapper(); foreach (MethodWrapper method in wrapper.GetMethods()) { StructMethod mt = method.methodStruct; if ((noSynthFlag || mt.IsSynthetic()) && mt.GetDescriptor().Equals("(Ljava/lang/String;)Ljava/lang/Class;" ) && mt.HasModifier(ICodeConstants.Acc_Static)) { RootStatement root = method.root; if (root != null && root.GetFirst().type == Statement.Type_Trycatch) { CatchStatement cst = (CatchStatement)root.GetFirst(); if (cst.GetStats().Count == 2 && cst.GetFirst().type == Statement.Type_Basicblock && cst.GetStats()[1].type == Statement.Type_Basicblock && cst.GetVars()[0].GetVarType ().Equals(new VarType(ICodeConstants.Type_Object, 0, "java/lang/ClassNotFoundException" ))) { BasicBlockStatement body = (BasicBlockStatement)cst.GetFirst(); BasicBlockStatement handler = (BasicBlockStatement)cst.GetStats()[1]; if (body.GetExprents().Count == 1 && handler.GetExprents().Count == 1) { if (Body_Expr.Equals(body.GetExprents()[0]) && Handler_Expr.Equals(handler.GetExprents ()[0])) { Sharpen.Collections.Put(map, wrapper, method); break; } } } } } } // iterate nested classes foreach (ClassesProcessor.ClassNode nd in node.nested) { MapClassMethods(nd, map); } }
private static void SimpleMerge(VarTypeProcessor typeProcessor, DirectGraph graph , StructMethod mt) { Dictionary <VarVersionPair, VarType> mapExprentMaxTypes = typeProcessor.GetMapExprentMaxTypes (); Dictionary <VarVersionPair, VarType> mapExprentMinTypes = typeProcessor.GetMapExprentMinTypes (); Dictionary <int, HashSet <int> > mapVarVersions = new Dictionary <int, HashSet <int> > (); foreach (VarVersionPair pair in mapExprentMinTypes.Keys) { if (pair.version >= 0) { // don't merge constants mapVarVersions.ComputeIfAbsent(pair.var, (int k) => new HashSet <int>()).Add(pair. version); } } bool is_method_static = mt.HasModifier(ICodeConstants.Acc_Static); Dictionary <VarVersionPair, int> mapMergedVersions = new Dictionary <VarVersionPair , int>(); foreach (KeyValuePair <int, HashSet <int> > ent in mapVarVersions) { if (ent.Value.Count > 1) { List <int> lstVersions = new List <int>(ent.Value); lstVersions.Sort(); for (int i = 0; i < lstVersions.Count; i++) { VarVersionPair firstPair = new VarVersionPair(ent.Key, lstVersions[i]); VarType firstType = mapExprentMinTypes.GetOrNull(firstPair); if (firstPair.var == 0 && firstPair.version == 1 && !is_method_static) { continue; } // don't merge 'this' variable for (int j = i + 1; j < lstVersions.Count; j++) { VarVersionPair secondPair = new VarVersionPair(ent.Key, lstVersions[j]); VarType secondType = mapExprentMinTypes.GetOrNull(secondPair); if (firstType.Equals(secondType) || (firstType.Equals(VarType.Vartype_Null) && secondType .type == ICodeConstants.Type_Object) || (secondType.Equals(VarType.Vartype_Null) && firstType.type == ICodeConstants.Type_Object)) { VarType firstMaxType = mapExprentMaxTypes.GetOrNull(firstPair); VarType secondMaxType = mapExprentMaxTypes.GetOrNull(secondPair); VarType type = firstMaxType == null ? secondMaxType : secondMaxType == null ? firstMaxType : VarType.GetCommonMinType(firstMaxType, secondMaxType); Sharpen.Collections.Put(mapExprentMaxTypes, firstPair, type); Sharpen.Collections.Put(mapMergedVersions, secondPair, firstPair.version); Sharpen.Collections.Remove(mapExprentMaxTypes, secondPair); Sharpen.Collections.Remove(mapExprentMinTypes, secondPair); if (firstType.Equals(VarType.Vartype_Null)) { Sharpen.Collections.Put(mapExprentMinTypes, firstPair, secondType); firstType = secondType; } Sharpen.Collections.Put(typeProcessor.GetMapFinalVars(), firstPair, VarTypeProcessor .Var_Non_Final); lstVersions.RemoveAtReturningValue(j); //noinspection AssignmentToForLoopParameter j--; } } } } } if (!(mapMergedVersions.Count == 0)) { UpdateVersions(graph, mapMergedVersions); } }
private void ComputeMethodType(ClassesProcessor.ClassNode node, MethodWrapper method ) { NestedMemberAccess.MethodAccess type = NestedMemberAccess.MethodAccess.Normal; if (method.root != null) { DirectGraph graph = method.GetOrBuildGraph(); StructMethod mt = method.methodStruct; if ((noSynthFlag || mt.IsSynthetic()) && mt.HasModifier(ICodeConstants.Acc_Static )) { if (graph.nodes.Count == 2) { // incl. dummy exit node if (graph.first.exprents.Count == 1) { Exprent exprent = graph.first.exprents[0]; MethodDescriptor mtdesc = MethodDescriptor.ParseDescriptor(mt.GetDescriptor()); int parcount = [email protected]; Exprent exprCore = exprent; if (exprent.type == Exprent.Exprent_Exit) { ExitExprent exexpr = (ExitExprent)exprent; if (exexpr.GetExitType() == ExitExprent.Exit_Return && exexpr.GetValue() != null) { exprCore = exexpr.GetValue(); } } switch (exprCore.type) { case Exprent.Exprent_Field: { FieldExprent fexpr = (FieldExprent)exprCore; if ((parcount == 1 && !fexpr.IsStatic()) || (parcount == 0 && fexpr.IsStatic())) { if (fexpr.GetClassname().Equals(node.classStruct.qualifiedName)) { // FIXME: check for private flag of the field if (fexpr.IsStatic() || (fexpr.GetInstance().type == Exprent.Exprent_Var && ((VarExprent )fexpr.GetInstance()).GetIndex() == 0)) { type = NestedMemberAccess.MethodAccess.Field_Get; } } } break; } case Exprent.Exprent_Var: { // qualified this if (parcount == 1) { // this or final variable if (((VarExprent)exprCore).GetIndex() != 0) { type = NestedMemberAccess.MethodAccess.Field_Get; } } break; } case Exprent.Exprent_Function: { // for now detect only increment/decrement FunctionExprent functionExprent = (FunctionExprent)exprCore; if (functionExprent.GetFuncType() >= FunctionExprent.Function_Imm && functionExprent .GetFuncType() <= FunctionExprent.Function_Ppi) { if (functionExprent.GetLstOperands()[0].type == Exprent.Exprent_Field) { type = NestedMemberAccess.MethodAccess.Function; } } break; } case Exprent.Exprent_Invocation: { type = NestedMemberAccess.MethodAccess.Method; break; } case Exprent.Exprent_Assignment: { AssignmentExprent asexpr = (AssignmentExprent)exprCore; if (asexpr.GetLeft().type == Exprent.Exprent_Field && asexpr.GetRight().type == Exprent .Exprent_Var) { FieldExprent fexpras = (FieldExprent)asexpr.GetLeft(); if ((parcount == 2 && !fexpras.IsStatic()) || (parcount == 1 && fexpras.IsStatic( ))) { if (fexpras.GetClassname().Equals(node.classStruct.qualifiedName)) { // FIXME: check for private flag of the field if (fexpras.IsStatic() || (fexpras.GetInstance().type == Exprent.Exprent_Var && ( (VarExprent)fexpras.GetInstance()).GetIndex() == 0)) { if (((VarExprent)asexpr.GetRight()).GetIndex() == parcount - 1) { type = NestedMemberAccess.MethodAccess.Field_Set; } } } } } break; } } if (type == NestedMemberAccess.MethodAccess.Method) { // FIXME: check for private flag of the method type = NestedMemberAccess.MethodAccess.Normal; InvocationExprent invexpr = (InvocationExprent)exprCore; bool isStatic = invexpr.IsStatic(); if ((isStatic && invexpr.GetLstParameters().Count == parcount) || (!isStatic && invexpr .GetInstance().type == Exprent.Exprent_Var && ((VarExprent)invexpr.GetInstance() ).GetIndex() == 0 && invexpr.GetLstParameters().Count == parcount - 1)) { bool equalpars = true; int index = isStatic ? 0 : 1; for (int i = 0; i < invexpr.GetLstParameters().Count; i++) { Exprent parexpr = invexpr.GetLstParameters()[i]; if (parexpr.type != Exprent.Exprent_Var || ((VarExprent)parexpr).GetIndex() != index) { equalpars = false; break; } index += mtdesc.@params[i + (isStatic ? 0 : 1)].stackSize; } if (equalpars) { type = NestedMemberAccess.MethodAccess.Method; } } } } else if (graph.first.exprents.Count == 2) { Exprent exprentFirst = graph.first.exprents[0]; Exprent exprentSecond = graph.first.exprents[1]; if (exprentFirst.type == Exprent.Exprent_Assignment && exprentSecond.type == Exprent .Exprent_Exit) { MethodDescriptor mtdesc = MethodDescriptor.ParseDescriptor(mt.GetDescriptor()); int parcount = [email protected]; AssignmentExprent asexpr = (AssignmentExprent)exprentFirst; if (asexpr.GetLeft().type == Exprent.Exprent_Field && asexpr.GetRight().type == Exprent .Exprent_Var) { FieldExprent fexpras = (FieldExprent)asexpr.GetLeft(); if ((parcount == 2 && !fexpras.IsStatic()) || (parcount == 1 && fexpras.IsStatic( ))) { if (fexpras.GetClassname().Equals(node.classStruct.qualifiedName)) { // FIXME: check for private flag of the field if (fexpras.IsStatic() || (fexpras.GetInstance().type == Exprent.Exprent_Var && ( (VarExprent)fexpras.GetInstance()).GetIndex() == 0)) { if (((VarExprent)asexpr.GetRight()).GetIndex() == parcount - 1) { ExitExprent exexpr = (ExitExprent)exprentSecond; if (exexpr.GetExitType() == ExitExprent.Exit_Return && exexpr.GetValue() != null) { if (exexpr.GetValue().type == Exprent.Exprent_Var && ((VarExprent)asexpr.GetRight ()).GetIndex() == parcount - 1) { type = NestedMemberAccess.MethodAccess.Field_Set; } } } } } } } } } } } } if (type != NestedMemberAccess.MethodAccess.Normal) { Sharpen.Collections.Put(mapMethodType, method, type); } else { Sharpen.Collections.Remove(mapMethodType, method); } }
private static bool FindAndRemoveParameterCheck(Statement stat, StructMethod mt) { Statement st = stat.GetFirst(); while (st.type == Statement.Type_Sequence) { st = st.GetFirst(); } if (st.type == Statement.Type_If) { IfStatement ifstat = (IfStatement)st; Statement ifbranch = ifstat.GetIfstat(); Exprent if_condition = ifstat.GetHeadexprent().GetCondition(); bool is_notnull_check = false; // TODO: FUNCTION_NE also possible if reversed order (in theory) if (ifbranch != null && if_condition.type == Exprent.Exprent_Function && ((FunctionExprent )if_condition).GetFuncType() == FunctionExprent.Function_Eq && ifbranch.type == Statement.Type_Basicblock && ifbranch.GetExprents().Count == 1 && ifbranch.GetExprents ()[0].type == Exprent.Exprent_Exit) { FunctionExprent func = (FunctionExprent)if_condition; Exprent first_param = func.GetLstOperands()[0]; Exprent second_param = func.GetLstOperands()[1]; if (second_param.type == Exprent.Exprent_Const && second_param.GetExprType().type == ICodeConstants.Type_Null) { // TODO: reversed parameter order if (first_param.type == Exprent.Exprent_Var) { VarExprent var = (VarExprent)first_param; bool thisvar = !mt.HasModifier(ICodeConstants.Acc_Static); MethodDescriptor md = MethodDescriptor.ParseDescriptor(mt.GetDescriptor()); // parameter annotations StructAnnotationParameterAttribute param_annotations = mt.GetAttribute(StructGeneralAttribute .Attribute_Runtime_Invisible_Parameter_Annotations); if (param_annotations != null) { List <List <AnnotationExprent> > param_annotations_lists = param_annotations.GetParamAnnotations (); int method_param_number = [email protected]; int index = thisvar ? 1 : 0; for (int i = 0; i < method_param_number; i++) { if (index == var.GetIndex()) { if (param_annotations_lists.Count >= method_param_number - i) { int shift = method_param_number - param_annotations_lists.Count; // NOTE: workaround for compiler bug, count annotations starting with the last parameter List <AnnotationExprent> annotations = param_annotations_lists[i - shift]; foreach (AnnotationExprent ann in annotations) { if (ann.GetClassName().Equals("org/jetbrains/annotations/NotNull")) { is_notnull_check = true; break; } } } break; } index += md.@params[i].stackSize; } } } } } if (!is_notnull_check) { return(false); } RemoveParameterCheck(stat); return(true); } return(false); }
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); }
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 ())); } } }