Search in sources :

Example 1 with MoveExceptionInstruction

use of soot.dexpler.instructions.MoveExceptionInstruction in project soot by Sable.

the class DexBody method addTraps.

/**
 * Add the traps of this body.
 *
 * Should only be called at the end jimplify.
 */
private void addTraps() {
    final Jimple jimple = Jimple.v();
    for (TryBlock<? extends ExceptionHandler> tryItem : tries) {
        int startAddress = tryItem.getStartCodeAddress();
        // .getTryLength();
        int length = tryItem.getCodeUnitCount();
        // - 1;
        int endAddress = startAddress + length;
        Unit beginStmt = instructionAtAddress(startAddress).getUnit();
        // (startAddress + length) typically points to the first byte of the
        // first instruction after the try block
        // except if there is no instruction after the try block in which
        // case it points to the last byte of the last
        // instruction of the try block. Removing 1 from (startAddress +
        // length) always points to "somewhere" in
        // the last instruction of the try block since the smallest
        // instruction is on two bytes (nop = 0x0000).
        Unit endStmt = instructionAtAddress(endAddress).getUnit();
        // the last instruction in the try block.
        if (jBody.getUnits().getLast() == endStmt && instructionAtAddress(endAddress - 1).getUnit() == endStmt) {
            Unit nop = jimple.newNopStmt();
            jBody.getUnits().insertAfter(nop, endStmt);
            endStmt = nop;
        }
        List<? extends ExceptionHandler> hList = tryItem.getExceptionHandlers();
        for (ExceptionHandler handler : hList) {
            String exceptionType = handler.getExceptionType();
            if (exceptionType == null)
                exceptionType = "Ljava/lang/Throwable;";
            Type t = DexType.toSoot(exceptionType);
            // exceptions can only be of RefType
            if (t instanceof RefType) {
                SootClass exception = ((RefType) t).getSootClass();
                DexlibAbstractInstruction instruction = instructionAtAddress(handler.getHandlerCodeAddress());
                if (!(instruction instanceof MoveExceptionInstruction))
                    logger.debug("" + String.format("First instruction of trap handler unit not MoveException but %s", instruction.getClass().getName()));
                else
                    ((MoveExceptionInstruction) instruction).setRealType(this, exception.getType());
                Trap trap = jimple.newTrap(exception, beginStmt, endStmt, instruction.getUnit());
                jBody.getTraps().add(trap);
            }
        }
    }
}
Also used : DexlibAbstractInstruction(soot.dexpler.instructions.DexlibAbstractInstruction) Trap(soot.Trap) Unit(soot.Unit) SootClass(soot.SootClass) MoveExceptionInstruction(soot.dexpler.instructions.MoveExceptionInstruction) ExceptionHandler(org.jf.dexlib2.iface.ExceptionHandler) RefType(soot.RefType) RefType(soot.RefType) Type(soot.Type) UnknownType(soot.UnknownType) DoubleType(soot.DoubleType) LongType(soot.LongType) NullType(soot.NullType) PrimType(soot.PrimType) Jimple(soot.jimple.Jimple)

Aggregations

ExceptionHandler (org.jf.dexlib2.iface.ExceptionHandler)1 DoubleType (soot.DoubleType)1 LongType (soot.LongType)1 NullType (soot.NullType)1 PrimType (soot.PrimType)1 RefType (soot.RefType)1 SootClass (soot.SootClass)1 Trap (soot.Trap)1 Type (soot.Type)1 Unit (soot.Unit)1 UnknownType (soot.UnknownType)1 DexlibAbstractInstruction (soot.dexpler.instructions.DexlibAbstractInstruction)1 MoveExceptionInstruction (soot.dexpler.instructions.MoveExceptionInstruction)1 Jimple (soot.jimple.Jimple)1