Search in sources :

Example 36 with Stmt

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

the class LatestValue method findReachable.

private Set<Stmt> findReachable(Stmt from, Stmt to) {
    Set<Stmt> res = new HashSet<>();
    BasicBlock f = from.getBlock();
    BasicBlock t = to.getBlock();
    int end = f == t ? f.indexOf(to) : f.size();
    for (int i = f.indexOf(from); i < end; i++) {
        res.add(f.get(i));
    }
    if (f != t) {
        for (BasicBlock r : cfg.wanderAllTrails(f, t)) {
            res.addAll(r);
        }
    }
    return res;
}
Also used : BasicBlock(org.mapleir.ir.cfg.BasicBlock) Stmt(org.mapleir.ir.code.Stmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) HashSet(java.util.HashSet)

Example 37 with Stmt

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

the class DumbExceptionAnalysis method getPossibleUserThrowables.

@Override
public Set<Type> getPossibleUserThrowables(CodeUnit u) {
    Set<Type> set = new HashSet<>();
    if (u.isFlagSet(CodeUnit.FLAG_STMT)) {
        Stmt s = (Stmt) u;
        canThrowStmt(s, set);
        for (Expr e : s.enumerateOnlyChildren()) {
            canThrowExpr(e, set);
        }
    } else {
        for (Expr e : ((Expr) u).enumerateWithSelf()) {
            canThrowExpr(e, set);
        }
    }
    return set;
}
Also used : Type(org.objectweb.asm.Type) ArithmeticExpr(org.mapleir.ir.code.expr.ArithmeticExpr) Expr(org.mapleir.ir.code.Expr) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) HashSet(java.util.HashSet) ThrowStmt(org.mapleir.ir.code.stmt.ThrowStmt) MonitorStmt(org.mapleir.ir.code.stmt.MonitorStmt) Stmt(org.mapleir.ir.code.Stmt)

Example 38 with Stmt

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

the class SensitiveCallGraphBuilder method process.

@Override
public void process(Worklist<MethodNode> worklist, MethodNode n) {
    if (worklist != this.worklist) {
        throw new IllegalStateException();
    }
    if (worklist.hasProcessed(n)) {
        throw new UnsupportedOperationException(String.format("Already processed %s", n));
    }
    /* this is not the same as getNode */
    CallGraphNode.CallReceiverNode currentReceiverNode = createNode(n, false);
    ControlFlowGraph cfg = context.getIRCache().get(n);
    if (cfg == null) {
        return;
    }
    for (Stmt stmt : cfg.stmts()) {
        for (Expr e : stmt.enumerateOnlyChildren()) {
            if (e instanceof Invocation) {
                Invocation invoke = (Invocation) e;
                CallGraphNode.CallSiteNode thisCallSiteNode = callGraph.addInvocation(n, invoke);
                /* link the current receiver to this call site. */
                FunctionOwnershipEdge foe = new FunctionOwnershipEdge(currentReceiverNode, thisCallSiteNode);
                callGraph.addEdge(currentReceiverNode, foe);
                Set<MethodNode> targets = invoke.resolveTargets(context.getInvocationResolver());
                for (MethodNode target : targets) {
                    CallGraphNode.CallReceiverNode targetReceiverNode = createNode(target, true);
                    /* link each target to the call site. */
                    SiteInvocationEdge sie = new SiteInvocationEdge(thisCallSiteNode, targetReceiverNode);
                    callGraph.addEdge(thisCallSiteNode, sie);
                }
            }
        }
    }
}
Also used : Invocation(org.mapleir.ir.code.expr.invoke.Invocation) SiteInvocationEdge(org.mapleir.deob.callgraph.CallGraphEdge.SiteInvocationEdge) Stmt(org.mapleir.ir.code.Stmt) Expr(org.mapleir.ir.code.Expr) MethodNode(org.objectweb.asm.tree.MethodNode) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) FunctionOwnershipEdge(org.mapleir.deob.callgraph.CallGraphEdge.FunctionOwnershipEdge)

Example 39 with Stmt

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

the class DemoteRangesPass method process.

