Search in sources :

Example 31 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class LiftConstructorCallsPass method tryLift.

private boolean tryLift(MethodNode m, ControlFlowGraph cfg) {
    Local lvar0_0 = cfg.getLocals().get(0, 0, false);
    /* only contains synthetic copies */
    BasicBlock entry = cfg.getEntries().iterator().next();
    for (BasicBlock b : cfg.vertices()) {
        for (Stmt stmt : b) {
            for (Expr e : stmt.enumerateOnlyChildren()) {
                if (e.getOpcode() == INVOKE) {
                    InvocationExpr invoke = (InvocationExpr) e;
                    if (invoke.getOwner().equals(m.owner.superName) && invoke.getName().equals("<init>")) {
                        Expr p1 = invoke.getPhysicalReceiver();
                        if (p1.getOpcode() == LOCAL_LOAD && ((VarExpr) p1).getLocal() == lvar0_0) {
                            Set<FlowEdge<BasicBlock>> predsEdges = cfg.getReverseEdges(b);
                            FlowEdge<BasicBlock> incoming;
                            if (predsEdges.size() == 1 && ((incoming = predsEdges.iterator().next()).getType() == FlowEdges.IMMEDIATE) && incoming.src() == entry) {
                                // BasicBlock liftBlock = new BasicBlock(cfg, cfg.vertices().size() + 1, new LabelNode());
                                /* split the block before the invocation and 
									 * insert a new block. */
                                split(cfg, b, stmt);
                                return true;
                            } else {
                                System.err.printf(" warn(nolift) for %s in %n%s%n", invoke, ControlFlowGraph.printBlock(b));
                                System.err.printf("  preds: %s%n", predsEdges);
                            }
                        } else {
                            throw new IllegalStateException(String.format("broken super call: %s", invoke));
                        }
                    }
                }
            }
        }
    }
    return false;
}
Also used : FlowEdge(org.mapleir.flowgraph.edges.FlowEdge) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) VarExpr(org.mapleir.ir.code.expr.VarExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Stmt(org.mapleir.ir.code.Stmt)

Example 32 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class IPAnalysis method visitMethod.

@Override
protected void visitMethod(MethodNode m) {
    // Callbacks
    for (IPAnalysisVisitor v : visitors) {
        v.preVisitMethod(this, m);
    }
    // Do not trace library calls
    if (context.getApplication().isLibraryClass(m.owner.name)) {
        return;
    }
    // Create a mapping between the actual variable table indices and the parameter
    // indices in the method descriptor.
    boolean isStatic = (m.access & Opcodes.ACC_STATIC) != 0;
    int paramCount = Type.getArgumentTypes(m.desc).length;
    int off = (isStatic ? 0 : 1);
    int synthCount = paramCount + off;
    List<List<Expr>> lists = new ArrayList<>(synthCount);
    int[] idxs = new int[synthCount];
    // Scan for synthetic copies to populate indices
    ControlFlowGraph cfg = context.getIRCache().getFor(m);
    BasicBlock entry = cfg.getEntries().iterator().next();
    /* static:
		 *  first arg = 0
		 *
		 * non-static:
		 *  this = 0
		 *  first arg = 1*/
    int paramIndex = 0;
    for (Stmt stmt : entry) {
        if (stmt.getOpcode() == LOCAL_STORE) {
            CopyVarStmt cvs = (CopyVarStmt) stmt;
            if (cvs.isSynthetic()) {
                int varIndex = cvs.getVariable().getLocal().getIndex();
                if (!isStatic && varIndex == 0)
                    continue;
                idxs[paramIndex++] = varIndex;
                continue;
            }
        }
        break;
    }
    for (int j = 0; j < paramCount; j++) {
        lists.add(new ArrayList<>());
    }
    paramIndices.put(m, idxs);
    parameterInputs.put(m, lists);
    callers.put(m, new HashSet<>());
    // Callbacks
    for (IPAnalysisVisitor v : visitors) {
        v.postVisitMethod(this, m);
    }
}
Also used : CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) ArrayList(java.util.ArrayList) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Stmt(org.mapleir.ir.code.Stmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) ArrayList(java.util.ArrayList) List(java.util.List)

