Search in sources :

Example 66 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class ClassResolver method createMethodDecl.

/**
 * Method Declaration Creation
 */
private void createMethodDecl(polyglot.ast.MethodDecl method) {
    String name = createName(method);
    // parameters
    ArrayList parameters = createParameters(method);
    // exceptions
    ArrayList<SootClass> exceptions = createExceptions(method);
    soot.SootMethod sootMethod = createSootMethod(name, method.flags(), method.returnType().type(), parameters, exceptions);
    finishProcedure(method, sootMethod);
}
Also used : SootMethod(soot.SootMethod) ArrayList(java.util.ArrayList) SootClass(soot.SootClass)

Example 67 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class ClassResolver method handleAssert.

/**
 * Handling for assert stmts - extra fields and methods are needed in the
 * Jimple
 */
private void handleAssert(polyglot.ast.ClassBody cBody) {
    // find any asserts in class body but not in inner class bodies
    AssertStmtChecker asc = new AssertStmtChecker();
    cBody.visit(asc);
    if (!asc.isHasAssert())
        return;
    // two extra fields
    // $assertionsDisabled field is added to the actual class where the
    // assert is found (even if its an inner class - interfaces cannot
    // have asserts stmts directly contained within them)
    String fieldName = "$assertionsDisabled";
    soot.Type fieldType = soot.BooleanType.v();
    if (!sootClass.declaresField(fieldName, fieldType)) {
        soot.SootField assertionsDisabledField = Scene.v().makeSootField(fieldName, fieldType, soot.Modifier.STATIC | soot.Modifier.FINAL);
        sootClass.addField(assertionsDisabledField);
        assertionsDisabledField.addTag(new soot.tagkit.SyntheticTag());
    }
    // class$ field is added to the outer most class if sootClass
    // containing the assert is inner - if the outer most class is
    // an interface - add instead to special interface anon class
    soot.SootClass addToClass = sootClass;
    while ((InitialResolver.v().getInnerClassInfoMap() != null) && (InitialResolver.v().getInnerClassInfoMap().containsKey(addToClass))) {
        addToClass = InitialResolver.v().getInnerClassInfoMap().get(addToClass).getOuterClass();
    }
    // this field is named after the outer class even if the outer
    // class is an interface and will be actually added to the
    // special interface anon class
    fieldName = "class$" + addToClass.getName().replaceAll(".", "$");
    if ((InitialResolver.v().getInterfacesList() != null) && (InitialResolver.v().getInterfacesList().contains(addToClass.getName()))) {
        addToClass = getSpecialInterfaceAnonClass(addToClass);
    }
    fieldType = soot.RefType.v("java.lang.Class");
    if (!addToClass.declaresField(fieldName, fieldType)) {
        soot.SootField classField = Scene.v().makeSootField(fieldName, fieldType, soot.Modifier.STATIC);
        addToClass.addField(classField);
        classField.addTag(new soot.tagkit.SyntheticTag());
    }
    // two extra methods
    // class$ method is added to the outer most class if sootClass
    // containing the assert is inner - if the outer most class is
    // an interface - add instead to special interface anon class
    String methodName = "class$";
    soot.Type methodRetType = soot.RefType.v("java.lang.Class");
    ArrayList paramTypes = new ArrayList();
    paramTypes.add(soot.RefType.v("java.lang.String"));
    // make meth
    soot.SootMethod sootMethod = Scene.v().makeSootMethod(methodName, paramTypes, methodRetType, soot.Modifier.STATIC);
    AssertClassMethodSource assertMSrc = new AssertClassMethodSource();
    sootMethod.setSource(assertMSrc);
    if (!addToClass.declaresMethod(methodName, paramTypes, methodRetType)) {
        addToClass.addMethod(sootMethod);
        sootMethod.addTag(new soot.tagkit.SyntheticTag());
    }
    // clinit method is added to actual class where assert is found
    // if the class already has a clinit method its method source is
    // informed of an assert
    methodName = "<clinit>";
    methodRetType = soot.VoidType.v();
    paramTypes = new ArrayList();
    // make meth
    sootMethod = Scene.v().makeSootMethod(methodName, paramTypes, methodRetType, soot.Modifier.STATIC);
    PolyglotMethodSource mSrc = new PolyglotMethodSource();
    mSrc.setJBB(InitialResolver.v().getJBBFactory().createJimpleBodyBuilder());
    mSrc.hasAssert(true);
    sootMethod.setSource(mSrc);
    if (!sootClass.declaresMethod(methodName, paramTypes, methodRetType)) {
        sootClass.addMethod(sootMethod);
    } else {
        ((soot.javaToJimple.PolyglotMethodSource) sootClass.getMethod(methodName, paramTypes, methodRetType).getSource()).hasAssert(true);
    }
}
Also used : SootMethod(soot.SootMethod) SootField(soot.SootField) ArrayList(java.util.ArrayList) SootClass(soot.SootClass)

Example 68 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class ConstantValueToInitializerTransformer method getOrCreateInitializer.

