Search in sources :

Example 1 with LongConstantValueTag

use of soot.tagkit.LongConstantValueTag in project soot by Sable.

the class SootClassBuilder method visitField.

@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    soot.Type type = AsmUtil.toJimpleType(desc);
    addDep(type);
    SootField field = Scene.v().makeSootField(name, type, access);
    Tag tag;
    if (value instanceof Integer)
        tag = new IntegerConstantValueTag((Integer) value);
    else if (value instanceof Float)
        tag = new FloatConstantValueTag((Float) value);
    else if (value instanceof Long)
        tag = new LongConstantValueTag((Long) value);
    else if (value instanceof Double)
        tag = new DoubleConstantValueTag((Double) value);
    else if (value instanceof String)
        tag = new StringConstantValueTag(value.toString());
    else
        tag = null;
    if (tag != null)
        field.addTag(tag);
    if (signature != null)
        field.addTag(new SignatureTag(signature));
    field = klass.getOrAddField(field);
    return new FieldBuilder(field, this);
}
Also used : IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SignatureTag(soot.tagkit.SignatureTag) SootField(soot.SootField) Tag(soot.tagkit.Tag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) InnerClassTag(soot.tagkit.InnerClassTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) SignatureTag(soot.tagkit.SignatureTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SourceFileTag(soot.tagkit.SourceFileTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag)

Example 2 with LongConstantValueTag

use of soot.tagkit.LongConstantValueTag 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 3 with LongConstantValueTag

use of soot.tagkit.LongConstantValueTag 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 4 with LongConstantValueTag

use of soot.tagkit.LongConstantValueTag in project soot by Sable.

the class DexPrinter method makeConstantItem.

private EncodedValue makeConstantItem(SootField sf, Tag t) {
    if (!(t instanceof ConstantValueTag))
        throw new RuntimeException("error: t not ConstantValueTag.");
    if (t instanceof IntegerConstantValueTag) {
        Type sft = sf.getType();
        IntegerConstantValueTag i = (IntegerConstantValueTag) t;
        if (sft instanceof BooleanType) {
            int v = i.getIntValue();
            if (v == 0) {
                return ImmutableBooleanEncodedValue.FALSE_VALUE;
            } else if (v == 1) {
                return ImmutableBooleanEncodedValue.TRUE_VALUE;
            } else {
                throw new RuntimeException("error: boolean value from int with value != 0 or 1.");
            }
        } else if (sft instanceof CharType) {
            return new ImmutableCharEncodedValue((char) i.getIntValue());
        } else if (sft instanceof ByteType) {
            return new ImmutableByteEncodedValue((byte) i.getIntValue());
        } else if (sft instanceof IntType) {
            return new ImmutableIntEncodedValue(i.getIntValue());
        } else if (sft instanceof ShortType) {
            return new ImmutableShortEncodedValue((short) i.getIntValue());
        } else {
            throw new RuntimeException("error: unexpected constant tag type: " + t + " for field " + sf);
        }
    } else if (t instanceof LongConstantValueTag) {
        LongConstantValueTag l = (LongConstantValueTag) t;
        return new ImmutableLongEncodedValue(l.getLongValue());
    } else if (t instanceof DoubleConstantValueTag) {
        DoubleConstantValueTag d = (DoubleConstantValueTag) t;
        return new ImmutableDoubleEncodedValue(d.getDoubleValue());
    } else if (t instanceof FloatConstantValueTag) {
        FloatConstantValueTag f = (FloatConstantValueTag) t;
        return new ImmutableFloatEncodedValue(f.getFloatValue());
    } else if (t instanceof StringConstantValueTag) {
        StringConstantValueTag s = (StringConstantValueTag) t;
        if (sf.getType().equals(RefType.v("java.lang.String")))
            return new ImmutableStringEncodedValue(s.getStringValue());
        else
            // Results in "Bogus static initialization"
            return null;
    } else
        throw new RuntimeException("Unexpected constant type");
}
Also used : ImmutableShortEncodedValue(org.jf.dexlib2.immutable.value.ImmutableShortEncodedValue) ImmutableCharEncodedValue(org.jf.dexlib2.immutable.value.ImmutableCharEncodedValue) ImmutableByteEncodedValue(org.jf.dexlib2.immutable.value.ImmutableByteEncodedValue) ShortType(soot.ShortType) BooleanType(soot.BooleanType) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) ByteType(soot.ByteType) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) IntType(soot.IntType) ConstantValueTag(soot.tagkit.ConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) BooleanType(soot.BooleanType) Type(soot.Type) DexType(soot.dexpler.DexType) RefType(soot.RefType) ShortType(soot.ShortType) ByteType(soot.ByteType) IntType(soot.IntType) CharType(soot.CharType) ImmutableDoubleEncodedValue(org.jf.dexlib2.immutable.value.ImmutableDoubleEncodedValue) ImmutableFloatEncodedValue(org.jf.dexlib2.immutable.value.ImmutableFloatEncodedValue) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) LongConstantValueTag(soot.tagkit.LongConstantValueTag) ImmutableIntEncodedValue(org.jf.dexlib2.immutable.value.ImmutableIntEncodedValue) CharType(soot.CharType) StringConstantValueTag(soot.tagkit.StringConstantValueTag) ImmutableLongEncodedValue(org.jf.dexlib2.immutable.value.ImmutableLongEncodedValue) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag)

