1 package Analysis.SSJava;
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Hashtable;
6 import java.util.Iterator;
9 import java.util.StringTokenizer;
10 import java.util.Vector;
12 import IR.AnnotationDescriptor;
13 import IR.ClassDescriptor;
15 import IR.FieldDescriptor;
16 import IR.MethodDescriptor;
17 import IR.NameDescriptor;
20 import IR.SymbolTable;
21 import IR.TypeDescriptor;
22 import IR.VarDescriptor;
23 import IR.Tree.ArrayAccessNode;
24 import IR.Tree.AssignmentNode;
25 import IR.Tree.BlockExpressionNode;
26 import IR.Tree.BlockNode;
27 import IR.Tree.BlockStatementNode;
28 import IR.Tree.CastNode;
29 import IR.Tree.CreateObjectNode;
30 import IR.Tree.DeclarationNode;
31 import IR.Tree.ExpressionNode;
32 import IR.Tree.FieldAccessNode;
33 import IR.Tree.IfStatementNode;
35 import IR.Tree.LiteralNode;
36 import IR.Tree.LoopNode;
37 import IR.Tree.MethodInvokeNode;
38 import IR.Tree.NameNode;
39 import IR.Tree.OpNode;
40 import IR.Tree.ReturnNode;
41 import IR.Tree.SubBlockNode;
42 import IR.Tree.TertiaryNode;
43 import IR.Tree.TreeNode;
45 public class FlowDownCheck {
48 static SSJavaAnalysis ssjava;
52 // mapping from 'descriptor' to 'composite location'
53 Hashtable<Descriptor, CompositeLocation> d2loc;
55 // mapping from 'locID' to 'class descriptor'
56 Hashtable<String, ClassDescriptor> fieldLocName2cd;
58 public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
61 this.toanalyze = new HashSet();
62 this.d2loc = new Hashtable<Descriptor, CompositeLocation>();
63 this.fieldLocName2cd = new Hashtable<String, ClassDescriptor>();
69 // construct mapping from the location name to the class descriptor
70 // assume that the location name is unique through the whole program
72 Set<ClassDescriptor> cdSet = ssjava.getCd2lattice().keySet();
73 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
74 ClassDescriptor cd = (ClassDescriptor) iterator.next();
75 SSJavaLattice<String> lattice = ssjava.getCd2lattice().get(cd);
76 Set<String> fieldLocNameSet = lattice.getKeySet();
78 for (Iterator iterator2 = fieldLocNameSet.iterator(); iterator2.hasNext();) {
79 String fieldLocName = (String) iterator2.next();
80 fieldLocName2cd.put(fieldLocName, cd);
87 public void flowDownCheck() {
88 SymbolTable classtable = state.getClassSymbolTable();
90 // phase 1 : checking declaration node and creating mapping of 'type
91 // desciptor' & 'location'
92 toanalyze.addAll(classtable.getValueSet());
93 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
94 while (!toanalyze.isEmpty()) {
95 Object obj = toanalyze.iterator().next();
96 ClassDescriptor cd = (ClassDescriptor) obj;
99 if (!cd.isInterface()) {
100 checkDeclarationInClass(cd);
101 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
102 MethodDescriptor md = (MethodDescriptor) method_it.next();
104 checkDeclarationInMethodBody(cd, md);
106 System.out.println("Error in " + md);
114 // phase2 : checking assignments
115 toanalyze.addAll(classtable.getValueSet());
116 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
117 while (!toanalyze.isEmpty()) {
118 Object obj = toanalyze.iterator().next();
119 ClassDescriptor cd = (ClassDescriptor) obj;
120 toanalyze.remove(cd);
123 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
124 MethodDescriptor md = (MethodDescriptor) method_it.next();
126 checkMethodBody(cd, md);
128 System.out.println("Error in " + md);
136 public Hashtable getMap() {
140 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
141 BlockNode bn = state.getMethodBody(md);
142 for (int i = 0; i < md.numParameters(); i++) {
143 // process annotations on method parameters
144 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
145 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
147 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
150 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
151 bn.getVarTable().setParent(nametable);
152 for (int i = 0; i < bn.size(); i++) {
153 BlockStatementNode bsn = bn.get(i);
154 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
158 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
159 BlockStatementNode bsn) {
161 switch (bsn.kind()) {
162 case Kind.SubBlockNode:
163 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
166 case Kind.DeclarationNode:
167 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
171 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
176 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
178 if (ln.getType() == LoopNode.FORLOOP) {
179 // check for loop case
180 ClassDescriptor cd = md.getClassDesc();
181 BlockNode bn = ln.getInitializer();
182 for (int i = 0; i < bn.size(); i++) {
183 BlockStatementNode bsn = bn.get(i);
184 checkDeclarationInBlockStatementNode(md, nametable, bsn);
189 checkDeclarationInBlockNode(md, nametable, ln.getBody());
192 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
193 BlockNode bn = state.getMethodBody(md);
194 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
197 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
200 bn.getVarTable().setParent(nametable);
201 // it will return the lowest location in the block node
202 CompositeLocation lowestLoc = null;
203 for (int i = 0; i < bn.size(); i++) {
204 BlockStatementNode bsn = bn.get(i);
205 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
206 if (lowestLoc == null) {
209 if (!bLoc.isEmpty()) {
210 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc)) {
219 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
220 SymbolTable nametable, BlockStatementNode bsn) {
222 CompositeLocation compLoc = null;
223 switch (bsn.kind()) {
224 case Kind.BlockExpressionNode:
225 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
228 case Kind.DeclarationNode:
229 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
232 case Kind.IfStatementNode:
233 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
237 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
240 case Kind.ReturnNode:
241 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
244 case Kind.SubBlockNode:
245 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
248 // case Kind.ContinueBreakNode:
249 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
256 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
259 ExpressionNode returnExp = rn.getReturnExpression();
261 CompositeLocation expLoc =
262 checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation());
264 // callee should have a relative ordering in-between parameters and return
265 // value, which is required by caller's constraint
266 for (int i = 0; i < md.numParameters(); i++) {
267 Descriptor calleevd = md.getParameter(i);
268 CompositeLocation calleeParamLoc = d2loc.get(calleevd);
270 // here, parameter(input value) should be higher than result(output)
271 if ((!expLoc.get(0).isTop()) && !CompositeLattice.isGreaterThan(calleeParamLoc, expLoc)) {
272 throw new Error("Callee " + md + " doesn't have the ordering relation between parameter '"
273 + calleevd + "' and its return value '" + returnExp.printNode(0) + "'.");
277 // by default, return node has "bottom" location
278 CompositeLocation loc = new CompositeLocation();
279 loc.addLocation(Location.createBottomLocation(md));
283 private boolean hasOnlyLiteralValue(ExpressionNode en) {
284 if (en.kind() == Kind.LiteralNode) {
291 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
294 ClassDescriptor cd = md.getClassDesc();
295 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
297 CompositeLocation condLoc =
298 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation());
299 addTypeLocation(ln.getCondition().getType(), (condLoc));
301 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
303 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc)) {
304 // loop condition should be higher than loop body
306 "The location of the while-condition statement is lower than the loop body at "
307 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
313 // check for loop case
314 BlockNode bn = ln.getInitializer();
315 bn.getVarTable().setParent(nametable);
317 // calculate glb location of condition and update statements
318 CompositeLocation condLoc =
319 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
320 new CompositeLocation());
321 addTypeLocation(ln.getCondition().getType(), condLoc);
323 CompositeLocation updateLoc =
324 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
326 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
327 glbInputSet.add(condLoc);
328 glbInputSet.add(updateLoc);
330 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(glbInputSet);
332 // check location of 'forloop' body
333 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
335 if (blockLoc == null) {
336 // when there is no statement in the loop body
337 return glbLocOfForLoopCond;
340 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc)) {
342 "The location of the for-condition statement is lower than the for-loop body at "
343 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
350 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
351 SymbolTable nametable, SubBlockNode sbn) {
352 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
356 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
357 SymbolTable nametable, IfStatementNode isn) {
359 ClassDescriptor localCD = md.getClassDesc();
360 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
362 CompositeLocation condLoc =
363 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation());
365 addTypeLocation(isn.getCondition().getType(), condLoc);
366 glbInputSet.add(condLoc);
368 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
369 if (locTrueBlock != null) {
370 glbInputSet.add(locTrueBlock);
371 // here, the location of conditional block should be higher than the
372 // location of true/false blocks
373 if (locTrueBlock != null && !CompositeLattice.isGreaterThan(condLoc, locTrueBlock)) {
376 "The location of the if-condition statement is lower than the conditional block at "
377 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
381 if (isn.getFalseBlock() != null) {
382 CompositeLocation locFalseBlock =
383 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
385 if (locFalseBlock != null) {
386 glbInputSet.add(locFalseBlock);
388 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock)) {
391 "The location of the if-condition statement is lower than the conditional block at "
392 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
398 // return GLB location of condition, true, and false block
399 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
404 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
405 SymbolTable nametable, DeclarationNode dn) {
407 VarDescriptor vd = dn.getVarDescriptor();
409 CompositeLocation destLoc = d2loc.get(vd);
411 if (dn.getExpression() != null) {
412 CompositeLocation expressionLoc =
413 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
414 new CompositeLocation());
415 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
417 if (expressionLoc != null) {
418 // checking location order
419 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc)) {
420 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
421 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
422 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
425 return expressionLoc;
429 // if (destLoc instanceof Location) {
430 // CompositeLocation comp = new CompositeLocation();
431 // comp.addLocation(destLoc);
434 // return (CompositeLocation) destLoc;
442 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
444 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
447 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
448 SymbolTable nametable, BlockExpressionNode ben) {
449 CompositeLocation compLoc =
450 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
451 // addTypeLocation(ben.getExpression().getType(), compLoc);
455 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
456 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
458 CompositeLocation compLoc = null;
461 case Kind.AssignmentNode:
462 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
465 case Kind.FieldAccessNode:
466 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
470 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
474 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
477 case Kind.CreateObjectNode:
478 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
481 case Kind.ArrayAccessNode:
482 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
485 case Kind.LiteralNode:
486 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
489 case Kind.MethodInvokeNode:
490 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc);
493 case Kind.TertiaryNode:
494 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
498 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
501 // case Kind.InstanceOfNode:
502 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
505 // case Kind.ArrayInitializerNode:
506 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
510 // case Kind.ClassTypeNode:
511 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
514 // case Kind.OffsetNode:
515 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
522 // addTypeLocation(en.getType(), compLoc);
527 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
530 ExpressionNode en = cn.getExpression();
531 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
535 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
536 SymbolTable nametable, TertiaryNode tn) {
537 ClassDescriptor cd = md.getClassDesc();
539 CompositeLocation condLoc =
540 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation());
541 addTypeLocation(tn.getCond().getType(), condLoc);
542 CompositeLocation trueLoc =
543 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation());
544 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
545 CompositeLocation falseLoc =
546 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation());
547 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
549 // check if condLoc is higher than trueLoc & falseLoc
550 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc)) {
552 "The location of the condition expression is lower than the true expression at "
553 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
556 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc)) {
558 "The location of the condition expression is lower than the true expression at "
559 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
562 // then, return glb of trueLoc & falseLoc
563 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
564 glbInputSet.add(trueLoc);
565 glbInputSet.add(falseLoc);
567 return CompositeLattice.calculateGLB(glbInputSet);
570 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
571 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc) {
573 checkCalleeConstraints(md, nametable, min);
575 // all we need to care about is that
576 // method output(return value) should be lower than input values(method
578 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
579 for (int i = 0; i < min.numArgs(); i++) {
580 ExpressionNode en = min.getArg(i);
581 CompositeLocation callerArg =
582 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
583 inputGLBSet.add(callerArg);
586 if (inputGLBSet.size() > 0) {
587 return CompositeLattice.calculateGLB(inputGLBSet);
589 // if there are no arguments, just return TOP location
590 CompositeLocation compLoc = new CompositeLocation();
591 compLoc.addLocation(Location.createTopLocation(md));
597 private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
598 MethodInvokeNode min) {
600 if (min.numArgs() > 1) {
601 // caller needs to guarantee that it passes arguments in regarding to
602 // callee's hierarchy
603 for (int i = 0; i < min.numArgs(); i++) {
604 ExpressionNode en = min.getArg(i);
605 CompositeLocation callerArg1 =
606 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
608 ClassDescriptor calleecd = min.getMethod().getClassDesc();
609 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
610 CompositeLocation calleeLoc1 = d2loc.get(calleevd);
612 if (!callerArg1.get(0).isTop()) {
613 // here, check if ordering relations among caller's args respect
614 // ordering relations in-between callee's args
615 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
616 if (currentIdx != i) { // skip itself
617 ExpressionNode argExp = min.getArg(currentIdx);
619 CompositeLocation callerArg2 =
620 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation());
622 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
623 CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
625 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2);
626 boolean calleeResult = CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2);
628 if (calleeResult && !callerResult) {
629 // If calleeLoc1 is higher than calleeLoc2
630 // then, caller should have same ordering relation in-bet
631 // callerLoc1 & callerLoc2
633 throw new Error("Caller doesn't respect ordering relations among method arguments:"
634 + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
648 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
649 SymbolTable nametable, ArrayAccessNode aan) {
651 // return glb location of array itself and index
653 ClassDescriptor cd = md.getClassDesc();
655 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
657 CompositeLocation arrayLoc =
658 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation());
659 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
660 glbInputSet.add(arrayLoc);
661 CompositeLocation indexLoc =
662 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation());
663 glbInputSet.add(indexLoc);
664 // addTypeLocation(aan.getIndex().getType(), indexLoc);
666 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
670 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
671 SymbolTable nametable, CreateObjectNode con) {
673 ClassDescriptor cd = md.getClassDesc();
676 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
677 for (int i = 0; i < con.numArgs(); i++) {
678 ExpressionNode en = con.getArg(i);
679 CompositeLocation argLoc =
680 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
681 glbInputSet.add(argLoc);
682 addTypeLocation(en.getType(), argLoc);
685 // check array initializers
686 // if ((con.getArrayInitializer() != null)) {
687 // checkLocationFromArrayInitializerNode(md, nametable,
688 // con.getArrayInitializer());
691 if (glbInputSet.size() > 0) {
692 return CompositeLattice.calculateGLB(glbInputSet);
695 CompositeLocation compLoc = new CompositeLocation();
696 compLoc.addLocation(Location.createTopLocation(md));
701 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
704 ClassDescriptor cd = md.getClassDesc();
705 CompositeLocation leftLoc = new CompositeLocation();
706 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
707 // addTypeLocation(on.getLeft().getType(), leftLoc);
709 CompositeLocation rightLoc = new CompositeLocation();
710 if (on.getRight() != null) {
711 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
712 // addTypeLocation(on.getRight().getType(), rightLoc);
715 // System.out.println("checking op node=" + on.printNode(0));
716 // System.out.println("left loc=" + leftLoc + " from " + on.getLeft().getClass());
717 // System.out.println("right loc=" + rightLoc + " from " + on.getRight().getClass());
719 Operation op = on.getOp();
721 switch (op.getOp()) {
723 case Operation.UNARYPLUS:
724 case Operation.UNARYMINUS:
725 case Operation.LOGIC_NOT:
729 case Operation.LOGIC_OR:
730 case Operation.LOGIC_AND:
732 case Operation.BIT_OR:
733 case Operation.BIT_XOR:
734 case Operation.BIT_AND:
735 case Operation.ISAVAILABLE:
736 case Operation.EQUAL:
737 case Operation.NOTEQUAL:
747 case Operation.LEFTSHIFT:
748 case Operation.RIGHTSHIFT:
749 case Operation.URIGHTSHIFT:
751 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
752 inputSet.add(leftLoc);
753 inputSet.add(rightLoc);
754 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
758 throw new Error(op.toString());
763 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
764 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
766 // literal value has the top location so that value can be flowed into any
768 Location literalLoc = Location.createTopLocation(md);
769 loc.addLocation(literalLoc);
774 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
775 NameNode nn, CompositeLocation loc) {
777 NameDescriptor nd = nn.getName();
778 if (nd.getBase() != null) {
780 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
781 // addTypeLocation(nn.getExpression().getType(), loc);
783 String varname = nd.toString();
785 if (varname.equals("this")) {
787 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
788 String thisLocId = methodLattice.getThisLoc();
789 if (thisLocId == null) {
790 throw new Error("The location for 'this' is not defined at "
791 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
793 Location locElement = new Location(md, thisLocId);
794 loc.addLocation(locElement);
797 Descriptor d = (Descriptor) nametable.get(varname);
799 // CompositeLocation localLoc = null;
800 if (d instanceof VarDescriptor) {
801 VarDescriptor vd = (VarDescriptor) d;
802 // localLoc = d2loc.get(vd);
803 // the type of var descriptor has a composite location!
804 loc = ((CompositeLocation) vd.getType().getExtension()).clone();
805 } else if (d instanceof FieldDescriptor) {
806 // the type of field descriptor has a location!
807 FieldDescriptor fd = (FieldDescriptor) d;
811 // if it is 'static final', the location has TOP since no one can
813 loc.addLocation(Location.createTopLocation(md));
815 // if 'static', the location has pre-assigned global loc
816 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
817 String globalLocId = localLattice.getGlobalLoc();
818 if (globalLocId == null) {
819 throw new Error("Global location element is not defined in the method " + md);
821 Location globalLoc = new Location(md, globalLocId);
823 loc.addLocation(globalLoc);
826 // the location of field access starts from this, followed by field
828 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
829 Location thisLoc = new Location(md, localLattice.getThisLoc());
830 loc.addLocation(thisLoc);
833 Location fieldLoc = (Location) fd.getType().getExtension();
834 loc.addLocation(fieldLoc);
840 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
841 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
843 ExpressionNode left = fan.getExpression();
844 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
845 // addTypeLocation(left.getType(), loc);
847 if (!left.getType().isPrimitive()) {
848 FieldDescriptor fd = fan.getField();
849 Location fieldLoc = (Location) fd.getType().getExtension();
850 loc.addLocation(fieldLoc);
856 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
857 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
859 ClassDescriptor cd = md.getClassDesc();
861 boolean postinc = true;
862 if (an.getOperation().getBaseOp() == null
863 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
864 .getBaseOp().getOp() != Operation.POSTDEC))
867 CompositeLocation destLocation =
868 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation());
870 CompositeLocation srcLocation = new CompositeLocation();
873 if (hasOnlyLiteralValue(an.getSrc())) {
874 // if source is literal value, src location is TOP. so do not need to
878 srcLocation = new CompositeLocation();
879 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
880 // System.out.println(" an= " + an.printNode(0) + " an.getSrc()=" + an.getSrc().getClass()
881 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
882 // System.out.println("srcLocation=" + srcLocation);
883 // System.out.println("dstLocation=" + destLocation);
884 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
885 throw new Error("The value flow from " + srcLocation + " to " + destLocation
886 + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
887 + cd.getSourceFileName() + "::" + an.getNumLine());
891 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
893 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
894 throw new Error("Location " + destLocation
895 + " is not allowed to have the value flow that moves within the same location at "
896 + cd.getSourceFileName() + "::" + an.getNumLine());
904 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
905 SymbolTable nametable, TreeNode n) {
907 ClassDescriptor cd = md.getClassDesc();
908 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
910 // currently enforce every variable to have corresponding location
911 if (annotationVec.size() == 0) {
912 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
913 + md.getSymbol() + " of the class " + cd.getSymbol());
916 if (annotationVec.size() > 1) { // variable can have at most one location
917 throw new Error(vd.getSymbol() + " has more than one location.");
920 AnnotationDescriptor ad = annotationVec.elementAt(0);
922 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
924 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
925 String locDec = ad.getValue(); // check if location is defined
927 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
928 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
929 d2loc.put(vd, deltaLoc);
930 addTypeLocation(vd.getType(), deltaLoc);
932 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
933 d2loc.put(vd, compLoc);
934 addTypeLocation(vd.getType(), compLoc);
942 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
945 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
948 int beginIdx = dIdx + 6;
949 locDec = locDec.substring(beginIdx, locDec.length() - 1);
950 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
953 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
954 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
959 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
961 CompositeLocation compLoc = new CompositeLocation();
963 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
964 List<String> locIdList = new ArrayList<String>();
965 while (tokenizer.hasMoreTokens()) {
966 String locId = tokenizer.nextToken();
967 locIdList.add(locId);
970 // at least,one location element needs to be here!
971 assert (locIdList.size() > 0);
973 // assume that loc with idx 0 comes from the local lattice
974 // loc with idx 1 comes from the field lattice
976 String localLocId = locIdList.get(0);
977 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
978 Location localLoc = new Location(md, localLocId);
979 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
980 throw new Error("Location " + localLocId
981 + " is not defined in the local variable lattice at "
982 + md.getClassDesc().getSourceFileName() + "::" + n.getNumLine() + ".");
984 compLoc.addLocation(localLoc);
986 for (int i = 1; i < locIdList.size(); i++) {
987 String locName = locIdList.get(i);
988 ClassDescriptor cd = fieldLocName2cd.get(locName);
990 SSJavaLattice<String> fieldLattice = CompositeLattice.getLatticeByDescriptor(cd);
992 if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
993 throw new Error("Location " + locName + " is not defined in the field lattice at "
994 + cd.getSourceFileName() + ".");
997 Location fieldLoc = new Location(cd, locName);
998 compLoc.addLocation(fieldLoc);
1005 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1006 VarDescriptor vd = dn.getVarDescriptor();
1007 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1010 private void checkClass(ClassDescriptor cd) {
1011 // Check to see that methods respects ss property
1012 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1013 MethodDescriptor md = (MethodDescriptor) method_it.next();
1014 checkMethodDeclaration(cd, md);
1018 private void checkDeclarationInClass(ClassDescriptor cd) {
1019 // Check to see that fields are okay
1020 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1021 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1022 checkFieldDeclaration(cd, fd);
1026 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1030 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1032 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1034 // currently enforce every field to have corresponding location
1035 if (annotationVec.size() == 0) {
1036 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
1040 if (annotationVec.size() > 1) {
1041 // variable can have at most one location
1042 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1043 + " has more than one location.");
1046 AnnotationDescriptor ad = annotationVec.elementAt(0);
1048 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1050 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1051 String locationID = ad.getValue();
1052 // check if location is defined
1053 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1054 if (lattice == null || (!lattice.containsKey(locationID))) {
1055 throw new Error("Location " + locationID
1056 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1057 + cd.getSourceFileName() + ".");
1059 Location loc = new Location(cd, locationID);
1060 // d2loc.put(fd, loc);
1061 addTypeLocation(fd.getType(), loc);
1068 private void addTypeLocation(TypeDescriptor type, CompositeLocation loc) {
1070 type.setExtension(loc);
1074 private void addTypeLocation(TypeDescriptor type, Location loc) {
1076 type.setExtension(loc);
1080 static class CompositeLattice {
1082 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1084 // System.out.println("isGreaterThan= " + loc1 + " " + loc2);
1086 int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1087 if (baseCompareResult == ComparisonResult.EQUAL) {
1088 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1093 } else if (baseCompareResult == ComparisonResult.GREATER) {
1101 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1103 int deltaCount1 = 0;
1104 int deltaCount2 = 0;
1105 if (dLoc1 instanceof DeltaLocation) {
1106 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1109 if (dLoc2 instanceof DeltaLocation) {
1110 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1112 if (deltaCount1 < deltaCount2) {
1113 return ComparisonResult.GREATER;
1114 } else if (deltaCount1 == deltaCount2) {
1115 return ComparisonResult.EQUAL;
1117 return ComparisonResult.LESS;
1122 private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1124 // if compLoc1 is greater than compLoc2, return true
1125 // else return false;
1127 // compare one by one in according to the order of the tuple
1129 for (int i = 0; i < compLoc1.getSize(); i++) {
1130 Location loc1 = compLoc1.get(i);
1131 if (i >= compLoc2.getSize()) {
1132 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1133 + " because they are not comparable.");
1135 Location loc2 = compLoc2.get(i);
1137 if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1138 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1139 + " because they are not comparable.");
1142 Descriptor d1 = loc1.getDescriptor();
1143 Descriptor d2 = loc2.getDescriptor();
1145 SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1146 SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1148 // check if the spin location is appeared only at the end of the
1149 // composite location
1150 if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1151 if (i != (compLoc1.getSize() - 1)) {
1152 throw new Error("The spin location " + loc1.getLocIdentifier()
1153 + " cannot be appeared in the middle of composite location.");
1157 if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1158 if (i != (compLoc2.getSize() - 1)) {
1159 throw new Error("The spin location " + loc2.getLocIdentifier()
1160 + " cannot be appeared in the middle of composite location.");
1164 if (!lattice1.equals(lattice2)) {
1165 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1166 + " because they are not comparable.");
1169 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1171 // check if the current location is the spinning location
1172 // note that the spinning location only can be appeared in the last
1173 // part of the composite location
1174 if (numOfTie == compLoc1.getSize()
1175 && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1176 return ComparisonResult.GREATER;
1179 } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1180 return ComparisonResult.GREATER;
1182 return ComparisonResult.LESS;
1187 if (numOfTie == compLoc1.getSize()) {
1189 if (numOfTie != compLoc2.getSize()) {
1190 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1191 + " because they are not comparable.");
1194 return ComparisonResult.EQUAL;
1197 return ComparisonResult.LESS;
1201 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1203 // System.out.println("Calculating GLB=" + inputSet);
1204 CompositeLocation glbCompLoc = new CompositeLocation();
1206 // calculate GLB of the first(priority) element
1207 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1208 Descriptor priorityDescriptor = null;
1210 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1211 new Hashtable<String, Set<CompositeLocation>>();
1212 // mapping from the priority loc ID to its full representation by the
1213 // composite location
1215 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1216 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1217 Location priorityLoc = compLoc.get(0);
1218 String priorityLocId = priorityLoc.getLocIdentifier();
1219 priorityLocIdentifierSet.add(priorityLocId);
1221 if (locId2CompLocSet.contains(priorityLocId)) {
1222 locId2CompLocSet.get(priorityLocId).add(compLoc);
1224 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1225 newSet.add(compLoc);
1226 locId2CompLocSet.put(priorityLocId, newSet);
1229 // check if priority location are coming from the same lattice
1230 if (priorityDescriptor == null) {
1231 priorityDescriptor = priorityLoc.getDescriptor();
1232 } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1233 throw new Error("Failed to calculate GLB of " + inputSet
1234 + " because they are from different lattices.");
1238 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1239 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1241 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1242 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1244 if (compSet == null) {
1245 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1246 // mean that the result is already lower than <x1,y1> and <x2,y2>
1247 // assign TOP to the rest of the location elements
1248 CompositeLocation inputComp = inputSet.iterator().next();
1249 for (int i = 1; i < inputComp.getSize(); i++) {
1250 glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1253 if (compSet.size() == 1) {
1254 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1255 CompositeLocation comp = compSet.iterator().next();
1256 for (int i = 1; i < comp.getSize(); i++) {
1257 glbCompLoc.addLocation(comp.get(i));
1260 // when GLB(x1,x2)==x1 and x2 : GLB case 1
1261 // if more than one location shares the same priority GLB
1262 // need to calculate the rest of GLB loc
1264 int compositeLocSize = compSet.iterator().next().getSize();
1266 Set<String> glbInputSet = new HashSet<String>();
1267 Descriptor currentD = null;
1268 for (int i = 1; i < compositeLocSize; i++) {
1269 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1270 CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1271 Location currentLoc = compositeLocation.get(i);
1272 currentD = currentLoc.getDescriptor();
1273 // making set of the current location sharing the same idx
1274 glbInputSet.add(currentLoc.getLocIdentifier());
1276 // calculate glb for the current lattice
1278 SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1279 String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1280 glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1290 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1292 SSJavaLattice<String> lattice = null;
1294 if (d instanceof ClassDescriptor) {
1295 lattice = ssjava.getCd2lattice().get(d);
1296 } else if (d instanceof MethodDescriptor) {
1297 if (ssjava.getMd2lattice().containsKey(d)) {
1298 lattice = ssjava.getMd2lattice().get(d);
1300 // use default lattice for the method
1301 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1310 class ComparisonResult {
1312 public static final int GREATER = 0;
1313 public static final int EQUAL = 1;
1314 public static final int LESS = 2;