Search in sources :

Example 1 with UnitBox

use of soot.UnitBox in project soot by Sable.

the class AsmMethodSource method emitUnits.

private void emitUnits() {
    AbstractInsnNode insn = instructions.getFirst();
    ArrayDeque<LabelNode> labls = new ArrayDeque<LabelNode>();
    while (insn != null) {
        // Save the label to assign it to the next real unit
        if (insn instanceof LabelNode)
            labls.add((LabelNode) insn);
        // Get the unit associated with the current instruction
        Unit u = units.get(insn);
        if (u == null) {
            insn = insn.getNext();
            continue;
        }
        emitUnits(u);
        // If this is an exception handler, register the starting unit for it
        {
            IdentityStmt caughtEx = null;
            if (u instanceof IdentityStmt)
                caughtEx = (IdentityStmt) u;
            else if (u instanceof UnitContainer)
                caughtEx = getIdentityRefFromContrainer((UnitContainer) u);
            if (insn instanceof LabelNode && caughtEx != null && caughtEx.getRightOp() instanceof CaughtExceptionRef) {
                // We directly place this label
                Collection<UnitBox> traps = trapHandlers.get((LabelNode) insn);
                for (UnitBox ub : traps) ub.setUnit(caughtEx);
            }
        }
        // Register this unit for all targets of the labels ending up at it
        while (!labls.isEmpty()) {
            LabelNode ln = labls.poll();
            Collection<UnitBox> boxes = labels.get(ln);
            if (boxes != null) {
                for (UnitBox box : boxes) {
                    box.setUnit(u instanceof UnitContainer ? ((UnitContainer) u).getFirstUnit() : u);
                }
            }
        }
        insn = insn.getNext();
    }
    // Emit the inline exception handlers
    for (LabelNode ln : this.inlineExceptionHandlers.keySet()) {
        Unit handler = this.inlineExceptionHandlers.get(ln);
        emitUnits(handler);
        Collection<UnitBox> traps = trapHandlers.get(ln);
        for (UnitBox ub : traps) ub.setUnit(handler);
        // We need to jump to the original implementation
        Unit targetUnit = units.get(ln);
        GotoStmt gotoImpl = Jimple.v().newGotoStmt(targetUnit);
        body.getUnits().add(gotoImpl);
    }
    /* set remaining labels & boxes to last unit of chain */
    if (labls.isEmpty())
        return;
    Unit end = Jimple.v().newNopStmt();
    body.getUnits().add(end);
    while (!labls.isEmpty()) {
        LabelNode ln = labls.poll();
        Collection<UnitBox> boxes = labels.get(ln);
        if (boxes != null) {
            for (UnitBox box : boxes) box.setUnit(end);
        }
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) UnitBox(soot.UnitBox) CaughtExceptionRef(soot.jimple.CaughtExceptionRef) GotoStmt(soot.jimple.GotoStmt) Collection(java.util.Collection) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) Unit(soot.Unit) ArrayDeque(java.util.ArrayDeque) IdentityStmt(soot.jimple.IdentityStmt)

Example 2 with UnitBox

use of soot.UnitBox in project soot by Sable.

the class AsmMethodSource method convertJumpInsn.

