Search in sources :

Example 16 with Body

use of soot.Body in project soot by Sable.

the class JimpleBasedInterproceduralCFG method initializeUnitToOwner.

public void initializeUnitToOwner(SootMethod m) {
    if (m.hasActiveBody()) {
        Body b = m.getActiveBody();
        PatchingChain<Unit> units = b.getUnits();
        for (Unit unit : units) {
            unitToOwner.put(unit, b);
        }
    }
}
Also used : Unit(soot.Unit) Body(soot.Body)

Example 17 with Body

use of soot.Body in project soot by Sable.

the class ReflectionTraceInfo method inferSource.

private Set<SootMethod> inferSource(String source, int lineNumber) {
    String className = source.substring(0, source.lastIndexOf("."));
    String methodName = source.substring(source.lastIndexOf(".") + 1);
    if (!Scene.v().containsClass(className)) {
        Scene.v().addBasicClass(className, SootClass.BODIES);
        Scene.v().loadBasicClasses();
        if (!Scene.v().containsClass(className)) {
            throw new RuntimeException("Trace file refers to unknown class: " + className);
        }
    }
    SootClass sootClass = Scene.v().getSootClass(className);
    Set<SootMethod> methodsWithRightName = new LinkedHashSet<SootMethod>();
    for (SootMethod m : sootClass.getMethods()) {
        if (m.isConcrete() && m.getName().equals(methodName)) {
            methodsWithRightName.add(m);
        }
    }
    if (methodsWithRightName.isEmpty()) {
        throw new RuntimeException("Trace file refers to unknown method with name " + methodName + " in Class " + className);
    } else if (methodsWithRightName.size() == 1) {
        return Collections.singleton(methodsWithRightName.iterator().next());
    } else {
        // more than one method with that name
        for (SootMethod sootMethod : methodsWithRightName) {
            if (coversLineNumber(lineNumber, sootMethod)) {
                return Collections.singleton(sootMethod);
            }
            if (sootMethod.isConcrete()) {
                if (!sootMethod.hasActiveBody())
                    sootMethod.retrieveActiveBody();
                Body body = sootMethod.getActiveBody();
                if (coversLineNumber(lineNumber, body)) {
                    return Collections.singleton(sootMethod);
                }
                for (Unit u : body.getUnits()) {
                    if (coversLineNumber(lineNumber, u)) {
                        return Collections.singleton(sootMethod);
                    }
                }
            }
        }
        // be conservative and return all method that we found
        return methodsWithRightName;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SootMethod(soot.SootMethod) SootClass(soot.SootClass) Unit(soot.Unit) Body(soot.Body)

Example 18 with Body

use of soot.Body 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 19 with Body

use of soot.Body in project soot by Sable.

the class ClassRenamer method internalTransform.

@Override
protected void internalTransform(String phaseName, Map<String, String> options) {
    if (isVerbose()) {
        logger.debug("Transforming Class Names...");
    }
    BodyBuilder.retrieveAllBodies();
    BodyBuilder.retrieveAllNames();
    final SootClass mainClass = getMainClassSafely();
    // iterate through application classes, rename classes with junk
    for (SootClass sootClass : Scene.v().getApplicationClasses()) {
        final String className = sootClass.getName();
        if (sootClass.equals(mainClass) || oldToNewClassNames.containsValue(className) || soot.jbco.Main.getWeight(phaseName, className) == 0) {
            continue;
        }
        String newClassName = oldToNewClassNames.get(className);
        if (newClassName == null) {
            newClassName = getNewName(getPackageName(className), className);
        }
        sootClass.setName(newClassName);
        RefType crt = RefType.v(newClassName);
        crt.setSootClass(sootClass);
        sootClass.setRefType(crt);
        sootClass.setResolvingLevel(SootClass.BODIES);
        // will this fix dangling classes?
        // scene.addRefType(sootClass.getType());
        newNameToClass.put(newClassName, sootClass);
        if (isVerbose()) {
            logger.info("\tRenaming " + className + " to " + newClassName);
        }
    }
    Scene.v().releaseActiveHierarchy();
    Scene.v().setFastHierarchy(new FastHierarchy());
    if (isVerbose()) {
        logger.info("\r\tUpdating bytecode class references");
    }
    for (SootClass sootClass : Scene.v().getApplicationClasses()) {
        for (SootMethod sootMethod : sootClass.getMethods()) {
            if (!sootMethod.isConcrete()) {
                continue;
            }
            if (isVerbose()) {
                logger.info("\t\t" + sootMethod.getSignature());
            }
            Body aBody;
            try {
                aBody = sootMethod.getActiveBody();
            } catch (Exception e) {
                continue;
            }
            for (Unit u : aBody.getUnits()) {
                for (ValueBox vb : u.getUseAndDefBoxes()) {
                    Value v = vb.getValue();
                    if (v instanceof ClassConstant) {
                        ClassConstant constant = (ClassConstant) v;
                        RefType type = (RefType) constant.toSootType();
                        RefType updatedType = type.getSootClass().getType();
                        vb.setValue(ClassConstant.fromType(updatedType));
                    } else if (v instanceof Expr) {
                        if (v instanceof CastExpr) {
                            CastExpr castExpr = (CastExpr) v;
                            updateType(castExpr.getCastType());
                        } else if (v instanceof InstanceOfExpr) {
                            InstanceOfExpr instanceOfExpr = (InstanceOfExpr) v;
                            updateType(instanceOfExpr.getCheckType());
                        }
                    } else if (v instanceof Ref) {
                        updateType(v.getType());
                    }
                }
            }
        }
    }
    Scene.v().releaseActiveHierarchy();
    Scene.v().setFastHierarchy(new FastHierarchy());
}
Also used : SootClass(soot.SootClass) Unit(soot.Unit) InstanceOfExpr(soot.jimple.InstanceOfExpr) RefType(soot.RefType) FastHierarchy(soot.FastHierarchy) Ref(soot.jimple.Ref) Expr(soot.jimple.Expr) InstanceOfExpr(soot.jimple.InstanceOfExpr) CastExpr(soot.jimple.CastExpr) ValueBox(soot.ValueBox) Value(soot.Value) CastExpr(soot.jimple.CastExpr) SootMethod(soot.SootMethod) Body(soot.Body) ClassConstant(soot.jimple.ClassConstant)

Example 20 with Body

use of soot.Body in project soot by Sable.

the class JimpleBody method clone.

/**
 * Clones the current body, making deep copies of the contents.
 */
@Override
public Object clone() {
    Body b = new JimpleBody(getMethod());
    b.importBodyContentsFrom(this);
    return b;
}
Also used : Body(soot.Body)

Aggregations

Body (soot.Body)57 Unit (soot.Unit)37 SootMethod (soot.SootMethod)32 Local (soot.Local)20 SootClass (soot.SootClass)19 Value (soot.Value)15 InvokeExpr (soot.jimple.InvokeExpr)14 StaticInvokeExpr (soot.jimple.StaticInvokeExpr)13 Type (soot.Type)12 Stmt (soot.jimple.Stmt)12 InstanceInvokeExpr (soot.jimple.InstanceInvokeExpr)11 JimpleBody (soot.jimple.JimpleBody)11 SpecialInvokeExpr (soot.jimple.SpecialInvokeExpr)11 RefType (soot.RefType)10 VoidType (soot.VoidType)10 VirtualInvokeExpr (soot.jimple.VirtualInvokeExpr)10 LinkedList (java.util.LinkedList)9 SootMethodRef (soot.SootMethodRef)9 ArrayList (java.util.ArrayList)8 PrimType (soot.PrimType)8