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 descriptor to a naive lattice
81 private Map<Descriptor, SSJavaLattice<String>> desc2naiveLattice;
83 // map a class descriptor to a field lattice
84 private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
86 // map a method descriptor to a method lattice
87 private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
89 // map a method/class descriptor to a hierarchy graph
90 private Map<Descriptor, HierarchyGraph> mapDescriptorToHierarchyGraph;
92 // map a method/class descriptor to a skeleton hierarchy graph
93 private Map<Descriptor, HierarchyGraph> mapDescriptorToSkeletonHierarchyGraph;
95 private Map<Descriptor, HierarchyGraph> mapDescriptorToSimpleHierarchyGraph;
97 // map a method/class descriptor to a skeleton hierarchy graph with combination nodes
98 private Map<Descriptor, HierarchyGraph> mapDescriptorToCombineSkeletonHierarchyGraph;
100 // map a descriptor to a simple lattice
101 private Map<Descriptor, SSJavaLattice<String>> mapDescriptorToSimpleLattice;
103 // map a method descriptor to the set of method invocation nodes which are
104 // invoked by the method descriptor
105 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
107 private Map<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>> mapMethodInvokeNodeToArgIdxMap;
109 private Map<MethodInvokeNode, NTuple<Descriptor>> mapMethodInvokeNodeToBaseTuple;
111 private Map<MethodInvokeNode, Set<NTuple<Location>>> mapMethodInvokeNodeToPCLocTupleSet;
113 private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
115 private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
117 private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
119 private Map<MethodDescriptor, Set<FlowNode>> mapMethodDescToParamNodeFlowsToReturnValue;
121 private Map<String, Vector<String>> mapFileNameToLineVector;
123 private Map<Descriptor, Integer> mapDescToDefinitionLine;
125 private Map<Descriptor, LocationSummary> mapDescToLocationSummary;
127 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescToMethodInvokeNodeSet;
129 // maps a method descriptor to a sub global flow graph that captures all value flows caused by the
130 // set of callees reachable from the method
131 private Map<MethodDescriptor, GlobalFlowGraph> mapMethodDescriptorToSubGlobalFlowGraph;
133 private Map<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>> mapMethodInvokeNodeToMapCallerArgToCalleeArg;
135 private Map<MethodDescriptor, Boolean> mapMethodDescriptorToCompositeReturnCase;
137 private Map<MethodDescriptor, MethodDescriptor> mapMethodDescToHighestOverriddenMethodDesc;
139 private Map<MethodDescriptor, Set<MethodDescriptor>> mapHighestOverriddenMethodDescToMethodDescSet;
141 private Map<MethodDescriptor, NTuple<Descriptor>> mapHighestOverriddenMethodDescToReturnLocTuple;
143 private Map<MethodDescriptor, NTuple<Descriptor>> mapHighestOverriddenMethodDescToPCLocTuple;
145 private Map<MethodDescriptor, Set<NTuple<Descriptor>>> mapHighestOverriddenMethodDescToSetLowerThanPCLoc;
147 private Map<MethodDescriptor, Set<NTuple<Descriptor>>> mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc;
149 public static final String GLOBALLOC = "GLOBALLOC";
151 public static final String INTERLOC = "INTERLOC";
153 public static final String PCLOC = "_PCLOC_";
155 public static final String RLOC = "_RLOC_";
157 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
159 public static final Descriptor TOPDESC = new NameDescriptor(SSJavaAnalysis.TOP);
161 public static final Descriptor BOTTOMDESC = new NameDescriptor(SSJavaAnalysis.BOTTOM);
163 public static final Descriptor RETURNLOC = new NameDescriptor(RLOC);
165 public static final Descriptor LITERALDESC = new NameDescriptor("LITERAL");
167 public static final HNode TOPHNODE = new HNode(TOPDESC);
169 public static final HNode BOTTOMHNODE = new HNode(BOTTOMDESC);
171 public static String newline = System.getProperty("line.separator");
173 LocationInfo curMethodInfo;
175 private boolean hasChanges = false;
177 boolean debug = true;
179 public static int locSeed = 0;
181 private Stack<String> arrayAccessNodeStack;
183 private ClassDescriptor rootClassDescriptor;
185 private BuildLattice buildLattice;
187 public static int numLocationsSInfer = 0;
188 public static int numLocationsNaive = 0;
190 public LocationInference(SSJavaAnalysis ssjava, State state, TypeUtil tu) {
191 this.ssjava = ssjava;
194 this.toanalyze_classDescSet = new HashSet<ClassDescriptor>();
195 this.temp_toanalyzeList = new ArrayList<ClassDescriptor>();
196 this.temp_toanalyzeMethodList = new ArrayList<MethodDescriptor>();
197 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
198 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
199 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
200 this.desc2naiveLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
202 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
203 this.mapMethodDescriptorToMethodInvokeNodeSet =
204 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
205 this.mapMethodInvokeNodeToArgIdxMap =
206 new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
207 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
208 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
209 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
211 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
212 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
213 this.mapMethodDescToParamNodeFlowsToReturnValue =
214 new HashMap<MethodDescriptor, Set<FlowNode>>();
216 this.mapDescriptorToHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
217 this.mapMethodInvokeNodeToBaseTuple = new HashMap<MethodInvokeNode, NTuple<Descriptor>>();
219 this.mapDescriptorToSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
220 this.mapDescriptorToCombineSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
221 this.mapDescriptorToSimpleHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
223 this.mapDescriptorToSimpleLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
225 this.mapDescToLocationSummary = new HashMap<Descriptor, LocationSummary>();
227 this.mapMethodDescriptorToSubGlobalFlowGraph = new HashMap<MethodDescriptor, GlobalFlowGraph>();
229 this.mapMethodInvokeNodeToMapCallerArgToCalleeArg =
230 new HashMap<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>>();
232 this.mapMethodInvokeNodeToPCLocTupleSet =
233 new HashMap<MethodInvokeNode, Set<NTuple<Location>>>();
235 this.arrayAccessNodeStack = new Stack<String>();
237 this.mapMethodDescToMethodInvokeNodeSet =
238 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
240 this.mapMethodDescriptorToCompositeReturnCase = new HashMap<MethodDescriptor, Boolean>();
242 mapMethodDescToHighestOverriddenMethodDesc = new HashMap<MethodDescriptor, MethodDescriptor>();
244 mapHighestOverriddenMethodDescToSetLowerThanPCLoc =
245 new HashMap<MethodDescriptor, Set<NTuple<Descriptor>>>();
247 mapHighestOverriddenMethodDescToMethodDescSet =
248 new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
250 mapHighestOverriddenMethodDescToReturnLocTuple =
251 new HashMap<MethodDescriptor, NTuple<Descriptor>>();
253 mapHighestOverriddenMethodDescToPCLocTuple =
254 new HashMap<MethodDescriptor, NTuple<Descriptor>>();
256 mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc =
257 new HashMap<MethodDescriptor, Set<NTuple<Descriptor>>>();
259 this.buildLattice = new BuildLattice(this);
263 public void setupToAnalyze() {
264 SymbolTable classtable = state.getClassSymbolTable();
265 temp_toanalyzeList.clear();
266 temp_toanalyzeList.addAll(classtable.getValueSet());
267 // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
268 // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
269 // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
274 public void setupToAnalazeMethod(ClassDescriptor cd) {
276 SymbolTable methodtable = cd.getMethodTable();
277 temp_toanalyzeMethodList.clear();
278 temp_toanalyzeMethodList.addAll(methodtable.getValueSet());
279 Collections.sort(temp_toanalyzeMethodList, new Comparator<MethodDescriptor>() {
280 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
281 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
286 public boolean toAnalyzeMethodIsEmpty() {
287 return temp_toanalyzeMethodList.isEmpty();
290 public boolean toAnalyzeIsEmpty() {
291 return temp_toanalyzeList.isEmpty();
294 public ClassDescriptor toAnalyzeNext() {
295 return temp_toanalyzeList.remove(0);
298 public MethodDescriptor toAnalyzeMethodNext() {
299 return temp_toanalyzeMethodList.remove(0);
302 public void inference() {
306 // construct value flow graph
307 constructFlowGraph();
309 constructGlobalFlowGraph();
313 assignCompositeLocation();
315 calculateExtraLocations();
316 addAdditionalOrderingConstraints();
318 _debug_writeFlowGraph();
320 buildInheritanceTree();
321 calculateReturnPCLocInheritance();
323 constructHierarchyGraph();
325 addInheritanceConstraintsToHierarchyGraph();
327 debug_writeHierarchyDotFiles();
329 simplifyHierarchyGraph();
331 debug_writeSimpleHierarchyDotFiles();
333 constructSkeletonHierarchyGraph();
335 debug_writeSkeletonHierarchyDotFiles();
337 insertCombinationNodes();
339 debug_writeSkeletonCombinationHierarchyDotFiles();
341 buildLatticeInheritanceTree();
344 debug_writeLattices();
346 updateCompositeLocationAssignments();
348 generateMethodSummary();
350 generateAnnoatedCode();
352 for (Iterator iterator = cd2lattice.keySet().iterator(); iterator.hasNext();) {
353 ClassDescriptor cd = (ClassDescriptor) iterator.next();
354 SSJavaLattice<String> lattice = getLattice(cd);
355 HierarchyGraph hg = mapDescriptorToHierarchyGraph.get(cd);
356 // System.out.println("~~~\t" + cd + "\t" + lattice.getKeySet().size() + "\t"
357 // + hg.getNodeSet().size());
360 for (Iterator iterator = md2lattice.keySet().iterator(); iterator.hasNext();) {
361 MethodDescriptor md = (MethodDescriptor) iterator.next();
362 SSJavaLattice<String> locOrder = getLattice(md);
363 // writeLatticeDotFile(md.getClassDesc(), md, getMethodLattice(md));
364 HierarchyGraph hg = mapDescriptorToHierarchyGraph.get(md);
365 // System.out.println("~~~\t" + md.getClassDesc() + "_" + md + "\t"
366 // + locOrder.getKeySet().size() + "\t" + hg.getNodeSet().size());
369 if (state.SSJAVA_INFER_NAIVE_WRITEDOTS) {
370 System.out.println("The number of elements: Naive=" + numLocationsNaive);
372 System.out.println("The number of elements: SInfer=" + numLocationsSInfer);
378 private void calculateReturnPCLocInheritance() {
379 calculateHighestPCLocInheritance();
380 calculateLowestReturnLocInheritance();
381 updateFlowGraphPCReturnLocInheritance();
384 private void updateFlowGraphPCReturnLocInheritance() {
386 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
387 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
388 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
390 if (mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc).size() == 1) {
394 Set<MethodDescriptor> methodDescSet =
395 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
397 NTuple<Descriptor> highestPCLocDescTuple =
398 mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc);
400 NTuple<Descriptor> highestRETURNLocDescTuple =
401 mapHighestOverriddenMethodDescToReturnLocTuple.get(highestMethodDesc);
403 // System.out.println("\n$$$$$$$$$$$$$$$$updateFlowGraphPCReturnLocInheritance="
404 // + highestMethodDesc);
405 // System.out.println("-----highestPCLoc=" + highestPCLocDescTuple);
406 // System.out.println("-----highestRETURNLoc=" + highestRETURNLocDescTuple);
408 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
409 MethodDescriptor md = (MethodDescriptor) iterator2.next();
410 // System.out.println("\n --------MD=" + md);
411 FlowGraph flowGraph = getFlowGraph(md);
413 MethodSummary summary = getMethodSummary(md);
414 CompositeLocation curPCLoc = summary.getPCLoc();
415 NTuple<Descriptor> curPCDescTuple = translateToDescTuple(curPCLoc.getTuple());
416 // System.out.println("md=" + md + " curPCLoc=" + curPCLoc);
417 // System.out.println("highestPCLoc=" + highestPCLocDescTuple);
419 if (highestPCLocDescTuple == null) {
420 // this case: PCLOC is top
421 // System.out.println("###SET PCLOC AS TOP");
422 if (curPCDescTuple != null && !curPCLoc.get(0).isTop()) {
423 FlowNode pcFlowNode = flowGraph.getFlowNode(curPCDescTuple);
424 flowGraph.removeNode(pcFlowNode);
426 summary.setPCLoc(new CompositeLocation(new Location(md, Location.TOP)));
428 NTuple<Descriptor> newPCDescTuple = new NTuple<Descriptor>();
429 if (highestPCLocDescTuple.size() == 1) {
430 newPCDescTuple.add(highestPCLocDescTuple.get(0));
432 newPCDescTuple.add(md.getThis());
433 newPCDescTuple.add(highestPCLocDescTuple.get(1));
435 if (!curPCDescTuple.equals(newPCDescTuple)) {
436 FlowNode pcFlowNode = flowGraph.getFlowNode(curPCDescTuple);
437 flowGraph.updateTuple(pcFlowNode, newPCDescTuple);
438 // flowGraph.removeNode(pcFlowNode);
439 Set<NTuple<Descriptor>> descSetLowerThanPCLoc =
440 mapHighestOverriddenMethodDescToSetLowerThanPCLoc.get(highestMethodDesc);
441 for (Iterator iterator3 = descSetLowerThanPCLoc.iterator(); iterator3.hasNext();) {
442 NTuple<Descriptor> lowerNTuple = (NTuple<Descriptor>) iterator3.next();
443 flowGraph.addValueFlowEdge(newPCDescTuple, lowerNTuple);
445 CompositeLocation newPCCompLoc =
446 new CompositeLocation(translateToLocTuple(md, newPCDescTuple));
447 summary.setPCLoc(newPCCompLoc);
453 if (highestRETURNLocDescTuple != null) {
454 CompositeLocation curRETURNLoc = summary.getRETURNLoc();
455 NTuple<Descriptor> curReturnDescTuple = translateToDescTuple(curRETURNLoc.getTuple());
457 if (!curReturnDescTuple.equals(highestRETURNLocDescTuple)) {
458 // handle the case that RETURNLOC is started with 'this'...
459 NTuple<Descriptor> newRETURNLocDescTuple = new NTuple<Descriptor>();
460 if (highestRETURNLocDescTuple.size() == 1) {
461 newRETURNLocDescTuple.add(highestRETURNLocDescTuple.get(0));
463 newRETURNLocDescTuple.add(md.getThis());
464 newRETURNLocDescTuple.add(highestRETURNLocDescTuple.get(1));
467 FlowNode returnFlowNode = flowGraph.getFlowNode(curReturnDescTuple);
468 flowGraph.updateTuple(returnFlowNode, newRETURNLocDescTuple);
470 Set<NTuple<Descriptor>> descSetHigherThanRETURNLoc =
471 mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc.get(highestMethodDesc);
472 for (Iterator iterator3 = descSetHigherThanRETURNLoc.iterator(); iterator3.hasNext();) {
473 NTuple<Descriptor> higherNTuple = (NTuple<Descriptor>) iterator3.next();
474 flowGraph.addValueFlowEdge(higherNTuple, newRETURNLocDescTuple);
477 CompositeLocation newRETURNLocCompLoc =
478 new CompositeLocation(translateToLocTuple(md, newRETURNLocDescTuple));
479 summary.setRETURNLoc(newRETURNLocCompLoc);
486 private void calculateHighestPCLocInheritance() {
488 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
490 Map<MethodDescriptor, Integer> mapMethodDescToParamCount =
491 new HashMap<MethodDescriptor, Integer>();
493 next: for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
494 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
496 NTuple<Descriptor> tempTuple = null;
498 if (getMethodSummary(highestMethodDesc).getPCLoc() != null) {
500 Set<MethodDescriptor> methodDescSet =
501 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
503 if (methodDescSet.size() > 1) {
504 // System.out.println("---method desc set=" + methodDescSet + " from=" +
505 // highestMethodDesc);
510 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
511 MethodDescriptor md = (MethodDescriptor) iterator2.next();
513 FlowGraph flowGraph = getFlowGraph(md);
514 if (flowGraph == null) {
517 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
518 // System.out.println("###md=" + md + " paramNodeSet=" + paramNodeSet);
520 CompositeLocation pcLOC = getMethodSummary(md).getPCLoc();
521 // System.out.println("---pcLOC=" + pcLOC);
523 if (md.equals(highestMethodDesc)) {
524 mapHighestOverriddenMethodDescToPCLocTuple.put(highestMethodDesc,
525 translateToDescTuple(pcLOC.getTuple()));
528 if (!pcLOC.get(0).isTop()) {
530 FlowNode pcFlowNode = flowGraph.getFlowNode(translateToDescTuple(pcLOC.getTuple()));
533 for (Iterator iterator3 = paramNodeSet.iterator(); iterator3.hasNext();) {
534 FlowNode paramNode = (FlowNode) iterator3.next();
535 if (flowGraph.getReachableSetFrom(pcFlowNode.getCurrentDescTuple().subList(0, 1))
536 .contains(paramNode)) {
540 mapMethodDescToParamCount.put(md, count);
544 // the PC location is top
545 // if one of pcloc among the method inheritance chain has the TOP,
546 // all methods in the same chain should have the TOP.
547 mapHighestOverriddenMethodDescToPCLocTuple.remove(highestMethodDesc);
548 // System.out.println("highest=" + highestMethodDesc + " HIGHEST PCLOC="
549 // + mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc));
551 Set<NTuple<Descriptor>> descTupleSetLowerThanPC = new HashSet<NTuple<Descriptor>>();
552 for (Iterator iterator3 = paramNodeSet.iterator(); iterator3.hasNext();) {
553 FlowNode flowNode = (FlowNode) iterator3.next();
554 descTupleSetLowerThanPC.add(flowNode.getCurrentDescTuple());
556 mapHighestOverriddenMethodDescToSetLowerThanPCLoc.put(highestMethodDesc,
557 descTupleSetLowerThanPC);
563 // identify which method in the inheritance chain has the highest PCLOC
564 // basically, finds a method that has the highest count or TOP location
565 int highestCount = -1;
566 MethodDescriptor methodDescHighestCount = null;
567 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
568 MethodDescriptor methodDesc = (MethodDescriptor) iterator2.next();
569 if (mapMethodDescToParamCount.containsKey(methodDesc)) {
570 int curCount = mapMethodDescToParamCount.get(methodDesc).intValue();
571 if (highestCount < curCount) {
572 highestCount = curCount;
573 methodDescHighestCount = methodDesc;
578 if (methodDescHighestCount != null) {
579 FlowGraph flowGraph = getFlowGraph(methodDescHighestCount);
580 CompositeLocation pcLOC = getMethodSummary(methodDescHighestCount).getPCLoc();
581 FlowNode pcFlowNode = flowGraph.getFlowNode(translateToDescTuple(pcLOC.getTuple()));
582 Set<FlowNode> reachableSet =
583 flowGraph.getReachableSetFrom(pcFlowNode.getCurrentDescTuple().subList(0, 1));
585 Set<FlowNode> reachableParamNodeSet = new HashSet<FlowNode>();
586 for (Iterator iterator3 = reachableSet.iterator(); iterator3.hasNext();) {
587 FlowNode flowNode = (FlowNode) iterator3.next();
588 if (flowGraph.isParameter(flowNode.getCurrentDescTuple())) {
589 reachableParamNodeSet.add(flowNode);
594 Set<NTuple<Descriptor>> descTupleSetLowerThanPC = new HashSet<NTuple<Descriptor>>();
595 for (Iterator iterator2 = reachableParamNodeSet.iterator(); iterator2.hasNext();) {
596 FlowNode flowNode = (FlowNode) iterator2.next();
597 descTupleSetLowerThanPC.add(flowNode.getCurrentDescTuple());
600 // mapHighestOverriddenMethodDescToPCLocTuple.remove(highestMethodDesc);
601 mapHighestOverriddenMethodDescToSetLowerThanPCLoc.put(highestMethodDesc,
602 descTupleSetLowerThanPC);
607 // System.out.println("####################################");
608 // System.out.println(" highest=" + highestMethodDesc + " HIGHEST PCLOC="
609 // + mapHighestOverriddenMethodDescToPCLocTuple.get(highestMethodDesc));
610 // System.out.println(" setLowerThanPCLoc="
611 // + mapHighestOverriddenMethodDescToSetLowerThanPCLoc.get(highestMethodDesc));
616 private void calculateLowestReturnLocInheritance() {
618 Set<MethodDescriptor> keySet = mapHighestOverriddenMethodDescToMethodDescSet.keySet();
620 Map<MethodDescriptor, Integer> mapMethodDescToParamCount =
621 new HashMap<MethodDescriptor, Integer>();
623 next: for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
624 MethodDescriptor highestMethodDesc = (MethodDescriptor) iterator.next();
626 Set<MethodDescriptor> methodDescSet =
627 mapHighestOverriddenMethodDescToMethodDescSet.get(highestMethodDesc);
629 if (methodDescSet.size() > 1 && getMethodSummary(highestMethodDesc).getRETURNLoc() != null) {
634 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
635 MethodDescriptor md = (MethodDescriptor) iterator2.next();
637 FlowGraph flowGraph = getFlowGraph(md);
638 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
640 CompositeLocation returnLoc = getMethodSummary(md).getRETURNLoc();
642 FlowNode returnFlowNode = flowGraph.getFlowNode(translateToDescTuple(returnLoc.getTuple()));
645 for (Iterator iterator3 = paramNodeSet.iterator(); iterator3.hasNext();) {
646 FlowNode paramNode = (FlowNode) iterator3.next();
647 if (flowGraph.getReachableSetFrom(paramNode.getCurrentDescTuple().subList(0, 1))
648 .contains(returnFlowNode)) {
652 mapMethodDescToParamCount.put(md, count);
653 // System.out.println("###returnLoc=" + returnLoc + " count higher=" + count);
656 // identify which method in the inheritance chain has the highest PCLOC
657 // basically, finds a method that has the highest count or TOP location
658 int highestCount = -1;
659 MethodDescriptor methodDescHighestCount = null;
660 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
661 MethodDescriptor methodDesc = (MethodDescriptor) iterator2.next();
662 int curCount = mapMethodDescToParamCount.get(methodDesc).intValue();
663 if (highestCount < curCount) {
664 highestCount = curCount;
665 methodDescHighestCount = methodDesc;
669 if (methodDescHighestCount != null) {
670 FlowGraph flowGraph = getFlowGraph(methodDescHighestCount);
671 CompositeLocation returnLOC = getMethodSummary(methodDescHighestCount).getRETURNLoc();
672 NTuple<Descriptor> returnLocTuple = translateToDescTuple(returnLOC.getTuple());
673 FlowNode returnFlowNode = flowGraph.getFlowNode(returnLocTuple);
675 Set<FlowNode> curMethodParamNodeSet = flowGraph.getParamFlowNodeSet();
676 Set<NTuple<Descriptor>> descTupleSetHigherThanPC = new HashSet<NTuple<Descriptor>>();
677 for (Iterator iterator3 = curMethodParamNodeSet.iterator(); iterator3.hasNext();) {
678 FlowNode paramNode = (FlowNode) iterator3.next();
679 if (flowGraph.getReachableSetFrom(paramNode.getCurrentDescTuple().subList(0, 1))
680 .contains(returnFlowNode)) {
681 descTupleSetHigherThanPC.add(paramNode.getCurrentDescTuple());
685 mapHighestOverriddenMethodDescToReturnLocTuple.put(highestMethodDesc, returnLocTuple);
686 mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc.put(highestMethodDesc,
687 descTupleSetHigherThanPC);
691 // System.out.println("####################################");
692 // System.out.println(" highest=" + highestMethodDesc + " LOWEST RETURNLOC="
693 // + mapHighestOverriddenMethodDescToReturnLocTuple.get(highestMethodDesc));
694 // System.out.println(" setHigherThanReturnLoc="
695 // + mapHighestOverriddenMethodDescToSetHigherThanRETURNLoc.get(highestMethodDesc));
701 private void addMapHighestMethodDescToMethodDesc(MethodDescriptor highest, MethodDescriptor md) {
702 if (!mapHighestOverriddenMethodDescToMethodDescSet.containsKey(highest)) {
703 mapHighestOverriddenMethodDescToMethodDescSet.put(highest, new HashSet<MethodDescriptor>());
705 mapHighestOverriddenMethodDescToMethodDescSet.get(highest).add(md);
708 private void DFSInheritanceTreeCalculatingHighestOverriddenMethod(ClassDescriptor cd) {
710 // ClassDescriptor cd = node.getData();
712 for (Iterator iterator = cd.getMethods(); iterator.hasNext();) {
713 MethodDescriptor md = (MethodDescriptor) iterator.next();
714 MethodDescriptor highestMethodDesc = getHighestOverriddenMethod(md.getClassDesc(), md);
715 mapMethodDescToHighestOverriddenMethodDesc.put(md, highestMethodDesc);
716 addMapHighestMethodDescToMethodDesc(highestMethodDesc, md);
721 Set<ClassDescriptor> children = getDirectSubClasses(cd);
722 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
723 ClassDescriptor child = (ClassDescriptor) iterator.next();
724 DFSInheritanceTreeCalculatingHighestOverriddenMethod(child);
729 private MethodDescriptor getHighestOverriddenMethod(ClassDescriptor curClassDesc,
730 MethodDescriptor curMethodDesc) {
732 // Node<ClassDescriptor> curNode = inheritanceTree.getTreeNode(curClassDesc);
733 // Node<ClassDescriptor> parentNode = curNode.getParent();
734 ClassDescriptor parentClassDesc = curClassDesc.getSuperDesc();
736 if (parentClassDesc != null) {
737 if (parentClassDesc.getMethodTable().contains(curMethodDesc.getSymbol())) {
738 Set<MethodDescriptor> methodDescSet =
739 parentClassDesc.getMethodTable().getSet(curMethodDesc.getSymbol());
740 for (Iterator iterator = methodDescSet.iterator(); iterator.hasNext();) {
741 MethodDescriptor md = (MethodDescriptor) iterator.next();
742 if (md.matches(curMethodDesc)) {
743 return getHighestOverriddenMethod(parentClassDesc, md);
747 // traverse to the parent!
748 return getHighestOverriddenMethod(parentClassDesc, curMethodDesc);
750 return curMethodDesc;
753 private void buildInheritanceTree() {
755 DFSInheritanceTreeCalculatingHighestOverriddenMethod(rootClassDescriptor);
759 private void addInheritanceConstraintsToHierarchyGraph() {
761 // DFS the inheritance tree and propagates nodes/edges of parent to child
763 // Node<ClassDescriptor> rootNode = inheritanceTree.getRootNode();
764 DFSInheritanceTree(rootClassDescriptor);
768 private void DFSInheritanceTree(ClassDescriptor parentClassDescriptor) {
770 // ClassDescriptor parentClassDescriptor = parentNode.getData();
772 Set<ClassDescriptor> children = getDirectSubClasses(parentClassDescriptor);
773 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
774 ClassDescriptor childClassDescriptor = (ClassDescriptor) iterator.next();
776 HierarchyGraph parentGraph = getHierarchyGraph(parentClassDescriptor);
777 HierarchyGraph childGraph = getHierarchyGraph(childClassDescriptor);
779 Set<HNode> parentNodeSet = parentGraph.getNodeSet();
780 for (Iterator iterator2 = parentNodeSet.iterator(); iterator2.hasNext();) {
781 HNode hNode = (HNode) iterator2.next();
782 childGraph.addNode(hNode);
785 // copies extra information from the parent hierarchy graph
786 Map<HNode, Set<HNode>> parentMergeNodeMap = parentGraph.getMapHNodetoMergeSet();
787 Map<HNode, Set<HNode>> childMergeNodeMap = childGraph.getMapHNodetoMergeSet();
789 Set<HNode> keySet = parentMergeNodeMap.keySet();
790 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
791 HNode parentKey = (HNode) iterator2.next();
792 if (!childMergeNodeMap.containsKey(parentKey)) {
793 childMergeNodeMap.put(parentKey, new HashSet<HNode>());
795 childMergeNodeMap.get(parentKey).addAll(parentMergeNodeMap.get(parentKey));
798 // copies nodes/edges from the parent class...
799 for (Iterator iterator2 = parentNodeSet.iterator(); iterator2.hasNext();) {
800 HNode parentHNode = (HNode) iterator2.next();
802 Set<HNode> parentIncomingHNode = parentGraph.getIncomingNodeSet(parentHNode);
803 Set<HNode> parentOutgoingHNode = parentGraph.getOutgoingNodeSet(parentHNode);
805 for (Iterator iterator3 = parentIncomingHNode.iterator(); iterator3.hasNext();) {
806 HNode inHNode = (HNode) iterator3.next();
807 childGraph.addEdge(inHNode.getDescriptor(), parentHNode.getDescriptor());
810 for (Iterator iterator3 = parentOutgoingHNode.iterator(); iterator3.hasNext();) {
811 HNode outHNode = (HNode) iterator3.next();
812 childGraph.addEdge(parentHNode.getDescriptor(), outHNode.getDescriptor());
817 // copies nodes/edges from parent methods to overridden methods
819 for (Iterator iterator3 = childClassDescriptor.getMethods(); iterator3.hasNext();) {
820 MethodDescriptor childMethodDescriptor = (MethodDescriptor) iterator3.next();
822 MethodDescriptor parentMethodDesc =
823 getParentMethodDesc(childMethodDescriptor.getClassDesc(), childMethodDescriptor);
825 if (parentMethodDesc != null) {
827 HierarchyGraph parentMethodGraph = getHierarchyGraph(parentMethodDesc);
828 HierarchyGraph childMethodGraph = getHierarchyGraph(childMethodDescriptor);
830 Set<HNode> parentMethodNodeSet = parentMethodGraph.getNodeSet();
831 for (Iterator iterator2 = parentMethodNodeSet.iterator(); iterator2.hasNext();) {
832 HNode hNode = (HNode) iterator2.next();
833 childMethodGraph.addNode(hNode);
836 // copies extra information from the parent hierarchy graph
837 Map<HNode, Set<HNode>> parentMethodMergeNodeMap =
838 parentMethodGraph.getMapHNodetoMergeSet();
839 Map<HNode, Set<HNode>> childMethodMergeNodeMap = childMethodGraph.getMapHNodetoMergeSet();
841 Set<HNode> methodKeySet = parentMethodMergeNodeMap.keySet();
842 for (Iterator iterator2 = methodKeySet.iterator(); iterator2.hasNext();) {
843 HNode parentKey = (HNode) iterator2.next();
844 if (!childMethodMergeNodeMap.containsKey(parentKey)) {
845 childMethodMergeNodeMap.put(parentKey, new HashSet<HNode>());
847 childMethodMergeNodeMap.get(parentKey).addAll(parentMethodMergeNodeMap.get(parentKey));
850 // copies nodes/edges from the parent method...
851 for (Iterator iterator2 = parentMethodGraph.getNodeSet().iterator(); iterator2.hasNext();) {
852 HNode parentHNode = (HNode) iterator2.next();
854 Set<HNode> parentIncomingHNode = parentMethodGraph.getIncomingNodeSet(parentHNode);
855 Set<HNode> parentOutgoingHNode = parentMethodGraph.getOutgoingNodeSet(parentHNode);
857 for (Iterator iterator4 = parentIncomingHNode.iterator(); iterator4.hasNext();) {
858 HNode inHNode = (HNode) iterator4.next();
859 childMethodGraph.addEdge(inHNode, parentHNode);
862 for (Iterator iterator4 = parentOutgoingHNode.iterator(); iterator4.hasNext();) {
863 HNode outHNode = (HNode) iterator4.next();
864 childMethodGraph.addEdge(parentHNode, outHNode);
873 DFSInheritanceTree(childClassDescriptor);
878 public MethodDescriptor getParentMethodDesc(ClassDescriptor classDesc, MethodDescriptor methodDesc) {
880 // Node<ClassDescriptor> childNode = inheritanceTree.getTreeNode(classDesc);
881 ClassDescriptor parentClassDesc = classDesc.getSuperDesc();
882 // Node<ClassDescriptor> parentNode = childNode.getParent();
884 if (parentClassDesc != null) {
885 // ClassDescriptor parentClassDesc = parentNode.getData();
886 if (parentClassDesc.getMethodTable().contains(methodDesc.getSymbol())) {
887 Set<MethodDescriptor> methodDescSet =
888 parentClassDesc.getMethodTable().getSet(methodDesc.getSymbol());
889 for (Iterator iterator = methodDescSet.iterator(); iterator.hasNext();) {
890 MethodDescriptor md = (MethodDescriptor) iterator.next();
891 if (md.matches(methodDesc)) {
897 // traverse to the parent!
898 getParentMethodDesc(parentClassDesc, methodDesc);
905 private void checkReturnNodes() {
906 LinkedList<MethodDescriptor> methodDescList =
907 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
909 while (!methodDescList.isEmpty()) {
910 MethodDescriptor md = methodDescList.removeLast();
912 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
913 checkFlowNodeReturnThisField(md);
915 // // in this case, this method will return the composite location that starts with 'this'
916 // FlowGraph flowGraph = getFlowGraph(md);
917 // Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
924 private void addSuperClasses(ClassDescriptor cd) {
925 ClassDescriptor parentClassDesc = cd.getSuperDesc();
926 if (parentClassDesc != null) {
927 toanalyze_classDescSet.add(parentClassDesc);
928 addSuperClasses(parentClassDesc);
932 private void updateFlowGraph() {
934 LinkedList<MethodDescriptor> methodDescList =
935 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
937 while (!methodDescList.isEmpty()) {
938 MethodDescriptor md = methodDescList.removeLast();
939 if (state.SSJAVADEBUG) {
940 System.out.println();
941 System.out.println("SSJAVA: Updating a flow graph: " + md);
942 propagateFlowsFromCalleesWithNoCompositeLocation(md);
945 Set<FlowNode> nodeSet = getFlowGraph(md).getNodeSet();
946 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
947 FlowNode flowNode = (FlowNode) iterator.next();
948 NTuple<Descriptor> descTuple = flowNode.getCurrentDescTuple();
949 NTuple<Location> locTuple = translateToLocTuple(md, descTuple);
950 for (int i = 0; i < locTuple.size(); i++) {
951 Location loc = locTuple.get(i);
952 if (loc.getDescriptor() instanceof ClassDescriptor) {
953 ClassDescriptor classDesc = (ClassDescriptor) loc.getDescriptor();
954 toanalyze_classDescSet.add(classDesc);
955 addSuperClasses(classDesc);
956 } else if (loc.getDescriptor() instanceof MethodDescriptor) {
957 toanalyze_classDescSet.add(((MethodDescriptor) loc.getDescriptor()).getClassDesc());
966 public Map<NTuple<Descriptor>, NTuple<Descriptor>> getMapCallerArgToCalleeParam(
967 MethodInvokeNode min) {
969 if (!mapMethodInvokeNodeToMapCallerArgToCalleeArg.containsKey(min)) {
970 mapMethodInvokeNodeToMapCallerArgToCalleeArg.put(min,
971 new HashMap<NTuple<Descriptor>, NTuple<Descriptor>>());
974 return mapMethodInvokeNodeToMapCallerArgToCalleeArg.get(min);
977 public void addMapCallerArgToCalleeParam(MethodInvokeNode min, NTuple<Descriptor> callerArg,
978 NTuple<Descriptor> calleeParam) {
979 getMapCallerArgToCalleeParam(min).put(callerArg, calleeParam);
982 private void assignCompositeLocation() {
983 calculateGlobalValueFlowCompositeLocation();
984 translateCompositeLocationAssignmentToFlowGraph();
987 private void translateCompositeLocationAssignmentToFlowGraph() {
988 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
989 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
990 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
993 private void translateCompositeLocationAssignmentToFlowGraph2() {
994 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
995 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
996 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
999 private void addAdditionalOrderingConstraints() {
1000 System.out.println("\nSSJAVA: Add addtional ordering constriants:");
1001 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
1002 addAddtionalOrderingConstraints(methodEventLoopDesc);
1003 // calculateReturnHolderLocation();
1006 private void calculateReturnHolderLocation() {
1007 LinkedList<MethodDescriptor> methodDescList =
1008 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
1010 while (!methodDescList.isEmpty()) {
1011 MethodDescriptor md = methodDescList.removeLast();
1013 FlowGraph fg = getFlowGraph(md);
1014 Set<FlowNode> nodeSet = fg.getNodeSet();
1015 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1016 FlowNode flowNode = (FlowNode) iterator.next();
1017 if (flowNode.isFromHolder()) {
1018 calculateCompositeLocationFromFlowGraph(md, flowNode);
1025 private void updateCompositeLocationAssignments() {
1027 LinkedList<MethodDescriptor> methodDescList =
1028 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
1030 while (!methodDescList.isEmpty()) {
1031 MethodDescriptor md = methodDescList.removeLast();
1033 // System.out.println("\n#updateCompositeLocationAssignments=" + md);
1035 FlowGraph flowGraph = getFlowGraph(md);
1037 MethodSummary methodSummary = getMethodSummary(md);
1039 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1040 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1041 FlowNode node = (FlowNode) iterator.next();
1042 // System.out.println("-node=" + node + " node.getDescTuple=" + node.getDescTuple());
1043 if (node.getCompositeLocation() != null) {
1044 CompositeLocation compLoc = node.getCompositeLocation();
1045 CompositeLocation updatedCompLoc = updateCompositeLocation(compLoc);
1046 node.setCompositeLocation(updatedCompLoc);
1047 // System.out.println("---updatedCompLoc1=" + updatedCompLoc);
1049 NTuple<Descriptor> descTuple = node.getDescTuple();
1050 // System.out.println("update desc=" + descTuple);
1051 CompositeLocation compLoc = convertToCompositeLocation(md, descTuple);
1052 compLoc = updateCompositeLocation(compLoc);
1053 node.setCompositeLocation(compLoc);
1054 // System.out.println("---updatedCompLoc2=" + compLoc);
1057 if (node.isDeclaratonNode()) {
1058 Descriptor localVarDesc = node.getDescTuple().get(0);
1059 CompositeLocation compLoc = updateCompositeLocation(node.getCompositeLocation());
1060 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, compLoc);
1064 // update PCLOC and RETURNLOC if they have a composite location assignment
1065 if (methodSummary.getRETURNLoc() != null) {
1066 methodSummary.setRETURNLoc(updateCompositeLocation(methodSummary.getRETURNLoc()));
1068 if (methodSummary.getPCLoc() != null) {
1069 methodSummary.setPCLoc(updateCompositeLocation(methodSummary.getPCLoc()));
1076 private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
1077 CompositeLocation updatedCompLoc = new CompositeLocation();
1078 for (int i = 0; i < compLoc.getSize(); i++) {
1079 Location loc = compLoc.get(i);
1080 String nodeIdentifier = loc.getLocIdentifier();
1081 Descriptor enclosingDesc = loc.getDescriptor();
1083 if (!enclosingDesc.equals(GLOBALDESC)) {
1084 LocationSummary locSummary = getLocationSummary(enclosingDesc);
1085 // HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(enclosingDesc);
1086 HierarchyGraph scGraph = getSimpleHierarchyGraph(enclosingDesc);
1087 if (scGraph != null) {
1088 HNode curNode = scGraph.getCurrentHNode(nodeIdentifier);
1089 // System.out.println("nodeID=" + nodeIdentifier + " curNode=" + curNode
1090 // + " enclosingDesc=" + enclosingDesc);
1091 if (curNode != null) {
1092 nodeIdentifier = curNode.getName();
1095 locName = locSummary.getLocationName(nodeIdentifier);
1097 locName = nodeIdentifier;
1099 Location updatedLoc = new Location(enclosingDesc, locName);
1100 updatedCompLoc.addLocation(updatedLoc);
1103 return updatedCompLoc;
1106 private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
1108 // System.out.println("\n\n###translateCompositeLocationAssignmentToFlowGraph mdCaller="
1111 // First, assign a composite location to a node in the flow graph
1112 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
1114 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
1115 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1116 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
1118 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
1119 for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
1120 Location methodLoc = (Location) iterator.next();
1121 if (methodLoc.getDescriptor().equals(mdCaller)) {
1122 CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
1123 assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
1127 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
1129 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
1130 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1131 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1132 // need to translate a composite location that is started with the base
1134 translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
1135 MethodDescriptor mdCallee = min.getMethod();
1136 calleeSet.add(mdCallee);
1140 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1141 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1142 translateCompositeLocationAssignmentToFlowGraph(callee);
1147 private void addAddtionalOrderingConstraints(MethodDescriptor mdCaller) {
1149 // First, assign a composite location to a node in the flow graph
1150 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
1152 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
1153 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1154 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
1155 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
1157 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
1159 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
1160 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1161 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1162 MethodDescriptor mdCallee = min.getMethod();
1163 calleeSet.add(mdCallee);
1166 // add an additional ordering constraint
1167 // if the first element of a parameter composite location matches 'this' reference,
1168 // the corresponding argument in the caller is required to be higher than the translated
1169 // parameter location in the caller lattice
1171 // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
1174 // update return flow nodes in the caller
1175 CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc();
1176 // System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc);
1177 if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis())
1178 && returnLoc.getSize() > 1) {
1179 // System.out.println("###RETURN COMP LOC=" + returnLoc);
1180 NTuple<Location> returnLocTuple = returnLoc.getTuple();
1181 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1182 // System.out.println("###basetuple=" + baseTuple);
1183 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
1184 for (int i = 1; i < returnLocTuple.size(); i++) {
1185 newReturnTuple.add(returnLocTuple.get(i).getLocDescriptor());
1187 // System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple);
1189 FlowReturnNode holderNode = callerFlowGraph.getFlowReturnNode(min);
1190 NodeTupleSet holderTupleSet =
1191 getNodeTupleSetFromReturnNode(getFlowGraph(mdCaller), holderNode);
1193 callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple);
1195 // then need to remove old constraints
1197 // System.out.println("###REMOVE OLD CONSTRAINTS=" + holderNode);
1198 for (Iterator<NTuple<Descriptor>> iter = holderTupleSet.iterator(); iter.hasNext();) {
1199 NTuple<Descriptor> tupleFromHolder = iter.next();
1200 Set<FlowEdge> holderOutEdge = callerFlowGraph.getOutEdgeSet(holderNode);
1201 for (Iterator iterator2 = holderOutEdge.iterator(); iterator2.hasNext();) {
1202 FlowEdge outEdge = (FlowEdge) iterator2.next();
1203 NTuple<Descriptor> toberemovedTuple = outEdge.getEndTuple();
1204 // System.out.println("---remove " + tupleFromHolder + " -> " + toberemovedTuple);
1205 callerFlowGraph.removeEdge(tupleFromHolder, toberemovedTuple);
1210 // if the return loc set was empty and later pcloc was connected to the return loc
1211 // need to make sure that return loc reflects to this changes.
1212 FlowReturnNode flowReturnNode = callerFlowGraph.getFlowReturnNode(min);
1213 if (flowReturnNode != null && flowReturnNode.getReturnTupleSet().isEmpty()) {
1215 if (needToUpdateReturnLocHolder(min.getMethod(), flowReturnNode)) {
1216 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1217 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
1218 flowReturnNode.addTuple(newReturnTuple);
1227 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1228 MethodDescriptor callee = (MethodDescriptor) iterator.next();
1229 addAddtionalOrderingConstraints(callee);
1234 private boolean needToUpdateReturnLocHolder(MethodDescriptor mdCallee,
1235 FlowReturnNode flowReturnNode) {
1236 FlowGraph fg = getFlowGraph(mdCallee);
1237 MethodSummary summary = getMethodSummary(mdCallee);
1238 CompositeLocation returnCompLoc = summary.getRETURNLoc();
1239 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnCompLoc.getTuple());
1240 Set<FlowNode> incomingNodeToReturnNode =
1241 fg.getIncomingFlowNodeSet(fg.getFlowNode(returnDescTuple));
1242 for (Iterator iterator = incomingNodeToReturnNode.iterator(); iterator.hasNext();) {
1243 FlowNode inNode = (FlowNode) iterator.next();
1244 if (inNode.getDescTuple().get(0).equals(mdCallee.getThis())) {
1251 private void addMapMethodDescToMethodInvokeNodeSet(MethodInvokeNode min) {
1252 MethodDescriptor md = min.getMethod();
1253 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
1254 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
1256 mapMethodDescToMethodInvokeNodeSet.get(md).add(min);
1259 private Set<MethodInvokeNode> getMethodInvokeNodeSetByMethodDesc(MethodDescriptor md) {
1260 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
1261 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
1263 return mapMethodDescToMethodInvokeNodeSet.get(md);
1266 public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
1267 CompositeLocation inferCompLoc) {
1268 Descriptor localDesc = loc.getLocDescriptor();
1270 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1271 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1272 FlowNode node = (FlowNode) iterator.next();
1273 if (node.getDescTuple().startsWith(localDesc)
1274 && !node.getDescTuple().get(0).equals(LITERALDESC)) {
1275 // need to assign the inferred composite location to this node
1276 CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
1277 node.setCompositeLocation(newCompLoc);
1278 // System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
1283 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
1284 CompositeLocation inferCompLoc) {
1286 // System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
1289 MethodDescriptor md = (MethodDescriptor) inferCompLoc.get(0).getDescriptor();
1291 CompositeLocation newCompLoc = new CompositeLocation();
1292 for (int i = 0; i < inferCompLoc.getSize(); i++) {
1293 newCompLoc.addLocation(inferCompLoc.get(i));
1296 Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
1297 Descriptor enclosingDescriptor;
1298 if (lastDescOfPrefix instanceof InterDescriptor) {
1299 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(lastDescOfPrefix);
1301 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
1304 for (int i = 1; i < nodeDescTuple.size(); i++) {
1305 Descriptor desc = nodeDescTuple.get(i);
1306 Location locElement = new Location(enclosingDescriptor, desc);
1307 newCompLoc.addLocation(locElement);
1309 enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
1315 private void translateMapLocationToInferCompositeLocationToCalleeGraph(
1316 GlobalFlowGraph callerGraph, MethodInvokeNode min) {
1318 MethodDescriptor mdCallee = min.getMethod();
1319 MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
1320 Map<Location, CompositeLocation> callerMapLocToCompLoc =
1321 callerGraph.getMapLocationToInferCompositeLocation();
1323 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
1325 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1326 GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1328 NTuple<Location> baseLocTuple = null;
1329 if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
1330 baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
1333 // System.out.println("\n-#translate caller=" + mdCaller + " infer composite loc to callee="
1334 // + mdCallee + " baseLocTuple=" + baseLocTuple);
1336 Set<Location> keySet = callerMapLocToCompLoc.keySet();
1337 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1338 Location key = (Location) iterator.next();
1339 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
1341 if (!key.getDescriptor().equals(mdCaller)) {
1343 CompositeLocation newCalleeCompLoc;
1344 if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
1345 // System.out.println("-----need to translate callerCompLoc=" + callerCompLoc
1346 // + " with baseTuple=" + baseLocTuple);
1348 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
1350 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1351 // System.out.println("1---key=" + key + " callerCompLoc=" + callerCompLoc
1352 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1353 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1354 if (!newCalleeCompLoc.get(0).getDescriptor().equals(mdCallee)) {
1358 // System.out.println("-----baseLoctuple=" + baseLocTuple);
1360 // check if it is the global access
1361 Location compLocFirstElement = callerCompLoc.getTuple().get(0);
1362 if (compLocFirstElement.getDescriptor().equals(mdCallee)
1363 && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
1365 newCalleeCompLoc = new CompositeLocation();
1366 Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
1368 newCalleeCompLoc.addLocation(newMethodLoc);
1369 for (int i = 1; i < callerCompLoc.getSize(); i++) {
1370 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1372 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1373 // System.out.println("2---key=" + key + " callerCompLoc=" + callerCompLoc
1374 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1375 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1378 int paramIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
1379 if (paramIdx == -1) {
1380 // here, the first element of the current composite location comes from the current
1382 // so transfer the same composite location to the callee
1383 if (!calleeGlobalGraph.contrainsInferCompositeLocationMapKey(key)) {
1384 if (callerCompLoc.get(0).getDescriptor().equals(mdCallee)) {
1385 // System.out.println("3---key=" + key + " callerCompLoc=" + callerCompLoc
1386 // + " newCalleeCompLoc=" + callerCompLoc);
1387 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1388 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, callerCompLoc);
1390 // System.out.println("3---SKIP key=" + key + " callerCompLoc=" + callerCompLoc);
1396 // It is the case where two parameters have relative orderings between them by having
1397 // composite locations
1398 // if we found the param idx, it means that the first part of the caller composite
1399 // location corresponds to the one of arguments.
1400 // for example, if the caller argument is <<caller.this>,<Decoder.br>>
1401 // and the current caller composite location mapping
1402 // <<caller.this>,<Decoder.br>,<Br.value>>
1403 // and the parameter which matches with the caller argument is 'Br brParam'
1404 // then, the translated callee composite location will be <<callee.brParam>,<Br.value>>
1405 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(paramIdx);
1407 FlowNode paramFlowNode = calleeFlowGraph.getParamFlowNode(paramIdx);
1408 NTuple<Location> paramLocTuple =
1409 translateToLocTuple(mdCallee, paramFlowNode.getDescTuple());
1410 newCalleeCompLoc = new CompositeLocation();
1411 for (int i = 0; i < paramLocTuple.size(); i++) {
1412 newCalleeCompLoc.addLocation(paramLocTuple.get(i));
1414 for (int i = argTuple.size(); i < callerCompLoc.getSize(); i++) {
1415 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1417 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
1418 // System.out.println("4---key=" + key + " callerCompLoc=" + callerCompLoc
1419 // + " newCalleeCompLoc=" + newCalleeCompLoc);
1420 // System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
1429 // System.out.println("#ASSIGN COMP LOC TO CALLEE PARAMS: callee=" + mdCallee + " caller="
1431 // If the location of an argument has a composite location
1432 // need to assign a proper composite location to the corresponding callee parameter
1433 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
1434 for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
1435 Integer idx = (Integer) iterator.next();
1437 if (idx == 0 && !min.getMethod().isStatic()) {
1441 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
1442 // System.out.println("-argTuple=" + argTuple + " idx=" + idx);
1443 if (argTuple.size() > 0) {
1444 // check if an arg tuple has been already assigned to a composite location
1445 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
1446 Location argLocalLoc = argLocTuple.get(0);
1448 // if (!isPrimitiveType(argTuple)) {
1449 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
1451 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
1452 for (int i = 1; i < argLocTuple.size(); i++) {
1453 callerCompLoc.addLocation(argLocTuple.get(i));
1456 // System.out.println("---callerCompLoc=" + callerCompLoc);
1458 // if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
1460 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
1462 NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
1463 NTuple<Location> calleeParamLocTuple =
1464 translateToLocTuple(mdCallee, calleeParamDescTuple);
1466 int refParamIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
1467 // System.out.println("-----paramIdx=" + refParamIdx);
1468 if (refParamIdx == 0 && !mdCallee.isStatic()) {
1470 // System.out.println("-------need to translate callerCompLoc=" + callerCompLoc
1471 // + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
1472 // + calleeParamLocTuple);
1474 CompositeLocation newCalleeCompLoc =
1475 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
1477 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1480 // System.out.println("---------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
1481 // + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
1483 } else if (refParamIdx != -1) {
1484 // the first element of an argument composite location matches with one of paramtere
1485 // composite locations
1487 // System.out.println("-------param match case=");
1489 NTuple<Descriptor> argTupleRef = mapIdxToArgTuple.get(refParamIdx);
1490 FlowNode refParamFlowNode = calleeFlowGraph.getParamFlowNode(refParamIdx);
1491 NTuple<Location> refParamLocTuple =
1492 translateToLocTuple(mdCallee, refParamFlowNode.getDescTuple());
1494 // System.out.println("---------refParamLocTuple=" + refParamLocTuple
1495 // + " from argTupleRef=" + argTupleRef);
1497 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1498 for (int i = 0; i < refParamLocTuple.size(); i++) {
1499 newCalleeCompLoc.addLocation(refParamLocTuple.get(i));
1501 for (int i = argTupleRef.size(); i < callerCompLoc.getSize(); i++) {
1502 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1505 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1508 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
1509 // System.out.println("-----------key=" + calleeParamLocTuple.get(0) +
1510 // " callerCompLoc="
1511 // + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
1514 CompositeLocation newCalleeCompLoc =
1515 calculateCompositeLocationFromSubGlobalGraph(mdCallee, calleeParamFlowNode);
1516 if (newCalleeCompLoc != null) {
1517 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
1519 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
1523 // System.out.println("-----------------calleeParamFlowNode="
1524 // + calleeParamFlowNode.getCompositeLocation());
1535 private CompositeLocation calculateCompositeLocationFromSubGlobalGraph(MethodDescriptor md,
1536 FlowNode paramNode) {
1538 // System.out.println("#############################################################");
1539 // System.out.println("calculateCompositeLocationFromSubGlobalGraph=" + paramNode);
1541 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1542 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
1543 GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
1545 List<NTuple<Location>> prefixList = calculatePrefixList(subGlobalFlowGraph, paramGlobalNode);
1547 Location prefixLoc = paramLocTuple.get(0);
1549 Set<GlobalFlowNode> reachableNodeSet =
1550 subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
1551 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1553 // System.out.println("node=" + node + " prefixList=" + prefixList);
1555 for (int i = 0; i < prefixList.size(); i++) {
1556 NTuple<Location> curPrefix = prefixList.get(i);
1557 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1559 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1560 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1561 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1562 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1565 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1567 if (!reachableCommonPrefixSet.isEmpty()) {
1569 MethodDescriptor curPrefixFirstElementMethodDesc =
1570 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1572 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1573 (MethodDescriptor) prefixLoc.getDescriptor();
1575 // System.out.println("curPrefixFirstElementMethodDesc=" +
1576 // curPrefixFirstElementMethodDesc);
1577 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1578 // + nodePrefixLocFirstElementMethodDesc);
1580 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1581 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1582 curPrefixFirstElementMethodDesc)) {
1585 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1587 Location curPrefixLocalLoc = curPrefix.get(0);
1588 if (subGlobalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1589 // in this case, the local variable of the current prefix has already got a composite
1591 // so we just ignore the current composite location.
1593 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1594 // + " DUE TO " + curPrefix);
1598 if (!needToGenerateCompositeLocation(paramGlobalNode, curPrefix)) {
1599 // System.out.println("NO NEED TO GENERATE COMP LOC to " + paramGlobalNode
1600 // + " with prefix=" + curPrefix);
1604 Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
1605 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1606 // System.out.println("NEED TO ASSIGN COMP LOC TO " + paramGlobalNode + " with prefix="
1608 // System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" +
1611 // makes sure that a newly generated location appears in the hierarchy graph
1612 for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
1613 Location curLoc = newCompLoc.get(compIdx);
1614 getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
1617 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1629 private int getParamIdx(CompositeLocation compLoc,
1630 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple) {
1632 // if the composite location is started with the argument descriptor
1633 // return the argument's index. o.t. return -1
1635 Set<Integer> keySet = mapIdxToArgTuple.keySet();
1636 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1637 Integer key = (Integer) iterator.next();
1638 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(key);
1639 if (argTuple.size() > 0 && translateToDescTuple(compLoc.getTuple()).startsWith(argTuple)) {
1640 // System.out.println("compLoc.getTuple=" + compLoc + " is started with " + argTuple);
1641 return key.intValue();
1648 private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
1650 Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
1652 if (lastDesc instanceof FieldDescriptor) {
1653 return ((FieldDescriptor) lastDesc).getType().isPrimitive();
1654 } else if (lastDesc instanceof VarDescriptor) {
1655 return ((VarDescriptor) lastDesc).getType().isPrimitive();
1656 } else if (lastDesc instanceof InterDescriptor) {
1663 private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
1664 NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
1666 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1668 Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
1669 newCalleeCompLoc.addLocation(calleeThisLoc);
1671 // remove the base tuple from the caller
1672 // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
1673 // ,which is relative to the 'this' variable, <THIS,...>
1674 for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
1675 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1678 return newCalleeCompLoc;
1682 private void calculateGlobalValueFlowCompositeLocation() {
1684 System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
1685 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1686 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1688 Set<Location> calculatedPrefixSet = new HashSet<Location>();
1690 Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
1692 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1693 GlobalFlowNode node = (GlobalFlowNode) iterator.next();
1695 Location prefixLoc = node.getLocTuple().get(0);
1697 if (calculatedPrefixSet.contains(prefixLoc)) {
1698 // the prefix loc has been already assigned to a composite location
1702 calculatedPrefixSet.add(prefixLoc);
1704 // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
1705 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
1707 Set<GlobalFlowNode> reachableNodeSet =
1708 globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1709 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1711 // System.out.println("node=" + node + " prefixList=" + prefixList);
1712 // System.out.println("---prefixList=" + prefixList);
1714 nextprefix: for (int i = 0; i < prefixList.size(); i++) {
1715 NTuple<Location> curPrefix = prefixList.get(i);
1716 // System.out.println("---curPrefix=" + curPrefix);
1717 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1719 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1720 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1721 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1722 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1725 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1727 if (!reachableCommonPrefixSet.isEmpty()) {
1729 MethodDescriptor curPrefixFirstElementMethodDesc =
1730 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1732 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1733 (MethodDescriptor) prefixLoc.getDescriptor();
1735 // System.out.println("curPrefixFirstElementMethodDesc=" +
1736 // curPrefixFirstElementMethodDesc);
1737 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1738 // + nodePrefixLocFirstElementMethodDesc);
1740 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1741 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1742 curPrefixFirstElementMethodDesc)) {
1745 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1747 Location curPrefixLocalLoc = curPrefix.get(0);
1748 if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1749 // in this case, the local variable of the current prefix has already got a composite
1751 // so we just ignore the current composite location.
1753 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1754 // + " DUE TO " + curPrefix);
1759 if (!needToGenerateCompositeLocation(node, curPrefix)) {
1760 // System.out.println("NO NEED TO GENERATE COMP LOC to " + node + " with prefix="
1762 // System.out.println("prefixList=" + prefixList);
1763 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1764 continue nextprefix;
1767 Location targetLocalLoc = node.getLocTuple().get(0);
1768 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1769 // System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" +
1771 // System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc="
1773 globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1788 private boolean checkFlowNodeReturnThisField(MethodDescriptor md) {
1790 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1791 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1793 FlowGraph flowGraph = getFlowGraph(md);
1795 ClassDescriptor enclosingDesc = getClassTypeDescriptor(md.getThis());
1796 if (enclosingDesc == null) {
1801 Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
1802 Set<GlobalFlowNode> globalReturnNodeSet = new HashSet<GlobalFlowNode>();
1803 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1804 FlowNode flowNode = (FlowNode) iterator.next();
1805 NTuple<Location> locTuple = translateToLocTuple(md, flowNode.getDescTuple());
1806 GlobalFlowNode globalReturnNode = globalFlowGraph.getFlowNode(locTuple);
1807 globalReturnNodeSet.add(globalReturnNode);
1809 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, globalReturnNode);
1810 for (int i = 0; i < prefixList.size(); i++) {
1811 NTuple<Location> curPrefix = prefixList.get(i);
1812 ClassDescriptor cd =
1813 getClassTypeDescriptor(curPrefix.get(curPrefix.size() - 1).getLocDescriptor());
1814 if (cd != null && cd.equals(enclosingDesc)) {
1822 if (count == returnNodeSet.size()) {
1823 // in this case, all return nodes in the method returns values coming from a location that
1824 // starts with "this"
1826 // System.out.println("$$$SET RETURN LOC TRUE=" + md);
1827 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.TRUE);
1829 // NameDescriptor returnLocDesc = new NameDescriptor("RLOC" + (locSeed++));
1830 // NTuple<Descriptor> rDescTuple = new NTuple<Descriptor>();
1831 // rDescTuple.add(md.getThis());
1832 // rDescTuple.add(returnLocDesc);
1834 // for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1835 // FlowNode rnode = (FlowNode) iterator.next();
1836 // flowGraph.addValueFlowEdge(rnode.getDescTuple(), rDescTuple);
1839 // getMethodSummary(md).setRETURNLoc(new CompositeLocation(translateToLocTuple(md,
1843 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.FALSE);
1846 return mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1850 private boolean needToGenerateCompositeLocation(GlobalFlowNode node, NTuple<Location> curPrefix) {
1851 // return true if there is a path between a node to which we want to give a composite location
1852 // and nodes which start with curPrefix
1854 // System.out.println("---needToGenerateCompositeLocation curPrefix=" + curPrefix);
1856 Location targetLocalLoc = node.getLocTuple().get(0);
1858 MethodDescriptor md = (MethodDescriptor) targetLocalLoc.getDescriptor();
1859 FlowGraph flowGraph = getFlowGraph(md);
1861 FlowNode flowNode = flowGraph.getFlowNode(node.getDescTuple());
1862 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
1864 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
1865 for (Iterator iterator = paramNodeSet.iterator(); iterator.hasNext();) {
1866 FlowNode paramFlowNode = (FlowNode) iterator.next();
1867 if (curPrefix.startsWith(translateToLocTuple(md, paramFlowNode.getDescTuple()))) {
1872 if (targetLocalLoc.getLocDescriptor() instanceof InterDescriptor) {
1873 Pair<MethodInvokeNode, Integer> pair =
1874 ((InterDescriptor) targetLocalLoc.getLocDescriptor()).getMethodArgIdxPair();
1877 // System.out.println("$$$TARGETLOCALLOC HOLDER=" + targetLocalLoc);
1879 MethodInvokeNode min = pair.getFirst();
1880 Integer paramIdx = pair.getSecond();
1881 MethodDescriptor mdCallee = min.getMethod();
1883 FlowNode paramNode = getFlowGraph(mdCallee).getParamFlowNode(paramIdx);
1884 if (checkNodeReachToReturnNode(mdCallee, paramNode)) {
1892 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1893 Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1895 if (!md.isStatic()) {
1896 ClassDescriptor currentMethodThisType = getClassTypeDescriptor(md.getThis());
1897 for (int i = 0; i < curPrefix.size(); i++) {
1898 ClassDescriptor prefixType = getClassTypeDescriptor(curPrefix.get(i).getLocDescriptor());
1899 if (prefixType != null && prefixType.equals(currentMethodThisType)) {
1900 // System.out.println("PREFIX TYPE MATCHES WITH=" + currentMethodThisType);
1902 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1903 boolean hasCompReturnLocWithThis =
1904 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1905 if (hasCompReturnLocWithThis) {
1906 if (checkNodeReachToReturnNode(md, flowNode)) {
1912 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1913 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1914 if (subGlobalReachalbeNode.getLocTuple().get(0).getLocDescriptor().equals(md.getThis())) {
1915 // System.out.println("PREFIX FOUND=" + subGlobalReachalbeNode);
1923 Location lastLocationOfPrefix = curPrefix.get(curPrefix.size() - 1);
1924 // check whether prefix appears in the list of parameters
1925 Set<MethodInvokeNode> minSet = mapMethodDescToMethodInvokeNodeSet.get(md);
1926 // System.out.println("$$$md=" + md + " minSet=" + minSet);
1927 if (minSet == null) {
1930 found: for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1931 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1932 Map<Integer, NTuple<Descriptor>> map = mapMethodInvokeNodeToArgIdxMap.get(min);
1933 Set<Integer> keySet = map.keySet();
1934 // System.out.println("min=" + min.printNode(0));
1936 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1937 Integer argIdx = (Integer) iterator2.next();
1938 NTuple<Descriptor> argTuple = map.get(argIdx);
1940 if (!(!md.isStatic() && argIdx == 0)) {
1941 // if the argTuple is empty, we don't need to do with anything(LITERAL CASE).
1942 if (argTuple.size() > 0
1943 && argTuple.get(argTuple.size() - 1).equals(lastLocationOfPrefix.getLocDescriptor())) {
1944 NTuple<Location> locTuple =
1945 translateToLocTuple(md, flowGraph.getParamFlowNode(argIdx).getDescTuple());
1946 lastLocationOfPrefix = locTuple.get(0);
1947 // System.out.println("ARG CASE=" + locTuple);
1948 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1949 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1950 // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
1951 NTuple<Location> globalReachlocTuple = subGlobalReachalbeNode.getLocTuple();
1952 for (int i = 0; i < globalReachlocTuple.size(); i++) {
1953 if (globalReachlocTuple.get(i).equals(lastLocationOfPrefix)) {
1954 // System.out.println("ARG " + argTuple + " IS MATCHED WITH="
1955 // + lastLocationOfPrefix);
1968 private boolean checkNodeReachToReturnNode(MethodDescriptor md, FlowNode node) {
1970 FlowGraph flowGraph = getFlowGraph(md);
1971 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(node);
1972 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1973 boolean hasCompReturnLocWithThis =
1974 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1976 if (hasCompReturnLocWithThis) {
1977 for (Iterator iterator = flowGraph.getReturnNodeSet().iterator(); iterator.hasNext();) {
1978 FlowNode returnFlowNode = (FlowNode) iterator.next();
1979 if (reachableSet.contains(returnFlowNode)) {
1988 private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
1989 CompositeLocation newCompLoc = compLocPrefix.clone();
1990 NTuple<Location> locTuple = node.getLocTuple();
1991 for (int i = 1; i < locTuple.size(); i++) {
1992 newCompLoc.addLocation(locTuple.get(i));
1994 node.setInferCompositeLocation(newCompLoc);
1997 private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
1999 // System.out.println("\n##### calculatePrefixList node=" + node);
2001 Set<GlobalFlowNode> incomingNodeSetPrefix =
2002 graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
2003 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
2005 Set<GlobalFlowNode> reachableNodeSetPrefix =
2006 graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
2007 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
2009 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
2011 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
2012 GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
2013 NTuple<Location> inNodeTuple = inNode.getLocTuple();
2015 if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
2016 || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
2020 for (int i = 1; i < inNodeTuple.size(); i++) {
2021 NTuple<Location> prefix = inNodeTuple.subList(0, i);
2022 if (!prefixList.contains(prefix)) {
2023 prefixList.add(prefix);
2028 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
2029 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
2030 int s0 = arg0.size();
2031 int s1 = arg1.size();
2034 } else if (s0 == s1) {
2046 private CompositeLocation calculateCompositeLocationFromFlowGraph(MethodDescriptor md,
2049 // System.out.println("#############################################################");
2050 // System.out.println("calculateCompositeLocationFromFlowGraph=" + node);
2052 FlowGraph flowGraph = getFlowGraph(md);
2053 // NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
2054 // GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
2056 List<NTuple<Location>> prefixList = calculatePrefixListFlowGraph(flowGraph, node);
2058 // Set<GlobalFlowNode> reachableNodeSet =
2059 // subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
2061 Set<FlowNode> reachableNodeSet =
2062 flowGraph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
2064 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
2066 // System.out.println("node=" + node + " prefixList=" + prefixList);
2068 for (int i = 0; i < prefixList.size(); i++) {
2069 NTuple<Location> curPrefix = prefixList.get(i);
2070 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
2072 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
2073 FlowNode reachNode = (FlowNode) iterator2.next();
2074 NTuple<Location> reachLocTuple = translateToLocTuple(md, reachNode.getCurrentDescTuple());
2075 if (reachLocTuple.startsWith(curPrefix)) {
2076 reachableCommonPrefixSet.add(reachLocTuple);
2079 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
2081 if (!reachableCommonPrefixSet.isEmpty()) {
2083 MethodDescriptor curPrefixFirstElementMethodDesc =
2084 (MethodDescriptor) curPrefix.get(0).getDescriptor();
2086 Location curPrefixLocalLoc = curPrefix.get(0);
2088 Location targetLocalLoc = new Location(md, node.getDescTuple().get(0));
2089 // Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
2091 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
2092 // System.out.println("NEED2ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
2093 // System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" +
2096 node.setCompositeLocation(newCompLoc);
2106 private List<NTuple<Location>> calculatePrefixListFlowGraph(FlowGraph graph, FlowNode node) {
2108 // System.out.println("\n##### calculatePrefixList node=" + node);
2110 MethodDescriptor md = graph.getMethodDescriptor();
2111 Set<FlowNode> incomingNodeSetPrefix =
2112 graph.getIncomingNodeSetByPrefix(node.getDescTuple().get(0));
2113 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
2115 Set<FlowNode> reachableNodeSetPrefix =
2116 graph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
2117 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
2119 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
2121 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
2122 FlowNode inNode = (FlowNode) iterator.next();
2123 NTuple<Location> inNodeTuple = translateToLocTuple(md, inNode.getCurrentDescTuple());
2125 // if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
2126 // || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
2130 for (int i = 1; i < inNodeTuple.size(); i++) {
2131 NTuple<Location> prefix = inNodeTuple.subList(0, i);
2132 if (!prefixList.contains(prefix)) {
2133 prefixList.add(prefix);
2138 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
2139 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
2140 int s0 = arg0.size();
2141 int s1 = arg1.size();
2144 } else if (s0 == s1) {
2156 private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
2158 MethodDescriptor md = flowGraph.getMethodDescriptor();
2160 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(md);
2162 // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
2163 Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
2165 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
2167 FlowEdge edge = (FlowEdge) iterator.next();
2168 NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
2169 NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
2171 if (flowGraph.getFlowNode(srcDescTuple) instanceof FlowReturnNode
2172 || flowGraph.getFlowNode(dstDescTuple) instanceof FlowReturnNode) {
2176 // here only keep the first element(method location) of the descriptor
2178 NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
2179 NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
2181 globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
2188 private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
2190 NTuple<Location> locTuple = new NTuple<Location>();
2192 Descriptor enclosingDesc = md;
2193 for (int i = 0; i < descTuple.size(); i++) {
2194 Descriptor desc = descTuple.get(i);
2196 Location loc = new Location(enclosingDesc, desc);
2199 if (desc instanceof VarDescriptor) {
2200 enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
2201 } else if (desc instanceof FieldDescriptor) {
2202 enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
2204 enclosingDesc = desc;
2213 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller) {
2215 // the transformation for a call site propagates flows through parameters
2216 // if the method is virtual, it also grab all relations from any possible
2219 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
2221 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
2222 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
2223 MethodDescriptor mdCallee = min.getMethod();
2224 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2225 if (mdCallee.isStatic()) {
2226 setPossibleCallees.add(mdCallee);
2228 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
2229 // removes method descriptors that are not invoked by the caller
2230 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
2231 setPossibleCallees.addAll(calleeSet);
2234 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
2235 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
2236 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
2243 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
2244 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
2246 // System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
2247 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
2248 Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
2250 // System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
2251 // + mapMethodInvokeNodeToArgIdxMap.get(min));
2253 Set<Integer> keySet = mapIdxToArg.keySet();
2254 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2255 Integer idx = (Integer) iterator.next();
2256 NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
2257 if (argDescTuple.size() > 0) {
2258 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
2259 NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
2260 NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
2261 // System.out.println("-------paramDescTuple=" + paramDescTuple + "->argDescTuple="
2263 addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
2267 // addValueFlowBetweenParametersToCaller(min, mdCaller, possibleMdCallee);
2269 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2270 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
2271 Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
2272 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
2273 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
2274 addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
2277 // System.out.println("$$$GLOBAL PC LOC ADD=" + mdCaller);
2278 Set<NTuple<Location>> pcLocTupleSet = mapMethodInvokeNodeToPCLocTupleSet.get(min);
2279 // System.out.println("---pcLocTupleSet=" + pcLocTupleSet);
2280 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
2281 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
2282 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
2283 if (calleeNode.isParamNodeWithIncomingFlows()) {
2284 // System.out.println("calleeNode.getLocTuple()" + calleeNode.getLocTuple());
2285 NTuple<Location> callerSrcNodeLocTuple =
2286 translateToCallerLocTuple(min, possibleMdCallee, mdCaller, calleeNode.getLocTuple());
2287 // System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
2288 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
2289 for (Iterator iterator2 = pcLocTupleSet.iterator(); iterator2.hasNext();) {
2290 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator2.next();
2292 callerSubGlobalGraph.addValueFlowEdge(pcLocTuple, callerSrcNodeLocTuple);
2301 private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
2302 MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
2304 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
2305 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
2307 // System.out.println("$addValueFlowFromCalleeNode calleeSrcNode=" + calleeSrcNode);
2309 NTuple<Location> callerSrcNodeLocTuple =
2310 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
2311 // System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
2313 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
2315 Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
2317 for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
2318 GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
2319 NTuple<Location> callerDstNodeLocTuple =
2320 translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
2321 // System.out.println("outNode=" + outNode + " callerDstNodeLocTuple="
2322 // + callerDstNodeLocTuple);
2323 if (callerSrcNodeLocTuple != null && callerDstNodeLocTuple != null
2324 && callerSrcNodeLocTuple.size() > 0 && callerDstNodeLocTuple.size() > 0) {
2325 callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
2332 private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
2333 MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
2334 // this method will return the same nodeLocTuple if the corresponding argument is literal
2337 // System.out.println("translateToCallerLocTuple=" + nodeLocTuple);
2339 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
2340 NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
2341 if (calleeFlowGraph.isParameter(nodeDescTuple)) {
2342 int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
2343 NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
2345 // if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
2346 // // the type of argument is primitive.
2347 // return nodeLocTuple.clone();
2349 // System.out.println("paramIdx=" + paramIdx + " argDescTuple=" + argDescTuple + " from min="
2350 // + min.printNode(0));
2351 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
2353 NTuple<Location> callerLocTuple = new NTuple<Location>();
2355 callerLocTuple.addAll(argLocTuple);
2356 for (int i = 1; i < nodeLocTuple.size(); i++) {
2357 callerLocTuple.add(nodeLocTuple.get(i));
2359 return callerLocTuple;
2361 return nodeLocTuple.clone();
2366 public static boolean isPrimitive(Descriptor desc) {
2368 if (desc instanceof FieldDescriptor) {
2369 return ((FieldDescriptor) desc).getType().isPrimitive();
2370 } else if (desc instanceof VarDescriptor) {
2371 return ((VarDescriptor) desc).getType().isPrimitive();
2372 } else if (desc instanceof InterDescriptor) {
2379 public static boolean isReference(Descriptor desc) {
2381 if (desc instanceof FieldDescriptor) {
2383 TypeDescriptor type = ((FieldDescriptor) desc).getType();
2384 if (type.isArray()) {
2385 return !type.isPrimitive();
2387 return type.isPtr();
2390 } else if (desc instanceof VarDescriptor) {
2391 TypeDescriptor type = ((VarDescriptor) desc).getType();
2392 if (type.isArray()) {
2393 return !type.isPrimitive();
2395 return type.isPtr();
2402 private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
2404 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
2405 for (int i = 0; i < locTuple.size(); i++) {
2406 descTuple.add(locTuple.get(i).getLocDescriptor());
2412 public LocationSummary getLocationSummary(Descriptor d) {
2413 if (!mapDescToLocationSummary.containsKey(d)) {
2414 if (d instanceof MethodDescriptor) {
2415 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
2416 } else if (d instanceof ClassDescriptor) {
2417 mapDescToLocationSummary.put(d, new FieldSummary());
2420 return mapDescToLocationSummary.get(d);
2423 private void generateMethodSummary() {
2425 Set<MethodDescriptor> keySet = md2lattice.keySet();
2426 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2427 MethodDescriptor md = (MethodDescriptor) iterator.next();
2429 System.out.println("\nSSJAVA: generate method summary: " + md);
2431 FlowGraph flowGraph = getFlowGraph(md);
2432 if (flowGraph == null) {
2435 MethodSummary methodSummary = getMethodSummary(md);
2437 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
2439 // set the 'this' reference location
2440 if (!md.isStatic()) {
2441 // System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
2442 methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
2445 // set the 'global' reference location if needed
2446 if (methodSummary.hasGlobalAccess()) {
2447 methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
2450 // construct a parameter mapping that maps a parameter descriptor to an
2451 // inferred composite location
2452 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
2453 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
2454 CompositeLocation inferredCompLoc =
2455 updateCompositeLocation(flowNode.getCompositeLocation());
2456 // System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc + " original="
2457 // + flowNode.getCompositeLocation());
2459 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
2460 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
2461 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
2468 private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2470 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2472 for (int idx = 0; idx < size; idx++) {
2473 Location loc1 = locTuple1.get(idx);
2474 Location loc2 = locTuple2.get(idx);
2476 Descriptor desc1 = loc1.getDescriptor();
2477 Descriptor desc2 = loc2.getDescriptor();
2479 if (!desc1.equals(desc2)) {
2480 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2483 Descriptor locDesc1 = loc1.getLocDescriptor();
2484 Descriptor locDesc2 = loc2.getLocDescriptor();
2486 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2488 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2489 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2491 // System.out.println("---node1=" + node1 + " node2=" + node2);
2492 // System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2493 // + hierarchyGraph.getIncomingNodeSet(node2));
2495 if (locDesc1.equals(locDesc2)) {
2497 } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
2498 && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
2510 private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2512 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2514 for (int idx = 0; idx < size; idx++) {
2515 Location loc1 = locTuple1.get(idx);
2516 Location loc2 = locTuple2.get(idx);
2518 Descriptor desc1 = loc1.getDescriptor();
2519 Descriptor desc2 = loc2.getDescriptor();
2521 if (!desc1.equals(desc2)) {
2522 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2525 Descriptor locDesc1 = loc1.getLocDescriptor();
2526 Descriptor locDesc2 = loc2.getLocDescriptor();
2528 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2530 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2531 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2533 // System.out.println("---node1=" + node1 + " node2=" + node2);
2534 // System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2535 // + hierarchyGraph.getIncomingNodeSet(node2));
2537 if (locDesc1.equals(locDesc2)) {
2539 } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
2550 private void debug_writeLattices() {
2552 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
2553 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2554 Descriptor key = (Descriptor) iterator.next();
2555 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
2556 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
2557 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
2558 if (key instanceof ClassDescriptor) {
2559 // writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
2561 } else if (key instanceof MethodDescriptor) {
2562 MethodDescriptor md = (MethodDescriptor) key;
2563 // writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
2567 LocationSummary ls = getLocationSummary(key);
2568 // System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
2571 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
2572 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
2573 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2574 // System.out.println("########cd=" + cd);
2575 writeInferredLatticeDotFile((ClassDescriptor) cd, cd2lattice.get(cd), "");
2576 COUNT += cd2lattice.get(cd).getKeySet().size();
2579 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
2580 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
2581 MethodDescriptor md = (MethodDescriptor) iterator.next();
2582 writeInferredLatticeDotFile(md.getClassDesc(), md, md2lattice.get(md), "");
2583 COUNT += md2lattice.get(md).getKeySet().size();
2585 System.out.println("###COUNT=" + COUNT);
2587 Set<Descriptor> descKeySet = desc2naiveLattice.keySet();
2588 for (Iterator iterator = descKeySet.iterator(); iterator.hasNext();) {
2589 Descriptor desc = (Descriptor) iterator.next();
2590 // System.out.println("########cd=" + cd);
2592 ClassDescriptor cd_naive;
2593 MethodDescriptor md_naive;
2594 if (desc instanceof ClassDescriptor) {
2595 cd_naive = (ClassDescriptor) desc;
2598 md_naive = (MethodDescriptor) desc;
2599 cd_naive = md_naive.getClassDesc();
2602 writeInferredLatticeDotFile(cd_naive, md_naive, desc2naiveLattice.get(desc), "_naive");
2606 private void buildLattice(Descriptor desc) {
2607 // System.out.println("buildLattice=" + desc);
2608 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
2610 addMapDescToSimpleLattice(desc, simpleLattice);
2612 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2613 // System.out.println("\n## insertIntermediateNodesToStraightLine:"
2614 // + simpleHierarchyGraph.getName());
2615 SSJavaLattice<String> lattice =
2616 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2617 lattice.removeRedundantEdges();
2619 LocationInference.numLocationsSInfer += lattice.getKeySet().size();
2621 if (desc instanceof ClassDescriptor) {
2623 cd2lattice.put((ClassDescriptor) desc, lattice);
2624 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2625 } else if (desc instanceof MethodDescriptor) {
2627 md2lattice.put((MethodDescriptor) desc, lattice);
2628 MethodDescriptor md = (MethodDescriptor) desc;
2629 ClassDescriptor cd = md.getClassDesc();
2630 // ssjava.writeLatticeDotFile(cd, md, lattice);
2635 // deprecated: it builds method/class lattices without considering class inheritance
2636 private void buildLattice() {
2638 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
2639 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2640 Descriptor desc = (Descriptor) iterator.next();
2642 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
2644 addMapDescToSimpleLattice(desc, simpleLattice);
2646 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2647 System.out.println("\n## insertIntermediateNodesToStraightLine:"
2648 + simpleHierarchyGraph.getName());
2649 SSJavaLattice<String> lattice =
2650 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2651 lattice.removeRedundantEdges();
2653 LocationInference.numLocationsSInfer += lattice.getKeySet().size();
2655 if (desc instanceof ClassDescriptor) {
2657 cd2lattice.put((ClassDescriptor) desc, lattice);
2658 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2659 } else if (desc instanceof MethodDescriptor) {
2661 md2lattice.put((MethodDescriptor) desc, lattice);
2662 MethodDescriptor md = (MethodDescriptor) desc;
2663 ClassDescriptor cd = md.getClassDesc();
2664 // ssjava.writeLatticeDotFile(cd, md, lattice);
2671 private void buildLatticeInheritanceTree() {
2672 // DFS the inheritance tree and propagates lattice nodes/edges from the parent to children
2673 // Node<ClassDescriptor> rootNode = inheritanceTree.getRootNode();
2674 DFSBuildLatticeInheritanceTree(rootClassDescriptor);
2677 public Set<ClassDescriptor> getDirectSubClasses(ClassDescriptor parent) {
2679 Set<ClassDescriptor> result = new HashSet<ClassDescriptor>();
2681 Set<ClassDescriptor> children = tu.getDirectSubClasses(parent);
2682 if (children == null) {
2683 children = new HashSet<ClassDescriptor>();
2686 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
2687 ClassDescriptor child = (ClassDescriptor) iterator.next();
2688 if (toanalyze_classDescSet.contains(child)) {
2696 private void DFSBuildLatticeInheritanceTree(ClassDescriptor cd) {
2697 // ClassDescriptor cd = node.getData();
2699 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2700 if (parentClassDesc != null) {
2701 Map<TripleItem, String> parentMap = buildLattice.getIntermediateLocMap(parentClassDesc);
2702 buildLattice.setIntermediateLocMap(cd, parentMap);
2707 for (Iterator iterator = cd.getMethods(); iterator.hasNext();) {
2708 MethodDescriptor md = (MethodDescriptor) iterator.next();
2709 if (toanalyze_methodDescList.contains(md)) {
2710 MethodDescriptor parentMethodDesc = getParentMethodDesc(md.getClassDesc(), md);
2711 if (parentMethodDesc != null) {
2712 Map<TripleItem, String> parentMap = buildLattice.getIntermediateLocMap(parentMethodDesc);
2713 Map<TripleItem, String> childMap = new HashMap<TripleItem, String>();
2714 Set<TripleItem> keySet = parentMap.keySet();
2715 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
2716 TripleItem key = (TripleItem) iterator2.next();
2717 childMap.put(key, parentMap.get(key));
2719 buildLattice.setIntermediateLocMap(md, childMap);
2725 // traverse children
2726 Set<ClassDescriptor> children = tu.getDirectSubClasses(cd);
2727 if (children != null) {
2728 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
2729 ClassDescriptor classDescriptor = (ClassDescriptor) iterator.next();
2730 if (toanalyze_classDescSet.contains(classDescriptor)) {
2731 DFSBuildLatticeInheritanceTree(classDescriptor);
2733 if (classDescriptor.isAbstract()) {
2734 DFSBuildLatticeInheritanceTree(classDescriptor);
2742 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
2743 mapDescriptorToSimpleLattice.put(desc, lattice);
2746 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
2747 return mapDescriptorToSimpleLattice.get(desc);
2750 private void simplifyHierarchyGraph() {
2751 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2752 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2753 Descriptor desc = (Descriptor) iterator.next();
2754 // System.out.println("SSJAVA: remove redundant edges: " + desc);
2755 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
2756 simpleHierarchyGraph.setName(desc + "_SIMPLE");
2757 simpleHierarchyGraph.removeRedundantEdges();
2758 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
2762 private void insertCombinationNodes() {
2763 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
2764 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2765 Descriptor desc = (Descriptor) iterator.next();
2766 System.out.println("\nSSJAVA: Inserting Combination Nodes:" + desc);
2767 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2768 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
2769 skeletonGraphWithCombinationNode.setName(desc + "_SC");
2771 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2772 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2773 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph,
2775 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2776 skeletonGraphWithCombinationNode.removeRedundantEdges();
2777 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
2781 private void constructSkeletonHierarchyGraph() {
2782 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2783 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2784 Descriptor desc = (Descriptor) iterator.next();
2785 System.out.println("SSJAVA: Constructing Skeleton Hierarchy Graph: " + desc);
2786 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
2787 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
2788 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
2789 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
2790 skeletonGraph.simplifyHierarchyGraph(this);
2791 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
2795 private void recurUpAccumulateInheritanceDesc(Descriptor curDesc, Set<Descriptor> set) {
2797 if (curDesc instanceof ClassDescriptor) {
2798 ClassDescriptor cd = (ClassDescriptor) curDesc;
2799 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2800 if (parentClassDesc != null && !parentClassDesc.equals(rootClassDescriptor)) {
2801 set.add(parentClassDesc);
2802 recurUpAccumulateInheritanceDesc(parentClassDesc, set);
2805 MethodDescriptor md = (MethodDescriptor) curDesc;
2806 ClassDescriptor cd = md.getClassDesc();
2809 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2810 if (parentClassDesc != null && !parentClassDesc.equals(rootClassDescriptor)) {
2812 Set<MethodDescriptor> methodDescSet =
2813 parentClassDesc.getMethodTable().getSet(md.getSymbol());
2814 for (Iterator iterator = methodDescSet.iterator(); iterator.hasNext();) {
2815 MethodDescriptor parentMethodDesc = (MethodDescriptor) iterator.next();
2816 if (parentMethodDesc.matches(md)) {
2817 set.add(parentMethodDesc);
2818 recurUpAccumulateInheritanceDesc(parentMethodDesc, set);
2827 private void recurDownAccumulateInheritanceDesc(Descriptor curDesc, Set<Descriptor> set) {
2829 if (curDesc instanceof ClassDescriptor) {
2830 ClassDescriptor cd = (ClassDescriptor) curDesc;
2831 ClassDescriptor parentClassDesc = cd.getSuperDesc();
2832 Set<ClassDescriptor> directSubClasses = tu.getDirectSubClasses(cd);
2833 for (Iterator iterator = directSubClasses.iterator(); iterator.hasNext();) {
2834 ClassDescriptor child = (ClassDescriptor) iterator.next();
2835 recurDownAccumulateInheritanceDesc(child, set);
2838 MethodDescriptor md = (MethodDescriptor) curDesc;
2839 ClassDescriptor cd = md.getClassDesc();
2842 Set<ClassDescriptor> directSubClasses = tu.getDirectSubClasses(cd);
2843 for (Iterator iterator = directSubClasses.iterator(); iterator.hasNext();) {
2844 ClassDescriptor child = (ClassDescriptor) iterator.next();
2846 Set<MethodDescriptor> methodDescSet = child.getMethodTable().getSet(md.getSymbol());
2847 for (Iterator iterator2 = methodDescSet.iterator(); iterator2.hasNext();) {
2848 MethodDescriptor childMethodDesc = (MethodDescriptor) iterator2.next();
2849 if (childMethodDesc.matches(md)) {
2850 set.add(childMethodDesc);
2851 recurDownAccumulateInheritanceDesc(childMethodDesc, set);
2860 private void accumulateInheritanceDesc(Descriptor curDesc, Set<Descriptor> set) {
2862 recurUpAccumulateInheritanceDesc(curDesc, set);
2863 recurDownAccumulateInheritanceDesc(curDesc, set);
2867 public boolean isValidMergeInheritanceCheck(Descriptor desc, Set<HNode> mergeSet) {
2869 // set up inheritance chain set...
2870 Set<Descriptor> inheritanceDescSet = new HashSet<Descriptor>();
2871 recurUpAccumulateInheritanceDesc(desc, inheritanceDescSet);
2873 nextgraph: for (Iterator iterator = inheritanceDescSet.iterator(); iterator.hasNext();) {
2874 Descriptor inheritDesc = (Descriptor) iterator.next();
2876 if (!desc.equals(inheritDesc)) {
2877 HierarchyGraph graph = getSkeletonCombinationHierarchyGraph(inheritDesc);
2879 // first check whether this graph includes all elements of the merge set
2880 for (Iterator iterator2 = mergeSet.iterator(); iterator2.hasNext();) {
2881 HNode node = (HNode) iterator2.next();
2882 if (!graph.contains(node)) {
2887 HNode firstNode = mergeSet.iterator().next();
2889 Set<HNode> incomingNode = graph.getIncomingNodeSet(firstNode);
2890 Set<HNode> outgoingNode = graph.getOutgoingNodeSet(firstNode);
2892 for (Iterator iterator2 = mergeSet.iterator(); iterator2.hasNext();) {
2893 HNode node = (HNode) iterator2.next();
2895 if (!graph.getIncomingNodeSet(node).equals(incomingNode)
2896 || !graph.getOutgoingNodeSet(node).equals(outgoingNode)) {
2908 private void debug_writeHierarchyDotFiles() {
2910 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2911 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2912 Descriptor desc = (Descriptor) iterator.next();
2913 getHierarchyGraph(desc).writeGraph();
2918 private void debug_writeSimpleHierarchyDotFiles() {
2920 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2921 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2922 Descriptor desc = (Descriptor) iterator.next();
2923 getHierarchyGraph(desc).writeGraph();
2924 getSimpleHierarchyGraph(desc).writeGraph();
2925 getSimpleHierarchyGraph(desc).writeGraph(true);
2930 private void debug_writeSkeletonHierarchyDotFiles() {
2932 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2933 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2934 Descriptor desc = (Descriptor) iterator.next();
2935 getSkeletonHierarchyGraph(desc).writeGraph();
2940 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
2942 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2943 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2944 Descriptor desc = (Descriptor) iterator.next();
2945 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
2950 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
2951 return mapDescriptorToSimpleHierarchyGraph.get(d);
2954 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
2955 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
2956 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2958 return mapDescriptorToSkeletonHierarchyGraph.get(d);
2961 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
2962 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
2963 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2965 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
2968 private void constructHierarchyGraph() {
2970 LinkedList<MethodDescriptor> methodDescList =
2971 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
2973 while (!methodDescList.isEmpty()) {
2974 MethodDescriptor md = methodDescList.removeLast();
2975 if (state.SSJAVADEBUG) {
2976 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
2977 System.out.println();
2978 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
2979 constructHierarchyGraph(md, hierarchyGraph);
2980 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
2986 while (!toAnalyzeIsEmpty()) {
2987 ClassDescriptor cd = toAnalyzeNext();
2988 HierarchyGraph graph = getHierarchyGraph(cd);
2989 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2990 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
2991 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
2992 graph.getHNode(fieldDesc);
2997 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2998 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2999 Descriptor key = (Descriptor) iterator.next();
3000 HierarchyGraph graph = getHierarchyGraph(key);
3002 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
3003 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
3004 HNode node = (HNode) iterator2.next();
3005 if (!node.isSkeleton() && !node.isCombinationNode()) {
3006 if (graph.getIncomingNodeSet(node).size() == 0) {
3007 nodeToBeConnected.add(node);
3012 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
3013 HNode node = (HNode) iterator2.next();
3014 // System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
3015 graph.addEdge(graph.getHNode(TOPDESC), node);
3022 private void constructHierarchyGraph2() {
3024 // do fixed-point analysis
3026 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
3028 // Collections.sort(descriptorListToAnalyze, new
3029 // Comparator<MethodDescriptor>() {
3030 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
3031 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
3035 // current descriptors to visit in fixed-point interprocedural analysis,
3036 // prioritized by dependency in the call graph
3037 methodDescriptorsToVisitStack.clear();
3039 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
3040 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
3042 while (!descriptorListToAnalyze.isEmpty()) {
3043 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
3044 methodDescriptorsToVisitStack.add(md);
3047 // analyze scheduled methods until there are no more to visit
3048 while (!methodDescriptorsToVisitStack.isEmpty()) {
3049 // start to analyze leaf node
3050 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
3052 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
3053 // MethodSummary methodSummary = new MethodSummary(md);
3055 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
3056 // curMethodInfo = methodInfo;
3058 System.out.println();
3059 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
3061 constructHierarchyGraph(md, hierarchyGraph);
3063 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
3064 // MethodSummary prevMethodSummary = getMethodSummary(md);
3066 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
3068 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
3069 // mapDescToLocationSummary.put(md, methodSummary);
3071 // results for callee changed, so enqueue dependents caller for
3073 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
3074 while (depsItr.hasNext()) {
3075 MethodDescriptor methodNext = depsItr.next();
3076 if (!methodDescriptorsToVisitStack.contains(methodNext)
3077 && methodDescriptorToVistSet.contains(methodNext)) {
3078 methodDescriptorsToVisitStack.add(methodNext);
3087 while (!toAnalyzeIsEmpty()) {
3088 ClassDescriptor cd = toAnalyzeNext();
3089 HierarchyGraph graph = getHierarchyGraph(cd);
3090 for (Iterator iter = cd.getFields(); iter.hasNext();) {
3091 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
3092 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
3093 graph.getHNode(fieldDesc);
3098 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
3099 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
3100 Descriptor key = (Descriptor) iterator.next();
3101 HierarchyGraph graph = getHierarchyGraph(key);
3103 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
3104 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
3105 HNode node = (HNode) iterator2.next();
3106 if (!node.isSkeleton() && !node.isCombinationNode()) {
3107 if (graph.getIncomingNodeSet(node).size() == 0) {
3108 nodeToBeConnected.add(node);
3113 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
3114 HNode node = (HNode) iterator2.next();
3115 // System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
3116 graph.addEdge(graph.getHNode(TOPDESC), node);
3123 private HierarchyGraph getHierarchyGraph(Descriptor d) {
3124 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
3125 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
3127 return mapDescriptorToHierarchyGraph.get(d);
3130 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
3132 // visit each node of method flow graph
3133 FlowGraph fg = getFlowGraph(md);
3134 // Set<FlowNode> nodeSet = fg.getNodeSet();
3136 Set<FlowEdge> edgeSet = fg.getEdgeSet();
3138 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
3139 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
3140 Descriptor desc = (Descriptor) iterator.next();
3141 methodGraph.getHNode(desc).setSkeleton(true);
3144 // for the method lattice, we need to look at the first element of
3145 // NTuple<Descriptor>
3146 boolean hasGlobalAccess = false;
3147 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3148 // FlowNode originalSrcNode = (FlowNode) iterator.next();
3149 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
3150 FlowEdge edge = (FlowEdge) iterator.next();
3152 FlowNode originalSrcNode = fg.getFlowNode(edge.getInitTuple());
3153 Set<FlowNode> sourceNodeSet = new HashSet<FlowNode>();
3154 if (originalSrcNode instanceof FlowReturnNode) {
3155 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
3156 // System.out.println("rnode=" + rnode);
3157 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
3158 for (Iterator iterator2 = tupleSet.iterator(); iterator2.hasNext();) {
3159 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator2.next();
3160 sourceNodeSet.add(fg.getFlowNode(nTuple));
3161 // System.out.println("&&&SOURCE fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
3164 sourceNodeSet.add(originalSrcNode);
3167 // System.out.println("---sourceNodeSet=" + sourceNodeSet + " from originalSrcNode="
3168 // + originalSrcNode);
3170 for (Iterator iterator3 = sourceNodeSet.iterator(); iterator3.hasNext();) {
3171 FlowNode srcNode = (FlowNode) iterator3.next();
3173 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
3174 Descriptor srcLocalDesc = srcNodeTuple.get(0);
3176 if (srcLocalDesc instanceof InterDescriptor
3177 && ((InterDescriptor) srcLocalDesc).getMethodArgIdxPair() != null) {
3179 if (srcNode.getCompositeLocation() == null) {
3184 // if the srcNode is started with the global descriptor
3185 // need to set as a skeleton node
3186 if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
3187 hasGlobalAccess = true;
3190 // Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(originalSrcNode);
3191 // for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3192 // FlowEdge outEdge = (FlowEdge) iterator2.next();
3193 // FlowNode originalDstNode = outEdge.getDst();
3194 FlowNode originalDstNode = fg.getFlowNode(edge.getEndTuple());
3196 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
3197 if (originalDstNode instanceof FlowReturnNode) {
3198 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
3199 // System.out.println("\n-returnNode=" + rnode);
3200 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
3201 for (Iterator iterator4 = tupleSet.iterator(); iterator4.hasNext();) {
3202 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator4.next();
3203 dstNodeSet.add(fg.getFlowNode(nTuple));
3204 // System.out.println("&&&DST fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
3207 dstNodeSet.add(originalDstNode);
3209 // System.out.println("---dstNodeSet=" + dstNodeSet);
3210 for (Iterator iterator4 = dstNodeSet.iterator(); iterator4.hasNext();) {
3211 FlowNode dstNode = (FlowNode) iterator4.next();
3213 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
3214 Descriptor dstLocalDesc = dstNodeTuple.get(0);
3216 if (dstLocalDesc instanceof InterDescriptor
3217 && ((InterDescriptor) dstLocalDesc).getMethodArgIdxPair() != null) {
3218 if (dstNode.getCompositeLocation() == null) {
3219 // System.out.println("%%%%%%%%%%%%%SKIP=" + dstNode);
3224 // if (outEdge.getInitTuple().equals(srcNodeTuple)
3225 // && outEdge.getEndTuple().equals(dstNodeTuple)) {
3227 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
3228 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
3230 // //////////////////////////
3231 // inheritance check
3232 if (mapMethodDescToHighestOverriddenMethodDesc.containsKey(md)) {
3234 MethodDescriptor highestOverriddenMethodDesc =
3235 mapMethodDescToHighestOverriddenMethodDesc.get(md);
3237 if (srcCurTuple.get(srcCurTuple.size() - 1).getSymbol().startsWith(PCLOC)) {
3241 // //////////////////////////
3243 // System.out.println("-srcCurTuple=" + srcCurTuple + " dstCurTuple=" + dstCurTuple
3244 // + " srcNode=" + srcNode + " dstNode=" + dstNode);
3246 // srcCurTuple = translateBaseTuple(srcNode, srcCurTuple);
3247 // dstCurTuple = translateBaseTuple(dstNode, dstCurTuple);
3249 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
3250 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
3252 // value flows between fields
3253 Descriptor desc = srcCurTuple.get(0);
3254 ClassDescriptor classDesc;
3256 if (desc.equals(GLOBALDESC)) {
3257 classDesc = md.getClassDesc();
3259 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
3260 classDesc = varDesc.getType().getClassDesc();
3262 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
3264 } else if ((srcCurTuple.size() == 1 && dstCurTuple.size() == 1)
3265 || ((srcCurTuple.size() > 1 || dstCurTuple.size() > 1) && !srcCurTuple.get(0).equals(
3266 dstCurTuple.get(0)))) {
3268 // value flow between a primitive local var - a primitive local var or local var -
3271 Descriptor srcDesc = srcCurTuple.get(0);
3272 Descriptor dstDesc = dstCurTuple.get(0);
3274 methodGraph.addEdge(srcDesc, dstDesc);
3276 if (fg.isParamDesc(srcDesc)) {
3277 methodGraph.setParamHNode(srcDesc);
3279 if (fg.isParamDesc(dstDesc)) {
3280 methodGraph.setParamHNode(dstDesc);
3294 // If the method accesses static fields
3295 // set hasGloabalAccess true in the method summary.
3296 if (hasGlobalAccess) {
3297 getMethodSummary(md).setHasGlobalAccess();
3299 methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
3301 if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
3302 // if the current method contains the event loop
3303 // we need to set all nodes of the hierarchy graph as a skeleton node
3304 Set<HNode> hnodeSet = methodGraph.getNodeSet();
3305 for (Iterator iterator = hnodeSet.iterator(); iterator.hasNext();) {
3306 HNode hnode = (HNode) iterator.next();
3307 hnode.setSkeleton(true);
3313 private NTuple<Descriptor> translateBaseTuple(FlowNode flowNode, NTuple<Descriptor> inTuple) {
3315 if (flowNode.getBaseTuple() != null) {
3317 NTuple<Descriptor> translatedTuple = new NTuple<Descriptor>();
3319 NTuple<Descriptor> baseTuple = flowNode.getBaseTuple();
3321 for (int i = 0; i < baseTuple.size(); i++) {
3322 translatedTuple.add(baseTuple.get(i));
3325 for (int i = 1; i < inTuple.size(); i++) {
3326 translatedTuple.add(inTuple.get(i));
3329 // System.out.println("------TRANSLATED " + inTuple + " -> " + translatedTuple);
3330 return translatedTuple;
3338 private MethodSummary getMethodSummary(MethodDescriptor md) {
3339 if (!mapDescToLocationSummary.containsKey(md)) {
3340 mapDescToLocationSummary.put(md, new MethodSummary(md));
3342 return (MethodSummary) mapDescToLocationSummary.get(md);
3345 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
3347 String classSymbol = cd.getSymbol();
3348 int idx = classSymbol.lastIndexOf("$");
3350 classSymbol = classSymbol.substring(idx + 1);
3353 String pattern = "class " + classSymbol + " ";
3354 if (strLine.indexOf(pattern) != -1) {
3355 mapDescToDefinitionLine.put(cd, lineNum);
3359 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
3361 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
3362 MethodDescriptor md = (MethodDescriptor) iterator.next();
3363 String pattern = md.getMethodDeclaration();
3364 if (strLine.indexOf(pattern) != -1) {
3365 mapDescToDefinitionLine.put(md, lineNum);
3366 methodSet.remove(md);
3373 private void readOriginalSourceFiles() {
3375 SymbolTable classtable = state.getClassSymbolTable();
3377 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
3378 classDescSet.addAll(classtable.getValueSet());
3381 // inefficient implement. it may re-visit the same file if the file
3382 // contains more than one class definitions.
3383 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
3384 ClassDescriptor cd = (ClassDescriptor) iterator.next();
3386 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
3387 methodSet.addAll(cd.getMethodTable().getValueSet());
3389 String sourceFileName = cd.getSourceFileName();
3390 Vector<String> lineVec = new Vector<String>();
3392 mapFileNameToLineVector.put(sourceFileName, lineVec);
3394 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
3397 lineVec.add(""); // the index is started from 1.
3398 while ((strLine = in.readLine()) != null) {
3399 lineVec.add(lineNum, strLine);
3400 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
3401 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
3407 } catch (IOException e) {
3408 e.printStackTrace();
3413 private String generateLatticeDefinition(Descriptor desc) {
3415 Set<String> sharedLocSet = new HashSet<String>();
3417 SSJavaLattice<String> lattice = getLattice(desc);
3418 String rtr = "@LATTICE(\"";
3420 Map<String, Set<String>> map = lattice.getTable();
3421 Set<String> keySet = map.keySet();
3423 // System.out.println("@generateLatticeDefinition=" + desc + " map=" + map);
3425 boolean first = true;
3426 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
3427 String key = (String) iterator.next();
3428 if (!key.equals(lattice.getTopItem())) {
3429 Set<String> connectedSet = map.get(key);
3431 if (connectedSet.size() == 1) {
3432 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
3440 if (lattice.isSharedLoc(key)) {
3441 rtr += "," + key + "*";
3446 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
3447 String loc = (String) iterator2.next();
3448 if (!loc.equals(lattice.getBottomItem())) {
3455 rtr += loc + "<" + key;
3456 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
3457 rtr += "," + key + "*";
3458 sharedLocSet.add(key);
3460 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
3461 rtr += "," + loc + "*";
3462 sharedLocSet.add(loc);
3470 if (desc instanceof MethodDescriptor) {
3471 // System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
3473 MethodDescriptor md = (MethodDescriptor) desc;
3474 MethodSummary methodSummary = getMethodSummary(md);
3476 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
3477 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc) && returnType != null
3478 && (!returnType.isVoid())) {
3479 CompositeLocation returnLoc = methodSummary.getRETURNLoc();
3480 if (returnLoc.getSize() == 1) {
3481 String returnLocStr = generateLocationAnnoatation(methodSummary.getRETURNLoc());
3482 if (rtr.indexOf(returnLocStr) == -1) {
3483 rtr += "," + returnLocStr;
3489 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
3490 if (returnType != null && (!returnType.isVoid())) {
3492 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
3495 CompositeLocation pcLoc = methodSummary.getPCLoc();
3496 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
3497 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
3501 if (!md.isStatic()) {
3502 rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
3504 rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
3513 private void generateAnnoatedCode() {
3515 readOriginalSourceFiles();
3518 while (!toAnalyzeIsEmpty()) {
3519 ClassDescriptor cd = toAnalyzeNext();
3521 setupToAnalazeMethod(cd);
3523 String sourceFileName = cd.getSourceFileName();
3525 if (cd.isInterface()) {
3529 int classDefLine = mapDescToDefinitionLine.get(cd);
3530 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
3532 LocationSummary fieldLocSummary = getLocationSummary(cd);
3534 String fieldLatticeDefStr = generateLatticeDefinition(cd);
3535 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
3536 sourceVec.set(classDefLine, annoatedSrc);
3538 // generate annotations for field declarations
3539 // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
3540 Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
3542 for (Iterator iter = cd.getFields(); iter.hasNext();) {
3543 FieldDescriptor fd = (FieldDescriptor) iter.next();
3545 String locAnnotationStr;
3546 // CompositeLocation inferLoc = inferLocMap.get(fd);
3547 String locName = mapFieldNameToLocName.get(fd.getSymbol());
3549 if (locName != null) {
3550 // infer loc is null if the corresponding field is static and final
3551 // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
3552 locAnnotationStr = "@LOC(\"" + locName + "\")";
3553 int fdLineNum = fd.getLineNum();
3554 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
3555 String fieldDeclaration = fd.toString();
3556 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
3557 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
3558 sourceVec.set(fdLineNum, annoatedStr);
3563 while (!toAnalyzeMethodIsEmpty()) {
3564 MethodDescriptor md = toAnalyzeMethodNext();
3566 if (!ssjava.needTobeAnnotated(md)) {
3570 SSJavaLattice<String> methodLattice = md2lattice.get(md);
3571 // System.out.println("md=" + md + " methodLattice=" + methodLattice);
3572 if (methodLattice != null) {
3574 int methodDefLine = md.getLineNum();
3576 // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
3577 // Map<Descriptor, CompositeLocation> methodInferLocMap =
3578 // methodLocInfo.getMapDescToInferLocation();
3580 MethodSummary methodSummary = getMethodSummary(md);
3582 Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
3583 methodSummary.getMapVarDescToInferCompositeLocation();
3584 // System.out.println("-----md=" + md + " methodDefLine=" + methodDefLine);
3585 // System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
3587 Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
3589 Set<String> localLocElementSet = methodLattice.getElementSet();
3591 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
3592 Descriptor localVarDesc = (Descriptor) iterator.next();
3593 // System.out.println("-------localVarDesc=" + localVarDesc);
3594 CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
3596 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
3597 if (!localLocElementSet.contains(localLocIdentifier)) {
3598 methodLattice.put(localLocIdentifier);
3601 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
3603 if (!isParameter(md, localVarDesc)) {
3604 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
3605 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
3606 String orgSourceLine = sourceVec.get(varLineNum);
3607 // System.out.println("varLineNum=" + varLineNum + " org src=" + orgSourceLine);
3609 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
3610 // System.out.println("idx=" + idx
3611 // + " generateVarDeclaration((VarDescriptor) localVarDesc)="
3612 // + generateVarDeclaration((VarDescriptor) localVarDesc));
3614 String annoatedStr =
3615 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
3616 + orgSourceLine.substring(idx);
3617 sourceVec.set(varLineNum, annoatedStr);
3620 String methodDefStr = sourceVec.get(methodDefLine);
3623 getParamLocation(methodDefStr,
3624 generateVarDeclaration((VarDescriptor) localVarDesc));
3625 // System.out.println("methodDefStr=" + methodDefStr + " localVarDesc=" + localVarDesc
3626 // + " idx=" + idx);
3629 String annoatedStr =
3630 methodDefStr.substring(0, idx) + locAnnotationStr + " "
3631 + methodDefStr.substring(idx);
3632 sourceVec.set(methodDefLine, annoatedStr);
3637 // check if the lattice has to have the location type for the this
3640 // boolean needToAddthisRef = hasThisReference(md);
3641 // if (localLocElementSet.contains("this")) {
3642 // methodLattice.put("this");
3645 String methodLatticeDefStr = generateLatticeDefinition(md);
3646 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
3647 sourceVec.set(methodDefLine, annoatedStr);
3657 private boolean hasThisReference(MethodDescriptor md) {
3659 FlowGraph fg = getFlowGraph(md);
3660 Set<FlowNode> nodeSet = fg.getNodeSet();
3661 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3662 FlowNode flowNode = (FlowNode) iterator.next();
3663 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
3671 private int getParamLocation(String methodStr, String paramStr) {
3673 String pattern = paramStr + ",";
3675 int idx = methodStr.indexOf(pattern);
3679 pattern = paramStr + ")";
3680 return methodStr.indexOf(pattern);
3685 private String generateVarDeclaration(VarDescriptor varDesc) {
3687 TypeDescriptor td = varDesc.getType();
3688 String rtr = td.toString();
3690 for (int i = 0; i < td.getArrayCount(); i++) {
3694 rtr += " " + varDesc.getName();
3699 private String generateLocationAnnoatation(CompositeLocation loc) {
3702 Location methodLoc = loc.get(0);
3703 rtr += methodLoc.getLocIdentifier();
3705 for (int i = 1; i < loc.getSize(); i++) {
3706 Location element = loc.get(i);
3707 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
3713 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
3714 return getFlowGraph(md).isParamDesc(localVarDesc);
3717 private String extractFileName(String fileName) {
3718 int idx = fileName.lastIndexOf("/");
3722 return fileName.substring(idx + 1);
3727 private void codeGen() {
3729 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
3730 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
3731 String orgFileName = (String) iterator.next();
3732 String outputFileName = extractFileName(orgFileName);
3734 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
3738 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
3739 BufferedWriter out = new BufferedWriter(fileWriter);
3741 for (int i = 0; i < sourceVec.size(); i++) {
3742 out.write(sourceVec.get(i));
3746 } catch (IOException e) {
3747 e.printStackTrace();
3754 private void checkLattices() {
3756 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
3758 // current descriptors to visit in fixed-point interprocedural analysis,
3760 // dependency in the call graph
3761 methodDescriptorsToVisitStack.clear();
3763 // descriptorListToAnalyze.removeFirst();
3765 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
3766 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
3768 while (!descriptorListToAnalyze.isEmpty()) {
3769 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
3770 checkLatticesOfVirtualMethods(md);
3775 private void debug_writeLatticeDotFile() {
3776 // generate lattice dot file
3780 while (!toAnalyzeIsEmpty()) {
3781 ClassDescriptor cd = toAnalyzeNext();
3783 setupToAnalazeMethod(cd);
3785 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
3786 if (classLattice != null) {
3787 ssjava.writeLatticeDotFile(cd, null, classLattice);
3788 // debug_printDescriptorToLocNameMapping(cd);
3791 while (!toAnalyzeMethodIsEmpty()) {
3792 MethodDescriptor md = toAnalyzeMethodNext();
3793 SSJavaLattice<String> methodLattice = md2lattice.get(md);
3794 if (methodLattice != null) {
3795 ssjava.writeLatticeDotFile(cd, md, methodLattice);
3796 // debug_printDescriptorToLocNameMapping(md);
3803 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
3805 LocationInfo info = getLocationInfo(desc);
3806 System.out.println("## " + desc + " ##");
3807 System.out.println(info.getMapDescToInferLocation());
3808 LocationInfo locInfo = getLocationInfo(desc);
3809 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
3810 System.out.println("###################");
3814 private void calculateExtraLocations() {
3816 // LinkedList<MethodDescriptor> methodDescList = ssjava.getSortedDescriptors();
3817 LinkedList<MethodDescriptor> methodDescList =
3818 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3819 for (Iterator iterator = methodDescList.iterator(); iterator.hasNext();) {
3820 MethodDescriptor md = (MethodDescriptor) iterator.next();
3821 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
3822 calculateExtraLocations(md);
3828 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
3830 if (!md.isStatic()) {
3831 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3832 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3834 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
3835 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
3836 if (!md.equals(mdCallee)) {
3837 checkConsistency(md, mdCallee);
3845 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
3847 // check that two lattice have the same relations between parameters(+PC
3848 // LOC, GLOBAL_LOC RETURN LOC)
3850 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
3851 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
3853 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
3854 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
3856 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
3857 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
3859 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
3861 // add location types of paramters
3862 for (int idx = 0; idx < numParam; idx++) {
3863 list1.add(paramMap1.get(Integer.valueOf(idx)));
3864 list2.add(paramMap2.get(Integer.valueOf(idx)));
3867 // add program counter location
3868 list1.add(locInfo1.getPCLoc());
3869 list2.add(locInfo2.getPCLoc());
3871 if (!md1.getReturnType().isVoid()) {
3872 // add return value location
3873 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
3874 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
3879 // add global location type
3880 if (md1.isStatic()) {
3881 CompositeLocation globalLoc1 =
3882 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
3883 CompositeLocation globalLoc2 =
3884 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
3885 list1.add(globalLoc1);
3886 list2.add(globalLoc2);
3889 for (int i = 0; i < list1.size(); i++) {
3890 CompositeLocation locA1 = list1.get(i);
3891 CompositeLocation locA2 = list2.get(i);
3892 for (int k = 0; k < list1.size(); k++) {
3894 CompositeLocation locB1 = list1.get(k);
3895 CompositeLocation locB2 = list2.get(k);
3896 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
3898 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
3901 throw new Error("The method " + md1 + " is not consistent with the method " + md2
3902 + ".:: They have a different ordering relation between locations (" + locA1 + ","
3903 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
3911 private String getSymbol(int idx, FlowNode node) {
3912 Descriptor desc = node.getDescTuple().get(idx);
3913 return desc.getSymbol();
3916 private Descriptor getDescriptor(int idx, FlowNode node) {
3917 Descriptor desc = node.getDescTuple().get(idx);
3921 private void calculatePCLOC(MethodDescriptor md) {
3923 // System.out.println("#CalculatePCLOC");
3924 MethodSummary methodSummary = getMethodSummary(md);
3925 FlowGraph fg = getFlowGraph(md);
3926 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
3928 // calculate the initial program counter location
3929 // PC location is higher than location types of parameters which has incoming flows.
3931 Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
3932 Set<Descriptor> paramDescNOTHavingInFlowSet = new HashSet<Descriptor>();
3933 // Set<FlowNode> paramNodeNOThavingInFlowSet = new HashSet<FlowNode>();
3935 int numParams = fg.getNumParameters();
3936 for (int i = 0; i < numParams; i++) {
3937 FlowNode paramFlowNode = fg.getParamFlowNode(i);
3938 Descriptor prefix = paramFlowNode.getDescTuple().get(0);
3939 NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
3940 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
3942 Set<FlowNode> inNodeToParamSet = fg.getIncomingNodeSetByPrefix(prefix);
3943 if (inNodeToParamSet.size() > 0) {
3944 // parameter has in-value flows
3946 for (Iterator iterator = inNodeToParamSet.iterator(); iterator.hasNext();) {
3947 FlowNode inNode = (FlowNode) iterator.next();
3948 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(inNode);
3949 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3950 FlowEdge flowEdge = (FlowEdge) iterator2.next();
3951 if (flowEdge.getEndTuple().startsWith(prefix)) {
3952 NTuple<Location> paramLocTupleWithIncomingFlow =
3953 translateToLocTuple(md, flowEdge.getEndTuple());
3954 paramLocTupleHavingInFlowSet.add(paramLocTupleWithIncomingFlow);
3959 // paramLocTupleHavingInFlowSet.add(paramLocTuple);
3961 // paramNodeNOThavingInFlowSet.add(fg.getFlowNode(paramDescTuple));
3962 paramDescNOTHavingInFlowSet.add(prefix);
3966 // System.out.println("paramLocTupleHavingInFlowSet=" + paramLocTupleHavingInFlowSet);
3968 if (paramLocTupleHavingInFlowSet.size() > 0
3969 && !coversAllParamters(md, fg, paramLocTupleHavingInFlowSet)) {
3971 // Here, generates a location in the method lattice that is higher than the
3972 // paramLocTupleHavingInFlowSet
3973 NTuple<Location> pcLocTuple =
3974 generateLocTupleRelativeTo(md, paramLocTupleHavingInFlowSet, PCLOC);
3976 NTuple<Descriptor> pcDescTuple = translateToDescTuple(pcLocTuple);
3978 // System.out.println("pcLoc=" + pcLocTuple);
3980 CompositeLocation curPCLoc = methodSummary.getPCLoc();
3981 if (curPCLoc.get(0).isTop() || pcLocTuple.size() > curPCLoc.getSize()) {
3982 methodSummary.setPCLoc(new CompositeLocation(pcLocTuple));
3984 Set<FlowNode> flowNodeLowerthanPCLocSet = new HashSet<FlowNode>();
3985 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
3986 // add ordering relations s.t. PCLOC is higher than all flow nodes except the set of
3987 // parameters that do not have incoming flows
3988 for (Iterator iterator = fg.getNodeSet().iterator(); iterator.hasNext();) {
3989 FlowNode node = (FlowNode) iterator.next();
3991 if (!(node instanceof FlowReturnNode)) {
3992 if (!paramDescNOTHavingInFlowSet.contains(node.getCurrentDescTuple().get(0))) {
3993 flowNodeLowerthanPCLocSet.add(node);
3994 fg.addValueFlowEdge(pcDescTuple, node.getDescTuple());
3996 subGlobalFlowGraph.addValueFlowEdge(pcLocTuple,
3997 translateToLocTuple(md, node.getDescTuple()));
4000 // System.out.println("***SKIP PCLOC -> RETURNLOC=" + node);
4004 fg.getFlowNode(translateToDescTuple(pcLocTuple)).setSkeleton(true);
4006 if (pcLocTuple.get(0).getLocDescriptor().equals(md.getThis())) {
4007 for (Iterator iterator = flowNodeLowerthanPCLocSet.iterator(); iterator.hasNext();) {
4008 FlowNode lowerNode = (FlowNode) iterator.next();
4009 if (lowerNode.getDescTuple().size() == 1 && lowerNode.getCompositeLocation() == null) {
4010 NTuple<Location> lowerLocTuple = translateToLocTuple(md, lowerNode.getDescTuple());
4011 CompositeLocation newComp =
4012 calculateCompositeLocationFromSubGlobalGraph(md, lowerNode);
4013 if (newComp != null) {
4014 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(lowerLocTuple.get(0),
4016 lowerNode.setCompositeLocation(newComp);
4017 // System.out.println("NEW COMP LOC=" + newComp + " to lowerNode=" + lowerNode);
4030 private int countFirstDescriptorSetSize(Set<NTuple<Location>> set) {
4032 Set<Descriptor> descSet = new HashSet<Descriptor>();
4034 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
4035 NTuple<Location> locTuple = (NTuple<Location>) iterator.next();
4036 descSet.add(locTuple.get(0).getLocDescriptor());
4039 return descSet.size();
4042 private boolean coversAllParamters(MethodDescriptor md, FlowGraph fg,
4043 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
4045 int numParam = fg.getNumParameters();
4046 // int size = paramLocTupleHavingInFlowSet.size();
4047 int size = countFirstDescriptorSetSize(paramLocTupleHavingInFlowSet);
4049 // System.out.println("numParam=" + numParam + " size=" + size);
4051 // if (!md.isStatic()) {
4053 // // if the method is not static && there is a parameter composite location &&
4054 // // it is started with 'this',
4055 // // paramLocTupleHavingInFlowSet need to have 'this' parameter.
4057 // FlowNode thisParamNode = fg.getParamFlowNode(0);
4058 // NTuple<Location> thisParamLocTuple =
4059 // translateToLocTuple(md, thisParamNode.getCurrentDescTuple());
4061 // if (!paramLocTupleHavingInFlowSet.contains(thisParamLocTuple)) {
4063 // for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
4064 // NTuple<Location> paramTuple = (NTuple<Location>) iterator.next();
4065 // if (paramTuple.size() > 1 && paramTuple.get(0).getLocDescriptor().equals(md.getThis())) {
4066 // // paramLocTupleHavingInFlowSet.add(thisParamLocTuple);
4075 if (size == numParam) {
4083 private void calculateRETURNLOC(MethodDescriptor md) {
4085 // System.out.println("#calculateRETURNLOC= " + md);
4087 // calculate a return location:
4088 // the return location type is lower than all parameters and the location of return values
4089 MethodSummary methodSummary = getMethodSummary(md);
4090 // if (methodSummary.getRETURNLoc() != null) {
4091 // System.out.println("$HERE?");
4095 FlowGraph fg = getFlowGraph(md);
4096 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
4097 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
4099 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
4100 // first, generate the set of return value location types that starts
4101 // with 'this' reference
4103 Set<FlowNode> paramFlowNodeFlowingToReturnValueSet = getParamNodeFlowingToReturnValue(md);
4104 // System.out.println("paramFlowNodeFlowingToReturnValueSet="
4105 // + paramFlowNodeFlowingToReturnValueSet);
4107 Set<NTuple<Location>> tupleToBeHigherThanReturnLocSet = new HashSet<NTuple<Location>>();
4108 for (Iterator iterator = paramFlowNodeFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
4109 FlowNode fn = (FlowNode) iterator.next();
4110 NTuple<Descriptor> paramDescTuple = fn.getCurrentDescTuple();
4111 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, paramDescTuple));
4114 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
4115 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
4116 FlowNode returnNode = (FlowNode) iterator.next();
4117 NTuple<Descriptor> returnDescTuple = returnNode.getCurrentDescTuple();
4118 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, returnDescTuple));
4120 // System.out.println("-flow graph's returnNodeSet=" + returnNodeSet);
4121 // System.out.println("tupleSetToBeHigherThanReturnLoc=" + tupleToBeHigherThanReturnLocSet);
4123 // Here, generates a return location in the method lattice that is lower than the
4124 // locFlowingToReturnValueSet
4125 NTuple<Location> returnLocTuple =
4126 generateLocTupleRelativeTo(md, tupleToBeHigherThanReturnLocSet, RLOC);
4128 // System.out.println("returnLocTuple=" + returnLocTuple);
4129 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnLocTuple);
4130 CompositeLocation curReturnLoc = methodSummary.getRETURNLoc();
4131 if (curReturnLoc == null || returnDescTuple.size() > curReturnLoc.getSize()) {
4132 methodSummary.setRETURNLoc(new CompositeLocation(returnLocTuple));
4134 for (Iterator iterator = tupleToBeHigherThanReturnLocSet.iterator(); iterator.hasNext();) {
4135 NTuple<Location> higherTuple = (NTuple<Location>) iterator.next();
4136 fg.addValueFlowEdge(translateToDescTuple(higherTuple), returnDescTuple);
4138 fg.getFlowNode(returnDescTuple).setSkeleton(true);
4142 // makes sure that PCLOC is higher than RETURNLOC
4143 CompositeLocation pcLoc = methodSummary.getPCLoc();
4144 if (!pcLoc.get(0).isTop()) {
4145 NTuple<Descriptor> pcLocDescTuple = translateToDescTuple(pcLoc.getTuple());
4146 fg.addValueFlowEdge(pcLocDescTuple, returnDescTuple);
4153 private void calculateExtraLocations(MethodDescriptor md) {
4154 // calcualte pcloc, returnloc,...
4156 System.out.println("\nSSJAVA:Calculate PCLOC/RETURNLOC locations: " + md);
4159 calculateRETURNLOC(md);
4163 private NTuple<Location> generateLocTupleRelativeTo(MethodDescriptor md,
4164 Set<NTuple<Location>> paramLocTupleHavingInFlowSet, String locNamePrefix) {
4166 // System.out.println("-generateLocTupleRelativeTo=" + paramLocTupleHavingInFlowSet);
4168 NTuple<Location> higherLocTuple = new NTuple<Location>();
4170 VarDescriptor thisVarDesc = md.getThis();
4171 // check if all paramter loc tuple is started with 'this' reference
4172 boolean hasParamNotStartedWithThisRef = false;
4176 Set<NTuple<Location>> paramLocTupleStartedWithThis = new HashSet<NTuple<Location>>();
4178 next: for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
4179 NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
4180 Descriptor paramLocalDesc = paramLocTuple.get(0).getLocDescriptor();
4181 if (!paramLocalDesc.equals(thisVarDesc)) {
4183 Set<FlowNode> inNodeSet = getFlowGraph(md).getIncomingNodeSetByPrefix(paramLocalDesc);
4184 for (Iterator iterator2 = inNodeSet.iterator(); iterator2.hasNext();) {
4185 FlowNode flowNode = (FlowNode) iterator2.next();
4186 if (flowNode.getDescTuple().startsWith(thisVarDesc)) {
4187 // System.out.println("paramLocTuple=" + paramLocTuple + " is lower than THIS");
4191 hasParamNotStartedWithThisRef = true;
4193 } else if (paramLocTuple.size() > 1) {
4194 paramLocTupleStartedWithThis.add(paramLocTuple);
4195 if (minSize == 0 || minSize > paramLocTuple.size()) {
4196 minSize = paramLocTuple.size();
4201 // System.out.println("---paramLocTupleStartedWithThis=" + paramLocTupleStartedWithThis);
4202 Descriptor enclosingDesc = md;
4203 if (hasParamNotStartedWithThisRef) {
4204 // in this case, PCLOC will be the local location
4206 // all parameter is started with 'this', so PCLOC will be set relative to the composite
4207 // location started with 'this'.
4208 // for (int idx = 0; idx < minSize - 1; idx++) {
4209 for (int idx = 0; idx < 1; idx++) {
4210 Set<Descriptor> locDescSet = new HashSet<Descriptor>();
4211 Location curLoc = null;
4212 NTuple<Location> paramLocTuple = null;
4213 for (Iterator iterator = paramLocTupleStartedWithThis.iterator(); iterator.hasNext();) {
4214 paramLocTuple = (NTuple<Location>) iterator.next();
4215 // System.out.println("-----paramLocTuple=" + paramLocTuple + " idx=" + idx);
4216 curLoc = paramLocTuple.get(idx);
4217 Descriptor locDesc = curLoc.getLocDescriptor();
4218 locDescSet.add(locDesc);
4220 // System.out.println("-----locDescSet=" + locDescSet + " idx=" + idx);
4221 if (locDescSet.size() != 1) {
4224 Location newLocElement = new Location(curLoc.getDescriptor(), curLoc.getLocDescriptor());
4225 // System.out.println("newLocElement" + newLocElement);
4226 higherLocTuple.add(newLocElement);
4227 enclosingDesc = getClassTypeDescriptor(curLoc.getLocDescriptor());
4232 String locIdentifier = locNamePrefix + (locSeed++);
4233 NameDescriptor locDesc = new NameDescriptor(locIdentifier);
4234 Location newLoc = new Location(enclosingDesc, locDesc);
4235 higherLocTuple.add(newLoc);
4236 // System.out.println("---new loc tuple=" + higherLocTuple);
4238 return higherLocTuple;
4242 public ClassDescriptor getClassTypeDescriptor(Descriptor in) {
4244 if (in instanceof VarDescriptor) {
4245 return ((VarDescriptor) in).getType().getClassDesc();
4246 } else if (in instanceof FieldDescriptor) {
4247 return ((FieldDescriptor) in).getType().getClassDesc();
4249 // else if (in instanceof LocationDescriptor) {
4250 // // here is the case that the descriptor 'in' is the last element of the assigned composite
4252 // return ((VarDescriptor) locTuple.get(0).getLocDescriptor()).getType().getClassDesc();
4260 private Set<NTuple<Location>> calculateHighestLocTupleSet(
4261 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
4263 Set<NTuple<Location>> highestSet = new HashSet<NTuple<Location>>();
4265 Iterator<NTuple<Location>> iterator = paramLocTupleHavingInFlowSet.iterator();
4266 NTuple<Location> highest = iterator.next();
4268 for (; iterator.hasNext();) {
4269 NTuple<Location> curLocTuple = (NTuple<Location>) iterator.next();
4270 if (isHigherThan(curLocTuple, highest)) {
4271 // System.out.println(curLocTuple + " is greater than " + highest);
4272 highest = curLocTuple;
4276 highestSet.add(highest);
4278 MethodDescriptor md = (MethodDescriptor) highest.get(0).getDescriptor();
4279 VarDescriptor thisVarDesc = md.getThis();
4281 // System.out.println("highest=" + highest);
4283 for (Iterator<NTuple<Location>> iter = paramLocTupleHavingInFlowSet.iterator(); iter.hasNext();) {
4284 NTuple<Location> curLocTuple = iter.next();
4286 if (!curLocTuple.equals(highest) && !hasOrderingRelation(highest, curLocTuple)) {
4288 // System.out.println("add it to the highest set=" + curLocTuple);
4289 highestSet.add(curLocTuple);
4298 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
4299 Set<String> higherLocSet = new HashSet<String>();
4301 Set<String> locSet = lattice.getTable().keySet();
4302 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
4303 String element = (String) iterator.next();
4304 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
4305 higherLocSet.add(element);
4308 return higherLocSet;
4311 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
4312 Set<CompositeLocation> set) {
4314 CompositeLocation lowest = set.iterator().next();
4316 if (set.size() == 1) {
4320 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
4321 CompositeLocation loc = (CompositeLocation) iterator.next();
4323 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
4324 // if there is a case where composite locations are incomparable, just
4329 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
4336 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
4337 CompositeLocation comp2) {
4339 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
4341 for (int idx = 0; idx < size; idx++) {
4342 Location loc1 = comp1.get(idx);
4343 Location loc2 = comp2.get(idx);
4345 Descriptor desc1 = loc1.getDescriptor();
4346 Descriptor desc2 = loc2.getDescriptor();
4348 if (!desc1.equals(desc2)) {
4349 throw new Error("Fail to compare " + comp1 + " and " + comp2);
4352 String symbol1 = loc1.getLocIdentifier();
4353 String symbol2 = loc2.getLocIdentifier();
4355 SSJavaLattice<String> lattice;
4357 lattice = methodLattice;
4359 lattice = getLattice(desc1);
4362 if (symbol1.equals(symbol2)) {
4364 } else if (!lattice.isComparable(symbol1, symbol2)) {
4373 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
4374 CompositeLocation comp2) {
4376 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
4378 for (int idx = 0; idx < size; idx++) {
4379 Location loc1 = comp1.get(idx);
4380 Location loc2 = comp2.get(idx);
4382 Descriptor desc1 = loc1.getDescriptor();
4383 Descriptor desc2 = loc2.getDescriptor();
4385 if (!desc1.equals(desc2)) {
4386 throw new Error("Fail to compare " + comp1 + " and " + comp2);
4389 String symbol1 = loc1.getLocIdentifier();
4390 String symbol2 = loc2.getLocIdentifier();
4392 SSJavaLattice<String> lattice;
4394 lattice = methodLattice;
4396 lattice = getLattice(desc1);
4399 if (symbol1.equals(symbol2)) {
4401 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
4412 private GlobalFlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
4414 if (!mapMethodDescriptorToSubGlobalFlowGraph.containsKey(md)) {
4415 mapMethodDescriptorToSubGlobalFlowGraph.put(md, new GlobalFlowGraph(md));
4417 return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
4420 private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
4421 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
4423 // System.out.println("-propagateFlowsToCallerWithNoCompositeLocation=" + min.printNode(0));
4424 // if the parameter A reaches to the parameter B
4425 // then, add an edge the argument A -> the argument B to the caller's flow
4428 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
4429 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
4430 int numParam = calleeFlowGraph.getNumParameters();
4432 for (int i = 0; i < numParam; i++) {
4433 for (int k = 0; k < numParam; k++) {
4437 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
4438 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
4440 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
4441 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
4443 // check if the callee propagates an ordering constraints through
4446 // Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
4447 Set<FlowNode> localReachSet =
4448 calleeFlowGraph.getReachableSetFrom(paramNode1.getDescTuple());
4450 NTuple<Descriptor> paramDescTuple1 = paramNode1.getCurrentDescTuple();
4451 NTuple<Descriptor> paramDescTuple2 = paramNode2.getCurrentDescTuple();
4453 // System.out.println("-param1CurTuple=" + paramDescTuple1 + " param2CurTuple="
4454 // + paramDescTuple2);
4455 // System.out.println("-- localReachSet from param1=" + localReachSet);
4457 if (paramDescTuple1.get(0).equals(paramDescTuple2.get(0))) {
4458 // if two parameters share the same prefix
4459 // it already has been assigned to a composite location
4460 // so we don't need to add an additional ordering relation caused by these two
4465 if (arg1Tuple.size() > 0 && arg2Tuple.size() > 0
4466 && containsPrefix(paramNode2.getDescTuple().get(0), localReachSet)) {
4467 // need to propagate an ordering relation s.t. arg1 is higher
4469 // System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
4471 // add a new flow between the corresponding arguments.
4472 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
4473 // System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
4476 // .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
4480 // System.out.println();
4485 // if a parameter has a composite location and the first element of the parameter location
4486 // matches the callee's 'this'
4487 // we have a more specific constraint: the caller's corresponding argument is higher than the
4488 // parameter location which is translated into the caller
4490 for (int idx = 0; idx < numParam; idx++) {
4491 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
4492 CompositeLocation compLoc = paramNode.getCompositeLocation();
4493 // System.out.println("paramNode=" + paramNode + " compLoc=" + compLoc);
4494 if (compLoc != null && compLoc.get(0).getLocDescriptor().equals(min.getMethod().getThis())) {
4495 // System.out.println("$$$COMPLOC CASE=" + compLoc + " idx=" + idx);
4497 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
4498 // System.out.println("--- argTuple=" + argTuple + " current compLoc="
4499 // + callerFlowGraph.getFlowNode(argTuple).getCompositeLocation());
4501 NTuple<Descriptor> translatedParamTuple =
4502 translateCompositeLocationToCaller(idx, min, compLoc);
4503 // System.out.println("add a flow edge= " + argTuple + "->" + translatedParamTuple);
4504 callerFlowGraph.addValueFlowEdge(argTuple, translatedParamTuple);
4506 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
4507 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
4508 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
4509 callerFlowGraph.addValueFlowEdge(translateToDescTuple(pcLocTuple), translatedParamTuple);
4517 private boolean containsPrefix(Descriptor prefixDesc, Set<FlowNode> set) {
4519 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
4520 FlowNode flowNode = (FlowNode) iterator.next();
4521 if (flowNode.getDescTuple().startsWith(prefixDesc)) {
4522 // System.out.println("FOUND=" + flowNode);
4529 private NTuple<Descriptor> translateCompositeLocationToCaller(int idx, MethodInvokeNode min,
4530 CompositeLocation compLocForParam1) {
4532 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
4534 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
4535 for (int i = 0; i < baseTuple.size(); i++) {
4536 tuple.add(baseTuple.get(i));
4539 for (int i = 1; i < compLocForParam1.getSize(); i++) {
4540 Location loc = compLocForParam1.get(i);
4541 tuple.add(loc.getLocDescriptor());
4547 private CompositeLocation generateCompositeLocation(NTuple<Location> prefixLocTuple) {
4549 // System.out.println("generateCompositeLocation=" + prefixLocTuple);
4551 CompositeLocation newCompLoc = new CompositeLocation();
4552 for (int i = 0; i < prefixLocTuple.size(); i++) {
4553 newCompLoc.addLocation(prefixLocTuple.get(i));
4556 Descriptor lastDescOfPrefix = prefixLocTuple.get(prefixLocTuple.size() - 1).getLocDescriptor();
4558 ClassDescriptor enclosingDescriptor;
4559 if (lastDescOfPrefix instanceof FieldDescriptor) {
4560 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
4561 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
4562 } else if (lastDescOfPrefix.equals(GLOBALDESC)) {
4563 MethodDescriptor currentMethodDesc = (MethodDescriptor) prefixLocTuple.get(0).getDescriptor();
4564 enclosingDescriptor = currentMethodDesc.getClassDesc();
4566 // var descriptor case
4567 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
4569 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
4571 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
4572 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
4574 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
4575 newLoc.setLocDescriptor(newLocDescriptor);
4576 newCompLoc.addLocation(newLoc);
4578 // System.out.println("--newCompLoc=" + newCompLoc);
4582 private CompositeLocation generateCompositeLocation(MethodDescriptor md,
4583 NTuple<Descriptor> paramPrefix) {
4585 // System.out.println("generateCompositeLocation=" + paramPrefix);
4587 CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
4589 Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
4590 // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
4591 // + lastDescOfPrefix.getClass());
4592 ClassDescriptor enclosingDescriptor;
4593 if (lastDescOfPrefix instanceof FieldDescriptor) {
4594 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
4595 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
4597 // var descriptor case
4598 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
4600 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
4602 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
4603 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
4605 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
4606 newLoc.setLocDescriptor(newLocDescriptor);
4607 newCompLoc.addLocation(newLoc);
4609 // System.out.println("--newCompLoc=" + newCompLoc);
4613 private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
4614 MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
4616 List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
4618 for (int i = 0; i < callerPrefixList.size(); i++) {
4619 NTuple<Descriptor> prefix = callerPrefixList.get(i);
4620 if (prefix.startsWith(baseRef)) {
4621 NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
4622 calleePrefix.add(mdCallee.getThis());
4623 for (int k = 1; k < prefix.size(); k++) {
4624 calleePrefix.add(prefix.get(k));
4626 calleePrefixList.add(calleePrefix);
4630 return calleePrefixList;
4634 public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
4636 CompositeLocation compLoc = new CompositeLocation();
4638 Descriptor enclosingDescriptor = md;
4640 for (int i = 0; i < tuple.size(); i++) {
4641 Descriptor curDescriptor = tuple.get(i);
4642 Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
4643 locElement.setLocDescriptor(curDescriptor);
4644 compLoc.addLocation(locElement);
4646 if (curDescriptor instanceof VarDescriptor) {
4647 enclosingDescriptor = md.getClassDesc();
4648 } else if (curDescriptor instanceof FieldDescriptor) {
4649 enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
4650 } else if (curDescriptor instanceof NameDescriptor) {
4651 // it is "GLOBAL LOC" case!
4652 enclosingDescriptor = GLOBALDESC;
4653 } else if (curDescriptor instanceof InterDescriptor) {
4654 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(curDescriptor);
4656 enclosingDescriptor = null;
4664 private LocationDescriptor generateNewLocationDescriptor() {
4665 return new LocationDescriptor("Loc" + (locSeed++));
4668 private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
4670 // return the index where the prefix shared by tuple1 and tuple2 is ended
4671 // if there is no prefix shared by both of them, return -1
4673 int minSize = tuple1.size();
4674 if (minSize > tuple2.size()) {
4675 minSize = tuple2.size();
4679 for (int i = 0; i < minSize; i++) {
4680 if (!tuple1.get(i).equals(tuple2.get(i))) {
4690 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
4691 NTuple<Location> tuple) {
4693 // first, retrieve inferred location by the local var descriptor
4694 CompositeLocation inferLoc = new CompositeLocation();
4696 CompositeLocation localVarInferLoc =
4697 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
4699 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
4701 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
4702 inferLoc.addLocation(localVarInferLoc.get(i));
4705 for (int i = 1; i < tuple.size(); i++) {
4706 Location cur = tuple.get(i);
4707 Descriptor enclosingDesc = cur.getDescriptor();
4708 Descriptor curDesc = cur.getLocDescriptor();
4710 Location inferLocElement;
4711 if (curDesc == null) {
4712 // in this case, we have a newly generated location.
4713 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
4715 String fieldLocSymbol =
4716 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
4717 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
4718 inferLocElement.setLocDescriptor(curDesc);
4721 inferLoc.addLocation(inferLocElement);
4725 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
4729 public LocationInfo getLocationInfo(Descriptor d) {
4730 if (d instanceof MethodDescriptor) {
4731 return getMethodLocationInfo((MethodDescriptor) d);
4733 return getFieldLocationInfo((ClassDescriptor) d);
4737 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
4739 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
4740 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
4743 return mapMethodDescToMethodLocationInfo.get(md);
4747 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
4749 if (!mapClassToLocationInfo.containsKey(cd)) {
4750 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
4753 return mapClassToLocationInfo.get(cd);
4757 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
4758 NTuple<Location> prefix, NTuple<Location> element) {
4760 if (!map.containsKey(prefix)) {
4761 map.put(prefix, new HashSet<NTuple<Location>>());
4763 map.get(prefix).add(element);
4766 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
4767 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
4768 Descriptor desc = (Descriptor) iterator.next();
4770 if (desc.equals(LocationInference.GLOBALDESC)) {
4772 } else if (desc instanceof VarDescriptor) {
4773 if (!((VarDescriptor) desc).getType().isPrimitive()) {
4776 } else if (desc instanceof FieldDescriptor) {
4777 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
4786 public SSJavaLattice<String> getLattice(Descriptor d) {
4787 if (d instanceof MethodDescriptor) {
4788 return getMethodLattice((MethodDescriptor) d);
4790 return getFieldLattice((ClassDescriptor) d);
4794 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
4795 if (!md2lattice.containsKey(md)) {
4796 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
4798 return md2lattice.get(md);
4801 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
4802 md2lattice.put(md, lattice);
4805 private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
4808 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
4809 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
4811 if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
4812 && dstCurTuple.size() > (idx + 1)) {
4813 // value flow between fields: we don't need to add a binary relation
4816 Descriptor desc = srcCurTuple.get(idx);
4817 ClassDescriptor classDesc;
4820 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
4822 if (desc instanceof FieldDescriptor) {
4823 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
4825 // this case is that the local variable has a composite location assignment
4826 // the following element after the composite location to the local variable
4827 // has the enclosing descriptor of the local variable
4828 Descriptor localDesc = srcNode.getDescTuple().get(0);
4829 classDesc = ((VarDescriptor) localDesc).getType().getClassDesc();
4832 extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
4836 Descriptor srcFieldDesc = srcCurTuple.get(idx);
4837 Descriptor dstFieldDesc = dstCurTuple.get(idx);
4839 // System.out.println("srcFieldDesc=" + srcFieldDesc + " dstFieldDesc=" + dstFieldDesc
4840 // + " idx=" + idx);
4841 if (!srcFieldDesc.equals(dstFieldDesc)) {
4843 // System.out.println("-ADD EDGE");
4844 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
4845 } else if (!isReference(srcFieldDesc) && !isReference(dstFieldDesc)) {
4846 // System.out.println("-ADD EDGE");
4847 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
4854 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
4855 if (!cd2lattice.containsKey(cd)) {
4856 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
4858 return cd2lattice.get(cd);
4861 public LinkedList<MethodDescriptor> computeMethodList() {
4863 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
4867 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
4868 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
4870 while (!toAnalyzeIsEmpty()) {
4871 ClassDescriptor cd = toAnalyzeNext();
4873 if (cd.getClassName().equals("Object")) {
4874 rootClassDescriptor = cd;
4875 // inheritanceTree = new InheritanceTree<ClassDescriptor>(cd);
4878 setupToAnalazeMethod(cd);
4879 temp_toanalyzeMethodList.removeAll(visited);
4881 while (!toAnalyzeMethodIsEmpty()) {
4882 MethodDescriptor md = toAnalyzeMethodNext();
4883 if ((!visited.contains(md))
4884 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
4886 // creates a mapping from a method descriptor to virtual methods
4887 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
4888 if (md.isStatic()) {
4889 setPossibleCallees.add(md);
4891 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
4894 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
4895 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
4897 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
4898 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
4899 if ((!ssjava.isTrustMethod(calleemd))
4900 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))
4901 && (!calleemd.getModifiers().isNative())) {
4902 if (!visited.contains(calleemd)) {
4903 temp_toanalyzeMethodList.add(calleemd);
4905 reachableCallee.add(calleemd);
4906 needToAnalyzeCalleeSet.add(calleemd);
4910 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
4919 return ssjava.topologicalSort(toSort);
4923 public boolean isTransitivelyCalledFrom(MethodDescriptor callee, MethodDescriptor caller) {
4924 // if the callee is transitively invoked from the caller
4927 int callerIdx = toanalyze_methodDescList.indexOf(caller);
4928 int calleeIdx = toanalyze_methodDescList.indexOf(callee);
4930 if (callerIdx < calleeIdx) {
4938 public void constructFlowGraph() {
4940 System.out.println("");
4941 toanalyze_methodDescList = computeMethodList();
4943 // hack... it seems that there is a problem with topological sorting.
4944 // so String.toString(Object o) is appeared too higher in the call chain.
4945 MethodDescriptor mdToString1 = null;
4946 MethodDescriptor mdToString2 = null;
4947 for (Iterator iterator = toanalyze_methodDescList.iterator(); iterator.hasNext();) {
4948 MethodDescriptor md = (MethodDescriptor) iterator.next();
4949 if (md.toString().equals("public static String String.valueOf(Object o)")) {
4952 if (md.toString().equals("public String Object.toString()")) {
4957 if (mdToString1 != null) {
4958 toanalyze_methodDescList.remove(mdToString1);
4959 toanalyze_methodDescList.addLast(mdToString1);
4961 if (mdToString2 != null) {
4962 toanalyze_methodDescList.remove(mdToString2);
4963 toanalyze_methodDescList.addLast(mdToString2);
4966 LinkedList<MethodDescriptor> methodDescList =
4967 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
4969 // System.out.println("@@@methodDescList=" + methodDescList);
4972 while (!methodDescList.isEmpty()) {
4973 MethodDescriptor md = methodDescList.removeLast();
4974 if (state.SSJAVADEBUG) {
4975 System.out.println();
4976 System.out.println("SSJAVA: Constructing a flow graph: " + md);
4978 // creates a mapping from a parameter descriptor to its index
4979 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
4981 if (!md.isStatic()) {
4983 mapParamDescToIdx.put(md.getThis(), 0);
4986 for (int i = 0; i < md.numParameters(); i++) {
4987 Descriptor paramDesc = (Descriptor) md.getParameter(i);
4988 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
4991 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
4992 mapMethodDescriptorToFlowGraph.put(md, fg);
4994 analyzeMethodBody(md.getClassDesc(), md);
4996 // System.out.println("##constructSubGlobalFlowGraph");
4997 // GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(fg);
4998 // mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
5001 // System.out.println("##addValueFlowsFromCalleeSubGlobalFlowGraph");
5002 // addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
5003 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
5005 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
5008 // _debug_printGraph();
5012 private void constructGlobalFlowGraph() {
5013 LinkedList<MethodDescriptor> methodDescList =
5014 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
5016 while (!methodDescList.isEmpty()) {
5017 MethodDescriptor md = methodDescList.removeLast();
5018 if (state.SSJAVADEBUG) {
5019 System.out.println();
5020 System.out.println("SSJAVA: Constructing a sub global flow graph: " + md);
5022 constructSubGlobalFlowGraph(getFlowGraph(md));
5025 // System.out.println("-add Value Flows From CalleeSubGlobalFlowGraph");
5026 addValueFlowsFromCalleeSubGlobalFlowGraph(md);
5027 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
5029 // System.out.println("-propagate Flows From Callees With No CompositeLocation");
5030 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
5032 // mark if a parameter has incoming flows
5033 checkParamNodesInSubGlobalFlowGraph(md);
5039 private void checkParamNodesInSubGlobalFlowGraph(MethodDescriptor md) {
5040 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5041 FlowGraph flowGraph = getFlowGraph(md);
5043 Set<FlowNode> paramFlowNodeSet = flowGraph.getParamFlowNodeSet();
5044 for (Iterator iterator = paramFlowNodeSet.iterator(); iterator.hasNext();) {
5045 FlowNode paramFlowNode = (FlowNode) iterator.next();
5046 // System.out.println("paramFlowNode=" + paramFlowNode);
5047 NTuple<Descriptor> paramDescTuple = paramFlowNode.getDescTuple();
5048 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
5049 GlobalFlowNode paramGlobalNode = globalFlowGraph.getFlowNode(paramLocTuple);
5051 Set<GlobalFlowNode> incomingNodeSet =
5052 globalFlowGraph.getIncomingNodeSetByPrefix(paramLocTuple.get(0));
5054 if (incomingNodeSet.size() > 0) {
5055 paramGlobalNode.setParamNodeWithIncomingFlows(true);
5061 private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
5062 if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
5063 mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
5065 return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
5068 private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
5070 // the transformation for a call site propagates flows through parameters
5071 // if the method is virtual, it also grab all relations from any possible
5074 Set<MethodInvokeNode> setMethodInvokeNode =
5075 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
5077 if (setMethodInvokeNode != null) {
5079 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
5080 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
5081 MethodDescriptor mdCallee = min.getMethod();
5082 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
5083 if (mdCallee.isStatic()) {
5084 setPossibleCallees.add(mdCallee);
5086 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
5087 // removes method descriptors that are not invoked by the caller
5088 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
5089 setPossibleCallees.addAll(calleeSet);
5092 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
5093 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
5094 propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
5102 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
5103 BlockNode bn = state.getMethodBody(md);
5104 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
5105 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
5108 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
5109 NodeTupleSet implicitFlowTupleSet) {
5111 bn.getVarTable().setParent(nametable);
5112 for (int i = 0; i < bn.size(); i++) {
5113 BlockStatementNode bsn = bn.get(i);
5114 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
5119 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
5120 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
5122 switch (bsn.kind()) {
5123 case Kind.BlockExpressionNode:
5124 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
5127 case Kind.DeclarationNode:
5128 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
5131 case Kind.IfStatementNode:
5132 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
5136 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
5139 case Kind.ReturnNode:
5140 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
5143 case Kind.SubBlockNode:
5144 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
5147 case Kind.ContinueBreakNode:
5150 case Kind.SwitchStatementNode:
5151 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
5158 private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
5159 SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
5161 analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
5165 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
5166 SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
5168 NodeTupleSet condTupleNode = new NodeTupleSet();
5169 analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
5170 implicitFlowTupleSet, false);
5172 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5174 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5175 newImplicitTupleSet.addTupleSet(condTupleNode);
5177 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5178 // need to create an intermediate node for the GLB of conditional
5179 // locations & implicit flows
5181 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5182 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5183 NTuple<Descriptor> tuple = idxIter.next();
5184 addFlowGraphEdge(md, tuple, interTuple);
5186 newImplicitTupleSet.clear();
5187 newImplicitTupleSet.addTuple(interTuple);
5190 BlockNode sbn = ssn.getSwitchBody();
5191 for (int i = 0; i < sbn.size(); i++) {
5192 analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
5197 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
5198 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
5199 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
5202 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
5203 NodeTupleSet implicitFlowTupleSet) {
5205 // System.out.println("-analyzeFlowReturnNode=" + rn.printNode(0));
5206 ExpressionNode returnExp = rn.getReturnExpression();
5208 if (returnExp != null) {
5209 NodeTupleSet nodeSet = new NodeTupleSet();
5210 // if a return expression returns a literal value, nodeSet is empty
5211 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
5212 FlowGraph fg = getFlowGraph(md);
5214 // if (implicitFlowTupleSet.size() == 1
5216 // fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate())
5219 // // since there is already an intermediate node for the GLB of implicit
5221 // // we don't need to create another intermediate node.
5222 // // just re-use the intermediate node for implicit flows.
5224 // FlowNode meetNode =
5225 // fg.getFlowNode(implicitFlowTupleSet.iterator().next());
5227 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
5228 // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>)
5230 // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
5235 NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
5237 // add tuples from return node
5238 currentFlowTupleSet.addTupleSet(nodeSet);
5240 // add tuples corresponding to the current implicit flows
5241 currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
5243 // System.out.println("---currentFlowTupleSet=" + currentFlowTupleSet);
5245 if (needToGenerateInterLoc(currentFlowTupleSet)) {
5247 FlowNode meetNode = fg.createIntermediateNode();
5248 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
5249 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
5250 fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
5252 fg.addReturnFlowNode(meetNode.getDescTuple());
5254 // currentFlowTupleSet = removeLiteralTuple(currentFlowTupleSet);
5255 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
5256 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
5257 fg.addReturnFlowNode(currentFlowTuple);
5265 private NodeTupleSet removeLiteralTuple(NodeTupleSet inSet) {
5266 NodeTupleSet tupleSet = new NodeTupleSet();
5267 for (Iterator<NTuple<Descriptor>> iter = inSet.iterator(); iter.hasNext();) {
5268 NTuple<Descriptor> tuple = iter.next();
5269 if (!tuple.get(0).equals(LITERALDESC)) {
5270 tupleSet.addTuple(tuple);
5276 private boolean needToGenerateInterLoc(NodeTupleSet tupleSet) {
5278 for (Iterator<NTuple<Descriptor>> iter = tupleSet.iterator(); iter.hasNext();) {
5279 NTuple<Descriptor> descTuple = iter.next();
5280 if (!descTuple.get(0).equals(LITERALDESC)) {
5285 // System.out.println("needToGenerateInterLoc=" + tupleSet + " size=" + size);
5292 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
5293 NodeTupleSet implicitFlowTupleSet) {
5295 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
5297 NodeTupleSet condTupleNode = new NodeTupleSet();
5298 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
5299 implicitFlowTupleSet, false);
5301 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5303 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5304 newImplicitTupleSet.addTupleSet(condTupleNode);
5306 newImplicitTupleSet.addGlobalFlowTupleSet(implicitFlowTupleSet.getGlobalLocTupleSet());
5307 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
5309 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5310 // need to create an intermediate node for the GLB of conditional
5311 // locations & implicit flows
5313 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5314 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
5316 NTuple<Descriptor> tuple = idxIter.next();
5317 addFlowGraphEdge(md, tuple, interTuple);
5319 newImplicitTupleSet.clear();
5320 newImplicitTupleSet.addTuple(interTuple);
5325 // System.out.println("condTupleNode="+condTupleNode);
5326 // NTuple<Descriptor> interTuple =
5327 // getFlowGraph(md).createIntermediateNode().getDescTuple();
5329 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator();
5330 // idxIter.hasNext();) {
5331 // NTuple<Descriptor> tuple = idxIter.next();
5332 // addFlowGraphEdge(md, tuple, interTuple);
5335 // for (Iterator<NTuple<Descriptor>> idxIter =
5336 // implicitFlowTupleSet.iterator(); idxIter
5338 // NTuple<Descriptor> tuple = idxIter.next();
5339 // addFlowGraphEdge(md, tuple, interTuple);
5342 // NodeTupleSet newImplicitSet = new NodeTupleSet();
5343 // newImplicitSet.addTuple(interTuple);
5344 analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
5347 // condTupleNode.addTupleSet(implicitFlowTupleSet);
5349 // add edges from condNodeTupleSet to all nodes of conditional nodes
5350 // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
5353 // check 'for loop' case
5354 BlockNode bn = ln.getInitializer();
5355 bn.getVarTable().setParent(nametable);
5356 for (int i = 0; i < bn.size(); i++) {
5357 BlockStatementNode bsn = bn.get(i);
5358 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
5361 NodeTupleSet condTupleNode = new NodeTupleSet();
5362 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
5363 implicitFlowTupleSet, false);
5365 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5366 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5367 newImplicitTupleSet.addTupleSet(condTupleNode);
5369 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5370 // need to create an intermediate node for the GLB of conditional
5371 // locations & implicit flows
5373 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5374 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
5376 NTuple<Descriptor> tuple = idxIter.next();
5377 addFlowGraphEdge(md, tuple, interTuple);
5379 newImplicitTupleSet.clear();
5380 newImplicitTupleSet.addTuple(interTuple);
5385 // NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5387 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
5388 // NTuple<Descriptor> tuple = idxIter.next();
5389 // addFlowGraphEdge(md, tuple, interTuple);
5392 // for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
5394 // NTuple<Descriptor> tuple = idxIter.next();
5395 // addFlowGraphEdge(md, tuple, interTuple);
5398 // NodeTupleSet newImplicitSet = new NodeTupleSet();
5399 // newImplicitSet.addTuple(interTuple);
5400 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitTupleSet);
5401 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitTupleSet);
5404 // condTupleNode.addTupleSet(implicitFlowTupleSet);
5406 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
5408 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
5415 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
5416 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
5418 // System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
5420 NodeTupleSet condTupleNode = new NodeTupleSet();
5421 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
5422 implicitFlowTupleSet, false);
5424 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5426 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5427 newImplicitTupleSet.addTupleSet(condTupleNode);
5429 // System.out.println("$$$GGGcondTupleNode=" + condTupleNode.getGlobalLocTupleSet());
5430 // System.out.println("-condTupleNode=" + condTupleNode);
5431 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5432 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
5434 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5436 // need to create an intermediate node for the GLB of conditional locations & implicit flows
5437 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5438 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5439 NTuple<Descriptor> tuple = idxIter.next();
5440 addFlowGraphEdge(md, tuple, interTuple);
5442 newImplicitTupleSet.clear();
5443 newImplicitTupleSet.addTuple(interTuple);
5446 // GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5447 // for (Iterator<NTuple<Location>> iterator = condTupleNode.globalIterator();
5448 // iterator.hasNext();) {
5449 // NTuple<Location> calleeReturnLocTuple = iterator.next();
5450 // for (Iterator<NTuple<Descriptor>> iter2 = newImplicitTupleSet.iterator(); iter2.hasNext();) {
5451 // NTuple<Descriptor> callerImplicitTuple = iter2.next();
5452 // globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5453 // translateToLocTuple(md, callerImplicitTuple));
5456 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
5458 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
5460 if (isn.getFalseBlock() != null) {
5461 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
5466 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
5467 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
5469 VarDescriptor vd = dn.getVarDescriptor();
5470 mapDescToDefinitionLine.put(vd, dn.getNumLine());
5471 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
5473 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
5474 fn.setDeclarationNode();
5476 if (dn.getExpression() != null) {
5477 // System.out.println("-analyzeFlowDeclarationNode=" + dn.printNode(0));
5479 NodeTupleSet nodeSetRHS = new NodeTupleSet();
5480 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
5481 implicitFlowTupleSet, false);
5483 // creates edges from RHS to LHS
5484 NTuple<Descriptor> interTuple = null;
5485 if (needToGenerateInterLoc(nodeSetRHS)) {
5486 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5489 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
5490 NTuple<Descriptor> fromTuple = iter.next();
5491 // System.out.println("fromTuple=" + fromTuple + " interTuple=" + interTuple + " tupleLSH="
5493 addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
5496 // creates edges from implicitFlowTupleSet to LHS
5497 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
5498 NTuple<Descriptor> implicitTuple = iter.next();
5499 addFlowGraphEdge(md, implicitTuple, tupleLHS);
5502 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5503 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
5504 NTuple<Location> calleeReturnLocTuple = iterator.next();
5506 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, tupleLHS));
5509 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
5511 NTuple<Location> implicitGlobalTuple = iterator.next();
5513 globalFlowGraph.addValueFlowEdge(implicitGlobalTuple, translateToLocTuple(md, tupleLHS));
5516 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
5517 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5523 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
5524 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
5525 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
5529 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
5530 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
5531 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
5534 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
5535 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
5536 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
5538 // System.out.println("en=" + en.printNode(0) + " class=" + en.getClass());
5540 // note that expression node can create more than one flow node
5541 // nodeSet contains of flow nodes
5542 // base is always assigned to null except the case of a name node!
5543 NTuple<Descriptor> flowTuple;
5544 switch (en.kind()) {
5545 case Kind.AssignmentNode:
5546 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
5547 implicitFlowTupleSet);
5550 case Kind.FieldAccessNode:
5552 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
5553 implicitFlowTupleSet, isLHS);
5554 if (flowTuple != null) {
5555 nodeSet.addTuple(flowTuple);
5560 NodeTupleSet nameNodeSet = new NodeTupleSet();
5562 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
5563 if (flowTuple != null) {
5564 nodeSet.addTuple(flowTuple);
5569 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
5572 case Kind.CreateObjectNode:
5573 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en, nodeSet, implicitFlowTupleSet);
5576 case Kind.ArrayAccessNode:
5577 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
5580 case Kind.LiteralNode:
5581 analyzeFlowLiteralNode(md, nametable, (LiteralNode) en, nodeSet);
5584 case Kind.MethodInvokeNode:
5585 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
5586 implicitFlowTupleSet);
5589 case Kind.TertiaryNode:
5590 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
5594 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
5596 // case Kind.InstanceOfNode:
5597 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
5600 // case Kind.ArrayInitializerNode:
5601 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
5605 // case Kind.ClassTypeNode:
5606 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
5609 // case Kind.OffsetNode:
5610 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
5619 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
5620 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
5622 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
5623 implicitFlowTupleSet, false);
5627 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
5628 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
5630 // System.out.println("analyzeFlowTertiaryNode=" + tn.printNode(0));
5632 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
5633 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
5634 implicitFlowTupleSet, false);
5636 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
5637 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
5638 newImplicitTupleSet.addTupleSet(tertiaryTupleNode);
5640 // System.out.println("$$$GGGcondTupleNode=" + tertiaryTupleNode.getGlobalLocTupleSet());
5641 // System.out.println("-tertiaryTupleNode=" + tertiaryTupleNode);
5642 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5643 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
5645 if (needToGenerateInterLoc(newImplicitTupleSet)) {
5646 // need to create an intermediate node for the GLB of conditional locations & implicit flows
5647 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5648 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
5649 NTuple<Descriptor> tuple = idxIter.next();
5650 addFlowGraphEdge(md, tuple, interTuple);
5652 newImplicitTupleSet.clear();
5653 newImplicitTupleSet.addTuple(interTuple);
5656 newImplicitTupleSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
5658 // System.out.println("---------newImplicitTupleSet=" + newImplicitTupleSet);
5660 // add edges from tertiaryTupleNode to all nodes of conditional nodes
5661 // tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
5662 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
5663 newImplicitTupleSet, false);
5665 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
5666 newImplicitTupleSet, false);
5668 nodeSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
5669 nodeSet.addTupleSet(tertiaryTupleNode);
5671 // System.out.println("#tertiary node set=" + nodeSet);
5674 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
5675 MethodInvokeNode min) {
5676 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
5678 set = new HashSet<MethodInvokeNode>();
5679 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
5684 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
5686 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
5687 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
5689 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
5692 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
5694 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
5695 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
5698 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
5701 private Set<NTuple<Location>> getPCLocTupleSet(MethodInvokeNode min) {
5702 if (!mapMethodInvokeNodeToPCLocTupleSet.containsKey(min)) {
5703 mapMethodInvokeNodeToPCLocTupleSet.put(min, new HashSet<NTuple<Location>>());
5705 return mapMethodInvokeNodeToPCLocTupleSet.get(min);
5708 private void analyzeFlowMethodInvokeNode(MethodDescriptor mdCaller, SymbolTable nametable,
5709 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
5711 // System.out.println("analyzeFlowMethodInvokeNode=" + min.printNode(0));
5713 if (!toanalyze_methodDescList.contains(min.getMethod())) {
5717 addMapMethodDescToMethodInvokeNodeSet(min);
5719 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
5720 for (Iterator iterator = implicitFlowTupleSet.iterator(); iterator.hasNext();) {
5721 NTuple<Descriptor> pcDescTuple = (NTuple<Descriptor>) iterator.next();
5722 if (!pcDescTuple.get(0).equals(LITERALDESC)) {
5723 // here we don't need to add the literal value as a PC location
5724 pcLocTupleSet.add(translateToLocTuple(mdCaller, pcDescTuple));
5728 mapMethodInvokeNodeToArgIdxMap.put(min, new HashMap<Integer, NTuple<Descriptor>>());
5730 if (nodeSet == null) {
5731 nodeSet = new NodeTupleSet();
5734 MethodDescriptor mdCallee = min.getMethod();
5736 NameDescriptor baseName = min.getBaseName();
5737 boolean isSystemout = false;
5738 if (baseName != null) {
5739 isSystemout = baseName.getSymbol().equals("System.out");
5742 if (!ssjava.isSSJavaUtil(mdCallee.getClassDesc()) && !ssjava.isTrustMethod(mdCallee)
5745 addMapCallerMethodDescToMethodInvokeNodeSet(mdCaller, min);
5747 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
5748 // System.out.println("mdCallee=" + mdCallee + " calleeFlowGraph=" + calleeFlowGraph);
5749 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
5751 // System.out.println("---calleeReturnSet=" + calleeReturnSet);
5753 NodeTupleSet tupleSet = new NodeTupleSet();
5755 if (min.getExpression() != null) {
5757 NodeTupleSet baseNodeSet = new NodeTupleSet();
5758 analyzeFlowExpressionNode(mdCaller, nametable, min.getExpression(), baseNodeSet, null,
5759 implicitFlowTupleSet, false);
5760 // System.out.println("baseNodeSet=" + baseNodeSet);
5762 assert (baseNodeSet.size() == 1);
5763 NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
5764 if (baseTuple.get(0) instanceof InterDescriptor) {
5765 if (baseTuple.size() > 1) {
5768 FlowNode interNode = getFlowGraph(mdCaller).getFlowNode(baseTuple);
5769 baseTuple = translateBaseTuple(interNode, baseTuple);
5771 mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
5773 if (!min.getMethod().isStatic()) {
5774 addArgIdxMap(min, 0, baseTuple);
5776 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
5777 FlowNode returnNode = (FlowNode) iterator.next();
5778 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
5779 if (returnDescTuple.startsWith(mdCallee.getThis())) {
5780 // the location type of the return value is started with 'this'
5782 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
5784 if (inFlowTuple.get(0) instanceof InterDescriptor) {
5785 // min.getExpression()
5790 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
5791 // nodeSet.addTuple(inFlowTuple);
5792 // System.out.println("1CREATE A NEW TUPLE=" + inFlowTuple + " from="
5793 // + mdCallee.getThis());
5794 // tupleSet.addTuple(inFlowTuple);
5795 tupleSet.addTuple(baseTuple);
5798 // System.out.println("returnNode=" + returnNode);
5799 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
5800 // System.out.println("inFlowSet=" + inFlowSet + " from retrunNode=" + returnNode);
5801 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
5802 FlowNode inFlowNode = (FlowNode) iterator2.next();
5803 if (inFlowNode.getDescTuple().startsWith(mdCallee.getThis())) {
5804 // nodeSet.addTupleSet(baseNodeSet);
5805 // System.out.println("2CREATE A NEW TUPLE=" + baseNodeSet + " from="
5806 // + mdCallee.getThis());
5807 tupleSet.addTupleSet(baseNodeSet);
5815 // analyze parameter flows
5817 if (min.numArgs() > 0) {
5820 if (min.getMethod().isStatic()) {
5826 for (int i = 0; i < min.numArgs(); i++) {
5827 ExpressionNode en = min.getArg(i);
5828 int idx = i + offset;
5829 NodeTupleSet argTupleSet = new NodeTupleSet();
5830 analyzeFlowExpressionNode(mdCaller, nametable, en, argTupleSet, false);
5831 // if argument is liternal node, argTuple is set to NULL
5832 // System.out.println("---arg idx=" + idx + " argTupleSet=" + argTupleSet);
5833 NTuple<Descriptor> argTuple = generateArgTuple(mdCaller, argTupleSet);
5835 // if an argument is literal value,
5836 // we need to create an itermediate node so that we could assign a composite location to
5837 // that node if needed
5838 if (argTuple.size() > 0
5839 && (argTuple.get(0).equals(GLOBALDESC) || argTuple.get(0).equals(LITERALDESC))) {
5841 * System.out.println("***GLOBAL ARG TUPLE CASE=" + argTuple); System.out.println("8");
5843 * NTuple<Descriptor> interTuple =
5844 * getFlowGraph(mdCaller).createIntermediateNode().getDescTuple(); ((InterDescriptor)
5845 * interTuple.get(0)).setMethodArgIdxPair(min, idx); addFlowGraphEdge(mdCaller,
5846 * argTuple, interTuple); argTuple = interTuple; addArgIdxMap(min, idx, argTuple);
5847 * System.out.println("new min mapping i=" + idx + " ->" + argTuple);
5849 argTuple = new NTuple<Descriptor>();
5852 addArgIdxMap(min, idx, argTuple);
5854 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
5856 // check whether a param node in the callee graph has incoming flows
5857 // if it has incoming flows, the corresponding arg should be lower than the current PC
5858 // Descriptor prefix = paramNode.getDescTuple().get(0);
5859 // if (calleeFlowGraph.getIncomingNodeSetByPrefix(prefix).size() > 0) {
5860 // for (Iterator<NTuple<Descriptor>> iterator = implicitFlowTupleSet.iterator(); iterator
5862 // NTuple<Descriptor> pcTuple = iterator.next();
5863 // System.out.println("add edge pcTuple =" + pcTuple + " -> " + argTuple);
5864 // addFlowGraphEdge(md, pcTuple, argTuple);
5868 // System.out.println("paramNode=" + paramNode + " calleeReturnSet=" + calleeReturnSet);
5869 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
5870 || mdCallee.getModifiers().isNative()) {
5871 addParamNodeFlowingToReturnValue(mdCallee, paramNode);
5872 // nodeSet.addTupleSet(argTupleSet);
5873 // System.out.println("3CREATE A NEW TUPLE=" + argTupleSet + " from=" + paramNode);
5874 tupleSet.addTupleSet(argTupleSet);
5880 if (mdCallee.getReturnType() != null && !mdCallee.getReturnType().isVoid()) {
5881 FlowReturnNode returnHolderNode = getFlowGraph(mdCaller).createReturnNode(min);
5883 if (needToGenerateInterLoc(tupleSet)) {
5884 FlowGraph fg = getFlowGraph(mdCaller);
5885 FlowNode interNode = fg.createIntermediateNode();
5886 interNode.setFormHolder(true);
5888 NTuple<Descriptor> interTuple = interNode.getDescTuple();
5890 for (Iterator iterator = tupleSet.iterator(); iterator.hasNext();) {
5891 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5893 Set<NTuple<Descriptor>> addSet = new HashSet<NTuple<Descriptor>>();
5894 FlowNode node = fg.getFlowNode(tuple);
5895 if (node instanceof FlowReturnNode) {
5896 addSet.addAll(fg.getReturnTupleSet(((FlowReturnNode) node).getReturnTupleSet()));
5900 for (Iterator iterator2 = addSet.iterator(); iterator2.hasNext();) {
5901 NTuple<Descriptor> higher = (NTuple<Descriptor>) iterator2.next();
5902 addFlowGraphEdge(mdCaller, higher, interTuple);
5906 returnHolderNode.addTuple(interTuple);
5908 nodeSet.addTuple(interTuple);
5909 // System.out.println("ADD TUPLESET=" + interTuple + " to returnnode=" +
5910 // returnHolderNode);
5913 returnHolderNode.addTupleSet(tupleSet);
5914 // System.out.println("ADD TUPLESET=" + tupleSet + " to returnnode=" + returnHolderNode);
5916 // setNode.addTupleSet(tupleSet);
5917 // NodeTupleSet setFromReturnNode=new NodeTupleSet();
5918 // setFromReturnNode.addTuple(tuple);
5920 NodeTupleSet holderTupleSet =
5921 getNodeTupleSetFromReturnNode(getFlowGraph(mdCaller), returnHolderNode);
5923 // System.out.println("HOLDER TUPLE SET=" + holderTupleSet);
5924 nodeSet.addTupleSet(holderTupleSet);
5926 nodeSet.addTuple(returnHolderNode.getDescTuple());
5929 // propagateFlowsFromCallee(min, md, min.getMethod());
5931 // when generating the global flow graph,
5932 // we need to add ordering relations from the set of callee return loc tuple to LHS of the
5933 // caller assignment
5934 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
5935 FlowNode calleeReturnNode = (FlowNode) iterator.next();
5936 NTuple<Location> calleeReturnLocTuple =
5937 translateToLocTuple(mdCallee, calleeReturnNode.getDescTuple());
5938 // System.out.println("calleeReturnLocTuple=" + calleeReturnLocTuple);
5939 NTuple<Location> transaltedToCaller =
5940 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeReturnLocTuple);
5941 // System.out.println("translateToCallerLocTuple="
5942 // + translateToCallerLocTuple(min, mdCallee, mdCaller, calleeReturnLocTuple));
5943 if (transaltedToCaller.size() > 0) {
5944 nodeSet.addGlobalFlowTuple(translateToCallerLocTuple(min, mdCallee, mdCaller,
5945 calleeReturnLocTuple));
5949 // System.out.println("min nodeSet=" + nodeSet);
5955 private NodeTupleSet getNodeTupleSetFromReturnNode(FlowGraph fg, FlowReturnNode node) {
5956 NodeTupleSet nts = new NodeTupleSet();
5958 Set<NTuple<Descriptor>> returnSet = node.getReturnTupleSet();
5960 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
5961 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5962 FlowNode flowNode = fg.getFlowNode(tuple);
5963 if (flowNode instanceof FlowReturnNode) {
5964 returnSet.addAll(recurGetNode(fg, (FlowReturnNode) flowNode));
5966 returnSet.add(tuple);
5970 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
5971 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator.next();
5972 nts.addTuple(nTuple);
5979 private Set<NTuple<Descriptor>> recurGetNode(FlowGraph fg, FlowReturnNode rnode) {
5981 Set<NTuple<Descriptor>> tupleSet = new HashSet<NTuple<Descriptor>>();
5983 Set<NTuple<Descriptor>> returnSet = rnode.getReturnTupleSet();
5984 for (Iterator iterator = returnSet.iterator(); iterator.hasNext();) {
5985 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5986 FlowNode flowNode = fg.getFlowNode(tuple);
5987 if (flowNode instanceof FlowReturnNode) {
5988 tupleSet.addAll(recurGetNode(fg, (FlowReturnNode) flowNode));
5990 tupleSet.add(tuple);
5996 private NTuple<Descriptor> generateArgTuple(MethodDescriptor mdCaller, NodeTupleSet argTupleSet) {
6000 // if argTupleSet is empty, it comes from the top location
6001 if (argTupleSet.size() == 0) {
6002 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
6003 descTuple.add(LITERALDESC);
6007 Set<NTuple<Descriptor>> argTupleSetNonLiteral = new HashSet<NTuple<Descriptor>>();
6009 for (Iterator<NTuple<Descriptor>> iter = argTupleSet.iterator(); iter.hasNext();) {
6010 NTuple<Descriptor> descTuple = iter.next();
6011 if (!descTuple.get(0).equals(LITERALDESC)) {
6012 argTupleSetNonLiteral.add(descTuple);
6016 if (argTupleSetNonLiteral.size() > 1) {
6018 NTuple<Descriptor> interTuple =
6019 getFlowGraph(mdCaller).createIntermediateNode().getDescTuple();
6020 for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
6021 NTuple<Descriptor> tuple = idxIter.next();
6022 addFlowGraphEdge(mdCaller, tuple, interTuple);
6025 } else if (argTupleSetNonLiteral.size() == 1) {
6026 return argTupleSetNonLiteral.iterator().next();
6028 return argTupleSet.iterator().next();
6033 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
6034 // return true if inNode has in-flows to nodeSet
6036 if (nodeSet.contains(inNode)) {
6037 // in this case, the method directly returns a parameter variable.
6040 // Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
6041 Set<FlowNode> reachableSet = fg.getReachableSetFrom(inNode.getDescTuple());
6042 // System.out.println("inNode=" + inNode + " reachalbeSet=" + reachableSet);
6044 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
6045 FlowNode fn = (FlowNode) iterator.next();
6046 if (nodeSet.contains(fn)) {
6053 private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
6054 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
6057 private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
6061 Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
6062 if (mapIdxToTuple == null) {
6063 mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
6064 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
6066 mapIdxToTuple.put(new Integer(idx), argTuple);
6069 private void analyzeFlowLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en,
6070 NodeTupleSet nodeSet) {
6071 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
6072 tuple.add(LITERALDESC);
6073 nodeSet.addTuple(tuple);
6076 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
6077 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
6079 // System.out.println("analyzeFlowArrayAccessNode aan=" + aan.printNode(0));
6080 String currentArrayAccessNodeExpStr = aan.printNode(0);
6081 arrayAccessNodeStack.push(aan.printNode(0));
6083 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
6084 NTuple<Descriptor> base =
6085 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
6086 // System.out.println("-base=" + base);
6088 nodeSet.setMethodInvokeBaseDescTuple(base);
6089 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
6090 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
6092 arrayAccessNodeStack.pop();
6095 // need to create an edge from idx to array
6096 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
6097 NTuple<Descriptor> idxTuple = idxIter.next();
6098 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
6099 NTuple<Descriptor> arrTuple = arrIter.next();
6100 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
6104 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6105 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
6107 NTuple<Location> calleeReturnLocTuple = iterator.next();
6108 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
6109 NTuple<Descriptor> arrTuple = arrIter.next();
6111 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, arrTuple));
6115 nodeSet.addTupleSet(expNodeTupleSet);
6118 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
6120 nodeSetArrayAccessExp.addTupleSet(expNodeTupleSet);
6121 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
6123 if (arrayAccessNodeStack.isEmpty()
6124 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
6126 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
6127 FlowNode interNode = getFlowGraph(md).createIntermediateNode();
6128 NTuple<Descriptor> interTuple = interNode.getDescTuple();
6130 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter.hasNext();) {
6131 NTuple<Descriptor> higherTuple = iter.next();
6132 addFlowGraphEdge(md, higherTuple, interTuple);
6134 nodeSetArrayAccessExp.clear();
6135 nodeSetArrayAccessExp.addTuple(interTuple);
6136 FlowGraph fg = getFlowGraph(md);
6138 // System.out.println("base=" + base);
6140 fg.addMapInterLocNodeToEnclosingDescriptor(interTuple.get(0),
6141 getClassTypeDescriptor(base.get(base.size() - 1)));
6142 interNode.setBaseTuple(base);
6147 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
6148 nodeSet.addTupleSet(nodeSetArrayAccessExp);
6154 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
6155 CreateObjectNode en, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
6156 // System.out.println("#analyzeCreateObjectNode=" + en.printNode(0));
6157 int numArgs = en.numArgs();
6158 NodeTupleSet argSet = new NodeTupleSet();
6160 for (int i = 0; i < numArgs; i++) {
6161 analyzeFlowExpressionNode(md, nametable, en.getArg(i), argSet, null, implicitFlowTupleSet,
6165 // System.out.println("###argSet=" + argSet);
6166 nodeSet.addTupleSet(argSet);
6170 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
6171 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
6173 NodeTupleSet leftOpSet = new NodeTupleSet();
6174 NodeTupleSet rightOpSet = new NodeTupleSet();
6176 // System.out.println("analyzeFlowOpNode=" + on.printNode(0));
6179 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
6182 if (on.getRight() != null) {
6184 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
6185 implicitFlowTupleSet, false);
6188 Operation op = on.getOp();
6190 switch (op.getOp()) {
6192 case Operation.UNARYPLUS:
6193 case Operation.UNARYMINUS:
6194 case Operation.LOGIC_NOT:
6196 nodeSet.addTupleSet(leftOpSet);
6199 case Operation.LOGIC_OR:
6200 case Operation.LOGIC_AND:
6201 case Operation.COMP:
6202 case Operation.BIT_OR:
6203 case Operation.BIT_XOR:
6204 case Operation.BIT_AND:
6205 case Operation.ISAVAILABLE:
6206 case Operation.EQUAL:
6207 case Operation.NOTEQUAL:
6214 case Operation.MULT:
6217 case Operation.LEFTSHIFT:
6218 case Operation.RIGHTSHIFT:
6219 case Operation.URIGHTSHIFT:
6221 // there are two operands
6222 nodeSet.addTupleSet(leftOpSet);
6223 nodeSet.addTupleSet(rightOpSet);
6225 nodeSet.addGlobalFlowTupleSet(leftOpSet.getGlobalLocTupleSet());
6226 nodeSet.addGlobalFlowTupleSet(rightOpSet.getGlobalLocTupleSet());
6231 throw new Error(op.toString());
6236 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
6237 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
6240 base = new NTuple<Descriptor>();
6243 NameDescriptor nd = nn.getName();
6245 if (nd.getBase() != null) {
6247 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
6248 implicitFlowTupleSet, false);
6250 // base node has the top location
6254 String varname = nd.toString();
6255 if (varname.equals("this")) {
6257 base.add(md.getThis());
6261 Descriptor d = (Descriptor) nametable.get(varname);
6263 if (d instanceof VarDescriptor) {
6264 VarDescriptor vd = (VarDescriptor) d;
6266 } else if (d instanceof FieldDescriptor) {
6267 // the type of field descriptor has a location!
6268 FieldDescriptor fd = (FieldDescriptor) d;
6269 if (fd.isStatic()) {
6271 // if it is 'static final', no need to have flow node for the TOP
6275 // if 'static', assign the default GLOBAL LOCATION to the first
6276 // element of the tuple
6277 base.add(GLOBALDESC);
6280 // the location of field access starts from this, followed by field
6282 base.add(md.getThis());
6286 } else if (d == null) {
6287 // access static field
6288 base.add(GLOBALDESC);
6289 base.add(nn.getField());
6292 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
6294 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
6295 // String globalLocId = localLattice.getGlobalLoc();
6296 // if (globalLocId == null) {
6298 // Error("Method lattice does not define global variable location at "
6299 // + generateErrorMessage(md.getClassDesc(), nn));
6301 // loc.addLocation(new Location(md, globalLocId));
6303 // Location fieldLoc = (Location) fd.getType().getExtension();
6304 // loc.addLocation(fieldLoc);
6310 getFlowGraph(md).createNewFlowNode(base);
6316 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
6317 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
6318 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
6319 // System.out.println("analyzeFlowFieldAccessNode=" + fan.printNode(0));
6321 String currentArrayAccessNodeExpStr = null;
6322 ExpressionNode left = fan.getExpression();
6323 TypeDescriptor ltd = left.getType();
6324 FieldDescriptor fd = fan.getField();
6325 ArrayAccessNode aan = null;
6327 String varName = null;
6328 if (left.kind() == Kind.NameNode) {
6329 NameDescriptor nd = ((NameNode) left).getName();
6330 varName = nd.toString();
6333 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
6334 // using a class name directly or access using this
6335 if (fd.isStatic() && fd.isFinal()) {
6340 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
6342 boolean isArrayCase = false;
6343 if (left instanceof ArrayAccessNode) {
6346 aan = (ArrayAccessNode) left;
6348 currentArrayAccessNodeExpStr = aan.printNode(0);
6349 arrayAccessNodeStack.push(currentArrayAccessNodeExpStr);
6351 left = aan.getExpression();
6352 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
6353 implicitFlowTupleSet, isLHS);
6357 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
6360 // in this case, field is TOP location
6364 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
6366 if (!left.getType().isPrimitive()) {
6367 if (!fd.getSymbol().equals("length")) {
6368 // array.length access, just have the location of the array
6369 flowFieldTuple.add(fd);
6370 nodeSet.removeTuple(base);
6373 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
6376 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
6377 NTuple<Descriptor> idxTuple = idxIter.next();
6378 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
6381 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6382 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
6384 NTuple<Location> calleeReturnLocTuple = iterator.next();
6386 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6387 translateToLocTuple(md, flowFieldTuple));
6391 nodeSet.addTupleSet(idxNodeTupleSet);
6393 // if it is the array case and not the LHS case
6395 arrayAccessNodeStack.pop();
6397 if (arrayAccessNodeStack.isEmpty()
6398 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
6399 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
6401 nodeSetArrayAccessExp.addTuple(flowFieldTuple);
6402 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
6403 nodeSetArrayAccessExp.addTupleSet(nodeSet);
6405 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
6406 // System.out.println("nodeSetArrayAccessExp=" + nodeSetArrayAccessExp);
6407 // System.out.println("idxNodeTupleSet.getGlobalLocTupleSet()="
6408 // + idxNodeTupleSet.getGlobalLocTupleSet());
6410 NTuple<Descriptor> interTuple =
6411 getFlowGraph(md).createIntermediateNode().getDescTuple();
6413 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter
6415 NTuple<Descriptor> higherTuple = iter.next();
6416 addFlowGraphEdge(md, higherTuple, interTuple);
6419 FlowGraph fg = getFlowGraph(md);
6420 fg.addMapInterLocNodeToEnclosingDescriptor(interTuple.get(0),
6421 getClassTypeDescriptor(base.get(base.size() - 1)));
6424 flowFieldTuple = interTuple;
6426 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
6432 return flowFieldTuple;
6437 private void debug_printTreeNode(TreeNode tn) {
6439 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
6443 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
6444 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
6445 NodeTupleSet implicitFlowTupleSet) {
6447 NodeTupleSet nodeSetRHS = new NodeTupleSet();
6448 NodeTupleSet nodeSetLHS = new NodeTupleSet();
6450 boolean postinc = true;
6451 if (an.getOperation().getBaseOp() == null
6452 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
6453 .getBaseOp().getOp() != Operation.POSTDEC)) {
6456 // if LHS is array access node, need to capture value flows between an array
6457 // and its index value
6458 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
6462 // analyze value flows of rhs expression
6463 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
6466 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
6467 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
6468 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
6469 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
6470 // System.out.println("-");
6472 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
6473 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
6475 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
6476 NTuple<Descriptor> fromTuple = iter.next();
6477 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6478 NTuple<Descriptor> toTuple = iter2.next();
6479 addFlowGraphEdge(md, fromTuple, toTuple);
6484 // creates edges from RHS to LHS
6485 NTuple<Descriptor> interTuple = null;
6486 if (needToGenerateInterLoc(nodeSetRHS)) {
6487 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
6490 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
6491 NTuple<Descriptor> fromTuple = iter.next();
6492 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6493 NTuple<Descriptor> toTuple = iter2.next();
6494 addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
6498 // creates edges from implicitFlowTupleSet to LHS
6499 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
6500 NTuple<Descriptor> fromTuple = iter.next();
6501 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6502 NTuple<Descriptor> toTuple = iter2.next();
6503 addFlowGraphEdge(md, fromTuple, toTuple);
6507 // create global flow edges if the callee gives return value flows to the caller
6508 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6509 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
6510 NTuple<Location> calleeReturnLocTuple = iterator.next();
6511 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6512 NTuple<Descriptor> callerLHSTuple = iter2.next();
6513 // System.out.println("$$$ GLOBAL FLOW ADD=" + calleeReturnLocTuple + " -> "
6514 // + translateToLocTuple(md, callerLHSTuple));
6516 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6517 translateToLocTuple(md, callerLHSTuple));
6521 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
6523 NTuple<Location> calleeReturnLocTuple = iterator.next();
6524 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6525 NTuple<Descriptor> callerLHSTuple = iter2.next();
6527 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6528 translateToLocTuple(md, callerLHSTuple));
6529 // System.out.println("$$$ GLOBAL FLOW PCLOC ADD=" + calleeReturnLocTuple + " -> "
6530 // + translateToLocTuple(md, callerLHSTuple));
6537 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6538 NTuple<Descriptor> tuple = iter2.next();
6539 addFlowGraphEdge(md, tuple, tuple);
6542 // creates edges from implicitFlowTupleSet to LHS
6543 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
6544 NTuple<Descriptor> fromTuple = iter.next();
6545 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6546 NTuple<Descriptor> toTuple = iter2.next();
6547 addFlowGraphEdge(md, fromTuple, toTuple);
6551 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
6552 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
6554 NTuple<Location> calleeReturnLocTuple = iterator.next();
6555 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
6556 NTuple<Descriptor> callerLHSTuple = iter2.next();
6557 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
6558 translateToLocTuple(md, callerLHSTuple));
6559 // System.out.println("$$$ GLOBAL FLOW PC ADD=" + calleeReturnLocTuple + " -> "
6560 // + translateToLocTuple(md, callerLHSTuple));
6566 if (nodeSet != null) {
6567 nodeSet.addTupleSet(nodeSetLHS);
6568 nodeSet.addGlobalFlowTupleSet(nodeSetLHS.getGlobalLocTupleSet());
6572 public FlowGraph getFlowGraph(MethodDescriptor md) {
6573 return mapMethodDescriptorToFlowGraph.get(md);
6576 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
6577 NTuple<Descriptor> to) {
6578 FlowGraph graph = getFlowGraph(md);
6579 graph.addValueFlowEdge(from, to);
6583 private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
6584 NTuple<Descriptor> inter, NTuple<Descriptor> to) {
6586 FlowGraph graph = getFlowGraph(md);
6588 if (inter != null) {
6589 graph.addValueFlowEdge(from, inter);
6590 graph.addValueFlowEdge(inter, to);
6592 graph.addValueFlowEdge(from, to);
6597 public void writeInferredLatticeDotFile(ClassDescriptor cd, SSJavaLattice<String> locOrder,
6598 String nameSuffix) {
6599 // System.out.println("@cd=" + cd);
6600 // System.out.println("@sharedLoc=" + locOrder.getSharedLocSet());
6601 writeInferredLatticeDotFile(cd, null, locOrder, nameSuffix);
6604 public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
6605 SSJavaLattice<String> locOrder, String nameSuffix) {
6607 String fileName = "lattice_";
6610 cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
6612 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
6615 fileName += nameSuffix;
6617 System.out.println("***lattice=" + fileName + " setsize=" + locOrder.getKeySet().size());
6619 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
6621 Set<String> addedLocSet = new HashSet<String>();
6623 if (pairSet.size() > 0) {
6625 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
6627 bw.write("digraph " + fileName + " {\n");
6629 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
6630 // pair is in the form of <higher, lower>
6631 Pair<String, String> pair = (Pair<String, String>) iterator.next();
6633 String highLocId = pair.getFirst();
6634 String lowLocId = pair.getSecond();
6635 if (!addedLocSet.contains(highLocId)) {
6636 addedLocSet.add(highLocId);
6637 drawNode(bw, locOrder, highLocId);
6640 if (!addedLocSet.contains(lowLocId)) {
6641 addedLocSet.add(lowLocId);
6642 drawNode(bw, locOrder, lowLocId);
6645 bw.write(highLocId + " -> " + lowLocId + ";\n");
6650 } catch (IOException e) {
6651 e.printStackTrace();
6658 private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
6660 for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
6661 HNode merged = (HNode) iterator.next();
6662 if (merged.isMergeNode()) {
6663 str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
6665 str += " " + merged.getName();
6671 public void addNaiveLattice(Descriptor desc, SSJavaLattice<String> lattice) {
6672 desc2naiveLattice.put(desc, lattice);
6675 private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, String locName)
6676 throws IOException {
6679 if (lattice.isSharedLoc(locName)) {
6680 prettyStr = locName + "*";
6682 prettyStr = locName;
6684 // HNode node = graph.getHNode(locName);
6685 // if (node != null && node.isMergeNode()) {
6686 // Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
6687 // prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
6689 bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
6692 public void _debug_writeFlowGraph() {
6693 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
6695 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
6696 MethodDescriptor md = (MethodDescriptor) iterator.next();
6697 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
6698 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
6701 subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
6702 } catch (IOException e) {
6703 e.printStackTrace();
6711 class CyclicFlowException extends Exception {
6715 class InterDescriptor extends Descriptor {
6717 Pair<MethodInvokeNode, Integer> minArgIdxPair;
6719 public InterDescriptor(String name) {
6723 public void setMethodArgIdxPair(MethodInvokeNode min, int idx) {
6724 minArgIdxPair = new Pair<MethodInvokeNode, Integer>(min, new Integer(idx));
6727 public Pair<MethodInvokeNode, Integer> getMethodArgIdxPair() {
6728 return minArgIdxPair;