private SootMethod getOrCreateInitializer(SootClass sc, Set<SootField> alreadyInitialized) {
    SootMethod smInit;
    // Create a static initializer if we don't already have one
    smInit = sc.getMethodByNameUnsafe("<clinit>");
    if (smInit == null) {
        smInit = Scene.v().makeSootMethod("<clinit>", Collections.<Type>emptyList(), VoidType.v());
        smInit.setActiveBody(Jimple.v().newBody(smInit));
        sc.addMethod(smInit);
        smInit.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
    } else if (smInit.isPhantom())
        return null;
    else {
        smInit.retrieveActiveBody();
        // somewhere
        for (Unit u : smInit.getActiveBody().getUnits()) {
            Stmt s = (Stmt) u;
            for (ValueBox vb : s.getDefBoxes()) if (vb.getValue() instanceof FieldRef)
                alreadyInitialized.add(((FieldRef) vb.getValue()).getField());
        }
    }
    return smInit;
}
Also used : Type(soot.Type) VoidType(soot.VoidType) FieldRef(soot.jimple.FieldRef) ValueBox(soot.ValueBox) SootMethod(soot.SootMethod) Unit(soot.Unit) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) Stmt(soot.jimple.Stmt)

Example 69 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class ConstantValueToInitializerTransformer method transformClass.

public void transformClass(SootClass sc) {
    SootMethod smInit = null;
    Set<SootField> alreadyInitialized = new HashSet<SootField>();
    for (SootField sf : sc.getFields()) {
        // different constructors might assign different values.
        if (!sf.isStatic() || !sf.isFinal())
            continue;
        // generate a second one
        if (alreadyInitialized.contains(sf))
            continue;
        // Look for constant values
        for (Tag t : sf.getTags()) {
            Stmt initStmt = null;
            if (t instanceof DoubleConstantValueTag) {
                double value = ((DoubleConstantValueTag) t).getDoubleValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), DoubleConstant.v(value));
            } else if (t instanceof FloatConstantValueTag) {
                float value = ((FloatConstantValueTag) t).getFloatValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), FloatConstant.v(value));
            } else if (t instanceof IntegerConstantValueTag) {
                int value = ((IntegerConstantValueTag) t).getIntValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), IntConstant.v(value));
            } else if (t instanceof LongConstantValueTag) {
                long value = ((LongConstantValueTag) t).getLongValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), LongConstant.v(value));
            } else if (t instanceof StringConstantValueTag) {
                String value = ((StringConstantValueTag) t).getStringValue();
                initStmt = Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(sf.makeRef()), StringConstant.v(value));
            }
            if (initStmt != null) {
                if (smInit == null)
                    smInit = getOrCreateInitializer(sc, alreadyInitialized);
                if (smInit != null)
                    smInit.getActiveBody().getUnits().addFirst(initStmt);
            }
        }
    }
    if (smInit != null) {
        Chain<Unit> units = smInit.getActiveBody().getUnits();
        if (units.isEmpty() || !(units.getLast() instanceof ReturnVoidStmt))
            units.add(Jimple.v().newReturnVoidStmt());
    }
}
Also used : ReturnVoidStmt(soot.jimple.ReturnVoidStmt) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) Unit(soot.Unit) ReturnVoidStmt(soot.jimple.ReturnVoidStmt) Stmt(soot.jimple.Stmt) SootMethod(soot.SootMethod) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SootField(soot.SootField) Tag(soot.tagkit.Tag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) HashSet(java.util.HashSet) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag)

Example 70 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class PhaseDumper method dumpAllBodies.

private void dumpAllBodies(String baseName, boolean deleteGraphFiles) {
    List<SootClass> classes = Scene.v().getClasses(SootClass.BODIES);
    for (SootClass cls : classes) {
        for (Iterator m = cls.getMethods().iterator(); m.hasNext(); ) {
            SootMethod method = (SootMethod) m.next();
            if (method.hasActiveBody()) {
                Body body = method.getActiveBody();
                if (deleteGraphFiles) {
                    deleteOldGraphFiles(body, baseName);
                }
                dumpBody(body, baseName);
            }
        }
    }
}
Also used : Iterator(java.util.Iterator) SootMethod(soot.SootMethod) SootClass(soot.SootClass) Body(soot.Body)

Aggregations

SootMethod (soot.SootMethod)237 SootClass (soot.SootClass)95 RefType (soot.RefType)56 ArrayList (java.util.ArrayList)49 Type (soot.Type)47 Unit (soot.Unit)47 Value (soot.Value)36 Stmt (soot.jimple.Stmt)35 Test (org.junit.Test)34 Local (soot.Local)34 Body (soot.Body)32 VoidType (soot.VoidType)31 PrimType (soot.PrimType)28 SootField (soot.SootField)28 BooleanType (soot.BooleanType)26 Iterator (java.util.Iterator)23 DoubleType (soot.DoubleType)23 FloatType (soot.FloatType)23 LongType (soot.LongType)23 InvokeExpr (soot.jimple.InvokeExpr)23