Search in sources :

Example 1 with TmpEdgeAttr

use of jadx.core.dex.attributes.nodes.TmpEdgeAttr in project jadx by skylot.

the class BlockExceptionHandler method initExcHandlers.

@SuppressWarnings("ForLoopReplaceableByForEach")
private static void initExcHandlers(MethodNode mth) {
    List<BlockNode> blocks = mth.getBasicBlocks();
    int blocksCount = blocks.size();
    for (int i = 0; i < blocksCount; i++) {
        // will add new blocks to list end
        BlockNode block = blocks.get(i);
        InsnNode firstInsn = BlockUtils.getFirstInsn(block);
        if (firstInsn == null) {
            continue;
        }
        ExcHandlerAttr excHandlerAttr = firstInsn.get(AType.EXC_HANDLER);
        if (excHandlerAttr == null) {
            continue;
        }
        firstInsn.remove(AType.EXC_HANDLER);
        TmpEdgeAttr tmpEdgeAttr = block.get(AType.TMP_EDGE);
        if (tmpEdgeAttr != null) {
            // remove temp connection
            BlockSplitter.removeConnection(tmpEdgeAttr.getBlock(), block);
            block.remove(AType.TMP_EDGE);
        }
        ExceptionHandler excHandler = excHandlerAttr.getHandler();
        if (block.getPredecessors().isEmpty()) {
            excHandler.setHandlerBlock(block);
            block.addAttr(excHandlerAttr);
            excHandler.addBlock(block);
            BlockUtils.collectBlocksDominatedByWithExcHandlers(mth, block, block).forEach(excHandler::addBlock);
        } else {
            // ignore already connected handlers -> make catch empty
            BlockNode emptyHandlerBlock = BlockSplitter.startNewBlock(mth, block.getStartOffset());
            emptyHandlerBlock.add(AFlag.SYNTHETIC);
            emptyHandlerBlock.addAttr(excHandlerAttr);
            BlockSplitter.connect(emptyHandlerBlock, block);
            excHandler.setHandlerBlock(emptyHandlerBlock);
            excHandler.addBlock(emptyHandlerBlock);
        }
        fixMoveExceptionInsn(block, excHandlerAttr);
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) InsnNode(jadx.core.dex.nodes.InsnNode) ExcHandlerAttr(jadx.core.dex.trycatch.ExcHandlerAttr) TmpEdgeAttr(jadx.core.dex.attributes.nodes.TmpEdgeAttr)

Example 2 with TmpEdgeAttr

use of jadx.core.dex.attributes.nodes.TmpEdgeAttr in project jadx by skylot.

the class BlockSplitter method addTempConnectionsForExcHandlers.

/**
 * Connect exception handlers to the throw block.
 * This temporary connection needed to build close to final dominators tree.
 * Will be used and removed in {@code jadx.core.dex.visitors.blocks.BlockExceptionHandler}
 */
private static void addTempConnectionsForExcHandlers(MethodNode mth, Map<Integer, BlockNode> blocksMap) {
    for (BlockNode block : mth.getBasicBlocks()) {
        for (InsnNode insn : block.getInstructions()) {
            CatchAttr catchAttr = insn.get(AType.EXC_CATCH);
            if (catchAttr == null) {
                continue;
            }
            for (ExceptionHandler handler : catchAttr.getHandlers()) {
                BlockNode handlerBlock = getBlock(handler.getHandlerOffset(), blocksMap);
                if (!handlerBlock.contains(AType.TMP_EDGE)) {
                    List<BlockNode> preds = block.getPredecessors();
                    if (preds.isEmpty()) {
                        throw new JadxRuntimeException("Unexpected missing predecessor for block: " + block);
                    }
                    BlockNode start = preds.size() == 1 ? preds.get(0) : block;
                    if (!start.getSuccessors().contains(handlerBlock)) {
                        connect(start, handlerBlock);
                        handlerBlock.addAttr(new TmpEdgeAttr(start));
                    }
                }
            }
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) TargetInsnNode(jadx.core.dex.instructions.TargetInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) TmpEdgeAttr(jadx.core.dex.attributes.nodes.TmpEdgeAttr) CatchAttr(jadx.core.dex.trycatch.CatchAttr) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Aggregations

TmpEdgeAttr (jadx.core.dex.attributes.nodes.TmpEdgeAttr)2 BlockNode (jadx.core.dex.nodes.BlockNode)2 InsnNode (jadx.core.dex.nodes.InsnNode)2 ExceptionHandler (jadx.core.dex.trycatch.ExceptionHandler)2 TargetInsnNode (jadx.core.dex.instructions.TargetInsnNode)1 CatchAttr (jadx.core.dex.trycatch.CatchAttr)1 ExcHandlerAttr (jadx.core.dex.trycatch.ExcHandlerAttr)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1