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 checkMethodBody(cd, md, null);
236 private void checkOrderingInheritance(ClassDescriptor superCd, ClassDescriptor cd) {
237 // here, we're going to check that sub class keeps same relative orderings
238 // in respect to super class
240 SSJavaLattice<String> superLattice = ssjava.getClassLattice(superCd);
241 SSJavaLattice<String> subLattice = ssjava.getClassLattice(cd);
243 if (superLattice != null) {
244 // if super class doesn't define lattice, then we don't need to check its
246 if (subLattice == null) {
247 throw new Error("If a parent class '" + superCd
248 + "' has a ordering lattice, its subclass '" + cd + "' should have one.");
251 Set<Pair<String, String>> superPairSet = superLattice.getOrderingPairSet();
252 Set<Pair<String, String>> subPairSet = subLattice.getOrderingPairSet();
254 for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
255 Pair<String, String> pair = (Pair<String, String>) iterator.next();
257 if (!subPairSet.contains(pair)) {
258 throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
259 + pair.getSecond() + " < " + pair.getFirst()
260 + "' that is defined by its superclass '" + superCd + "'.");
265 MethodLattice<String> superMethodDefaultLattice = ssjava.getMethodDefaultLattice(superCd);
266 MethodLattice<String> subMethodDefaultLattice = ssjava.getMethodDefaultLattice(cd);
268 if (superMethodDefaultLattice != null) {
269 if (subMethodDefaultLattice == null) {
270 throw new Error("When a parent class '" + superCd
271 + "' defines a default method lattice, its subclass '" + cd + "' should define one.");
274 Set<Pair<String, String>> superPairSet = superMethodDefaultLattice.getOrderingPairSet();
275 Set<Pair<String, String>> subPairSet = subMethodDefaultLattice.getOrderingPairSet();
277 for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
278 Pair<String, String> pair = (Pair<String, String>) iterator.next();
280 if (!subPairSet.contains(pair)) {
281 throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
282 + pair.getSecond() + " < " + pair.getFirst()
283 + "' that is defined by its superclass '" + superCd
284 + "' in the method default lattice.");
292 public Hashtable getMap() {
296 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
297 BlockNode bn = state.getMethodBody(md);
299 // first, check annotations on method parameters
300 List<CompositeLocation> paramList = new ArrayList<CompositeLocation>();
301 for (int i = 0; i < md.numParameters(); i++) {
302 // process annotations on method parameters
303 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
304 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), null);
305 paramList.add(d2loc.get(vd));
307 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
309 CompositeLocation returnLocComp = null;
311 boolean hasReturnLocDeclaration = false;
312 if (methodAnnotations != null) {
313 for (int i = 0; i < methodAnnotations.size(); i++) {
314 AnnotationDescriptor an = methodAnnotations.elementAt(i);
315 if (an.getMarker().equals(ssjava.RETURNLOC)) {
316 // this case, developer explicitly defines method lattice
317 String returnLocDeclaration = an.getValue();
318 returnLocComp = parseLocationDeclaration(md, null, returnLocDeclaration);
319 hasReturnLocDeclaration = true;
320 } else if (an.getMarker().equals(ssjava.THISLOC)) {
321 String thisLoc = an.getValue();
322 ssjava.getMethodLattice(md).setThisLoc(thisLoc);
327 // second, check return location annotation
328 if (!md.getReturnType().isVoid()) {
329 if (!hasReturnLocDeclaration) {
330 // if developer does not define method lattice
331 // search return location in the method default lattice
332 String rtrStr = ssjava.getMethodLattice(md).getReturnLoc();
333 if (rtrStr != null) {
334 returnLocComp = new CompositeLocation(new Location(md, rtrStr));
338 if (returnLocComp == null) {
339 throw new Error("Return location is not specified for the method " + md + " at "
340 + cd.getSourceFileName());
343 md2ReturnLoc.put(md, returnLocComp);
347 if (!md.getReturnType().isVoid()) {
348 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
349 String thisLocId = methodLattice.getThisLoc();
350 if ((!md.isStatic()) && thisLocId == null) {
351 throw new Error("Method '" + md + "' does not have the definition of 'this' location at "
352 + md.getClassDesc().getSourceFileName());
354 CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
355 paramList.add(0, thisLoc);
356 md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), md, paramList, md
357 + " of " + cd.getSourceFileName()));
360 // fourth, check declarations inside of method
362 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
366 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
367 bn.getVarTable().setParent(nametable);
368 for (int i = 0; i < bn.size(); i++) {
369 BlockStatementNode bsn = bn.get(i);
370 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
374 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
375 BlockStatementNode bsn) {
377 switch (bsn.kind()) {
378 case Kind.SubBlockNode:
379 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
382 case Kind.DeclarationNode:
383 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
387 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
390 case Kind.IfStatementNode:
391 checkDeclarationInIfStatementNode(md, nametable, (IfStatementNode) bsn);
394 case Kind.SwitchStatementNode:
395 checkDeclarationInSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
398 case Kind.SynchronizedNode:
399 checkDeclarationInSynchronizedNode(md, nametable, (SynchronizedNode) bsn);
405 private void checkDeclarationInSynchronizedNode(MethodDescriptor md, SymbolTable nametable,
406 SynchronizedNode sbn) {
407 checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
410 private void checkDeclarationInSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
411 SwitchStatementNode ssn) {
412 BlockNode sbn = ssn.getSwitchBody();
413 for (int i = 0; i < sbn.size(); i++) {
414 SwitchBlockNode node = (SwitchBlockNode) sbn.get(i);
415 checkDeclarationInBlockNode(md, nametable, node.getSwitchBlockStatement());
419 private void checkDeclarationInIfStatementNode(MethodDescriptor md, SymbolTable nametable,
420 IfStatementNode isn) {
421 checkDeclarationInBlockNode(md, nametable, isn.getTrueBlock());
422 if (isn.getFalseBlock() != null)
423 checkDeclarationInBlockNode(md, nametable, isn.getFalseBlock());
426 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
428 if (ln.getType() == LoopNode.FORLOOP) {
429 // check for loop case
430 ClassDescriptor cd = md.getClassDesc();
431 BlockNode bn = ln.getInitializer();
432 for (int i = 0; i < bn.size(); i++) {
433 BlockStatementNode bsn = bn.get(i);
434 checkDeclarationInBlockStatementNode(md, nametable, bsn);
439 checkDeclarationInBlockNode(md, nametable, ln.getBody());
442 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md,
443 CompositeLocation constraints) {
444 BlockNode bn = state.getMethodBody(md);
445 checkLocationFromBlockNode(md, md.getParameterTable(), bn, constraints);
448 private String generateErrorMessage(ClassDescriptor cd, TreeNode tn) {
450 return cd.getSourceFileName() + "::" + tn.getNumLine();
452 return cd.getSourceFileName();
457 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
458 BlockNode bn, CompositeLocation constraint) {
460 bn.getVarTable().setParent(nametable);
461 for (int i = 0; i < bn.size(); i++) {
462 BlockStatementNode bsn = bn.get(i);
463 checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn, constraint);
465 return new CompositeLocation();
469 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
470 SymbolTable nametable, BlockStatementNode bsn, CompositeLocation constraint) {
472 CompositeLocation compLoc = null;
473 switch (bsn.kind()) {
474 case Kind.BlockExpressionNode:
476 checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, constraint);
479 case Kind.DeclarationNode:
480 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn, constraint);
483 case Kind.IfStatementNode:
484 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn, constraint);
488 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn, constraint);
491 case Kind.ReturnNode:
492 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn, constraint);
495 case Kind.SubBlockNode:
496 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn, constraint);
499 case Kind.ContinueBreakNode:
500 compLoc = new CompositeLocation();
503 case Kind.SwitchStatementNode:
505 checkLocationFromSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, constraint);
511 private CompositeLocation checkLocationFromSwitchStatementNode(MethodDescriptor md,
512 SymbolTable nametable, SwitchStatementNode ssn, CompositeLocation constraint) {
514 ClassDescriptor cd = md.getClassDesc();
515 CompositeLocation condLoc =
516 checkLocationFromExpressionNode(md, nametable, ssn.getCondition(), new CompositeLocation(),
518 BlockNode sbn = ssn.getSwitchBody();
520 constraint = generateNewConstraint(constraint, condLoc);
522 for (int i = 0; i < sbn.size(); i++) {
523 checkLocationFromSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), constraint);
525 return new CompositeLocation();
528 private CompositeLocation checkLocationFromSwitchBlockNode(MethodDescriptor md,
529 SymbolTable nametable, SwitchBlockNode sbn, CompositeLocation constraint) {
531 CompositeLocation blockLoc =
532 checkLocationFromBlockNode(md, nametable, sbn.getSwitchBlockStatement(), constraint);
538 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
539 ReturnNode rn, CompositeLocation constraint) {
541 ExpressionNode returnExp = rn.getReturnExpression();
543 CompositeLocation returnValueLoc;
544 if (returnExp != null) {
546 checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation(),
549 // System.out.println("# RETURN VALUE LOC=" + returnValueLoc +
550 // " with constraint=" + constraint);
552 // TODO: do we need to check here?
553 // if this return statement is inside branch, return value has an implicit
554 // flow from conditional location
555 // if (constraint != null) {
556 // Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
557 // inputGLB.add(returnValueLoc);
558 // inputGLB.add(constraint);
560 // CompositeLattice.calculateGLB(inputGLB,
561 // generateErrorMessage(md.getClassDesc(), rn));
564 // check if return value is equal or higher than RETRUNLOC of method
565 // declaration annotation
566 CompositeLocation declaredReturnLoc = md2ReturnLoc.get(md);
569 CompositeLattice.compare(returnValueLoc, declaredReturnLoc, false,
570 generateErrorMessage(md.getClassDesc(), rn));
572 if (compareResult == ComparisonResult.LESS || compareResult == ComparisonResult.INCOMPARABLE) {
574 "Return value location is not equal or higher than the declaraed return location at "
575 + md.getClassDesc().getSourceFileName() + "::" + rn.getNumLine());
579 return new CompositeLocation();
582 private boolean hasOnlyLiteralValue(ExpressionNode en) {
583 if (en.kind() == Kind.LiteralNode) {
590 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
591 LoopNode ln, CompositeLocation constraint) {
593 ClassDescriptor cd = md.getClassDesc();
594 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
596 CompositeLocation condLoc =
597 checkLocationFromExpressionNode(md, nametable, ln.getCondition(),
598 new CompositeLocation(), constraint, false);
599 // addLocationType(ln.getCondition().getType(), (condLoc));
601 constraint = generateNewConstraint(constraint, condLoc);
602 checkLocationFromBlockNode(md, nametable, ln.getBody(), constraint);
604 return new CompositeLocation();
607 // check 'for loop' case
608 BlockNode bn = ln.getInitializer();
609 // need to check initialization node
610 checkLocationFromBlockNode(md, nametable, bn, constraint);
611 bn.getVarTable().setParent(nametable);
613 // calculate glb location of condition and update statements
614 CompositeLocation condLoc =
615 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
616 new CompositeLocation(), constraint, false);
617 // addLocationType(ln.getCondition().getType(), condLoc);
619 constraint = generateNewConstraint(constraint, condLoc);
621 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate(), constraint);
622 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody(), constraint);
624 return new CompositeLocation();
630 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
631 SymbolTable nametable, SubBlockNode sbn, CompositeLocation constraint) {
632 CompositeLocation compLoc =
633 checkLocationFromBlockNode(md, nametable, sbn.getBlockNode(), constraint);
637 private CompositeLocation generateNewConstraint(CompositeLocation currentCon,
638 CompositeLocation newCon) {
640 if (currentCon == null) {
643 // compute GLB of current constraint and new constraint
644 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
645 inputSet.add(currentCon);
646 inputSet.add(newCon);
647 return CompositeLattice.calculateGLB(inputSet, "");
652 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
653 SymbolTable nametable, IfStatementNode isn, CompositeLocation constraint) {
655 CompositeLocation condLoc =
656 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(),
659 // addLocationType(isn.getCondition().getType(), condLoc);
661 constraint = generateNewConstraint(constraint, condLoc);
662 checkLocationFromBlockNode(md, nametable, isn.getTrueBlock(), constraint);
664 if (isn.getFalseBlock() != null) {
665 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock(), constraint);
668 return new CompositeLocation();
671 private void checkOwnership(MethodDescriptor md, TreeNode tn, ExpressionNode srcExpNode) {
673 if (srcExpNode.kind() == Kind.NameNode || srcExpNode.kind() == Kind.FieldAccessNode) {
674 if (srcExpNode.getType().isPtr() && !srcExpNode.getType().isNull()) {
675 // first, check the linear type
676 // RHS reference should be owned by the current method
677 FieldDescriptor fd = getFieldDescriptorFromExpressionNode(srcExpNode);
681 isOwned = ((SSJavaType) srcExpNode.getType().getExtension()).isOwned();
684 isOwned = ssjava.isOwnedByMethod(md, fd);
688 "It is not allowed to create the reference alias from the reference not owned by the method at "
689 + generateErrorMessage(md.getClassDesc(), tn));
697 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
698 SymbolTable nametable, DeclarationNode dn, CompositeLocation constraint) {
700 VarDescriptor vd = dn.getVarDescriptor();
702 CompositeLocation destLoc = d2loc.get(vd);
704 if (dn.getExpression() != null) {
706 checkOwnership(md, dn, dn.getExpression());
708 CompositeLocation expressionLoc =
709 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
710 new CompositeLocation(), constraint, false);
711 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
713 if (expressionLoc != null) {
715 // checking location order
716 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc,
717 generateErrorMessage(md.getClassDesc(), dn))) {
718 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
719 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
720 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
723 return expressionLoc;
727 return new CompositeLocation();
733 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
735 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
738 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
739 SymbolTable nametable, BlockExpressionNode ben, CompositeLocation constraint) {
740 CompositeLocation compLoc =
741 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null, constraint, false);
742 // addTypeLocation(ben.getExpression().getType(), compLoc);
746 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
747 SymbolTable nametable, ExpressionNode en, CompositeLocation loc,
748 CompositeLocation constraint, boolean isLHS) {
750 CompositeLocation compLoc = null;
753 case Kind.AssignmentNode:
755 checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc, constraint);
758 case Kind.FieldAccessNode:
760 checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc, constraint);
764 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc, constraint);
768 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en, constraint);
771 case Kind.CreateObjectNode:
772 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
775 case Kind.ArrayAccessNode:
777 checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en, constraint, isLHS);
780 case Kind.LiteralNode:
781 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
784 case Kind.MethodInvokeNode:
786 checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc, constraint);
789 case Kind.TertiaryNode:
790 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en, constraint);
794 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en, constraint);
797 // case Kind.InstanceOfNode:
798 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
801 // case Kind.ArrayInitializerNode:
802 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
806 // case Kind.ClassTypeNode:
807 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
810 // case Kind.OffsetNode:
811 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
818 // addTypeLocation(en.getType(), compLoc);
823 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
824 CastNode cn, CompositeLocation constraint) {
826 ExpressionNode en = cn.getExpression();
827 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
832 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
833 SymbolTable nametable, TertiaryNode tn, CompositeLocation constraint) {
834 ClassDescriptor cd = md.getClassDesc();
836 CompositeLocation condLoc =
837 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(),
839 // addLocationType(tn.getCond().getType(), condLoc);
840 CompositeLocation trueLoc =
841 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(),
843 // addLocationType(tn.getTrueExpr().getType(), trueLoc);
844 CompositeLocation falseLoc =
845 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(),
847 // addLocationType(tn.getFalseExpr().getType(), falseLoc);
849 // locations from true/false branches can be TOP when there are only literal
851 // in this case, we don't need to check flow down rule!
853 // System.out.println("\n#tertiary cond=" + tn.getCond().printNode(0) +
854 // " Loc=" + condLoc);
855 // System.out.println("# true=" + tn.getTrueExpr().printNode(0) + " Loc=" +
857 // System.out.println("# false=" + tn.getFalseExpr().printNode(0) + " Loc="
860 // check if condLoc is higher than trueLoc & falseLoc
861 if (!trueLoc.get(0).isTop()
862 && !CompositeLattice.isGreaterThan(condLoc, trueLoc, generateErrorMessage(cd, tn))) {
864 "The location of the condition expression is lower than the true expression at "
865 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
868 if (!falseLoc.get(0).isTop()
869 && !CompositeLattice.isGreaterThan(condLoc, falseLoc,
870 generateErrorMessage(cd, tn.getCond()))) {
872 "The location of the condition expression is lower than the false expression at "
873 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
876 // then, return glb of trueLoc & falseLoc
877 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
878 glbInputSet.add(trueLoc);
879 glbInputSet.add(falseLoc);
881 if (glbInputSet.size() == 1) {
884 return CompositeLattice.calculateGLB(glbInputSet, generateErrorMessage(cd, tn));
889 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
890 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc,
891 CompositeLocation constraint) {
893 ClassDescriptor cd = md.getClassDesc();
894 MethodDescriptor calleeMD = min.getMethod();
896 NameDescriptor baseName = min.getBaseName();
897 boolean isSystemout = false;
898 if (baseName != null) {
899 isSystemout = baseName.getSymbol().equals("System.out");
902 if (!ssjava.isSSJavaUtil(calleeMD.getClassDesc()) && !ssjava.isTrustMethod(calleeMD)
903 && !calleeMD.getModifiers().isNative() && !isSystemout) {
905 CompositeLocation baseLocation = null;
906 if (min.getExpression() != null) {
908 checkLocationFromExpressionNode(md, nametable, min.getExpression(),
909 new CompositeLocation(), constraint, false);
911 if (min.getMethod().isStatic()) {
912 String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
913 if (globalLocId == null) {
914 throw new Error("Method lattice does not define global variable location at "
915 + generateErrorMessage(md.getClassDesc(), min));
917 baseLocation = new CompositeLocation(new Location(md, globalLocId));
919 String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
920 baseLocation = new CompositeLocation(new Location(md, thisLocId));
924 // System.out.println("\n#checkLocationFromMethodInvokeNode=" +
926 // + " baseLocation=" + baseLocation + " constraint=" + constraint);
928 if (constraint != null) {
930 CompositeLattice.compare(constraint, baseLocation, true, generateErrorMessage(cd, min));
931 if (compareResult != ComparisonResult.GREATER) {
932 // if the current constraint is higher than method's THIS location
933 // no need to check constraints!
934 CompositeLocation calleeConstraint =
935 translateCallerLocToCalleeLoc(calleeMD, baseLocation, constraint);
936 // System.out.println("check method body for constraint:" + calleeMD +
937 // " calleeConstraint="
938 // + calleeConstraint);
939 checkMethodBody(calleeMD.getClassDesc(), calleeMD, calleeConstraint);
943 checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
945 // checkCallerArgumentLocationConstraints(md, nametable, min,
946 // baseLocation, constraint);
948 if (!min.getMethod().getReturnType().isVoid()) {
949 // If method has a return value, compute the highest possible return
950 // location in the caller's perspective
951 CompositeLocation ceilingLoc =
952 computeCeilingLocationForCaller(md, nametable, min, baseLocation, constraint);
957 return new CompositeLocation(Location.createTopLocation(md));
961 private CompositeLocation translateCallerLocToCalleeLoc(MethodDescriptor calleeMD,
962 CompositeLocation calleeBaseLoc, CompositeLocation constraint) {
964 CompositeLocation calleeConstraint = new CompositeLocation();
966 // if (constraint.startsWith(calleeBaseLoc)) {
967 // if the first part of constraint loc is matched with callee base loc
968 Location thisLoc = new Location(calleeMD, ssjava.getMethodLattice(calleeMD).getThisLoc());
969 calleeConstraint.addLocation(thisLoc);
970 for (int i = calleeBaseLoc.getSize(); i < constraint.getSize(); i++) {
971 calleeConstraint.addLocation(constraint.get(i));
976 return calleeConstraint;
979 private void checkCallerArgumentLocationConstraints(MethodDescriptor md, SymbolTable nametable,
980 MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
981 // if parameter location consists of THIS and FIELD location,
982 // caller should pass an argument that is comparable to the declared
983 // parameter location
984 // and is not lower than the declared parameter location in the field
987 MethodDescriptor calleemd = min.getMethod();
989 List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
990 List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
992 MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
993 Location calleeThisLoc = new Location(calleemd, calleeLattice.getThisLoc());
995 for (int i = 0; i < min.numArgs(); i++) {
996 ExpressionNode en = min.getArg(i);
997 CompositeLocation callerArgLoc =
998 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1000 callerArgList.add(callerArgLoc);
1003 // setup callee params set
1004 for (int i = 0; i < calleemd.numParameters(); i++) {
1005 VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
1006 CompositeLocation calleeLoc = d2loc.get(calleevd);
1007 calleeParamList.add(calleeLoc);
1010 String errorMsg = generateErrorMessage(md.getClassDesc(), min);
1012 // System.out.println("checkCallerArgumentLocationConstraints=" +
1013 // min.printNode(0));
1014 // System.out.println("base location=" + callerBaseLoc + " constraint=" +
1017 for (int i = 0; i < calleeParamList.size(); i++) {
1018 CompositeLocation calleeParamLoc = calleeParamList.get(i);
1019 if (calleeParamLoc.get(0).equals(calleeThisLoc) && calleeParamLoc.getSize() > 1) {
1021 // callee parameter location has field information
1022 CompositeLocation callerArgLoc = callerArgList.get(i);
1024 CompositeLocation paramLocation =
1025 translateCalleeParamLocToCaller(md, calleeParamLoc, callerBaseLoc, errorMsg);
1027 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
1028 if (constraint != null) {
1029 inputGLBSet.add(callerArgLoc);
1030 inputGLBSet.add(constraint);
1032 CompositeLattice.calculateGLB(inputGLBSet,
1033 generateErrorMessage(md.getClassDesc(), min));
1036 if (!CompositeLattice.isGreaterThan(callerArgLoc, paramLocation, errorMsg)) {
1037 throw new Error("Caller argument '" + min.getArg(i).printNode(0) + " : " + callerArgLoc
1038 + "' should be higher than corresponding callee's parameter : " + paramLocation
1039 + " at " + errorMsg);
1047 private CompositeLocation translateCalleeParamLocToCaller(MethodDescriptor md,
1048 CompositeLocation calleeParamLoc, CompositeLocation callerBaseLocation, String errorMsg) {
1050 CompositeLocation translate = new CompositeLocation();
1052 for (int i = 0; i < callerBaseLocation.getSize(); i++) {
1053 translate.addLocation(callerBaseLocation.get(i));
1056 for (int i = 1; i < calleeParamLoc.getSize(); i++) {
1057 translate.addLocation(calleeParamLoc.get(i));
1060 // System.out.println("TRANSLATED=" + translate + " from calleeParamLoc=" +
1066 private CompositeLocation computeCeilingLocationForCaller(MethodDescriptor md,
1067 SymbolTable nametable, MethodInvokeNode min, CompositeLocation baseLocation,
1068 CompositeLocation constraint) {
1069 List<CompositeLocation> argList = new ArrayList<CompositeLocation>();
1071 // by default, method has a THIS parameter
1072 argList.add(baseLocation);
1074 for (int i = 0; i < min.numArgs(); i++) {
1075 ExpressionNode en = min.getArg(i);
1076 CompositeLocation callerArg =
1077 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1079 argList.add(callerArg);
1082 // System.out.println("\n## computeReturnLocation=" + min.getMethod() +
1083 // " argList=" + argList);
1084 CompositeLocation ceilLoc = md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
1085 // System.out.println("## ReturnLocation=" + ceilLoc);
1091 private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
1092 MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
1094 // System.out.println("checkCalleeConstraints=" + min.printNode(0));
1096 MethodDescriptor calleemd = min.getMethod();
1098 MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
1099 CompositeLocation calleeThisLoc =
1100 new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
1102 List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
1103 List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
1105 if (min.numArgs() > 0) {
1106 // caller needs to guarantee that it passes arguments in regarding to
1107 // callee's hierarchy
1109 // setup caller args set
1110 // first, add caller's base(this) location
1111 callerArgList.add(callerBaseLoc);
1112 // second, add caller's arguments
1113 for (int i = 0; i < min.numArgs(); i++) {
1114 ExpressionNode en = min.getArg(i);
1115 CompositeLocation callerArgLoc =
1116 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
1118 callerArgList.add(callerArgLoc);
1121 // setup callee params set
1122 // first, add callee's this location
1123 calleeParamList.add(calleeThisLoc);
1124 // second, add callee's parameters
1125 for (int i = 0; i < calleemd.numParameters(); i++) {
1126 VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
1127 CompositeLocation calleeLoc = d2loc.get(calleevd);
1128 // System.out.println("calleevd=" + calleevd + " loc=" + calleeLoc);
1129 calleeParamList.add(calleeLoc);
1132 // here, check if ordering relations among caller's args respect
1133 // ordering relations in-between callee's args
1134 CHECK: for (int i = 0; i < calleeParamList.size(); i++) {
1135 CompositeLocation calleeLoc1 = calleeParamList.get(i);
1136 CompositeLocation callerLoc1 = callerArgList.get(i);
1138 for (int j = 0; j < calleeParamList.size(); j++) {
1140 CompositeLocation calleeLoc2 = calleeParamList.get(j);
1141 CompositeLocation callerLoc2 = callerArgList.get(j);
1143 if (callerLoc1.get(callerLoc1.getSize() - 1).isTop()
1144 || callerLoc2.get(callerLoc2.getSize() - 1).isTop()) {
1148 // System.out.println("calleeLoc1=" + calleeLoc1);
1149 // System.out.println("calleeLoc2=" + calleeLoc2 +
1150 // "calleeParamList=" + calleeParamList);
1153 CompositeLattice.compare(callerLoc1, callerLoc2, true,
1154 generateErrorMessage(md.getClassDesc(), min));
1155 // System.out.println("callerResult=" + callerResult);
1157 CompositeLattice.compare(calleeLoc1, calleeLoc2, true,
1158 generateErrorMessage(md.getClassDesc(), min));
1159 // System.out.println("calleeResult=" + calleeResult);
1161 if (callerResult == ComparisonResult.EQUAL) {
1162 if (ssjava.isSharedLocation(callerLoc1.get(callerLoc1.getSize() - 1))
1163 && ssjava.isSharedLocation(callerLoc2.get(callerLoc2.getSize() - 1))) {
1164 // if both of them are shared locations, promote them to
1165 // "GREATER relation"
1166 callerResult = ComparisonResult.GREATER;
1170 if (calleeResult == ComparisonResult.GREATER
1171 && callerResult != ComparisonResult.GREATER) {
1172 // If calleeLoc1 is higher than calleeLoc2
1173 // then, caller should have same ordering relation in-bet
1174 // callerLoc1 & callerLoc2
1176 String paramName1, paramName2;
1179 paramName1 = "'THIS'";
1181 paramName1 = "'parameter " + calleemd.getParamName(i - 1) + "'";
1185 paramName2 = "'THIS'";
1187 paramName2 = "'parameter " + calleemd.getParamName(j - 1) + "'";
1191 "Caller doesn't respect an ordering relation among method arguments: callee expects that "
1192 + paramName1 + " should be higher than " + paramName2 + " in " + calleemd
1193 + " at " + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
1204 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
1205 SymbolTable nametable, ArrayAccessNode aan, CompositeLocation constraint, boolean isLHS) {
1207 ClassDescriptor cd = md.getClassDesc();
1209 CompositeLocation arrayLoc =
1210 checkLocationFromExpressionNode(md, nametable, aan.getExpression(),
1211 new CompositeLocation(), constraint, isLHS);
1213 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
1214 CompositeLocation indexLoc =
1215 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(),
1217 // addTypeLocation(aan.getIndex().getType(), indexLoc);
1220 if (!CompositeLattice.isGreaterThan(indexLoc, arrayLoc, generateErrorMessage(cd, aan))) {
1221 throw new Error("Array index value is not higher than array location at "
1222 + generateErrorMessage(cd, aan));
1226 Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
1227 inputGLB.add(arrayLoc);
1228 inputGLB.add(indexLoc);
1229 return CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(cd, aan));
1234 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
1235 SymbolTable nametable, CreateObjectNode con) {
1237 ClassDescriptor cd = md.getClassDesc();
1239 CompositeLocation compLoc = new CompositeLocation();
1240 compLoc.addLocation(Location.createTopLocation(md));
1245 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
1246 OpNode on, CompositeLocation constraint) {
1248 ClassDescriptor cd = md.getClassDesc();
1249 CompositeLocation leftLoc = new CompositeLocation();
1251 checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc, constraint, false);
1252 // addTypeLocation(on.getLeft().getType(), leftLoc);
1254 CompositeLocation rightLoc = new CompositeLocation();
1255 if (on.getRight() != null) {
1257 checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc, constraint, false);
1258 // addTypeLocation(on.getRight().getType(), rightLoc);
1261 // System.out.println("\n# OP NODE=" + on.printNode(0));
1262 // System.out.println("# left loc=" + leftLoc + " from " +
1263 // on.getLeft().getClass());
1264 // if (on.getRight() != null) {
1265 // System.out.println("# right loc=" + rightLoc + " from " +
1266 // on.getRight().getClass());
1269 Operation op = on.getOp();
1271 switch (op.getOp()) {
1273 case Operation.UNARYPLUS:
1274 case Operation.UNARYMINUS:
1275 case Operation.LOGIC_NOT:
1279 case Operation.LOGIC_OR:
1280 case Operation.LOGIC_AND:
1281 case Operation.COMP:
1282 case Operation.BIT_OR:
1283 case Operation.BIT_XOR:
1284 case Operation.BIT_AND:
1285 case Operation.ISAVAILABLE:
1286 case Operation.EQUAL:
1287 case Operation.NOTEQUAL:
1294 case Operation.MULT:
1297 case Operation.LEFTSHIFT:
1298 case Operation.RIGHTSHIFT:
1299 case Operation.URIGHTSHIFT:
1301 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
1302 inputSet.add(leftLoc);
1303 inputSet.add(rightLoc);
1304 CompositeLocation glbCompLoc =
1305 CompositeLattice.calculateGLB(inputSet, generateErrorMessage(cd, on));
1309 throw new Error(op.toString());
1314 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
1315 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
1317 // literal value has the top location so that value can be flowed into any
1319 Location literalLoc = Location.createTopLocation(md);
1320 loc.addLocation(literalLoc);
1325 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
1326 NameNode nn, CompositeLocation loc, CompositeLocation constraint) {
1328 NameDescriptor nd = nn.getName();
1329 if (nd.getBase() != null) {
1331 checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc, constraint, false);
1333 String varname = nd.toString();
1334 if (varname.equals("this")) {
1336 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
1337 String thisLocId = methodLattice.getThisLoc();
1338 if (thisLocId == null) {
1339 throw new Error("The location for 'this' is not defined at "
1340 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
1342 Location locElement = new Location(md, thisLocId);
1343 loc.addLocation(locElement);
1348 Descriptor d = (Descriptor) nametable.get(varname);
1350 // CompositeLocation localLoc = null;
1351 if (d instanceof VarDescriptor) {
1352 VarDescriptor vd = (VarDescriptor) d;
1353 // localLoc = d2loc.get(vd);
1354 // the type of var descriptor has a composite location!
1355 loc = ((SSJavaType) vd.getType().getExtension()).getCompLoc().clone();
1356 } else if (d instanceof FieldDescriptor) {
1357 // the type of field descriptor has a location!
1358 FieldDescriptor fd = (FieldDescriptor) d;
1359 if (fd.isStatic()) {
1361 // if it is 'static final', the location has TOP since no one can
1363 loc.addLocation(Location.createTopLocation(md));
1366 // if 'static', the location has pre-assigned global loc
1367 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1368 String globalLocId = localLattice.getGlobalLoc();
1369 if (globalLocId == null) {
1370 throw new Error("Global location element is not defined in the method " + md);
1372 Location globalLoc = new Location(md, globalLocId);
1374 loc.addLocation(globalLoc);
1377 // the location of field access starts from this, followed by field
1379 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1380 Location thisLoc = new Location(md, localLattice.getThisLoc());
1381 loc.addLocation(thisLoc);
1384 Location fieldLoc = (Location) fd.getType().getExtension();
1385 loc.addLocation(fieldLoc);
1386 } else if (d == null) {
1387 // access static field
1388 FieldDescriptor fd = nn.getField();
1390 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1391 String globalLocId = localLattice.getGlobalLoc();
1392 if (globalLocId == null) {
1393 throw new Error("Method lattice does not define global variable location at "
1394 + generateErrorMessage(md.getClassDesc(), nn));
1396 loc.addLocation(new Location(md, globalLocId));
1398 Location fieldLoc = (Location) fd.getType().getExtension();
1399 loc.addLocation(fieldLoc);
1408 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
1409 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc,
1410 CompositeLocation constraint) {
1412 ExpressionNode left = fan.getExpression();
1413 TypeDescriptor ltd = left.getType();
1415 FieldDescriptor fd = fan.getField();
1417 String varName = null;
1418 if (left.kind() == Kind.NameNode) {
1419 NameDescriptor nd = ((NameNode) left).getName();
1420 varName = nd.toString();
1423 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
1424 // using a class name directly or access using this
1425 if (fd.isStatic() && fd.isFinal()) {
1426 loc.addLocation(Location.createTopLocation(md));
1431 if (left instanceof ArrayAccessNode) {
1432 ArrayAccessNode aan = (ArrayAccessNode) left;
1433 left = aan.getExpression();
1436 loc = checkLocationFromExpressionNode(md, nametable, left, loc, constraint, false);
1437 // System.out.println("### checkLocationFromFieldAccessNode=" +
1438 // fan.printNode(0));
1439 // System.out.println("### left=" + left.printNode(0));
1441 if (!left.getType().isPrimitive()) {
1443 if (fd.getSymbol().equals("length")) {
1444 // array.length access, return the location of the array
1448 Location fieldLoc = getFieldLocation(fd);
1449 loc.addLocation(fieldLoc);
1454 private Location getFieldLocation(FieldDescriptor fd) {
1456 // System.out.println("### getFieldLocation=" + fd);
1457 // System.out.println("### fd.getType().getExtension()=" +
1458 // fd.getType().getExtension());
1460 Location fieldLoc = (Location) fd.getType().getExtension();
1462 // handle the case that method annotation checking skips checking field
1464 if (fieldLoc == null) {
1465 fieldLoc = checkFieldDeclaration(fd.getClassDescriptor(), fd);
1472 private FieldDescriptor getFieldDescriptorFromExpressionNode(ExpressionNode en) {
1474 if (en.kind() == Kind.NameNode) {
1475 NameNode nn = (NameNode) en;
1476 if (nn.getField() != null) {
1477 return nn.getField();
1480 if (nn.getName() != null && nn.getName().getBase() != null) {
1481 return getFieldDescriptorFromExpressionNode(nn.getExpression());
1484 } else if (en.kind() == Kind.FieldAccessNode) {
1485 FieldAccessNode fan = (FieldAccessNode) en;
1486 return fan.getField();
1492 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
1493 SymbolTable nametable, AssignmentNode an, CompositeLocation loc, CompositeLocation constraint) {
1495 // System.out.println("\n# ASSIGNMENTNODE=" + an.printNode(0));
1497 ClassDescriptor cd = md.getClassDesc();
1499 Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
1501 boolean postinc = true;
1502 if (an.getOperation().getBaseOp() == null
1503 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
1504 .getBaseOp().getOp() != Operation.POSTDEC))
1507 // if LHS is array access node, need to check if array index is higher
1508 // than array itself
1509 CompositeLocation destLocation =
1510 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(),
1513 CompositeLocation rhsLocation;
1514 CompositeLocation srcLocation;
1518 checkOwnership(md, an, an.getSrc());
1521 checkLocationFromExpressionNode(md, nametable, an.getSrc(), new CompositeLocation(),
1524 srcLocation = rhsLocation;
1526 // if (!rhsLocation.get(rhsLocation.getSize() - 1).isTop()) {
1527 if (constraint != null) {
1528 inputGLBSet.add(rhsLocation);
1529 inputGLBSet.add(constraint);
1530 srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
1534 // System.out.println("dstLocation=" + destLocation);
1535 // System.out.println("rhsLocation=" + rhsLocation);
1536 // System.out.println("srcLocation=" + srcLocation);
1537 // System.out.println("constraint=" + constraint);
1539 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
1541 String context = "";
1542 if (constraint != null) {
1543 context = " and the current context constraint is " + constraint;
1546 throw new Error("The value flow from " + srcLocation + " to " + destLocation
1547 + " does not respect location hierarchy on the assignment " + an.printNode(0) + context
1548 + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1551 if (srcLocation.equals(destLocation)) {
1552 // keep it for definitely written analysis
1553 Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
1554 for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
1555 FlatNode fn = (FlatNode) iterator.next();
1556 ssjava.addSameHeightWriteFlatNode(fn);
1564 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation(),
1567 if (constraint != null) {
1568 inputGLBSet.add(rhsLocation);
1569 inputGLBSet.add(constraint);
1570 srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
1572 srcLocation = rhsLocation;
1575 // System.out.println("srcLocation=" + srcLocation);
1576 // System.out.println("rhsLocation=" + rhsLocation);
1577 // System.out.println("constraint=" + constraint);
1579 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
1581 if (srcLocation.equals(destLocation)) {
1582 throw new Error("Location " + srcLocation
1583 + " is not allowed to have the value flow that moves within the same location at '"
1584 + an.printNode(0) + "' of " + cd.getSourceFileName() + "::" + an.getNumLine());
1586 throw new Error("The value flow from " + srcLocation + " to " + destLocation
1587 + " does not respect location hierarchy on the assignment " + an.printNode(0)
1588 + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
1593 if (srcLocation.equals(destLocation)) {
1594 // keep it for definitely written analysis
1595 Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
1596 for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
1597 FlatNode fn = (FlatNode) iterator.next();
1598 ssjava.addSameHeightWriteFlatNode(fn);
1604 return destLocation;
1607 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
1608 SymbolTable nametable, TreeNode n) {
1610 ClassDescriptor cd = md.getClassDesc();
1611 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
1613 // currently enforce every variable to have corresponding location
1614 if (annotationVec.size() == 0) {
1615 throw new Error("Location is not assigned to variable '" + vd.getSymbol()
1616 + "' in the method '" + md + "' of the class " + cd.getSymbol() + " at "
1617 + generateErrorMessage(cd, n));
1620 int locDecCount = 0;
1621 for (int i = 0; i < annotationVec.size(); i++) {
1622 AnnotationDescriptor ad = annotationVec.elementAt(i);
1624 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1626 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1628 if (locDecCount > 1) {// variable can have at most one location
1629 throw new Error(vd.getSymbol() + " has more than one location declaration.");
1631 String locDec = ad.getValue(); // check if location is defined
1633 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
1634 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
1635 d2loc.put(vd, deltaLoc);
1636 addLocationType(vd.getType(), deltaLoc);
1638 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1640 Location lastElement = compLoc.get(compLoc.getSize() - 1);
1641 if (ssjava.isSharedLocation(lastElement)) {
1642 ssjava.mapSharedLocation2Descriptor(lastElement, vd);
1645 d2loc.put(vd, compLoc);
1646 addLocationType(vd.getType(), compLoc);
1655 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1658 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1661 int beginIdx = dIdx + 6;
1662 locDec = locDec.substring(beginIdx, locDec.length() - 1);
1663 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1666 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1667 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
1672 private Location parseFieldLocDeclaraton(String decl, String msg) throws Exception {
1674 int idx = decl.indexOf(".");
1676 String className = decl.substring(0, idx);
1677 String fieldName = decl.substring(idx + 1);
1679 className.replaceAll(" ", "");
1680 fieldName.replaceAll(" ", "");
1682 Descriptor d = state.getClassSymbolTable().get(className);
1685 // System.out.println("state.getClassSymbolTable()=" +
1686 // state.getClassSymbolTable());
1687 throw new Error("The class in the location declaration '" + decl + "' does not exist at "
1691 assert (d instanceof ClassDescriptor);
1692 SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1693 if (!lattice.containsKey(fieldName)) {
1694 throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1695 + className + "' at " + msg);
1698 return new Location(d, fieldName);
1701 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1703 CompositeLocation compLoc = new CompositeLocation();
1705 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1706 List<String> locIdList = new ArrayList<String>();
1707 while (tokenizer.hasMoreTokens()) {
1708 String locId = tokenizer.nextToken();
1709 locIdList.add(locId);
1712 // at least,one location element needs to be here!
1713 assert (locIdList.size() > 0);
1715 // assume that loc with idx 0 comes from the local lattice
1716 // loc with idx 1 comes from the field lattice
1718 String localLocId = locIdList.get(0);
1719 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1720 Location localLoc = new Location(md, localLocId);
1721 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1722 throw new Error("Location " + localLocId
1723 + " is not defined in the local variable lattice at "
1724 + md.getClassDesc().getSourceFileName() + "::" + (n != null ? n.getNumLine() : md) + ".");
1726 compLoc.addLocation(localLoc);
1728 for (int i = 1; i < locIdList.size(); i++) {
1729 String locName = locIdList.get(i);
1732 parseFieldLocDeclaraton(locName, generateErrorMessage(md.getClassDesc(), n));
1733 compLoc.addLocation(fieldLoc);
1734 } catch (Exception e) {
1735 throw new Error("The location declaration '" + locName + "' is wrong at "
1736 + generateErrorMessage(md.getClassDesc(), n));
1744 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1745 VarDescriptor vd = dn.getVarDescriptor();
1746 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1749 private void checkDeclarationInClass(ClassDescriptor cd) {
1750 // Check to see that fields are okay
1751 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1752 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1754 if (!(fd.isFinal() && fd.isStatic())) {
1755 checkFieldDeclaration(cd, fd);
1757 // for static final, assign top location by default
1758 Location loc = Location.createTopLocation(cd);
1759 addLocationType(fd.getType(), loc);
1764 private Location checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1766 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1768 // currently enforce every field to have corresponding location
1769 if (annotationVec.size() == 0) {
1770 throw new Error("Location is not assigned to the field '" + fd.getSymbol()
1771 + "' of the class " + cd.getSymbol() + " at " + cd.getSourceFileName());
1774 if (annotationVec.size() > 1) {
1775 // variable can have at most one location
1776 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1777 + " has more than one location.");
1780 AnnotationDescriptor ad = annotationVec.elementAt(0);
1781 Location loc = null;
1783 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1784 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1785 String locationID = ad.getValue();
1786 // check if location is defined
1787 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1788 if (lattice == null || (!lattice.containsKey(locationID))) {
1789 throw new Error("Location " + locationID
1790 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1791 + cd.getSourceFileName() + ".");
1793 loc = new Location(cd, locationID);
1795 if (ssjava.isSharedLocation(loc)) {
1796 ssjava.mapSharedLocation2Descriptor(loc, fd);
1799 addLocationType(fd.getType(), loc);
1807 private void addLocationType(TypeDescriptor type, CompositeLocation loc) {
1809 TypeExtension te = type.getExtension();
1812 ssType = (SSJavaType) te;
1813 ssType.setCompLoc(loc);
1815 ssType = new SSJavaType(loc);
1816 type.setExtension(ssType);
1821 private void addLocationType(TypeDescriptor type, Location loc) {
1823 type.setExtension(loc);
1827 static class CompositeLattice {
1829 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2, String msg) {
1831 // System.out.println("\nisGreaterThan=" + loc1 + " " + loc2 + " msg=" +
1833 int baseCompareResult = compareBaseLocationSet(loc1, loc2, true, false, msg);
1834 if (baseCompareResult == ComparisonResult.EQUAL) {
1835 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1840 } else if (baseCompareResult == ComparisonResult.GREATER) {
1848 public static int compare(CompositeLocation loc1, CompositeLocation loc2, boolean ignore,
1851 // System.out.println("compare=" + loc1 + " " + loc2);
1852 int baseCompareResult = compareBaseLocationSet(loc1, loc2, false, ignore, msg);
1854 if (baseCompareResult == ComparisonResult.EQUAL) {
1855 return compareDelta(loc1, loc2);
1857 return baseCompareResult;
1862 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1864 int deltaCount1 = 0;
1865 int deltaCount2 = 0;
1866 if (dLoc1 instanceof DeltaLocation) {
1867 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1870 if (dLoc2 instanceof DeltaLocation) {
1871 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1873 if (deltaCount1 < deltaCount2) {
1874 return ComparisonResult.GREATER;
1875 } else if (deltaCount1 == deltaCount2) {
1876 return ComparisonResult.EQUAL;
1878 return ComparisonResult.LESS;
1883 private static int compareBaseLocationSet(CompositeLocation compLoc1,
1884 CompositeLocation compLoc2, boolean awareSharedLoc, boolean ignore, String msg) {
1886 // if compLoc1 is greater than compLoc2, return true
1887 // else return false;
1889 // compare one by one in according to the order of the tuple
1891 for (int i = 0; i < compLoc1.getSize(); i++) {
1892 Location loc1 = compLoc1.get(i);
1893 if (i >= compLoc2.getSize()) {
1895 return ComparisonResult.INCOMPARABLE;
1897 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1898 + " because they are not comparable at " + msg);
1901 Location loc2 = compLoc2.get(i);
1903 Descriptor descriptor = getCommonParentDescriptor(loc1, loc2, msg);
1904 SSJavaLattice<String> lattice = getLatticeByDescriptor(descriptor);
1906 // check if the shared location is appeared only at the end of the
1907 // composite location
1908 if (lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
1909 if (i != (compLoc1.getSize() - 1)) {
1910 throw new Error("The shared location " + loc1.getLocIdentifier()
1911 + " cannot be appeared in the middle of composite location at" + msg);
1915 if (lattice.getSharedLocSet().contains(loc2.getLocIdentifier())) {
1916 if (i != (compLoc2.getSize() - 1)) {
1917 throw new Error("The shared location " + loc2.getLocIdentifier()
1918 + " cannot be appeared in the middle of composite location at " + msg);
1922 // if (!lattice1.equals(lattice2)) {
1923 // throw new Error("Failed to compare two locations of " + compLoc1 +
1924 // " and " + compLoc2
1925 // + " because they are not comparable at " + msg);
1928 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1930 // check if the current location is the spinning location
1931 // note that the spinning location only can be appeared in the last
1932 // part of the composite location
1933 if (awareSharedLoc && numOfTie == compLoc1.getSize()
1934 && lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
1935 return ComparisonResult.GREATER;
1938 } else if (lattice.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1939 return ComparisonResult.GREATER;
1941 return ComparisonResult.LESS;
1946 if (numOfTie == compLoc1.getSize()) {
1948 if (numOfTie != compLoc2.getSize()) {
1951 return ComparisonResult.INCOMPARABLE;
1953 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1954 + " because they are not comparable at " + msg);
1959 return ComparisonResult.EQUAL;
1962 return ComparisonResult.LESS;
1966 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet, String errMsg) {
1968 // System.out.println("Calculating GLB=" + inputSet);
1969 CompositeLocation glbCompLoc = new CompositeLocation();
1971 // calculate GLB of the first(priority) element
1972 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1973 Descriptor priorityDescriptor = null;
1975 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1976 new Hashtable<String, Set<CompositeLocation>>();
1977 // mapping from the priority loc ID to its full representation by the
1978 // composite location
1980 int maxTupleSize = 0;
1981 CompositeLocation maxCompLoc = null;
1983 Location prevPriorityLoc = null;
1984 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1985 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1986 if (compLoc.getSize() > maxTupleSize) {
1987 maxTupleSize = compLoc.getSize();
1988 maxCompLoc = compLoc;
1990 Location priorityLoc = compLoc.get(0);
1991 String priorityLocId = priorityLoc.getLocIdentifier();
1992 priorityLocIdentifierSet.add(priorityLocId);
1994 if (locId2CompLocSet.containsKey(priorityLocId)) {
1995 locId2CompLocSet.get(priorityLocId).add(compLoc);
1997 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1998 newSet.add(compLoc);
1999 locId2CompLocSet.put(priorityLocId, newSet);
2002 // check if priority location are coming from the same lattice
2003 if (priorityDescriptor == null) {
2004 priorityDescriptor = priorityLoc.getDescriptor();
2006 priorityDescriptor = getCommonParentDescriptor(priorityLoc, prevPriorityLoc, errMsg);
2008 prevPriorityLoc = priorityLoc;
2009 // else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
2010 // throw new Error("Failed to calculate GLB of " + inputSet
2011 // + " because they are from different lattices.");
2015 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
2016 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
2018 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
2019 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
2021 if (compSet == null) {
2022 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
2023 // mean that the result is already lower than <x1,y1> and <x2,y2>
2024 // assign TOP to the rest of the location elements
2026 // in this case, do not take care about delta
2027 // CompositeLocation inputComp = inputSet.iterator().next();
2028 for (int i = 1; i < maxTupleSize; i++) {
2029 glbCompLoc.addLocation(Location.createTopLocation(maxCompLoc.get(i).getDescriptor()));
2033 // here find out composite location that has a maximum length tuple
2034 // if we have three input set: [A], [A,B], [A,B,C]
2035 // maximum length tuple will be [A,B,C]
2037 CompositeLocation maxFromCompSet = null;
2038 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2039 CompositeLocation c = (CompositeLocation) iterator.next();
2040 if (c.getSize() > max) {
2046 if (compSet.size() == 1) {
2047 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
2048 CompositeLocation comp = compSet.iterator().next();
2049 for (int i = 1; i < comp.getSize(); i++) {
2050 glbCompLoc.addLocation(comp.get(i));
2053 // if input location corresponding to glb is a delta, need to apply
2054 // delta to glb result
2055 if (comp instanceof DeltaLocation) {
2056 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
2060 // when GLB(x1,x2)==x1 and x2 : GLB case 1
2061 // if more than one location shares the same priority GLB
2062 // need to calculate the rest of GLB loc
2064 // setup input set starting from the second tuple item
2065 Set<CompositeLocation> innerGLBInput = new HashSet<CompositeLocation>();
2066 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2067 CompositeLocation compLoc = (CompositeLocation) iterator.next();
2068 CompositeLocation innerCompLoc = new CompositeLocation();
2069 for (int idx = 1; idx < compLoc.getSize(); idx++) {
2070 innerCompLoc.addLocation(compLoc.get(idx));
2072 if (innerCompLoc.getSize() > 0) {
2073 innerGLBInput.add(innerCompLoc);
2077 if (innerGLBInput.size() > 0) {
2078 CompositeLocation innerGLB = CompositeLattice.calculateGLB(innerGLBInput, errMsg);
2079 for (int idx = 0; idx < innerGLB.getSize(); idx++) {
2080 glbCompLoc.addLocation(innerGLB.get(idx));
2084 // if input location corresponding to glb is a delta, need to apply
2085 // delta to glb result
2087 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
2088 CompositeLocation compLoc = (CompositeLocation) iterator.next();
2089 if (compLoc instanceof DeltaLocation) {
2090 if (glbCompLoc.equals(compLoc)) {
2091 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
2100 // System.out.println("GLB=" + glbCompLoc);
2105 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
2107 SSJavaLattice<String> lattice = null;
2109 if (d instanceof ClassDescriptor) {
2110 lattice = ssjava.getCd2lattice().get(d);
2111 } else if (d instanceof MethodDescriptor) {
2112 if (ssjava.getMd2lattice().containsKey(d)) {
2113 lattice = ssjava.getMd2lattice().get(d);
2115 // use default lattice for the method
2116 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
2123 static Descriptor getCommonParentDescriptor(Location loc1, Location loc2, String msg) {
2125 Descriptor d1 = loc1.getDescriptor();
2126 Descriptor d2 = loc2.getDescriptor();
2128 Descriptor descriptor;
2130 if (d1 instanceof ClassDescriptor && d2 instanceof ClassDescriptor) {
2132 if (d1.equals(d2)) {
2135 // identifying which one is parent class
2136 Set<Descriptor> d1SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d1);
2137 Set<Descriptor> d2SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d2);
2139 if (d1 == null && d2 == null) {
2140 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2141 + " because they are not comparable at " + msg);
2142 } else if (d1SubClassesSet != null && d1SubClassesSet.contains(d2)) {
2144 } else if (d2SubClassesSet != null && d2SubClassesSet.contains(d1)) {
2147 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2148 + " because they are not comparable at " + msg);
2152 } else if (d1 instanceof MethodDescriptor && d2 instanceof MethodDescriptor) {
2154 if (d1.equals(d2)) {
2158 // identifying which one is parent class
2159 MethodDescriptor md1 = (MethodDescriptor) d1;
2160 MethodDescriptor md2 = (MethodDescriptor) d2;
2162 if (!md1.matches(md2)) {
2163 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2164 + " because they are not comparable at " + msg);
2167 Set<Descriptor> d1SubClassesSet =
2168 ssjava.tu.getSubClasses(((MethodDescriptor) d1).getClassDesc());
2169 Set<Descriptor> d2SubClassesSet =
2170 ssjava.tu.getSubClasses(((MethodDescriptor) d2).getClassDesc());
2172 if (d1 == null && d2 == null) {
2173 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2174 + " because they are not comparable at " + msg);
2175 } else if (d1 != null && d1SubClassesSet.contains(d2)) {
2177 } else if (d2 != null && d2SubClassesSet.contains(d1)) {
2180 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2181 + " because they are not comparable at " + msg);
2186 throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
2187 + " because they are not comparable at " + msg);
2196 class ComparisonResult {
2198 public static final int GREATER = 0;
2199 public static final int EQUAL = 1;
2200 public static final int LESS = 2;
2201 public static final int INCOMPARABLE = 3;
2208 class ReturnLocGenerator {
2210 public static final int PARAMISHIGHER = 0;
2211 public static final int PARAMISSAME = 1;
2212 public static final int IGNORE = 2;
2214 private Hashtable<Integer, Integer> paramIdx2paramType;
2216 private CompositeLocation declaredReturnLoc = null;
2218 public ReturnLocGenerator(CompositeLocation returnLoc, MethodDescriptor md,
2219 List<CompositeLocation> params, String msg) {
2221 CompositeLocation thisLoc = params.get(0);
2222 if (returnLoc.get(0).equals(thisLoc.get(0)) && returnLoc.getSize() > 1) {
2223 // if the declared return location consists of THIS and field location,
2224 // return location for the caller's side has to have same field element
2225 this.declaredReturnLoc = returnLoc;
2227 // creating mappings
2228 paramIdx2paramType = new Hashtable<Integer, Integer>();
2229 for (int i = 0; i < params.size(); i++) {
2230 CompositeLocation param = params.get(i);
2231 int compareResult = CompositeLattice.compare(param, returnLoc, true, msg);
2234 if (compareResult == ComparisonResult.GREATER) {
2236 } else if (compareResult == ComparisonResult.EQUAL) {
2241 paramIdx2paramType.put(new Integer(i), new Integer(type));
2247 public CompositeLocation computeReturnLocation(List<CompositeLocation> args) {
2249 if (declaredReturnLoc != null) {
2250 // when developer specify that the return value is [THIS,field]
2251 // needs to translate to the caller's location
2252 CompositeLocation callerLoc = new CompositeLocation();
2253 CompositeLocation callerBaseLocation = args.get(0);
2255 for (int i = 0; i < callerBaseLocation.getSize(); i++) {
2256 callerLoc.addLocation(callerBaseLocation.get(i));
2258 for (int i = 1; i < declaredReturnLoc.getSize(); i++) {
2259 callerLoc.addLocation(declaredReturnLoc.get(i));
2263 // compute the highest possible location in caller's side
2264 assert paramIdx2paramType.keySet().size() == args.size();
2266 Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
2267 for (int i = 0; i < args.size(); i++) {
2268 int type = (paramIdx2paramType.get(new Integer(i))).intValue();
2269 CompositeLocation argLoc = args.get(i);
2270 if (type == PARAMISHIGHER || type == PARAMISSAME) {
2271 // return loc is equal to or lower than param
2272 inputGLB.add(argLoc);
2276 // compute GLB of arguments subset that are same or higher than return
2278 if (inputGLB.isEmpty()) {
2279 CompositeLocation rtr =
2280 new CompositeLocation(Location.createTopLocation(args.get(0).get(0).getDescriptor()));
2283 CompositeLocation glb = CompositeLattice.calculateGLB(inputGLB, "");