1 package Analysis.SSJava;
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.LinkedList;
12 import java.util.List;
15 import java.util.Stack;
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.VarDescriptor;
27 import IR.Tree.ArrayAccessNode;
28 import IR.Tree.AssignmentNode;
29 import IR.Tree.BlockExpressionNode;
30 import IR.Tree.BlockNode;
31 import IR.Tree.BlockStatementNode;
32 import IR.Tree.CastNode;
33 import IR.Tree.CreateObjectNode;
34 import IR.Tree.DeclarationNode;
35 import IR.Tree.ExpressionNode;
36 import IR.Tree.FieldAccessNode;
37 import IR.Tree.IfStatementNode;
39 import IR.Tree.LiteralNode;
40 import IR.Tree.LoopNode;
41 import IR.Tree.MethodInvokeNode;
42 import IR.Tree.NameNode;
43 import IR.Tree.OpNode;
44 import IR.Tree.ReturnNode;
45 import IR.Tree.SubBlockNode;
46 import IR.Tree.SwitchStatementNode;
47 import IR.Tree.TertiaryNode;
48 import IR.Tree.TreeNode;
51 public class LocationInference {
54 SSJavaAnalysis ssjava;
56 List<ClassDescriptor> toanalyzeList;
57 List<MethodDescriptor> toanalyzeMethodList;
58 Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToFlowGraph;
60 // map a method descriptor to its set of parameter descriptors
61 Map<MethodDescriptor, Set<Descriptor>> mapMethodDescriptorToParamDescSet;
63 // keep current descriptors to visit in fixed-point interprocedural analysis,
64 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
66 // map a class descriptor to a field lattice
67 private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
69 // map a method descriptor to a method lattice
70 private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
72 // map a method descriptor to the set of method invocation nodes which are
73 // invoked by the method descriptor
74 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
76 private Map<MethodInvokeNode, Map<Integer, NodeTupleSet>> mapMethodInvokeNodeToArgIdxMap;
78 private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
80 private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
82 private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
84 public static final String GLOBALLOC = "GLOBALLOC";
86 public static final String TOPLOC = "TOPLOC";
88 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
90 public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
92 LocationInfo curMethodInfo;
96 public LocationInference(SSJavaAnalysis ssjava, State state) {
99 this.toanalyzeList = new ArrayList<ClassDescriptor>();
100 this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
101 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
102 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
103 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
104 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
105 this.mapMethodDescriptorToMethodInvokeNodeSet =
106 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
107 this.mapMethodInvokeNodeToArgIdxMap =
108 new HashMap<MethodInvokeNode, Map<Integer, NodeTupleSet>>();
109 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
110 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
111 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
114 public void setupToAnalyze() {
115 SymbolTable classtable = state.getClassSymbolTable();
116 toanalyzeList.clear();
117 toanalyzeList.addAll(classtable.getValueSet());
118 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
119 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
120 return o1.getClassName().compareToIgnoreCase(o2.getClassName());
125 public void setupToAnalazeMethod(ClassDescriptor cd) {
127 SymbolTable methodtable = cd.getMethodTable();
128 toanalyzeMethodList.clear();
129 toanalyzeMethodList.addAll(methodtable.getValueSet());
130 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
131 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
132 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
137 public boolean toAnalyzeMethodIsEmpty() {
138 return toanalyzeMethodList.isEmpty();
141 public boolean toAnalyzeIsEmpty() {
142 return toanalyzeList.isEmpty();
145 public ClassDescriptor toAnalyzeNext() {
146 return toanalyzeList.remove(0);
149 public MethodDescriptor toAnalyzeMethodNext() {
150 return toanalyzeMethodList.remove(0);
153 public void inference() {
155 // 1) construct value flow graph
156 constructFlowGraph();
158 // 2) construct lattices
163 debug_writeLatticeDotFile();
165 // 3) check properties
170 private void simplifyLattices() {
172 // generate lattice dot file
175 while (!toAnalyzeIsEmpty()) {
176 ClassDescriptor cd = toAnalyzeNext();
178 setupToAnalazeMethod(cd);
180 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
181 if (classLattice != null) {
182 classLattice.removeRedundantEdges();
185 while (!toAnalyzeMethodIsEmpty()) {
186 MethodDescriptor md = toAnalyzeMethodNext();
187 if (ssjava.needTobeAnnotated(md)) {
188 SSJavaLattice<String> methodLattice = md2lattice.get(md);
189 if (methodLattice != null) {
190 methodLattice.removeRedundantEdges();
198 private void checkLattices() {
200 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
202 // current descriptors to visit in fixed-point interprocedural analysis,
204 // dependency in the call graph
205 methodDescriptorsToVisitStack.clear();
207 descriptorListToAnalyze.removeFirst();
209 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
210 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
212 while (!descriptorListToAnalyze.isEmpty()) {
213 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
214 checkLatticesOfVirtualMethods(md);
219 private void debug_writeLatticeDotFile() {
220 // generate lattice dot file
224 while (!toAnalyzeIsEmpty()) {
225 ClassDescriptor cd = toAnalyzeNext();
227 setupToAnalazeMethod(cd);
229 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
230 if (classLattice != null) {
231 ssjava.writeLatticeDotFile(cd, null, classLattice);
232 debug_printDescriptorToLocNameMapping(cd);
235 while (!toAnalyzeMethodIsEmpty()) {
236 MethodDescriptor md = toAnalyzeMethodNext();
237 if (ssjava.needTobeAnnotated(md)) {
238 SSJavaLattice<String> methodLattice = md2lattice.get(md);
239 if (methodLattice != null) {
240 ssjava.writeLatticeDotFile(cd, md, methodLattice);
241 debug_printDescriptorToLocNameMapping(md);
249 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
251 LocationInfo info = getLocationInfo(desc);
252 System.out.println("## " + desc + " ##");
253 System.out.println(info.getMapDescToInferLocation());
254 LocationInfo locInfo = getLocationInfo(desc);
255 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
256 System.out.println("###################");
260 private void inferLattices() {
262 // do fixed-point analysis
264 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
266 Collections.sort(descriptorListToAnalyze, new Comparator<MethodDescriptor>() {
267 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
268 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
272 // current descriptors to visit in fixed-point interprocedural analysis,
274 // dependency in the call graph
275 methodDescriptorsToVisitStack.clear();
277 descriptorListToAnalyze.removeFirst();
279 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
280 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
282 while (!descriptorListToAnalyze.isEmpty()) {
283 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
284 methodDescriptorsToVisitStack.add(md);
287 // analyze scheduled methods until there are no more to visit
288 while (!methodDescriptorsToVisitStack.isEmpty()) {
289 // start to analyze leaf node
290 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
292 SSJavaLattice<String> methodLattice =
293 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
295 MethodLocationInfo methodInfo = new MethodLocationInfo(md);
296 curMethodInfo = methodInfo;
298 System.out.println();
299 System.out.println("SSJAVA: Inferencing the lattice from " + md);
302 analyzeMethodLattice(md, methodLattice, methodInfo);
303 } catch (CyclicFlowException e) {
304 throw new Error("Fail to generate the method lattice for " + md);
307 SSJavaLattice<String> prevMethodLattice = getMethodLattice(md);
308 MethodLocationInfo prevMethodInfo = getMethodLocationInfo(md);
310 if ((!methodLattice.equals(prevMethodLattice)) || (!methodInfo.equals(prevMethodInfo))) {
312 setMethodLattice(md, methodLattice);
313 setMethodLocInfo(md, methodInfo);
315 // results for callee changed, so enqueue dependents caller for
317 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
318 while (depsItr.hasNext()) {
319 MethodDescriptor methodNext = depsItr.next();
320 if (!methodDescriptorsToVisitStack.contains(methodNext)
321 && methodDescriptorToVistSet.contains(methodNext)) {
322 methodDescriptorsToVisitStack.add(methodNext);
331 private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
332 mapMethodDescToMethodLocationInfo.put(md, methodInfo);
335 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
337 if (!md.isStatic()) {
338 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
339 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
341 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
342 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
343 if (!md.equals(mdCallee)) {
344 checkConsistency(md, mdCallee);
352 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
354 // check that two lattice have the same relations between parameters(+PC
355 // LOC, GLOBAL_LOC RETURN LOC)
357 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
358 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
360 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
361 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
363 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
364 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
366 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
368 // add location types of paramters
369 for (int idx = 0; idx < numParam; idx++) {
370 list1.add(paramMap1.get(Integer.valueOf(idx)));
371 list2.add(paramMap2.get(Integer.valueOf(idx)));
374 // add program counter location
375 list1.add(locInfo1.getPCLoc());
376 list2.add(locInfo2.getPCLoc());
378 if (!md1.getReturnType().isVoid()) {
379 // add return value location
380 CompositeLocation rtrLoc1 =
381 new CompositeLocation(new Location(md1, locInfo1.getReturnLocName()));
382 CompositeLocation rtrLoc2 =
383 new CompositeLocation(new Location(md2, locInfo2.getReturnLocName()));
388 // add global location type
389 if (md1.isStatic()) {
390 CompositeLocation globalLoc1 =
391 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
392 CompositeLocation globalLoc2 =
393 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
394 list1.add(globalLoc1);
395 list2.add(globalLoc2);
398 for (int i = 0; i < list1.size(); i++) {
399 CompositeLocation locA1 = list1.get(i);
400 CompositeLocation locA2 = list2.get(i);
401 for (int k = 0; k < list1.size(); k++) {
403 CompositeLocation locB1 = list1.get(k);
404 CompositeLocation locB2 = list2.get(k);
405 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
407 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
410 throw new Error("The method " + md1 + " is not consistent with the method " + md2
411 + ".:: They have a different ordering relation between locations (" + locA1 + ","
412 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
420 private String getSymbol(int idx, FlowNode node) {
421 Descriptor desc = node.getDescTuple().get(idx);
422 return desc.getSymbol();
425 private Descriptor getDescriptor(int idx, FlowNode node) {
426 Descriptor desc = node.getDescTuple().get(idx);
430 private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
431 MethodLocationInfo methodInfo) throws CyclicFlowException {
433 // first take a look at method invocation nodes to newly added relations
435 analyzeLatticeMethodInvocationNode(md, methodLattice, methodInfo);
437 if (!md.isStatic()) {
438 // set the this location
439 String thisLocSymbol = md.getThis().getSymbol();
440 methodInfo.setThisLocName(thisLocSymbol);
443 // set the global location
444 methodInfo.setGlobalLocName(LocationInference.GLOBALLOC);
445 methodInfo.mapDescriptorToLocation(GLOBALDESC, new CompositeLocation(
446 new Location(md, GLOBALLOC)));
448 // visit each node of method flow graph
449 FlowGraph fg = getFlowGraph(md);
450 Set<FlowNode> nodeSet = fg.getNodeSet();
452 // for the method lattice, we need to look at the first element of
453 // NTuple<Descriptor>
454 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
455 FlowNode srcNode = (FlowNode) iterator.next();
457 Set<FlowEdge> outEdgeSet = srcNode.getOutEdgeSet();
458 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
459 FlowEdge outEdge = (FlowEdge) iterator2.next();
460 FlowNode dstNode = outEdge.getDst();
462 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
463 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
465 if (outEdge.getInitTuple().equals(srcNodeTuple)
466 && outEdge.getEndTuple().equals(dstNodeTuple)) {
468 if ((srcNodeTuple.size() > 1 && dstNodeTuple.size() > 1)
469 && srcNodeTuple.get(0).equals(dstNodeTuple.get(0))) {
471 // value flows between fields
472 Descriptor desc = srcNodeTuple.get(0);
473 ClassDescriptor classDesc;
475 if (desc.equals(GLOBALDESC)) {
476 classDesc = md.getClassDesc();
478 VarDescriptor varDesc = (VarDescriptor) srcNodeTuple.get(0);
479 classDesc = varDesc.getType().getClassDesc();
482 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, 1);
484 } else if (srcNodeTuple.size() == 1 || dstNodeTuple.size() == 1) {
485 // for the method lattice, we need to look at the first element of
486 // NTuple<Descriptor>
487 // in this case, take a look at connected nodes at the local level
488 addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
491 if (!srcNode.getDescTuple().get(0).equals(dstNode.getDescTuple().get(0))) {
492 // in this case, take a look at connected nodes at the local level
493 addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
495 Descriptor srcDesc = srcNode.getDescTuple().get(0);
496 Descriptor dstDesc = dstNode.getDescTuple().get(0);
497 recursivelyAddCompositeRelation(md, fg, methodInfo, srcNode, dstNode, srcDesc,
499 // recursiveAddRelationToLattice(1, md, srcNode, dstNode);
507 // create mapping from param idx to inferred composite location
510 if (!md.isStatic()) {
511 // add 'this' reference location
513 methodInfo.addMapParamIdxToInferLoc(0, methodInfo.getInferLocation(md.getThis()));
518 for (int idx = 0; idx < md.numParameters(); idx++) {
519 Descriptor paramDesc = md.getParameter(idx);
520 CompositeLocation inferParamLoc = methodInfo.getInferLocation(paramDesc);
521 methodInfo.addMapParamIdxToInferLoc(idx + offset, inferParamLoc);
524 // calculate the initial program counter location
525 // PC location is higher than location types of all parameters
526 String pcLocSymbol = "PCLOC";
527 Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
528 Set<Integer> keySet = mapParamToLoc.keySet();
529 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
530 Integer paramIdx = (Integer) iterator.next();
531 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
532 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
533 if (!methodLattice.isGreaterThan(pcLocSymbol, paramLocLocalSymbol)) {
534 addRelationHigherToLower(methodLattice, methodInfo, pcLocSymbol, paramLocLocalSymbol);
538 // calculate a return location
539 // the return location type is lower than all parameters
540 if (!md.getReturnType().isVoid()) {
542 String returnLocSymbol = "RETURNLOC";
544 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
545 Integer paramIdx = (Integer) iterator.next();
546 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
547 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
548 if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
549 addRelationHigherToLower(methodLattice, methodInfo, paramLocLocalSymbol, returnLocSymbol);
556 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
557 CompositeLocation comp2) {
559 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
561 for (int idx = 0; idx < size; idx++) {
562 Location loc1 = comp1.get(idx);
563 Location loc2 = comp2.get(idx);
565 Descriptor desc1 = loc1.getDescriptor();
566 Descriptor desc2 = loc2.getDescriptor();
568 if (!desc1.equals(desc2)) {
569 throw new Error("Fail to compare " + comp1 + " and " + comp2);
572 String symbol1 = loc1.getLocIdentifier();
573 String symbol2 = loc2.getLocIdentifier();
575 SSJavaLattice<String> lattice;
577 lattice = methodLattice;
579 lattice = getLattice(desc1);
581 if (symbol1.equals(symbol2)) {
583 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
594 private void recursiveAddRelationToLattice(int idx, MethodDescriptor md,
595 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
597 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
598 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
600 if (srcLocSymbol.equals(dstLocSymbol)) {
601 recursiveAddRelationToLattice(idx + 1, md, srcInferLoc, dstInferLoc);
604 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
605 LocationInfo locInfo = getLocationInfo(parentDesc);
607 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
613 private void analyzeLatticeMethodInvocationNode(MethodDescriptor mdCaller,
614 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo)
615 throws CyclicFlowException {
617 // the transformation for a call site propagates all relations between
618 // parameters from the callee
619 // if the method is virtual, it also grab all relations from any possible
622 Set<MethodInvokeNode> setMethodInvokeNode =
623 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
625 if (setMethodInvokeNode != null) {
627 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
628 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
629 MethodDescriptor mdCallee = min.getMethod();
630 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
631 if (mdCallee.isStatic()) {
632 setPossibleCallees.add(mdCallee);
634 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
635 // removes method descriptors that are not invoked by the caller
636 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
637 setPossibleCallees.addAll(calleeSet);
640 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
641 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
642 propagateRelationToCaller(min, mdCaller, possibleMdCallee, methodLattice, methodInfo);
650 private void propagateRelationToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
651 MethodDescriptor possibleMdCallee, SSJavaLattice<String> methodLattice,
652 MethodLocationInfo methodInfo) throws CyclicFlowException {
654 SSJavaLattice<String> calleeLattice = getMethodLattice(possibleMdCallee);
655 MethodLocationInfo calleeLocInfo = getMethodLocationInfo(possibleMdCallee);
656 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
658 int numParam = calleeLocInfo.getNumParam();
659 for (int i = 0; i < numParam; i++) {
660 CompositeLocation param1 = calleeLocInfo.getParamCompositeLocation(i);
661 for (int k = 0; k < numParam; k++) {
663 CompositeLocation param2 = calleeLocInfo.getParamCompositeLocation(k);
664 if (isGreaterThan(getLattice(possibleMdCallee), param1, param2)) {
665 NodeTupleSet argDescTupleSet1 = getNodeTupleSetByArgIdx(min, i);
666 NodeTupleSet argDescTupleSet2 = getNodeTupleSetByArgIdx(min, k);
668 // the callee has the relation in which param1 is higher than param2
669 // therefore, the caller has to have the relation in which arg1 is
672 for (Iterator<NTuple<Descriptor>> iterator = argDescTupleSet1.iterator(); iterator
674 NTuple<Descriptor> argDescTuple1 = iterator.next();
676 for (Iterator<NTuple<Descriptor>> iterator2 = argDescTupleSet2.iterator(); iterator2
678 NTuple<Descriptor> argDescTuple2 = iterator2.next();
680 // retreive inferred location by the local var descriptor
682 NTuple<Location> tuple1 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple1);
683 NTuple<Location> tuple2 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple2);
685 // CompositeLocation higherInferLoc =
686 // methodInfo.getInferLocation(argTuple1.get(0));
687 // CompositeLocation lowerInferLoc =
688 // methodInfo.getInferLocation(argTuple2.get(0));
690 CompositeLocation inferLoc1 = generateInferredCompositeLocation(methodInfo, tuple1);
691 CompositeLocation inferLoc2 = generateInferredCompositeLocation(methodInfo, tuple2);
693 addRelation(methodLattice, methodInfo, inferLoc1, inferLoc2);
706 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
707 NTuple<Location> tuple) {
709 // System.out.println("@@@@@generateInferredCompositeLocation=" + tuple);
710 // System.out.println("generateInferredCompositeLocation=" + tuple + " 0="
711 // + tuple.get(0).getLocDescriptor());
712 // first, retrieve inferred location by the local var descriptor
713 CompositeLocation inferLoc = new CompositeLocation();
715 CompositeLocation localVarInferLoc =
716 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
718 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
720 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
721 inferLoc.addLocation(localVarInferLoc.get(i));
723 // System.out.println("@@@@@localVarInferLoc=" + localVarInferLoc);
725 for (int i = 1; i < tuple.size(); i++) {
726 Location cur = tuple.get(i);
727 Descriptor enclosingDesc = cur.getDescriptor();
728 Descriptor curDesc = cur.getLocDescriptor();
730 Location inferLocElement;
731 if (curDesc == null) {
732 // in this case, we have a newly generated location.
733 // System.out.println("!!! generated location=" +
734 // cur.getLocIdentifier());
735 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
737 String fieldLocSymbol =
738 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
739 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
740 inferLocElement.setLocDescriptor(curDesc);
743 inferLoc.addLocation(inferLocElement);
746 // System.out.println("@@@@@inferLoc=" + inferLoc);
750 private void addRelation(SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo,
751 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
753 System.out.println("--- srcInferLoc=" + srcInferLoc + " dstInferLoc=" + dstInferLoc);
754 String srcLocalLocSymbol = srcInferLoc.get(0).getLocIdentifier();
755 String dstLocalLocSymbol = dstInferLoc.get(0).getLocIdentifier();
757 if (srcInferLoc.getSize() == 1 && dstInferLoc.getSize() == 1) {
758 // add a new relation to the local lattice
759 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
760 } else if (srcInferLoc.getSize() > 1 && dstInferLoc.getSize() > 1) {
761 // both src and dst have assigned to a composite location
763 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
764 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
766 recursivelyAddRelation(1, srcInferLoc, dstInferLoc);
769 // either src or dst has assigned to a composite location
770 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
771 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
775 System.out.println();
779 public LocationInfo getLocationInfo(Descriptor d) {
780 if (d instanceof MethodDescriptor) {
781 return getMethodLocationInfo((MethodDescriptor) d);
783 return getFieldLocationInfo((ClassDescriptor) d);
787 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
789 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
790 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
793 return mapMethodDescToMethodLocationInfo.get(md);
797 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
799 if (!mapClassToLocationInfo.containsKey(cd)) {
800 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
803 return mapClassToLocationInfo.get(cd);
807 private void addRelationToLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
808 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode) throws CyclicFlowException {
810 System.out.println();
811 System.out.println("### addRelationToLattice src=" + srcNode + " dst=" + dstNode);
813 // add a new binary relation of dstNode < srcNode
814 FlowGraph flowGraph = getFlowGraph(md);
816 System.out.println("***** src composite case::");
817 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, srcNode);
819 CompositeLocation srcInferLoc =
820 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
821 CompositeLocation dstInferLoc =
822 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
824 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
825 } catch (CyclicFlowException e) {
826 // there is a cyclic value flow... try to calculate a composite location
827 // for the destination node
828 System.out.println("***** dst composite case::");
829 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, dstNode);
830 CompositeLocation srcInferLoc =
831 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
832 CompositeLocation dstInferLoc =
833 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
835 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
836 } catch (CyclicFlowException e1) {
837 throw new Error("Failed to merge cyclic value flows into a shared location.");
843 private void recursivelyAddRelation(int idx, CompositeLocation srcInferLoc,
844 CompositeLocation dstInferLoc) throws CyclicFlowException {
846 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
847 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
849 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
851 if (srcLocSymbol.equals(dstLocSymbol)) {
852 // check if it is the case of shared location
853 if (srcInferLoc.getSize() == (idx + 1) && dstInferLoc.getSize() == (idx + 1)) {
854 Location inferLocElement = srcInferLoc.get(idx);
855 System.out.println("SET SHARED LOCATION=" + inferLocElement);
856 getLattice(inferLocElement.getDescriptor())
857 .addSharedLoc(inferLocElement.getLocIdentifier());
858 } else if (srcInferLoc.getSize() > (idx + 1) && dstInferLoc.getSize() > (idx + 1)) {
859 recursivelyAddRelation(idx + 1, srcInferLoc, dstInferLoc);
862 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
867 private void recursivelyAddCompositeRelation(MethodDescriptor md, FlowGraph flowGraph,
868 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode, Descriptor srcDesc,
869 Descriptor dstDesc) throws CyclicFlowException {
871 CompositeLocation inferSrcLoc;
872 CompositeLocation inferDstLoc = methodInfo.getInferLocation(dstDesc);
874 if (srcNode.getDescTuple().size() > 1) {
876 inferSrcLoc = new CompositeLocation();
878 NTuple<Location> locTuple = flowGraph.getLocationTuple(srcNode);
879 for (int i = 0; i < locTuple.size(); i++) {
880 inferSrcLoc.addLocation(locTuple.get(i));
884 inferSrcLoc = methodInfo.getInferLocation(srcDesc);
887 if (dstNode.getDescTuple().size() > 1) {
889 inferDstLoc = new CompositeLocation();
891 NTuple<Location> locTuple = flowGraph.getLocationTuple(dstNode);
892 for (int i = 0; i < locTuple.size(); i++) {
893 inferDstLoc.addLocation(locTuple.get(i));
897 inferDstLoc = methodInfo.getInferLocation(dstDesc);
900 recursiveAddRelationToLattice(1, md, inferSrcLoc, inferDstLoc);
903 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
904 NTuple<Location> prefix, NTuple<Location> element) {
906 if (!map.containsKey(prefix)) {
907 map.put(prefix, new HashSet<NTuple<Location>>());
909 map.get(prefix).add(element);
912 private boolean calculateCompositeLocation(FlowGraph flowGraph,
913 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo, FlowNode flowNode)
914 throws CyclicFlowException {
916 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
918 if (localVarDesc.equals(methodInfo.getMethodDesc())) {
922 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
923 Set<FlowNode> reachableNodeSet = flowGraph.getReachableFlowNodeSet(flowNode);
925 Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
926 new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
928 Set<FlowNode> localInNodeSet = new HashSet<FlowNode>();
929 Set<FlowNode> localOutNodeSet = new HashSet<FlowNode>();
931 CompositeLocation flowNodeInferLoc =
932 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(flowNode));
934 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
936 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
937 FlowNode inNode = (FlowNode) iterator.next();
938 NTuple<Location> inNodeTuple = flowGraph.getLocationTuple(inNode);
940 CompositeLocation inNodeInferredLoc =
941 generateInferredCompositeLocation(methodInfo, inNodeTuple);
943 NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
945 if (inNodeTuple.size() > 1) {
946 for (int i = 1; i < inNodeInferredLocTuple.size(); i++) {
947 NTuple<Location> prefix = inNodeInferredLocTuple.subList(0, i);
948 if (!prefixList.contains(prefix)) {
949 prefixList.add(prefix);
951 addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inNodeInferredLocTuple);
954 localInNodeSet.add(inNode);
958 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
959 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
960 int s0 = arg0.size();
961 int s1 = arg1.size();
964 } else if (s0 == s1) {
972 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
973 FlowNode reachableNode = (FlowNode) iterator2.next();
974 if (reachableNode.getDescTuple().size() == 1) {
975 localOutNodeSet.add(reachableNode);
979 // find out reachable nodes that have the longest common prefix
980 for (int i = 0; i < prefixList.size(); i++) {
981 NTuple<Location> curPrefix = prefixList.get(i);
982 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
984 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
985 FlowNode reachableNode = (FlowNode) iterator2.next();
986 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
987 CompositeLocation reachLocInferLoc =
988 generateInferredCompositeLocation(methodInfo, reachLocTuple);
989 if (reachLocInferLoc.getTuple().startsWith(curPrefix)) {
990 reachableCommonPrefixSet.add(reachLocTuple);
994 // check if the lattice has the relation in which higher prefix is
995 // actually lower than the current node
996 CompositeLocation prefixInferLoc = generateInferredCompositeLocation(methodInfo, curPrefix);
997 if (isGreaterThan(methodLattice, flowNodeInferLoc, prefixInferLoc)) {
998 reachableCommonPrefixSet.add(curPrefix);
1001 if (!reachableCommonPrefixSet.isEmpty()) {
1002 // found reachable nodes that start with the prefix curPrefix
1003 // need to assign a composite location
1005 // first, check if there are more than one the set of locations that has
1006 // the same length of the longest reachable prefix, no way to assign
1007 // a composite location to the input local var
1008 prefixSanityCheck(prefixList, i, flowGraph, reachableNodeSet);
1010 Set<NTuple<Location>> incomingCommonPrefixSet =
1011 mapPrefixToIncomingLocTupleSet.get(curPrefix);
1013 int idx = curPrefix.size();
1014 NTuple<Location> element = incomingCommonPrefixSet.iterator().next();
1015 Descriptor desc = element.get(idx).getDescriptor();
1017 SSJavaLattice<String> lattice = getLattice(desc);
1018 LocationInfo locInfo = getLocationInfo(desc);
1020 CompositeLocation inferLocation = methodInfo.getInferLocation(localVarDesc);
1021 CompositeLocation newInferLocation = new CompositeLocation();
1023 if (inferLocation.getTuple().startsWith(curPrefix)) {
1024 // the same infer location is already existed. no need to do
1028 // assign a new composite location
1030 // String oldMethodLocationSymbol =
1031 // inferLocation.get(0).getLocIdentifier();
1032 String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
1033 for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
1034 newInferLocation.addLocation(curPrefix.get(locIdx));
1036 Location fieldLoc = new Location(desc, newLocSymbol);
1037 newInferLocation.addLocation(fieldLoc);
1039 NTuple<Location> locTuple = flowGraph.getLocationTuple(flowNode);
1040 for (int tidx = 1; tidx < locTuple.size(); tidx++) {
1041 Location cur = locTuple.get(tidx);
1042 Descriptor enclosingDesc = cur.getDescriptor();
1043 Descriptor curDesc = cur.getLocDescriptor();
1044 Location inferLocElement;
1045 if (curDesc == null) {
1046 // in this case, we have a newly generated location.
1047 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
1049 String fieldLocSymbol =
1050 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0)
1051 .getLocIdentifier();
1052 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
1053 inferLocElement.setLocDescriptor(curDesc);
1055 newInferLocation.addLocation(inferLocElement);
1058 methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation);
1059 addMapLocSymbolToInferredLocation(methodInfo.getMethodDesc(), localVarDesc,
1061 methodInfo.removeMaplocalVarToLocSet(localVarDesc);
1065 String newlyInsertedLocName =
1066 newInferLocation.get(inferLocation.getSize() - 1).getLocIdentifier();
1068 for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
1069 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1070 Location loc = tuple.get(idx);
1071 String higher = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
1072 addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
1075 for (Iterator iterator = localInNodeSet.iterator(); iterator.hasNext();) {
1076 FlowNode localNode = (FlowNode) iterator.next();
1078 if (localNode.equals(flowNode)) {
1082 CompositeLocation inNodeInferLoc =
1083 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(localNode));
1085 if (isCompositeLocation(inNodeInferLoc)) {
1086 // need to make sure that newLocSymbol is lower than the infernode
1087 // location in the field lattice
1088 System.out.println("----srcNode=" + localNode + " dstNode=" + flowNode);
1089 addRelationToLattice(methodInfo.getMethodDesc(), methodLattice, methodInfo, localNode,
1096 for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
1097 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1098 if (tuple.size() > idx) {
1099 Location loc = tuple.get(idx);
1100 String lower = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
1101 addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
1105 for (Iterator iterator = localOutNodeSet.iterator(); iterator.hasNext();) {
1106 FlowNode localOutNode = (FlowNode) iterator.next();
1108 if (localOutNode.equals(flowNode)) {
1112 CompositeLocation outNodeInferLoc =
1113 generateInferredCompositeLocation(methodInfo,
1114 flowGraph.getLocationTuple(localOutNode));
1116 if (isCompositeLocation(outNodeInferLoc)) {
1117 // need to make sure that newLocSymbol is higher than the infernode
1119 System.out.println("--- srcNode=" + flowNode + " dstNode=" + localOutNode);
1120 addRelationToLattice(methodInfo.getMethodDesc(), methodLattice, methodInfo, flowNode,
1135 private void addMapLocSymbolToInferredLocation(MethodDescriptor md, Descriptor localVar,
1136 CompositeLocation inferLoc) {
1138 Location locElement = inferLoc.get((inferLoc.getSize() - 1));
1139 Descriptor enclosingDesc = locElement.getDescriptor();
1140 LocationInfo locInfo = getLocationInfo(enclosingDesc);
1141 locInfo.addMapLocSymbolToRelatedInferLoc(locElement.getLocIdentifier(), md, localVar);
1144 private boolean isCompositeLocation(CompositeLocation cl) {
1145 return cl.getSize() > 1;
1148 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
1149 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
1150 Descriptor desc = (Descriptor) iterator.next();
1152 if (desc.equals(LocationInference.GLOBALDESC)) {
1154 } else if (desc instanceof VarDescriptor) {
1155 if (!((VarDescriptor) desc).getType().isPrimitive()) {
1158 } else if (desc instanceof FieldDescriptor) {
1159 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
1168 private void addRelationHigherToLower(SSJavaLattice<String> lattice, LocationInfo locInfo,
1169 String higher, String lower) throws CyclicFlowException {
1171 // if (higher.equals(lower) && lattice.isSharedLoc(higher)) {
1174 Set<String> cycleElementSet = lattice.getPossibleCycleElements(higher, lower);
1176 boolean hasNonPrimitiveElement = false;
1177 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1178 String cycleElementLocSymbol = (String) iterator.next();
1180 Set<Descriptor> descSet = locInfo.getDescSet(cycleElementLocSymbol);
1181 if (containsNonPrimitiveElement(descSet)) {
1182 hasNonPrimitiveElement = true;
1187 if (hasNonPrimitiveElement) {
1188 System.out.println("#Check cycle= " + lower + " < " + higher + " cycleElementSet="
1190 // if there is non-primitive element in the cycle, no way to merge cyclic
1191 // elements into the shared location
1192 throw new CyclicFlowException();
1195 if (cycleElementSet.size() > 0) {
1197 String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
1199 System.out.println("$$$ASSIGN NEW SHARED LOC=" + newSharedLoc + " to " + cycleElementSet);
1200 lattice.mergeIntoSharedLocation(cycleElementSet, newSharedLoc);
1202 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1203 String oldLocSymbol = (String) iterator.next();
1205 Set<Pair<Descriptor, Descriptor>> inferLocSet = locInfo.getRelatedInferLocSet(oldLocSymbol);
1206 System.out.println("$$$update related locations=" + inferLocSet);
1207 for (Iterator iterator2 = inferLocSet.iterator(); iterator2.hasNext();) {
1208 Pair<Descriptor, Descriptor> pair = (Pair<Descriptor, Descriptor>) iterator2.next();
1209 Descriptor enclosingDesc = pair.getFirst();
1210 Descriptor desc = pair.getSecond();
1212 CompositeLocation inferLoc;
1213 if (curMethodInfo.md.equals(enclosingDesc)) {
1214 inferLoc = curMethodInfo.getInferLocation(desc);
1216 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1219 Location locElement = inferLoc.get(inferLoc.getSize() - 1);
1221 locElement.setLocIdentifier(newSharedLoc);
1222 locInfo.addMapLocSymbolToRelatedInferLoc(newSharedLoc, enclosingDesc, desc);
1224 if (curMethodInfo.md.equals(enclosingDesc)) {
1225 inferLoc = curMethodInfo.getInferLocation(desc);
1227 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1229 System.out.println("$$$New Infer Loc=" + inferLoc);
1232 locInfo.removeRelatedInferLocSet(oldLocSymbol, newSharedLoc);
1236 lattice.addSharedLoc(newSharedLoc);
1238 } else if (!lattice.isGreaterThan(higher, lower)) {
1239 lattice.addRelationHigherToLower(higher, lower);
1243 private void replaceOldLocWithNewLoc(SSJavaLattice<String> methodLattice, String oldLocSymbol,
1244 String newLocSymbol) {
1246 if (methodLattice.containsKey(oldLocSymbol)) {
1247 methodLattice.substituteLocation(oldLocSymbol, newLocSymbol);
1252 private void prefixSanityCheck(List<NTuple<Location>> prefixList, int curIdx,
1253 FlowGraph flowGraph, Set<FlowNode> reachableNodeSet) {
1255 NTuple<Location> curPrefix = prefixList.get(curIdx);
1257 for (int i = curIdx + 1; i < prefixList.size(); i++) {
1258 NTuple<Location> prefixTuple = prefixList.get(i);
1260 if (curPrefix.startsWith(prefixTuple)) {
1264 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1265 FlowNode reachableNode = (FlowNode) iterator2.next();
1266 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1267 if (reachLocTuple.startsWith(prefixTuple)) {
1269 throw new Error("Failed to generate a composite location");
1275 public boolean isPrimitiveLocalVariable(FlowNode node) {
1276 VarDescriptor varDesc = (VarDescriptor) node.getDescTuple().get(0);
1277 return varDesc.getType().isPrimitive();
1280 private SSJavaLattice<String> getLattice(Descriptor d) {
1281 if (d instanceof MethodDescriptor) {
1282 return getMethodLattice((MethodDescriptor) d);
1284 return getFieldLattice((ClassDescriptor) d);
1288 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
1289 if (!md2lattice.containsKey(md)) {
1290 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1292 return md2lattice.get(md);
1295 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
1296 md2lattice.put(md, lattice);
1299 private void extractRelationFromFieldFlows(ClassDescriptor cd, FlowNode srcNode,
1300 FlowNode dstNode, int idx) throws CyclicFlowException {
1302 if (srcNode.getDescTuple().get(idx).equals(dstNode.getDescTuple().get(idx))
1303 && srcNode.getDescTuple().size() > (idx + 1) && dstNode.getDescTuple().size() > (idx + 1)) {
1304 // value flow between fields: we don't need to add a binary relation
1307 Descriptor desc = srcNode.getDescTuple().get(idx);
1308 ClassDescriptor classDesc;
1311 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
1313 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1316 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, idx + 1);
1320 Descriptor srcFieldDesc = srcNode.getDescTuple().get(idx);
1321 Descriptor dstFieldDesc = dstNode.getDescTuple().get(idx);
1323 // add a new binary relation of dstNode < srcNode
1324 SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
1325 LocationInfo fieldInfo = getFieldLocationInfo(cd);
1327 String srcSymbol = fieldInfo.getFieldInferLocation(srcFieldDesc).getLocIdentifier();
1328 String dstSymbol = fieldInfo.getFieldInferLocation(dstFieldDesc).getLocIdentifier();
1330 addRelationHigherToLower(fieldLattice, fieldInfo, srcSymbol, dstSymbol);
1336 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
1337 if (!cd2lattice.containsKey(cd)) {
1338 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1340 return cd2lattice.get(cd);
1343 public void constructFlowGraph() {
1347 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
1348 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
1350 while (!toAnalyzeIsEmpty()) {
1351 ClassDescriptor cd = toAnalyzeNext();
1353 setupToAnalazeMethod(cd);
1354 toanalyzeMethodList.removeAll(visited);
1356 while (!toAnalyzeMethodIsEmpty()) {
1357 MethodDescriptor md = toAnalyzeMethodNext();
1358 if ((!visited.contains(md))
1359 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
1360 if (state.SSJAVADEBUG) {
1361 System.out.println();
1362 System.out.println("SSJAVA: Constructing a flow graph: " + md);
1365 // creates a mapping from a method descriptor to virtual methods
1366 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1367 if (md.isStatic()) {
1368 setPossibleCallees.add(md);
1370 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
1373 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
1374 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
1376 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1377 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
1378 if ((!ssjava.isTrustMethod(calleemd))
1379 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
1380 if (!visited.contains(calleemd)) {
1381 toanalyzeMethodList.add(calleemd);
1383 reachableCallee.add(calleemd);
1384 needToAnalyzeCalleeSet.add(calleemd);
1388 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
1390 // creates a mapping from a parameter descriptor to its index
1391 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
1392 int offset = md.isStatic() ? 0 : 1;
1393 for (int i = 0; i < md.numParameters(); i++) {
1394 Descriptor paramDesc = (Descriptor) md.getParameter(i);
1395 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
1398 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
1399 mapMethodDescriptorToFlowGraph.put(md, fg);
1402 analyzeMethodBody(cd, md);
1408 _debug_printGraph();
1411 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
1412 BlockNode bn = state.getMethodBody(md);
1413 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
1414 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
1417 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
1418 NodeTupleSet implicitFlowTupleSet) {
1420 bn.getVarTable().setParent(nametable);
1421 for (int i = 0; i < bn.size(); i++) {
1422 BlockStatementNode bsn = bn.get(i);
1423 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1428 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
1429 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
1431 switch (bsn.kind()) {
1432 case Kind.BlockExpressionNode:
1433 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
1436 case Kind.DeclarationNode:
1437 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
1440 case Kind.IfStatementNode:
1441 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
1445 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
1448 case Kind.ReturnNode:
1449 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
1452 case Kind.SubBlockNode:
1453 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
1456 case Kind.ContinueBreakNode:
1459 case Kind.SwitchStatementNode:
1460 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
1467 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
1468 SwitchStatementNode bsn) {
1469 // TODO Auto-generated method stub
1472 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
1473 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
1474 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
1477 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
1478 NodeTupleSet implicitFlowTupleSet) {
1480 ExpressionNode returnExp = rn.getReturnExpression();
1482 if (returnExp != null) {
1483 NodeTupleSet nodeSet = new NodeTupleSet();
1484 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
1486 FlowGraph fg = getFlowGraph(md);
1488 // annotate the elements of the node set as the return location
1489 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1490 NTuple<Descriptor> returnDescTuple = (NTuple<Descriptor>) iterator.next();
1491 fg.setReturnFlowNode(returnDescTuple);
1492 for (Iterator iterator2 = implicitFlowTupleSet.iterator(); iterator2.hasNext();) {
1493 NTuple<Descriptor> implicitFlowDescTuple = (NTuple<Descriptor>) iterator2.next();
1494 fg.addValueFlowEdge(implicitFlowDescTuple, returnDescTuple);
1501 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
1502 NodeTupleSet implicitFlowTupleSet) {
1504 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
1506 NodeTupleSet condTupleNode = new NodeTupleSet();
1507 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
1508 implicitFlowTupleSet, false);
1509 condTupleNode.addTupleSet(implicitFlowTupleSet);
1511 // add edges from condNodeTupleSet to all nodes of conditional nodes
1512 analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
1515 // check 'for loop' case
1516 BlockNode bn = ln.getInitializer();
1517 bn.getVarTable().setParent(nametable);
1518 for (int i = 0; i < bn.size(); i++) {
1519 BlockStatementNode bsn = bn.get(i);
1520 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1523 NodeTupleSet condTupleNode = new NodeTupleSet();
1524 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
1525 implicitFlowTupleSet, false);
1526 condTupleNode.addTupleSet(implicitFlowTupleSet);
1528 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), condTupleNode);
1529 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), condTupleNode);
1535 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
1536 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
1538 NodeTupleSet condTupleNode = new NodeTupleSet();
1539 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
1540 implicitFlowTupleSet, false);
1542 // add edges from condNodeTupleSet to all nodes of conditional nodes
1543 condTupleNode.addTupleSet(implicitFlowTupleSet);
1544 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), condTupleNode);
1546 if (isn.getFalseBlock() != null) {
1547 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), condTupleNode);
1552 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
1553 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
1555 VarDescriptor vd = dn.getVarDescriptor();
1556 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
1558 getFlowGraph(md).createNewFlowNode(tupleLHS);
1560 if (dn.getExpression() != null) {
1562 NodeTupleSet tupleSetRHS = new NodeTupleSet();
1563 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), tupleSetRHS, null,
1564 implicitFlowTupleSet, false);
1566 // add a new flow edge from rhs to lhs
1567 for (Iterator<NTuple<Descriptor>> iter = tupleSetRHS.iterator(); iter.hasNext();) {
1568 NTuple<Descriptor> from = iter.next();
1569 addFlowGraphEdge(md, from, tupleLHS);
1576 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
1577 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
1578 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
1582 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
1583 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
1584 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
1587 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
1588 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
1589 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
1591 // note that expression node can create more than one flow node
1592 // nodeSet contains of flow nodes
1593 // base is always assigned to null except the case of a name node!
1595 NTuple<Descriptor> flowTuple;
1597 switch (en.kind()) {
1599 case Kind.AssignmentNode:
1600 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, base, implicitFlowTupleSet);
1603 case Kind.FieldAccessNode:
1605 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
1606 implicitFlowTupleSet, isLHS);
1607 if (flowTuple != null) {
1608 nodeSet.addTuple(flowTuple);
1613 NodeTupleSet nameNodeSet = new NodeTupleSet();
1615 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
1616 if (flowTuple != null) {
1617 nodeSet.addTuple(flowTuple);
1622 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
1625 case Kind.CreateObjectNode:
1626 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
1629 case Kind.ArrayAccessNode:
1630 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
1633 case Kind.LiteralNode:
1634 analyzeLiteralNode(md, nametable, (LiteralNode) en);
1637 case Kind.MethodInvokeNode:
1638 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, implicitFlowTupleSet);
1641 case Kind.TertiaryNode:
1642 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
1646 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
1648 // case Kind.InstanceOfNode:
1649 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
1652 // case Kind.ArrayInitializerNode:
1653 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
1657 // case Kind.ClassTypeNode:
1658 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
1661 // case Kind.OffsetNode:
1662 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
1670 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
1671 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
1673 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
1674 implicitFlowTupleSet, false);
1678 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
1679 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
1681 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
1682 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
1683 implicitFlowTupleSet, false);
1685 // add edges from tertiaryTupleNode to all nodes of conditional nodes
1686 tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
1687 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
1688 implicitFlowTupleSet, false);
1690 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
1691 implicitFlowTupleSet, false);
1693 nodeSet.addTupleSet(tertiaryTupleNode);
1697 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
1698 MethodInvokeNode min) {
1699 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
1701 set = new HashSet<MethodInvokeNode>();
1702 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
1707 private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
1708 MethodInvokeNode min, NodeTupleSet implicitFlowTupleSet) {
1710 addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
1712 MethodDescriptor calleeMD = min.getMethod();
1714 NameDescriptor baseName = min.getBaseName();
1715 boolean isSystemout = false;
1716 if (baseName != null) {
1717 isSystemout = baseName.getSymbol().equals("System.out");
1720 if (!ssjava.isSSJavaUtil(calleeMD.getClassDesc()) && !ssjava.isTrustMethod(calleeMD)
1721 && !calleeMD.getModifiers().isNative() && !isSystemout) {
1723 // CompositeLocation baseLocation = null;
1724 if (min.getExpression() != null) {
1726 NodeTupleSet baseNodeSet = new NodeTupleSet();
1727 analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
1728 implicitFlowTupleSet, false);
1731 if (min.getMethod().isStatic()) {
1732 // String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
1733 // if (globalLocId == null) {
1735 // Error("Method lattice does not define global variable location at "
1736 // + generateErrorMessage(md.getClassDesc(), min));
1738 // baseLocation = new CompositeLocation(new Location(md,
1742 // String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
1743 // baseLocation = new CompositeLocation(new Location(md, thisLocId));
1748 // if (constraint != null) {
1749 // int compareResult =
1750 // CompositeLattice.compare(constraint, baseLocation, true,
1751 // generateErrorMessage(cd, min));
1752 // if (compareResult != ComparisonResult.GREATER) {
1753 // // if the current constraint is higher than method's THIS location
1754 // // no need to check constraints!
1755 // CompositeLocation calleeConstraint =
1756 // translateCallerLocToCalleeLoc(calleeMD, baseLocation, constraint);
1757 // // System.out.println("check method body for constraint:" + calleeMD +
1758 // // " calleeConstraint="
1759 // // + calleeConstraint);
1760 // checkMethodBody(calleeMD.getClassDesc(), calleeMD, calleeConstraint);
1764 analyzeFlowMethodParameters(md, nametable, min);
1766 // checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
1768 // checkCallerArgumentLocationConstraints(md, nametable, min,
1769 // baseLocation, constraint);
1771 if (min.getMethod().getReturnType() != null && !min.getMethod().getReturnType().isVoid()) {
1772 // If method has a return value, compute the highest possible return
1773 // location in the caller's perspective
1774 // CompositeLocation ceilingLoc =
1775 // computeCeilingLocationForCaller(md, nametable, min, baseLocation,
1777 // return ceilingLoc;
1781 // return new CompositeLocation(Location.createTopLocation(md));
1785 private NodeTupleSet getNodeTupleSetByArgIdx(MethodInvokeNode min, int idx) {
1786 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
1789 private void addArgIdxMap(MethodInvokeNode min, int idx, NodeTupleSet tupleSet) {
1790 Map<Integer, NodeTupleSet> mapIdxToTupleSet = mapMethodInvokeNodeToArgIdxMap.get(min);
1791 if (mapIdxToTupleSet == null) {
1792 mapIdxToTupleSet = new HashMap<Integer, NodeTupleSet>();
1793 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTupleSet);
1795 mapIdxToTupleSet.put(new Integer(idx), tupleSet);
1798 private void analyzeFlowMethodParameters(MethodDescriptor callermd, SymbolTable nametable,
1799 MethodInvokeNode min) {
1801 if (min.numArgs() > 0) {
1804 if (min.getMethod().isStatic()) {
1808 NTuple<Descriptor> thisArgTuple = new NTuple<Descriptor>();
1809 thisArgTuple.add(callermd.getThis());
1810 NodeTupleSet argTupleSet = new NodeTupleSet();
1811 argTupleSet.addTuple(thisArgTuple);
1812 addArgIdxMap(min, 0, argTupleSet);
1815 for (int i = 0; i < min.numArgs(); i++) {
1816 ExpressionNode en = min.getArg(i);
1817 NodeTupleSet argTupleSet = new NodeTupleSet();
1818 analyzeFlowExpressionNode(callermd, nametable, en, argTupleSet, false);
1819 // if argument is liternal node, argTuple is set to NULL.
1820 addArgIdxMap(min, i + offset, argTupleSet);
1827 private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
1831 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
1832 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
1834 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
1835 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
1837 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
1838 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
1841 // need to create an edge from idx to array
1843 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
1844 NTuple<Descriptor> idxTuple = idxIter.next();
1845 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
1846 NTuple<Descriptor> arrTuple = arrIter.next();
1847 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
1851 nodeSet.addTupleSet(expNodeTupleSet);
1853 nodeSet.addTupleSet(expNodeTupleSet);
1854 nodeSet.addTupleSet(idxNodeTupleSet);
1858 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
1859 CreateObjectNode en) {
1860 // TODO Auto-generated method stub
1864 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
1865 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
1867 NodeTupleSet leftOpSet = new NodeTupleSet();
1868 NodeTupleSet rightOpSet = new NodeTupleSet();
1871 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
1874 if (on.getRight() != null) {
1876 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
1877 implicitFlowTupleSet, false);
1880 Operation op = on.getOp();
1882 switch (op.getOp()) {
1884 case Operation.UNARYPLUS:
1885 case Operation.UNARYMINUS:
1886 case Operation.LOGIC_NOT:
1888 nodeSet.addTupleSet(leftOpSet);
1891 case Operation.LOGIC_OR:
1892 case Operation.LOGIC_AND:
1893 case Operation.COMP:
1894 case Operation.BIT_OR:
1895 case Operation.BIT_XOR:
1896 case Operation.BIT_AND:
1897 case Operation.ISAVAILABLE:
1898 case Operation.EQUAL:
1899 case Operation.NOTEQUAL:
1906 case Operation.MULT:
1909 case Operation.LEFTSHIFT:
1910 case Operation.RIGHTSHIFT:
1911 case Operation.URIGHTSHIFT:
1913 // there are two operands
1914 nodeSet.addTupleSet(leftOpSet);
1915 nodeSet.addTupleSet(rightOpSet);
1919 throw new Error(op.toString());
1924 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
1925 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
1928 base = new NTuple<Descriptor>();
1931 NameDescriptor nd = nn.getName();
1933 if (nd.getBase() != null) {
1935 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
1936 implicitFlowTupleSet, false);
1938 // base node has the top location
1942 String varname = nd.toString();
1943 if (varname.equals("this")) {
1945 base.add(md.getThis());
1949 Descriptor d = (Descriptor) nametable.get(varname);
1951 if (d instanceof VarDescriptor) {
1952 VarDescriptor vd = (VarDescriptor) d;
1954 } else if (d instanceof FieldDescriptor) {
1955 // the type of field descriptor has a location!
1956 FieldDescriptor fd = (FieldDescriptor) d;
1957 if (fd.isStatic()) {
1959 // if it is 'static final', no need to have flow node for the TOP
1963 // if 'static', assign the default GLOBAL LOCATION to the first
1964 // element of the tuple
1965 base.add(GLOBALDESC);
1968 // the location of field access starts from this, followed by field
1970 base.add(md.getThis());
1974 } else if (d == null) {
1975 // access static field
1976 base.add(GLOBALDESC);
1977 // base.add(nn.getField());
1980 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
1982 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1983 // String globalLocId = localLattice.getGlobalLoc();
1984 // if (globalLocId == null) {
1986 // Error("Method lattice does not define global variable location at "
1987 // + generateErrorMessage(md.getClassDesc(), nn));
1989 // loc.addLocation(new Location(md, globalLocId));
1991 // Location fieldLoc = (Location) fd.getType().getExtension();
1992 // loc.addLocation(fieldLoc);
1999 getFlowGraph(md).createNewFlowNode(base);
2005 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
2006 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2007 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
2009 ExpressionNode left = fan.getExpression();
2010 TypeDescriptor ltd = left.getType();
2011 FieldDescriptor fd = fan.getField();
2013 String varName = null;
2014 if (left.kind() == Kind.NameNode) {
2015 NameDescriptor nd = ((NameNode) left).getName();
2016 varName = nd.toString();
2019 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
2020 // using a class name directly or access using this
2021 if (fd.isStatic() && fd.isFinal()) {
2026 if (left instanceof ArrayAccessNode) {
2028 ArrayAccessNode aan = (ArrayAccessNode) left;
2029 left = aan.getExpression();
2030 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), nodeSet, base, implicitFlowTupleSet,
2035 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
2037 // in this case, field is TOP location
2041 if (!left.getType().isPrimitive()) {
2043 if (!fd.getSymbol().equals("length")) {
2044 // array.length access, just have the location of the array
2050 getFlowGraph(md).createNewFlowNode(base);
2057 private void debug_printTreeNode(TreeNode tn) {
2059 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
2063 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
2064 AssignmentNode an, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
2066 NodeTupleSet nodeSetRHS = new NodeTupleSet();
2067 NodeTupleSet nodeSetLHS = new NodeTupleSet();
2069 boolean postinc = true;
2070 if (an.getOperation().getBaseOp() == null
2071 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
2072 .getBaseOp().getOp() != Operation.POSTDEC)) {
2075 // if LHS is array access node, need to capture value flows between an array
2076 // and its index value
2077 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
2081 // analyze value flows of rhs expression
2082 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
2085 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
2086 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
2087 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
2088 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
2089 // System.out.println("-");
2091 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
2092 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
2093 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
2094 NTuple<Descriptor> fromTuple = iter.next();
2095 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2096 NTuple<Descriptor> toTuple = iter2.next();
2097 addFlowGraphEdge(md, fromTuple, toTuple);
2102 // creates edges from RHS to LHS
2103 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
2104 NTuple<Descriptor> fromTuple = iter.next();
2105 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2106 NTuple<Descriptor> toTuple = iter2.next();
2107 addFlowGraphEdge(md, fromTuple, toTuple);
2111 // creates edges from implicitFlowTupleSet to LHS
2112 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2113 NTuple<Descriptor> fromTuple = iter.next();
2114 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2115 NTuple<Descriptor> toTuple = iter2.next();
2116 addFlowGraphEdge(md, fromTuple, toTuple);
2122 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2123 NTuple<Descriptor> tuple = iter2.next();
2124 addFlowGraphEdge(md, tuple, tuple);
2127 // creates edges from implicitFlowTupleSet to LHS
2128 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2129 NTuple<Descriptor> fromTuple = iter.next();
2130 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2131 NTuple<Descriptor> toTuple = iter2.next();
2132 addFlowGraphEdge(md, fromTuple, toTuple);
2140 public FlowGraph getFlowGraph(MethodDescriptor md) {
2141 return mapMethodDescriptorToFlowGraph.get(md);
2144 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
2145 NTuple<Descriptor> to) {
2147 // return true if it adds a new edge
2148 FlowGraph graph = getFlowGraph(md);
2149 graph.addValueFlowEdge(from, to);
2153 public void _debug_printGraph() {
2154 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
2156 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
2157 MethodDescriptor md = (MethodDescriptor) iterator.next();
2158 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
2161 } catch (IOException e) {
2162 e.printStackTrace();
2170 class CyclicFlowException extends Exception {