Search in sources :

Example 21 with IRegion

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

the class RegionUtils method getFirstInsn.

public static InsnNode getFirstInsn(IContainer container) {
    if (container instanceof IBlock) {
        IBlock block = (IBlock) container;
        List<InsnNode> insnList = block.getInstructions();
        if (insnList.isEmpty()) {
            return null;
        }
        return insnList.get(0);
    } 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 getFirstInsn(blocks.get(0));
    } 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)

Example 22 with IRegion

use of jadx.core.dex.nodes.IRegion 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)

Example 23 with IRegion

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

the class RegionUtils method isRegionContainsRegion.

/**
 * Check if {@code region} contains in {@code container}.
 * <br>
 * For simple region (not from exception handlers) search in parents
 * otherwise run recursive search because exception handlers can have several parents
 */
public static boolean isRegionContainsRegion(IContainer container, IRegion region) {
    if (container == region) {
        return true;
    }
    if (region == null) {
        return false;
    }
    IRegion parent = region.getParent();
    while (container != parent) {
        if (parent == null) {
            if (region.contains(AType.EXC_HANDLER)) {
                return isRegionContainsExcHandlerRegion(container, region);
            }
            return false;
        }
        region = parent;
        parent = region.getParent();
    }
    return true;
}
Also used : IRegion(jadx.core.dex.nodes.IRegion)

Example 24 with IRegion

use of jadx.core.dex.nodes.IRegion 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, TryCatchBlockAttr 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);
    // 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) LoopRegion(jadx.core.dex.regions.loops.LoopRegion) TryCatchRegion(jadx.core.dex.regions.TryCatchRegion) AbstractRegion(jadx.core.dex.regions.AbstractRegion) Region(jadx.core.dex.regions.Region) IBranchRegion(jadx.core.dex.nodes.IBranchRegion) LoopRegion(jadx.core.dex.regions.loops.LoopRegion) AbstractRegion(jadx.core.dex.regions.AbstractRegion) IContainer(jadx.core.dex.nodes.IContainer)

Example 25 with IRegion

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

the class RegionMakerVisitor method visit.

@Override
public void visit(MethodNode mth) throws JadxException {
    if (mth.isNoCode() || mth.getBasicBlocks().isEmpty()) {
        return;
    }
    RegionMaker rm = new RegionMaker(mth);
    RegionStack state = new RegionStack(mth);
    // fill region structure
    BlockNode startBlock = Utils.first(mth.getEnterBlock().getCleanSuccessors());
    mth.setRegion(rm.makeRegion(startBlock, state));
    if (!mth.isNoExceptionHandlers()) {
        IRegion expOutBlock = rm.processTryCatchBlocks(mth);
        if (expOutBlock != null) {
            mth.getRegion().add(expOutBlock);
        }
    }
    postProcessRegions(mth);
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) IRegion(jadx.core.dex.nodes.IRegion)

Aggregations

IRegion (jadx.core.dex.nodes.IRegion)26 IContainer (jadx.core.dex.nodes.IContainer)14 Region (jadx.core.dex.regions.Region)12 LoopRegion (jadx.core.dex.regions.loops.LoopRegion)11 BlockNode (jadx.core.dex.nodes.BlockNode)10 IBlock (jadx.core.dex.nodes.IBlock)10 IfRegion (jadx.core.dex.regions.conditions.IfRegion)8 IBranchRegion (jadx.core.dex.nodes.IBranchRegion)7 SwitchRegion (jadx.core.dex.regions.SwitchRegion)7 SynchronizedRegion (jadx.core.dex.regions.SynchronizedRegion)7 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)5 HashSet (java.util.HashSet)4 InsnNode (jadx.core.dex.nodes.InsnNode)3 MethodNode (jadx.core.dex.nodes.MethodNode)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 LoopInfo (jadx.core.dex.attributes.nodes.LoopInfo)2 Edge (jadx.core.dex.nodes.Edge)2 AbstractRegion (jadx.core.dex.regions.AbstractRegion)2 TryCatchRegion (jadx.core.dex.regions.TryCatchRegion)2