Search in sources :

Example 11 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class RegionUtils method getExcHandlersForRegion.

public static List<IContainer> getExcHandlersForRegion(IContainer region) {
    CatchAttr cb = region.get(AType.CATCH_BLOCK);
    if (cb != null) {
        TryCatchBlock tb = cb.getTryBlock();
        List<IContainer> list = new ArrayList<IContainer>(tb.getHandlersCount());
        for (ExceptionHandler eh : tb.getHandlers()) {
            list.add(eh.getHandlerRegion());
        }
        return list;
    }
    return Collections.emptyList();
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) ArrayList(java.util.ArrayList) CatchAttr(jadx.core.dex.trycatch.CatchAttr) TryCatchBlock(jadx.core.dex.trycatch.TryCatchBlock) IContainer(jadx.core.dex.nodes.IContainer)

Example 12 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class RegionGen method makeTryCatch.

private void makeTryCatch(TryCatchRegion region, CodeWriter code) throws CodegenException {
    code.startLine("try {");
    makeRegionIndent(code, region.getTryRegion());
    // TODO: move search of 'allHandler' to 'TryCatchRegion'
    ExceptionHandler allHandler = null;
    for (Map.Entry<ExceptionHandler, IContainer> entry : region.getCatchRegions().entrySet()) {
        ExceptionHandler handler = entry.getKey();
        if (handler.isCatchAll()) {
            if (allHandler != null) {
                LOG.warn("Several 'all' handlers in try/catch block in {}", mth);
            }
            allHandler = handler;
        } else {
            makeCatchBlock(code, handler);
        }
    }
    if (allHandler != null) {
        makeCatchBlock(code, allHandler);
    }
    IContainer finallyRegion = region.getFinallyRegion();
    if (finallyRegion != null) {
        code.startLine("} finally {");
        makeRegionIndent(code, finallyRegion);
    }
    code.startLine('}');
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) IContainer(jadx.core.dex.nodes.IContainer) Map(java.util.Map)

Example 13 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class DepthRegionTraversal method traverseIncludingExcHandlers.

public static void traverseIncludingExcHandlers(MethodNode mth, IRegionIterativeVisitor visitor) {
    boolean repeat;
    int k = 0;
    do {
        repeat = traverseIterativeStepInternal(mth, visitor, mth.getRegion());
        if (!repeat) {
            for (ExceptionHandler h : mth.getExceptionHandlers()) {
                repeat = traverseIterativeStepInternal(mth, visitor, h.getHandlerRegion());
                if (repeat) {
                    break;
                }
            }
        }
        if (k++ > ITERATIVE_LIMIT) {
            throw new JadxOverflowException("Iterative traversal limit reached, method: " + mth);
        }
    } while (repeat);
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException)

Example 14 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class BlockExceptionHandler method processExceptionHandlers.

private static void processExceptionHandlers(MethodNode mth, BlockNode block) {
    ExcHandlerAttr handlerAttr = block.get(AType.EXC_HANDLER);
    if (handlerAttr == null) {
        return;
    }
    ExceptionHandler excHandler = handlerAttr.getHandler();
    excHandler.addBlock(block);
    for (BlockNode node : BlockUtils.collectBlocksDominatedBy(block, block)) {
        excHandler.addBlock(node);
    }
    for (BlockNode excBlock : excHandler.getBlocks()) {
        // remove 'monitor-exit' from exception handler blocks
        InstructionRemover remover = new InstructionRemover(mth, excBlock);
        for (InsnNode insn : excBlock.getInstructions()) {
            if (insn.getType() == InsnType.MONITOR_ENTER) {
                break;
            }
            if (insn.getType() == InsnType.MONITOR_EXIT) {
                remover.add(insn);
            }
        }
        remover.perform();
        // if 'throw' in exception handler block have 'catch' - merge these catch blocks
        for (InsnNode insn : excBlock.getInstructions()) {
            CatchAttr catchAttr = insn.get(AType.CATCH_BLOCK);
            if (catchAttr == null) {
                continue;
            }
            if (insn.getType() == InsnType.THROW || onlyAllHandler(catchAttr.getTryBlock())) {
                TryCatchBlock handlerBlock = handlerAttr.getTryBlock();
                TryCatchBlock catchBlock = catchAttr.getTryBlock();
                handlerBlock.merge(mth, catchBlock);
            }
        }
    }
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) ExcHandlerAttr(jadx.core.dex.trycatch.ExcHandlerAttr) CatchAttr(jadx.core.dex.trycatch.CatchAttr) TryCatchBlock(jadx.core.dex.trycatch.TryCatchBlock) InstructionRemover(jadx.core.utils.InstructionRemover)

Aggregations

ExceptionHandler (jadx.core.dex.trycatch.ExceptionHandler)14 TryCatchBlock (jadx.core.dex.trycatch.TryCatchBlock)7 BlockNode (jadx.core.dex.nodes.BlockNode)6 HashSet (java.util.HashSet)5 IContainer (jadx.core.dex.nodes.IContainer)4 InsnNode (jadx.core.dex.nodes.InsnNode)4 CatchAttr (jadx.core.dex.trycatch.CatchAttr)4 ExcHandlerAttr (jadx.core.dex.trycatch.ExcHandlerAttr)4 ArrayList (java.util.ArrayList)4 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)3 SplitterBlockAttr (jadx.core.dex.trycatch.SplitterBlockAttr)3 ArgType (jadx.core.dex.instructions.args.ArgType)2 BitSet (java.util.BitSet)2 CatchHandler (com.android.dex.Code.CatchHandler)1 Try (com.android.dex.Code.Try)1 ClassInfo (jadx.core.dex.info.ClassInfo)1 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)1 NamedArg (jadx.core.dex.instructions.args.NamedArg)1 SSAVar (jadx.core.dex.instructions.args.SSAVar)1 IBlock (jadx.core.dex.nodes.IBlock)1