private void process(ApplicationClassSource app, ControlFlowGraph cfg, ExceptionAnalysis analysis) {
    TarjanSCC<BasicBlock> sccComputor = new TarjanSCC<>(cfg);
    for (BasicBlock b : cfg.vertices()) {
        if (sccComputor.low(b) == -1) {
            sccComputor.search(b);
        }
    }
    Map<BasicBlock, List<BasicBlock>> sccs = new HashMap<>();
    for (List<BasicBlock> l : sccComputor.getComponents()) {
        for (BasicBlock e : l) {
            if (sccs.containsKey(e)) {
                throw new IllegalStateException();
            } else {
                sccs.put(e, l);
            }
        }
    }
    for (ExceptionRange<BasicBlock> er : cfg.getRanges()) {
        /* if the handler catches */
        for (BasicBlock b : er.get()) {
            Set<Type> canThrow = new HashSet<>();
            List<BasicBlock> comp = new ArrayList<>();
            if (sccs.containsKey(b)) {
                comp.addAll(sccs.get(b));
            } else {
                comp.add(b);
            }
            for (BasicBlock e : comp) {
                for (Stmt stmt : e) {
                    canThrow.addAll(analysis.getPossibleUserThrowables(stmt));
                }
            }
            if (!catchesAny(app, er.getTypes(), canThrow)) {
                if (comp.size() > 1) {
                    System.out.println("promote: " + GraphUtils.toNodeArray(comp));
                    for (BasicBlock e : comp) {
                        System.out.println(ControlFlowGraph.printBlock(e));
                    }
                    System.out.println(" canThrow: " + canThrow);
                    System.out.println(" catching: " + er.getTypes());
                    System.out.println();
                    System.out.println();
                    System.out.println();
                    return;
                }
            } else {
                break;
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) BasicBlock(org.mapleir.ir.cfg.BasicBlock) ArrayList(java.util.ArrayList) Stmt(org.mapleir.ir.code.Stmt) Type(org.objectweb.asm.Type) TarjanSCC(org.mapleir.stdlib.collections.graph.algorithms.TarjanSCC) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 40 with Stmt

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

the class FieldRSADecryptionPass method transform.

private void transform(AnalysisContext cxt) {
    for (ClassNode cn : cxt.getApplication().iterate()) {
        for (MethodNode m : cn.methods) {
            ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
            for (BasicBlock b : cfg.vertices()) {
                for (Stmt stmt : b) {
                    // String fsKey = "";
                    if (stmt.getOpcode() == Opcode.FIELD_STORE) {
                        FieldStoreStmt fs = (FieldStoreStmt) stmt;
                        // [enc, dec]
                        Number[] p = pairs.get(key(fs));
                        if (p != null) {
                            Expr e = fs.getValueExpression();
                            e.unlink();
                            ArithmeticExpr ae = new ArithmeticExpr(new ConstantExpr(p[1], ConstantExpr.computeType(p[1])), e, Operator.MUL);
                            fs.setValueExpression(ae);
                        // fsKey = key(fs);
                        }
                    }
                    for (Expr e : stmt.enumerateOnlyChildren()) {
                        if (e.getOpcode() == FIELD_LOAD) {
                            CodeUnit par = e.getParent();
                            FieldLoadExpr fl = (FieldLoadExpr) e;
                            // [enc, dec]
                            Number[] p = pairs.get(key(fl));
                            if (p == null) {
                                continue;
                            }
                            if (par.getOpcode() == ARITHMETIC) {
                                ArithmeticExpr ae = (ArithmeticExpr) par;
                                if (ae.getRight().getOpcode() == CONST_LOAD) {
                                    ConstantExpr ce = (ConstantExpr) ae.getRight();
                                    Number cst = (Number) ce.getConstant();
                                    Number res = __mul(cst, p[0], p[0].getClass().equals(Long.class));
                                    // if(!__eq(res, 1, p[0].getClass().equals(Long.class))) {
                                    // System.out.println(cst + " -> " + res);
                                    // System.out.println("  expr: " + fl.getRootParent());
                                    // }
                                    par.overwrite(new ConstantExpr(res, ConstantExpr.computeType(res)), par.indexOf(ce));
                                    continue;
                                }
                            }
                            ArithmeticExpr ae = new ArithmeticExpr(new ConstantExpr(p[0], ConstantExpr.computeType(p[0])), fl.copy(), Operator.MUL);
                            par.overwrite(ae, par.indexOf(fl));
                        }
                    }
                }
            }
        }
    }
// for(ClassNode cn : cxt.getClassTree().getClasses().values()) {
// for(MethodNode m : cn.methods) {
// ControlFlowGraph cfg = cxt.getCFGS().getIR(m);
// 
// for(BasicBlock b : cfg.vertices()) {
// for(Stmt stmt : b) {
// for(Expr e : stmt.enumerateOnlyChildren()) {
// if(e.getOpcode() == Opcode.ARITHMETIC) {
// ArithmeticExpr ae = (ArithmeticExpr) e;
// if(ae.getRight().getOpcode() == Opcode.CONST_LOAD) {
// ConstantExpr c = (ConstantExpr) ae.getRight();
// Object o = c.getConstant();
// 
// if(o instanceof Long || o instanceof Integer) {
// Number n = (Number) o;
// if(__eq(n, 1, ae.getType().equals(Type.LONG_TYPE))) {
// Expr l = ae.getLeft();
// l.unlink();
// 
// CodeUnit aePar = ae.getParent();
// aePar.overwrite(l, aePar.indexOf(ae));
// } else if(__eq(n, 0, ae.getType().equals(Type.LONG_TYPE))) {
// c.unlink();
// 
// CodeUnit aePar = ae.getParent();
// aePar.overwrite(c, aePar.indexOf(ae));
// }
// }
// }
// }
// }
// }
// }
// }
// }
}
Also used : FieldStoreStmt(org.mapleir.ir.code.stmt.FieldStoreStmt) ClassNode(org.objectweb.asm.tree.ClassNode) FieldLoadExpr(org.mapleir.ir.code.expr.FieldLoadExpr) BasicBlock(org.mapleir.ir.cfg.BasicBlock) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) FieldStoreStmt(org.mapleir.ir.code.stmt.FieldStoreStmt) Stmt(org.mapleir.ir.code.Stmt) CodeUnit(org.mapleir.ir.code.CodeUnit) MethodNode(org.objectweb.asm.tree.MethodNode) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) ArithmeticExpr(org.mapleir.ir.code.expr.ArithmeticExpr) Expr(org.mapleir.ir.code.Expr) FieldLoadExpr(org.mapleir.ir.code.expr.FieldLoadExpr) ArithmeticExpr(org.mapleir.ir.code.expr.ArithmeticExpr) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph)

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