Search in sources :

Example 26 with AssignStmt

use of soot.jimple.AssignStmt in project soot by Sable.

the class ExceptionChecker method checkInvokeExpr.

protected void checkInvokeExpr(Body b, InvokeExpr ie, Stmt s) {
    if (ie instanceof InstanceInvokeExpr && ((InstanceInvokeExpr) ie).getBase().getType() instanceof ArrayType && ie.getMethodRef().name().equals("clone") && ie.getMethodRef().parameterTypes().size() == 0)
        // the call is to the clone() method of an array type, which
        return;
    // is defined not to throw any exceptions; if we left this to
    // normal resolution we'd get the method in Object which does
    // throw CloneNotSupportedException
    List exceptions = ie instanceof InterfaceInvokeExpr ? // the method in supertypes.
    getExceptionSpec(ie.getMethodRef().declaringClass(), ie.getMethodRef().getSubSignature()) : // Otherwise, we just do normal resolution.
    ie.getMethod().getExceptionsUnsafe();
    if (exceptions == null)
        return;
    Iterator it = exceptions.iterator();
    while (it.hasNext()) {
        SootClass sc = (SootClass) it.next();
        if (isThrowDeclared(b, sc) || isExceptionCaught(b, s, sc.getType()))
            continue;
        if (reporter != null) {
            if (s instanceof InvokeStmt) {
                reporter.reportError(new ExceptionCheckerError(b.getMethod(), sc, s, (SourceLnPosTag) s.getTag("SourceLnPosTag")));
            } else if (s instanceof AssignStmt) {
                reporter.reportError(new ExceptionCheckerError(b.getMethod(), sc, s, (SourceLnPosTag) ((AssignStmt) s).getRightOpBox().getTag("SourceLnPosTag")));
            }
        }
    }
}
Also used : ArrayType(soot.ArrayType) SourceLnPosTag(soot.tagkit.SourceLnPosTag) InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) Iterator(java.util.Iterator) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) List(java.util.List) SootClass(soot.SootClass)

Example 27 with AssignStmt

use of soot.jimple.AssignStmt in project soot by Sable.

the class ExceptionChecker method internalTransform.

@Override
protected void internalTransform(Body b, String phaseName, Map options) {
    Iterator it = b.getUnits().iterator();
    while (it.hasNext()) {
        Stmt s = (Stmt) it.next();
        if (s instanceof ThrowStmt) {
            ThrowStmt ts = (ThrowStmt) s;
            checkThrow(b, ts);
        } else if (s instanceof InvokeStmt) {
            InvokeStmt is = (InvokeStmt) s;
            checkInvoke(b, is);
        } else if ((s instanceof AssignStmt) && (((AssignStmt) s).getRightOp() instanceof InvokeExpr)) {
            InvokeExpr ie = (InvokeExpr) ((AssignStmt) s).getRightOp();
            checkInvokeExpr(b, ie, s);
        }
    }
}
Also used : InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) Iterator(java.util.Iterator) ThrowStmt(soot.jimple.ThrowStmt) Stmt(soot.jimple.Stmt) InvokeStmt(soot.jimple.InvokeStmt) ThrowStmt(soot.jimple.ThrowStmt) AssignStmt(soot.jimple.AssignStmt)

Example 28 with AssignStmt

use of soot.jimple.AssignStmt in project soot by Sable.

the class DexArrayInitDetector method constructArrayInitializations.

/**
 * Constructs packed array initializations from the individual element
 * assignments in the given body
 * @param body The body in which to look for element assignments
 */
public void constructArrayInitializations(Body body) {
    // Find an array construction followed by consecutive element
    // assignments
    Unit arrayInitStmt = null;
    List<Value> arrayValues = null;
    Set<Unit> curIgnoreUnits = null;
    int arraySize = 0;
    for (Unit u : body.getUnits()) {
        if (!(u instanceof AssignStmt)) {
            arrayValues = null;
            continue;
        }
        AssignStmt assignStmt = (AssignStmt) u;
        if (assignStmt.getRightOp() instanceof NewArrayExpr) {
            NewArrayExpr newArrayExp = (NewArrayExpr) assignStmt.getRightOp();
            if (newArrayExp.getSize() instanceof IntConstant) {
                IntConstant intConst = (IntConstant) newArrayExp.getSize();
                arrayValues = new ArrayList<Value>();
                arraySize = intConst.value;
                curIgnoreUnits = new HashSet<Unit>();
            } else {
                arrayValues = null;
            }
        } else if (assignStmt.getLeftOp() instanceof ArrayRef && assignStmt.getRightOp() instanceof IntConstant && /*NumericConstant*/
        arrayValues != null) {
            ArrayRef aref = (ArrayRef) assignStmt.getLeftOp();
            if (aref.getIndex() instanceof IntConstant) {
                IntConstant intConst = (IntConstant) aref.getIndex();
                if (intConst.value == arrayValues.size()) {
                    arrayValues.add(assignStmt.getRightOp());
                    if (intConst.value == 0)
                        arrayInitStmt = u;
                    else if (intConst.value == arraySize - 1) {
                        curIgnoreUnits.add(u);
                        checkAndSave(arrayInitStmt, arrayValues, arraySize, curIgnoreUnits);
                        arrayValues = null;
                    } else
                        curIgnoreUnits.add(u);
                } else {
                    arrayValues = null;
                }
            } else {
                arrayValues = null;
            }
        } else {
            arrayValues = null;
        }
    }
}
Also used : ArrayRef(soot.jimple.ArrayRef) NewArrayExpr(soot.jimple.NewArrayExpr) AssignStmt(soot.jimple.AssignStmt) Value(soot.Value) IntConstant(soot.jimple.IntConstant) Unit(soot.Unit)

