Advertisement
Guest User

PointsToAnalysis.JAVA

a guest
Apr 16th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.09 KB | None | 0 0
  1. import java.util.Collection;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5.  
  6. import soot.EntryPoints;
  7. import soot.Local;
  8. import soot.PointsToSet;
  9. import soot.Scene;
  10. import soot.SootClass;
  11. import soot.SootField;
  12. import soot.SootMethod;
  13. import soot.Value;
  14. import soot.ValueBox;
  15. import soot.jimple.JimpleBody;
  16. import soot.jimple.Stmt;
  17. import soot.jimple.paddle.PaddleTransformer;
  18. import soot.jimple.spark.SparkTransformer;
  19. import soot.options.PaddleOptions;
  20. import soot.tagkit.LineNumberTag;
  21.  
  22. public class PointsToAnalysis {
  23.    
  24.     // Make sure we get line numbers and whole program analysis
  25.     static {
  26.         soot.options.Options.v().set_keep_line_number(true);
  27.         soot.options.Options.v().set_whole_program(true);
  28.         soot.options.Options.v().setPhaseOption("cg","verbose:true");
  29.     }
  30.    
  31.     private static SootClass loadClass(String name, boolean main) {
  32.         SootClass c = Scene.v().loadClassAndSupport(name);
  33.         c.setApplicationClass();
  34.         if (main) Scene.v().setMainClass(c);
  35.         return c;
  36.     }
  37.    
  38.     public static void main(String[] args) {
  39.         loadClass("Item",false);
  40.         loadClass("Container",false);
  41.         SootClass c = loadClass("PointsToAnalysis",true);
  42.  
  43.         soot.Scene.v().loadNecessaryClasses();
  44.         soot.Scene.v().setEntryPoints(EntryPoints.v().all());
  45.        
  46.         if (args[0].equals("paddle"))
  47.             setPaddlePointsToAnalysis();
  48.         else if (args[0].equals("spark"))
  49.             setSparkPointsToAnalysis();
  50.  
  51.         SootField f = getField("Container","item");    
  52.         Map/*<Local>*/ ls = getLocals(c,args[2],"Container");
  53.        
  54.         printLocalIntersects(ls);  
  55.         printFieldIntersects(ls,f);    
  56.     }
  57.    
  58.     static void setSparkPointsToAnalysis() {
  59.         System.out.println("[spark] Starting analysis ...");
  60.                
  61.         HashMap opt = new HashMap();
  62.         opt.put("enabled","true");
  63.         opt.put("verbose","true");
  64.         opt.put("ignore-types","false");          
  65.         opt.put("force-gc","false");            
  66.         opt.put("pre-jimplify","false");          
  67.         opt.put("vta","false");                  
  68.         opt.put("rta","false");                  
  69.         opt.put("field-based","false");          
  70.         opt.put("types-for-sites","false");        
  71.         opt.put("merge-stringbuffer","true");  
  72.         opt.put("string-constants","false");    
  73.         opt.put("simulate-natives","true");      
  74.         opt.put("simple-edges-bidirectional","false");
  75.         opt.put("on-fly-cg","true");            
  76.         opt.put("simplify-offline","false");    
  77.         opt.put("simplify-sccs","false");        
  78.         opt.put("ignore-types-for-sccs","false");
  79.         opt.put("propagator","worklist");
  80.         opt.put("set-impl","double");
  81.         opt.put("double-set-old","hybrid");        
  82.         opt.put("double-set-new","hybrid");
  83.         opt.put("dump-html","false");          
  84.         opt.put("dump-pag","false");            
  85.         opt.put("dump-solution","false");        
  86.         opt.put("topo-sort","false");          
  87.         opt.put("dump-types","true");            
  88.         opt.put("class-method-var","true");    
  89.         opt.put("dump-answer","false");          
  90.         opt.put("add-tags","false");            
  91.         opt.put("set-mass","false");       
  92.        
  93.         SparkTransformer.v().transform("",opt);
  94.        
  95.         System.out.println("[spark] Done!");
  96.     }
  97.    
  98.     private static void setPaddlePointsToAnalysis() {
  99.         System.out.println("[paddle] Starting analysis ...");
  100.  
  101.         System.err.println("Soot version string: "+soot.Main.v().versionString);
  102.  
  103.         HashMap opt = new HashMap();
  104.         opt.put("enabled","true");
  105.         opt.put("verbose","true");
  106.         opt.put("bdd","true");
  107.         opt.put("backend","buddy");
  108.         opt.put("context","kcfa");
  109.         opt.put("k","2");
  110.         //      opt.put("context-heap","true");
  111.         opt.put("propagator","auto");
  112.         opt.put("conf","ofcg");
  113.         opt.put("order","32");
  114.         opt.put("q","auto");
  115.         opt.put("set-impl","double");
  116.         opt.put("double-set-old","hybrid");
  117.         opt.put("double-set-new","hybrid");
  118.         opt.put("pre-jimplify","false");
  119.  
  120.        
  121.         PaddleTransformer pt = new PaddleTransformer();
  122.         PaddleOptions paddle_opt = new PaddleOptions(opt);
  123.         pt.setup(paddle_opt);
  124.         pt.solve(paddle_opt);
  125.         soot.jimple.paddle.Results.v().makeStandardSootResults();  
  126.        
  127.         System.out.println("[paddle] Done!");
  128.     }
  129.    
  130.     private static int getLineNumber(Stmt s) {
  131.         Iterator ti = s.getTags().iterator();
  132.         while (ti.hasNext()) {
  133.             Object o = ti.next();
  134.             if (o instanceof LineNumberTag)
  135.                 return Integer.parseInt(o.toString());
  136.         }
  137.         return -1;
  138.     }
  139.    
  140.     private static SootField getField(String classname, String fieldname) {
  141.         Collection app = Scene.v().getApplicationClasses();
  142.         Iterator ci = app.iterator();
  143.         while (ci.hasNext()) {
  144.             SootClass sc = (SootClass)ci.next();
  145.             if (sc.getName().equals(classname))
  146.                 return sc.getFieldByName(fieldname);
  147.         }
  148.         throw new RuntimeException("Field "+fieldname+" was not found in class "+classname);
  149.     }
  150.    
  151.     private static Map/*<Integer,Local>*/ getLocals(SootClass sc, String methodname, String typename) {
  152.         Map res = new HashMap();
  153.         Iterator mi = sc.getMethods().iterator();
  154.         while (mi.hasNext()) {
  155.             SootMethod sm = (SootMethod)mi.next();
  156.             System.err.println(sm.getName());
  157.             if (true && sm.getName().equals(methodname) && sm.isConcrete()) {
  158.                 JimpleBody jb = (JimpleBody)sm.retrieveActiveBody();
  159.                 Iterator ui = jb.getUnits().iterator();
  160.                 while (ui.hasNext()) {
  161.                     Stmt s = (Stmt)ui.next();                      
  162.                     int line = getLineNumber(s);
  163.                     // find definitions
  164.                     Iterator bi = s.getDefBoxes().iterator();
  165.                     while (bi.hasNext()) {
  166.                         Object o = bi.next();
  167.                         if (o instanceof ValueBox) {
  168.                             Value v = ((ValueBox)o).getValue();
  169.                             if (v.getType().toString().equals(typename) && v instanceof Local)
  170.                                 res.put(new Integer(line),v);
  171.                         }
  172.                     }                  
  173.                 }
  174.             }
  175.         }
  176.        
  177.         return res;
  178.     }
  179.    
  180.     private static void printLocalIntersects(Map/*<Integer,Local>*/ ls) {
  181.         soot.PointsToAnalysis pta = Scene.v().getPointsToAnalysis();
  182.         Iterator i1 = ls.entrySet().iterator();
  183.         while (i1.hasNext()) {
  184.             Map.Entry e1 = (Map.Entry)i1.next();
  185.             int p1 = ((Integer)e1.getKey()).intValue();
  186.             Local l1 = (Local)e1.getValue();
  187.             PointsToSet r1 = pta.reachingObjects(l1);
  188.             Iterator i2 = ls.entrySet().iterator();
  189.             while (i2.hasNext()) {
  190.                 Map.Entry e2 = (Map.Entry)i2.next();
  191.                 int p2 = ((Integer)e2.getKey()).intValue();
  192.                 Local l2 = (Local)e2.getValue();
  193.                 PointsToSet r2 = pta.reachingObjects(l2);  
  194.                 if (p1<=p2)
  195.                     System.out.println("["+p1+","+p2+"]\t Container intersect? "+r1.hasNonEmptyIntersection(r2));
  196.             }
  197.         }
  198.     }
  199.    
  200.    
  201.     private static void printFieldIntersects(Map/*<Integer,Local>*/ ls, SootField f) {
  202.         soot.PointsToAnalysis pta = Scene.v().getPointsToAnalysis();
  203.         Iterator i1 = ls.entrySet().iterator();
  204.         while (i1.hasNext()) {
  205.             Map.Entry e1 = (Map.Entry)i1.next();
  206.             int p1 = ((Integer)e1.getKey()).intValue();
  207.             Local l1 = (Local)e1.getValue();
  208.             PointsToSet r1 = pta.reachingObjects(l1,f);
  209.             Iterator i2 = ls.entrySet().iterator();
  210.             while (i2.hasNext()) {
  211.                 Map.Entry e2 = (Map.Entry)i2.next();
  212.                 int p2 = ((Integer)e2.getKey()).intValue();
  213.                 Local l2 = (Local)e2.getValue();
  214.                 PointsToSet r2 = pta.reachingObjects(l2,f);
  215.                 if (p1<=p2)
  216.                     System.out.println("["+p1+","+p2+"]\t Container.item intersect? "+r1.hasNonEmptyIntersection(r2));
  217.             }
  218.         }
  219.     }
  220.    
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement