Guest User

Untitled

a guest
Nov 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package org.saev.deob.trans;
  2.  
  3. import org.saev.deob.*;
  4. import java.util.*;
  5. import org.apache.bcel.generic.*;
  6. import org.apache.bcel.classfile.*;
  7. import org.apache.bcel.util.*;
  8.  
  9. public class UnconditionalBranchFolder extends ApplicationTransformer {
  10.     private static final boolean DEBUG = false;
  11.  
  12.     public UnconditionalBranchFolder() {
  13.         super();
  14.     }
  15.  
  16.     protected Method internalTransform(MethodGen mg) {
  17.         int n = 0;
  18.         InstructionList il = mg.getInstructionList();
  19.         Iterator<InstructionHandle[]> it = new InstructionFinder(il).search("IfInstruction");
  20.  
  21.         while(it.hasNext()) {
  22.             InstructionHandle ih = it.next() [0];
  23.             InstructionHandle target = ((BranchHandle) ih).getTarget();
  24.            
  25.             if(target.getInstruction() instanceof GOTO) {
  26.                 n++;
  27.                 ((BranchHandle) ih).setTarget(((BranchHandle) target).getTarget());
  28.             }
  29.         }
  30.        
  31.         if(DEBUG && n > 0) {
  32.             System.out.println("[ " + new org.saev.deob.util.MethodID(mg) +
  33.                 " ] retargeted " + n + " instructions");
  34.         }
  35.         it = new InstructionFinder(il).search("GOTO GOTO");
  36.        
  37.         while(it.hasNext()) {
  38.             InstructionHandle[] match = it.next();
  39.            
  40.             try {
  41.                 il.delete(match[0]);
  42.             } catch(TargetLostException e) {
  43.                 InstructionHandle[] targets = e.getTargets();
  44.                
  45.                 for(int j = 0; j < targets.length; j++) {
  46.                     InstructionTargeter[] targeters = targets[j].getTargeters();
  47.                    
  48.                     for(int k = 0; k < targeters.length; k++) {
  49.                         targeters[k].updateTarget(targets[j], match[1]);
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.            
  55.         mg.setInstructionList(il);
  56.         return mg.getMethod();
  57.     }
  58.  
  59.     public String getName() {
  60.         return "ub-folder";
  61.     }
  62.  
  63.     public String getDescription() {
  64.         return "folds unconditional branches, ie. if instructions\n" +
  65.             "\tthat target goto instructions are modified to target the\n" +
  66.             "\ttarget of the goto. in some cases the goto can be removed.\n" +
  67.             "\tyes, i realize that description sucks, but its the best i\n" +
  68.             "\tcould come up with.";
  69.     }
  70.  
  71.     public String getAuthor() {
  72.         return "saevion <saevion at gmail dot com>";
  73.     }
  74. }
Add Comment
Please, Sign In to add comment