1 package Analysis.SSJava;
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
8 import java.util.StringTokenizer;
9 import java.util.Vector;
11 import IR.AnnotationDescriptor;
12 import IR.ClassDescriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.NameDescriptor;
19 import IR.SymbolTable;
20 import IR.TypeDescriptor;
21 import IR.VarDescriptor;
22 import IR.Tree.ArrayAccessNode;
23 import IR.Tree.AssignmentNode;
24 import IR.Tree.BlockExpressionNode;
25 import IR.Tree.BlockNode;
26 import IR.Tree.BlockStatementNode;
27 import IR.Tree.CastNode;
28 import IR.Tree.CreateObjectNode;
29 import IR.Tree.DeclarationNode;
30 import IR.Tree.ExpressionNode;
31 import IR.Tree.FieldAccessNode;
32 import IR.Tree.IfStatementNode;
34 import IR.Tree.LiteralNode;
35 import IR.Tree.LoopNode;
36 import IR.Tree.MethodInvokeNode;
37 import IR.Tree.NameNode;
38 import IR.Tree.OpNode;
39 import IR.Tree.ReturnNode;
40 import IR.Tree.SubBlockNode;
41 import IR.Tree.TertiaryNode;
42 import IR.Tree.TreeNode;
45 public class FlowDownCheck {
49 Hashtable<Descriptor, Location> td2loc; // mapping from 'type descriptor'
51 Hashtable<String, ClassDescriptor> id2cd; // mapping from 'locID' to 'class
54 public FlowDownCheck(State state) {
56 this.toanalyze = new HashSet();
57 this.td2loc = new Hashtable<Descriptor, Location>();
62 id2cd = new Hashtable<String, ClassDescriptor>();
63 Hashtable cd2lattice = state.getCd2LocationOrder();
65 Set cdSet = cd2lattice.keySet();
66 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
67 ClassDescriptor cd = (ClassDescriptor) iterator.next();
68 Lattice<String> lattice = (Lattice<String>) cd2lattice.get(cd);
70 Set<String> locIdSet = lattice.getKeySet();
71 for (Iterator iterator2 = locIdSet.iterator(); iterator2.hasNext();) {
72 String locID = (String) iterator2.next();
79 public void flowDownCheck() {
80 SymbolTable classtable = state.getClassSymbolTable();
82 // phase 1 : checking declaration node and creating mapping of 'type
83 // desciptor' & 'location'
84 toanalyze.addAll(classtable.getValueSet());
85 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
86 while (!toanalyze.isEmpty()) {
87 Object obj = toanalyze.iterator().next();
88 ClassDescriptor cd = (ClassDescriptor) obj;
91 if (!cd.isInterface()) {
92 checkDeclarationInClass(cd);
93 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
94 MethodDescriptor md = (MethodDescriptor) method_it.next();
96 checkDeclarationInMethodBody(cd, md);
98 System.out.println("Error in " + md);
106 // post-processing for delta location
107 // for a nested delta location, assigning a concrete reference to delta
109 Set<Descriptor> tdSet = td2loc.keySet();
110 for (Iterator iterator = tdSet.iterator(); iterator.hasNext();) {
111 Descriptor td = (Descriptor) iterator.next();
112 Location loc = td2loc.get(td);
114 if (loc.getType() == Location.DELTA) {
115 // if it contains delta reference pointing to another location element
116 CompositeLocation compLoc = (CompositeLocation) loc;
118 Location locElement = compLoc.getTuple().at(0);
119 assert (locElement instanceof DeltaLocation);
121 DeltaLocation delta = (DeltaLocation) locElement;
122 Descriptor refType = delta.getRefLocationId();
123 if (refType != null) {
124 Location refLoc = td2loc.get(refType);
126 assert (refLoc instanceof CompositeLocation);
127 CompositeLocation refCompLoc = (CompositeLocation) refLoc;
129 assert (refCompLoc.getTuple().at(0) instanceof DeltaLocation);
130 DeltaLocation refDelta = (DeltaLocation) refCompLoc.getTuple().at(0);
132 delta.addDeltaOperand(refDelta);
133 // compLoc.addLocation(refDelta);
139 // phase2 : checking assignments
140 toanalyze.addAll(classtable.getValueSet());
141 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
142 while (!toanalyze.isEmpty()) {
143 Object obj = toanalyze.iterator().next();
144 ClassDescriptor cd = (ClassDescriptor) obj;
145 toanalyze.remove(cd);
148 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
149 MethodDescriptor md = (MethodDescriptor) method_it.next();
151 checkMethodBody(cd, md);
153 System.out.println("Error in " + md);
161 public Hashtable getMap() {
165 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
166 BlockNode bn = state.getMethodBody(md);
167 for (int i = 0; i < md.numParameters(); i++) {
168 // process annotations on method parameters
169 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
170 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
172 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
175 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
176 bn.getVarTable().setParent(nametable);
177 for (int i = 0; i < bn.size(); i++) {
178 BlockStatementNode bsn = bn.get(i);
179 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
183 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
184 BlockStatementNode bsn) {
186 switch (bsn.kind()) {
187 case Kind.SubBlockNode:
188 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
191 case Kind.DeclarationNode:
192 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
196 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
201 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
203 if (ln.getType() == LoopNode.FORLOOP) {
204 // check for loop case
205 ClassDescriptor cd = md.getClassDesc();
206 BlockNode bn = ln.getInitializer();
207 for (int i = 0; i < bn.size(); i++) {
208 BlockStatementNode bsn = bn.get(i);
209 checkDeclarationInBlockStatementNode(md, nametable, bsn);
214 checkDeclarationInBlockNode(md, nametable, ln.getBody());
217 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
218 BlockNode bn = state.getMethodBody(md);
219 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
222 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
225 bn.getVarTable().setParent(nametable);
226 // it will return the lowest location in the block node
227 CompositeLocation lowestLoc = null;
228 for (int i = 0; i < bn.size(); i++) {
229 BlockStatementNode bsn = bn.get(i);
230 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
232 if (lowestLoc == null) {
235 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc, md.getClassDesc())) {
243 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
244 SymbolTable nametable, BlockStatementNode bsn) {
246 CompositeLocation compLoc = null;
247 switch (bsn.kind()) {
248 case Kind.BlockExpressionNode:
249 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
252 case Kind.DeclarationNode:
253 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
256 case Kind.IfStatementNode:
257 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
261 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
264 case Kind.ReturnNode:
265 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
268 case Kind.SubBlockNode:
269 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
272 // case Kind.ContinueBreakNode:
273 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
280 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
282 ClassDescriptor cd = md.getClassDesc();
283 CompositeLocation loc = new CompositeLocation(cd);
284 // TODO: by default, return node has "bottom" location no matter what it is
286 // but definitely I need to consider it more!!!
287 loc.addLocation(Location.createBottomLocation(cd));
289 * ExpressionNode returnExp = rn.getReturnExpression(); if
290 * (rn.getReturnExpression() != null) { loc =
291 * checkLocationFromExpressionNode(md, nametable, returnExp, loc); }
293 // System.out.println(rn.printNode(0) + " has loc=" + loc);
297 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
300 ClassDescriptor cd = md.getClassDesc();
301 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
303 CompositeLocation condLoc =
304 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation(
306 addTypeLocation(ln.getCondition().getType(), (condLoc));
308 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
310 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc, cd)) {
311 // loop condition should be higher than loop body
313 "The location of the while-condition statement is lower than the loop body at "
314 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
320 // check for loop case
321 BlockNode bn = ln.getInitializer();
322 bn.getVarTable().setParent(nametable);
324 // calculate glb location of condition and update statements
325 CompositeLocation condLoc =
326 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
327 new CompositeLocation(cd));
328 addTypeLocation(ln.getCondition().getType(), condLoc);
330 CompositeLocation updateLoc =
331 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
333 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
334 glbInputSet.add(condLoc);
335 glbInputSet.add(updateLoc);
337 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
339 // check location of 'forloop' body
340 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
342 if (blockLoc == null) {
343 // when there is no statement in the loop body
344 return glbLocOfForLoopCond;
347 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc, cd)) {
349 "The location of the for-condition statement is lower than the for-loop body at "
350 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
357 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
358 SymbolTable nametable, SubBlockNode sbn) {
359 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
363 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
364 SymbolTable nametable, IfStatementNode isn) {
366 ClassDescriptor localCD = md.getClassDesc();
367 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
369 CompositeLocation condLoc =
370 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(
372 addTypeLocation(isn.getCondition().getType(), condLoc);
373 glbInputSet.add(condLoc);
375 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
376 glbInputSet.add(locTrueBlock);
378 // here, the location of conditional block should be higher than the
379 // location of true/false blocks
381 if (!CompositeLattice.isGreaterThan(condLoc, locTrueBlock, localCD)) {
384 "The location of the if-condition statement is lower than the conditional block at "
385 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
388 if (isn.getFalseBlock() != null) {
389 CompositeLocation locFalseBlock =
390 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
391 glbInputSet.add(locFalseBlock);
393 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock, localCD)) {
396 "The location of the if-condition statement is lower than the conditional block at "
397 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
402 // return GLB location of condition, true, and false block
403 CompositeLocation glbLoc = CompositeLattice.calculateGLB(localCD, glbInputSet, localCD);
408 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
409 SymbolTable nametable, DeclarationNode dn) {
410 VarDescriptor vd = dn.getVarDescriptor();
412 Location destLoc = td2loc.get(vd);
414 ClassDescriptor localCD = md.getClassDesc();
415 if (dn.getExpression() != null) {
416 CompositeLocation expressionLoc =
417 checkLocationFromExpressionNode(md, nametable, dn.getExpression(), new CompositeLocation(
419 addTypeLocation(dn.getExpression().getType(), expressionLoc);
421 if (expressionLoc != null) {
422 // checking location order
423 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc, localCD)) {
424 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
425 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
426 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
429 return expressionLoc;
433 if (destLoc instanceof Location) {
434 CompositeLocation comp = new CompositeLocation(localCD);
435 comp.addLocation(destLoc);
438 return (CompositeLocation) destLoc;
445 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
447 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
450 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
451 SymbolTable nametable, BlockExpressionNode ben) {
452 CompositeLocation compLoc =
453 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
454 addTypeLocation(ben.getExpression().getType(), compLoc);
458 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
459 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
461 CompositeLocation compLoc = null;
464 case Kind.AssignmentNode:
465 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
468 case Kind.FieldAccessNode:
469 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
473 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
477 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
480 case Kind.CreateObjectNode:
481 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
484 case Kind.ArrayAccessNode:
485 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
488 case Kind.LiteralNode:
489 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
492 case Kind.MethodInvokeNode:
493 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en);
496 case Kind.TertiaryNode:
497 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
501 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
504 // case Kind.InstanceOfNode:
505 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
508 // case Kind.ArrayInitializerNode:
509 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
513 // case Kind.ClassTypeNode:
514 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
517 // case Kind.OffsetNode:
518 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
526 addTypeLocation(en.getType(), compLoc);
531 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
534 ExpressionNode en = cn.getExpression();
535 return checkLocationFromExpressionNode(md, nametable, en,
536 new CompositeLocation(md.getClassDesc()));
540 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
541 SymbolTable nametable, TertiaryNode tn) {
542 ClassDescriptor cd = md.getClassDesc();
544 CompositeLocation condLoc =
545 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(cd));
546 addTypeLocation(tn.getCond().getType(), condLoc);
547 CompositeLocation trueLoc =
548 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(cd));
549 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
550 CompositeLocation falseLoc =
551 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(cd));
552 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
554 // check if condLoc is higher than trueLoc & falseLoc
555 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc, cd)) {
557 "The location of the condition expression is lower than the true expression at "
558 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
561 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc, cd)) {
563 "The location of the condition expression is lower than the true expression at "
564 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
567 // then, return glb of trueLoc & falseLoc
568 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
569 glbInputSet.add(trueLoc);
570 glbInputSet.add(falseLoc);
572 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
575 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
576 SymbolTable nametable, MethodInvokeNode min) {
578 ClassDescriptor cd = md.getClassDesc();
580 if (min.numArgs() > 1) {
582 // caller needs to guarantee that it passes arguments in regarding to
583 // callee's hierarchy
585 for (int i = 0; i < min.numArgs(); i++) {
586 ExpressionNode en = min.getArg(i);
587 CompositeLocation callerArg1 =
588 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
590 ClassDescriptor calleecd = min.getMethod().getClassDesc();
591 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
592 Location calleeLoc1 = td2loc.get(calleevd);
594 if (!callerArg1.getLocation(cd).isTop()) {
595 // here, check if ordering relations among caller's args respect
596 // ordering relations in-between callee's args
597 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
598 if (currentIdx != i) { // skip itself
599 ExpressionNode argExp = min.getArg(currentIdx);
600 CompositeLocation callerArg2 =
601 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation(cd));
603 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
604 Location calleeLoc2 = td2loc.get(calleevd2);
605 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2, cd);
606 boolean calleeResult =
607 CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2, calleecd);
609 if (calleeResult && !callerResult) {
610 // in callee, calleeLoc1 is higher than calleeLoc2
611 // then, caller should have same ordering relation in-bet
612 // callerLoc1 & callerLoc2
614 throw new Error("Caller doesn't respect ordering relations among method arguments:"
615 + cd.getSourceFileName() + ":" + min.getNumLine());
626 // all arguments should be higher than the location of return value
628 // first, calculate glb of arguments
629 Set<CompositeLocation> argLocSet = new HashSet<CompositeLocation>();
630 for (int i = 0; i < min.numArgs(); i++) {
631 ExpressionNode en = min.getArg(i);
632 CompositeLocation argLoc =
633 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
634 addTypeLocation(en.getType(), argLoc);
635 argLocSet.add(argLoc);
638 if (argLocSet.size() > 0) {
639 CompositeLocation argGLBLoc = CompositeLattice.calculateGLB(cd, argLocSet, cd);
642 // if there are no arguments,
643 CompositeLocation returnLoc = new CompositeLocation(cd);
644 returnLoc.addLocation(Location.createTopLocation(cd));
649 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
650 SymbolTable nametable, ArrayAccessNode aan) {
652 // return glb location of array itself and index
654 ClassDescriptor cd = md.getClassDesc();
656 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
658 CompositeLocation arrayLoc =
659 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation(
661 addTypeLocation(aan.getExpression().getType(), arrayLoc);
662 glbInputSet.add(arrayLoc);
663 CompositeLocation indexLoc =
664 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(cd));
665 glbInputSet.add(indexLoc);
666 addTypeLocation(aan.getIndex().getType(), indexLoc);
668 CompositeLocation glbLoc = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
672 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
673 SymbolTable nametable, CreateObjectNode con) {
675 ClassDescriptor cd = md.getClassDesc();
678 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
679 for (int i = 0; i < con.numArgs(); i++) {
680 ExpressionNode en = con.getArg(i);
681 CompositeLocation argLoc =
682 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
683 glbInputSet.add(argLoc);
684 addTypeLocation(en.getType(), argLoc);
687 // check array initializers
688 // if ((con.getArrayInitializer() != null)) {
689 // checkLocationFromArrayInitializerNode(md, nametable,
690 // con.getArrayInitializer());
693 if (glbInputSet.size() > 0) {
694 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
697 CompositeLocation compLoc = new CompositeLocation(cd);
698 compLoc.addLocation(Location.createTopLocation(cd));
703 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
706 ClassDescriptor cd = md.getClassDesc();
707 CompositeLocation leftLoc = new CompositeLocation(cd);
708 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
709 addTypeLocation(on.getLeft().getType(), leftLoc);
711 CompositeLocation rightLoc = new CompositeLocation(cd);
712 if (on.getRight() != null) {
713 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
714 addTypeLocation(on.getRight().getType(), rightLoc);
717 // System.out.println("checking op node=" + on.printNode(0));
718 // System.out.println("left loc=" + leftLoc + " from " +
719 // on.getLeft().getClass());
720 // System.out.println("right loc=" + rightLoc + " from " +
721 // on.getLeft().getClass());
723 Operation op = on.getOp();
725 switch (op.getOp()) {
727 case Operation.UNARYPLUS:
728 case Operation.UNARYMINUS:
729 case Operation.LOGIC_NOT:
733 case Operation.LOGIC_OR:
734 case Operation.LOGIC_AND:
736 case Operation.BIT_OR:
737 case Operation.BIT_XOR:
738 case Operation.BIT_AND:
739 case Operation.ISAVAILABLE:
740 case Operation.EQUAL:
741 case Operation.NOTEQUAL:
751 case Operation.LEFTSHIFT:
752 case Operation.RIGHTSHIFT:
753 case Operation.URIGHTSHIFT:
755 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
756 inputSet.add(leftLoc);
757 inputSet.add(rightLoc);
758 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(cd, inputSet, cd);
762 throw new Error(op.toString());
767 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
768 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
770 // literal value has the top location so that value can be flowed into any
772 Location literalLoc = Location.createTopLocation(md.getClassDesc());
773 loc.addLocation(literalLoc);
778 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
779 NameNode nn, CompositeLocation loc) {
781 NameDescriptor nd = nn.getName();
782 if (nd.getBase() != null) {
783 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
784 addTypeLocation(nn.getExpression().getType(), loc);
787 String varname = nd.toString();
788 if (varname.equals("this")) {
789 // 'this' itself is top location in the local hierarchy
790 loc.addLocation(Location.createTopLocation(md.getClassDesc()));
793 Descriptor d = (Descriptor) nametable.get(varname);
795 Location localLoc = null;
796 if (d instanceof VarDescriptor) {
797 VarDescriptor vd = (VarDescriptor) d;
798 localLoc = td2loc.get(vd);
799 } else if (d instanceof FieldDescriptor) {
800 FieldDescriptor fd = (FieldDescriptor) d;
801 localLoc = td2loc.get(fd);
803 assert (localLoc != null);
805 if (localLoc instanceof CompositeLocation) {
806 loc = (CompositeLocation) localLoc;
808 loc.addLocation(localLoc);
815 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
816 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
818 ExpressionNode left = fan.getExpression();
819 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
820 addTypeLocation(left.getType(), loc);
822 if (!left.getType().isPrimitive()) {
823 FieldDescriptor fd = fan.getField();
824 Location fieldLoc = td2loc.get(fd);
826 // in the case of "this.field", need to get rid of 'this' location from
827 // the composite location
828 if (loc.getCd2Loc().containsKey(md.getClassDesc())) {
829 loc.removieLocation(md.getClassDesc());
831 loc.addLocation(fieldLoc);
837 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
838 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
839 ClassDescriptor cd = md.getClassDesc();
841 boolean postinc = true;
842 if (an.getOperation().getBaseOp() == null
843 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
844 .getBaseOp().getOp() != Operation.POSTDEC))
847 CompositeLocation destLocation =
848 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(cd));
850 CompositeLocation srcLocation = new CompositeLocation(cd);
852 srcLocation = new CompositeLocation(cd);
853 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
854 // System.out.println("an=" + an.printNode(0) + " an.getSrc()=" +
855 // an.getSrc().getClass()
856 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
857 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, cd)) {
858 throw new Error("The value flow from " + srcLocation + " to " + destLocation
859 + " does not respect location hierarchy on the assignment " + an.printNode(0) + "at "
860 + cd.getSourceFileName() + "::" + an.getNumLine());
864 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
866 if (!((Set<String>) state.getCd2LocationPropertyMap().get(cd)).contains(destLocation
867 .getLocation(cd).getLocIdentifier())) {
868 throw new Error("Location " + destLocation + " is not allowed to have spinning values at "
869 + cd.getSourceFileName() + ":" + an.getNumLine());
873 if (an.getSrc() != null) {
874 addTypeLocation(an.getSrc().getType(), srcLocation);
876 addTypeLocation(an.getDest().getType(), destLocation);
881 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
882 SymbolTable nametable, TreeNode n) {
884 ClassDescriptor cd = md.getClassDesc();
885 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
887 // currently enforce every variable to have corresponding location
888 if (annotationVec.size() == 0) {
889 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
890 + md.getSymbol() + " of the class " + cd.getSymbol());
893 if (annotationVec.size() > 1) {
894 // variable can have at most one location
895 throw new Error(vd.getSymbol() + " has more than one location.");
898 AnnotationDescriptor ad = annotationVec.elementAt(0);
900 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
902 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
903 String locationID = ad.getValue();
904 // check if location is defined
905 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
906 if (lattice == null || (!lattice.containsKey(locationID))) {
907 throw new Error("Location " + locationID
908 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
910 Location loc = new Location(cd, locationID);
912 addTypeLocation(vd.getType(), loc);
914 } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
916 CompositeLocation compLoc = new CompositeLocation(cd);
918 if (ad.getValue().length() == 0) {
919 throw new Error("Delta function of " + vd.getSymbol() + " does not have any locations: "
920 + cd.getSymbol() + ".");
923 String deltaStr = ad.getValue();
924 if (deltaStr.startsWith("LOC(")) {
926 if (!deltaStr.endsWith(")")) {
927 throw new Error("The declaration of the delta location is wrong at "
928 + cd.getSourceFileName() + ":" + n.getNumLine());
930 String locationOperand = deltaStr.substring(4, deltaStr.length() - 1);
932 nametable.get(locationOperand);
933 Descriptor d = (Descriptor) nametable.get(locationOperand);
935 if (d instanceof VarDescriptor) {
936 VarDescriptor varDescriptor = (VarDescriptor) d;
937 DeltaLocation deltaLoc = new DeltaLocation(cd, varDescriptor);
938 // td2loc.put(vd.getType(), compLoc);
939 compLoc.addLocation(deltaLoc);
940 } else if (d instanceof FieldDescriptor) {
941 throw new Error("Applying delta operation to the field " + locationOperand
942 + " is not allowed at " + cd.getSourceFileName() + ":" + n.getNumLine());
945 StringTokenizer token = new StringTokenizer(deltaStr, ",");
946 DeltaLocation deltaLoc = new DeltaLocation(cd);
948 while (token.hasMoreTokens()) {
949 String deltaOperand = token.nextToken();
950 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
951 if (deltaCD == null) {
952 // delta operand is not defined in the location hierarchy
953 throw new Error("Delta operand '" + deltaOperand + "' of declaration node '" + vd
954 + "' is not defined by location hierarchies.");
957 Location loc = new Location(deltaCD, deltaOperand);
958 deltaLoc.addDeltaOperand(loc);
960 compLoc.addLocation(deltaLoc);
964 td2loc.put(vd, compLoc);
965 addTypeLocation(vd.getType(), compLoc);
972 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
973 VarDescriptor vd = dn.getVarDescriptor();
974 assignLocationOfVarDescriptor(vd, md, nametable, dn);
977 private void checkClass(ClassDescriptor cd) {
978 // Check to see that methods respects ss property
979 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
980 MethodDescriptor md = (MethodDescriptor) method_it.next();
981 checkMethodDeclaration(cd, md);
985 private void checkDeclarationInClass(ClassDescriptor cd) {
986 // Check to see that fields are okay
987 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
988 FieldDescriptor fd = (FieldDescriptor) field_it.next();
989 checkFieldDeclaration(cd, fd);
993 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
997 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
999 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1001 // currently enforce every field to have corresponding location
1002 if (annotationVec.size() == 0) {
1003 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1007 if (annotationVec.size() > 1) {
1008 // variable can have at most one location
1009 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1010 + " has more than one location.");
1013 AnnotationDescriptor ad = annotationVec.elementAt(0);
1015 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1017 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1018 String locationID = ad.getValue();
1019 // check if location is defined
1020 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
1021 if (lattice == null || (!lattice.containsKey(locationID))) {
1022 throw new Error("Location " + locationID
1023 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
1025 Location loc = new Location(cd, locationID);
1026 td2loc.put(fd, loc);
1027 addTypeLocation(fd.getType(), loc);
1029 } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
1031 if (ad.getValue().length() == 0) {
1032 throw new Error("Delta function of " + fd.getSymbol() + " does not have any locations: "
1033 + cd.getSymbol() + ".");
1036 CompositeLocation compLoc = new CompositeLocation(cd);
1037 DeltaLocation deltaLoc = new DeltaLocation(cd);
1039 StringTokenizer token = new StringTokenizer(ad.getValue(), ",");
1040 while (token.hasMoreTokens()) {
1041 String deltaOperand = token.nextToken();
1042 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
1043 if (deltaCD == null) {
1044 // delta operand is not defined in the location hierarchy
1045 throw new Error("Delta operand '" + deltaOperand + "' of field node '" + fd
1046 + "' is not defined by location hierarchies.");
1049 Location loc = new Location(deltaCD, deltaOperand);
1050 deltaLoc.addDeltaOperand(loc);
1052 compLoc.addLocation(deltaLoc);
1053 td2loc.put(fd, compLoc);
1054 addTypeLocation(fd.getType(), compLoc);
1062 private void addTypeLocation(TypeDescriptor type, Location loc) {
1064 type.setExtension(loc);
1068 static class CompositeLattice {
1070 public static boolean isGreaterThan(Location loc1, Location loc2, ClassDescriptor priorityCD) {
1072 // System.out.println("\nisGreaterThan=" + loc1 + " ? " + loc2);
1073 CompositeLocation compLoc1;
1074 CompositeLocation compLoc2;
1076 if (loc1 instanceof CompositeLocation) {
1077 compLoc1 = (CompositeLocation) loc1;
1079 // create a bogus composite location for a single location
1080 compLoc1 = new CompositeLocation(loc1.getClassDescriptor());
1081 compLoc1.addLocation(loc1);
1084 if (loc2 instanceof CompositeLocation) {
1085 compLoc2 = (CompositeLocation) loc2;
1087 // create a bogus composite location for a single location
1088 compLoc2 = new CompositeLocation(loc2.getClassDescriptor());
1089 compLoc2.addLocation(loc2);
1092 // comparing two composite locations
1093 // System.out.println("compare base location=" + compLoc1 + " ? " +
1096 int baseCompareResult = compareBaseLocationSet(compLoc1, compLoc2, priorityCD);
1097 if (baseCompareResult == ComparisonResult.EQUAL) {
1098 if (compareDelta(compLoc1, compLoc2) == ComparisonResult.GREATER) {
1103 } else if (baseCompareResult == ComparisonResult.GREATER) {
1111 private static int compareDelta(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1112 if (compLoc1.getNumofDelta() < compLoc2.getNumofDelta()) {
1113 return ComparisonResult.GREATER;
1115 return ComparisonResult.LESS;
1119 private static int compareBaseLocationSet(CompositeLocation compLoc1,
1120 CompositeLocation compLoc2, ClassDescriptor priorityCD) {
1122 // if compLoc1 is greater than compLoc2, return true
1123 // else return false;
1125 Map<ClassDescriptor, Location> cd2loc1 = compLoc1.getCd2Loc();
1126 Map<ClassDescriptor, Location> cd2loc2 = compLoc2.getCd2Loc();
1128 // compare first the priority loc elements
1129 Location priorityLoc1 = cd2loc1.get(priorityCD);
1130 Location priorityLoc2 = cd2loc2.get(priorityCD);
1132 assert (priorityLoc1.getClassDescriptor().equals(priorityLoc2.getClassDescriptor()));
1134 ClassDescriptor cd = priorityLoc1.getClassDescriptor();
1135 Lattice<String> locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd);
1137 if (priorityLoc1.getLocIdentifier().equals(priorityLoc2.getLocIdentifier())) {
1138 // have the same level of local hierarchy
1140 Set<String> spinSet = (Set<String>) state.getCd2LocationPropertyMap().get(cd);
1141 if (spinSet != null && spinSet.contains(priorityLoc1.getLocIdentifier())) {
1142 // this location can be spinning
1143 return ComparisonResult.GREATER;
1146 } else if (locationOrder.isGreaterThan(priorityLoc1.getLocIdentifier(),
1147 priorityLoc2.getLocIdentifier())) {
1148 // if priority loc of compLoc1 is higher than compLoc2
1149 // then, compLoc 1 is higher than compLoc2
1150 return ComparisonResult.GREATER;
1152 // if priority loc of compLoc1 is NOT higher than compLoc2
1153 // then, compLoc 1 is NOT higher than compLoc2
1154 return ComparisonResult.LESS;
1157 // compare base locations except priority by class descriptor
1158 Set<ClassDescriptor> keySet1 = cd2loc1.keySet();
1159 int numEqualLoc = 0;
1161 for (Iterator iterator = keySet1.iterator(); iterator.hasNext();) {
1162 ClassDescriptor cd1 = (ClassDescriptor) iterator.next();
1164 Location loc1 = cd2loc1.get(cd1);
1165 Location loc2 = cd2loc2.get(cd1);
1167 if (priorityLoc1.equals(loc1)) {
1172 // if comploc2 doesn't have corresponding location,
1173 // then we determines that comploc1 is lower than comploc 2
1174 return ComparisonResult.LESS;
1177 System.out.println("lattice comparison:" + loc1.getLocIdentifier() + " ? "
1178 + loc2.getLocIdentifier());
1179 locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd1);
1180 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1181 // have the same level of local hierarchy
1183 } else if (!locationOrder.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1184 // if one element of composite location 1 is not higher than composite
1186 // then, composite loc 1 is not higher than composite loc 2
1188 System.out.println(compLoc1 + " < " + compLoc2);
1189 return ComparisonResult.LESS;
1194 if (numEqualLoc == (compLoc1.getBaseLocationSize() - 1)) {
1195 return ComparisonResult.EQUAL;
1198 System.out.println(compLoc1 + " > " + compLoc2);
1199 return ComparisonResult.GREATER;
1202 public static CompositeLocation calculateGLB(ClassDescriptor cd,
1203 Set<CompositeLocation> inputSet, ClassDescriptor priorityCD) {
1205 CompositeLocation glbCompLoc = new CompositeLocation(cd);
1206 int maxDeltaFunction = 0;
1208 // calculate GLB of priority element first
1210 Hashtable<ClassDescriptor, Set<Location>> cd2locSet =
1211 new Hashtable<ClassDescriptor, Set<Location>>();
1213 // creating mapping from class to set of locations
1214 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1215 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1217 int numOfDelta = compLoc.getNumofDelta();
1218 if (numOfDelta > maxDeltaFunction) {
1219 maxDeltaFunction = numOfDelta;
1222 Set<Location> baseLocationSet = compLoc.getBaseLocationSet();
1223 for (Iterator iterator2 = baseLocationSet.iterator(); iterator2.hasNext();) {
1224 Location locElement = (Location) iterator2.next();
1225 ClassDescriptor locCD = locElement.getClassDescriptor();
1227 Set<Location> locSet = cd2locSet.get(locCD);
1228 if (locSet == null) {
1229 locSet = new HashSet<Location>();
1231 locSet.add(locElement);
1233 cd2locSet.put(locCD, locSet);
1238 Set<Location> locSetofClass = cd2locSet.get(priorityCD);
1239 Set<String> locIdentifierSet = new HashSet<String>();
1241 for (Iterator<Location> locIterator = locSetofClass.iterator(); locIterator.hasNext();) {
1242 Location locElement = locIterator.next();
1243 locIdentifierSet.add(locElement.getLocIdentifier());
1246 Lattice<String> locOrder = (Lattice<String>) state.getCd2LocationOrder().get(priorityCD);
1247 String glbLocIdentifer = locOrder.getGLB(locIdentifierSet);
1249 Location priorityGLB = new Location(priorityCD, glbLocIdentifer);
1251 Set<CompositeLocation> sameGLBLoc = new HashSet<CompositeLocation>();
1253 for (Iterator<CompositeLocation> iterator = inputSet.iterator(); iterator.hasNext();) {
1254 CompositeLocation inputComploc = iterator.next();
1255 Location locElement = inputComploc.getLocation(priorityCD);
1257 if (locElement.equals(priorityGLB)) {
1258 sameGLBLoc.add(inputComploc);
1261 glbCompLoc.addLocation(priorityGLB);
1262 if (sameGLBLoc.size() > 0) {
1263 // if more than one location shares the same priority GLB
1264 // need to calculate the rest of GLB loc
1266 Set<Location> glbElementSet = new HashSet<Location>();
1268 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1269 ClassDescriptor localCD = iterator.next();
1270 if (!localCD.equals(priorityCD)) {
1271 Set<Location> localLocSet = cd2locSet.get(localCD);
1272 Set<String> LocalLocIdSet = new HashSet<String>();
1274 for (Iterator<Location> locIterator = localLocSet.iterator(); locIterator.hasNext();) {
1275 Location locElement = locIterator.next();
1276 LocalLocIdSet.add(locElement.getLocIdentifier());
1279 Lattice<String> localOrder = (Lattice<String>) state.getCd2LocationOrder().get(localCD);
1280 Location localGLBLoc = new Location(localCD, localOrder.getGLB(LocalLocIdSet));
1281 glbCompLoc.addLocation(localGLBLoc);
1285 // if priority glb loc is lower than all of input loc
1286 // assign top location to the rest of loc element
1288 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1289 ClassDescriptor localCD = iterator.next();
1290 if (!localCD.equals(priorityCD)) {
1291 Location localGLBLoc = Location.createTopLocation(localCD);
1292 glbCompLoc.addLocation(localGLBLoc);
1305 class ComparisonResult {
1307 public static final int GREATER = 0;
1308 public static final int EQUAL = 1;
1309 public static final int LESS = 2;