Package jadx.core.dex.nodes

Examples of jadx.core.dex.nodes.BlockNode


  private static boolean sameElements(Collection<BlockNode> c1, Collection<BlockNode> c2) {
    return c1.size() == c2.size() && c1.containsAll(c2);
  }

  static IfInfo mergeNestedIfNodes(IfInfo currentIf) {
    BlockNode curThen = currentIf.getThenBlock();
    BlockNode curElse = currentIf.getElseBlock();
    if (curThen == curElse) {
      return null;
    }
    boolean followThenBranch;
    IfInfo nextIf = getNextIf(currentIf, curThen);
    if (nextIf != null) {
      followThenBranch = true;
    } else {
      nextIf = getNextIf(currentIf, curElse);
      if (nextIf != null) {
        followThenBranch = false;
      } else {
        return null;
      }
    }
    if (isInversionNeeded(currentIf, nextIf)) {
      // invert current node for match pattern
      nextIf = IfInfo.invert(nextIf);
    }
    if (!RegionMaker.isEqualPaths(curElse, nextIf.getElseBlock())
        && !RegionMaker.isEqualPaths(curThen, nextIf.getThenBlock())) {
      // complex condition, run additional checks
      if (checkConditionBranches(curThen, curElse)
          || checkConditionBranches(curElse, curThen)) {
        return null;
      }
      BlockNode otherBranchBlock = followThenBranch ? curElse : curThen;
      if (!isPathExists(nextIf.getIfBlock(), otherBranchBlock)) {
        return checkForTernaryInCondition(currentIf);
      }
      if (isPathExists(nextIf.getThenBlock(), otherBranchBlock)
          && isPathExists(nextIf.getElseBlock(), otherBranchBlock)) {
View Full Code Here


    IfCondition condition = IfCondition.merge(mergeOperation, first.getCondition(), second.getCondition());
    IfInfo result = new IfInfo(condition, second);
    result.setIfBlock(first.getIfBlock());
    result.merge(first, second);

    BlockNode otherPathBlock = followThenBranch ? first.getElseBlock() : first.getThenBlock();
    skipSimplePath(otherPathBlock, result.getSkipBlocks());
    return result;
  }
View Full Code Here

  private static IfInfo getNextIf(IfInfo info, BlockNode block) {
    if (!canSelectNext(info, block)) {
      return null;
    }
    BlockNode nestedIfBlock = getNextIfNode(block);
    if (nestedIfBlock != null) {
      return makeIfInfo(nestedIfBlock);
    }
    return null;
  }
View Full Code Here

    List<BlockNode> successors = block.getSuccessors();
    if (successors.size() != 1) {
      return null;
    }

    BlockNode next = successors.get(0);
    if (next.getPredecessors().size() != 1) {
      return null;
    }
    boolean pass = true;
    if (!insns.isEmpty()) {
      // check that all instructions can be inlined
View Full Code Here

    }
    RegisterArg arg = args.get(0);
    if (!arg.getType().equals(fieldsCls.getClassInfo().getType())) {
      return false;
    }
    BlockNode block = mth.getBasicBlocks().get(0);
    List<InsnNode> instructions = block.getInstructions();
    if (instructions.isEmpty()) {
      return false;
    }
    InsnNode insn = instructions.get(0);
    if (insn.getType() != InsnType.IPUT) {
View Full Code Here

    IContainer thenRegion = ifRegion.getThenRegion();
    IContainer elseRegion = ifRegion.getElseRegion();
    if (thenRegion == null || elseRegion == null) {
      return false;
    }
    BlockNode tb = getTernaryInsnBlock(thenRegion);
    BlockNode eb = getTernaryInsnBlock(elseRegion);
    if (tb == null || eb == null) {
      return false;
    }
    BlockNode header = ifRegion.getHeader();
    InsnNode t = tb.getInstructions().get(0);
    InsnNode e = eb.getInstructions().get(0);

    if (t.getSourceLine() != e.getSourceLine()) {
      if (t.getSourceLine() != 0 && e.getSourceLine() != 0) {
        // sometimes source lines incorrect
        if (!checkLineStats(t, e)) {
          return false;
        }
      } else {
        // no debug info
        if (containsTernary(t) || containsTernary(e)) {
          // don't make nested ternary by default
          // TODO: add addition checks
          return false;
        }
      }
    }

    if (t.getResult() != null && e.getResult() != null) {
      PhiInsn phi = t.getResult().getSVar().getUsedInPhi();
      if (phi == null || !t.getResult().equalRegisterAndType(e.getResult())) {
        return false;
      }
      if (!ifRegion.getParent().replaceSubBlock(ifRegion, header)) {
        return false;
      }
      InsnList.remove(tb, t);
      InsnList.remove(eb, e);

      RegisterArg resArg;
      if (phi.getArgsCount() == 2) {
        resArg = phi.getResult();
      } else {
        resArg = t.getResult();
        phi.removeArg(e.getResult());
      }
      TernaryInsn ternInsn = new TernaryInsn(ifRegion.getCondition(),
          resArg, InsnArg.wrapArg(t), InsnArg.wrapArg(e));
      ternInsn.setSourceLine(t.getSourceLine());

      // remove 'if' instruction
      header.getInstructions().clear();
      header.getInstructions().add(ternInsn);

      // shrink method again
      CodeShrinker.shrinkMethod(mth);
      return true;
    }

    if (!mth.getReturnType().equals(ArgType.VOID)
        && t.getType() == InsnType.RETURN && e.getType() == InsnType.RETURN) {

      if (!ifRegion.getParent().replaceSubBlock(ifRegion, header)) {
        return false;
      }
      InsnList.remove(tb, t);
      InsnList.remove(eb, e);
      tb.remove(AFlag.RETURN);
      eb.remove(AFlag.RETURN);

      TernaryInsn ternInsn = new TernaryInsn(ifRegion.getCondition(), null, t.getArg(0), e.getArg(0));
      ternInsn.setSourceLine(t.getSourceLine());
      InsnNode retInsn = new InsnNode(InsnType.RETURN, 1);
      retInsn.addArg(InsnArg.wrapArg(ternInsn));

      header.getInstructions().clear();
      header.getInstructions().add(retInsn);
      header.add(AFlag.RETURN);

      CodeShrinker.shrinkMethod(mth);
      return true;
    }
    return false;
View Full Code Here

    if (thenRegion instanceof Region) {
      Region r = (Region) thenRegion;
      if (r.getSubBlocks().size() == 1) {
        IContainer container = r.getSubBlocks().get(0);
        if (container instanceof BlockNode) {
          BlockNode block = (BlockNode) container;
          if (block.getInstructions().size() == 1) {
            return block;
          }
        }
      }
    }
View Full Code Here

      @Override
      public void processBlock(MethodNode mth, IBlock container) {
        if (!(container instanceof BlockNode)) {
          return;
        }
        BlockNode block = (BlockNode) container;
        if (blocksInRegions.add(block)) {
          return;
        }
        if (!block.contains(AFlag.RETURN)
            && !block.contains(AFlag.SKIP)
            && !block.contains(AFlag.SYNTHETIC)
            && !block.getInstructions().isEmpty()) {
          // TODO
          // mth.add(AFlag.INCONSISTENT_CODE);
          LOG.debug(" Duplicated block: {} in {}", block, mth);
          // printRegionsWithBlock(mth, block);
        }
      }
    });
    if (mth.getBasicBlocks().size() != blocksInRegions.size()) {
      for (BlockNode block : mth.getBasicBlocks()) {
        if (!blocksInRegions.contains(block)
            && !block.getInstructions().isEmpty()
            && !block.contains(AFlag.SKIP)) {
          mth.add(AFlag.INCONSISTENT_CODE);
          LOG.debug(" Missing block: {} in {}", block, mth);
        }
      }
    }

    // check loop conditions
    DepthRegionTraversal.traverseAll(mth, new AbstractRegionVisitor() {
      @Override
      public void enterRegion(MethodNode mth, IRegion region) {
        if (region instanceof LoopRegion) {
          BlockNode loopHeader = ((LoopRegion) region).getHeader();
          if (loopHeader != null && loopHeader.getInstructions().size() != 1) {
            ErrorsCounter.methodError(mth, "Incorrect condition in loop: " + loopHeader);
          }
        }
      }
    });
View Full Code Here

      if (domBlocks.size() != 1) {
        throw new JadxRuntimeException(
            "Exception block dominator not found, method:" + mth + ". bs: " + bs);
      }

      BlockNode domBlock = domBlocks.get(0);

      TryCatchBlock prevTB = tryBlocksMap.put(domBlock, tb);
      if (prevTB != null) {
        LOG.info("!!! TODO: merge try blocks in {}", mth);
      }
View Full Code Here

  }

  private static void checkAndWrap(MethodNode mth, Map<BlockNode, TryCatchBlock> tryBlocksMap, IRegion region) {
    // search dominator blocks in this region (don't need to go deeper)
    for (Map.Entry<BlockNode, TryCatchBlock> entry : tryBlocksMap.entrySet()) {
      BlockNode dominator = entry.getKey();
      if (region.getSubBlocks().contains(dominator)) {
        TryCatchBlock tb = tryBlocksMap.get(dominator);
        if (!wrapBlocks(region, tb, dominator)) {
          ErrorsCounter.methodError(mth, "Can't wrap try/catch for " + region);
        }
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.