Search in sources :

Example 1 with InvocationExpr

use of org.mapleir.ir.code.expr.invoke.InvocationExpr in project maple-ir by LLVM-but-worse.

the class SSAGenPass method aggregateInitialisers.

private void aggregateInitialisers() {
    for (BasicBlock b : builder.graph.vertices()) {
        for (Stmt stmt : new ArrayList<>(b)) {
            if (stmt.getOpcode() == Opcode.POP) {
                PopStmt pop = (PopStmt) stmt;
                Expr expr = pop.getExpression();
                if (expr.getOpcode() == Opcode.INVOKE) {
                    InvocationExpr invoke = (InvocationExpr) expr;
                    if (invoke.getCallType() == InvocationExpr.CallType.SPECIAL && invoke.getName().equals("<init>")) {
                        Expr inst = invoke.getPhysicalReceiver();
                        if (inst.getOpcode() == Opcode.LOCAL_LOAD) {
                            VarExpr var = (VarExpr) inst;
                            VersionedLocal local = (VersionedLocal) var.getLocal();
                            AbstractCopyStmt def = pool.defs.get(local);
                            Expr rhs = def.getExpression();
                            if (rhs.getOpcode() == Opcode.ALLOC_OBJ) {
                                // replace pop(x.<init>()) with x := new Klass();
                                // remove x := new Klass;
                                // here we are assuming that the new object
                                // can't be used until it is initialised.
                                Expr[] args = invoke.getParameterExprs();
                                // we want to reuse the exprs, so free it first.
                                pop.deleteAt(0);
                                Expr[] newArgs = Arrays.copyOf(args, args.length);
                                for (int i = args.length - 1; i >= 0; i--) {
                                    args[i].unlink();
                                }
                                // remove the old def
                                def.delete();
                                int index = b.indexOf(pop);
                                // add a copy statement before the pop (x = newExpr)
                                InitialisedObjectExpr newExpr = new InitialisedObjectExpr(invoke.getOwner(), invoke.getDesc(), newArgs);
                                CopyVarStmt newCvs = new CopyVarStmt(var, newExpr);
                                pool.defs.put(local, newCvs);
                                pool.uses.get(local).remove(var);
                                b.add(index, newCvs);
                                // remove the pop statement
                                b.remove(pop);
                                // update the latestval constraints
                                LatestValue lval = latest.get(local);
                                if (lval.hasConstraints()) {
                                    /* need to check this out (shouldn't happen) */
                                    System.out.println("Constraints:");
                                    for (Constraint c : lval.getConstraints()) {
                                        System.out.println("  " + c);
                                    }
                                    throw new IllegalStateException(lval.toString());
                                } else {
                                    lval.makeConstraints(newExpr);
                                }
                            }
                        } else if (inst.getOpcode() == Opcode.ALLOC_OBJ) {
                            // replace pop(new Klass.<init>(args)) with pop(new Klass(args))
                            // UninitialisedObjectExpr obj = (UninitialisedObjectExpr) inst;
                            Expr[] args = invoke.getParameterExprs();
                            // we want to reuse the exprs, so free it first.
                            invoke.unlink();
                            for (Expr e : args) {
                                e.unlink();
                            }
                            Expr[] newArgs = Arrays.copyOf(args, args.length);
                            InitialisedObjectExpr newExpr = new InitialisedObjectExpr(invoke.getOwner(), invoke.getDesc(), newArgs);
                            // replace pop contents
                            // no changes to defs or uses
                            pop.setExpression(newExpr);
                        } else {
                            System.err.println(b);
                            System.err.println("Stmt: " + stmt.getDisplayName() + ". " + stmt);
                            System.err.println("Inst: " + inst);
                            System.err.println(builder.graph);
                            throw new RuntimeException("interesting1 " + inst.getClass());
                        }
                    }
                }
            }
        }
    }
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) Constraint(org.mapleir.ir.cfg.builder.ssaopt.Constraint) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) Constraint(org.mapleir.ir.cfg.builder.ssaopt.Constraint) 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) LatestValue(org.mapleir.ir.cfg.builder.ssaopt.LatestValue) 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) PopStmt(org.mapleir.ir.code.stmt.PopStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr)

Example 2 with InvocationExpr

use of org.mapleir.ir.code.expr.invoke.InvocationExpr in project maple-ir by LLVM-but-worse.

the class GenerationPass method _call.

