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;
11 import java.util.StringTokenizer;
12 import java.util.Vector;
14 import Analysis.SSJava.FlowDownCheck.ComparisonResult;
15 import Analysis.SSJava.FlowDownCheck.CompositeLattice;
16 import IR.AnnotationDescriptor;
17 import IR.ClassDescriptor;
19 import IR.FieldDescriptor;
20 import IR.MethodDescriptor;
21 import IR.NameDescriptor;
24 import IR.SymbolTable;
25 import IR.TypeDescriptor;
26 import IR.TypeExtension;
27 import IR.VarDescriptor;
28 import IR.Flat.FlatNode;
29 import IR.Tree.ArrayAccessNode;
30 import IR.Tree.AssignmentNode;
31 import IR.Tree.BlockExpressionNode;
32 import IR.Tree.BlockNode;
33 import IR.Tree.BlockStatementNode;
34 import IR.Tree.CastNode;
35 import IR.Tree.CreateObjectNode;
36 import IR.Tree.DeclarationNode;
37 import IR.Tree.ExpressionNode;
38 import IR.Tree.FieldAccessNode;
39 import IR.Tree.IfStatementNode;
41 import IR.Tree.LiteralNode;
42 import IR.Tree.LoopNode;
43 import IR.Tree.MethodInvokeNode;
44 import IR.Tree.NameNode;
45 import IR.Tree.OpNode;
46 import IR.Tree.ReturnNode;
47 import IR.Tree.SubBlockNode;
48 import IR.Tree.SwitchBlockNode;
49 import IR.Tree.SwitchStatementNode;
50 import IR.Tree.SynchronizedNode;
51 import IR.Tree.TertiaryNode;
52 import IR.Tree.TreeNode;
55 public class FlowDownCheck {
58 static SSJavaAnalysis ssjava;
60 Set<ClassDescriptor> toanalyze;
61 List<ClassDescriptor> toanalyzeList;
63 Set<MethodDescriptor> toanalyzeMethod;
64 List<MethodDescriptor> toanalyzeMethodList;
66 // mapping from 'descriptor' to 'composite location'
67 Hashtable<Descriptor, CompositeLocation> d2loc;
69 Hashtable<MethodDescriptor, CompositeLocation> md2ReturnLoc;
70 Hashtable<MethodDescriptor, ReturnLocGenerator> md2ReturnLocGen;
72 // mapping from 'locID' to 'class descriptor'
73 Hashtable<String, ClassDescriptor> fieldLocName2cd;
75 boolean deterministic = true;
77 public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
81 this.toanalyzeList = new ArrayList<ClassDescriptor>();
83 this.toanalyze = new HashSet<ClassDescriptor>();
86 this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
88 this.toanalyzeMethod = new HashSet<MethodDescriptor>();
90 this.d2loc = new Hashtable<Descriptor, CompositeLocation>();
91 this.fieldLocName2cd = new Hashtable<String, ClassDescriptor>();
92 this.md2ReturnLoc = new Hashtable<MethodDescriptor, CompositeLocation>();
93 this.md2ReturnLocGen = new Hashtable<MethodDescriptor, ReturnLocGenerator>();
98 // construct mapping from the location name to the class descriptor
99 // assume that the location name is unique through the whole program
101 Set<ClassDescriptor> cdSet = ssjava.getCd2lattice().keySet();
102 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
103 ClassDescriptor cd = (ClassDescriptor) iterator.next();
104 SSJavaLattice<String> lattice = ssjava.getCd2lattice().get(cd);
105 Set<String> fieldLocNameSet = lattice.getKeySet();
107 for (Iterator iterator2 = fieldLocNameSet.iterator(); iterator2.hasNext();) {
108 String fieldLocName = (String) iterator2.next();
109 fieldLocName2cd.put(fieldLocName, cd);
116 public boolean toAnalyzeIsEmpty() {
118 return toanalyzeList.isEmpty();
120 return toanalyze.isEmpty();
124 public ClassDescriptor toAnalyzeNext() {
126 return toanalyzeList.remove(0);
128 ClassDescriptor cd = toanalyze.iterator().next();
129 toanalyze.remove(cd);
134 public void setupToAnalyze() {
135 SymbolTable classtable = state.getClassSymbolTable();
137 toanalyzeList.clear();
138 toanalyzeList.addAll(classtable.getValueSet());
139 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
140 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
141 return o1.getClassName().compareToIgnoreCase(o2.getClassName());
146 toanalyze.addAll(classtable.getValueSet());
150 public void setupToAnalazeMethod(ClassDescriptor cd) {
152 SymbolTable methodtable = cd.getMethodTable();
154 toanalyzeMethodList.clear();
155 toanalyzeMethodList.addAll(methodtable.getValueSet());
156 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
157 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
158 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
162 toanalyzeMethod.clear();
163 toanalyzeMethod.addAll(methodtable.getValueSet());
167 public boolean toAnalyzeMethodIsEmpty() {
169 return toanalyzeMethodList.isEmpty();
171 return toanalyzeMethod.isEmpty();
175 public MethodDescriptor toAnalyzeMethodNext() {
177 return toanalyzeMethodList.remove(0);
179 MethodDescriptor md = toanalyzeMethod.iterator().next();
180 toanalyzeMethod.remove(md);
185 public void flowDownCheck() {
187 // phase 1 : checking declaration node and creating mapping of 'type
188 // desciptor' & 'location'
191 while (!toAnalyzeIsEmpty()) {
192 ClassDescriptor cd = toAnalyzeNext();
194 if (ssjava.needToBeAnnoated(cd)) {
196 ClassDescriptor superDesc = cd.getSuperDesc();
198 if (superDesc != null && (!superDesc.getSymbol().equals("Object"))) {
199 checkOrderingInheritance(superDesc, cd);
202 checkDeclarationInClass(cd);
204 setupToAnalazeMethod(cd);
205 while (!toAnalyzeMethodIsEmpty()) {
206 MethodDescriptor md = toAnalyzeMethodNext();
207 if (ssjava.needTobeAnnotated(md)) {
208 checkDeclarationInMethodBody(cd, md);
216 // phase2 : checking assignments
219 while (!toAnalyzeIsEmpty()) {
220 ClassDescriptor cd = toAnalyzeNext();
222 setupToAnalazeMethod(cd);
223 while (!toAnalyzeMethodIsEmpty()) {
224 MethodDescriptor md = toAnalyzeMethodNext();
225 if (ssjava.needTobeAnnotated(md)) {
226 if (state.SSJAVADEBUG) {
227 System.out.println("SSJAVA: Checking Flow-down Rules: " + md);
229 CompositeLocation calleePCLOC = ssjava.getPCLocation(md);
230 checkMethodBody(cd, md, calleePCLOC);
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);
324 } else if (an.getMarker().equals(ssjava.GLOBALLOC)) {
325 String globalLoc = an.getValue();
326 ssjava.getMethodLattice(md).setGlobalLoc(globalLoc);
327 } else if (an.getMarker().equals(ssjava.PCLOC)) {
328 String pcLocDeclaration = an.getValue();
329 ssjava.setPCLocation(md, parseLocationDeclaration(md, null, pcLocDeclaration));
334 // second, check return location annotation
335 if (!md.getReturnType().isVoid()) {
336 if (!hasReturnLocDeclaration) {
337 // if developer does not define method lattice
338 // search return location in the method default lattice
339 String rtrStr = ssjava.getMethodLattice(md).getReturnLoc();
340 if (rtrStr != null) {
341 returnLocComp = new CompositeLocation(new Location(md, rtrStr));
345 if (returnLocComp == null) {
346 throw new Error("Return location is not specified for the method " + md + " at "
347 + cd.getSourceFileName());
350 md2ReturnLoc.put(md, returnLocComp);
354 if (!md.getReturnType().isVoid()) {
355 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
356 String thisLocId = methodLattice.getThisLoc();
357 if ((!md.isStatic()) && thisLocId == null) {
358 throw new Error("Method '" + md + "' does not have the definition of 'this' location at "
359 + md.getClassDesc().getSourceFileName());
361 CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
362 paramList.add(0, thisLoc);
363 md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), md, paramList, md
364 + " of " + cd.getSourceFileName()));
367 // fourth, check declarations inside of method
369 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
373 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
374 bn.getVarTable().setParent(nametable);
375 for (int i = 0; i < bn.size(); i++) {
376 BlockStatementNode bsn = bn.get(i);
377 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
381 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
382 BlockStatementNode bsn) {
384 switch (bsn.kind()) {
385 case Kind.SubBlockNode:
386 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
389 case Kind.DeclarationNode:
390 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
394 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
397 case Kind.IfStatementNode:
398 checkDeclarationInIfStatementNode(md, nametable, (IfStatementNode) bsn);
401 case Kind.SwitchStatementNode:
402 checkDeclarationInSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
405 case Kind.SynchronizedNode:
406 checkDeclarationInSynchronizedNode(md, nametable, (SynchronizedNode) bsn);
412 private void checkDeclarationInSynchronizedNode(MethodDescriptor md, SymbolTable nametable,
413 SynchronizedNode sbn) {
414 checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
417 private void checkDeclarationInSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
418 SwitchStatementNode ssn) {
419 BlockNode sbn = ssn.getSwitchBody();
420 for (int i = 0; i < sbn.size(); i++) {
421 SwitchBlockNode node = (SwitchBlockNode) sbn.get(i);
422 checkDeclarationInBlockNode(md, nametable, node.getSwitchBlockStatement());
426 private void checkDeclarationInIfStatementNode(MethodDescriptor md, SymbolTable nametable,
427 IfStatementNode isn) {
428 checkDeclarationInBlockNode(md, nametable, isn.getTrueBlock());
429 if (isn.getFalseBlock() != null)
430 checkDeclarationInBlockNode(md, nametable, isn.getFalseBlock());
433 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
435 if (ln.getType() == LoopNode.FORLOOP) {
436 // check for loop case
437 ClassDescriptor cd = md.getClassDesc();
438 BlockNode bn = ln.getInitializer();
439 for (int i = 0; i < bn.size(); i++) {
440 BlockStatementNode bsn = bn.get(i);
441 checkDeclarationInBlockStatementNode(md, nametable, bsn);
446 checkDeclarationInBlockNode(md, nametable, ln.getBody());
449 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md,
450 CompositeLocation constraints) {
451 BlockNode bn = state.getMethodBody(md);
452 checkLocationFromBlockNode(md, md.getParameterTable(), bn, constraints);
455 private String generateErrorMessage(ClassDescriptor cd, TreeNode tn) {
457 return cd.getSourceFileName() + "::" + tn.getNumLine();
459 return cd.getSourceFileName();
464 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
465 BlockNode bn, CompositeLocation constraint) {
467 bn.getVarTable().setParent(nametable);
468 for (int i = 0; i < bn.size(); i++) {
469 BlockStatementNode bsn = bn.get(i);
470 checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn, constraint);
472 return new CompositeLocation();
476 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
477 SymbolTable nametable, BlockStatementNode bsn, CompositeLocation constraint) {
479 CompositeLocation compLoc = null;
480 switch (bsn.kind()) {
481 case Kind.BlockExpressionNode:
483 checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, constraint);
486 case Kind.DeclarationNode:
487 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn, constraint);
490 case Kind.IfStatementNode:
491 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn, constraint);
495 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn, constraint);
498 case Kind.ReturnNode:
499 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn, constraint);
502 case Kind.SubBlockNode:
503 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn, constraint);
506 case Kind.ContinueBreakNode:
507 compLoc = new CompositeLocation();
510 case Kind.SwitchStatementNode:
512 checkLocationFromSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, constraint);
518 private CompositeLocation checkLocationFromSwitchStatementNode(MethodDescriptor md,
519 SymbolTable nametable, SwitchStatementNode ssn, CompositeLocation constraint) {
521 ClassDescriptor cd = md.getClassDesc();
522 CompositeLocation condLoc =
523 checkLocationFromExpressionNode(md, nametable, ssn.getCondition(), new CompositeLocation(),
525 BlockNode sbn = ssn.getSwitchBody();
527 constraint = generateNewConstraint(constraint, condLoc);
529 for (int i = 0; i < sbn.size(); i++) {
530 checkLocationFromSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), constraint);
532 return new CompositeLocation();
535 private CompositeLocation checkLocationFromSwitchBlockNode(MethodDescriptor md,
536 SymbolTable nametable, SwitchBlockNode sbn, CompositeLocation constraint) {
538 CompositeLocation blockLoc =
539 checkLocationFromBlockNode(md, nametable, sbn.getSwitchBlockStatement(), constraint);
545 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
546 ReturnNode rn, CompositeLocation constraint) {
548 ExpressionNode returnExp = rn.getReturnExpression();
550 CompositeLocation returnValueLoc;
551 if (returnExp != null) {
553 checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation(),
556 // System.out.println("# RETURN VALUE LOC=" + returnValueLoc +
557 // " with constraint=" + constraint);
559 // TODO: do we need to check here?
560 // if this return statement is inside branch, return value has an implicit
561 // flow from conditional location
562 // if (constraint != null) {
563 // Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
564 // inputGLB.add(returnValueLoc);
565 // inputGLB.add(constraint);
567 // CompositeLattice.calculateGLB(inputGLB,
568 // generateErrorMessage(md.getClassDesc(), rn));
571 // check if return value is equal or higher than RETRUNLOC of method
572 // declaration annotation
573 CompositeLocation declaredReturnLoc = md2ReturnLoc.get(md);
576 CompositeLattice.compare(returnValueLoc, declaredReturnLoc, false,
577 generateErrorMessage(md.getClassDesc(), rn));
579 if (compareResult == ComparisonResult.LESS || compareResult == ComparisonResult.INCOMPARABLE) {
581 "Return value location is not equal or higher than the declaraed return location at "
582 + md.getClassDesc().getSourceFileName() + "::" + rn.getNumLine());
586 return new CompositeLocation();
589 private boolean hasOnlyLiteralValue(ExpressionNode en) {
590 if (en.kind() == Kind.LiteralNode) {
597 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
598 LoopNode ln, CompositeLocation constraint) {
600 ClassDescriptor cd = md.getClassDesc();
601 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
603 CompositeLocation condLoc =
604 checkLocationFromExpressionNode(md, nametable, ln.getCondition(),
605 new CompositeLocation(), constraint, false);
606 // addLocationType(ln.getCondition().getType(), (condLoc));
608 constraint = generateNewConstraint(constraint, condLoc);
609 checkLocationFromBlockNode(md, nametable, ln.getBody(), constraint);
611 return new CompositeLocation();
614 // check 'for loop' case
615 BlockNode bn = ln.getInitializer();
616 bn.getVarTable().setParent(nametable);
617 // need to check initialization node
618 // checkLocationFromBlockNode(md, bn.getVarTable(), bn, constraint);
619 for (int i = 0; i < bn.size(); i++) {
620 BlockStatementNode bsn = bn.get(i);
621 checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn, constraint);
624 // calculate glb location of condition and update statements
625 CompositeLocation condLoc =
626 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
627 new CompositeLocation(), constraint, false);
628 // addLocationType(ln.getCondition().getType(), condLoc);
630 constraint = generateNewConstraint(constraint, condLoc);
632 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate(), constraint);
633 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody(), constraint);
635 return new CompositeLocation();
641 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
642 SymbolTable nametable, SubBlockNode sbn, CompositeLocation constraint) {
643 CompositeLocation compLoc =
644 checkLocationFromBlockNode(md, nametable, sbn.getBlockNode(), constraint);
648 private CompositeLocation generateNewConstraint(CompositeLocation currentCon,
649 CompositeLocation newCon) {
651 if (currentCon == null) {
654 // compute GLB of current constraint and new constraint
655 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
656 inputSet.add(currentCon);
657 inputSet.add(newCon);
658 return CompositeLattice.calculateGLB(inputSet, "");
663 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
664 SymbolTable nametable, IfStatementNode isn, CompositeLocation constraint) {
666 CompositeLocation condLoc =
667 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(),
670 // addLocationType(isn.getCondition().getType(), condLoc);
672 constraint = generateNewConstraint(constraint, condLoc);
673 checkLocationFromBlockNode(md, nametable, isn.getTrueBlock(), constraint);
675 if (isn.getFalseBlock() != null) {
676 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock(), constraint);
679 return new CompositeLocation();
682 private void checkOwnership(MethodDescriptor md, TreeNode tn, ExpressionNode srcExpNode) {
684 if (srcExpNode.kind() == Kind.NameNode || srcExpNode.kind() == Kind.FieldAccessNode) {
685 if (srcExpNode.getType().isPtr() && !srcExpNode.getType().isNull()) {
686 // first, check the linear type
687 // RHS reference should be owned by the current method
688 FieldDescriptor fd = getFieldDescriptorFromExpressionNode(srcExpNode);
692 isOwned = ((SSJavaType) srcExpNode.getType().getExtension()).isOwned();
695 isOwned = ssjava.isOwnedByMethod(md, fd);
699 "It is not allowed to create the reference alias from the reference not owned by the method at "
700 + generateErrorMessage(md.getClassDesc(), tn));
708 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
709 SymbolTable nametable, DeclarationNode dn, CompositeLocation constraint) {
711 VarDescriptor vd = dn.getVarDescriptor();
713 CompositeLocation destLoc = d2loc.get(vd);
715 if (dn.getExpression() != null) {
717 checkOwnership(md, dn, dn.getExpression());
719 CompositeLocation expressionLoc =
720 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
721 new CompositeLocation(), constraint, false);
722 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
724 if (expressionLoc != null) {
726 // checking location order
727 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc,
728 generateErrorMessage(md.getClassDesc(), dn))) {
729 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
730 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
731 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
734 return expressionLoc;
738 return new CompositeLocation();
744 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
746 checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
749 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
750 SymbolTable nametable, BlockExpressionNode ben, CompositeLocation constraint) {
752 CompositeLocation compLoc =
753 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null, constraint, false);
754 // addTypeLocation(ben.getExpression().getType(), compLoc);
758 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
759 SymbolTable nametable, ExpressionNode en, CompositeLocation loc,
760 CompositeLocation constraint, boolean isLHS) {
762 CompositeLocation compLoc = null;
765 case Kind.AssignmentNode:
767 checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc, constraint);
770 case Kind.FieldAccessNode:
772 checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc, constraint);
776 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc, constraint);
780 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en, constraint);
783 case Kind.CreateObjectNode:
784 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
787 case Kind.ArrayAccessNode:
789 checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en, constraint, isLHS);
792 case Kind.LiteralNode:
793 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
796 case Kind.MethodInvokeNode:
798 checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc, constraint);
801 case Kind.TertiaryNode:
802 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en, constraint);
806 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en, constraint);
809 // case Kind.InstanceOfNode:
810 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
813 // case Kind.ArrayInitializerNode:
814 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
818 // case Kind.ClassTypeNode:
819 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
822 // case Kind.OffsetNode:
823 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
830 // addTypeLocation(en.getType(), compLoc);
835 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
836 CastNode cn, CompositeLocation constraint) {
838 ExpressionNode en = cn.getExpression();
839 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
844 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
845 SymbolTable nametable, TertiaryNode tn, CompositeLocation constraint) {
846 ClassDescriptor cd = md.getClassDesc();
848 CompositeLocation condLoc =
849 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(),
851 // addLocationType(tn.getCond().getType(), condLoc);
852 CompositeLocation trueLoc =
853 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(),
855 // addLocationType(tn.getTrueExpr().getType(), trueLoc);
856 CompositeLocation falseLoc =
857 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(),
859 // addLocationType(tn.getFalseExpr().getType(), falseLoc);
861 // locations from true/false branches can be TOP when there are only literal
863 // in this case, we don't need to check flow down rule!
865 // System.out.println("\n#tertiary cond=" + tn.getCond().printNode(0) +
866 // " Loc=" + condLoc);
867 // System.out.println("# true=" + tn.getTrueExpr().printNode(0) + " Loc=" +
869 // System.out.println("# false=" + tn.getFalseExpr().printNode(0) + " Loc="
872 // check if condLoc is higher than trueLoc & falseLoc
873 if (!trueLoc.get(0).isTop()
874 && !CompositeLattice.isGreaterThan(condLoc, trueLoc, generateErrorMessage(cd, tn))) {
876 "The location of the condition expression is lower than the true expression at "
877 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
880 if (!falseLoc.get(0).isTop()
881 && !CompositeLattice.isGreaterThan(condLoc, falseLoc,
882 generateErrorMessage(cd, tn.getCond()))) {
884 "The location of the condition expression is lower than the false expression at "
885 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
888 // then, return glb of trueLoc & falseLoc
889 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
890 glbInputSet.add(trueLoc);
891 glbInputSet.add(falseLoc);
893 if (glbInputSet.size() == 1) {
896 return CompositeLattice.calculateGLB(glbInputSet, generateErrorMessage(cd, tn));
901 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
902 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc,
903 CompositeLocation constraint) {
905 ClassDescriptor cd = md.getClassDesc();
906 MethodDescriptor calleeMethodDesc = min.getMethod();
908 NameDescriptor baseName = min.getBaseName();
909 boolean isSystemout = false;
910 if (baseName != null) {
911 isSystemout = baseName.getSymbol().equals("System.out");
914 if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
915 && !ssjava.isTrustMethod(calleeMethodDesc) && !calleeMethodDesc.getModifiers().isNative()
918 CompositeLocation baseLocation = null;
919 if (min.getExpression() != null) {
921 checkLocationFromExpressionNode(md, nametable, min.getExpression(),
922 new CompositeLocation(), constraint, false);
924 if (min.getMethod().isStatic()) {
925 String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
926 if (globalLocId == null) {
927 throw new Error("Method lattice does not define global variable location at "
928 + generateErrorMessage(md.getClassDesc(), min));
930 baseLocation = new CompositeLocation(new Location(md, globalLocId));
932 String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
933 baseLocation = new CompositeLocation(new Location(md, thisLocId));
937 // System.out.println("\n#checkLocationFromMethodInvokeNode=" +
939 // + " baseLocation=" + baseLocation + " constraint=" + constraint);
941 // setup the location list of caller's arguments
942 List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
944 // setup the location list of callee's parameters
945 MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleeMethodDesc);
946 List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
948 if (min.numArgs() > 0) {
949 if (!calleeMethodDesc.isStatic()) {
950 callerArgList.add(baseLocation);
952 for (int i = 0; i < min.numArgs(); i++) {
953 ExpressionNode en = min.getArg(i);
954 CompositeLocation callerArgLoc =
955 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(),
957 callerArgList.add(callerArgLoc);
960 if (!calleeMethodDesc.isStatic()) {
961 CompositeLocation calleeThisLoc =
962 new CompositeLocation(new Location(calleeMethodDesc, calleeLattice.getThisLoc()));
963 calleeParamList.add(calleeThisLoc);
966 for (int i = 0; i < calleeMethodDesc.numParameters(); i++) {
967 VarDescriptor calleevd = (VarDescriptor) calleeMethodDesc.getParameter(i);
968 CompositeLocation calleeLoc = d2loc.get(calleevd);
969 calleeParamList.add(calleeLoc);
973 if (constraint != null) {
974 // check whether the PC location is lower than one of the
975 // argument locations. If it is lower, the callee has to have @PCLOC
976 // annotation that declares the program counter that is higher than
977 // corresponding parameter
979 CompositeLocation calleePCLOC = ssjava.getPCLocation(calleeMethodDesc);
981 for (int idx = 0; idx < callerArgList.size(); idx++) {
982 CompositeLocation argLocation = callerArgList.get(idx);
984 // need to check that param is higher than PCLOC
985 if (!argLocation.get(0).isTop()
986 && CompositeLattice.compare(argLocation, constraint, true,
987 generateErrorMessage(cd, min)) == ComparisonResult.GREATER) {
989 CompositeLocation paramLocation = calleeParamList.get(idx);
991 int paramCompareResult =
992 CompositeLattice.compare(calleePCLOC, paramLocation, true,
993 generateErrorMessage(cd, min));
995 if (paramCompareResult == ComparisonResult.GREATER) {
997 "The program counter location "
999 + " is lower than the argument(idx="
1003 + ". Need to specify that the initial PC location of the callee, which is currently set to "
1004 + calleePCLOC + ", is lower than " + paramLocation + " in the method "
1005 + calleeMethodDesc.getSymbol() + ":" + min.getNumLine());
1014 checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
1016 // checkCallerArgumentLocationConstraints(md, nametable, min,
1017 // baseLocation, constraint);
1019 if (!min.getMethod().getReturnType().isVoid()) {
1020 // If method has a return value, compute the highest possible return
1021 // location in the caller's perspective
1022 CompositeLocation ceilingLoc =
1023 computeCeilingLocationForCaller(md, nametable, min, baseLocation, constraint);
1028 return new CompositeLocation(Location.createTopLocation(md));
1032 private CompositeLocation translateCallerLocToCalleeLoc(MethodDescriptor calleeMD,
1033 CompositeLocation calleeBaseLoc, CompositeLocation constraint) {
1035 CompositeLocation calleeConstraint = new CompositeLocation();
1037 // if (constraint.startsWith(calleeBaseLoc)) {
1038 // if the first part of constraint loc is matched with callee base loc
1039 Location thisLoc = new Location(calleeMD, ssjava.getMethodLattice(calleeMD).getThisLoc());
1040 calleeConstraint.addLocation(thisLoc);
1041 for (int i = calleeBaseLoc.getSize(); i < constraint.getSize(); i++) {
1042 calleeConstraint.addLocation(constraint.get(i));
1047 return calleeConstraint;
1050 private void checkCallerArgumentLocationConstraints(MethodDescriptor md, SymbolTable nametable,
1051 MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
1052 // if parameter location consists of THIS and FIELD location,
1053 // caller should pass an argument that is comparable to the declared
1054 // parameter location
1055 // and is not lower than the declared parameter location in the field
1058 MethodDescriptor calleemd = min.getMethod();
1060 List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
1061 List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
1063 MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
1064 Location calleeThisLoc = new Location(calleemd, calleeLattice.getThisLoc());
1066 for (int i = 0; i < min.numArgs(); i++) {
1067 ExpressionNode en = min.getArg(i);
1068 CompositeLocation callerArgLoc =
1069 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1071 callerArgList.add(callerArgLoc);
1074 // setup callee params set
1075 for (int i = 0; i < calleemd.numParameters(); i++) {
1076 VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
1077 CompositeLocation calleeLoc = d2loc.get(calleevd);
1078 calleeParamList.add(calleeLoc);
1081 String errorMsg = generateErrorMessage(md.getClassDesc(), min);
1083 // System.out.println("checkCallerArgumentLocationConstraints=" +
1084 // min.printNode(0));
1085 // System.out.println("base location=" + callerBaseLoc + " constraint=" +
1088 for (int i = 0; i < calleeParamList.size(); i++) {
1089 CompositeLocation calleeParamLoc = calleeParamList.get(i);
1090 if (calleeParamLoc.get(0).equals(calleeThisLoc) && calleeParamLoc.getSize() > 1) {
1092 // callee parameter location has field information
1093 CompositeLocation callerArgLoc = callerArgList.get(i);
1095 CompositeLocation paramLocation =
1096 translateCalleeParamLocToCaller(md, calleeParamLoc, callerBaseLoc, errorMsg);
1098 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
1099 if (constraint != null) {
1100 inputGLBSet.add(callerArgLoc);
1101 inputGLBSet.add(constraint);
1103 CompositeLattice.calculateGLB(inputGLBSet,
1104 generateErrorMessage(md.getClassDesc(), min));
1107 if (!CompositeLattice.isGreaterThan(callerArgLoc, paramLocation, errorMsg)) {
1108 throw new Error("Caller argument '" + min.getArg(i).printNode(0) + " : " + callerArgLoc
1109 + "' should be higher than corresponding callee's parameter : " + paramLocation
1110 + " at " + errorMsg);
1118 private CompositeLocation translateCalleeParamLocToCaller(MethodDescriptor md,
1119 CompositeLocation calleeParamLoc, CompositeLocation callerBaseLocation, String errorMsg) {
1121 CompositeLocation translate = new CompositeLocation();
1123 for (int i = 0; i < callerBaseLocation.getSize(); i++) {
1124 translate.addLocation(callerBaseLocation.get(i));
1127 for (int i = 1; i < calleeParamLoc.getSize(); i++) {
1128 translate.addLocation(calleeParamLoc.get(i));
1131 // System.out.println("TRANSLATED=" + translate + " from calleeParamLoc=" +
1137 private CompositeLocation computeCeilingLocationForCaller(MethodDescriptor md,
1138 SymbolTable nametable, MethodInvokeNode min, CompositeLocation baseLocation,
1139 CompositeLocation constraint) {
1140 List<CompositeLocation> argList = new ArrayList<CompositeLocation>();
1142 // by default, method has a THIS parameter
1143 argList.add(baseLocation);
1145 for (int i = 0; i < min.numArgs(); i++) {
1146 ExpressionNode en = min.getArg(i);
1147 CompositeLocation callerArg =
1148 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1150 argList.add(callerArg);
1153 // System.out.println("\n## computeReturnLocation=" + min.getMethod() +
1154 // " argList=" + argList);
1155 CompositeLocation ceilLoc = md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
1156 // System.out.println("## ReturnLocation=" + ceilLoc);
1162 private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
1163 MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
1165 // System.out.println("checkCalleeConstraints=" + min.printNode(0));
1167 MethodDescriptor calleemd = min.getMethod();
1169 MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
1170 CompositeLocation calleeThisLoc =
1171 new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
1173 List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
1174 List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
1176 if (min.numArgs() > 0) {
1177 // caller needs to guarantee that it passes arguments in regarding to
1178 // callee's hierarchy
1180 // setup caller args set
1181 // first, add caller's base(this) location
1182 callerArgList.add(callerBaseLoc);
1183 // second, add caller's arguments
1184 for (int i = 0; i < min.numArgs(); i++) {
1185 ExpressionNode en = min.getArg(i);
1186 CompositeLocation callerArgLoc =
1187 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1189 callerArgList.add(callerArgLoc);
1192 // setup callee params set
1193 // first, add callee's this location
1194 calleeParamList.add(calleeThisLoc);
1195 // second, add callee's parameters
1196 for (int i = 0; i < calleemd.numParameters(); i++) {
1197 VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
1198 CompositeLocation calleeLoc = d2loc.get(calleevd);
1199 // System.out.println("calleevd=" + calleevd + " loc=" + calleeLoc);
1200 calleeParamList.add(calleeLoc);
1203 // here, check if ordering relations among caller's args respect
1204 // ordering relations in-between callee's args
1205 CHECK: for (int i = 0; i < calleeParamList.size(); i++) {
1206 CompositeLocation calleeLoc1 = calleeParamList.get(i);
1207 CompositeLocation callerLoc1 = callerArgList.get(i);
1209 for (int j = 0; j < calleeParamList.size(); j++) {
1211 CompositeLocation calleeLoc2 = calleeParamList.get(j);
1212 CompositeLocation callerLoc2 = callerArgList.get(j);
1214 if (callerLoc1.get(callerLoc1.getSize() - 1).isTop()
1215 || callerLoc2.get(callerLoc2.getSize() - 1).isTop()) {
1219 // System.out.println("calleeLoc1=" + calleeLoc1);
1220 // System.out.println("calleeLoc2=" + calleeLoc2 +
1221 // "calleeParamList=" + calleeParamList);
1224 CompositeLattice.compare(callerLoc1, callerLoc2, true,
1225 generateErrorMessage(md.getClassDesc(), min));
1226 // System.out.println("callerResult=" + callerResult);
1228 CompositeLattice.compare(calleeLoc1, calleeLoc2, true,
1229 generateErrorMessage(md.getClassDesc(), min));
1230 // System.out.println("calleeResult=" + calleeResult);
1232 if (callerResult == ComparisonResult.EQUAL) {
1233 if (ssjava.isSharedLocation(callerLoc1.get(callerLoc1.getSize() - 1))
1234 && ssjava.isSharedLocation(callerLoc2.get(callerLoc2.getSize() - 1))) {
1235 // if both of them are shared locations, promote them to
1236 // "GREATER relation"
1237 callerResult = ComparisonResult.GREATER;
1241 if (calleeResult == ComparisonResult.GREATER
1242 && callerResult != ComparisonResult.GREATER) {
1243 // If calleeLoc1 is higher than calleeLoc2
1244 // then, caller should have same ordering relation in-bet
1245 // callerLoc1 & callerLoc2
1247 String paramName1, paramName2;
1250 paramName1 = "'THIS'";
1252 paramName1 = "'parameter " + calleemd.getParamName(i - 1) + "'";
1256 paramName2 = "'THIS'";
1258 paramName2 = "'parameter " + calleemd.getParamName(j - 1) + "'";
1262 "Caller doesn't respect an ordering relation among method arguments: callee expects that "
1263 + paramName1 + " should be higher than " + paramName2 + " in " + calleemd
1264 + " at " + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
1275 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
1276 SymbolTable nametable, ArrayAccessNode aan, CompositeLocation constraint, boolean isLHS) {
1278 ClassDescriptor cd = md.getClassDesc();
1280 CompositeLocation arrayLoc =
1281 checkLocationFromExpressionNode(md, nametable, aan.getExpression(),
1282 new CompositeLocation(), constraint, isLHS);
1284 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
1285 CompositeLocation indexLoc =
1286 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(),
1288 // addTypeLocation(aan.getIndex().getType(), indexLoc);
1291 if (!CompositeLattice.isGreaterThan(indexLoc, arrayLoc, generateErrorMessage(cd, aan))) {
1292 throw new Error("Array index value is not higher than array location at "
1293 + generateErrorMessage(cd, aan));
1297 Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
1298 inputGLB.add(arrayLoc);
1299 inputGLB.add(indexLoc);
1300 return CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(cd, aan));
1305 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
1306 SymbolTable nametable, CreateObjectNode con) {
1308 ClassDescriptor cd = md.getClassDesc();
1310 CompositeLocation compLoc = new CompositeLocation();
1311 compLoc.addLocation(Location.createTopLocation(md));
1316 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
1317 OpNode on, CompositeLocation constraint) {
1319 ClassDescriptor cd = md.getClassDesc();
1320 CompositeLocation leftLoc = new CompositeLocation();
1322 checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc, constraint, false);
1323 // addTypeLocation(on.getLeft().getType(), leftLoc);
1325 CompositeLocation rightLoc = new CompositeLocation();
1326 if (on.getRight() != null) {
1328 checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc, constraint, false);
1329 // addTypeLocation(on.getRight().getType(), rightLoc);
1332 // System.out.println("\n# OP NODE=" + on.printNode(0));
1333 // System.out.println("# left loc=" + leftLoc + " from " +
1334 // on.getLeft().getClass());
1335 // if (on.getRight() != null) {
1336 // System.out.println("# right loc=" + rightLoc + " from " +
1337 // on.getRight().getClass());
1340 Operation op = on.getOp();
1342 switch (op.getOp()) {
1344 case Operation.UNARYPLUS:
1345 case Operation.UNARYMINUS:
1346 case Operation.LOGIC_NOT:
1350 case Operation.LOGIC_OR:
1351 case Operation.LOGIC_AND:
1352 case Operation.COMP:
1353 case Operation.BIT_OR:
1354 case Operation.BIT_XOR:
1355 case Operation.BIT_AND:
1356 case Operation.ISAVAILABLE:
1357 case Operation.EQUAL:
1358 case Operation.NOTEQUAL:
1365 case Operation.MULT:
1368 case Operation.LEFTSHIFT:
1369 case Operation.RIGHTSHIFT:
1370 case Operation.URIGHTSHIFT:
1372 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
1373 inputSet.add(leftLoc);
1374 inputSet.add(rightLoc);
1375 CompositeLocation glbCompLoc =
1376 CompositeLattice.calculateGLB(inputSet, generateErrorMessage(cd, on));
1380 throw new Error(op.toString());
1385 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
1386 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
1388 // literal value has the top location so that value can be flowed into any
1390 Location literalLoc = Location.createTopLocation(md);
1391 loc.addLocation(literalLoc);
1396 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
1397 NameNode nn, CompositeLocation loc, CompositeLocation constraint) {
1399 NameDescriptor nd = nn.getName();
1400 if (nd.getBase() != null) {
1402 checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc, constraint, false);
1404 String varname = nd.toString();
1405 if (varname.equals("this")) {
1407 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
1408 String thisLocId = methodLattice.getThisLoc();
1409 if (thisLocId == null) {
1410 throw new Error("The location for 'this' is not defined at "
1411 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
1413 Location locElement = new Location(md, thisLocId);
1414 loc.addLocation(locElement);
1419 Descriptor d = (Descriptor) nametable.get(varname);
1421 // CompositeLocation localLoc = null;
1422 if (d instanceof VarDescriptor) {
1423 VarDescriptor vd = (VarDescriptor) d;
1424 // localLoc = d2loc.get(vd);
1425 // the type of var descriptor has a composite location!
1426 loc = ((SSJavaType) vd.getType().getExtension()).getCompLoc().clone();
1427 } else if (d instanceof FieldDescriptor) {
1428 // the type of field descriptor has a location!
1429 FieldDescriptor fd = (FieldDescriptor) d;
1430 if (fd.isStatic()) {
1432 // if it is 'static final', the location has TOP since no one can
1434 loc.addLocation(Location.createTopLocation(md));
1437 // if 'static', the location has pre-assigned global loc
1438 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1439 String globalLocId = localLattice.getGlobalLoc();
1440 if (globalLocId == null) {
1441 throw new Error("Global location element is not defined in the method " + md);
1443 Location globalLoc = new Location(md, globalLocId);
1445 loc.addLocation(globalLoc);
1448 // the location of field access starts from this, followed by field
1450 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1451 Location thisLoc = new Location(md, localLattice.getThisLoc());
1452 loc.addLocation(thisLoc);
1455 Location fieldLoc = (Location) fd.getType().getExtension();
1456 loc.addLocation(fieldLoc);
1457 } else if (d == null) {
1458 // access static field
1459 FieldDescriptor fd = nn.getField();
1461 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1462 String globalLocId = localLattice.getGlobalLoc();
1463 if (globalLocId == null) {
1464 throw new Error("Method lattice does not define global variable location at "
1465 + generateErrorMessage(md.getClassDesc(), nn));
1467 loc.addLocation(new Location(md, globalLocId));
1469 Location fieldLoc = (Location) fd.getType().getExtension();
1470 loc.addLocation(fieldLoc);
1479 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
1480 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc,
1481 CompositeLocation constraint) {
1483 ExpressionNode left = fan.getExpression();
1484 TypeDescriptor ltd = left.getType();
1486 FieldDescriptor fd = fan.getField();
1488 String varName = null;
1489 if (left.kind() == Kind.NameNode) {
1490 NameDescriptor nd = ((NameNode) left).getName();
1491 varName = nd.toString();
1494 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
1495 // using a class name directly or access using this
1496 if (fd.isStatic() && fd.isFinal()) {
1497 loc.addLocation(Location.createTopLocation(md));
1502 if (left instanceof ArrayAccessNode) {
1503 ArrayAccessNode aan = (ArrayAccessNode) left;
1504 left = aan.getExpression();
1507 loc = checkLocationFromExpressionNode(md, nametable, left, loc, constraint, false);
1508 // System.out.println("### checkLocationFromFieldAccessNode=" +
1509 // fan.printNode(0));
1510 // System.out.println("### left=" + left.printNode(0));
1512 if (!left.getType().isPrimitive()) {
1514 if (fd.getSymbol().equals("length")) {
1515 // array.length access, return the location of the array
1519 Location fieldLoc = getFieldLocation(fd);
1520 loc.addLocation(fieldLoc);
1525 private Location getFieldLocation(FieldDescriptor fd) {
1527 // System.out.println("### getFieldLocation=" + fd);
1528 // System.out.println("### fd.getType().getExtension()=" +
1529 // fd.getType().getExtension());
1531 Location fieldLoc = (Location) fd.getType().getExtension();
1533 // handle the case that method annotation checking skips checking field
1535 if (fieldLoc == null) {
1536 fieldLoc = checkFieldDeclaration(fd.getClassDescriptor(), fd);
1543 private FieldDescriptor getFieldDescriptorFromExpressionNode(ExpressionNode en) {
1545 if (en.kind() == Kind.NameNode) {
1546 NameNode nn = (NameNode) en;
1547 if (nn.getField() != null) {
1548 return nn.getField();
1551 if (nn.getName() != null && nn.getName().getBase() != null) {
1552 return getFieldDescriptorFromExpressionNode(nn.getExpression());
1555 } else if (en.kind() == Kind.FieldAccessNode) {
1556 FieldAccessNode fan = (FieldAccessNode) en;
1557 return fan.getField();
1563 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
1564 SymbolTable nametable, AssignmentNode an, CompositeLocation loc, CompositeLocation constraint) {
1566 ClassDescriptor cd = md.getClassDesc();
1568 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
1570 boolean postinc = true;
1571 if (an.getOperation().getBaseOp() == null
1572 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
1573 .getBaseOp().getOp() != Operation.POSTDEC))
1576 // if LHS is array access node, need to check if array index is higher
1577 // than array itself
1578 CompositeLocation destLocation =
1579 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(),
1582 CompositeLocation rhsLocation;
1583 CompositeLocation srcLocation;
1587 checkOwnership(md, an, an.getSrc());
1590 checkLocationFromExpressionNode(md, nametable, an.getSrc(), new CompositeLocation(),
1593 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
1594 // if assignment contains OP+EQ operator, need to merge location types
1595 // of LHS & RHS into the RHS
1596 Set<CompositeLocation> srcGLBSet = new HashSet<CompositeLocation>();
1597 srcGLBSet.add(rhsLocation);
1598 srcGLBSet.add(destLocation);
1599 srcLocation = CompositeLattice.calculateGLB(srcGLBSet, generateErrorMessage(cd, an));
1601 srcLocation = rhsLocation;
1604 if (constraint != null) {
1605 inputGLBSet.add(srcLocation);
1606 inputGLBSet.add(constraint);
1607 srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
1610 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
1612 String context = "";
1613 if (constraint != null) {
1614 context = " and the current context constraint is " + constraint;
1617 throw new Error("The value flow from " + srcLocation + " to " + destLocation
1618 + " does not respect location hierarchy on the assignment " + an.printNode(0) + context
1619 + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1622 if (srcLocation.equals(destLocation)) {
1623 // keep it for definitely written analysis
1624 Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
1625 for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
1626 FlatNode fn = (FlatNode) iterator.next();
1627 ssjava.addSameHeightWriteFlatNode(fn);
1635 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(),
1638 if (constraint != null) {
1639 inputGLBSet.add(rhsLocation);
1640 inputGLBSet.add(constraint);
1641 srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
1643 srcLocation = rhsLocation;
1646 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
1648 if (srcLocation.equals(destLocation)) {
1649 throw new Error("Location " + srcLocation
1650 + " is not allowed to have the value flow that moves within the same location at '"
1651 + an.printNode(0) + "' of " + cd.getSourceFileName() + "::" + an.getNumLine());
1653 throw new Error("The value flow from " + srcLocation + " to " + destLocation
1654 + " does not respect location hierarchy on the assignment " + an.printNode(0)
1655 + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1660 if (srcLocation.equals(destLocation)) {
1661 // keep it for definitely written analysis
1662 Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
1663 for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
1664 FlatNode fn = (FlatNode) iterator.next();
1665 ssjava.addSameHeightWriteFlatNode(fn);
1671 return destLocation;
1674 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
1675 SymbolTable nametable, TreeNode n) {
1677 ClassDescriptor cd = md.getClassDesc();
1678 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
1680 // currently enforce every variable to have corresponding location
1681 if (annotationVec.size() == 0) {
1682 throw new Error("Location is not assigned to variable '" + vd.getSymbol()
1683 + "' in the method '" + md + "' of the class " + cd.getSymbol() + " at "
1684 + generateErrorMessage(cd, n));
1687 int locDecCount = 0;
1688 for (int i = 0; i < annotationVec.size(); i++) {
1689 AnnotationDescriptor ad = annotationVec.elementAt(i);
1691 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1693 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1695 if (locDecCount > 1) {// variable can have at most one location
1696 throw new Error(vd.getSymbol() + " has more than one location declaration.");
1698 String locDec = ad.getValue(); // check if location is defined
1700 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
1701 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
1702 d2loc.put(vd, deltaLoc);
1703 addLocationType(vd.getType(), deltaLoc);
1705 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1707 Location lastElement = compLoc.get(compLoc.getSize() - 1);
1708 if (ssjava.isSharedLocation(lastElement)) {
1709 ssjava.mapSharedLocation2Descriptor(lastElement, vd);
1712 d2loc.put(vd, compLoc);
1713 addLocationType(vd.getType(), compLoc);
1722 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1725 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1728 int beginIdx = dIdx + 6;
1729 locDec = locDec.substring(beginIdx, locDec.length() - 1);
1730 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1733 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1734 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
1739 private Location parseFieldLocDeclaraton(String decl, String msg) throws Exception {
1741 int idx = decl.indexOf(".");
1743 String className = decl.substring(0, idx);
1744 String fieldName = decl.substring(idx + 1);
1746 className.replaceAll(" ", "");
1747 fieldName.replaceAll(" ", "");
1749 Descriptor d = state.getClassSymbolTable().get(className);
1752 // System.out.println("state.getClassSymbolTable()=" +
1753 // state.getClassSymbolTable());
1754 throw new Error("The class in the location declaration '" + decl + "' does not exist at "
1758 assert (d instanceof ClassDescriptor);
1759 SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1760 if (!lattice.containsKey(fieldName)) {
1761 throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1762 + className + "' at " + msg);
1765 return new Location(d, fieldName);
1768 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1770 CompositeLocation compLoc = new CompositeLocation();
1772 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1773 List<String> locIdList = new ArrayList<String>();
1774 while (tokenizer.hasMoreTokens()) {
1775 String locId = tokenizer.nextToken();
1776 locIdList.add(locId);
1779 // at least,one location element needs to be here!
1780 assert (locIdList.size() > 0);
1782 // assume that loc with idx 0 comes from the local lattice
1783 // loc with idx 1 comes from the field lattice
1785 String localLocId = locIdList.get(0);
1786 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1787 Location localLoc = new Location(md, localLocId);
1788 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1789 throw new Error("Location " + localLocId
1790 + " is not defined in the local variable lattice at "
1791 + md.getClassDesc().getSourceFileName() + "::" + (n != null ? n.getNumLine() : md) + ".");
1793 compLoc.addLocation(localLoc);
1795 for (int i = 1; i < locIdList.size(); i++) {
1796 String locName = locIdList.get(i);
1799 parseFieldLocDeclaraton(locName, generateErrorMessage(md.getClassDesc(), n));
1800 compLoc.addLocation(fieldLoc);
1801 } catch (Exception e) {
1802 throw new Error("The location declaration '" + locName + "' is wrong at "
1803 + generateErrorMessage(md.getClassDesc(), n));
1811 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1812 VarDescriptor vd = dn.getVarDescriptor();
1813 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1816 private void checkDeclarationInClass(ClassDescriptor cd) {
1817 // Check to see that fields are okay
1818 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1819 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1821 if (!(fd.isFinal() && fd.isStatic())) {
1822 checkFieldDeclaration(cd, fd);
1824 // for static final, assign top location by default
1825 Location loc = Location.createTopLocation(cd);
1826 addLocationType(fd.getType(), loc);
1831 private Location checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1833 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1835 // currently enforce every field to have corresponding location
1836 if (annotationVec.size() == 0) {
1837 throw new Error("Location is not assigned to the field '" + fd.getSymbol()
1838 + "' of the class " + cd.getSymbol() + " at " + cd.getSourceFileName());
1841 if (annotationVec.size() > 1) {
1842 // variable can have at most one location
1843 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1844 + " has more than one location.");
1847 AnnotationDescriptor ad = annotationVec.elementAt(0);
1848 Location loc = null;
1850 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1851 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1852 String locationID = ad.getValue();
1853 // check if location is defined
1854 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1855 if (lattice == null || (!lattice.containsKey(locationID))) {
1856 throw new Error("Location " + locationID
1857 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1858 + cd.getSourceFileName() + ".");
1860 loc = new Location(cd, locationID);
1862 if (ssjava.isSharedLocation(loc)) {
1863 ssjava.mapSharedLocation2Descriptor(loc, fd);
1866 addLocationType(fd.getType(), loc);
1874 private void addLocationType(TypeDescriptor type, CompositeLocation loc) {
1876 TypeExtension te = type.getExtension();
1879 ssType = (SSJavaType) te;
1880 ssType.setCompLoc(loc);
1882 ssType = new SSJavaType(loc);
1883 type.setExtension(ssType);
1888 private void addLocationType(TypeDescriptor type, Location loc) {
1890 type.setExtension(loc);
1894 static class CompositeLattice {
1896 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2, String msg) {
1898 // System.out.println("\nisGreaterThan=" + loc1 + " " + loc2 + " msg=" +
1900 int baseCompareResult = compareBaseLocationSet(loc1, loc2, true, false, msg);
1901 if (baseCompareResult == ComparisonResult.EQUAL) {
1902 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1907 } else if (baseCompareResult == ComparisonResult.GREATER) {
1915 public static int compare(CompositeLocation loc1, CompositeLocation loc2, boolean ignore,
1918 // System.out.println("compare=" + loc1 + " " + loc2);
1919 int baseCompareResult = compareBaseLocationSet(loc1, loc2, false, ignore, msg);
1921 if (baseCompareResult == ComparisonResult.EQUAL) {
1922 return compareDelta(loc1, loc2);
1924 return baseCompareResult;
1929 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1931 int deltaCount1 = 0;
1932 int deltaCount2 = 0;
1933 if (dLoc1 instanceof DeltaLocation) {
1934 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1937 if (dLoc2 instanceof DeltaLocation) {
1938 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1940 if (deltaCount1 < deltaCount2) {
1941 return ComparisonResult.GREATER;
1942 } else if (deltaCount1 == deltaCount2) {
1943 return ComparisonResult.EQUAL;
1945 return ComparisonResult.LESS;
1950 private static int compareBaseLocationSet(CompositeLocation compLoc1,
1951 CompositeLocation compLoc2, boolean awareSharedLoc, boolean ignore, String msg) {
1953 // if compLoc1 is greater than compLoc2, return true
1954 // else return false;
1956 // compare one by one in according to the order of the tuple
1958 for (int i = 0; i < compLoc1.getSize(); i++) {
1959 Location loc1 = compLoc1.get(i);
1960 if (i >= compLoc2.getSize()) {
1962 return ComparisonResult.INCOMPARABLE;
1964 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1965 + " because they are not comparable at " + msg);
1968 Location loc2 = compLoc2.get(i);
1970 Descriptor descriptor = getCommonParentDescriptor(loc1, loc2, msg);
1971 SSJavaLattice<String> lattice = getLatticeByDescriptor(descriptor);
1973 // check if the shared location is appeared only at the end of the
1974 // composite location
1975 if (lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
1976 if (i != (compLoc1.getSize() - 1)) {
1977 throw new Error("The shared location " + loc1.getLocIdentifier()
1978 + " cannot be appeared in the middle of composite location at" + msg);
1982 if (lattice.getSharedLocSet().contains(loc2.getLocIdentifier())) {
1983 if (i != (compLoc2.getSize() - 1)) {
1984 throw new Error("The shared location " + loc2.getLocIdentifier()
1985 + " cannot be appeared in the middle of composite location at " + msg);
1989 // if (!lattice1.equals(lattice2)) {
1990 // throw new Error("Failed to compare two locations of " + compLoc1 +
1991 // " and " + compLoc2
1992 // + " because they are not comparable at " + msg);
1995 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1997 // check if the current location is the spinning location
1998 // note that the spinning location only can be appeared in the last
1999 // part of the composite location
2000 if (awareSharedLoc && numOfTie == compLoc1.getSize()
2001 && lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
2002 return ComparisonResult.GREATER;
2005 } else if (lattice.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
2006 return ComparisonResult.GREATER;
2008 return ComparisonResult.LESS;
2013 if (numOfTie == compLoc1.getSize()) {
2015 if (numOfTie != compLoc2.getSize()) {
2018 return ComparisonResult.INCOMPARABLE;
2020 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
2021 + " because they are not comparable at " + msg);
2026 return ComparisonResult.EQUAL;
2029 return ComparisonResult.LESS;
2033 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet, String errMsg) {
2035 // System.out.println("Calculating GLB=" + inputSet);
2036 CompositeLocation glbCompLoc = new CompositeLocation();
2038 // calculate GLB of the first(priority) element
2039 Set<String> priorityLocIdentifierSet = new HashSet<String>();
2040 Descriptor priorityDescriptor = null;
2042 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
2043 new Hashtable<String, Set<CompositeLocation>>();
2044 // mapping from the priority loc ID to its full representation by the
2045 // composite location
2047 int maxTupleSize = 0;
2048 CompositeLocation maxCompLoc = null;
2050 Location prevPriorityLoc = null;
2051 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
2052 CompositeLocation compLoc = (CompositeLocation) iterator.next();
2053 if (compLoc.getSize() > maxTupleSize) {
2054 maxTupleSize = compLoc.getSize();
2055 maxCompLoc = compLoc;
2057 Location priorityLoc = compLoc.get(0);
2058 String priorityLocId = priorityLoc.getLocIdentifier();
2059 priorityLocIdentifierSet.add(priorityLocId);
2061 if (locId2CompLocSet.containsKey(priorityLocId)) {
2062 locId2CompLocSet.get(priorityLocId).add(compLoc);
2064 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
2065 newSet.add(compLoc);
2066 locId2CompLocSet.put(priorityLocId, newSet);
2069 // check if priority location are coming from the same lattice
2070 if (priorityDescriptor == null) {
2071 priorityDescriptor = priorityLoc.getDescriptor();
2073 priorityDescriptor = getCommonParentDescriptor(priorityLoc, prevPriorityLoc, errMsg);
2075 prevPriorityLoc = priorityLoc;
2076 // else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
2077 // throw new Error("Failed to calculate GLB of " + inputSet
2078 // + " because they are from different lattices.");
2082 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
2083 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
2085 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
2086 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
2088 if (compSet == null) {
2089 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
2090 // mean that the result is already lower than <x1,y1> and <x2,y2>
2091 // assign TOP to the rest of the location elements
2093 // in this case, do not take care about delta
2094 // CompositeLocation inputComp = inputSet.iterator().next();
2095 for (int i = 1; i < maxTupleSize; i++) {
2096 glbCompLoc.addLocation(Location.createTopLocation(maxCompLoc.get(i).getDescriptor()));
2100 // here find out composite location that has a maximum length tuple
2101 // if we have three input set: [A], [A,B], [A,B,C]
2102 // maximum length tuple will be [A,B,C]
2104 CompositeLocation maxFromCompSet = null;
2105 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2106 CompositeLocation c = (CompositeLocation) iterator.next();
2107 if (c.getSize() > max) {
2113 if (compSet.size() == 1) {
2114 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
2115 CompositeLocation comp = compSet.iterator().next();
2116 for (int i = 1; i < comp.getSize(); i++) {
2117 glbCompLoc.addLocation(comp.get(i));
2120 // if input location corresponding to glb is a delta, need to apply
2121 // delta to glb result
2122 if (comp instanceof DeltaLocation) {
2123 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
2127 // when GLB(x1,x2)==x1 and x2 : GLB case 1
2128 // if more than one location shares the same priority GLB
2129 // need to calculate the rest of GLB loc
2131 // setup input set starting from the second tuple item
2132 Set<CompositeLocation> innerGLBInput = new HashSet<CompositeLocation>();
2133 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2134 CompositeLocation compLoc = (CompositeLocation) iterator.next();
2135 CompositeLocation innerCompLoc = new CompositeLocation();
2136 for (int idx = 1; idx < compLoc.getSize(); idx++) {
2137 innerCompLoc.addLocation(compLoc.get(idx));
2139 if (innerCompLoc.getSize() > 0) {
2140 innerGLBInput.add(innerCompLoc);
2144 if (innerGLBInput.size() > 0) {
2145 CompositeLocation innerGLB = CompositeLattice.calculateGLB(innerGLBInput, errMsg);
2146 for (int idx = 0; idx < innerGLB.getSize(); idx++) {
2147 glbCompLoc.addLocation(innerGLB.get(idx));
2151 // if input location corresponding to glb is a delta, need to apply
2152 // delta to glb result
2154 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2155 CompositeLocation compLoc = (CompositeLocation) iterator.next();
2156 if (compLoc instanceof DeltaLocation) {
2157 if (glbCompLoc.equals(compLoc)) {
2158 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
2167 // System.out.println("GLB=" + glbCompLoc);
2172 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
2174 SSJavaLattice<String> lattice = null;
2176 if (d instanceof ClassDescriptor) {
2177 lattice = ssjava.getCd2lattice().get(d);
2178 } else if (d instanceof MethodDescriptor) {
2179 if (ssjava.getMd2lattice().containsKey(d)) {
2180 lattice = ssjava.getMd2lattice().get(d);
2182 // use default lattice for the method
2183 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
2190 static Descriptor getCommonParentDescriptor(Location loc1, Location loc2, String msg) {
2192 Descriptor d1 = loc1.getDescriptor();
2193 Descriptor d2 = loc2.getDescriptor();
2195 Descriptor descriptor;
2197 if (d1 instanceof ClassDescriptor && d2 instanceof ClassDescriptor) {
2199 if (d1.equals(d2)) {
2202 // identifying which one is parent class
2203 Set<Descriptor> d1SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d1);
2204 Set<Descriptor> d2SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d2);
2206 if (d1 == null && d2 == null) {
2207 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2208 + " because they are not comparable at " + msg);
2209 } else if (d1SubClassesSet != null && d1SubClassesSet.contains(d2)) {
2211 } else if (d2SubClassesSet != null && d2SubClassesSet.contains(d1)) {
2214 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2215 + " because they are not comparable at " + msg);
2219 } else if (d1 instanceof MethodDescriptor && d2 instanceof MethodDescriptor) {
2221 if (d1.equals(d2)) {
2225 // identifying which one is parent class
2226 MethodDescriptor md1 = (MethodDescriptor) d1;
2227 MethodDescriptor md2 = (MethodDescriptor) d2;
2229 if (!md1.matches(md2)) {
2230 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2231 + " because they are not comparable at " + msg);
2234 Set<Descriptor> d1SubClassesSet =
2235 ssjava.tu.getSubClasses(((MethodDescriptor) d1).getClassDesc());
2236 Set<Descriptor> d2SubClassesSet =
2237 ssjava.tu.getSubClasses(((MethodDescriptor) d2).getClassDesc());
2239 if (d1 == null && d2 == null) {
2240 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2241 + " because they are not comparable at " + msg);
2242 } else if (d1 != null && d1SubClassesSet.contains(d2)) {
2244 } else if (d2 != null && d2SubClassesSet.contains(d1)) {
2247 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2248 + " because they are not comparable at " + msg);
2253 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2254 + " because they are not comparable at " + msg);
2263 class ComparisonResult {
2265 public static final int GREATER = 0;
2266 public static final int EQUAL = 1;
2267 public static final int LESS = 2;
2268 public static final int INCOMPARABLE = 3;
2275 class ReturnLocGenerator {
2277 public static final int PARAMISHIGHER = 0;
2278 public static final int PARAMISSAME = 1;
2279 public static final int IGNORE = 2;
2281 private Hashtable<Integer, Integer> paramIdx2paramType;
2283 private CompositeLocation declaredReturnLoc = null;
2285 public ReturnLocGenerator(CompositeLocation returnLoc, MethodDescriptor md,
2286 List<CompositeLocation> params, String msg) {
2288 CompositeLocation thisLoc = params.get(0);
2289 if (returnLoc.get(0).equals(thisLoc.get(0)) && returnLoc.getSize() > 1) {
2290 // if the declared return location consists of THIS and field location,
2291 // return location for the caller's side has to have same field element
2292 this.declaredReturnLoc = returnLoc;
2294 // creating mappings
2295 paramIdx2paramType = new Hashtable<Integer, Integer>();
2296 for (int i = 0; i < params.size(); i++) {
2297 CompositeLocation param = params.get(i);
2298 int compareResult = CompositeLattice.compare(param, returnLoc, true, msg);
2301 if (compareResult == ComparisonResult.GREATER) {
2303 } else if (compareResult == ComparisonResult.EQUAL) {
2308 paramIdx2paramType.put(new Integer(i), new Integer(type));
2314 public CompositeLocation computeReturnLocation(List<CompositeLocation> args) {
2316 if (declaredReturnLoc != null) {
2317 // when developer specify that the return value is [THIS,field]
2318 // needs to translate to the caller's location
2319 CompositeLocation callerLoc = new CompositeLocation();
2320 CompositeLocation callerBaseLocation = args.get(0);
2322 for (int i = 0; i < callerBaseLocation.getSize(); i++) {
2323 callerLoc.addLocation(callerBaseLocation.get(i));
2325 for (int i = 1; i < declaredReturnLoc.getSize(); i++) {
2326 callerLoc.addLocation(declaredReturnLoc.get(i));
2330 // compute the highest possible location in caller's side
2331 assert paramIdx2paramType.keySet().size() == args.size();
2333 Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
2334 for (int i = 0; i < args.size(); i++) {
2335 int type = (paramIdx2paramType.get(new Integer(i))).intValue();
2336 CompositeLocation argLoc = args.get(i);
2337 if (type == PARAMISHIGHER || type == PARAMISSAME) {
2338 // return loc is equal to or lower than param
2339 inputGLB.add(argLoc);
2343 // compute GLB of arguments subset that are same or higher than return
2345 if (inputGLB.isEmpty()) {
2346 CompositeLocation rtr =
2347 new CompositeLocation(Location.createTopLocation(args.get(0).get(0).getDescriptor()));
2350 CompositeLocation glb = CompositeLattice.calculateGLB(inputGLB, "");