Search in sources :

Example 1 with DecompilationException

use of soot.dava.DecompilationException in project soot by Sable.

the class ConstantFieldValueFinder method valuesForPrimTypeFields.

/*
	 * This method gives values to all the fields in all the classes if they can be determined statically
	 * We only care about fields which have primitive types
	 */
private void valuesForPrimTypeFields() {
    // go through all the classes
    Iterator classIt = appClasses.iterator();
    while (classIt.hasNext()) {
        SootClass s = (SootClass) classIt.next();
        debug("\nvaluesforPrimTypeFields", "Processing class " + s.getName());
        String declaringClass = s.getName();
        Iterator fieldIt = s.getFields().iterator();
        while (fieldIt.hasNext()) {
            SootField f = (SootField) fieldIt.next();
            String fieldName = f.getName();
            Type fieldType = f.getType();
            if (!(fieldType instanceof PrimType))
                continue;
            String combined = declaringClass + combiner + fieldName;
            classNameFieldNameToSootFieldMapping.put(combined, f);
            Object value = null;
            // check for constant value tags
            if (fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")) {
                double val = ((DoubleConstantValueTag) f.getTag("DoubleConstantValueTag")).getDoubleValue();
                value = new Double(val);
            } else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")) {
                float val = ((FloatConstantValueTag) f.getTag("FloatConstantValueTag")).getFloatValue();
                value = new Float(val);
            } else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")) {
                long val = ((LongConstantValueTag) f.getTag("LongConstantValueTag")).getLongValue();
                value = new Long(val);
            } else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                value = new Integer(val);
            } else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                if (val == 0)
                    value = new Boolean(false);
                else
                    value = new Boolean(true);
            } else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                value = new Integer(val);
            }
            // if there was a constant value tag we have its value now
            if (value != null) {
                debug("TAGGED value found for field: " + combined);
                primTypeFieldValueToUse.put(combined, value);
                // continue with next field
                continue;
            }
            // see if the field was never assigned in which case it gets default values
            Object temp = fieldToValues.get(combined);
            if (temp == null) {
                if (fieldType instanceof DoubleType)
                    value = new Double(0);
                else if (fieldType instanceof FloatType)
                    value = new Float(0);
                else if (fieldType instanceof LongType)
                    value = new Long(0);
                else if (fieldType instanceof BooleanType)
                    value = new Boolean(false);
                else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) || fieldType instanceof CharType) {
                    value = new Integer(0);
                } else
                    throw new DecompilationException("Unknown primitive type...please report to developer");
                primTypeFieldValueToUse.put(combined, value);
                debug("DEFAULT value for field: " + combined);
                // continue with next field
                continue;
            }
            // havent got a tag with value and havent use default since SOME method did define the field atleast once
            // there was some value assigned!!!!!!!!!
            debug("CHECKING USER ASSIGNED VALUES FOR: " + combined);
            ArrayList values = (ArrayList) temp;
            // check if they are all constants and that too the same constant
            Iterator it = values.iterator();
            NumericConstant tempConstant = null;
            while (it.hasNext()) {
                Value val = (Value) it.next();
                if (!(val instanceof NumericConstant)) {
                    tempConstant = null;
                    debug("Not numeric constant hence giving up");
                    break;
                }
                if (tempConstant == null) {
                    tempConstant = (NumericConstant) val;
                } else {
                    // check that this value is the same as previous
                    if (!tempConstant.equals(val)) {
                        tempConstant = null;
                        break;
                    }
                }
            }
            if (tempConstant == null) {
                // continue with next field cant do anything about this one
                continue;
            }
            if (tempConstant instanceof LongConstant) {
                Long tempVal = new Long(((LongConstant) tempConstant).value);
                if (tempVal.compareTo(new Long(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof DoubleConstant) {
                Double tempVal = new Double(((DoubleConstant) tempConstant).value);
                if (tempVal.compareTo(new Double(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof FloatConstant) {
                Float tempVal = new Float(((FloatConstant) tempConstant).value);
                if (tempVal.compareTo(new Float(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof IntConstant) {
                Integer tempVal = new Integer(((IntConstant) tempConstant).value);
                if (tempVal.compareTo(new Integer(0)) == 0) {
                    SootField tempField = classNameFieldNameToSootFieldMapping.get(combined);
                    if (tempField.getType() instanceof BooleanType) {
                        primTypeFieldValueToUse.put(combined, new Boolean(false));
                    // System.out.println("puttingvalue false for"+combined);
                    } else {
                        primTypeFieldValueToUse.put(combined, tempVal);
                    // System.out.println("puttingvalue 0 for"+combined);
                    }
                } else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else {
                throw new DecompilationException("Un handled Numberic Constant....report to programmer");
            }
        }
    // all fields of the class
    }
// all classes
}
Also used : DoubleConstant(soot.jimple.DoubleConstant) LongType(soot.LongType) FloatConstant(soot.jimple.FloatConstant) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException) ByteType(soot.ByteType) FloatType(soot.FloatType) IntType(soot.IntType) Iterator(java.util.Iterator) PrimType(soot.PrimType) LongConstantValueTag(soot.tagkit.LongConstantValueTag) IntConstant(soot.jimple.IntConstant) LongConstant(soot.jimple.LongConstant) ShortType(soot.ShortType) BooleanType(soot.BooleanType) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) SootClass(soot.SootClass) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) CharType(soot.CharType) LongType(soot.LongType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) PrimType(soot.PrimType) DoubleType(soot.DoubleType) NumericConstant(soot.jimple.NumericConstant) Value(soot.Value) SootField(soot.SootField) CharType(soot.CharType)

Example 2 with DecompilationException

use of soot.dava.DecompilationException in project soot by Sable.

the class IfElseSplitter method outASTIfElseNode.

public void outASTIfElseNode(ASTIfElseNode node) {
    // if some pattern has already matched cant do another one in this go
    if (transform)
        return;
    List<Object> subBodies = node.get_SubBodies();
    if (subBodies.size() != 2)
        throw new DecompilationException("IfelseNode without two subBodies. report to developer");
    List<Object> ifBody = (List<Object>) subBodies.get(0);
    List<Object> elseBody = (List<Object>) subBodies.get(1);
    boolean patternMatched = tryBodyPattern(ifBody, node.get_Label(), elseBody);
    List<Object> newIfBody = null;
    List<Object> outerScopeBody = null;
    boolean negateIfCondition = false;
    if (patternMatched) {
        if (DEBUG)
            System.out.println("First pattern matched");
        newIfBody = ifBody;
        outerScopeBody = elseBody;
        negateIfCondition = false;
    } else {
        patternMatched = tryBodyPattern(elseBody, node.get_Label(), ifBody);
        if (patternMatched) {
            if (DEBUG)
                System.out.println("Second pattern matched");
            newIfBody = elseBody;
            outerScopeBody = ifBody;
            negateIfCondition = true;
        }
    }
    // if at this point newIfBody and outerScopeBody are non null we got ourselves a transformation :)
    if (newIfBody != null && outerScopeBody != null) {
        ASTCondition cond = node.get_Condition();
        if (negateIfCondition)
            cond.flip();
        ASTIfNode newNode = new ASTIfNode(node.get_Label(), cond, newIfBody);
        if (DEBUG) {
            System.out.println("New IF Node is: " + newNode.toString());
            System.out.println("Outer scope body list is:\n");
            for (int i = 0; i < outerScopeBody.size(); i++) System.out.println("\n\n " + outerScopeBody.get(i).toString());
        }
        ASTParentNodeFinder finder = new ASTParentNodeFinder();
        methodNode.apply(finder);
        Object returned = finder.getParentOf(node);
        if (returned == null) {
            // coundnt find parent so cant do anything
            return;
        }
        /*
			 * Setting globals since everything is ready for transformation
			 * BECAUSE we cant modify the parent here we are going to do some 
			 * bad coding style
			 * store the information needed for this into globals
			 * set a flag
			 * and the outASTMethod checks for this
			 */
        parent = (ASTNode) returned;
        toReplace = node;
        toInsert = newNode;
        bodyAfterInsert = outerScopeBody;
        transform = true;
    }
}
Also used : ASTIfNode(soot.dava.internal.AST.ASTIfNode) ASTParentNodeFinder(soot.dava.toolkits.base.AST.traversals.ASTParentNodeFinder) DecompilationException(soot.dava.DecompilationException) List(java.util.List) ASTCondition(soot.dava.internal.AST.ASTCondition)

Example 3 with DecompilationException

use of soot.dava.DecompilationException 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 4 with DecompilationException

use of soot.dava.DecompilationException 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 5 with DecompilationException

use of soot.dava.DecompilationException in project soot by Sable.

the class SuperFirstStmtHandler method createStmtAccordingToType.

public AugmentedStmt createStmtAccordingToType(Type tempType, Value tempVal, Local newLocal, SootMethodRef getMethodRef) {
    if (tempType instanceof RefType) {
        return createAugmentedStmtToAdd(newLocal, getMethodRef, tempVal);
    } else if (tempType instanceof PrimType) {
        // The value is a primitive type
        // create wrapper object new Integer(tempVal)
        PrimType t = (PrimType) tempType;
        // create ArgList to be sent to DNewInvokeExpr constructor
        ArrayList argList = new ArrayList();
        argList.add(tempVal);
        // LongType, ShortType
        if (t == BooleanType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(IntType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Boolean"), makeMethodRef("Boolean", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == ByteType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(ByteType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Byte"), makeMethodRef("Byte", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == CharType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(CharType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Character"), makeMethodRef("Character", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == DoubleType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(DoubleType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Double"), makeMethodRef("Double", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == FloatType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(FloatType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Float"), makeMethodRef("Float", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == IntType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(IntType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Integer"), makeMethodRef("Integer", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == LongType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(LongType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Long"), makeMethodRef("Long", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else if (t == ShortType.v()) {
            // create TypeList to be sent to makeMethodRef
            ArrayList typeList = new ArrayList();
            typeList.add(ShortType.v());
            DNewInvokeExpr argForStore = new DNewInvokeExpr(RefType.v("java.lang.Short"), makeMethodRef("Short", typeList), argList);
            return createAugmentedStmtToAdd(newLocal, getMethodRef, argForStore);
        } else {
            throw new DecompilationException("UNHANDLED PRIMTYPE:" + tempType);
        }
    } else // end of primitivetypes
    {
        throw new DecompilationException("The type:" + tempType + " is neither a reftype or a primtype");
    }
}
Also used : RefType(soot.RefType) DNewInvokeExpr(soot.dava.internal.javaRep.DNewInvokeExpr) ArrayList(java.util.ArrayList) PrimType(soot.PrimType) DecompilationException(soot.dava.DecompilationException)

Aggregations

DecompilationException (soot.dava.DecompilationException)12 ArrayList (java.util.ArrayList)8 List (java.util.List)6 PrimType (soot.PrimType)6 RefType (soot.RefType)5 SootClass (soot.SootClass)5 Value (soot.Value)5 Iterator (java.util.Iterator)4 BooleanType (soot.BooleanType)4 ByteType (soot.ByteType)4 CharType (soot.CharType)4 DoubleType (soot.DoubleType)4 FloatType (soot.FloatType)4 IntType (soot.IntType)4 LongType (soot.LongType)4 ShortType (soot.ShortType)4 SootMethod (soot.SootMethod)4 SootMethodRef (soot.SootMethodRef)4 Type (soot.Type)4 AugmentedStmt (soot.dava.internal.asg.AugmentedStmt)4