Search in sources :

Example 61 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class MultiRunStatementsFinder method findMultiCalledMethodsIntra.

private void findMultiCalledMethodsIntra(Set<SootMethod> multiCalledMethods, CallGraph callGraph) {
    Iterator<Unit> it = multiRunStatements.iterator();
    while (it.hasNext()) {
        Stmt stmt = (Stmt) it.next();
        if (stmt.containsInvokeExpr()) {
            InvokeExpr invokeExpr = stmt.getInvokeExpr();
            List<SootMethod> targetList = new ArrayList<SootMethod>();
            SootMethod method = invokeExpr.getMethod();
            if (invokeExpr instanceof StaticInvokeExpr) {
                targetList.add(method);
            } else if (invokeExpr instanceof InstanceInvokeExpr) {
                if (method.isConcrete() && !method.getDeclaringClass().isLibraryClass()) {
                    TargetMethodsFinder tmd = new TargetMethodsFinder();
                    targetList = tmd.find(stmt, callGraph, true, true);
                }
            }
            if (targetList != null) {
                Iterator<SootMethod> iterator = targetList.iterator();
                while (iterator.hasNext()) {
                    SootMethod obj = iterator.next();
                    if (!obj.isNative()) {
                        multiCalledMethods.add(obj);
                    }
                }
            }
        }
    }
}
Also used : StaticInvokeExpr(soot.jimple.StaticInvokeExpr) TargetMethodsFinder(soot.jimple.toolkits.thread.mhp.TargetMethodsFinder) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) ArrayList(java.util.ArrayList) SootMethod(soot.SootMethod) InstanceInvokeExpr(soot.jimple.InstanceInvokeExpr) Unit(soot.Unit) Stmt(soot.jimple.Stmt)

Example 62 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class XMLPrinter method printXMLTo.

private void printXMLTo(SootClass cl, PrintWriter out) {
    root = new XMLRoot();
    XMLNode xmlRootNode = null;
    XMLNode xmlHistoryNode = null;
    XMLNode xmlClassNode = null;
    XMLNode xmlTempNode = null;
    // Print XML class output
    {
        // add header nodes
        xmlRootNode = root.addElement("jil");
        // add history node
        // TODO: grab the software version and command line
        String cmdlineStr = "";
        for (String element : Main.v().cmdLineArgs) {
            cmdlineStr += element + " ";
        }
        String dateStr = new Date().toString();
        xmlHistoryNode = xmlRootNode.addChild("history");
        xmlHistoryNode.addAttribute("created", dateStr);
        xmlHistoryNode.addChild("soot", new String[] { "version", "command", "timestamp" }, new String[] { Main.v().versionString, cmdlineStr.trim(), dateStr });
        // add class root node
        xmlClassNode = xmlRootNode.addChild("class", new String[] { "name" }, new String[] { Scene.v().quotedNameOf(cl.getName()).toString() });
        if (cl.getPackageName().length() > 0)
            xmlClassNode.addAttribute("package", cl.getPackageName());
        if (cl.hasSuperclass())
            xmlClassNode.addAttribute("extends", Scene.v().quotedNameOf(cl.getSuperclass().getName()).toString());
        // add modifiers subnode
        xmlTempNode = xmlClassNode.addChild("modifiers");
        StringTokenizer st = new StringTokenizer(Modifier.toString(cl.getModifiers()));
        while (st.hasMoreTokens()) xmlTempNode.addChild("modifier", new String[] { "name" }, new String[] { st.nextToken() + "" });
        xmlTempNode.addAttribute("count", xmlTempNode.getNumberOfChildren() + "");
    }
    // Print interfaces
    {
        xmlTempNode = xmlClassNode.addChild("interfaces", "", new String[] { "count" }, new String[] { cl.getInterfaceCount() + "" });
        Iterator<SootClass> interfaceIt = cl.getInterfaces().iterator();
        if (interfaceIt.hasNext()) {
            while (interfaceIt.hasNext()) xmlTempNode.addChild("implements", "", new String[] { "class" }, new String[] { Scene.v().quotedNameOf(interfaceIt.next().getName()).toString() });
        }
    }
    // Print fields
    {
        xmlTempNode = xmlClassNode.addChild("fields", "", new String[] { "count" }, new String[] { cl.getFieldCount() + "" });
        Iterator<SootField> fieldIt = cl.getFields().iterator();
        if (fieldIt.hasNext()) {
            int i = 0;
            while (fieldIt.hasNext()) {
                SootField f = fieldIt.next();
                if (f.isPhantom())
                    continue;
                String type = f.getType().toString();
                String name = f.getName().toString();
                // add the field node
                XMLNode xmlFieldNode = xmlTempNode.addChild("field", "", new String[] { "id", "name", "type" }, new String[] { (i++) + "", name, type });
                XMLNode xmlModifiersNode = xmlFieldNode.addChild("modifiers");
                StringTokenizer st = new StringTokenizer(Modifier.toString(f.getModifiers()));
                while (st.hasMoreTokens()) xmlModifiersNode.addChild("modifier", new String[] { "name" }, new String[] { st.nextToken() + "" });
                xmlModifiersNode.addAttribute("count", xmlModifiersNode.getNumberOfChildren() + "");
            }
        }
    }
    // Print methods
    {
        Iterator<SootMethod> methodIt = cl.methodIterator();
        setXMLNode(xmlClassNode.addChild("methods", new String[] { "count" }, new String[] { cl.getMethodCount() + "" }));
        while (methodIt.hasNext()) {
            SootMethod method = methodIt.next();
            if (method.isPhantom())
                continue;
            if (!Modifier.isAbstract(method.getModifiers()) && !Modifier.isNative(method.getModifiers())) {
                if (!method.hasActiveBody())
                    throw new RuntimeException("method " + method.getName() + " has no active body!");
                else
                    printTo(method.getActiveBody(), out);
            }
        }
    }
    out.println(toString());
}
Also used : StringTokenizer(java.util.StringTokenizer) Iterator(java.util.Iterator) SootMethod(soot.SootMethod) SootField(soot.SootField) Date(java.util.Date)

