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, NTuple<Descriptor>> mapHighestOverriddenMethodDescToReturnLocTuple;
140 private Map<MethodDescriptor, NTuple<Descriptor>> mapHighestOverriddenMethodDescToPCLocTuple;
142 private Map<MethodDescriptor, Set<NTuple<Descriptor>>> mapHighestOverriddenMethodDescToSetLowerThanPCLoc;
144 private Map<MethodDescriptor, Set<NTuple<Descriptor>>> mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc;
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 mapHighestOverriddenMethodDescToSetLowerThanPCLoc =
237 new HashMap<MethodDescriptor, Set<NTuple<Descriptor>>>();
239 mapHighestOverriddenMethodDescToMethodDescSet =
240 new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
242 mapHighestOverriddenMethodDescToReturnLocTuple =
243 new HashMap<MethodDescriptor, NTuple<Descriptor>>();
245 mapHighestOverriddenMethodDescToPCLocTuple =
246 new HashMap<MethodDescriptor, NTuple<Descriptor>>();
248 mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc =
249 new HashMap<MethodDescriptor, Set<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();
317 constructHierarchyGraph();
319 addInheritanceConstraintsToHierarchyGraph();
321 debug_writeHierarchyDotFiles();
323 simplifyHierarchyGraph();
325 debug_writeSimpleHierarchyDotFiles();
327 constructSkeletonHierarchyGraph();
329 debug_writeSkeletonHierarchyDotFiles();
331 insertCombinationNodes();
333 debug_writeSkeletonCombinationHierarchyDotFiles();
335 buildLatticeInheritanceTree();
338 debug_writeLattices();
340 updateCompositeLocationAssignments();
342 generateMethodSummary();
344 generateAnnoatedCode();
346 for (Iterator iterator = cd2lattice.keySet().iterator(); iterator.hasNext();) {
347 ClassDescriptor cd = (ClassDescriptor) iterator.next();
348 SSJavaLattice<String> lattice = getLattice(cd);
349 HierarchyGraph hg = mapDescriptorToHierarchyGraph.get(cd);
350 // System.out.println("~~~\t" + cd + "\t" + lattice.getKeySet().size() + "\t"
351 // + hg.getNodeSet().size());
354 for (Iterator iterator = md2lattice.keySet().iterator(); iterator.hasNext();) {
355 MethodDescriptor md = (MethodDescriptor) iterator.next();
356 SSJavaLattice<String> locOrder = getLattice(md);
357 // writeLatticeDotFile(md.getClassDesc(), md, getMethodLattice(md));
358 HierarchyGraph hg = mapDescriptorToHierarchyGraph.get(md);
359 // System.out.println("~~~\t" + md.getClassDesc() + "_" + md + "\t"
360 // + locOrder.getKeySet().size() + "\t" + hg.getNodeSet().size());
367 private void calculateReturnPCLocInheritance() {
368 calculateHighestPCLocInheritance();
369 calculateLowestReturnLocInheritance();
370 updateFlowGraphPCReturnLocInheritance();
373 private void updateFlowGraphPCReturnLocInheritance() {
375 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
376 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
377 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
379 if (mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc).size() == 1) {
383 Set<MethodDescriptor> methodDescSet =
384 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
386 NTuple<Descriptor> highestPCLocDescTuple =
387 mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc);
389 NTuple<Descriptor> highestRETURNLocDescTuple =
390 mapHighestOverriddenMethodDescToReturnLocTuple.get(highestMethodDesc);
392 System.out.println("---updateFlowGraphPCReturnLocInheritance=" + highestMethodDesc);
393 System.out.println("-----highestPCLoc=" + highestPCLocDescTuple);
394 System.out.println("-----highestRETURNLoc=" + highestRETURNLocDescTuple);
396 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
397 MethodDescriptor md = (MethodDescriptor) iterator2.next();
398 FlowGraph flowGraph = getFlowGraph(md);
400 MethodSummary summary = getMethodSummary(md);
401 CompositeLocation curPCLoc = summary.getPCLoc();
402 NTuple<Descriptor> curPCDescTuple = translateToDescTuple(curPCLoc.getTuple());
403 System.out.println("md=" + md + " curPCLoc=" + curPCLoc);
404 System.out.println("highestPCLoc=" + highestPCLocDescTuple);
406 if (highestPCLocDescTuple == null) {
407 // this case: PCLOC is top
408 System.out.println("###SET PCLOC AS TOP");
409 if (curPCDescTuple != null && !curPCLoc.get(0).isTop()) {
410 FlowNode pcFlowNode = flowGraph.getFlowNode(curPCDescTuple);
411 flowGraph.removeNode(pcFlowNode);
413 summary.setPCLoc(new CompositeLocation(new Location(md, Location.TOP)));
415 NTuple<Descriptor> newPCDescTuple = new NTuple<Descriptor>();
416 if (highestPCLocDescTuple.size() == 1) {
417 newPCDescTuple.add(highestPCLocDescTuple.get(0));
419 newPCDescTuple.add(md.getThis());
420 newPCDescTuple.add(highestPCLocDescTuple.get(1));
422 if (!curPCDescTuple.equals(newPCDescTuple)) {
423 FlowNode pcFlowNode = flowGraph.getFlowNode(curPCDescTuple);
424 flowGraph.updateTuple(pcFlowNode, newPCDescTuple);
425 // flowGraph.removeNode(pcFlowNode);
426 System.out.println("####UPDATE PCLOC=" + newPCDescTuple);
427 Set<NTuple<Descriptor>> descSetLowerThanPCLoc =
428 mapHighestOverriddenMethodDescToSetLowerThanPCLoc.get(highestMethodDesc);
429 System.out.println("####descSetLowerThanPCLoc=" + descSetLowerThanPCLoc);
430 for (Iterator iterator3 = descSetLowerThanPCLoc.iterator(); iterator3.hasNext();) {
431 NTuple<Descriptor> lowerNTuple = (NTuple<Descriptor>) iterator3.next();
432 flowGraph.addValueFlowEdge(newPCDescTuple, lowerNTuple);
434 CompositeLocation newPCCompLoc =
435 new CompositeLocation(translateToLocTuple(md, newPCDescTuple));
436 summary.setPCLoc(newPCCompLoc);
438 System.out.println("####DO NOTHING!:)");
444 if (highestRETURNLocDescTuple != null) {
445 CompositeLocation curRETURNLoc = summary.getRETURNLoc();
446 NTuple<Descriptor> curReturnDescTuple = translateToDescTuple(curRETURNLoc.getTuple());
447 System.out.println("curRETURNLoc=" + curRETURNLoc);
449 if (!curReturnDescTuple.equals(highestRETURNLocDescTuple)) {
450 // handle the case that RETURNLOC is started with 'this'...
451 NTuple<Descriptor> newRETURNLocDescTuple = new NTuple<Descriptor>();
452 if (highestRETURNLocDescTuple.size() == 1) {
453 newRETURNLocDescTuple.add(highestRETURNLocDescTuple.get(0));
455 newRETURNLocDescTuple.add(md.getThis());
456 newRETURNLocDescTuple.add(highestRETURNLocDescTuple.get(1));
459 FlowNode returnFlowNode = flowGraph.getFlowNode(curReturnDescTuple);
460 System.out.println("####UPDATE RETURNLOC=" + newRETURNLocDescTuple);
461 flowGraph.updateTuple(returnFlowNode, newRETURNLocDescTuple);
463 Set<NTuple<Descriptor>> descSetHigherThanRETURNLoc =
464 mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc.get(highestMethodDesc);
465 System.out.println("####descSetLowerThanPCLoc=" + descSetHigherThanRETURNLoc);
466 for (Iterator iterator3 = descSetHigherThanRETURNLoc.iterator(); iterator3.hasNext();) {
467 NTuple<Descriptor> higherNTuple = (NTuple<Descriptor>) iterator3.next();
468 flowGraph.addValueFlowEdge(higherNTuple, newRETURNLocDescTuple);
471 CompositeLocation newRETURNLocCompLoc =
472 new CompositeLocation(translateToLocTuple(md, newRETURNLocDescTuple));
473 summary.setPCLoc(newRETURNLocCompLoc);
474 System.out.println("md=" + md + "###newRETURNLocCompLoc=" + newRETURNLocCompLoc);
476 System.out.println("####DO NOTHING!:)");
482 // flowGraph.writeGraph("2");
483 // } catch (IOException e) {
484 // // TODO Auto-generated catch block
485 // e.printStackTrace();
491 private void calculateHighestPCLocInheritance() {
493 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
495 Map<MethodDescriptor, Integer> mapMethodDescToParamCount =
496 new HashMap<MethodDescriptor, Integer>();
498 next: for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
499 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
501 NTuple<Descriptor> tempTuple = null;
503 if (getMethodSummary(highestMethodDesc).getPCLoc() != null) {
505 Set<MethodDescriptor> methodDescSet =
506 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
508 if (methodDescSet.size() > 1) {
509 System.out.println("---method desc set=" + methodDescSet + " from=" + highestMethodDesc);
514 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
515 MethodDescriptor md = (MethodDescriptor) iterator2.next();
517 FlowGraph flowGraph = getFlowGraph(md);
518 if (flowGraph == null) {
521 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
522 System.out.println("###md=" + md + " paramNodeSet=" + paramNodeSet);
524 CompositeLocation pcLOC = getMethodSummary(md).getPCLoc();
525 System.out.println("---pcLOC=" + pcLOC);
527 if (md.equals(highestMethodDesc)) {
528 mapHighestOverriddenMethodDescToPCLocTuple.put(highestMethodDesc,
529 translateToDescTuple(pcLOC.getTuple()));
532 if (!pcLOC.get(0).isTop()) {
534 FlowNode pcFlowNode = flowGraph.getFlowNode(translateToDescTuple(pcLOC.getTuple()));
537 for (Iterator iterator3 = paramNodeSet.iterator(); iterator3.hasNext();) {
538 FlowNode paramNode = (FlowNode) iterator3.next();
539 if (flowGraph.getReachableSetFrom(pcFlowNode.getCurrentDescTuple().subList(0, 1))
540 .contains(paramNode)) {
542 System.out.println("-------" + pcFlowNode + " -> " + paramNode);
545 System.out.println("$$$ pcLOC=" + pcLOC + " count higher=" + count);
546 mapMethodDescToParamCount.put(md, count);
550 // the PC location is top
551 // if one of pcloc among the method inheritance chain has the TOP,
552 // all methods in the same chain should have the TOP.
553 mapHighestOverriddenMethodDescToPCLocTuple.remove(highestMethodDesc);
554 // System.out.println("highest=" + highestMethodDesc + " HIGHEST PCLOC="
555 // + mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc));
557 Set<NTuple<Descriptor>> descTupleSetLowerThanPC = new HashSet<NTuple<Descriptor>>();
558 for (Iterator iterator3 = paramNodeSet.iterator(); iterator3.hasNext();) {
559 FlowNode flowNode = (FlowNode) iterator3.next();
560 descTupleSetLowerThanPC.add(flowNode.getCurrentDescTuple());
562 System.out.println("###TOP CASE");
563 System.out.println("descTupleSetLowerThanPC=" + descTupleSetLowerThanPC);
564 mapHighestOverriddenMethodDescToSetLowerThanPCLoc.put(highestMethodDesc,
565 descTupleSetLowerThanPC);
571 System.out.println("#INDENTIFY WHICH METHOD...");
572 // identify which method in the inheritance chain has the highest PCLOC
573 // basically, finds a method that has the highest count or TOP location
574 int highestCount = -1;
575 MethodDescriptor methodDescHighestCount = null;
576 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
577 MethodDescriptor methodDesc = (MethodDescriptor) iterator2.next();
578 if (mapMethodDescToParamCount.containsKey(methodDesc)) {
579 int curCount = mapMethodDescToParamCount.get(methodDesc).intValue();
580 if (highestCount < curCount) {
581 highestCount = curCount;
582 methodDescHighestCount = methodDesc;
587 if (methodDescHighestCount != null) {
588 FlowGraph flowGraph = getFlowGraph(methodDescHighestCount);
589 CompositeLocation pcLOC = getMethodSummary(methodDescHighestCount).getPCLoc();
590 FlowNode pcFlowNode = flowGraph.getFlowNode(translateToDescTuple(pcLOC.getTuple()));
591 Set<FlowNode> reachableSet =
592 flowGraph.getReachableSetFrom(pcFlowNode.getCurrentDescTuple().subList(0, 1));
594 Set<FlowNode> reachableParamNodeSet = new HashSet<FlowNode>();
595 for (Iterator iterator3 = reachableSet.iterator(); iterator3.hasNext();) {
596 FlowNode flowNode = (FlowNode) iterator3.next();
597 if (flowGraph.isParameter(flowNode.getCurrentDescTuple())) {
598 reachableParamNodeSet.add(flowNode);
601 System.out.println(flowNode + " is PARAM="
602 + flowGraph.isParameter(flowNode.getCurrentDescTuple()));
605 Set<NTuple<Descriptor>> descTupleSetLowerThanPC = new HashSet<NTuple<Descriptor>>();
606 for (Iterator iterator2 = reachableParamNodeSet.iterator(); iterator2.hasNext();) {
607 FlowNode flowNode = (FlowNode) iterator2.next();
608 descTupleSetLowerThanPC.add(flowNode.getCurrentDescTuple());
611 // mapHighestOverriddenMethodDescToPCLocTuple.remove(highestMethodDesc);
612 mapHighestOverriddenMethodDescToSetLowerThanPCLoc.put(highestMethodDesc,
613 descTupleSetLowerThanPC);
618 System.out.println("####################################");
619 System.out.println(" highest=" + highestMethodDesc + " HIGHEST PCLOC="
620 + mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc));
621 System.out.println(" setLowerThanPCLoc="
622 + mapHighestOverriddenMethodDescToSetLowerThanPCLoc.get(highestMethodDesc));
627 private void calculateLowestReturnLocInheritance() {
629 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
631 Map<MethodDescriptor, Integer> mapMethodDescToParamCount =
632 new HashMap<MethodDescriptor, Integer>();
634 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
635 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
637 NTuple<Descriptor> tempTuple = null;
639 if (getMethodSummary(highestMethodDesc).getRETURNLoc() != null) {
641 System.out.println("---calculateLowestReturnLocInheritance=" + highestMethodDesc);
643 Set<MethodDescriptor> methodDescSet =
644 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
646 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
647 MethodDescriptor md = (MethodDescriptor) iterator2.next();
649 FlowGraph flowGraph = getFlowGraph(md);
650 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
651 System.out.println("###md=" + md + " paramNodeSet=" + paramNodeSet + " returnLoc="
652 + getMethodSummary(md).getRETURNLoc());
654 CompositeLocation returnLoc = getMethodSummary(md).getRETURNLoc();
656 FlowNode returnFlowNode =
657 flowGraph.getFlowNode(translateToDescTuple(returnLoc.getTuple()));
660 for (Iterator iterator3 = paramNodeSet.iterator(); iterator3.hasNext();) {
661 FlowNode paramNode = (FlowNode) iterator3.next();
662 if (flowGraph.getReachableSetFrom(paramNode.getCurrentDescTuple().subList(0, 1))
663 .contains(returnFlowNode)) {
667 mapMethodDescToParamCount.put(md, count);
668 System.out.println("###returnLoc=" + returnLoc + " count higher=" + count);
671 System.out.println("#INDENTIFY WHICH METHOD...");
672 // identify which method in the inheritance chain has the highest PCLOC
673 // basically, finds a method that has the highest count or TOP location
674 int highestCount = -1;
675 MethodDescriptor methodDescHighestCount = null;
676 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
677 MethodDescriptor methodDesc = (MethodDescriptor) iterator2.next();
678 int curCount = mapMethodDescToParamCount.get(methodDesc).intValue();
679 if (highestCount < curCount) {
680 highestCount = curCount;
681 methodDescHighestCount = methodDesc;
685 if (methodDescHighestCount != null) {
686 FlowGraph flowGraph = getFlowGraph(methodDescHighestCount);
687 CompositeLocation returnLOC = getMethodSummary(methodDescHighestCount).getRETURNLoc();
688 NTuple<Descriptor> returnLocTuple = translateToDescTuple(returnLOC.getTuple());
689 FlowNode returnFlowNode = flowGraph.getFlowNode(returnLocTuple);
691 Set<FlowNode> curMethodParamNodeSet = flowGraph.getParamFlowNodeSet();
692 Set<NTuple<Descriptor>> descTupleSetHigherThanPC = new HashSet<NTuple<Descriptor>>();
693 for (Iterator iterator3 = curMethodParamNodeSet.iterator(); iterator3.hasNext();) {
694 FlowNode paramNode = (FlowNode) iterator3.next();
695 if (flowGraph.getReachableSetFrom(paramNode.getCurrentDescTuple().subList(0, 1))
696 .contains(returnFlowNode)) {
697 descTupleSetHigherThanPC.add(paramNode.getCurrentDescTuple());
701 mapHighestOverriddenMethodDescToReturnLocTuple.put(highestMethodDesc, returnLocTuple);
702 mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc.put(highestMethodDesc,
703 descTupleSetHigherThanPC);
707 System.out.println("####################################");
708 System.out.println(" highest=" + highestMethodDesc + " LOWEST RETURNLOC="
709 + mapHighestOverriddenMethodDescToReturnLocTuple.get(highestMethodDesc));
710 System.out.println(" setHigherThanReturnLoc="
711 + mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc.get(highestMethodDesc));
719 private void addMapHighestMethodDescToMethodDesc(MethodDescriptor highest, MethodDescriptor md) {
720 if (!mapHighestOverriddenMethodDescToMethodDescSet.containsKey(highest)) {
721 mapHighestOverriddenMethodDescToMethodDescSet.put(highest, new HashSet<MethodDescriptor>());
723 mapHighestOverriddenMethodDescToMethodDescSet.get(highest).add(md);
726 private void DFSInheritanceTreeCalculatingHighestOverriddenMethod(ClassDescriptor cd) {
728 // ClassDescriptor cd = node.getData();
730 for (Iterator iterator = cd.getMethods(); iterator.hasNext();) {
731 MethodDescriptor md = (MethodDescriptor) iterator.next();
732 MethodDescriptor highestMethodDesc = getHighestOverriddenMethod(md.getClassDesc(), md);
733 mapMethodDescToHighestOverriddenMethodDesc.put(md, highestMethodDesc);
734 addMapHighestMethodDescToMethodDesc(highestMethodDesc, md);
739 Set<ClassDescriptor> children = getDirectSubClasses(cd);
740 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
741 ClassDescriptor child = (ClassDescriptor) iterator.next();
742 DFSInheritanceTreeCalculatingHighestOverriddenMethod(child);
747 private MethodDescriptor getHighestOverriddenMethod(ClassDescriptor curClassDesc,
748 MethodDescriptor curMethodDesc) {
750 // Node<ClassDescriptor> curNode = inheritanceTree.getTreeNode(curClassDesc);
751 // Node<ClassDescriptor> parentNode = curNode.getParent();
752 ClassDescriptor parentClassDesc = curClassDesc.getSuperDesc();
754 if (parentClassDesc != null) {
755 if (parentClassDesc.getMethodTable().contains(curMethodDesc.getSymbol())) {
756 Set<MethodDescriptor> methodDescSet =
757 parentClassDesc.getMethodTable().getSet(curMethodDesc.getSymbol());
758 for (Iterator iterator = methodDescSet.iterator(); iterator.hasNext();) {
759 MethodDescriptor md = (MethodDescriptor) iterator.next();
760 if (md.matches(curMethodDesc)) {
761 return getHighestOverriddenMethod(parentClassDesc, md);
765 // traverse to the parent!
766 return getHighestOverriddenMethod(parentClassDesc, curMethodDesc);
768 return curMethodDesc;
771 private void buildInheritanceTree() {
773 DFSInheritanceTreeCalculatingHighestOverriddenMethod(rootClassDescriptor);
777 private void addInheritanceConstraintsToHierarchyGraph() {
779 // DFS the inheritance tree and propagates nodes/edges of parent to child
781 // Node<ClassDescriptor> rootNode = inheritanceTree.getRootNode();
782 DFSInheritanceTree(rootClassDescriptor);
786 private void DFSInheritanceTree(ClassDescriptor parentClassDescriptor) {
788 // ClassDescriptor parentClassDescriptor = parentNode.getData();
790 Set<ClassDescriptor> children = getDirectSubClasses(parentClassDescriptor);
791 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
792 ClassDescriptor childClassDescriptor = (ClassDescriptor) iterator.next();
794 HierarchyGraph parentGraph = getHierarchyGraph(parentClassDescriptor);
795 HierarchyGraph childGraph = getHierarchyGraph(childClassDescriptor);
797 Set<HNode> parentNodeSet = parentGraph.getNodeSet();
798 for (Iterator iterator2 = parentNodeSet.iterator(); iterator2.hasNext();) {
799 HNode hNode = (HNode) iterator2.next();
800 childGraph.addNode(hNode);
803 // copies extra information from the parent hierarchy graph
804 Map<HNode, Set<HNode>> parentMergeNodeMap = parentGraph.getMapHNodetoMergeSet();
805 Map<HNode, Set<HNode>> childMergeNodeMap = childGraph.getMapHNodetoMergeSet();
807 Set<HNode> keySet = parentMergeNodeMap.keySet();
808 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
809 HNode parentKey = (HNode) iterator2.next();
810 if (!childMergeNodeMap.containsKey(parentKey)) {
811 childMergeNodeMap.put(parentKey, new HashSet<HNode>());
813 childMergeNodeMap.get(parentKey).addAll(parentMergeNodeMap.get(parentKey));
816 // copies nodes/edges from the parent class...
817 for (Iterator iterator2 = parentNodeSet.iterator(); iterator2.hasNext();) {
818 HNode parentHNode = (HNode) iterator2.next();
820 Set<HNode> parentIncomingHNode = parentGraph.getIncomingNodeSet(parentHNode);
821 Set<HNode> parentOutgoingHNode = parentGraph.getOutgoingNodeSet(parentHNode);
823 for (Iterator iterator3 = parentIncomingHNode.iterator(); iterator3.hasNext();) {
824 HNode inHNode = (HNode) iterator3.next();
825 childGraph.addEdge(inHNode.getDescriptor(), parentHNode.getDescriptor());
828 for (Iterator iterator3 = parentOutgoingHNode.iterator(); iterator3.hasNext();) {
829 HNode outHNode = (HNode) iterator3.next();
830 childGraph.addEdge(parentHNode.getDescriptor(), outHNode.getDescriptor());
835 // copies nodes/edges from parent methods to overridden methods
837 for (Iterator iterator3 = childClassDescriptor.getMethods(); iterator3.hasNext();) {
838 MethodDescriptor childMethodDescriptor = (MethodDescriptor) iterator3.next();
840 MethodDescriptor parentMethodDesc =
841 getParentMethodDesc(childMethodDescriptor.getClassDesc(), childMethodDescriptor);
843 if (parentMethodDesc != null) {
845 HierarchyGraph parentMethodGraph = getHierarchyGraph(parentMethodDesc);
846 HierarchyGraph childMethodGraph = getHierarchyGraph(childMethodDescriptor);
848 Set<HNode> parentMethodNodeSet = parentMethodGraph.getNodeSet();
849 for (Iterator iterator2 = parentMethodNodeSet.iterator(); iterator2.hasNext();) {
850 HNode hNode = (HNode) iterator2.next();
851 childMethodGraph.addNode(hNode);
854 // copies extra information from the parent hierarchy graph
855 Map<HNode, Set<HNode>> parentMethodMergeNodeMap =
856 parentMethodGraph.getMapHNodetoMergeSet();
857 Map<HNode, Set<HNode>> childMethodMergeNodeMap = childMethodGraph.getMapHNodetoMergeSet();
859 Set<HNode> methodKeySet = parentMethodMergeNodeMap.keySet();
860 for (Iterator iterator2 = methodKeySet.iterator(); iterator2.hasNext();) {
861 HNode parentKey = (HNode) iterator2.next();
862 if (!childMethodMergeNodeMap.containsKey(parentKey)) {
863 childMethodMergeNodeMap.put(parentKey, new HashSet<HNode>());
865 childMethodMergeNodeMap.get(parentKey).addAll(parentMethodMergeNodeMap.get(parentKey));
868 // copies nodes/edges from the parent method...
869 for (Iterator iterator2 = parentMethodGraph.getNodeSet().iterator(); iterator2.hasNext();) {
870 HNode parentHNode = (HNode) iterator2.next();
872 Set<HNode> parentIncomingHNode = parentMethodGraph.getIncomingNodeSet(parentHNode);
873 Set<HNode> parentOutgoingHNode = parentMethodGraph.getOutgoingNodeSet(parentHNode);
875 for (Iterator iterator4 = parentIncomingHNode.iterator(); iterator4.hasNext();) {
876 HNode inHNode = (HNode) iterator4.next();
877 childMethodGraph.addEdge(inHNode, parentHNode);
880 for (Iterator iterator4 = parentOutgoingHNode.iterator(); iterator4.hasNext();) {
881 HNode outHNode = (HNode) iterator4.next();
882 childMethodGraph.addEdge(parentHNode, outHNode);
891 DFSInheritanceTree(childClassDescriptor);
896 public MethodDescriptor getParentMethodDesc(ClassDescriptor classDesc, MethodDescriptor methodDesc) {
898 // Node<ClassDescriptor> childNode = inheritanceTree.getTreeNode(classDesc);
899 ClassDescriptor parentClassDesc = classDesc.getSuperDesc();
900 // Node<ClassDescriptor> parentNode = childNode.getParent();
902 if (parentClassDesc != null) {
903 // ClassDescriptor parentClassDesc = parentNode.getData();
904 if (parentClassDesc.getMethodTable().contains(methodDesc.getSymbol())) {
905 Set<MethodDescriptor> methodDescSet =
906 parentClassDesc.getMethodTable().getSet(methodDesc.getSymbol());
907 for (Iterator iterator = methodDescSet.iterator(); iterator.hasNext();) {
908 MethodDescriptor md = (MethodDescriptor) iterator.next();
909 if (md.matches(methodDesc)) {
915 // traverse to the parent!
916 getParentMethodDesc(parentClassDesc, methodDesc);
923 private void checkReturnNodes() {
924 LinkedList<MethodDescriptor> methodDescList =
925 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
927 while (!methodDescList.isEmpty()) {
928 MethodDescriptor md = methodDescList.removeLast();
930 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
931 checkFlowNodeReturnThisField(md);
933 // // in this case, this method will return the composite location that starts with 'this'
934 // FlowGraph flowGraph = getFlowGraph(md);
935 // Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
942 private void updateFlowGraph() {
944 LinkedList<MethodDescriptor> methodDescList =
945 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
947 while (!methodDescList.isEmpty()) {
948 MethodDescriptor md = methodDescList.removeLast();
949 if (state.SSJAVADEBUG) {
950 System.out.println();
951 System.out.println("SSJAVA: Updating a flow graph: " + md);
952 propagateFlowsFromCalleesWithNoCompositeLocation(md);
955 Set<FlowNode> nodeSet = getFlowGraph(md).getNodeSet();
956 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
957 FlowNode flowNode = (FlowNode) iterator.next();
958 NTuple<Descriptor> descTuple = flowNode.getCurrentDescTuple();
959 NTuple<Location> locTuple = translateToLocTuple(md, descTuple);
960 for (int i = 0; i < locTuple.size(); i++) {
961 Location loc = locTuple.get(i);
962 if (loc.getDescriptor() instanceof ClassDescriptor) {
963 toanalyze_classDescSet.add((ClassDescriptor) loc.getDescriptor());
964 } else if (loc.getDescriptor() instanceof MethodDescriptor) {
965 toanalyze_classDescSet.add(((MethodDescriptor) loc.getDescriptor()).getClassDesc());
974 public Map<NTuple<Descriptor>, NTuple<Descriptor>> getMapCallerArgToCalleeParam(
975 MethodInvokeNode min) {
977 if (!mapMethodInvokeNodeToMapCallerArgToCalleeArg.containsKey(min)) {
978 mapMethodInvokeNodeToMapCallerArgToCalleeArg.put(min,
979 new HashMap<NTuple<Descriptor>, NTuple<Descriptor>>());
982 return mapMethodInvokeNodeToMapCallerArgToCalleeArg.get(min);
985 public void addMapCallerArgToCalleeParam(MethodInvokeNode min, NTuple<Descriptor> callerArg,
986 NTuple<Descriptor> calleeParam) {
987 getMapCallerArgToCalleeParam(min).put(callerArg, calleeParam);
990 private void assignCompositeLocation() {
991 calculateGlobalValueFlowCompositeLocation();
992 translateCompositeLocationAssignmentToFlowGraph();
995 private void translateCompositeLocationAssignmentToFlowGraph() {
996 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
997 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
998 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
1001 private void translateCompositeLocationAssignmentToFlowGraph2() {
1002 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
1003 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
1004 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
1007 private void addAdditionalOrderingConstraints() {
1008 System.out.println("\nSSJAVA: Add addtional ordering constriants:");
1009 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
1010 addAddtionalOrderingConstraints(methodEventLoopDesc);
1011 // calculateReturnHolderLocation();
1014 private void calculateReturnHolderLocation() {
1015 LinkedList<MethodDescriptor> methodDescList =
1016 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
1018 while (!methodDescList.isEmpty()) {
1019 MethodDescriptor md = methodDescList.removeLast();
1021 FlowGraph fg = getFlowGraph(md);
1022 Set<FlowNode> nodeSet = fg.getNodeSet();
1023 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1024 FlowNode flowNode = (FlowNode) iterator.next();
1025 if (flowNode.isFromHolder()) {
1026 calculateCompositeLocationFromFlowGraph(md, flowNode);
1033 private void updateCompositeLocationAssignments() {
1035 LinkedList<MethodDescriptor> methodDescList =
1036 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
1038 while (!methodDescList.isEmpty()) {
1039 MethodDescriptor md = methodDescList.removeLast();
1041 // System.out.println("\n#updateCompositeLocationAssignments=" + md);
1043 FlowGraph flowGraph = getFlowGraph(md);
1045 MethodSummary methodSummary = getMethodSummary(md);
1047 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1048 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1049 FlowNode node = (FlowNode) iterator.next();
1050 System.out.println("-node=" + node + " node.getDescTuple=" + node.getDescTuple());
1051 if (node.getCompositeLocation() != null) {
1052 CompositeLocation compLoc = node.getCompositeLocation();
1053 CompositeLocation updatedCompLoc = updateCompositeLocation(compLoc);
1054 node.setCompositeLocation(updatedCompLoc);
1055 System.out.println("---updatedCompLoc1=" + updatedCompLoc);
1057 NTuple<Descriptor> descTuple = node.getDescTuple();
1058 System.out.println("update desc=" + descTuple);
1059 CompositeLocation compLoc = convertToCompositeLocation(md, descTuple);
1060 compLoc = updateCompositeLocation(compLoc);
1061 node.setCompositeLocation(compLoc);
1062 System.out.println("---updatedCompLoc2=" + compLoc);
1065 if (node.isDeclaratonNode()) {
1066 Descriptor localVarDesc = node.getDescTuple().get(0);
1067 CompositeLocation compLoc = updateCompositeLocation(node.getCompositeLocation());
1068 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, compLoc);
1072 // update PCLOC and RETURNLOC if they have a composite location assignment
1073 if (methodSummary.getRETURNLoc() != null) {
1074 methodSummary.setRETURNLoc(updateCompositeLocation(methodSummary.getRETURNLoc()));
1076 if (methodSummary.getPCLoc() != null) {
1077 methodSummary.setPCLoc(updateCompositeLocation(methodSummary.getPCLoc()));
1084 private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
1085 CompositeLocation updatedCompLoc = new CompositeLocation();
1086 for (int i = 0; i < compLoc.getSize(); i++) {
1087 Location loc = compLoc.get(i);
1088 String nodeIdentifier = loc.getLocIdentifier();
1089 Descriptor enclosingDesc = loc.getDescriptor();
1091 if (!enclosingDesc.equals(GLOBALDESC)) {
1092 LocationSummary locSummary = getLocationSummary(enclosingDesc);
1093 // HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(enclosingDesc);
1094 HierarchyGraph scGraph = getSimpleHierarchyGraph(enclosingDesc);
1095 if (scGraph != null) {
1096 HNode curNode = scGraph.getCurrentHNode(nodeIdentifier);
1097 System.out.println("nodeID=" + nodeIdentifier + " curNode=" + curNode
1098 + " enclosingDesc=" + enclosingDesc);
1099 if (curNode != null) {
1100 nodeIdentifier = curNode.getName();
1103 locName = locSummary.getLocationName(nodeIdentifier);
1105 locName = nodeIdentifier;
1107 Location updatedLoc = new Location(enclosingDesc, locName);
1108 updatedCompLoc.addLocation(updatedLoc);
1111 return updatedCompLoc;
1114 private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
1116 // System.out.println("\n\n###translateCompositeLocationAssignmentToFlowGraph mdCaller="
1119 // First, assign a composite location to a node in the flow graph
1120 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
1122 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
1123 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1124 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
1126 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
1127 for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
1128 Location methodLoc = (Location) iterator.next();
1129 if (methodLoc.getDescriptor().equals(mdCaller)) {
1130 CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
1131 assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
1135 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
1137 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
1138 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1139 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1140 // need to translate a composite location that is started with the base
1142 translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
1143 MethodDescriptor mdCallee = min.getMethod();
1144 calleeSet.add(mdCallee);
1148 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1149 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1150 translateCompositeLocationAssignmentToFlowGraph(callee);
1155 private void addAddtionalOrderingConstraints(MethodDescriptor mdCaller) {
1157 // First, assign a composite location to a node in the flow graph
1158 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
1160 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
1161 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1162 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
1163 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
1165 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
1167 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
1168 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1169 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1170 MethodDescriptor mdCallee = min.getMethod();
1171 calleeSet.add(mdCallee);
1174 // add an additional ordering constraint
1175 // if the first element of a parameter composite location matches 'this' reference,
1176 // the corresponding argument in the caller is required to be higher than the translated
1177 // parameter location in the caller lattice
1179 // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
1182 // update return flow nodes in the caller
1183 CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc();
1184 System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc);
1185 if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis())
1186 && returnLoc.getSize() > 1) {
1187 System.out.println("###RETURN COMP LOC=" + returnLoc);
1188 NTuple<Location> returnLocTuple = returnLoc.getTuple();
1189 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1190 System.out.println("###basetuple=" + baseTuple);
1191 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
1192 for (int i = 1; i < returnLocTuple.size(); i++) {
1193 newReturnTuple.add(returnLocTuple.get(i).getLocDescriptor());
1195 System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple);
1197 FlowReturnNode holderNode = callerFlowGraph.getFlowReturnNode(min);
1198 NodeTupleSet holderTupleSet =
1199 getNodeTupleSetFromReturnNode(getFlowGraph(mdCaller), holderNode);
1201 callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple);
1203 // then need to remove old constraints
1205 System.out.println("###REMOVE OLD CONSTRAINTS=" + holderNode);
1206 for (Iterator<NTuple<Descriptor>> iter = holderTupleSet.iterator(); iter.hasNext();) {
1207 NTuple<Descriptor> tupleFromHolder = iter.next();
1208 Set<FlowEdge> holderOutEdge = callerFlowGraph.getOutEdgeSet(holderNode);
1209 for (Iterator iterator2 = holderOutEdge.iterator(); iterator2.hasNext();) {
1210 FlowEdge outEdge = (FlowEdge) iterator2.next();
1211 NTuple<Descriptor> toberemovedTuple = outEdge.getEndTuple();
1212 // System.out.println("---remove " + tupleFromHolder + " -> " + toberemovedTuple);
1213 callerFlowGraph.removeEdge(tupleFromHolder, toberemovedTuple);
1218 // if the return loc set was empty and later pcloc was connected to the return loc
1219 // need to make sure that return loc reflects to this changes.
1220 FlowReturnNode flowReturnNode = callerFlowGraph.getFlowReturnNode(min);
1221 if (flowReturnNode != null && flowReturnNode.getReturnTupleSet().isEmpty()) {
1223 if (needToUpdateReturnLocHolder(min.getMethod(), flowReturnNode)) {
1224 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1225 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
1226 flowReturnNode.addTuple(newReturnTuple);
1235 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1236 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1237 addAddtionalOrderingConstraints(callee);
1242 private boolean needToUpdateReturnLocHolder(MethodDescriptor mdCallee,
1243 FlowReturnNode flowReturnNode) {
1244 FlowGraph fg = getFlowGraph(mdCallee);
1245 MethodSummary summary = getMethodSummary(mdCallee);
1246 CompositeLocation returnCompLoc = summary.getRETURNLoc();
1247 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnCompLoc.getTuple());
1248 Set<FlowNode> incomingNodeToReturnNode =
1249 fg.getIncomingFlowNodeSet(fg.getFlowNode(returnDescTuple));
1250 for (Iterator iterator = incomingNodeToReturnNode.iterator(); iterator.hasNext();) {
1251 FlowNode inNode = (FlowNode) iterator.next();
1252 if (inNode.getDescTuple().get(0).equals(mdCallee.getThis())) {
1259 private void addMapMethodDescToMethodInvokeNodeSet(MethodInvokeNode min) {
1260 MethodDescriptor md = min.getMethod();
1261 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
1262 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
1264 mapMethodDescToMethodInvokeNodeSet.get(md).add(min);
1267 private Set<MethodInvokeNode> getMethodInvokeNodeSetByMethodDesc(MethodDescriptor md) {
1268 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
1269 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
1271 return mapMethodDescToMethodInvokeNodeSet.get(md);
1274 private void addOrderingConstraintFromCompLocParamToArg(MethodDescriptor mdCaller,
1275 MethodInvokeNode min) {
1276 System.out.println("-addOrderingConstraintFromCompLocParamToArg=" + min.printNode(0));
1278 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(ssjava.getMethodContainingSSJavaLoop());
1280 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
1282 MethodDescriptor mdCallee = min.getMethod();
1284 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1285 for (int idx = 0; idx < calleeFlowGraph.getNumParameters(); idx++) {
1286 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
1287 NTuple<Location> globalParamLocTuple =
1288 translateToLocTuple(mdCallee, paramNode.getDescTuple());
1289 translateToLocTuple(mdCallee, paramNode.getDescTuple());
1290 CompositeLocation compLoc = paramNode.getCompositeLocation();
1291 System.out.println("---paramNode=" + paramNode + " compLoc=" + compLoc);
1292 if (compLoc != null) {
1293 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
1294 NTuple<Location> globalArgLocTuple = translateToLocTuple(mdCaller, argTuple);
1296 if (!isLiteralValueLocTuple(globalArgLocTuple)
1297 && !isLiteralValueLocTuple(globalParamLocTuple)) {
1298 if (!globalGraph.hasValueFlowEdge(globalArgLocTuple, globalParamLocTuple)) {
1299 System.out.println("----- add global flow globalArgLocTuple=" + globalArgLocTuple
1300 + "-> globalParamLocTuple=" + globalParamLocTuple);
1302 globalGraph.addValueFlowEdge(globalArgLocTuple, globalParamLocTuple);
1306 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
1307 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
1309 if (!isLiteralValueLocTuple(pcLocTuple) && !isLiteralValueLocTuple(globalParamLocTuple)) {
1310 if (!globalGraph.hasValueFlowEdge(pcLocTuple, globalParamLocTuple)) {
1312 .println("----- add global flow PCLOC="
1314 + "-> globalParamLocTu!globalArgLocTuple.get(0).getLocDescriptor().equals(LITERALDESC)ple="
1315 + globalParamLocTuple);
1318 globalGraph.addValueFlowEdge(pcLocTuple, globalParamLocTuple);
1327 private boolean isLiteralValueLocTuple(NTuple<Location> locTuple) {
1328 return locTuple.get(0).getLocDescriptor().equals(LITERALDESC);
1331 public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
1332 CompositeLocation inferCompLoc) {
1333 Descriptor localDesc = loc.getLocDescriptor();
1335 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1336 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1337 FlowNode node = (FlowNode) iterator.next();
1338 if (node.getDescTuple().startsWith(localDesc)
1339 && !node.getDescTuple().get(0).equals(LITERALDESC)) {
1340 // need to assign the inferred composite location to this node
1341 CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
1342 node.setCompositeLocation(newCompLoc);
1343 System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
1348 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
1349 CompositeLocation inferCompLoc) {
1351 System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
1354 MethodDescriptor md = (MethodDescriptor) inferCompLoc.get(0).getDescriptor();
1356 CompositeLocation newCompLoc = new CompositeLocation();
1357 for (int i = 0; i < inferCompLoc.getSize(); i++) {
1358 newCompLoc.addLocation(inferCompLoc.get(i));
1361 Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
1362 Descriptor enclosingDescriptor;
1363 if (lastDescOfPrefix instanceof InterDescriptor) {
1364 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(lastDescOfPrefix);
1366 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
1369 for (int i = 1; i < nodeDescTuple.size(); i++) {
1370 Descriptor desc = nodeDescTuple.get(i);
1371 Location locElement = new Location(enclosingDescriptor, desc);
1372 newCompLoc.addLocation(locElement);
1374 enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
1380 private void translateMapLocationToInferCompositeLocationToCalleeGraph(
1381 GlobalFlowGraph callerGraph, MethodInvokeNode min) {
1383 MethodDescriptor mdCallee = min.getMethod();
1384 MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
1385 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1386 callerGraph.getMapLocationToInferCompositeLocation();
1388 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
1390 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1391 GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1393 NTuple<Location> baseLocTuple = null;
1394 if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
1395 baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
1398 // System.out.println("\n-#translate caller=" + mdCaller + " infer composite loc to callee="
1399 // + mdCallee + " baseLocTuple=" + baseLocTuple);
1401 Set<Location> keySet = callerMapLocToCompLoc.keySet();
1402 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1403 Location key = (Location) iterator.next();
1404 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
1406 if (!key.getDescriptor().equals(mdCaller)) {
1408 CompositeLocation newCalleeCompLoc;
1409 if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
1410 // System.out.println("-----need to translate callerCompLoc=" + callerCompLoc
1411 // + " with baseTuple=" + baseLocTuple);
1413 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
1415 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1416 // System.out.println("1---key=" + key + " callerCompLoc=" + callerCompLoc
1417 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1418 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1419 if (!newCalleeCompLoc.get(0).getDescriptor().equals(mdCallee)) {
1423 // System.out.println("-----baseLoctuple=" + baseLocTuple);
1425 // check if it is the global access
1426 Location compLocFirstElement = callerCompLoc.getTuple().get(0);
1427 if (compLocFirstElement.getDescriptor().equals(mdCallee)
1428 && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
1430 newCalleeCompLoc = new CompositeLocation();
1431 Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
1433 newCalleeCompLoc.addLocation(newMethodLoc);
1434 for (int i = 1; i < callerCompLoc.getSize(); i++) {
1435 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1437 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1438 // System.out.println("2---key=" + key + " callerCompLoc=" + callerCompLoc
1439 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1440 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1443 int paramIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
1444 if (paramIdx == -1) {
1445 // here, the first element of the current composite location comes from the current
1447 // so transfer the same composite location to the callee
1448 if (!calleeGlobalGraph.contrainsInferCompositeLocationMapKey(key)) {
1449 if (callerCompLoc.get(0).getDescriptor().equals(mdCallee)) {
1450 // System.out.println("3---key=" + key + " callerCompLoc=" + callerCompLoc
1451 // + " newCalleeCompLoc=" + callerCompLoc);
1452 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1453 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, callerCompLoc);
1455 // System.out.println("3---SKIP key=" + key + " callerCompLoc=" + callerCompLoc);
1461 // It is the case where two parameters have relative orderings between them by having
1462 // composite locations
1463 // if we found the param idx, it means that the first part of the caller composite
1464 // location corresponds to the one of arguments.
1465 // for example, if the caller argument is <<caller.this>,<Decoder.br>>
1466 // and the current caller composite location mapping
1467 // <<caller.this>,<Decoder.br>,<Br.value>>
1468 // and the parameter which matches with the caller argument is 'Br brParam'
1469 // then, the translated callee composite location will be <<callee.brParam>,<Br.value>>
1470 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(paramIdx);
1472 FlowNode paramFlowNode = calleeFlowGraph.getParamFlowNode(paramIdx);
1473 NTuple<Location> paramLocTuple =
1474 translateToLocTuple(mdCallee, paramFlowNode.getDescTuple());
1475 newCalleeCompLoc = new CompositeLocation();
1476 for (int i = 0; i < paramLocTuple.size(); i++) {
1477 newCalleeCompLoc.addLocation(paramLocTuple.get(i));
1479 for (int i = argTuple.size(); i < callerCompLoc.getSize(); i++) {
1480 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1482 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1483 // System.out.println("4---key=" + key + " callerCompLoc=" + callerCompLoc
1484 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1485 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1494 System.out.println("#ASSIGN COMP LOC TO CALLEE PARAMS: callee=" + mdCallee + " caller="
1496 // If the location of an argument has a composite location
1497 // need to assign a proper composite location to the corresponding callee parameter
1498 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
1499 for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
1500 Integer idx = (Integer) iterator.next();
1502 if (idx == 0 && !min.getMethod().isStatic()) {
1506 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
1507 System.out.println("-argTuple=" + argTuple + " idx=" + idx);
1508 if (argTuple.size() > 0) {
1509 // check if an arg tuple has been already assigned to a composite location
1510 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
1511 Location argLocalLoc = argLocTuple.get(0);
1513 // if (!isPrimitiveType(argTuple)) {
1514 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
1516 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
1517 for (int i = 1; i < argLocTuple.size(); i++) {
1518 callerCompLoc.addLocation(argLocTuple.get(i));
1521 System.out.println("---callerCompLoc=" + callerCompLoc);
1523 // if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
1525 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
1527 NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
1528 NTuple<Location> calleeParamLocTuple =
1529 translateToLocTuple(mdCallee, calleeParamDescTuple);
1531 int refParamIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
1532 System.out.println("-----paramIdx=" + refParamIdx);
1533 if (refParamIdx == 0 && !mdCallee.isStatic()) {
1535 System.out.println("-------need to translate callerCompLoc=" + callerCompLoc
1536 + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
1537 + calleeParamLocTuple);
1539 CompositeLocation newCalleeCompLoc =
1540 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
1542 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1545 System.out.println("---------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
1546 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
1548 } else if (refParamIdx != -1) {
1549 // the first element of an argument composite location matches with one of paramtere
1550 // composite locations
1552 System.out.println("-------param match case=");
1554 NTuple<Descriptor> argTupleRef = mapIdxToArgTuple.get(refParamIdx);
1555 FlowNode refParamFlowNode = calleeFlowGraph.getParamFlowNode(refParamIdx);
1556 NTuple<Location> refParamLocTuple =
1557 translateToLocTuple(mdCallee, refParamFlowNode.getDescTuple());
1559 System.out.println("---------refParamLocTuple=" + refParamLocTuple
1560 + " from argTupleRef=" + argTupleRef);
1562 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1563 for (int i = 0; i < refParamLocTuple.size(); i++) {
1564 newCalleeCompLoc.addLocation(refParamLocTuple.get(i));
1566 for (int i = argTupleRef.size(); i < callerCompLoc.getSize(); i++) {
1567 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1570 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1573 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
1574 System.out.println("-----------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
1575 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
1578 CompositeLocation newCalleeCompLoc =
1579 calculateCompositeLocationFromSubGlobalGraph(mdCallee, calleeParamFlowNode);
1580 if (newCalleeCompLoc != null) {
1581 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1583 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
1587 System.out.println("-----------------calleeParamFlowNode="
1588 + calleeParamFlowNode.getCompositeLocation());
1599 private CompositeLocation calculateCompositeLocationFromSubGlobalGraph(MethodDescriptor md,
1600 FlowNode paramNode) {
1602 System.out.println("#############################################################");
1603 System.out.println("calculateCompositeLocationFromSubGlobalGraph=" + paramNode);
1605 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1606 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
1607 GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
1609 List<NTuple<Location>> prefixList = calculatePrefixList(subGlobalFlowGraph, paramGlobalNode);
1611 Location prefixLoc = paramLocTuple.get(0);
1613 Set<GlobalFlowNode> reachableNodeSet =
1614 subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
1615 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1617 // System.out.println("node=" + node + " prefixList=" + prefixList);
1619 for (int i = 0; i < prefixList.size(); i++) {
1620 NTuple<Location> curPrefix = prefixList.get(i);
1621 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1623 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1624 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1625 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1626 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1629 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1631 if (!reachableCommonPrefixSet.isEmpty()) {
1633 MethodDescriptor curPrefixFirstElementMethodDesc =
1634 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1636 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1637 (MethodDescriptor) prefixLoc.getDescriptor();
1639 // System.out.println("curPrefixFirstElementMethodDesc=" +
1640 // curPrefixFirstElementMethodDesc);
1641 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1642 // + nodePrefixLocFirstElementMethodDesc);
1644 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1645 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1646 curPrefixFirstElementMethodDesc)) {
1649 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1651 Location curPrefixLocalLoc = curPrefix.get(0);
1652 if (subGlobalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1653 // in this case, the local variable of the current prefix has already got a composite
1655 // so we just ignore the current composite location.
1657 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1658 // + " DUE TO " + curPrefix);
1662 if (!needToGenerateCompositeLocation(paramGlobalNode, curPrefix)) {
1663 System.out.println("NO NEED TO GENERATE COMP LOC to " + paramGlobalNode
1664 + " with prefix=" + curPrefix);
1668 Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
1669 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1670 System.out.println("NEED TO ASSIGN COMP LOC TO " + paramGlobalNode + " with prefix="
1672 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
1674 // makes sure that a newly generated location appears in the hierarchy graph
1675 for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
1676 Location curLoc = newCompLoc.get(compIdx);
1677 getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
1680 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1692 private int getParamIdx(CompositeLocation compLoc,
1693 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple) {
1695 // if the composite location is started with the argument descriptor
1696 // return the argument's index. o.t. return -1
1698 Set<Integer> keySet = mapIdxToArgTuple.keySet();
1699 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1700 Integer key = (Integer) iterator.next();
1701 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(key);
1702 if (argTuple.size() > 0 && translateToDescTuple(compLoc.getTuple()).startsWith(argTuple)) {
1703 System.out.println("compLoc.getTuple=" + compLoc + " is started with " + argTuple);
1704 return key.intValue();
1711 private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
1713 Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
1715 if (lastDesc instanceof FieldDescriptor) {
1716 return ((FieldDescriptor) lastDesc).getType().isPrimitive();
1717 } else if (lastDesc instanceof VarDescriptor) {
1718 return ((VarDescriptor) lastDesc).getType().isPrimitive();
1719 } else if (lastDesc instanceof InterDescriptor) {
1726 private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
1727 NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
1729 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1731 Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
1732 newCalleeCompLoc.addLocation(calleeThisLoc);
1734 // remove the base tuple from the caller
1735 // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
1736 // ,which is relative to the 'this' variable, <THIS,...>
1737 for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
1738 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1741 return newCalleeCompLoc;
1745 private void calculateGlobalValueFlowCompositeLocation() {
1747 System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
1748 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1749 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1751 Set<Location> calculatedPrefixSet = new HashSet<Location>();
1753 Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
1755 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1756 GlobalFlowNode node = (GlobalFlowNode) iterator.next();
1758 Location prefixLoc = node.getLocTuple().get(0);
1760 if (calculatedPrefixSet.contains(prefixLoc)) {
1761 // the prefix loc has been already assigned to a composite location
1765 calculatedPrefixSet.add(prefixLoc);
1767 // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
1768 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
1770 Set<GlobalFlowNode> reachableNodeSet =
1771 globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1772 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1774 // System.out.println("node=" + node + " prefixList=" + prefixList);
1775 System.out.println("---prefixList=" + prefixList);
1777 nextprefix: for (int i = 0; i < prefixList.size(); i++) {
1778 NTuple<Location> curPrefix = prefixList.get(i);
1779 System.out.println("---curPrefix=" + curPrefix);
1780 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1782 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1783 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1784 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1785 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1788 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1790 if (!reachableCommonPrefixSet.isEmpty()) {
1792 MethodDescriptor curPrefixFirstElementMethodDesc =
1793 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1795 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1796 (MethodDescriptor) prefixLoc.getDescriptor();
1798 // System.out.println("curPrefixFirstElementMethodDesc=" +
1799 // curPrefixFirstElementMethodDesc);
1800 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1801 // + nodePrefixLocFirstElementMethodDesc);
1803 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1804 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1805 curPrefixFirstElementMethodDesc)) {
1808 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1810 Location curPrefixLocalLoc = curPrefix.get(0);
1811 if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1812 // in this case, the local variable of the current prefix has already got a composite
1814 // so we just ignore the current composite location.
1816 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1817 // + " DUE TO " + curPrefix);
1822 if (!needToGenerateCompositeLocation(node, curPrefix)) {
1823 System.out.println("NO NEED TO GENERATE COMP LOC to " + node + " with prefix="
1825 // System.out.println("prefixList=" + prefixList);
1826 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1827 continue nextprefix;
1830 Location targetLocalLoc = node.getLocTuple().get(0);
1831 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1832 System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
1833 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc="
1835 globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1850 private boolean checkFlowNodeReturnThisField(MethodDescriptor md) {
1852 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1853 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1855 FlowGraph flowGraph = getFlowGraph(md);
1857 ClassDescriptor enclosingDesc = getClassTypeDescriptor(md.getThis());
1858 if (enclosingDesc == null) {
1863 Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
1864 Set<GlobalFlowNode> globalReturnNodeSet = new HashSet<GlobalFlowNode>();
1865 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1866 FlowNode flowNode = (FlowNode) iterator.next();
1867 NTuple<Location> locTuple = translateToLocTuple(md, flowNode.getDescTuple());
1868 GlobalFlowNode globalReturnNode = globalFlowGraph.getFlowNode(locTuple);
1869 globalReturnNodeSet.add(globalReturnNode);
1871 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, globalReturnNode);
1872 for (int i = 0; i < prefixList.size(); i++) {
1873 NTuple<Location> curPrefix = prefixList.get(i);
1874 ClassDescriptor cd =
1875 getClassTypeDescriptor(curPrefix.get(curPrefix.size() - 1).getLocDescriptor());
1876 if (cd != null && cd.equals(enclosingDesc)) {
1884 if (count == returnNodeSet.size()) {
1885 // in this case, all return nodes in the method returns values coming from a location that
1886 // starts with "this"
1888 System.out.println("$$$SET RETURN LOC TRUE=" + md);
1889 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.TRUE);
1891 // NameDescriptor returnLocDesc = new NameDescriptor("RLOC" + (locSeed++));
1892 // NTuple<Descriptor> rDescTuple = new NTuple<Descriptor>();
1893 // rDescTuple.add(md.getThis());
1894 // rDescTuple.add(returnLocDesc);
1896 // for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1897 // FlowNode rnode = (FlowNode) iterator.next();
1898 // flowGraph.addValueFlowEdge(rnode.getDescTuple(), rDescTuple);
1901 // getMethodSummary(md).setRETURNLoc(new CompositeLocation(translateToLocTuple(md,
1905 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.FALSE);
1908 return mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1912 private boolean needToGenerateCompositeLocation(GlobalFlowNode node, NTuple<Location> curPrefix) {
1913 // return true if there is a path between a node to which we want to give a composite location
1914 // and nodes which start with curPrefix
1916 System.out.println("---needToGenerateCompositeLocation curPrefix=" + curPrefix);
1918 Location targetLocalLoc = node.getLocTuple().get(0);
1920 MethodDescriptor md = (MethodDescriptor) targetLocalLoc.getDescriptor();
1921 FlowGraph flowGraph = getFlowGraph(md);
1923 FlowNode flowNode = flowGraph.getFlowNode(node.getDescTuple());
1924 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
1926 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
1927 for (Iterator iterator = paramNodeSet.iterator(); iterator.hasNext();) {
1928 FlowNode paramFlowNode = (FlowNode) iterator.next();
1929 if (curPrefix.startsWith(translateToLocTuple(md, paramFlowNode.getDescTuple()))) {
1934 if (targetLocalLoc.getLocDescriptor() instanceof InterDescriptor) {
1935 Pair<MethodInvokeNode, Integer> pair =
1936 ((InterDescriptor) targetLocalLoc.getLocDescriptor()).getMethodArgIdxPair();
1939 System.out.println("$$$TARGETLOCALLOC HOLDER=" + targetLocalLoc);
1941 MethodInvokeNode min = pair.getFirst();
1942 Integer paramIdx = pair.getSecond();
1943 MethodDescriptor mdCallee = min.getMethod();
1945 FlowNode paramNode = getFlowGraph(mdCallee).getParamFlowNode(paramIdx);
1946 if (checkNodeReachToReturnNode(mdCallee, paramNode)) {
1954 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1955 Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1957 if (!md.isStatic()) {
1958 ClassDescriptor currentMethodThisType = getClassTypeDescriptor(md.getThis());
1959 for (int i = 0; i < curPrefix.size(); i++) {
1960 ClassDescriptor prefixType = getClassTypeDescriptor(curPrefix.get(i).getLocDescriptor());
1961 if (prefixType != null && prefixType.equals(currentMethodThisType)) {
1962 System.out.println("PREFIX TYPE MATCHES WITH=" + currentMethodThisType);
1964 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1965 boolean hasCompReturnLocWithThis =
1966 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1967 if (hasCompReturnLocWithThis) {
1968 if (checkNodeReachToReturnNode(md, flowNode)) {
1974 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1975 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1976 if (subGlobalReachalbeNode.getLocTuple().get(0).getLocDescriptor().equals(md.getThis())) {
1977 System.out.println("PREFIX FOUND=" + subGlobalReachalbeNode);
1985 Location lastLocationOfPrefix = curPrefix.get(curPrefix.size() - 1);
1986 // check whether prefix appears in the list of parameters
1987 Set<MethodInvokeNode> minSet = mapMethodDescToMethodInvokeNodeSet.get(md);
1988 System.out.println("$$$md=" + md + " minSet=" + minSet);
1989 if (minSet == null) {
1992 found: for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1993 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1994 Map<Integer, NTuple<Descriptor>> map = mapMethodInvokeNodeToArgIdxMap.get(min);
1995 Set<Integer> keySet = map.keySet();
1996 // System.out.println("min=" + min.printNode(0));
1998 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1999 Integer argIdx = (Integer) iterator2.next();
2000 NTuple<Descriptor> argTuple = map.get(argIdx);
2002 if (!(!md.isStatic() && argIdx == 0)) {
2003 // if the argTuple is empty, we don't need to do with anything(LITERAL CASE).
2004 if (argTuple.size() > 0
2005 && argTuple.get(argTuple.size() - 1).equals(lastLocationOfPrefix.getLocDescriptor())) {
2006 NTuple<Location> locTuple =
2007 translateToLocTuple(md, flowGraph.getParamFlowNode(argIdx).getDescTuple());
2008 lastLocationOfPrefix = locTuple.get(0);
2009 System.out.println("ARG CASE=" + locTuple);
2010 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
2011 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
2012 // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
2013 NTuple<Location> globalReachlocTuple = subGlobalReachalbeNode.getLocTuple();
2014 for (int i = 0; i < globalReachlocTuple.size(); i++) {
2015 if (globalReachlocTuple.get(i).equals(lastLocationOfPrefix)) {
2016 System.out.println("ARG " + argTuple + " IS MATCHED WITH="
2017 + lastLocationOfPrefix);
2030 private boolean checkNodeReachToReturnNode(MethodDescriptor md, FlowNode node) {
2032 FlowGraph flowGraph = getFlowGraph(md);
2033 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(node);
2034 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
2035 boolean hasCompReturnLocWithThis =
2036 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
2038 if (hasCompReturnLocWithThis) {
2039 for (Iterator iterator = flowGraph.getReturnNodeSet().iterator(); iterator.hasNext();) {
2040 FlowNode returnFlowNode = (FlowNode) iterator.next();
2041 if (reachableSet.contains(returnFlowNode)) {
2050 private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
2051 CompositeLocation newCompLoc = compLocPrefix.clone();
2052 NTuple<Location> locTuple = node.getLocTuple();
2053 for (int i = 1; i < locTuple.size(); i++) {
2054 newCompLoc.addLocation(locTuple.get(i));
2056 node.setInferCompositeLocation(newCompLoc);
2059 private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
2061 System.out.println("\n##### calculatePrefixList node=" + node);
2063 Set<GlobalFlowNode> incomingNodeSetPrefix =
2064 graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
2065 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
2067 Set<GlobalFlowNode> reachableNodeSetPrefix =
2068 graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
2069 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
2071 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
2073 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
2074 GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
2075 NTuple<Location> inNodeTuple = inNode.getLocTuple();
2077 if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
2078 || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
2082 for (int i = 1; i < inNodeTuple.size(); i++) {
2083 NTuple<Location> prefix = inNodeTuple.subList(0, i);
2084 if (!prefixList.contains(prefix)) {
2085 prefixList.add(prefix);
2090 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
2091 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
2092 int s0 = arg0.size();
2093 int s1 = arg1.size();
2096 } else if (s0 == s1) {
2108 private CompositeLocation calculateCompositeLocationFromFlowGraph(MethodDescriptor md,
2111 System.out.println("#############################################################");
2112 System.out.println("calculateCompositeLocationFromFlowGraph=" + node);
2114 FlowGraph flowGraph = getFlowGraph(md);
2115 // NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
2116 // GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
2118 List<NTuple<Location>> prefixList = calculatePrefixListFlowGraph(flowGraph, node);
2120 // Set<GlobalFlowNode> reachableNodeSet =
2121 // subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
2123 Set<FlowNode> reachableNodeSet =
2124 flowGraph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
2126 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
2128 // System.out.println("node=" + node + " prefixList=" + prefixList);
2130 for (int i = 0; i < prefixList.size(); i++) {
2131 NTuple<Location> curPrefix = prefixList.get(i);
2132 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
2134 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
2135 FlowNode reachNode = (FlowNode) iterator2.next();
2136 NTuple<Location> reachLocTuple = translateToLocTuple(md, reachNode.getCurrentDescTuple());
2137 if (reachLocTuple.startsWith(curPrefix)) {
2138 reachableCommonPrefixSet.add(reachLocTuple);
2141 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
2143 if (!reachableCommonPrefixSet.isEmpty()) {
2145 MethodDescriptor curPrefixFirstElementMethodDesc =
2146 (MethodDescriptor) curPrefix.get(0).getDescriptor();
2148 Location curPrefixLocalLoc = curPrefix.get(0);
2150 Location targetLocalLoc = new Location(md, node.getDescTuple().get(0));
2151 // Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
2153 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
2154 System.out.println("NEED2ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
2155 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
2157 node.setCompositeLocation(newCompLoc);
2167 private List<NTuple<Location>> calculatePrefixListFlowGraph(FlowGraph graph, FlowNode node) {
2169 System.out.println("\n##### calculatePrefixList node=" + node);
2171 MethodDescriptor md = graph.getMethodDescriptor();
2172 Set<FlowNode> incomingNodeSetPrefix =
2173 graph.getIncomingNodeSetByPrefix(node.getDescTuple().get(0));
2174 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
2176 Set<FlowNode> reachableNodeSetPrefix =
2177 graph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
2178 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
2180 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
2182 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
2183 FlowNode inNode = (FlowNode) iterator.next();
2184 NTuple<Location> inNodeTuple = translateToLocTuple(md, inNode.getCurrentDescTuple());
2186 // if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
2187 // || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
2191 for (int i = 1; i < inNodeTuple.size(); i++) {
2192 NTuple<Location> prefix = inNodeTuple.subList(0, i);
2193 if (!prefixList.contains(prefix)) {
2194 prefixList.add(prefix);
2199 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
2200 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
2201 int s0 = arg0.size();
2202 int s1 = arg1.size();
2205 } else if (s0 == s1) {
2217 private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
2219 MethodDescriptor md = flowGraph.getMethodDescriptor();
2221 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(md);
2223 // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
2224 Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
2226 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
2228 FlowEdge edge = (FlowEdge) iterator.next();
2229 NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
2230 NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
2232 if (flowGraph.getFlowNode(srcDescTuple) instanceof FlowReturnNode
2233 || flowGraph.getFlowNode(dstDescTuple) instanceof FlowReturnNode) {
2237 // here only keep the first element(method location) of the descriptor
2239 NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
2240 NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
2242 globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
2249 private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
2251 NTuple<Location> locTuple = new NTuple<Location>();
2253 Descriptor enclosingDesc = md;
2254 for (int i = 0; i < descTuple.size(); i++) {
2255 Descriptor desc = descTuple.get(i);
2257 Location loc = new Location(enclosingDesc, desc);
2260 if (desc instanceof VarDescriptor) {
2261 enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
2262 } else if (desc instanceof FieldDescriptor) {
2263 enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
2265 enclosingDesc = desc;
2274 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller) {
2276 // the transformation for a call site propagates flows through parameters
2277 // if the method is virtual, it also grab all relations from any possible
2280 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
2282 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
2283 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
2284 MethodDescriptor mdCallee = min.getMethod();
2285 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2286 if (mdCallee.isStatic()) {
2287 setPossibleCallees.add(mdCallee);
2289 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
2290 // removes method descriptors that are not invoked by the caller
2291 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
2292 setPossibleCallees.addAll(calleeSet);
2295 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
2296 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
2297 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
2304 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
2305 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
2307 System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
2308 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
2309 Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
2311 System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
2312 + mapMethodInvokeNodeToArgIdxMap.get(min));
2314 Set<Integer> keySet = mapIdxToArg.keySet();
2315 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2316 Integer idx = (Integer) iterator.next();
2317 NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
2318 if (argDescTuple.size() > 0) {
2319 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
2320 NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
2321 NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
2322 System.out.println("-------paramDescTuple=" + paramDescTuple + "->argDescTuple="
2324 addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
2328 // addValueFlowBetweenParametersToCaller(min, mdCaller, possibleMdCallee);
2330 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2331 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
2332 Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
2333 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
2334 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
2335 addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
2338 System.out.println("$$$GLOBAL PC LOC ADD=" + mdCaller);
2339 Set<NTuple<Location>> pcLocTupleSet = mapMethodInvokeNodeToPCLocTupleSet.get(min);
2340 System.out.println("---pcLocTupleSet=" + pcLocTupleSet);
2341 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
2342 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
2343 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
2344 if (calleeNode.isParamNodeWithIncomingFlows()) {
2345 System.out.println("calleeNode.getLocTuple()" + calleeNode.getLocTuple());
2346 NTuple<Location> callerSrcNodeLocTuple =
2347 translateToCallerLocTuple(min, possibleMdCallee, mdCaller, calleeNode.getLocTuple());
2348 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
2349 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
2350 for (Iterator iterator2 = pcLocTupleSet.iterator(); iterator2.hasNext();) {
2351 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator2.next();
2353 callerSubGlobalGraph.addValueFlowEdge(pcLocTuple, callerSrcNodeLocTuple);
2362 private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
2363 MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
2365 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
2366 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
2368 System.out.println("$addValueFlowFromCalleeNode calleeSrcNode=" + calleeSrcNode);
2370 NTuple<Location> callerSrcNodeLocTuple =
2371 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
2372 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
2374 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
2376 Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
2378 for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
2379 GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
2380 NTuple<Location> callerDstNodeLocTuple =
2381 translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
2382 // System.out.println("outNode=" + outNode + " callerDstNodeLocTuple="
2383 // + callerDstNodeLocTuple);
2384 if (callerSrcNodeLocTuple != null && callerDstNodeLocTuple != null
2385 && callerSrcNodeLocTuple.size() > 0 && callerDstNodeLocTuple.size() > 0) {
2386 callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
2393 private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
2394 MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
2395 // this method will return the same nodeLocTuple if the corresponding argument is literal
2398 // System.out.println("translateToCallerLocTuple=" + nodeLocTuple);
2400 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
2401 NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
2402 if (calleeFlowGraph.isParameter(nodeDescTuple)) {
2403 int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
2404 NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
2406 // if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
2407 // // the type of argument is primitive.
2408 // return nodeLocTuple.clone();
2410 // System.out.println("paramIdx=" + paramIdx + " argDescTuple=" + argDescTuple + " from min="
2411 // + min.printNode(0));
2412 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
2414 NTuple<Location> callerLocTuple = new NTuple<Location>();
2416 callerLocTuple.addAll(argLocTuple);
2417 for (int i = 1; i < nodeLocTuple.size(); i++) {
2418 callerLocTuple.add(nodeLocTuple.get(i));
2420 return callerLocTuple;
2422 return nodeLocTuple.clone();
2427 public static boolean isPrimitive(Descriptor desc) {
2429 if (desc instanceof FieldDescriptor) {
2430 return ((FieldDescriptor) desc).getType().isPrimitive();
2431 } else if (desc instanceof VarDescriptor) {
2432 return ((VarDescriptor) desc).getType().isPrimitive();
2433 } else if (desc instanceof InterDescriptor) {
2440 public static boolean isReference(Descriptor desc) {
2442 if (desc instanceof FieldDescriptor) {
2444 TypeDescriptor type = ((FieldDescriptor) desc).getType();
2445 if (type.isArray()) {
2446 return !type.isPrimitive();
2448 return type.isPtr();
2451 } else if (desc instanceof VarDescriptor) {
2452 TypeDescriptor type = ((VarDescriptor) desc).getType();
2453 if (type.isArray()) {
2454 return !type.isPrimitive();
2456 return type.isPtr();
2463 private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
2465 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
2466 for (int i = 0; i < locTuple.size(); i++) {
2467 descTuple.add(locTuple.get(i).getLocDescriptor());
2473 public LocationSummary getLocationSummary(Descriptor d) {
2474 if (!mapDescToLocationSummary.containsKey(d)) {
2475 if (d instanceof MethodDescriptor) {
2476 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
2477 } else if (d instanceof ClassDescriptor) {
2478 mapDescToLocationSummary.put(d, new FieldSummary());
2481 return mapDescToLocationSummary.get(d);
2484 private void generateMethodSummary() {
2486 Set<MethodDescriptor> keySet = md2lattice.keySet();
2487 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2488 MethodDescriptor md = (MethodDescriptor) iterator.next();
2490 System.out.println("\nSSJAVA: generate method summary: " + md);
2492 FlowGraph flowGraph = getFlowGraph(md);
2493 if (flowGraph == null) {
2496 MethodSummary methodSummary = getMethodSummary(md);
2498 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
2500 // set the 'this' reference location
2501 if (!md.isStatic()) {
2502 System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
2503 methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
2506 // set the 'global' reference location if needed
2507 if (methodSummary.hasGlobalAccess()) {
2508 methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
2511 // construct a parameter mapping that maps a parameter descriptor to an
2512 // inferred composite location
2513 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
2514 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
2515 CompositeLocation inferredCompLoc =
2516 updateCompositeLocation(flowNode.getCompositeLocation());
2517 System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc + " original="
2518 + flowNode.getCompositeLocation());
2520 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
2521 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
2522 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
2529 private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2531 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2533 for (int idx = 0; idx < size; idx++) {
2534 Location loc1 = locTuple1.get(idx);
2535 Location loc2 = locTuple2.get(idx);
2537 Descriptor desc1 = loc1.getDescriptor();
2538 Descriptor desc2 = loc2.getDescriptor();
2540 if (!desc1.equals(desc2)) {
2541 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2544 Descriptor locDesc1 = loc1.getLocDescriptor();
2545 Descriptor locDesc2 = loc2.getLocDescriptor();
2547 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2549 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2550 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2552 System.out.println("---node1=" + node1 + " node2=" + node2);
2553 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2554 + hierarchyGraph.getIncomingNodeSet(node2));
2556 if (locDesc1.equals(locDesc2)) {
2558 } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
2559 && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
2571 private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2573 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2575 for (int idx = 0; idx < size; idx++) {
2576 Location loc1 = locTuple1.get(idx);
2577 Location loc2 = locTuple2.get(idx);
2579 Descriptor desc1 = loc1.getDescriptor();
2580 Descriptor desc2 = loc2.getDescriptor();
2582 if (!desc1.equals(desc2)) {
2583 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2586 Descriptor locDesc1 = loc1.getLocDescriptor();
2587 Descriptor locDesc2 = loc2.getLocDescriptor();
2589 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2591 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2592 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2594 System.out.println("---node1=" + node1 + " node2=" + node2);
2595 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2596 + hierarchyGraph.getIncomingNodeSet(node2));
2598 if (locDesc1.equals(locDesc2)) {
2600 } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
2611 private void debug_writeLattices() {
2613 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
2614 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2615 Descriptor key = (Descriptor) iterator.next();
2616 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
2617 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
2618 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
2619 if (key instanceof ClassDescriptor) {
2620 // writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
2622 } else if (key instanceof MethodDescriptor) {
2623 MethodDescriptor md = (MethodDescriptor) key;
2624 // writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
2628 LocationSummary ls = getLocationSummary(key);
2629 System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
2632 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
2633 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
2634 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2635 System.out.println("########cd=" + cd);
2636 writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
2637 cd2lattice.get(cd), "");
2638 COUNT += cd2lattice.get(cd).getKeySet().size();
2641 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
2642 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
2643 MethodDescriptor md = (MethodDescriptor) iterator.next();
2644 writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
2645 md2lattice.get(md), "");
2646 COUNT += md2lattice.get(md).getKeySet().size();
2648 System.out.println("###COUNT=" + COUNT);
2651 private void buildLattice(Descriptor desc) {
2652 System.out.println("buildLattice=" + desc);
2653 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
2655 addMapDescToSimpleLattice(desc, simpleLattice);
2657 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2658 System.out.println("\n## insertIntermediateNodesToStraightLine:"
2659 + simpleHierarchyGraph.getName());
2660 SSJavaLattice<String> lattice =
2661 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2662 lattice.removeRedundantEdges();
2664 if (desc instanceof ClassDescriptor) {
2666 cd2lattice.put((ClassDescriptor) desc, lattice);
2667 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2668 } else if (desc instanceof MethodDescriptor) {
2670 md2lattice.put((MethodDescriptor) desc, lattice);
2671 MethodDescriptor md = (MethodDescriptor) desc;
2672 ClassDescriptor cd = md.getClassDesc();
2673 // ssjava.writeLatticeDotFile(cd, md, lattice);
2678 private void buildLattice() {
2680 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
2681 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2682 Descriptor desc = (Descriptor) iterator.next();
2684 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
2686 addMapDescToSimpleLattice(desc, simpleLattice);
2688 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2689 System.out.println("\n## insertIntermediateNodesToStraightLine:"
2690 + simpleHierarchyGraph.getName());
2691 SSJavaLattice<String> lattice =
2692 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2693 lattice.removeRedundantEdges();
2695 if (desc instanceof ClassDescriptor) {
2697 cd2lattice.put((ClassDescriptor) desc, lattice);
2698 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2699 } else if (desc instanceof MethodDescriptor) {
2701 md2lattice.put((MethodDescriptor) desc, lattice);
2702 MethodDescriptor md = (MethodDescriptor) desc;
2703 ClassDescriptor cd = md.getClassDesc();
2704 // ssjava.writeLatticeDotFile(cd, md, lattice);
2711 private void buildLatticeInheritanceTree() {
2712 // DFS the inheritance tree and propagates lattice nodes/edges from the parent to children
2713 // Node<ClassDescriptor> rootNode = inheritanceTree.getRootNode();
2714 DFSBuildLatticeInheritanceTree(rootClassDescriptor);
2717 public Set<ClassDescriptor> getDirectSubClasses(ClassDescriptor parent) {
2719 System.out.println("$$$toanalyze_classDescSet=" + toanalyze_classDescSet);
2720 Set<ClassDescriptor> result = new HashSet<ClassDescriptor>();
2722 Set<ClassDescriptor> children = tu.getDirectSubClasses(parent);
2723 if (children == null) {
2724 children = new HashSet<ClassDescriptor>();
2727 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
2728 ClassDescriptor child = (ClassDescriptor) iterator.next();
2729 if (toanalyze_classDescSet.contains(child)) {
2737 private void DFSBuildLatticeInheritanceTree(ClassDescriptor cd) {
2738 // ClassDescriptor cd = node.getData();
2740 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2741 if (parentClassDesc != null) {
2742 Map<TripleItem, String> parentMap = buildLattice.getIntermediateLocMap(parentClassDesc);
2743 buildLattice.setIntermediateLocMap(cd, parentMap);
2748 for (Iterator iterator = cd.getMethods(); iterator.hasNext();) {
2749 MethodDescriptor md = (MethodDescriptor) iterator.next();
2750 if (toanalyze_methodDescList.contains(md)) {
2751 MethodDescriptor parentMethodDesc = getParentMethodDesc(md.getClassDesc(), md);
2752 if (parentMethodDesc != null) {
2753 Map<TripleItem, String> parentMap = buildLattice.getIntermediateLocMap(parentMethodDesc);
2754 buildLattice.setIntermediateLocMap(md, parentMap);
2760 // traverse children
2761 Set<ClassDescriptor> children = getDirectSubClasses(cd);
2762 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
2763 ClassDescriptor classDescriptor = (ClassDescriptor) iterator.next();
2764 if (toanalyze_classDescSet.contains(classDescriptor)) {
2765 DFSBuildLatticeInheritanceTree(classDescriptor);
2772 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
2773 mapDescriptorToSimpleLattice.put(desc, lattice);
2776 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
2777 return mapDescriptorToSimpleLattice.get(desc);
2780 private void simplifyHierarchyGraph() {
2781 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2782 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2783 Descriptor desc = (Descriptor) iterator.next();
2784 // System.out.println("SSJAVA: remove redundant edges: " + desc);
2785 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
2786 simpleHierarchyGraph.setName(desc + "_SIMPLE");
2787 simpleHierarchyGraph.removeRedundantEdges();
2788 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
2792 private void insertCombinationNodes() {
2793 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
2794 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2795 Descriptor desc = (Descriptor) iterator.next();
2796 System.out.println("\nSSJAVA: Inserting Combination Nodes:" + desc);
2797 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2798 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
2799 skeletonGraphWithCombinationNode.setName(desc + "_SC");
2801 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2802 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2803 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph,
2805 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2806 skeletonGraphWithCombinationNode.removeRedundantEdges();
2807 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
2811 private void constructSkeletonHierarchyGraph() {
2812 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2813 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2814 Descriptor desc = (Descriptor) iterator.next();
2815 System.out.println("SSJAVA: Constructing Skeleton Hierarchy Graph: " + desc);
2816 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
2817 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
2818 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
2819 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
2820 skeletonGraph.simplifyHierarchyGraph(this);
2821 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
2825 private void recurUpAccumulateInheritanceDesc(Descriptor curDesc, Set<Descriptor> set) {
2827 if (curDesc instanceof ClassDescriptor) {
2828 ClassDescriptor cd = (ClassDescriptor) curDesc;
2829 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2830 if (parentClassDesc != null && !parentClassDesc.equals(rootClassDescriptor)) {
2831 set.add(parentClassDesc);
2832 recurUpAccumulateInheritanceDesc(parentClassDesc, set);
2835 MethodDescriptor md = (MethodDescriptor) curDesc;
2836 ClassDescriptor cd = md.getClassDesc();
2839 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2840 if (parentClassDesc != null && !parentClassDesc.equals(rootClassDescriptor)) {
2842 Set<MethodDescriptor> methodDescSet =
2843 parentClassDesc.getMethodTable().getSet(md.getSymbol());
2844 for (Iterator iterator = methodDescSet.iterator(); iterator.hasNext();) {
2845 MethodDescriptor parentMethodDesc = (MethodDescriptor) iterator.next();
2846 if (parentMethodDesc.matches(md)) {
2847 set.add(parentMethodDesc);
2848 recurUpAccumulateInheritanceDesc(parentMethodDesc, set);
2857 private void recurDownAccumulateInheritanceDesc(Descriptor curDesc, Set<Descriptor> set) {
2859 if (curDesc instanceof ClassDescriptor) {
2860 ClassDescriptor cd = (ClassDescriptor) curDesc;
2861 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2862 Set<ClassDescriptor> directSubClasses = tu.getDirectSubClasses(cd);
2863 for (Iterator iterator = directSubClasses.iterator(); iterator.hasNext();) {
2864 ClassDescriptor child = (ClassDescriptor) iterator.next();
2865 recurDownAccumulateInheritanceDesc(child, set);
2868 MethodDescriptor md = (MethodDescriptor) curDesc;
2869 ClassDescriptor cd = md.getClassDesc();
2872 Set<ClassDescriptor> directSubClasses = tu.getDirectSubClasses(cd);
2873 for (Iterator iterator = directSubClasses.iterator(); iterator.hasNext();) {
2874 ClassDescriptor child = (ClassDescriptor) iterator.next();
2876 Set<MethodDescriptor> methodDescSet = child.getMethodTable().getSet(md.getSymbol());
2877 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
2878 MethodDescriptor childMethodDesc = (MethodDescriptor) iterator2.next();
2879 if (childMethodDesc.matches(md)) {
2880 set.add(childMethodDesc);
2881 recurDownAccumulateInheritanceDesc(childMethodDesc, set);
2890 private void accumulateInheritanceDesc(Descriptor curDesc, Set<Descriptor> set) {
2892 recurUpAccumulateInheritanceDesc(curDesc, set);
2893 recurDownAccumulateInheritanceDesc(curDesc, set);
2897 public boolean isValidMergeInheritanceCheck(Descriptor desc, Set<HNode> mergeSet) {
2899 // set up inheritance chain set...
2900 Set<Descriptor> inheritanceDescSet = new HashSet<Descriptor>();
2901 recurUpAccumulateInheritanceDesc(desc, inheritanceDescSet);
2903 nextgraph: for (Iterator iterator = inheritanceDescSet.iterator(); iterator.hasNext();) {
2904 Descriptor inheritDesc = (Descriptor) iterator.next();
2906 if (!desc.equals(inheritDesc)) {
2907 HierarchyGraph graph = getSkeletonCombinationHierarchyGraph(inheritDesc);
2909 // first check whether this graph includes all elements of the merge set
2910 for (Iterator iterator2 = mergeSet.iterator(); iterator2.hasNext();) {
2911 HNode node = (HNode) iterator2.next();
2912 if (!graph.contains(node)) {
2917 HNode firstNode = mergeSet.iterator().next();
2919 Set<HNode> incomingNode = graph.getIncomingNodeSet(firstNode);
2920 Set<HNode> outgoingNode = graph.getOutgoingNodeSet(firstNode);
2922 for (Iterator iterator2 = mergeSet.iterator(); iterator2.hasNext();) {
2923 HNode node = (HNode) iterator2.next();
2925 if (!graph.getIncomingNodeSet(node).equals(incomingNode)
2926 || !graph.getOutgoingNodeSet(node).equals(outgoingNode)) {
2938 private void debug_writeHierarchyDotFiles() {
2940 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2941 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2942 Descriptor desc = (Descriptor) iterator.next();
2943 getHierarchyGraph(desc).writeGraph();
2948 private void debug_writeSimpleHierarchyDotFiles() {
2950 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2951 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2952 Descriptor desc = (Descriptor) iterator.next();
2953 getHierarchyGraph(desc).writeGraph();
2954 getSimpleHierarchyGraph(desc).writeGraph();
2955 getSimpleHierarchyGraph(desc).writeGraph(true);
2960 private void debug_writeSkeletonHierarchyDotFiles() {
2962 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2963 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2964 Descriptor desc = (Descriptor) iterator.next();
2965 getSkeletonHierarchyGraph(desc).writeGraph();
2970 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
2972 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2973 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2974 Descriptor desc = (Descriptor) iterator.next();
2975 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
2980 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
2981 return mapDescriptorToSimpleHierarchyGraph.get(d);
2984 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
2985 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
2986 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2988 return mapDescriptorToSkeletonHierarchyGraph.get(d);
2991 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
2992 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
2993 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2995 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
2998 private void constructHierarchyGraph() {
3000 LinkedList<MethodDescriptor> methodDescList =
3001 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3003 while (!methodDescList.isEmpty()) {
3004 MethodDescriptor md = methodDescList.removeLast();
3005 if (state.SSJAVADEBUG) {
3006 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
3007 System.out.println();
3008 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
3009 constructHierarchyGraph(md, hierarchyGraph);
3010 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
3016 while (!toAnalyzeIsEmpty()) {
3017 ClassDescriptor cd = toAnalyzeNext();
3018 HierarchyGraph graph = getHierarchyGraph(cd);
3019 for (Iterator iter = cd.getFields(); iter.hasNext();) {
3020 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
3021 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
3022 graph.getHNode(fieldDesc);
3027 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
3028 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
3029 Descriptor key = (Descriptor) iterator.next();
3030 HierarchyGraph graph = getHierarchyGraph(key);
3032 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
3033 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
3034 HNode node = (HNode) iterator2.next();
3035 if (!node.isSkeleton() && !node.isCombinationNode()) {
3036 if (graph.getIncomingNodeSet(node).size() == 0) {
3037 nodeToBeConnected.add(node);
3042 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
3043 HNode node = (HNode) iterator2.next();
3044 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
3045 graph.addEdge(graph.getHNode(TOPDESC), node);
3052 private void constructHierarchyGraph2() {
3054 // do fixed-point analysis
3056 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
3058 // Collections.sort(descriptorListToAnalyze, new
3059 // Comparator<MethodDescriptor>() {
3060 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
3061 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
3065 // current descriptors to visit in fixed-point interprocedural analysis,
3066 // prioritized by dependency in the call graph
3067 methodDescriptorsToVisitStack.clear();
3069 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
3070 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
3072 while (!descriptorListToAnalyze.isEmpty()) {
3073 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
3074 methodDescriptorsToVisitStack.add(md);
3077 // analyze scheduled methods until there are no more to visit
3078 while (!methodDescriptorsToVisitStack.isEmpty()) {
3079 // start to analyze leaf node
3080 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
3082 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
3083 // MethodSummary methodSummary = new MethodSummary(md);
3085 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
3086 // curMethodInfo = methodInfo;
3088 System.out.println();
3089 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
3091 constructHierarchyGraph(md, hierarchyGraph);
3093 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
3094 // MethodSummary prevMethodSummary = getMethodSummary(md);
3096 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
3098 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
3099 // mapDescToLocationSummary.put(md, methodSummary);
3101 // results for callee changed, so enqueue dependents caller for
3103 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
3104 while (depsItr.hasNext()) {
3105 MethodDescriptor methodNext = depsItr.next();
3106 if (!methodDescriptorsToVisitStack.contains(methodNext)
3107 && methodDescriptorToVistSet.contains(methodNext)) {
3108 methodDescriptorsToVisitStack.add(methodNext);
3117 while (!toAnalyzeIsEmpty()) {
3118 ClassDescriptor cd = toAnalyzeNext();
3119 HierarchyGraph graph = getHierarchyGraph(cd);
3120 for (Iterator iter = cd.getFields(); iter.hasNext();) {
3121 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
3122 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
3123 graph.getHNode(fieldDesc);
3128 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
3129 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
3130 Descriptor key = (Descriptor) iterator.next();
3131 HierarchyGraph graph = getHierarchyGraph(key);
3133 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
3134 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
3135 HNode node = (HNode) iterator2.next();
3136 if (!node.isSkeleton() && !node.isCombinationNode()) {
3137 if (graph.getIncomingNodeSet(node).size() == 0) {
3138 nodeToBeConnected.add(node);
3143 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
3144 HNode node = (HNode) iterator2.next();
3145 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
3146 graph.addEdge(graph.getHNode(TOPDESC), node);
3153 private HierarchyGraph getHierarchyGraph(Descriptor d) {
3154 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
3155 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
3157 return mapDescriptorToHierarchyGraph.get(d);
3160 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
3162 // visit each node of method flow graph
3163 FlowGraph fg = getFlowGraph(md);
3164 // Set<FlowNode> nodeSet = fg.getNodeSet();
3166 Set<FlowEdge> edgeSet = fg.getEdgeSet();
3168 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
3169 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
3170 Descriptor desc = (Descriptor) iterator.next();
3171 methodGraph.getHNode(desc).setSkeleton(true);
3174 // for the method lattice, we need to look at the first element of
3175 // NTuple<Descriptor>
3176 boolean hasGlobalAccess = false;
3177 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3178 // FlowNode originalSrcNode = (FlowNode) iterator.next();
3179 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
3180 FlowEdge edge = (FlowEdge) iterator.next();
3182 FlowNode originalSrcNode = fg.getFlowNode(edge.getInitTuple());
3183 Set<FlowNode> sourceNodeSet = new HashSet<FlowNode>();
3184 if (originalSrcNode instanceof FlowReturnNode) {
3185 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
3186 System.out.println("rnode=" + rnode);
3187 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
3188 for (Iterator iterator2 = tupleSet.iterator(); iterator2.hasNext();) {
3189 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator2.next();
3190 sourceNodeSet.add(fg.getFlowNode(nTuple));
3191 System.out.println("&&&SOURCE fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
3194 sourceNodeSet.add(originalSrcNode);
3197 // System.out.println("---sourceNodeSet=" + sourceNodeSet + " from originalSrcNode="
3198 // + originalSrcNode);
3200 for (Iterator iterator3 = sourceNodeSet.iterator(); iterator3.hasNext();) {
3201 FlowNode srcNode = (FlowNode) iterator3.next();
3203 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
3204 Descriptor srcLocalDesc = srcNodeTuple.get(0);
3206 if (srcLocalDesc instanceof InterDescriptor
3207 && ((InterDescriptor) srcLocalDesc).getMethodArgIdxPair() != null) {
3209 if (srcNode.getCompositeLocation() == null) {
3214 // if the srcNode is started with the global descriptor
3215 // need to set as a skeleton node
3216 if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
3217 hasGlobalAccess = true;
3220 // Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(originalSrcNode);
3221 // for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3222 // FlowEdge outEdge = (FlowEdge) iterator2.next();
3223 // FlowNode originalDstNode = outEdge.getDst();
3224 FlowNode originalDstNode = fg.getFlowNode(edge.getEndTuple());
3226 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
3227 if (originalDstNode instanceof FlowReturnNode) {
3228 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
3229 // System.out.println("\n-returnNode=" + rnode);
3230 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
3231 for (Iterator iterator4 = tupleSet.iterator(); iterator4.hasNext();) {
3232 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator4.next();
3233 dstNodeSet.add(fg.getFlowNode(nTuple));
3234 System.out.println("&&&DST fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
3237 dstNodeSet.add(originalDstNode);
3239 // System.out.println("---dstNodeSet=" + dstNodeSet);
3240 for (Iterator iterator4 = dstNodeSet.iterator(); iterator4.hasNext();) {
3241 FlowNode dstNode = (FlowNode) iterator4.next();
3243 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
3244 Descriptor dstLocalDesc = dstNodeTuple.get(0);
3246 if (dstLocalDesc instanceof InterDescriptor
3247 && ((InterDescriptor) dstLocalDesc).getMethodArgIdxPair() != null) {
3248 if (dstNode.getCompositeLocation() == null) {
3249 System.out.println("%%%%%%%%%%%%%SKIP=" + dstNode);
3254 // if (outEdge.getInitTuple().equals(srcNodeTuple)
3255 // && outEdge.getEndTuple().equals(dstNodeTuple)) {
3257 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
3258 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
3260 // //////////////////////////
3261 // inheritance check
3262 if (mapMethodDescToHighestOverriddenMethodDesc.containsKey(md)) {
3264 MethodDescriptor highestOverriddenMethodDesc =
3265 mapMethodDescToHighestOverriddenMethodDesc.get(md);
3267 if (srcCurTuple.get(srcCurTuple.size() - 1).getSymbol().startsWith(PCLOC)) {
3271 // //////////////////////////
3273 System.out.println("-srcCurTuple=" + srcCurTuple + " dstCurTuple=" + dstCurTuple
3274 + " srcNode=" + srcNode + " dstNode=" + dstNode);
3276 // srcCurTuple = translateBaseTuple(srcNode, srcCurTuple);
3277 // dstCurTuple = translateBaseTuple(dstNode, dstCurTuple);
3279 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
3280 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
3282 // value flows between fields
3283 Descriptor desc = srcCurTuple.get(0);
3284 ClassDescriptor classDesc;
3286 if (desc.equals(GLOBALDESC)) {
3287 classDesc = md.getClassDesc();
3289 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
3290 classDesc = varDesc.getType().getClassDesc();
3292 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
3294 } else if ((srcCurTuple.size() == 1 && dstCurTuple.size() == 1)
3295 || ((srcCurTuple.size() > 1 || dstCurTuple.size() > 1) && !srcCurTuple.get(0).equals(
3296 dstCurTuple.get(0)))) {
3298 // value flow between a primitive local var - a primitive local var or local var -
3301 Descriptor srcDesc = srcCurTuple.get(0);
3302 Descriptor dstDesc = dstCurTuple.get(0);
3304 methodGraph.addEdge(srcDesc, dstDesc);
3306 if (fg.isParamDesc(srcDesc)) {
3307 methodGraph.setParamHNode(srcDesc);
3309 if (fg.isParamDesc(dstDesc)) {
3310 methodGraph.setParamHNode(dstDesc);
3324 // If the method accesses static fields
3325 // set hasGloabalAccess true in the method summary.
3326 if (hasGlobalAccess) {
3327 getMethodSummary(md).setHasGlobalAccess();
3329 methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
3331 if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
3332 // if the current method contains the event loop
3333 // we need to set all nodes of the hierarchy graph as a skeleton node
3334 Set<HNode> hnodeSet = methodGraph.getNodeSet();
3335 for (Iterator iterator = hnodeSet.iterator(); iterator.hasNext();) {
3336 HNode hnode = (HNode) iterator.next();
3337 hnode.setSkeleton(true);
3343 private NTuple<Descriptor> translateBaseTuple(FlowNode flowNode, NTuple<Descriptor> inTuple) {
3345 if (flowNode.getBaseTuple() != null) {
3347 NTuple<Descriptor> translatedTuple = new NTuple<Descriptor>();
3349 NTuple<Descriptor> baseTuple = flowNode.getBaseTuple();
3351 for (int i = 0; i < baseTuple.size(); i++) {
3352 translatedTuple.add(baseTuple.get(i));
3355 for (int i = 1; i < inTuple.size(); i++) {
3356 translatedTuple.add(inTuple.get(i));
3359 System.out.println("------TRANSLATED " + inTuple + " -> " + translatedTuple);
3360 return translatedTuple;
3368 private MethodSummary getMethodSummary(MethodDescriptor md) {
3369 if (!mapDescToLocationSummary.containsKey(md)) {
3370 mapDescToLocationSummary.put(md, new MethodSummary(md));
3372 return (MethodSummary) mapDescToLocationSummary.get(md);
3375 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
3377 String classSymbol = cd.getSymbol();
3378 int idx = classSymbol.lastIndexOf("$");
3380 classSymbol = classSymbol.substring(idx + 1);
3383 String pattern = "class " + classSymbol + " ";
3384 if (strLine.indexOf(pattern) != -1) {
3385 mapDescToDefinitionLine.put(cd, lineNum);
3389 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
3391 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
3392 MethodDescriptor md = (MethodDescriptor) iterator.next();
3393 String pattern = md.getMethodDeclaration();
3394 if (strLine.indexOf(pattern) != -1) {
3395 mapDescToDefinitionLine.put(md, lineNum);
3396 methodSet.remove(md);
3403 private void readOriginalSourceFiles() {
3405 SymbolTable classtable = state.getClassSymbolTable();
3407 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
3408 classDescSet.addAll(classtable.getValueSet());
3411 // inefficient implement. it may re-visit the same file if the file
3412 // contains more than one class definitions.
3413 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
3414 ClassDescriptor cd = (ClassDescriptor) iterator.next();
3416 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
3417 methodSet.addAll(cd.getMethodTable().getValueSet());
3419 String sourceFileName = cd.getSourceFileName();
3420 Vector<String> lineVec = new Vector<String>();
3422 mapFileNameToLineVector.put(sourceFileName, lineVec);
3424 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
3427 lineVec.add(""); // the index is started from 1.
3428 while ((strLine = in.readLine()) != null) {
3429 lineVec.add(lineNum, strLine);
3430 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
3431 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
3437 } catch (IOException e) {
3438 e.printStackTrace();
3443 private String generateLatticeDefinition(Descriptor desc) {
3445 Set<String> sharedLocSet = new HashSet<String>();
3447 SSJavaLattice<String> lattice = getLattice(desc);
3448 String rtr = "@LATTICE(\"";
3450 Map<String, Set<String>> map = lattice.getTable();
3451 Set<String> keySet = map.keySet();
3452 boolean first = true;
3453 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
3454 String key = (String) iterator.next();
3455 if (!key.equals(lattice.getTopItem())) {
3456 Set<String> connectedSet = map.get(key);
3458 if (connectedSet.size() == 1) {
3459 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
3467 if (lattice.isSharedLoc(key)) {
3468 rtr += "," + key + "*";
3473 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
3474 String loc = (String) iterator2.next();
3475 if (!loc.equals(lattice.getBottomItem())) {
3482 rtr += loc + "<" + key;
3483 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
3484 rtr += "," + key + "*";
3485 sharedLocSet.add(key);
3487 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
3488 rtr += "," + loc + "*";
3489 sharedLocSet.add(loc);
3497 if (desc instanceof MethodDescriptor) {
3498 System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
3500 MethodDescriptor md = (MethodDescriptor) desc;
3501 MethodSummary methodSummary = getMethodSummary(md);
3503 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
3504 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc) && returnType != null
3505 && (!returnType.isVoid())) {
3506 CompositeLocation returnLoc = methodSummary.getRETURNLoc();
3507 if (returnLoc.getSize() == 1) {
3508 String returnLocStr = generateLocationAnnoatation(methodSummary.getRETURNLoc());
3509 if (rtr.indexOf(returnLocStr) == -1) {
3510 rtr += "," + returnLocStr;
3516 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
3517 if (returnType != null && (!returnType.isVoid())) {
3519 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
3522 CompositeLocation pcLoc = methodSummary.getPCLoc();
3523 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
3524 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
3528 if (!md.isStatic()) {
3529 rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
3531 rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
3540 private void generateAnnoatedCode() {
3542 readOriginalSourceFiles();
3545 while (!toAnalyzeIsEmpty()) {
3546 ClassDescriptor cd = toAnalyzeNext();
3548 setupToAnalazeMethod(cd);
3550 String sourceFileName = cd.getSourceFileName();
3552 if (cd.isInterface()) {
3556 int classDefLine = mapDescToDefinitionLine.get(cd);
3557 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
3559 LocationSummary fieldLocSummary = getLocationSummary(cd);
3561 String fieldLatticeDefStr = generateLatticeDefinition(cd);
3562 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
3563 sourceVec.set(classDefLine, annoatedSrc);
3565 // generate annotations for field declarations
3566 // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
3567 Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
3569 for (Iterator iter = cd.getFields(); iter.hasNext();) {
3570 FieldDescriptor fd = (FieldDescriptor) iter.next();
3572 String locAnnotationStr;
3573 // CompositeLocation inferLoc = inferLocMap.get(fd);
3574 String locName = mapFieldNameToLocName.get(fd.getSymbol());
3576 if (locName != null) {
3577 // infer loc is null if the corresponding field is static and final
3578 // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
3579 locAnnotationStr = "@LOC(\"" + locName + "\")";
3580 int fdLineNum = fd.getLineNum();
3581 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
3582 String fieldDeclaration = fd.toString();
3583 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
3584 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
3585 sourceVec.set(fdLineNum, annoatedStr);
3590 while (!toAnalyzeMethodIsEmpty()) {
3591 MethodDescriptor md = toAnalyzeMethodNext();
3593 if (!ssjava.needTobeAnnotated(md)) {
3597 SSJavaLattice<String> methodLattice = md2lattice.get(md);
3598 if (methodLattice != null) {
3600 int methodDefLine = md.getLineNum();
3602 // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
3603 // Map<Descriptor, CompositeLocation> methodInferLocMap =
3604 // methodLocInfo.getMapDescToInferLocation();
3606 MethodSummary methodSummary = getMethodSummary(md);
3608 Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
3609 methodSummary.getMapVarDescToInferCompositeLocation();
3610 System.out.println("-----md=" + md);
3611 System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
3613 Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
3615 Set<String> localLocElementSet = methodLattice.getElementSet();
3617 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
3618 Descriptor localVarDesc = (Descriptor) iterator.next();
3619 System.out.println("-------localVarDesc=" + localVarDesc);
3620 CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
3622 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
3623 if (!localLocElementSet.contains(localLocIdentifier)) {
3624 methodLattice.put(localLocIdentifier);
3627 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
3629 if (!isParameter(md, localVarDesc)) {
3630 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
3631 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
3632 String orgSourceLine = sourceVec.get(varLineNum);
3633 System.out.println("varLineNum=" + varLineNum + " org src=" + orgSourceLine);
3635 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
3636 System.out.println("idx=" + idx
3637 + " generateVarDeclaration((VarDescriptor) localVarDesc)="
3638 + generateVarDeclaration((VarDescriptor) localVarDesc));
3640 String annoatedStr =
3641 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
3642 + orgSourceLine.substring(idx);
3643 sourceVec.set(varLineNum, annoatedStr);
3646 String methodDefStr = sourceVec.get(methodDefLine);
3649 getParamLocation(methodDefStr,
3650 generateVarDeclaration((VarDescriptor) localVarDesc));
3651 System.out.println("methodDefStr=" + methodDefStr + " localVarDesc=" + localVarDesc
3655 String annoatedStr =
3656 methodDefStr.substring(0, idx) + locAnnotationStr + " "
3657 + methodDefStr.substring(idx);
3658 sourceVec.set(methodDefLine, annoatedStr);
3663 // check if the lattice has to have the location type for the this
3666 // boolean needToAddthisRef = hasThisReference(md);
3667 // if (localLocElementSet.contains("this")) {
3668 // methodLattice.put("this");
3671 String methodLatticeDefStr = generateLatticeDefinition(md);
3672 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
3673 sourceVec.set(methodDefLine, annoatedStr);
3683 private boolean hasThisReference(MethodDescriptor md) {
3685 FlowGraph fg = getFlowGraph(md);
3686 Set<FlowNode> nodeSet = fg.getNodeSet();
3687 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3688 FlowNode flowNode = (FlowNode) iterator.next();
3689 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
3697 private int getParamLocation(String methodStr, String paramStr) {
3699 String pattern = paramStr + ",";
3701 int idx = methodStr.indexOf(pattern);
3705 pattern = paramStr + ")";
3706 return methodStr.indexOf(pattern);
3711 private String generateVarDeclaration(VarDescriptor varDesc) {
3713 TypeDescriptor td = varDesc.getType();
3714 String rtr = td.toString();
3716 for (int i = 0; i < td.getArrayCount(); i++) {
3720 rtr += " " + varDesc.getName();
3725 private String generateLocationAnnoatation(CompositeLocation loc) {
3728 Location methodLoc = loc.get(0);
3729 rtr += methodLoc.getLocIdentifier();
3731 for (int i = 1; i < loc.getSize(); i++) {
3732 Location element = loc.get(i);
3733 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
3739 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
3740 return getFlowGraph(md).isParamDesc(localVarDesc);
3743 private String extractFileName(String fileName) {
3744 int idx = fileName.lastIndexOf("/");
3748 return fileName.substring(idx + 1);
3753 private void codeGen() {
3755 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
3756 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
3757 String orgFileName = (String) iterator.next();
3758 String outputFileName = extractFileName(orgFileName);
3760 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
3764 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
3765 BufferedWriter out = new BufferedWriter(fileWriter);
3767 for (int i = 0; i < sourceVec.size(); i++) {
3768 out.write(sourceVec.get(i));
3772 } catch (IOException e) {
3773 e.printStackTrace();
3780 private void checkLattices() {
3782 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
3784 // current descriptors to visit in fixed-point interprocedural analysis,
3786 // dependency in the call graph
3787 methodDescriptorsToVisitStack.clear();
3789 // descriptorListToAnalyze.removeFirst();
3791 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
3792 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
3794 while (!descriptorListToAnalyze.isEmpty()) {
3795 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
3796 checkLatticesOfVirtualMethods(md);
3801 private void debug_writeLatticeDotFile() {
3802 // generate lattice dot file
3806 while (!toAnalyzeIsEmpty()) {
3807 ClassDescriptor cd = toAnalyzeNext();
3809 setupToAnalazeMethod(cd);
3811 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
3812 if (classLattice != null) {
3813 ssjava.writeLatticeDotFile(cd, null, classLattice);
3814 debug_printDescriptorToLocNameMapping(cd);
3817 while (!toAnalyzeMethodIsEmpty()) {
3818 MethodDescriptor md = toAnalyzeMethodNext();
3819 SSJavaLattice<String> methodLattice = md2lattice.get(md);
3820 if (methodLattice != null) {
3821 ssjava.writeLatticeDotFile(cd, md, methodLattice);
3822 debug_printDescriptorToLocNameMapping(md);
3829 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
3831 LocationInfo info = getLocationInfo(desc);
3832 System.out.println("## " + desc + " ##");
3833 System.out.println(info.getMapDescToInferLocation());
3834 LocationInfo locInfo = getLocationInfo(desc);
3835 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
3836 System.out.println("###################");
3840 private void calculateExtraLocations() {
3842 // LinkedList<MethodDescriptor> methodDescList = ssjava.getSortedDescriptors();
3843 LinkedList<MethodDescriptor> methodDescList =
3844 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3845 for (Iterator iterator = methodDescList.iterator(); iterator.hasNext();) {
3846 MethodDescriptor md = (MethodDescriptor) iterator.next();
3847 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
3848 calculateExtraLocations(md);
3854 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
3856 if (!md.isStatic()) {
3857 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3858 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3860 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
3861 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
3862 if (!md.equals(mdCallee)) {
3863 checkConsistency(md, mdCallee);
3871 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
3873 // check that two lattice have the same relations between parameters(+PC
3874 // LOC, GLOBAL_LOC RETURN LOC)
3876 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
3877 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
3879 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
3880 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
3882 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
3883 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
3885 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
3887 // add location types of paramters
3888 for (int idx = 0; idx < numParam; idx++) {
3889 list1.add(paramMap1.get(Integer.valueOf(idx)));
3890 list2.add(paramMap2.get(Integer.valueOf(idx)));
3893 // add program counter location
3894 list1.add(locInfo1.getPCLoc());
3895 list2.add(locInfo2.getPCLoc());
3897 if (!md1.getReturnType().isVoid()) {
3898 // add return value location
3899 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
3900 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
3905 // add global location type
3906 if (md1.isStatic()) {
3907 CompositeLocation globalLoc1 =
3908 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
3909 CompositeLocation globalLoc2 =
3910 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
3911 list1.add(globalLoc1);
3912 list2.add(globalLoc2);
3915 for (int i = 0; i < list1.size(); i++) {
3916 CompositeLocation locA1 = list1.get(i);
3917 CompositeLocation locA2 = list2.get(i);
3918 for (int k = 0; k < list1.size(); k++) {
3920 CompositeLocation locB1 = list1.get(k);
3921 CompositeLocation locB2 = list2.get(k);
3922 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
3924 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
3927 throw new Error("The method " + md1 + " is not consistent with the method " + md2
3928 + ".:: They have a different ordering relation between locations (" + locA1 + ","
3929 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
3937 private String getSymbol(int idx, FlowNode node) {
3938 Descriptor desc = node.getDescTuple().get(idx);
3939 return desc.getSymbol();
3942 private Descriptor getDescriptor(int idx, FlowNode node) {
3943 Descriptor desc = node.getDescTuple().get(idx);
3947 private void calculatePCLOC(MethodDescriptor md) {
3949 System.out.println("#CalculatePCLOC");
3950 MethodSummary methodSummary = getMethodSummary(md);
3951 FlowGraph fg = getFlowGraph(md);
3952 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
3954 // calculate the initial program counter location
3955 // PC location is higher than location types of parameters which has incoming flows.
3957 Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
3958 Set<Descriptor> paramDescNOTHavingInFlowSet = new HashSet<Descriptor>();
3959 // Set<FlowNode> paramNodeNOThavingInFlowSet = new HashSet<FlowNode>();
3961 int numParams = fg.getNumParameters();
3962 for (int i = 0; i < numParams; i++) {
3963 FlowNode paramFlowNode = fg.getParamFlowNode(i);
3964 Descriptor prefix = paramFlowNode.getDescTuple().get(0);
3965 NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
3966 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
3968 Set<FlowNode> inNodeToParamSet = fg.getIncomingNodeSetByPrefix(prefix);
3969 if (inNodeToParamSet.size() > 0) {
3970 // parameter has in-value flows
3972 for (Iterator iterator = inNodeToParamSet.iterator(); iterator.hasNext();) {
3973 FlowNode inNode = (FlowNode) iterator.next();
3974 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(inNode);
3975 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3976 FlowEdge flowEdge = (FlowEdge) iterator2.next();
3977 if (flowEdge.getEndTuple().startsWith(prefix)) {
3978 NTuple<Location> paramLocTupleWithIncomingFlow =
3979 translateToLocTuple(md, flowEdge.getEndTuple());
3980 paramLocTupleHavingInFlowSet.add(paramLocTupleWithIncomingFlow);
3985 // paramLocTupleHavingInFlowSet.add(paramLocTuple);
3987 // paramNodeNOThavingInFlowSet.add(fg.getFlowNode(paramDescTuple));
3988 paramDescNOTHavingInFlowSet.add(prefix);
3992 System.out.println("paramLocTupleHavingInFlowSet=" + paramLocTupleHavingInFlowSet);
3994 if (paramLocTupleHavingInFlowSet.size() > 0
3995 && !coversAllParamters(md, fg, paramLocTupleHavingInFlowSet)) {
3997 // Here, generates a location in the method lattice that is higher than the
3998 // paramLocTupleHavingInFlowSet
3999 NTuple<Location> pcLocTuple =
4000 generateLocTupleRelativeTo(md, paramLocTupleHavingInFlowSet, PCLOC);
4002 NTuple<Descriptor> pcDescTuple = translateToDescTuple(pcLocTuple);
4004 // System.out.println("pcLoc=" + pcLocTuple);
4006 CompositeLocation curPCLoc = methodSummary.getPCLoc();
4007 if (curPCLoc.get(0).isTop() || pcLocTuple.size() > curPCLoc.getSize()) {
4008 methodSummary.setPCLoc(new CompositeLocation(pcLocTuple));
4010 Set<FlowNode> flowNodeLowerthanPCLocSet = new HashSet<FlowNode>();
4011 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
4012 // add ordering relations s.t. PCLOC is higher than all flow nodes except the set of
4013 // parameters that do not have incoming flows
4014 for (Iterator iterator = fg.getNodeSet().iterator(); iterator.hasNext();) {
4015 FlowNode node = (FlowNode) iterator.next();
4017 if (!(node instanceof FlowReturnNode)) {
4018 if (!paramDescNOTHavingInFlowSet.contains(node.getCurrentDescTuple().get(0))) {
4019 flowNodeLowerthanPCLocSet.add(node);
4020 fg.addValueFlowEdge(pcDescTuple, node.getDescTuple());
4022 subGlobalFlowGraph.addValueFlowEdge(pcLocTuple,
4023 translateToLocTuple(md, node.getDescTuple()));
4026 System.out.println("***SKIP PCLOC -> RETURNLOC=" + node);
4030 fg.getFlowNode(translateToDescTuple(pcLocTuple)).setSkeleton(true);
4032 if (pcLocTuple.get(0).getLocDescriptor().equals(md.getThis())) {
4033 System.out.println("#########################################");
4034 for (Iterator iterator = flowNodeLowerthanPCLocSet.iterator(); iterator.hasNext();) {
4035 FlowNode lowerNode = (FlowNode) iterator.next();
4036 if (lowerNode.getDescTuple().size() == 1 && lowerNode.getCompositeLocation() == null) {
4037 NTuple<Location> lowerLocTuple = translateToLocTuple(md, lowerNode.getDescTuple());
4038 CompositeLocation newComp =
4039 calculateCompositeLocationFromSubGlobalGraph(md, lowerNode);
4040 if (newComp != null) {
4041 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(lowerLocTuple.get(0),
4043 lowerNode.setCompositeLocation(newComp);
4044 System.out.println("NEW COMP LOC=" + newComp + " to lowerNode=" + lowerNode);
4057 private int countFirstDescriptorSetSize(Set<NTuple<Location>> set) {
4059 Set<Descriptor> descSet = new HashSet<Descriptor>();
4061 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
4062 NTuple<Location> locTuple = (NTuple<Location>) iterator.next();
4063 descSet.add(locTuple.get(0).getLocDescriptor());
4066 return descSet.size();
4069 private boolean coversAllParamters(MethodDescriptor md, FlowGraph fg,
4070 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
4072 int numParam = fg.getNumParameters();
4073 // int size = paramLocTupleHavingInFlowSet.size();
4074 int size = countFirstDescriptorSetSize(paramLocTupleHavingInFlowSet);
4076 System.out.println("numParam=" + numParam + " size=" + size);
4078 // if (!md.isStatic()) {
4080 // // if the method is not static && there is a parameter composite location &&
4081 // // it is started with 'this',
4082 // // paramLocTupleHavingInFlowSet need to have 'this' parameter.
4084 // FlowNode thisParamNode = fg.getParamFlowNode(0);
4085 // NTuple<Location> thisParamLocTuple =
4086 // translateToLocTuple(md, thisParamNode.getCurrentDescTuple());
4088 // if (!paramLocTupleHavingInFlowSet.contains(thisParamLocTuple)) {
4090 // for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
4091 // NTuple<Location> paramTuple = (NTuple<Location>) iterator.next();
4092 // if (paramTuple.size() > 1 && paramTuple.get(0).getLocDescriptor().equals(md.getThis())) {
4093 // // paramLocTupleHavingInFlowSet.add(thisParamLocTuple);
4102 if (size == numParam) {
4110 private void calculateRETURNLOC(MethodDescriptor md) {
4112 System.out.println("#calculateRETURNLOC= " + md);
4114 // calculate a return location:
4115 // the return location type is lower than all parameters and the location of return values
4116 MethodSummary methodSummary = getMethodSummary(md);
4117 // if (methodSummary.getRETURNLoc() != null) {
4118 // System.out.println("$HERE?");
4122 FlowGraph fg = getFlowGraph(md);
4123 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
4124 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
4126 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
4127 // first, generate the set of return value location types that starts
4128 // with 'this' reference
4130 Set<FlowNode> paramFlowNodeFlowingToReturnValueSet = getParamNodeFlowingToReturnValue(md);
4131 // System.out.println("paramFlowNodeFlowingToReturnValueSet="
4132 // + paramFlowNodeFlowingToReturnValueSet);
4134 Set<NTuple<Location>> tupleToBeHigherThanReturnLocSet = new HashSet<NTuple<Location>>();
4135 for (Iterator iterator = paramFlowNodeFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
4136 FlowNode fn = (FlowNode) iterator.next();
4137 NTuple<Descriptor> paramDescTuple = fn.getCurrentDescTuple();
4138 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, paramDescTuple));
4141 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
4142 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
4143 FlowNode returnNode = (FlowNode) iterator.next();
4144 NTuple<Descriptor> returnDescTuple = returnNode.getCurrentDescTuple();
4145 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, returnDescTuple));
4147 System.out.println("-flow graph's returnNodeSet=" + returnNodeSet);
4148 System.out.println("tupleSetToBeHigherThanReturnLoc=" + tupleToBeHigherThanReturnLocSet);
4150 // Here, generates a return location in the method lattice that is lower than the
4151 // locFlowingToReturnValueSet
4152 NTuple<Location> returnLocTuple =
4153 generateLocTupleRelativeTo(md, tupleToBeHigherThanReturnLocSet, RLOC);
4155 // System.out.println("returnLocTuple=" + returnLocTuple);
4156 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnLocTuple);
4157 CompositeLocation curReturnLoc = methodSummary.getRETURNLoc();
4158 if (curReturnLoc == null || returnDescTuple.size() > curReturnLoc.getSize()) {
4159 methodSummary.setRETURNLoc(new CompositeLocation(returnLocTuple));
4161 for (Iterator iterator = tupleToBeHigherThanReturnLocSet.iterator(); iterator.hasNext();) {
4162 NTuple<Location> higherTuple = (NTuple<Location>) iterator.next();
4163 fg.addValueFlowEdge(translateToDescTuple(higherTuple), returnDescTuple);
4165 fg.getFlowNode(returnDescTuple).setSkeleton(true);
4169 // makes sure that PCLOC is higher than RETURNLOC
4170 CompositeLocation pcLoc = methodSummary.getPCLoc();
4171 if (!pcLoc.get(0).isTop()) {
4172 NTuple<Descriptor> pcLocDescTuple = translateToDescTuple(pcLoc.getTuple());
4173 fg.addValueFlowEdge(pcLocDescTuple, returnDescTuple);
4180 private void calculateExtraLocations(MethodDescriptor md) {
4181 // calcualte pcloc, returnloc,...
4183 System.out.println("\nSSJAVA:Calculate PCLOC/RETURNLOC locations: " + md);
4186 calculateRETURNLOC(md);
4190 private NTuple<Location> generateLocTupleRelativeTo(MethodDescriptor md,
4191 Set<NTuple<Location>> paramLocTupleHavingInFlowSet, String locNamePrefix) {
4193 // System.out.println("-generateLocTupleRelativeTo=" + paramLocTupleHavingInFlowSet);
4195 NTuple<Location> higherLocTuple = new NTuple<Location>();
4197 VarDescriptor thisVarDesc = md.getThis();
4198 // check if all paramter loc tuple is started with 'this' reference
4199 boolean hasParamNotStartedWithThisRef = false;
4203 Set<NTuple<Location>> paramLocTupleStartedWithThis = new HashSet<NTuple<Location>>();
4205 next: for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
4206 NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
4207 Descriptor paramLocalDesc = paramLocTuple.get(0).getLocDescriptor();
4208 if (!paramLocalDesc.equals(thisVarDesc)) {
4210 Set<FlowNode> inNodeSet = getFlowGraph(md).getIncomingNodeSetByPrefix(paramLocalDesc);
4211 for (Iterator iterator2 = inNodeSet.iterator(); iterator2.hasNext();) {
4212 FlowNode flowNode = (FlowNode) iterator2.next();
4213 if (flowNode.getDescTuple().startsWith(thisVarDesc)) {
4214 // System.out.println("paramLocTuple=" + paramLocTuple + " is lower than THIS");
4218 hasParamNotStartedWithThisRef = true;
4220 } else if (paramLocTuple.size() > 1) {
4221 paramLocTupleStartedWithThis.add(paramLocTuple);
4222 if (minSize == 0 || minSize > paramLocTuple.size()) {
4223 minSize = paramLocTuple.size();
4228 // System.out.println("---paramLocTupleStartedWithThis=" + paramLocTupleStartedWithThis);
4229 Descriptor enclosingDesc = md;
4230 if (hasParamNotStartedWithThisRef) {
4231 // in this case, PCLOC will be the local location
4233 // all parameter is started with 'this', so PCLOC will be set relative to the composite
4234 // location started with 'this'.
4235 // for (int idx = 0; idx < minSize - 1; idx++) {
4236 for (int idx = 0; idx < 1; idx++) {
4237 Set<Descriptor> locDescSet = new HashSet<Descriptor>();
4238 Location curLoc = null;
4239 NTuple<Location> paramLocTuple = null;
4240 for (Iterator iterator = paramLocTupleStartedWithThis.iterator(); iterator.hasNext();) {
4241 paramLocTuple = (NTuple<Location>) iterator.next();
4242 // System.out.println("-----paramLocTuple=" + paramLocTuple + " idx=" + idx);
4243 curLoc = paramLocTuple.get(idx);
4244 Descriptor locDesc = curLoc.getLocDescriptor();
4245 locDescSet.add(locDesc);
4247 // System.out.println("-----locDescSet=" + locDescSet + " idx=" + idx);
4248 if (locDescSet.size() != 1) {
4251 Location newLocElement = new Location(curLoc.getDescriptor(), curLoc.getLocDescriptor());
4252 System.out.println("newLocElement" + newLocElement);
4253 higherLocTuple.add(newLocElement);
4254 enclosingDesc = getClassTypeDescriptor(curLoc.getLocDescriptor());
4259 String locIdentifier = locNamePrefix + (locSeed++);
4260 NameDescriptor locDesc = new NameDescriptor(locIdentifier);
4261 Location newLoc = new Location(enclosingDesc, locDesc);
4262 higherLocTuple.add(newLoc);
4263 System.out.println("---new loc tuple=" + higherLocTuple);
4265 return higherLocTuple;
4269 public ClassDescriptor getClassTypeDescriptor(Descriptor in) {
4271 if (in instanceof VarDescriptor) {
4272 return ((VarDescriptor) in).getType().getClassDesc();
4273 } else if (in instanceof FieldDescriptor) {
4274 return ((FieldDescriptor) in).getType().getClassDesc();
4276 // else if (in instanceof LocationDescriptor) {
4277 // // here is the case that the descriptor 'in' is the last element of the assigned composite
4279 // return ((VarDescriptor) locTuple.get(0).getLocDescriptor()).getType().getClassDesc();
4287 private Set<NTuple<Location>> calculateHighestLocTupleSet(
4288 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
4290 Set<NTuple<Location>> highestSet = new HashSet<NTuple<Location>>();
4292 Iterator<NTuple<Location>> iterator = paramLocTupleHavingInFlowSet.iterator();
4293 NTuple<Location> highest = iterator.next();
4295 for (; iterator.hasNext();) {
4296 NTuple<Location> curLocTuple = (NTuple<Location>) iterator.next();
4297 if (isHigherThan(curLocTuple, highest)) {
4298 // System.out.println(curLocTuple + " is greater than " + highest);
4299 highest = curLocTuple;
4303 highestSet.add(highest);
4305 MethodDescriptor md = (MethodDescriptor) highest.get(0).getDescriptor();
4306 VarDescriptor thisVarDesc = md.getThis();
4308 // System.out.println("highest=" + highest);
4310 for (Iterator<NTuple<Location>> iter = paramLocTupleHavingInFlowSet.iterator(); iter.hasNext();) {
4311 NTuple<Location> curLocTuple = iter.next();
4313 if (!curLocTuple.equals(highest) && !hasOrderingRelation(highest, curLocTuple)) {
4315 // System.out.println("add it to the highest set=" + curLocTuple);
4316 highestSet.add(curLocTuple);
4325 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
4326 Set<String> higherLocSet = new HashSet<String>();
4328 Set<String> locSet = lattice.getTable().keySet();
4329 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
4330 String element = (String) iterator.next();
4331 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
4332 higherLocSet.add(element);
4335 return higherLocSet;
4338 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
4339 Set<CompositeLocation> set) {
4341 CompositeLocation lowest = set.iterator().next();
4343 if (set.size() == 1) {
4347 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
4348 CompositeLocation loc = (CompositeLocation) iterator.next();
4350 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
4351 // if there is a case where composite locations are incomparable, just
4356 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
4363 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
4364 CompositeLocation comp2) {
4366 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
4368 for (int idx = 0; idx < size; idx++) {
4369 Location loc1 = comp1.get(idx);
4370 Location loc2 = comp2.get(idx);
4372 Descriptor desc1 = loc1.getDescriptor();
4373 Descriptor desc2 = loc2.getDescriptor();
4375 if (!desc1.equals(desc2)) {
4376 throw new Error("Fail to compare " + comp1 + " and " + comp2);
4379 String symbol1 = loc1.getLocIdentifier();
4380 String symbol2 = loc2.getLocIdentifier();
4382 SSJavaLattice<String> lattice;
4384 lattice = methodLattice;
4386 lattice = getLattice(desc1);
4389 if (symbol1.equals(symbol2)) {
4391 } else if (!lattice.isComparable(symbol1, symbol2)) {
4400 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
4401 CompositeLocation comp2) {
4403 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
4405 for (int idx = 0; idx < size; idx++) {
4406 Location loc1 = comp1.get(idx);
4407 Location loc2 = comp2.get(idx);
4409 Descriptor desc1 = loc1.getDescriptor();
4410 Descriptor desc2 = loc2.getDescriptor();
4412 if (!desc1.equals(desc2)) {
4413 throw new Error("Fail to compare " + comp1 + " and " + comp2);
4416 String symbol1 = loc1.getLocIdentifier();
4417 String symbol2 = loc2.getLocIdentifier();
4419 SSJavaLattice<String> lattice;
4421 lattice = methodLattice;
4423 lattice = getLattice(desc1);
4426 if (symbol1.equals(symbol2)) {
4428 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
4439 private GlobalFlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
4441 if (!mapMethodDescriptorToSubGlobalFlowGraph.containsKey(md)) {
4442 mapMethodDescriptorToSubGlobalFlowGraph.put(md, new GlobalFlowGraph(md));
4444 return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
4447 private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
4448 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
4450 System.out.println("-propagateFlowsToCallerWithNoCompositeLocation=" + min.printNode(0));
4451 // if the parameter A reaches to the parameter B
4452 // then, add an edge the argument A -> the argument B to the caller's flow
4455 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
4456 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
4457 int numParam = calleeFlowGraph.getNumParameters();
4459 for (int i = 0; i < numParam; i++) {
4460 for (int k = 0; k < numParam; k++) {
4464 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
4465 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
4467 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
4468 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
4470 // check if the callee propagates an ordering constraints through
4473 // Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
4474 Set<FlowNode> localReachSet =
4475 calleeFlowGraph.getReachableSetFrom(paramNode1.getDescTuple());
4477 NTuple<Descriptor> paramDescTuple1 = paramNode1.getCurrentDescTuple();
4478 NTuple<Descriptor> paramDescTuple2 = paramNode2.getCurrentDescTuple();
4480 // System.out.println("-param1CurTuple=" + paramDescTuple1 + " param2CurTuple="
4481 // + paramDescTuple2);
4482 // System.out.println("-- localReachSet from param1=" + localReachSet);
4484 if (paramDescTuple1.get(0).equals(paramDescTuple2.get(0))) {
4485 // if two parameters share the same prefix
4486 // it already has been assigned to a composite location
4487 // so we don't need to add an additional ordering relation caused by these two
4492 if (arg1Tuple.size() > 0 && arg2Tuple.size() > 0
4493 && containsPrefix(paramNode2.getDescTuple().get(0), localReachSet)) {
4494 // need to propagate an ordering relation s.t. arg1 is higher
4496 // System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
4498 // add a new flow between the corresponding arguments.
4499 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
4500 // System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
4503 // .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
4507 // System.out.println();
4512 // if a parameter has a composite location and the first element of the parameter location
4513 // matches the callee's 'this'
4514 // we have a more specific constraint: the caller's corresponding argument is higher than the
4515 // parameter location which is translated into the caller
4517 for (int idx = 0; idx < numParam; idx++) {
4518 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
4519 CompositeLocation compLoc = paramNode.getCompositeLocation();
4520 System.out.println("paramNode=" + paramNode + " compLoc=" + compLoc);
4521 if (compLoc != null && compLoc.get(0).getLocDescriptor().equals(min.getMethod().getThis())) {
4522 System.out.println("$$$COMPLOC CASE=" + compLoc + " idx=" + idx);
4524 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
4525 System.out.println("--- argTuple=" + argTuple + " current compLoc="
4526 + callerFlowGraph.getFlowNode(argTuple).getCompositeLocation());
4528 NTuple<Descriptor> translatedParamTuple =
4529 translateCompositeLocationToCaller(idx, min, compLoc);
4530 System.out.println("add a flow edge= " + argTuple + "->" + translatedParamTuple);
4531 callerFlowGraph.addValueFlowEdge(argTuple, translatedParamTuple);
4533 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
4534 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
4535 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
4536 callerFlowGraph.addValueFlowEdge(translateToDescTuple(pcLocTuple), translatedParamTuple);
4544 private boolean containsPrefix(Descriptor prefixDesc, Set<FlowNode> set) {
4546 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
4547 FlowNode flowNode = (FlowNode) iterator.next();
4548 if (flowNode.getDescTuple().startsWith(prefixDesc)) {
4549 System.out.println("FOUND=" + flowNode);
4556 private NTuple<Descriptor> translateCompositeLocationToCaller(int idx, MethodInvokeNode min,
4557 CompositeLocation compLocForParam1) {
4559 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
4561 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
4562 for (int i = 0; i < baseTuple.size(); i++) {
4563 tuple.add(baseTuple.get(i));
4566 for (int i = 1; i < compLocForParam1.getSize(); i++) {
4567 Location loc = compLocForParam1.get(i);
4568 tuple.add(loc.getLocDescriptor());
4574 private CompositeLocation generateCompositeLocation(NTuple<Location> prefixLocTuple) {
4576 System.out.println("generateCompositeLocation=" + prefixLocTuple);
4578 CompositeLocation newCompLoc = new CompositeLocation();
4579 for (int i = 0; i < prefixLocTuple.size(); i++) {
4580 newCompLoc.addLocation(prefixLocTuple.get(i));
4583 Descriptor lastDescOfPrefix = prefixLocTuple.get(prefixLocTuple.size() - 1).getLocDescriptor();
4585 ClassDescriptor enclosingDescriptor;
4586 if (lastDescOfPrefix instanceof FieldDescriptor) {
4587 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
4588 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
4589 } else if (lastDescOfPrefix.equals(GLOBALDESC)) {
4590 MethodDescriptor currentMethodDesc = (MethodDescriptor) prefixLocTuple.get(0).getDescriptor();
4591 enclosingDescriptor = currentMethodDesc.getClassDesc();
4593 // var descriptor case
4594 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
4596 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
4598 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
4599 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
4601 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
4602 newLoc.setLocDescriptor(newLocDescriptor);
4603 newCompLoc.addLocation(newLoc);
4605 // System.out.println("--newCompLoc=" + newCompLoc);
4609 private CompositeLocation generateCompositeLocation(MethodDescriptor md,
4610 NTuple<Descriptor> paramPrefix) {
4612 System.out.println("generateCompositeLocation=" + paramPrefix);
4614 CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
4616 Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
4617 // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
4618 // + lastDescOfPrefix.getClass());
4619 ClassDescriptor enclosingDescriptor;
4620 if (lastDescOfPrefix instanceof FieldDescriptor) {
4621 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
4622 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
4624 // var descriptor case
4625 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
4627 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
4629 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
4630 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
4632 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
4633 newLoc.setLocDescriptor(newLocDescriptor);
4634 newCompLoc.addLocation(newLoc);
4636 // System.out.println("--newCompLoc=" + newCompLoc);
4640 private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
4641 MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
4643 List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
4645 for (int i = 0; i < callerPrefixList.size(); i++) {
4646 NTuple<Descriptor> prefix = callerPrefixList.get(i);
4647 if (prefix.startsWith(baseRef)) {
4648 NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
4649 calleePrefix.add(mdCallee.getThis());
4650 for (int k = 1; k < prefix.size(); k++) {
4651 calleePrefix.add(prefix.get(k));
4653 calleePrefixList.add(calleePrefix);
4657 return calleePrefixList;
4661 public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
4663 CompositeLocation compLoc = new CompositeLocation();
4665 Descriptor enclosingDescriptor = md;
4667 for (int i = 0; i < tuple.size(); i++) {
4668 Descriptor curDescriptor = tuple.get(i);
4669 Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
4670 locElement.setLocDescriptor(curDescriptor);
4671 compLoc.addLocation(locElement);
4673 if (curDescriptor instanceof VarDescriptor) {
4674 enclosingDescriptor = md.getClassDesc();
4675 } else if (curDescriptor instanceof FieldDescriptor) {
4676 enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
4677 } else if (curDescriptor instanceof NameDescriptor) {
4678 // it is "GLOBAL LOC" case!
4679 enclosingDescriptor = GLOBALDESC;
4680 } else if (curDescriptor instanceof InterDescriptor) {
4681 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(curDescriptor);
4683 enclosingDescriptor = null;
4691 private LocationDescriptor generateNewLocationDescriptor() {
4692 return new LocationDescriptor("Loc" + (locSeed++));
4695 private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
4697 // return the index where the prefix shared by tuple1 and tuple2 is ended
4698 // if there is no prefix shared by both of them, return -1
4700 int minSize = tuple1.size();
4701 if (minSize > tuple2.size()) {
4702 minSize = tuple2.size();
4706 for (int i = 0; i < minSize; i++) {
4707 if (!tuple1.get(i).equals(tuple2.get(i))) {
4717 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
4718 NTuple<Location> tuple) {
4720 // first, retrieve inferred location by the local var descriptor
4721 CompositeLocation inferLoc = new CompositeLocation();
4723 CompositeLocation localVarInferLoc =
4724 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
4726 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
4728 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
4729 inferLoc.addLocation(localVarInferLoc.get(i));
4732 for (int i = 1; i < tuple.size(); i++) {
4733 Location cur = tuple.get(i);
4734 Descriptor enclosingDesc = cur.getDescriptor();
4735 Descriptor curDesc = cur.getLocDescriptor();
4737 Location inferLocElement;
4738 if (curDesc == null) {
4739 // in this case, we have a newly generated location.
4740 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
4742 String fieldLocSymbol =
4743 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
4744 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
4745 inferLocElement.setLocDescriptor(curDesc);
4748 inferLoc.addLocation(inferLocElement);
4752 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
4756 public LocationInfo getLocationInfo(Descriptor d) {
4757 if (d instanceof MethodDescriptor) {
4758 return getMethodLocationInfo((MethodDescriptor) d);
4760 return getFieldLocationInfo((ClassDescriptor) d);
4764 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
4766 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
4767 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
4770 return mapMethodDescToMethodLocationInfo.get(md);
4774 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
4776 if (!mapClassToLocationInfo.containsKey(cd)) {
4777 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
4780 return mapClassToLocationInfo.get(cd);
4784 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
4785 NTuple<Location> prefix, NTuple<Location> element) {
4787 if (!map.containsKey(prefix)) {
4788 map.put(prefix, new HashSet<NTuple<Location>>());
4790 map.get(prefix).add(element);
4793 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
4794 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
4795 Descriptor desc = (Descriptor) iterator.next();
4797 if (desc.equals(LocationInference.GLOBALDESC)) {
4799 } else if (desc instanceof VarDescriptor) {
4800 if (!((VarDescriptor) desc).getType().isPrimitive()) {
4803 } else if (desc instanceof FieldDescriptor) {
4804 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
4813 public SSJavaLattice<String> getLattice(Descriptor d) {
4814 if (d instanceof MethodDescriptor) {
4815 return getMethodLattice((MethodDescriptor) d);
4817 return getFieldLattice((ClassDescriptor) d);
4821 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
4822 if (!md2lattice.containsKey(md)) {
4823 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
4825 return md2lattice.get(md);
4828 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
4829 md2lattice.put(md, lattice);
4832 private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
4835 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
4836 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
4838 if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
4839 && dstCurTuple.size() > (idx + 1)) {
4840 // value flow between fields: we don't need to add a binary relation
4843 Descriptor desc = srcCurTuple.get(idx);
4844 ClassDescriptor classDesc;
4847 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
4849 if (desc instanceof FieldDescriptor) {
4850 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
4852 // this case is that the local variable has a composite location assignment
4853 // the following element after the composite location to the local variable
4854 // has the enclosing descriptor of the local variable
4855 Descriptor localDesc = srcNode.getDescTuple().get(0);
4856 classDesc = ((VarDescriptor) localDesc).getType().getClassDesc();
4859 extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
4863 Descriptor srcFieldDesc = srcCurTuple.get(idx);
4864 Descriptor dstFieldDesc = dstCurTuple.get(idx);
4866 System.out.println("srcFieldDesc=" + srcFieldDesc + " dstFieldDesc=" + dstFieldDesc
4868 if (!srcFieldDesc.equals(dstFieldDesc)) {
4870 System.out.println("-ADD EDGE");
4871 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
4872 } else if (!isReference(srcFieldDesc) && !isReference(dstFieldDesc)) {
4873 System.out.println("-ADD EDGE");
4874 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
4881 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
4882 if (!cd2lattice.containsKey(cd)) {
4883 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
4885 return cd2lattice.get(cd);
4888 public LinkedList<MethodDescriptor> computeMethodList() {
4890 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
4894 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
4895 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
4897 while (!toAnalyzeIsEmpty()) {
4898 ClassDescriptor cd = toAnalyzeNext();
4900 if (cd.getClassName().equals("Object")) {
4901 rootClassDescriptor = cd;
4902 // inheritanceTree = new InheritanceTree<ClassDescriptor>(cd);
4905 setupToAnalazeMethod(cd);
4906 temp_toanalyzeMethodList.removeAll(visited);
4908 while (!toAnalyzeMethodIsEmpty()) {
4909 MethodDescriptor md = toAnalyzeMethodNext();
4910 if ((!visited.contains(md))
4911 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
4913 // creates a mapping from a method descriptor to virtual methods
4914 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
4915 if (md.isStatic()) {
4916 setPossibleCallees.add(md);
4918 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
4921 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
4922 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
4924 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
4925 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
4926 if ((!ssjava.isTrustMethod(calleemd))
4927 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))
4928 && (!calleemd.getModifiers().isNative())) {
4929 if (!visited.contains(calleemd)) {
4930 temp_toanalyzeMethodList.add(calleemd);
4932 reachableCallee.add(calleemd);
4933 needToAnalyzeCalleeSet.add(calleemd);
4937 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
4946 return ssjava.topologicalSort(toSort);
4950 public boolean isTransitivelyCalledFrom(MethodDescriptor callee, MethodDescriptor caller) {
4951 // if the callee is transitively invoked from the caller
4954 int callerIdx = toanalyze_methodDescList.indexOf(caller);
4955 int calleeIdx = toanalyze_methodDescList.indexOf(callee);
4957 if (callerIdx < calleeIdx) {
4965 public void constructFlowGraph() {
4967 System.out.println("");
4968 toanalyze_methodDescList = computeMethodList();
4970 // hack... it seems that there is a problem with topological sorting.
4971 // so String.toString(Object o) is appeared too higher in the call chain.
4972 // MethodDescriptor mdToString = null;
4973 // for (Iterator iterator = toanalyze_methodDescList.iterator(); iterator.hasNext();) {
4974 // MethodDescriptor md = (MethodDescriptor) iterator.next();
4975 // if (md.toString().equals("public static String String.valueOf(Object o)")) {
4980 // if (mdToString != null) {
4981 // toanalyze_methodDescList.remove(mdToString);
4982 // toanalyze_methodDescList.addLast(mdToString);
4985 LinkedList<MethodDescriptor> methodDescList =
4986 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
4988 System.out.println("@@@methodDescList=" + methodDescList);
4991 while (!methodDescList.isEmpty()) {
4992 MethodDescriptor md = methodDescList.removeLast();
4993 if (state.SSJAVADEBUG) {
4994 System.out.println();
4995 System.out.println("SSJAVA: Constructing a flow graph: " + md);
4997 // creates a mapping from a parameter descriptor to its index
4998 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
5000 if (!md.isStatic()) {
5002 mapParamDescToIdx.put(md.getThis(), 0);
5005 for (int i = 0; i < md.numParameters(); i++) {
5006 Descriptor paramDesc = (Descriptor) md.getParameter(i);
5007 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
5010 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
5011 mapMethodDescriptorToFlowGraph.put(md, fg);
5013 analyzeMethodBody(md.getClassDesc(), md);
5015 // System.out.println("##constructSubGlobalFlowGraph");
5016 // GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(fg);
5017 // mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
5020 // System.out.println("##addValueFlowsFromCalleeSubGlobalFlowGraph");
5021 // addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
5022 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
5024 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
5027 // _debug_printGraph();
5031 private void constructGlobalFlowGraph() {
5032 LinkedList<MethodDescriptor> methodDescList =
5033 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
5035 while (!methodDescList.isEmpty()) {
5036 MethodDescriptor md = methodDescList.removeLast();
5037 if (state.SSJAVADEBUG) {
5038 System.out.println();
5039 System.out.println("SSJAVA: Constructing a sub global flow graph: " + md);
5041 constructSubGlobalFlowGraph(getFlowGraph(md));
5044 System.out.println("-add Value Flows From CalleeSubGlobalFlowGraph");
5045 addValueFlowsFromCalleeSubGlobalFlowGraph(md);
5046 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
5048 // System.out.println("-propagate Flows From Callees With No CompositeLocation");
5049 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
5051 // mark if a parameter has incoming flows
5052 checkParamNodesInSubGlobalFlowGraph(md);
5058 private void checkParamNodesInSubGlobalFlowGraph(MethodDescriptor md) {
5059 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5060 FlowGraph flowGraph = getFlowGraph(md);
5062 Set<FlowNode> paramFlowNodeSet = flowGraph.getParamFlowNodeSet();
5063 for (Iterator iterator = paramFlowNodeSet.iterator(); iterator.hasNext();) {
5064 FlowNode paramFlowNode = (FlowNode) iterator.next();
5065 System.out.println("paramFlowNode=" + paramFlowNode);
5066 NTuple<Descriptor> paramDescTuple = paramFlowNode.getDescTuple();
5067 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
5068 GlobalFlowNode paramGlobalNode = globalFlowGraph.getFlowNode(paramLocTuple);
5070 Set<GlobalFlowNode> incomingNodeSet =
5071 globalFlowGraph.getIncomingNodeSetByPrefix(paramLocTuple.get(0));
5073 if (incomingNodeSet.size() > 0) {
5074 paramGlobalNode.setParamNodeWithIncomingFlows(true);
5080 private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
5081 if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
5082 mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
5084 return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
5087 private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
5089 // the transformation for a call site propagates flows through parameters
5090 // if the method is virtual, it also grab all relations from any possible
5093 Set<MethodInvokeNode> setMethodInvokeNode =
5094 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
5096 if (setMethodInvokeNode != null) {
5098 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
5099 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
5100 MethodDescriptor mdCallee = min.getMethod();
5101 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
5102 if (mdCallee.isStatic()) {
5103 setPossibleCallees.add(mdCallee);
5105 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
5106 // removes method descriptors that are not invoked by the caller
5107 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
5108 setPossibleCallees.addAll(calleeSet);
5111 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
5112 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
5113 propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
5121 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
5122 BlockNode bn = state.getMethodBody(md);
5123 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
5124 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
5127 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
5128 NodeTupleSet implicitFlowTupleSet) {
5130 bn.getVarTable().setParent(nametable);
5131 for (int i = 0; i < bn.size(); i++) {
5132 BlockStatementNode bsn = bn.get(i);
5133 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
5138 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
5139 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
5141 switch (bsn.kind()) {
5142 case Kind.BlockExpressionNode:
5143 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
5146 case Kind.DeclarationNode:
5147 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
5150 case Kind.IfStatementNode:
5151 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
5155 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
5158 case Kind.ReturnNode:
5159 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
5162 case Kind.SubBlockNode:
5163 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
5166 case Kind.ContinueBreakNode:
5169 case Kind.SwitchStatementNode:
5170 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
5177 private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
5178 SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
5180 analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
5184 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
5185 SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
5187 NodeTupleSet condTupleNode = new NodeTupleSet();
5188 analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
5189 implicitFlowTupleSet, false);
5191 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5193 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5194 newImplicitTupleSet.addTupleSet(condTupleNode);
5196 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5197 // need to create an intermediate node for the GLB of conditional
5198 // locations & implicit flows
5199 System.out.println("10");
5201 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5202 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5203 NTuple<Descriptor> tuple = idxIter.next();
5204 addFlowGraphEdge(md, tuple, interTuple);
5206 newImplicitTupleSet.clear();
5207 newImplicitTupleSet.addTuple(interTuple);
5210 BlockNode sbn = ssn.getSwitchBody();
5211 for (int i = 0; i < sbn.size(); i++) {
5212 analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
5217 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
5218 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
5219 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
5222 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
5223 NodeTupleSet implicitFlowTupleSet) {
5225 // System.out.println("-analyzeFlowReturnNode=" + rn.printNode(0));
5226 ExpressionNode returnExp = rn.getReturnExpression();
5228 if (returnExp != null) {
5229 NodeTupleSet nodeSet = new NodeTupleSet();
5230 // if a return expression returns a literal value, nodeSet is empty
5231 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
5232 FlowGraph fg = getFlowGraph(md);
5234 // if (implicitFlowTupleSet.size() == 1
5236 // fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate())
5239 // // since there is already an intermediate node for the GLB of implicit
5241 // // we don't need to create another intermediate node.
5242 // // just re-use the intermediate node for implicit flows.
5244 // FlowNode meetNode =
5245 // fg.getFlowNode(implicitFlowTupleSet.iterator().next());
5247 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
5248 // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>)
5250 // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
5255 NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
5257 // add tuples from return node
5258 currentFlowTupleSet.addTupleSet(nodeSet);
5260 // add tuples corresponding to the current implicit flows
5261 currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
5263 // System.out.println("---currentFlowTupleSet=" + currentFlowTupleSet);
5265 if (needToGenerateInterLoc(currentFlowTupleSet)) {
5266 System.out.println("9");
5268 FlowNode meetNode = fg.createIntermediateNode();
5269 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
5270 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
5271 fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
5273 fg.addReturnFlowNode(meetNode.getDescTuple());
5275 // currentFlowTupleSet = removeLiteralTuple(currentFlowTupleSet);
5276 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
5277 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
5278 fg.addReturnFlowNode(currentFlowTuple);
5286 private NodeTupleSet removeLiteralTuple(NodeTupleSet inSet) {
5287 NodeTupleSet tupleSet = new NodeTupleSet();
5288 for (Iterator<NTuple<Descriptor>> iter = inSet.iterator(); iter.hasNext();) {
5289 NTuple<Descriptor> tuple = iter.next();
5290 if (!tuple.get(0).equals(LITERALDESC)) {
5291 tupleSet.addTuple(tuple);
5297 private boolean needToGenerateInterLoc(NodeTupleSet tupleSet) {
5299 for (Iterator<NTuple<Descriptor>> iter = tupleSet.iterator(); iter.hasNext();) {
5300 NTuple<Descriptor> descTuple = iter.next();
5301 if (!descTuple.get(0).equals(LITERALDESC)) {
5306 System.out.println("needToGenerateInterLoc=" + tupleSet + " size=" + size);
5313 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
5314 NodeTupleSet implicitFlowTupleSet) {
5316 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
5318 NodeTupleSet condTupleNode = new NodeTupleSet();
5319 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
5320 implicitFlowTupleSet, false);
5322 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5324 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5325 newImplicitTupleSet.addTupleSet(condTupleNode);
5327 newImplicitTupleSet.addGlobalFlowTupleSet(implicitFlowTupleSet.getGlobalLocTupleSet());
5328 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
5330 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5331 // need to create an intermediate node for the GLB of conditional
5332 // locations & implicit flows
5333 System.out.println("6");
5335 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5336 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
5338 NTuple<Descriptor> tuple = idxIter.next();
5339 addFlowGraphEdge(md, tuple, interTuple);
5341 newImplicitTupleSet.clear();
5342 newImplicitTupleSet.addTuple(interTuple);
5347 // System.out.println("condTupleNode="+condTupleNode);
5348 // NTuple<Descriptor> interTuple =
5349 // getFlowGraph(md).createIntermediateNode().getDescTuple();
5351 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator();
5352 // idxIter.hasNext();) {
5353 // NTuple<Descriptor> tuple = idxIter.next();
5354 // addFlowGraphEdge(md, tuple, interTuple);
5357 // for (Iterator<NTuple<Descriptor>> idxIter =
5358 // implicitFlowTupleSet.iterator(); idxIter
5360 // NTuple<Descriptor> tuple = idxIter.next();
5361 // addFlowGraphEdge(md, tuple, interTuple);
5364 // NodeTupleSet newImplicitSet = new NodeTupleSet();
5365 // newImplicitSet.addTuple(interTuple);
5366 analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
5369 // condTupleNode.addTupleSet(implicitFlowTupleSet);
5371 // add edges from condNodeTupleSet to all nodes of conditional nodes
5372 // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
5375 // check 'for loop' case
5376 BlockNode bn = ln.getInitializer();
5377 bn.getVarTable().setParent(nametable);
5378 for (int i = 0; i < bn.size(); i++) {
5379 BlockStatementNode bsn = bn.get(i);
5380 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
5383 NodeTupleSet condTupleNode = new NodeTupleSet();
5384 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
5385 implicitFlowTupleSet, false);
5387 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5388 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5389 newImplicitTupleSet.addTupleSet(condTupleNode);
5391 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5392 // need to create an intermediate node for the GLB of conditional
5393 // locations & implicit flows
5394 System.out.println("7");
5396 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5397 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
5399 NTuple<Descriptor> tuple = idxIter.next();
5400 addFlowGraphEdge(md, tuple, interTuple);
5402 newImplicitTupleSet.clear();
5403 newImplicitTupleSet.addTuple(interTuple);
5408 // NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5410 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
5411 // NTuple<Descriptor> tuple = idxIter.next();
5412 // addFlowGraphEdge(md, tuple, interTuple);
5415 // for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
5417 // NTuple<Descriptor> tuple = idxIter.next();
5418 // addFlowGraphEdge(md, tuple, interTuple);
5421 // NodeTupleSet newImplicitSet = new NodeTupleSet();
5422 // newImplicitSet.addTuple(interTuple);
5423 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitTupleSet);
5424 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitTupleSet);
5427 // condTupleNode.addTupleSet(implicitFlowTupleSet);
5429 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
5431 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
5438 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
5439 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
5441 // System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
5443 NodeTupleSet condTupleNode = new NodeTupleSet();
5444 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
5445 implicitFlowTupleSet, false);
5447 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5449 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5450 newImplicitTupleSet.addTupleSet(condTupleNode);
5452 // System.out.println("$$$GGGcondTupleNode=" + condTupleNode.getGlobalLocTupleSet());
5453 // System.out.println("-condTupleNode=" + condTupleNode);
5454 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5455 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
5457 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5458 System.out.println("5");
5460 // need to create an intermediate node for the GLB of conditional locations & implicit flows
5461 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5462 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5463 NTuple<Descriptor> tuple = idxIter.next();
5464 addFlowGraphEdge(md, tuple, interTuple);
5466 newImplicitTupleSet.clear();
5467 newImplicitTupleSet.addTuple(interTuple);
5470 // GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5471 // for (Iterator<NTuple<Location>> iterator = condTupleNode.globalIterator();
5472 // iterator.hasNext();) {
5473 // NTuple<Location> calleeReturnLocTuple = iterator.next();
5474 // for (Iterator<NTuple<Descriptor>> iter2 = newImplicitTupleSet.iterator(); iter2.hasNext();) {
5475 // NTuple<Descriptor> callerImplicitTuple = iter2.next();
5476 // globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5477 // translateToLocTuple(md, callerImplicitTuple));
5480 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
5482 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
5484 if (isn.getFalseBlock() != null) {
5485 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
5490 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
5491 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
5493 VarDescriptor vd = dn.getVarDescriptor();
5494 mapDescToDefinitionLine.put(vd, dn.getNumLine());
5495 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
5497 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
5498 fn.setDeclarationNode();
5500 if (dn.getExpression() != null) {
5501 System.out.println("-analyzeFlowDeclarationNode=" + dn.printNode(0));
5503 NodeTupleSet nodeSetRHS = new NodeTupleSet();
5504 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
5505 implicitFlowTupleSet, false);
5507 // creates edges from RHS to LHS
5508 NTuple<Descriptor> interTuple = null;
5509 if (needToGenerateInterLoc(nodeSetRHS)) {
5510 System.out.println("3");
5511 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5514 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
5515 NTuple<Descriptor> fromTuple = iter.next();
5516 System.out.println("fromTuple=" + fromTuple + " interTuple=" + interTuple + " tupleLSH="
5518 addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
5521 // creates edges from implicitFlowTupleSet to LHS
5522 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
5523 NTuple<Descriptor> implicitTuple = iter.next();
5524 addFlowGraphEdge(md, implicitTuple, tupleLHS);
5527 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5528 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
5529 NTuple<Location> calleeReturnLocTuple = iterator.next();
5531 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, tupleLHS));
5534 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
5536 NTuple<Location> implicitGlobalTuple = iterator.next();
5538 globalFlowGraph.addValueFlowEdge(implicitGlobalTuple, translateToLocTuple(md, tupleLHS));
5541 System.out.println("-nodeSetRHS=" + nodeSetRHS);
5542 System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5548 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
5549 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
5550 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
5554 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
5555 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
5556 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
5559 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
5560 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
5561 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
5563 // System.out.println("en=" + en.printNode(0) + " class=" + en.getClass());
5565 // note that expression node can create more than one flow node
5566 // nodeSet contains of flow nodes
5567 // base is always assigned to null except the case of a name node!
5568 NTuple<Descriptor> flowTuple;
5569 switch (en.kind()) {
5570 case Kind.AssignmentNode:
5571 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
5572 implicitFlowTupleSet);
5575 case Kind.FieldAccessNode:
5577 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
5578 implicitFlowTupleSet, isLHS);
5579 if (flowTuple != null) {
5580 nodeSet.addTuple(flowTuple);
5585 NodeTupleSet nameNodeSet = new NodeTupleSet();
5587 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
5588 if (flowTuple != null) {
5589 nodeSet.addTuple(flowTuple);
5594 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
5597 case Kind.CreateObjectNode:
5598 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
5601 case Kind.ArrayAccessNode:
5602 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
5605 case Kind.LiteralNode:
5606 analyzeFlowLiteralNode(md, nametable, (LiteralNode) en, nodeSet);
5609 case Kind.MethodInvokeNode:
5610 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
5611 implicitFlowTupleSet);
5614 case Kind.TertiaryNode:
5615 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
5619 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
5621 // case Kind.InstanceOfNode:
5622 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
5625 // case Kind.ArrayInitializerNode:
5626 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
5630 // case Kind.ClassTypeNode:
5631 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
5634 // case Kind.OffsetNode:
5635 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
5644 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
5645 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
5647 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
5648 implicitFlowTupleSet, false);
5652 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
5653 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
5655 // System.out.println("analyzeFlowTertiaryNode=" + tn.printNode(0));
5657 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
5658 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
5659 implicitFlowTupleSet, false);
5661 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5662 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5663 newImplicitTupleSet.addTupleSet(tertiaryTupleNode);
5665 // System.out.println("$$$GGGcondTupleNode=" + tertiaryTupleNode.getGlobalLocTupleSet());
5666 // System.out.println("-tertiaryTupleNode=" + tertiaryTupleNode);
5667 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5668 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
5670 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5671 System.out.println("15");
5672 // need to create an intermediate node for the GLB of conditional locations & implicit flows
5673 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5674 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5675 NTuple<Descriptor> tuple = idxIter.next();
5676 addFlowGraphEdge(md, tuple, interTuple);
5678 newImplicitTupleSet.clear();
5679 newImplicitTupleSet.addTuple(interTuple);
5682 newImplicitTupleSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
5684 System.out.println("---------newImplicitTupleSet=" + newImplicitTupleSet);
5685 // add edges from tertiaryTupleNode to all nodes of conditional nodes
5686 // tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
5687 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
5688 newImplicitTupleSet, false);
5690 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
5691 newImplicitTupleSet, false);
5693 nodeSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
5694 nodeSet.addTupleSet(tertiaryTupleNode);
5696 System.out.println("#tertiary node set=" + nodeSet);
5699 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
5700 MethodInvokeNode min) {
5701 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
5703 set = new HashSet<MethodInvokeNode>();
5704 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
5709 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
5711 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
5712 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
5714 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
5717 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
5719 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
5720 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
5723 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
5726 private Set<NTuple<Location>> getPCLocTupleSet(MethodInvokeNode min) {
5727 if (!mapMethodInvokeNodeToPCLocTupleSet.containsKey(min)) {
5728 mapMethodInvokeNodeToPCLocTupleSet.put(min, new HashSet<NTuple<Location>>());
5730 return mapMethodInvokeNodeToPCLocTupleSet.get(min);
5733 private void analyzeFlowMethodInvokeNode(MethodDescriptor mdCaller, SymbolTable nametable,
5734 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
5736 System.out.println("analyzeFlowMethodInvokeNode=" + min.printNode(0));
5738 if (!toanalyze_methodDescList.contains(min.getMethod())) {
5742 addMapMethodDescToMethodInvokeNodeSet(min);
5744 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
5745 for (Iterator iterator = implicitFlowTupleSet.iterator(); iterator.hasNext();) {
5746 NTuple<Descriptor> pcDescTuple = (NTuple<Descriptor>) iterator.next();
5747 if (!pcDescTuple.get(0).equals(LITERALDESC)) {
5748 // here we don't need to add the literal value as a PC location
5749 pcLocTupleSet.add(translateToLocTuple(mdCaller, pcDescTuple));
5753 mapMethodInvokeNodeToArgIdxMap.put(min, new HashMap<Integer, NTuple<Descriptor>>());
5755 if (nodeSet == null) {
5756 nodeSet = new NodeTupleSet();
5759 MethodDescriptor mdCallee = min.getMethod();
5761 NameDescriptor baseName = min.getBaseName();
5762 boolean isSystemout = false;
5763 if (baseName != null) {
5764 isSystemout = baseName.getSymbol().equals("System.out");
5767 if (!ssjava.isSSJavaUtil(mdCallee.getClassDesc()) && !ssjava.isTrustMethod(mdCallee)
5770 addMapCallerMethodDescToMethodInvokeNodeSet(mdCaller, min);
5772 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
5773 System.out.println("mdCallee=" + mdCallee + " calleeFlowGraph=" + calleeFlowGraph);
5774 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
5776 System.out.println("---calleeReturnSet=" + calleeReturnSet);
5778 NodeTupleSet tupleSet = new NodeTupleSet();
5780 if (min.getExpression() != null) {
5782 NodeTupleSet baseNodeSet = new NodeTupleSet();
5783 analyzeFlowExpressionNode(mdCaller, nametable, min.getExpression(), baseNodeSet, null,
5784 implicitFlowTupleSet, false);
5785 System.out.println("baseNodeSet=" + baseNodeSet);
5787 assert (baseNodeSet.size() == 1);
5788 NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
5789 if (baseTuple.get(0) instanceof InterDescriptor) {
5790 if (baseTuple.size() > 1) {
5793 FlowNode interNode = getFlowGraph(mdCaller).getFlowNode(baseTuple);
5794 baseTuple = translateBaseTuple(interNode, baseTuple);
5796 mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
5798 if (!min.getMethod().isStatic()) {
5799 addArgIdxMap(min, 0, baseTuple);
5801 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
5802 FlowNode returnNode = (FlowNode) iterator.next();
5803 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
5804 if (returnDescTuple.startsWith(mdCallee.getThis())) {
5805 // the location type of the return value is started with 'this'
5807 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
5809 if (inFlowTuple.get(0) instanceof InterDescriptor) {
5810 // min.getExpression()
5815 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
5816 // nodeSet.addTuple(inFlowTuple);
5817 System.out.println("1CREATE A NEW TUPLE=" + inFlowTuple + " from="
5818 + mdCallee.getThis());
5819 // tupleSet.addTuple(inFlowTuple);
5820 tupleSet.addTuple(baseTuple);
5823 System.out.println("returnNode=" + returnNode);
5824 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
5825 // System.out.println("inFlowSet=" + inFlowSet + " from retrunNode=" + returnNode);
5826 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
5827 FlowNode inFlowNode = (FlowNode) iterator2.next();
5828 if (inFlowNode.getDescTuple().startsWith(mdCallee.getThis())) {
5829 // nodeSet.addTupleSet(baseNodeSet);
5830 System.out.println("2CREATE A NEW TUPLE=" + baseNodeSet + " from="
5831 + mdCallee.getThis());
5832 tupleSet.addTupleSet(baseNodeSet);
5840 // analyze parameter flows
5842 if (min.numArgs() > 0) {
5845 if (min.getMethod().isStatic()) {
5851 for (int i = 0; i < min.numArgs(); i++) {
5852 ExpressionNode en = min.getArg(i);
5853 int idx = i + offset;
5854 NodeTupleSet argTupleSet = new NodeTupleSet();
5855 analyzeFlowExpressionNode(mdCaller, nametable, en, argTupleSet, false);
5856 // if argument is liternal node, argTuple is set to NULL
5857 System.out.println("---arg idx=" + idx + " argTupleSet=" + argTupleSet);
5858 NTuple<Descriptor> argTuple = generateArgTuple(mdCaller, argTupleSet);
5860 // if an argument is literal value,
5861 // we need to create an itermediate node so that we could assign a composite location to
5862 // that node if needed
5863 if (argTuple.size() > 0
5864 && (argTuple.get(0).equals(GLOBALDESC) || argTuple.get(0).equals(LITERALDESC))) {
5866 * System.out.println("***GLOBAL ARG TUPLE CASE=" + argTuple); System.out.println("8");
5868 * NTuple<Descriptor> interTuple =
5869 * getFlowGraph(mdCaller).createIntermediateNode().getDescTuple(); ((InterDescriptor)
5870 * interTuple.get(0)).setMethodArgIdxPair(min, idx); addFlowGraphEdge(mdCaller,
5871 * argTuple, interTuple); argTuple = interTuple; addArgIdxMap(min, idx, argTuple);
5872 * System.out.println("new min mapping i=" + idx + " ->" + argTuple);
5874 argTuple = new NTuple<Descriptor>();
5877 addArgIdxMap(min, idx, argTuple);
5879 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
5881 // check whether a param node in the callee graph has incoming flows
5882 // if it has incoming flows, the corresponding arg should be lower than the current PC
5883 // Descriptor prefix = paramNode.getDescTuple().get(0);
5884 // if (calleeFlowGraph.getIncomingNodeSetByPrefix(prefix).size() > 0) {
5885 // for (Iterator<NTuple<Descriptor>> iterator = implicitFlowTupleSet.iterator(); iterator
5887 // NTuple<Descriptor> pcTuple = iterator.next();
5888 // System.out.println("add edge pcTuple =" + pcTuple + " -> " + argTuple);
5889 // addFlowGraphEdge(md, pcTuple, argTuple);
5893 System.out.println("paramNode=" + paramNode + " calleeReturnSet=" + calleeReturnSet);
5894 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
5895 || mdCallee.getModifiers().isNative()) {
5896 addParamNodeFlowingToReturnValue(mdCallee, paramNode);
5897 // nodeSet.addTupleSet(argTupleSet);
5898 System.out.println("3CREATE A NEW TUPLE=" + argTupleSet + " from=" + paramNode);
5899 tupleSet.addTupleSet(argTupleSet);
5905 if (mdCallee.getReturnType() != null && !mdCallee.getReturnType().isVoid()) {
5906 FlowReturnNode returnHolderNode = getFlowGraph(mdCaller).createReturnNode(min);
5908 if (needToGenerateInterLoc(tupleSet)) {
5909 System.out.println("20");
5910 FlowGraph fg = getFlowGraph(mdCaller);
5911 FlowNode interNode = fg.createIntermediateNode();
5912 interNode.setFormHolder(true);
5914 NTuple<Descriptor> interTuple = interNode.getDescTuple();
5916 for (Iterator iterator = tupleSet.iterator(); iterator.hasNext();) {
5917 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5919 Set<NTuple<Descriptor>> addSet = new HashSet<NTuple<Descriptor>>();
5920 FlowNode node = fg.getFlowNode(tuple);
5921 if (node instanceof FlowReturnNode) {
5922 addSet.addAll(fg.getReturnTupleSet(((FlowReturnNode) node).getReturnTupleSet()));
5926 for (Iterator iterator2 = addSet.iterator(); iterator2.hasNext();) {
5927 NTuple<Descriptor> higher = (NTuple<Descriptor>) iterator2.next();
5928 addFlowGraphEdge(mdCaller, higher, interTuple);
5932 returnHolderNode.addTuple(interTuple);
5934 nodeSet.addTuple(interTuple);
5935 System.out.println("ADD TUPLESET=" + interTuple + " to returnnode=" + returnHolderNode);
5938 returnHolderNode.addTupleSet(tupleSet);
5939 System.out.println("ADD TUPLESET=" + tupleSet + " to returnnode=" + returnHolderNode);
5941 // setNode.addTupleSet(tupleSet);
5942 // NodeTupleSet setFromReturnNode=new NodeTupleSet();
5943 // setFromReturnNode.addTuple(tuple);
5945 NodeTupleSet holderTupleSet =
5946 getNodeTupleSetFromReturnNode(getFlowGraph(mdCaller), returnHolderNode);
5948 System.out.println("HOLDER TUPLe SET=" + holderTupleSet);
5949 nodeSet.addTupleSet(holderTupleSet);
5951 nodeSet.addTuple(returnHolderNode.getDescTuple());
5954 // propagateFlowsFromCallee(min, md, min.getMethod());
5956 // when generating the global flow graph,
5957 // we need to add ordering relations from the set of callee return loc tuple to LHS of the
5958 // caller assignment
5959 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
5960 FlowNode calleeReturnNode = (FlowNode) iterator.next();
5961 NTuple<Location> calleeReturnLocTuple =
5962 translateToLocTuple(mdCallee, calleeReturnNode.getDescTuple());
5963 System.out.println("calleeReturnLocTuple=" + calleeReturnLocTuple);
5964 NTuple<Location> transaltedToCaller =
5965 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeReturnLocTuple);
5966 // System.out.println("translateToCallerLocTuple="
5967 // + translateToCallerLocTuple(min, mdCallee, mdCaller, calleeReturnLocTuple));
5968 if (transaltedToCaller.size() > 0) {
5969 nodeSet.addGlobalFlowTuple(translateToCallerLocTuple(min, mdCallee, mdCaller,
5970 calleeReturnLocTuple));
5974 System.out.println("min nodeSet=" + nodeSet);
5980 private NodeTupleSet getNodeTupleSetFromReturnNode(FlowGraph fg, FlowReturnNode node) {
5981 NodeTupleSet nts = new NodeTupleSet();
5983 Set<NTuple<Descriptor>> returnSet = node.getReturnTupleSet();
5985 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
5986 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5987 FlowNode flowNode = fg.getFlowNode(tuple);
5988 if (flowNode instanceof FlowReturnNode) {
5989 returnSet.addAll(recurGetNode(fg, (FlowReturnNode) flowNode));
5991 returnSet.add(tuple);
5995 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
5996 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator.next();
5997 nts.addTuple(nTuple);
6004 private Set<NTuple<Descriptor>> recurGetNode(FlowGraph fg, FlowReturnNode rnode) {
6006 Set<NTuple<Descriptor>> tupleSet = new HashSet<NTuple<Descriptor>>();
6008 Set<NTuple<Descriptor>> returnSet = rnode.getReturnTupleSet();
6009 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
6010 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
6011 FlowNode flowNode = fg.getFlowNode(tuple);
6012 if (flowNode instanceof FlowReturnNode) {
6013 tupleSet.addAll(recurGetNode(fg, (FlowReturnNode) flowNode));
6015 tupleSet.add(tuple);
6021 private NTuple<Descriptor> generateArgTuple(MethodDescriptor mdCaller, NodeTupleSet argTupleSet) {
6025 // if argTupleSet is empty, it comes from the top location
6026 if (argTupleSet.size() == 0) {
6027 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
6028 descTuple.add(LITERALDESC);
6032 Set<NTuple<Descriptor>> argTupleSetNonLiteral = new HashSet<NTuple<Descriptor>>();
6034 for (Iterator<NTuple<Descriptor>> iter = argTupleSet.iterator(); iter.hasNext();) {
6035 NTuple<Descriptor> descTuple = iter.next();
6036 if (!descTuple.get(0).equals(LITERALDESC)) {
6037 argTupleSetNonLiteral.add(descTuple);
6041 if (argTupleSetNonLiteral.size() > 1) {
6042 System.out.println("11");
6044 NTuple<Descriptor> interTuple =
6045 getFlowGraph(mdCaller).createIntermediateNode().getDescTuple();
6046 for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
6047 NTuple<Descriptor> tuple = idxIter.next();
6048 addFlowGraphEdge(mdCaller, tuple, interTuple);
6051 } else if (argTupleSetNonLiteral.size() == 1) {
6052 return argTupleSetNonLiteral.iterator().next();
6054 return argTupleSet.iterator().next();
6059 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
6060 // return true if inNode has in-flows to nodeSet
6062 if (nodeSet.contains(inNode)) {
6063 // in this case, the method directly returns a parameter variable.
6066 // Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
6067 Set<FlowNode> reachableSet = fg.getReachableSetFrom(inNode.getDescTuple());
6068 // System.out.println("inNode=" + inNode + " reachalbeSet=" + reachableSet);
6070 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
6071 FlowNode fn = (FlowNode) iterator.next();
6072 if (nodeSet.contains(fn)) {
6079 private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
6080 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
6083 private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
6087 Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
6088 if (mapIdxToTuple == null) {
6089 mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
6090 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
6092 mapIdxToTuple.put(new Integer(idx), argTuple);
6095 private void analyzeFlowLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en,
6096 NodeTupleSet nodeSet) {
6097 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
6098 tuple.add(LITERALDESC);
6099 nodeSet.addTuple(tuple);
6102 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
6103 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
6105 System.out.println("analyzeFlowArrayAccessNode aan=" + aan.printNode(0));
6106 String currentArrayAccessNodeExpStr = aan.printNode(0);
6107 arrayAccessNodeStack.push(aan.printNode(0));
6109 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
6110 NTuple<Descriptor> base =
6111 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
6112 System.out.println("-base=" + base);
6114 nodeSet.setMethodInvokeBaseDescTuple(base);
6115 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
6116 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
6118 arrayAccessNodeStack.pop();
6121 // need to create an edge from idx to array
6122 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
6123 NTuple<Descriptor> idxTuple = idxIter.next();
6124 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
6125 NTuple<Descriptor> arrTuple = arrIter.next();
6126 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
6130 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6131 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
6133 NTuple<Location> calleeReturnLocTuple = iterator.next();
6134 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
6135 NTuple<Descriptor> arrTuple = arrIter.next();
6137 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, arrTuple));
6141 nodeSet.addTupleSet(expNodeTupleSet);
6144 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
6146 nodeSetArrayAccessExp.addTupleSet(expNodeTupleSet);
6147 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
6149 if (arrayAccessNodeStack.isEmpty()
6150 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
6152 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
6153 System.out.println("1");
6154 FlowNode interNode = getFlowGraph(md).createIntermediateNode();
6155 NTuple<Descriptor> interTuple = interNode.getDescTuple();
6157 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter.hasNext();) {
6158 NTuple<Descriptor> higherTuple = iter.next();
6159 addFlowGraphEdge(md, higherTuple, interTuple);
6161 nodeSetArrayAccessExp.clear();
6162 nodeSetArrayAccessExp.addTuple(interTuple);
6163 FlowGraph fg = getFlowGraph(md);
6165 System.out.println("base=" + base);
6167 fg.addMapInterLocNodeToEnclosingDescriptor(interTuple.get(0),
6168 getClassTypeDescriptor(base.get(base.size() - 1)));
6169 interNode.setBaseTuple(base);
6174 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
6175 nodeSet.addTupleSet(nodeSetArrayAccessExp);
6181 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
6182 CreateObjectNode en) {
6183 // TODO Auto-generated method stub
6187 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
6188 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
6190 NodeTupleSet leftOpSet = new NodeTupleSet();
6191 NodeTupleSet rightOpSet = new NodeTupleSet();
6193 System.out.println("analyzeFlowOpNode=" + on.printNode(0));
6196 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
6198 System.out.println("--leftOpSet=" + leftOpSet);
6200 if (on.getRight() != null) {
6202 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
6203 implicitFlowTupleSet, false);
6205 System.out.println("--rightOpSet=" + rightOpSet);
6207 Operation op = on.getOp();
6209 switch (op.getOp()) {
6211 case Operation.UNARYPLUS:
6212 case Operation.UNARYMINUS:
6213 case Operation.LOGIC_NOT:
6215 nodeSet.addTupleSet(leftOpSet);
6218 case Operation.LOGIC_OR:
6219 case Operation.LOGIC_AND:
6220 case Operation.COMP:
6221 case Operation.BIT_OR:
6222 case Operation.BIT_XOR:
6223 case Operation.BIT_AND:
6224 case Operation.ISAVAILABLE:
6225 case Operation.EQUAL:
6226 case Operation.NOTEQUAL:
6233 case Operation.MULT:
6236 case Operation.LEFTSHIFT:
6237 case Operation.RIGHTSHIFT:
6238 case Operation.URIGHTSHIFT:
6240 // there are two operands
6241 nodeSet.addTupleSet(leftOpSet);
6242 nodeSet.addTupleSet(rightOpSet);
6244 nodeSet.addGlobalFlowTupleSet(leftOpSet.getGlobalLocTupleSet());
6245 nodeSet.addGlobalFlowTupleSet(rightOpSet.getGlobalLocTupleSet());
6250 throw new Error(op.toString());
6255 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
6256 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
6259 base = new NTuple<Descriptor>();
6262 NameDescriptor nd = nn.getName();
6264 if (nd.getBase() != null) {
6266 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
6267 implicitFlowTupleSet, false);
6269 // base node has the top location
6273 String varname = nd.toString();
6274 if (varname.equals("this")) {
6276 base.add(md.getThis());
6280 Descriptor d = (Descriptor) nametable.get(varname);
6282 if (d instanceof VarDescriptor) {
6283 VarDescriptor vd = (VarDescriptor) d;
6285 } else if (d instanceof FieldDescriptor) {
6286 // the type of field descriptor has a location!
6287 FieldDescriptor fd = (FieldDescriptor) d;
6288 if (fd.isStatic()) {
6290 // if it is 'static final', no need to have flow node for the TOP
6294 // if 'static', assign the default GLOBAL LOCATION to the first
6295 // element of the tuple
6296 base.add(GLOBALDESC);
6299 // the location of field access starts from this, followed by field
6301 base.add(md.getThis());
6305 } else if (d == null) {
6306 // access static field
6307 base.add(GLOBALDESC);
6308 base.add(nn.getField());
6311 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
6313 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
6314 // String globalLocId = localLattice.getGlobalLoc();
6315 // if (globalLocId == null) {
6317 // Error("Method lattice does not define global variable location at "
6318 // + generateErrorMessage(md.getClassDesc(), nn));
6320 // loc.addLocation(new Location(md, globalLocId));
6322 // Location fieldLoc = (Location) fd.getType().getExtension();
6323 // loc.addLocation(fieldLoc);
6329 getFlowGraph(md).createNewFlowNode(base);
6335 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
6336 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
6337 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
6338 // System.out.println("analyzeFlowFieldAccessNode=" + fan.printNode(0));
6340 String currentArrayAccessNodeExpStr = null;
6341 ExpressionNode left = fan.getExpression();
6342 TypeDescriptor ltd = left.getType();
6343 FieldDescriptor fd = fan.getField();
6344 ArrayAccessNode aan = null;
6346 String varName = null;
6347 if (left.kind() == Kind.NameNode) {
6348 NameDescriptor nd = ((NameNode) left).getName();
6349 varName = nd.toString();
6352 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
6353 // using a class name directly or access using this
6354 if (fd.isStatic() && fd.isFinal()) {
6359 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
6361 boolean isArrayCase = false;
6362 if (left instanceof ArrayAccessNode) {
6365 aan = (ArrayAccessNode) left;
6367 currentArrayAccessNodeExpStr = aan.printNode(0);
6368 arrayAccessNodeStack.push(currentArrayAccessNodeExpStr);
6370 left = aan.getExpression();
6371 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
6372 implicitFlowTupleSet, isLHS);
6376 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
6379 // in this case, field is TOP location
6383 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
6385 if (!left.getType().isPrimitive()) {
6386 if (!fd.getSymbol().equals("length")) {
6387 // array.length access, just have the location of the array
6388 flowFieldTuple.add(fd);
6389 nodeSet.removeTuple(base);
6392 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
6395 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
6396 NTuple<Descriptor> idxTuple = idxIter.next();
6397 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
6400 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6401 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
6403 NTuple<Location> calleeReturnLocTuple = iterator.next();
6405 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6406 translateToLocTuple(md, flowFieldTuple));
6410 nodeSet.addTupleSet(idxNodeTupleSet);
6412 // if it is the array case and not the LHS case
6414 arrayAccessNodeStack.pop();
6416 if (arrayAccessNodeStack.isEmpty()
6417 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
6418 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
6420 nodeSetArrayAccessExp.addTuple(flowFieldTuple);
6421 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
6422 nodeSetArrayAccessExp.addTupleSet(nodeSet);
6424 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
6425 System.out.println("4");
6426 System.out.println("nodeSetArrayAccessExp=" + nodeSetArrayAccessExp);
6427 // System.out.println("idxNodeTupleSet.getGlobalLocTupleSet()="
6428 // + idxNodeTupleSet.getGlobalLocTupleSet());
6430 NTuple<Descriptor> interTuple =
6431 getFlowGraph(md).createIntermediateNode().getDescTuple();
6433 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter
6435 NTuple<Descriptor> higherTuple = iter.next();
6436 addFlowGraphEdge(md, higherTuple, interTuple);
6439 FlowGraph fg = getFlowGraph(md);
6440 fg.addMapInterLocNodeToEnclosingDescriptor(interTuple.get(0),
6441 getClassTypeDescriptor(base.get(base.size() - 1)));
6444 flowFieldTuple = interTuple;
6446 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
6452 return flowFieldTuple;
6457 private void debug_printTreeNode(TreeNode tn) {
6459 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
6463 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
6464 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
6465 NodeTupleSet implicitFlowTupleSet) {
6467 NodeTupleSet nodeSetRHS = new NodeTupleSet();
6468 NodeTupleSet nodeSetLHS = new NodeTupleSet();
6470 boolean postinc = true;
6471 if (an.getOperation().getBaseOp() == null
6472 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
6473 .getBaseOp().getOp() != Operation.POSTDEC)) {
6476 // if LHS is array access node, need to capture value flows between an array
6477 // and its index value
6478 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
6482 // analyze value flows of rhs expression
6483 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
6486 System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
6487 System.out.println("-nodeSetLHS=" + nodeSetLHS);
6488 System.out.println("-nodeSetRHS=" + nodeSetRHS);
6489 System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
6490 // System.out.println("-");
6492 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
6493 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
6495 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
6496 NTuple<Descriptor> fromTuple = iter.next();
6497 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6498 NTuple<Descriptor> toTuple = iter2.next();
6499 addFlowGraphEdge(md, fromTuple, toTuple);
6504 // creates edges from RHS to LHS
6505 NTuple<Descriptor> interTuple = null;
6506 if (needToGenerateInterLoc(nodeSetRHS)) {
6507 System.out.println("2");
6508 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
6511 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
6512 NTuple<Descriptor> fromTuple = iter.next();
6513 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6514 NTuple<Descriptor> toTuple = iter2.next();
6515 addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
6519 // creates edges from implicitFlowTupleSet to LHS
6520 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
6521 NTuple<Descriptor> fromTuple = iter.next();
6522 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6523 NTuple<Descriptor> toTuple = iter2.next();
6524 addFlowGraphEdge(md, fromTuple, toTuple);
6528 // create global flow edges if the callee gives return value flows to the caller
6529 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6530 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
6531 NTuple<Location> calleeReturnLocTuple = iterator.next();
6532 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6533 NTuple<Descriptor> callerLHSTuple = iter2.next();
6534 System.out.println("$$$ GLOBAL FLOW ADD=" + calleeReturnLocTuple + " -> "
6535 + translateToLocTuple(md, callerLHSTuple));
6537 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6538 translateToLocTuple(md, callerLHSTuple));
6542 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
6544 NTuple<Location> calleeReturnLocTuple = iterator.next();
6545 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6546 NTuple<Descriptor> callerLHSTuple = iter2.next();
6548 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6549 translateToLocTuple(md, callerLHSTuple));
6550 System.out.println("$$$ GLOBAL FLOW PCLOC ADD=" + calleeReturnLocTuple + " -> "
6551 + translateToLocTuple(md, callerLHSTuple));
6558 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6559 NTuple<Descriptor> tuple = iter2.next();
6560 addFlowGraphEdge(md, tuple, tuple);
6563 // creates edges from implicitFlowTupleSet to LHS
6564 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
6565 NTuple<Descriptor> fromTuple = iter.next();
6566 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6567 NTuple<Descriptor> toTuple = iter2.next();
6568 addFlowGraphEdge(md, fromTuple, toTuple);
6572 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6573 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
6575 NTuple<Location> calleeReturnLocTuple = iterator.next();
6576 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6577 NTuple<Descriptor> callerLHSTuple = iter2.next();
6578 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6579 translateToLocTuple(md, callerLHSTuple));
6580 System.out.println("$$$ GLOBAL FLOW PC ADD=" + calleeReturnLocTuple + " -> "
6581 + translateToLocTuple(md, callerLHSTuple));
6587 if (nodeSet != null) {
6588 nodeSet.addTupleSet(nodeSetLHS);
6589 nodeSet.addGlobalFlowTupleSet(nodeSetLHS.getGlobalLocTupleSet());
6593 public FlowGraph getFlowGraph(MethodDescriptor md) {
6594 return mapMethodDescriptorToFlowGraph.get(md);
6597 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
6598 NTuple<Descriptor> to) {
6599 FlowGraph graph = getFlowGraph(md);
6600 graph.addValueFlowEdge(from, to);
6604 private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
6605 NTuple<Descriptor> inter, NTuple<Descriptor> to) {
6607 FlowGraph graph = getFlowGraph(md);
6609 if (inter != null) {
6610 graph.addValueFlowEdge(from, inter);
6611 graph.addValueFlowEdge(inter, to);
6613 graph.addValueFlowEdge(from, to);
6618 public void writeInferredLatticeDotFile(ClassDescriptor cd, HierarchyGraph simpleHierarchyGraph,
6619 SSJavaLattice<String> locOrder, String nameSuffix) {
6620 System.out.println("@cd=" + cd);
6621 System.out.println("@sharedLoc=" + locOrder.getSharedLocSet());
6622 writeInferredLatticeDotFile(cd, null, simpleHierarchyGraph, locOrder, nameSuffix);
6625 public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
6626 HierarchyGraph simpleHierarchyGraph, SSJavaLattice<String> locOrder, String nameSuffix) {
6628 String fileName = "lattice_";
6631 cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
6633 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
6636 fileName += nameSuffix;
6638 System.out.println("***lattice=" + fileName + " setsize=" + locOrder.getKeySet().size());
6640 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
6642 Set<String> addedLocSet = new HashSet<String>();
6644 if (pairSet.size() > 0) {
6646 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
6648 bw.write("digraph " + fileName + " {\n");
6650 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
6651 // pair is in the form of <higher, lower>
6652 Pair<String, String> pair = (Pair<String, String>) iterator.next();
6654 String highLocId = pair.getFirst();
6655 String lowLocId = pair.getSecond();
6656 if (!addedLocSet.contains(highLocId)) {
6657 addedLocSet.add(highLocId);
6658 drawNode(bw, locOrder, simpleHierarchyGraph, highLocId);
6661 if (!addedLocSet.contains(lowLocId)) {
6662 addedLocSet.add(lowLocId);
6663 drawNode(bw, locOrder, simpleHierarchyGraph, lowLocId);
6666 bw.write(highLocId + " -> " + lowLocId + ";\n");
6671 } catch (IOException e) {
6672 e.printStackTrace();
6679 private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
6681 for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
6682 HNode merged = (HNode) iterator.next();
6683 if (merged.isMergeNode()) {
6684 str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
6686 str += " " + merged.getName();
6692 private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, HierarchyGraph graph,
6693 String locName) throws IOException {
6696 if (lattice.isSharedLoc(locName)) {
6697 prettyStr = locName + "*";
6699 prettyStr = locName;
6701 // HNode node = graph.getHNode(locName);
6702 // if (node != null && node.isMergeNode()) {
6703 // Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
6704 // prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
6706 bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
6709 public void _debug_writeFlowGraph() {
6710 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
6712 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
6713 MethodDescriptor md = (MethodDescriptor) iterator.next();
6714 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
6715 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
6718 subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
6719 } catch (IOException e) {
6720 e.printStackTrace();
6728 class CyclicFlowException extends Exception {
6732 class InterDescriptor extends Descriptor {
6734 Pair<MethodInvokeNode, Integer> minArgIdxPair;
6736 public InterDescriptor(String name) {
6740 public void setMethodArgIdxPair(MethodInvokeNode min, int idx) {
6741 minArgIdxPair = new Pair<MethodInvokeNode, Integer>(min, new Integer(idx));
6744 public Pair<MethodInvokeNode, Integer> getMethodArgIdxPair() {
6745 return minArgIdxPair;