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 {
51 static SSJavaAnalysis ssjava;
54 Hashtable<Descriptor, Location> d2loc; // mapping from 'descriptor'
56 Hashtable<String, ClassDescriptor> id2cd; // mapping from 'locID' to 'class
58 public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
61 this.toanalyze = new HashSet();
62 this.d2loc = new Hashtable<Descriptor, Location>();
66 // public void init() {
67 // id2cd = new Hashtable<String, ClassDescriptor>();
68 // Hashtable cd2lattice = state.getCd2LocationOrder();
70 // Set cdSet = cd2lattice.keySet();
71 // for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
72 // ClassDescriptor cd = (ClassDescriptor) iterator.next();
73 // Lattice<String> lattice = (Lattice<String>) cd2lattice.get(cd);
75 // Set<String> locIdSet = lattice.getKeySet();
76 // for (Iterator iterator2 = locIdSet.iterator(); iterator2.hasNext();) {
77 // String locID = (String) iterator2.next();
78 // id2cd.put(locID, cd);
84 public void flowDownCheck() {
85 SymbolTable classtable = state.getClassSymbolTable();
87 // phase 1 : checking declaration node and creating mapping of 'type
88 // desciptor' & 'location'
89 toanalyze.addAll(classtable.getValueSet());
90 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
91 while (!toanalyze.isEmpty()) {
92 Object obj = toanalyze.iterator().next();
93 ClassDescriptor cd = (ClassDescriptor) obj;
96 if (!cd.isInterface()) {
97 checkDeclarationInClass(cd);
98 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
99 MethodDescriptor md = (MethodDescriptor) method_it.next();
101 checkDeclarationInMethodBody(cd, md);
103 System.out.println("Error in " + md);
111 // post-processing for delta location
112 // for a nested delta location, assigning a concrete reference to delta
114 Set<Descriptor> tdSet = d2loc.keySet();
115 for (Iterator iterator = tdSet.iterator(); iterator.hasNext();) {
116 Descriptor td = (Descriptor) iterator.next();
117 Location loc = d2loc.get(td);
119 if (loc.getType() == Location.DELTA) {
120 // if it contains delta reference pointing to another location element
121 CompositeLocation compLoc = (CompositeLocation) loc;
123 Location locElement = compLoc.getTuple().at(0);
124 assert (locElement instanceof DeltaLocation);
126 DeltaLocation delta = (DeltaLocation) locElement;
127 Descriptor refType = delta.getRefLocationId();
128 if (refType != null) {
129 Location refLoc = d2loc.get(refType);
131 assert (refLoc instanceof CompositeLocation);
132 CompositeLocation refCompLoc = (CompositeLocation) refLoc;
134 assert (refCompLoc.getTuple().at(0) instanceof DeltaLocation);
135 DeltaLocation refDelta = (DeltaLocation) refCompLoc.getTuple().at(0);
137 delta.addDeltaOperand(refDelta);
138 // compLoc.addLocation(refDelta);
144 // phase2 : checking assignments
145 toanalyze.addAll(classtable.getValueSet());
146 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
147 while (!toanalyze.isEmpty()) {
148 Object obj = toanalyze.iterator().next();
149 ClassDescriptor cd = (ClassDescriptor) obj;
150 toanalyze.remove(cd);
153 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
154 MethodDescriptor md = (MethodDescriptor) method_it.next();
156 checkMethodBody(cd, md);
158 System.out.println("Error in " + md);
166 public Hashtable getMap() {
170 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
171 BlockNode bn = state.getMethodBody(md);
172 for (int i = 0; i < md.numParameters(); i++) {
173 // process annotations on method parameters
174 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
175 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
177 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
180 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
181 bn.getVarTable().setParent(nametable);
182 for (int i = 0; i < bn.size(); i++) {
183 BlockStatementNode bsn = bn.get(i);
184 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
188 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
189 BlockStatementNode bsn) {
191 switch (bsn.kind()) {
192 case Kind.SubBlockNode:
193 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
196 case Kind.DeclarationNode:
197 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
201 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
206 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
208 if (ln.getType() == LoopNode.FORLOOP) {
209 // check for loop case
210 ClassDescriptor cd = md.getClassDesc();
211 BlockNode bn = ln.getInitializer();
212 for (int i = 0; i < bn.size(); i++) {
213 BlockStatementNode bsn = bn.get(i);
214 checkDeclarationInBlockStatementNode(md, nametable, bsn);
219 checkDeclarationInBlockNode(md, nametable, ln.getBody());
222 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
223 BlockNode bn = state.getMethodBody(md);
224 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
227 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
230 bn.getVarTable().setParent(nametable);
231 // it will return the lowest location in the block node
232 CompositeLocation lowestLoc = null;
233 for (int i = 0; i < bn.size(); i++) {
234 BlockStatementNode bsn = bn.get(i);
235 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
237 if (lowestLoc == null) {
240 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc, md.getClassDesc())) {
248 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
249 SymbolTable nametable, BlockStatementNode bsn) {
251 CompositeLocation compLoc = null;
252 switch (bsn.kind()) {
253 case Kind.BlockExpressionNode:
254 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
257 case Kind.DeclarationNode:
258 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
261 case Kind.IfStatementNode:
262 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
266 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
269 case Kind.ReturnNode:
270 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
273 case Kind.SubBlockNode:
274 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
277 // case Kind.ContinueBreakNode:
278 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
285 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
287 ClassDescriptor cd = md.getClassDesc();
288 CompositeLocation loc = new CompositeLocation(cd);
290 ExpressionNode returnExp = rn.getReturnExpression();
292 if (rn == null || hasOnlyLiteralValue(returnExp)) {
293 // when it returns literal value, return node has "bottom" location no
294 // matter what it is going to return.
295 loc.addLocation(Location.createBottomLocation(cd));
297 loc = checkLocationFromExpressionNode(md, nametable, returnExp, loc);
302 private boolean hasOnlyLiteralValue(ExpressionNode returnExp) {
303 if (returnExp.kind() == Kind.LiteralNode) {
310 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
313 ClassDescriptor cd = md.getClassDesc();
314 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
316 CompositeLocation condLoc =
317 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation(
319 addTypeLocation(ln.getCondition().getType(), (condLoc));
321 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
323 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc, cd)) {
324 // loop condition should be higher than loop body
326 "The location of the while-condition statement is lower than the loop body at "
327 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
333 // check for loop case
334 BlockNode bn = ln.getInitializer();
335 bn.getVarTable().setParent(nametable);
337 // calculate glb location of condition and update statements
338 CompositeLocation condLoc =
339 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
340 new CompositeLocation(cd));
341 addTypeLocation(ln.getCondition().getType(), condLoc);
343 CompositeLocation updateLoc =
344 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
346 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
347 glbInputSet.add(condLoc);
348 glbInputSet.add(updateLoc);
350 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
352 // check location of 'forloop' body
353 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
355 if (blockLoc == null) {
356 // when there is no statement in the loop body
357 return glbLocOfForLoopCond;
360 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc, cd)) {
362 "The location of the for-condition statement is lower than the for-loop body at "
363 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
370 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
371 SymbolTable nametable, SubBlockNode sbn) {
372 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
376 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
377 SymbolTable nametable, IfStatementNode isn) {
379 ClassDescriptor localCD = md.getClassDesc();
380 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
382 CompositeLocation condLoc =
383 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(
385 addTypeLocation(isn.getCondition().getType(), condLoc);
386 glbInputSet.add(condLoc);
388 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
389 glbInputSet.add(locTrueBlock);
391 // here, the location of conditional block should be higher than the
392 // location of true/false blocks
394 if (!CompositeLattice.isGreaterThan(condLoc, locTrueBlock, localCD)) {
397 "The location of the if-condition statement is lower than the conditional block at "
398 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
401 if (isn.getFalseBlock() != null) {
402 CompositeLocation locFalseBlock =
403 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
404 glbInputSet.add(locFalseBlock);
406 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock, localCD)) {
409 "The location of the if-condition statement is lower than the conditional block at "
410 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
415 // return GLB location of condition, true, and false block
416 CompositeLocation glbLoc = CompositeLattice.calculateGLB(localCD, glbInputSet, localCD);
421 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
422 SymbolTable nametable, DeclarationNode dn) {
423 VarDescriptor vd = dn.getVarDescriptor();
425 Location destLoc = d2loc.get(vd);
427 ClassDescriptor localCD = md.getClassDesc();
428 if (dn.getExpression() != null) {
429 CompositeLocation expressionLoc =
430 checkLocationFromExpressionNode(md, nametable, dn.getExpression(), new CompositeLocation(
432 addTypeLocation(dn.getExpression().getType(), expressionLoc);
434 if (expressionLoc != null) {
435 // checking location order
436 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc, localCD)) {
437 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
438 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
439 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
442 return expressionLoc;
446 if (destLoc instanceof Location) {
447 CompositeLocation comp = new CompositeLocation(localCD);
448 comp.addLocation(destLoc);
451 return (CompositeLocation) destLoc;
458 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
460 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
463 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
464 SymbolTable nametable, BlockExpressionNode ben) {
465 CompositeLocation compLoc =
466 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
467 addTypeLocation(ben.getExpression().getType(), compLoc);
471 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
472 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
474 CompositeLocation compLoc = null;
477 case Kind.AssignmentNode:
478 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
481 case Kind.FieldAccessNode:
482 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
486 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
490 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
493 case Kind.CreateObjectNode:
494 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
497 case Kind.ArrayAccessNode:
498 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
501 case Kind.LiteralNode:
502 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
505 case Kind.MethodInvokeNode:
506 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en);
509 case Kind.TertiaryNode:
510 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
514 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
517 // case Kind.InstanceOfNode:
518 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
521 // case Kind.ArrayInitializerNode:
522 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
526 // case Kind.ClassTypeNode:
527 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
530 // case Kind.OffsetNode:
531 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
539 addTypeLocation(en.getType(), compLoc);
544 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
547 ExpressionNode en = cn.getExpression();
548 return checkLocationFromExpressionNode(md, nametable, en,
549 new CompositeLocation(md.getClassDesc()));
553 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
554 SymbolTable nametable, TertiaryNode tn) {
555 ClassDescriptor cd = md.getClassDesc();
557 CompositeLocation condLoc =
558 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(cd));
559 addTypeLocation(tn.getCond().getType(), condLoc);
560 CompositeLocation trueLoc =
561 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(cd));
562 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
563 CompositeLocation falseLoc =
564 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(cd));
565 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
567 // check if condLoc is higher than trueLoc & falseLoc
568 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc, cd)) {
570 "The location of the condition expression is lower than the true expression at "
571 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
574 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc, cd)) {
576 "The location of the condition expression is lower than the true expression at "
577 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
580 // then, return glb of trueLoc & falseLoc
581 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
582 glbInputSet.add(trueLoc);
583 glbInputSet.add(falseLoc);
585 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
588 private CompositeLocation convertCompositeLocation(Location l, ClassDescriptor c) {
589 if (l instanceof CompositeLocation) {
590 return (CompositeLocation) l;
592 CompositeLocation returnLoc = new CompositeLocation(c);
593 returnLoc.addLocation(l);
598 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
599 SymbolTable nametable, MethodInvokeNode min) {
601 ClassDescriptor cd = md.getClassDesc();
603 CompositeLocation baseLoc = null;
604 if (min.getBaseName() != null) {
605 if (nametable.contains(min.getBaseName().getSymbol())) {
606 Location loc = d2loc.get(nametable.get(min.getBaseName().getSymbol()));
608 baseLoc = convertCompositeLocation(loc, cd);
613 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
614 for (int i = 0; i < min.numArgs(); i++) {
615 ExpressionNode en = min.getArg(i);
616 CompositeLocation callerArg =
617 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
618 inputGLBSet.add(callerArg);
621 // ex) base.method(arg1,arg2,arg3) -> the location of base should be lower
623 // GLB(arg1,arg2,arg3)
624 CompositeLocation argGLBLoc = null;
625 if (inputGLBSet.size() > 0) {
626 argGLBLoc = CompositeLattice.calculateGLB(cd, inputGLBSet, cd);
627 System.out.println("baseLoc=" + baseLoc + " argGLBLoc=" + argGLBLoc);
628 if (baseLoc != null) {
629 if (!CompositeLattice.isGreaterThan(argGLBLoc, baseLoc, cd)) {
630 throw new Error("The base location of method invocation " + min.printNode(0)
631 + " is higher than its argument's location at " + cd.getSourceFileName() + "::"
637 if (min.numArgs() > 1) {
639 // caller needs to guarantee that it passes arguments in regarding to
640 // callee's hierarchy
642 for (int i = 0; i < min.numArgs(); i++) {
643 ExpressionNode en = min.getArg(i);
644 CompositeLocation callerArg1 =
645 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
647 ClassDescriptor calleecd = min.getMethod().getClassDesc();
648 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
649 Location calleeLoc1 = d2loc.get(calleevd);
651 if (!callerArg1.getLocation(cd).isTop()) {
652 // here, check if ordering relations among caller's args respect
653 // ordering relations in-between callee's args
654 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
655 if (currentIdx != i) { // skip itself
656 ExpressionNode argExp = min.getArg(currentIdx);
657 CompositeLocation callerArg2 =
658 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation(cd));
660 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
661 Location calleeLoc2 = d2loc.get(calleevd2);
662 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2, cd);
663 boolean calleeResult =
664 CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2, calleecd);
666 if (calleeResult && !callerResult) {
667 // in callee, calleeLoc1 is higher than calleeLoc2
668 // then, caller should have same ordering relation in-bet
669 // callerLoc1 & callerLoc2
671 throw new Error("Caller doesn't respect ordering relations among method arguments:"
672 + cd.getSourceFileName() + ":" + min.getNumLine());
683 // all arguments should be higher than the location of return value
685 if (inputGLBSet.size() > 0) {
686 if (baseLoc != null) {
687 inputGLBSet.add(baseLoc);
688 return CompositeLattice.calculateGLB(cd, inputGLBSet, cd);
693 // if there are no arguments,
694 if (baseLoc != null) {
697 // method invocation from the same class
698 CompositeLocation returnLoc = new CompositeLocation(cd);
699 returnLoc.addLocation(Location.createTopLocation(cd));
705 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
706 SymbolTable nametable, ArrayAccessNode aan) {
708 // return glb location of array itself and index
710 ClassDescriptor cd = md.getClassDesc();
712 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
714 CompositeLocation arrayLoc =
715 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation(
717 addTypeLocation(aan.getExpression().getType(), arrayLoc);
718 glbInputSet.add(arrayLoc);
719 CompositeLocation indexLoc =
720 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(cd));
721 glbInputSet.add(indexLoc);
722 addTypeLocation(aan.getIndex().getType(), indexLoc);
724 CompositeLocation glbLoc = CompositeLattice.calculateGLB(cd, glbInputSet, cd);
728 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
729 SymbolTable nametable, CreateObjectNode con) {
731 ClassDescriptor cd = md.getClassDesc();
734 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
735 for (int i = 0; i < con.numArgs(); i++) {
736 ExpressionNode en = con.getArg(i);
737 CompositeLocation argLoc =
738 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(cd));
739 glbInputSet.add(argLoc);
740 addTypeLocation(en.getType(), argLoc);
743 // check array initializers
744 // if ((con.getArrayInitializer() != null)) {
745 // checkLocationFromArrayInitializerNode(md, nametable,
746 // con.getArrayInitializer());
749 if (glbInputSet.size() > 0) {
750 return CompositeLattice.calculateGLB(cd, glbInputSet, cd);
753 CompositeLocation compLoc = new CompositeLocation(cd);
754 compLoc.addLocation(Location.createTopLocation(cd));
759 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
762 ClassDescriptor cd = md.getClassDesc();
763 CompositeLocation leftLoc = new CompositeLocation(cd);
764 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
765 addTypeLocation(on.getLeft().getType(), leftLoc);
767 CompositeLocation rightLoc = new CompositeLocation(cd);
768 if (on.getRight() != null) {
769 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
770 addTypeLocation(on.getRight().getType(), rightLoc);
773 // System.out.println("checking op node=" + on.printNode(0));
774 // System.out.println("left loc=" + leftLoc + " from " +
775 // on.getLeft().getClass());
776 // System.out.println("right loc=" + rightLoc + " from " +
777 // on.getLeft().getClass());
779 Operation op = on.getOp();
781 switch (op.getOp()) {
783 case Operation.UNARYPLUS:
784 case Operation.UNARYMINUS:
785 case Operation.LOGIC_NOT:
789 case Operation.LOGIC_OR:
790 case Operation.LOGIC_AND:
792 case Operation.BIT_OR:
793 case Operation.BIT_XOR:
794 case Operation.BIT_AND:
795 case Operation.ISAVAILABLE:
796 case Operation.EQUAL:
797 case Operation.NOTEQUAL:
807 case Operation.LEFTSHIFT:
808 case Operation.RIGHTSHIFT:
809 case Operation.URIGHTSHIFT:
811 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
812 inputSet.add(leftLoc);
813 inputSet.add(rightLoc);
814 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(cd, inputSet, cd);
818 throw new Error(op.toString());
823 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
824 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
826 // literal value has the top location so that value can be flowed into any
828 Location literalLoc = Location.createTopLocation(md.getClassDesc());
829 loc.addLocation(literalLoc);
834 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
835 NameNode nn, CompositeLocation loc) {
837 NameDescriptor nd = nn.getName();
838 if (nd.getBase() != null) {
839 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
840 addTypeLocation(nn.getExpression().getType(), loc);
843 String varname = nd.toString();
844 if (varname.equals("this")) {
845 // 'this' itself is top location in the local hierarchy
846 loc.addLocation(Location.createTopLocation(md.getClassDesc()));
849 Descriptor d = (Descriptor) nametable.get(varname);
851 Location localLoc = null;
852 if (d instanceof VarDescriptor) {
853 VarDescriptor vd = (VarDescriptor) d;
854 localLoc = d2loc.get(vd);
855 } else if (d instanceof FieldDescriptor) {
856 FieldDescriptor fd = (FieldDescriptor) d;
857 localLoc = d2loc.get(fd);
859 assert (localLoc != null);
861 if (localLoc instanceof CompositeLocation) {
862 loc = (CompositeLocation) localLoc;
864 loc.addLocation(localLoc);
871 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
872 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
874 ExpressionNode left = fan.getExpression();
875 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
876 addTypeLocation(left.getType(), loc);
878 if (!left.getType().isPrimitive()) {
879 FieldDescriptor fd = fan.getField();
880 Location fieldLoc = d2loc.get(fd);
882 // in the case of "this.field", need to get rid of 'this' location from
883 // the composite location
884 if (loc.getCd2Loc().containsKey(md.getClassDesc())) {
885 loc.removieLocation(md.getClassDesc());
887 loc.addLocation(fieldLoc);
893 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
894 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
895 ClassDescriptor cd = md.getClassDesc();
897 boolean postinc = true;
898 if (an.getOperation().getBaseOp() == null
899 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
900 .getBaseOp().getOp() != Operation.POSTDEC))
903 CompositeLocation destLocation =
904 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(cd));
906 CompositeLocation srcLocation = new CompositeLocation(cd);
908 srcLocation = new CompositeLocation(cd);
909 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
910 // System.out.println("an=" + an.printNode(0) + " an.getSrc()=" +
911 // an.getSrc().getClass()
912 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
913 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, cd)) {
914 throw new Error("The value flow from " + srcLocation + " to " + destLocation
915 + " does not respect location hierarchy on the assignment " + an.printNode(0) + "at "
916 + cd.getSourceFileName() + "::" + an.getNumLine());
920 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
922 if (!ssjava.getClassLattice(cd).getSpinLocSet()
923 .contains(destLocation.getLocation(cd).getLocIdentifier())) {
924 throw new Error("Location " + destLocation + " is not allowed to have spinning values at "
925 + cd.getSourceFileName() + ":" + an.getNumLine());
929 if (an.getSrc() != null) {
930 addTypeLocation(an.getSrc().getType(), srcLocation);
932 addTypeLocation(an.getDest().getType(), destLocation);
937 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
938 SymbolTable nametable, TreeNode n) {
940 ClassDescriptor cd = md.getClassDesc();
941 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
943 // currently enforce every variable to have corresponding location
944 if (annotationVec.size() == 0) {
945 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
946 + md.getSymbol() + " of the class " + cd.getSymbol());
949 if (annotationVec.size() > 1) {
950 // variable can have at most one location
951 throw new Error(vd.getSymbol() + " has more than one location.");
954 AnnotationDescriptor ad = annotationVec.elementAt(0);
956 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
958 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
959 String locationID = ad.getValue();
960 // check if location is defined
961 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
962 if (lattice == null || (!lattice.containsKey(locationID))) {
963 throw new Error("Location " + locationID
964 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
966 Location loc = new Location(cd, locationID);
968 addTypeLocation(vd.getType(), loc);
970 } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
972 CompositeLocation compLoc = new CompositeLocation(cd);
974 if (ad.getValue().length() == 0) {
975 throw new Error("Delta function of " + vd.getSymbol() + " does not have any locations: "
976 + cd.getSymbol() + ".");
979 String deltaStr = ad.getValue();
980 if (deltaStr.startsWith("LOC(")) {
982 if (!deltaStr.endsWith(")")) {
983 throw new Error("The declaration of the delta location is wrong at "
984 + cd.getSourceFileName() + ":" + n.getNumLine());
986 String locationOperand = deltaStr.substring(4, deltaStr.length() - 1);
988 nametable.get(locationOperand);
989 Descriptor d = (Descriptor) nametable.get(locationOperand);
991 if (d instanceof VarDescriptor) {
992 VarDescriptor varDescriptor = (VarDescriptor) d;
993 DeltaLocation deltaLoc = new DeltaLocation(cd, varDescriptor);
994 // td2loc.put(vd.getType(), compLoc);
995 compLoc.addLocation(deltaLoc);
996 } else if (d instanceof FieldDescriptor) {
997 throw new Error("Applying delta operation to the field " + locationOperand
998 + " is not allowed at " + cd.getSourceFileName() + ":" + n.getNumLine());
1001 StringTokenizer token = new StringTokenizer(deltaStr, ",");
1002 DeltaLocation deltaLoc = new DeltaLocation(cd);
1004 while (token.hasMoreTokens()) {
1005 String deltaOperand = token.nextToken();
1006 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
1007 if (deltaCD == null) {
1008 // delta operand is not defined in the location hierarchy
1009 throw new Error("Delta operand '" + deltaOperand + "' of declaration node '" + vd
1010 + "' is not defined by location hierarchies.");
1013 Location loc = new Location(deltaCD, deltaOperand);
1014 deltaLoc.addDeltaOperand(loc);
1016 compLoc.addLocation(deltaLoc);
1020 d2loc.put(vd, compLoc);
1021 addTypeLocation(vd.getType(), compLoc);
1028 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1029 VarDescriptor vd = dn.getVarDescriptor();
1030 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1033 private void checkClass(ClassDescriptor cd) {
1034 // Check to see that methods respects ss property
1035 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1036 MethodDescriptor md = (MethodDescriptor) method_it.next();
1037 checkMethodDeclaration(cd, md);
1041 private void checkDeclarationInClass(ClassDescriptor cd) {
1042 // Check to see that fields are okay
1043 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1044 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1045 checkFieldDeclaration(cd, fd);
1049 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1053 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1055 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1057 // currently enforce every field to have corresponding location
1058 if (annotationVec.size() == 0) {
1059 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1063 if (annotationVec.size() > 1) {
1064 // variable can have at most one location
1065 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1066 + " has more than one location.");
1069 AnnotationDescriptor ad = annotationVec.elementAt(0);
1071 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1073 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1074 String locationID = ad.getValue();
1075 // check if location is defined
1076 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1077 if (lattice == null || (!lattice.containsKey(locationID))) {
1078 throw new Error("Location " + locationID
1079 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
1081 Location loc = new Location(cd, locationID);
1083 addTypeLocation(fd.getType(), loc);
1085 } else if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
1087 if (ad.getValue().length() == 0) {
1088 throw new Error("Delta function of " + fd.getSymbol() + " does not have any locations: "
1089 + cd.getSymbol() + ".");
1092 CompositeLocation compLoc = new CompositeLocation(cd);
1093 DeltaLocation deltaLoc = new DeltaLocation(cd);
1095 StringTokenizer token = new StringTokenizer(ad.getValue(), ",");
1096 while (token.hasMoreTokens()) {
1097 String deltaOperand = token.nextToken();
1098 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
1099 if (deltaCD == null) {
1100 // delta operand is not defined in the location hierarchy
1101 throw new Error("Delta operand '" + deltaOperand + "' of field node '" + fd
1102 + "' is not defined by location hierarchies.");
1105 Location loc = new Location(deltaCD, deltaOperand);
1106 deltaLoc.addDeltaOperand(loc);
1108 compLoc.addLocation(deltaLoc);
1109 d2loc.put(fd, compLoc);
1110 addTypeLocation(fd.getType(), compLoc);
1117 private void addTypeLocation(TypeDescriptor type, Location loc) {
1119 type.setExtension(loc);
1123 static class CompositeLattice {
1125 public static boolean isGreaterThan(Location loc1, Location loc2, ClassDescriptor priorityCD) {
1127 // System.out.println("\nisGreaterThan=" + loc1 + " ? " + loc2);
1128 CompositeLocation compLoc1;
1129 CompositeLocation compLoc2;
1131 if (loc1 instanceof CompositeLocation) {
1132 compLoc1 = (CompositeLocation) loc1;
1134 // create a bogus composite location for a single location
1135 compLoc1 = new CompositeLocation(loc1.getClassDescriptor());
1136 compLoc1.addLocation(loc1);
1139 if (loc2 instanceof CompositeLocation) {
1140 compLoc2 = (CompositeLocation) loc2;
1142 // create a bogus composite location for a single location
1143 compLoc2 = new CompositeLocation(loc2.getClassDescriptor());
1144 compLoc2.addLocation(loc2);
1147 // comparing two composite locations
1148 // System.out.println("compare base location=" + compLoc1 + " ? " +
1151 int baseCompareResult = compareBaseLocationSet(compLoc1, compLoc2, priorityCD);
1152 if (baseCompareResult == ComparisonResult.EQUAL) {
1153 if (compareDelta(compLoc1, compLoc2) == ComparisonResult.GREATER) {
1158 } else if (baseCompareResult == ComparisonResult.GREATER) {
1166 private static int compareDelta(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1167 if (compLoc1.getNumofDelta() < compLoc2.getNumofDelta()) {
1168 return ComparisonResult.GREATER;
1170 return ComparisonResult.LESS;
1174 private static int compareBaseLocationSet(CompositeLocation compLoc1,
1175 CompositeLocation compLoc2, ClassDescriptor priorityCD) {
1177 // if compLoc1 is greater than compLoc2, return true
1178 // else return false;
1180 Map<ClassDescriptor, Location> cd2loc1 = compLoc1.getCd2Loc();
1181 Map<ClassDescriptor, Location> cd2loc2 = compLoc2.getCd2Loc();
1183 // compare first the priority loc elements
1184 Location priorityLoc1 = cd2loc1.get(priorityCD);
1185 Location priorityLoc2 = cd2loc2.get(priorityCD);
1187 assert (priorityLoc1.getClassDescriptor().equals(priorityLoc2.getClassDescriptor()));
1189 ClassDescriptor cd = priorityLoc1.getClassDescriptor();
1191 SSJavaLattice<String> locationOrder = ssjava.getClassLattice(cd);
1193 if (priorityLoc1.getLocIdentifier().equals(priorityLoc2.getLocIdentifier())) {
1194 // have the same level of local hierarchy
1196 Set<String> spinSet = ssjava.getClassLattice(cd).getSpinLocSet();
1198 if (spinSet != null && spinSet.contains(priorityLoc1.getLocIdentifier())) {
1199 // this location can be spinning
1200 return ComparisonResult.GREATER;
1203 } else if (locationOrder.isGreaterThan(priorityLoc1.getLocIdentifier(),
1204 priorityLoc2.getLocIdentifier())) {
1205 // if priority loc of compLoc1 is higher than compLoc2
1206 // then, compLoc 1 is higher than compLoc2
1207 return ComparisonResult.GREATER;
1209 // if priority loc of compLoc1 is NOT higher than compLoc2
1210 // then, compLoc 1 is NOT higher than compLoc2
1211 return ComparisonResult.LESS;
1214 // compare base locations except priority by class descriptor
1215 Set<ClassDescriptor> keySet1 = cd2loc1.keySet();
1216 int numEqualLoc = 0;
1218 for (Iterator iterator = keySet1.iterator(); iterator.hasNext();) {
1219 ClassDescriptor cd1 = (ClassDescriptor) iterator.next();
1221 Location loc1 = cd2loc1.get(cd1);
1222 Location loc2 = cd2loc2.get(cd1);
1224 if (priorityLoc1.equals(loc1)) {
1229 // if comploc2 doesn't have corresponding location,
1230 // then we determines that comploc1 is lower than comploc 2
1231 return ComparisonResult.LESS;
1234 System.out.println("lattice comparison:" + loc1.getLocIdentifier() + " ? "
1235 + loc2.getLocIdentifier());
1236 locationOrder = ssjava.getClassLattice(cd1);
1237 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1238 // have the same level of local hierarchy
1240 } else if (!locationOrder.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1241 // if one element of composite location 1 is not higher than composite
1243 // then, composite loc 1 is not higher than composite loc 2
1245 System.out.println(compLoc1 + " < " + compLoc2);
1246 return ComparisonResult.LESS;
1251 if (numEqualLoc == (compLoc1.getBaseLocationSize() - 1)) {
1252 return ComparisonResult.EQUAL;
1255 System.out.println(compLoc1 + " > " + compLoc2);
1256 return ComparisonResult.GREATER;
1259 public static CompositeLocation calculateGLB(ClassDescriptor cd,
1260 Set<CompositeLocation> inputSet, ClassDescriptor priorityCD) {
1262 CompositeLocation glbCompLoc = new CompositeLocation(cd);
1263 int maxDeltaFunction = 0;
1265 // calculate GLB of priority element first
1267 Hashtable<ClassDescriptor, Set<Location>> cd2locSet =
1268 new Hashtable<ClassDescriptor, Set<Location>>();
1270 // creating mapping from class to set of locations
1271 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1272 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1274 int numOfDelta = compLoc.getNumofDelta();
1275 if (numOfDelta > maxDeltaFunction) {
1276 maxDeltaFunction = numOfDelta;
1279 Set<Location> baseLocationSet = compLoc.getBaseLocationSet();
1280 for (Iterator iterator2 = baseLocationSet.iterator(); iterator2.hasNext();) {
1281 Location locElement = (Location) iterator2.next();
1282 ClassDescriptor locCD = locElement.getClassDescriptor();
1284 Set<Location> locSet = cd2locSet.get(locCD);
1285 if (locSet == null) {
1286 locSet = new HashSet<Location>();
1288 locSet.add(locElement);
1290 cd2locSet.put(locCD, locSet);
1295 Set<Location> locSetofClass = cd2locSet.get(priorityCD);
1296 Set<String> locIdentifierSet = new HashSet<String>();
1298 for (Iterator<Location> locIterator = locSetofClass.iterator(); locIterator.hasNext();) {
1299 Location locElement = locIterator.next();
1300 locIdentifierSet.add(locElement.getLocIdentifier());
1303 Lattice<String> locOrder = ssjava.getClassLattice(priorityCD);
1304 String glbLocIdentifer = locOrder.getGLB(locIdentifierSet);
1306 Location priorityGLB = new Location(priorityCD, glbLocIdentifer);
1308 Set<CompositeLocation> sameGLBLoc = new HashSet<CompositeLocation>();
1310 for (Iterator<CompositeLocation> iterator = inputSet.iterator(); iterator.hasNext();) {
1311 CompositeLocation inputComploc = iterator.next();
1312 Location locElement = inputComploc.getLocation(priorityCD);
1314 if (locElement.equals(priorityGLB)) {
1315 sameGLBLoc.add(inputComploc);
1318 glbCompLoc.addLocation(priorityGLB);
1319 if (sameGLBLoc.size() > 0) {
1320 // if more than one location shares the same priority GLB
1321 // need to calculate the rest of GLB loc
1323 Set<Location> glbElementSet = new HashSet<Location>();
1325 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1326 ClassDescriptor localCD = iterator.next();
1327 if (!localCD.equals(priorityCD)) {
1328 Set<Location> localLocSet = cd2locSet.get(localCD);
1329 Set<String> LocalLocIdSet = new HashSet<String>();
1331 for (Iterator<Location> locIterator = localLocSet.iterator(); locIterator.hasNext();) {
1332 Location locElement = locIterator.next();
1333 LocalLocIdSet.add(locElement.getLocIdentifier());
1336 Lattice<String> localOrder = ssjava.getClassLattice(localCD);
1337 Location localGLBLoc = new Location(localCD, localOrder.getGLB(LocalLocIdSet));
1338 glbCompLoc.addLocation(localGLBLoc);
1342 // if priority glb loc is lower than all of input loc
1343 // assign top location to the rest of loc element
1345 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
1346 ClassDescriptor localCD = iterator.next();
1347 if (!localCD.equals(priorityCD)) {
1348 Location localGLBLoc = Location.createTopLocation(localCD);
1349 glbCompLoc.addLocation(localGLBLoc);
1362 class ComparisonResult {
1364 public static final int GREATER = 0;
1365 public static final int EQUAL = 1;
1366 public static final int LESS = 2;