Advertisement
Guest User

Verifier Jonas

a guest
May 24th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.37 KB | None | 0 0
  1. package ch.ethz.sae;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import apron.ApronException;
  6. import soot.Unit;
  7. import soot.jimple.IntConstant;
  8. import soot.jimple.internal.AbstractBinopExpr;
  9. import soot.jimple.internal.JAssignStmt;
  10. import soot.jimple.internal.JDivExpr;
  11. import soot.jimple.internal.JInvokeStmt;
  12. import soot.jimple.internal.JSpecialInvokeExpr;
  13. import soot.jimple.internal.JVirtualInvokeExpr;
  14. import soot.jimple.spark.sets.DoublePointsToSet;
  15. import soot.jimple.spark.sets.P2SetVisitor;
  16. import soot.jimple.spark.SparkTransformer;
  17. import soot.jimple.spark.pag.Node;
  18. import soot.jimple.spark.pag.PAG;
  19. import soot.Local;
  20. import soot.Scene;
  21. import soot.SootClass;
  22. import soot.SootMethod;
  23. import soot.toolkits.graph.BriefUnitGraph;
  24.  
  25. public class Verifier {
  26.    
  27.     public static void main(String[] args) {
  28.         if (args.length != 1) {
  29.             System.err.println("Usage: java -classpath soot-2.5.0.jar:./bin ch.ethz.sae.Verifier <class to test>");
  30.             System.exit(-1);
  31.         }
  32.         String analyzedClass = args[0];
  33.         SootClass c = loadClass(analyzedClass);
  34.  
  35.         PAG pointsToAnalysis = doPointsToAnalysis(c);
  36.  
  37.         int programCorrectFlag = 1;
  38.         int divisionByZeroFlag = 1;
  39.  
  40.         for (SootMethod method : c.getMethods()) {
  41.  
  42.             Analysis analysis = new Analysis(new BriefUnitGraph(method.retrieveActiveBody()), c);
  43.             analysis.run();
  44.  
  45.             if (!verifyBounds(method, analysis, pointsToAnalysis)) {
  46.                 programCorrectFlag = 0;
  47.             }
  48.             if (!verifyDivisionByZero(method, analysis)) {
  49.                 divisionByZeroFlag = 0;
  50.             }
  51.         }
  52.        
  53.         if (divisionByZeroFlag == 1) {
  54.             System.out.println(analyzedClass + " NO_DIV_ZERO");
  55.         } else {
  56.             System.out.println(analyzedClass + " MAY_DIV_ZERO");
  57.         }
  58.        
  59.         if (programCorrectFlag == 1) {
  60.             System.out.println(analyzedClass + " NO_OUT_OF_BOUNDS");
  61.         } else {
  62.             System.out.println(analyzedClass + " MAY_OUT_OF_BOUNDS");
  63.         }
  64.     }
  65.    
  66.     private static boolean verifyDivisionByZero(SootMethod method, Analysis fixPoint) {
  67.         System.out.println("Start Division by zero Verifier");
  68.         for (Unit u : method.retrieveActiveBody().getUnits()) {
  69.             AWrapper state = fixPoint.getFlowBefore(u);
  70.             try {
  71.                     if (state.get().isBottom(Analysis.man)s)
  72.                     // unreachable code
  73.                     continue;
  74.             } catch (ApronException e) {
  75.                 e.printStackTrace();
  76.             }
  77.            
  78.            
  79.             if(u instanceof JAssignStmt){
  80.                 JAssignStmt s = (JAssignStmt) u;
  81.                
  82.                 if(s.getRightOp() instanceof JDivExpr){
  83.                     JDivExpr myDivExpr = (JDivExpr) s.getRightOp();
  84.                     System.out.println(u.toString()+"  "+ u.getClass().getName() + " " + myDivExpr.toString());
  85.                     if(myDivExpr.getOp2() instanceof IntConstant){
  86.                         IntConstant myIntConst = (IntConstant) myDivExpr.getOp2();
  87.                         System.out.println("p1");
  88.                         if(myIntConst.value == 0){
  89.                             System.out.println("p2");
  90.                             return false;
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.            
  96.  
  97.             if(u instanceof AbstractBinopExpr){
  98.                 System.out.println("hello");
  99.             }
  100.             //TODO: Check that all divisors are not zero
  101.         }
  102.        
  103.         //Return false if the method may have division by zero errors
  104.         return true;
  105.     }
  106.  
  107.     private static boolean verifyBounds(SootMethod method, Analysis fixPoint,
  108.             PAG pointsTo) {
  109.                
  110.         //TODO: Create a list of all allocation sites for PrinterArray
  111.        
  112.         for (Unit u : method.retrieveActiveBody().getUnits()) {
  113.             AWrapper state = fixPoint.getFlowBefore(u);
  114.        
  115.             try {
  116.                 if (state.get().isBottom(Analysis.man)) {
  117.                     // unreachable code
  118.                     continue;
  119.                 }
  120.             } catch (ApronException e) {
  121.                 e.printStackTrace();
  122.             }
  123.  
  124.            
  125.             if (u instanceof JInvokeStmt && ((JInvokeStmt) u).getInvokeExpr() instanceof JSpecialInvokeExpr) {
  126.                 // TODO: Get the size of the PrinterArray given as argument to the constructor
  127.                
  128.             }
  129.            
  130.             if (u instanceof JInvokeStmt && ((JInvokeStmt) u).getInvokeExpr() instanceof JVirtualInvokeExpr) {
  131.                
  132.                 JInvokeStmt jInvStmt = (JInvokeStmt)u;
  133.                
  134.                 JVirtualInvokeExpr invokeExpr = (JVirtualInvokeExpr)jInvStmt.getInvokeExpr();
  135.                
  136.                 Local base = (Local) invokeExpr.getBase();
  137.                 DoublePointsToSet pts = (DoublePointsToSet) pointsTo
  138.                         .reachingObjects(base);
  139.                
  140.                 if (invokeExpr.getMethod().getName().equals(Analysis.functionName)) {
  141.                    
  142.                     // TODO: Check whether the 'sendJob' method's argument is within bounds
  143.                    
  144.                     // Visit all allocation sites that the base pointer may reference
  145.                     MyP2SetVisitor visitor = new MyP2SetVisitor();
  146.                     pts.forall(visitor);
  147.                 }
  148.             }
  149.         }
  150.  
  151.         return false;
  152.     }
  153.  
  154.     private static SootClass loadClass(String name) {
  155.         SootClass c = Scene.v().loadClassAndSupport(name);
  156.         c.setApplicationClass();
  157.         return c;
  158.     }
  159.  
  160.     private static PAG doPointsToAnalysis(SootClass c) {
  161.         Scene.v().setEntryPoints(c.getMethods());
  162.  
  163.         HashMap<String, String> options = new HashMap<String, String>();
  164.         options.put("enabled", "true");
  165.         options.put("verbose", "false");
  166.         options.put("propagator", "worklist");
  167.         options.put("simple-edges-bidirectional", "false");
  168.         options.put("on-fly-cg", "true");
  169.         options.put("set-impl", "double");
  170.         options.put("double-set-old", "hybrid");
  171.         options.put("double-set-new", "hybrid");
  172.  
  173.         SparkTransformer.v().transform("", options);
  174.         PAG pag = (PAG) Scene.v().getPointsToAnalysis();
  175.  
  176.         return pag;
  177.     }  
  178. }
  179.  
  180. class MyP2SetVisitor extends P2SetVisitor{
  181.    
  182.     @Override
  183.     public void visit(Node arg0) {
  184.         //TODO: Check whether the argument given to sendJob is within bounds
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement