1 package Analysis.SSJava;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashSet;
7 import java.util.Hashtable;
8 import java.util.Iterator;
9 import java.util.LinkedList;
10 import java.util.List;
12 import java.util.StringTokenizer;
13 import java.util.Vector;
15 import Analysis.SSJava.FlowDownCheck.ComparisonResult;
16 import Analysis.SSJava.FlowDownCheck.CompositeLattice;
17 import IR.AnnotationDescriptor;
18 import IR.ClassDescriptor;
20 import IR.FieldDescriptor;
21 import IR.MethodDescriptor;
22 import IR.NameDescriptor;
25 import IR.SymbolTable;
26 import IR.TypeDescriptor;
27 import IR.TypeExtension;
28 import IR.VarDescriptor;
29 import IR.Flat.FlatNode;
30 import IR.Tree.ArrayAccessNode;
31 import IR.Tree.AssignmentNode;
32 import IR.Tree.BlockExpressionNode;
33 import IR.Tree.BlockNode;
34 import IR.Tree.BlockStatementNode;
35 import IR.Tree.CastNode;
36 import IR.Tree.CreateObjectNode;
37 import IR.Tree.DeclarationNode;
38 import IR.Tree.ExpressionNode;
39 import IR.Tree.FieldAccessNode;
40 import IR.Tree.IfStatementNode;
42 import IR.Tree.LiteralNode;
43 import IR.Tree.LoopNode;
44 import IR.Tree.MethodInvokeNode;
45 import IR.Tree.NameNode;
46 import IR.Tree.OpNode;
47 import IR.Tree.ReturnNode;
48 import IR.Tree.SubBlockNode;
49 import IR.Tree.SwitchBlockNode;
50 import IR.Tree.SwitchStatementNode;
51 import IR.Tree.SynchronizedNode;
52 import IR.Tree.TertiaryNode;
53 import IR.Tree.TreeNode;
56 public class FlowDownCheck {
59 static SSJavaAnalysis ssjava;
61 Set<ClassDescriptor> toanalyze;
62 List<ClassDescriptor> toanalyzeList;
64 Set<MethodDescriptor> toanalyzeMethod;
65 List<MethodDescriptor> toanalyzeMethodList;
67 // mapping from 'descriptor' to 'composite location'
68 Hashtable<Descriptor, CompositeLocation> d2loc;
70 Hashtable<MethodDescriptor, CompositeLocation> md2ReturnLoc;
71 Hashtable<MethodDescriptor, ReturnLocGenerator> md2ReturnLocGen;
73 // mapping from 'locID' to 'class descriptor'
74 Hashtable<String, ClassDescriptor> fieldLocName2cd;
76 boolean deterministic = true;
78 public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
82 this.toanalyzeList = new ArrayList<ClassDescriptor>();
84 this.toanalyze = new HashSet<ClassDescriptor>();
87 this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
89 this.toanalyzeMethod = new HashSet<MethodDescriptor>();
91 this.d2loc = new Hashtable<Descriptor, CompositeLocation>();
92 this.fieldLocName2cd = new Hashtable<String, ClassDescriptor>();
93 this.md2ReturnLoc = new Hashtable<MethodDescriptor, CompositeLocation>();
94 this.md2ReturnLocGen = new Hashtable<MethodDescriptor, ReturnLocGenerator>();
99 // construct mapping from the location name to the class descriptor
100 // assume that the location name is unique through the whole program
102 Set<ClassDescriptor> cdSet = ssjava.getCd2lattice().keySet();
103 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
104 ClassDescriptor cd = (ClassDescriptor) iterator.next();
105 SSJavaLattice<String> lattice = ssjava.getCd2lattice().get(cd);
106 Set<String> fieldLocNameSet = lattice.getKeySet();
108 for (Iterator iterator2 = fieldLocNameSet.iterator(); iterator2.hasNext();) {
109 String fieldLocName = (String) iterator2.next();
110 fieldLocName2cd.put(fieldLocName, cd);
117 public boolean toAnalyzeIsEmpty() {
119 return toanalyzeList.isEmpty();
121 return toanalyze.isEmpty();
125 public ClassDescriptor toAnalyzeNext() {
127 return toanalyzeList.remove(0);
129 ClassDescriptor cd = toanalyze.iterator().next();
130 toanalyze.remove(cd);
135 public void setupToAnalyze() {
136 SymbolTable classtable = state.getClassSymbolTable();
138 toanalyzeList.clear();
139 toanalyzeList.addAll(classtable.getValueSet());
140 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
141 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
142 return o1.getClassName().compareToIgnoreCase(o2.getClassName());
147 toanalyze.addAll(classtable.getValueSet());
151 public void setupToAnalazeMethod(ClassDescriptor cd) {
153 SymbolTable methodtable = cd.getMethodTable();
155 toanalyzeMethodList.clear();
156 toanalyzeMethodList.addAll(methodtable.getValueSet());
157 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
158 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
159 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
163 toanalyzeMethod.clear();
164 toanalyzeMethod.addAll(methodtable.getValueSet());
168 public boolean toAnalyzeMethodIsEmpty() {
170 return toanalyzeMethodList.isEmpty();
172 return toanalyzeMethod.isEmpty();
176 public MethodDescriptor toAnalyzeMethodNext() {
178 return toanalyzeMethodList.remove(0);
180 MethodDescriptor md = toanalyzeMethod.iterator().next();
181 toanalyzeMethod.remove(md);
186 public void flowDownCheck() {
188 // phase 1 : checking declaration node and creating mapping of 'type
189 // desciptor' & 'location'
192 while (!toAnalyzeIsEmpty()) {
193 ClassDescriptor cd = toAnalyzeNext();
195 if (ssjava.needToBeAnnoated(cd)) {
197 ClassDescriptor superDesc = cd.getSuperDesc();
199 if (superDesc != null && (!superDesc.getSymbol().equals("Object"))) {
200 checkOrderingInheritance(superDesc, cd);
203 checkDeclarationInClass(cd);
205 setupToAnalazeMethod(cd);
206 while (!toAnalyzeMethodIsEmpty()) {
207 MethodDescriptor md = toAnalyzeMethodNext();
208 if (ssjava.needTobeAnnotated(md)) {
209 checkDeclarationInMethodBody(cd, md);
217 // phase2 : checking assignments
220 while (!toAnalyzeIsEmpty()) {
221 ClassDescriptor cd = toAnalyzeNext();
223 setupToAnalazeMethod(cd);
224 while (!toAnalyzeMethodIsEmpty()) {
225 MethodDescriptor md = toAnalyzeMethodNext();
226 if (ssjava.needTobeAnnotated(md)) {
227 if (state.SSJAVADEBUG) {
228 System.out.println("SSJAVA: Checking Flow-down Rules: " + md);
230 checkMethodBody(cd, md, null);
237 private void checkOrderingInheritance(ClassDescriptor superCd, ClassDescriptor cd) {
238 // here, we're going to check that sub class keeps same relative orderings
239 // in respect to super class
241 SSJavaLattice<String> superLattice = ssjava.getClassLattice(superCd);
242 SSJavaLattice<String> subLattice = ssjava.getClassLattice(cd);
244 if (superLattice != null) {
245 // if super class doesn't define lattice, then we don't need to check its
247 if (subLattice == null) {
248 throw new Error("If a parent class '" + superCd
249 + "' has a ordering lattice, its subclass '" + cd + "' should have one.");
252 Set<Pair<String, String>> superPairSet = superLattice.getOrderingPairSet();
253 Set<Pair<String, String>> subPairSet = subLattice.getOrderingPairSet();
255 for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
256 Pair<String, String> pair = (Pair<String, String>) iterator.next();
258 if (!subPairSet.contains(pair)) {
259 throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
260 + pair.getSecond() + " < " + pair.getFirst()
261 + "' that is defined by its superclass '" + superCd + "'.");
266 MethodLattice<String> superMethodDefaultLattice = ssjava.getMethodDefaultLattice(superCd);
267 MethodLattice<String> subMethodDefaultLattice = ssjava.getMethodDefaultLattice(cd);
269 if (superMethodDefaultLattice != null) {
270 if (subMethodDefaultLattice == null) {
271 throw new Error("When a parent class '" + superCd
272 + "' defines a default method lattice, its subclass '" + cd + "' should define one.");
275 Set<Pair<String, String>> superPairSet = superMethodDefaultLattice.getOrderingPairSet();
276 Set<Pair<String, String>> subPairSet = subMethodDefaultLattice.getOrderingPairSet();
278 for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
279 Pair<String, String> pair = (Pair<String, String>) iterator.next();
281 if (!subPairSet.contains(pair)) {
282 throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
283 + pair.getSecond() + " < " + pair.getFirst()
284 + "' that is defined by its superclass '" + superCd
285 + "' in the method default lattice.");
293 public Hashtable getMap() {
297 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
298 BlockNode bn = state.getMethodBody(md);
300 // first, check annotations on method parameters
301 List<CompositeLocation> paramList = new ArrayList<CompositeLocation>();
302 for (int i = 0; i < md.numParameters(); i++) {
303 // process annotations on method parameters
304 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
305 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), null);
306 paramList.add(d2loc.get(vd));
308 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
310 CompositeLocation returnLocComp = null;
312 boolean hasReturnLocDeclaration = false;
313 if (methodAnnotations != null) {
314 for (int i = 0; i < methodAnnotations.size(); i++) {
315 AnnotationDescriptor an = methodAnnotations.elementAt(i);
316 if (an.getMarker().equals(ssjava.RETURNLOC)) {
317 // this case, developer explicitly defines method lattice
318 String returnLocDeclaration = an.getValue();
319 returnLocComp = parseLocationDeclaration(md, null, returnLocDeclaration);
320 hasReturnLocDeclaration = true;
321 } else if (an.getMarker().equals(ssjava.THISLOC)) {
322 String thisLoc = an.getValue();
323 ssjava.getMethodLattice(md).setThisLoc(thisLoc);
328 // second, check return location annotation
329 if (!md.getReturnType().isVoid()) {
330 if (!hasReturnLocDeclaration) {
331 // if developer does not define method lattice
332 // search return location in the method default lattice
333 String rtrStr = ssjava.getMethodLattice(md).getReturnLoc();
334 if (rtrStr != null) {
335 returnLocComp = new CompositeLocation(new Location(md, rtrStr));
339 if (returnLocComp == null) {
340 throw new Error("Return location is not specified for the method " + md + " at "
341 + cd.getSourceFileName());
344 md2ReturnLoc.put(md, returnLocComp);
348 if (!md.getReturnType().isVoid()) {
349 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
350 String thisLocId = methodLattice.getThisLoc();
351 if ((!md.isStatic()) && thisLocId == null) {
352 throw new Error("Method '" + md + "' does not have the definition of 'this' location at "
353 + md.getClassDesc().getSourceFileName());
355 CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
356 paramList.add(0, thisLoc);
357 md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), md, paramList, md
358 + " of " + cd.getSourceFileName()));
361 // fourth, check declarations inside of method
363 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
367 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
368 bn.getVarTable().setParent(nametable);
369 for (int i = 0; i < bn.size(); i++) {
370 BlockStatementNode bsn = bn.get(i);
371 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
375 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
376 BlockStatementNode bsn) {
378 switch (bsn.kind()) {
379 case Kind.SubBlockNode:
380 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
383 case Kind.DeclarationNode:
384 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
388 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
391 case Kind.IfStatementNode:
392 checkDeclarationInIfStatementNode(md, nametable, (IfStatementNode) bsn);
395 case Kind.SwitchStatementNode:
396 checkDeclarationInSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
399 case Kind.SynchronizedNode:
400 checkDeclarationInSynchronizedNode(md, nametable, (SynchronizedNode) bsn);
406 private void checkDeclarationInSynchronizedNode(MethodDescriptor md, SymbolTable nametable,
407 SynchronizedNode sbn) {
408 checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
411 private void checkDeclarationInSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
412 SwitchStatementNode ssn) {
413 BlockNode sbn = ssn.getSwitchBody();
414 for (int i = 0; i < sbn.size(); i++) {
415 SwitchBlockNode node = (SwitchBlockNode) sbn.get(i);
416 checkDeclarationInBlockNode(md, nametable, node.getSwitchBlockStatement());
420 private void checkDeclarationInIfStatementNode(MethodDescriptor md, SymbolTable nametable,
421 IfStatementNode isn) {
422 checkDeclarationInBlockNode(md, nametable, isn.getTrueBlock());
423 if (isn.getFalseBlock() != null)
424 checkDeclarationInBlockNode(md, nametable, isn.getFalseBlock());
427 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
429 if (ln.getType() == LoopNode.FORLOOP) {
430 // check for loop case
431 ClassDescriptor cd = md.getClassDesc();
432 BlockNode bn = ln.getInitializer();
433 for (int i = 0; i < bn.size(); i++) {
434 BlockStatementNode bsn = bn.get(i);
435 checkDeclarationInBlockStatementNode(md, nametable, bsn);
440 checkDeclarationInBlockNode(md, nametable, ln.getBody());
443 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md,
444 CompositeLocation constraints) {
445 BlockNode bn = state.getMethodBody(md);
446 checkLocationFromBlockNode(md, md.getParameterTable(), bn, constraints);
449 private String generateErrorMessage(ClassDescriptor cd, TreeNode tn) {
451 return cd.getSourceFileName() + "::" + tn.getNumLine();
453 return cd.getSourceFileName();
458 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
459 BlockNode bn, CompositeLocation constraint) {
461 bn.getVarTable().setParent(nametable);
462 for (int i = 0; i < bn.size(); i++) {
463 BlockStatementNode bsn = bn.get(i);
464 checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn, constraint);
466 return new CompositeLocation();
470 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
471 SymbolTable nametable, BlockStatementNode bsn, CompositeLocation constraint) {
473 CompositeLocation compLoc = null;
474 switch (bsn.kind()) {
475 case Kind.BlockExpressionNode:
477 checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, constraint);
480 case Kind.DeclarationNode:
481 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn, constraint);
484 case Kind.IfStatementNode:
485 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn, constraint);
489 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn, constraint);
492 case Kind.ReturnNode:
493 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn, constraint);
496 case Kind.SubBlockNode:
497 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn, constraint);
500 case Kind.ContinueBreakNode:
501 compLoc = new CompositeLocation();
504 case Kind.SwitchStatementNode:
506 checkLocationFromSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, constraint);
512 private CompositeLocation checkLocationFromSwitchStatementNode(MethodDescriptor md,
513 SymbolTable nametable, SwitchStatementNode ssn, CompositeLocation constraint) {
515 ClassDescriptor cd = md.getClassDesc();
516 CompositeLocation condLoc =
517 checkLocationFromExpressionNode(md, nametable, ssn.getCondition(), new CompositeLocation(),
519 BlockNode sbn = ssn.getSwitchBody();
521 constraint = generateNewConstraint(constraint, condLoc);
523 for (int i = 0; i < sbn.size(); i++) {
524 checkLocationFromSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), constraint);
526 return new CompositeLocation();
529 private CompositeLocation checkLocationFromSwitchBlockNode(MethodDescriptor md,
530 SymbolTable nametable, SwitchBlockNode sbn, CompositeLocation constraint) {
532 CompositeLocation blockLoc =
533 checkLocationFromBlockNode(md, nametable, sbn.getSwitchBlockStatement(), constraint);
539 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
540 ReturnNode rn, CompositeLocation constraint) {
542 ExpressionNode returnExp = rn.getReturnExpression();
544 CompositeLocation returnValueLoc;
545 if (returnExp != null) {
547 checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation(),
550 // System.out.println("# RETURN VALUE LOC=" + returnValueLoc +
551 // " with constraint=" + constraint);
553 // TODO: do we need to check here?
554 // if this return statement is inside branch, return value has an implicit
555 // flow from conditional location
556 // if (constraint != null) {
557 // Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
558 // inputGLB.add(returnValueLoc);
559 // inputGLB.add(constraint);
561 // CompositeLattice.calculateGLB(inputGLB,
562 // generateErrorMessage(md.getClassDesc(), rn));
565 // check if return value is equal or higher than RETRUNLOC of method
566 // declaration annotation
567 CompositeLocation declaredReturnLoc = md2ReturnLoc.get(md);
570 CompositeLattice.compare(returnValueLoc, declaredReturnLoc, false,
571 generateErrorMessage(md.getClassDesc(), rn));
573 if (compareResult == ComparisonResult.LESS || compareResult == ComparisonResult.INCOMPARABLE) {
575 "Return value location is not equal or higher than the declaraed return location at "
576 + md.getClassDesc().getSourceFileName() + "::" + rn.getNumLine());
580 return new CompositeLocation();
583 private boolean hasOnlyLiteralValue(ExpressionNode en) {
584 if (en.kind() == Kind.LiteralNode) {
591 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
592 LoopNode ln, CompositeLocation constraint) {
594 ClassDescriptor cd = md.getClassDesc();
595 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
597 CompositeLocation condLoc =
598 checkLocationFromExpressionNode(md, nametable, ln.getCondition(),
599 new CompositeLocation(), constraint, false);
600 // addLocationType(ln.getCondition().getType(), (condLoc));
602 constraint = generateNewConstraint(constraint, condLoc);
603 checkLocationFromBlockNode(md, nametable, ln.getBody(), constraint);
605 return new CompositeLocation();
608 // check 'for loop' case
609 BlockNode bn = ln.getInitializer();
610 bn.getVarTable().setParent(nametable);
611 // need to check initialization node
612 // checkLocationFromBlockNode(md, bn.getVarTable(), bn, constraint);
613 for(int i=0; i<bn.size(); i++) {
614 BlockStatementNode bsn=bn.get(i);
615 checkLocationFromBlockStatementNode(md, bn.getVarTable(),bsn, constraint);
618 // calculate glb location of condition and update statements
619 CompositeLocation condLoc =
620 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
621 new CompositeLocation(), constraint, false);
622 // addLocationType(ln.getCondition().getType(), condLoc);
624 constraint = generateNewConstraint(constraint, condLoc);
626 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate(), constraint);
627 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody(), constraint);
629 return new CompositeLocation();
635 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
636 SymbolTable nametable, SubBlockNode sbn, CompositeLocation constraint) {
637 CompositeLocation compLoc =
638 checkLocationFromBlockNode(md, nametable, sbn.getBlockNode(), constraint);
642 private CompositeLocation generateNewConstraint(CompositeLocation currentCon,
643 CompositeLocation newCon) {
645 if (currentCon == null) {
648 // compute GLB of current constraint and new constraint
649 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
650 inputSet.add(currentCon);
651 inputSet.add(newCon);
652 return CompositeLattice.calculateGLB(inputSet, "");
657 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
658 SymbolTable nametable, IfStatementNode isn, CompositeLocation constraint) {
660 CompositeLocation condLoc =
661 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(),
664 // addLocationType(isn.getCondition().getType(), condLoc);
666 constraint = generateNewConstraint(constraint, condLoc);
667 checkLocationFromBlockNode(md, nametable, isn.getTrueBlock(), constraint);
669 if (isn.getFalseBlock() != null) {
670 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock(), constraint);
673 return new CompositeLocation();
676 private void checkOwnership(MethodDescriptor md, TreeNode tn, ExpressionNode srcExpNode) {
678 if (srcExpNode.kind() == Kind.NameNode || srcExpNode.kind() == Kind.FieldAccessNode) {
679 if (srcExpNode.getType().isPtr() && !srcExpNode.getType().isNull()) {
680 // first, check the linear type
681 // RHS reference should be owned by the current method
682 FieldDescriptor fd = getFieldDescriptorFromExpressionNode(srcExpNode);
686 isOwned = ((SSJavaType) srcExpNode.getType().getExtension()).isOwned();
689 isOwned = ssjava.isOwnedByMethod(md, fd);
693 "It is not allowed to create the reference alias from the reference not owned by the method at "
694 + generateErrorMessage(md.getClassDesc(), tn));
702 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
703 SymbolTable nametable, DeclarationNode dn, CompositeLocation constraint) {
705 VarDescriptor vd = dn.getVarDescriptor();
707 CompositeLocation destLoc = d2loc.get(vd);
709 if (dn.getExpression() != null) {
711 checkOwnership(md, dn, dn.getExpression());
713 CompositeLocation expressionLoc =
714 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
715 new CompositeLocation(), constraint, false);
716 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
718 if (expressionLoc != null) {
720 // checking location order
721 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc,
722 generateErrorMessage(md.getClassDesc(), dn))) {
723 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
724 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
725 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
728 return expressionLoc;
732 return new CompositeLocation();
738 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
740 checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
743 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
744 SymbolTable nametable, BlockExpressionNode ben, CompositeLocation constraint) {
746 CompositeLocation compLoc =
747 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null, constraint, false);
748 // addTypeLocation(ben.getExpression().getType(), compLoc);
752 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
753 SymbolTable nametable, ExpressionNode en, CompositeLocation loc,
754 CompositeLocation constraint, boolean isLHS) {
756 CompositeLocation compLoc = null;
759 case Kind.AssignmentNode:
761 checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc, constraint);
764 case Kind.FieldAccessNode:
766 checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc, constraint);
770 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc, constraint);
774 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en, constraint);
777 case Kind.CreateObjectNode:
778 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
781 case Kind.ArrayAccessNode:
783 checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en, constraint, isLHS);
786 case Kind.LiteralNode:
787 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
790 case Kind.MethodInvokeNode:
792 checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc, constraint);
795 case Kind.TertiaryNode:
796 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en, constraint);
800 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en, constraint);
803 // case Kind.InstanceOfNode:
804 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
807 // case Kind.ArrayInitializerNode:
808 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
812 // case Kind.ClassTypeNode:
813 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
816 // case Kind.OffsetNode:
817 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
824 // addTypeLocation(en.getType(), compLoc);
829 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
830 CastNode cn, CompositeLocation constraint) {
832 ExpressionNode en = cn.getExpression();
833 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
838 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
839 SymbolTable nametable, TertiaryNode tn, CompositeLocation constraint) {
840 ClassDescriptor cd = md.getClassDesc();
842 CompositeLocation condLoc =
843 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(),
845 // addLocationType(tn.getCond().getType(), condLoc);
846 CompositeLocation trueLoc =
847 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(),
849 // addLocationType(tn.getTrueExpr().getType(), trueLoc);
850 CompositeLocation falseLoc =
851 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(),
853 // addLocationType(tn.getFalseExpr().getType(), falseLoc);
855 // locations from true/false branches can be TOP when there are only literal
857 // in this case, we don't need to check flow down rule!
859 // System.out.println("\n#tertiary cond=" + tn.getCond().printNode(0) +
860 // " Loc=" + condLoc);
861 // System.out.println("# true=" + tn.getTrueExpr().printNode(0) + " Loc=" +
863 // System.out.println("# false=" + tn.getFalseExpr().printNode(0) + " Loc="
866 // check if condLoc is higher than trueLoc & falseLoc
867 if (!trueLoc.get(0).isTop()
868 && !CompositeLattice.isGreaterThan(condLoc, trueLoc, generateErrorMessage(cd, tn))) {
870 "The location of the condition expression is lower than the true expression at "
871 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
874 if (!falseLoc.get(0).isTop()
875 && !CompositeLattice.isGreaterThan(condLoc, falseLoc,
876 generateErrorMessage(cd, tn.getCond()))) {
878 "The location of the condition expression is lower than the false expression at "
879 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
882 // then, return glb of trueLoc & falseLoc
883 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
884 glbInputSet.add(trueLoc);
885 glbInputSet.add(falseLoc);
887 if (glbInputSet.size() == 1) {
890 return CompositeLattice.calculateGLB(glbInputSet, generateErrorMessage(cd, tn));
895 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
896 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc,
897 CompositeLocation constraint) {
899 ClassDescriptor cd = md.getClassDesc();
900 MethodDescriptor calleeMD = min.getMethod();
902 NameDescriptor baseName = min.getBaseName();
903 boolean isSystemout = false;
904 if (baseName != null) {
905 isSystemout = baseName.getSymbol().equals("System.out");
908 if (!ssjava.isSSJavaUtil(calleeMD.getClassDesc()) && !ssjava.isTrustMethod(calleeMD)
909 && !calleeMD.getModifiers().isNative() && !isSystemout) {
911 CompositeLocation baseLocation = null;
912 if (min.getExpression() != null) {
914 checkLocationFromExpressionNode(md, nametable, min.getExpression(),
915 new CompositeLocation(), constraint, false);
917 if (min.getMethod().isStatic()) {
918 String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
919 if (globalLocId == null) {
920 throw new Error("Method lattice does not define global variable location at "
921 + generateErrorMessage(md.getClassDesc(), min));
923 baseLocation = new CompositeLocation(new Location(md, globalLocId));
925 String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
926 baseLocation = new CompositeLocation(new Location(md, thisLocId));
930 // System.out.println("\n#checkLocationFromMethodInvokeNode=" +
932 // + " baseLocation=" + baseLocation + " constraint=" + constraint);
934 if (constraint != null) {
936 CompositeLattice.compare(constraint, baseLocation, true, generateErrorMessage(cd, min));
937 if (compareResult != ComparisonResult.GREATER) {
938 // if the current constraint is higher than method's THIS location
939 // no need to check constraints!
940 CompositeLocation calleeConstraint =
941 translateCallerLocToCalleeLoc(calleeMD, baseLocation, constraint);
942 // System.out.println("check method body for constraint:" + calleeMD +
943 // " calleeConstraint="
944 // + calleeConstraint);
945 checkMethodBody(calleeMD.getClassDesc(), calleeMD, calleeConstraint);
949 checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
951 // checkCallerArgumentLocationConstraints(md, nametable, min,
952 // baseLocation, constraint);
954 if (!min.getMethod().getReturnType().isVoid()) {
955 // If method has a return value, compute the highest possible return
956 // location in the caller's perspective
957 CompositeLocation ceilingLoc =
958 computeCeilingLocationForCaller(md, nametable, min, baseLocation, constraint);
963 return new CompositeLocation(Location.createTopLocation(md));
967 private CompositeLocation translateCallerLocToCalleeLoc(MethodDescriptor calleeMD,
968 CompositeLocation calleeBaseLoc, CompositeLocation constraint) {
970 CompositeLocation calleeConstraint = new CompositeLocation();
972 // if (constraint.startsWith(calleeBaseLoc)) {
973 // if the first part of constraint loc is matched with callee base loc
974 Location thisLoc = new Location(calleeMD, ssjava.getMethodLattice(calleeMD).getThisLoc());
975 calleeConstraint.addLocation(thisLoc);
976 for (int i = calleeBaseLoc.getSize(); i < constraint.getSize(); i++) {
977 calleeConstraint.addLocation(constraint.get(i));
982 return calleeConstraint;
985 private void checkCallerArgumentLocationConstraints(MethodDescriptor md, SymbolTable nametable,
986 MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
987 // if parameter location consists of THIS and FIELD location,
988 // caller should pass an argument that is comparable to the declared
989 // parameter location
990 // and is not lower than the declared parameter location in the field
993 MethodDescriptor calleemd = min.getMethod();
995 List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
996 List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
998 MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
999 Location calleeThisLoc = new Location(calleemd, calleeLattice.getThisLoc());
1001 for (int i = 0; i < min.numArgs(); i++) {
1002 ExpressionNode en = min.getArg(i);
1003 CompositeLocation callerArgLoc =
1004 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1006 callerArgList.add(callerArgLoc);
1009 // setup callee params set
1010 for (int i = 0; i < calleemd.numParameters(); i++) {
1011 VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
1012 CompositeLocation calleeLoc = d2loc.get(calleevd);
1013 calleeParamList.add(calleeLoc);
1016 String errorMsg = generateErrorMessage(md.getClassDesc(), min);
1018 // System.out.println("checkCallerArgumentLocationConstraints=" +
1019 // min.printNode(0));
1020 // System.out.println("base location=" + callerBaseLoc + " constraint=" +
1023 for (int i = 0; i < calleeParamList.size(); i++) {
1024 CompositeLocation calleeParamLoc = calleeParamList.get(i);
1025 if (calleeParamLoc.get(0).equals(calleeThisLoc) && calleeParamLoc.getSize() > 1) {
1027 // callee parameter location has field information
1028 CompositeLocation callerArgLoc = callerArgList.get(i);
1030 CompositeLocation paramLocation =
1031 translateCalleeParamLocToCaller(md, calleeParamLoc, callerBaseLoc, errorMsg);
1033 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
1034 if (constraint != null) {
1035 inputGLBSet.add(callerArgLoc);
1036 inputGLBSet.add(constraint);
1038 CompositeLattice.calculateGLB(inputGLBSet,
1039 generateErrorMessage(md.getClassDesc(), min));
1042 if (!CompositeLattice.isGreaterThan(callerArgLoc, paramLocation, errorMsg)) {
1043 throw new Error("Caller argument '" + min.getArg(i).printNode(0) + " : " + callerArgLoc
1044 + "' should be higher than corresponding callee's parameter : " + paramLocation
1045 + " at " + errorMsg);
1053 private CompositeLocation translateCalleeParamLocToCaller(MethodDescriptor md,
1054 CompositeLocation calleeParamLoc, CompositeLocation callerBaseLocation, String errorMsg) {
1056 CompositeLocation translate = new CompositeLocation();
1058 for (int i = 0; i < callerBaseLocation.getSize(); i++) {
1059 translate.addLocation(callerBaseLocation.get(i));
1062 for (int i = 1; i < calleeParamLoc.getSize(); i++) {
1063 translate.addLocation(calleeParamLoc.get(i));
1066 // System.out.println("TRANSLATED=" + translate + " from calleeParamLoc=" +
1072 private CompositeLocation computeCeilingLocationForCaller(MethodDescriptor md,
1073 SymbolTable nametable, MethodInvokeNode min, CompositeLocation baseLocation,
1074 CompositeLocation constraint) {
1075 List<CompositeLocation> argList = new ArrayList<CompositeLocation>();
1077 // by default, method has a THIS parameter
1078 argList.add(baseLocation);
1080 for (int i = 0; i < min.numArgs(); i++) {
1081 ExpressionNode en = min.getArg(i);
1082 CompositeLocation callerArg =
1083 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1085 argList.add(callerArg);
1088 // System.out.println("\n## computeReturnLocation=" + min.getMethod() +
1089 // " argList=" + argList);
1090 CompositeLocation ceilLoc = md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
1091 // System.out.println("## ReturnLocation=" + ceilLoc);
1097 private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
1098 MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
1100 // System.out.println("checkCalleeConstraints=" + min.printNode(0));
1102 MethodDescriptor calleemd = min.getMethod();
1104 MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
1105 CompositeLocation calleeThisLoc =
1106 new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
1108 List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
1109 List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
1111 if (min.numArgs() > 0) {
1112 // caller needs to guarantee that it passes arguments in regarding to
1113 // callee's hierarchy
1115 // setup caller args set
1116 // first, add caller's base(this) location
1117 callerArgList.add(callerBaseLoc);
1118 // second, add caller's arguments
1119 for (int i = 0; i < min.numArgs(); i++) {
1120 ExpressionNode en = min.getArg(i);
1121 CompositeLocation callerArgLoc =
1122 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1124 callerArgList.add(callerArgLoc);
1127 // setup callee params set
1128 // first, add callee's this location
1129 calleeParamList.add(calleeThisLoc);
1130 // second, add callee's parameters
1131 for (int i = 0; i < calleemd.numParameters(); i++) {
1132 VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
1133 CompositeLocation calleeLoc = d2loc.get(calleevd);
1134 // System.out.println("calleevd=" + calleevd + " loc=" + calleeLoc);
1135 calleeParamList.add(calleeLoc);
1138 // here, check if ordering relations among caller's args respect
1139 // ordering relations in-between callee's args
1140 CHECK: for (int i = 0; i < calleeParamList.size(); i++) {
1141 CompositeLocation calleeLoc1 = calleeParamList.get(i);
1142 CompositeLocation callerLoc1 = callerArgList.get(i);
1144 for (int j = 0; j < calleeParamList.size(); j++) {
1146 CompositeLocation calleeLoc2 = calleeParamList.get(j);
1147 CompositeLocation callerLoc2 = callerArgList.get(j);
1149 if (callerLoc1.get(callerLoc1.getSize() - 1).isTop()
1150 || callerLoc2.get(callerLoc2.getSize() - 1).isTop()) {
1154 // System.out.println("calleeLoc1=" + calleeLoc1);
1155 // System.out.println("calleeLoc2=" + calleeLoc2 +
1156 // "calleeParamList=" + calleeParamList);
1159 CompositeLattice.compare(callerLoc1, callerLoc2, true,
1160 generateErrorMessage(md.getClassDesc(), min));
1161 // System.out.println("callerResult=" + callerResult);
1163 CompositeLattice.compare(calleeLoc1, calleeLoc2, true,
1164 generateErrorMessage(md.getClassDesc(), min));
1165 // System.out.println("calleeResult=" + calleeResult);
1167 if (callerResult == ComparisonResult.EQUAL) {
1168 if (ssjava.isSharedLocation(callerLoc1.get(callerLoc1.getSize() - 1))
1169 && ssjava.isSharedLocation(callerLoc2.get(callerLoc2.getSize() - 1))) {
1170 // if both of them are shared locations, promote them to
1171 // "GREATER relation"
1172 callerResult = ComparisonResult.GREATER;
1176 if (calleeResult == ComparisonResult.GREATER
1177 && callerResult != ComparisonResult.GREATER) {
1178 // If calleeLoc1 is higher than calleeLoc2
1179 // then, caller should have same ordering relation in-bet
1180 // callerLoc1 & callerLoc2
1182 String paramName1, paramName2;
1185 paramName1 = "'THIS'";
1187 paramName1 = "'parameter " + calleemd.getParamName(i - 1) + "'";
1191 paramName2 = "'THIS'";
1193 paramName2 = "'parameter " + calleemd.getParamName(j - 1) + "'";
1197 "Caller doesn't respect an ordering relation among method arguments: callee expects that "
1198 + paramName1 + " should be higher than " + paramName2 + " in " + calleemd
1199 + " at " + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
1210 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
1211 SymbolTable nametable, ArrayAccessNode aan, CompositeLocation constraint, boolean isLHS) {
1213 ClassDescriptor cd = md.getClassDesc();
1215 CompositeLocation arrayLoc =
1216 checkLocationFromExpressionNode(md, nametable, aan.getExpression(),
1217 new CompositeLocation(), constraint, isLHS);
1219 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
1220 CompositeLocation indexLoc =
1221 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(),
1223 // addTypeLocation(aan.getIndex().getType(), indexLoc);
1226 if (!CompositeLattice.isGreaterThan(indexLoc, arrayLoc, generateErrorMessage(cd, aan))) {
1227 throw new Error("Array index value is not higher than array location at "
1228 + generateErrorMessage(cd, aan));
1232 Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
1233 inputGLB.add(arrayLoc);
1234 inputGLB.add(indexLoc);
1235 return CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(cd, aan));
1240 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
1241 SymbolTable nametable, CreateObjectNode con) {
1243 ClassDescriptor cd = md.getClassDesc();
1245 CompositeLocation compLoc = new CompositeLocation();
1246 compLoc.addLocation(Location.createTopLocation(md));
1251 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
1252 OpNode on, CompositeLocation constraint) {
1254 ClassDescriptor cd = md.getClassDesc();
1255 CompositeLocation leftLoc = new CompositeLocation();
1257 checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc, constraint, false);
1258 // addTypeLocation(on.getLeft().getType(), leftLoc);
1260 CompositeLocation rightLoc = new CompositeLocation();
1261 if (on.getRight() != null) {
1263 checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc, constraint, false);
1264 // addTypeLocation(on.getRight().getType(), rightLoc);
1267 // System.out.println("\n# OP NODE=" + on.printNode(0));
1268 // System.out.println("# left loc=" + leftLoc + " from " +
1269 // on.getLeft().getClass());
1270 // if (on.getRight() != null) {
1271 // System.out.println("# right loc=" + rightLoc + " from " +
1272 // on.getRight().getClass());
1275 Operation op = on.getOp();
1277 switch (op.getOp()) {
1279 case Operation.UNARYPLUS:
1280 case Operation.UNARYMINUS:
1281 case Operation.LOGIC_NOT:
1285 case Operation.LOGIC_OR:
1286 case Operation.LOGIC_AND:
1287 case Operation.COMP:
1288 case Operation.BIT_OR:
1289 case Operation.BIT_XOR:
1290 case Operation.BIT_AND:
1291 case Operation.ISAVAILABLE:
1292 case Operation.EQUAL:
1293 case Operation.NOTEQUAL:
1300 case Operation.MULT:
1303 case Operation.LEFTSHIFT:
1304 case Operation.RIGHTSHIFT:
1305 case Operation.URIGHTSHIFT:
1307 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
1308 inputSet.add(leftLoc);
1309 inputSet.add(rightLoc);
1310 CompositeLocation glbCompLoc =
1311 CompositeLattice.calculateGLB(inputSet, generateErrorMessage(cd, on));
1315 throw new Error(op.toString());
1320 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
1321 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
1323 // literal value has the top location so that value can be flowed into any
1325 Location literalLoc = Location.createTopLocation(md);
1326 loc.addLocation(literalLoc);
1331 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
1332 NameNode nn, CompositeLocation loc, CompositeLocation constraint) {
1334 NameDescriptor nd = nn.getName();
1335 if (nd.getBase() != null) {
1337 checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc, constraint, false);
1339 String varname = nd.toString();
1340 if (varname.equals("this")) {
1342 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
1343 String thisLocId = methodLattice.getThisLoc();
1344 if (thisLocId == null) {
1345 throw new Error("The location for 'this' is not defined at "
1346 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
1348 Location locElement = new Location(md, thisLocId);
1349 loc.addLocation(locElement);
1354 Descriptor d = (Descriptor) nametable.get(varname);
1356 // CompositeLocation localLoc = null;
1357 if (d instanceof VarDescriptor) {
1358 VarDescriptor vd = (VarDescriptor) d;
1359 // localLoc = d2loc.get(vd);
1360 // the type of var descriptor has a composite location!
1361 loc = ((SSJavaType) vd.getType().getExtension()).getCompLoc().clone();
1362 } else if (d instanceof FieldDescriptor) {
1363 // the type of field descriptor has a location!
1364 FieldDescriptor fd = (FieldDescriptor) d;
1365 if (fd.isStatic()) {
1367 // if it is 'static final', the location has TOP since no one can
1369 loc.addLocation(Location.createTopLocation(md));
1372 // if 'static', the location has pre-assigned global loc
1373 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1374 String globalLocId = localLattice.getGlobalLoc();
1375 if (globalLocId == null) {
1376 throw new Error("Global location element is not defined in the method " + md);
1378 Location globalLoc = new Location(md, globalLocId);
1380 loc.addLocation(globalLoc);
1383 // the location of field access starts from this, followed by field
1385 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1386 Location thisLoc = new Location(md, localLattice.getThisLoc());
1387 loc.addLocation(thisLoc);
1390 Location fieldLoc = (Location) fd.getType().getExtension();
1391 loc.addLocation(fieldLoc);
1392 } else if (d == null) {
1393 // access static field
1394 FieldDescriptor fd = nn.getField();
1396 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1397 String globalLocId = localLattice.getGlobalLoc();
1398 if (globalLocId == null) {
1399 throw new Error("Method lattice does not define global variable location at "
1400 + generateErrorMessage(md.getClassDesc(), nn));
1402 loc.addLocation(new Location(md, globalLocId));
1404 Location fieldLoc = (Location) fd.getType().getExtension();
1405 loc.addLocation(fieldLoc);
1414 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
1415 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc,
1416 CompositeLocation constraint) {
1418 ExpressionNode left = fan.getExpression();
1419 TypeDescriptor ltd = left.getType();
1421 FieldDescriptor fd = fan.getField();
1423 String varName = null;
1424 if (left.kind() == Kind.NameNode) {
1425 NameDescriptor nd = ((NameNode) left).getName();
1426 varName = nd.toString();
1429 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
1430 // using a class name directly or access using this
1431 if (fd.isStatic() && fd.isFinal()) {
1432 loc.addLocation(Location.createTopLocation(md));
1437 if (left instanceof ArrayAccessNode) {
1438 ArrayAccessNode aan = (ArrayAccessNode) left;
1439 left = aan.getExpression();
1442 loc = checkLocationFromExpressionNode(md, nametable, left, loc, constraint, false);
1443 // System.out.println("### checkLocationFromFieldAccessNode=" +
1444 // fan.printNode(0));
1445 // System.out.println("### left=" + left.printNode(0));
1447 if (!left.getType().isPrimitive()) {
1449 if (fd.getSymbol().equals("length")) {
1450 // array.length access, return the location of the array
1454 Location fieldLoc = getFieldLocation(fd);
1455 loc.addLocation(fieldLoc);
1460 private Location getFieldLocation(FieldDescriptor fd) {
1462 // System.out.println("### getFieldLocation=" + fd);
1463 // System.out.println("### fd.getType().getExtension()=" +
1464 // fd.getType().getExtension());
1466 Location fieldLoc = (Location) fd.getType().getExtension();
1468 // handle the case that method annotation checking skips checking field
1470 if (fieldLoc == null) {
1471 fieldLoc = checkFieldDeclaration(fd.getClassDescriptor(), fd);
1478 private FieldDescriptor getFieldDescriptorFromExpressionNode(ExpressionNode en) {
1480 if (en.kind() == Kind.NameNode) {
1481 NameNode nn = (NameNode) en;
1482 if (nn.getField() != null) {
1483 return nn.getField();
1486 if (nn.getName() != null && nn.getName().getBase() != null) {
1487 return getFieldDescriptorFromExpressionNode(nn.getExpression());
1490 } else if (en.kind() == Kind.FieldAccessNode) {
1491 FieldAccessNode fan = (FieldAccessNode) en;
1492 return fan.getField();
1498 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
1499 SymbolTable nametable, AssignmentNode an, CompositeLocation loc, CompositeLocation constraint) {
1501 // System.out.println("\n# ASSIGNMENTNODE=" + an.printNode(0));
1503 ClassDescriptor cd = md.getClassDesc();
1505 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
1507 boolean postinc = true;
1508 if (an.getOperation().getBaseOp() == null
1509 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
1510 .getBaseOp().getOp() != Operation.POSTDEC))
1513 // if LHS is array access node, need to check if array index is higher
1514 // than array itself
1515 CompositeLocation destLocation =
1516 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(),
1519 CompositeLocation rhsLocation;
1520 CompositeLocation srcLocation;
1524 checkOwnership(md, an, an.getSrc());
1527 checkLocationFromExpressionNode(md, nametable, an.getSrc(), new CompositeLocation(),
1530 srcLocation = rhsLocation;
1532 // if (!rhsLocation.get(rhsLocation.getSize() - 1).isTop()) {
1533 if (constraint != null) {
1534 inputGLBSet.add(rhsLocation);
1535 inputGLBSet.add(constraint);
1536 srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
1540 // System.out.println("dstLocation=" + destLocation);
1541 // System.out.println("rhsLocation=" + rhsLocation);
1542 // System.out.println("srcLocation=" + srcLocation);
1543 // System.out.println("constraint=" + constraint);
1545 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
1547 String context = "";
1548 if (constraint != null) {
1549 context = " and the current context constraint is " + constraint;
1552 throw new Error("The value flow from " + srcLocation + " to " + destLocation
1553 + " does not respect location hierarchy on the assignment " + an.printNode(0) + context
1554 + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1557 if (srcLocation.equals(destLocation)) {
1558 // keep it for definitely written analysis
1559 Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
1560 for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
1561 FlatNode fn = (FlatNode) iterator.next();
1562 ssjava.addSameHeightWriteFlatNode(fn);
1570 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(),
1573 if (constraint != null) {
1574 inputGLBSet.add(rhsLocation);
1575 inputGLBSet.add(constraint);
1576 srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
1578 srcLocation = rhsLocation;
1581 // System.out.println("srcLocation=" + srcLocation);
1582 // System.out.println("rhsLocation=" + rhsLocation);
1583 // System.out.println("constraint=" + constraint);
1585 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
1587 if (srcLocation.equals(destLocation)) {
1588 throw new Error("Location " + srcLocation
1589 + " is not allowed to have the value flow that moves within the same location at '"
1590 + an.printNode(0) + "' of " + cd.getSourceFileName() + "::" + an.getNumLine());
1592 throw new Error("The value flow from " + srcLocation + " to " + destLocation
1593 + " does not respect location hierarchy on the assignment " + an.printNode(0)
1594 + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1599 if (srcLocation.equals(destLocation)) {
1600 // keep it for definitely written analysis
1601 Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
1602 for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
1603 FlatNode fn = (FlatNode) iterator.next();
1604 ssjava.addSameHeightWriteFlatNode(fn);
1610 return destLocation;
1613 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
1614 SymbolTable nametable, TreeNode n) {
1616 ClassDescriptor cd = md.getClassDesc();
1617 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
1619 // currently enforce every variable to have corresponding location
1620 if (annotationVec.size() == 0) {
1621 throw new Error("Location is not assigned to variable '" + vd.getSymbol()
1622 + "' in the method '" + md + "' of the class " + cd.getSymbol() + " at "
1623 + generateErrorMessage(cd, n));
1626 int locDecCount = 0;
1627 for (int i = 0; i < annotationVec.size(); i++) {
1628 AnnotationDescriptor ad = annotationVec.elementAt(i);
1630 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1632 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1634 if (locDecCount > 1) {// variable can have at most one location
1635 throw new Error(vd.getSymbol() + " has more than one location declaration.");
1637 String locDec = ad.getValue(); // check if location is defined
1639 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
1640 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
1641 d2loc.put(vd, deltaLoc);
1642 addLocationType(vd.getType(), deltaLoc);
1644 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1646 Location lastElement = compLoc.get(compLoc.getSize() - 1);
1647 if (ssjava.isSharedLocation(lastElement)) {
1648 ssjava.mapSharedLocation2Descriptor(lastElement, vd);
1651 d2loc.put(vd, compLoc);
1652 addLocationType(vd.getType(), compLoc);
1661 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1664 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1667 int beginIdx = dIdx + 6;
1668 locDec = locDec.substring(beginIdx, locDec.length() - 1);
1669 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1672 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1673 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
1678 private Location parseFieldLocDeclaraton(String decl, String msg) throws Exception {
1680 int idx = decl.indexOf(".");
1682 String className = decl.substring(0, idx);
1683 String fieldName = decl.substring(idx + 1);
1685 className.replaceAll(" ", "");
1686 fieldName.replaceAll(" ", "");
1688 Descriptor d = state.getClassSymbolTable().get(className);
1691 // System.out.println("state.getClassSymbolTable()=" +
1692 // state.getClassSymbolTable());
1693 throw new Error("The class in the location declaration '" + decl + "' does not exist at "
1697 assert (d instanceof ClassDescriptor);
1698 SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1699 if (!lattice.containsKey(fieldName)) {
1700 throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1701 + className + "' at " + msg);
1704 return new Location(d, fieldName);
1707 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1709 CompositeLocation compLoc = new CompositeLocation();
1711 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1712 List<String> locIdList = new ArrayList<String>();
1713 while (tokenizer.hasMoreTokens()) {
1714 String locId = tokenizer.nextToken();
1715 locIdList.add(locId);
1718 // at least,one location element needs to be here!
1719 assert (locIdList.size() > 0);
1721 // assume that loc with idx 0 comes from the local lattice
1722 // loc with idx 1 comes from the field lattice
1724 String localLocId = locIdList.get(0);
1725 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1726 Location localLoc = new Location(md, localLocId);
1727 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1728 throw new Error("Location " + localLocId
1729 + " is not defined in the local variable lattice at "
1730 + md.getClassDesc().getSourceFileName() + "::" + (n != null ? n.getNumLine() : md) + ".");
1732 compLoc.addLocation(localLoc);
1734 for (int i = 1; i < locIdList.size(); i++) {
1735 String locName = locIdList.get(i);
1738 parseFieldLocDeclaraton(locName, generateErrorMessage(md.getClassDesc(), n));
1739 compLoc.addLocation(fieldLoc);
1740 } catch (Exception e) {
1741 throw new Error("The location declaration '" + locName + "' is wrong at "
1742 + generateErrorMessage(md.getClassDesc(), n));
1750 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1751 VarDescriptor vd = dn.getVarDescriptor();
1752 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1755 private void checkDeclarationInClass(ClassDescriptor cd) {
1756 // Check to see that fields are okay
1757 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1758 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1760 if (!(fd.isFinal() && fd.isStatic())) {
1761 checkFieldDeclaration(cd, fd);
1763 // for static final, assign top location by default
1764 Location loc = Location.createTopLocation(cd);
1765 addLocationType(fd.getType(), loc);
1770 private Location checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1772 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1774 // currently enforce every field to have corresponding location
1775 if (annotationVec.size() == 0) {
1776 throw new Error("Location is not assigned to the field '" + fd.getSymbol()
1777 + "' of the class " + cd.getSymbol() + " at " + cd.getSourceFileName());
1780 if (annotationVec.size() > 1) {
1781 // variable can have at most one location
1782 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1783 + " has more than one location.");
1786 AnnotationDescriptor ad = annotationVec.elementAt(0);
1787 Location loc = null;
1789 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1790 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1791 String locationID = ad.getValue();
1792 // check if location is defined
1793 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1794 if (lattice == null || (!lattice.containsKey(locationID))) {
1795 throw new Error("Location " + locationID
1796 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1797 + cd.getSourceFileName() + ".");
1799 loc = new Location(cd, locationID);
1801 if (ssjava.isSharedLocation(loc)) {
1802 ssjava.mapSharedLocation2Descriptor(loc, fd);
1805 addLocationType(fd.getType(), loc);
1813 private void addLocationType(TypeDescriptor type, CompositeLocation loc) {
1815 TypeExtension te = type.getExtension();
1818 ssType = (SSJavaType) te;
1819 ssType.setCompLoc(loc);
1821 ssType = new SSJavaType(loc);
1822 type.setExtension(ssType);
1827 private void addLocationType(TypeDescriptor type, Location loc) {
1829 type.setExtension(loc);
1833 static class CompositeLattice {
1835 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2, String msg) {
1837 // System.out.println("\nisGreaterThan=" + loc1 + " " + loc2 + " msg=" +
1839 int baseCompareResult = compareBaseLocationSet(loc1, loc2, true, false, msg);
1840 if (baseCompareResult == ComparisonResult.EQUAL) {
1841 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1846 } else if (baseCompareResult == ComparisonResult.GREATER) {
1854 public static int compare(CompositeLocation loc1, CompositeLocation loc2, boolean ignore,
1857 // System.out.println("compare=" + loc1 + " " + loc2);
1858 int baseCompareResult = compareBaseLocationSet(loc1, loc2, false, ignore, msg);
1860 if (baseCompareResult == ComparisonResult.EQUAL) {
1861 return compareDelta(loc1, loc2);
1863 return baseCompareResult;
1868 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1870 int deltaCount1 = 0;
1871 int deltaCount2 = 0;
1872 if (dLoc1 instanceof DeltaLocation) {
1873 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1876 if (dLoc2 instanceof DeltaLocation) {
1877 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1879 if (deltaCount1 < deltaCount2) {
1880 return ComparisonResult.GREATER;
1881 } else if (deltaCount1 == deltaCount2) {
1882 return ComparisonResult.EQUAL;
1884 return ComparisonResult.LESS;
1889 private static int compareBaseLocationSet(CompositeLocation compLoc1,
1890 CompositeLocation compLoc2, boolean awareSharedLoc, boolean ignore, String msg) {
1892 // if compLoc1 is greater than compLoc2, return true
1893 // else return false;
1895 // compare one by one in according to the order of the tuple
1897 for (int i = 0; i < compLoc1.getSize(); i++) {
1898 Location loc1 = compLoc1.get(i);
1899 if (i >= compLoc2.getSize()) {
1901 return ComparisonResult.INCOMPARABLE;
1903 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1904 + " because they are not comparable at " + msg);
1907 Location loc2 = compLoc2.get(i);
1909 Descriptor descriptor = getCommonParentDescriptor(loc1, loc2, msg);
1910 SSJavaLattice<String> lattice = getLatticeByDescriptor(descriptor);
1912 // check if the shared location is appeared only at the end of the
1913 // composite location
1914 if (lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
1915 if (i != (compLoc1.getSize() - 1)) {
1916 throw new Error("The shared location " + loc1.getLocIdentifier()
1917 + " cannot be appeared in the middle of composite location at" + msg);
1921 if (lattice.getSharedLocSet().contains(loc2.getLocIdentifier())) {
1922 if (i != (compLoc2.getSize() - 1)) {
1923 throw new Error("The shared location " + loc2.getLocIdentifier()
1924 + " cannot be appeared in the middle of composite location at " + msg);
1928 // if (!lattice1.equals(lattice2)) {
1929 // throw new Error("Failed to compare two locations of " + compLoc1 +
1930 // " and " + compLoc2
1931 // + " because they are not comparable at " + msg);
1934 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1936 // check if the current location is the spinning location
1937 // note that the spinning location only can be appeared in the last
1938 // part of the composite location
1939 if (awareSharedLoc && numOfTie == compLoc1.getSize()
1940 && lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
1941 return ComparisonResult.GREATER;
1944 } else if (lattice.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1945 return ComparisonResult.GREATER;
1947 return ComparisonResult.LESS;
1952 if (numOfTie == compLoc1.getSize()) {
1954 if (numOfTie != compLoc2.getSize()) {
1957 return ComparisonResult.INCOMPARABLE;
1959 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1960 + " because they are not comparable at " + msg);
1965 return ComparisonResult.EQUAL;
1968 return ComparisonResult.LESS;
1972 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet, String errMsg) {
1974 // System.out.println("Calculating GLB=" + inputSet);
1975 CompositeLocation glbCompLoc = new CompositeLocation();
1977 // calculate GLB of the first(priority) element
1978 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1979 Descriptor priorityDescriptor = null;
1981 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1982 new Hashtable<String, Set<CompositeLocation>>();
1983 // mapping from the priority loc ID to its full representation by the
1984 // composite location
1986 int maxTupleSize = 0;
1987 CompositeLocation maxCompLoc = null;
1989 Location prevPriorityLoc = null;
1990 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1991 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1992 if (compLoc.getSize() > maxTupleSize) {
1993 maxTupleSize = compLoc.getSize();
1994 maxCompLoc = compLoc;
1996 Location priorityLoc = compLoc.get(0);
1997 String priorityLocId = priorityLoc.getLocIdentifier();
1998 priorityLocIdentifierSet.add(priorityLocId);
2000 if (locId2CompLocSet.containsKey(priorityLocId)) {
2001 locId2CompLocSet.get(priorityLocId).add(compLoc);
2003 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
2004 newSet.add(compLoc);
2005 locId2CompLocSet.put(priorityLocId, newSet);
2008 // check if priority location are coming from the same lattice
2009 if (priorityDescriptor == null) {
2010 priorityDescriptor = priorityLoc.getDescriptor();
2012 priorityDescriptor = getCommonParentDescriptor(priorityLoc, prevPriorityLoc, errMsg);
2014 prevPriorityLoc = priorityLoc;
2015 // else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
2016 // throw new Error("Failed to calculate GLB of " + inputSet
2017 // + " because they are from different lattices.");
2021 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
2022 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
2024 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
2025 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
2027 if (compSet == null) {
2028 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
2029 // mean that the result is already lower than <x1,y1> and <x2,y2>
2030 // assign TOP to the rest of the location elements
2032 // in this case, do not take care about delta
2033 // CompositeLocation inputComp = inputSet.iterator().next();
2034 for (int i = 1; i < maxTupleSize; i++) {
2035 glbCompLoc.addLocation(Location.createTopLocation(maxCompLoc.get(i).getDescriptor()));
2039 // here find out composite location that has a maximum length tuple
2040 // if we have three input set: [A], [A,B], [A,B,C]
2041 // maximum length tuple will be [A,B,C]
2043 CompositeLocation maxFromCompSet = null;
2044 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2045 CompositeLocation c = (CompositeLocation) iterator.next();
2046 if (c.getSize() > max) {
2052 if (compSet.size() == 1) {
2053 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
2054 CompositeLocation comp = compSet.iterator().next();
2055 for (int i = 1; i < comp.getSize(); i++) {
2056 glbCompLoc.addLocation(comp.get(i));
2059 // if input location corresponding to glb is a delta, need to apply
2060 // delta to glb result
2061 if (comp instanceof DeltaLocation) {
2062 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
2066 // when GLB(x1,x2)==x1 and x2 : GLB case 1
2067 // if more than one location shares the same priority GLB
2068 // need to calculate the rest of GLB loc
2070 // setup input set starting from the second tuple item
2071 Set<CompositeLocation> innerGLBInput = new HashSet<CompositeLocation>();
2072 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2073 CompositeLocation compLoc = (CompositeLocation) iterator.next();
2074 CompositeLocation innerCompLoc = new CompositeLocation();
2075 for (int idx = 1; idx < compLoc.getSize(); idx++) {
2076 innerCompLoc.addLocation(compLoc.get(idx));
2078 if (innerCompLoc.getSize() > 0) {
2079 innerGLBInput.add(innerCompLoc);
2083 if (innerGLBInput.size() > 0) {
2084 CompositeLocation innerGLB = CompositeLattice.calculateGLB(innerGLBInput, errMsg);
2085 for (int idx = 0; idx < innerGLB.getSize(); idx++) {
2086 glbCompLoc.addLocation(innerGLB.get(idx));
2090 // if input location corresponding to glb is a delta, need to apply
2091 // delta to glb result
2093 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2094 CompositeLocation compLoc = (CompositeLocation) iterator.next();
2095 if (compLoc instanceof DeltaLocation) {
2096 if (glbCompLoc.equals(compLoc)) {
2097 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
2106 // System.out.println("GLB=" + glbCompLoc);
2111 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
2113 SSJavaLattice<String> lattice = null;
2115 if (d instanceof ClassDescriptor) {
2116 lattice = ssjava.getCd2lattice().get(d);
2117 } else if (d instanceof MethodDescriptor) {
2118 if (ssjava.getMd2lattice().containsKey(d)) {
2119 lattice = ssjava.getMd2lattice().get(d);
2121 // use default lattice for the method
2122 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
2129 static Descriptor getCommonParentDescriptor(Location loc1, Location loc2, String msg) {
2131 Descriptor d1 = loc1.getDescriptor();
2132 Descriptor d2 = loc2.getDescriptor();
2134 Descriptor descriptor;
2136 if (d1 instanceof ClassDescriptor && d2 instanceof ClassDescriptor) {
2138 if (d1.equals(d2)) {
2141 // identifying which one is parent class
2142 Set<Descriptor> d1SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d1);
2143 Set<Descriptor> d2SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d2);
2145 if (d1 == null && d2 == null) {
2146 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2147 + " because they are not comparable at " + msg);
2148 } else if (d1SubClassesSet != null && d1SubClassesSet.contains(d2)) {
2150 } else if (d2SubClassesSet != null && d2SubClassesSet.contains(d1)) {
2153 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2154 + " because they are not comparable at " + msg);
2158 } else if (d1 instanceof MethodDescriptor && d2 instanceof MethodDescriptor) {
2160 if (d1.equals(d2)) {
2164 // identifying which one is parent class
2165 MethodDescriptor md1 = (MethodDescriptor) d1;
2166 MethodDescriptor md2 = (MethodDescriptor) d2;
2168 if (!md1.matches(md2)) {
2169 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2170 + " because they are not comparable at " + msg);
2173 Set<Descriptor> d1SubClassesSet =
2174 ssjava.tu.getSubClasses(((MethodDescriptor) d1).getClassDesc());
2175 Set<Descriptor> d2SubClassesSet =
2176 ssjava.tu.getSubClasses(((MethodDescriptor) d2).getClassDesc());
2178 if (d1 == null && d2 == null) {
2179 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2180 + " because they are not comparable at " + msg);
2181 } else if (d1 != null && d1SubClassesSet.contains(d2)) {
2183 } else if (d2 != null && d2SubClassesSet.contains(d1)) {
2186 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2187 + " because they are not comparable at " + msg);
2192 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2193 + " because they are not comparable at " + msg);
2202 class ComparisonResult {
2204 public static final int GREATER = 0;
2205 public static final int EQUAL = 1;
2206 public static final int LESS = 2;
2207 public static final int INCOMPARABLE = 3;
2214 class ReturnLocGenerator {
2216 public static final int PARAMISHIGHER = 0;
2217 public static final int PARAMISSAME = 1;
2218 public static final int IGNORE = 2;
2220 private Hashtable<Integer, Integer> paramIdx2paramType;
2222 private CompositeLocation declaredReturnLoc = null;
2224 public ReturnLocGenerator(CompositeLocation returnLoc, MethodDescriptor md,
2225 List<CompositeLocation> params, String msg) {
2227 CompositeLocation thisLoc = params.get(0);
2228 if (returnLoc.get(0).equals(thisLoc.get(0)) && returnLoc.getSize() > 1) {
2229 // if the declared return location consists of THIS and field location,
2230 // return location for the caller's side has to have same field element
2231 this.declaredReturnLoc = returnLoc;
2233 // creating mappings
2234 paramIdx2paramType = new Hashtable<Integer, Integer>();
2235 for (int i = 0; i < params.size(); i++) {
2236 CompositeLocation param = params.get(i);
2237 int compareResult = CompositeLattice.compare(param, returnLoc, true, msg);
2240 if (compareResult == ComparisonResult.GREATER) {
2242 } else if (compareResult == ComparisonResult.EQUAL) {
2247 paramIdx2paramType.put(new Integer(i), new Integer(type));
2253 public CompositeLocation computeReturnLocation(List<CompositeLocation> args) {
2255 if (declaredReturnLoc != null) {
2256 // when developer specify that the return value is [THIS,field]
2257 // needs to translate to the caller's location
2258 CompositeLocation callerLoc = new CompositeLocation();
2259 CompositeLocation callerBaseLocation = args.get(0);
2261 for (int i = 0; i < callerBaseLocation.getSize(); i++) {
2262 callerLoc.addLocation(callerBaseLocation.get(i));
2264 for (int i = 1; i < declaredReturnLoc.getSize(); i++) {
2265 callerLoc.addLocation(declaredReturnLoc.get(i));
2269 // compute the highest possible location in caller's side
2270 assert paramIdx2paramType.keySet().size() == args.size();
2272 Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
2273 for (int i = 0; i < args.size(); i++) {
2274 int type = (paramIdx2paramType.get(new Integer(i))).intValue();
2275 CompositeLocation argLoc = args.get(i);
2276 if (type == PARAMISHIGHER || type == PARAMISSAME) {
2277 // return loc is equal to or lower than param
2278 inputGLB.add(argLoc);
2282 // compute GLB of arguments subset that are same or higher than return
2284 if (inputGLB.isEmpty()) {
2285 CompositeLocation rtr =
2286 new CompositeLocation(Location.createTopLocation(args.get(0).get(0).getDescriptor()));
2289 CompositeLocation glb = CompositeLattice.calculateGLB(inputGLB, "");