Search in sources :

Example 1 with FieldNode

use of org.mapleir.asm.FieldNode in project maple-ir by LLVM-but-worse.

the class FieldRSADecryptionPass method lookupField0.

private String lookupField0(AnalysisContext cxt, String oldKey, String owner, String name, String desc, boolean isStatic) {
    ClassNode cn = cxt.getApplication().findClassNode(owner);
    if (cn == null) {
        String newKey = key(owner, name, desc);
        fieldLookupCache.put(oldKey, newKey);
        return newKey;
    }
    for (FieldNode fn : cn.getFields()) {
        if (fn.getName().equals(name) && fn.getDesc().equals(desc) && (Modifier.isStatic(fn.node.access) == isStatic)) {
            String newKey = key(cn.getName(), fn.getName(), fn.getDesc());
            fieldLookupCache.put(oldKey, newKey);
            return newKey;
        }
    }
    return lookupField0(cxt, oldKey, cn.node.superName, name, desc, isStatic);
}
Also used : ClassNode(org.mapleir.asm.ClassNode) FieldNode(org.mapleir.asm.FieldNode)

Example 2 with FieldNode

use of org.mapleir.asm.FieldNode in project maple-ir by LLVM-but-worse.

the class FieldRenamerPass method accept.

@Override
public PassResult accept(PassContext pcxt) {
    AnalysisContext cxt = pcxt.getAnalysis();
    Map<FieldNode, String> remapped = new HashMap<>();
    // int totalFields = 0;
    // int i = RenamingUtil.computeMinimum(totalFields);
    ApplicationClassSource source = cxt.getApplication();
    int i = RenamingUtil.numeric("aaaaa");
    for (ClassNode cn : source.iterate()) {
        // totalFields += cn.fields.size();
        for (FieldNode fn : cn.getFields()) {
            remapped.put(fn, RenamingUtil.createName(i++));
        }
    }
    InvocationResolver resolver = cxt.getInvocationResolver();
    for (ClassNode cn : source.iterate()) {
        for (MethodNode m : cn.getMethods()) {
            ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
            for (BasicBlock b : cfg.vertices()) {
                for (Stmt stmt : b) {
                    if (stmt.getOpcode() == Opcode.FIELD_STORE) {
                        FieldStoreStmt fs = (FieldStoreStmt) stmt;
                        FieldNode f = resolver.findField(fs.getOwner(), fs.getName(), fs.getDesc(), fs.getInstanceExpression() == null);
                        if (f != null) {
                            if (remapped.containsKey(f)) {
                                fs.setName(remapped.get(f));
                            } else if (mustMark(source, f.getOwner())) {
                                System.err.println("  no remap for " + f + ", owner: " + f.getOwner());
                            }
                        } else {
                            if (mustMark(source, fs.getOwner())) {
                                System.err.println("  can't resolve field(set): " + fs.getOwner() + "." + fs.getName() + " " + fs.getDesc() + ", " + (fs.getInstanceExpression() == null));
                            }
                        }
                    }
                    for (Expr e : stmt.enumerateOnlyChildren()) {
                        if (e.getOpcode() == Opcode.FIELD_LOAD) {
                            FieldLoadExpr fl = (FieldLoadExpr) e;
                            FieldNode f = resolver.findField(fl.getOwner(), fl.getName(), fl.getDesc(), fl.getInstanceExpression() == null);
                            if (f != null) {
                                if (remapped.containsKey(f)) {
                                    fl.setName(remapped.get(f));
                                } else if (mustMark(source, f.getOwner())) {
                                    System.err.println("  no remap for " + f + ", owner: " + f.getOwner());
                                }
                            } else {
                                if (mustMark(source, fl.getOwner())) {
                                    System.err.println("  can't resolve field(get): " + fl.getOwner() + "." + fl.getName() + " " + fl.getDesc() + ", " + (fl.getInstanceExpression() == null));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    for (Entry<FieldNode, String> e : remapped.entrySet()) {
        e.getKey().node.name = e.getValue();
    }
    System.out.printf("  Renamed %d fields.%n", remapped.size());
    return PassResult.with(pcxt, this).finished().make();
}
Also used : FieldStoreStmt(org.mapleir.ir.code.stmt.FieldStoreStmt) ClassNode(org.mapleir.asm.ClassNode) FieldLoadExpr(org.mapleir.ir.code.expr.FieldLoadExpr) FieldNode(org.mapleir.asm.FieldNode) HashMap(java.util.HashMap) BasicBlock(org.mapleir.ir.cfg.BasicBlock) AnalysisContext(org.mapleir.context.AnalysisContext) FieldStoreStmt(org.mapleir.ir.code.stmt.FieldStoreStmt) Stmt(org.mapleir.ir.code.Stmt) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) MethodNode(org.mapleir.asm.MethodNode) Expr(org.mapleir.ir.code.Expr) FieldLoadExpr(org.mapleir.ir.code.expr.FieldLoadExpr) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) InvocationResolver(org.mapleir.app.service.InvocationResolver)

Example 3 with FieldNode

use of org.mapleir.asm.FieldNode in project maple-ir by LLVM-but-worse.

the class DefaultInvocationResolver method findStaticField.

// FIXME: these are taken directly from the old resolver
@Override
public FieldNode findStaticField(String owner, String name, String desc) {
    Set<FieldNode> set = new HashSet<>();
    ClassNode cn = app.findClassNode(owner);
    /* we do this because static fields can be in
		 * interfaces. */
    if (cn != null) {
        Set<ClassNode> lvl = new HashSet<>();
        lvl.add(cn);
        for (; ; ) {
            if (lvl.size() == 0) {
                break;
            }
            Set<FieldNode> lvlSites = new HashSet<>();
            for (ClassNode c : lvl) {
                for (FieldNode f : c.getFields()) {
                    if (Modifier.isStatic(f.node.access) && f.getName().equals(name) && f.getDesc().equals(desc)) {
                        lvlSites.add(f);
                    }
                }
            }
            if (lvlSites.size() > 1) {
                LOGGER.info(String.format("(warn) resolved %s.%s %s to %s", owner, name, desc, lvlSites));
            }
            if (lvlSites.size() > 0) {
                set.addAll(lvlSites);
                break;
            }
            Set<ClassNode> newLvl = new HashSet<>();
            for (ClassNode c : lvl) {
                ClassNode sup = app.findClassNode(c.node.superName);
                if (sup != null) {
                    newLvl.add(sup);
                }
                for (String iface : c.node.interfaces) {
                    ClassNode ifaceN = app.findClassNode(iface);
                    if (ifaceN != null) {
                        newLvl.add(ifaceN);
                    }
                }
            }
            lvl.clear();
            lvl = newLvl;
        }
    }
    if (set.size() > 1) {
        throw new UnsupportedOperationException(String.format("multi dispatch?: %s.%s %s results:%s", owner, name, desc, set));
    } else if (set.size() == 1) {
        return set.iterator().next();
    } else {
        return null;
    }
}
Also used : ClassNode(org.mapleir.asm.ClassNode) FieldNode(org.mapleir.asm.FieldNode)

Example 4 with FieldNode

use of org.mapleir.asm.FieldNode in project maple-ir by LLVM-but-worse.

the class ClassPrinter method print.

public static String print(ClassNode cn) {
    StringBuilder sb = new StringBuilder();
    printModifiers(sb, cn.node.access);
    sb.append(cn.getName()).append(" extends ").append(cn.node.superName).append(" ");
    if (cn.node.interfaces.size() > 0) {
        sb.append("implements ");
        Iterator<String> it = cn.node.interfaces.iterator();
        while (it.hasNext()) {
            String intf = it.next();
            sb.append(intf);
            if (it.hasNext())
                sb.append(",");
            sb.append(" ");
        }
    }
    sb.append("{").append("\n");
    sb.append("\n");
    for (FieldNode f : cn.getFields()) {
        sb.append("    ");
        printModifiers(sb, f.node.access);
        sb.append(f.getName()).append(" ").append(f.getDesc()).append("\n");
    }
    sb.append("\n");
    for (MethodNode m : cn.getMethods()) {
        sb.append("    ");
        printModifiers(sb, m.node.access);
        sb.append(m.getName()).append(" ").append(m.getDesc()).append(" {").append("\n");
        for (String s : InstructionPrinter.getLines(m.node)) {
            sb.append("       ").append(s).append("\n");
        }
        sb.append("    }").append("\n\n");
    }
    sb.append("}");
    return sb.toString();
}
Also used : FieldNode(org.mapleir.asm.FieldNode) MethodNode(org.mapleir.asm.MethodNode)

Example 5 with FieldNode

use of org.mapleir.asm.FieldNode in project maple-ir by LLVM-but-worse.

the class ClassRenamerPass method accept.

/*private String getClassName(String name) {
		int i = name.lastIndexOf('/');
		if(i == -1) {
			return name;
		} else {
			return name.substring(i + 1, name.length());
		}
	}*/
@Override
public PassResult accept(PassContext pcxt) {
    AnalysisContext cxt = pcxt.getAnalysis();
    ApplicationClassSource source = cxt.getApplication();
    Collection<ClassNode> classes = CollectionUtils.collate(source.iterator());
    // int min = RenamingUtil.computeMinimum(classes.size());
    int n = RenamingUtil.numeric("aaa");
    int step = 27;
    for (ClassNode cn : classes) {
        String className = RenamingUtil.getClassName(cn.getName());
        if (!heuristic.shouldRename(className, cn.node.access)) {
            System.out.println("Heuristic bypass " + cn.getName());
        }
        String newName = heuristic.shouldRename(className, cn.node.access) ? RenamingUtil.createName(n) : className;
        String s = RenamingUtil.getPackage(cn.getName()) + newName;
        n += step;
        remapping.put(cn.getName(), s);
        // System.out.println(cn.getName() + " -> " + s);
        cn.node.name = s;
    }
    for (ClassNode cn : classes) {
        cn.node.superName = remapping.getOrDefault(cn.node.superName, cn.node.superName);
        {
            List<String> ifaces = new ArrayList<>();
            for (int i = 0; i < cn.node.interfaces.size(); i++) {
                String s = cn.node.interfaces.get(i);
                ifaces.add(remapping.getOrDefault(s, s));
            }
            cn.node.interfaces = ifaces;
        }
        unsupported(cn.node.signature);
        // unsupported(cn.sourceFile);
        // unsupported(cn.sourceDebug);
        cn.node.outerClass = remapping.getOrDefault(cn.node.outerClass, cn.node.outerClass);
        // unsupported(cn.outerMethod);
        // unsupported(cn.outerMethodDesc);
        unsupported(cn.node.visibleAnnotations);
        unsupported(cn.node.invisibleAnnotations);
        unsupported(cn.node.visibleTypeAnnotations);
        unsupported(cn.node.invisibleTypeAnnotations);
        unsupported(cn.node.attrs);
        unsupported(cn.node.innerClasses);
        for (FieldNode f : cn.getFields()) {
            unsupported(cn.node.signature);
            {
                Type type = Type.getType(f.node.desc);
                String newType = resolveType(type, remapping);
                if (newType != null) {
                    f.node.desc = newType;
                }
            }
            unsupported(f.node.visibleAnnotations);
            unsupported(f.node.invisibleAnnotations);
            unsupported(f.node.visibleTypeAnnotations);
            unsupported(f.node.invisibleTypeAnnotations);
            unsupported(f.node.attrs);
        }
        for (MethodNode m : cn.getMethods()) {
            m.node.desc = resolveMethod(m.node.desc, remapping);
            unsupported(m.node.signature);
            {
                List<String> exceptions = new ArrayList<>();
                for (int i = 0; i < m.node.exceptions.size(); i++) {
                    String s = m.node.exceptions.get(i);
                    exceptions.add(remapping.getOrDefault(s, s));
                }
                m.node.exceptions = exceptions;
            }
            unsupported(m.node.parameters);
            unsupported(m.node.visibleAnnotations);
            unsupported(m.node.invisibleAnnotations);
            unsupported(m.node.visibleTypeAnnotations);
            unsupported(m.node.invisibleTypeAnnotations);
            unsupported(m.node.attrs);
            unsupported(m.node.annotationDefault);
            unsupported(m.node.visibleParameterAnnotations);
            unsupported(m.node.invisibleParameterAnnotations);
            for (TryCatchBlockNode tcbn : m.node.tryCatchBlocks) {
                tcbn.type = remapping.getOrDefault(tcbn.type, tcbn.type);
            }
            ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
            for (ExceptionRange<BasicBlock> er : cfg.getRanges()) {
                Set<Type> newTypeSet = new HashSet<>();
                for (Type t : er.getTypes()) {
                    // FIXME:
                    String s = t.getInternalName();
                    if (remapping.containsKey(s)) {
                        newTypeSet.add(Type.getType("L" + remapping.get(s) + ";"));
                    } else {
                        newTypeSet.add(t);
                    }
                }
                er.setTypes(newTypeSet);
            }
            if (m.node.localVariables != null) {
                m.node.localVariables.clear();
                for (LocalVariableNode lvn : m.node.localVariables) {
                    String newDesc = resolveType(Type.getType(lvn.desc), remapping);
                    if (newDesc != null) {
                        lvn.desc = newDesc;
                    }
                    unsupported(lvn.signature);
                }
            }
            unsupported(m.node.visibleLocalVariableAnnotations);
            unsupported(m.node.invisibleLocalVariableAnnotations);
            for (BasicBlock b : cfg.vertices()) {
                for (Stmt stmt : b) {
                    if (stmt.getOpcode() == Opcode.FIELD_STORE) {
                        FieldStoreStmt fs = (FieldStoreStmt) stmt;
                        String owner = fs.getOwner();
                        fs.setOwner(remapping.getOrDefault(owner, owner));
                        {
                            Type type = Type.getType(fs.getDesc());
                            String newType = resolveType(type, remapping);
                            if (newType != null) {
                                fs.setDesc(newType);
                            }
                        }
                    } else if (stmt.getOpcode() == Opcode.RETURN) {
                        ReturnStmt ret = (ReturnStmt) stmt;
                        String newType = resolveType(ret.getType(), remapping);
                        if (newType != null) {
                            ret.setType(Type.getType(newType));
                        }
                    } else if (stmt instanceof AbstractCopyStmt) {
                        AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
                        VarExpr v = copy.getVariable();
                        String newType = resolveType(v.getType(), remapping);
                        if (newType != null) {
                            v.setType(Type.getType(newType));
                        }
                    }
                    for (Expr e : stmt.enumerateOnlyChildren()) {
                        if (e.getOpcode() == Opcode.CAST) {
                            CastExpr cast = (CastExpr) e;
                            String newType = resolveType(cast.getType(), remapping);
                            if (newType != null) {
                                cast.setType(Type.getType(newType));
                            }
                        } else if (e.getOpcode() == Opcode.CATCH) {
                            CaughtExceptionExpr caught = (CaughtExceptionExpr) e;
                            String newType = resolveType(caught.getType(), remapping);
                            if (newType != null) {
                                caught.setType(Type.getType(newType));
                            }
                        } else if (e.getOpcode() == Opcode.INVOKE) {
                            InvocationExpr invoke = (InvocationExpr) e;
                            if (invoke.isDynamic())
                                throw new UnsupportedOperationException();
                            invoke.setOwner(remapping.getOrDefault(invoke.getOwner(), invoke.getOwner()));
                            invoke.setDesc(resolveMethod(invoke.getDesc(), remapping));
                        } else if (e.getOpcode() == Opcode.FIELD_LOAD) {
                            FieldLoadExpr fl = (FieldLoadExpr) e;
                            fl.setOwner(remapping.getOrDefault(fl.getOwner(), fl.getOwner()));
                            String newType = resolveType(fl.getType(), remapping);
                            if (newType != null) {
                                fl.setDesc(newType);
                            }
                        } else if (e.getOpcode() == Opcode.INIT_OBJ) {
                            InitialisedObjectExpr init = (InitialisedObjectExpr) e;
                            init.setOwner(remapping.getOrDefault(init.getOwner(), init.getOwner()));
                            init.setDesc(resolveMethod(init.getDesc(), remapping));
                        } else if (e.getOpcode() == Opcode.INSTANCEOF) {
                            InstanceofExpr inst = (InstanceofExpr) e;
                            String newType = resolveType(inst.getCheckType(), remapping);
                            if (newType != null) {
                                inst.setCheckType(Type.getType(newType));
                            }
                        } else if (e.getOpcode() == Opcode.NEW_ARRAY) {
                            NewArrayExpr na = (NewArrayExpr) e;
                            String newType = resolveType(na.getType(), remapping);
                            if (newType != null) {
                                na.setType(Type.getType(newType));
                            }
                        } else if (e.getOpcode() == Opcode.ALLOC_OBJ) {
                            AllocObjectExpr uninit = (AllocObjectExpr) e;
                            String newType = resolveType(uninit.getType(), remapping);
                            if (newType != null) {
                                uninit.setType(Type.getType(newType));
                            }
                        } else if (e.getOpcode() == Opcode.LOCAL_LOAD) {
                            VarExpr v = (VarExpr) e;
                            String newType = resolveType(v.getType(), remapping);
                            if (newType != null) {
                                v.setType(Type.getType(newType));
                            }
                        } else if (e.getOpcode() == Opcode.CONST_LOAD) {
                            ConstantExpr c = (ConstantExpr) e;
                            Object cst = c.getConstant();
                            if (cst instanceof Type) {
                                Type t = (Type) cst;
                                if (t.getSort() == Type.OBJECT) {
                                    String newType = resolveType(t, remapping);
                                    if (newType != null) {
                                        c.setConstant(Type.getType(newType));
                                    }
                                } else {
                                    throw new UnsupportedOperationException(String.format("Unsupported ctype %s (%d)", t, t.getSort()));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    source.rebuildTable();
    return PassResult.with(pcxt, this).finished().make();
}
Also used : AnalysisContext(org.mapleir.context.AnalysisContext) FieldStoreStmt(org.mapleir.ir.code.stmt.FieldStoreStmt) Stmt(org.mapleir.ir.code.Stmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) ReturnStmt(org.mapleir.ir.code.stmt.ReturnStmt) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) MethodNode(org.mapleir.asm.MethodNode) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) FieldStoreStmt(org.mapleir.ir.code.stmt.FieldStoreStmt) ClassNode(org.mapleir.asm.ClassNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) FieldNode(org.mapleir.asm.FieldNode) BasicBlock(org.mapleir.ir.cfg.BasicBlock) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode) Type(org.objectweb.asm.Type) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Expr(org.mapleir.ir.code.Expr) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) ReturnStmt(org.mapleir.ir.code.stmt.ReturnStmt) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr)

Aggregations

FieldNode (org.mapleir.asm.FieldNode)5 ClassNode (org.mapleir.asm.ClassNode)4 MethodNode (org.mapleir.asm.MethodNode)3 ApplicationClassSource (org.mapleir.app.service.ApplicationClassSource)2 AnalysisContext (org.mapleir.context.AnalysisContext)2 BasicBlock (org.mapleir.ir.cfg.BasicBlock)2 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)2 Expr (org.mapleir.ir.code.Expr)2 Stmt (org.mapleir.ir.code.Stmt)2 FieldStoreStmt (org.mapleir.ir.code.stmt.FieldStoreStmt)2 HashMap (java.util.HashMap)1 InvocationResolver (org.mapleir.app.service.InvocationResolver)1 FieldLoadExpr (org.mapleir.ir.code.expr.FieldLoadExpr)1 InitialisedObjectExpr (org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr)1 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)1 ReturnStmt (org.mapleir.ir.code.stmt.ReturnStmt)1 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)1 Type (org.objectweb.asm.Type)1 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)1 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)1