private void convertJumpInsn(JumpInsnNode insn) {
    int op = insn.getOpcode();
    if (op == GOTO) {
        if (!units.containsKey(insn)) {
            UnitBox box = Jimple.v().newStmtBox(null);
            labels.put(insn.label, box);
            setUnit(insn, Jimple.v().newGotoStmt(box));
        }
        return;
    }
    /* must be ifX insn */
    StackFrame frame = getFrame(insn);
    if (!units.containsKey(insn)) {
        Operand val = popImmediate();
        Value v = val.stackOrValue();
        ConditionExpr cond;
        if (op >= IF_ICMPEQ && op <= IF_ACMPNE) {
            Operand val1 = popImmediate();
            Value v1 = val1.stackOrValue();
            if (op == IF_ICMPEQ)
                cond = Jimple.v().newEqExpr(v1, v);
            else if (op == IF_ICMPNE)
                cond = Jimple.v().newNeExpr(v1, v);
            else if (op == IF_ICMPLT)
                cond = Jimple.v().newLtExpr(v1, v);
            else if (op == IF_ICMPGE)
                cond = Jimple.v().newGeExpr(v1, v);
            else if (op == IF_ICMPGT)
                cond = Jimple.v().newGtExpr(v1, v);
            else if (op == IF_ICMPLE)
                cond = Jimple.v().newLeExpr(v1, v);
            else if (op == IF_ACMPEQ)
                cond = Jimple.v().newEqExpr(v1, v);
            else if (op == IF_ACMPNE)
                cond = Jimple.v().newNeExpr(v1, v);
            else
                throw new AssertionError("Unknown if op: " + op);
            val1.addBox(cond.getOp1Box());
            val.addBox(cond.getOp2Box());
            frame.boxes(cond.getOp2Box(), cond.getOp1Box());
            frame.in(val, val1);
        } else {
            if (op == IFEQ)
                cond = Jimple.v().newEqExpr(v, IntConstant.v(0));
            else if (op == IFNE)
                cond = Jimple.v().newNeExpr(v, IntConstant.v(0));
            else if (op == IFLT)
                cond = Jimple.v().newLtExpr(v, IntConstant.v(0));
            else if (op == IFGE)
                cond = Jimple.v().newGeExpr(v, IntConstant.v(0));
            else if (op == IFGT)
                cond = Jimple.v().newGtExpr(v, IntConstant.v(0));
            else if (op == IFLE)
                cond = Jimple.v().newLeExpr(v, IntConstant.v(0));
            else if (op == IFNULL)
                cond = Jimple.v().newEqExpr(v, NullConstant.v());
            else if (op == IFNONNULL)
                cond = Jimple.v().newNeExpr(v, NullConstant.v());
            else
                throw new AssertionError("Unknown if op: " + op);
            val.addBox(cond.getOp1Box());
            frame.boxes(cond.getOp1Box());
            frame.in(val);
        }
        UnitBox box = Jimple.v().newStmtBox(null);
        labels.put(insn.label, box);
        setUnit(insn, Jimple.v().newIfStmt(cond, box));
    } else {
        if (op >= IF_ICMPEQ && op <= IF_ACMPNE)
            frame.mergeIn(pop(), pop());
        else
            frame.mergeIn(pop());
    }
}
Also used : UnitBox(soot.UnitBox) ConditionExpr(soot.jimple.ConditionExpr) Value(soot.Value)

Example 3 with UnitBox

use of soot.UnitBox in project soot by Sable.

the class AsmMethodSource method convertLookupSwitchInsn.

private void convertLookupSwitchInsn(LookupSwitchInsnNode insn) {
    StackFrame frame = getFrame(insn);
    if (units.containsKey(insn)) {
        frame.mergeIn(pop());
        return;
    }
    Operand key = popImmediate();
    UnitBox dflt = Jimple.v().newStmtBox(null);
    List<UnitBox> targets = new ArrayList<UnitBox>(insn.labels.size());
    labels.put(insn.dflt, dflt);
    for (LabelNode ln : insn.labels) {
        UnitBox box = Jimple.v().newStmtBox(null);
        targets.add(box);
        labels.put(ln, box);
    }
    List<IntConstant> keys = new ArrayList<IntConstant>(insn.keys.size());
    for (Integer i : insn.keys) keys.add(IntConstant.v(i));
    LookupSwitchStmt lss = Jimple.v().newLookupSwitchStmt(key.stackOrValue(), keys, targets, dflt);
    key.addBox(lss.getKeyBox());
    frame.in(key);
    frame.boxes(lss.getKeyBox());
    setUnit(insn, lss);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) UnitBox(soot.UnitBox) ArrayList(java.util.ArrayList) IntConstant(soot.jimple.IntConstant) LookupSwitchStmt(soot.jimple.LookupSwitchStmt)