Example 29 with AssignStmt

use of soot.jimple.AssignStmt in project soot by Sable.

the class SootMethodRefImpl method createUnresolvedErrorMethod.

/**
 * Creates a method body that throws an "unresolved compilation error" message
 *
 * @param declaringClass
 *            The class that was supposed to contain the method
 * @return The created SootMethod
 */
private SootMethod createUnresolvedErrorMethod(SootClass declaringClass) {
    SootMethod m = Scene.v().makeSootMethod(name, parameterTypes, returnType, isStatic() ? Modifier.STATIC : 0);
    // we don't know who will be calling us
    int modifiers = Modifier.PUBLIC;
    if (isStatic())
        modifiers |= Modifier.STATIC;
    m.setModifiers(modifiers);
    JimpleBody body = Jimple.v().newBody(m);
    m.setActiveBody(body);
    final LocalGenerator lg = new LocalGenerator(body);
    // For producing valid Jimple code, we need to access all parameters.
    // Otherwise, methods like "getThisLocal()" will fail.
    body.insertIdentityStmts(declaringClass);
    // exc = new Error
    RefType runtimeExceptionType = RefType.v("java.lang.Error");
    NewExpr newExpr = Jimple.v().newNewExpr(runtimeExceptionType);
    Local exceptionLocal = lg.generateLocal(runtimeExceptionType);
    AssignStmt assignStmt = Jimple.v().newAssignStmt(exceptionLocal, newExpr);
    body.getUnits().add(assignStmt);
    // exc.<init>(message)
    SootMethodRef cref = Scene.v().makeConstructorRef(runtimeExceptionType.getSootClass(), Collections.<Type>singletonList(RefType.v("java.lang.String")));
    SpecialInvokeExpr constructorInvokeExpr = Jimple.v().newSpecialInvokeExpr(exceptionLocal, cref, StringConstant.v("Unresolved compilation error: Method " + getSignature() + " does not exist!"));
    InvokeStmt initStmt = Jimple.v().newInvokeStmt(constructorInvokeExpr);
    body.getUnits().insertAfter(initStmt, assignStmt);
    // throw exc
    body.getUnits().insertAfter(Jimple.v().newThrowStmt(exceptionLocal), initStmt);
    return declaringClass.getOrAddMethod(m);
}
Also used : LocalGenerator(soot.javaToJimple.LocalGenerator) InvokeStmt(soot.jimple.InvokeStmt) AssignStmt(soot.jimple.AssignStmt) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) NewExpr(soot.jimple.NewExpr) JimpleBody(soot.jimple.JimpleBody)

Example 30 with AssignStmt

use of soot.jimple.AssignStmt 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

AssignStmt (soot.jimple.AssignStmt)83 Local (soot.Local)50 Value (soot.Value)44 Unit (soot.Unit)40 Type (soot.Type)28 Stmt (soot.jimple.Stmt)24 InvokeExpr (soot.jimple.InvokeExpr)20 RefType (soot.RefType)19 ArrayRef (soot.jimple.ArrayRef)19 ArrayType (soot.ArrayType)17 CastExpr (soot.jimple.CastExpr)17 InvokeStmt (soot.jimple.InvokeStmt)17 ArrayList (java.util.ArrayList)15 IdentityStmt (soot.jimple.IdentityStmt)15 DefinitionStmt (soot.jimple.DefinitionStmt)13 FieldRef (soot.jimple.FieldRef)13 InstanceFieldRef (soot.jimple.InstanceFieldRef)13 IntConstant (soot.jimple.IntConstant)13 ReturnStmt (soot.jimple.ReturnStmt)13 HashSet (java.util.HashSet)12