protected void _call(int op, String owner, String name, String desc) {
    save_stack(false);
    int argLen = Type.getArgumentTypes(desc).length + (op == INVOKESTATIC ? 0 : 1);
    Expr[] args = new Expr[argLen];
    for (int i = args.length - 1; i >= 0; i--) {
        args[i] = pop();
    }
    InvocationExpr callExpr = new InvocationExpr(InvocationExpr.CallType.resolveCallType(op), args, owner, name, desc);
    if (callExpr.getType() == Type.VOID_TYPE) {
        addStmt(new PopStmt(callExpr));
    } else {
        int index = currentStack.height();
        Type type = assign_stack(index, callExpr);
        push(load_stack(index, type));
    }
}
Also used : Type(org.objectweb.asm.Type) ComparisonType(org.mapleir.ir.code.stmt.ConditionalJumpStmt.ComparisonType) ValueComparisonType(org.mapleir.ir.code.expr.ComparisonExpr.ValueComparisonType) ArrayType(org.mapleir.ir.TypeUtils.ArrayType) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Expr(org.mapleir.ir.code.Expr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr)

Example 3 with InvocationExpr

use of org.mapleir.ir.code.expr.invoke.InvocationExpr in project maple-ir by LLVM-but-worse.

the class ConcreteStaticInvocationPass method accept.

@Override
public int accept(AnalysisContext cxt, IPass prev, List<IPass> completed) {
    int fixed = 0;
    InvocationResolver resolver = cxt.getInvocationResolver();
    for (ClassNode cn : cxt.getApplication().iterate()) {
        for (MethodNode mn : cn.methods) {
            ControlFlowGraph cfg = cxt.getIRCache().getFor(mn);
            for (BasicBlock b : cfg.vertices()) {
                for (Stmt stmt : b) {
                    for (Expr e : stmt.enumerateOnlyChildren()) {
                        if (e.getOpcode() == Opcode.INVOKE) {
                            InvocationExpr invoke = (InvocationExpr) e;
                            if (invoke.isStatic()) {
                                MethodNode invoked = resolver.resolveStaticCall(invoke.getOwner(), invoke.getName(), invoke.getDesc());
                                if (invoked != null) {
                                    if (!invoked.owner.name.equals(invoke.getOwner())) {
                                        invoke.setOwner(invoked.owner.name);
                                        fixed++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    System.out.printf("  corrected %d dodgy static calls.%n", fixed);
    return fixed;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Expr(org.mapleir.ir.code.Expr) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) InvocationResolver(org.mapleir.app.service.InvocationResolver) BasicBlock(org.mapleir.ir.cfg.BasicBlock) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Stmt(org.mapleir.ir.code.Stmt)

Example 4 with InvocationExpr

use of org.mapleir.ir.code.expr.invoke.InvocationExpr in project maple-ir by LLVM-but-worse.

the class ConstantParameterPass method patchCall.

private void patchCall(String newDesc, Expr call, boolean[] dead) {
    if (call.getOpcode() == Opcode.INIT_OBJ) {
        InitialisedObjectExpr init = (InitialisedObjectExpr) call;
        CodeUnit parent = init.getParent();
        Expr[] newArgs = buildArgs(init.getArgumentExprs(), false, dead);
        InitialisedObjectExpr init2 = new InitialisedObjectExpr(init.getOwner(), newDesc, newArgs);
        parent.overwrite(init2, parent.indexOf(init));
    } else if (call.getOpcode() == Opcode.INVOKE) {
        InvocationExpr invoke = (InvocationExpr) call;
        CodeUnit parent = invoke.getParent();
        Expr[] newArgs = buildArgs(invoke.getArgumentExprs(), invoke.getCallType() != InvocationExpr.CallType.STATIC, dead);
        InvocationExpr invoke2 = new InvocationExpr(invoke.getCallType(), newArgs, invoke.getOwner(), invoke.getName(), newDesc);
        parent.overwrite(invoke2, parent.indexOf(invoke));
    } else {
        throw new UnsupportedOperationException(call.toString());
    }
}
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) InitialisedObjectExpr(org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) CodeUnit(org.mapleir.ir.code.CodeUnit)

Example 5 with InvocationExpr

use of org.mapleir.ir.code.expr.invoke.InvocationExpr 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)

Aggregations

Expr (org.mapleir.ir.code.Expr)8 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)8 BasicBlock (org.mapleir.ir.cfg.BasicBlock)5 Stmt (org.mapleir.ir.code.Stmt)5 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)4 VarExpr (org.mapleir.ir.code.expr.VarExpr)4 InitialisedObjectExpr (org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr)4 MethodNode (org.objectweb.asm.tree.MethodNode)4 InvocationResolver (org.mapleir.app.service.InvocationResolver)3 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)3 Type (org.objectweb.asm.Type)3 ClassNode (org.objectweb.asm.tree.ClassNode)3 HashSet (java.util.HashSet)2 ApplicationClassSource (org.mapleir.app.service.ApplicationClassSource)2 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Set (java.util.Set)1 IPAnalysis (org.mapleir.deob.interproc.IPAnalysis)1