Search in sources :

Example 6 with BlockNode

use of jadx.core.dex.nodes.BlockNode in project jadx by skylot.

the class BlockFinallyExtract method countInstructions.

private static int countInstructions(ExceptionHandler excHandler) {
    int totalSize = 0;
    for (BlockNode excBlock : excHandler.getBlocks()) {
        List<InsnNode> list = excBlock.getInstructions();
        if (!list.isEmpty() && list.get(0).getType() == InsnType.MOVE_EXCEPTION) {
            // don't count 'move-exception' it will be removed later
            totalSize--;
        }
        totalSize += list.size();
    }
    return totalSize;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode)

Example 7 with BlockNode

use of jadx.core.dex.nodes.BlockNode in project jadx by skylot.

the class BlockFinallyExtract method extractFinally.

/**
	 * Search and remove common code from 'catch' and 'handlers'.
	 */
private static boolean extractFinally(MethodNode mth, ExceptionHandler handler) {
    int count = handler.getBlocks().size();
    BitSet bs = new BitSet(count);
    List<BlockNode> blocks = new ArrayList<BlockNode>(count);
    for (BlockNode block : handler.getBlocks()) {
        List<InsnNode> insns = block.getInstructions();
        if (!insns.isEmpty()) {
            if (insns.get(0).getType() != InsnType.MOVE_EXCEPTION) {
                blocks.add(block);
            }
            bs.set(block.getId());
        }
    }
    if (blocks.isEmpty()) {
        // nothing to do
        return false;
    }
    List<BlocksRemoveInfo> removes = new LinkedList<BlocksRemoveInfo>();
    Set<BlockNode> splitters = new HashSet<BlockNode>();
    // remove 'finally' from handlers
    TryCatchBlock tryBlock = handler.getTryBlock();
    if (tryBlock.getHandlersCount() > 1) {
        for (ExceptionHandler otherHandler : tryBlock.getHandlers()) {
            if (otherHandler == handler) {
                continue;
            }
            for (BlockNode hb : otherHandler.getBlocks()) {
                BlocksRemoveInfo removeInfo = removeInsns(mth, hb, blocks, bs);
                if (removeInfo != null) {
                    removes.add(removeInfo);
                    break;
                }
            }
        }
        if (removes.size() != tryBlock.getHandlersCount() - 1) {
            return false;
        }
    }
    for (ExceptionHandler otherHandler : tryBlock.getHandlers()) {
        SplitterBlockAttr splitterAttr = otherHandler.getHandlerBlock().get(AType.SPLITTER_BLOCK);
        if (splitterAttr != null) {
            BlockNode splBlock = splitterAttr.getBlock();
            if (!splBlock.getCleanSuccessors().isEmpty()) {
                splitters.add(splBlock);
            }
        }
    }
    // remove 'finally' from 'try' blocks (dominated by splitter block)
    boolean removed = false;
    for (BlockNode splitter : splitters) {
        BlockNode start = splitter.getCleanSuccessors().get(0);
        List<BlockNode> list = BlockUtils.collectBlocksDominatedBy(splitter, start);
        for (BlockNode block : list) {
            if (bs.get(block.getId())) {
                continue;
            }
            BlocksRemoveInfo removeInfo = removeInsns(mth, block, blocks, bs);
            if (removeInfo != null) {
                removes.add(removeInfo);
                removed = true;
                break;
            }
        }
    }
    if (!removed) {
        return false;
    }
    // 'finally' extract confirmed, run remove steps
    LiveVarAnalysis laBefore = null;
    boolean runReMap = isReMapNeeded(removes);
    if (runReMap) {
        laBefore = new LiveVarAnalysis(mth);
        laBefore.runAnalysis();
    }
    for (BlocksRemoveInfo removeInfo : removes) {
        if (!applyRemove(mth, removeInfo)) {
            return false;
        }
    }
    LiveVarAnalysis laAfter = null;
    // remove 'move-exception' instruction
    BlockNode handlerBlock = handler.getHandlerBlock();
    InsnNode me = BlockUtils.getLastInsn(handlerBlock);
    if (me != null && me.getType() == InsnType.MOVE_EXCEPTION) {
        boolean replaced = false;
        List<InsnNode> insnsList = handlerBlock.getInstructions();
        if (!handlerBlock.getCleanSuccessors().isEmpty()) {
            laAfter = new LiveVarAnalysis(mth);
            laAfter.runAnalysis();
            RegisterArg resArg = me.getResult();
            BlockNode succ = handlerBlock.getCleanSuccessors().get(0);
            if (laAfter.isLive(succ, resArg.getRegNum())) {
                // kill variable
                InsnNode kill = new InsnNode(InsnType.NOP, 0);
                kill.setResult(resArg);
                kill.add(AFlag.REMOVE);
                insnsList.set(insnsList.size() - 1, kill);
                replaced = true;
            }
        }
        if (!replaced) {
            insnsList.remove(insnsList.size() - 1);
            handlerBlock.add(AFlag.SKIP);
        }
    }
    // generate 'move' instruction for mapped register pairs
    if (runReMap) {
        if (laAfter == null) {
            laAfter = new LiveVarAnalysis(mth);
            laAfter.runAnalysis();
        }
        performVariablesReMap(mth, removes, laBefore, laAfter);
    }
    handler.setFinally(true);
    return true;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) SplitterBlockAttr(jadx.core.dex.trycatch.SplitterBlockAttr) LiveVarAnalysis(jadx.core.dex.visitors.ssa.LiveVarAnalysis) TryCatchBlock(jadx.core.dex.trycatch.TryCatchBlock) BlocksRemoveInfo(jadx.core.dex.visitors.blocksmaker.helpers.BlocksRemoveInfo) HashSet(java.util.HashSet)

