1) changes on the definitely written analysis: it only takes care about locations...
[IRC.git] / Robust / src / Analysis / SSJava / SSJavaAnalysis.java
index 21ae3920f1146231f9a47a998c2d44ee79f54cda..28635cab814d01d53f614bb8b247b91a15a33f41 100644 (file)
@@ -1,13 +1,21 @@
 package Analysis.SSJava;
 
+import java.io.BufferedWriter;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
 import Analysis.CallGraph.CallGraph;
+import Analysis.Loops.GlobalFieldType;
 import Analysis.Loops.LoopOptimize;
 import Analysis.Loops.LoopTerminate;
 import IR.AnnotationDescriptor;
@@ -15,10 +23,11 @@ import IR.ClassDescriptor;
 import IR.Descriptor;
 import IR.MethodDescriptor;
 import IR.State;
+import IR.SymbolTable;
 import IR.TypeUtil;
 import IR.Flat.BuildFlat;
 import IR.Flat.FlatMethod;
-import IR.Flat.TempDescriptor;
+import Util.Pair;
 
 public class SSJavaAnalysis {
 
@@ -31,11 +40,15 @@ public class SSJavaAnalysis {
   public static final String LOC = "LOC";
   public static final String DELTA = "DELTA";
   public static final String TERMINATE = "TERMINATE";
+  public static final String DELEGATE = "DELEGATE";
+  public static final String DELEGATETHIS = "DELEGATETHIS";
+  public static final String TRUST = "TRUST";
 
   State state;
   TypeUtil tu;
   FlowDownCheck flowDownChecker;
   MethodAnnotationCheck methodAnnotationChecker;
+  BuildFlat bf;
 
   // set containing method requires to be annoated
   Set<MethodDescriptor> annotationRequireSet;
@@ -58,9 +71,20 @@ public class SSJavaAnalysis {
   // set containing a class that has at least one annoated method
   Set<ClassDescriptor> annotationRequireClassSet;
 
+  // the set of method descriptor required to check the linear type property
+  Set<MethodDescriptor> linearTypeCheckMethodSet;
+
+  // the set of method descriptors annotated as "TRUST"
+  Set<MethodDescriptor> trustWorthyMDSet;
+
+  // points to method containing SSJAVA Loop
+  private MethodDescriptor methodContainingSSJavaLoop;
+
   CallGraph callgraph;
 
-  public SSJavaAnalysis(State state, TypeUtil tu, CallGraph callgraph) {
+  LinearTypeCheck checker;
+
+  public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
     this.state = state;
     this.tu = tu;
     this.callgraph = callgraph;
@@ -71,25 +95,112 @@ public class SSJavaAnalysis {
     this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
     this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
     this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
+    this.linearTypeCheckMethodSet = new HashSet<MethodDescriptor>();
+    this.bf = bf;
+    this.trustWorthyMDSet = new HashSet<MethodDescriptor>();
   }
 
   public void doCheck() {
     doMethodAnnotationCheck();
+    // computeLinearTypeCheckMethodSet();
+    // doLinearTypeCheck();
     if (state.SSJAVADEBUG) {
       debugPrint();
     }
     parseLocationAnnotation();
-    doFlowDownCheck();
+    // doFlowDownCheck();
     doDefinitelyWrittenCheck();
-    doSingleReferenceCheck();
+    // debugDoLoopCheck();
+  }
+
+  private void debugDoLoopCheck() {
+    GlobalFieldType gft = new GlobalFieldType(callgraph, state, tu.getMain());
+    LoopOptimize lo = new LoopOptimize(gft, tu);
+
+    SymbolTable classtable = state.getClassSymbolTable();
+
+    List<ClassDescriptor> toanalyzeList = new ArrayList<ClassDescriptor>();
+    List<MethodDescriptor> toanalyzeMethodList = new ArrayList<MethodDescriptor>();
+
+    toanalyzeList.addAll(classtable.getValueSet());
+    Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
+      public int compare(ClassDescriptor o1, ClassDescriptor o2) {
+        return o1.getClassName().compareTo(o2.getClassName());
+      }
+    });
+
+    for (int i = 0; i < toanalyzeList.size(); i++) {
+      ClassDescriptor cd = toanalyzeList.get(i);
+
+      SymbolTable methodtable = cd.getMethodTable();
+      toanalyzeMethodList.clear();
+      toanalyzeMethodList.addAll(methodtable.getValueSet());
+      Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
+        public int compare(MethodDescriptor o1, MethodDescriptor o2) {
+          return o1.getSymbol().compareTo(o2.getSymbol());
+        }
+      });
+
+      for (int mdIdx = 0; mdIdx < toanalyzeMethodList.size(); mdIdx++) {
+        MethodDescriptor md = toanalyzeMethodList.get(mdIdx);
+        if (needTobeAnnotated(md)) {
+          lo.analyze(state.getMethodFlat(md));
+          doLoopTerminationCheck(lo, state.getMethodFlat(md));
+        }
+      }
+
+    }
+
+  }
+
+  public void addTrustMethod(MethodDescriptor md) {
+    trustWorthyMDSet.add(md);
+  }
+
+  public boolean isTrustMethod(MethodDescriptor md) {
+    return trustWorthyMDSet.contains(md);
+  }
+
+  private void computeLinearTypeCheckMethodSet() {
+
+    Set<MethodDescriptor> allCalledSet = callgraph.getMethodCalls(tu.getMain());
+    linearTypeCheckMethodSet.addAll(allCalledSet);
+
+    Set<MethodDescriptor> trustedSet = new HashSet<MethodDescriptor>();
+
+    for (Iterator iterator = trustWorthyMDSet.iterator(); iterator.hasNext();) {
+      MethodDescriptor trustMethod = (MethodDescriptor) iterator.next();
+      Set<MethodDescriptor> calledFromTrustMethodSet = callgraph.getMethodCalls(trustMethod);
+      trustedSet.add(trustMethod);
+      trustedSet.addAll(calledFromTrustMethodSet);
+    }
+
+    linearTypeCheckMethodSet.removeAll(trustedSet);
+
+    // if a method is called only by trusted method, no need to check linear
+    // type & flow down rule
+    for (Iterator iterator = trustedSet.iterator(); iterator.hasNext();) {
+      MethodDescriptor md = (MethodDescriptor) iterator.next();
+      Set<MethodDescriptor> callerSet = callgraph.getCallerSet(md);
+      if (!trustedSet.containsAll(callerSet) && !trustWorthyMDSet.contains(md)) {
+        linearTypeCheckMethodSet.add(md);
+      }
+    }
+
+  }
+
+  private void doLinearTypeCheck() {
+    LinearTypeCheck checker = new LinearTypeCheck(this, state);
+    checker.linearTypeCheck();
   }
 
   public void debugPrint() {
     System.out.println("SSJAVA: SSJava is checking the following methods:");
     for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
       MethodDescriptor md = iterator.next();
-      System.out.println("SSJAVA: " + md);
+      System.out.print(" " + md);
     }
