Search in sources :

Example 1 with IBranchRegion

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

the class ProcessTryCatchRegions method wrapBlocks.

/**
	 * Extract all block dominated by 'dominator' to separate region and mark as try/catch block
	 */
private static boolean wrapBlocks(IRegion replaceRegion, TryCatchBlock tb, BlockNode dominator) {
    if (replaceRegion == null) {
        return false;
    }
    if (replaceRegion instanceof LoopRegion) {
        LoopRegion loop = (LoopRegion) replaceRegion;
        return wrapBlocks(loop.getBody(), tb, dominator);
    }
    if (replaceRegion instanceof IBranchRegion) {
        return wrapBlocks(replaceRegion.getParent(), tb, dominator);
    }
    Region tryRegion = new Region(replaceRegion);
    List<IContainer> subBlocks = replaceRegion.getSubBlocks();
    for (IContainer cont : subBlocks) {
        if (RegionUtils.hasPathThroughBlock(dominator, cont)) {
            if (isHandlerPath(tb, cont)) {
                break;
            }
            tryRegion.getSubBlocks().add(cont);
        }
    }
    if (tryRegion.getSubBlocks().isEmpty()) {
        return false;
    }
    TryCatchRegion tryCatchRegion = new TryCatchRegion(replaceRegion, tryRegion);
    tryRegion.setParent(tryCatchRegion);
    tryCatchRegion.setTryCatchBlock(tb.getCatchAttr().getTryBlock());
    // replace first node by region
    IContainer firstNode = tryRegion.getSubBlocks().get(0);
    if (!replaceRegion.replaceSubBlock(firstNode, tryCatchRegion)) {
        return false;
    }
    subBlocks.removeAll(tryRegion.getSubBlocks());
    // fix parents for tryRegion sub blocks
    for (IContainer cont : tryRegion.getSubBlocks()) {
        if (cont instanceof AbstractRegion) {
            AbstractRegion aReg = (AbstractRegion) cont;
            aReg.setParent(tryRegion);
        }
    }
    return true;
}
Also used : TryCatchRegion(jadx.core.dex.regions.TryCatchRegion) IBranchRegion(jadx.core.dex.nodes.IBranchRegion) IRegion(jadx.core.dex.nodes.IRegion) TryCatchRegion(jadx.core.dex.regions.TryCatchRegion) IBranchRegion(jadx.core.dex.nodes.IBranchRegion) LoopRegion(jadx.core.dex.regions.loops.LoopRegion) AbstractRegion(jadx.core.dex.regions.AbstractRegion) Region(jadx.core.dex.regions.Region) LoopRegion(jadx.core.dex.regions.loops.LoopRegion) AbstractRegion(jadx.core.dex.regions.AbstractRegion) IContainer(jadx.core.dex.nodes.IContainer)

Example 2 with IBranchRegion

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

the class ProcessVariables method calculateOrder.

private static int calculateOrder(IContainer container, Map<IContainer, Integer> regionsOrder, int id, boolean inc) {
    if (!(container instanceof IRegion)) {
        return id;
    }
    IRegion region = (IRegion) container;
    Integer previous = regionsOrder.put(region, id);
    if (previous != null) {
        return id;
    }
    for (IContainer c : region.getSubBlocks()) {
        if (c instanceof IBranchRegion) {
            // on branch set for all inner regions same order id
            id = calculateOrder(c, regionsOrder, inc ? id + 1 : id, false);
        } else {
            List<IContainer> handlers = RegionUtils.getExcHandlersForRegion(c);
            if (!handlers.isEmpty()) {
                for (IContainer handler : handlers) {
                    id = calculateOrder(handler, regionsOrder, inc ? id + 1 : id, inc);
                }
            }
            id = calculateOrder(c, regionsOrder, inc ? id + 1 : id, inc);
        }
    }
    return id;
}
Also used : IBranchRegion(jadx.core.dex.nodes.IBranchRegion) IContainer(jadx.core.dex.nodes.IContainer) IRegion(jadx.core.dex.nodes.IRegion)

Example 3 with IBranchRegion

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

the class RegionUtils method hasExitEdge.

public static boolean hasExitEdge(IContainer container) {
    if (container instanceof IBlock) {
        InsnNode lastInsn = BlockUtils.getLastInsn((IBlock) container);
        if (lastInsn == null) {
            return false;
        }
        InsnType type = lastInsn.getType();
        return type == InsnType.RETURN || type == InsnType.CONTINUE || type == InsnType.BREAK || type == InsnType.THROW;
    } else if (container instanceof IBranchRegion) {
        for (IContainer br : ((IBranchRegion) container).getBranches()) {
            if (br == null || !hasExitEdge(br)) {
                return false;
            }
        }
        return true;
    } else if (container instanceof IRegion) {
        IRegion region = (IRegion) container;
        List<IContainer> blocks = region.getSubBlocks();
        return !blocks.isEmpty() && hasExitEdge(blocks.get(blocks.size() - 1));
    } else {
        throw new JadxRuntimeException(unknownContainerType(container));
    }
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) IBlock(jadx.core.dex.nodes.IBlock) IBranchRegion(jadx.core.dex.nodes.IBranchRegion) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) InsnType(jadx.core.dex.instructions.InsnType) IContainer(jadx.core.dex.nodes.IContainer) IRegion(jadx.core.dex.nodes.IRegion)

Example 4 with IBranchRegion

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

the class RegionUtils method getLastInsn.

public static InsnNode getLastInsn(IContainer container) {
    if (container instanceof IBlock) {
        IBlock block = (IBlock) container;
        List<InsnNode> insnList = block.getInstructions();
        if (insnList.isEmpty()) {
            return null;
        }
        return insnList.get(insnList.size() - 1);
    } else if (container instanceof IBranchRegion) {
        return null;
    } else if (container instanceof IRegion) {
        IRegion region = (IRegion) container;
        List<IContainer> blocks = region.getSubBlocks();
        if (blocks.isEmpty()) {
            return null;
        }
        return getLastInsn(blocks.get(blocks.size() - 1));
    } else {
        throw new JadxRuntimeException(unknownContainerType(container));
    }
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) IBlock(jadx.core.dex.nodes.IBlock) IBranchRegion(jadx.core.dex.nodes.IBranchRegion) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IContainer(jadx.core.dex.nodes.IContainer) IRegion(jadx.core.dex.nodes.IRegion)

Aggregations

IBranchRegion (jadx.core.dex.nodes.IBranchRegion)4 IContainer (jadx.core.dex.nodes.IContainer)4 IRegion (jadx.core.dex.nodes.IRegion)4 IBlock (jadx.core.dex.nodes.IBlock)2 InsnNode (jadx.core.dex.nodes.InsnNode)2 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)2 InsnType (jadx.core.dex.instructions.InsnType)1 AbstractRegion (jadx.core.dex.regions.AbstractRegion)1 Region (jadx.core.dex.regions.Region)1 TryCatchRegion (jadx.core.dex.regions.TryCatchRegion)1 LoopRegion (jadx.core.dex.regions.loops.LoopRegion)1