Search in sources :

Example 1 with CatchAttr

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

the class ProcessTryCatchRegions method searchTryCatchDominators.

private static void searchTryCatchDominators(MethodNode mth, Map<BlockNode, TryCatchBlock> tryBlocksMap) {
    Set<TryCatchBlock> tryBlocks = new HashSet<TryCatchBlock>();
    // collect all try/catch blocks
    for (BlockNode block : mth.getBasicBlocks()) {
        CatchAttr c = block.get(AType.CATCH_BLOCK);
        if (c != null) {
            tryBlocks.add(c.getTryBlock());
        }
    }
    // for each try block search nearest dominator block
    for (TryCatchBlock tb : tryBlocks) {
        if (tb.getHandlersCount() == 0) {
            LOG.warn("No exception handlers in catch block, method: {}", mth);
            continue;
        }
        BitSet bs = new BitSet(mth.getBasicBlocks().size());
        for (ExceptionHandler excHandler : tb.getHandlers()) {
            SplitterBlockAttr splitter = excHandler.getHandlerBlock().get(AType.SPLITTER_BLOCK);
            if (splitter != null) {
                BlockNode block = splitter.getBlock();
                bs.set(block.getId());
            }
        }
        List<BlockNode> domBlocks = BlockUtils.bitSetToBlocks(mth, bs);
        BlockNode domBlock;
        if (domBlocks.size() != 1) {
            domBlock = BlockUtils.getTopBlock(domBlocks);
            if (domBlock == null) {
                throw new JadxRuntimeException("Exception block dominator not found, method:" + mth + ". bs: " + domBlocks);
            }
        } else {
            domBlock = domBlocks.get(0);
        }
        TryCatchBlock prevTB = tryBlocksMap.put(domBlock, tb);
        if (prevTB != null) {
            ErrorsCounter.methodError(mth, "Failed to process nested try/catch");
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) SplitterBlockAttr(jadx.core.dex.trycatch.SplitterBlockAttr) BitSet(java.util.BitSet) TryCatchBlock(jadx.core.dex.trycatch.TryCatchBlock) CatchAttr(jadx.core.dex.trycatch.CatchAttr) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) HashSet(java.util.HashSet)

Example 2 with CatchAttr

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

the class BlockProcessor method removeBlocks.

private static void removeBlocks(MethodNode mth) {
    Iterator<BlockNode> it = mth.getBasicBlocks().iterator();
    while (it.hasNext()) {
        BlockNode block = it.next();
        if (block.contains(AFlag.REMOVE)) {
            if (!block.getPredecessors().isEmpty() || !block.getSuccessors().isEmpty()) {
                LOG.error("Block {} not deleted, method: {}", block, mth);
            } else {
                CatchAttr catchAttr = block.get(AType.CATCH_BLOCK);
                if (catchAttr != null) {
                    catchAttr.getTryBlock().removeBlock(mth, block);
                }
                it.remove();
            }
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) CatchAttr(jadx.core.dex.trycatch.CatchAttr)

Example 3 with CatchAttr

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

the class BlockSplitter method connectExceptionHandlers.

private static void connectExceptionHandlers(Map<Integer, BlockNode> blocksMap, BlockNode block, InsnNode insn) {
    CatchAttr catches = insn.get(AType.CATCH_BLOCK);
    SplitterBlockAttr spl = block.get(AType.SPLITTER_BLOCK);
    if (catches == null || spl == null) {
        return;
    }
    BlockNode splitterBlock = spl.getBlock();
    boolean tryEnd = insn.contains(AFlag.TRY_LEAVE);
    for (ExceptionHandler h : catches.getTryBlock().getHandlers()) {
        BlockNode handlerBlock = getBlock(h.getHandleOffset(), blocksMap);
        // skip self loop in handler
        if (splitterBlock != handlerBlock) {
            if (!handlerBlock.contains(AType.SPLITTER_BLOCK)) {
                handlerBlock.addAttr(spl);
            }
            connect(splitterBlock, handlerBlock);
        }
        if (tryEnd) {
            connect(block, handlerBlock);
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) SplitterBlockAttr(jadx.core.dex.trycatch.SplitterBlockAttr) CatchAttr(jadx.core.dex.trycatch.CatchAttr)

Example 4 with CatchAttr

use of jadx.core.dex.trycatch.CatchAttr 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 5 with CatchAttr

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

the class MethodGen method addFallbackInsns.

public static void addFallbackInsns(CodeWriter code, MethodNode mth, InsnNode[] insnArr, boolean addLabels) {
    InsnGen insnGen = new InsnGen(getFallbackMethodGen(mth), true);
    for (InsnNode insn : insnArr) {
        if (insn == null || insn.getType() == InsnType.NOP) {
            continue;
        }
        if (addLabels && (insn.contains(AType.JUMP) || insn.contains(AType.EXC_HANDLER))) {
            code.decIndent();
            code.startLine(getLabelName(insn.getOffset()) + ":");
            code.incIndent();
        }
        try {
            if (insnGen.makeInsn(insn, code)) {
                CatchAttr catchAttr = insn.get(AType.CATCH_BLOCK);
                if (catchAttr != null) {
                    code.add("\t " + catchAttr);
                }
            }
        } catch (CodegenException e) {
            LOG.debug("Error generate fallback instruction: ", e.getCause());
            code.startLine("// error: " + insn);
        }
    }
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) CodegenException(jadx.core.utils.exceptions.CodegenException) CatchAttr(jadx.core.dex.trycatch.CatchAttr)

Aggregations

CatchAttr (jadx.core.dex.trycatch.CatchAttr)7 BlockNode (jadx.core.dex.nodes.BlockNode)5 ExceptionHandler (jadx.core.dex.trycatch.ExceptionHandler)4 TryCatchBlock (jadx.core.dex.trycatch.TryCatchBlock)3 InsnNode (jadx.core.dex.nodes.InsnNode)2 SplitterBlockAttr (jadx.core.dex.trycatch.SplitterBlockAttr)2 IContainer (jadx.core.dex.nodes.IContainer)1 ExcHandlerAttr (jadx.core.dex.trycatch.ExcHandlerAttr)1 InstructionRemover (jadx.core.utils.InstructionRemover)1 CodegenException (jadx.core.utils.exceptions.CodegenException)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 HashSet (java.util.HashSet)1