Example 8 with BlockNode

use of jadx.core.dex.nodes.BlockNode in project jadx by skylot.

the class BlockFinallyExtract method removeInsns.

private static BlocksRemoveInfo removeInsns(MethodNode mth, BlockNode remBlock, List<BlockNode> blocks, BitSet bs) {
    if (blocks.isEmpty()) {
        return null;
    }
    BlockNode startBlock = blocks.get(0);
    BlocksRemoveInfo removeInfo = checkFromFirstBlock(remBlock, startBlock, bs);
    if (removeInfo == null) {
        return null;
    }
    Set<BlocksPair> outs = removeInfo.getOuts();
    if (outs.size() == 1) {
        return removeInfo;
    }
    // check if several 'return' blocks maps to one out
    if (mergeReturns(mth, outs)) {
        return removeInfo;
    }
    LOG.debug("Unexpected finally block outs count: {}", outs);
    return null;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) BlocksPair(jadx.core.dex.visitors.blocksmaker.helpers.BlocksPair) BlocksRemoveInfo(jadx.core.dex.visitors.blocksmaker.helpers.BlocksRemoveInfo)

Example 9 with BlockNode

use of jadx.core.dex.nodes.BlockNode in project jadx by skylot.

the class BlockFinallyExtract method splitBlock.

/**
	 * Split one block into connected 2 blocks with same connections.
	 *
	 * @return new successor block
	 */
private static BlockNode splitBlock(MethodNode mth, BlockNode block, int splitIndex) {
    BlockNode newBlock = BlockSplitter.startNewBlock(mth, -1);
    newBlock.getSuccessors().addAll(block.getSuccessors());
    for (BlockNode s : new ArrayList<BlockNode>(block.getSuccessors())) {
        removeConnection(block, s);
        connect(newBlock, s);
    }
    block.getSuccessors().clear();
    connect(block, newBlock);
    block.updateCleanSuccessors();
    newBlock.updateCleanSuccessors();
    List<InsnNode> insns = block.getInstructions();
    int size = insns.size();
    for (int i = splitIndex; i < size; i++) {
        InsnNode insnNode = insns.get(i);
        insnNode.add(AFlag.SKIP);
        newBlock.getInstructions().add(insnNode);
    }
    Iterator<InsnNode> it = insns.iterator();
    while (it.hasNext()) {
        InsnNode insnNode = it.next();
        if (insnNode.contains(AFlag.SKIP)) {
            it.remove();
        }
    }
    for (InsnNode insnNode : newBlock.getInstructions()) {
        insnNode.remove(AFlag.SKIP);
    }
    return newBlock;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) ArrayList(java.util.ArrayList)

Example 10 with BlockNode

use of jadx.core.dex.nodes.BlockNode in project jadx by skylot.

the class BlockFinallyExtract method injectInsn.

private static void injectInsn(MethodNode mth, BlockNode insertBlock, InsnNode insn) {
    insn.add(AFlag.SYNTHETIC);
    if (insertBlock.getInstructions().isEmpty()) {
        insertBlock.getInstructions().add(insn);
    } else {
        BlockNode succBlock = splitBlock(mth, insertBlock, 0);
        BlockNode predBlock = succBlock.getPredecessors().get(0);
        predBlock.getInstructions().add(insn);
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode)

Aggregations

BlockNode (jadx.core.dex.nodes.BlockNode)220 InsnNode (jadx.core.dex.nodes.InsnNode)91 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)43 ArrayList (java.util.ArrayList)41 BitSet (java.util.BitSet)31 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)29 InsnArg (jadx.core.dex.instructions.args.InsnArg)25 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)21 ExceptionHandler (jadx.core.dex.trycatch.ExceptionHandler)19 PhiInsn (jadx.core.dex.instructions.PhiInsn)14 SSAVar (jadx.core.dex.instructions.args.SSAVar)14 Nullable (org.jetbrains.annotations.Nullable)14 LoopInfo (jadx.core.dex.attributes.nodes.LoopInfo)13 IRegion (jadx.core.dex.nodes.IRegion)13 HashSet (java.util.HashSet)13 MethodNode (jadx.core.dex.nodes.MethodNode)12 Region (jadx.core.dex.regions.Region)12 LoopRegion (jadx.core.dex.regions.loops.LoopRegion)12 InsnType (jadx.core.dex.instructions.InsnType)11 SynchronizedRegion (jadx.core.dex.regions.SynchronizedRegion)11