Package jadx.core.dex.regions.loops

Examples of jadx.core.dex.regions.loops.LoopRegion


    if (mth.getLoopsCount() != 0) {
      DepthRegionTraversal.traverseAll(mth, new AbstractRegionVisitor() {
        @Override
        public void enterRegion(MethodNode mth, IRegion region) {
          if (region instanceof LoopRegion) {
            LoopRegion loop = (LoopRegion) region;
            loop.mergePreCondition();
          }
        }
      });
    }
View Full Code Here


      }
    }

    private void regionProcess(MethodNode mth, IRegion region) {
      if (region instanceof LoopRegion) {
        LoopRegion loopRegion = (LoopRegion) region;
        LoopType loopType = loopRegion.getType();
        if (loopType instanceof ForLoop) {
          ForLoop forLoop = (ForLoop) loopType;
          processInsn(forLoop.getInitInsn(), region);
          processInsn(forLoop.getIncrInsn(), region);
        }
View Full Code Here

   * 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) {
    IRegion region = replaceRegion;
    if (region instanceof LoopRegion) {
      LoopRegion loop = (LoopRegion) region;
      region = loop.getBody();
    }

    Region tryRegion = new Region(region);
    List<IContainer> subBlocks = region.getSubBlocks();
    for (IContainer cont : subBlocks) {
View Full Code Here

    if (exitBlocksSet.remove(loop.getEnd())) {
      exitBlocks.add(loop.getEnd());
    }
    exitBlocks.addAll(exitBlocksSet);

    LoopRegion loopRegion = makeLoopRegion(curRegion, loop, exitBlocks);
    if (loopRegion == null) {
      BlockNode exit = makeEndlessLoop(curRegion, stack, loop, loopStart);
      insertContinue(loop);
      return exit;
    }
    curRegion.getSubBlocks().add(loopRegion);
    IRegion outerRegion = stack.peekRegion();
    stack.push(loopRegion);

    IfInfo condInfo = makeIfInfo(loopRegion.getHeader());
    condInfo = searchNestedIf(condInfo);
    confirmMerge(condInfo);
    if (!loop.getLoopBlocks().contains(condInfo.getThenBlock())) {
      // invert loop condition if 'then' points to exit
      condInfo = IfInfo.invert(condInfo);
    }
    loopRegion.setCondition(condInfo.getCondition());
    exitBlocks.removeAll(condInfo.getMergedBlocks());

    if (!exitBlocks.isEmpty()) {
      BlockNode loopExit = condInfo.getElseBlock();
      if (loopExit != null) {
        // add 'break' instruction before path cross between main loop exit and sub-exit
        for (Edge exitEdge : loop.getExitEdges()) {
          if (!exitBlocks.contains(exitEdge.getSource())) {
            continue;
          }
          insertBreak(stack, loopExit, exitEdge);
        }
      }
    }

    BlockNode out;
    if (loopRegion.isConditionAtEnd()) {
      BlockNode thenBlock = condInfo.getThenBlock();
      out = (thenBlock == loopStart ? condInfo.getElseBlock() : thenBlock);
      loopStart.remove(AType.LOOP);
      loop.getEnd().add(AFlag.SKIP);
      stack.addExit(loop.getEnd());
      loopRegion.setBody(makeRegion(loopStart, stack));
      loopStart.addAttr(AType.LOOP, loop);
      loop.getEnd().remove(AFlag.SKIP);
    } else {
      out = condInfo.getElseBlock();
      if (outerRegion != null
          && out.contains(AFlag.LOOP_START)
          && !out.getAll(AType.LOOP).contains(loop)
          && RegionUtils.isRegionContainsBlock(outerRegion, out)) {
        // exit to already processed outer loop
        out = null;
      }
      stack.addExit(out);
      BlockNode loopBody = condInfo.getThenBlock();
      Region body = makeRegion(loopBody, stack);
      // add blocks from loop start to first condition block
      BlockNode conditionBlock = condInfo.getIfBlock();
      if (loopStart != conditionBlock) {
        Set<BlockNode> blocks = BlockUtils.getAllPathsBlocks(loopStart, conditionBlock);
        blocks.remove(conditionBlock);
        for (BlockNode block : blocks) {
          if (block.getInstructions().isEmpty()
              && !block.contains(AFlag.SKIP)
              && !RegionUtils.isRegionContainsBlock(body, block)) {
            body.add(block);
          }
        }
      }
      loopRegion.setBody(body);
    }
    stack.pop();
    insertContinue(loop);
    return out;
  }
View Full Code Here

      List<LoopInfo> loops = block.getAll(AType.LOOP);
      if (!loops.isEmpty() && loops.get(0) != loop) {
        // skip nested loop condition
        continue;
      }
      LoopRegion loopRegion = new LoopRegion(curRegion, loop, block, block == loop.getEnd());
      boolean found;
      if (block == loop.getStart() || block == loop.getEnd()
          || BlockUtils.isEmptySimplePath(loop.getStart(), block)) {
        found = true;
      } else if (block.getPredecessors().contains(loop.getStart())) {
        loopRegion.setPreCondition(loop.getStart());
        // if we can't merge pre-condition this is not correct header
        found = loopRegion.checkPreCondition();
      } else {
        found = false;
      }
      if (found) {
        List<LoopInfo> list = mth.getAllLoopsForBlock(block);
View Full Code Here

    // no exit found => endless loop
    return null;
  }

  private BlockNode makeEndlessLoop(IRegion curRegion, RegionStack stack, LoopInfo loop, BlockNode loopStart) {
    LoopRegion loopRegion = new LoopRegion(curRegion, loop, null, false);
    curRegion.getSubBlocks().add(loopRegion);

    loopStart.remove(AType.LOOP);
    stack.push(loopRegion);

    BlockNode loopExit = null;
    // insert 'break' for exits
    List<Edge> exitEdges = loop.getExitEdges();
    for (Edge exitEdge : exitEdges) {
      BlockNode exit = exitEdge.getTarget();
      if (insertBreak(stack, exit, exitEdge)) {
        BlockNode nextBlock = getNextBlock(exit);
        if (nextBlock != null) {
          stack.addExit(nextBlock);
          loopExit = nextBlock;
        }
      }
    }

    Region body = makeRegion(loopStart, stack);
    BlockNode loopEnd = loop.getEnd();
    if (!RegionUtils.isRegionContainsBlock(body, loopEnd)
        && !loopEnd.contains(AType.EXC_HANDLER)) {
      body.getSubBlocks().add(loopEnd);
    }
    loopRegion.setBody(body);

    if (loopExit == null) {
      BlockNode next = getNextBlock(loopEnd);
      loopExit = RegionUtils.isRegionContainsBlock(body, next) ? null : next;
    }
View Full Code Here

TOP

Related Classes of jadx.core.dex.regions.loops.LoopRegion

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.