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;
45 public class FlowDownCheck {
48 static SSJavaAnalysis ssjava;
52 // mapping from 'descriptor' to 'composite location'
53 Hashtable<Descriptor, CompositeLocation> d2loc;
55 // mapping from 'locID' to 'class descriptor'
56 Hashtable<String, ClassDescriptor> fieldLocName2cd;
58 public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
61 this.toanalyze = new HashSet();
62 this.d2loc = new Hashtable<Descriptor, CompositeLocation>();
63 this.fieldLocName2cd = new Hashtable<String, ClassDescriptor>();
69 // construct mapping from the location name to the class descriptor
70 // assume that the location name is unique through the whole program
72 Set<ClassDescriptor> cdSet = ssjava.getCd2lattice().keySet();
73 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
74 ClassDescriptor cd = (ClassDescriptor) iterator.next();
75 SSJavaLattice<String> lattice = ssjava.getCd2lattice().get(cd);
76 Set<String> fieldLocNameSet = lattice.getKeySet();
78 for (Iterator iterator2 = fieldLocNameSet.iterator(); iterator2.hasNext();) {
79 String fieldLocName = (String) iterator2.next();
80 fieldLocName2cd.put(fieldLocName, cd);
87 public void flowDownCheck() {
88 SymbolTable classtable = state.getClassSymbolTable();
90 // phase 1 : checking declaration node and creating mapping of 'type
91 // desciptor' & 'location'
92 toanalyze.addAll(classtable.getValueSet());
93 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
94 while (!toanalyze.isEmpty()) {
95 Object obj = toanalyze.iterator().next();
96 ClassDescriptor cd = (ClassDescriptor) obj;
99 if (!cd.isInterface()) {
100 checkDeclarationInClass(cd);
101 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
102 MethodDescriptor md = (MethodDescriptor) method_it.next();
104 checkDeclarationInMethodBody(cd, md);
106 System.out.println("Error in " + md);
114 // phase2 : checking assignments
115 toanalyze.addAll(classtable.getValueSet());
116 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
117 while (!toanalyze.isEmpty()) {
118 Object obj = toanalyze.iterator().next();
119 ClassDescriptor cd = (ClassDescriptor) obj;
120 toanalyze.remove(cd);
123 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
124 MethodDescriptor md = (MethodDescriptor) method_it.next();
126 checkMethodBody(cd, md);
128 System.out.println("Error in " + md);
136 public Hashtable getMap() {
140 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
141 BlockNode bn = state.getMethodBody(md);
142 for (int i = 0; i < md.numParameters(); i++) {
143 // process annotations on method parameters
144 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
145 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
147 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
150 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
151 bn.getVarTable().setParent(nametable);
152 for (int i = 0; i < bn.size(); i++) {
153 BlockStatementNode bsn = bn.get(i);
154 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
158 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
159 BlockStatementNode bsn) {
161 switch (bsn.kind()) {
162 case Kind.SubBlockNode:
163 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
166 case Kind.DeclarationNode:
167 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
171 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
176 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
178 if (ln.getType() == LoopNode.FORLOOP) {
179 // check for loop case
180 ClassDescriptor cd = md.getClassDesc();
181 BlockNode bn = ln.getInitializer();
182 for (int i = 0; i < bn.size(); i++) {
183 BlockStatementNode bsn = bn.get(i);
184 checkDeclarationInBlockStatementNode(md, nametable, bsn);
189 checkDeclarationInBlockNode(md, nametable, ln.getBody());
192 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
193 BlockNode bn = state.getMethodBody(md);
194 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
197 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
200 bn.getVarTable().setParent(nametable);
201 // it will return the lowest location in the block node
202 CompositeLocation lowestLoc = null;
203 for (int i = 0; i < bn.size(); i++) {
204 BlockStatementNode bsn = bn.get(i);
205 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
206 if (lowestLoc == null) {
209 if (!bLoc.isEmpty()) {
210 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc)) {
219 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
220 SymbolTable nametable, BlockStatementNode bsn) {
222 CompositeLocation compLoc = null;
223 switch (bsn.kind()) {
224 case Kind.BlockExpressionNode:
225 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
228 case Kind.DeclarationNode:
229 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
232 case Kind.IfStatementNode:
233 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
237 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
240 case Kind.ReturnNode:
241 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
244 case Kind.SubBlockNode:
245 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
248 // case Kind.ContinueBreakNode:
249 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
256 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
258 ClassDescriptor cd = md.getClassDesc();
259 CompositeLocation loc = new CompositeLocation();
261 ExpressionNode returnExp = rn.getReturnExpression();
263 if (rn == null || hasOnlyLiteralValue(returnExp)) {
264 // when it returns literal value, return node has "bottom" location no
265 // matter what it is going to return.
266 loc.addLocation(Location.createBottomLocation(md));
268 // by default, return node has "bottom" location
269 // loc = checkLocationFromExpressionNode(md, nametable, returnExp, loc);
270 loc.addLocation(Location.createBottomLocation(md));
275 private boolean hasOnlyLiteralValue(ExpressionNode returnExp) {
276 if (returnExp.kind() == Kind.LiteralNode) {
283 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
286 ClassDescriptor cd = md.getClassDesc();
287 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
289 CompositeLocation condLoc =
290 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation());
291 addTypeLocation(ln.getCondition().getType(), (condLoc));
293 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
295 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc)) {
296 // loop condition should be higher than loop body
298 "The location of the while-condition statement is lower than the loop body at "
299 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
305 // check for loop case
306 BlockNode bn = ln.getInitializer();
307 bn.getVarTable().setParent(nametable);
309 // calculate glb location of condition and update statements
310 CompositeLocation condLoc =
311 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
312 new CompositeLocation());
313 addTypeLocation(ln.getCondition().getType(), condLoc);
315 CompositeLocation updateLoc =
316 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
318 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
319 glbInputSet.add(condLoc);
320 glbInputSet.add(updateLoc);
322 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(glbInputSet);
324 // check location of 'forloop' body
325 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
327 if (blockLoc == null) {
328 // when there is no statement in the loop body
329 return glbLocOfForLoopCond;
332 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc)) {
334 "The location of the for-condition statement is lower than the for-loop body at "
335 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
342 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
343 SymbolTable nametable, SubBlockNode sbn) {
344 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
348 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
349 SymbolTable nametable, IfStatementNode isn) {
351 ClassDescriptor localCD = md.getClassDesc();
352 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
354 CompositeLocation condLoc =
355 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation());
357 addTypeLocation(isn.getCondition().getType(), condLoc);
358 glbInputSet.add(condLoc);
360 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
361 glbInputSet.add(locTrueBlock);
363 // here, the location of conditional block should be higher than the
364 // location of true/false blocks
366 if (!CompositeLattice.isGreaterThan(condLoc, locTrueBlock)) {
369 "The location of the if-condition statement is lower than the conditional block at "
370 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
373 if (isn.getFalseBlock() != null) {
374 CompositeLocation locFalseBlock =
375 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
376 glbInputSet.add(locFalseBlock);
378 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock)) {
381 "The location of the if-condition statement is lower than the conditional block at "
382 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
387 // return GLB location of condition, true, and false block
388 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
393 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
394 SymbolTable nametable, DeclarationNode dn) {
396 VarDescriptor vd = dn.getVarDescriptor();
398 CompositeLocation destLoc = d2loc.get(vd);
400 ClassDescriptor localCD = md.getClassDesc();
401 if (dn.getExpression() != null) {
402 CompositeLocation expressionLoc =
403 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
404 new CompositeLocation());
405 addTypeLocation(dn.getExpression().getType(), expressionLoc);
407 if (expressionLoc != null) {
408 // checking location order
409 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc)) {
410 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
411 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
412 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
415 return expressionLoc;
419 // if (destLoc instanceof Location) {
420 // CompositeLocation comp = new CompositeLocation();
421 // comp.addLocation(destLoc);
424 // return (CompositeLocation) destLoc;
432 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
434 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
437 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
438 SymbolTable nametable, BlockExpressionNode ben) {
439 CompositeLocation compLoc =
440 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
441 // addTypeLocation(ben.getExpression().getType(), compLoc);
445 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
446 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
448 CompositeLocation compLoc = null;
451 case Kind.AssignmentNode:
452 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
455 case Kind.FieldAccessNode:
456 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
460 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
464 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
467 case Kind.CreateObjectNode:
468 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
471 case Kind.ArrayAccessNode:
472 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
475 case Kind.LiteralNode:
476 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
479 case Kind.MethodInvokeNode:
480 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc);
483 case Kind.TertiaryNode:
484 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
488 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
491 // case Kind.InstanceOfNode:
492 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
495 // case Kind.ArrayInitializerNode:
496 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
500 // case Kind.ClassTypeNode:
501 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
504 // case Kind.OffsetNode:
505 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
512 // addTypeLocation(en.getType(), compLoc);
517 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
520 ExpressionNode en = cn.getExpression();
521 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
525 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
526 SymbolTable nametable, TertiaryNode tn) {
527 ClassDescriptor cd = md.getClassDesc();
529 CompositeLocation condLoc =
530 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation());
531 addTypeLocation(tn.getCond().getType(), condLoc);
532 CompositeLocation trueLoc =
533 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation());
534 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
535 CompositeLocation falseLoc =
536 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation());
537 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
539 // check if condLoc is higher than trueLoc & falseLoc
540 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc)) {
542 "The location of the condition expression is lower than the true expression at "
543 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
546 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc)) {
548 "The location of the condition expression is lower than the true expression at "
549 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
552 // then, return glb of trueLoc & falseLoc
553 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
554 glbInputSet.add(trueLoc);
555 glbInputSet.add(falseLoc);
557 return CompositeLattice.calculateGLB(glbInputSet);
560 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
561 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc) {
563 if (min.numArgs() > 1) {
564 // caller needs to guarantee that it passes arguments in regarding to
565 // callee's hierarchy
566 for (int i = 0; i < min.numArgs(); i++) {
567 ExpressionNode en = min.getArg(i);
568 CompositeLocation callerArg1 =
569 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
571 ClassDescriptor calleecd = min.getMethod().getClassDesc();
572 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
573 CompositeLocation calleeLoc1 = d2loc.get(calleevd);
575 if (!callerArg1.get(0).isTop()) {
576 // here, check if ordering relations among caller's args respect
577 // ordering relations in-between callee's args
578 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
579 if (currentIdx != i) { // skip itself
580 ExpressionNode argExp = min.getArg(currentIdx);
582 CompositeLocation callerArg2 =
583 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation());
585 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
586 CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
588 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2);
589 boolean calleeResult = CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2);
591 if (calleeResult && !callerResult) {
592 // If calleeLoc1 is higher than calleeLoc2
593 // then, caller should have same ordering relation in-bet
594 // callerLoc1 & callerLoc2
596 throw new Error("Caller doesn't respect ordering relations among method arguments:"
597 + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
608 // all arguments should be higher than the location of return value
609 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
610 for (int i = 0; i < min.numArgs(); i++) {
611 ExpressionNode en = min.getArg(i);
612 CompositeLocation callerArg =
613 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
614 inputGLBSet.add(callerArg);
617 // if (inputGLBSet.size() > 0) {
618 // return CompositeLattice.calculateGLB(inputGLBSet);
620 // // if there are no arguments,
621 // // method invocation from the same class
622 // CompositeLocation compLoc = new CompositeLocation();
626 Location baseLoc = null;
627 if (min.getBaseName() != null) {
628 Descriptor d = nametable.get(min.getBaseName().getSymbol());
629 if (d instanceof VarDescriptor) {
630 CompositeLocation varLoc = (CompositeLocation) ((VarDescriptor) d).getType().getExtension();
633 // it is field descriptor
634 assert (d instanceof FieldDescriptor);
635 Location fieldLoc = (Location) min.getExpression().getType().getExtension();
636 CompositeLocation compLoc = new CompositeLocation();
637 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
638 Location thisLoc = new Location(md, methodLattice.getThisLoc());
639 compLoc.addLocation(thisLoc);
640 compLoc.addLocation(fieldLoc);
644 // method invocation starting from this
645 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
646 String thisLocId = methodLattice.getThisLoc();
647 baseLoc = new Location(md, thisLocId);
648 CompositeLocation compLoc = new CompositeLocation();
649 compLoc.addLocation(baseLoc);
655 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
656 SymbolTable nametable, ArrayAccessNode aan) {
658 // return glb location of array itself and index
660 ClassDescriptor cd = md.getClassDesc();
662 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
664 CompositeLocation arrayLoc =
665 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation());
666 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
667 glbInputSet.add(arrayLoc);
668 CompositeLocation indexLoc =
669 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation());
670 glbInputSet.add(indexLoc);
671 // addTypeLocation(aan.getIndex().getType(), indexLoc);
673 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
677 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
678 SymbolTable nametable, CreateObjectNode con) {
680 ClassDescriptor cd = md.getClassDesc();
683 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
684 for (int i = 0; i < con.numArgs(); i++) {
685 ExpressionNode en = con.getArg(i);
686 CompositeLocation argLoc =
687 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
688 glbInputSet.add(argLoc);
689 addTypeLocation(en.getType(), argLoc);
692 // check array initializers
693 // if ((con.getArrayInitializer() != null)) {
694 // checkLocationFromArrayInitializerNode(md, nametable,
695 // con.getArrayInitializer());
698 if (glbInputSet.size() > 0) {
699 return CompositeLattice.calculateGLB(glbInputSet);
702 CompositeLocation compLoc = new CompositeLocation();
703 compLoc.addLocation(Location.createTopLocation(md));
708 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
711 ClassDescriptor cd = md.getClassDesc();
712 CompositeLocation leftLoc = new CompositeLocation();
713 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
714 // addTypeLocation(on.getLeft().getType(), leftLoc);
716 CompositeLocation rightLoc = new CompositeLocation();
717 if (on.getRight() != null) {
718 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
719 // addTypeLocation(on.getRight().getType(), rightLoc);
722 // System.out.println("checking op node=" + on.printNode(0));
723 // System.out.println("left loc=" + leftLoc + " from " + on.getLeft().getClass());
724 // System.out.println("right loc=" + rightLoc + " from " + on.getRight().getClass());
726 Operation op = on.getOp();
728 switch (op.getOp()) {
730 case Operation.UNARYPLUS:
731 case Operation.UNARYMINUS:
732 case Operation.LOGIC_NOT:
736 case Operation.LOGIC_OR:
737 case Operation.LOGIC_AND:
739 case Operation.BIT_OR:
740 case Operation.BIT_XOR:
741 case Operation.BIT_AND:
742 case Operation.ISAVAILABLE:
743 case Operation.EQUAL:
744 case Operation.NOTEQUAL:
754 case Operation.LEFTSHIFT:
755 case Operation.RIGHTSHIFT:
756 case Operation.URIGHTSHIFT:
758 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
759 inputSet.add(leftLoc);
760 inputSet.add(rightLoc);
761 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
765 throw new Error(op.toString());
770 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
771 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
773 // literal value has the top location so that value can be flowed into any
775 Location literalLoc = Location.createTopLocation(md);
776 loc.addLocation(literalLoc);
781 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
782 NameNode nn, CompositeLocation loc) {
784 NameDescriptor nd = nn.getName();
785 if (nd.getBase() != null) {
786 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
787 // addTypeLocation(nn.getExpression().getType(), loc);
789 String varname = nd.toString();
791 if (varname.equals("this")) {
793 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
794 String thisLocId = methodLattice.getThisLoc();
795 if (thisLocId == null) {
796 throw new Error("The location for 'this' is not defined at "
797 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
799 Location locElement = new Location(md, thisLocId);
800 loc.addLocation(locElement);
803 Descriptor d = (Descriptor) nametable.get(varname);
805 // CompositeLocation localLoc = null;
806 if (d instanceof VarDescriptor) {
807 VarDescriptor vd = (VarDescriptor) d;
808 // localLoc = d2loc.get(vd);
809 // the type of var descriptor has a composite location!
810 loc = ((CompositeLocation) vd.getType().getExtension()).clone();
811 } else if (d instanceof FieldDescriptor) {
812 // the type of field descriptor has a location!
813 FieldDescriptor fd = (FieldDescriptor) d;
815 // the location of field access starts from this, followed by field
817 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
818 Location thisLoc = new Location(md, localLattice.getThisLoc());
819 loc.addLocation(thisLoc);
820 Location fieldLoc = (Location) fd.getType().getExtension();
821 loc.addLocation(fieldLoc);
827 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
828 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
830 ExpressionNode left = fan.getExpression();
831 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
832 // addTypeLocation(left.getType(), loc);
834 if (!left.getType().isPrimitive()) {
835 FieldDescriptor fd = fan.getField();
836 Location fieldLoc = (Location) fd.getType().getExtension();
837 loc.addLocation(fieldLoc);
843 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
844 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
846 ClassDescriptor cd = md.getClassDesc();
848 boolean postinc = true;
849 if (an.getOperation().getBaseOp() == null
850 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
851 .getBaseOp().getOp() != Operation.POSTDEC))
854 CompositeLocation destLocation =
855 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation());
857 CompositeLocation srcLocation = new CompositeLocation();
859 srcLocation = new CompositeLocation();
860 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
861 // System.out.println("an=" + an.printNode(0) + " an.getSrc()=" + an.getSrc().getClass()
862 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
863 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
864 throw new Error("The value flow from " + srcLocation + " to " + destLocation
865 + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
866 + cd.getSourceFileName() + "::" + an.getNumLine());
870 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
872 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
873 throw new Error("Location " + destLocation
874 + " is not allowed to have the value flow that moves within the same location at "
875 + cd.getSourceFileName() + "::" + an.getNumLine());
883 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
884 SymbolTable nametable, TreeNode n) {
886 ClassDescriptor cd = md.getClassDesc();
887 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
889 // currently enforce every variable to have corresponding location
890 if (annotationVec.size() == 0) {
891 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
892 + md.getSymbol() + " of the class " + cd.getSymbol());
895 if (annotationVec.size() > 1) { // variable can have at most one location
896 throw new Error(vd.getSymbol() + " has more than one location.");
899 AnnotationDescriptor ad = annotationVec.elementAt(0);
901 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
903 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
904 String locDec = ad.getValue(); // check if location is defined
906 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
907 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
908 d2loc.put(vd, deltaLoc);
909 addTypeLocation(vd.getType(), deltaLoc);
911 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
912 d2loc.put(vd, compLoc);
913 addTypeLocation(vd.getType(), compLoc);
921 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
924 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
927 int beginIdx = dIdx + 6;
928 locDec = locDec.substring(beginIdx, locDec.length() - 1);
929 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
932 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
933 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
938 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
940 CompositeLocation compLoc = new CompositeLocation();
942 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
943 List<String> locIdList = new ArrayList<String>();
944 while (tokenizer.hasMoreTokens()) {
945 String locId = tokenizer.nextToken();
946 locIdList.add(locId);
949 // at least,one location element needs to be here!
950 assert (locIdList.size() > 0);
952 // assume that loc with idx 0 comes from the local lattice
953 // loc with idx 1 comes from the field lattice
955 String localLocId = locIdList.get(0);
956 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
957 Location localLoc = new Location(md, localLocId);
958 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
959 throw new Error("Location " + localLocId
960 + " is not defined in the local variable lattice at "
961 + md.getClassDesc().getSourceFileName() + "::" + n.getNumLine() + ".");
963 compLoc.addLocation(localLoc);
965 for (int i = 1; i < locIdList.size(); i++) {
966 String locName = locIdList.get(i);
967 ClassDescriptor cd = fieldLocName2cd.get(locName);
969 SSJavaLattice<String> fieldLattice = CompositeLattice.getLatticeByDescriptor(cd);
971 if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
972 throw new Error("Location " + locName + " is not defined in the field lattice at "
973 + cd.getSourceFileName() + ".");
976 Location fieldLoc = new Location(cd, locName);
977 compLoc.addLocation(fieldLoc);
984 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
985 VarDescriptor vd = dn.getVarDescriptor();
986 assignLocationOfVarDescriptor(vd, md, nametable, dn);
989 private void checkClass(ClassDescriptor cd) {
990 // Check to see that methods respects ss property
991 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
992 MethodDescriptor md = (MethodDescriptor) method_it.next();
993 checkMethodDeclaration(cd, md);
997 private void checkDeclarationInClass(ClassDescriptor cd) {
998 // Check to see that fields are okay
999 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1000 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1001 checkFieldDeclaration(cd, fd);
1005 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1009 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1011 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1013 // currently enforce every field to have corresponding location
1014 if (annotationVec.size() == 0) {
1015 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1019 if (annotationVec.size() > 1) {
1020 // variable can have at most one location
1021 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1022 + " has more than one location.");
1025 AnnotationDescriptor ad = annotationVec.elementAt(0);
1027 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1029 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1030 String locationID = ad.getValue();
1031 // check if location is defined
1032 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1033 if (lattice == null || (!lattice.containsKey(locationID))) {
1034 throw new Error("Location " + locationID
1035 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1036 + cd.getSourceFileName() + ".");
1038 Location loc = new Location(cd, locationID);
1039 // d2loc.put(fd, loc);
1040 addTypeLocation(fd.getType(), loc);
1047 private void addTypeLocation(TypeDescriptor type, CompositeLocation loc) {
1049 type.setExtension(loc);
1053 private void addTypeLocation(TypeDescriptor type, Location loc) {
1055 type.setExtension(loc);
1059 static class CompositeLattice {
1061 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1063 // System.out.println("isGreaterThan= " + loc1 + " " + loc2);
1065 int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1066 if (baseCompareResult == ComparisonResult.EQUAL) {
1067 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1072 } else if (baseCompareResult == ComparisonResult.GREATER) {
1080 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1082 int deltaCount1 = 0;
1083 int deltaCount2 = 0;
1084 if (dLoc1 instanceof DeltaLocation) {
1085 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1088 if (dLoc2 instanceof DeltaLocation) {
1089 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1091 if (deltaCount1 < deltaCount2) {
1092 return ComparisonResult.GREATER;
1093 } else if (deltaCount1 == deltaCount2) {
1094 return ComparisonResult.EQUAL;
1096 return ComparisonResult.LESS;
1101 private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1103 // if compLoc1 is greater than compLoc2, return true
1104 // else return false;
1106 // compare one by one in according to the order of the tuple
1108 for (int i = 0; i < compLoc1.getSize(); i++) {
1109 Location loc1 = compLoc1.get(i);
1110 if (i >= compLoc2.getSize()) {
1111 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1112 + " because they are not comparable.");
1114 Location loc2 = compLoc2.get(i);
1116 if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1117 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1118 + " because they are not comparable.");
1121 Descriptor d1 = loc1.getDescriptor();
1122 Descriptor d2 = loc2.getDescriptor();
1124 SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1125 SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1127 // check if the spin location is appeared only at the end of the
1128 // composite location
1129 if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1130 if (i != (compLoc1.getSize() - 1)) {
1131 throw new Error("The spin location " + loc1.getLocIdentifier()
1132 + " cannot be appeared in the middle of composite location.");
1136 if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1137 if (i != (compLoc2.getSize() - 1)) {
1138 throw new Error("The spin location " + loc2.getLocIdentifier()
1139 + " cannot be appeared in the middle of composite location.");
1143 if (!lattice1.equals(lattice2)) {
1144 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1145 + " because they are not comparable.");
1148 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1150 // check if the current location is the spinning location
1151 // note that the spinning location only can be appeared in the last
1152 // part of the composite location
1153 if (numOfTie == compLoc1.getSize()
1154 && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1155 return ComparisonResult.GREATER;
1158 } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1159 return ComparisonResult.GREATER;
1161 return ComparisonResult.LESS;
1166 if (numOfTie == compLoc1.getSize()) {
1168 if (numOfTie != compLoc2.getSize()) {
1169 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1170 + " because they are not comparable.");
1173 return ComparisonResult.EQUAL;
1176 return ComparisonResult.LESS;
1180 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1182 // System.out.println("Calculating GLB=" + inputSet);
1183 CompositeLocation glbCompLoc = new CompositeLocation();
1185 // calculate GLB of the first(priority) element
1186 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1187 Descriptor priorityDescriptor = null;
1189 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1190 new Hashtable<String, Set<CompositeLocation>>();
1191 // mapping from the priority loc ID to its full representation by the
1192 // composite location
1194 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1195 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1196 Location priorityLoc = compLoc.get(0);
1197 String priorityLocId = priorityLoc.getLocIdentifier();
1198 priorityLocIdentifierSet.add(priorityLocId);
1200 if (locId2CompLocSet.contains(priorityLocId)) {
1201 locId2CompLocSet.get(priorityLocId).add(compLoc);
1203 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1204 newSet.add(compLoc);
1205 locId2CompLocSet.put(priorityLocId, newSet);
1208 // check if priority location are coming from the same lattice
1209 if (priorityDescriptor == null) {
1210 priorityDescriptor = priorityLoc.getDescriptor();
1211 } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1212 throw new Error("Failed to calculate GLB of " + inputSet
1213 + " because they are from different lattices.");
1217 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1218 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1220 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1221 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1223 if (compSet == null) {
1224 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1225 // mean that the result is already lower than <x1,y1> and <x2,y2>
1226 // assign TOP to the rest of the location elements
1227 CompositeLocation inputComp = inputSet.iterator().next();
1228 for (int i = 1; i < inputComp.getSize(); i++) {
1229 glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1232 if (compSet.size() == 1) {
1233 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1234 CompositeLocation comp = compSet.iterator().next();
1235 for (int i = 1; i < comp.getSize(); i++) {
1236 glbCompLoc.addLocation(comp.get(i));
1239 // when GLB(x1,x2)==x1 and x2 : GLB case 1
1240 // if more than one location shares the same priority GLB
1241 // need to calculate the rest of GLB loc
1243 int compositeLocSize = compSet.iterator().next().getSize();
1245 Set<String> glbInputSet = new HashSet<String>();
1246 Descriptor currentD = null;
1247 for (int i = 1; i < compositeLocSize; i++) {
1248 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1249 CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1250 Location currentLoc = compositeLocation.get(i);
1251 currentD = currentLoc.getDescriptor();
1252 // making set of the current location sharing the same idx
1253 glbInputSet.add(currentLoc.getLocIdentifier());
1255 // calculate glb for the current lattice
1257 SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1258 String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1259 glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1269 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1271 SSJavaLattice<String> lattice = null;
1273 if (d instanceof ClassDescriptor) {
1274 lattice = ssjava.getCd2lattice().get(d);
1275 } else if (d instanceof MethodDescriptor) {
1276 if (ssjava.getMd2lattice().containsKey(d)) {
1277 lattice = ssjava.getMd2lattice().get(d);
1279 // use default lattice for the method
1280 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1289 class ComparisonResult {
1291 public static final int GREATER = 0;
1292 public static final int EQUAL = 1;
1293 public static final int LESS = 2;