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>();
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()) {
101 ClassDescriptor superDesc = cd.getSuperDesc();
102 if (superDesc != null && (!superDesc.isInterface())
103 && (!superDesc.getSymbol().equals("Object"))) {
104 checkOrderingInheritance(superDesc, cd);
107 checkDeclarationInClass(cd);
108 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
109 MethodDescriptor md = (MethodDescriptor) method_it.next();
110 if (ssjava.hasAnnotation(md)) {
111 checkDeclarationInMethodBody(cd, md);
118 // phase2 : checking assignments
119 toanalyze.addAll(classtable.getValueSet());
120 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
121 while (!toanalyze.isEmpty()) {
122 Object obj = toanalyze.iterator().next();
123 ClassDescriptor cd = (ClassDescriptor) obj;
124 toanalyze.remove(cd);
127 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
128 MethodDescriptor md = (MethodDescriptor) method_it.next();
129 if (ssjava.hasAnnotation(md)) {
130 checkMethodBody(cd, md);
137 private void checkOrderingInheritance(ClassDescriptor superCd, ClassDescriptor cd) {
138 // here, we're going to check that sub class keeps same relative orderings
139 // in respect to super class
141 SSJavaLattice<String> superLattice = ssjava.getClassLattice(superCd);
142 SSJavaLattice<String> subLattice = ssjava.getClassLattice(cd);
144 if (superLattice != null && subLattice == null) {
145 throw new Error("If a parent class '" + superCd + "' has a ordering lattice, its subclass '"
146 + cd + "' should have one.");
149 Set<Pair<String, String>> superPairSet = superLattice.getOrderingPairSet();
150 Set<Pair<String, String>> subPairSet = subLattice.getOrderingPairSet();
152 for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
153 Pair<String, String> pair = (Pair<String, String>) iterator.next();
155 if (!subPairSet.contains(pair)) {
156 throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
157 + pair.getSecond() + " < " + pair.getFirst() + "' that is defined by its superclass '"
164 public Hashtable getMap() {
168 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
169 BlockNode bn = state.getMethodBody(md);
170 for (int i = 0; i < md.numParameters(); i++) {
171 // process annotations on method parameters
172 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
173 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
175 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
178 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
179 bn.getVarTable().setParent(nametable);
180 for (int i = 0; i < bn.size(); i++) {
181 BlockStatementNode bsn = bn.get(i);
182 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
186 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
187 BlockStatementNode bsn) {
189 switch (bsn.kind()) {
190 case Kind.SubBlockNode:
191 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
194 case Kind.DeclarationNode:
195 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
199 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
204 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
206 if (ln.getType() == LoopNode.FORLOOP) {
207 // check for loop case
208 ClassDescriptor cd = md.getClassDesc();
209 BlockNode bn = ln.getInitializer();
210 for (int i = 0; i < bn.size(); i++) {
211 BlockStatementNode bsn = bn.get(i);
212 checkDeclarationInBlockStatementNode(md, nametable, bsn);
217 checkDeclarationInBlockNode(md, nametable, ln.getBody());
220 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
221 BlockNode bn = state.getMethodBody(md);
222 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
225 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
228 bn.getVarTable().setParent(nametable);
229 // it will return the lowest location in the block node
230 CompositeLocation lowestLoc = null;
232 // if(bn.get(0).kind() == Kind.LoopNode){
233 // System.out.println("####="+bn.get(0).printNode(0));
236 for (int i = 0; i < bn.size(); i++) {
237 BlockStatementNode bsn = bn.get(i);
238 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
239 if (lowestLoc == null) {
242 if (!bLoc.isEmpty()) {
243 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc)) {
252 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
253 SymbolTable nametable, BlockStatementNode bsn) {
255 CompositeLocation compLoc = null;
256 switch (bsn.kind()) {
257 case Kind.BlockExpressionNode:
258 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
261 case Kind.DeclarationNode:
262 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
265 case Kind.IfStatementNode:
266 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
270 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
273 case Kind.ReturnNode:
274 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
277 case Kind.SubBlockNode:
278 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
281 // case Kind.ContinueBreakNode:
282 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
289 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
292 ExpressionNode returnExp = rn.getReturnExpression();
294 CompositeLocation expLoc =
295 checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation());
297 // callee should have a relative ordering in-between parameters and return
298 // value, which is required by caller's constraint
299 for (int i = 0; i < md.numParameters(); i++) {
300 Descriptor calleevd = md.getParameter(i);
301 CompositeLocation calleeParamLoc = d2loc.get(calleevd);
303 // here, parameter(input value) should be higher than result(output)
304 if ((!expLoc.get(0).isTop()) && !CompositeLattice.isGreaterThan(calleeParamLoc, expLoc)) {
305 throw new Error("Callee " + md + " doesn't have the ordering relation between parameter '"
306 + calleevd + "' and its return value '" + returnExp.printNode(0) + "'.");
310 // by default, return node has "bottom" location
311 CompositeLocation loc = new CompositeLocation();
312 loc.addLocation(Location.createBottomLocation(md));
316 private boolean hasOnlyLiteralValue(ExpressionNode en) {
317 if (en.kind() == Kind.LiteralNode) {
324 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
327 ClassDescriptor cd = md.getClassDesc();
328 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
330 CompositeLocation condLoc =
331 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation());
332 addTypeLocation(ln.getCondition().getType(), (condLoc));
334 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
336 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc)) {
337 // loop condition should be higher than loop body
339 "The location of the while-condition statement is lower than the loop body at "
340 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
346 // check for loop case
347 BlockNode bn = ln.getInitializer();
348 bn.getVarTable().setParent(nametable);
350 // calculate glb location of condition and update statements
351 CompositeLocation condLoc =
352 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
353 new CompositeLocation());
354 addTypeLocation(ln.getCondition().getType(), condLoc);
356 CompositeLocation updateLoc =
357 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
359 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
360 glbInputSet.add(condLoc);
361 glbInputSet.add(updateLoc);
363 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(glbInputSet);
365 // check location of 'forloop' body
366 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
368 if (blockLoc == null) {
369 // when there is no statement in the loop body
370 return glbLocOfForLoopCond;
373 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc)) {
375 "The location of the for-condition statement is lower than the for-loop body at "
376 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
383 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
384 SymbolTable nametable, SubBlockNode sbn) {
385 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
389 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
390 SymbolTable nametable, IfStatementNode isn) {
392 ClassDescriptor localCD = md.getClassDesc();
393 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
395 CompositeLocation condLoc =
396 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation());
398 addTypeLocation(isn.getCondition().getType(), condLoc);
399 glbInputSet.add(condLoc);
401 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
402 if (locTrueBlock != null) {
403 glbInputSet.add(locTrueBlock);
404 // here, the location of conditional block should be higher than the
405 // location of true/false blocks
406 if (locTrueBlock != null && !CompositeLattice.isGreaterThan(condLoc, locTrueBlock)) {
409 "The location of the if-condition statement is lower than the conditional block at "
410 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
414 if (isn.getFalseBlock() != null) {
415 CompositeLocation locFalseBlock =
416 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
418 if (locFalseBlock != null) {
419 glbInputSet.add(locFalseBlock);
421 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock)) {
424 "The location of the if-condition statement is lower than the conditional block at "
425 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
431 // return GLB location of condition, true, and false block
432 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
437 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
438 SymbolTable nametable, DeclarationNode dn) {
440 VarDescriptor vd = dn.getVarDescriptor();
442 CompositeLocation destLoc = d2loc.get(vd);
444 if (dn.getExpression() != null) {
445 CompositeLocation expressionLoc =
446 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
447 new CompositeLocation());
448 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
450 if (expressionLoc != null) {
451 // checking location order
452 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc)) {
453 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
454 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
455 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
458 return expressionLoc;
462 // if (destLoc instanceof Location) {
463 // CompositeLocation comp = new CompositeLocation();
464 // comp.addLocation(destLoc);
467 // return (CompositeLocation) destLoc;
475 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
477 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
480 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
481 SymbolTable nametable, BlockExpressionNode ben) {
482 CompositeLocation compLoc =
483 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
484 // addTypeLocation(ben.getExpression().getType(), compLoc);
488 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
489 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
491 CompositeLocation compLoc = null;
494 case Kind.AssignmentNode:
495 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
498 case Kind.FieldAccessNode:
499 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
503 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
507 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
510 case Kind.CreateObjectNode:
511 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
514 case Kind.ArrayAccessNode:
515 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
518 case Kind.LiteralNode:
519 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
522 case Kind.MethodInvokeNode:
523 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc);
526 case Kind.TertiaryNode:
527 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
531 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
534 // case Kind.InstanceOfNode:
535 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
538 // case Kind.ArrayInitializerNode:
539 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
543 // case Kind.ClassTypeNode:
544 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
547 // case Kind.OffsetNode:
548 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
555 // addTypeLocation(en.getType(), compLoc);
560 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
563 ExpressionNode en = cn.getExpression();
564 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
568 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
569 SymbolTable nametable, TertiaryNode tn) {
570 ClassDescriptor cd = md.getClassDesc();
572 CompositeLocation condLoc =
573 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation());
574 addTypeLocation(tn.getCond().getType(), condLoc);
575 CompositeLocation trueLoc =
576 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation());
577 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
578 CompositeLocation falseLoc =
579 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation());
580 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
582 // check if condLoc is higher than trueLoc & falseLoc
583 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc)) {
585 "The location of the condition expression is lower than the true expression at "
586 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
589 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc)) {
591 "The location of the condition expression is lower than the true expression at "
592 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
595 // then, return glb of trueLoc & falseLoc
596 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
597 glbInputSet.add(trueLoc);
598 glbInputSet.add(falseLoc);
600 return CompositeLattice.calculateGLB(glbInputSet);
603 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
604 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc) {
606 checkCalleeConstraints(md, nametable, min);
608 // all we need to care about is that
609 // method output(return value) should be lower than input values(method
611 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
612 for (int i = 0; i < min.numArgs(); i++) {
613 ExpressionNode en = min.getArg(i);
614 CompositeLocation callerArg =
615 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
616 inputGLBSet.add(callerArg);
619 if (inputGLBSet.size() > 0) {
620 return CompositeLattice.calculateGLB(inputGLBSet);
622 // if there are no arguments, just return TOP location
623 CompositeLocation compLoc = new CompositeLocation();
624 compLoc.addLocation(Location.createTopLocation(md));
630 private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
631 MethodInvokeNode min) {
633 if (min.numArgs() > 1) {
634 // caller needs to guarantee that it passes arguments in regarding to
635 // callee's hierarchy
636 for (int i = 0; i < min.numArgs(); i++) {
637 ExpressionNode en = min.getArg(i);
638 CompositeLocation callerArg1 =
639 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
641 ClassDescriptor calleecd = min.getMethod().getClassDesc();
642 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
643 CompositeLocation calleeLoc1 = d2loc.get(calleevd);
645 if (!callerArg1.get(0).isTop()) {
646 // here, check if ordering relations among caller's args respect
647 // ordering relations in-between callee's args
648 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
649 if (currentIdx != i) { // skip itself
650 ExpressionNode argExp = min.getArg(currentIdx);
652 CompositeLocation callerArg2 =
653 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation());
655 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
656 CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
658 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2);
659 boolean calleeResult = CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2);
661 if (calleeResult && !callerResult) {
662 // If calleeLoc1 is higher than calleeLoc2
663 // then, caller should have same ordering relation in-bet
664 // callerLoc1 & callerLoc2
666 throw new Error("Caller doesn't respect ordering relations among method arguments:"
667 + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
680 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
681 SymbolTable nametable, ArrayAccessNode aan) {
683 // return glb location of array itself and index
685 ClassDescriptor cd = md.getClassDesc();
687 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
689 CompositeLocation arrayLoc =
690 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation());
691 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
692 glbInputSet.add(arrayLoc);
693 CompositeLocation indexLoc =
694 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation());
695 glbInputSet.add(indexLoc);
696 // addTypeLocation(aan.getIndex().getType(), indexLoc);
698 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
702 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
703 SymbolTable nametable, CreateObjectNode con) {
705 ClassDescriptor cd = md.getClassDesc();
708 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
709 for (int i = 0; i < con.numArgs(); i++) {
710 ExpressionNode en = con.getArg(i);
711 CompositeLocation argLoc =
712 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
713 glbInputSet.add(argLoc);
714 addTypeLocation(en.getType(), argLoc);
717 // check array initializers
718 // if ((con.getArrayInitializer() != null)) {
719 // checkLocationFromArrayInitializerNode(md, nametable,
720 // con.getArrayInitializer());
723 if (glbInputSet.size() > 0) {
724 return CompositeLattice.calculateGLB(glbInputSet);
727 CompositeLocation compLoc = new CompositeLocation();
728 compLoc.addLocation(Location.createTopLocation(md));
733 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
736 ClassDescriptor cd = md.getClassDesc();
737 CompositeLocation leftLoc = new CompositeLocation();
738 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
739 // addTypeLocation(on.getLeft().getType(), leftLoc);
741 CompositeLocation rightLoc = new CompositeLocation();
742 if (on.getRight() != null) {
743 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
744 // addTypeLocation(on.getRight().getType(), rightLoc);
747 // System.out.println("checking op node=" + on.printNode(0));
748 // System.out.println("left loc=" + leftLoc + " from " +
749 // on.getLeft().getClass());
750 // System.out.println("right loc=" + rightLoc + " from " +
751 // on.getRight().getClass());
753 Operation op = on.getOp();
755 switch (op.getOp()) {
757 case Operation.UNARYPLUS:
758 case Operation.UNARYMINUS:
759 case Operation.LOGIC_NOT:
763 case Operation.LOGIC_OR:
764 case Operation.LOGIC_AND:
766 case Operation.BIT_OR:
767 case Operation.BIT_XOR:
768 case Operation.BIT_AND:
769 case Operation.ISAVAILABLE:
770 case Operation.EQUAL:
771 case Operation.NOTEQUAL:
781 case Operation.LEFTSHIFT:
782 case Operation.RIGHTSHIFT:
783 case Operation.URIGHTSHIFT:
785 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
786 inputSet.add(leftLoc);
787 inputSet.add(rightLoc);
788 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
792 throw new Error(op.toString());
797 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
798 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
800 // literal value has the top location so that value can be flowed into any
802 Location literalLoc = Location.createTopLocation(md);
803 loc.addLocation(literalLoc);
808 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
809 NameNode nn, CompositeLocation loc) {
811 NameDescriptor nd = nn.getName();
812 if (nd.getBase() != null) {
814 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
815 // addTypeLocation(nn.getExpression().getType(), loc);
817 String varname = nd.toString();
819 if (varname.equals("this")) {
821 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
822 String thisLocId = methodLattice.getThisLoc();
823 if (thisLocId == null) {
824 throw new Error("The location for 'this' is not defined at "
825 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
827 Location locElement = new Location(md, thisLocId);
828 loc.addLocation(locElement);
831 Descriptor d = (Descriptor) nametable.get(varname);
833 // CompositeLocation localLoc = null;
834 if (d instanceof VarDescriptor) {
835 VarDescriptor vd = (VarDescriptor) d;
836 // localLoc = d2loc.get(vd);
837 // the type of var descriptor has a composite location!
838 loc = ((CompositeLocation) vd.getType().getExtension()).clone();
839 } else if (d instanceof FieldDescriptor) {
840 // the type of field descriptor has a location!
841 FieldDescriptor fd = (FieldDescriptor) d;
845 // if it is 'static final', the location has TOP since no one can
847 loc.addLocation(Location.createTopLocation(md));
849 // if 'static', the location has pre-assigned global loc
850 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
851 String globalLocId = localLattice.getGlobalLoc();
852 if (globalLocId == null) {
853 throw new Error("Global location element is not defined in the method " + md);
855 Location globalLoc = new Location(md, globalLocId);
857 loc.addLocation(globalLoc);
860 // the location of field access starts from this, followed by field
862 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
863 Location thisLoc = new Location(md, localLattice.getThisLoc());
864 loc.addLocation(thisLoc);
867 Location fieldLoc = (Location) fd.getType().getExtension();
868 loc.addLocation(fieldLoc);
874 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
875 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
877 ExpressionNode left = fan.getExpression();
878 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
879 // addTypeLocation(left.getType(), loc);
881 if (!left.getType().isPrimitive()) {
882 FieldDescriptor fd = fan.getField();
883 Location fieldLoc = (Location) fd.getType().getExtension();
884 loc.addLocation(fieldLoc);
890 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
891 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
893 ClassDescriptor cd = md.getClassDesc();
895 boolean postinc = true;
896 if (an.getOperation().getBaseOp() == null
897 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
898 .getBaseOp().getOp() != Operation.POSTDEC))
901 CompositeLocation destLocation =
902 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation());
904 CompositeLocation srcLocation = new CompositeLocation();
907 if (hasOnlyLiteralValue(an.getSrc())) {
908 // if source is literal value, src location is TOP. so do not need to
912 srcLocation = new CompositeLocation();
913 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
914 // System.out.println(" an= " + an.printNode(0) + " an.getSrc()=" +
915 // an.getSrc().getClass()
916 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
917 // System.out.println("srcLocation=" + srcLocation);
918 // System.out.println("dstLocation=" + destLocation);
919 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
920 throw new Error("The value flow from " + srcLocation + " to " + destLocation
921 + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
922 + cd.getSourceFileName() + "::" + an.getNumLine());
926 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
928 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
929 throw new Error("Location " + destLocation
930 + " is not allowed to have the value flow that moves within the same location at "
931 + cd.getSourceFileName() + "::" + an.getNumLine());
939 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
940 SymbolTable nametable, TreeNode n) {
942 ClassDescriptor cd = md.getClassDesc();
943 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
945 // currently enforce every variable to have corresponding location
946 if (annotationVec.size() == 0) {
947 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
948 + md.getSymbol() + " of the class " + cd.getSymbol());
951 if (annotationVec.size() > 1) { // variable can have at most one location
952 throw new Error(vd.getSymbol() + " has more than one location.");
955 AnnotationDescriptor ad = annotationVec.elementAt(0);
957 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
959 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
960 String locDec = ad.getValue(); // check if location is defined
962 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
963 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
964 d2loc.put(vd, deltaLoc);
965 addTypeLocation(vd.getType(), deltaLoc);
967 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
968 d2loc.put(vd, compLoc);
969 addTypeLocation(vd.getType(), compLoc);
977 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
980 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
983 int beginIdx = dIdx + 6;
984 locDec = locDec.substring(beginIdx, locDec.length() - 1);
985 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
988 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
989 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
994 private Location parseFieldLocDeclaraton(String decl) {
996 int idx = decl.indexOf(".");
997 String className = decl.substring(0, idx);
998 String fieldName = decl.substring(idx + 1);
1000 Descriptor d = state.getClassSymbolTable().get(className);
1002 assert (d instanceof ClassDescriptor);
1003 SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1004 if (!lattice.containsKey(fieldName)) {
1005 throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1006 + className + "'.");
1009 return new Location(d, fieldName);
1012 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1014 CompositeLocation compLoc = new CompositeLocation();
1016 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1017 List<String> locIdList = new ArrayList<String>();
1018 while (tokenizer.hasMoreTokens()) {
1019 String locId = tokenizer.nextToken();
1020 locIdList.add(locId);
1023 // at least,one location element needs to be here!
1024 assert (locIdList.size() > 0);
1026 // assume that loc with idx 0 comes from the local lattice
1027 // loc with idx 1 comes from the field lattice
1029 String localLocId = locIdList.get(0);
1030 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1031 Location localLoc = new Location(md, localLocId);
1032 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1033 throw new Error("Location " + localLocId
1034 + " is not defined in the local variable lattice at "
1035 + md.getClassDesc().getSourceFileName() + "::" + n.getNumLine() + ".");
1037 compLoc.addLocation(localLoc);
1039 for (int i = 1; i < locIdList.size(); i++) {
1040 String locName = locIdList.get(i);
1042 Location fieldLoc = parseFieldLocDeclaraton(locName);
1043 // ClassDescriptor cd = fieldLocName2cd.get(locName);
1044 // SSJavaLattice<String> fieldLattice =
1045 // CompositeLattice.getLatticeByDescriptor(cd);
1047 // if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
1048 // throw new Error("Location " + locName +
1049 // " is not defined in the field lattice at "
1050 // + cd.getSourceFileName() + ".");
1052 // Location fieldLoc = new Location(cd, locName);
1053 compLoc.addLocation(fieldLoc);
1060 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1061 VarDescriptor vd = dn.getVarDescriptor();
1062 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1065 private void checkClass(ClassDescriptor cd) {
1066 // Check to see that methods respects ss property
1067 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1068 MethodDescriptor md = (MethodDescriptor) method_it.next();
1069 checkMethodDeclaration(cd, md);
1073 private void checkDeclarationInClass(ClassDescriptor cd) {
1074 // Check to see that fields are okay
1075 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1076 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1077 checkFieldDeclaration(cd, fd);
1081 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1085 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1087 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1089 // currently enforce every field to have corresponding location
1090 if (annotationVec.size() == 0) {
1091 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1095 if (annotationVec.size() > 1) {
1096 // variable can have at most one location
1097 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1098 + " has more than one location.");
1101 AnnotationDescriptor ad = annotationVec.elementAt(0);
1103 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1105 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1106 String locationID = ad.getValue();
1107 // check if location is defined
1108 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1109 if (lattice == null || (!lattice.containsKey(locationID))) {
1110 throw new Error("Location " + locationID
1111 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1112 + cd.getSourceFileName() + ".");
1114 Location loc = new Location(cd, locationID);
1115 // d2loc.put(fd, loc);
1116 addTypeLocation(fd.getType(), loc);
1123 private void addTypeLocation(TypeDescriptor type, CompositeLocation loc) {
1125 type.setExtension(loc);
1129 private void addTypeLocation(TypeDescriptor type, Location loc) {
1131 type.setExtension(loc);
1135 static class CompositeLattice {
1137 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1139 // System.out.println("isGreaterThan= " + loc1 + " " + loc2);
1141 int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1142 if (baseCompareResult == ComparisonResult.EQUAL) {
1143 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1148 } else if (baseCompareResult == ComparisonResult.GREATER) {
1156 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1158 int deltaCount1 = 0;
1159 int deltaCount2 = 0;
1160 if (dLoc1 instanceof DeltaLocation) {
1161 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1164 if (dLoc2 instanceof DeltaLocation) {
1165 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1167 if (deltaCount1 < deltaCount2) {
1168 return ComparisonResult.GREATER;
1169 } else if (deltaCount1 == deltaCount2) {
1170 return ComparisonResult.EQUAL;
1172 return ComparisonResult.LESS;
1177 private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1179 // if compLoc1 is greater than compLoc2, return true
1180 // else return false;
1182 // compare one by one in according to the order of the tuple
1184 for (int i = 0; i < compLoc1.getSize(); i++) {
1185 Location loc1 = compLoc1.get(i);
1186 if (i >= compLoc2.getSize()) {
1187 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1188 + " because they are not comparable.");
1190 Location loc2 = compLoc2.get(i);
1192 if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1193 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1194 + " because they are not comparable.");
1197 Descriptor d1 = loc1.getDescriptor();
1198 Descriptor d2 = loc2.getDescriptor();
1200 SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1201 SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1203 // check if the spin location is appeared only at the end of the
1204 // composite location
1205 if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1206 if (i != (compLoc1.getSize() - 1)) {
1207 throw new Error("The spin location " + loc1.getLocIdentifier()
1208 + " cannot be appeared in the middle of composite location.");
1212 if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1213 if (i != (compLoc2.getSize() - 1)) {
1214 throw new Error("The spin location " + loc2.getLocIdentifier()
1215 + " cannot be appeared in the middle of composite location.");
1219 if (!lattice1.equals(lattice2)) {
1220 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1221 + " because they are not comparable.");
1224 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1226 // check if the current location is the spinning location
1227 // note that the spinning location only can be appeared in the last
1228 // part of the composite location
1229 if (numOfTie == compLoc1.getSize()
1230 && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1231 return ComparisonResult.GREATER;
1234 } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1235 return ComparisonResult.GREATER;
1237 return ComparisonResult.LESS;
1242 if (numOfTie == compLoc1.getSize()) {
1244 if (numOfTie != compLoc2.getSize()) {
1245 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1246 + " because they are not comparable.");
1249 return ComparisonResult.EQUAL;
1252 return ComparisonResult.LESS;
1256 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1258 // System.out.println("Calculating GLB=" + inputSet);
1259 CompositeLocation glbCompLoc = new CompositeLocation();
1261 // calculate GLB of the first(priority) element
1262 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1263 Descriptor priorityDescriptor = null;
1265 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1266 new Hashtable<String, Set<CompositeLocation>>();
1267 // mapping from the priority loc ID to its full representation by the
1268 // composite location
1270 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1271 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1272 Location priorityLoc = compLoc.get(0);
1273 String priorityLocId = priorityLoc.getLocIdentifier();
1274 priorityLocIdentifierSet.add(priorityLocId);
1276 if (locId2CompLocSet.contains(priorityLocId)) {
1277 locId2CompLocSet.get(priorityLocId).add(compLoc);
1279 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1280 newSet.add(compLoc);
1281 locId2CompLocSet.put(priorityLocId, newSet);
1284 // check if priority location are coming from the same lattice
1285 if (priorityDescriptor == null) {
1286 priorityDescriptor = priorityLoc.getDescriptor();
1287 } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1288 throw new Error("Failed to calculate GLB of " + inputSet
1289 + " because they are from different lattices.");
1293 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1294 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1296 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1297 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1299 if (compSet == null) {
1300 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1301 // mean that the result is already lower than <x1,y1> and <x2,y2>
1302 // assign TOP to the rest of the location elements
1303 CompositeLocation inputComp = inputSet.iterator().next();
1304 for (int i = 1; i < inputComp.getSize(); i++) {
1305 glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1308 if (compSet.size() == 1) {
1309 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1310 CompositeLocation comp = compSet.iterator().next();
1311 for (int i = 1; i < comp.getSize(); i++) {
1312 glbCompLoc.addLocation(comp.get(i));
1315 // when GLB(x1,x2)==x1 and x2 : GLB case 1
1316 // if more than one location shares the same priority GLB
1317 // need to calculate the rest of GLB loc
1319 int compositeLocSize = compSet.iterator().next().getSize();
1321 Set<String> glbInputSet = new HashSet<String>();
1322 Descriptor currentD = null;
1323 for (int i = 1; i < compositeLocSize; i++) {
1324 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1325 CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1326 Location currentLoc = compositeLocation.get(i);
1327 currentD = currentLoc.getDescriptor();
1328 // making set of the current location sharing the same idx
1329 glbInputSet.add(currentLoc.getLocIdentifier());
1331 // calculate glb for the current lattice
1333 SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1334 String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1335 glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1345 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1347 SSJavaLattice<String> lattice = null;
1349 if (d instanceof ClassDescriptor) {
1350 lattice = ssjava.getCd2lattice().get(d);
1351 } else if (d instanceof MethodDescriptor) {
1352 if (ssjava.getMd2lattice().containsKey(d)) {
1353 lattice = ssjava.getMd2lattice().get(d);
1355 // use default lattice for the method
1356 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1365 class ComparisonResult {
1367 public static final int GREATER = 0;
1368 public static final int EQUAL = 1;
1369 public static final int LESS = 2;