+    System.out.println();
   }
 
   private void doMethodAnnotationCheck() {
@@ -108,11 +219,6 @@ public class SSJavaAnalysis {
     checker.definitelyWrittenCheck();
   }
 
-  public void doSingleReferenceCheck() {
-    SingleReferenceCheck checker = new SingleReferenceCheck(this, state);
-    checker.singleReferenceCheck();
-  }
-
   private void parseLocationAnnotation() {
     Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
     while (it.hasNext()) {
@@ -127,11 +233,17 @@ public class SSJavaAnalysis {
               new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
           cd2lattice.put(cd, locOrder);
           parseClassLatticeDefinition(cd, an.getValue(), locOrder);
+
+          if (state.SSJAVADEBUG) {
+            // generate lattice dot file
+            writeLatticeDotFile(cd, locOrder);
+          }
+
         } else if (marker.equals(METHODDEFAULT)) {
           MethodLattice<String> locOrder =
               new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
           cd2methodDefault.put(cd, locOrder);
-          parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
+          parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
         }
       }
 
@@ -149,7 +261,7 @@ public class SSJavaAnalysis {
                 MethodLattice<String> locOrder =
                     new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
                 md2lattice.put(md, locOrder);
-                parseMethodLatticeDefinition(cd, an.getValue(), locOrder);
+                parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
               } else if (an.getMarker().equals(TERMINATE)) {
                 // developer explicitly wants to skip loop termination analysis
                 String value = an.getValue();
@@ -157,7 +269,6 @@ public class SSJavaAnalysis {
                 if (value != null) {
                   maxIteration = Integer.parseInt(value);
                 }
-                System.out.println("###md=" + md);
                 skipLoopTerminate.put(md, new Integer(maxIteration));
               }
             }
@@ -169,7 +280,41 @@ public class SSJavaAnalysis {
     }
   }
 
-  private void parseMethodLatticeDefinition(ClassDescriptor cd, String value,
+  private void writeLatticeDotFile(ClassDescriptor cd, SSJavaLattice<String> locOrder) {
+
+    String className = cd.getSymbol().replaceAll("[\\W_]", "");
+
+    Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
+
+    try {
+      BufferedWriter bw = new BufferedWriter(new FileWriter(className + ".dot"));
+
+      bw.write("digraph " + className + " {\n");
+
+      for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
+        // pair is in the form of <higher, lower>
+        Pair<String, String> pair = (Pair<String, String>) iterator.next();
+
+        String highLocId = pair.getFirst();
+        if (locOrder.isSharedLoc(highLocId)) {
+          highLocId = "\"" + highLocId + "*\"";
+        }
+        String lowLocId = pair.getSecond();
+        if (locOrder.isSharedLoc(lowLocId)) {
+          lowLocId = "\"" + lowLocId + "*\"";
+        }
+        bw.write(highLocId + " -> " + lowLocId + ";\n");
+      }
+      bw.write("}\n");
+      bw.close();
+
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+
+  }
+
+  private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
       MethodLattice<String> locOrder) {
 
     value = value.replaceAll(" ", ""); // remove all blank spaces
@@ -193,9 +338,12 @@ public class SSJavaAnalysis {
       } else if (orderElement.startsWith(GLOBALLOC + "=")) {
         String globalLoc = orderElement.substring(10);
         locOrder.setGlobalLoc(globalLoc);
-      } else if (orderElement.contains("*")) {
+      } else if (orderElement.startsWith(RETURNLOC + "=")) {
+        String returnLoc = orderElement.substring(10);
+        locOrder.setReturnLoc(returnLoc);
+      } else if (orderElement.endsWith("*")) {
         // spin loc definition
-        locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
+        locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
       } else {
         // single element
         locOrder.put(orderElement);
@@ -235,7 +383,7 @@ public class SSJavaAnalysis {
         }
       } else if (orderElement.contains("*")) {
         // spin loc definition
-        locOrder.addSpinLoc(orderElement.substring(0, orderElement.length() - 1));
+        locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
       } else {
         // single element
         locOrder.put(orderElement);
@@ -243,7 +391,7 @@ public class SSJavaAnalysis {
     }
 
     // sanity check
-    Set<String> spinLocSet = locOrder.getSpinLocSet();
+    Set<String> spinLocSet = locOrder.getSharedLocSet();
     for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
       String spinLoc = (String) iterator.next();
       if (!locOrder.containsKey(spinLoc)) {
@@ -269,6 +417,10 @@ public class SSJavaAnalysis {
     return cd2lattice.get(cd);
   }
 
+  public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
+    return cd2methodDefault.get(cd);
+  }
+
   public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
     if (md2lattice.containsKey(md)) {
       return md2lattice.get(md);
@@ -277,6 +429,10 @@ public class SSJavaAnalysis {
     }
   }
 
+  public boolean needToCheckLinearType(MethodDescriptor md) {
+    return linearTypeCheckMethodSet.contains(md);
+  }
+
   public boolean needTobeAnnotated(MethodDescriptor md) {
     return annotationRequireSet.contains(md);
   }
@@ -285,12 +441,19 @@ public class SSJavaAnalysis {
     return annotationRequireClassSet.contains(cd);
   }
 
+  public void addAnnotationRequire(ClassDescriptor cd) {
+    annotationRequireClassSet.add(cd);
+  }
+
   public void addAnnotationRequire(MethodDescriptor md) {
 
+    ClassDescriptor cd = md.getClassDesc();
     // if a method requires to be annotated, class containg that method also
     // requires to be annotated
-    annotationRequireClassSet.add(md.getClassDesc());
-    annotationRequireSet.add(md);
+    if (!isSSJavaUtil(cd)) {
+      annotationRequireClassSet.add(cd);
+      annotationRequireSet.add(md);
+    }
   }
 
   public Set<MethodDescriptor> getAnnotationRequireSet() {
@@ -298,24 +461,12 @@ public class SSJavaAnalysis {
   }
 
   public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
-    LoopTerminate lt = new LoopTerminate();
+    LoopTerminate lt = new LoopTerminate(this, state);
     if (needTobeAnnotated(fm.getMethod())) {
       lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
     }
   }
 
-  public void doLoopTerminationCheck(LoopOptimize lo) {
-    LoopTerminate lt = new LoopTerminate();
-    for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
-      MethodDescriptor md = (MethodDescriptor) iterator.next();
-      if (!skipLoopTerminate.containsKey(md)) {
-        FlatMethod fm = state.getMethodFlat(md);
-        lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
-      }
-    }
-
-  }
-
   public CallGraph getCallGraph() {
     return callgraph;
   }
@@ -332,7 +483,7 @@ public class SSJavaAnalysis {
 
   public boolean isSharedLocation(Location loc) {
     SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
-    return lattice.getSpinLocSet().contains(loc.getLocIdentifier());
+    return lattice.getSharedLocSet().contains(loc.getLocIdentifier());
   }
 
   public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
@@ -344,4 +495,22 @@ public class SSJavaAnalysis {
     set.add(d);
   }
 
+  public BuildFlat getBuildFlat() {
+    return bf;
+  }
+
+  public MethodDescriptor getMethodContainingSSJavaLoop() {
+    return methodContainingSSJavaLoop;
+  }
+
+  public void setMethodContainingSSJavaLoop(MethodDescriptor methodContainingSSJavaLoop) {
+    this.methodContainingSSJavaLoop = methodContainingSSJavaLoop;
+  }
+
+  public boolean isSSJavaUtil(ClassDescriptor cd) {
+    if (cd.getSymbol().equals("SSJAVA")) {
+      return true;
+    }
+    return false;
+  }
 }