Example 33 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class MethodRenamerPass method rename.

public static void rename(AnalysisContext cxt, Map<MethodNode, String> remapped, boolean warn) {
    ApplicationClassSource source = cxt.getApplication();
    InvocationResolver resolver = cxt.getInvocationResolver();
    for (ClassNode cn : source.iterate()) {
        {
            if (cn.outerMethod != null) {
                // ClassNode owner = tree.getClass(cn.outerClass);
                System.out.println("Outer: " + cn.outerClass + "." + cn.outerMethod + " " + cn.outerMethodDesc);
                cn.outerClass = null;
                cn.outerMethod = null;
                cn.outerMethodDesc = null;
            // System.out.println(owner.name);
            // do {
            // for(MethodNode m : owner.methods) {
            // System.out.println(m);
            // if(m.name.equals(cn.outerMethod) && m.desc.equals(cn.outerMethodDesc)) {
            // System.out.println("m: " + m);
            // }
            // }
            // owner = tree.getClass(owner.superName);
            // System.out.println(cn.superName);
            // System.out.println(owner);
            // } while(owner != null);
            }
        }
        Set<Expr> visited = new HashSet<>();
        for (MethodNode m : cn.methods) {
            ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
            for (BasicBlock b : cfg.vertices()) {
                for (Stmt stmt : b) {
                    for (Expr e : stmt.enumerateOnlyChildren()) {
                        if (e.getOpcode() == Opcode.INVOKE) {
                            InvocationExpr invoke = (InvocationExpr) e;
                            if (visited.contains(invoke)) {
                                throw new RuntimeException(invoke.toString());
                            }
                            visited.add(invoke);
                            if (invoke.getOwner().startsWith("[")) {
                                System.err.println("  ignore array object invoke: " + invoke + ", owner: " + invoke.getOwner());
                                continue;
                            }
                            if (invoke.isStatic()) {
                                MethodNode site = resolver.resolveStaticCall(invoke.getOwner(), invoke.getName(), invoke.getDesc());
                                if (site != null) {
                                    if (remapped.containsKey(site)) {
                                        invoke.setName(remapped.get(site));
                                    } else {
                                        if (warn && mustMark(source, invoke.getOwner())) {
                                            System.err.println("  invalid site(s): " + invoke);
                                        }
                                    }
                                } else {
                                    if (mustMark(source, invoke.getOwner())) {
                                        System.err.printf("  can't resolve(s) %s ; %s.%s %s%n", invoke, invoke.getOwner(), invoke.getName(), invoke.getDesc());
                                        if (invoke.getOwner().equals("hey")) {
                                            for (MethodNode mm : cxt.getApplication().findClassNode(invoke.getOwner()).methods) {
                                                System.out.println(mm);
                                            }
                                            throw new UnsupportedOperationException();
                                        }
                                    }
                                }
                            } else {
                                // Set<MethodNode> sites = resolver.resolveVirtualCalls(invoke.getOwner(), invoke.getName(), invoke.getDesc());
                                // Set<ClassNode> classes = source.getStructures().dfsTree(cn, true, true, true);
                                // Set<MethodNode> sites = getVirtualMethods(cxt, classes, invoke.getName(), invoke.getDesc());
                                Set<MethodNode> sites = resolver.getHierarchyMethodChain(source.findClassNode(invoke.getOwner()), invoke.getName(), invoke.getDesc(), true);
                                if (sites.size() > 0) {
                                    /* all of the sites must be linked by the same name,
										 * so we can use any to find the new name. */
                                    boolean anyContains = false;
                                    boolean allContains = true;
                                    for (MethodNode s : sites) {
                                        anyContains |= remapped.containsKey(s);
                                        allContains &= remapped.containsKey(s);
                                    }
                                    if (anyContains && !allContains) {
                                        System.err.println("mismatch: ");
                                        // System.err.println(classes);
                                        System.err.println(sites);
                                        throw new RuntimeException();
                                    }
                                    MethodNode site = sites.iterator().next();
                                    if (remapped.containsKey(site)) {
                                        invoke.setName(remapped.get(site));
                                    } else {
                                        if (warn && !site.name.equals("<init>") && canRename(cxt, sites)) {
                                            System.err.println("  invalid site(v): " + invoke + ", " + sites);
                                        }
                                    }
                                } else {
                                    if (mustMark(source, invoke.getOwner())) {
                                        System.err.println("  can't resolve(v) " + invoke + ", owner: " + invoke.getOwner() + " desc " + invoke.getDesc());
                                    // System.err.println("  classes: " + classes);
                                    }
                                }
                            }
                        } else if (e.getOpcode() == Opcode.DYNAMIC_INVOKE) {
                            throw new UnsupportedOperationException();
                        }
                    }
                }
            }
        }
    }
    /* Rename the methods after as we need to resolve
		 * them using the old names during the invocation 
		 * analysis above. */
    for (Entry<MethodNode, String> e : remapped.entrySet()) {
        // System.out.printf("%s -> %s%n", e.getKey(), e.getValue());
        e.getKey().name = e.getValue();
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Stmt(org.mapleir.ir.code.Stmt) ApplicationClassSource(org.mapleir.app.service.ApplicationClassSource) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Expr(org.mapleir.ir.code.Expr) MethodNode(org.objectweb.asm.tree.MethodNode) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) InvocationResolver(org.mapleir.app.service.InvocationResolver) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) HashSet(java.util.HashSet)

