Search in sources :

Example 6 with Edge

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

the class RegionMaker method checkLoopExits.

private boolean checkLoopExits(LoopInfo loop, BlockNode mainExitBlock) {
    List<Edge> exitEdges = loop.getExitEdges();
    if (exitEdges.size() < 2) {
        return true;
    }
    Optional<Edge> mainEdgeOpt = exitEdges.stream().filter(edge -> edge.getSource() == mainExitBlock).findFirst();
    if (!mainEdgeOpt.isPresent()) {
        throw new JadxRuntimeException("Not found exit edge by exit block: " + mainExitBlock);
    }
    Edge mainExitEdge = mainEdgeOpt.get();
    BlockNode mainOutBlock = mainExitEdge.getTarget();
    for (Edge exitEdge : exitEdges) {
        if (exitEdge != mainExitEdge) {
            // all exit paths must be same or don't cross (will be inside loop)
            BlockNode exitBlock = exitEdge.getTarget();
            if (!isEqualPaths(mainOutBlock, exitBlock)) {
                BlockNode crossBlock = BlockUtils.getPathCross(mth, mainOutBlock, exitBlock);
                if (crossBlock != null) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : BlockUtils.followEmptyPath(jadx.core.utils.BlockUtils.followEmptyPath) BlockUtils.isPathExists(jadx.core.utils.BlockUtils.isPathExists) MethodNode(jadx.core.dex.nodes.MethodNode) AType(jadx.core.dex.attributes.AType) IfMakerHelper.mergeNestedIfNodes(jadx.core.dex.visitors.regions.IfMakerHelper.mergeNestedIfNodes) LoggerFactory(org.slf4j.LoggerFactory) EdgeInsnAttr(jadx.core.dex.attributes.nodes.EdgeInsnAttr) IfMakerHelper.makeIfInfo(jadx.core.dex.visitors.regions.IfMakerHelper.makeIfInfo) LoopInfo(jadx.core.dex.attributes.nodes.LoopInfo) ExcHandlerAttr(jadx.core.dex.trycatch.ExcHandlerAttr) Map(java.util.Map) InsnNode(jadx.core.dex.nodes.InsnNode) Set(java.util.Set) IfInfo(jadx.core.dex.regions.conditions.IfInfo) Edge(jadx.core.dex.nodes.Edge) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) SwitchInsn(jadx.core.dex.instructions.SwitchInsn) Region(jadx.core.dex.regions.Region) IContainer(jadx.core.dex.nodes.IContainer) Entry(java.util.Map.Entry) Optional(java.util.Optional) IRegion(jadx.core.dex.nodes.IRegion) AFlag(jadx.core.dex.attributes.AFlag) IfMakerHelper.searchNestedIf(jadx.core.dex.visitors.regions.IfMakerHelper.searchNestedIf) InsnType(jadx.core.dex.instructions.InsnType) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException) BlockUtils(jadx.core.utils.BlockUtils) SwitchRegion(jadx.core.dex.regions.SwitchRegion) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) IfNode(jadx.core.dex.instructions.IfNode) BlockUtils.getNextBlock(jadx.core.utils.BlockUtils.getNextBlock) TryCatchBlockAttr(jadx.core.dex.trycatch.TryCatchBlockAttr) InsnArg(jadx.core.dex.instructions.args.InsnArg) LinkedHashSet(java.util.LinkedHashSet) SynchronizedRegion(jadx.core.dex.regions.SynchronizedRegion) RegionUtils(jadx.core.utils.RegionUtils) Logger(org.slf4j.Logger) LoopRegion(jadx.core.dex.regions.loops.LoopRegion) IfRegion(jadx.core.dex.regions.conditions.IfRegion) LoopLabelAttr(jadx.core.dex.attributes.nodes.LoopLabelAttr) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) BlockNode(jadx.core.dex.nodes.BlockNode) InsnContainer(jadx.core.dex.nodes.InsnContainer) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) BitSet(java.util.BitSet) Collections(java.util.Collections) IfMakerHelper.confirmMerge(jadx.core.dex.visitors.regions.IfMakerHelper.confirmMerge) IBlock(jadx.core.dex.nodes.IBlock) Utils(jadx.core.utils.Utils) BlockNode(jadx.core.dex.nodes.BlockNode) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) Edge(jadx.core.dex.nodes.Edge)

Example 7 with Edge

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

the class RegionMaker method insertLoopBreak.

private boolean insertLoopBreak(RegionStack stack, LoopInfo loop, BlockNode loopExit, Edge exitEdge) {
    BlockNode exit = exitEdge.getTarget();
    Edge insertEdge = null;
    boolean confirm = false;
    // process special cases:
    // 1. jump to outer loop
    BlockNode exitEnd = BlockUtils.followEmptyPath(exit);
    List<LoopInfo> loops = exitEnd.getAll(AType.LOOP);
    for (LoopInfo loopAtEnd : loops) {
        if (loopAtEnd != loop) {
            insertEdge = exitEdge;
            confirm = true;
            break;
        }
    }
    if (!confirm) {
        BlockNode insertBlock = null;
        while (exit != null) {
            if (insertBlock != null && isPathExists(loopExit, exit)) {
                // found cross
                if (canInsertBreak(insertBlock)) {
                    insertEdge = new Edge(insertBlock, insertBlock.getSuccessors().get(0));
                    confirm = true;
                    break;
                }
                return false;
            }
            insertBlock = exit;
            List<BlockNode> cs = exit.getCleanSuccessors();
            exit = cs.size() == 1 ? cs.get(0) : null;
        }
    }
    if (!confirm) {
        return false;
    }
    InsnNode breakInsn = new InsnNode(InsnType.BREAK, 0);
    breakInsn.addAttr(AType.LOOP, loop);
    EdgeInsnAttr.addEdgeInsn(insertEdge, breakInsn);
    stack.addExit(exit);
    // add label to 'break' if needed
    addBreakLabel(exitEdge, exit, breakInsn);
    return true;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) LoopInfo(jadx.core.dex.attributes.nodes.LoopInfo) Edge(jadx.core.dex.nodes.Edge)

Aggregations

BlockNode (jadx.core.dex.nodes.BlockNode)7 Edge (jadx.core.dex.nodes.Edge)7 ArrayList (java.util.ArrayList)4 LoopInfo (jadx.core.dex.attributes.nodes.LoopInfo)3 IRegion (jadx.core.dex.nodes.IRegion)3 Region (jadx.core.dex.regions.Region)3 SwitchRegion (jadx.core.dex.regions.SwitchRegion)3 SynchronizedRegion (jadx.core.dex.regions.SynchronizedRegion)3 IfRegion (jadx.core.dex.regions.conditions.IfRegion)3 LoopRegion (jadx.core.dex.regions.loops.LoopRegion)3 InsnNode (jadx.core.dex.nodes.InsnNode)2 IfInfo (jadx.core.dex.regions.conditions.IfInfo)2 IfMakerHelper.makeIfInfo (jadx.core.dex.visitors.regions.IfMakerHelper.makeIfInfo)2 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)2 AFlag (jadx.core.dex.attributes.AFlag)1 AType (jadx.core.dex.attributes.AType)1 EdgeInsnAttr (jadx.core.dex.attributes.nodes.EdgeInsnAttr)1 LoopLabelAttr (jadx.core.dex.attributes.nodes.LoopLabelAttr)1 IfNode (jadx.core.dex.instructions.IfNode)1 InsnType (jadx.core.dex.instructions.InsnType)1