Package jadx.core.dex.nodes

Examples of jadx.core.dex.nodes.BlockNode


    @Override
    public void processBlockTraced(MethodNode mth, IBlock container, IRegion currentRegion) {
      if (container.getClass() != BlockNode.class) {
        return;
      }
      BlockNode block = (BlockNode) container;
      if (block.contains(AFlag.RETURN)) {
        List<InsnNode> insns = block.getInstructions();
        if (insns.size() == 1
            && blockNotInLoop(mth, block)
            && noTrailInstructions(block)) {
          insns.remove(0);
          block.remove(AFlag.RETURN);
        }
      }
    }
View Full Code Here


     * Check if container not contains instructions,
     * don't count one 'return' instruction (it will be removed later).
     */
    private static boolean isEmpty(IContainer container) {
      if (container instanceof BlockNode) {
        BlockNode block = (BlockNode) container;
        return block.getInstructions().isEmpty() || block.contains(AFlag.RETURN);
      } else if (container instanceof IRegion) {
        IRegion region = (IRegion) container;
        for (IContainer block : region.getSubBlocks()) {
          if (!isEmpty(block)) {
            return false;
View Full Code Here

    if (regionsCount > REGIONS_LIMIT) {
      throw new JadxOverflowException("Regions count limit reached");
    }

    Region r = new Region(stack.peekRegion());
    BlockNode next = startBlock;
    while (next != null) {
      next = traverse(r, next, stack);
    }
    return r;
  }
View Full Code Here

  /**
   * Recursively traverse all blocks from 'block' until block from 'exits'
   */
  private BlockNode traverse(IRegion r, BlockNode block, RegionStack stack) {
    BlockNode next = null;
    boolean processed = false;

    List<LoopInfo> loops = block.getAll(AType.LOOP);
    int loopCount = loops.size();
    if (loopCount != 0 && block.contains(AFlag.LOOP_START)) {
View Full Code Here

    }
    return null;
  }

  private BlockNode processLoop(IRegion curRegion, LoopInfo loop, RegionStack stack) {
    BlockNode loopStart = loop.getStart();
    Set<BlockNode> exitBlocksSet = loop.getExitNodes();

    // set exit blocks scan order priority
    // this can help if loop have several exits (after using 'break' or 'return' in loop)
    List<BlockNode> exitBlocks = new ArrayList<BlockNode>(exitBlocksSet.size());
    BlockNode nextStart = getNextBlock(loopStart);
    if (nextStart != null && exitBlocksSet.remove(nextStart)) {
      exitBlocks.add(nextStart);
    }
    if (exitBlocksSet.remove(loopStart)) {
      exitBlocks.add(loopStart);
    }
    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()
View Full Code Here

    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;
    }
    stack.pop();
    loopStart.addAttr(AType.LOOP, loop);
    return loopExit;
View Full Code Here

    }
    return true;
  }

  private boolean insertBreak(RegionStack stack, BlockNode loopExit, Edge exitEdge) {
    BlockNode exit = exitEdge.getTarget();
    BlockNode insertBlock = null;
    boolean confirm = false;
    // process special cases
    if (loopExit == exit) {
      // try/catch at loop end
      BlockNode source = exitEdge.getSource();
      if (source.contains(AType.CATCH_BLOCK)
          && source.getSuccessors().size() == 2) {
        BlockNode other = BlockUtils.selectOther(loopExit, source.getSuccessors());
        if (other != null) {
          other = BlockUtils.skipSyntheticSuccessor(other);
          if (other.contains(AType.EXC_HANDLER)) {
            insertBlock = source;
            confirm = true;
          }
        }
      }
View Full Code Here

    addBreakLabel(exitEdge, exit, breakInsn);
    return true;
  }

  private void addBreakLabel(Edge exitEdge, BlockNode exit, InsnNode breakInsn) {
    BlockNode outBlock = BlockUtils.getNextBlock(exitEdge.getTarget());
    if (outBlock == null) {
      return;
    }
    List<LoopInfo> exitLoop = mth.getAllLoopsForBlock(outBlock);
    if (!exitLoop.isEmpty()) {
View Full Code Here

      parentLoop.getStart().addAttr(labelAttr);
    }
  }

  private static void insertContinue(LoopInfo loop) {
    BlockNode loopEnd = loop.getEnd();
    List<BlockNode> predecessors = loopEnd.getPredecessors();
    if (predecessors.size() <= 1) {
      return;
    }
    Set<BlockNode> loopExitNodes = loop.getExitNodes();
    for (BlockNode pred : predecessors) {
View Full Code Here

    }
    List<BlockNode> preds = pred.getPredecessors();
    if (preds.isEmpty()) {
      return false;
    }
    BlockNode codePred = preds.get(0);
    if (codePred.contains(AFlag.SKIP)) {
      return false;
    }
    if (loopEnd.isDominator(codePred)
        || loopExitNodes.contains(codePred)) {
      return false;
View Full Code Here

TOP

Related Classes of jadx.core.dex.nodes.BlockNode

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.