1 package Analysis.SSJava;
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Comparator;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.LinkedList;
15 import java.util.List;
18 import java.util.Stack;
19 import java.util.Vector;
21 import IR.ClassDescriptor;
23 import IR.FieldDescriptor;
24 import IR.MethodDescriptor;
25 import IR.NameDescriptor;
28 import IR.SymbolTable;
29 import IR.TypeDescriptor;
31 import IR.VarDescriptor;
32 import IR.Tree.ArrayAccessNode;
33 import IR.Tree.AssignmentNode;
34 import IR.Tree.BlockExpressionNode;
35 import IR.Tree.BlockNode;
36 import IR.Tree.BlockStatementNode;
37 import IR.Tree.CastNode;
38 import IR.Tree.CreateObjectNode;
39 import IR.Tree.DeclarationNode;
40 import IR.Tree.ExpressionNode;
41 import IR.Tree.FieldAccessNode;
42 import IR.Tree.IfStatementNode;
44 import IR.Tree.LiteralNode;
45 import IR.Tree.LoopNode;
46 import IR.Tree.MethodInvokeNode;
47 import IR.Tree.NameNode;
48 import IR.Tree.OpNode;
49 import IR.Tree.ReturnNode;
50 import IR.Tree.SubBlockNode;
51 import IR.Tree.SwitchBlockNode;
52 import IR.Tree.SwitchStatementNode;
53 import IR.Tree.TertiaryNode;
54 import IR.Tree.TreeNode;
57 public class LocationInference {
62 SSJavaAnalysis ssjava;
65 List<ClassDescriptor> temp_toanalyzeList;
66 List<MethodDescriptor> temp_toanalyzeMethodList;
67 Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToFlowGraph;
69 LinkedList<MethodDescriptor> toanalyze_methodDescList;
70 Set<ClassDescriptor> toanalyze_classDescSet;
72 // InheritanceTree<ClassDescriptor> inheritanceTree;
74 // map a method descriptor to its set of parameter descriptors
75 Map<MethodDescriptor, Set<Descriptor>> mapMethodDescriptorToParamDescSet;
77 // keep current descriptors to visit in fixed-point interprocedural analysis,
78 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
80 // map a class descriptor to a field lattice
81 private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
83 // map a method descriptor to a method lattice
84 private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
86 // map a method/class descriptor to a hierarchy graph
87 private Map<Descriptor, HierarchyGraph> mapDescriptorToHierarchyGraph;
89 // map a method/class descriptor to a skeleton hierarchy graph
90 private Map<Descriptor, HierarchyGraph> mapDescriptorToSkeletonHierarchyGraph;
92 private Map<Descriptor, HierarchyGraph> mapDescriptorToSimpleHierarchyGraph;
94 // map a method/class descriptor to a skeleton hierarchy graph with combination nodes
95 private Map<Descriptor, HierarchyGraph> mapDescriptorToCombineSkeletonHierarchyGraph;
97 // map a descriptor to a simple lattice
98 private Map<Descriptor, SSJavaLattice<String>> mapDescriptorToSimpleLattice;
100 // map a method descriptor to the set of method invocation nodes which are
101 // invoked by the method descriptor
102 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
104 private Map<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>> mapMethodInvokeNodeToArgIdxMap;
106 private Map<MethodInvokeNode, NTuple<Descriptor>> mapMethodInvokeNodeToBaseTuple;
108 private Map<MethodInvokeNode, Set<NTuple<Location>>> mapMethodInvokeNodeToPCLocTupleSet;
110 private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
112 private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
114 private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
116 private Map<MethodDescriptor, Set<FlowNode>> mapMethodDescToParamNodeFlowsToReturnValue;
118 private Map<String, Vector<String>> mapFileNameToLineVector;
120 private Map<Descriptor, Integer> mapDescToDefinitionLine;
122 private Map<Descriptor, LocationSummary> mapDescToLocationSummary;
124 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescToMethodInvokeNodeSet;
126 // maps a method descriptor to a sub global flow graph that captures all value flows caused by the
127 // set of callees reachable from the method
128 private Map<MethodDescriptor, GlobalFlowGraph> mapMethodDescriptorToSubGlobalFlowGraph;
130 private Map<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>> mapMethodInvokeNodeToMapCallerArgToCalleeArg;
132 private Map<MethodDescriptor, Boolean> mapMethodDescriptorToCompositeReturnCase;
134 private Map<MethodDescriptor, MethodDescriptor> mapMethodDescToHighestOverriddenMethodDesc;
136 private Map<MethodDescriptor, Set<MethodDescriptor>> mapHighestOverriddenMethodDescToMethodDescSet;
138 private Map<MethodDescriptor, Set<NTuple<Descriptor>>> mapHighestOverriddenMethodDescToSetHigherThanRLoc;
140 private Map<MethodDescriptor, NTuple<Descriptor>> mapHighestOverriddenMethodDescToReturnLocTuple;
142 private Map<MethodDescriptor, NTuple<Descriptor>> mapHighestOverriddenMethodDescToPCLocTuple;
144 private Map<MethodDescriptor, Set<NTuple<Descriptor>>> mapHighestOverriddenMethodDescToSetLowerThanPCLoc;
146 public static final String GLOBALLOC = "GLOBALLOC";
148 public static final String INTERLOC = "INTERLOC";
150 public static final String PCLOC = "_PCLOC_";
152 public static final String RLOC = "_RLOC_";
154 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
156 public static final Descriptor TOPDESC = new NameDescriptor(SSJavaAnalysis.TOP);
158 public static final Descriptor BOTTOMDESC = new NameDescriptor(SSJavaAnalysis.BOTTOM);
160 public static final Descriptor RETURNLOC = new NameDescriptor(RLOC);
162 public static final Descriptor LITERALDESC = new NameDescriptor("LITERAL");
164 public static final HNode TOPHNODE = new HNode(TOPDESC);
166 public static final HNode BOTTOMHNODE = new HNode(BOTTOMDESC);
168 public static String newline = System.getProperty("line.separator");
170 LocationInfo curMethodInfo;
172 private boolean hasChanges = false;
174 boolean debug = true;
176 public static int locSeed = 0;
178 private Stack<String> arrayAccessNodeStack;
180 private ClassDescriptor rootClassDescriptor;
182 private BuildLattice buildLattice;
184 public LocationInference(SSJavaAnalysis ssjava, State state, TypeUtil tu) {
185 this.ssjava = ssjava;
188 this.toanalyze_classDescSet = new HashSet<ClassDescriptor>();
189 this.temp_toanalyzeList = new ArrayList<ClassDescriptor>();
190 this.temp_toanalyzeMethodList = new ArrayList<MethodDescriptor>();
191 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
192 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
193 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
194 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
195 this.mapMethodDescriptorToMethodInvokeNodeSet =
196 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
197 this.mapMethodInvokeNodeToArgIdxMap =
198 new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
199 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
200 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
201 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
203 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
204 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
205 this.mapMethodDescToParamNodeFlowsToReturnValue =
206 new HashMap<MethodDescriptor, Set<FlowNode>>();
208 this.mapDescriptorToHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
209 this.mapMethodInvokeNodeToBaseTuple = new HashMap<MethodInvokeNode, NTuple<Descriptor>>();
211 this.mapDescriptorToSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
212 this.mapDescriptorToCombineSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
213 this.mapDescriptorToSimpleHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
215 this.mapDescriptorToSimpleLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
217 this.mapDescToLocationSummary = new HashMap<Descriptor, LocationSummary>();
219 this.mapMethodDescriptorToSubGlobalFlowGraph = new HashMap<MethodDescriptor, GlobalFlowGraph>();
221 this.mapMethodInvokeNodeToMapCallerArgToCalleeArg =
222 new HashMap<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>>();
224 this.mapMethodInvokeNodeToPCLocTupleSet =
225 new HashMap<MethodInvokeNode, Set<NTuple<Location>>>();
227 this.arrayAccessNodeStack = new Stack<String>();
229 this.mapMethodDescToMethodInvokeNodeSet =
230 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
232 this.mapMethodDescriptorToCompositeReturnCase = new HashMap<MethodDescriptor, Boolean>();
234 mapMethodDescToHighestOverriddenMethodDesc = new HashMap<MethodDescriptor, MethodDescriptor>();
236 mapHighestOverriddenMethodDescToSetHigherThanRLoc =
237 new HashMap<MethodDescriptor, Set<NTuple<Descriptor>>>();
239 mapHighestOverriddenMethodDescToSetLowerThanPCLoc =
240 new HashMap<MethodDescriptor, Set<NTuple<Descriptor>>>();
242 mapHighestOverriddenMethodDescToMethodDescSet =
243 new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
245 mapHighestOverriddenMethodDescToReturnLocTuple =
246 new HashMap<MethodDescriptor, NTuple<Descriptor>>();
248 mapHighestOverriddenMethodDescToPCLocTuple =
249 new HashMap<MethodDescriptor, NTuple<Descriptor>>();
251 this.buildLattice = new BuildLattice(this);
255 public void setupToAnalyze() {
256 SymbolTable classtable = state.getClassSymbolTable();
257 temp_toanalyzeList.clear();
258 temp_toanalyzeList.addAll(classtable.getValueSet());
259 // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
260 // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
261 // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
266 public void setupToAnalazeMethod(ClassDescriptor cd) {
268 SymbolTable methodtable = cd.getMethodTable();
269 temp_toanalyzeMethodList.clear();
270 temp_toanalyzeMethodList.addAll(methodtable.getValueSet());
271 Collections.sort(temp_toanalyzeMethodList, new Comparator<MethodDescriptor>() {
272 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
273 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
278 public boolean toAnalyzeMethodIsEmpty() {
279 return temp_toanalyzeMethodList.isEmpty();
282 public boolean toAnalyzeIsEmpty() {
283 return temp_toanalyzeList.isEmpty();
286 public ClassDescriptor toAnalyzeNext() {
287 return temp_toanalyzeList.remove(0);
290 public MethodDescriptor toAnalyzeMethodNext() {
291 return temp_toanalyzeMethodList.remove(0);
294 public void inference() {
298 // construct value flow graph
299 constructFlowGraph();
301 constructGlobalFlowGraph();
305 assignCompositeLocation();
307 calculateExtraLocations();
308 addAdditionalOrderingConstraints();
310 _debug_writeFlowGraph();
312 buildInheritanceTree();
313 calculateReturnPCLocInheritance();
315 constructHierarchyGraph();
317 addInheritanceConstraintsToHierarchyGraph();
319 debug_writeHierarchyDotFiles();
321 simplifyHierarchyGraph();
323 debug_writeSimpleHierarchyDotFiles();
325 constructSkeletonHierarchyGraph();
327 debug_writeSkeletonHierarchyDotFiles();
329 insertCombinationNodes();
331 debug_writeSkeletonCombinationHierarchyDotFiles();
333 buildLatticeInheritanceTree();
336 debug_writeLattices();
338 updateCompositeLocationAssignments();
340 generateMethodSummary();
342 generateAnnoatedCode();
344 for (Iterator iterator = cd2lattice.keySet().iterator(); iterator.hasNext();) {
345 ClassDescriptor cd = (ClassDescriptor) iterator.next();
346 SSJavaLattice<String> lattice = getLattice(cd);
347 HierarchyGraph hg = mapDescriptorToHierarchyGraph.get(cd);
348 // System.out.println("~~~\t" + cd + "\t" + lattice.getKeySet().size() + "\t"
349 // + hg.getNodeSet().size());
352 for (Iterator iterator = md2lattice.keySet().iterator(); iterator.hasNext();) {
353 MethodDescriptor md = (MethodDescriptor) iterator.next();
354 SSJavaLattice<String> locOrder = getLattice(md);
355 // writeLatticeDotFile(md.getClassDesc(), md, getMethodLattice(md));
356 HierarchyGraph hg = mapDescriptorToHierarchyGraph.get(md);
357 // System.out.println("~~~\t" + md.getClassDesc() + "_" + md + "\t"
358 // + locOrder.getKeySet().size() + "\t" + hg.getNodeSet().size());
365 private void calculateReturnPCLocInheritance() {
366 calculateHighestPCLocInheritance();
367 calculateLowestReturnLocInheritance();
368 updateFlowGraphPCReturnLocInheritance();
371 private void updateFlowGraphPCReturnLocInheritance() {
372 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
373 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
374 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
376 if (mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc).size() == 1) {
380 Set<MethodDescriptor> methodDescSet =
381 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
383 NTuple<Descriptor> highestPCLoc =
384 mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc);
386 NTuple<Descriptor> highestRETURNLoc =
387 mapHighestOverriddenMethodDescToReturnLocTuple.get(highestMethodDesc);
389 System.out.println("---highestMethodDesc=" + highestMethodDesc);
391 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
392 MethodDescriptor md = (MethodDescriptor) iterator2.next();
393 FlowGraph flowGraph = getFlowGraph(md);
395 MethodSummary summary = getMethodSummary(md);
396 CompositeLocation curPCLoc = summary.getPCLoc();
399 if (highestPCLoc != null) {
400 // handle the case that PCLOC is started with 'this'...
401 NTuple<Descriptor> newPCLoc = new NTuple<Descriptor>();
402 if (highestPCLoc.size() == 1) {
403 newPCLoc.add(highestPCLoc.get(0));
405 newPCLoc.add(md.getThis());
406 newPCLoc.add(highestPCLoc.get(1));
409 FlowNode pcFlowNode = flowGraph.getFlowNode(translateToDescTuple(curPCLoc.getTuple()));
410 pcFlowNode.setBaseTuple(newPCLoc);
412 CompositeLocation newPCLocCompLoc =
413 new CompositeLocation(translateToLocTuple(md, newPCLoc));
414 summary.setPCLoc(newPCLocCompLoc);
416 // need to remove PCLOC if the overridden method defines it
417 if (curPCLoc != null && !curPCLoc.get(0).isTop()) {
418 System.out.println("md=" + md + " curPCLoc=" + curPCLoc);
419 FlowNode pcFlowNode = flowGraph.getFlowNode(translateToDescTuple(curPCLoc.getTuple()));
420 System.out.println("#######REMOVE PCLOCNODE=" + pcFlowNode);
421 flowGraph.removeNode(pcFlowNode);
425 // need to update RETURNLOC
426 if (highestRETURNLoc != null) {
428 CompositeLocation curRETURNLoc = summary.getRETURNLoc();
429 System.out.println("curRETURNLoc=" + curRETURNLoc);
431 // handle the case that RETURNLOC is started with 'this'...
432 NTuple<Descriptor> newRETURNLoc = new NTuple<Descriptor>();
433 if (highestRETURNLoc.size() == 1) {
434 newRETURNLoc.add(highestRETURNLoc.get(0));
436 newRETURNLoc.add(md.getThis());
437 newRETURNLoc.add(highestRETURNLoc.get(1));
440 FlowNode returnFlowNode =
441 flowGraph.getFlowNode(translateToDescTuple(curRETURNLoc.getTuple()));
442 returnFlowNode.setBaseTuple(newRETURNLoc);
444 CompositeLocation newRETURNLocCompLoc =
445 new CompositeLocation(translateToLocTuple(md, newRETURNLoc));
446 summary.setPCLoc(newRETURNLocCompLoc);
447 System.out.println("md=" + md + "###newRETURNLocCompLoc=" + newRETURNLocCompLoc);
455 private void calculateHighestPCLocInheritance() {
457 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
458 next: for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
459 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
461 NTuple<Descriptor> tempTuple = null;
463 if (getMethodSummary(highestMethodDesc).getPCLoc() != null) {
465 Set<MethodDescriptor> methodDescSet =
466 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
468 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
469 MethodDescriptor md = (MethodDescriptor) iterator2.next();
471 FlowGraph flowGraph = getFlowGraph(md);
472 if (flowGraph == null) {
475 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
476 System.out.println("###md=" + md + " paramNodeSet=" + paramNodeSet);
478 CompositeLocation pcLOC = getMethodSummary(md).getPCLoc();
480 if (!pcLOC.get(0).isTop()) {
481 if (pcLOC.getSize() == 1) {
482 // return location is not started with 'this'
483 // check whether the return location is lower than all parameters.
485 FlowNode pcFlowNode = flowGraph.getFlowNode(translateToDescTuple(pcLOC.getTuple()));
488 for (Iterator iterator3 = paramNodeSet.iterator(); iterator3.hasNext();) {
489 FlowNode paramNode = (FlowNode) iterator3.next();
490 if (flowGraph.getReachableSetFrom(pcFlowNode.getCurrentDescTuple().subList(0, 1))
491 .contains(paramNode)) {
493 System.out.println("-------" + pcFlowNode + " -> " + paramNode);
498 if (!md.isStatic()) {
502 NTuple<Descriptor> rTuple = new NTuple<Descriptor>();
503 rTuple.add(pcLOC.get(0).getLocDescriptor());
504 if (count == (md.numParameters() + offset)) {
505 // means return loc is lower than a composite location starting with 'this'
506 mapHighestOverriddenMethodDescToPCLocTuple.put(highestMethodDesc, rTuple);
508 if (tempTuple == null) {
513 // if the current overridden method has a composite pc loc(size>1)
514 // and it has not yet finalized the pc location,
515 // the highest overridden method would have the composite pc location starting with
517 NTuple<Descriptor> rTuple = new NTuple<Descriptor>();
518 for (int i = 0; i < pcLOC.getSize(); i++) {
519 rTuple.add(pcLOC.get(i).getLocDescriptor());
524 mapHighestOverriddenMethodDescToPCLocTuple.remove(highestMethodDesc);
525 System.out.println("highest=" + highestMethodDesc + " HIGHEST PCLOC="
526 + mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc));
533 if (!mapHighestOverriddenMethodDescToPCLocTuple.containsKey(highestMethodDesc)
534 && tempTuple != null) {
535 mapHighestOverriddenMethodDescToPCLocTuple.put(highestMethodDesc, tempTuple);
537 System.out.println("highest=" + highestMethodDesc + " HIGHEST PCLOC="
538 + mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc));
543 private void calculateLowestReturnLocInheritance() {
545 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
546 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
547 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
549 NTuple<Descriptor> tempTuple = null;
551 if (getMethodSummary(highestMethodDesc).getRETURNLoc() != null) {
552 Set<MethodDescriptor> methodDescSet =
553 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
554 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
555 MethodDescriptor md = (MethodDescriptor) iterator2.next();
557 FlowGraph flowGraph = getFlowGraph(md);
558 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
559 System.out.println("###md=" + md + " paramNodeSet=" + paramNodeSet);
561 CompositeLocation returnLoc = getMethodSummary(md).getRETURNLoc();
562 if (returnLoc.getSize() == 1) {
563 // return location is not started with 'this'
564 // check whether the return location is lower than all parameters.
566 FlowNode returnFlowNode =
567 flowGraph.getFlowNode(translateToDescTuple(returnLoc.getTuple()));
570 for (Iterator iterator3 = paramNodeSet.iterator(); iterator3.hasNext();) {
571 FlowNode paramNode = (FlowNode) iterator3.next();
572 if (flowGraph.getReachableSetFrom(paramNode.getCurrentDescTuple().subList(0, 1))
573 .contains(returnFlowNode)) {
575 System.out.println("-------" + paramNode + " -> " + returnFlowNode);
580 if (!md.isStatic()) {
584 NTuple<Descriptor> rTuple = new NTuple<Descriptor>();
585 rTuple.add(returnLoc.get(0).getLocDescriptor());
586 if (count == (md.numParameters() + offset)) {
587 // means return loc is lower than a composite location starting with 'this'
588 mapHighestOverriddenMethodDescToReturnLocTuple.put(highestMethodDesc, rTuple);
590 if (tempTuple == null) {
595 // if the current overridden method has a composite return loc(size>1)
596 // and it has not yet finalized the return location
597 // the highest overridden method has the composite return location starting with
599 NTuple<Descriptor> rTuple = new NTuple<Descriptor>();
600 for (int i = 0; i < returnLoc.getSize(); i++) {
601 rTuple.add(returnLoc.get(i).getLocDescriptor());
610 if (!mapHighestOverriddenMethodDescToReturnLocTuple.containsKey(highestMethodDesc)
611 && tempTuple != null) {
612 mapHighestOverriddenMethodDescToReturnLocTuple.put(highestMethodDesc, tempTuple);
614 System.out.println("highest=" + highestMethodDesc + " rTuple="
615 + mapHighestOverriddenMethodDescToReturnLocTuple.get(highestMethodDesc));
620 private void addMapHighestMethodDescToMethodDesc(MethodDescriptor highest, MethodDescriptor md) {
621 if (!mapHighestOverriddenMethodDescToMethodDescSet.containsKey(highest)) {
622 mapHighestOverriddenMethodDescToMethodDescSet.put(highest, new HashSet<MethodDescriptor>());
624 mapHighestOverriddenMethodDescToMethodDescSet.get(highest).add(md);
627 private void DFSInheritanceTreeCalculatingHighestOverriddenMethod(ClassDescriptor cd) {
629 // ClassDescriptor cd = node.getData();
631 for (Iterator iterator = cd.getMethods(); iterator.hasNext();) {
632 MethodDescriptor md = (MethodDescriptor) iterator.next();
633 MethodDescriptor highestMethodDesc = getHighestOverriddenMethod(md.getClassDesc(), md);
634 mapMethodDescToHighestOverriddenMethodDesc.put(md, highestMethodDesc);
635 addMapHighestMethodDescToMethodDesc(highestMethodDesc, md);
640 Set<ClassDescriptor> children = getDirectSubClasses(cd);
641 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
642 ClassDescriptor child = (ClassDescriptor) iterator.next();
643 DFSInheritanceTreeCalculatingHighestOverriddenMethod(child);
648 private void addTupleLowerThanPCLoc(NTuple<Descriptor> tuple) {
652 private MethodDescriptor getHighestOverriddenMethod(ClassDescriptor curClassDesc,
653 MethodDescriptor curMethodDesc) {
655 // Node<ClassDescriptor> curNode = inheritanceTree.getTreeNode(curClassDesc);
656 // Node<ClassDescriptor> parentNode = curNode.getParent();
657 ClassDescriptor parentClassDesc = curClassDesc.getSuperDesc();
659 if (parentClassDesc != null) {
660 if (parentClassDesc.getMethodTable().contains(curMethodDesc.getSymbol())) {
661 Set<MethodDescriptor> methodDescSet =
662 parentClassDesc.getMethodTable().getSet(curMethodDesc.getSymbol());
663 for (Iterator iterator = methodDescSet.iterator(); iterator.hasNext();) {
664 MethodDescriptor md = (MethodDescriptor) iterator.next();
665 if (md.matches(curMethodDesc)) {
666 return getHighestOverriddenMethod(parentClassDesc, md);
670 // traverse to the parent!
671 return getHighestOverriddenMethod(parentClassDesc, curMethodDesc);
673 return curMethodDesc;
676 private void buildInheritanceTree() {
678 DFSInheritanceTreeCalculatingHighestOverriddenMethod(rootClassDescriptor);
682 private void addInheritanceConstraintsToHierarchyGraph() {
684 // DFS the inheritance tree and propagates nodes/edges of parent to child
686 // Node<ClassDescriptor> rootNode = inheritanceTree.getRootNode();
687 DFSInheritanceTree(rootClassDescriptor);
691 private void DFSInheritanceTree(ClassDescriptor parentClassDescriptor) {
693 // ClassDescriptor parentClassDescriptor = parentNode.getData();
695 Set<ClassDescriptor> children = getDirectSubClasses(parentClassDescriptor);
696 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
697 ClassDescriptor childClassDescriptor = (ClassDescriptor) iterator.next();
699 HierarchyGraph parentGraph = getHierarchyGraph(parentClassDescriptor);
700 HierarchyGraph childGraph = getHierarchyGraph(childClassDescriptor);
702 // copies extra information from the parent hierarchy graph
703 Map<HNode, Set<HNode>> parentMergeNodeMap = parentGraph.getMapHNodetoMergeSet();
704 Map<HNode, Set<HNode>> childMergeNodeMap = childGraph.getMapHNodetoMergeSet();
706 Set<HNode> keySet = parentMergeNodeMap.keySet();
707 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
708 HNode parentKey = (HNode) iterator2.next();
709 if (!childMergeNodeMap.containsKey(parentKey)) {
710 childMergeNodeMap.put(parentKey, new HashSet<HNode>());
712 childMergeNodeMap.get(parentKey).addAll(parentMergeNodeMap.get(parentKey));
715 // copies nodes/edges from the parent class...
716 Set<HNode> parentNodeSet = parentGraph.getNodeSet();
717 for (Iterator iterator2 = parentNodeSet.iterator(); iterator2.hasNext();) {
718 HNode parentHNode = (HNode) iterator2.next();
720 Set<HNode> parentIncomingHNode = parentGraph.getIncomingNodeSet(parentHNode);
721 Set<HNode> parentOutgoingHNode = parentGraph.getOutgoingNodeSet(parentHNode);
723 for (Iterator iterator3 = parentIncomingHNode.iterator(); iterator3.hasNext();) {
724 HNode inHNode = (HNode) iterator3.next();
725 childGraph.addEdge(inHNode.getDescriptor(), parentHNode.getDescriptor());
728 for (Iterator iterator3 = parentOutgoingHNode.iterator(); iterator3.hasNext();) {
729 HNode outHNode = (HNode) iterator3.next();
730 childGraph.addEdge(parentHNode.getDescriptor(), outHNode.getDescriptor());
735 // copies nodes/edges from parent methods to overridden methods
737 for (Iterator iterator3 = childClassDescriptor.getMethods(); iterator3.hasNext();) {
738 MethodDescriptor childMethodDescriptor = (MethodDescriptor) iterator3.next();
740 MethodDescriptor parentMethodDesc =
741 getParentMethodDesc(childMethodDescriptor.getClassDesc(), childMethodDescriptor);
743 if (parentMethodDesc != null) {
745 HierarchyGraph parentMethodGraph = getHierarchyGraph(parentMethodDesc);
746 HierarchyGraph childMethodGraph = getHierarchyGraph(childMethodDescriptor);
748 // copies extra information from the parent hierarchy graph
749 Map<HNode, Set<HNode>> parentMethodMergeNodeMap =
750 parentMethodGraph.getMapHNodetoMergeSet();
751 Map<HNode, Set<HNode>> childMethodMergeNodeMap = childMethodGraph.getMapHNodetoMergeSet();
753 Set<HNode> methodKeySet = parentMethodMergeNodeMap.keySet();
754 for (Iterator iterator2 = methodKeySet.iterator(); iterator2.hasNext();) {
755 HNode parentKey = (HNode) iterator2.next();
756 if (!childMethodMergeNodeMap.containsKey(parentKey)) {
757 childMethodMergeNodeMap.put(parentKey, new HashSet<HNode>());
759 childMethodMergeNodeMap.get(parentKey).addAll(parentMethodMergeNodeMap.get(parentKey));
762 // copies nodes/edges from the parent method...
763 for (Iterator iterator2 = parentMethodGraph.getNodeSet().iterator(); iterator2.hasNext();) {
764 HNode parentHNode = (HNode) iterator2.next();
766 Set<HNode> parentIncomingHNode = parentMethodGraph.getIncomingNodeSet(parentHNode);
767 Set<HNode> parentOutgoingHNode = parentMethodGraph.getOutgoingNodeSet(parentHNode);
769 for (Iterator iterator4 = parentIncomingHNode.iterator(); iterator4.hasNext();) {
770 HNode inHNode = (HNode) iterator4.next();
771 childMethodGraph.addEdge(inHNode, parentHNode);
774 for (Iterator iterator4 = parentOutgoingHNode.iterator(); iterator4.hasNext();) {
775 HNode outHNode = (HNode) iterator4.next();
776 childMethodGraph.addEdge(parentHNode, outHNode);
785 DFSInheritanceTree(childClassDescriptor);
790 public MethodDescriptor getParentMethodDesc(ClassDescriptor classDesc, MethodDescriptor methodDesc) {
792 // Node<ClassDescriptor> childNode = inheritanceTree.getTreeNode(classDesc);
793 ClassDescriptor parentClassDesc = classDesc.getSuperDesc();
794 // Node<ClassDescriptor> parentNode = childNode.getParent();
796 if (parentClassDesc != null) {
797 // ClassDescriptor parentClassDesc = parentNode.getData();
798 if (parentClassDesc.getMethodTable().contains(methodDesc.getSymbol())) {
799 Set<MethodDescriptor> methodDescSet =
800 parentClassDesc.getMethodTable().getSet(methodDesc.getSymbol());
801 for (Iterator iterator = methodDescSet.iterator(); iterator.hasNext();) {
802 MethodDescriptor md = (MethodDescriptor) iterator.next();
803 if (md.matches(methodDesc)) {
809 // traverse to the parent!
810 getParentMethodDesc(parentClassDesc, methodDesc);
817 private void checkReturnNodes() {
818 LinkedList<MethodDescriptor> methodDescList =
819 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
821 while (!methodDescList.isEmpty()) {
822 MethodDescriptor md = methodDescList.removeLast();
824 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
825 checkFlowNodeReturnThisField(md);
827 // // in this case, this method will return the composite location that starts with 'this'
828 // FlowGraph flowGraph = getFlowGraph(md);
829 // Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
836 private void updateFlowGraph() {
838 LinkedList<MethodDescriptor> methodDescList =
839 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
841 while (!methodDescList.isEmpty()) {
842 MethodDescriptor md = methodDescList.removeLast();
843 if (state.SSJAVADEBUG) {
844 System.out.println();
845 System.out.println("SSJAVA: Updating a flow graph: " + md);
846 propagateFlowsFromCalleesWithNoCompositeLocation(md);
849 Set<FlowNode> nodeSet = getFlowGraph(md).getNodeSet();
850 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
851 FlowNode flowNode = (FlowNode) iterator.next();
852 NTuple<Descriptor> descTuple = flowNode.getCurrentDescTuple();
853 NTuple<Location> locTuple = translateToLocTuple(md, descTuple);
854 for (int i = 0; i < locTuple.size(); i++) {
855 Location loc = locTuple.get(i);
856 if (loc.getDescriptor() instanceof ClassDescriptor) {
857 toanalyze_classDescSet.add((ClassDescriptor) loc.getDescriptor());
858 } else if (loc.getDescriptor() instanceof MethodDescriptor) {
859 toanalyze_classDescSet.add(((MethodDescriptor) loc.getDescriptor()).getClassDesc());
868 public Map<NTuple<Descriptor>, NTuple<Descriptor>> getMapCallerArgToCalleeParam(
869 MethodInvokeNode min) {
871 if (!mapMethodInvokeNodeToMapCallerArgToCalleeArg.containsKey(min)) {
872 mapMethodInvokeNodeToMapCallerArgToCalleeArg.put(min,
873 new HashMap<NTuple<Descriptor>, NTuple<Descriptor>>());
876 return mapMethodInvokeNodeToMapCallerArgToCalleeArg.get(min);
879 public void addMapCallerArgToCalleeParam(MethodInvokeNode min, NTuple<Descriptor> callerArg,
880 NTuple<Descriptor> calleeParam) {
881 getMapCallerArgToCalleeParam(min).put(callerArg, calleeParam);
884 private void assignCompositeLocation() {
885 calculateGlobalValueFlowCompositeLocation();
886 translateCompositeLocationAssignmentToFlowGraph();
889 private void translateCompositeLocationAssignmentToFlowGraph() {
890 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
891 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
892 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
895 private void translateCompositeLocationAssignmentToFlowGraph2() {
896 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
897 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
898 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
901 private void addAdditionalOrderingConstraints() {
902 System.out.println("\nSSJAVA: Add addtional ordering constriants:");
903 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
904 addAddtionalOrderingConstraints(methodEventLoopDesc);
905 // calculateReturnHolderLocation();
908 private void calculateReturnHolderLocation() {
909 LinkedList<MethodDescriptor> methodDescList =
910 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
912 while (!methodDescList.isEmpty()) {
913 MethodDescriptor md = methodDescList.removeLast();
915 FlowGraph fg = getFlowGraph(md);
916 Set<FlowNode> nodeSet = fg.getNodeSet();
917 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
918 FlowNode flowNode = (FlowNode) iterator.next();
919 if (flowNode.isFromHolder()) {
920 calculateCompositeLocationFromFlowGraph(md, flowNode);
927 private void updateCompositeLocationAssignments() {
929 LinkedList<MethodDescriptor> methodDescList =
930 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
932 while (!methodDescList.isEmpty()) {
933 MethodDescriptor md = methodDescList.removeLast();
935 // System.out.println("\n#updateCompositeLocationAssignments=" + md);
937 FlowGraph flowGraph = getFlowGraph(md);
939 MethodSummary methodSummary = getMethodSummary(md);
941 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
942 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
943 FlowNode node = (FlowNode) iterator.next();
944 System.out.println("-node=" + node + " node.getDescTuple=" + node.getDescTuple());
945 if (node.getCompositeLocation() != null) {
946 CompositeLocation compLoc = node.getCompositeLocation();
947 CompositeLocation updatedCompLoc = updateCompositeLocation(compLoc);
948 node.setCompositeLocation(updatedCompLoc);
949 System.out.println("---updatedCompLoc1=" + updatedCompLoc);
951 NTuple<Descriptor> descTuple = node.getDescTuple();
952 System.out.println("update desc=" + descTuple);
953 CompositeLocation compLoc = convertToCompositeLocation(md, descTuple);
954 compLoc = updateCompositeLocation(compLoc);
955 node.setCompositeLocation(compLoc);
956 System.out.println("---updatedCompLoc2=" + compLoc);
959 if (node.isDeclaratonNode()) {
960 Descriptor localVarDesc = node.getDescTuple().get(0);
961 CompositeLocation compLoc = updateCompositeLocation(node.getCompositeLocation());
962 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, compLoc);
966 // update PCLOC and RETURNLOC if they have a composite location assignment
967 if (methodSummary.getRETURNLoc() != null) {
968 methodSummary.setRETURNLoc(updateCompositeLocation(methodSummary.getRETURNLoc()));
970 if (methodSummary.getPCLoc() != null) {
971 methodSummary.setPCLoc(updateCompositeLocation(methodSummary.getPCLoc()));
978 private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
979 CompositeLocation updatedCompLoc = new CompositeLocation();
980 for (int i = 0; i < compLoc.getSize(); i++) {
981 Location loc = compLoc.get(i);
982 String nodeIdentifier = loc.getLocIdentifier();
983 Descriptor enclosingDesc = loc.getDescriptor();
985 if (!enclosingDesc.equals(GLOBALDESC)) {
986 LocationSummary locSummary = getLocationSummary(enclosingDesc);
987 // HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(enclosingDesc);
988 HierarchyGraph scGraph = getSimpleHierarchyGraph(enclosingDesc);
989 if (scGraph != null) {
990 HNode curNode = scGraph.getCurrentHNode(nodeIdentifier);
991 System.out.println("nodeID=" + nodeIdentifier + " curNode=" + curNode
992 + " enclosingDesc=" + enclosingDesc);
993 if (curNode != null) {
994 nodeIdentifier = curNode.getName();
997 locName = locSummary.getLocationName(nodeIdentifier);
999 locName = nodeIdentifier;
1001 Location updatedLoc = new Location(enclosingDesc, locName);
1002 updatedCompLoc.addLocation(updatedLoc);
1005 return updatedCompLoc;
1008 private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
1010 // System.out.println("\n\n###translateCompositeLocationAssignmentToFlowGraph mdCaller="
1013 // First, assign a composite location to a node in the flow graph
1014 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
1016 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
1017 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1018 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
1020 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
1021 for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
1022 Location methodLoc = (Location) iterator.next();
1023 if (methodLoc.getDescriptor().equals(mdCaller)) {
1024 CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
1025 assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
1029 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
1031 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
1032 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1033 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1034 // need to translate a composite location that is started with the base
1036 translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
1037 MethodDescriptor mdCallee = min.getMethod();
1038 calleeSet.add(mdCallee);
1042 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1043 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1044 translateCompositeLocationAssignmentToFlowGraph(callee);
1049 private CompositeLocation translateArgCompLocToParamCompLoc(MethodInvokeNode min,
1050 CompositeLocation argCompLoc) {
1052 System.out.println("--------translateArgCompLocToParamCompLoc argCompLoc=" + argCompLoc);
1053 MethodDescriptor mdCallee = min.getMethod();
1054 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1056 NTuple<Location> argLocTuple = argCompLoc.getTuple();
1057 Location argLocalLoc = argLocTuple.get(0);
1059 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
1060 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
1061 for (Iterator iterator2 = idxSet.iterator(); iterator2.hasNext();) {
1062 Integer idx = (Integer) iterator2.next();
1064 if (idx == 0 && !min.getMethod().isStatic()) {
1068 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
1069 if (argTuple.size() > 0 && argTuple.get(0).equals(argLocalLoc.getLocDescriptor())) {
1070 // it matches with the current argument composite location
1071 // so what is the corresponding parameter local descriptor?
1072 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
1073 // System.out.println("----------found paramNode=" + paramNode);
1074 NTuple<Descriptor> paramDescTuple = paramNode.getCurrentDescTuple();
1076 NTuple<Location> newParamTupleFromArgTuple = translateToLocTuple(mdCallee, paramDescTuple);
1077 for (int i = 1; i < argLocTuple.size(); i++) {
1078 newParamTupleFromArgTuple.add(argLocTuple.get(i));
1081 // System.out.println("-----------newParamTuple=" + newParamTupleFromArgTuple);
1082 return new CompositeLocation(newParamTupleFromArgTuple);
1089 private void addAddtionalOrderingConstraints(MethodDescriptor mdCaller) {
1091 // First, assign a composite location to a node in the flow graph
1092 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
1094 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
1095 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1096 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
1097 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
1099 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
1101 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
1102 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1103 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1104 MethodDescriptor mdCallee = min.getMethod();
1105 calleeSet.add(mdCallee);
1108 // add an additional ordering constraint
1109 // if the first element of a parameter composite location matches 'this' reference,
1110 // the corresponding argument in the caller is required to be higher than the translated
1111 // parameter location in the caller lattice
1113 // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
1116 // update return flow nodes in the caller
1117 CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc();
1118 System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc);
1119 if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis())
1120 && returnLoc.getSize() > 1) {
1121 System.out.println("###RETURN COMP LOC=" + returnLoc);
1122 NTuple<Location> returnLocTuple = returnLoc.getTuple();
1123 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1124 System.out.println("###basetuple=" + baseTuple);
1125 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
1126 for (int i = 1; i < returnLocTuple.size(); i++) {
1127 newReturnTuple.add(returnLocTuple.get(i).getLocDescriptor());
1129 System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple);
1131 FlowReturnNode holderNode = callerFlowGraph.getFlowReturnNode(min);
1132 NodeTupleSet holderTupleSet =
1133 getNodeTupleSetFromReturnNode(getFlowGraph(mdCaller), holderNode);
1135 callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple);
1137 // then need to remove old constraints
1139 System.out.println("###REMOVE OLD CONSTRAINTS=" + holderNode);
1140 for (Iterator<NTuple<Descriptor>> iter = holderTupleSet.iterator(); iter.hasNext();) {
1141 NTuple<Descriptor> tupleFromHolder = iter.next();
1142 Set<FlowEdge> holderOutEdge = callerFlowGraph.getOutEdgeSet(holderNode);
1143 for (Iterator iterator2 = holderOutEdge.iterator(); iterator2.hasNext();) {
1144 FlowEdge outEdge = (FlowEdge) iterator2.next();
1145 NTuple<Descriptor> toberemovedTuple = outEdge.getEndTuple();
1146 // System.out.println("---remove " + tupleFromHolder + " -> " + toberemovedTuple);
1147 callerFlowGraph.removeEdge(tupleFromHolder, toberemovedTuple);
1152 // if the return loc set was empty and later pcloc was connected to the return loc
1153 // need to make sure that return loc reflects to this changes.
1154 FlowReturnNode flowReturnNode = callerFlowGraph.getFlowReturnNode(min);
1155 if (flowReturnNode != null && flowReturnNode.getReturnTupleSet().isEmpty()) {
1157 if (needToUpdateReturnLocHolder(min.getMethod(), flowReturnNode)) {
1158 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1159 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
1160 flowReturnNode.addTuple(newReturnTuple);
1169 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1170 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1171 addAddtionalOrderingConstraints(callee);
1176 private boolean needToUpdateReturnLocHolder(MethodDescriptor mdCallee,
1177 FlowReturnNode flowReturnNode) {
1178 FlowGraph fg = getFlowGraph(mdCallee);
1179 MethodSummary summary = getMethodSummary(mdCallee);
1180 CompositeLocation returnCompLoc = summary.getRETURNLoc();
1181 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnCompLoc.getTuple());
1182 Set<FlowNode> incomingNodeToReturnNode =
1183 fg.getIncomingFlowNodeSet(fg.getFlowNode(returnDescTuple));
1184 for (Iterator iterator = incomingNodeToReturnNode.iterator(); iterator.hasNext();) {
1185 FlowNode inNode = (FlowNode) iterator.next();
1186 if (inNode.getDescTuple().get(0).equals(mdCallee.getThis())) {
1193 private void addMapMethodDescToMethodInvokeNodeSet(MethodInvokeNode min) {
1194 MethodDescriptor md = min.getMethod();
1195 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
1196 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
1198 mapMethodDescToMethodInvokeNodeSet.get(md).add(min);
1201 private Set<MethodInvokeNode> getMethodInvokeNodeSetByMethodDesc(MethodDescriptor md) {
1202 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
1203 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
1205 return mapMethodDescToMethodInvokeNodeSet.get(md);
1208 private void addOrderingConstraintFromCompLocParamToArg(MethodDescriptor mdCaller,
1209 MethodInvokeNode min) {
1210 System.out.println("-addOrderingConstraintFromCompLocParamToArg=" + min.printNode(0));
1212 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(ssjava.getMethodContainingSSJavaLoop());
1214 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
1216 MethodDescriptor mdCallee = min.getMethod();
1218 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1219 for (int idx = 0; idx < calleeFlowGraph.getNumParameters(); idx++) {
1220 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
1221 NTuple<Location> globalParamLocTuple =
1222 translateToLocTuple(mdCallee, paramNode.getDescTuple());
1223 translateToLocTuple(mdCallee, paramNode.getDescTuple());
1224 CompositeLocation compLoc = paramNode.getCompositeLocation();
1225 System.out.println("---paramNode=" + paramNode + " compLoc=" + compLoc);
1226 if (compLoc != null) {
1227 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
1228 NTuple<Location> globalArgLocTuple = translateToLocTuple(mdCaller, argTuple);
1230 if (!isLiteralValueLocTuple(globalArgLocTuple)
1231 && !isLiteralValueLocTuple(globalParamLocTuple)) {
1232 if (!globalGraph.hasValueFlowEdge(globalArgLocTuple, globalParamLocTuple)) {
1233 System.out.println("----- add global flow globalArgLocTuple=" + globalArgLocTuple
1234 + "-> globalParamLocTuple=" + globalParamLocTuple);
1236 globalGraph.addValueFlowEdge(globalArgLocTuple, globalParamLocTuple);
1240 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
1241 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
1243 if (!isLiteralValueLocTuple(pcLocTuple) && !isLiteralValueLocTuple(globalParamLocTuple)) {
1244 if (!globalGraph.hasValueFlowEdge(pcLocTuple, globalParamLocTuple)) {
1246 .println("----- add global flow PCLOC="
1248 + "-> globalParamLocTu!globalArgLocTuple.get(0).getLocDescriptor().equals(LITERALDESC)ple="
1249 + globalParamLocTuple);
1252 globalGraph.addValueFlowEdge(pcLocTuple, globalParamLocTuple);
1261 private boolean isLiteralValueLocTuple(NTuple<Location> locTuple) {
1262 return locTuple.get(0).getLocDescriptor().equals(LITERALDESC);
1265 public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
1266 CompositeLocation inferCompLoc) {
1267 Descriptor localDesc = loc.getLocDescriptor();
1269 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1270 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1271 FlowNode node = (FlowNode) iterator.next();
1272 if (node.getDescTuple().startsWith(localDesc)
1273 && !node.getDescTuple().get(0).equals(LITERALDESC)) {
1274 // need to assign the inferred composite location to this node
1275 CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
1276 node.setCompositeLocation(newCompLoc);
1277 System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
1282 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
1283 CompositeLocation inferCompLoc) {
1285 System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
1288 MethodDescriptor md = (MethodDescriptor) inferCompLoc.get(0).getDescriptor();
1290 CompositeLocation newCompLoc = new CompositeLocation();
1291 for (int i = 0; i < inferCompLoc.getSize(); i++) {
1292 newCompLoc.addLocation(inferCompLoc.get(i));
1295 Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
1296 Descriptor enclosingDescriptor;
1297 if (lastDescOfPrefix instanceof InterDescriptor) {
1298 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(lastDescOfPrefix);
1300 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
1303 for (int i = 1; i < nodeDescTuple.size(); i++) {
1304 Descriptor desc = nodeDescTuple.get(i);
1305 Location locElement = new Location(enclosingDescriptor, desc);
1306 newCompLoc.addLocation(locElement);
1308 enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
1314 private void translateMapLocationToInferCompositeLocationToCalleeGraph(
1315 GlobalFlowGraph callerGraph, MethodInvokeNode min) {
1317 MethodDescriptor mdCallee = min.getMethod();
1318 MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
1319 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1320 callerGraph.getMapLocationToInferCompositeLocation();
1322 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
1324 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1325 GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1327 NTuple<Location> baseLocTuple = null;
1328 if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
1329 baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
1332 // System.out.println("\n-#translate caller=" + mdCaller + " infer composite loc to callee="
1333 // + mdCallee + " baseLocTuple=" + baseLocTuple);
1335 Set<Location> keySet = callerMapLocToCompLoc.keySet();
1336 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1337 Location key = (Location) iterator.next();
1338 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
1340 if (!key.getDescriptor().equals(mdCaller)) {
1342 CompositeLocation newCalleeCompLoc;
1343 if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
1344 // System.out.println("-----need to translate callerCompLoc=" + callerCompLoc
1345 // + " with baseTuple=" + baseLocTuple);
1347 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
1349 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1350 // System.out.println("1---key=" + key + " callerCompLoc=" + callerCompLoc
1351 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1352 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1353 if (!newCalleeCompLoc.get(0).getDescriptor().equals(mdCallee)) {
1357 // System.out.println("-----baseLoctuple=" + baseLocTuple);
1359 // check if it is the global access
1360 Location compLocFirstElement = callerCompLoc.getTuple().get(0);
1361 if (compLocFirstElement.getDescriptor().equals(mdCallee)
1362 && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
1364 newCalleeCompLoc = new CompositeLocation();
1365 Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
1367 newCalleeCompLoc.addLocation(newMethodLoc);
1368 for (int i = 1; i < callerCompLoc.getSize(); i++) {
1369 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1371 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1372 // System.out.println("2---key=" + key + " callerCompLoc=" + callerCompLoc
1373 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1374 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1377 int paramIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
1378 if (paramIdx == -1) {
1379 // here, the first element of the current composite location comes from the current
1381 // so transfer the same composite location to the callee
1382 if (!calleeGlobalGraph.contrainsInferCompositeLocationMapKey(key)) {
1383 if (callerCompLoc.get(0).getDescriptor().equals(mdCallee)) {
1384 // System.out.println("3---key=" + key + " callerCompLoc=" + callerCompLoc
1385 // + " newCalleeCompLoc=" + callerCompLoc);
1386 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1387 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, callerCompLoc);
1389 // System.out.println("3---SKIP key=" + key + " callerCompLoc=" + callerCompLoc);
1395 // It is the case where two parameters have relative orderings between them by having
1396 // composite locations
1397 // if we found the param idx, it means that the first part of the caller composite
1398 // location corresponds to the one of arguments.
1399 // for example, if the caller argument is <<caller.this>,<Decoder.br>>
1400 // and the current caller composite location mapping
1401 // <<caller.this>,<Decoder.br>,<Br.value>>
1402 // and the parameter which matches with the caller argument is 'Br brParam'
1403 // then, the translated callee composite location will be <<callee.brParam>,<Br.value>>
1404 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(paramIdx);
1406 FlowNode paramFlowNode = calleeFlowGraph.getParamFlowNode(paramIdx);
1407 NTuple<Location> paramLocTuple =
1408 translateToLocTuple(mdCallee, paramFlowNode.getDescTuple());
1409 newCalleeCompLoc = new CompositeLocation();
1410 for (int i = 0; i < paramLocTuple.size(); i++) {
1411 newCalleeCompLoc.addLocation(paramLocTuple.get(i));
1413 for (int i = argTuple.size(); i < callerCompLoc.getSize(); i++) {
1414 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1416 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1417 // System.out.println("4---key=" + key + " callerCompLoc=" + callerCompLoc
1418 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1419 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1428 System.out.println("#ASSIGN COMP LOC TO CALLEE PARAMS: callee=" + mdCallee + " caller="
1430 // If the location of an argument has a composite location
1431 // need to assign a proper composite location to the corresponding callee parameter
1432 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
1433 for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
1434 Integer idx = (Integer) iterator.next();
1436 if (idx == 0 && !min.getMethod().isStatic()) {
1440 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
1441 System.out.println("-argTuple=" + argTuple + " idx=" + idx);
1442 if (argTuple.size() > 0) {
1443 // check if an arg tuple has been already assigned to a composite location
1444 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
1445 Location argLocalLoc = argLocTuple.get(0);
1447 // if (!isPrimitiveType(argTuple)) {
1448 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
1450 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
1451 for (int i = 1; i < argLocTuple.size(); i++) {
1452 callerCompLoc.addLocation(argLocTuple.get(i));
1455 System.out.println("---callerCompLoc=" + callerCompLoc);
1457 // if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
1459 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
1461 NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
1462 NTuple<Location> calleeParamLocTuple =
1463 translateToLocTuple(mdCallee, calleeParamDescTuple);
1465 int refParamIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
1466 System.out.println("-----paramIdx=" + refParamIdx);
1467 if (refParamIdx == 0 && !mdCallee.isStatic()) {
1469 System.out.println("-------need to translate callerCompLoc=" + callerCompLoc
1470 + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
1471 + calleeParamLocTuple);
1473 CompositeLocation newCalleeCompLoc =
1474 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
1476 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1479 System.out.println("---------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
1480 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
1482 } else if (refParamIdx != -1) {
1483 // the first element of an argument composite location matches with one of paramtere
1484 // composite locations
1486 System.out.println("-------param match case=");
1488 NTuple<Descriptor> argTupleRef = mapIdxToArgTuple.get(refParamIdx);
1489 FlowNode refParamFlowNode = calleeFlowGraph.getParamFlowNode(refParamIdx);
1490 NTuple<Location> refParamLocTuple =
1491 translateToLocTuple(mdCallee, refParamFlowNode.getDescTuple());
1493 System.out.println("---------refParamLocTuple=" + refParamLocTuple
1494 + " from argTupleRef=" + argTupleRef);
1496 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1497 for (int i = 0; i < refParamLocTuple.size(); i++) {
1498 newCalleeCompLoc.addLocation(refParamLocTuple.get(i));
1500 for (int i = argTupleRef.size(); i < callerCompLoc.getSize(); i++) {
1501 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1504 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1507 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
1508 System.out.println("-----------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
1509 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
1512 CompositeLocation newCalleeCompLoc =
1513 calculateCompositeLocationFromSubGlobalGraph(mdCallee, calleeParamFlowNode);
1514 if (newCalleeCompLoc != null) {
1515 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1517 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
1521 System.out.println("-----------------calleeParamFlowNode="
1522 + calleeParamFlowNode.getCompositeLocation());
1533 private CompositeLocation calculateCompositeLocationFromSubGlobalGraph(MethodDescriptor md,
1534 FlowNode paramNode) {
1536 System.out.println("#############################################################");
1537 System.out.println("calculateCompositeLocationFromSubGlobalGraph=" + paramNode);
1539 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1540 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
1541 GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
1543 List<NTuple<Location>> prefixList = calculatePrefixList(subGlobalFlowGraph, paramGlobalNode);
1545 Location prefixLoc = paramLocTuple.get(0);
1547 Set<GlobalFlowNode> reachableNodeSet =
1548 subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
1549 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1551 // System.out.println("node=" + node + " prefixList=" + prefixList);
1553 for (int i = 0; i < prefixList.size(); i++) {
1554 NTuple<Location> curPrefix = prefixList.get(i);
1555 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1557 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1558 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1559 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1560 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1563 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1565 if (!reachableCommonPrefixSet.isEmpty()) {
1567 MethodDescriptor curPrefixFirstElementMethodDesc =
1568 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1570 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1571 (MethodDescriptor) prefixLoc.getDescriptor();
1573 // System.out.println("curPrefixFirstElementMethodDesc=" +
1574 // curPrefixFirstElementMethodDesc);
1575 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1576 // + nodePrefixLocFirstElementMethodDesc);
1578 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1579 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1580 curPrefixFirstElementMethodDesc)) {
1583 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1585 Location curPrefixLocalLoc = curPrefix.get(0);
1586 if (subGlobalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1587 // in this case, the local variable of the current prefix has already got a composite
1589 // so we just ignore the current composite location.
1591 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1592 // + " DUE TO " + curPrefix);
1596 if (!needToGenerateCompositeLocation(paramGlobalNode, curPrefix)) {
1597 System.out.println("NO NEED TO GENERATE COMP LOC to " + paramGlobalNode
1598 + " with prefix=" + curPrefix);
1602 Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
1603 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1604 System.out.println("NEED TO ASSIGN COMP LOC TO " + paramGlobalNode + " with prefix="
1606 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
1608 // makes sure that a newly generated location appears in the hierarchy graph
1609 for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
1610 Location curLoc = newCompLoc.get(compIdx);
1611 getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
1614 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1626 private int getParamIdx(CompositeLocation compLoc,
1627 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple) {
1629 // if the composite location is started with the argument descriptor
1630 // return the argument's index. o.t. return -1
1632 Set<Integer> keySet = mapIdxToArgTuple.keySet();
1633 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1634 Integer key = (Integer) iterator.next();
1635 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(key);
1636 if (argTuple.size() > 0 && translateToDescTuple(compLoc.getTuple()).startsWith(argTuple)) {
1637 System.out.println("compLoc.getTuple=" + compLoc + " is started with " + argTuple);
1638 return key.intValue();
1645 private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
1647 Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
1649 if (lastDesc instanceof FieldDescriptor) {
1650 return ((FieldDescriptor) lastDesc).getType().isPrimitive();
1651 } else if (lastDesc instanceof VarDescriptor) {
1652 return ((VarDescriptor) lastDesc).getType().isPrimitive();
1653 } else if (lastDesc instanceof InterDescriptor) {
1660 private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
1661 NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
1663 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1665 Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
1666 newCalleeCompLoc.addLocation(calleeThisLoc);
1668 // remove the base tuple from the caller
1669 // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
1670 // ,which is relative to the 'this' variable, <THIS,...>
1671 for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
1672 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1675 return newCalleeCompLoc;
1679 private void calculateGlobalValueFlowCompositeLocation() {
1681 System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
1682 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1683 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1685 Set<Location> calculatedPrefixSet = new HashSet<Location>();
1687 Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
1689 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1690 GlobalFlowNode node = (GlobalFlowNode) iterator.next();
1692 Location prefixLoc = node.getLocTuple().get(0);
1694 if (calculatedPrefixSet.contains(prefixLoc)) {
1695 // the prefix loc has been already assigned to a composite location
1699 calculatedPrefixSet.add(prefixLoc);
1701 // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
1702 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
1704 Set<GlobalFlowNode> reachableNodeSet =
1705 globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1706 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1708 // System.out.println("node=" + node + " prefixList=" + prefixList);
1709 System.out.println("---prefixList=" + prefixList);
1711 nextprefix: for (int i = 0; i < prefixList.size(); i++) {
1712 NTuple<Location> curPrefix = prefixList.get(i);
1713 System.out.println("---curPrefix=" + curPrefix);
1714 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1716 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1717 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1718 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1719 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1722 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1724 if (!reachableCommonPrefixSet.isEmpty()) {
1726 MethodDescriptor curPrefixFirstElementMethodDesc =
1727 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1729 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1730 (MethodDescriptor) prefixLoc.getDescriptor();
1732 // System.out.println("curPrefixFirstElementMethodDesc=" +
1733 // curPrefixFirstElementMethodDesc);
1734 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1735 // + nodePrefixLocFirstElementMethodDesc);
1737 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1738 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1739 curPrefixFirstElementMethodDesc)) {
1742 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1744 Location curPrefixLocalLoc = curPrefix.get(0);
1745 if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1746 // in this case, the local variable of the current prefix has already got a composite
1748 // so we just ignore the current composite location.
1750 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1751 // + " DUE TO " + curPrefix);
1756 if (!needToGenerateCompositeLocation(node, curPrefix)) {
1757 System.out.println("NO NEED TO GENERATE COMP LOC to " + node + " with prefix="
1759 // System.out.println("prefixList=" + prefixList);
1760 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1761 continue nextprefix;
1764 Location targetLocalLoc = node.getLocTuple().get(0);
1765 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1766 System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
1767 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc="
1769 globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1784 private boolean checkFlowNodeReturnThisField(MethodDescriptor md) {
1786 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1787 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1789 FlowGraph flowGraph = getFlowGraph(md);
1791 ClassDescriptor enclosingDesc = getClassTypeDescriptor(md.getThis());
1792 if (enclosingDesc == null) {
1797 Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
1798 Set<GlobalFlowNode> globalReturnNodeSet = new HashSet<GlobalFlowNode>();
1799 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1800 FlowNode flowNode = (FlowNode) iterator.next();
1801 NTuple<Location> locTuple = translateToLocTuple(md, flowNode.getDescTuple());
1802 GlobalFlowNode globalReturnNode = globalFlowGraph.getFlowNode(locTuple);
1803 globalReturnNodeSet.add(globalReturnNode);
1805 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, globalReturnNode);
1806 for (int i = 0; i < prefixList.size(); i++) {
1807 NTuple<Location> curPrefix = prefixList.get(i);
1808 ClassDescriptor cd =
1809 getClassTypeDescriptor(curPrefix.get(curPrefix.size() - 1).getLocDescriptor());
1810 if (cd != null && cd.equals(enclosingDesc)) {
1818 if (count == returnNodeSet.size()) {
1819 // in this case, all return nodes in the method returns values coming from a location that
1820 // starts with "this"
1822 System.out.println("$$$SET RETURN LOC TRUE=" + md);
1823 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.TRUE);
1825 // NameDescriptor returnLocDesc = new NameDescriptor("RLOC" + (locSeed++));
1826 // NTuple<Descriptor> rDescTuple = new NTuple<Descriptor>();
1827 // rDescTuple.add(md.getThis());
1828 // rDescTuple.add(returnLocDesc);
1830 // for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1831 // FlowNode rnode = (FlowNode) iterator.next();
1832 // flowGraph.addValueFlowEdge(rnode.getDescTuple(), rDescTuple);
1835 // getMethodSummary(md).setRETURNLoc(new CompositeLocation(translateToLocTuple(md,
1839 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.FALSE);
1842 return mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1846 private boolean needToGenerateCompositeLocation(GlobalFlowNode node, NTuple<Location> curPrefix) {
1847 // return true if there is a path between a node to which we want to give a composite location
1848 // and nodes which start with curPrefix
1850 System.out.println("---needToGenerateCompositeLocation curPrefix=" + curPrefix);
1852 Location targetLocalLoc = node.getLocTuple().get(0);
1854 MethodDescriptor md = (MethodDescriptor) targetLocalLoc.getDescriptor();
1855 FlowGraph flowGraph = getFlowGraph(md);
1857 FlowNode flowNode = flowGraph.getFlowNode(node.getDescTuple());
1858 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
1860 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
1861 for (Iterator iterator = paramNodeSet.iterator(); iterator.hasNext();) {
1862 FlowNode paramFlowNode = (FlowNode) iterator.next();
1863 if (curPrefix.startsWith(translateToLocTuple(md, paramFlowNode.getDescTuple()))) {
1868 if (targetLocalLoc.getLocDescriptor() instanceof InterDescriptor) {
1869 Pair<MethodInvokeNode, Integer> pair =
1870 ((InterDescriptor) targetLocalLoc.getLocDescriptor()).getMethodArgIdxPair();
1873 System.out.println("$$$TARGETLOCALLOC HOLDER=" + targetLocalLoc);
1875 MethodInvokeNode min = pair.getFirst();
1876 Integer paramIdx = pair.getSecond();
1877 MethodDescriptor mdCallee = min.getMethod();
1879 FlowNode paramNode = getFlowGraph(mdCallee).getParamFlowNode(paramIdx);
1880 if (checkNodeReachToReturnNode(mdCallee, paramNode)) {
1888 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1889 Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1891 if (!md.isStatic()) {
1892 ClassDescriptor currentMethodThisType = getClassTypeDescriptor(md.getThis());
1893 for (int i = 0; i < curPrefix.size(); i++) {
1894 ClassDescriptor prefixType = getClassTypeDescriptor(curPrefix.get(i).getLocDescriptor());
1895 if (prefixType != null && prefixType.equals(currentMethodThisType)) {
1896 System.out.println("PREFIX TYPE MATCHES WITH=" + currentMethodThisType);
1898 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1899 boolean hasCompReturnLocWithThis =
1900 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1901 if (hasCompReturnLocWithThis) {
1902 if (checkNodeReachToReturnNode(md, flowNode)) {
1908 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1909 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1910 if (subGlobalReachalbeNode.getLocTuple().get(0).getLocDescriptor().equals(md.getThis())) {
1911 System.out.println("PREFIX FOUND=" + subGlobalReachalbeNode);
1919 Location lastLocationOfPrefix = curPrefix.get(curPrefix.size() - 1);
1920 // check whether prefix appears in the list of parameters
1921 Set<MethodInvokeNode> minSet = mapMethodDescToMethodInvokeNodeSet.get(md);
1922 System.out.println("$$$md=" + md + " minSet=" + minSet);
1923 if (minSet == null) {
1926 found: for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1927 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1928 Map<Integer, NTuple<Descriptor>> map = mapMethodInvokeNodeToArgIdxMap.get(min);
1929 Set<Integer> keySet = map.keySet();
1930 // System.out.println("min=" + min.printNode(0));
1932 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1933 Integer argIdx = (Integer) iterator2.next();
1934 NTuple<Descriptor> argTuple = map.get(argIdx);
1936 if (!(!md.isStatic() && argIdx == 0)) {
1937 // if the argTuple is empty, we don't need to do with anything(LITERAL CASE).
1938 if (argTuple.size() > 0
1939 && argTuple.get(argTuple.size() - 1).equals(lastLocationOfPrefix.getLocDescriptor())) {
1940 NTuple<Location> locTuple =
1941 translateToLocTuple(md, flowGraph.getParamFlowNode(argIdx).getDescTuple());
1942 lastLocationOfPrefix = locTuple.get(0);
1943 System.out.println("ARG CASE=" + locTuple);
1944 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1945 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1946 // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
1947 NTuple<Location> globalReachlocTuple = subGlobalReachalbeNode.getLocTuple();
1948 for (int i = 0; i < globalReachlocTuple.size(); i++) {
1949 if (globalReachlocTuple.get(i).equals(lastLocationOfPrefix)) {
1950 System.out.println("ARG " + argTuple + " IS MATCHED WITH="
1951 + lastLocationOfPrefix);
1964 private boolean checkNodeReachToReturnNode(MethodDescriptor md, FlowNode node) {
1966 FlowGraph flowGraph = getFlowGraph(md);
1967 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(node);
1968 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1969 boolean hasCompReturnLocWithThis =
1970 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1972 if (hasCompReturnLocWithThis) {
1973 for (Iterator iterator = flowGraph.getReturnNodeSet().iterator(); iterator.hasNext();) {
1974 FlowNode returnFlowNode = (FlowNode) iterator.next();
1975 if (reachableSet.contains(returnFlowNode)) {
1984 private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
1985 CompositeLocation newCompLoc = compLocPrefix.clone();
1986 NTuple<Location> locTuple = node.getLocTuple();
1987 for (int i = 1; i < locTuple.size(); i++) {
1988 newCompLoc.addLocation(locTuple.get(i));
1990 node.setInferCompositeLocation(newCompLoc);
1993 private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
1995 System.out.println("\n##### calculatePrefixList node=" + node);
1997 Set<GlobalFlowNode> incomingNodeSetPrefix =
1998 graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
1999 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
2001 Set<GlobalFlowNode> reachableNodeSetPrefix =
2002 graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
2003 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
2005 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
2007 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
2008 GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
2009 NTuple<Location> inNodeTuple = inNode.getLocTuple();
2011 if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
2012 || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
2016 for (int i = 1; i < inNodeTuple.size(); i++) {
2017 NTuple<Location> prefix = inNodeTuple.subList(0, i);
2018 if (!prefixList.contains(prefix)) {
2019 prefixList.add(prefix);
2024 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
2025 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
2026 int s0 = arg0.size();
2027 int s1 = arg1.size();
2030 } else if (s0 == s1) {
2042 private CompositeLocation calculateCompositeLocationFromFlowGraph(MethodDescriptor md,
2045 System.out.println("#############################################################");
2046 System.out.println("calculateCompositeLocationFromFlowGraph=" + node);
2048 FlowGraph flowGraph = getFlowGraph(md);
2049 // NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
2050 // GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
2052 List<NTuple<Location>> prefixList = calculatePrefixListFlowGraph(flowGraph, node);
2054 // Set<GlobalFlowNode> reachableNodeSet =
2055 // subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
2057 Set<FlowNode> reachableNodeSet =
2058 flowGraph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
2060 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
2062 // System.out.println("node=" + node + " prefixList=" + prefixList);
2064 for (int i = 0; i < prefixList.size(); i++) {
2065 NTuple<Location> curPrefix = prefixList.get(i);
2066 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
2068 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
2069 FlowNode reachNode = (FlowNode) iterator2.next();
2070 NTuple<Location> reachLocTuple = translateToLocTuple(md, reachNode.getCurrentDescTuple());
2071 if (reachLocTuple.startsWith(curPrefix)) {
2072 reachableCommonPrefixSet.add(reachLocTuple);
2075 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
2077 if (!reachableCommonPrefixSet.isEmpty()) {
2079 MethodDescriptor curPrefixFirstElementMethodDesc =
2080 (MethodDescriptor) curPrefix.get(0).getDescriptor();
2082 Location curPrefixLocalLoc = curPrefix.get(0);
2084 Location targetLocalLoc = new Location(md, node.getDescTuple().get(0));
2085 // Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
2087 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
2088 System.out.println("NEED2ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
2089 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
2091 node.setCompositeLocation(newCompLoc);
2101 private List<NTuple<Location>> calculatePrefixListFlowGraph(FlowGraph graph, FlowNode node) {
2103 System.out.println("\n##### calculatePrefixList node=" + node);
2105 MethodDescriptor md = graph.getMethodDescriptor();
2106 Set<FlowNode> incomingNodeSetPrefix =
2107 graph.getIncomingNodeSetByPrefix(node.getDescTuple().get(0));
2108 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
2110 Set<FlowNode> reachableNodeSetPrefix =
2111 graph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
2112 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
2114 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
2116 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
2117 FlowNode inNode = (FlowNode) iterator.next();
2118 NTuple<Location> inNodeTuple = translateToLocTuple(md, inNode.getCurrentDescTuple());
2120 // if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
2121 // || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
2125 for (int i = 1; i < inNodeTuple.size(); i++) {
2126 NTuple<Location> prefix = inNodeTuple.subList(0, i);
2127 if (!prefixList.contains(prefix)) {
2128 prefixList.add(prefix);
2133 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
2134 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
2135 int s0 = arg0.size();
2136 int s1 = arg1.size();
2139 } else if (s0 == s1) {
2151 private boolean containsClassDesc(ClassDescriptor cd, NTuple<Location> prefixLocTuple) {
2152 for (int i = 0; i < prefixLocTuple.size(); i++) {
2153 Location loc = prefixLocTuple.get(i);
2154 Descriptor locDesc = loc.getLocDescriptor();
2155 if (locDesc != null) {
2156 ClassDescriptor type = getClassTypeDescriptor(locDesc);
2157 if (type != null && type.equals(cd)) {
2165 private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
2167 MethodDescriptor md = flowGraph.getMethodDescriptor();
2169 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(md);
2171 // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
2172 Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
2174 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
2176 FlowEdge edge = (FlowEdge) iterator.next();
2177 NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
2178 NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
2180 if (flowGraph.getFlowNode(srcDescTuple) instanceof FlowReturnNode
2181 || flowGraph.getFlowNode(dstDescTuple) instanceof FlowReturnNode) {
2185 // here only keep the first element(method location) of the descriptor
2187 NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
2188 NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
2190 globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
2197 private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
2199 NTuple<Location> locTuple = new NTuple<Location>();
2201 Descriptor enclosingDesc = md;
2202 for (int i = 0; i < descTuple.size(); i++) {
2203 Descriptor desc = descTuple.get(i);
2205 Location loc = new Location(enclosingDesc, desc);
2208 if (desc instanceof VarDescriptor) {
2209 enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
2210 } else if (desc instanceof FieldDescriptor) {
2211 enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
2213 enclosingDesc = desc;
2222 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller) {
2224 // the transformation for a call site propagates flows through parameters
2225 // if the method is virtual, it also grab all relations from any possible
2228 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
2230 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
2231 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
2232 MethodDescriptor mdCallee = min.getMethod();
2233 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2234 if (mdCallee.isStatic()) {
2235 setPossibleCallees.add(mdCallee);
2237 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
2238 // removes method descriptors that are not invoked by the caller
2239 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
2240 setPossibleCallees.addAll(calleeSet);
2243 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
2244 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
2245 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
2252 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
2253 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
2255 System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
2256 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
2257 Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
2259 System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
2260 + mapMethodInvokeNodeToArgIdxMap.get(min));
2262 Set<Integer> keySet = mapIdxToArg.keySet();
2263 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2264 Integer idx = (Integer) iterator.next();
2265 NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
2266 if (argDescTuple.size() > 0) {
2267 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
2268 NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
2269 NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
2270 System.out.println("-------paramDescTuple=" + paramDescTuple + "->argDescTuple="
2272 addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
2276 // addValueFlowBetweenParametersToCaller(min, mdCaller, possibleMdCallee);
2278 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2279 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
2280 Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
2281 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
2282 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
2283 addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
2286 System.out.println("$$$GLOBAL PC LOC ADD=" + mdCaller);
2287 Set<NTuple<Location>> pcLocTupleSet = mapMethodInvokeNodeToPCLocTupleSet.get(min);
2288 System.out.println("---pcLocTupleSet=" + pcLocTupleSet);
2289 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
2290 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
2291 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
2292 if (calleeNode.isParamNodeWithIncomingFlows()) {
2293 System.out.println("calleeNode.getLocTuple()" + calleeNode.getLocTuple());
2294 NTuple<Location> callerSrcNodeLocTuple =
2295 translateToCallerLocTuple(min, possibleMdCallee, mdCaller, calleeNode.getLocTuple());
2296 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
2297 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
2298 for (Iterator iterator2 = pcLocTupleSet.iterator(); iterator2.hasNext();) {
2299 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator2.next();
2301 callerSubGlobalGraph.addValueFlowEdge(pcLocTuple, callerSrcNodeLocTuple);
2310 private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
2311 MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
2313 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
2314 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
2316 System.out.println("$addValueFlowFromCalleeNode calleeSrcNode=" + calleeSrcNode);
2318 NTuple<Location> callerSrcNodeLocTuple =
2319 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
2320 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
2322 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
2324 Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
2326 for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
2327 GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
2328 NTuple<Location> callerDstNodeLocTuple =
2329 translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
2330 // System.out.println("outNode=" + outNode + " callerDstNodeLocTuple="
2331 // + callerDstNodeLocTuple);
2332 if (callerSrcNodeLocTuple != null && callerDstNodeLocTuple != null
2333 && callerSrcNodeLocTuple.size() > 0 && callerDstNodeLocTuple.size() > 0) {
2334 callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
2341 private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
2342 MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
2343 // this method will return the same nodeLocTuple if the corresponding argument is literal
2346 // System.out.println("translateToCallerLocTuple=" + nodeLocTuple);
2348 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
2349 NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
2350 if (calleeFlowGraph.isParameter(nodeDescTuple)) {
2351 int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
2352 NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
2354 // if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
2355 // // the type of argument is primitive.
2356 // return nodeLocTuple.clone();
2358 // System.out.println("paramIdx=" + paramIdx + " argDescTuple=" + argDescTuple + " from min="
2359 // + min.printNode(0));
2360 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
2362 NTuple<Location> callerLocTuple = new NTuple<Location>();
2364 callerLocTuple.addAll(argLocTuple);
2365 for (int i = 1; i < nodeLocTuple.size(); i++) {
2366 callerLocTuple.add(nodeLocTuple.get(i));
2368 return callerLocTuple;
2370 return nodeLocTuple.clone();
2375 public static boolean isPrimitive(Descriptor desc) {
2377 if (desc instanceof FieldDescriptor) {
2378 return ((FieldDescriptor) desc).getType().isPrimitive();
2379 } else if (desc instanceof VarDescriptor) {
2380 return ((VarDescriptor) desc).getType().isPrimitive();
2381 } else if (desc instanceof InterDescriptor) {
2388 public static boolean isReference(Descriptor desc) {
2390 if (desc instanceof FieldDescriptor) {
2392 TypeDescriptor type = ((FieldDescriptor) desc).getType();
2393 if (type.isArray()) {
2394 return !type.isPrimitive();
2396 return type.isPtr();
2399 } else if (desc instanceof VarDescriptor) {
2400 TypeDescriptor type = ((VarDescriptor) desc).getType();
2401 if (type.isArray()) {
2402 return !type.isPrimitive();
2404 return type.isPtr();
2411 private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
2413 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
2414 for (int i = 0; i < locTuple.size(); i++) {
2415 descTuple.add(locTuple.get(i).getLocDescriptor());
2421 public LocationSummary getLocationSummary(Descriptor d) {
2422 if (!mapDescToLocationSummary.containsKey(d)) {
2423 if (d instanceof MethodDescriptor) {
2424 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
2425 } else if (d instanceof ClassDescriptor) {
2426 mapDescToLocationSummary.put(d, new FieldSummary());
2429 return mapDescToLocationSummary.get(d);
2432 private void generateMethodSummary() {
2434 Set<MethodDescriptor> keySet = md2lattice.keySet();
2435 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2436 MethodDescriptor md = (MethodDescriptor) iterator.next();
2438 System.out.println("\nSSJAVA: generate method summary: " + md);
2440 FlowGraph flowGraph = getFlowGraph(md);
2441 MethodSummary methodSummary = getMethodSummary(md);
2443 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
2445 // set the 'this' reference location
2446 if (!md.isStatic()) {
2447 System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
2448 methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
2451 // set the 'global' reference location if needed
2452 if (methodSummary.hasGlobalAccess()) {
2453 methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
2456 // construct a parameter mapping that maps a parameter descriptor to an
2457 // inferred composite location
2458 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
2459 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
2460 CompositeLocation inferredCompLoc =
2461 updateCompositeLocation(flowNode.getCompositeLocation());
2462 System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc + " original="
2463 + flowNode.getCompositeLocation());
2465 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
2466 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
2467 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
2474 private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2476 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2478 for (int idx = 0; idx < size; idx++) {
2479 Location loc1 = locTuple1.get(idx);
2480 Location loc2 = locTuple2.get(idx);
2482 Descriptor desc1 = loc1.getDescriptor();
2483 Descriptor desc2 = loc2.getDescriptor();
2485 if (!desc1.equals(desc2)) {
2486 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2489 Descriptor locDesc1 = loc1.getLocDescriptor();
2490 Descriptor locDesc2 = loc2.getLocDescriptor();
2492 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2494 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2495 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2497 System.out.println("---node1=" + node1 + " node2=" + node2);
2498 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2499 + hierarchyGraph.getIncomingNodeSet(node2));
2501 if (locDesc1.equals(locDesc2)) {
2503 } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
2504 && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
2516 private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2518 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2520 for (int idx = 0; idx < size; idx++) {
2521 Location loc1 = locTuple1.get(idx);
2522 Location loc2 = locTuple2.get(idx);
2524 Descriptor desc1 = loc1.getDescriptor();
2525 Descriptor desc2 = loc2.getDescriptor();
2527 if (!desc1.equals(desc2)) {
2528 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2531 Descriptor locDesc1 = loc1.getLocDescriptor();
2532 Descriptor locDesc2 = loc2.getLocDescriptor();
2534 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2536 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2537 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2539 System.out.println("---node1=" + node1 + " node2=" + node2);
2540 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2541 + hierarchyGraph.getIncomingNodeSet(node2));
2543 if (locDesc1.equals(locDesc2)) {
2545 } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
2556 private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
2557 CompositeLocation newCompLoc = new CompositeLocation();
2559 // System.out.println("compLoc=" + compLoc);
2560 for (int i = 0; i < compLoc.getSize(); i++) {
2561 Location loc = compLoc.get(i);
2562 Descriptor enclosingDescriptor = loc.getDescriptor();
2563 Descriptor locDescriptor = loc.getLocDescriptor();
2565 HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
2566 // System.out.println("-hnode=" + hnode + " from=" + locDescriptor +
2567 // " enclosingDescriptor="
2568 // + enclosingDescriptor);
2569 // System.out.println("-getLocationSummary(enclosingDescriptor)="
2570 // + getLocationSummary(enclosingDescriptor));
2571 String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
2572 // System.out.println("-locName=" + locName);
2573 Location newLoc = new Location(enclosingDescriptor, locName);
2574 newLoc.setLocDescriptor(locDescriptor);
2575 newCompLoc.addLocation(newLoc);
2581 private void debug_writeLattices() {
2583 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
2584 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2585 Descriptor key = (Descriptor) iterator.next();
2586 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
2587 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
2588 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
2589 if (key instanceof ClassDescriptor) {
2590 // writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
2592 } else if (key instanceof MethodDescriptor) {
2593 MethodDescriptor md = (MethodDescriptor) key;
2594 // writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
2598 LocationSummary ls = getLocationSummary(key);
2599 System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
2602 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
2603 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
2604 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2605 System.out.println("########cd=" + cd);
2606 writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
2607 cd2lattice.get(cd), "");
2608 COUNT += cd2lattice.get(cd).getKeySet().size();
2611 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
2612 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
2613 MethodDescriptor md = (MethodDescriptor) iterator.next();
2614 writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
2615 md2lattice.get(md), "");
2616 COUNT += md2lattice.get(md).getKeySet().size();
2618 System.out.println("###COUNT=" + COUNT);
2621 private void buildLattice(Descriptor desc) {
2622 System.out.println("buildLattice=" + desc);
2623 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
2625 addMapDescToSimpleLattice(desc, simpleLattice);
2627 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2628 System.out.println("\n## insertIntermediateNodesToStraightLine:"
2629 + simpleHierarchyGraph.getName());
2630 SSJavaLattice<String> lattice =
2631 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2632 lattice.removeRedundantEdges();
2634 if (desc instanceof ClassDescriptor) {
2636 cd2lattice.put((ClassDescriptor) desc, lattice);
2637 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2638 } else if (desc instanceof MethodDescriptor) {
2640 md2lattice.put((MethodDescriptor) desc, lattice);
2641 MethodDescriptor md = (MethodDescriptor) desc;
2642 ClassDescriptor cd = md.getClassDesc();
2643 // ssjava.writeLatticeDotFile(cd, md, lattice);
2648 private void buildLattice() {
2650 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
2651 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2652 Descriptor desc = (Descriptor) iterator.next();
2654 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
2656 addMapDescToSimpleLattice(desc, simpleLattice);
2658 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2659 System.out.println("\n## insertIntermediateNodesToStraightLine:"
2660 + simpleHierarchyGraph.getName());
2661 SSJavaLattice<String> lattice =
2662 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2663 lattice.removeRedundantEdges();
2665 if (desc instanceof ClassDescriptor) {
2667 cd2lattice.put((ClassDescriptor) desc, lattice);
2668 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2669 } else if (desc instanceof MethodDescriptor) {
2671 md2lattice.put((MethodDescriptor) desc, lattice);
2672 MethodDescriptor md = (MethodDescriptor) desc;
2673 ClassDescriptor cd = md.getClassDesc();
2674 // ssjava.writeLatticeDotFile(cd, md, lattice);
2681 private void buildLatticeInheritanceTree() {
2682 // DFS the inheritance tree and propagates lattice nodes/edges from the parent to children
2683 // Node<ClassDescriptor> rootNode = inheritanceTree.getRootNode();
2684 DFSBuildLatticeInheritanceTree(rootClassDescriptor);
2687 public Set<ClassDescriptor> getDirectSubClasses(ClassDescriptor parent) {
2689 System.out.println("$$$toanalyze_classDescSet=" + toanalyze_classDescSet);
2690 Set<ClassDescriptor> result = new HashSet<ClassDescriptor>();
2692 Set<ClassDescriptor> children = tu.getDirectSubClasses(parent);
2693 if (children == null) {
2694 children = new HashSet<ClassDescriptor>();
2697 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
2698 ClassDescriptor child = (ClassDescriptor) iterator.next();
2699 if (toanalyze_classDescSet.contains(child)) {
2707 private void DFSBuildLatticeInheritanceTree(ClassDescriptor cd) {
2708 // ClassDescriptor cd = node.getData();
2710 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2711 if (parentClassDesc != null) {
2712 Map<TripleItem, String> parentMap = buildLattice.getIntermediateLocMap(parentClassDesc);
2713 buildLattice.setIntermediateLocMap(cd, parentMap);
2718 for (Iterator iterator = cd.getMethods(); iterator.hasNext();) {
2719 MethodDescriptor md = (MethodDescriptor) iterator.next();
2720 if (toanalyze_methodDescList.contains(md)) {
2721 MethodDescriptor parentMethodDesc = getParentMethodDesc(md.getClassDesc(), md);
2722 if (parentMethodDesc != null) {
2723 Map<TripleItem, String> parentMap = buildLattice.getIntermediateLocMap(parentMethodDesc);
2724 buildLattice.setIntermediateLocMap(md, parentMap);
2730 // traverse children
2731 Set<ClassDescriptor> children = getDirectSubClasses(cd);
2732 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
2733 ClassDescriptor classDescriptor = (ClassDescriptor) iterator.next();
2734 if (toanalyze_classDescSet.contains(classDescriptor)) {
2735 DFSBuildLatticeInheritanceTree(classDescriptor);
2742 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
2743 mapDescriptorToSimpleLattice.put(desc, lattice);
2746 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
2747 return mapDescriptorToSimpleLattice.get(desc);
2750 private void simplifyHierarchyGraph() {
2751 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2752 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2753 Descriptor desc = (Descriptor) iterator.next();
2754 // System.out.println("SSJAVA: remove redundant edges: " + desc);
2755 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
2756 simpleHierarchyGraph.setName(desc + "_SIMPLE");
2757 simpleHierarchyGraph.removeRedundantEdges();
2758 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
2762 private void insertCombinationNodes() {
2763 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
2764 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2765 Descriptor desc = (Descriptor) iterator.next();
2766 System.out.println("\nSSJAVA: Inserting Combination Nodes:" + desc);
2767 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2768 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
2769 skeletonGraphWithCombinationNode.setName(desc + "_SC");
2771 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2772 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2773 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph,
2775 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2776 skeletonGraphWithCombinationNode.removeRedundantEdges();
2777 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
2781 private void constructSkeletonHierarchyGraph() {
2782 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2783 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2784 Descriptor desc = (Descriptor) iterator.next();
2785 System.out.println("SSJAVA: Constructing Skeleton Hierarchy Graph: " + desc);
2786 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
2787 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
2788 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
2789 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
2790 skeletonGraph.simplifyHierarchyGraph();
2791 // skeletonGraph.combineRedundantNodes(false);
2792 // skeletonGraph.removeRedundantEdges();
2793 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
2797 private void debug_writeHierarchyDotFiles() {
2799 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2800 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2801 Descriptor desc = (Descriptor) iterator.next();
2802 getHierarchyGraph(desc).writeGraph();
2807 private void debug_writeSimpleHierarchyDotFiles() {
2809 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2810 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2811 Descriptor desc = (Descriptor) iterator.next();
2812 getHierarchyGraph(desc).writeGraph();
2813 getSimpleHierarchyGraph(desc).writeGraph();
2814 getSimpleHierarchyGraph(desc).writeGraph(true);
2819 private void debug_writeSkeletonHierarchyDotFiles() {
2821 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2822 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2823 Descriptor desc = (Descriptor) iterator.next();
2824 getSkeletonHierarchyGraph(desc).writeGraph();
2829 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
2831 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2832 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2833 Descriptor desc = (Descriptor) iterator.next();
2834 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
2839 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
2840 return mapDescriptorToSimpleHierarchyGraph.get(d);
2843 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
2844 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
2845 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2847 return mapDescriptorToSkeletonHierarchyGraph.get(d);
2850 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
2851 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
2852 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2854 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
2857 private void constructHierarchyGraph() {
2859 LinkedList<MethodDescriptor> methodDescList =
2860 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
2862 while (!methodDescList.isEmpty()) {
2863 MethodDescriptor md = methodDescList.removeLast();
2864 if (state.SSJAVADEBUG) {
2865 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
2866 System.out.println();
2867 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
2868 constructHierarchyGraph(md, hierarchyGraph);
2869 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
2875 while (!toAnalyzeIsEmpty()) {
2876 ClassDescriptor cd = toAnalyzeNext();
2877 HierarchyGraph graph = getHierarchyGraph(cd);
2878 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2879 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
2880 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
2881 graph.getHNode(fieldDesc);
2886 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2887 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2888 Descriptor key = (Descriptor) iterator.next();
2889 HierarchyGraph graph = getHierarchyGraph(key);
2891 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
2892 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
2893 HNode node = (HNode) iterator2.next();
2894 if (!node.isSkeleton() && !node.isCombinationNode()) {
2895 if (graph.getIncomingNodeSet(node).size() == 0) {
2896 nodeToBeConnected.add(node);
2901 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
2902 HNode node = (HNode) iterator2.next();
2903 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
2904 graph.addEdge(graph.getHNode(TOPDESC), node);
2911 private void constructHierarchyGraph2() {
2913 // do fixed-point analysis
2915 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2917 // Collections.sort(descriptorListToAnalyze, new
2918 // Comparator<MethodDescriptor>() {
2919 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
2920 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
2924 // current descriptors to visit in fixed-point interprocedural analysis,
2925 // prioritized by dependency in the call graph
2926 methodDescriptorsToVisitStack.clear();
2928 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2929 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2931 while (!descriptorListToAnalyze.isEmpty()) {
2932 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2933 methodDescriptorsToVisitStack.add(md);
2936 // analyze scheduled methods until there are no more to visit
2937 while (!methodDescriptorsToVisitStack.isEmpty()) {
2938 // start to analyze leaf node
2939 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2941 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
2942 // MethodSummary methodSummary = new MethodSummary(md);
2944 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
2945 // curMethodInfo = methodInfo;
2947 System.out.println();
2948 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
2950 constructHierarchyGraph(md, hierarchyGraph);
2952 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
2953 // MethodSummary prevMethodSummary = getMethodSummary(md);
2955 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
2957 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
2958 // mapDescToLocationSummary.put(md, methodSummary);
2960 // results for callee changed, so enqueue dependents caller for
2962 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
2963 while (depsItr.hasNext()) {
2964 MethodDescriptor methodNext = depsItr.next();
2965 if (!methodDescriptorsToVisitStack.contains(methodNext)
2966 && methodDescriptorToVistSet.contains(methodNext)) {
2967 methodDescriptorsToVisitStack.add(methodNext);
2976 while (!toAnalyzeIsEmpty()) {
2977 ClassDescriptor cd = toAnalyzeNext();
2978 HierarchyGraph graph = getHierarchyGraph(cd);
2979 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2980 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
2981 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
2982 graph.getHNode(fieldDesc);
2987 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2988 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2989 Descriptor key = (Descriptor) iterator.next();
2990 HierarchyGraph graph = getHierarchyGraph(key);
2992 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
2993 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
2994 HNode node = (HNode) iterator2.next();
2995 if (!node.isSkeleton() && !node.isCombinationNode()) {
2996 if (graph.getIncomingNodeSet(node).size() == 0) {
2997 nodeToBeConnected.add(node);
3002 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
3003 HNode node = (HNode) iterator2.next();
3004 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
3005 graph.addEdge(graph.getHNode(TOPDESC), node);
3012 private HierarchyGraph getHierarchyGraph(Descriptor d) {
3013 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
3014 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
3016 return mapDescriptorToHierarchyGraph.get(d);
3019 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
3021 // visit each node of method flow graph
3022 FlowGraph fg = getFlowGraph(md);
3023 // Set<FlowNode> nodeSet = fg.getNodeSet();
3025 Set<FlowEdge> edgeSet = fg.getEdgeSet();
3027 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
3028 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
3029 Descriptor desc = (Descriptor) iterator.next();
3030 methodGraph.getHNode(desc).setSkeleton(true);
3033 // for the method lattice, we need to look at the first element of
3034 // NTuple<Descriptor>
3035 boolean hasGlobalAccess = false;
3036 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3037 // FlowNode originalSrcNode = (FlowNode) iterator.next();
3038 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
3039 FlowEdge edge = (FlowEdge) iterator.next();
3041 FlowNode originalSrcNode = fg.getFlowNode(edge.getInitTuple());
3042 Set<FlowNode> sourceNodeSet = new HashSet<FlowNode>();
3043 if (originalSrcNode instanceof FlowReturnNode) {
3044 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
3045 System.out.println("rnode=" + rnode);
3046 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
3047 for (Iterator iterator2 = tupleSet.iterator(); iterator2.hasNext();) {
3048 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator2.next();
3049 sourceNodeSet.add(fg.getFlowNode(nTuple));
3050 System.out.println("&&&SOURCE fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
3053 sourceNodeSet.add(originalSrcNode);
3056 // System.out.println("---sourceNodeSet=" + sourceNodeSet + " from originalSrcNode="
3057 // + originalSrcNode);
3059 for (Iterator iterator3 = sourceNodeSet.iterator(); iterator3.hasNext();) {
3060 FlowNode srcNode = (FlowNode) iterator3.next();
3062 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
3063 Descriptor srcLocalDesc = srcNodeTuple.get(0);
3065 if (srcLocalDesc instanceof InterDescriptor
3066 && ((InterDescriptor) srcLocalDesc).getMethodArgIdxPair() != null) {
3068 if (srcNode.getCompositeLocation() == null) {
3073 // if the srcNode is started with the global descriptor
3074 // need to set as a skeleton node
3075 if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
3076 hasGlobalAccess = true;
3079 // Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(originalSrcNode);
3080 // for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3081 // FlowEdge outEdge = (FlowEdge) iterator2.next();
3082 // FlowNode originalDstNode = outEdge.getDst();
3083 FlowNode originalDstNode = fg.getFlowNode(edge.getEndTuple());
3085 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
3086 if (originalDstNode instanceof FlowReturnNode) {
3087 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
3088 // System.out.println("\n-returnNode=" + rnode);
3089 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
3090 for (Iterator iterator4 = tupleSet.iterator(); iterator4.hasNext();) {
3091 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator4.next();
3092 dstNodeSet.add(fg.getFlowNode(nTuple));
3093 System.out.println("&&&DST fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
3096 dstNodeSet.add(originalDstNode);
3098 // System.out.println("---dstNodeSet=" + dstNodeSet);
3099 for (Iterator iterator4 = dstNodeSet.iterator(); iterator4.hasNext();) {
3100 FlowNode dstNode = (FlowNode) iterator4.next();
3102 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
3103 Descriptor dstLocalDesc = dstNodeTuple.get(0);
3105 if (dstLocalDesc instanceof InterDescriptor
3106 && ((InterDescriptor) dstLocalDesc).getMethodArgIdxPair() != null) {
3107 if (dstNode.getCompositeLocation() == null) {
3108 System.out.println("%%%%%%%%%%%%%SKIP=" + dstNode);
3113 // if (outEdge.getInitTuple().equals(srcNodeTuple)
3114 // && outEdge.getEndTuple().equals(dstNodeTuple)) {
3116 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
3117 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
3119 // //////////////////////////
3120 // inheritance check
3121 if (mapMethodDescToHighestOverriddenMethodDesc.containsKey(md)) {
3123 MethodDescriptor highestOverriddenMethodDesc =
3124 mapMethodDescToHighestOverriddenMethodDesc.get(md);
3126 if (srcCurTuple.get(srcCurTuple.size() - 1).getSymbol().startsWith(PCLOC)) {
3130 // //////////////////////////
3132 System.out.println("-srcCurTuple=" + srcCurTuple + " dstCurTuple=" + dstCurTuple
3133 + " srcNode=" + srcNode + " dstNode=" + dstNode);
3135 // srcCurTuple = translateBaseTuple(srcNode, srcCurTuple);
3136 // dstCurTuple = translateBaseTuple(dstNode, dstCurTuple);
3138 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
3139 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
3141 // value flows between fields
3142 Descriptor desc = srcCurTuple.get(0);
3143 ClassDescriptor classDesc;
3145 if (desc.equals(GLOBALDESC)) {
3146 classDesc = md.getClassDesc();
3148 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
3149 classDesc = varDesc.getType().getClassDesc();
3151 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
3153 } else if ((srcCurTuple.size() == 1 && dstCurTuple.size() == 1)
3154 || ((srcCurTuple.size() > 1 || dstCurTuple.size() > 1) && !srcCurTuple.get(0).equals(
3155 dstCurTuple.get(0)))) {
3157 // value flow between a primitive local var - a primitive local var or local var -
3160 Descriptor srcDesc = srcCurTuple.get(0);
3161 Descriptor dstDesc = dstCurTuple.get(0);
3163 methodGraph.addEdge(srcDesc, dstDesc);
3165 if (fg.isParamDesc(srcDesc)) {
3166 methodGraph.setParamHNode(srcDesc);
3168 if (fg.isParamDesc(dstDesc)) {
3169 methodGraph.setParamHNode(dstDesc);
3183 // If the method accesses static fields
3184 // set hasGloabalAccess true in the method summary.
3185 if (hasGlobalAccess) {
3186 getMethodSummary(md).setHasGlobalAccess();
3188 methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
3190 if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
3191 // if the current method contains the event loop
3192 // we need to set all nodes of the hierarchy graph as a skeleton node
3193 Set<HNode> hnodeSet = methodGraph.getNodeSet();
3194 for (Iterator iterator = hnodeSet.iterator(); iterator.hasNext();) {
3195 HNode hnode = (HNode) iterator.next();
3196 hnode.setSkeleton(true);
3202 private NTuple<Descriptor> translateBaseTuple(FlowNode flowNode, NTuple<Descriptor> inTuple) {
3204 if (flowNode.getBaseTuple() != null) {
3206 NTuple<Descriptor> translatedTuple = new NTuple<Descriptor>();
3208 NTuple<Descriptor> baseTuple = flowNode.getBaseTuple();
3210 for (int i = 0; i < baseTuple.size(); i++) {
3211 translatedTuple.add(baseTuple.get(i));
3214 for (int i = 1; i < inTuple.size(); i++) {
3215 translatedTuple.add(inTuple.get(i));
3218 System.out.println("------TRANSLATED " + inTuple + " -> " + translatedTuple);
3219 return translatedTuple;
3227 private MethodSummary getMethodSummary(MethodDescriptor md) {
3228 if (!mapDescToLocationSummary.containsKey(md)) {
3229 mapDescToLocationSummary.put(md, new MethodSummary(md));
3231 return (MethodSummary) mapDescToLocationSummary.get(md);
3234 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
3236 String classSymbol = cd.getSymbol();
3237 int idx = classSymbol.lastIndexOf("$");
3239 classSymbol = classSymbol.substring(idx + 1);
3242 String pattern = "class " + classSymbol + " ";
3243 if (strLine.indexOf(pattern) != -1) {
3244 mapDescToDefinitionLine.put(cd, lineNum);
3248 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
3250 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
3251 MethodDescriptor md = (MethodDescriptor) iterator.next();
3252 String pattern = md.getMethodDeclaration();
3253 if (strLine.indexOf(pattern) != -1) {
3254 mapDescToDefinitionLine.put(md, lineNum);
3255 methodSet.remove(md);
3262 private void readOriginalSourceFiles() {
3264 SymbolTable classtable = state.getClassSymbolTable();
3266 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
3267 classDescSet.addAll(classtable.getValueSet());
3270 // inefficient implement. it may re-visit the same file if the file
3271 // contains more than one class definitions.
3272 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
3273 ClassDescriptor cd = (ClassDescriptor) iterator.next();
3275 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
3276 methodSet.addAll(cd.getMethodTable().getValueSet());
3278 String sourceFileName = cd.getSourceFileName();
3279 Vector<String> lineVec = new Vector<String>();
3281 mapFileNameToLineVector.put(sourceFileName, lineVec);
3283 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
3286 lineVec.add(""); // the index is started from 1.
3287 while ((strLine = in.readLine()) != null) {
3288 lineVec.add(lineNum, strLine);
3289 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
3290 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
3296 } catch (IOException e) {
3297 e.printStackTrace();
3302 private String generateLatticeDefinition(Descriptor desc) {
3304 Set<String> sharedLocSet = new HashSet<String>();
3306 SSJavaLattice<String> lattice = getLattice(desc);
3307 String rtr = "@LATTICE(\"";
3309 Map<String, Set<String>> map = lattice.getTable();
3310 Set<String> keySet = map.keySet();
3311 boolean first = true;
3312 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
3313 String key = (String) iterator.next();
3314 if (!key.equals(lattice.getTopItem())) {
3315 Set<String> connectedSet = map.get(key);
3317 if (connectedSet.size() == 1) {
3318 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
3326 if (lattice.isSharedLoc(key)) {
3327 rtr += "," + key + "*";
3332 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
3333 String loc = (String) iterator2.next();
3334 if (!loc.equals(lattice.getBottomItem())) {
3341 rtr += loc + "<" + key;
3342 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
3343 rtr += "," + key + "*";
3344 sharedLocSet.add(key);
3346 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
3347 rtr += "," + loc + "*";
3348 sharedLocSet.add(loc);
3356 if (desc instanceof MethodDescriptor) {
3357 System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
3359 MethodDescriptor md = (MethodDescriptor) desc;
3360 MethodSummary methodSummary = getMethodSummary(md);
3362 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
3363 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc) && returnType != null
3364 && (!returnType.isVoid())) {
3365 CompositeLocation returnLoc = methodSummary.getRETURNLoc();
3366 if (returnLoc.getSize() == 1) {
3367 String returnLocStr = generateLocationAnnoatation(methodSummary.getRETURNLoc());
3368 if (rtr.indexOf(returnLocStr) == -1) {
3369 rtr += "," + returnLocStr;
3375 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
3376 if (returnType != null && (!returnType.isVoid())) {
3378 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
3381 CompositeLocation pcLoc = methodSummary.getPCLoc();
3382 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
3383 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
3387 if (!md.isStatic()) {
3388 rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
3390 rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
3399 private void generateAnnoatedCode() {
3401 readOriginalSourceFiles();
3404 while (!toAnalyzeIsEmpty()) {
3405 ClassDescriptor cd = toAnalyzeNext();
3407 setupToAnalazeMethod(cd);
3409 String sourceFileName = cd.getSourceFileName();
3411 if (cd.isInterface()) {
3415 int classDefLine = mapDescToDefinitionLine.get(cd);
3416 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
3418 LocationSummary fieldLocSummary = getLocationSummary(cd);
3420 String fieldLatticeDefStr = generateLatticeDefinition(cd);
3421 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
3422 sourceVec.set(classDefLine, annoatedSrc);
3424 // generate annotations for field declarations
3425 // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
3426 Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
3428 for (Iterator iter = cd.getFields(); iter.hasNext();) {
3429 FieldDescriptor fd = (FieldDescriptor) iter.next();
3431 String locAnnotationStr;
3432 // CompositeLocation inferLoc = inferLocMap.get(fd);
3433 String locName = mapFieldNameToLocName.get(fd.getSymbol());
3435 if (locName != null) {
3436 // infer loc is null if the corresponding field is static and final
3437 // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
3438 locAnnotationStr = "@LOC(\"" + locName + "\")";
3439 int fdLineNum = fd.getLineNum();
3440 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
3441 String fieldDeclaration = fd.toString();
3442 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
3443 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
3444 sourceVec.set(fdLineNum, annoatedStr);
3449 while (!toAnalyzeMethodIsEmpty()) {
3450 MethodDescriptor md = toAnalyzeMethodNext();
3452 if (!ssjava.needTobeAnnotated(md)) {
3456 SSJavaLattice<String> methodLattice = md2lattice.get(md);
3457 if (methodLattice != null) {
3459 int methodDefLine = md.getLineNum();
3461 // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
3462 // Map<Descriptor, CompositeLocation> methodInferLocMap =
3463 // methodLocInfo.getMapDescToInferLocation();
3465 MethodSummary methodSummary = getMethodSummary(md);
3467 Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
3468 methodSummary.getMapVarDescToInferCompositeLocation();
3469 System.out.println("-----md=" + md);
3470 System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
3472 Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
3474 Set<String> localLocElementSet = methodLattice.getElementSet();
3476 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
3477 Descriptor localVarDesc = (Descriptor) iterator.next();
3478 System.out.println("-------localVarDesc=" + localVarDesc);
3479 CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
3481 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
3482 if (!localLocElementSet.contains(localLocIdentifier)) {
3483 methodLattice.put(localLocIdentifier);
3486 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
3488 if (!isParameter(md, localVarDesc)) {
3489 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
3490 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
3491 String orgSourceLine = sourceVec.get(varLineNum);
3492 System.out.println("varLineNum=" + varLineNum + " org src=" + orgSourceLine);
3494 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
3495 System.out.println("idx=" + idx
3496 + " generateVarDeclaration((VarDescriptor) localVarDesc)="
3497 + generateVarDeclaration((VarDescriptor) localVarDesc));
3499 String annoatedStr =
3500 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
3501 + orgSourceLine.substring(idx);
3502 sourceVec.set(varLineNum, annoatedStr);
3505 String methodDefStr = sourceVec.get(methodDefLine);
3508 getParamLocation(methodDefStr,
3509 generateVarDeclaration((VarDescriptor) localVarDesc));
3510 System.out.println("methodDefStr=" + methodDefStr + " localVarDesc=" + localVarDesc
3514 String annoatedStr =
3515 methodDefStr.substring(0, idx) + locAnnotationStr + " "
3516 + methodDefStr.substring(idx);
3517 sourceVec.set(methodDefLine, annoatedStr);
3522 // check if the lattice has to have the location type for the this
3525 // boolean needToAddthisRef = hasThisReference(md);
3526 // if (localLocElementSet.contains("this")) {
3527 // methodLattice.put("this");
3530 String methodLatticeDefStr = generateLatticeDefinition(md);
3531 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
3532 sourceVec.set(methodDefLine, annoatedStr);
3542 private boolean hasThisReference(MethodDescriptor md) {
3544 FlowGraph fg = getFlowGraph(md);
3545 Set<FlowNode> nodeSet = fg.getNodeSet();
3546 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3547 FlowNode flowNode = (FlowNode) iterator.next();
3548 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
3556 private int getParamLocation(String methodStr, String paramStr) {
3558 String pattern = paramStr + ",";
3560 int idx = methodStr.indexOf(pattern);
3564 pattern = paramStr + ")";
3565 return methodStr.indexOf(pattern);
3570 private String generateVarDeclaration(VarDescriptor varDesc) {
3572 TypeDescriptor td = varDesc.getType();
3573 String rtr = td.toString();
3575 for (int i = 0; i < td.getArrayCount(); i++) {
3579 rtr += " " + varDesc.getName();
3584 private String generateLocationAnnoatation(CompositeLocation loc) {
3587 Location methodLoc = loc.get(0);
3588 rtr += methodLoc.getLocIdentifier();
3590 for (int i = 1; i < loc.getSize(); i++) {
3591 Location element = loc.get(i);
3592 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
3598 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
3599 return getFlowGraph(md).isParamDesc(localVarDesc);
3602 private String extractFileName(String fileName) {
3603 int idx = fileName.lastIndexOf("/");
3607 return fileName.substring(idx + 1);
3612 private void codeGen() {
3614 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
3615 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
3616 String orgFileName = (String) iterator.next();
3617 String outputFileName = extractFileName(orgFileName);
3619 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
3623 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
3624 BufferedWriter out = new BufferedWriter(fileWriter);
3626 for (int i = 0; i < sourceVec.size(); i++) {
3627 out.write(sourceVec.get(i));
3631 } catch (IOException e) {
3632 e.printStackTrace();
3639 private void checkLattices() {
3641 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
3643 // current descriptors to visit in fixed-point interprocedural analysis,
3645 // dependency in the call graph
3646 methodDescriptorsToVisitStack.clear();
3648 // descriptorListToAnalyze.removeFirst();
3650 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
3651 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
3653 while (!descriptorListToAnalyze.isEmpty()) {
3654 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
3655 checkLatticesOfVirtualMethods(md);
3660 private void debug_writeLatticeDotFile() {
3661 // generate lattice dot file
3665 while (!toAnalyzeIsEmpty()) {
3666 ClassDescriptor cd = toAnalyzeNext();
3668 setupToAnalazeMethod(cd);
3670 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
3671 if (classLattice != null) {
3672 ssjava.writeLatticeDotFile(cd, null, classLattice);
3673 debug_printDescriptorToLocNameMapping(cd);
3676 while (!toAnalyzeMethodIsEmpty()) {
3677 MethodDescriptor md = toAnalyzeMethodNext();
3678 SSJavaLattice<String> methodLattice = md2lattice.get(md);
3679 if (methodLattice != null) {
3680 ssjava.writeLatticeDotFile(cd, md, methodLattice);
3681 debug_printDescriptorToLocNameMapping(md);
3688 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
3690 LocationInfo info = getLocationInfo(desc);
3691 System.out.println("## " + desc + " ##");
3692 System.out.println(info.getMapDescToInferLocation());
3693 LocationInfo locInfo = getLocationInfo(desc);
3694 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
3695 System.out.println("###################");
3699 private void calculateExtraLocations() {
3701 LinkedList<MethodDescriptor> methodDescList = ssjava.getSortedDescriptors();
3702 for (Iterator iterator = methodDescList.iterator(); iterator.hasNext();) {
3703 MethodDescriptor md = (MethodDescriptor) iterator.next();
3704 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
3705 calculateExtraLocations(md);
3711 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
3713 if (!md.isStatic()) {
3714 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3715 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3717 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
3718 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
3719 if (!md.equals(mdCallee)) {
3720 checkConsistency(md, mdCallee);
3728 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
3730 // check that two lattice have the same relations between parameters(+PC
3731 // LOC, GLOBAL_LOC RETURN LOC)
3733 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
3734 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
3736 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
3737 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
3739 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
3740 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
3742 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
3744 // add location types of paramters
3745 for (int idx = 0; idx < numParam; idx++) {
3746 list1.add(paramMap1.get(Integer.valueOf(idx)));
3747 list2.add(paramMap2.get(Integer.valueOf(idx)));
3750 // add program counter location
3751 list1.add(locInfo1.getPCLoc());
3752 list2.add(locInfo2.getPCLoc());
3754 if (!md1.getReturnType().isVoid()) {
3755 // add return value location
3756 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
3757 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
3762 // add global location type
3763 if (md1.isStatic()) {
3764 CompositeLocation globalLoc1 =
3765 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
3766 CompositeLocation globalLoc2 =
3767 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
3768 list1.add(globalLoc1);
3769 list2.add(globalLoc2);
3772 for (int i = 0; i < list1.size(); i++) {
3773 CompositeLocation locA1 = list1.get(i);
3774 CompositeLocation locA2 = list2.get(i);
3775 for (int k = 0; k < list1.size(); k++) {
3777 CompositeLocation locB1 = list1.get(k);
3778 CompositeLocation locB2 = list2.get(k);
3779 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
3781 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
3784 throw new Error("The method " + md1 + " is not consistent with the method " + md2
3785 + ".:: They have a different ordering relation between locations (" + locA1 + ","
3786 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
3794 private String getSymbol(int idx, FlowNode node) {
3795 Descriptor desc = node.getDescTuple().get(idx);
3796 return desc.getSymbol();
3799 private Descriptor getDescriptor(int idx, FlowNode node) {
3800 Descriptor desc = node.getDescTuple().get(idx);
3804 private void calculatePCLOC(MethodDescriptor md) {
3806 System.out.println("#CalculatePCLOC");
3807 MethodSummary methodSummary = getMethodSummary(md);
3808 FlowGraph fg = getFlowGraph(md);
3809 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
3811 // calculate the initial program counter location
3812 // PC location is higher than location types of parameters which has incoming flows.
3814 Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
3815 Set<Descriptor> paramDescNOTHavingInFlowSet = new HashSet<Descriptor>();
3816 // Set<FlowNode> paramNodeNOThavingInFlowSet = new HashSet<FlowNode>();
3818 int numParams = fg.getNumParameters();
3819 for (int i = 0; i < numParams; i++) {
3820 FlowNode paramFlowNode = fg.getParamFlowNode(i);
3821 Descriptor prefix = paramFlowNode.getDescTuple().get(0);
3822 NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
3823 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
3825 Set<FlowNode> inNodeToParamSet = fg.getIncomingNodeSetByPrefix(prefix);
3826 if (inNodeToParamSet.size() > 0) {
3827 // parameter has in-value flows
3829 for (Iterator iterator = inNodeToParamSet.iterator(); iterator.hasNext();) {
3830 FlowNode inNode = (FlowNode) iterator.next();
3831 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(inNode);
3832 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3833 FlowEdge flowEdge = (FlowEdge) iterator2.next();
3834 if (flowEdge.getEndTuple().startsWith(prefix)) {
3835 NTuple<Location> paramLocTupleWithIncomingFlow =
3836 translateToLocTuple(md, flowEdge.getEndTuple());
3837 paramLocTupleHavingInFlowSet.add(paramLocTupleWithIncomingFlow);
3842 // paramLocTupleHavingInFlowSet.add(paramLocTuple);
3844 // paramNodeNOThavingInFlowSet.add(fg.getFlowNode(paramDescTuple));
3845 paramDescNOTHavingInFlowSet.add(prefix);
3849 System.out.println("paramLocTupleHavingInFlowSet=" + paramLocTupleHavingInFlowSet);
3851 if (paramLocTupleHavingInFlowSet.size() > 0
3852 && !coversAllParamters(md, fg, paramLocTupleHavingInFlowSet)) {
3854 // Here, generates a location in the method lattice that is higher than the
3855 // paramLocTupleHavingInFlowSet
3856 NTuple<Location> pcLocTuple =
3857 generateLocTupleRelativeTo(md, paramLocTupleHavingInFlowSet, PCLOC);
3859 NTuple<Descriptor> pcDescTuple = translateToDescTuple(pcLocTuple);
3861 // System.out.println("pcLoc=" + pcLocTuple);
3863 CompositeLocation curPCLoc = methodSummary.getPCLoc();
3864 if (curPCLoc.get(0).isTop() || pcLocTuple.size() > curPCLoc.getSize()) {
3865 methodSummary.setPCLoc(new CompositeLocation(pcLocTuple));
3867 Set<FlowNode> flowNodeLowerthanPCLocSet = new HashSet<FlowNode>();
3868 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
3869 // add ordering relations s.t. PCLOC is higher than all flow nodes except the set of
3870 // parameters that do not have incoming flows
3871 for (Iterator iterator = fg.getNodeSet().iterator(); iterator.hasNext();) {
3872 FlowNode node = (FlowNode) iterator.next();
3874 if (!(node instanceof FlowReturnNode)) {
3875 if (!paramDescNOTHavingInFlowSet.contains(node.getCurrentDescTuple().get(0))) {
3876 flowNodeLowerthanPCLocSet.add(node);
3877 fg.addValueFlowEdge(pcDescTuple, node.getDescTuple());
3879 subGlobalFlowGraph.addValueFlowEdge(pcLocTuple,
3880 translateToLocTuple(md, node.getDescTuple()));
3883 System.out.println("***SKIP PCLOC -> RETURNLOC=" + node);
3887 fg.getFlowNode(translateToDescTuple(pcLocTuple)).setSkeleton(true);
3889 if (pcLocTuple.get(0).getLocDescriptor().equals(md.getThis())) {
3890 System.out.println("#########################################");
3891 for (Iterator iterator = flowNodeLowerthanPCLocSet.iterator(); iterator.hasNext();) {
3892 FlowNode lowerNode = (FlowNode) iterator.next();
3893 if (lowerNode.getDescTuple().size() == 1 && lowerNode.getCompositeLocation() == null) {
3894 NTuple<Location> lowerLocTuple = translateToLocTuple(md, lowerNode.getDescTuple());
3895 CompositeLocation newComp =
3896 calculateCompositeLocationFromSubGlobalGraph(md, lowerNode);
3897 if (newComp != null) {
3898 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(lowerLocTuple.get(0),
3900 lowerNode.setCompositeLocation(newComp);
3901 System.out.println("NEW COMP LOC=" + newComp + " to lowerNode=" + lowerNode);
3914 private int countFirstDescriptorSetSize(Set<NTuple<Location>> set) {
3916 Set<Descriptor> descSet = new HashSet<Descriptor>();
3918 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
3919 NTuple<Location> locTuple = (NTuple<Location>) iterator.next();
3920 descSet.add(locTuple.get(0).getLocDescriptor());
3923 return descSet.size();
3926 private boolean coversAllParamters(MethodDescriptor md, FlowGraph fg,
3927 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
3929 int numParam = fg.getNumParameters();
3930 // int size = paramLocTupleHavingInFlowSet.size();
3931 int size = countFirstDescriptorSetSize(paramLocTupleHavingInFlowSet);
3933 System.out.println("numParam=" + numParam + " size=" + size);
3935 // if (!md.isStatic()) {
3937 // // if the method is not static && there is a parameter composite location &&
3938 // // it is started with 'this',
3939 // // paramLocTupleHavingInFlowSet need to have 'this' parameter.
3941 // FlowNode thisParamNode = fg.getParamFlowNode(0);
3942 // NTuple<Location> thisParamLocTuple =
3943 // translateToLocTuple(md, thisParamNode.getCurrentDescTuple());
3945 // if (!paramLocTupleHavingInFlowSet.contains(thisParamLocTuple)) {
3947 // for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
3948 // NTuple<Location> paramTuple = (NTuple<Location>) iterator.next();
3949 // if (paramTuple.size() > 1 && paramTuple.get(0).getLocDescriptor().equals(md.getThis())) {
3950 // // paramLocTupleHavingInFlowSet.add(thisParamLocTuple);
3959 if (size == numParam) {
3967 private void calculateRETURNLOC(MethodDescriptor md) {
3969 System.out.println("#calculateRETURNLOC= " + md);
3971 // calculate a return location:
3972 // the return location type is lower than all parameters and the location of return values
3973 MethodSummary methodSummary = getMethodSummary(md);
3974 // if (methodSummary.getRETURNLoc() != null) {
3975 // System.out.println("$HERE?");
3979 FlowGraph fg = getFlowGraph(md);
3980 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
3981 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
3983 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
3984 // first, generate the set of return value location types that starts
3985 // with 'this' reference
3987 Set<FlowNode> paramFlowNodeFlowingToReturnValueSet = getParamNodeFlowingToReturnValue(md);
3988 // System.out.println("paramFlowNodeFlowingToReturnValueSet="
3989 // + paramFlowNodeFlowingToReturnValueSet);
3991 Set<NTuple<Location>> tupleToBeHigherThanReturnLocSet = new HashSet<NTuple<Location>>();
3992 for (Iterator iterator = paramFlowNodeFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
3993 FlowNode fn = (FlowNode) iterator.next();
3994 NTuple<Descriptor> paramDescTuple = fn.getCurrentDescTuple();
3995 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, paramDescTuple));
3998 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
3999 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
4000 FlowNode returnNode = (FlowNode) iterator.next();
4001 NTuple<Descriptor> returnDescTuple = returnNode.getCurrentDescTuple();
4002 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, returnDescTuple));
4004 System.out.println("-flow graph's returnNodeSet=" + returnNodeSet);
4005 System.out.println("tupleSetToBeHigherThanReturnLoc=" + tupleToBeHigherThanReturnLocSet);
4007 // Here, generates a return location in the method lattice that is lower than the
4008 // locFlowingToReturnValueSet
4009 NTuple<Location> returnLocTuple =
4010 generateLocTupleRelativeTo(md, tupleToBeHigherThanReturnLocSet, RLOC);
4012 // System.out.println("returnLocTuple=" + returnLocTuple);
4013 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnLocTuple);
4014 CompositeLocation curReturnLoc = methodSummary.getRETURNLoc();
4015 if (curReturnLoc == null || returnDescTuple.size() > curReturnLoc.getSize()) {
4016 methodSummary.setRETURNLoc(new CompositeLocation(returnLocTuple));
4018 for (Iterator iterator = tupleToBeHigherThanReturnLocSet.iterator(); iterator.hasNext();) {
4019 NTuple<Location> higherTuple = (NTuple<Location>) iterator.next();
4020 fg.addValueFlowEdge(translateToDescTuple(higherTuple), returnDescTuple);
4022 fg.getFlowNode(returnDescTuple).setSkeleton(true);
4026 // makes sure that PCLOC is higher than RETURNLOC
4027 CompositeLocation pcLoc = methodSummary.getPCLoc();
4028 if (!pcLoc.get(0).isTop()) {
4029 NTuple<Descriptor> pcLocDescTuple = translateToDescTuple(pcLoc.getTuple());
4030 fg.addValueFlowEdge(pcLocDescTuple, returnDescTuple);
4037 private void calculateExtraLocations(MethodDescriptor md) {
4038 // calcualte pcloc, returnloc,...
4040 System.out.println("\nSSJAVA:Calculate PCLOC/RETURNLOC locations: " + md);
4043 calculateRETURNLOC(md);
4047 private NTuple<Location> generateLocTupleRelativeTo(MethodDescriptor md,
4048 Set<NTuple<Location>> paramLocTupleHavingInFlowSet, String locNamePrefix) {
4050 // System.out.println("-generateLocTupleRelativeTo=" + paramLocTupleHavingInFlowSet);
4052 NTuple<Location> higherLocTuple = new NTuple<Location>();
4054 VarDescriptor thisVarDesc = md.getThis();
4055 // check if all paramter loc tuple is started with 'this' reference
4056 boolean hasParamNotStartedWithThisRef = false;
4060 Set<NTuple<Location>> paramLocTupleStartedWithThis = new HashSet<NTuple<Location>>();
4062 next: for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
4063 NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
4064 Descriptor paramLocalDesc = paramLocTuple.get(0).getLocDescriptor();
4065 if (!paramLocalDesc.equals(thisVarDesc)) {
4067 Set<FlowNode> inNodeSet = getFlowGraph(md).getIncomingNodeSetByPrefix(paramLocalDesc);
4068 for (Iterator iterator2 = inNodeSet.iterator(); iterator2.hasNext();) {
4069 FlowNode flowNode = (FlowNode) iterator2.next();
4070 if (flowNode.getDescTuple().startsWith(thisVarDesc)) {
4071 // System.out.println("paramLocTuple=" + paramLocTuple + " is lower than THIS");
4075 hasParamNotStartedWithThisRef = true;
4077 } else if (paramLocTuple.size() > 1) {
4078 paramLocTupleStartedWithThis.add(paramLocTuple);
4079 if (minSize == 0 || minSize > paramLocTuple.size()) {
4080 minSize = paramLocTuple.size();
4085 // System.out.println("---paramLocTupleStartedWithThis=" + paramLocTupleStartedWithThis);
4086 Descriptor enclosingDesc = md;
4087 if (hasParamNotStartedWithThisRef) {
4088 // in this case, PCLOC will be the local location
4090 // all parameter is started with 'this', so PCLOC will be set relative to the composite
4091 // location started with 'this'.
4092 // for (int idx = 0; idx < minSize - 1; idx++) {
4093 for (int idx = 0; idx < 1; idx++) {
4094 Set<Descriptor> locDescSet = new HashSet<Descriptor>();
4095 Location curLoc = null;
4096 NTuple<Location> paramLocTuple = null;
4097 for (Iterator iterator = paramLocTupleStartedWithThis.iterator(); iterator.hasNext();) {
4098 paramLocTuple = (NTuple<Location>) iterator.next();
4099 // System.out.println("-----paramLocTuple=" + paramLocTuple + " idx=" + idx);
4100 curLoc = paramLocTuple.get(idx);
4101 Descriptor locDesc = curLoc.getLocDescriptor();
4102 locDescSet.add(locDesc);
4104 // System.out.println("-----locDescSet=" + locDescSet + " idx=" + idx);
4105 if (locDescSet.size() != 1) {
4108 Location newLocElement = new Location(curLoc.getDescriptor(), curLoc.getLocDescriptor());
4109 System.out.println("newLocElement" + newLocElement);
4110 higherLocTuple.add(newLocElement);
4111 enclosingDesc = getClassTypeDescriptor(curLoc.getLocDescriptor());
4116 String locIdentifier = locNamePrefix + (locSeed++);
4117 NameDescriptor locDesc = new NameDescriptor(locIdentifier);
4118 Location newLoc = new Location(enclosingDesc, locDesc);
4119 higherLocTuple.add(newLoc);
4120 System.out.println("---new loc tuple=" + higherLocTuple);
4122 return higherLocTuple;
4126 public ClassDescriptor getClassTypeDescriptor(Descriptor in) {
4128 if (in instanceof VarDescriptor) {
4129 return ((VarDescriptor) in).getType().getClassDesc();
4130 } else if (in instanceof FieldDescriptor) {
4131 return ((FieldDescriptor) in).getType().getClassDesc();
4133 // else if (in instanceof LocationDescriptor) {
4134 // // here is the case that the descriptor 'in' is the last element of the assigned composite
4136 // return ((VarDescriptor) locTuple.get(0).getLocDescriptor()).getType().getClassDesc();
4144 private Set<NTuple<Location>> calculateHighestLocTupleSet(
4145 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
4147 Set<NTuple<Location>> highestSet = new HashSet<NTuple<Location>>();
4149 Iterator<NTuple<Location>> iterator = paramLocTupleHavingInFlowSet.iterator();
4150 NTuple<Location> highest = iterator.next();
4152 for (; iterator.hasNext();) {
4153 NTuple<Location> curLocTuple = (NTuple<Location>) iterator.next();
4154 if (isHigherThan(curLocTuple, highest)) {
4155 // System.out.println(curLocTuple + " is greater than " + highest);
4156 highest = curLocTuple;
4160 highestSet.add(highest);
4162 MethodDescriptor md = (MethodDescriptor) highest.get(0).getDescriptor();
4163 VarDescriptor thisVarDesc = md.getThis();
4165 // System.out.println("highest=" + highest);
4167 for (Iterator<NTuple<Location>> iter = paramLocTupleHavingInFlowSet.iterator(); iter.hasNext();) {
4168 NTuple<Location> curLocTuple = iter.next();
4170 if (!curLocTuple.equals(highest) && !hasOrderingRelation(highest, curLocTuple)) {
4172 // System.out.println("add it to the highest set=" + curLocTuple);
4173 highestSet.add(curLocTuple);
4182 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
4183 Set<String> higherLocSet = new HashSet<String>();
4185 Set<String> locSet = lattice.getTable().keySet();
4186 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
4187 String element = (String) iterator.next();
4188 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
4189 higherLocSet.add(element);
4192 return higherLocSet;
4195 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
4196 Set<CompositeLocation> set) {
4198 CompositeLocation lowest = set.iterator().next();
4200 if (set.size() == 1) {
4204 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
4205 CompositeLocation loc = (CompositeLocation) iterator.next();
4207 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
4208 // if there is a case where composite locations are incomparable, just
4213 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
4220 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
4221 CompositeLocation comp2) {
4223 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
4225 for (int idx = 0; idx < size; idx++) {
4226 Location loc1 = comp1.get(idx);
4227 Location loc2 = comp2.get(idx);
4229 Descriptor desc1 = loc1.getDescriptor();
4230 Descriptor desc2 = loc2.getDescriptor();
4232 if (!desc1.equals(desc2)) {
4233 throw new Error("Fail to compare " + comp1 + " and " + comp2);
4236 String symbol1 = loc1.getLocIdentifier();
4237 String symbol2 = loc2.getLocIdentifier();
4239 SSJavaLattice<String> lattice;
4241 lattice = methodLattice;
4243 lattice = getLattice(desc1);
4246 if (symbol1.equals(symbol2)) {
4248 } else if (!lattice.isComparable(symbol1, symbol2)) {
4257 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
4258 CompositeLocation comp2) {
4260 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
4262 for (int idx = 0; idx < size; idx++) {
4263 Location loc1 = comp1.get(idx);
4264 Location loc2 = comp2.get(idx);
4266 Descriptor desc1 = loc1.getDescriptor();
4267 Descriptor desc2 = loc2.getDescriptor();
4269 if (!desc1.equals(desc2)) {
4270 throw new Error("Fail to compare " + comp1 + " and " + comp2);
4273 String symbol1 = loc1.getLocIdentifier();
4274 String symbol2 = loc2.getLocIdentifier();
4276 SSJavaLattice<String> lattice;
4278 lattice = methodLattice;
4280 lattice = getLattice(desc1);
4283 if (symbol1.equals(symbol2)) {
4285 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
4296 private GlobalFlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
4298 if (!mapMethodDescriptorToSubGlobalFlowGraph.containsKey(md)) {
4299 mapMethodDescriptorToSubGlobalFlowGraph.put(md, new GlobalFlowGraph(md));
4301 return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
4304 private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
4305 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
4307 System.out.println("-propagateFlowsToCallerWithNoCompositeLocation=" + min.printNode(0));
4308 // if the parameter A reaches to the parameter B
4309 // then, add an edge the argument A -> the argument B to the caller's flow
4312 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
4313 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
4314 int numParam = calleeFlowGraph.getNumParameters();
4316 for (int i = 0; i < numParam; i++) {
4317 for (int k = 0; k < numParam; k++) {
4321 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
4322 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
4324 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
4325 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
4327 // check if the callee propagates an ordering constraints through
4330 // Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
4331 Set<FlowNode> localReachSet =
4332 calleeFlowGraph.getReachableSetFrom(paramNode1.getDescTuple());
4334 NTuple<Descriptor> paramDescTuple1 = paramNode1.getCurrentDescTuple();
4335 NTuple<Descriptor> paramDescTuple2 = paramNode2.getCurrentDescTuple();
4337 // System.out.println("-param1CurTuple=" + paramDescTuple1 + " param2CurTuple="
4338 // + paramDescTuple2);
4339 // System.out.println("-- localReachSet from param1=" + localReachSet);
4341 if (paramDescTuple1.get(0).equals(paramDescTuple2.get(0))) {
4342 // if two parameters share the same prefix
4343 // it already has been assigned to a composite location
4344 // so we don't need to add an additional ordering relation caused by these two
4349 if (arg1Tuple.size() > 0 && arg2Tuple.size() > 0
4350 && containsPrefix(paramNode2.getDescTuple().get(0), localReachSet)) {
4351 // need to propagate an ordering relation s.t. arg1 is higher
4353 // System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
4355 // add a new flow between the corresponding arguments.
4356 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
4357 // System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
4360 // .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
4364 // System.out.println();
4369 // if a parameter has a composite location and the first element of the parameter location
4370 // matches the callee's 'this'
4371 // we have a more specific constraint: the caller's corresponding argument is higher than the
4372 // parameter location which is translated into the caller
4374 for (int idx = 0; idx < numParam; idx++) {
4375 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
4376 CompositeLocation compLoc = paramNode.getCompositeLocation();
4377 System.out.println("paramNode=" + paramNode + " compLoc=" + compLoc);
4378 if (compLoc != null && compLoc.get(0).getLocDescriptor().equals(min.getMethod().getThis())) {
4379 System.out.println("$$$COMPLOC CASE=" + compLoc + " idx=" + idx);
4381 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
4382 System.out.println("--- argTuple=" + argTuple + " current compLoc="
4383 + callerFlowGraph.getFlowNode(argTuple).getCompositeLocation());
4385 NTuple<Descriptor> translatedParamTuple =
4386 translateCompositeLocationToCaller(idx, min, compLoc);
4387 System.out.println("add a flow edge= " + argTuple + "->" + translatedParamTuple);
4388 callerFlowGraph.addValueFlowEdge(argTuple, translatedParamTuple);
4390 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
4391 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
4392 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
4393 callerFlowGraph.addValueFlowEdge(translateToDescTuple(pcLocTuple), translatedParamTuple);
4401 private boolean containsPrefix(Descriptor prefixDesc, Set<FlowNode> set) {
4403 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
4404 FlowNode flowNode = (FlowNode) iterator.next();
4405 if (flowNode.getDescTuple().startsWith(prefixDesc)) {
4406 System.out.println("FOUND=" + flowNode);
4413 private NTuple<Descriptor> translateCompositeLocationToCaller(int idx, MethodInvokeNode min,
4414 CompositeLocation compLocForParam1) {
4416 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
4418 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
4419 for (int i = 0; i < baseTuple.size(); i++) {
4420 tuple.add(baseTuple.get(i));
4423 for (int i = 1; i < compLocForParam1.getSize(); i++) {
4424 Location loc = compLocForParam1.get(i);
4425 tuple.add(loc.getLocDescriptor());
4431 private CompositeLocation generateCompositeLocation(NTuple<Location> prefixLocTuple) {
4433 System.out.println("generateCompositeLocation=" + prefixLocTuple);
4435 CompositeLocation newCompLoc = new CompositeLocation();
4436 for (int i = 0; i < prefixLocTuple.size(); i++) {
4437 newCompLoc.addLocation(prefixLocTuple.get(i));
4440 Descriptor lastDescOfPrefix = prefixLocTuple.get(prefixLocTuple.size() - 1).getLocDescriptor();
4442 ClassDescriptor enclosingDescriptor;
4443 if (lastDescOfPrefix instanceof FieldDescriptor) {
4444 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
4445 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
4446 } else if (lastDescOfPrefix.equals(GLOBALDESC)) {
4447 MethodDescriptor currentMethodDesc = (MethodDescriptor) prefixLocTuple.get(0).getDescriptor();
4448 enclosingDescriptor = currentMethodDesc.getClassDesc();
4450 // var descriptor case
4451 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
4453 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
4455 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
4456 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
4458 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
4459 newLoc.setLocDescriptor(newLocDescriptor);
4460 newCompLoc.addLocation(newLoc);
4462 // System.out.println("--newCompLoc=" + newCompLoc);
4466 private CompositeLocation generateCompositeLocation(MethodDescriptor md,
4467 NTuple<Descriptor> paramPrefix) {
4469 System.out.println("generateCompositeLocation=" + paramPrefix);
4471 CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
4473 Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
4474 // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
4475 // + lastDescOfPrefix.getClass());
4476 ClassDescriptor enclosingDescriptor;
4477 if (lastDescOfPrefix instanceof FieldDescriptor) {
4478 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
4479 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
4481 // var descriptor case
4482 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
4484 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
4486 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
4487 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
4489 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
4490 newLoc.setLocDescriptor(newLocDescriptor);
4491 newCompLoc.addLocation(newLoc);
4493 // System.out.println("--newCompLoc=" + newCompLoc);
4497 private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
4498 MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
4500 List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
4502 for (int i = 0; i < callerPrefixList.size(); i++) {
4503 NTuple<Descriptor> prefix = callerPrefixList.get(i);
4504 if (prefix.startsWith(baseRef)) {
4505 NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
4506 calleePrefix.add(mdCallee.getThis());
4507 for (int k = 1; k < prefix.size(); k++) {
4508 calleePrefix.add(prefix.get(k));
4510 calleePrefixList.add(calleePrefix);
4514 return calleePrefixList;
4518 public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
4520 CompositeLocation compLoc = new CompositeLocation();
4522 Descriptor enclosingDescriptor = md;
4524 for (int i = 0; i < tuple.size(); i++) {
4525 Descriptor curDescriptor = tuple.get(i);
4526 Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
4527 locElement.setLocDescriptor(curDescriptor);
4528 compLoc.addLocation(locElement);
4530 if (curDescriptor instanceof VarDescriptor) {
4531 enclosingDescriptor = md.getClassDesc();
4532 } else if (curDescriptor instanceof FieldDescriptor) {
4533 enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
4534 } else if (curDescriptor instanceof NameDescriptor) {
4535 // it is "GLOBAL LOC" case!
4536 enclosingDescriptor = GLOBALDESC;
4537 } else if (curDescriptor instanceof InterDescriptor) {
4538 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(curDescriptor);
4540 enclosingDescriptor = null;
4548 private LocationDescriptor generateNewLocationDescriptor() {
4549 return new LocationDescriptor("Loc" + (locSeed++));
4552 private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
4554 // return the index where the prefix shared by tuple1 and tuple2 is ended
4555 // if there is no prefix shared by both of them, return -1
4557 int minSize = tuple1.size();
4558 if (minSize > tuple2.size()) {
4559 minSize = tuple2.size();
4563 for (int i = 0; i < minSize; i++) {
4564 if (!tuple1.get(i).equals(tuple2.get(i))) {
4574 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
4575 NTuple<Location> tuple) {
4577 // first, retrieve inferred location by the local var descriptor
4578 CompositeLocation inferLoc = new CompositeLocation();
4580 CompositeLocation localVarInferLoc =
4581 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
4583 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
4585 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
4586 inferLoc.addLocation(localVarInferLoc.get(i));
4589 for (int i = 1; i < tuple.size(); i++) {
4590 Location cur = tuple.get(i);
4591 Descriptor enclosingDesc = cur.getDescriptor();
4592 Descriptor curDesc = cur.getLocDescriptor();
4594 Location inferLocElement;
4595 if (curDesc == null) {
4596 // in this case, we have a newly generated location.
4597 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
4599 String fieldLocSymbol =
4600 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
4601 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
4602 inferLocElement.setLocDescriptor(curDesc);
4605 inferLoc.addLocation(inferLocElement);
4609 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
4613 public LocationInfo getLocationInfo(Descriptor d) {
4614 if (d instanceof MethodDescriptor) {
4615 return getMethodLocationInfo((MethodDescriptor) d);
4617 return getFieldLocationInfo((ClassDescriptor) d);
4621 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
4623 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
4624 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
4627 return mapMethodDescToMethodLocationInfo.get(md);
4631 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
4633 if (!mapClassToLocationInfo.containsKey(cd)) {
4634 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
4637 return mapClassToLocationInfo.get(cd);
4641 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
4642 NTuple<Location> prefix, NTuple<Location> element) {
4644 if (!map.containsKey(prefix)) {
4645 map.put(prefix, new HashSet<NTuple<Location>>());
4647 map.get(prefix).add(element);
4650 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
4651 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
4652 Descriptor desc = (Descriptor) iterator.next();
4654 if (desc.equals(LocationInference.GLOBALDESC)) {
4656 } else if (desc instanceof VarDescriptor) {
4657 if (!((VarDescriptor) desc).getType().isPrimitive()) {
4660 } else if (desc instanceof FieldDescriptor) {
4661 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
4670 public SSJavaLattice<String> getLattice(Descriptor d) {
4671 if (d instanceof MethodDescriptor) {
4672 return getMethodLattice((MethodDescriptor) d);
4674 return getFieldLattice((ClassDescriptor) d);
4678 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
4679 if (!md2lattice.containsKey(md)) {
4680 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
4682 return md2lattice.get(md);
4685 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
4686 md2lattice.put(md, lattice);
4689 private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
4692 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
4693 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
4695 if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
4696 && dstCurTuple.size() > (idx + 1)) {
4697 // value flow between fields: we don't need to add a binary relation
4700 Descriptor desc = srcCurTuple.get(idx);
4701 ClassDescriptor classDesc;
4704 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
4706 if (desc instanceof FieldDescriptor) {
4707 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
4709 // this case is that the local variable has a composite location assignment
4710 // the following element after the composite location to the local variable
4711 // has the enclosing descriptor of the local variable
4712 Descriptor localDesc = srcNode.getDescTuple().get(0);
4713 classDesc = ((VarDescriptor) localDesc).getType().getClassDesc();
4716 extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
4720 Descriptor srcFieldDesc = srcCurTuple.get(idx);
4721 Descriptor dstFieldDesc = dstCurTuple.get(idx);
4723 System.out.println("srcFieldDesc=" + srcFieldDesc + " dstFieldDesc=" + dstFieldDesc
4725 if (!srcFieldDesc.equals(dstFieldDesc)) {
4727 System.out.println("-ADD EDGE");
4728 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
4729 } else if (!isReference(srcFieldDesc) && !isReference(dstFieldDesc)) {
4730 System.out.println("-ADD EDGE");
4731 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
4738 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
4739 if (!cd2lattice.containsKey(cd)) {
4740 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
4742 return cd2lattice.get(cd);
4745 public LinkedList<MethodDescriptor> computeMethodList() {
4747 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
4751 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
4752 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
4754 while (!toAnalyzeIsEmpty()) {
4755 ClassDescriptor cd = toAnalyzeNext();
4757 if (cd.getClassName().equals("Object")) {
4758 rootClassDescriptor = cd;
4759 // inheritanceTree = new InheritanceTree<ClassDescriptor>(cd);
4762 setupToAnalazeMethod(cd);
4763 temp_toanalyzeMethodList.removeAll(visited);
4765 while (!toAnalyzeMethodIsEmpty()) {
4766 MethodDescriptor md = toAnalyzeMethodNext();
4767 if ((!visited.contains(md))
4768 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
4770 // creates a mapping from a method descriptor to virtual methods
4771 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
4772 if (md.isStatic()) {
4773 setPossibleCallees.add(md);
4775 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
4778 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
4779 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
4781 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
4782 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
4783 if ((!ssjava.isTrustMethod(calleemd))
4784 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))
4785 && (!calleemd.getModifiers().isNative())) {
4786 if (!visited.contains(calleemd)) {
4787 temp_toanalyzeMethodList.add(calleemd);
4789 reachableCallee.add(calleemd);
4790 needToAnalyzeCalleeSet.add(calleemd);
4794 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
4803 return ssjava.topologicalSort(toSort);
4807 public boolean isTransitivelyCalledFrom(MethodDescriptor callee, MethodDescriptor caller) {
4808 // if the callee is transitively invoked from the caller
4811 int callerIdx = toanalyze_methodDescList.indexOf(caller);
4812 int calleeIdx = toanalyze_methodDescList.indexOf(callee);
4814 if (callerIdx < calleeIdx) {
4822 public void constructFlowGraph() {
4824 System.out.println("");
4825 toanalyze_methodDescList = computeMethodList();
4827 // hack... it seems that there is a problem with topological sorting.
4828 // so String.toString(Object o) is appeared too higher in the call chain.
4829 MethodDescriptor mdToString = null;
4830 for (Iterator iterator = toanalyze_methodDescList.iterator(); iterator.hasNext();) {
4831 MethodDescriptor md = (MethodDescriptor) iterator.next();
4832 if (md.toString().equals("public static String String.valueOf(Object o)")) {
4837 if (mdToString != null) {
4838 toanalyze_methodDescList.remove(mdToString);
4839 toanalyze_methodDescList.addLast(mdToString);
4842 LinkedList<MethodDescriptor> methodDescList =
4843 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
4845 System.out.println("@@@methodDescList=" + methodDescList);
4848 while (!methodDescList.isEmpty()) {
4849 MethodDescriptor md = methodDescList.removeLast();
4850 if (state.SSJAVADEBUG) {
4851 System.out.println();
4852 System.out.println("SSJAVA: Constructing a flow graph: " + md);
4854 // creates a mapping from a parameter descriptor to its index
4855 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
4857 if (!md.isStatic()) {
4859 mapParamDescToIdx.put(md.getThis(), 0);
4862 for (int i = 0; i < md.numParameters(); i++) {
4863 Descriptor paramDesc = (Descriptor) md.getParameter(i);
4864 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
4867 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
4868 mapMethodDescriptorToFlowGraph.put(md, fg);
4870 analyzeMethodBody(md.getClassDesc(), md);
4872 // System.out.println("##constructSubGlobalFlowGraph");
4873 // GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(fg);
4874 // mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
4877 // System.out.println("##addValueFlowsFromCalleeSubGlobalFlowGraph");
4878 // addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
4879 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
4881 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
4884 // _debug_printGraph();
4888 private void constructGlobalFlowGraph() {
4889 LinkedList<MethodDescriptor> methodDescList =
4890 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
4892 while (!methodDescList.isEmpty()) {
4893 MethodDescriptor md = methodDescList.removeLast();
4894 if (state.SSJAVADEBUG) {
4895 System.out.println();
4896 System.out.println("SSJAVA: Constructing a sub global flow graph: " + md);
4898 constructSubGlobalFlowGraph(getFlowGraph(md));
4901 System.out.println("-add Value Flows From CalleeSubGlobalFlowGraph");
4902 addValueFlowsFromCalleeSubGlobalFlowGraph(md);
4903 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
4905 // System.out.println("-propagate Flows From Callees With No CompositeLocation");
4906 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
4908 // mark if a parameter has incoming flows
4909 checkParamNodesInSubGlobalFlowGraph(md);
4915 private void checkParamNodesInSubGlobalFlowGraph(MethodDescriptor md) {
4916 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4917 FlowGraph flowGraph = getFlowGraph(md);
4919 Set<FlowNode> paramFlowNodeSet = flowGraph.getParamFlowNodeSet();
4920 for (Iterator iterator = paramFlowNodeSet.iterator(); iterator.hasNext();) {
4921 FlowNode paramFlowNode = (FlowNode) iterator.next();
4922 System.out.println("paramFlowNode=" + paramFlowNode);
4923 NTuple<Descriptor> paramDescTuple = paramFlowNode.getDescTuple();
4924 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
4925 GlobalFlowNode paramGlobalNode = globalFlowGraph.getFlowNode(paramLocTuple);
4927 Set<GlobalFlowNode> incomingNodeSet =
4928 globalFlowGraph.getIncomingNodeSetByPrefix(paramLocTuple.get(0));
4930 if (incomingNodeSet.size() > 0) {
4931 paramGlobalNode.setParamNodeWithIncomingFlows(true);
4937 private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
4938 if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
4939 mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
4941 return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
4944 private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
4946 // the transformation for a call site propagates flows through parameters
4947 // if the method is virtual, it also grab all relations from any possible
4950 Set<MethodInvokeNode> setMethodInvokeNode =
4951 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
4953 if (setMethodInvokeNode != null) {
4955 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
4956 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
4957 MethodDescriptor mdCallee = min.getMethod();
4958 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
4959 if (mdCallee.isStatic()) {
4960 setPossibleCallees.add(mdCallee);
4962 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
4963 // removes method descriptors that are not invoked by the caller
4964 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
4965 setPossibleCallees.addAll(calleeSet);
4968 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
4969 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
4970 propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
4978 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
4979 BlockNode bn = state.getMethodBody(md);
4980 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
4981 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
4984 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
4985 NodeTupleSet implicitFlowTupleSet) {
4987 bn.getVarTable().setParent(nametable);
4988 for (int i = 0; i < bn.size(); i++) {
4989 BlockStatementNode bsn = bn.get(i);
4990 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
4995 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
4996 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
4998 switch (bsn.kind()) {
4999 case Kind.BlockExpressionNode:
5000 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
5003 case Kind.DeclarationNode:
5004 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
5007 case Kind.IfStatementNode:
5008 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
5012 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
5015 case Kind.ReturnNode:
5016 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
5019 case Kind.SubBlockNode:
5020 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
5023 case Kind.ContinueBreakNode:
5026 case Kind.SwitchStatementNode:
5027 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
5034 private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
5035 SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
5037 analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
5041 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
5042 SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
5044 NodeTupleSet condTupleNode = new NodeTupleSet();
5045 analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
5046 implicitFlowTupleSet, false);
5048 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5050 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5051 newImplicitTupleSet.addTupleSet(condTupleNode);
5053 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5054 // need to create an intermediate node for the GLB of conditional
5055 // locations & implicit flows
5056 System.out.println("10");
5058 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5059 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5060 NTuple<Descriptor> tuple = idxIter.next();
5061 addFlowGraphEdge(md, tuple, interTuple);
5063 newImplicitTupleSet.clear();
5064 newImplicitTupleSet.addTuple(interTuple);
5067 BlockNode sbn = ssn.getSwitchBody();
5068 for (int i = 0; i < sbn.size(); i++) {
5069 analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
5074 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
5075 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
5076 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
5079 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
5080 NodeTupleSet implicitFlowTupleSet) {
5082 // System.out.println("-analyzeFlowReturnNode=" + rn.printNode(0));
5083 ExpressionNode returnExp = rn.getReturnExpression();
5085 if (returnExp != null) {
5086 NodeTupleSet nodeSet = new NodeTupleSet();
5087 // if a return expression returns a literal value, nodeSet is empty
5088 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
5089 FlowGraph fg = getFlowGraph(md);
5091 // if (implicitFlowTupleSet.size() == 1
5093 // fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate())
5096 // // since there is already an intermediate node for the GLB of implicit
5098 // // we don't need to create another intermediate node.
5099 // // just re-use the intermediate node for implicit flows.
5101 // FlowNode meetNode =
5102 // fg.getFlowNode(implicitFlowTupleSet.iterator().next());
5104 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
5105 // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>)
5107 // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
5112 NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
5114 // add tuples from return node
5115 currentFlowTupleSet.addTupleSet(nodeSet);
5117 // add tuples corresponding to the current implicit flows
5118 currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
5120 // System.out.println("---currentFlowTupleSet=" + currentFlowTupleSet);
5122 if (needToGenerateInterLoc(currentFlowTupleSet)) {
5123 System.out.println("9");
5125 FlowNode meetNode = fg.createIntermediateNode();
5126 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
5127 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
5128 fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
5130 fg.addReturnFlowNode(meetNode.getDescTuple());
5132 // currentFlowTupleSet = removeLiteralTuple(currentFlowTupleSet);
5133 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
5134 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
5135 fg.addReturnFlowNode(currentFlowTuple);
5143 private NodeTupleSet removeLiteralTuple(NodeTupleSet inSet) {
5144 NodeTupleSet tupleSet = new NodeTupleSet();
5145 for (Iterator<NTuple<Descriptor>> iter = inSet.iterator(); iter.hasNext();) {
5146 NTuple<Descriptor> tuple = iter.next();
5147 if (!tuple.get(0).equals(LITERALDESC)) {
5148 tupleSet.addTuple(tuple);
5154 private boolean needToGenerateInterLoc(NodeTupleSet tupleSet) {
5156 for (Iterator<NTuple<Descriptor>> iter = tupleSet.iterator(); iter.hasNext();) {
5157 NTuple<Descriptor> descTuple = iter.next();
5158 if (!descTuple.get(0).equals(LITERALDESC)) {
5163 System.out.println("needToGenerateInterLoc=" + tupleSet + " size=" + size);
5170 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
5171 NodeTupleSet implicitFlowTupleSet) {
5173 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
5175 NodeTupleSet condTupleNode = new NodeTupleSet();
5176 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
5177 implicitFlowTupleSet, false);
5179 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5181 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5182 newImplicitTupleSet.addTupleSet(condTupleNode);
5184 newImplicitTupleSet.addGlobalFlowTupleSet(implicitFlowTupleSet.getGlobalLocTupleSet());
5185 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
5187 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5188 // need to create an intermediate node for the GLB of conditional
5189 // locations & implicit flows
5190 System.out.println("6");
5192 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5193 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
5195 NTuple<Descriptor> tuple = idxIter.next();
5196 addFlowGraphEdge(md, tuple, interTuple);
5198 newImplicitTupleSet.clear();
5199 newImplicitTupleSet.addTuple(interTuple);
5204 // System.out.println("condTupleNode="+condTupleNode);
5205 // NTuple<Descriptor> interTuple =
5206 // getFlowGraph(md).createIntermediateNode().getDescTuple();
5208 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator();
5209 // idxIter.hasNext();) {
5210 // NTuple<Descriptor> tuple = idxIter.next();
5211 // addFlowGraphEdge(md, tuple, interTuple);
5214 // for (Iterator<NTuple<Descriptor>> idxIter =
5215 // implicitFlowTupleSet.iterator(); idxIter
5217 // NTuple<Descriptor> tuple = idxIter.next();
5218 // addFlowGraphEdge(md, tuple, interTuple);
5221 // NodeTupleSet newImplicitSet = new NodeTupleSet();
5222 // newImplicitSet.addTuple(interTuple);
5223 analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
5226 // condTupleNode.addTupleSet(implicitFlowTupleSet);
5228 // add edges from condNodeTupleSet to all nodes of conditional nodes
5229 // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
5232 // check 'for loop' case
5233 BlockNode bn = ln.getInitializer();
5234 bn.getVarTable().setParent(nametable);
5235 for (int i = 0; i < bn.size(); i++) {
5236 BlockStatementNode bsn = bn.get(i);
5237 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
5240 NodeTupleSet condTupleNode = new NodeTupleSet();
5241 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
5242 implicitFlowTupleSet, false);
5244 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5245 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5246 newImplicitTupleSet.addTupleSet(condTupleNode);
5248 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5249 // need to create an intermediate node for the GLB of conditional
5250 // locations & implicit flows
5251 System.out.println("7");
5253 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5254 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
5256 NTuple<Descriptor> tuple = idxIter.next();
5257 addFlowGraphEdge(md, tuple, interTuple);
5259 newImplicitTupleSet.clear();
5260 newImplicitTupleSet.addTuple(interTuple);
5265 // NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5267 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
5268 // NTuple<Descriptor> tuple = idxIter.next();
5269 // addFlowGraphEdge(md, tuple, interTuple);
5272 // for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
5274 // NTuple<Descriptor> tuple = idxIter.next();
5275 // addFlowGraphEdge(md, tuple, interTuple);
5278 // NodeTupleSet newImplicitSet = new NodeTupleSet();
5279 // newImplicitSet.addTuple(interTuple);
5280 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitTupleSet);
5281 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitTupleSet);
5284 // condTupleNode.addTupleSet(implicitFlowTupleSet);
5286 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
5288 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
5295 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
5296 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
5298 // System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
5300 NodeTupleSet condTupleNode = new NodeTupleSet();
5301 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
5302 implicitFlowTupleSet, false);
5304 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5306 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5307 newImplicitTupleSet.addTupleSet(condTupleNode);
5309 // System.out.println("$$$GGGcondTupleNode=" + condTupleNode.getGlobalLocTupleSet());
5310 // System.out.println("-condTupleNode=" + condTupleNode);
5311 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5312 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
5314 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5315 System.out.println("5");
5317 // need to create an intermediate node for the GLB of conditional locations & implicit flows
5318 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5319 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5320 NTuple<Descriptor> tuple = idxIter.next();
5321 addFlowGraphEdge(md, tuple, interTuple);
5323 newImplicitTupleSet.clear();
5324 newImplicitTupleSet.addTuple(interTuple);
5327 // GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5328 // for (Iterator<NTuple<Location>> iterator = condTupleNode.globalIterator();
5329 // iterator.hasNext();) {
5330 // NTuple<Location> calleeReturnLocTuple = iterator.next();
5331 // for (Iterator<NTuple<Descriptor>> iter2 = newImplicitTupleSet.iterator(); iter2.hasNext();) {
5332 // NTuple<Descriptor> callerImplicitTuple = iter2.next();
5333 // globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5334 // translateToLocTuple(md, callerImplicitTuple));
5337 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
5339 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
5341 if (isn.getFalseBlock() != null) {
5342 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
5347 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
5348 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
5350 VarDescriptor vd = dn.getVarDescriptor();
5351 mapDescToDefinitionLine.put(vd, dn.getNumLine());
5352 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
5354 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
5355 fn.setDeclarationNode();
5357 if (dn.getExpression() != null) {
5358 System.out.println("-analyzeFlowDeclarationNode=" + dn.printNode(0));
5360 NodeTupleSet nodeSetRHS = new NodeTupleSet();
5361 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
5362 implicitFlowTupleSet, false);
5364 // creates edges from RHS to LHS
5365 NTuple<Descriptor> interTuple = null;
5366 if (needToGenerateInterLoc(nodeSetRHS)) {
5367 System.out.println("3");
5368 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5371 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
5372 NTuple<Descriptor> fromTuple = iter.next();
5373 System.out.println("fromTuple=" + fromTuple + " interTuple=" + interTuple + " tupleLSH="
5375 addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
5378 // creates edges from implicitFlowTupleSet to LHS
5379 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
5380 NTuple<Descriptor> implicitTuple = iter.next();
5381 addFlowGraphEdge(md, implicitTuple, tupleLHS);
5384 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5385 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
5386 NTuple<Location> calleeReturnLocTuple = iterator.next();
5388 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, tupleLHS));
5391 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
5393 NTuple<Location> implicitGlobalTuple = iterator.next();
5395 globalFlowGraph.addValueFlowEdge(implicitGlobalTuple, translateToLocTuple(md, tupleLHS));
5398 System.out.println("-nodeSetRHS=" + nodeSetRHS);
5399 System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5405 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
5406 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
5407 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
5411 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
5412 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
5413 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
5416 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
5417 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
5418 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
5420 // System.out.println("en=" + en.printNode(0) + " class=" + en.getClass());
5422 // note that expression node can create more than one flow node
5423 // nodeSet contains of flow nodes
5424 // base is always assigned to null except the case of a name node!
5425 NTuple<Descriptor> flowTuple;
5426 switch (en.kind()) {
5427 case Kind.AssignmentNode:
5428 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
5429 implicitFlowTupleSet);
5432 case Kind.FieldAccessNode:
5434 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
5435 implicitFlowTupleSet, isLHS);
5436 if (flowTuple != null) {
5437 nodeSet.addTuple(flowTuple);
5442 NodeTupleSet nameNodeSet = new NodeTupleSet();
5444 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
5445 if (flowTuple != null) {
5446 nodeSet.addTuple(flowTuple);
5451 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
5454 case Kind.CreateObjectNode:
5455 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
5458 case Kind.ArrayAccessNode:
5459 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
5462 case Kind.LiteralNode:
5463 analyzeFlowLiteralNode(md, nametable, (LiteralNode) en, nodeSet);
5466 case Kind.MethodInvokeNode:
5467 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
5468 implicitFlowTupleSet);
5471 case Kind.TertiaryNode:
5472 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
5476 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
5478 // case Kind.InstanceOfNode:
5479 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
5482 // case Kind.ArrayInitializerNode:
5483 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
5487 // case Kind.ClassTypeNode:
5488 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
5491 // case Kind.OffsetNode:
5492 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
5501 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
5502 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
5504 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
5505 implicitFlowTupleSet, false);
5509 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
5510 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
5512 // System.out.println("analyzeFlowTertiaryNode=" + tn.printNode(0));
5514 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
5515 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
5516 implicitFlowTupleSet, false);
5518 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5519 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5520 newImplicitTupleSet.addTupleSet(tertiaryTupleNode);
5522 // System.out.println("$$$GGGcondTupleNode=" + tertiaryTupleNode.getGlobalLocTupleSet());
5523 // System.out.println("-tertiaryTupleNode=" + tertiaryTupleNode);
5524 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5525 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
5527 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5528 System.out.println("15");
5529 // need to create an intermediate node for the GLB of conditional locations & implicit flows
5530 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5531 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5532 NTuple<Descriptor> tuple = idxIter.next();
5533 addFlowGraphEdge(md, tuple, interTuple);
5535 newImplicitTupleSet.clear();
5536 newImplicitTupleSet.addTuple(interTuple);
5539 newImplicitTupleSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
5541 System.out.println("---------newImplicitTupleSet=" + newImplicitTupleSet);
5542 // add edges from tertiaryTupleNode to all nodes of conditional nodes
5543 // tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
5544 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
5545 newImplicitTupleSet, false);
5547 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
5548 newImplicitTupleSet, false);
5550 nodeSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
5551 nodeSet.addTupleSet(tertiaryTupleNode);
5553 System.out.println("#tertiary node set=" + nodeSet);
5556 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
5557 MethodInvokeNode min) {
5558 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
5560 set = new HashSet<MethodInvokeNode>();
5561 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
5566 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
5568 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
5569 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
5571 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
5574 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
5576 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
5577 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
5580 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
5583 private Set<NTuple<Location>> getPCLocTupleSet(MethodInvokeNode min) {
5584 if (!mapMethodInvokeNodeToPCLocTupleSet.containsKey(min)) {
5585 mapMethodInvokeNodeToPCLocTupleSet.put(min, new HashSet<NTuple<Location>>());
5587 return mapMethodInvokeNodeToPCLocTupleSet.get(min);
5590 private void analyzeFlowMethodInvokeNode(MethodDescriptor mdCaller, SymbolTable nametable,
5591 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
5593 System.out.println("analyzeFlowMethodInvokeNode=" + min.printNode(0));
5595 if (!toanalyze_methodDescList.contains(min.getMethod())) {
5599 addMapMethodDescToMethodInvokeNodeSet(min);
5601 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
5602 for (Iterator iterator = implicitFlowTupleSet.iterator(); iterator.hasNext();) {
5603 NTuple<Descriptor> pcDescTuple = (NTuple<Descriptor>) iterator.next();
5604 if (!pcDescTuple.get(0).equals(LITERALDESC)) {
5605 // here we don't need to add the literal value as a PC location
5606 pcLocTupleSet.add(translateToLocTuple(mdCaller, pcDescTuple));
5610 mapMethodInvokeNodeToArgIdxMap.put(min, new HashMap<Integer, NTuple<Descriptor>>());
5612 if (nodeSet == null) {
5613 nodeSet = new NodeTupleSet();
5616 MethodDescriptor mdCallee = min.getMethod();
5618 NameDescriptor baseName = min.getBaseName();
5619 boolean isSystemout = false;
5620 if (baseName != null) {
5621 isSystemout = baseName.getSymbol().equals("System.out");
5624 if (!ssjava.isSSJavaUtil(mdCallee.getClassDesc()) && !ssjava.isTrustMethod(mdCallee)
5627 addMapCallerMethodDescToMethodInvokeNodeSet(mdCaller, min);
5629 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
5630 System.out.println("mdCallee=" + mdCallee + " calleeFlowGraph=" + calleeFlowGraph);
5631 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
5633 System.out.println("---calleeReturnSet=" + calleeReturnSet);
5635 NodeTupleSet tupleSet = new NodeTupleSet();
5637 if (min.getExpression() != null) {
5639 NodeTupleSet baseNodeSet = new NodeTupleSet();
5640 analyzeFlowExpressionNode(mdCaller, nametable, min.getExpression(), baseNodeSet, null,
5641 implicitFlowTupleSet, false);
5642 System.out.println("baseNodeSet=" + baseNodeSet);
5644 assert (baseNodeSet.size() == 1);
5645 NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
5646 if (baseTuple.get(0) instanceof InterDescriptor) {
5647 if (baseTuple.size() > 1) {
5650 FlowNode interNode = getFlowGraph(mdCaller).getFlowNode(baseTuple);
5651 baseTuple = translateBaseTuple(interNode, baseTuple);
5653 mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
5655 if (!min.getMethod().isStatic()) {
5656 addArgIdxMap(min, 0, baseTuple);
5658 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
5659 FlowNode returnNode = (FlowNode) iterator.next();
5660 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
5661 if (returnDescTuple.startsWith(mdCallee.getThis())) {
5662 // the location type of the return value is started with 'this'
5664 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
5666 if (inFlowTuple.get(0) instanceof InterDescriptor) {
5667 // min.getExpression()
5672 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
5673 // nodeSet.addTuple(inFlowTuple);
5674 System.out.println("1CREATE A NEW TUPLE=" + inFlowTuple + " from="
5675 + mdCallee.getThis());
5676 // tupleSet.addTuple(inFlowTuple);
5677 tupleSet.addTuple(baseTuple);
5680 System.out.println("returnNode=" + returnNode);
5681 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
5682 // System.out.println("inFlowSet=" + inFlowSet + " from retrunNode=" + returnNode);
5683 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
5684 FlowNode inFlowNode = (FlowNode) iterator2.next();
5685 if (inFlowNode.getDescTuple().startsWith(mdCallee.getThis())) {
5686 // nodeSet.addTupleSet(baseNodeSet);
5687 System.out.println("2CREATE A NEW TUPLE=" + baseNodeSet + " from="
5688 + mdCallee.getThis());
5689 tupleSet.addTupleSet(baseNodeSet);
5697 // analyze parameter flows
5699 if (min.numArgs() > 0) {
5702 if (min.getMethod().isStatic()) {
5708 for (int i = 0; i < min.numArgs(); i++) {
5709 ExpressionNode en = min.getArg(i);
5710 int idx = i + offset;
5711 NodeTupleSet argTupleSet = new NodeTupleSet();
5712 analyzeFlowExpressionNode(mdCaller, nametable, en, argTupleSet, false);
5713 // if argument is liternal node, argTuple is set to NULL
5714 System.out.println("---arg idx=" + idx + " argTupleSet=" + argTupleSet);
5715 NTuple<Descriptor> argTuple = generateArgTuple(mdCaller, argTupleSet);
5717 // if an argument is literal value,
5718 // we need to create an itermediate node so that we could assign a composite location to
5719 // that node if needed
5720 if (argTuple.size() > 0
5721 && (argTuple.get(0).equals(GLOBALDESC) || argTuple.get(0).equals(LITERALDESC))) {
5723 * System.out.println("***GLOBAL ARG TUPLE CASE=" + argTuple); System.out.println("8");
5725 * NTuple<Descriptor> interTuple =
5726 * getFlowGraph(mdCaller).createIntermediateNode().getDescTuple(); ((InterDescriptor)
5727 * interTuple.get(0)).setMethodArgIdxPair(min, idx); addFlowGraphEdge(mdCaller,
5728 * argTuple, interTuple); argTuple = interTuple; addArgIdxMap(min, idx, argTuple);
5729 * System.out.println("new min mapping i=" + idx + " ->" + argTuple);
5731 argTuple = new NTuple<Descriptor>();
5734 addArgIdxMap(min, idx, argTuple);
5736 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
5738 // check whether a param node in the callee graph has incoming flows
5739 // if it has incoming flows, the corresponding arg should be lower than the current PC
5740 // Descriptor prefix = paramNode.getDescTuple().get(0);
5741 // if (calleeFlowGraph.getIncomingNodeSetByPrefix(prefix).size() > 0) {
5742 // for (Iterator<NTuple<Descriptor>> iterator = implicitFlowTupleSet.iterator(); iterator
5744 // NTuple<Descriptor> pcTuple = iterator.next();
5745 // System.out.println("add edge pcTuple =" + pcTuple + " -> " + argTuple);
5746 // addFlowGraphEdge(md, pcTuple, argTuple);
5750 System.out.println("paramNode=" + paramNode + " calleeReturnSet=" + calleeReturnSet);
5751 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
5752 || mdCallee.getModifiers().isNative()) {
5753 addParamNodeFlowingToReturnValue(mdCallee, paramNode);
5754 // nodeSet.addTupleSet(argTupleSet);
5755 System.out.println("3CREATE A NEW TUPLE=" + argTupleSet + " from=" + paramNode);
5756 tupleSet.addTupleSet(argTupleSet);
5762 if (mdCallee.getReturnType() != null && !mdCallee.getReturnType().isVoid()) {
5763 FlowReturnNode returnHolderNode = getFlowGraph(mdCaller).createReturnNode(min);
5765 if (needToGenerateInterLoc(tupleSet)) {
5766 System.out.println("20");
5767 FlowGraph fg = getFlowGraph(mdCaller);
5768 FlowNode interNode = fg.createIntermediateNode();
5769 interNode.setFormHolder(true);
5771 NTuple<Descriptor> interTuple = interNode.getDescTuple();
5773 for (Iterator iterator = tupleSet.iterator(); iterator.hasNext();) {
5774 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5776 Set<NTuple<Descriptor>> addSet = new HashSet<NTuple<Descriptor>>();
5777 FlowNode node = fg.getFlowNode(tuple);
5778 if (node instanceof FlowReturnNode) {
5779 addSet.addAll(fg.getReturnTupleSet(((FlowReturnNode) node).getReturnTupleSet()));
5783 for (Iterator iterator2 = addSet.iterator(); iterator2.hasNext();) {
5784 NTuple<Descriptor> higher = (NTuple<Descriptor>) iterator2.next();
5785 addFlowGraphEdge(mdCaller, higher, interTuple);
5789 returnHolderNode.addTuple(interTuple);
5791 nodeSet.addTuple(interTuple);
5792 System.out.println("ADD TUPLESET=" + interTuple + " to returnnode=" + returnHolderNode);
5795 returnHolderNode.addTupleSet(tupleSet);
5796 System.out.println("ADD TUPLESET=" + tupleSet + " to returnnode=" + returnHolderNode);
5798 // setNode.addTupleSet(tupleSet);
5799 // NodeTupleSet setFromReturnNode=new NodeTupleSet();
5800 // setFromReturnNode.addTuple(tuple);
5802 NodeTupleSet holderTupleSet =
5803 getNodeTupleSetFromReturnNode(getFlowGraph(mdCaller), returnHolderNode);
5805 System.out.println("HOLDER TUPLe SET=" + holderTupleSet);
5806 nodeSet.addTupleSet(holderTupleSet);
5808 nodeSet.addTuple(returnHolderNode.getDescTuple());
5811 // propagateFlowsFromCallee(min, md, min.getMethod());
5813 // when generating the global flow graph,
5814 // we need to add ordering relations from the set of callee return loc tuple to LHS of the
5815 // caller assignment
5816 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
5817 FlowNode calleeReturnNode = (FlowNode) iterator.next();
5818 NTuple<Location> calleeReturnLocTuple =
5819 translateToLocTuple(mdCallee, calleeReturnNode.getDescTuple());
5820 System.out.println("calleeReturnLocTuple=" + calleeReturnLocTuple);
5821 NTuple<Location> transaltedToCaller =
5822 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeReturnLocTuple);
5823 // System.out.println("translateToCallerLocTuple="
5824 // + translateToCallerLocTuple(min, mdCallee, mdCaller, calleeReturnLocTuple));
5825 if (transaltedToCaller.size() > 0) {
5826 nodeSet.addGlobalFlowTuple(translateToCallerLocTuple(min, mdCallee, mdCaller,
5827 calleeReturnLocTuple));
5831 System.out.println("min nodeSet=" + nodeSet);
5837 private NodeTupleSet getNodeTupleSetFromReturnNode(FlowGraph fg, FlowReturnNode node) {
5838 NodeTupleSet nts = new NodeTupleSet();
5840 Set<NTuple<Descriptor>> returnSet = node.getReturnTupleSet();
5842 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
5843 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5844 FlowNode flowNode = fg.getFlowNode(tuple);
5845 if (flowNode instanceof FlowReturnNode) {
5846 returnSet.addAll(recurGetNode(fg, (FlowReturnNode) flowNode));
5848 returnSet.add(tuple);
5852 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
5853 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator.next();
5854 nts.addTuple(nTuple);
5861 private Set<NTuple<Descriptor>> recurGetNode(FlowGraph fg, FlowReturnNode rnode) {
5863 Set<NTuple<Descriptor>> tupleSet = new HashSet<NTuple<Descriptor>>();
5865 Set<NTuple<Descriptor>> returnSet = rnode.getReturnTupleSet();
5866 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
5867 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5868 FlowNode flowNode = fg.getFlowNode(tuple);
5869 if (flowNode instanceof FlowReturnNode) {
5870 tupleSet.addAll(recurGetNode(fg, (FlowReturnNode) flowNode));
5872 tupleSet.add(tuple);
5878 private NTuple<Descriptor> generateArgTuple(MethodDescriptor mdCaller, NodeTupleSet argTupleSet) {
5882 // if argTupleSet is empty, it comes from the top location
5883 if (argTupleSet.size() == 0) {
5884 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
5885 descTuple.add(LITERALDESC);
5889 Set<NTuple<Descriptor>> argTupleSetNonLiteral = new HashSet<NTuple<Descriptor>>();
5891 for (Iterator<NTuple<Descriptor>> iter = argTupleSet.iterator(); iter.hasNext();) {
5892 NTuple<Descriptor> descTuple = iter.next();
5893 if (!descTuple.get(0).equals(LITERALDESC)) {
5894 argTupleSetNonLiteral.add(descTuple);
5898 if (argTupleSetNonLiteral.size() > 1) {
5899 System.out.println("11");
5901 NTuple<Descriptor> interTuple =
5902 getFlowGraph(mdCaller).createIntermediateNode().getDescTuple();
5903 for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
5904 NTuple<Descriptor> tuple = idxIter.next();
5905 addFlowGraphEdge(mdCaller, tuple, interTuple);
5908 } else if (argTupleSetNonLiteral.size() == 1) {
5909 return argTupleSetNonLiteral.iterator().next();
5911 return argTupleSet.iterator().next();
5916 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
5917 // return true if inNode has in-flows to nodeSet
5919 if (nodeSet.contains(inNode)) {
5920 // in this case, the method directly returns a parameter variable.
5923 // Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
5924 Set<FlowNode> reachableSet = fg.getReachableSetFrom(inNode.getDescTuple());
5925 // System.out.println("inNode=" + inNode + " reachalbeSet=" + reachableSet);
5927 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
5928 FlowNode fn = (FlowNode) iterator.next();
5929 if (nodeSet.contains(fn)) {
5936 private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
5937 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
5940 private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
5944 Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
5945 if (mapIdxToTuple == null) {
5946 mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
5947 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
5949 mapIdxToTuple.put(new Integer(idx), argTuple);
5952 private void analyzeFlowLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en,
5953 NodeTupleSet nodeSet) {
5954 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
5955 tuple.add(LITERALDESC);
5956 nodeSet.addTuple(tuple);
5959 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
5960 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
5962 System.out.println("analyzeFlowArrayAccessNode aan=" + aan.printNode(0));
5963 String currentArrayAccessNodeExpStr = aan.printNode(0);
5964 arrayAccessNodeStack.push(aan.printNode(0));
5966 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
5967 NTuple<Descriptor> base =
5968 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
5969 System.out.println("-base=" + base);
5971 nodeSet.setMethodInvokeBaseDescTuple(base);
5972 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
5973 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
5975 arrayAccessNodeStack.pop();
5978 // need to create an edge from idx to array
5979 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
5980 NTuple<Descriptor> idxTuple = idxIter.next();
5981 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
5982 NTuple<Descriptor> arrTuple = arrIter.next();
5983 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
5987 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5988 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
5990 NTuple<Location> calleeReturnLocTuple = iterator.next();
5991 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
5992 NTuple<Descriptor> arrTuple = arrIter.next();
5994 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, arrTuple));
5998 nodeSet.addTupleSet(expNodeTupleSet);
6001 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
6003 nodeSetArrayAccessExp.addTupleSet(expNodeTupleSet);
6004 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
6006 if (arrayAccessNodeStack.isEmpty()
6007 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
6009 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
6010 System.out.println("1");
6011 FlowNode interNode = getFlowGraph(md).createIntermediateNode();
6012 NTuple<Descriptor> interTuple = interNode.getDescTuple();
6014 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter.hasNext();) {
6015 NTuple<Descriptor> higherTuple = iter.next();
6016 addFlowGraphEdge(md, higherTuple, interTuple);
6018 nodeSetArrayAccessExp.clear();
6019 nodeSetArrayAccessExp.addTuple(interTuple);
6020 FlowGraph fg = getFlowGraph(md);
6022 System.out.println("base=" + base);
6024 fg.addMapInterLocNodeToEnclosingDescriptor(interTuple.get(0),
6025 getClassTypeDescriptor(base.get(base.size() - 1)));
6026 interNode.setBaseTuple(base);
6031 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
6032 nodeSet.addTupleSet(nodeSetArrayAccessExp);
6038 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
6039 CreateObjectNode en) {
6040 // TODO Auto-generated method stub
6044 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
6045 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
6047 NodeTupleSet leftOpSet = new NodeTupleSet();
6048 NodeTupleSet rightOpSet = new NodeTupleSet();
6050 System.out.println("analyzeFlowOpNode=" + on.printNode(0));
6053 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
6055 System.out.println("--leftOpSet=" + leftOpSet);
6057 if (on.getRight() != null) {
6059 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
6060 implicitFlowTupleSet, false);
6062 System.out.println("--rightOpSet=" + rightOpSet);
6064 Operation op = on.getOp();
6066 switch (op.getOp()) {
6068 case Operation.UNARYPLUS:
6069 case Operation.UNARYMINUS:
6070 case Operation.LOGIC_NOT:
6072 nodeSet.addTupleSet(leftOpSet);
6075 case Operation.LOGIC_OR:
6076 case Operation.LOGIC_AND:
6077 case Operation.COMP:
6078 case Operation.BIT_OR:
6079 case Operation.BIT_XOR:
6080 case Operation.BIT_AND:
6081 case Operation.ISAVAILABLE:
6082 case Operation.EQUAL:
6083 case Operation.NOTEQUAL:
6090 case Operation.MULT:
6093 case Operation.LEFTSHIFT:
6094 case Operation.RIGHTSHIFT:
6095 case Operation.URIGHTSHIFT:
6097 // there are two operands
6098 nodeSet.addTupleSet(leftOpSet);
6099 nodeSet.addTupleSet(rightOpSet);
6101 nodeSet.addGlobalFlowTupleSet(leftOpSet.getGlobalLocTupleSet());
6102 nodeSet.addGlobalFlowTupleSet(rightOpSet.getGlobalLocTupleSet());
6107 throw new Error(op.toString());
6112 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
6113 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
6116 base = new NTuple<Descriptor>();
6119 NameDescriptor nd = nn.getName();
6121 if (nd.getBase() != null) {
6123 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
6124 implicitFlowTupleSet, false);
6126 // base node has the top location
6130 String varname = nd.toString();
6131 if (varname.equals("this")) {
6133 base.add(md.getThis());
6137 Descriptor d = (Descriptor) nametable.get(varname);
6139 if (d instanceof VarDescriptor) {
6140 VarDescriptor vd = (VarDescriptor) d;
6142 } else if (d instanceof FieldDescriptor) {
6143 // the type of field descriptor has a location!
6144 FieldDescriptor fd = (FieldDescriptor) d;
6145 if (fd.isStatic()) {
6147 // if it is 'static final', no need to have flow node for the TOP
6151 // if 'static', assign the default GLOBAL LOCATION to the first
6152 // element of the tuple
6153 base.add(GLOBALDESC);
6156 // the location of field access starts from this, followed by field
6158 base.add(md.getThis());
6162 } else if (d == null) {
6163 // access static field
6164 base.add(GLOBALDESC);
6165 base.add(nn.getField());
6168 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
6170 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
6171 // String globalLocId = localLattice.getGlobalLoc();
6172 // if (globalLocId == null) {
6174 // Error("Method lattice does not define global variable location at "
6175 // + generateErrorMessage(md.getClassDesc(), nn));
6177 // loc.addLocation(new Location(md, globalLocId));
6179 // Location fieldLoc = (Location) fd.getType().getExtension();
6180 // loc.addLocation(fieldLoc);
6186 getFlowGraph(md).createNewFlowNode(base);
6192 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
6193 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
6194 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
6195 // System.out.println("analyzeFlowFieldAccessNode=" + fan.printNode(0));
6197 String currentArrayAccessNodeExpStr = null;
6198 ExpressionNode left = fan.getExpression();
6199 TypeDescriptor ltd = left.getType();
6200 FieldDescriptor fd = fan.getField();
6201 ArrayAccessNode aan = null;
6203 String varName = null;
6204 if (left.kind() == Kind.NameNode) {
6205 NameDescriptor nd = ((NameNode) left).getName();
6206 varName = nd.toString();
6209 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
6210 // using a class name directly or access using this
6211 if (fd.isStatic() && fd.isFinal()) {
6216 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
6218 boolean isArrayCase = false;
6219 if (left instanceof ArrayAccessNode) {
6222 aan = (ArrayAccessNode) left;
6224 currentArrayAccessNodeExpStr = aan.printNode(0);
6225 arrayAccessNodeStack.push(currentArrayAccessNodeExpStr);
6227 left = aan.getExpression();
6228 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
6229 implicitFlowTupleSet, isLHS);
6233 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
6236 // in this case, field is TOP location
6240 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
6242 if (!left.getType().isPrimitive()) {
6243 if (!fd.getSymbol().equals("length")) {
6244 // array.length access, just have the location of the array
6245 flowFieldTuple.add(fd);
6246 nodeSet.removeTuple(base);
6249 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
6252 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
6253 NTuple<Descriptor> idxTuple = idxIter.next();
6254 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
6257 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6258 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
6260 NTuple<Location> calleeReturnLocTuple = iterator.next();
6262 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6263 translateToLocTuple(md, flowFieldTuple));
6267 nodeSet.addTupleSet(idxNodeTupleSet);
6269 // if it is the array case and not the LHS case
6271 arrayAccessNodeStack.pop();
6273 if (arrayAccessNodeStack.isEmpty()
6274 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
6275 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
6277 nodeSetArrayAccessExp.addTuple(flowFieldTuple);
6278 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
6279 nodeSetArrayAccessExp.addTupleSet(nodeSet);
6281 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
6282 System.out.println("4");
6283 System.out.println("nodeSetArrayAccessExp=" + nodeSetArrayAccessExp);
6284 // System.out.println("idxNodeTupleSet.getGlobalLocTupleSet()="
6285 // + idxNodeTupleSet.getGlobalLocTupleSet());
6287 NTuple<Descriptor> interTuple =
6288 getFlowGraph(md).createIntermediateNode().getDescTuple();
6290 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter
6292 NTuple<Descriptor> higherTuple = iter.next();
6293 addFlowGraphEdge(md, higherTuple, interTuple);
6296 FlowGraph fg = getFlowGraph(md);
6297 fg.addMapInterLocNodeToEnclosingDescriptor(interTuple.get(0),
6298 getClassTypeDescriptor(base.get(base.size() - 1)));
6301 flowFieldTuple = interTuple;
6303 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
6309 return flowFieldTuple;
6314 private void debug_printTreeNode(TreeNode tn) {
6316 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
6320 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
6321 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
6322 NodeTupleSet implicitFlowTupleSet) {
6324 NodeTupleSet nodeSetRHS = new NodeTupleSet();
6325 NodeTupleSet nodeSetLHS = new NodeTupleSet();
6327 boolean postinc = true;
6328 if (an.getOperation().getBaseOp() == null
6329 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
6330 .getBaseOp().getOp() != Operation.POSTDEC)) {
6333 // if LHS is array access node, need to capture value flows between an array
6334 // and its index value
6335 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
6339 // analyze value flows of rhs expression
6340 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
6343 System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
6344 System.out.println("-nodeSetLHS=" + nodeSetLHS);
6345 System.out.println("-nodeSetRHS=" + nodeSetRHS);
6346 System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
6347 // System.out.println("-");
6349 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
6350 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
6352 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
6353 NTuple<Descriptor> fromTuple = iter.next();
6354 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6355 NTuple<Descriptor> toTuple = iter2.next();
6356 addFlowGraphEdge(md, fromTuple, toTuple);
6361 // creates edges from RHS to LHS
6362 NTuple<Descriptor> interTuple = null;
6363 if (needToGenerateInterLoc(nodeSetRHS)) {
6364 System.out.println("2");
6365 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
6368 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
6369 NTuple<Descriptor> fromTuple = iter.next();
6370 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6371 NTuple<Descriptor> toTuple = iter2.next();
6372 addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
6376 // creates edges from implicitFlowTupleSet to LHS
6377 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
6378 NTuple<Descriptor> fromTuple = iter.next();
6379 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6380 NTuple<Descriptor> toTuple = iter2.next();
6381 addFlowGraphEdge(md, fromTuple, toTuple);
6385 // create global flow edges if the callee gives return value flows to the caller
6386 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6387 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
6388 NTuple<Location> calleeReturnLocTuple = iterator.next();
6389 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6390 NTuple<Descriptor> callerLHSTuple = iter2.next();
6391 System.out.println("$$$ GLOBAL FLOW ADD=" + calleeReturnLocTuple + " -> "
6392 + translateToLocTuple(md, callerLHSTuple));
6394 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6395 translateToLocTuple(md, callerLHSTuple));
6399 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
6401 NTuple<Location> calleeReturnLocTuple = iterator.next();
6402 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6403 NTuple<Descriptor> callerLHSTuple = iter2.next();
6405 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6406 translateToLocTuple(md, callerLHSTuple));
6407 System.out.println("$$$ GLOBAL FLOW PCLOC ADD=" + calleeReturnLocTuple + " -> "
6408 + translateToLocTuple(md, callerLHSTuple));
6415 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6416 NTuple<Descriptor> tuple = iter2.next();
6417 addFlowGraphEdge(md, tuple, tuple);
6420 // creates edges from implicitFlowTupleSet to LHS
6421 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
6422 NTuple<Descriptor> fromTuple = iter.next();
6423 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6424 NTuple<Descriptor> toTuple = iter2.next();
6425 addFlowGraphEdge(md, fromTuple, toTuple);
6429 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6430 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
6432 NTuple<Location> calleeReturnLocTuple = iterator.next();
6433 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6434 NTuple<Descriptor> callerLHSTuple = iter2.next();
6435 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6436 translateToLocTuple(md, callerLHSTuple));
6437 System.out.println("$$$ GLOBAL FLOW PC ADD=" + calleeReturnLocTuple + " -> "
6438 + translateToLocTuple(md, callerLHSTuple));
6444 if (nodeSet != null) {
6445 nodeSet.addTupleSet(nodeSetLHS);
6446 nodeSet.addGlobalFlowTupleSet(nodeSetLHS.getGlobalLocTupleSet());
6450 public FlowGraph getFlowGraph(MethodDescriptor md) {
6451 return mapMethodDescriptorToFlowGraph.get(md);
6454 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
6455 NTuple<Descriptor> to) {
6456 FlowGraph graph = getFlowGraph(md);
6457 graph.addValueFlowEdge(from, to);
6461 private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
6462 NTuple<Descriptor> inter, NTuple<Descriptor> to) {
6464 FlowGraph graph = getFlowGraph(md);
6466 if (inter != null) {
6467 graph.addValueFlowEdge(from, inter);
6468 graph.addValueFlowEdge(inter, to);
6470 graph.addValueFlowEdge(from, to);
6475 public void writeInferredLatticeDotFile(ClassDescriptor cd, HierarchyGraph simpleHierarchyGraph,
6476 SSJavaLattice<String> locOrder, String nameSuffix) {
6477 System.out.println("@cd=" + cd);
6478 System.out.println("@sharedLoc=" + locOrder.getSharedLocSet());
6479 writeInferredLatticeDotFile(cd, null, simpleHierarchyGraph, locOrder, nameSuffix);
6482 public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
6483 HierarchyGraph simpleHierarchyGraph, SSJavaLattice<String> locOrder, String nameSuffix) {
6485 String fileName = "lattice_";
6488 cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
6490 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
6493 fileName += nameSuffix;
6495 System.out.println("***lattice=" + fileName + " setsize=" + locOrder.getKeySet().size());
6497 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
6499 Set<String> addedLocSet = new HashSet<String>();
6501 if (pairSet.size() > 0) {
6503 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
6505 bw.write("digraph " + fileName + " {\n");
6507 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
6508 // pair is in the form of <higher, lower>
6509 Pair<String, String> pair = (Pair<String, String>) iterator.next();
6511 String highLocId = pair.getFirst();
6512 String lowLocId = pair.getSecond();
6513 if (!addedLocSet.contains(highLocId)) {
6514 addedLocSet.add(highLocId);
6515 drawNode(bw, locOrder, simpleHierarchyGraph, highLocId);
6518 if (!addedLocSet.contains(lowLocId)) {
6519 addedLocSet.add(lowLocId);
6520 drawNode(bw, locOrder, simpleHierarchyGraph, lowLocId);
6523 bw.write(highLocId + " -> " + lowLocId + ";\n");
6528 } catch (IOException e) {
6529 e.printStackTrace();
6536 private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
6538 for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
6539 HNode merged = (HNode) iterator.next();
6540 if (merged.isMergeNode()) {
6541 str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
6543 str += " " + merged.getName();
6549 private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, HierarchyGraph graph,
6550 String locName) throws IOException {
6553 if (lattice.isSharedLoc(locName)) {
6554 prettyStr = locName + "*";
6556 prettyStr = locName;
6558 // HNode node = graph.getHNode(locName);
6559 // if (node != null && node.isMergeNode()) {
6560 // Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
6561 // prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
6563 bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
6566 public void _debug_writeFlowGraph() {
6567 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
6569 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
6570 MethodDescriptor md = (MethodDescriptor) iterator.next();
6571 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
6572 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
6575 subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
6576 } catch (IOException e) {
6577 e.printStackTrace();
6585 class CyclicFlowException extends Exception {
6589 class InterDescriptor extends Descriptor {
6591 Pair<MethodInvokeNode, Integer> minArgIdxPair;
6593 public InterDescriptor(String name) {
6597 public void setMethodArgIdxPair(MethodInvokeNode min, int idx) {
6598 minArgIdxPair = new Pair<MethodInvokeNode, Integer>(min, new Integer(idx));
6601 public Pair<MethodInvokeNode, Integer> getMethodArgIdxPair() {
6602 return minArgIdxPair;