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.List;
14 import java.util.StringTokenizer;
15 import java.util.Vector;
17 import Analysis.CallGraph.CallGraph;
18 import Analysis.Loops.GlobalFieldType;
19 import Analysis.Loops.LoopOptimize;
20 import Analysis.Loops.LoopTerminate;
21 import IR.AnnotationDescriptor;
22 import IR.ClassDescriptor;
24 import IR.FieldDescriptor;
25 import IR.MethodDescriptor;
27 import IR.SymbolTable;
29 import IR.Flat.BuildFlat;
30 import IR.Flat.FlatMethod;
33 public class SSJavaAnalysis {
35 public static final String SSJAVA = "SSJAVA";
36 public static final String LATTICE = "LATTICE";
37 public static final String METHODDEFAULT = "METHODDEFAULT";
38 public static final String THISLOC = "THISLOC";
39 public static final String GLOBALLOC = "GLOBALLOC";
40 public static final String RETURNLOC = "RETURNLOC";
41 public static final String LOC = "LOC";
42 public static final String DELTA = "DELTA";
43 public static final String TERMINATE = "TERMINATE";
44 public static final String DELEGATE = "DELEGATE";
45 public static final String DELEGATETHIS = "DELEGATETHIS";
46 public static final String TRUST = "TRUST";
50 FlowDownCheck flowDownChecker;
51 MethodAnnotationCheck methodAnnotationChecker;
54 // set containing method requires to be annoated
55 Set<MethodDescriptor> annotationRequireSet;
57 // class -> field lattice
58 Hashtable<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
60 // class -> default local variable lattice
61 Hashtable<ClassDescriptor, MethodLattice<String>> cd2methodDefault;
63 // method -> local variable lattice
64 Hashtable<MethodDescriptor, MethodLattice<String>> md2lattice;
66 // method set that does not want to have loop termination analysis
67 Hashtable<MethodDescriptor, Integer> skipLoopTerminate;
69 // map shared location to its descriptors
70 Hashtable<Location, Set<Descriptor>> mapSharedLocation2DescriptorSet;
72 // set containing a class that has at least one annoated method
73 Set<ClassDescriptor> annotationRequireClassSet;
75 // the set of method descriptor required to check the linear type property
76 Set<MethodDescriptor> linearTypeCheckMethodSet;
78 // the set of method descriptors annotated as "TRUST"
79 Set<MethodDescriptor> trustWorthyMDSet;
81 // points to method containing SSJAVA Loop
82 private MethodDescriptor methodContainingSSJavaLoop;
84 // keep the field ownership from the linear type checking
85 Hashtable<MethodDescriptor, Set<FieldDescriptor>> mapMethodToOwnedFieldSet;
89 LinearTypeCheck checker;
91 public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
94 this.callgraph = callgraph;
95 this.cd2lattice = new Hashtable<ClassDescriptor, SSJavaLattice<String>>();
96 this.cd2methodDefault = new Hashtable<ClassDescriptor, MethodLattice<String>>();
97 this.md2lattice = new Hashtable<MethodDescriptor, MethodLattice<String>>();
98 this.annotationRequireSet = new HashSet<MethodDescriptor>();
99 this.annotationRequireClassSet = new HashSet<ClassDescriptor>();
100 this.skipLoopTerminate = new Hashtable<MethodDescriptor, Integer>();
101 this.mapSharedLocation2DescriptorSet = new Hashtable<Location, Set<Descriptor>>();
102 this.linearTypeCheckMethodSet = new HashSet<MethodDescriptor>();
104 this.trustWorthyMDSet = new HashSet<MethodDescriptor>();
105 this.mapMethodToOwnedFieldSet = new Hashtable<MethodDescriptor, Set<FieldDescriptor>>();
108 public void doCheck() {
109 doMethodAnnotationCheck();
110 computeLinearTypeCheckMethodSet();
112 // if (state.SSJAVADEBUG) {
115 parseLocationAnnotation();
117 doDefinitelyWrittenCheck();
121 private void debugDoLoopCheck() {
122 GlobalFieldType gft = new GlobalFieldType(callgraph, state, tu.getMain());
123 LoopOptimize lo = new LoopOptimize(gft, tu);
125 SymbolTable classtable = state.getClassSymbolTable();
127 List<ClassDescriptor> toanalyzeList = new ArrayList<ClassDescriptor>();
128 List<MethodDescriptor> toanalyzeMethodList = new ArrayList<MethodDescriptor>();
130 toanalyzeList.addAll(classtable.getValueSet());
131 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
132 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
133 return o1.getClassName().compareTo(o2.getClassName());
137 for (int i = 0; i < toanalyzeList.size(); i++) {
138 ClassDescriptor cd = toanalyzeList.get(i);
140 SymbolTable methodtable = cd.getMethodTable();
141 toanalyzeMethodList.clear();
142 toanalyzeMethodList.addAll(methodtable.getValueSet());
143 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
144 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
145 return o1.getSymbol().compareTo(o2.getSymbol());
149 for (int mdIdx = 0; mdIdx < toanalyzeMethodList.size(); mdIdx++) {
150 MethodDescriptor md = toanalyzeMethodList.get(mdIdx);
151 if (needTobeAnnotated(md)) {
152 lo.analyze(state.getMethodFlat(md));
153 doLoopTerminationCheck(lo, state.getMethodFlat(md));
161 public void addTrustMethod(MethodDescriptor md) {
162 trustWorthyMDSet.add(md);
165 public boolean isTrustMethod(MethodDescriptor md) {
166 return trustWorthyMDSet.contains(md);
169 private void computeLinearTypeCheckMethodSet() {
171 Set<MethodDescriptor> allCalledSet = callgraph.getMethodCalls(tu.getMain());
172 linearTypeCheckMethodSet.addAll(allCalledSet);
174 Set<MethodDescriptor> trustedSet = new HashSet<MethodDescriptor>();
176 for (Iterator iterator = trustWorthyMDSet.iterator(); iterator.hasNext();) {
177 MethodDescriptor trustMethod = (MethodDescriptor) iterator.next();
178 Set<MethodDescriptor> calledFromTrustMethodSet = callgraph.getMethodCalls(trustMethod);
179 trustedSet.add(trustMethod);
180 trustedSet.addAll(calledFromTrustMethodSet);
183 linearTypeCheckMethodSet.removeAll(trustedSet);
185 // if a method is called only by trusted method, no need to check linear
186 // type & flow down rule
187 for (Iterator iterator = trustedSet.iterator(); iterator.hasNext();) {
188 MethodDescriptor md = (MethodDescriptor) iterator.next();
189 Set<MethodDescriptor> callerSet = callgraph.getCallerSet(md);
190 if (!trustedSet.containsAll(callerSet) && !trustWorthyMDSet.contains(md)) {
191 linearTypeCheckMethodSet.add(md);
197 private void doLinearTypeCheck() {
198 LinearTypeCheck checker = new LinearTypeCheck(this, state);
199 checker.linearTypeCheck();
202 public void debugPrint() {
203 System.out.println("SSJAVA: SSJava is checking the following methods:");
204 for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
205 MethodDescriptor md = iterator.next();
206 System.out.print(" " + md);
208 System.out.println();
211 private void doMethodAnnotationCheck() {
212 methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
213 methodAnnotationChecker.methodAnnoatationCheck();
214 methodAnnotationChecker.methodAnnoataionInheritanceCheck();
217 public void doFlowDownCheck() {
218 flowDownChecker = new FlowDownCheck(this, state);
219 flowDownChecker.flowDownCheck();
222 public void doDefinitelyWrittenCheck() {
223 DefinitelyWrittenCheck checker = new DefinitelyWrittenCheck(this, state);
224 checker.definitelyWrittenCheck();
227 private void parseLocationAnnotation() {
228 Iterator it = state.getClassSymbolTable().getDescriptorsIterator();
229 while (it.hasNext()) {
230 ClassDescriptor cd = (ClassDescriptor) it.next();
231 // parsing location hierarchy declaration for the class
232 Vector<AnnotationDescriptor> classAnnotations = cd.getModifier().getAnnotations();
233 for (int i = 0; i < classAnnotations.size(); i++) {
234 AnnotationDescriptor an = classAnnotations.elementAt(i);
235 String marker = an.getMarker();
236 if (marker.equals(LATTICE)) {
237 SSJavaLattice<String> locOrder =
238 new SSJavaLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
239 cd2lattice.put(cd, locOrder);
240 parseClassLatticeDefinition(cd, an.getValue(), locOrder);
242 if (state.SSJAVADEBUG) {
243 // generate lattice dot file
244 writeLatticeDotFile(cd, locOrder);
247 } else if (marker.equals(METHODDEFAULT)) {
248 MethodLattice<String> locOrder =
249 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
250 cd2methodDefault.put(cd, locOrder);
251 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
255 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
256 MethodDescriptor md = (MethodDescriptor) method_it.next();
257 // parsing location hierarchy declaration for the method
259 if (needTobeAnnotated(md)) {
260 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
261 if (methodAnnotations != null) {
262 for (int i = 0; i < methodAnnotations.size(); i++) {
263 AnnotationDescriptor an = methodAnnotations.elementAt(i);
264 if (an.getMarker().equals(LATTICE)) {
265 // developer explicitly defines method lattice
266 MethodLattice<String> locOrder =
267 new MethodLattice<String>(SSJavaLattice.TOP, SSJavaLattice.BOTTOM);
268 md2lattice.put(md, locOrder);
269 parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
270 } else if (an.getMarker().equals(TERMINATE)) {
271 // developer explicitly wants to skip loop termination analysis
272 String value = an.getValue();
273 int maxIteration = 0;
275 maxIteration = Integer.parseInt(value);
277 skipLoopTerminate.put(md, new Integer(maxIteration));
288 private void writeLatticeDotFile(ClassDescriptor cd, SSJavaLattice<String> locOrder) {
290 String className = cd.getSymbol().replaceAll("[\\W_]", "");
292 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
295 BufferedWriter bw = new BufferedWriter(new FileWriter(className + ".dot"));
297 bw.write("digraph " + className + " {\n");
299 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
300 // pair is in the form of <higher, lower>
301 Pair<String, String> pair = (Pair<String, String>) iterator.next();
303 String highLocId = pair.getFirst();
304 if (locOrder.isSharedLoc(highLocId)) {
305 highLocId = "\"" + highLocId + "*\"";
307 String lowLocId = pair.getSecond();
308 if (locOrder.isSharedLoc(lowLocId)) {
309 lowLocId = "\"" + lowLocId + "*\"";
311 bw.write(highLocId + " -> " + lowLocId + ";\n");
316 } catch (IOException e) {
322 private void parseMethodDefaultLatticeDefinition(ClassDescriptor cd, String value,
323 MethodLattice<String> locOrder) {
325 value = value.replaceAll(" ", ""); // remove all blank spaces
327 StringTokenizer tokenizer = new StringTokenizer(value, ",");
329 while (tokenizer.hasMoreTokens()) {
330 String orderElement = tokenizer.nextToken();
331 int idx = orderElement.indexOf("<");
332 if (idx > 0) {// relative order element
333 String lowerLoc = orderElement.substring(0, idx);
334 String higherLoc = orderElement.substring(idx + 1);
335 locOrder.put(higherLoc, lowerLoc);
336 if (locOrder.isIntroducingCycle(higherLoc)) {
337 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
338 + " introduces a cycle.");
340 } else if (orderElement.startsWith(THISLOC + "=")) {
341 String thisLoc = orderElement.substring(8);
342 locOrder.setThisLoc(thisLoc);
343 } else if (orderElement.startsWith(GLOBALLOC + "=")) {
344 String globalLoc = orderElement.substring(10);
345 locOrder.setGlobalLoc(globalLoc);
346 } else if (orderElement.startsWith(RETURNLOC + "=")) {
347 String returnLoc = orderElement.substring(10);
348 locOrder.setReturnLoc(returnLoc);
349 } else if (orderElement.endsWith("*")) {
350 // spin loc definition
351 locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
354 locOrder.put(orderElement);
359 if (locOrder.getThisLoc() != null && !locOrder.containsKey(locOrder.getThisLoc())) {
360 throw new Error("Variable 'this' location '" + locOrder.getThisLoc()
361 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
364 if (locOrder.getGlobalLoc() != null && !locOrder.containsKey(locOrder.getGlobalLoc())) {
365 throw new Error("Variable global location '" + locOrder.getGlobalLoc()
366 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
370 private void parseClassLatticeDefinition(ClassDescriptor cd, String value,
371 SSJavaLattice<String> locOrder) {
373 value = value.replaceAll(" ", ""); // remove all blank spaces
375 StringTokenizer tokenizer = new StringTokenizer(value, ",");
377 while (tokenizer.hasMoreTokens()) {
378 String orderElement = tokenizer.nextToken();
379 int idx = orderElement.indexOf("<");
381 if (idx > 0) {// relative order element
382 String lowerLoc = orderElement.substring(0, idx);
383 String higherLoc = orderElement.substring(idx + 1);
384 locOrder.put(higherLoc, lowerLoc);
385 if (locOrder.isIntroducingCycle(higherLoc)) {
386 throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
387 + " introduces a cycle.");
389 } else if (orderElement.contains("*")) {
390 // spin loc definition
391 locOrder.addSharedLoc(orderElement.substring(0, orderElement.length() - 1));
394 locOrder.put(orderElement);
399 Set<String> spinLocSet = locOrder.getSharedLocSet();
400 for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
401 String spinLoc = (String) iterator.next();
402 if (!locOrder.containsKey(spinLoc)) {
403 throw new Error("Spin location '" + spinLoc
404 + "' is not defined in the default local variable lattice at " + cd.getSourceFileName());
409 public Hashtable<ClassDescriptor, SSJavaLattice<String>> getCd2lattice() {
413 public Hashtable<ClassDescriptor, MethodLattice<String>> getCd2methodDefault() {
414 return cd2methodDefault;
417 public Hashtable<MethodDescriptor, MethodLattice<String>> getMd2lattice() {
421 public SSJavaLattice<String> getClassLattice(ClassDescriptor cd) {
422 return cd2lattice.get(cd);
425 public MethodLattice<String> getMethodDefaultLattice(ClassDescriptor cd) {
426 return cd2methodDefault.get(cd);
429 public MethodLattice<String> getMethodLattice(MethodDescriptor md) {
430 if (md2lattice.containsKey(md)) {
431 return md2lattice.get(md);
433 return cd2methodDefault.get(md.getClassDesc());
437 public boolean needToCheckLinearType(MethodDescriptor md) {
438 return linearTypeCheckMethodSet.contains(md);
441 public boolean needTobeAnnotated(MethodDescriptor md) {
442 return annotationRequireSet.contains(md);
445 public boolean needToBeAnnoated(ClassDescriptor cd) {
446 return annotationRequireClassSet.contains(cd);
449 public void addAnnotationRequire(ClassDescriptor cd) {
450 annotationRequireClassSet.add(cd);
453 public void addAnnotationRequire(MethodDescriptor md) {
455 ClassDescriptor cd = md.getClassDesc();
456 // if a method requires to be annotated, class containg that method also
457 // requires to be annotated
458 if (!isSSJavaUtil(cd)) {
459 annotationRequireClassSet.add(cd);
460 annotationRequireSet.add(md);
464 public Set<MethodDescriptor> getAnnotationRequireSet() {
465 return annotationRequireSet;
468 public void doLoopTerminationCheck(LoopOptimize lo, FlatMethod fm) {
469 LoopTerminate lt = new LoopTerminate(this, state);
470 if (needTobeAnnotated(fm.getMethod())) {
471 lt.terminateAnalysis(fm, lo.getLoopInvariant(fm));
475 public CallGraph getCallGraph() {
479 public SSJavaLattice<String> getLattice(Descriptor d) {
481 if (d instanceof MethodDescriptor) {
482 return getMethodLattice((MethodDescriptor) d);
484 return getClassLattice((ClassDescriptor) d);
489 public boolean isSharedLocation(Location loc) {
490 SSJavaLattice<String> lattice = getLattice(loc.getDescriptor());
491 return lattice.getSharedLocSet().contains(loc.getLocIdentifier());
494 public void mapSharedLocation2Descriptor(Location loc, Descriptor d) {
495 Set<Descriptor> set = mapSharedLocation2DescriptorSet.get(loc);
497 set = new HashSet<Descriptor>();
498 mapSharedLocation2DescriptorSet.put(loc, set);
503 public BuildFlat getBuildFlat() {
507 public MethodDescriptor getMethodContainingSSJavaLoop() {
508 return methodContainingSSJavaLoop;
511 public void setMethodContainingSSJavaLoop(MethodDescriptor methodContainingSSJavaLoop) {
512 this.methodContainingSSJavaLoop = methodContainingSSJavaLoop;
515 public boolean isSSJavaUtil(ClassDescriptor cd) {
516 if (cd.getSymbol().equals("SSJAVA")) {
522 public void setFieldOnwership(MethodDescriptor md, FieldDescriptor field) {
524 Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
525 if (fieldSet == null) {
526 fieldSet = new HashSet<FieldDescriptor>();
527 mapMethodToOwnedFieldSet.put(md, fieldSet);
532 public boolean isOwnedByMethod(MethodDescriptor md, FieldDescriptor field) {
533 Set<FieldDescriptor> fieldSet = mapMethodToOwnedFieldSet.get(md);
534 if (fieldSet != null) {
535 return fieldSet.contains(field);