1 package Analysis.SSJava;
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Hashtable;
6 import java.util.Iterator;
10 import java.util.StringTokenizer;
11 import java.util.Vector;
13 import IR.AnnotationDescriptor;
14 import IR.ClassDescriptor;
16 import IR.FieldDescriptor;
17 import IR.MethodDescriptor;
18 import IR.NameDescriptor;
21 import IR.SymbolTable;
22 import IR.TypeDescriptor;
23 import IR.VarDescriptor;
24 import IR.Tree.ArrayAccessNode;
25 import IR.Tree.AssignmentNode;
26 import IR.Tree.BlockExpressionNode;
27 import IR.Tree.BlockNode;
28 import IR.Tree.BlockStatementNode;
29 import IR.Tree.CastNode;
30 import IR.Tree.CreateObjectNode;
31 import IR.Tree.DeclarationNode;
32 import IR.Tree.ExpressionNode;
33 import IR.Tree.FieldAccessNode;
34 import IR.Tree.IfStatementNode;
36 import IR.Tree.LiteralNode;
37 import IR.Tree.LoopNode;
38 import IR.Tree.MethodInvokeNode;
39 import IR.Tree.NameNode;
40 import IR.Tree.OpNode;
41 import IR.Tree.ReturnNode;
42 import IR.Tree.SubBlockNode;
43 import IR.Tree.TertiaryNode;
44 import IR.Tree.TreeNode;
48 public class FlowDownCheck {
52 Hashtable<Descriptor, Location> td2loc; // mapping from 'type descriptor'
54 Hashtable<String, ClassDescriptor> id2cd; // mapping from 'locID' to 'class
57 public FlowDownCheck(State state) {
59 this.toanalyze = new HashSet();
60 this.td2loc = new Hashtable<Descriptor, Location>();
65 id2cd = new Hashtable<String, ClassDescriptor>();
66 Hashtable cd2lattice = state.getCd2LocationOrder();
68 Set cdSet = cd2lattice.keySet();
69 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
70 ClassDescriptor cd = (ClassDescriptor) iterator.next();
71 Lattice<String> lattice = (Lattice<String>) cd2lattice.get(cd);
73 Set<String> locIdSet = lattice.getKeySet();
74 for (Iterator iterator2 = locIdSet.iterator(); iterator2.hasNext();) {
75 String locID = (String) iterator2.next();
82 public void flowDownCheck() {
83 SymbolTable classtable = state.getClassSymbolTable();
85 // phase 1 : checking declaration node and creating mapping of 'type
86 // desciptor' & 'location'
87 toanalyze.addAll(classtable.getValueSet());
88 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
89 while (!toanalyze.isEmpty()) {
90 Object obj = toanalyze.iterator().next();
91 ClassDescriptor cd = (ClassDescriptor) obj;
94 if (!cd.isInterface()) {
95 checkDeclarationInClass(cd);
96 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
97 MethodDescriptor md = (MethodDescriptor) method_it.next();
99 checkDeclarationInMethodBody(cd, md);
101 System.out.println("Error in " + md);
109 // post-processing for delta location
110 // for a nested delta location, assigning a concrete reference to delta
112 Set<Descriptor> tdSet = td2loc.keySet();
113 for (Iterator iterator = tdSet.iterator(); iterator.hasNext();) {
114 Descriptor td = (Descriptor) iterator.next();
115 Location loc = td2loc.get(td);
117 if (loc.getType() == Location.DELTA) {
118 // if it contains delta reference pointing to another location element
119 CompositeLocation compLoc = (CompositeLocation) loc;
121 Location locElement = compLoc.getTuple().at(0);
122 assert (locElement instanceof DeltaLocation);
124 DeltaLocation delta = (DeltaLocation) locElement;
125 Descriptor refType = delta.getRefLocationId();
126 if (refType != null) {
127 Location refLoc = td2loc.get(refType);
129 assert (refLoc instanceof CompositeLocation);
130 CompositeLocation refCompLoc = (CompositeLocation) refLoc;
132 assert (refCompLoc.getTuple().at(0) instanceof DeltaLocation);
133 DeltaLocation refDelta = (DeltaLocation) refCompLoc.getTuple().at(0);
135 delta.addDeltaOperand(refDelta);
136 // compLoc.addLocation(refDelta);
142 // phase2 : checking assignments
143 toanalyze.addAll(classtable.getValueSet());
144 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
145 while (!toanalyze.isEmpty()) {
146 Object obj = toanalyze.iterator().next();
147 ClassDescriptor cd = (ClassDescriptor) obj;
148 toanalyze.remove(cd);
151 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
152 MethodDescriptor md = (MethodDescriptor) method_it.next();
154 checkMethodBody(cd, md);
156 System.out.println("Error in " + md);
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;
231 for (int i = 0; i < bn.size(); i++) {
232 BlockStatementNode bsn = bn.get(i);
233 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
235 if (lowestLoc == null) {
238 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc, md.getClassDesc())) {
246 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
247 SymbolTable nametable, BlockStatementNode bsn) {
249 CompositeLocation compLoc = null;
250 switch (bsn.kind()) {
251 case Kind.BlockExpressionNode:
252 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
255 case Kind.DeclarationNode:
256 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
259 case Kind.IfStatementNode:
260 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
264 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
267 case Kind.ReturnNode:
268 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
271 case Kind.SubBlockNode:
272 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
275 // case Kind.ContinueBreakNode:
276 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
283 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
285 ClassDescriptor cd = md.getClassDesc();
286 CompositeLocation loc = new CompositeLocation(cd);
288 ExpressionNode returnExp = rn.getReturnExpression();
290 if (rn == null || hasOnlyLiteralValue(returnExp)) {
291 // when it returns literal value, return node has "bottom" location no
292 // matter what it is going to return.
293 loc.addLocation(Location.createBottomLocation(cd));
295 loc = checkLocationFromExpressionNode(md, nametable, returnExp, loc);
300 private boolean hasOnlyLiteralValue(ExpressionNode returnExp) {
301 if (returnExp.kind() == Kind.LiteralNode) {
308 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
311 ClassDescriptor cd = md.getClassDesc();
312 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
314 CompositeLocation condLoc =
315 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation(
317 addTypeLocation(ln.getCondition().getType(), (condLoc));
319 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
321 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc, cd)) {
322 // loop condition should be higher than loop body
324 "The location of the while-condition statement is lower than the loop body at "
325 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
331 // check for loop case
332 BlockNode bn = ln.getInitializer();
333 bn.getVarTable().setParent(nametable);
335 // calculate glb location of condition and update statements
336 CompositeLocation condLoc =
337 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
338 new CompositeLocation(cd));
339 addTypeLocation(ln.getCondition().getType(), condLoc);
341 CompositeLocation updateLoc =
342 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
344 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
345 glbInputSet.add(condLoc);
346 glbInputSet.add(updateLoc);
348 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
350 // check location of 'forloop' body
351 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
353 if (blockLoc == null) {
354 // when there is no statement in the loop body
355 return glbLocOfForLoopCond;
358 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc, cd)) {
360 "The location of the for-condition statement is lower than the for-loop body at "
361 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
368 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
369 SymbolTable nametable, SubBlockNode sbn) {
370 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
374 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
375 SymbolTable nametable, IfStatementNode isn) {
377 ClassDescriptor localCD = md.getClassDesc();
378 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
380 CompositeLocation condLoc =
381 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(
383 addTypeLocation(isn.getCondition().getType(), condLoc);
384 glbInputSet.add(condLoc);
386 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
387 glbInputSet.add(locTrueBlock);
389 // here, the location of conditional block should be higher than the
390 // location of true/false blocks
392 if (!CompositeLattice.isGreaterThan(condLoc, locTrueBlock, localCD)) {
395 "The location of the if-condition statement is lower than the conditional block at "
396 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
399 if (isn.getFalseBlock() != null) {
400 CompositeLocation locFalseBlock =
401 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
402 glbInputSet.add(locFalseBlock);
404 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock, localCD)) {
407 "The location of the if-condition statement is lower than the conditional block at "
408 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
413 // return GLB location of condition, true, and false block
414 CompositeLocation glbLoc = CompositeLattice.calculateGLB(localCD, glbInputSet, localCD);
419 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
420 SymbolTable nametable, DeclarationNode dn) {
421 VarDescriptor vd = dn.getVarDescriptor();
423 Location destLoc = td2loc.get(vd);
425 ClassDescriptor localCD = md.getClassDesc();
426 if (dn.getExpression() != null) {
427 CompositeLocation expressionLoc =
428 checkLocationFromExpressionNode(md, nametable, dn.getExpression(), new CompositeLocation(
430 addTypeLocation(dn.getExpression().getType(), expressionLoc);
432 if (expressionLoc != null) {
433 // checking location order
434 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc, localCD)) {
435 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
436 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
437 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
440 return expressionLoc;
444 if (destLoc instanceof Location) {
445 CompositeLocation comp = new CompositeLocation(localCD);
446 comp.addLocation(destLoc);
449 return (CompositeLocation) destLoc;
456 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
458 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
461 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
462 SymbolTable nametable, BlockExpressionNode ben) {
463 CompositeLocation compLoc =
464 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
465 addTypeLocation(ben.getExpression().getType(), compLoc);
469 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
470 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
472 CompositeLocation compLoc = null;
475 case Kind.AssignmentNode:
476 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
479 case Kind.FieldAccessNode:
480 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
484 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
488 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
491 case Kind.CreateObjectNode:
492 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
495 case Kind.ArrayAccessNode:
496 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
499 case Kind.LiteralNode:
500 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
503 case Kind.MethodInvokeNode:
504 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en);
507 case Kind.TertiaryNode:
508 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
512 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
515 // case Kind.InstanceOfNode:
516 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
519 // case Kind.ArrayInitializerNode:
520 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
524 // case Kind.ClassTypeNode:
525 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
528 // case Kind.OffsetNode:
529 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
537 addTypeLocation(en.getType(), compLoc);
542 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
545 ExpressionNode en = cn.getExpression();
546 return checkLocationFromExpressionNode(md, nametable, en,
547 new CompositeLocation(md.getClassDesc()));
551 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
552 SymbolTable nametable, TertiaryNode tn) {
553 ClassDescriptor cd = md.getClassDesc();
555 CompositeLocation condLoc =
556 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(cd));
557 addTypeLocation(tn.getCond().getType(), condLoc);
558 CompositeLocation trueLoc =
559 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(cd));
560 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
561 CompositeLocation falseLoc =
562 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(cd));
563 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
565 // check if condLoc is higher than trueLoc & falseLoc
566 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc, cd)) {
568 "The location of the condition expression is lower than the true expression at "
569 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
572 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc, cd)) {
574 "The location of the condition expression is lower than the true expression at "
575 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
578 // then, return glb of trueLoc & falseLoc
579 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
580 glbInputSet.add(trueLoc);
581 glbInputSet.add(falseLoc);
583 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
586 private CompositeLocation convertCompositeLocation(Location l, ClassDescriptor c) {
587 if (l instanceof CompositeLocation) {
588 return (CompositeLocation) l;
590 CompositeLocation returnLoc = new CompositeLocation(c);
591 returnLoc.addLocation(l);
596 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
597 SymbolTable nametable, MethodInvokeNode min) {
599 ClassDescriptor cd = md.getClassDesc();
601 CompositeLocation baseLoc = null;
602 if (min.getBaseName() != null) {
603 if (nametable.contains(min.getBaseName().getSymbol())) {
604 Location loc = td2loc.get(nametable.get(min.getBaseName().getSymbol()));
606 baseLoc = convertCompositeLocation(loc, cd);
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(cd));
616 inputGLBSet.add(callerArg);
619 // ex) base.method(arg1,arg2,arg3) -> the location of base should be lower
621 // GLB(arg1,arg2,arg3)
622 CompositeLocation argGLBLoc = null;
623 if (inputGLBSet.size() > 0) {
624 argGLBLoc = CompositeLattice.calculateGLB(cd, inputGLBSet, cd);
625 if (baseLoc != null) {
626 if (!CompositeLattice.isGreaterThan(argGLBLoc, baseLoc, cd)) {
627 throw new Error("The base location of method invocation " + min.printNode(0)
628 + " is higher than its argument's location at " + cd.getSourceFileName() + "::"
634 if (min.numArgs() > 1) {
636 // caller needs to guarantee that it passes arguments in regarding to
637 // callee's hierarchy
639 for (int i = 0; i < min.numArgs(); i++) {
640 ExpressionNode en = min.getArg(i);
641 CompositeLocation callerArg1 =
642 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
644 ClassDescriptor calleecd = min.getMethod().getClassDesc();
645 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
646 Location calleeLoc1 = td2loc.get(calleevd);
648 if (!callerArg1.getLocation(cd).isTop()) {
649 // here, check if ordering relations among caller's args respect
650 // ordering relations in-between callee's args
651 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
652 if (currentIdx != i) { // skip itself
653 ExpressionNode argExp = min.getArg(currentIdx);
654 CompositeLocation callerArg2 =
655 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation(cd));
657 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
658 Location calleeLoc2 = td2loc.get(calleevd2);
659 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2, cd);
660 boolean calleeResult =
661 CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2, calleecd);
663 if (calleeResult && !callerResult) {
664 // in callee, calleeLoc1 is higher than calleeLoc2
665 // then, caller should have same ordering relation in-bet
666 // callerLoc1 & callerLoc2
668 throw new Error("Caller doesn't respect ordering relations among method arguments:"
669 + cd.getSourceFileName() + ":" + min.getNumLine());
680 // all arguments should be higher than the location of return value
682 if (inputGLBSet.size() > 0) {
683 if (baseLoc != null) {
684 inputGLBSet.add(baseLoc);
685 return CompositeLattice.calculateGLB(cd, inputGLBSet, cd);
690 // if there are no arguments,
691 if (baseLoc != null) {
694 // method invocation from the same class
695 CompositeLocation returnLoc = new CompositeLocation(cd);
696 returnLoc.addLocation(Location.createTopLocation(cd));
702 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
703 SymbolTable nametable, ArrayAccessNode aan) {
705 // return glb location of array itself and index
707 ClassDescriptor cd = md.getClassDesc();
709 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
711 CompositeLocation arrayLoc =
712 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation(
714 addTypeLocation(aan.getExpression().getType(), arrayLoc);
715 glbInputSet.add(arrayLoc);
716 CompositeLocation indexLoc =
717 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(cd));
718 glbInputSet.add(indexLoc);
719 addTypeLocation(aan.getIndex().getType(), indexLoc);
721 CompositeLocation glbLoc = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
725 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
726 SymbolTable nametable, CreateObjectNode con) {
728 ClassDescriptor cd = md.getClassDesc();
731 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
732 for (int i = 0; i < con.numArgs(); i++) {
733 ExpressionNode en = con.getArg(i);
734 CompositeLocation argLoc =
735 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
736 glbInputSet.add(argLoc);
737 addTypeLocation(en.getType(), argLoc);
740 // check array initializers
741 // if ((con.getArrayInitializer() != null)) {
742 // checkLocationFromArrayInitializerNode(md, nametable,
743 // con.getArrayInitializer());
746 if (glbInputSet.size() > 0) {
747 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
750 CompositeLocation compLoc = new CompositeLocation(cd);
751 compLoc.addLocation(Location.createTopLocation(cd));
756 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
759 ClassDescriptor cd = md.getClassDesc();
760 CompositeLocation leftLoc = new CompositeLocation(cd);
761 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
762 addTypeLocation(on.getLeft().getType(), leftLoc);
764 CompositeLocation rightLoc = new CompositeLocation(cd);
765 if (on.getRight() != null) {
766 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
767 addTypeLocation(on.getRight().getType(), rightLoc);
770 // System.out.println("checking op node=" + on.printNode(0));
771 // System.out.println("left loc=" + leftLoc + " from " +
772 // on.getLeft().getClass());
773 // System.out.println("right loc=" + rightLoc + " from " +
774 // on.getLeft().getClass());
776 Operation op = on.getOp();
778 switch (op.getOp()) {
780 case Operation.UNARYPLUS:
781 case Operation.UNARYMINUS:
782 case Operation.LOGIC_NOT:
786 case Operation.LOGIC_OR:
787 case Operation.LOGIC_AND:
789 case Operation.BIT_OR:
790 case Operation.BIT_XOR:
791 case Operation.BIT_AND:
792 case Operation.ISAVAILABLE:
793 case Operation.EQUAL:
794 case Operation.NOTEQUAL:
804 case Operation.LEFTSHIFT:
805 case Operation.RIGHTSHIFT:
806 case Operation.URIGHTSHIFT:
808 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
809 inputSet.add(leftLoc);
810 inputSet.add(rightLoc);
811 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(cd, inputSet, cd);
815 throw new Error(op.toString());
820 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
821 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
823 // literal value has the top location so that value can be flowed into any
825 Location literalLoc = Location.createTopLocation(md.getClassDesc());
826 loc.addLocation(literalLoc);
831 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
832 NameNode nn, CompositeLocation loc) {
834 NameDescriptor nd = nn.getName();
835 if (nd.getBase() != null) {
836 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
837 addTypeLocation(nn.getExpression().getType(), loc);
840 String varname = nd.toString();
841 if (varname.equals("this")) {
842 // 'this' itself is top location in the local hierarchy
843 loc.addLocation(Location.createTopLocation(md.getClassDesc()));
846 Descriptor d = (Descriptor) nametable.get(varname);
848 Location localLoc = null;
849 if (d instanceof VarDescriptor) {
850 VarDescriptor vd = (VarDescriptor) d;
851 localLoc = td2loc.get(vd);
852 } else if (d instanceof FieldDescriptor) {
853 FieldDescriptor fd = (FieldDescriptor) d;
854 localLoc = td2loc.get(fd);
856 assert (localLoc != null);
858 if (localLoc instanceof CompositeLocation) {
859 loc = (CompositeLocation) localLoc;
861 loc.addLocation(localLoc);
868 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
869 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
871 ExpressionNode left = fan.getExpression();
872 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
873 addTypeLocation(left.getType(), loc);
875 if (!left.getType().isPrimitive()) {
876 FieldDescriptor fd = fan.getField();
877 Location fieldLoc = td2loc.get(fd);
879 // in the case of "this.field", need to get rid of 'this' location from
880 // the composite location
881 if (loc.getCd2Loc().containsKey(md.getClassDesc())) {
882 loc.removieLocation(md.getClassDesc());
884 loc.addLocation(fieldLoc);
890 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
891 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
892 ClassDescriptor cd = md.getClassDesc();
894 boolean postinc = true;
895 if (an.getOperation().getBaseOp() == null
896 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
897 .getBaseOp().getOp() != Operation.POSTDEC))
900 CompositeLocation destLocation =
901 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(cd));
903 CompositeLocation srcLocation = new CompositeLocation(cd);
905 srcLocation = new CompositeLocation(cd);
906 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
907 // System.out.println("an=" + an.printNode(0) + " an.getSrc()=" +
908 // an.getSrc().getClass()
909 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
910 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, cd)) {
911 throw new Error("The value flow from " + srcLocation + " to " + destLocation
912 + " does not respect location hierarchy on the assignment " + an.printNode(0) + "at "
913 + cd.getSourceFileName() + "::" + an.getNumLine());
917 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
919 if (!((Set<String>) state.getCd2LocationPropertyMap().get(new Pair(cd, "spin")))
920 .contains(destLocation.getLocation(cd).getLocIdentifier())) {
921 throw new Error("Location " + destLocation + " is not allowed to have spinning values at "
922 + cd.getSourceFileName() + ":" + an.getNumLine());
926 if (an.getSrc() != null) {
927 addTypeLocation(an.getSrc().getType(), srcLocation);
929 addTypeLocation(an.getDest().getType(), destLocation);
934 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
935 SymbolTable nametable, TreeNode n) {
937 ClassDescriptor cd = md.getClassDesc();
938 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
940 // currently enforce every variable to have corresponding location
941 if (annotationVec.size() == 0) {
942 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
943 + md.getSymbol() + " of the class " + cd.getSymbol());
946 if (annotationVec.size() > 1) {
947 // variable can have at most one location
948 throw new Error(vd.getSymbol() + " has more than one location.");
951 AnnotationDescriptor ad = annotationVec.elementAt(0);
953 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
955 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
956 String locationID = ad.getValue();
957 // check if location is defined
958 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
959 if (lattice == null || (!lattice.containsKey(locationID))) {
960 throw new Error("Location " + locationID
961 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
963 Location loc = new Location(cd, locationID);
965 addTypeLocation(vd.getType(), loc);
967 } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
969 CompositeLocation compLoc = new CompositeLocation(cd);
971 if (ad.getValue().length() == 0) {
972 throw new Error("Delta function of " + vd.getSymbol() + " does not have any locations: "
973 + cd.getSymbol() + ".");
976 String deltaStr = ad.getValue();
977 if (deltaStr.startsWith("LOC(")) {
979 if (!deltaStr.endsWith(")")) {
980 throw new Error("The declaration of the delta location is wrong at "
981 + cd.getSourceFileName() + ":" + n.getNumLine());
983 String locationOperand = deltaStr.substring(4, deltaStr.length() - 1);
985 nametable.get(locationOperand);
986 Descriptor d = (Descriptor) nametable.get(locationOperand);
988 if (d instanceof VarDescriptor) {
989 VarDescriptor varDescriptor = (VarDescriptor) d;
990 DeltaLocation deltaLoc = new DeltaLocation(cd, varDescriptor);
991 // td2loc.put(vd.getType(), compLoc);
992 compLoc.addLocation(deltaLoc);
993 } else if (d instanceof FieldDescriptor) {
994 throw new Error("Applying delta operation to the field " + locationOperand
995 + " is not allowed at " + cd.getSourceFileName() + ":" + n.getNumLine());
998 StringTokenizer token = new StringTokenizer(deltaStr, ",");
999 DeltaLocation deltaLoc = new DeltaLocation(cd);
1001 while (token.hasMoreTokens()) {
1002 String deltaOperand = token.nextToken();
1003 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
1004 if (deltaCD == null) {
1005 // delta operand is not defined in the location hierarchy
1006 throw new Error("Delta operand '" + deltaOperand + "' of declaration node '" + vd
1007 + "' is not defined by location hierarchies.");
1010 Location loc = new Location(deltaCD, deltaOperand);
1011 deltaLoc.addDeltaOperand(loc);
1013 compLoc.addLocation(deltaLoc);
1017 td2loc.put(vd, compLoc);
1018 addTypeLocation(vd.getType(), compLoc);
1025 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1026 VarDescriptor vd = dn.getVarDescriptor();
1027 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1030 private void checkClass(ClassDescriptor cd) {
1031 // Check to see that methods respects ss property
1032 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1033 MethodDescriptor md = (MethodDescriptor) method_it.next();
1034 checkMethodDeclaration(cd, md);
1038 private void checkDeclarationInClass(ClassDescriptor cd) {
1039 // Check to see that fields are okay
1040 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1041 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1042 checkFieldDeclaration(cd, fd);
1046 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1050 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1052 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1054 // currently enforce every field to have corresponding location
1055 if (annotationVec.size() == 0) {
1056 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1060 if (annotationVec.size() > 1) {
1061 // variable can have at most one location
1062 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1063 + " has more than one location.");
1066 AnnotationDescriptor ad = annotationVec.elementAt(0);
1068 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1070 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1071 String locationID = ad.getValue();
1072 // check if location is defined
1073 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
1074 if (lattice == null || (!lattice.containsKey(locationID))) {
1075 throw new Error("Location " + locationID
1076 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
1078 Location loc = new Location(cd, locationID);
1079 td2loc.put(fd, loc);
1080 addTypeLocation(fd.getType(), loc);
1082 } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
1084 if (ad.getValue().length() == 0) {
1085 throw new Error("Delta function of " + fd.getSymbol() + " does not have any locations: "
1086 + cd.getSymbol() + ".");
1089 CompositeLocation compLoc = new CompositeLocation(cd);
1090 DeltaLocation deltaLoc = new DeltaLocation(cd);
1092 StringTokenizer token = new StringTokenizer(ad.getValue(), ",");
1093 while (token.hasMoreTokens()) {
1094 String deltaOperand = token.nextToken();
1095 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
1096 if (deltaCD == null) {
1097 // delta operand is not defined in the location hierarchy
1098 throw new Error("Delta operand '" + deltaOperand + "' of field node '" + fd
1099 + "' is not defined by location hierarchies.");
1102 Location loc = new Location(deltaCD, deltaOperand);
1103 deltaLoc.addDeltaOperand(loc);
1105 compLoc.addLocation(deltaLoc);
1106 td2loc.put(fd, compLoc);
1107 addTypeLocation(fd.getType(), compLoc);
1114 private void addTypeLocation(TypeDescriptor type, Location loc) {
1116 type.setExtension(loc);
1120 static class CompositeLattice {
1122 public static boolean isGreaterThan(Location loc1, Location loc2, ClassDescriptor priorityCD) {
1124 // System.out.println("\nisGreaterThan=" + loc1 + " ? " + loc2);
1125 CompositeLocation compLoc1;
1126 CompositeLocation compLoc2;
1128 if (loc1 instanceof CompositeLocation) {
1129 compLoc1 = (CompositeLocation) loc1;
1131 // create a bogus composite location for a single location
1132 compLoc1 = new CompositeLocation(loc1.getClassDescriptor());
1133 compLoc1.addLocation(loc1);
1136 if (loc2 instanceof CompositeLocation) {
1137 compLoc2 = (CompositeLocation) loc2;
1139 // create a bogus composite location for a single location
1140 compLoc2 = new CompositeLocation(loc2.getClassDescriptor());
1141 compLoc2.addLocation(loc2);
1144 // comparing two composite locations
1145 // System.out.println("compare base location=" + compLoc1 + " ? " +
1148 int baseCompareResult = compareBaseLocationSet(compLoc1, compLoc2, priorityCD);
1149 if (baseCompareResult == ComparisonResult.EQUAL) {
1150 if (compareDelta(compLoc1, compLoc2) == ComparisonResult.GREATER) {
1155 } else if (baseCompareResult == ComparisonResult.GREATER) {
1163 private static int compareDelta(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1164 if (compLoc1.getNumofDelta() < compLoc2.getNumofDelta()) {
1165 return ComparisonResult.GREATER;
1167 return ComparisonResult.LESS;
1171 private static int compareBaseLocationSet(CompositeLocation compLoc1,
1172 CompositeLocation compLoc2, ClassDescriptor priorityCD) {
1174 // if compLoc1 is greater than compLoc2, return true
1175 // else return false;
1177 Map<ClassDescriptor, Location> cd2loc1 = compLoc1.getCd2Loc();
1178 Map<ClassDescriptor, Location> cd2loc2 = compLoc2.getCd2Loc();
1180 // compare first the priority loc elements
1181 Location priorityLoc1 = cd2loc1.get(priorityCD);
1182 Location priorityLoc2 = cd2loc2.get(priorityCD);
1184 assert (priorityLoc1.getClassDescriptor().equals(priorityLoc2.getClassDescriptor()));
1186 ClassDescriptor cd = priorityLoc1.getClassDescriptor();
1187 Lattice<String> locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd);
1189 if (priorityLoc1.getLocIdentifier().equals(priorityLoc2.getLocIdentifier())) {
1190 // have the same level of local hierarchy
1192 Set<String> spinSet =
1193 (Set<String>) state.getCd2LocationPropertyMap().get(new Pair(cd, "spin"));
1194 if (spinSet != null && spinSet.contains(priorityLoc1.getLocIdentifier())) {
1195 // this location can be spinning
1196 return ComparisonResult.GREATER;
1199 } else if (locationOrder.isGreaterThan(priorityLoc1.getLocIdentifier(),
1200 priorityLoc2.getLocIdentifier())) {
1201 // if priority loc of compLoc1 is higher than compLoc2
1202 // then, compLoc 1 is higher than compLoc2
1203 return ComparisonResult.GREATER;
1205 // if priority loc of compLoc1 is NOT higher than compLoc2
1206 // then, compLoc 1 is NOT higher than compLoc2
1207 return ComparisonResult.LESS;
1210 // compare base locations except priority by class descriptor
1211 Set<ClassDescriptor> keySet1 = cd2loc1.keySet();
1212 int numEqualLoc = 0;
1214 for (Iterator iterator = keySet1.iterator(); iterator.hasNext();) {
1215 ClassDescriptor cd1 = (ClassDescriptor) iterator.next();
1217 Location loc1 = cd2loc1.get(cd1);
1218 Location loc2 = cd2loc2.get(cd1);
1220 if (priorityLoc1.equals(loc1)) {
1225 // if comploc2 doesn't have corresponding location,
1226 // then we determines that comploc1 is lower than comploc 2
1227 return ComparisonResult.LESS;
1230 System.out.println("lattice comparison:" + loc1.getLocIdentifier() + " ? "
1231 + loc2.getLocIdentifier());
1232 locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd1);
1233 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1234 // have the same level of local hierarchy
1236 } else if (!locationOrder.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1237 // if one element of composite location 1 is not higher than composite
1239 // then, composite loc 1 is not higher than composite loc 2
1241 System.out.println(compLoc1 + " < " + compLoc2);
1242 return ComparisonResult.LESS;
1247 if (numEqualLoc == (compLoc1.getBaseLocationSize() - 1)) {
1248 return ComparisonResult.EQUAL;
1251 System.out.println(compLoc1 + " > " + compLoc2);
1252 return ComparisonResult.GREATER;
1255 public static CompositeLocation calculateGLB(ClassDescriptor cd,
1256 Set<CompositeLocation> inputSet, ClassDescriptor priorityCD) {
1258 CompositeLocation glbCompLoc = new CompositeLocation(cd);
1259 int maxDeltaFunction = 0;
1261 // calculate GLB of priority element first
1263 Hashtable<ClassDescriptor, Set<Location>> cd2locSet =
1264 new Hashtable<ClassDescriptor, Set<Location>>();
1266 // creating mapping from class to set of locations
1267 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1268 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1270 int numOfDelta = compLoc.getNumofDelta();
1271 if (numOfDelta > maxDeltaFunction) {
1272 maxDeltaFunction = numOfDelta;
1275 Set<Location> baseLocationSet = compLoc.getBaseLocationSet();
1276 for (Iterator iterator2 = baseLocationSet.iterator(); iterator2.hasNext();) {
1277 Location locElement = (Location) iterator2.next();
1278 ClassDescriptor locCD = locElement.getClassDescriptor();
1280 Set<Location> locSet = cd2locSet.get(locCD);
1281 if (locSet == null) {
1282 locSet = new HashSet<Location>();
1284 locSet.add(locElement);
1286 cd2locSet.put(locCD, locSet);
1291 Set<Location> locSetofClass = cd2locSet.get(priorityCD);
1292 Set<String> locIdentifierSet = new HashSet<String>();
1294 for (Iterator<Location> locIterator = locSetofClass.iterator(); locIterator.hasNext();) {
1295 Location locElement = locIterator.next();
1296 locIdentifierSet.add(locElement.getLocIdentifier());
1299 Lattice<String> locOrder = (Lattice<String>) state.getCd2LocationOrder().get(priorityCD);
1300 String glbLocIdentifer = locOrder.getGLB(locIdentifierSet);
1302 Location priorityGLB = new Location(priorityCD, glbLocIdentifer);
1304 Set<CompositeLocation> sameGLBLoc = new HashSet<CompositeLocation>();
1306 for (Iterator<CompositeLocation> iterator = inputSet.iterator(); iterator.hasNext();) {
1307 CompositeLocation inputComploc = iterator.next();
1308 Location locElement = inputComploc.getLocation(priorityCD);
1310 if (locElement.equals(priorityGLB)) {
1311 sameGLBLoc.add(inputComploc);
1314 glbCompLoc.addLocation(priorityGLB);
1315 if (sameGLBLoc.size() > 0) {
1316 // if more than one location shares the same priority GLB
1317 // need to calculate the rest of GLB loc
1319 Set<Location> glbElementSet = new HashSet<Location>();
1321 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1322 ClassDescriptor localCD = iterator.next();
1323 if (!localCD.equals(priorityCD)) {
1324 Set<Location> localLocSet = cd2locSet.get(localCD);
1325 Set<String> LocalLocIdSet = new HashSet<String>();
1327 for (Iterator<Location> locIterator = localLocSet.iterator(); locIterator.hasNext();) {
1328 Location locElement = locIterator.next();
1329 LocalLocIdSet.add(locElement.getLocIdentifier());
1332 Lattice<String> localOrder = (Lattice<String>) state.getCd2LocationOrder().get(localCD);
1333 Location localGLBLoc = new Location(localCD, localOrder.getGLB(LocalLocIdSet));
1334 glbCompLoc.addLocation(localGLBLoc);
1338 // if priority glb loc is lower than all of input loc
1339 // assign top location to the rest of loc element
1341 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1342 ClassDescriptor localCD = iterator.next();
1343 if (!localCD.equals(priorityCD)) {
1344 Location localGLBLoc = Location.createTopLocation(localCD);
1345 glbCompLoc.addLocation(localGLBLoc);
1358 class ComparisonResult {
1360 public static final int GREATER = 0;
1361 public static final int EQUAL = 1;
1362 public static final int LESS = 2;