Search in sources :

Example 96 with Unit

use of soot.Unit in project soot by Sable.

the class ExceptionalUnitGraph method toString.

@Override
public String toString() {
    StringBuffer buf = new StringBuffer();
    for (Unit u : unitChain) {
        buf.append("  preds: " + getPredsOf(u) + "\n");
        buf.append("  unexceptional preds: " + getUnexceptionalPredsOf(u) + "\n");
        buf.append("  exceptional preds: " + getExceptionalPredsOf(u) + "\n");
        buf.append(u.toString() + '\n');
        buf.append("  exception destinations: " + getExceptionDests(u) + "\n");
        buf.append("  unexceptional succs: " + getUnexceptionalSuccsOf(u) + "\n");
        buf.append("  exceptional succs: " + getExceptionalSuccsOf(u) + "\n");
        buf.append("  succs " + getSuccsOf(u) + "\n\n");
    }
    return buf.toString();
}
Also used : Unit(soot.Unit)

Example 97 with Unit

use of soot.Unit in project soot by Sable.

the class ExceptionalBlockGraph method buildBlocks.

/**
 * {@inheritDoc}
 *
 * This implementation calls the inherited implementation to split
 * units into blocks, before adding the distinctions between
 * exceptional and unexceptional control flow.
 *
 * @param {@inheritDoc}
 *
 * @return {@inheritDoc}
 */
@Override
protected Map<Unit, Block> buildBlocks(Set<Unit> leaders, UnitGraph uncastUnitGraph) {
    ExceptionalUnitGraph unitGraph = (ExceptionalUnitGraph) uncastUnitGraph;
    Map<Unit, Block> unitToBlock = super.buildBlocks(leaders, unitGraph);
    if (unitGraph.getBody().getTraps().size() == 0) {
        // All exceptions escape the method.  Cache the ThrowAnalysis
        // to respond to getExceptionDests() on demand.
        throwAnalysis = unitGraph.getThrowAnalysis();
        if (throwAnalysis == null) {
            throw new IllegalStateException("ExceptionalUnitGraph lacked a cached ThrowAnalysis for a Body with no Traps.");
        }
    } else {
        int initialMapSize = (mBlocks.size() * 2) / 3;
        blockToUnexceptionalPreds = new HashMap<Block, List<Block>>(initialMapSize);
        blockToUnexceptionalSuccs = new HashMap<Block, List<Block>>(initialMapSize);
        blockToExceptionalPreds = new HashMap<Block, List<Block>>(initialMapSize);
        blockToExceptionalSuccs = new HashMap<Block, List<Block>>(initialMapSize);
        for (Block block : mBlocks) {
            Unit blockHead = block.getHead();
            List<Unit> exceptionalPredUnits = unitGraph.getExceptionalPredsOf(blockHead);
            if (exceptionalPredUnits.size() != 0) {
                List<Block> exceptionalPreds = mappedValues(exceptionalPredUnits, unitToBlock);
                exceptionalPreds = Collections.unmodifiableList(exceptionalPreds);
                blockToExceptionalPreds.put(block, exceptionalPreds);
                List<Unit> unexceptionalPredUnits = unitGraph.getUnexceptionalPredsOf(blockHead);
                List<Block> unexceptionalPreds = null;
                if (unexceptionalPredUnits.size() == 0) {
                    unexceptionalPreds = Collections.emptyList();
                } else {
                    unexceptionalPreds = mappedValues(unexceptionalPredUnits, unitToBlock);
                    unexceptionalPreds = Collections.unmodifiableList(unexceptionalPreds);
                }
                blockToUnexceptionalPreds.put(block, unexceptionalPreds);
            }
            Unit blockTail = block.getTail();
            List<Unit> exceptionalSuccUnits = unitGraph.getExceptionalSuccsOf(blockTail);
            if (exceptionalSuccUnits.size() != 0) {
                List<Block> exceptionalSuccs = mappedValues(exceptionalSuccUnits, unitToBlock);
                exceptionalSuccs = Collections.unmodifiableList(exceptionalSuccs);
                blockToExceptionalSuccs.put(block, exceptionalSuccs);
                List<Unit> unexceptionalSuccUnits = unitGraph.getUnexceptionalSuccsOf(blockTail);
                List<Block> unexceptionalSuccs = null;
                if (unexceptionalSuccUnits.size() == 0) {
                    unexceptionalSuccs = Collections.emptyList();
                } else {
                    unexceptionalSuccs = mappedValues(unexceptionalSuccUnits, unitToBlock);
                    unexceptionalSuccs = Collections.unmodifiableList(unexceptionalSuccs);
                }
                blockToUnexceptionalSuccs.put(block, unexceptionalSuccs);
            }
        }
        blockToExceptionDests = buildExceptionDests(unitGraph, unitToBlock);
    }
    return unitToBlock;
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) Unit(soot.Unit)

