Search in sources :

Example 61 with SootClass

use of soot.SootClass 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 62 with SootClass

use of soot.SootClass in project soot by Sable.

the class MethodInvocationInstruction method getSootMethodRef.

/**
 * Return the SootMethodRef for the invoked method.
 *
 * @param invType The invocation type
 */
private SootMethodRef getSootMethodRef(InvocationType invType) {
    if (methodRef != null)
        return methodRef;
    MethodReference mItem = (MethodReference) ((ReferenceInstruction) instruction).getReference();
    String tItem = mItem.getDefiningClass();
    String className = tItem;
    if (className.startsWith("[")) {
        className = "java.lang.Object";
    } else {
        className = dottedClassName(tItem);
    }
    SootClass sc = SootResolver.v().makeClassRef(className);
    if (invType == InvocationType.Interface && sc.isPhantom())
        sc.setModifiers(sc.getModifiers() | Modifier.INTERFACE);
    String methodName = mItem.getName();
    Type returnType = DexType.toSoot(mItem.getReturnType());
    List<Type> parameterTypes = new ArrayList<Type>();
    List<? extends CharSequence> paramTypes = mItem.getParameterTypes();
    if (paramTypes != null)
        for (CharSequence type : paramTypes) parameterTypes.add(DexType.toSoot(type.toString()));
    methodRef = Scene.v().makeMethodRef(sc, methodName, parameterTypes, returnType, invType == InvocationType.Static);
    return methodRef;
}
Also used : RefType(soot.RefType) Type(soot.Type) DexType(soot.dexpler.DexType) ArrayList(java.util.ArrayList) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) SootClass(soot.SootClass)

Example 63 with SootClass

use of soot.SootClass 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)

Example 64 with SootClass

use of soot.SootClass in project soot by Sable.

the class BadFields method internalTransform.

protected void internalTransform(String phaseName, Map<String, String> options) {
    lastClass = null;
    for (Iterator<SootClass> clIt = Scene.v().getApplicationClasses().iterator(); clIt.hasNext(); ) {
        final SootClass cl = clIt.next();
        currentClass = cl;
        handleClass(cl);
        for (Iterator<SootMethod> it = cl.methodIterator(); it.hasNext(); ) {
            handleMethod(it.next());
        }
    }
    Scene.v().setCallGraph(Scene.v().internalMakeCallGraph());
}
Also used : SootMethod(soot.SootMethod) SootClass(soot.SootClass)

Example 65 with SootClass

use of soot.SootClass in project soot by Sable.

the class UnitThrowAnalysisTest method testJInvokeStmt.

@Ignore("Fails")
@Test
public void testJInvokeStmt() {
    List voidList = new ArrayList();
    Stmt s = Jimple.v().newInvokeStmt(Jimple.v().newVirtualInvokeExpr(Jimple.v().newLocal("local1", RefType.v("java.lang.Object")), Scene.v().makeMethodRef(Scene.v().getSootClass("java.lang.Object"), "wait", voidList, VoidType.v(), false), voidList));
    ExceptionHashSet expectedRep = new ExceptionHashSet(utility.VM_AND_RESOLVE_METHOD_ERRORS_REP);
    expectedRep.add(utility.NULL_POINTER_EXCEPTION);
    ExceptionHashSet expectedCatch = new ExceptionHashSet(utility.VM_AND_RESOLVE_METHOD_ERRORS_PLUS_SUPERTYPES);
    expectedCatch.add(utility.NULL_POINTER_EXCEPTION);
    expectedCatch.add(utility.RUNTIME_EXCEPTION);
    expectedCatch.add(utility.EXCEPTION);
    assertTrue(ExceptionTestUtility.sameMembers(expectedRep, Collections.EMPTY_SET, immaculateAnalysis.mightThrow(s)));
    assertEquals(expectedCatch, utility.catchableSubset(immaculateAnalysis.mightThrow(s)));
    assertTrue(ExceptionTestUtility.sameMembers(utility.ALL_THROWABLES_REP, Collections.EMPTY_SET, unitAnalysis.mightThrow(s)));
    assertEquals(utility.ALL_TEST_THROWABLES, utility.catchableSubset(unitAnalysis.mightThrow(s)));
    SootClass bogusClass = new SootClass("BogusClass");
    bogusClass.addMethod(Scene.v().makeSootMethod("emptyMethod", voidList, VoidType.v(), Modifier.STATIC));
    s = Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(Scene.v().makeMethodRef(bogusClass, "emptyMethod", voidList, VoidType.v(), true), voidList));
    assertTrue(ExceptionTestUtility.sameMembers(utility.ALL_ERRORS_REP, Collections.EMPTY_SET, immaculateAnalysis.mightThrow(s)));
    assertEquals(utility.ALL_TEST_ERRORS_PLUS_SUPERTYPES, utility.catchableSubset(immaculateAnalysis.mightThrow(s)));
    assertTrue(ExceptionTestUtility.sameMembers(utility.ALL_THROWABLES_REP, Collections.EMPTY_SET, unitAnalysis.mightThrow(s)));
    assertEquals(utility.ALL_TEST_THROWABLES, utility.catchableSubset(unitAnalysis.mightThrow(s)));
}
Also used : ExceptionHashSet(soot.toolkits.exceptions.ExceptionTestUtility.ExceptionHashSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SootClass(soot.SootClass) ThrowStmt(soot.jimple.ThrowStmt) IfStmt(soot.jimple.IfStmt) Stmt(soot.jimple.Stmt) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

SootClass (soot.SootClass)194 SootMethod (soot.SootMethod)99 RefType (soot.RefType)69 ArrayList (java.util.ArrayList)60 Type (soot.Type)57 VoidType (soot.VoidType)33 ArrayType (soot.ArrayType)32 Iterator (java.util.Iterator)29 BooleanType (soot.BooleanType)29 DoubleType (soot.DoubleType)29 LongType (soot.LongType)29 Value (soot.Value)29 FloatType (soot.FloatType)28 Local (soot.Local)27 SootField (soot.SootField)27 List (java.util.List)26 CharType (soot.CharType)26 IntType (soot.IntType)26 ByteType (soot.ByteType)25 PrimType (soot.PrimType)23