Search in sources :

Example 16 with LongType

use of soot.LongType in project soot by Sable.

the class Util method emptyBody.

/**
 * Remove all statements except from IdentityStatements for parameters.
 * Return default value (null or zero or nothing depending on the return
 * type).
 *
 * @param jBody
 */
public static void emptyBody(Body jBody) {
    // identity statements
    List<Unit> idStmts = new ArrayList<Unit>();
    List<Local> idLocals = new ArrayList<Local>();
    for (Unit u : jBody.getUnits()) {
        if (u instanceof IdentityStmt) {
            IdentityStmt i = (IdentityStmt) u;
            if (i.getRightOp() instanceof ParameterRef || i.getRightOp() instanceof ThisRef) {
                idStmts.add(u);
                idLocals.add((Local) i.getLeftOp());
            }
        }
    }
    jBody.getUnits().clear();
    jBody.getLocals().clear();
    jBody.getTraps().clear();
    final LocalGenerator lg = new LocalGenerator(jBody);
    for (Unit u : idStmts) jBody.getUnits().add(u);
    for (Local l : idLocals) jBody.getLocals().add(l);
    Type rType = jBody.getMethod().getReturnType();
    jBody.getUnits().add(Jimple.v().newNopStmt());
    if (rType instanceof VoidType) {
        jBody.getUnits().add(Jimple.v().newReturnVoidStmt());
    } else {
        Type t = jBody.getMethod().getReturnType();
        Local l = lg.generateLocal(t);
        AssignStmt ass = null;
        if (t instanceof RefType || t instanceof ArrayType) {
            ass = Jimple.v().newAssignStmt(l, NullConstant.v());
        } else if (t instanceof LongType) {
            ass = Jimple.v().newAssignStmt(l, LongConstant.v(0));
        } else if (t instanceof FloatType) {
            ass = Jimple.v().newAssignStmt(l, FloatConstant.v(0.0f));
        } else if (t instanceof IntType) {
            ass = Jimple.v().newAssignStmt(l, IntConstant.v(0));
        } else if (t instanceof DoubleType) {
            ass = Jimple.v().newAssignStmt(l, DoubleConstant.v(0));
        } else if (t instanceof BooleanType || t instanceof ByteType || t instanceof CharType || t instanceof ShortType) {
            ass = Jimple.v().newAssignStmt(l, IntConstant.v(0));
        } else {
            throw new RuntimeException("error: return type unknown: " + t + " class: " + t.getClass());
        }
        jBody.getUnits().add(ass);
        jBody.getUnits().add(Jimple.v().newReturnStmt(l));
    }
}
Also used : VoidType(soot.VoidType) LocalGenerator(soot.javaToJimple.LocalGenerator) LongType(soot.LongType) AssignStmt(soot.jimple.AssignStmt) ShortType(soot.ShortType) ArrayList(java.util.ArrayList) BooleanType(soot.BooleanType) Local(soot.Local) ByteType(soot.ByteType) Unit(soot.Unit) FloatType(soot.FloatType) IntType(soot.IntType) RefType(soot.RefType) ArrayType(soot.ArrayType) RefType(soot.RefType) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) CharType(soot.CharType) LongType(soot.LongType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) ArrayType(soot.ArrayType) Type(soot.Type) VoidType(soot.VoidType) ParameterRef(soot.jimple.ParameterRef) ThisRef(soot.jimple.ThisRef) DoubleType(soot.DoubleType) CharType(soot.CharType) IdentityStmt(soot.jimple.IdentityStmt)

Example 17 with LongType

use of soot.LongType in project soot by Sable.

the class FillArrayDataInstruction method getArrayElement.

private NumericConstant getArrayElement(Number element, DexBody body, int arrayRegister) {
    List<DexlibAbstractInstruction> instructions = body.instructionsBefore(this);
    Set<Integer> usedRegisters = new HashSet<Integer>();
    usedRegisters.add(arrayRegister);
    Type elementType = null;
    Outer: for (DexlibAbstractInstruction i : instructions) {
        if (usedRegisters.isEmpty())
            break;
        for (int reg : usedRegisters) if (i instanceof NewArrayInstruction) {
            NewArrayInstruction newArrayInstruction = (NewArrayInstruction) i;
            Instruction22c instruction22c = (Instruction22c) newArrayInstruction.instruction;
            if (instruction22c.getRegisterA() == reg) {
                ArrayType arrayType = (ArrayType) DexType.toSoot((TypeReference) instruction22c.getReference());
                elementType = arrayType.getElementType();
                break Outer;
            }
        }
        // look for new registers
        for (int reg : usedRegisters) {
            int newRegister = i.movesToRegister(reg);
            if (newRegister != -1) {
                usedRegisters.add(newRegister);
                usedRegisters.remove(reg);
                // there can't be more than one new
                break;
            }
        }
    }
    if (elementType == null) {
        // throw new InternalError("Unable to find array type to type array elements!");
        logger.warn("Unable to find array type to type array elements! Array was not defined! (obfuscated bytecode?)");
        return null;
    }
    NumericConstant value;
    if (elementType instanceof BooleanType) {
        value = IntConstant.v(element.intValue());
        IntConstant ic = (IntConstant) value;
        if (ic.value != 0) {
            value = IntConstant.v(1);
        }
    } else if (elementType instanceof ByteType) {
        value = IntConstant.v(element.byteValue());
    } else if (elementType instanceof CharType || elementType instanceof ShortType) {
        value = IntConstant.v(element.shortValue());
    } else if (elementType instanceof DoubleType) {
        value = DoubleConstant.v(Double.longBitsToDouble(element.longValue()));
    } else if (elementType instanceof FloatType) {
        value = FloatConstant.v(Float.intBitsToFloat(element.intValue()));
    } else if (elementType instanceof IntType) {
        value = IntConstant.v(element.intValue());
    } else if (elementType instanceof LongType) {
        value = LongConstant.v(element.longValue());
    } else {
        throw new RuntimeException("Invalid Array Type occured in FillArrayDataInstruction: " + elementType);
    }
    return value;
}
Also used : Instruction22c(org.jf.dexlib2.iface.instruction.formats.Instruction22c) LongType(soot.LongType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) FloatType(soot.FloatType) IntType(soot.IntType) ArrayType(soot.ArrayType) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) CharType(soot.CharType) LongType(soot.LongType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) ArrayType(soot.ArrayType) Type(soot.Type) DexType(soot.dexpler.DexType) NumericConstant(soot.jimple.NumericConstant) DoubleType(soot.DoubleType) IntConstant(soot.jimple.IntConstant) TypeReference(org.jf.dexlib2.iface.reference.TypeReference) CharType(soot.CharType) HashSet(java.util.HashSet)

Aggregations

LongType (soot.LongType)17 DoubleType (soot.DoubleType)16 IntType (soot.IntType)16 Type (soot.Type)15 FloatType (soot.FloatType)14 ByteType (soot.ByteType)12 ShortType (soot.ShortType)12 BooleanType (soot.BooleanType)11 CharType (soot.CharType)11 Value (soot.Value)11 ArrayList (java.util.ArrayList)10 Local (soot.Local)10 RefType (soot.RefType)9 List (java.util.List)7 ArrayType (soot.ArrayType)7 NullType (soot.NullType)6 IntConstant (soot.jimple.IntConstant)6 PrimType (soot.PrimType)5 Unit (soot.Unit)5 ValueBox (soot.ValueBox)5