Example 98 with Unit

use of soot.Unit in project soot by Sable.

the class FieldRenamer method setBooleanTo.

protected void setBooleanTo(SootClass sc, SootField f, boolean value) {
    if (!value && f.getType() instanceof IntegerType && Rand.getInt() % 2 > 0) {
        return;
    }
    RefType boolRef = Scene.v().getRefType(booleanClassName);
    Body body;
    boolean newInit = false;
    if (!sc.declaresMethodByName(SootMethod.staticInitializerName)) {
        SootMethod m = Scene.v().makeSootMethod(SootMethod.staticInitializerName, emptyList(), VoidType.v(), Modifier.STATIC);
        sc.addMethod(m);
        body = Jimple.v().newBody(m);
        m.setActiveBody(body);
        newInit = true;
    } else {
        SootMethod m = sc.getMethodByName(SootMethod.staticInitializerName);
        body = m.getActiveBody();
    }
    PatchingChain<Unit> units = body.getUnits();
    if (f.getType() instanceof IntegerType) {
        units.addFirst(Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(f.makeRef()), IntConstant.v(value ? 1 : 0)));
    } else {
        Local bool = Jimple.v().newLocal("boolLcl", boolRef);
        body.getLocals().add(bool);
        SootMethod boolInit = boolRef.getSootClass().getMethod("void <init>(boolean)");
        units.addFirst(Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(f.makeRef()), bool));
        units.addFirst(Jimple.v().newInvokeStmt(Jimple.v().newSpecialInvokeExpr(bool, boolInit.makeRef(), IntConstant.v(value ? 1 : 0))));
        units.addFirst(Jimple.v().newAssignStmt(bool, Jimple.v().newNewExpr(boolRef)));
    }
    if (newInit) {
        units.addLast(Jimple.v().newReturnVoidStmt());
    }
}
Also used : IntegerType(soot.IntegerType) RefType(soot.RefType) SootMethod(soot.SootMethod) Local(soot.Local) Unit(soot.Unit) Body(soot.Body)

Example 99 with Unit

use of soot.Unit in project soot by Sable.

the class LibraryMethodWrappersBuilder method getFirstNotIdentityStmt.

private static Unit getFirstNotIdentityStmt(Body body) {
    final Iterator<Unit> unitIterator = body.getUnits().snapshotIterator();
    while (unitIterator.hasNext()) {
        final Unit unit = unitIterator.next();
        if (unit instanceof IdentityStmt) {
            continue;
        }
        return unit;
    }
    logger.debug("There are no non-identity units in the method body.");
    return null;
}
Also used : Unit(soot.Unit) IdentityStmt(soot.jimple.IdentityStmt)

Example 100 with Unit

use of soot.Unit in project soot by Sable.

the class ArithmeticTransformer method internalTransform.

protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
    int weight = soot.jbco.Main.getWeight(phaseName, b.getMethod().getSignature());
    if (weight == 0) {
        return;
    }
    PatchingChain<Unit> units = b.getUnits();
    int localCount = 0;
    Chain<Local> locals = b.getLocals();
    if (output) {
        out.println("*** Performing Arithmetic Transformation on " + b.getMethod().getSignature());
    }
    Iterator<Unit> it = units.snapshotIterator();
    while (it.hasNext()) {
        Unit u = it.next();
        if (u instanceof AssignStmt) {
            AssignStmt as = (AssignStmt) u;
            Value v = as.getRightOp();
            if (v instanceof MulExpr) {
                total++;
                MulExpr me = (MulExpr) v;
                Value op1 = me.getOp1();
                Value op = null, op2 = me.getOp2();
                NumericConstant nc = null;
                if (op1 instanceof NumericConstant) {
                    nc = (NumericConstant) op1;
                    op = op2;
                } else if (op2 instanceof NumericConstant) {
                    nc = (NumericConstant) op2;
                    op = op1;
                }
                if (nc != null) {
                    if (output) {
                        out.println("Considering: " + as + "\r");
                    }
                    Type opType = op.getType();
                    int max = opType instanceof IntType ? 32 : opType instanceof LongType ? 64 : 0;
                    if (max != 0) {
                        Object[] shft_rem = checkNumericValue(nc);
                        if (shft_rem[0] != null && (Integer) shft_rem[0] < max && Rand.getInt(10) <= weight) {
                            List<Unit> unitsBuilt = new ArrayList<>();
                            int rand = Rand.getInt(16);
                            int shift = (Integer) shft_rem[0];
                            boolean neg = (Boolean) shft_rem[2];
                            if (rand % 2 == 0) {
                                shift += rand * max;
                            } else {
                                shift -= rand * max;
                            }
                            Expr e;
                            if (shft_rem[1] != null) {
                                // if there is an additive floating component
                                Local tmp2 = null, tmp1 = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, opType);
                                locals.add(tmp1);
                                // shift the integral portion
                                Unit newU = Jimple.v().newAssignStmt(tmp1, Jimple.v().newShlExpr(op, IntConstant.v(shift)));
                                unitsBuilt.add(newU);
                                units.insertBefore(newU, u);
                                // grab remainder (that not part of the 2^x)
                                double rem = (Double) shft_rem[1];
                                if (rem != 1) {
                                    if (rem == ((int) rem) && opType instanceof IntType) {
                                        nc = IntConstant.v((int) rem);
                                    } else if (rem == ((long) rem) && opType instanceof LongType) {
                                        nc = LongConstant.v((long) rem);
                                    } else {
                                        nc = DoubleConstant.v(rem);
                                    }
                                    if (nc instanceof DoubleConstant) {
                                        tmp2 = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, DoubleType.v());
                                        locals.add(tmp2);
                                        newU = Jimple.v().newAssignStmt(tmp2, Jimple.v().newCastExpr(op, DoubleType.v()));
                                        unitsBuilt.add(newU);
                                        units.insertBefore(newU, u);
                                        newU = Jimple.v().newAssignStmt(tmp2, Jimple.v().newMulExpr(tmp2, nc));
                                    } else {
                                        tmp2 = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, nc.getType());
                                        locals.add(tmp2);
                                        newU = Jimple.v().newAssignStmt(tmp2, Jimple.v().newMulExpr(op, nc));
                                    }
                                    unitsBuilt.add(newU);
                                    units.insertBefore(newU, u);
                                }
                                if (tmp2 == null) {
                                    e = Jimple.v().newAddExpr(tmp1, op);
                                } else if (tmp2.getType().getClass() != tmp1.getType().getClass()) {
                                    Local tmp3 = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, tmp2.getType());
                                    locals.add(tmp3);
                                    newU = Jimple.v().newAssignStmt(tmp3, Jimple.v().newCastExpr(tmp1, tmp2.getType()));
                                    unitsBuilt.add(newU);
                                    units.insertBefore(newU, u);
                                    e = Jimple.v().newAddExpr(tmp3, tmp2);
                                } else {
                                    e = Jimple.v().newAddExpr(tmp1, tmp2);
                                }
                            } else {
                                e = Jimple.v().newShlExpr(op, IntConstant.v(shift));
                            }
                            if (e.getType().getClass() != as.getLeftOp().getType().getClass()) {
                                Local tmp = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, e.getType());
                                locals.add(tmp);
                                Unit newU = Jimple.v().newAssignStmt(tmp, e);
                                unitsBuilt.add(newU);
                                units.insertAfter(newU, u);
                                e = Jimple.v().newCastExpr(tmp, as.getLeftOp().getType());
                            }
                            as.setRightOp(e);
                            unitsBuilt.add(as);
                            if (neg) {
                                Unit newU = Jimple.v().newAssignStmt(as.getLeftOp(), Jimple.v().newNegExpr(as.getLeftOp()));
                                unitsBuilt.add(newU);
                                units.insertAfter(newU, u);
                            }
                            mulPerformed++;
                            printOutput(unitsBuilt);
                        }
                    }
                }
            } else if (v instanceof DivExpr) {
                total++;
                DivExpr de = (DivExpr) v;
                Value op2 = de.getOp2();
                NumericConstant nc;
                if (op2 instanceof NumericConstant) {
                    nc = (NumericConstant) op2;
                    Type opType = de.getOp1().getType();
                    int max = opType instanceof IntType ? 32 : opType instanceof LongType ? 64 : 0;
                    if (max != 0) {
                        Object[] shft_rem = checkNumericValue(nc);
                        if (shft_rem[0] != null && (shft_rem[1] == null || (Double) shft_rem[1] == 0) && (Integer) shft_rem[0] < max && Rand.getInt(10) <= weight) {
                            List<Unit> unitsBuilt = new ArrayList<>();
                            int rand = Rand.getInt(16);
                            int shift = (Integer) shft_rem[0];
                            boolean neg = (Boolean) shft_rem[2];
                            if (Rand.getInt() % 2 == 0) {
                                shift += rand * max;
                            } else {
                                shift -= rand * max;
                            }
                            Expr e = Jimple.v().newShrExpr(de.getOp1(), IntConstant.v(shift));
                            if (e.getType().getClass() != as.getLeftOp().getType().getClass()) {
                                Local tmp = Jimple.v().newLocal("__tmp_shft_lcl" + localCount++, e.getType());
                                locals.add(tmp);
                                Unit newU = Jimple.v().newAssignStmt(tmp, e);
                                unitsBuilt.add(newU);
                                units.insertAfter(newU, u);
                                e = Jimple.v().newCastExpr(tmp, as.getLeftOp().getType());
                            }
                            as.setRightOp(e);
                            unitsBuilt.add(as);
                            if (neg) {
                                Unit newU = Jimple.v().newAssignStmt(as.getLeftOp(), Jimple.v().newNegExpr(as.getLeftOp()));
                                unitsBuilt.add(newU);
                                units.insertAfter(newU, u);
                            }
                            divPerformed++;
                            printOutput(unitsBuilt);
                        }
                    }
                }
            }
        }
    }
}
Also used : MulExpr(soot.jimple.MulExpr) DoubleConstant(soot.jimple.DoubleConstant) LongType(soot.LongType) AssignStmt(soot.jimple.AssignStmt) ArrayList(java.util.ArrayList) Unit(soot.Unit) IntType(soot.IntType) ArrayList(java.util.ArrayList) List(java.util.List) Local(soot.Local) DoubleType(soot.DoubleType) IntType(soot.IntType) LongType(soot.LongType) Type(soot.Type) DivExpr(soot.jimple.DivExpr) MulExpr(soot.jimple.MulExpr) Expr(soot.jimple.Expr) DivExpr(soot.jimple.DivExpr) NumericConstant(soot.jimple.NumericConstant) Value(soot.Value)

Aggregations

Unit (soot.Unit)240 Local (soot.Local)77 Stmt (soot.jimple.Stmt)77 Value (soot.Value)74 ArrayList (java.util.ArrayList)65 AssignStmt (soot.jimple.AssignStmt)58 SootMethod (soot.SootMethod)47 Body (soot.Body)37 InvokeStmt (soot.jimple.InvokeStmt)35 Type (soot.Type)34 HashSet (java.util.HashSet)33 ValueBox (soot.ValueBox)33 InvokeExpr (soot.jimple.InvokeExpr)33 Trap (soot.Trap)32 RefType (soot.RefType)30 IdentityStmt (soot.jimple.IdentityStmt)28 HashMap (java.util.HashMap)27 IfStmt (soot.jimple.IfStmt)27 DefinitionStmt (soot.jimple.DefinitionStmt)25 List (java.util.List)23