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 en) {
276 if (en.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 if (dn.getExpression() != null) {
401 CompositeLocation expressionLoc =
402 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
403 new CompositeLocation());
404 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
406 if (expressionLoc != null) {
407 // checking location order
408 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc)) {
409 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
410 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
411 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
414 return expressionLoc;
418 // if (destLoc instanceof Location) {
419 // CompositeLocation comp = new CompositeLocation();
420 // comp.addLocation(destLoc);
423 // return (CompositeLocation) destLoc;
431 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
433 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
436 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
437 SymbolTable nametable, BlockExpressionNode ben) {
438 CompositeLocation compLoc =
439 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
440 // addTypeLocation(ben.getExpression().getType(), compLoc);
444 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
445 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
447 CompositeLocation compLoc = null;
450 case Kind.AssignmentNode:
451 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
454 case Kind.FieldAccessNode:
455 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
459 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
463 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
466 case Kind.CreateObjectNode:
467 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
470 case Kind.ArrayAccessNode:
471 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
474 case Kind.LiteralNode:
475 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
478 case Kind.MethodInvokeNode:
479 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc);
482 case Kind.TertiaryNode:
483 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
487 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
490 // case Kind.InstanceOfNode:
491 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
494 // case Kind.ArrayInitializerNode:
495 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
499 // case Kind.ClassTypeNode:
500 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
503 // case Kind.OffsetNode:
504 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
511 // addTypeLocation(en.getType(), compLoc);
516 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
519 ExpressionNode en = cn.getExpression();
520 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
524 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
525 SymbolTable nametable, TertiaryNode tn) {
526 ClassDescriptor cd = md.getClassDesc();
528 CompositeLocation condLoc =
529 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation());
530 addTypeLocation(tn.getCond().getType(), condLoc);
531 CompositeLocation trueLoc =
532 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation());
533 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
534 CompositeLocation falseLoc =
535 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation());
536 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
538 // check if condLoc is higher than trueLoc & falseLoc
539 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc)) {
541 "The location of the condition expression is lower than the true expression at "
542 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
545 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc)) {
547 "The location of the condition expression is lower than the true expression at "
548 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
551 // then, return glb of trueLoc & falseLoc
552 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
553 glbInputSet.add(trueLoc);
554 glbInputSet.add(falseLoc);
556 return CompositeLattice.calculateGLB(glbInputSet);
559 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
560 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc) {
562 if (min.numArgs() > 1) {
563 // caller needs to guarantee that it passes arguments in regarding to
564 // callee's hierarchy
565 for (int i = 0; i < min.numArgs(); i++) {
566 ExpressionNode en = min.getArg(i);
567 CompositeLocation callerArg1 =
568 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
570 ClassDescriptor calleecd = min.getMethod().getClassDesc();
571 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
572 CompositeLocation calleeLoc1 = d2loc.get(calleevd);
574 if (!callerArg1.get(0).isTop()) {
575 // here, check if ordering relations among caller's args respect
576 // ordering relations in-between callee's args
577 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
578 if (currentIdx != i) { // skip itself
579 ExpressionNode argExp = min.getArg(currentIdx);
581 CompositeLocation callerArg2 =
582 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation());
584 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
585 CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
587 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2);
588 boolean calleeResult = CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2);
590 if (calleeResult && !callerResult) {
591 // If calleeLoc1 is higher than calleeLoc2
592 // then, caller should have same ordering relation in-bet
593 // callerLoc1 & callerLoc2
595 throw new Error("Caller doesn't respect ordering relations among method arguments:"
596 + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
607 // all arguments should be higher than the location of return value
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,
620 // // method invocation from the same class
621 // CompositeLocation compLoc = new CompositeLocation();
625 Location baseLoc = null;
626 if (min.getBaseName() != null) {
627 Descriptor d = nametable.get(min.getBaseName().getSymbol());
628 if (d instanceof VarDescriptor) {
629 CompositeLocation varLoc =
630 ((CompositeLocation) ((VarDescriptor) d).getType().getExtension()).clone();
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 " +
724 // on.getLeft().getClass());
725 // System.out.println("right loc=" + rightLoc + " from " +
726 // on.getRight().getClass());
728 Operation op = on.getOp();
730 switch (op.getOp()) {
732 case Operation.UNARYPLUS:
733 case Operation.UNARYMINUS:
734 case Operation.LOGIC_NOT:
738 case Operation.LOGIC_OR:
739 case Operation.LOGIC_AND:
741 case Operation.BIT_OR:
742 case Operation.BIT_XOR:
743 case Operation.BIT_AND:
744 case Operation.ISAVAILABLE:
745 case Operation.EQUAL:
746 case Operation.NOTEQUAL:
756 case Operation.LEFTSHIFT:
757 case Operation.RIGHTSHIFT:
758 case Operation.URIGHTSHIFT:
760 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
761 inputSet.add(leftLoc);
762 inputSet.add(rightLoc);
763 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
767 throw new Error(op.toString());
772 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
773 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
775 // literal value has the top location so that value can be flowed into any
777 Location literalLoc = Location.createTopLocation(md);
778 loc.addLocation(literalLoc);
783 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
784 NameNode nn, CompositeLocation loc) {
786 NameDescriptor nd = nn.getName();
787 if (nd.getBase() != null) {
789 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
790 // addTypeLocation(nn.getExpression().getType(), loc);
792 String varname = nd.toString();
794 if (varname.equals("this")) {
796 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
797 String thisLocId = methodLattice.getThisLoc();
798 if (thisLocId == null) {
799 throw new Error("The location for 'this' is not defined at "
800 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
802 Location locElement = new Location(md, thisLocId);
803 loc.addLocation(locElement);
806 Descriptor d = (Descriptor) nametable.get(varname);
808 // CompositeLocation localLoc = null;
809 if (d instanceof VarDescriptor) {
810 VarDescriptor vd = (VarDescriptor) d;
811 // localLoc = d2loc.get(vd);
812 // the type of var descriptor has a composite location!
813 loc = ((CompositeLocation) vd.getType().getExtension()).clone();
814 } else if (d instanceof FieldDescriptor) {
815 // the type of field descriptor has a location!
816 FieldDescriptor fd = (FieldDescriptor) d;
820 // if it is 'static final', the location has TOP since no one can
822 loc.addLocation(Location.createTopLocation(md));
824 // if 'static', the location has pre-assigned global loc
825 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
826 String globalLocId = localLattice.getGlobalLoc();
827 if (globalLocId == null) {
828 throw new Error("Global location element is not defined in the method " + md);
830 Location globalLoc = new Location(md, globalLocId);
832 loc.addLocation(globalLoc);
835 // the location of field access starts from this, followed by field
837 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
838 Location thisLoc = new Location(md, localLattice.getThisLoc());
839 loc.addLocation(thisLoc);
842 Location fieldLoc = (Location) fd.getType().getExtension();
843 loc.addLocation(fieldLoc);
849 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
850 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
852 ExpressionNode left = fan.getExpression();
853 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
854 // addTypeLocation(left.getType(), loc);
856 if (!left.getType().isPrimitive()) {
857 FieldDescriptor fd = fan.getField();
858 Location fieldLoc = (Location) fd.getType().getExtension();
859 loc.addLocation(fieldLoc);
865 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
866 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
868 ClassDescriptor cd = md.getClassDesc();
870 boolean postinc = true;
871 if (an.getOperation().getBaseOp() == null
872 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
873 .getBaseOp().getOp() != Operation.POSTDEC))
876 CompositeLocation destLocation =
877 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation());
879 CompositeLocation srcLocation = new CompositeLocation();
882 if (hasOnlyLiteralValue(an.getSrc())) {
883 // if source is literal value, src location is TOP. so do not need to
887 srcLocation = new CompositeLocation();
888 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
889 // System.out.println(" an= " + an.printNode(0) + " an.getSrc()=" + an.getSrc().getClass()
890 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
891 // System.out.println("srcLocation=" + srcLocation);
892 // System.out.println("dstLocation=" + destLocation);
893 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
894 throw new Error("The value flow from " + srcLocation + " to " + destLocation
895 + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
896 + cd.getSourceFileName() + "::" + an.getNumLine());
900 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
902 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
903 throw new Error("Location " + destLocation
904 + " is not allowed to have the value flow that moves within the same location at "
905 + cd.getSourceFileName() + "::" + an.getNumLine());
913 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
914 SymbolTable nametable, TreeNode n) {
916 ClassDescriptor cd = md.getClassDesc();
917 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
919 // currently enforce every variable to have corresponding location
920 if (annotationVec.size() == 0) {
921 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
922 + md.getSymbol() + " of the class " + cd.getSymbol());
925 if (annotationVec.size() > 1) { // variable can have at most one location
926 throw new Error(vd.getSymbol() + " has more than one location.");
929 AnnotationDescriptor ad = annotationVec.elementAt(0);
931 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
933 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
934 String locDec = ad.getValue(); // check if location is defined
936 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
937 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
938 d2loc.put(vd, deltaLoc);
939 addTypeLocation(vd.getType(), deltaLoc);
941 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
942 d2loc.put(vd, compLoc);
943 addTypeLocation(vd.getType(), compLoc);
951 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
954 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
957 int beginIdx = dIdx + 6;
958 locDec = locDec.substring(beginIdx, locDec.length() - 1);
959 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
962 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
963 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
968 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
970 CompositeLocation compLoc = new CompositeLocation();
972 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
973 List<String> locIdList = new ArrayList<String>();
974 while (tokenizer.hasMoreTokens()) {
975 String locId = tokenizer.nextToken();
976 locIdList.add(locId);
979 // at least,one location element needs to be here!
980 assert (locIdList.size() > 0);
982 // assume that loc with idx 0 comes from the local lattice
983 // loc with idx 1 comes from the field lattice
985 String localLocId = locIdList.get(0);
986 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
987 Location localLoc = new Location(md, localLocId);
988 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
989 throw new Error("Location " + localLocId
990 + " is not defined in the local variable lattice at "
991 + md.getClassDesc().getSourceFileName() + "::" + n.getNumLine() + ".");
993 compLoc.addLocation(localLoc);
995 for (int i = 1; i < locIdList.size(); i++) {
996 String locName = locIdList.get(i);
997 ClassDescriptor cd = fieldLocName2cd.get(locName);
999 SSJavaLattice<String> fieldLattice = CompositeLattice.getLatticeByDescriptor(cd);
1001 if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
1002 throw new Error("Location " + locName + " is not defined in the field lattice at "
1003 + cd.getSourceFileName() + ".");
1006 Location fieldLoc = new Location(cd, locName);
1007 compLoc.addLocation(fieldLoc);
1014 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1015 VarDescriptor vd = dn.getVarDescriptor();
1016 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1019 private void checkClass(ClassDescriptor cd) {
1020 // Check to see that methods respects ss property
1021 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1022 MethodDescriptor md = (MethodDescriptor) method_it.next();
1023 checkMethodDeclaration(cd, md);
1027 private void checkDeclarationInClass(ClassDescriptor cd) {
1028 // Check to see that fields are okay
1029 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1030 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1031 checkFieldDeclaration(cd, fd);
1035 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1039 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1041 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1043 // currently enforce every field to have corresponding location
1044 if (annotationVec.size() == 0) {
1045 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1049 if (annotationVec.size() > 1) {
1050 // variable can have at most one location
1051 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1052 + " has more than one location.");
1055 AnnotationDescriptor ad = annotationVec.elementAt(0);
1057 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1059 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1060 String locationID = ad.getValue();
1061 // check if location is defined
1062 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1063 if (lattice == null || (!lattice.containsKey(locationID))) {
1064 throw new Error("Location " + locationID
1065 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1066 + cd.getSourceFileName() + ".");
1068 Location loc = new Location(cd, locationID);
1069 // d2loc.put(fd, loc);
1070 addTypeLocation(fd.getType(), loc);
1077 private void addTypeLocation(TypeDescriptor type, CompositeLocation loc) {
1079 type.setExtension(loc);
1083 private void addTypeLocation(TypeDescriptor type, Location loc) {
1085 type.setExtension(loc);
1089 static class CompositeLattice {
1091 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1093 // System.out.println("isGreaterThan= " + loc1 + " " + loc2);
1095 int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1096 if (baseCompareResult == ComparisonResult.EQUAL) {
1097 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1102 } else if (baseCompareResult == ComparisonResult.GREATER) {
1110 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1112 int deltaCount1 = 0;
1113 int deltaCount2 = 0;
1114 if (dLoc1 instanceof DeltaLocation) {
1115 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1118 if (dLoc2 instanceof DeltaLocation) {
1119 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1121 if (deltaCount1 < deltaCount2) {
1122 return ComparisonResult.GREATER;
1123 } else if (deltaCount1 == deltaCount2) {
1124 return ComparisonResult.EQUAL;
1126 return ComparisonResult.LESS;
1131 private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1133 // if compLoc1 is greater than compLoc2, return true
1134 // else return false;
1136 // compare one by one in according to the order of the tuple
1138 for (int i = 0; i < compLoc1.getSize(); i++) {
1139 Location loc1 = compLoc1.get(i);
1140 if (i >= compLoc2.getSize()) {
1141 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1142 + " because they are not comparable.");
1144 Location loc2 = compLoc2.get(i);
1146 if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1147 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1148 + " because they are not comparable.");
1151 Descriptor d1 = loc1.getDescriptor();
1152 Descriptor d2 = loc2.getDescriptor();
1154 SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1155 SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1157 // check if the spin location is appeared only at the end of the
1158 // composite location
1159 if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1160 if (i != (compLoc1.getSize() - 1)) {
1161 throw new Error("The spin location " + loc1.getLocIdentifier()
1162 + " cannot be appeared in the middle of composite location.");
1166 if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1167 if (i != (compLoc2.getSize() - 1)) {
1168 throw new Error("The spin location " + loc2.getLocIdentifier()
1169 + " cannot be appeared in the middle of composite location.");
1173 if (!lattice1.equals(lattice2)) {
1174 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1175 + " because they are not comparable.");
1178 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1180 // check if the current location is the spinning location
1181 // note that the spinning location only can be appeared in the last
1182 // part of the composite location
1183 if (numOfTie == compLoc1.getSize()
1184 && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1185 return ComparisonResult.GREATER;
1188 } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1189 return ComparisonResult.GREATER;
1191 return ComparisonResult.LESS;
1196 if (numOfTie == compLoc1.getSize()) {
1198 if (numOfTie != compLoc2.getSize()) {
1199 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1200 + " because they are not comparable.");
1203 return ComparisonResult.EQUAL;
1206 return ComparisonResult.LESS;
1210 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1212 // System.out.println("Calculating GLB=" + inputSet);
1213 CompositeLocation glbCompLoc = new CompositeLocation();
1215 // calculate GLB of the first(priority) element
1216 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1217 Descriptor priorityDescriptor = null;
1219 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1220 new Hashtable<String, Set<CompositeLocation>>();
1221 // mapping from the priority loc ID to its full representation by the
1222 // composite location
1224 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1225 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1226 Location priorityLoc = compLoc.get(0);
1227 String priorityLocId = priorityLoc.getLocIdentifier();
1228 priorityLocIdentifierSet.add(priorityLocId);
1230 if (locId2CompLocSet.contains(priorityLocId)) {
1231 locId2CompLocSet.get(priorityLocId).add(compLoc);
1233 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1234 newSet.add(compLoc);
1235 locId2CompLocSet.put(priorityLocId, newSet);
1238 // check if priority location are coming from the same lattice
1239 if (priorityDescriptor == null) {
1240 priorityDescriptor = priorityLoc.getDescriptor();
1241 } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1242 throw new Error("Failed to calculate GLB of " + inputSet
1243 + " because they are from different lattices.");
1247 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1248 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1250 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1251 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1253 if (compSet == null) {
1254 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1255 // mean that the result is already lower than <x1,y1> and <x2,y2>
1256 // assign TOP to the rest of the location elements
1257 CompositeLocation inputComp = inputSet.iterator().next();
1258 for (int i = 1; i < inputComp.getSize(); i++) {
1259 glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1262 if (compSet.size() == 1) {
1263 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1264 CompositeLocation comp = compSet.iterator().next();
1265 for (int i = 1; i < comp.getSize(); i++) {
1266 glbCompLoc.addLocation(comp.get(i));
1269 // when GLB(x1,x2)==x1 and x2 : GLB case 1
1270 // if more than one location shares the same priority GLB
1271 // need to calculate the rest of GLB loc
1273 int compositeLocSize = compSet.iterator().next().getSize();
1275 Set<String> glbInputSet = new HashSet<String>();
1276 Descriptor currentD = null;
1277 for (int i = 1; i < compositeLocSize; i++) {
1278 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1279 CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1280 Location currentLoc = compositeLocation.get(i);
1281 currentD = currentLoc.getDescriptor();
1282 // making set of the current location sharing the same idx
1283 glbInputSet.add(currentLoc.getLocIdentifier());
1285 // calculate glb for the current lattice
1287 SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1288 String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1289 glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1299 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1301 SSJavaLattice<String> lattice = null;
1303 if (d instanceof ClassDescriptor) {
1304 lattice = ssjava.getCd2lattice().get(d);
1305 } else if (d instanceof MethodDescriptor) {
1306 if (ssjava.getMd2lattice().containsKey(d)) {
1307 lattice = ssjava.getMd2lattice().get(d);
1309 // use default lattice for the method
1310 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1319 class ComparisonResult {
1321 public static final int GREATER = 0;
1322 public static final int EQUAL = 1;
1323 public static final int LESS = 2;