Example #1
0
 /** Select loop exit and construct LoopRegion */
 private LoopRegion makeLoopRegion(IRegion curRegion, LoopInfo loop, List<BlockNode> exitBlocks) {
   for (BlockNode block : exitBlocks) {
     if (block.contains(AType.EXC_HANDLER)
         || block.getInstructions().size() != 1
         || block.getInstructions().get(0).getType() != InsnType.IF) {
       continue;
     }
     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);
       if (list.size() >= 2) {
         // bad condition if successors going out of all loops
         boolean allOuter = true;
         for (BlockNode outerBlock : block.getCleanSuccessors()) {
           List<LoopInfo> outLoopList = mth.getAllLoopsForBlock(outerBlock);
           outLoopList.remove(loop);
           if (!outLoopList.isEmpty()) {
             // goes to outer loop
             allOuter = false;
             break;
           }
         }
         if (allOuter) {
           found = false;
         }
       }
     }
     if (found) {
       return loopRegion;
     }
   }
   // no exit found => endless loop
   return null;
 }
Example #2
0
 private static void insertContinueInSwitch(BlockNode block, BlockNode out, BlockNode end) {
   int endId = end.getId();
   for (BlockNode s : block.getCleanSuccessors()) {
     if (s.getDomFrontier().get(endId) && s != out) {
       // search predecessor of loop end on path from this successor
       List<BlockNode> list = BlockUtils.collectBlocksDominatedBy(s, s);
       for (BlockNode p : end.getPredecessors()) {
         if (list.contains(p)) {
           if (p.isSynthetic()) {
             p.getInstructions().add(new InsnNode(InsnType.CONTINUE, 0));
           }
           break;
         }
       }
     }
   }
 }