Example 63 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class SuperFirstStmtHandler method getUniqueName.

/*
	 * Check the sootClass that it doesnt have a name we have suggested ALSO
	 * VERY IMPORTANT TO CHECK THE NAMES IN THE SOOTMETHODSADDED Variable since
	 * these will be added to this sootclass by the PackManager
	 */
private String getUniqueName() {
    String toReturn = "preInit";
    int counter = 0;
    List methodList = originalSootClass.getMethods();
    // havent found the name
    boolean done = false;
    while (!done) {
        // as long as name not found
        // assume name found
        done = true;
        Iterator it = methodList.iterator();
        while (it.hasNext()) {
            Object temp = it.next();
            if (temp instanceof SootMethod) {
                SootMethod method = (SootMethod) temp;
                String name = method.getName();
                if (toReturn.compareTo(name) == 0) {
                    // method exists with this name so change the name
                    counter++;
                    toReturn = "preInit" + counter;
                    // name was not found
                    done = false;
                    // breaks the inner while since the name has been
                    break;
                // changed
                }
            } else
                throw new DecompilationException("SootClass returned a non SootMethod method");
        }
        // if we get here this means that the orignal names are different
        // check the to be added names also
        it = G.v().SootMethodsAdded.iterator();
        while (it.hasNext()) {
            // are sure its a sootMethod
            SootMethod method = (SootMethod) it.next();
            String name = method.getName();
            if (toReturn.compareTo(name) == 0) {
                // method exists with this name so change the name
                counter++;
                toReturn = "preInit" + counter;
                // name was not found
                done = false;
                // breaks the inner while since the name has been
                break;
            // changed
            }
        }
    }
    // end outer while
    return toReturn;
}
Also used : Iterator(java.util.Iterator) SootMethod(soot.SootMethod) List(java.util.List) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException)

Example 64 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class SuperFirstStmtHandler method getProperCasting.

public Value getProperCasting(Type tempType, DVirtualInvokeExpr tempInvokeExpr) {
    if (tempType instanceof RefType) {
        // System.out.println("This is a reftype:"+tempType);
        return new GCastExpr(tempInvokeExpr, tempType);
    } else if (tempType instanceof PrimType) {
        PrimType t = (PrimType) tempType;
        if (t == BooleanType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Boolean"));
            // booleanValue
            SootMethod tempMethod = Scene.v().makeSootMethod("booleanValue", new ArrayList(), BooleanType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Boolean"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == ByteType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Byte"));
            // byteValue
            SootMethod tempMethod = Scene.v().makeSootMethod("byteValue", new ArrayList(), ByteType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Byte"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == CharType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Character"));
            // charValue
            SootMethod tempMethod = Scene.v().makeSootMethod("charValue", new ArrayList(), CharType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Character"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == DoubleType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Double"));
            // doubleValue
            SootMethod tempMethod = Scene.v().makeSootMethod("doubleValue", new ArrayList(), DoubleType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Double"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == FloatType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Float"));
            // floatValue
            SootMethod tempMethod = Scene.v().makeSootMethod("floatValue", new ArrayList(), FloatType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Float"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == IntType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Integer"));
            // intValue
            SootMethod tempMethod = Scene.v().makeSootMethod("intValue", new ArrayList(), IntType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Integer"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == LongType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Long"));
            // longValue
            SootMethod tempMethod = Scene.v().makeSootMethod("longValue", new ArrayList(), LongType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Long"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else if (t == ShortType.v()) {
            Value tempExpr = new GCastExpr(tempInvokeExpr, RefType.v("java.lang.Short"));
            // shortValue
            SootMethod tempMethod = Scene.v().makeSootMethod("shortValue", new ArrayList(), ShortType.v());
            tempMethod.setDeclaringClass(new SootClass("java.lang.Short"));
            SootMethodRef tempMethodRef = tempMethod.makeRef();
            return new DVirtualInvokeExpr(tempExpr, tempMethodRef, new ArrayList(), new HashSet<Object>());
        } else {
            throw new DecompilationException("Unhandle primType:" + tempType);
        }
    } else {
        throw new DecompilationException("The type:" + tempType + " was not a reftye or primtype. PLEASE REPORT.");
    }
}
Also used : SootMethodRef(soot.SootMethodRef) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException) SootClass(soot.SootClass) RefType(soot.RefType) DVirtualInvokeExpr(soot.dava.internal.javaRep.DVirtualInvokeExpr) Value(soot.Value) PrimType(soot.PrimType) SootMethod(soot.SootMethod) GCastExpr(soot.grimp.internal.GCastExpr) HashSet(java.util.HashSet)

Example 65 with SootMethod

use of soot.SootMethod in project soot by Sable.

the class ClassResolver method createConstructorDecl.

/**
 * Constructor Declaration Creation
 */
private void createConstructorDecl(polyglot.ast.ConstructorDecl constructor) {
    String name = "<init>";
    ArrayList parameters = createParameters(constructor);
    ArrayList<SootClass> exceptions = createExceptions(constructor);
    soot.SootMethod sootMethod = createSootConstructor(name, constructor.flags(), parameters, exceptions);
    finishProcedure(constructor, sootMethod);
}
Also used : SootMethod(soot.SootMethod) ArrayList(java.util.ArrayList) SootClass(soot.SootClass)

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