Example 5 with LongConstantValueTag

use of soot.tagkit.LongConstantValueTag in project robovm by robovm.

the class MethodCompiler method initializeClassFields.

private void initializeClassFields() {
    for (SootField field : sootMethod.getDeclaringClass().getFields()) {
        if (!field.isStatic()) {
            continue;
        }
        for (Tag tag : field.getTags()) {
            Value value = null;
            if (tag instanceof DoubleConstantValueTag) {
                DoubleConstantValueTag dtag = (DoubleConstantValueTag) tag;
                value = new FloatingPointConstant(dtag.getDoubleValue());
            } else if (tag instanceof FloatConstantValueTag) {
                FloatConstantValueTag ftag = (FloatConstantValueTag) tag;
                value = new FloatingPointConstant(ftag.getFloatValue());
            } else if (tag instanceof IntegerConstantValueTag) {
                IntegerConstantValueTag itag = (IntegerConstantValueTag) tag;
                value = new IntegerConstant(itag.getIntValue());
                IntegerType type = (IntegerType) getType(field.getType());
                if (type.getBits() < 32) {
                    value = new ConstantTrunc((Constant) value, type);
                }
            } else if (tag instanceof LongConstantValueTag) {
                LongConstantValueTag ltag = (LongConstantValueTag) tag;
                value = new IntegerConstant(ltag.getLongValue());
            } else if (tag instanceof StringConstantValueTag) {
                String s = ((StringConstantValueTag) tag).getStringValue();
                value = call(ldcString(s), env);
            }
            if (value != null) {
                FunctionRef fn = FunctionBuilder.setter(field).ref();
                call(fn, env, value);
            }
        }
    }
}
Also used : FloatingPointConstant(org.robovm.compiler.llvm.FloatingPointConstant) FloatingPointConstant(org.robovm.compiler.llvm.FloatingPointConstant) Constant(org.robovm.compiler.llvm.Constant) NullConstant(org.robovm.compiler.llvm.NullConstant) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant) IntegerType(org.robovm.compiler.llvm.IntegerType) ConstantTrunc(org.robovm.compiler.llvm.ConstantTrunc) Value(org.robovm.compiler.llvm.Value) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SootField(soot.SootField) ArrayCheckTag(soot.jimple.toolkits.annotation.tags.ArrayCheckTag) NullCheckTag(soot.jimple.toolkits.annotation.tags.NullCheckTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) Tag(soot.tagkit.Tag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) FunctionRef(org.robovm.compiler.llvm.FunctionRef)

Aggregations

DoubleConstantValueTag (soot.tagkit.DoubleConstantValueTag)7 IntegerConstantValueTag (soot.tagkit.IntegerConstantValueTag)7 LongConstantValueTag (soot.tagkit.LongConstantValueTag)7 FloatConstantValueTag (soot.tagkit.FloatConstantValueTag)6 StringConstantValueTag (soot.tagkit.StringConstantValueTag)6 SootField (soot.SootField)5 Tag (soot.tagkit.Tag)4 BooleanType (soot.BooleanType)3 ByteType (soot.ByteType)3 CharType (soot.CharType)3 IntType (soot.IntType)3 ShortType (soot.ShortType)3 Type (soot.Type)3 ArrayList (java.util.ArrayList)2 DoubleType (soot.DoubleType)2 FloatType (soot.FloatType)2 LongType (soot.LongType)2 RefType (soot.RefType)2 SootClass (soot.SootClass)2 SootMethod (soot.SootMethod)2