1 package Analysis.SSJava;
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.HashSet;
10 import java.util.Hashtable;
11 import java.util.Iterator;
12 import java.util.LinkedList;
13 import java.util.List;
15 import java.util.StringTokenizer;
16 import java.util.Vector;
18 import Analysis.CallGraph.CallGraph;
19 import Analysis.Loops.GlobalFieldType;
20 import Analysis.Loops.LoopFinder;
21 import Analysis.Loops.LoopOptimize;
22 import Analysis.Loops.LoopTerminate;
23 import IR.AnnotationDescriptor;
24 import IR.ClassDescriptor;
26 import IR.FieldDescriptor;
27 import IR.MethodDescriptor;
29 import IR.SymbolTable;
31 import IR.Flat.BuildFlat;
32 import IR.Flat.FlatMethod;
33 import IR.Flat.FlatNode;
36 public class SSJavaAnalysis {
38 public static final String SSJAVA = "SSJAVA";
39 public static final String LATTICE = "LATTICE";
40 public static final String METHODDEFAULT = "METHODDEFAULT";
41 public static final String THISLOC = "THISLOC";
42 public static final String GLOBALLOC = "GLOBALLOC";
43 public static final String RETURNLOC = "RETURNLOC";
44 public static final String LOC = "LOC";
45 public static final String DELTA = "DELTA";
46 public static final String TERMINATE = "TERMINATE";
47 public static final String DELEGATE = "DELEGATE";
48 public static final String DELEGATETHIS = "DELEGATETHIS";
49 public static final String TRUST = "TRUST";
53 FlowDownCheck flowDownChecker;
54 MethodAnnotationCheck methodAnnotationChecker;
57 // set containing method requires to be annoated
58 Set<MethodDescriptor> annotationRequireSet;
60 // class -> field lattice
61 Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
63 // class -> default local variable lattice
64 Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
66 // method -> local variable lattice
67 Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
69 // method set that does not want to have loop termination analysis
70 Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
72 // map shared location to its descriptors
73 Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
75 // set containing a class that has at least one annoated method
76 Set<ClassDescriptor> annotationRequireClassSet;
78 // the set of method descriptor required to check the linear type property
79 Set<MethodDescriptor> linearTypeCheckMethodSet;
81 // the set of method descriptors annotated as "TRUST"
82 Set<MethodDescriptor> trustWorthyMDSet;
84 // points to method containing SSJAVA Loop
85 private MethodDescriptor methodContainingSSJavaLoop;
87 private FlatNode ssjavaLoopEntrance;
89 // keep the field ownership from the linear type checking
90 Hashtable<MethodDescriptor, Set<FieldDescriptor>> mapMethodToOwnedFieldSet;
92 Set<FlatNode> sameHeightWriteFlatNodeSet;
96 LinearTypeCheck checker;
98 // maps a descriptor to its known dependents: namely
99 // methods or tasks that call the descriptor's method
100 // AND are part of this analysis (reachable from main)
101 private Hashtable<Descriptor, Set<MethodDescriptor>> mapDescriptorToSetDependents;
103 private LinkedList<MethodDescriptor> sortedDescriptors;
105 public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
108 this.callgraph = callgraph;
109 this.cd2lattice = new Hashtable<ClassDescriptor, SSJavaLattice<String>>();
110 this.cd2methodDefault = new Hashtable<ClassDescriptor, MethodLattice<String>>();
111 this.md2lattice = new Hashtable<MethodDescriptor, MethodLattice<String>>();
112 this.annotationRequireSet = new HashSet<MethodDescriptor>();
113 this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
114 this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
115 this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
116 this.linearTypeCheckMethodSet = new HashSet<MethodDescriptor>();
118 this.trustWorthyMDSet = new HashSet<MethodDescriptor>();
119 this.mapMethodToOwnedFieldSet = new Hashtable<MethodDescriptor, Set<FieldDescriptor>>();
120 this.sameHeightWriteFlatNodeSet = new HashSet<FlatNode>();
121 this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
122 this.sortedDescriptors = new LinkedList<MethodDescriptor>();
125 public void doCheck() {
126 doMethodAnnotationCheck();
127 computeLinearTypeCheckMethodSet();
132 if (state.SSJAVADEBUG) {
135 if (state.SSJAVAINFER) {
138 parseLocationAnnotation();
140 doDefinitelyWrittenCheck();
145 private void init() {
146 // perform topological sort over the set of methods accessed by the main
148 Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
149 methodDescriptorsToAnalyze.addAll(getAnnotationRequireSet());
150 sortedDescriptors = topologicalSort(methodDescriptorsToAnalyze);
153 public LinkedList<MethodDescriptor> getSortedDescriptors() {
154 return (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
157 private void inference() {
158 LocationInference inferEngine = new LocationInference(this, state);
159 inferEngine.inference();
162 private void doLoopCheck() {
163 GlobalFieldType gft = new GlobalFieldType(callgraph, state, tu.getMain());
164 LoopOptimize lo = new LoopOptimize(gft, tu);
166 SymbolTable classtable = state.getClassSymbolTable();
168 List<ClassDescriptor> toanalyzeList = new ArrayList<ClassDescriptor>();
169 List<MethodDescriptor> toanalyzeMethodList = new ArrayList<MethodDescriptor>();
171 toanalyzeList.addAll(classtable.getValueSet());
172 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
173 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
174 return o1.getClassName().compareTo(o2.getClassName());
178 for (int i = 0; i < toanalyzeList.size(); i++) {
179 ClassDescriptor cd = toanalyzeList.get(i);
181 SymbolTable methodtable = cd.getMethodTable();
182 toanalyzeMethodList.clear();
183 toanalyzeMethodList.addAll(methodtable.getValueSet());
184 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
185 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
186 return o1.getSymbol().compareTo(o2.getSymbol());
190 for (int mdIdx = 0; mdIdx < toanalyzeMethodList.size(); mdIdx++) {
191 MethodDescriptor md = toanalyzeMethodList.get(mdIdx);
192 if (needTobeAnnotated(md)) {
193 lo.analyze(state.getMethodFlat(md));
194 doLoopTerminationCheck(lo, state.getMethodFlat(md));
202 public void addTrustMethod(MethodDescriptor md) {
203 trustWorthyMDSet.add(md);
206 public boolean isTrustMethod(MethodDescriptor md) {
207 return trustWorthyMDSet.contains(md);
210 private void computeLinearTypeCheckMethodSet() {
212 Set<MethodDescriptor> allCalledSet = callgraph.getMethodCalls(tu.getMain());
213 linearTypeCheckMethodSet.addAll(allCalledSet);
215 Set<MethodDescriptor> trustedSet = new HashSet<MethodDescriptor>();
217 for (Iterator iterator = trustWorthyMDSet.iterator(); iterator.hasNext();) {
218 MethodDescriptor trustMethod = (MethodDescriptor) iterator.next();
219 Set<MethodDescriptor> calledFromTrustMethodSet = callgraph.getMethodCalls(trustMethod);
220 trustedSet.add(trustMethod);
221 trustedSet.addAll(calledFromTrustMethodSet);
224 linearTypeCheckMethodSet.removeAll(trustedSet);
226 // if a method is called only by trusted method, no need to check linear
227 // type & flow down rule
228 for (Iterator iterator = trustedSet.iterator(); iterator.hasNext();) {
229 MethodDescriptor md = (MethodDescriptor) iterator.next();
230 Set<MethodDescriptor> callerSet = callgraph.getCallerSet(md);
231 if (!trustedSet.containsAll(callerSet) && !trustWorthyMDSet.contains(md)) {
232 linearTypeCheckMethodSet.add(md);
238 private void doLinearTypeCheck() {
239 LinearTypeCheck checker = new LinearTypeCheck(this, state);
240 checker.linearTypeCheck();
243 public void debugPrint() {
244 System.out.println("SSJAVA: SSJava is checking the following methods:");
245 for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
246 MethodDescriptor md = iterator.next();
247 System.out.print(" " + md);
249 System.out.println();
252 private void doMethodAnnotationCheck() {
253 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
254 methodAnnotationChecker.methodAnnoatationCheck();
255 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
256 state.setAnnotationRequireSet(annotationRequireSet);
259 public void doFlowDownCheck() {
260 flowDownChecker = new FlowDownCheck(this, state);
261 flowDownChecker.flowDownCheck();
264 public void doDefinitelyWrittenCheck() {
265 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
266 checker.definitelyWrittenCheck();
269 private void parseLocationAnnotation() {
270 Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
271 while (it.hasNext()) {
272 ClassDescriptor cd = (ClassDescriptor) it.next();
273 // parsing location hierarchy declaration for the class
274 Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
275 for (int i = 0; i < classAnnotations.size(); i++) {
276 AnnotationDescriptor an = classAnnotations.elementAt(i);
277 String marker = an.getMarker();
278 if (marker.equals(LATTICE)) {
279 SSJavaLattice<String> locOrder =
280 new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
281 cd2lattice.put(cd, locOrder);
282 parseClassLatticeDefinition(cd, an.getValue(), locOrder);
284 if (state.SSJAVADEBUG) {
285 // generate lattice dot file
286 writeLatticeDotFile(cd, locOrder);
289 } else if (marker.equals(METHODDEFAULT)) {
290 MethodLattice<String> locOrder =
291 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
292 cd2methodDefault.put(cd, locOrder);
293 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
297 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
298 MethodDescriptor md = (MethodDescriptor) method_it.next();
299 // parsing location hierarchy declaration for the method
301 if (needTobeAnnotated(md)) {
302 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
303 if (methodAnnotations != null) {
304 for (int i = 0; i < methodAnnotations.size(); i++) {
305 AnnotationDescriptor an = methodAnnotations.elementAt(i);
306 if (an.getMarker().equals(LATTICE)) {
307 // developer explicitly defines method lattice
308 MethodLattice<String> locOrder =
309 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
310 md2lattice.put(md, locOrder);
311 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
312 } else if (an.getMarker().equals(TERMINATE)) {
313 // developer explicitly wants to skip loop termination analysis
314 String value = an.getValue();
315 int maxIteration = 0;
317 maxIteration = Integer.parseInt(value);
319 skipLoopTerminate.put(md, new Integer(maxIteration));
330 public void writeLatticeDotFile(ClassDescriptor cd, SSJavaLattice<String> locOrder) {
332 String className = cd.getSymbol().replaceAll("[\\W_]", "");
334 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
337 BufferedWriter bw = new BufferedWriter(new FileWriter(className + ".dot"));
339 bw.write("digraph " + className + " {\n");
341 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
342 // pair is in the form of <higher, lower>
343 Pair<String, String> pair = (Pair<String, String>) iterator.next();
345 String highLocId = pair.getFirst();
346 if (locOrder.isSharedLoc(highLocId)) {
347 highLocId = "\"" + highLocId + "*\"";
349 String lowLocId = pair.getSecond();
350 if (locOrder.isSharedLoc(lowLocId)) {
351 lowLocId = "\"" + lowLocId + "*\"";
353 bw.write(highLocId + " -> " + lowLocId + ";\n");
358 } catch (IOException e) {
364 private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
365 MethodLattice<String> locOrder) {
367 value = value.replaceAll(" ", ""); // remove all blank spaces
369 StringTokenizer tokenizer = new StringTokenizer(value, ",");
371 while (tokenizer.hasMoreTokens()) {
372 String orderElement = tokenizer.nextToken();
373 int idx = orderElement.indexOf("<");
374 if (idx > 0) {// relative order element
375 String lowerLoc = orderElement.substring(0, idx);
376 String higherLoc = orderElement.substring(idx + 1);
377 locOrder.put(higherLoc, lowerLoc);
378 if (locOrder.isIntroducingCycle(higherLoc)) {
379 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
380 + " introduces a cycle.");
382 } else if (orderElement.startsWith(THISLOC + "=")) {
383 String thisLoc = orderElement.substring(8);
384 locOrder.setThisLoc(thisLoc);
385 } else if (orderElement.startsWith(GLOBALLOC + "=")) {
386 String globalLoc = orderElement.substring(10);
387 locOrder.setGlobalLoc(globalLoc);
388 } else if (orderElement.startsWith(RETURNLOC + "=")) {
389 String returnLoc = orderElement.substring(10);
390 locOrder.setReturnLoc(returnLoc);
391 } else if (orderElement.endsWith("*")) {
392 // spin loc definition
393 locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
396 locOrder.put(orderElement);
401 if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
402 throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
403 + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
406 if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
407 throw new Error("Variable global location '" + locOrder.getGlobalLoc()
408 + "' is not defined in the local variable lattice at " + cd.getSourceFileName());
412 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
413 SSJavaLattice<String> locOrder) {
415 value = value.replaceAll(" ", ""); // remove all blank spaces
417 StringTokenizer tokenizer = new StringTokenizer(value, ",");
419 while (tokenizer.hasMoreTokens()) {
420 String orderElement = tokenizer.nextToken();
421 int idx = orderElement.indexOf("<");
423 if (idx > 0) {// relative order element
424 String lowerLoc = orderElement.substring(0, idx);
425 String higherLoc = orderElement.substring(idx + 1);
426 locOrder.put(higherLoc, lowerLoc);
427 if (locOrder.isIntroducingCycle(higherLoc)) {
428 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
429 + " introduces a cycle.");
431 } else if (orderElement.contains("*")) {
432 // spin loc definition
433 locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
436 locOrder.put(orderElement);
441 Set<String> spinLocSet = locOrder.getSharedLocSet();
442 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
443 String spinLoc = (String) iterator.next();
444 if (!locOrder.containsKey(spinLoc)) {
445 throw new Error("Spin location '" + spinLoc
446 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
451 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
455 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
456 return cd2methodDefault;
459 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
463 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
464 return cd2lattice.get(cd);
467 public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
468 return cd2methodDefault.get(cd);
471 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
472 if (md2lattice.containsKey(md)) {
473 return md2lattice.get(md);
476 if (cd2methodDefault.containsKey(md.getClassDesc())) {
477 return cd2methodDefault.get(md.getClassDesc());
479 throw new Error("Method Lattice of " + md + " is not defined.");
485 public boolean needToCheckLinearType(MethodDescriptor md) {
486 return linearTypeCheckMethodSet.contains(md);
489 public boolean needTobeAnnotated(MethodDescriptor md) {
490 return annotationRequireSet.contains(md);
493 public boolean needToBeAnnoated(ClassDescriptor cd) {
494 return annotationRequireClassSet.contains(cd);
497 public void addAnnotationRequire(ClassDescriptor cd) {
498 annotationRequireClassSet.add(cd);
501 public void addAnnotationRequire(MethodDescriptor md) {
503 ClassDescriptor cd = md.getClassDesc();
504 // if a method requires to be annotated, class containg that method also
505 // requires to be annotated
506 if (!isSSJavaUtil(cd)) {
507 annotationRequireClassSet.add(cd);
508 annotationRequireSet.add(md);
512 public Set<MethodDescriptor> getAnnotationRequireSet() {
513 return annotationRequireSet;
516 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
517 LoopTerminate lt = new LoopTerminate(this, state);
518 if (needTobeAnnotated(fm.getMethod())) {
519 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
523 public CallGraph getCallGraph() {
527 public SSJavaLattice<String> getLattice(Descriptor d) {
529 if (d instanceof MethodDescriptor) {
530 return getMethodLattice((MethodDescriptor) d);
532 return getClassLattice((ClassDescriptor) d);
537 public boolean isSharedLocation(Location loc) {
538 SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
539 return lattice.getSharedLocSet().contains(loc.getLocIdentifier());
542 public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
543 Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
545 set = new HashSet<Descriptor>();
546 mapSharedLocation2DescriptorSet.put(loc, set);
551 public BuildFlat getBuildFlat() {
555 public MethodDescriptor getMethodContainingSSJavaLoop() {
556 return methodContainingSSJavaLoop;
559 public void setMethodContainingSSJavaLoop(MethodDescriptor methodContainingSSJavaLoop) {
560 this.methodContainingSSJavaLoop = methodContainingSSJavaLoop;
563 public boolean isSSJavaUtil(ClassDescriptor cd) {
564 if (cd.getSymbol().equals("SSJAVA")) {
570 public void setFieldOnwership(MethodDescriptor md, FieldDescriptor field) {
572 Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
573 if (fieldSet == null) {
574 fieldSet = new HashSet<FieldDescriptor>();
575 mapMethodToOwnedFieldSet.put(md, fieldSet);
580 public boolean isOwnedByMethod(MethodDescriptor md, FieldDescriptor field) {
581 Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
582 if (fieldSet != null) {
583 return fieldSet.contains(field);
588 public FlatNode getSSJavaLoopEntrance() {
589 return ssjavaLoopEntrance;
592 public void setSSJavaLoopEntrance(FlatNode ssjavaLoopEntrance) {
593 this.ssjavaLoopEntrance = ssjavaLoopEntrance;
596 public void addSameHeightWriteFlatNode(FlatNode fn) {
597 this.sameHeightWriteFlatNodeSet.add(fn);
600 public boolean isSameHeightWrite(FlatNode fn) {
601 return this.sameHeightWriteFlatNodeSet.contains(fn);
604 public LinkedList<MethodDescriptor> topologicalSort(Set<MethodDescriptor> toSort) {
606 Set<MethodDescriptor> discovered = new HashSet<MethodDescriptor>();
608 LinkedList<MethodDescriptor> sorted = new LinkedList<MethodDescriptor>();
610 Iterator<MethodDescriptor> itr = toSort.iterator();
611 while (itr.hasNext()) {
612 MethodDescriptor d = itr.next();
614 if (!discovered.contains(d)) {
615 dfsVisit(d, toSort, sorted, discovered);
622 // While we're doing DFS on call graph, remember
623 // dependencies for efficient queuing of methods
624 // during interprocedural analysis:
626 // a dependent of a method decriptor d for this analysis is:
627 // 1) a method or task that invokes d
628 // 2) in the descriptorsToAnalyze set
629 private void dfsVisit(MethodDescriptor md, Set<MethodDescriptor> toSort,
630 LinkedList<MethodDescriptor> sorted, Set<MethodDescriptor> discovered) {
634 Iterator itr2 = callgraph.getCalleeSet(md).iterator();
635 while (itr2.hasNext()) {
636 MethodDescriptor dCallee = (MethodDescriptor) itr2.next();
637 addDependent(dCallee, md);
640 Iterator itr = callgraph.getCallerSet(md).iterator();
641 while (itr.hasNext()) {
642 MethodDescriptor dCaller = (MethodDescriptor) itr.next();
643 // only consider callers in the original set to analyze
644 if (!toSort.contains(dCaller)) {
647 if (!discovered.contains(dCaller)) {
648 addDependent(md, // callee
652 dfsVisit(dCaller, toSort, sorted, discovered);
656 // for leaf-nodes last now!
660 public void addDependent(MethodDescriptor callee, MethodDescriptor caller) {
661 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
663 deps = new HashSet<MethodDescriptor>();
666 mapDescriptorToSetDependents.put(callee, deps);
669 public Set<MethodDescriptor> getDependents(MethodDescriptor callee) {
670 Set<MethodDescriptor> deps = mapDescriptorToSetDependents.get(callee);
672 deps = new HashSet<MethodDescriptor>();
673 mapDescriptorToSetDependents.put(callee, deps);