Example #3
0
 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;
         }
       }
     }
   }
   if (!confirm) {
     while (exit != null) {
       if (insertBlock != null && isPathExists(loopExit, exit)) {
         // found cross
         if (canInsertBreak(insertBlock)) {
           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);
   insertBlock.getInstructions().add(breakInsn);
   stack.addExit(exit);
   // add label to 'break' if needed
   addBreakLabel(exitEdge, exit, breakInsn);
   return true;
 }
Example #4
0
 /** Traverse from monitor-enter thru successors and search for exit paths cross */
 private static BlockNode traverseMonitorExitsCross(
     BlockNode block, Set<BlockNode> exits, Set<BlockNode> visited) {
   visited.add(block);
   for (BlockNode node : block.getCleanSuccessors()) {
     boolean cross = true;
     for (BlockNode exitBlock : exits) {
       boolean p = isPathExists(exitBlock, node);
       if (!p) {
         cross = false;
         break;
       }
     }
     if (cross) {
       return node;
     }
     if (!visited.contains(node)) {
       BlockNode res = traverseMonitorExitsCross(node, exits, visited);
       if (res != null) {
         return res;
       }
     }
   }
   return null;
 }
Example #5
0
  private BlockNode processSwitch(
      IRegion currentRegion, BlockNode block, SwitchNode insn, RegionStack stack) {
    SwitchRegion sw = new SwitchRegion(currentRegion, block);
    currentRegion.getSubBlocks().add(sw);

    int len = insn.getTargets().length;
    // sort by target
    Map<Integer, List<Object>> casesMap = new LinkedHashMap<Integer, List<Object>>(len);
    for (int i = 0; i < len; i++) {
      Object key = insn.getKeys()[i];
      int targ = insn.getTargets()[i];
      List<Object> keys = casesMap.get(targ);
      if (keys == null) {
        keys = new ArrayList<Object>(2);
        casesMap.put(targ, keys);
      }
      keys.add(key);
    }

    Map<BlockNode, List<Object>> blocksMap = new LinkedHashMap<BlockNode, List<Object>>(len);
    for (Map.Entry<Integer, List<Object>> entry : casesMap.entrySet()) {
      BlockNode c = getBlockByOffset(entry.getKey(), block.getSuccessors());
      assert c != null;
      blocksMap.put(c, entry.getValue());
    }
    BlockNode defCase = getBlockByOffset(insn.getDefaultCaseOffset(), block.getSuccessors());
    if (defCase != null) {
      blocksMap.remove(defCase);
    }
    LoopInfo loop = mth.getLoopForBlock(block);

    Map<BlockNode, BlockNode> fallThroughCases = new LinkedHashMap<BlockNode, BlockNode>();

    List<BlockNode> basicBlocks = mth.getBasicBlocks();
    BitSet outs = new BitSet(basicBlocks.size());
    outs.or(block.getDomFrontier());
    for (BlockNode s : block.getCleanSuccessors()) {
      BitSet df = s.getDomFrontier();
      // fall through case block
      if (df.cardinality() > 1) {
        if (df.cardinality() > 2) {
          LOG.debug("Unexpected case pattern, block: {}, mth: {}", s, mth);
        } else {
          BlockNode first = basicBlocks.get(df.nextSetBit(0));
          BlockNode second = basicBlocks.get(df.nextSetBit(first.getId() + 1));
          if (second.getDomFrontier().get(first.getId())) {
            fallThroughCases.put(s, second);
            df = new BitSet(df.size());
            df.set(first.getId());
          } else if (first.getDomFrontier().get(second.getId())) {
            fallThroughCases.put(s, first);
            df = new BitSet(df.size());
            df.set(second.getId());
          }
        }
      }
      outs.or(df);
    }
    outs.clear(block.getId());
    if (loop != null) {
      outs.clear(loop.getStart().getId());
    }

    stack.push(sw);
    stack.addExits(BlockUtils.bitSetToBlocks(mth, outs));

    // check cases order if fall through case exists
    if (!fallThroughCases.isEmpty()) {
      if (isBadCasesOrder(blocksMap, fallThroughCases)) {
        LOG.debug("Fixing incorrect switch cases order, method: {}", mth);
        blocksMap = reOrderSwitchCases(blocksMap, fallThroughCases);
        if (isBadCasesOrder(blocksMap, fallThroughCases)) {
          LOG.error("Can't fix incorrect switch cases order, method: {}", mth);
          mth.add(AFlag.INCONSISTENT_CODE);
        }
      }
    }

    // filter 'out' block
    if (outs.cardinality() > 1) {
      // remove exception handlers
      BlockUtils.cleanBitSet(mth, outs);
    }
    if (outs.cardinality() > 1) {
      // filter loop start and successors of other blocks
      for (int i = outs.nextSetBit(0); i >= 0; i = outs.nextSetBit(i + 1)) {
        BlockNode b = basicBlocks.get(i);
        outs.andNot(b.getDomFrontier());
        if (b.contains(AFlag.LOOP_START)) {
          outs.clear(b.getId());
        } else {
          for (BlockNode s : b.getCleanSuccessors()) {
            outs.clear(s.getId());
          }
        }
      }
    }

    if (loop != null && outs.cardinality() > 1) {
      outs.clear(loop.getEnd().getId());
    }
    if (outs.cardinality() == 0) {
      // one or several case blocks are empty,
      // run expensive algorithm for find 'out' block
      for (BlockNode maybeOut : block.getSuccessors()) {
        boolean allReached = true;
        for (BlockNode s : block.getSuccessors()) {
          if (!isPathExists(s, maybeOut)) {
            allReached = false;
            break;
          }
        }
        if (allReached) {
          outs.set(maybeOut.getId());
          break;
        }
      }
    }
    BlockNode out = null;
    if (outs.cardinality() == 1) {
      out = basicBlocks.get(outs.nextSetBit(0));
      stack.addExit(out);
    } else if (loop == null && outs.cardinality() > 1) {
      LOG.warn("Can't detect out node for switch block: {} in {}", block, mth);
    }
    if (loop != null) {
      // check if 'continue' must be inserted
      BlockNode end = loop.getEnd();
      if (out != end && out != null) {
        insertContinueInSwitch(block, out, end);
      }
    }

    if (!stack.containsExit(defCase)) {
      sw.setDefaultCase(makeRegion(defCase, stack));
    }
    for (Entry<BlockNode, List<Object>> entry : blocksMap.entrySet()) {
      BlockNode caseBlock = entry.getKey();
      if (stack.containsExit(caseBlock)) {
        // empty case block
        sw.addCase(entry.getValue(), new Region(stack.peekRegion()));
      } else {
        BlockNode next = fallThroughCases.get(caseBlock);
        stack.addExit(next);
        Region caseRegion = makeRegion(caseBlock, stack);
        stack.removeExit(next);
        if (next != null) {
          next.add(AFlag.FALL_THROUGH);
          caseRegion.add(AFlag.FALL_THROUGH);
        }
        sw.addCase(entry.getValue(), caseRegion);
        // 'break' instruction will be inserted in RegionMakerVisitor.PostRegionVisitor
      }
    }

    stack.pop();
    return out;
  }