1 package Analysis.SSJava;
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Hashtable;
6 import java.util.Iterator;
9 import java.util.StringTokenizer;
10 import java.util.Vector;
12 import IR.AnnotationDescriptor;
13 import IR.ClassDescriptor;
15 import IR.FieldDescriptor;
16 import IR.MethodDescriptor;
17 import IR.NameDescriptor;
20 import IR.SymbolTable;
21 import IR.TypeDescriptor;
22 import IR.VarDescriptor;
23 import IR.Tree.ArrayAccessNode;
24 import IR.Tree.AssignmentNode;
25 import IR.Tree.BlockExpressionNode;
26 import IR.Tree.BlockNode;
27 import IR.Tree.BlockStatementNode;
28 import IR.Tree.CastNode;
29 import IR.Tree.CreateObjectNode;
30 import IR.Tree.DeclarationNode;
31 import IR.Tree.ExpressionNode;
32 import IR.Tree.FieldAccessNode;
33 import IR.Tree.IfStatementNode;
35 import IR.Tree.LiteralNode;
36 import IR.Tree.LoopNode;
37 import IR.Tree.MethodInvokeNode;
38 import IR.Tree.NameNode;
39 import IR.Tree.OpNode;
40 import IR.Tree.ReturnNode;
41 import IR.Tree.SubBlockNode;
42 import IR.Tree.TertiaryNode;
43 import IR.Tree.TreeNode;
46 public class FlowDownCheck {
49 static SSJavaAnalysis ssjava;
53 // mapping from 'descriptor' to 'composite location'
54 Hashtable<Descriptor, CompositeLocation> d2loc;
56 // mapping from 'locID' to 'class descriptor'
57 Hashtable<String, ClassDescriptor> fieldLocName2cd;
59 public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
62 this.toanalyze = new HashSet();
63 this.d2loc = new Hashtable<Descriptor, CompositeLocation>();
64 this.fieldLocName2cd = new Hashtable<String, ClassDescriptor>();
70 // construct mapping from the location name to the class descriptor
71 // assume that the location name is unique through the whole program
73 Set<ClassDescriptor> cdSet = ssjava.getCd2lattice().keySet();
74 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
75 ClassDescriptor cd = (ClassDescriptor) iterator.next();
76 SSJavaLattice<String> lattice = ssjava.getCd2lattice().get(cd);
77 Set<String> fieldLocNameSet = lattice.getKeySet();
79 for (Iterator iterator2 = fieldLocNameSet.iterator(); iterator2.hasNext();) {
80 String fieldLocName = (String) iterator2.next();
81 fieldLocName2cd.put(fieldLocName, cd);
88 public void flowDownCheck() {
89 SymbolTable classtable = state.getClassSymbolTable();
91 // phase 1 : checking declaration node and creating mapping of 'type
92 // desciptor' & 'location'
93 toanalyze.addAll(classtable.getValueSet());
94 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
95 while (!toanalyze.isEmpty()) {
96 Object obj = toanalyze.iterator().next();
97 ClassDescriptor cd = (ClassDescriptor) obj;
100 if (!cd.isInterface()) {
102 ClassDescriptor superDesc = cd.getSuperDesc();
103 if (superDesc != null && (!superDesc.isInterface())
104 && (!superDesc.getSymbol().equals("Object"))) {
105 checkOrderingInheritance(superDesc, cd);
108 checkDeclarationInClass(cd);
109 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
110 MethodDescriptor md = (MethodDescriptor) method_it.next();
112 checkDeclarationInMethodBody(cd, md);
114 System.out.println("Error in " + md);
122 // phase2 : checking assignments
123 toanalyze.addAll(classtable.getValueSet());
124 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
125 while (!toanalyze.isEmpty()) {
126 Object obj = toanalyze.iterator().next();
127 ClassDescriptor cd = (ClassDescriptor) obj;
128 toanalyze.remove(cd);
131 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
132 MethodDescriptor md = (MethodDescriptor) method_it.next();
134 checkMethodBody(cd, md);
136 System.out.println("Error in " + md);
144 private void checkOrderingInheritance(ClassDescriptor superCd, ClassDescriptor cd) {
145 // here, we're going to check that sub class keeps same relative orderings
146 // in respect to super class
148 SSJavaLattice<String> superLattice = ssjava.getClassLattice(superCd);
149 SSJavaLattice<String> subLattice = ssjava.getClassLattice(cd);
151 Set<Pair<String, String>> superPairSet = superLattice.getOrderingPairSet();
152 Set<Pair<String, String>> subPairSet = subLattice.getOrderingPairSet();
154 for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
155 Pair<String, String> pair = (Pair<String, String>) iterator.next();
157 if (!subPairSet.contains(pair)) {
158 throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
159 + pair.getSecond() + " < " + pair.getFirst() + "' that is defined by its superclass '"
166 public Hashtable getMap() {
170 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
171 BlockNode bn = state.getMethodBody(md);
172 for (int i = 0; i < md.numParameters(); i++) {
173 // process annotations on method parameters
174 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
175 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
177 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
180 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
181 bn.getVarTable().setParent(nametable);
182 for (int i = 0; i < bn.size(); i++) {
183 BlockStatementNode bsn = bn.get(i);
184 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
188 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
189 BlockStatementNode bsn) {
191 switch (bsn.kind()) {
192 case Kind.SubBlockNode:
193 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
196 case Kind.DeclarationNode:
197 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
201 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
206 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
208 if (ln.getType() == LoopNode.FORLOOP) {
209 // check for loop case
210 ClassDescriptor cd = md.getClassDesc();
211 BlockNode bn = ln.getInitializer();
212 for (int i = 0; i < bn.size(); i++) {
213 BlockStatementNode bsn = bn.get(i);
214 checkDeclarationInBlockStatementNode(md, nametable, bsn);
219 checkDeclarationInBlockNode(md, nametable, ln.getBody());
222 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
223 BlockNode bn = state.getMethodBody(md);
224 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
227 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
230 bn.getVarTable().setParent(nametable);
231 // it will return the lowest location in the block node
232 CompositeLocation lowestLoc = null;
233 for (int i = 0; i < bn.size(); i++) {
234 BlockStatementNode bsn = bn.get(i);
235 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
236 if (lowestLoc == null) {
239 if (!bLoc.isEmpty()) {
240 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc)) {
249 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
250 SymbolTable nametable, BlockStatementNode bsn) {
252 CompositeLocation compLoc = null;
253 switch (bsn.kind()) {
254 case Kind.BlockExpressionNode:
255 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
258 case Kind.DeclarationNode:
259 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
262 case Kind.IfStatementNode:
263 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
267 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
270 case Kind.ReturnNode:
271 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
274 case Kind.SubBlockNode:
275 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
278 // case Kind.ContinueBreakNode:
279 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
286 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
289 ExpressionNode returnExp = rn.getReturnExpression();
291 CompositeLocation expLoc =
292 checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation());
294 // callee should have a relative ordering in-between parameters and return
295 // value, which is required by caller's constraint
296 for (int i = 0; i < md.numParameters(); i++) {
297 Descriptor calleevd = md.getParameter(i);
298 CompositeLocation calleeParamLoc = d2loc.get(calleevd);
300 // here, parameter(input value) should be higher than result(output)
301 if ((!expLoc.get(0).isTop()) && !CompositeLattice.isGreaterThan(calleeParamLoc, expLoc)) {
302 throw new Error("Callee " + md + " doesn't have the ordering relation between parameter '"
303 + calleevd + "' and its return value '" + returnExp.printNode(0) + "'.");
307 // by default, return node has "bottom" location
308 CompositeLocation loc = new CompositeLocation();
309 loc.addLocation(Location.createBottomLocation(md));
313 private boolean hasOnlyLiteralValue(ExpressionNode en) {
314 if (en.kind() == Kind.LiteralNode) {
321 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
324 ClassDescriptor cd = md.getClassDesc();
325 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
327 CompositeLocation condLoc =
328 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation());
329 addTypeLocation(ln.getCondition().getType(), (condLoc));
331 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
333 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc)) {
334 // loop condition should be higher than loop body
336 "The location of the while-condition statement is lower than the loop body at "
337 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
343 // check for loop case
344 BlockNode bn = ln.getInitializer();
345 bn.getVarTable().setParent(nametable);
347 // calculate glb location of condition and update statements
348 CompositeLocation condLoc =
349 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
350 new CompositeLocation());
351 addTypeLocation(ln.getCondition().getType(), condLoc);
353 CompositeLocation updateLoc =
354 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
356 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
357 glbInputSet.add(condLoc);
358 glbInputSet.add(updateLoc);
360 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(glbInputSet);
362 // check location of 'forloop' body
363 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
365 if (blockLoc == null) {
366 // when there is no statement in the loop body
367 return glbLocOfForLoopCond;
370 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc)) {
372 "The location of the for-condition statement is lower than the for-loop body at "
373 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
380 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
381 SymbolTable nametable, SubBlockNode sbn) {
382 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
386 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
387 SymbolTable nametable, IfStatementNode isn) {
389 ClassDescriptor localCD = md.getClassDesc();
390 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
392 CompositeLocation condLoc =
393 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation());
395 addTypeLocation(isn.getCondition().getType(), condLoc);
396 glbInputSet.add(condLoc);
398 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
399 if (locTrueBlock != null) {
400 glbInputSet.add(locTrueBlock);
401 // here, the location of conditional block should be higher than the
402 // location of true/false blocks
403 if (locTrueBlock != null && !CompositeLattice.isGreaterThan(condLoc, locTrueBlock)) {
406 "The location of the if-condition statement is lower than the conditional block at "
407 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
411 if (isn.getFalseBlock() != null) {
412 CompositeLocation locFalseBlock =
413 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
415 if (locFalseBlock != null) {
416 glbInputSet.add(locFalseBlock);
418 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock)) {
421 "The location of the if-condition statement is lower than the conditional block at "
422 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
428 // return GLB location of condition, true, and false block
429 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
434 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
435 SymbolTable nametable, DeclarationNode dn) {
437 VarDescriptor vd = dn.getVarDescriptor();
439 CompositeLocation destLoc = d2loc.get(vd);
441 if (dn.getExpression() != null) {
442 CompositeLocation expressionLoc =
443 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
444 new CompositeLocation());
445 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
447 if (expressionLoc != null) {
448 // checking location order
449 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc)) {
450 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
451 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
452 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
455 return expressionLoc;
459 // if (destLoc instanceof Location) {
460 // CompositeLocation comp = new CompositeLocation();
461 // comp.addLocation(destLoc);
464 // return (CompositeLocation) destLoc;
472 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
474 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
477 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
478 SymbolTable nametable, BlockExpressionNode ben) {
479 CompositeLocation compLoc =
480 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
481 // addTypeLocation(ben.getExpression().getType(), compLoc);
485 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
486 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
488 CompositeLocation compLoc = null;
491 case Kind.AssignmentNode:
492 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
495 case Kind.FieldAccessNode:
496 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
500 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
504 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
507 case Kind.CreateObjectNode:
508 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
511 case Kind.ArrayAccessNode:
512 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
515 case Kind.LiteralNode:
516 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
519 case Kind.MethodInvokeNode:
520 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc);
523 case Kind.TertiaryNode:
524 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
528 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
531 // case Kind.InstanceOfNode:
532 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
535 // case Kind.ArrayInitializerNode:
536 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
540 // case Kind.ClassTypeNode:
541 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
544 // case Kind.OffsetNode:
545 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
552 // addTypeLocation(en.getType(), compLoc);
557 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
560 ExpressionNode en = cn.getExpression();
561 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
565 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
566 SymbolTable nametable, TertiaryNode tn) {
567 ClassDescriptor cd = md.getClassDesc();
569 CompositeLocation condLoc =
570 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation());
571 addTypeLocation(tn.getCond().getType(), condLoc);
572 CompositeLocation trueLoc =
573 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation());
574 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
575 CompositeLocation falseLoc =
576 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation());
577 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
579 // check if condLoc is higher than trueLoc & falseLoc
580 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc)) {
582 "The location of the condition expression is lower than the true expression at "
583 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
586 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc)) {
588 "The location of the condition expression is lower than the true expression at "
589 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
592 // then, return glb of trueLoc & falseLoc
593 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
594 glbInputSet.add(trueLoc);
595 glbInputSet.add(falseLoc);
597 return CompositeLattice.calculateGLB(glbInputSet);
600 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
601 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc) {
603 checkCalleeConstraints(md, nametable, min);
605 // all we need to care about is that
606 // method output(return value) should be lower than input values(method
608 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
609 for (int i = 0; i < min.numArgs(); i++) {
610 ExpressionNode en = min.getArg(i);
611 CompositeLocation callerArg =
612 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
613 inputGLBSet.add(callerArg);
616 if (inputGLBSet.size() > 0) {
617 return CompositeLattice.calculateGLB(inputGLBSet);
619 // if there are no arguments, just return TOP location
620 CompositeLocation compLoc = new CompositeLocation();
621 compLoc.addLocation(Location.createTopLocation(md));
627 private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
628 MethodInvokeNode min) {
630 if (min.numArgs() > 1) {
631 // caller needs to guarantee that it passes arguments in regarding to
632 // callee's hierarchy
633 for (int i = 0; i < min.numArgs(); i++) {
634 ExpressionNode en = min.getArg(i);
635 CompositeLocation callerArg1 =
636 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
638 ClassDescriptor calleecd = min.getMethod().getClassDesc();
639 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
640 CompositeLocation calleeLoc1 = d2loc.get(calleevd);
642 if (!callerArg1.get(0).isTop()) {
643 // here, check if ordering relations among caller's args respect
644 // ordering relations in-between callee's args
645 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
646 if (currentIdx != i) { // skip itself
647 ExpressionNode argExp = min.getArg(currentIdx);
649 CompositeLocation callerArg2 =
650 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation());
652 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
653 CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
655 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2);
656 boolean calleeResult = CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2);
658 if (calleeResult && !callerResult) {
659 // If calleeLoc1 is higher than calleeLoc2
660 // then, caller should have same ordering relation in-bet
661 // callerLoc1 & callerLoc2
663 throw new Error("Caller doesn't respect ordering relations among method arguments:"
664 + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
677 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
678 SymbolTable nametable, ArrayAccessNode aan) {
680 // return glb location of array itself and index
682 ClassDescriptor cd = md.getClassDesc();
684 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
686 CompositeLocation arrayLoc =
687 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation());
688 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
689 glbInputSet.add(arrayLoc);
690 CompositeLocation indexLoc =
691 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation());
692 glbInputSet.add(indexLoc);
693 // addTypeLocation(aan.getIndex().getType(), indexLoc);
695 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
699 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
700 SymbolTable nametable, CreateObjectNode con) {
702 ClassDescriptor cd = md.getClassDesc();
705 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
706 for (int i = 0; i < con.numArgs(); i++) {
707 ExpressionNode en = con.getArg(i);
708 CompositeLocation argLoc =
709 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
710 glbInputSet.add(argLoc);
711 addTypeLocation(en.getType(), argLoc);
714 // check array initializers
715 // if ((con.getArrayInitializer() != null)) {
716 // checkLocationFromArrayInitializerNode(md, nametable,
717 // con.getArrayInitializer());
720 if (glbInputSet.size() > 0) {
721 return CompositeLattice.calculateGLB(glbInputSet);
724 CompositeLocation compLoc = new CompositeLocation();
725 compLoc.addLocation(Location.createTopLocation(md));
730 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
733 ClassDescriptor cd = md.getClassDesc();
734 CompositeLocation leftLoc = new CompositeLocation();
735 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
736 // addTypeLocation(on.getLeft().getType(), leftLoc);
738 CompositeLocation rightLoc = new CompositeLocation();
739 if (on.getRight() != null) {
740 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
741 // addTypeLocation(on.getRight().getType(), rightLoc);
744 // System.out.println("checking op node=" + on.printNode(0));
745 // System.out.println("left loc=" + leftLoc + " from " +
746 // on.getLeft().getClass());
747 // System.out.println("right loc=" + rightLoc + " from " +
748 // on.getRight().getClass());
750 Operation op = on.getOp();
752 switch (op.getOp()) {
754 case Operation.UNARYPLUS:
755 case Operation.UNARYMINUS:
756 case Operation.LOGIC_NOT:
760 case Operation.LOGIC_OR:
761 case Operation.LOGIC_AND:
763 case Operation.BIT_OR:
764 case Operation.BIT_XOR:
765 case Operation.BIT_AND:
766 case Operation.ISAVAILABLE:
767 case Operation.EQUAL:
768 case Operation.NOTEQUAL:
778 case Operation.LEFTSHIFT:
779 case Operation.RIGHTSHIFT:
780 case Operation.URIGHTSHIFT:
782 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
783 inputSet.add(leftLoc);
784 inputSet.add(rightLoc);
785 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
789 throw new Error(op.toString());
794 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
795 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
797 // literal value has the top location so that value can be flowed into any
799 Location literalLoc = Location.createTopLocation(md);
800 loc.addLocation(literalLoc);
805 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
806 NameNode nn, CompositeLocation loc) {
808 NameDescriptor nd = nn.getName();
809 if (nd.getBase() != null) {
811 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
812 // addTypeLocation(nn.getExpression().getType(), loc);
814 String varname = nd.toString();
816 if (varname.equals("this")) {
818 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
819 String thisLocId = methodLattice.getThisLoc();
820 if (thisLocId == null) {
821 throw new Error("The location for 'this' is not defined at "
822 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
824 Location locElement = new Location(md, thisLocId);
825 loc.addLocation(locElement);
828 Descriptor d = (Descriptor) nametable.get(varname);
830 // CompositeLocation localLoc = null;
831 if (d instanceof VarDescriptor) {
832 VarDescriptor vd = (VarDescriptor) d;
833 // localLoc = d2loc.get(vd);
834 // the type of var descriptor has a composite location!
835 loc = ((CompositeLocation) vd.getType().getExtension()).clone();
836 } else if (d instanceof FieldDescriptor) {
837 // the type of field descriptor has a location!
838 FieldDescriptor fd = (FieldDescriptor) d;
842 // if it is 'static final', the location has TOP since no one can
844 loc.addLocation(Location.createTopLocation(md));
846 // if 'static', the location has pre-assigned global loc
847 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
848 String globalLocId = localLattice.getGlobalLoc();
849 if (globalLocId == null) {
850 throw new Error("Global location element is not defined in the method " + md);
852 Location globalLoc = new Location(md, globalLocId);
854 loc.addLocation(globalLoc);
857 // the location of field access starts from this, followed by field
859 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
860 Location thisLoc = new Location(md, localLattice.getThisLoc());
861 loc.addLocation(thisLoc);
864 Location fieldLoc = (Location) fd.getType().getExtension();
865 loc.addLocation(fieldLoc);
871 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
872 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
874 ExpressionNode left = fan.getExpression();
875 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
876 // addTypeLocation(left.getType(), loc);
878 if (!left.getType().isPrimitive()) {
879 FieldDescriptor fd = fan.getField();
880 Location fieldLoc = (Location) fd.getType().getExtension();
881 loc.addLocation(fieldLoc);
887 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
888 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
890 ClassDescriptor cd = md.getClassDesc();
892 boolean postinc = true;
893 if (an.getOperation().getBaseOp() == null
894 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
895 .getBaseOp().getOp() != Operation.POSTDEC))
898 CompositeLocation destLocation =
899 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation());
901 CompositeLocation srcLocation = new CompositeLocation();
904 if (hasOnlyLiteralValue(an.getSrc())) {
905 // if source is literal value, src location is TOP. so do not need to
909 srcLocation = new CompositeLocation();
910 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
911 // System.out.println(" an= " + an.printNode(0) + " an.getSrc()=" +
912 // an.getSrc().getClass()
913 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
914 // System.out.println("srcLocation=" + srcLocation);
915 // System.out.println("dstLocation=" + destLocation);
916 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
917 throw new Error("The value flow from " + srcLocation + " to " + destLocation
918 + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
919 + cd.getSourceFileName() + "::" + an.getNumLine());
923 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
925 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
926 throw new Error("Location " + destLocation
927 + " is not allowed to have the value flow that moves within the same location at "
928 + cd.getSourceFileName() + "::" + an.getNumLine());
936 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
937 SymbolTable nametable, TreeNode n) {
939 ClassDescriptor cd = md.getClassDesc();
940 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
942 // currently enforce every variable to have corresponding location
943 if (annotationVec.size() == 0) {
944 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
945 + md.getSymbol() + " of the class " + cd.getSymbol());
948 if (annotationVec.size() > 1) { // variable can have at most one location
949 throw new Error(vd.getSymbol() + " has more than one location.");
952 AnnotationDescriptor ad = annotationVec.elementAt(0);
954 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
956 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
957 String locDec = ad.getValue(); // check if location is defined
959 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
960 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
961 d2loc.put(vd, deltaLoc);
962 addTypeLocation(vd.getType(), deltaLoc);
964 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
965 d2loc.put(vd, compLoc);
966 addTypeLocation(vd.getType(), compLoc);
974 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
977 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
980 int beginIdx = dIdx + 6;
981 locDec = locDec.substring(beginIdx, locDec.length() - 1);
982 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
985 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
986 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
991 private Location parseFieldLocDeclaraton(String decl) {
993 int idx = decl.indexOf(".");
994 String className = decl.substring(0, idx);
995 String fieldName = decl.substring(idx + 1);
997 Descriptor d = state.getClassSymbolTable().get(className);
999 assert (d instanceof ClassDescriptor);
1000 SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1001 if (!lattice.containsKey(fieldName)) {
1002 throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1003 + className + "'.");
1006 return new Location(d, fieldName);
1009 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1011 CompositeLocation compLoc = new CompositeLocation();
1013 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1014 List<String> locIdList = new ArrayList<String>();
1015 while (tokenizer.hasMoreTokens()) {
1016 String locId = tokenizer.nextToken();
1017 locIdList.add(locId);
1020 // at least,one location element needs to be here!
1021 assert (locIdList.size() > 0);
1023 // assume that loc with idx 0 comes from the local lattice
1024 // loc with idx 1 comes from the field lattice
1026 String localLocId = locIdList.get(0);
1027 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1028 Location localLoc = new Location(md, localLocId);
1029 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1030 throw new Error("Location " + localLocId
1031 + " is not defined in the local variable lattice at "
1032 + md.getClassDesc().getSourceFileName() + "::" + n.getNumLine() + ".");
1034 compLoc.addLocation(localLoc);
1036 for (int i = 1; i < locIdList.size(); i++) {
1037 String locName = locIdList.get(i);
1039 Location fieldLoc = parseFieldLocDeclaraton(locName);
1040 // ClassDescriptor cd = fieldLocName2cd.get(locName);
1041 // SSJavaLattice<String> fieldLattice =
1042 // CompositeLattice.getLatticeByDescriptor(cd);
1044 // if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
1045 // throw new Error("Location " + locName +
1046 // " is not defined in the field lattice at "
1047 // + cd.getSourceFileName() + ".");
1049 // Location fieldLoc = new Location(cd, locName);
1050 compLoc.addLocation(fieldLoc);
1057 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1058 VarDescriptor vd = dn.getVarDescriptor();
1059 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1062 private void checkClass(ClassDescriptor cd) {
1063 // Check to see that methods respects ss property
1064 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1065 MethodDescriptor md = (MethodDescriptor) method_it.next();
1066 checkMethodDeclaration(cd, md);
1070 private void checkDeclarationInClass(ClassDescriptor cd) {
1071 // Check to see that fields are okay
1072 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1073 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1074 checkFieldDeclaration(cd, fd);
1078 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1082 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1084 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1086 // currently enforce every field to have corresponding location
1087 if (annotationVec.size() == 0) {
1088 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1092 if (annotationVec.size() > 1) {
1093 // variable can have at most one location
1094 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1095 + " has more than one location.");
1098 AnnotationDescriptor ad = annotationVec.elementAt(0);
1100 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1102 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1103 String locationID = ad.getValue();
1104 // check if location is defined
1105 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1106 if (lattice == null || (!lattice.containsKey(locationID))) {
1107 throw new Error("Location " + locationID
1108 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1109 + cd.getSourceFileName() + ".");
1111 Location loc = new Location(cd, locationID);
1112 // d2loc.put(fd, loc);
1113 addTypeLocation(fd.getType(), loc);
1120 private void addTypeLocation(TypeDescriptor type, CompositeLocation loc) {
1122 type.setExtension(loc);
1126 private void addTypeLocation(TypeDescriptor type, Location loc) {
1128 type.setExtension(loc);
1132 static class CompositeLattice {
1134 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1136 // System.out.println("isGreaterThan= " + loc1 + " " + loc2);
1138 int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1139 if (baseCompareResult == ComparisonResult.EQUAL) {
1140 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1145 } else if (baseCompareResult == ComparisonResult.GREATER) {
1153 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1155 int deltaCount1 = 0;
1156 int deltaCount2 = 0;
1157 if (dLoc1 instanceof DeltaLocation) {
1158 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1161 if (dLoc2 instanceof DeltaLocation) {
1162 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1164 if (deltaCount1 < deltaCount2) {
1165 return ComparisonResult.GREATER;
1166 } else if (deltaCount1 == deltaCount2) {
1167 return ComparisonResult.EQUAL;
1169 return ComparisonResult.LESS;
1174 private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1176 // if compLoc1 is greater than compLoc2, return true
1177 // else return false;
1179 // compare one by one in according to the order of the tuple
1181 for (int i = 0; i < compLoc1.getSize(); i++) {
1182 Location loc1 = compLoc1.get(i);
1183 if (i >= compLoc2.getSize()) {
1184 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1185 + " because they are not comparable.");
1187 Location loc2 = compLoc2.get(i);
1189 if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1190 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1191 + " because they are not comparable.");
1194 Descriptor d1 = loc1.getDescriptor();
1195 Descriptor d2 = loc2.getDescriptor();
1197 SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1198 SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1200 // check if the spin location is appeared only at the end of the
1201 // composite location
1202 if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1203 if (i != (compLoc1.getSize() - 1)) {
1204 throw new Error("The spin location " + loc1.getLocIdentifier()
1205 + " cannot be appeared in the middle of composite location.");
1209 if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1210 if (i != (compLoc2.getSize() - 1)) {
1211 throw new Error("The spin location " + loc2.getLocIdentifier()
1212 + " cannot be appeared in the middle of composite location.");
1216 if (!lattice1.equals(lattice2)) {
1217 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1218 + " because they are not comparable.");
1221 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1223 // check if the current location is the spinning location
1224 // note that the spinning location only can be appeared in the last
1225 // part of the composite location
1226 if (numOfTie == compLoc1.getSize()
1227 && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1228 return ComparisonResult.GREATER;
1231 } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1232 return ComparisonResult.GREATER;
1234 return ComparisonResult.LESS;
1239 if (numOfTie == compLoc1.getSize()) {
1241 if (numOfTie != compLoc2.getSize()) {
1242 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1243 + " because they are not comparable.");
1246 return ComparisonResult.EQUAL;
1249 return ComparisonResult.LESS;
1253 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1255 // System.out.println("Calculating GLB=" + inputSet);
1256 CompositeLocation glbCompLoc = new CompositeLocation();
1258 // calculate GLB of the first(priority) element
1259 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1260 Descriptor priorityDescriptor = null;
1262 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1263 new Hashtable<String, Set<CompositeLocation>>();
1264 // mapping from the priority loc ID to its full representation by the
1265 // composite location
1267 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1268 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1269 Location priorityLoc = compLoc.get(0);
1270 String priorityLocId = priorityLoc.getLocIdentifier();
1271 priorityLocIdentifierSet.add(priorityLocId);
1273 if (locId2CompLocSet.contains(priorityLocId)) {
1274 locId2CompLocSet.get(priorityLocId).add(compLoc);
1276 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1277 newSet.add(compLoc);
1278 locId2CompLocSet.put(priorityLocId, newSet);
1281 // check if priority location are coming from the same lattice
1282 if (priorityDescriptor == null) {
1283 priorityDescriptor = priorityLoc.getDescriptor();
1284 } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1285 throw new Error("Failed to calculate GLB of " + inputSet
1286 + " because they are from different lattices.");
1290 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1291 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1293 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1294 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1296 if (compSet == null) {
1297 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1298 // mean that the result is already lower than <x1,y1> and <x2,y2>
1299 // assign TOP to the rest of the location elements
1300 CompositeLocation inputComp = inputSet.iterator().next();
1301 for (int i = 1; i < inputComp.getSize(); i++) {
1302 glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1305 if (compSet.size() == 1) {
1306 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1307 CompositeLocation comp = compSet.iterator().next();
1308 for (int i = 1; i < comp.getSize(); i++) {
1309 glbCompLoc.addLocation(comp.get(i));
1312 // when GLB(x1,x2)==x1 and x2 : GLB case 1
1313 // if more than one location shares the same priority GLB
1314 // need to calculate the rest of GLB loc
1316 int compositeLocSize = compSet.iterator().next().getSize();
1318 Set<String> glbInputSet = new HashSet<String>();
1319 Descriptor currentD = null;
1320 for (int i = 1; i < compositeLocSize; i++) {
1321 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1322 CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1323 Location currentLoc = compositeLocation.get(i);
1324 currentD = currentLoc.getDescriptor();
1325 // making set of the current location sharing the same idx
1326 glbInputSet.add(currentLoc.getLocIdentifier());
1328 // calculate glb for the current lattice
1330 SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1331 String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1332 glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1342 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1344 SSJavaLattice<String> lattice = null;
1346 if (d instanceof ClassDescriptor) {
1347 lattice = ssjava.getCd2lattice().get(d);
1348 } else if (d instanceof MethodDescriptor) {
1349 if (ssjava.getMd2lattice().containsKey(d)) {
1350 lattice = ssjava.getMd2lattice().get(d);
1352 // use default lattice for the method
1353 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1362 class ComparisonResult {
1364 public static final int GREATER = 0;
1365 public static final int EQUAL = 1;
1366 public static final int LESS = 2;