Exemple #1
0
 private static boolean canInsertContinue(
     BlockNode pred,
     List<BlockNode> predecessors,
     BlockNode loopEnd,
     Set<BlockNode> loopExitNodes) {
   if (!pred.contains(AFlag.SYNTHETIC) || BlockUtils.checkLastInsnType(pred, InsnType.CONTINUE)) {
     return false;
   }
   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;
   }
   if (isDominatedOnBlocks(codePred, predecessors)) {
     return false;
   }
   boolean gotoExit = false;
   for (BlockNode exit : loopExitNodes) {
     if (BlockUtils.isPathExists(codePred, exit)) {
       gotoExit = true;
       break;
     }
   }
   return gotoExit;
 }
Exemple #2
0
 private static void renameVar(MethodNode mth, SSAVar[] vars, int[] vers, BlockNode block) {
   SSAVar[] inputVars = Arrays.copyOf(vars, vars.length);
   for (InsnNode insn : block.getInstructions()) {
     if (insn.getType() != InsnType.PHI) {
       for (InsnArg arg : insn.getArguments()) {
         if (!arg.isRegister()) {
           continue;
         }
         RegisterArg reg = (RegisterArg) arg;
         int regNum = reg.getRegNum();
         SSAVar var = vars[regNum];
         if (var == null) {
           throw new JadxRuntimeException(
               "Not initialized variable reg: "
                   + regNum
                   + ", insn: "
                   + insn
                   + ", block:"
                   + block
                   + ", method: "
                   + mth);
         }
         var.use(reg);
       }
     }
     RegisterArg result = insn.getResult();
     if (result != null) {
       int regNum = result.getRegNum();
       vars[regNum] = mth.makeNewSVar(regNum, vers, result);
     }
   }
   for (BlockNode s : block.getSuccessors()) {
     PhiListAttr phiList = s.get(AType.PHI_LIST);
     if (phiList == null) {
       continue;
     }
     int j = s.getPredecessors().indexOf(block);
     if (j == -1) {
       throw new JadxRuntimeException("Can't find predecessor for " + block + " " + s);
     }
     for (PhiInsn phiInsn : phiList.getList()) {
       if (j >= phiInsn.getArgsCount()) {
         continue;
       }
       int regNum = phiInsn.getResult().getRegNum();
       SSAVar var = vars[regNum];
       if (var == null) {
         continue;
       }
       var.use(phiInsn.getArg(j));
       var.setUsedInPhi(phiInsn);
     }
   }
   for (BlockNode domOn : block.getDominatesOn()) {
     renameVar(mth, vars, vers, domOn);
   }
   System.arraycopy(inputVars, 0, vars, 0, vars.length);
 }
Exemple #3
0
 private static void addPhi(BlockNode block, int regNum) {
   PhiListAttr phiList = block.get(AType.PHI_LIST);
   if (phiList == null) {
     phiList = new PhiListAttr();
     block.addAttr(phiList);
   }
   PhiInsn phiInsn = new PhiInsn(regNum, block.getPredecessors().size());
   phiList.getList().add(phiInsn);
   phiInsn.setOffset(block.getStartOffset());
   block.getInstructions().add(0, phiInsn);
 }
Exemple #4
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;
 }
Exemple #5
0
 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) {
     if (canInsertContinue(pred, predecessors, loopEnd, loopExitNodes)) {
       InsnNode cont = new InsnNode(InsnType.CONTINUE, 0);
       pred.getInstructions().add(cont);
     }
   }
 }
Exemple #6
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;
         }
       }
     }
   }
 }
Exemple #7
0
 public void processTryCatchBlocks(MethodNode mth) {
   Set<TryCatchBlock> tcs = new HashSet<TryCatchBlock>();
   for (ExceptionHandler handler : mth.getExceptionHandlers()) {
     tcs.add(handler.getTryBlock());
   }
   for (TryCatchBlock tc : tcs) {
     List<BlockNode> blocks = new ArrayList<BlockNode>(tc.getHandlersCount());
     Set<BlockNode> splitters = new HashSet<BlockNode>();
     for (ExceptionHandler handler : tc.getHandlers()) {
       BlockNode handlerBlock = handler.getHandlerBlock();
       if (handlerBlock != null) {
         blocks.add(handlerBlock);
         splitters.addAll(handlerBlock.getPredecessors());
       } else {
         LOG.debug(ErrorsCounter.formatErrorMsg(mth, "No exception handler block: " + handler));
       }
     }
     Set<BlockNode> exits = new HashSet<BlockNode>();
     for (BlockNode splitter : splitters) {
       for (BlockNode handler : blocks) {
         List<BlockNode> s = splitter.getSuccessors();
         if (s.isEmpty()) {
           LOG.debug(ErrorsCounter.formatErrorMsg(mth, "No successors for splitter: " + splitter));
           continue;
         }
         BlockNode ss = s.get(0);
         BlockNode cross = BlockUtils.getPathCross(mth, ss, handler);
         if (cross != null && cross != ss && cross != handler) {
           exits.add(cross);
         }
       }
     }
     for (ExceptionHandler handler : tc.getHandlers()) {
       processExcHandler(handler, exits);
     }
   }
 }