Example 34 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class SSAGenPass method updatePhiArgTypes.

private void updatePhiArgTypes(Set<BasicBlock> vis) {
    // update types for phi args
    for (BasicBlock b : order) {
        for (Stmt s : b) {
            if (s.getOpcode() != Opcode.PHI_STORE) {
                break;
            }
            CopyPhiStmt cps = (CopyPhiStmt) s;
            for (Entry<BasicBlock, Expr> e : cps.getExpression().getArguments().entrySet()) {
                BasicBlock src = e.getKey();
                if (vis.contains(src))
                    continue;
                VarExpr v = (VarExpr) e.getValue();
                Local l = v.getLocal();
                // what if the def is never reached?
                AbstractCopyStmt def = pool.defs.get(l);
                v.setType(def.getType());
            }
        }
    }
}
Also used : ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) BasicBlock(org.mapleir.ir.cfg.BasicBlock) VarExpr(org.mapleir.ir.code.expr.VarExpr) BasicLocal(org.mapleir.ir.locals.impl.BasicLocal) Local(org.mapleir.ir.locals.Local) VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) SwitchStmt(org.mapleir.ir.code.stmt.SwitchStmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) PopStmt(org.mapleir.ir.code.stmt.PopStmt) ThrowStmt(org.mapleir.ir.code.stmt.ThrowStmt) UnconditionalJumpStmt(org.mapleir.ir.code.stmt.UnconditionalJumpStmt) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) ConditionalJumpStmt(org.mapleir.ir.code.stmt.ConditionalJumpStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 35 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class SSAGenPass method insertPhis.

private void insertPhis(BasicBlock b, Local l, int i, LinkedList<BasicBlock> queue) {
    if (b == null || b == builder.head) {
        // exit
        return;
    }
    Local newl = builder.graph.getLocals().get(l.getIndex(), 0, l.isStack());
    for (BasicBlock x : doms.iteratedFrontier(b)) {
        if (insertion.get(x) < i) {
            // pruned SSA
            if (liveness.in(x).contains(l)) {
                if ((l == svar0) && handlers.contains(x)) /* == faster than contains. */
                {
                    /* Note: this is quite subtle. Since there is a
						 * copy, (svar0 = catch()) at the start of each
						 * handler block, technically any natural flowing
						 * svar0 definition is killed upon entry to the
						 * block, so it is not considered live. One way to
						 * check if the variable is live-in, therefore, is
						 * by checking whether svar0 is live-out of the
						 * catch() definition. We handle it here, since
						 * the previous liveness check which is used for
						 * pruned SSA will fail in this case. */
                    /* Ok fuck that that, it's considered live-in
						 * even if there is a catch()::
						 *  #see SSaBlockLivenessAnalyser.precomputeBlock*/
                    boolean naturalFlow = false;
                    for (FlowEdge<BasicBlock> e : builder.graph.getReverseEdges(x)) {
                        if (e.getType() != FlowEdges.TRYCATCH) {
                            naturalFlow = true;
                            break;
                        }
                    }
                    if (naturalFlow) {
                        CopyVarStmt catcher = null;
                        for (Stmt stmt : x) {
                            if (stmt.getOpcode() == Opcode.LOCAL_STORE) {
                                CopyVarStmt copy = (CopyVarStmt) stmt;
                                Expr e = copy.getExpression();
                                if (e.getOpcode() == Opcode.CATCH) {
                                    catcher = copy;
                                    break;
                                }
                            }
                        }
                        if (catcher == null) {
                            /* Handler but no catch copy?
								 * This can't happen since svar0 is
								 * the only reserved register for
								 * catch copies, and this block cannot
								 * be visited twice to insert a phi or
								 * psi(ephi) node. */
                            throw new IllegalStateException(x.getDisplayName());
                        }
                        /* Map<BasicBlock, Expression> vls = new HashMap<>();
							for(FlowEdge<BasicBlock> fe : builder.graph.getReverseEdges(x)) {
								vls.put(fe.src, new VarExpr(newl, null));
							}
							vls.put(x, catcher.getExpression().copy());
							catcher.delete();
							
							PhiExpr phi = new PhiExceptionExpr(vls);
							CopyPhiStmt assign = new CopyPhiStmt(new VarExpr(l, null), phi);
							
							x.add(0, assign); */
                        throw new UnsupportedOperationException(builder.method.toString());
                    }
                }
                if (builder.graph.getReverseEdges(x).size() > 1) {
                    Map<BasicBlock, Expr> vls = new HashMap<>();
                    for (FlowEdge<BasicBlock> fe : builder.graph.getReverseEdges(x)) {
                        vls.put(fe.src(), new VarExpr(newl, null));
                    }
                    PhiExpr phi = new PhiExpr(vls);
                    CopyPhiStmt assign = new CopyPhiStmt(new VarExpr(l, null), phi);
                    x.add(0, assign);
                }
            }
            insertion.put(x, i);
            if (process.get(x) < i) {
                process.put(x, i);
                queue.add(x);
            }
        }
    }
}
Also used : NullPermeableHashMap(org.mapleir.stdlib.collections.map.NullPermeableHashMap) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) BasicLocal(org.mapleir.ir.locals.impl.BasicLocal) Local(org.mapleir.ir.locals.Local) VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) SwitchStmt(org.mapleir.ir.code.stmt.SwitchStmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) PopStmt(org.mapleir.ir.code.stmt.PopStmt) ThrowStmt(org.mapleir.ir.code.stmt.ThrowStmt) UnconditionalJumpStmt(org.mapleir.ir.code.stmt.UnconditionalJumpStmt) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) ConditionalJumpStmt(org.mapleir.ir.code.stmt.ConditionalJumpStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Aggregations

Stmt (org.mapleir.ir.code.Stmt)53 BasicBlock (org.mapleir.ir.cfg.BasicBlock)35 Expr (org.mapleir.ir.code.Expr)29 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)28 VarExpr (org.mapleir.ir.code.expr.VarExpr)26 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)25 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)24 Local (org.mapleir.ir.locals.Local)21 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)19 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)15 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)13 UnconditionalJumpStmt (org.mapleir.ir.code.stmt.UnconditionalJumpStmt)13 ThrowStmt (org.mapleir.ir.code.stmt.ThrowStmt)12 PopStmt (org.mapleir.ir.code.stmt.PopStmt)11 SwitchStmt (org.mapleir.ir.code.stmt.SwitchStmt)11 HashSet (java.util.HashSet)10 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)9 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)8 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)8 BasicLocal (org.mapleir.ir.locals.impl.BasicLocal)7