Example 4 with UnitBox

use of soot.UnitBox in project soot by Sable.

the class Walker method outACatchClause.

/*
	 * catch_clause = catch [name]:class_name from [from_label]:label_name to
	 * [to_label]:label_name with [with_label]:label_name semicolon;
	 */
// public void caseACatchClause(ACatchClause node){}
public void outACatchClause(ACatchClause node) {
    String exceptionName;
    UnitBox withUnit, fromUnit, toUnit;
    withUnit = Jimple.v().newStmtBox(null);
    addBoxToPatch((String) mProductions.removeLast(), withUnit);
    toUnit = Jimple.v().newStmtBox(null);
    addBoxToPatch((String) mProductions.removeLast(), toUnit);
    fromUnit = Jimple.v().newStmtBox(null);
    addBoxToPatch((String) mProductions.removeLast(), fromUnit);
    exceptionName = (String) mProductions.removeLast();
    Trap trap = Jimple.v().newTrap(mResolver.makeClassRef(exceptionName), fromUnit, toUnit, withUnit);
    mProductions.addLast(trap);
}
Also used : UnitBox(soot.UnitBox) Trap(soot.Trap)

Example 5 with UnitBox

use of soot.UnitBox in project soot by Sable.

the class Walker method outALookupswitchStatement.

public void outALookupswitchStatement(ALookupswitchStatement node) {
    List<IntConstant> lookupValues = new ArrayList<IntConstant>();
    List<UnitBox> targets = new ArrayList<UnitBox>();
    UnitBox defaultTarget = null;
    if (node.getCaseStmt() != null) {
        int size = node.getCaseStmt().size();
        for (int i = 0; i < size; i++) {
            Object valueTargetPair = mProductions.removeLast();
            if (valueTargetPair instanceof UnitBox) {
                if (defaultTarget != null)
                    throw new RuntimeException("error: can't ;have more than 1 default stmt");
                defaultTarget = (UnitBox) valueTargetPair;
            } else {
                Object[] pair = (Object[]) valueTargetPair;
                lookupValues.add(0, (IntConstant) pair[0]);
                targets.add(0, (UnitBox) pair[1]);
            }
        }
    } else {
        throw new RuntimeException("error: switch stmt has no case stmts");
    }
    Value key = (Value) mProductions.removeLast();
    Unit switchStmt = Jimple.v().newLookupSwitchStmt(key, lookupValues, targets, defaultTarget);
    mProductions.addLast(switchStmt);
}
Also used : UnitBox(soot.UnitBox) ArrayList(java.util.ArrayList) Value(soot.Value) IntConstant(soot.jimple.IntConstant) Unit(soot.Unit)

Aggregations

UnitBox (soot.UnitBox)23 Unit (soot.Unit)16 ArrayList (java.util.ArrayList)9 Trap (soot.Trap)9 Value (soot.Value)8 HashMap (java.util.HashMap)5 Local (soot.Local)5 Iterator (java.util.Iterator)4 LabelNode (org.objectweb.asm.tree.LabelNode)4 GotoStmt (soot.jimple.GotoStmt)4 IntConstant (soot.jimple.IntConstant)4 ReturnStmt (soot.jimple.ReturnStmt)4 HashSet (java.util.HashSet)3 List (java.util.List)3 CaughtExceptionRef (soot.jimple.CaughtExceptionRef)3 IdentityStmt (soot.jimple.IdentityStmt)3 IfStmt (soot.jimple.IfStmt)3 ParameterRef (soot.jimple.ParameterRef)3 Stmt (soot.jimple.Stmt)3 ThisRef (soot.jimple.ThisRef)3