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;
30 import IR.VarDescriptor;
31 import IR.Tree.ArrayAccessNode;
32 import IR.Tree.AssignmentNode;
33 import IR.Tree.BlockExpressionNode;
34 import IR.Tree.BlockNode;
35 import IR.Tree.BlockStatementNode;
36 import IR.Tree.CastNode;
37 import IR.Tree.CreateObjectNode;
38 import IR.Tree.DeclarationNode;
39 import IR.Tree.ExpressionNode;
40 import IR.Tree.FieldAccessNode;
41 import IR.Tree.IfStatementNode;
43 import IR.Tree.LiteralNode;
44 import IR.Tree.LoopNode;
45 import IR.Tree.MethodInvokeNode;
46 import IR.Tree.NameNode;
47 import IR.Tree.OpNode;
48 import IR.Tree.ReturnNode;
49 import IR.Tree.SubBlockNode;
50 import IR.Tree.SwitchBlockNode;
51 import IR.Tree.SwitchStatementNode;
52 import IR.Tree.TertiaryNode;
53 import IR.Tree.TreeNode;
56 public class LocationInference {
59 SSJavaAnalysis ssjava;
61 List<ClassDescriptor> temp_toanalyzeList;
62 List<MethodDescriptor> temp_toanalyzeMethodList;
63 Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToFlowGraph;
65 LinkedList<MethodDescriptor> toanalyze_methodDescList;
67 // map a method descriptor to its set of parameter descriptors
68 Map<MethodDescriptor, Set<Descriptor>> mapMethodDescriptorToParamDescSet;
70 // keep current descriptors to visit in fixed-point interprocedural analysis,
71 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
73 // map a class descriptor to a field lattice
74 private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
76 // map a method descriptor to a method lattice
77 private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
79 // map a method/class descriptor to a hierarchy graph
80 private Map<Descriptor, HierarchyGraph> mapDescriptorToHierarchyGraph;
82 // map a method/class descriptor to a skeleton hierarchy graph
83 private Map<Descriptor, HierarchyGraph> mapDescriptorToSkeletonHierarchyGraph;
85 private Map<Descriptor, HierarchyGraph> mapDescriptorToSimpleHierarchyGraph;
87 // map a method/class descriptor to a skeleton hierarchy graph with combination nodes
88 private Map<Descriptor, HierarchyGraph> mapDescriptorToCombineSkeletonHierarchyGraph;
90 // map a descriptor to a simple lattice
91 private Map<Descriptor, SSJavaLattice<String>> mapDescriptorToSimpleLattice;
93 // map a method descriptor to the set of method invocation nodes which are
94 // invoked by the method descriptor
95 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
97 private Map<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>> mapMethodInvokeNodeToArgIdxMap;
99 private Map<MethodInvokeNode, NTuple<Descriptor>> mapMethodInvokeNodeToBaseTuple;
101 private Map<MethodInvokeNode, Set<NTuple<Location>>> mapMethodInvokeNodeToPCLocTupleSet;
103 private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
105 private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
107 private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
109 private Map<MethodDescriptor, Set<FlowNode>> mapMethodDescToParamNodeFlowsToReturnValue;
111 private Map<String, Vector<String>> mapFileNameToLineVector;
113 private Map<Descriptor, Integer> mapDescToDefinitionLine;
115 private Map<Descriptor, LocationSummary> mapDescToLocationSummary;
117 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescToMethodInvokeNodeSet;
119 // maps a method descriptor to a sub global flow graph that captures all value flows caused by the
120 // set of callees reachable from the method
121 private Map<MethodDescriptor, GlobalFlowGraph> mapMethodDescriptorToSubGlobalFlowGraph;
123 private Map<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>> mapMethodInvokeNodeToMapCallerArgToCalleeArg;
125 private Map<MethodDescriptor, Boolean> mapMethodDescriptorToCompositeReturnCase;
127 public static final String GLOBALLOC = "GLOBALLOC";
129 public static final String INTERLOC = "INTERLOC";
131 public static final String PCLOC = "_PCLOC_";
133 public static final String RLOC = "_RLOC_";
135 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
137 public static final Descriptor TOPDESC = new NameDescriptor(SSJavaAnalysis.TOP);
139 public static final Descriptor BOTTOMDESC = new NameDescriptor(SSJavaAnalysis.BOTTOM);
141 public static final Descriptor RETURNLOC = new NameDescriptor(RLOC);
143 public static final Descriptor LITERALDESC = new NameDescriptor("LITERAL");
145 public static final HNode TOPHNODE = new HNode(TOPDESC);
147 public static final HNode BOTTOMHNODE = new HNode(BOTTOMDESC);
149 public static String newline = System.getProperty("line.separator");
151 LocationInfo curMethodInfo;
153 private boolean hasChanges = false;
155 boolean debug = true;
157 public static int locSeed = 0;
159 private Stack<String> arrayAccessNodeStack;
161 public LocationInference(SSJavaAnalysis ssjava, State state) {
162 this.ssjava = ssjava;
164 this.temp_toanalyzeList = new ArrayList<ClassDescriptor>();
165 this.temp_toanalyzeMethodList = new ArrayList<MethodDescriptor>();
166 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
167 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
168 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
169 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
170 this.mapMethodDescriptorToMethodInvokeNodeSet =
171 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
172 this.mapMethodInvokeNodeToArgIdxMap =
173 new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
174 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
175 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
176 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
178 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
179 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
180 this.mapMethodDescToParamNodeFlowsToReturnValue =
181 new HashMap<MethodDescriptor, Set<FlowNode>>();
183 this.mapDescriptorToHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
184 this.mapMethodInvokeNodeToBaseTuple = new HashMap<MethodInvokeNode, NTuple<Descriptor>>();
186 this.mapDescriptorToSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
187 this.mapDescriptorToCombineSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
188 this.mapDescriptorToSimpleHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
190 this.mapDescriptorToSimpleLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
192 this.mapDescToLocationSummary = new HashMap<Descriptor, LocationSummary>();
194 this.mapMethodDescriptorToSubGlobalFlowGraph = new HashMap<MethodDescriptor, GlobalFlowGraph>();
196 this.mapMethodInvokeNodeToMapCallerArgToCalleeArg =
197 new HashMap<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>>();
199 this.mapMethodInvokeNodeToPCLocTupleSet =
200 new HashMap<MethodInvokeNode, Set<NTuple<Location>>>();
202 this.arrayAccessNodeStack = new Stack<String>();
204 this.mapMethodDescToMethodInvokeNodeSet =
205 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
207 this.mapMethodDescriptorToCompositeReturnCase = new HashMap<MethodDescriptor, Boolean>();
211 public void setupToAnalyze() {
212 SymbolTable classtable = state.getClassSymbolTable();
213 temp_toanalyzeList.clear();
214 temp_toanalyzeList.addAll(classtable.getValueSet());
215 // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
216 // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
217 // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
222 public void setupToAnalazeMethod(ClassDescriptor cd) {
224 SymbolTable methodtable = cd.getMethodTable();
225 temp_toanalyzeMethodList.clear();
226 temp_toanalyzeMethodList.addAll(methodtable.getValueSet());
227 Collections.sort(temp_toanalyzeMethodList, new Comparator<MethodDescriptor>() {
228 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
229 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
234 public boolean toAnalyzeMethodIsEmpty() {
235 return temp_toanalyzeMethodList.isEmpty();
238 public boolean toAnalyzeIsEmpty() {
239 return temp_toanalyzeList.isEmpty();
242 public ClassDescriptor toAnalyzeNext() {
243 return temp_toanalyzeList.remove(0);
246 public MethodDescriptor toAnalyzeMethodNext() {
247 return temp_toanalyzeMethodList.remove(0);
250 public void inference() {
254 // construct value flow graph
255 constructFlowGraph();
257 constructGlobalFlowGraph();
261 assignCompositeLocation();
263 calculateExtraLocations();
264 addAdditionalOrderingConstraints();
266 _debug_writeFlowGraph();
270 constructHierarchyGraph();
272 debug_writeHierarchyDotFiles();
276 simplifyHierarchyGraph();
278 debug_writeSimpleHierarchyDotFiles();
280 constructSkeletonHierarchyGraph();
282 debug_writeSkeletonHierarchyDotFiles();
284 insertCombinationNodes();
286 debug_writeSkeletonCombinationHierarchyDotFiles();
290 debug_writeLattices();
292 updateCompositeLocationAssignments();
294 generateMethodSummary();
296 generateAnnoatedCode();
302 private void checkReturnNodes() {
303 LinkedList<MethodDescriptor> methodDescList =
304 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
306 while (!methodDescList.isEmpty()) {
307 MethodDescriptor md = methodDescList.removeLast();
309 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
310 checkFlowNodeReturnThisField(md);
312 // // in this case, this method will return the composite location that starts with 'this'
313 // FlowGraph flowGraph = getFlowGraph(md);
314 // Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
321 private void updateFlowGraph() {
323 LinkedList<MethodDescriptor> methodDescList =
324 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
326 while (!methodDescList.isEmpty()) {
327 MethodDescriptor md = methodDescList.removeLast();
328 if (state.SSJAVADEBUG) {
329 System.out.println();
330 System.out.println("SSJAVA: Updating a flow graph: " + md);
331 propagateFlowsFromCalleesWithNoCompositeLocation(md);
336 public Map<NTuple<Descriptor>, NTuple<Descriptor>> getMapCallerArgToCalleeParam(
337 MethodInvokeNode min) {
339 if (!mapMethodInvokeNodeToMapCallerArgToCalleeArg.containsKey(min)) {
340 mapMethodInvokeNodeToMapCallerArgToCalleeArg.put(min,
341 new HashMap<NTuple<Descriptor>, NTuple<Descriptor>>());
344 return mapMethodInvokeNodeToMapCallerArgToCalleeArg.get(min);
347 public void addMapCallerArgToCalleeParam(MethodInvokeNode min, NTuple<Descriptor> callerArg,
348 NTuple<Descriptor> calleeParam) {
349 getMapCallerArgToCalleeParam(min).put(callerArg, calleeParam);
352 private void assignCompositeLocation() {
353 calculateGlobalValueFlowCompositeLocation();
354 translateCompositeLocationAssignmentToFlowGraph();
357 private void translateCompositeLocationAssignmentToFlowGraph() {
358 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
359 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
360 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
363 private void translateCompositeLocationAssignmentToFlowGraph2() {
364 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
365 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
366 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
369 private void addAdditionalOrderingConstraints() {
370 System.out.println("\nSSJAVA: Add addtional ordering constriants:");
371 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
372 addAddtionalOrderingConstraints(methodEventLoopDesc);
375 private void updateCompositeLocationAssignments() {
377 LinkedList<MethodDescriptor> methodDescList =
378 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
380 while (!methodDescList.isEmpty()) {
381 MethodDescriptor md = methodDescList.removeLast();
383 System.out.println("\n#updateCompositeLocationAssignments=" + md);
385 FlowGraph flowGraph = getFlowGraph(md);
387 MethodSummary methodSummary = getMethodSummary(md);
389 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
390 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
391 FlowNode node = (FlowNode) iterator.next();
392 System.out.println("-node=" + node + " node.getDescTuple=" + node.getDescTuple());
393 if (node.getCompositeLocation() != null) {
394 CompositeLocation compLoc = node.getCompositeLocation();
395 CompositeLocation updatedCompLoc = updateCompositeLocation(compLoc);
396 node.setCompositeLocation(updatedCompLoc);
397 System.out.println("---updatedCompLoc1=" + updatedCompLoc);
399 NTuple<Descriptor> descTuple = node.getDescTuple();
400 System.out.println("update desc=" + descTuple);
401 CompositeLocation compLoc = convertToCompositeLocation(md, descTuple);
402 compLoc = updateCompositeLocation(compLoc);
403 node.setCompositeLocation(compLoc);
404 System.out.println("---updatedCompLoc2=" + compLoc);
407 if (node.isDeclaratonNode()) {
408 Descriptor localVarDesc = node.getDescTuple().get(0);
409 CompositeLocation compLoc = updateCompositeLocation(node.getCompositeLocation());
410 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, compLoc);
414 // update PCLOC and RETURNLOC if they have a composite location assignment
415 if (methodSummary.getRETURNLoc() != null) {
416 methodSummary.setRETURNLoc(updateCompositeLocation(methodSummary.getRETURNLoc()));
418 if (methodSummary.getPCLoc() != null) {
419 methodSummary.setPCLoc(updateCompositeLocation(methodSummary.getPCLoc()));
426 private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
427 CompositeLocation updatedCompLoc = new CompositeLocation();
428 for (int i = 0; i < compLoc.getSize(); i++) {
429 Location loc = compLoc.get(i);
430 String nodeIdentifier = loc.getLocIdentifier();
431 Descriptor enclosingDesc = loc.getDescriptor();
433 if (!enclosingDesc.equals(GLOBALDESC)) {
434 LocationSummary locSummary = getLocationSummary(enclosingDesc);
435 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(enclosingDesc);
436 if (scGraph != null) {
437 HNode curNode = scGraph.getCurrentHNode(nodeIdentifier);
438 if (curNode != null) {
439 nodeIdentifier = curNode.getName();
442 locName = locSummary.getLocationName(nodeIdentifier);
444 locName = nodeIdentifier;
446 Location updatedLoc = new Location(enclosingDesc, locName);
447 updatedCompLoc.addLocation(updatedLoc);
450 return updatedCompLoc;
453 private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
455 System.out.println("\n\n###translateCompositeLocationAssignmentToFlowGraph mdCaller="
458 // First, assign a composite location to a node in the flow graph
459 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
461 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
462 Map<Location, CompositeLocation> callerMapLocToCompLoc =
463 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
465 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
466 for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
467 Location methodLoc = (Location) iterator.next();
468 if (methodLoc.getDescriptor().equals(mdCaller)) {
469 CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
470 assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
474 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
476 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
477 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
478 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
479 // need to translate a composite location that is started with the base
481 translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
482 MethodDescriptor mdCallee = min.getMethod();
483 calleeSet.add(mdCallee);
487 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
488 MethodDescriptor callee = (MethodDescriptor) iterator.next();
489 translateCompositeLocationAssignmentToFlowGraph(callee);
494 private CompositeLocation translateArgCompLocToParamCompLoc(MethodInvokeNode min,
495 CompositeLocation argCompLoc) {
497 System.out.println("--------translateArgCompLocToParamCompLoc argCompLoc=" + argCompLoc);
498 MethodDescriptor mdCallee = min.getMethod();
499 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
501 NTuple<Location> argLocTuple = argCompLoc.getTuple();
502 Location argLocalLoc = argLocTuple.get(0);
504 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
505 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
506 for (Iterator iterator2 = idxSet.iterator(); iterator2.hasNext();) {
507 Integer idx = (Integer) iterator2.next();
509 if (idx == 0 && !min.getMethod().isStatic()) {
513 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
514 if (argTuple.size() > 0 && argTuple.get(0).equals(argLocalLoc.getLocDescriptor())) {
515 // it matches with the current argument composite location
516 // so what is the corresponding parameter local descriptor?
517 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
518 System.out.println("----------found paramNode=" + paramNode);
519 NTuple<Descriptor> paramDescTuple = paramNode.getCurrentDescTuple();
521 NTuple<Location> newParamTupleFromArgTuple = translateToLocTuple(mdCallee, paramDescTuple);
522 for (int i = 1; i < argLocTuple.size(); i++) {
523 newParamTupleFromArgTuple.add(argLocTuple.get(i));
526 System.out.println("-----------newParamTuple=" + newParamTupleFromArgTuple);
527 return new CompositeLocation(newParamTupleFromArgTuple);
534 private void addAddtionalOrderingConstraints(MethodDescriptor mdCaller) {
536 // First, assign a composite location to a node in the flow graph
537 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
539 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
540 Map<Location, CompositeLocation> callerMapLocToCompLoc =
541 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
542 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
544 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
546 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
547 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
548 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
549 MethodDescriptor mdCallee = min.getMethod();
550 calleeSet.add(mdCallee);
553 // add an additional ordering constraint
554 // if the first element of a parameter composite location matches 'this' reference,
555 // the corresponding argument in the caller is required to be higher than the translated
556 // parameter location in the caller lattice
558 // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
561 // update return flow nodes in the caller
562 CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc();
564 System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc);
565 if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis())
566 && returnLoc.getSize() > 1) {
567 System.out.println("###RETURN COMP LOC=" + returnLoc);
568 NTuple<Location> returnLocTuple = returnLoc.getTuple();
569 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
570 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
571 for (int i = 1; i < returnLocTuple.size(); i++) {
572 newReturnTuple.add(returnLocTuple.get(i).getLocDescriptor());
574 System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple);
575 callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple);
580 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
581 MethodDescriptor callee = (MethodDescriptor) iterator.next();
582 addAddtionalOrderingConstraints(callee);
587 private void addMapMethodDescToMethodInvokeNodeSet(MethodInvokeNode min) {
588 MethodDescriptor md = min.getMethod();
589 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
590 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
592 mapMethodDescToMethodInvokeNodeSet.get(md).add(min);
595 private Set<MethodInvokeNode> getMethodInvokeNodeSetByMethodDesc(MethodDescriptor md) {
596 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
597 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
599 return mapMethodDescToMethodInvokeNodeSet.get(md);
602 private void addOrderingConstraintFromCompLocParamToArg(MethodDescriptor mdCaller,
603 MethodInvokeNode min) {
604 System.out.println("-addOrderingConstraintFromCompLocParamToArg=" + min.printNode(0));
606 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(ssjava.getMethodContainingSSJavaLoop());
608 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
610 MethodDescriptor mdCallee = min.getMethod();
612 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
613 for (int idx = 0; idx < calleeFlowGraph.getNumParameters(); idx++) {
614 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
615 NTuple<Location> globalParamLocTuple =
616 translateToLocTuple(mdCallee, paramNode.getDescTuple());
617 translateToLocTuple(mdCallee, paramNode.getDescTuple());
618 CompositeLocation compLoc = paramNode.getCompositeLocation();
619 System.out.println("---paramNode=" + paramNode + " compLoc=" + compLoc);
620 if (compLoc != null) {
621 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
622 NTuple<Location> globalArgLocTuple = translateToLocTuple(mdCaller, argTuple);
624 if (!isLiteralValueLocTuple(globalArgLocTuple)
625 && !isLiteralValueLocTuple(globalParamLocTuple)) {
626 if (!globalGraph.hasValueFlowEdge(globalArgLocTuple, globalParamLocTuple)) {
627 System.out.println("----- add global flow globalArgLocTuple=" + globalArgLocTuple
628 + "-> globalParamLocTuple=" + globalParamLocTuple);
630 globalGraph.addValueFlowEdge(globalArgLocTuple, globalParamLocTuple);
634 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
635 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
637 if (!isLiteralValueLocTuple(pcLocTuple) && !isLiteralValueLocTuple(globalParamLocTuple)) {
638 if (!globalGraph.hasValueFlowEdge(pcLocTuple, globalParamLocTuple)) {
640 .println("----- add global flow PCLOC="
642 + "-> globalParamLocTu!globalArgLocTuple.get(0).getLocDescriptor().equals(LITERALDESC)ple="
643 + globalParamLocTuple);
645 globalGraph.addValueFlowEdge(pcLocTuple, globalParamLocTuple);
654 private boolean isLiteralValueLocTuple(NTuple<Location> locTuple) {
655 return locTuple.get(0).getLocDescriptor().equals(LITERALDESC);
658 public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
659 CompositeLocation inferCompLoc) {
660 Descriptor localDesc = loc.getLocDescriptor();
662 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
663 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
664 FlowNode node = (FlowNode) iterator.next();
665 if (node.getDescTuple().startsWith(localDesc)
666 && !node.getDescTuple().get(0).equals(LITERALDESC)) {
667 // need to assign the inferred composite location to this node
668 CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
669 node.setCompositeLocation(newCompLoc);
670 System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
675 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
676 CompositeLocation inferCompLoc) {
678 System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
681 CompositeLocation newCompLoc = new CompositeLocation();
682 for (int i = 0; i < inferCompLoc.getSize(); i++) {
683 newCompLoc.addLocation(inferCompLoc.get(i));
686 Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
687 Descriptor enclosingDescriptor;
688 if (lastDescOfPrefix instanceof InterDescriptor) {
689 enclosingDescriptor = null;
691 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
694 for (int i = 1; i < nodeDescTuple.size(); i++) {
695 Descriptor desc = nodeDescTuple.get(i);
696 Location locElement = new Location(enclosingDescriptor, desc);
697 newCompLoc.addLocation(locElement);
699 enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
705 private void translateMapLocationToInferCompositeLocationToCalleeGraph(
706 GlobalFlowGraph callerGraph, MethodInvokeNode min) {
708 MethodDescriptor mdCallee = min.getMethod();
709 MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
710 Map<Location, CompositeLocation> callerMapLocToCompLoc =
711 callerGraph.getMapLocationToInferCompositeLocation();
713 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
715 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
716 GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
718 NTuple<Location> baseLocTuple = null;
719 if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
720 baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
723 System.out.println("\n-#translate caller=" + mdCaller + " infer composite loc to callee="
724 + mdCallee + " baseLocTuple=" + baseLocTuple);
725 // System.out.println("-mapIdxToArgTuple=" + mapIdxToArgTuple);
726 // System.out.println("-callerMapLocToCompLoc=" + callerMapLocToCompLoc);
728 Set<Location> keySet = callerMapLocToCompLoc.keySet();
729 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
730 Location key = (Location) iterator.next();
731 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
733 if (!key.getDescriptor().equals(mdCaller)) {
735 CompositeLocation newCalleeCompLoc;
736 if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
737 // System.out.println("-----need to translate callerCompLoc=" + callerCompLoc
738 // + " with baseTuple=" + baseLocTuple);
740 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
742 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
743 System.out.println("1---key=" + key + " callerCompLoc=" + callerCompLoc
744 + " newCalleeCompLoc=" + newCalleeCompLoc);
745 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
746 if (!newCalleeCompLoc.get(0).getDescriptor().equals(mdCallee)) {
750 // System.out.println("-----baseLoctuple=" + baseLocTuple);
752 // check if it is the global access
753 Location compLocFirstElement = callerCompLoc.getTuple().get(0);
754 if (compLocFirstElement.getDescriptor().equals(mdCallee)
755 && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
757 newCalleeCompLoc = new CompositeLocation();
758 Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
760 newCalleeCompLoc.addLocation(newMethodLoc);
761 for (int i = 1; i < callerCompLoc.getSize(); i++) {
762 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
764 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
765 System.out.println("2---key=" + key + " callerCompLoc=" + callerCompLoc
766 + " newCalleeCompLoc=" + newCalleeCompLoc);
767 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
770 int paramIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
771 if (paramIdx == -1) {
772 // here, the first element of the current composite location comes from the current
774 // so transfer the same composite location to the callee
775 if (!calleeGlobalGraph.contrainsInferCompositeLocationMapKey(key)) {
776 if (callerCompLoc.get(0).getDescriptor().equals(mdCallee)) {
777 System.out.println("3---key=" + key + " callerCompLoc=" + callerCompLoc
778 + " newCalleeCompLoc=" + callerCompLoc);
779 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
780 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, callerCompLoc);
782 System.out.println("3---SKIP key=" + key + " callerCompLoc=" + callerCompLoc);
788 // It is the case where two parameters have relative orderings between them by having
789 // composite locations
790 // if we found the param idx, it means that the first part of the caller composite
791 // location corresponds to the one of arguments.
792 // for example, if the caller argument is <<caller.this>,<Decoder.br>>
793 // and the current caller composite location mapping
794 // <<caller.this>,<Decoder.br>,<Br.value>>
795 // and the parameter which matches with the caller argument is 'Br brParam'
796 // then, the translated callee composite location will be <<callee.brParam>,<Br.value>>
797 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(paramIdx);
799 FlowNode paramFlowNode = calleeFlowGraph.getParamFlowNode(paramIdx);
800 NTuple<Location> paramLocTuple =
801 translateToLocTuple(mdCallee, paramFlowNode.getDescTuple());
802 newCalleeCompLoc = new CompositeLocation();
803 for (int i = 0; i < paramLocTuple.size(); i++) {
804 newCalleeCompLoc.addLocation(paramLocTuple.get(i));
806 for (int i = argTuple.size(); i < callerCompLoc.getSize(); i++) {
807 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
809 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
810 System.out.println("4---key=" + key + " callerCompLoc=" + callerCompLoc
811 + " newCalleeCompLoc=" + newCalleeCompLoc);
812 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
814 // System.out.println("-----argTuple=" + argTuple + " caller=" + mdCaller +
817 // System.out.println("-----paramIdx=" + paramIdx + " paramFlowNode=" + paramFlowNode);
826 // System.out.println("-----*AFTER TRANSLATING COMP LOC MAPPING, CALLEE MAPPING="
827 // + calleeGlobalGraph.getMapLocationToInferCompositeLocation());
829 System.out.println("#ASSIGN COMP LOC TO CALLEE PARAMS: callee=" + mdCallee + " caller="
831 // If the location of an argument has a composite location
832 // need to assign a proper composite location to the corresponding callee parameter
833 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
834 for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
835 Integer idx = (Integer) iterator.next();
837 if (idx == 0 && !min.getMethod().isStatic()) {
841 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
842 System.out.println("-argTuple=" + argTuple + " idx=" + idx);
843 if (argTuple.size() > 0) {
844 // check if an arg tuple has been already assigned to a composite location
845 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
846 Location argLocalLoc = argLocTuple.get(0);
848 // if (!isPrimitiveType(argTuple)) {
849 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
851 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
852 for (int i = 1; i < argLocTuple.size(); i++) {
853 callerCompLoc.addLocation(argLocTuple.get(i));
856 System.out.println("---callerCompLoc=" + callerCompLoc);
858 // if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
860 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
862 NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
863 NTuple<Location> calleeParamLocTuple =
864 translateToLocTuple(mdCallee, calleeParamDescTuple);
866 int refParamIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
867 System.out.println("-----paramIdx=" + refParamIdx);
868 if (refParamIdx == 0 && !mdCallee.isStatic()) {
870 System.out.println("-------need to translate callerCompLoc=" + callerCompLoc
871 + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
872 + calleeParamLocTuple);
874 CompositeLocation newCalleeCompLoc =
875 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
877 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
880 System.out.println("---------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
881 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
883 } else if (refParamIdx != -1) {
884 // the first element of an argument composite location matches with one of paramtere
885 // composite locations
887 System.out.println("-------param match case=");
889 NTuple<Descriptor> argTupleRef = mapIdxToArgTuple.get(refParamIdx);
890 FlowNode refParamFlowNode = calleeFlowGraph.getParamFlowNode(refParamIdx);
891 NTuple<Location> refParamLocTuple =
892 translateToLocTuple(mdCallee, refParamFlowNode.getDescTuple());
894 System.out.println("---------refParamLocTuple=" + refParamLocTuple
895 + " from argTupleRef=" + argTupleRef);
897 CompositeLocation newCalleeCompLoc = new CompositeLocation();
898 for (int i = 0; i < refParamLocTuple.size(); i++) {
899 newCalleeCompLoc.addLocation(refParamLocTuple.get(i));
901 for (int i = argTupleRef.size(); i < callerCompLoc.getSize(); i++) {
902 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
905 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
908 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
909 System.out.println("-----------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
910 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
913 CompositeLocation newCalleeCompLoc =
914 calculateCompositeLocationFromSubGlobalGraph(mdCallee, calleeParamFlowNode);
915 if (newCalleeCompLoc != null) {
916 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
918 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
922 System.out.println("-----------------calleeParamFlowNode="
923 + calleeParamFlowNode.getCompositeLocation());
934 private CompositeLocation calculateCompositeLocationFromSubGlobalGraph(MethodDescriptor md,
935 FlowNode paramNode) {
937 System.out.println("#############################################################");
938 System.out.println("calculateCompositeLocationFromSubGlobalGraph=" + paramNode);
940 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
941 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
942 GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
944 List<NTuple<Location>> prefixList = calculatePrefixList(subGlobalFlowGraph, paramGlobalNode);
946 Location prefixLoc = paramLocTuple.get(0);
948 Set<GlobalFlowNode> reachableNodeSet =
949 subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
950 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
952 // System.out.println("node=" + node + " prefixList=" + prefixList);
954 for (int i = 0; i < prefixList.size(); i++) {
955 NTuple<Location> curPrefix = prefixList.get(i);
956 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
958 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
959 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
960 if (reachNode.getLocTuple().startsWith(curPrefix)) {
961 reachableCommonPrefixSet.add(reachNode.getLocTuple());
964 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
966 if (!reachableCommonPrefixSet.isEmpty()) {
968 MethodDescriptor curPrefixFirstElementMethodDesc =
969 (MethodDescriptor) curPrefix.get(0).getDescriptor();
971 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
972 (MethodDescriptor) prefixLoc.getDescriptor();
974 // System.out.println("curPrefixFirstElementMethodDesc=" +
975 // curPrefixFirstElementMethodDesc);
976 // System.out.println("nodePrefixLocFirstElementMethodDesc="
977 // + nodePrefixLocFirstElementMethodDesc);
979 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
980 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
981 curPrefixFirstElementMethodDesc)) {
984 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
986 Location curPrefixLocalLoc = curPrefix.get(0);
987 if (subGlobalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
988 // in this case, the local variable of the current prefix has already got a composite
990 // so we just ignore the current composite location.
992 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
993 // + " DUE TO " + curPrefix);
997 if (!needToGenerateCompositeLocation(paramGlobalNode, curPrefix)) {
998 System.out.println("NO NEED TO GENERATE COMP LOC to " + paramGlobalNode
999 + " with prefix=" + curPrefix);
1000 // System.out.println("prefixList=" + prefixList);
1001 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1005 Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
1006 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1007 System.out.println("NEED TO ASSIGN COMP LOC TO " + paramGlobalNode + " with prefix="
1009 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
1011 // makes sure that a newly generated location appears in the hierarchy graph
1012 for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
1013 Location curLoc = newCompLoc.get(compIdx);
1014 getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
1017 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1029 private int getParamIdx(CompositeLocation compLoc,
1030 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple) {
1032 // if the composite location is started with the argument descriptor
1033 // return the argument's index. o.t. return -1
1035 Set<Integer> keySet = mapIdxToArgTuple.keySet();
1036 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1037 Integer key = (Integer) iterator.next();
1038 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(key);
1039 if (argTuple.size() > 0 && translateToDescTuple(compLoc.getTuple()).startsWith(argTuple)) {
1040 System.out.println("compLoc.getTuple=" + compLoc + " is started with " + argTuple);
1041 return key.intValue();
1048 private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
1050 Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
1052 if (lastDesc instanceof FieldDescriptor) {
1053 return ((FieldDescriptor) lastDesc).getType().isPrimitive();
1054 } else if (lastDesc instanceof VarDescriptor) {
1055 return ((VarDescriptor) lastDesc).getType().isPrimitive();
1056 } else if (lastDesc instanceof InterDescriptor) {
1063 private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
1064 NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
1066 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1068 Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
1069 newCalleeCompLoc.addLocation(calleeThisLoc);
1071 // remove the base tuple from the caller
1072 // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
1073 // ,which is relative to the 'this' variable, <THIS,...>
1074 for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
1075 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1078 return newCalleeCompLoc;
1082 private void calculateGlobalValueFlowCompositeLocation() {
1084 System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
1085 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1086 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1088 Set<Location> calculatedPrefixSet = new HashSet<Location>();
1090 Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
1092 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1093 GlobalFlowNode node = (GlobalFlowNode) iterator.next();
1095 Location prefixLoc = node.getLocTuple().get(0);
1097 if (calculatedPrefixSet.contains(prefixLoc)) {
1098 // the prefix loc has been already assigned to a composite location
1102 calculatedPrefixSet.add(prefixLoc);
1104 // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
1105 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
1107 Set<GlobalFlowNode> reachableNodeSet =
1108 globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1109 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1111 // System.out.println("node=" + node + " prefixList=" + prefixList);
1112 System.out.println("---prefixList=" + prefixList);
1114 nextprefix: for (int i = 0; i < prefixList.size(); i++) {
1115 NTuple<Location> curPrefix = prefixList.get(i);
1116 System.out.println("---curPrefix=" + curPrefix);
1117 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1119 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1120 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1121 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1122 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1125 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1127 if (!reachableCommonPrefixSet.isEmpty()) {
1129 MethodDescriptor curPrefixFirstElementMethodDesc =
1130 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1132 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1133 (MethodDescriptor) prefixLoc.getDescriptor();
1135 // System.out.println("curPrefixFirstElementMethodDesc=" +
1136 // curPrefixFirstElementMethodDesc);
1137 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1138 // + nodePrefixLocFirstElementMethodDesc);
1140 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1141 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1142 curPrefixFirstElementMethodDesc)) {
1145 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1147 Location curPrefixLocalLoc = curPrefix.get(0);
1148 if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1149 // in this case, the local variable of the current prefix has already got a composite
1151 // so we just ignore the current composite location.
1153 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1154 // + " DUE TO " + curPrefix);
1159 if (!needToGenerateCompositeLocation(node, curPrefix)) {
1160 System.out.println("NO NEED TO GENERATE COMP LOC to " + node + " with prefix="
1162 // System.out.println("prefixList=" + prefixList);
1163 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1164 continue nextprefix;
1167 Location targetLocalLoc = node.getLocTuple().get(0);
1168 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1169 System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
1170 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc="
1172 globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1187 private boolean checkFlowNodeReturnThisField(MethodDescriptor md) {
1189 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1190 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1192 FlowGraph flowGraph = getFlowGraph(md);
1194 ClassDescriptor enclosingDesc = getClassTypeDescriptor(md.getThis());
1195 if (enclosingDesc == null) {
1200 Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
1201 Set<GlobalFlowNode> globalReturnNodeSet = new HashSet<GlobalFlowNode>();
1202 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1203 FlowNode flowNode = (FlowNode) iterator.next();
1204 NTuple<Location> locTuple = translateToLocTuple(md, flowNode.getDescTuple());
1205 GlobalFlowNode globalReturnNode = globalFlowGraph.getFlowNode(locTuple);
1206 globalReturnNodeSet.add(globalReturnNode);
1208 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, globalReturnNode);
1209 for (int i = 0; i < prefixList.size(); i++) {
1210 NTuple<Location> curPrefix = prefixList.get(i);
1211 ClassDescriptor cd =
1212 getClassTypeDescriptor(curPrefix.get(curPrefix.size() - 1).getLocDescriptor());
1213 if (cd != null && cd.equals(enclosingDesc)) {
1221 if (count == returnNodeSet.size()) {
1222 // in this case, all return nodes in the method returns values coming from a location that
1223 // starts with "this"
1225 System.out.println("$$$SET RETURN LOC TRUE=" + md);
1226 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.TRUE);
1228 // NameDescriptor returnLocDesc = new NameDescriptor("RLOC" + (locSeed++));
1229 // NTuple<Descriptor> rDescTuple = new NTuple<Descriptor>();
1230 // rDescTuple.add(md.getThis());
1231 // rDescTuple.add(returnLocDesc);
1233 // for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1234 // FlowNode rnode = (FlowNode) iterator.next();
1235 // flowGraph.addValueFlowEdge(rnode.getDescTuple(), rDescTuple);
1238 // getMethodSummary(md).setRETURNLoc(new CompositeLocation(translateToLocTuple(md,
1242 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.FALSE);
1245 return mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1249 private boolean needToGenerateCompositeLocation(GlobalFlowNode node, NTuple<Location> curPrefix) {
1250 // return true if there is a path between a node to which we want to give a composite location
1251 // and nodes which start with curPrefix
1253 System.out.println("---needToGenerateCompositeLocation curPrefix=" + curPrefix);
1255 Location targetLocalLoc = node.getLocTuple().get(0);
1257 MethodDescriptor md = (MethodDescriptor) targetLocalLoc.getDescriptor();
1258 FlowGraph flowGraph = getFlowGraph(md);
1259 FlowNode flowNode = flowGraph.getFlowNode(node.getDescTuple());
1260 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
1262 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
1263 for (Iterator iterator = paramNodeSet.iterator(); iterator.hasNext();) {
1264 FlowNode paramFlowNode = (FlowNode) iterator.next();
1265 if (curPrefix.startsWith(translateToLocTuple(md, paramFlowNode.getDescTuple()))) {
1266 System.out.println("here1?!");
1271 if (targetLocalLoc.getLocDescriptor() instanceof InterDescriptor) {
1272 Pair<MethodInvokeNode, Integer> pair =
1273 ((InterDescriptor) targetLocalLoc.getLocDescriptor()).getMethodArgIdxPair();
1276 System.out.println("$$$TARGETLOCALLOC HOLDER=" + targetLocalLoc);
1278 MethodInvokeNode min = pair.getFirst();
1279 Integer paramIdx = pair.getSecond();
1280 MethodDescriptor mdCallee = min.getMethod();
1282 FlowNode paramNode = getFlowGraph(mdCallee).getParamFlowNode(paramIdx);
1283 if (checkNodeReachToReturnNode(mdCallee, paramNode)) {
1284 System.out.println("here2?!");
1292 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1293 Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1295 if (!md.isStatic()) {
1296 ClassDescriptor currentMethodThisType = getClassTypeDescriptor(md.getThis());
1297 for (int i = 0; i < curPrefix.size(); i++) {
1298 ClassDescriptor prefixType = getClassTypeDescriptor(curPrefix.get(i).getLocDescriptor());
1299 if (prefixType != null && prefixType.equals(currentMethodThisType)) {
1300 System.out.println("PREFIX TYPE MATCHES WITH=" + currentMethodThisType);
1302 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1303 boolean hasCompReturnLocWithThis =
1304 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1305 if (hasCompReturnLocWithThis) {
1306 if (checkNodeReachToReturnNode(md, flowNode)) {
1307 System.out.println("here3?!");
1313 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1314 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1315 if (subGlobalReachalbeNode.getLocTuple().get(0).getLocDescriptor().equals(md.getThis())) {
1316 System.out.println("PREFIX FOUND=" + subGlobalReachalbeNode);
1317 System.out.println("here4?!");
1325 // System.out.println("flowGraph.getReturnNodeSet()=" + flowGraph.getReturnNodeSet());
1326 // System.out.println("flowGraph.contains(node.getDescTuple())="
1327 // + flowGraph.contains(node.getDescTuple()) + " flowGraph.getFlowNode(node.getDescTuple())="
1328 // + flowGraph.getFlowNode(node.getDescTuple()));reachableSet
1330 // if (flowGraph.contains(node.getDescTuple())
1331 // && flowGraph.getReturnNodeSet().contains(flowGraph.getFlowNode(node.getDescTuple()))) {
1332 // // return checkFlowNodeReturnThisField(flowGraph);
1335 Location lastLocationOfPrefix = curPrefix.get(curPrefix.size() - 1);
1336 // check whether prefix appears in the list of parameters
1337 Set<MethodInvokeNode> minSet = mapMethodDescToMethodInvokeNodeSet.get(md);
1338 found: for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1339 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1340 Map<Integer, NTuple<Descriptor>> map = mapMethodInvokeNodeToArgIdxMap.get(min);
1341 Set<Integer> keySet = map.keySet();
1342 // System.out.println("min=" + min.printNode(0));
1344 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1345 Integer argIdx = (Integer) iterator2.next();
1346 NTuple<Descriptor> argTuple = map.get(argIdx);
1348 if (!(!md.isStatic() && argIdx == 0)) {
1349 // if the argTuple is empty, we don't need to do with anything(LITERAL CASE).
1350 if (argTuple.size() > 0
1351 && argTuple.get(argTuple.size() - 1).equals(lastLocationOfPrefix.getLocDescriptor())) {
1352 NTuple<Location> locTuple =
1353 translateToLocTuple(md, flowGraph.getParamFlowNode(argIdx).getDescTuple());
1354 lastLocationOfPrefix = locTuple.get(0);
1355 System.out.println("ARG CASE=" + locTuple);
1356 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1357 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1358 // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
1359 NTuple<Location> globalReachlocTuple = subGlobalReachalbeNode.getLocTuple();
1360 for (int i = 0; i < globalReachlocTuple.size(); i++) {
1361 if (globalReachlocTuple.get(i).equals(lastLocationOfPrefix)) {
1362 System.out.println("ARG " + argTuple + " IS MATCHED WITH="
1363 + lastLocationOfPrefix);
1364 System.out.println("here5?!");
1375 // ClassDescriptor cd;
1376 // if (lastLocationOfPrefix.getLocDescriptor() instanceof VarDescriptor) {
1377 // cd = ((VarDescriptor) lastLocationOfPrefix.getLocDescriptor()).getType().getClassDesc();
1379 // // it is a field descriptor
1380 // cd = ((FieldDescriptor) lastLocationOfPrefix.getLocDescriptor()).getType().getClassDesc();
1383 // GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1384 // Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1386 // System.out.println("TRY TO FIND lastLocationOfPrefix=" + lastLocationOfPrefix);
1387 // for (Iterator iterator2 = subGlobalReachableSet.iterator(); iterator2.hasNext();) {
1388 // GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator2.next();
1389 // // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
1390 // NTuple<Location> locTuple = subGlobalReachalbeNode.getLocTuple();
1392 // for (int i = 0; i < locTuple.size(); i++) {
1393 // if (locTuple.get(i).equals(lastLocationOfPrefix)) {
1398 // Location lastLoc = locTuple.get(locTuple.size() - 1);
1399 // Descriptor enclosingDescriptor = lastLoc.getDescriptor();
1401 // if (enclosingDescriptor != null && enclosingDescriptor.equals(cd)) {
1402 // System.out.println("# WHY HERE?");
1403 // System.out.println("subGlobalReachalbeNode=" + subGlobalReachalbeNode);
1411 private boolean checkNodeReachToReturnNode(MethodDescriptor md, FlowNode node) {
1413 FlowGraph flowGraph = getFlowGraph(md);
1414 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(node);
1415 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1416 boolean hasCompReturnLocWithThis =
1417 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1419 if (hasCompReturnLocWithThis) {
1420 for (Iterator iterator = flowGraph.getReturnNodeSet().iterator(); iterator.hasNext();) {
1421 FlowNode returnFlowNode = (FlowNode) iterator.next();
1422 if (reachableSet.contains(returnFlowNode)) {
1431 private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
1432 CompositeLocation newCompLoc = compLocPrefix.clone();
1433 NTuple<Location> locTuple = node.getLocTuple();
1434 for (int i = 1; i < locTuple.size(); i++) {
1435 newCompLoc.addLocation(locTuple.get(i));
1437 node.setInferCompositeLocation(newCompLoc);
1440 private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
1442 System.out.println("\n##### calculatePrefixList node=" + node);
1444 Set<GlobalFlowNode> incomingNodeSetPrefix =
1445 graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
1446 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
1448 Set<GlobalFlowNode> reachableNodeSetPrefix =
1449 graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1450 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
1452 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1454 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
1455 GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
1456 NTuple<Location> inNodeTuple = inNode.getLocTuple();
1458 if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
1459 || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
1463 for (int i = 1; i < inNodeTuple.size(); i++) {
1464 NTuple<Location> prefix = inNodeTuple.subList(0, i);
1465 if (!prefixList.contains(prefix)) {
1466 prefixList.add(prefix);
1471 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1472 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1473 int s0 = arg0.size();
1474 int s1 = arg1.size();
1477 } else if (s0 == s1) {
1485 // remove a prefix which is not suitable for generating composite location
1486 Location localVarLoc = node.getLocTuple().get(0);
1487 MethodDescriptor md = (MethodDescriptor) localVarLoc.getDescriptor();
1488 ClassDescriptor cd = md.getClassDesc();
1491 Set<NTuple<Location>> toberemoved = new HashSet<NTuple<Location>>();
1492 // for (int i = 0; i < prefixList.size(); i++) {
1493 // NTuple<Location> prefixLocTuple = prefixList.get(i);
1494 // if (!containsClassDesc(cd, prefixLocTuple)) {
1495 // toberemoved.add(prefixLocTuple);
1499 // System.out.println("method class=" + cd + " toberemoved=" + toberemoved);
1501 prefixList.removeAll(toberemoved);
1507 private boolean containsClassDesc(ClassDescriptor cd, NTuple<Location> prefixLocTuple) {
1508 for (int i = 0; i < prefixLocTuple.size(); i++) {
1509 Location loc = prefixLocTuple.get(i);
1510 Descriptor locDesc = loc.getLocDescriptor();
1511 if (locDesc != null) {
1512 ClassDescriptor type = getClassTypeDescriptor(locDesc);
1513 if (type != null && type.equals(cd)) {
1521 private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
1523 MethodDescriptor md = flowGraph.getMethodDescriptor();
1525 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(md);
1527 // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1528 Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
1530 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
1532 FlowEdge edge = (FlowEdge) iterator.next();
1533 NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
1534 NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
1536 if (flowGraph.getFlowNode(srcDescTuple) instanceof FlowReturnNode
1537 || flowGraph.getFlowNode(dstDescTuple) instanceof FlowReturnNode) {
1541 // here only keep the first element(method location) of the descriptor
1543 NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
1544 // Location srcMethodLoc = srcLocTuple.get(0);
1545 // Descriptor srcVarDesc = srcMethodLoc.getLocDescriptor();
1546 // // if (flowGraph.isParamDesc(srcVarDesc) &&
1547 // (!srcVarDesc.equals(md.getThis()))) {
1548 // if (!srcVarDesc.equals(md.getThis())) {
1549 // srcLocTuple = new NTuple<Location>();
1550 // Location loc = new Location(md, srcVarDesc);
1551 // srcLocTuple.add(loc);
1554 NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
1555 // Location dstMethodLoc = dstLocTuple.get(0);
1556 // Descriptor dstVarDesc = dstMethodLoc.getLocDescriptor();
1557 // if (!dstVarDesc.equals(md.getThis())) {
1558 // dstLocTuple = new NTuple<Location>();
1559 // Location loc = new Location(md, dstVarDesc);
1560 // dstLocTuple.add(loc);
1563 globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
1570 private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
1572 NTuple<Location> locTuple = new NTuple<Location>();
1574 Descriptor enclosingDesc = md;
1575 // System.out.println("md=" + md + " descTuple=" + descTuple);
1576 for (int i = 0; i < descTuple.size(); i++) {
1577 Descriptor desc = descTuple.get(i);
1579 Location loc = new Location(enclosingDesc, desc);
1582 if (desc instanceof VarDescriptor) {
1583 enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
1584 } else if (desc instanceof FieldDescriptor) {
1585 enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1587 // TODO: inter descriptor case
1588 enclosingDesc = desc;
1597 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller) {
1599 // the transformation for a call site propagates flows through parameters
1600 // if the method is virtual, it also grab all relations from any possible
1603 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
1605 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
1606 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1607 MethodDescriptor mdCallee = min.getMethod();
1608 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1609 if (mdCallee.isStatic()) {
1610 setPossibleCallees.add(mdCallee);
1612 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
1613 // removes method descriptors that are not invoked by the caller
1614 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
1615 setPossibleCallees.addAll(calleeSet);
1618 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
1619 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
1620 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
1627 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
1628 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
1630 System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
1631 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
1632 Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
1634 System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
1635 + mapMethodInvokeNodeToArgIdxMap.get(min));
1637 Set<Integer> keySet = mapIdxToArg.keySet();
1638 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1639 Integer idx = (Integer) iterator.next();
1640 NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
1641 if (argDescTuple.size() > 0) {
1642 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1643 NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
1644 NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
1645 System.out.println("-------paramDescTuple=" + paramDescTuple + "->argDescTuple="
1647 addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
1651 // addValueFlowBetweenParametersToCaller(min, mdCaller, possibleMdCallee);
1653 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1654 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
1655 Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
1656 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
1657 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
1658 addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
1661 System.out.println("$$$GLOBAL PC LOC ADD=" + mdCaller);
1662 Set<NTuple<Location>> pcLocTupleSet = mapMethodInvokeNodeToPCLocTupleSet.get(min);
1663 System.out.println("---pcLocTupleSet=" + pcLocTupleSet);
1664 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
1665 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
1666 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
1667 if (calleeNode.isParamNodeWithIncomingFlows()) {
1668 System.out.println("calleeNode.getLocTuple()" + calleeNode.getLocTuple());
1669 NTuple<Location> callerSrcNodeLocTuple =
1670 translateToCallerLocTuple(min, possibleMdCallee, mdCaller, calleeNode.getLocTuple());
1671 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
1672 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
1673 for (Iterator iterator2 = pcLocTupleSet.iterator(); iterator2.hasNext();) {
1674 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator2.next();
1675 callerSubGlobalGraph.addValueFlowEdge(pcLocTuple, callerSrcNodeLocTuple);
1684 private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
1685 MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
1687 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1688 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
1690 // System.out.println("$addValueFlowFromCalleeNode calleeSrcNode=" + calleeSrcNode);
1692 NTuple<Location> callerSrcNodeLocTuple =
1693 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
1694 // System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
1696 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
1698 Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
1700 for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
1701 GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
1702 NTuple<Location> callerDstNodeLocTuple =
1703 translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
1704 // System.out.println("outNode=" + outNode + " callerDstNodeLocTuple="
1705 // + callerDstNodeLocTuple);
1706 if (callerDstNodeLocTuple != null) {
1707 callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
1714 private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
1715 MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
1716 // this method will return the same nodeLocTuple if the corresponding argument is literal
1719 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1721 NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
1722 if (calleeFlowGraph.isParameter(nodeDescTuple)) {
1723 int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
1724 NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
1726 // if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
1727 // // the type of argument is primitive.
1728 // return nodeLocTuple.clone();
1730 // System.out.println("paramIdx=" + paramIdx + " argDescTuple=" + argDescTuple);
1731 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1733 NTuple<Location> callerLocTuple = new NTuple<Location>();
1735 callerLocTuple.addAll(argLocTuple);
1736 for (int i = 1; i < nodeLocTuple.size(); i++) {
1737 callerLocTuple.add(nodeLocTuple.get(i));
1739 return callerLocTuple;
1741 return nodeLocTuple.clone();
1746 public static boolean isPrimitive(Descriptor desc) {
1748 if (desc instanceof FieldDescriptor) {
1749 return ((FieldDescriptor) desc).getType().isPrimitive();
1750 } else if (desc instanceof VarDescriptor) {
1751 return ((VarDescriptor) desc).getType().isPrimitive();
1752 } else if (desc instanceof InterDescriptor) {
1759 public static boolean isReference(Descriptor desc) {
1761 if (desc instanceof FieldDescriptor) {
1762 return ((FieldDescriptor) desc).getType().isPtr();
1763 } else if (desc instanceof VarDescriptor) {
1764 return ((VarDescriptor) desc).getType().isPtr();
1770 private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
1772 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
1773 for (int i = 0; i < locTuple.size(); i++) {
1774 descTuple.add(locTuple.get(i).getLocDescriptor());
1780 public LocationSummary getLocationSummary(Descriptor d) {
1781 if (!mapDescToLocationSummary.containsKey(d)) {
1782 if (d instanceof MethodDescriptor) {
1783 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
1784 } else if (d instanceof ClassDescriptor) {
1785 mapDescToLocationSummary.put(d, new FieldSummary());
1788 return mapDescToLocationSummary.get(d);
1791 private void generateMethodSummary() {
1793 Set<MethodDescriptor> keySet = md2lattice.keySet();
1794 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1795 MethodDescriptor md = (MethodDescriptor) iterator.next();
1797 System.out.println("\nSSJAVA: generate method summary: " + md);
1799 FlowGraph flowGraph = getFlowGraph(md);
1800 MethodSummary methodSummary = getMethodSummary(md);
1802 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
1804 // set the 'this' reference location
1805 if (!md.isStatic()) {
1806 System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
1807 methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
1810 // set the 'global' reference location if needed
1811 if (methodSummary.hasGlobalAccess()) {
1812 methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
1815 // construct a parameter mapping that maps a parameter descriptor to an
1816 // inferred composite location
1817 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
1818 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
1819 CompositeLocation inferredCompLoc =
1820 updateCompositeLocation(flowNode.getCompositeLocation());
1821 // NTuple<Descriptor> descTuple = flowNode.getDescTuple();
1823 // CompositeLocation assignedCompLoc = flowNode.getCompositeLocation();
1824 // CompositeLocation inferredCompLoc;
1825 // if (assignedCompLoc != null) {
1826 // inferredCompLoc = translateCompositeLocation(assignedCompLoc);
1828 // Descriptor locDesc = descTuple.get(0);
1829 // Location loc = new Location(md, locDesc.getSymbol());
1830 // loc.setLocDescriptor(locDesc);
1831 // inferredCompLoc = new CompositeLocation(loc);
1833 System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc + " original="
1834 + flowNode.getCompositeLocation());
1836 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
1837 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
1838 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
1845 private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
1847 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
1849 for (int idx = 0; idx < size; idx++) {
1850 Location loc1 = locTuple1.get(idx);
1851 Location loc2 = locTuple2.get(idx);
1853 Descriptor desc1 = loc1.getDescriptor();
1854 Descriptor desc2 = loc2.getDescriptor();
1856 if (!desc1.equals(desc2)) {
1857 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
1860 Descriptor locDesc1 = loc1.getLocDescriptor();
1861 Descriptor locDesc2 = loc2.getLocDescriptor();
1863 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
1865 HNode node1 = hierarchyGraph.getHNode(locDesc1);
1866 HNode node2 = hierarchyGraph.getHNode(locDesc2);
1868 System.out.println("---node1=" + node1 + " node2=" + node2);
1869 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
1870 + hierarchyGraph.getIncomingNodeSet(node2));
1872 if (locDesc1.equals(locDesc2)) {
1874 } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
1875 && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
1887 private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
1889 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
1891 for (int idx = 0; idx < size; idx++) {
1892 Location loc1 = locTuple1.get(idx);
1893 Location loc2 = locTuple2.get(idx);
1895 Descriptor desc1 = loc1.getDescriptor();
1896 Descriptor desc2 = loc2.getDescriptor();
1898 if (!desc1.equals(desc2)) {
1899 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
1902 Descriptor locDesc1 = loc1.getLocDescriptor();
1903 Descriptor locDesc2 = loc2.getLocDescriptor();
1905 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
1907 HNode node1 = hierarchyGraph.getHNode(locDesc1);
1908 HNode node2 = hierarchyGraph.getHNode(locDesc2);
1910 System.out.println("---node1=" + node1 + " node2=" + node2);
1911 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
1912 + hierarchyGraph.getIncomingNodeSet(node2));
1914 if (locDesc1.equals(locDesc2)) {
1916 } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
1927 private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
1928 CompositeLocation newCompLoc = new CompositeLocation();
1930 // System.out.println("compLoc=" + compLoc);
1931 for (int i = 0; i < compLoc.getSize(); i++) {
1932 Location loc = compLoc.get(i);
1933 Descriptor enclosingDescriptor = loc.getDescriptor();
1934 Descriptor locDescriptor = loc.getLocDescriptor();
1936 HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
1937 // System.out.println("-hnode=" + hnode + " from=" + locDescriptor +
1938 // " enclosingDescriptor="
1939 // + enclosingDescriptor);
1940 // System.out.println("-getLocationSummary(enclosingDescriptor)="
1941 // + getLocationSummary(enclosingDescriptor));
1942 String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
1943 // System.out.println("-locName=" + locName);
1944 Location newLoc = new Location(enclosingDescriptor, locName);
1945 newLoc.setLocDescriptor(locDescriptor);
1946 newCompLoc.addLocation(newLoc);
1952 private void debug_writeLattices() {
1954 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
1955 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1956 Descriptor key = (Descriptor) iterator.next();
1957 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
1958 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
1959 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
1960 if (key instanceof ClassDescriptor) {
1961 writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
1963 } else if (key instanceof MethodDescriptor) {
1964 MethodDescriptor md = (MethodDescriptor) key;
1965 writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
1969 LocationSummary ls = getLocationSummary(key);
1970 System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
1973 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
1974 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
1975 ClassDescriptor cd = (ClassDescriptor) iterator.next();
1976 writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
1977 cd2lattice.get(cd), "");
1980 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
1981 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
1982 MethodDescriptor md = (MethodDescriptor) iterator.next();
1983 writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
1984 md2lattice.get(md), "");
1989 private void buildLattice() {
1991 BuildLattice buildLattice = new BuildLattice(this);
1993 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
1994 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1995 Descriptor desc = (Descriptor) iterator.next();
1997 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
1999 addMapDescToSimpleLattice(desc, simpleLattice);
2001 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2002 System.out.println("\n## insertIntermediateNodesToStraightLine:"
2003 + simpleHierarchyGraph.getName());
2004 SSJavaLattice<String> lattice =
2005 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2006 lattice.removeRedundantEdges();
2008 if (desc instanceof ClassDescriptor) {
2010 cd2lattice.put((ClassDescriptor) desc, lattice);
2011 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2012 } else if (desc instanceof MethodDescriptor) {
2014 md2lattice.put((MethodDescriptor) desc, lattice);
2015 MethodDescriptor md = (MethodDescriptor) desc;
2016 ClassDescriptor cd = md.getClassDesc();
2017 // ssjava.writeLatticeDotFile(cd, md, lattice);
2020 // System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
2021 // HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2022 // HierarchyGraph skeletonGraphWithCombinationNode =
2023 // skeletonGraph.clone();
2024 // skeletonGraphWithCombinationNode.setName(desc + "_SC");
2026 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2027 // System.out.println("Identifying Combination Nodes:");
2028 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2029 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2030 // mapDescriptorToCombineSkeletonHierarchyGraph.put(desc,
2031 // skeletonGraphWithCombinationNode);
2036 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
2037 mapDescriptorToSimpleLattice.put(desc, lattice);
2040 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
2041 return mapDescriptorToSimpleLattice.get(desc);
2044 private void simplifyHierarchyGraph() {
2045 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2046 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2047 Descriptor desc = (Descriptor) iterator.next();
2048 // System.out.println("SSJAVA: remove redundant edges: " + desc);
2049 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
2050 simpleHierarchyGraph.setName(desc + "_SIMPLE");
2051 simpleHierarchyGraph.removeRedundantEdges();
2052 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
2056 private void insertCombinationNodes() {
2057 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
2058 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2059 Descriptor desc = (Descriptor) iterator.next();
2060 System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
2061 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2062 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
2063 skeletonGraphWithCombinationNode.setName(desc + "_SC");
2065 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2066 System.out.println("Identifying Combination Nodes:");
2067 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2068 skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2069 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
2073 private void constructSkeletonHierarchyGraph() {
2074 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2075 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2076 Descriptor desc = (Descriptor) iterator.next();
2077 System.out.println("SSJAVA: Constructing Skeleton Hierarchy Graph: " + desc);
2078 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
2079 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
2080 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
2081 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
2082 skeletonGraph.simplifyHierarchyGraph();
2083 // skeletonGraph.combineRedundantNodes(false);
2084 // skeletonGraph.removeRedundantEdges();
2085 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
2089 private void debug_writeHierarchyDotFiles() {
2091 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2092 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2093 Descriptor desc = (Descriptor) iterator.next();
2094 getHierarchyGraph(desc).writeGraph();
2099 private void debug_writeSimpleHierarchyDotFiles() {
2101 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2102 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2103 Descriptor desc = (Descriptor) iterator.next();
2104 getHierarchyGraph(desc).writeGraph();
2105 getSimpleHierarchyGraph(desc).writeGraph();
2110 private void debug_writeSkeletonHierarchyDotFiles() {
2112 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2113 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2114 Descriptor desc = (Descriptor) iterator.next();
2115 getSkeletonHierarchyGraph(desc).writeGraph();
2120 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
2122 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2123 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2124 Descriptor desc = (Descriptor) iterator.next();
2125 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
2130 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
2131 return mapDescriptorToSimpleHierarchyGraph.get(d);
2134 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
2135 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
2136 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2138 return mapDescriptorToSkeletonHierarchyGraph.get(d);
2141 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
2142 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
2143 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2145 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
2148 private void constructHierarchyGraph() {
2150 // do fixed-point analysis
2152 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2154 // Collections.sort(descriptorListToAnalyze, new
2155 // Comparator<MethodDescriptor>() {
2156 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
2157 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
2161 // current descriptors to visit in fixed-point interprocedural analysis,
2162 // prioritized by dependency in the call graph
2163 methodDescriptorsToVisitStack.clear();
2165 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2166 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2168 while (!descriptorListToAnalyze.isEmpty()) {
2169 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2170 methodDescriptorsToVisitStack.add(md);
2173 // analyze scheduled methods until there are no more to visit
2174 while (!methodDescriptorsToVisitStack.isEmpty()) {
2175 // start to analyze leaf node
2176 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2178 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
2179 // MethodSummary methodSummary = new MethodSummary(md);
2181 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
2182 // curMethodInfo = methodInfo;
2184 System.out.println();
2185 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
2187 constructHierarchyGraph(md, hierarchyGraph);
2189 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
2190 // MethodSummary prevMethodSummary = getMethodSummary(md);
2192 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
2194 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
2195 // mapDescToLocationSummary.put(md, methodSummary);
2197 // results for callee changed, so enqueue dependents caller for
2199 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
2200 while (depsItr.hasNext()) {
2201 MethodDescriptor methodNext = depsItr.next();
2202 if (!methodDescriptorsToVisitStack.contains(methodNext)
2203 && methodDescriptorToVistSet.contains(methodNext)) {
2204 methodDescriptorsToVisitStack.add(methodNext);
2213 while (!toAnalyzeIsEmpty()) {
2214 ClassDescriptor cd = toAnalyzeNext();
2215 HierarchyGraph graph = getHierarchyGraph(cd);
2216 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2217 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
2218 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
2219 graph.getHNode(fieldDesc);
2224 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2225 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2226 Descriptor key = (Descriptor) iterator.next();
2227 HierarchyGraph graph = getHierarchyGraph(key);
2229 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
2230 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
2231 HNode node = (HNode) iterator2.next();
2232 if (!node.isSkeleton() && !node.isCombinationNode()) {
2233 if (graph.getIncomingNodeSet(node).size() == 0) {
2234 nodeToBeConnected.add(node);
2239 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
2240 HNode node = (HNode) iterator2.next();
2241 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
2242 graph.addEdge(graph.getHNode(TOPDESC), node);
2249 private HierarchyGraph getHierarchyGraph(Descriptor d) {
2250 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
2251 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
2253 return mapDescriptorToHierarchyGraph.get(d);
2256 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
2258 // visit each node of method flow graph
2259 FlowGraph fg = getFlowGraph(md);
2260 // Set<FlowNode> nodeSet = fg.getNodeSet();
2262 Set<FlowEdge> edgeSet = fg.getEdgeSet();
2264 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
2265 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
2266 Descriptor desc = (Descriptor) iterator.next();
2267 methodGraph.getHNode(desc).setSkeleton(true);
2270 // for the method lattice, we need to look at the first element of
2271 // NTuple<Descriptor>
2272 boolean hasGlobalAccess = false;
2273 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2274 // FlowNode originalSrcNode = (FlowNode) iterator.next();
2275 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
2276 FlowEdge edge = (FlowEdge) iterator.next();
2278 FlowNode originalSrcNode = fg.getFlowNode(edge.getInitTuple());
2279 Set<FlowNode> sourceNodeSet = new HashSet<FlowNode>();
2280 if (originalSrcNode instanceof FlowReturnNode) {
2281 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
2282 System.out.println("rnode=" + rnode);
2283 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
2284 for (Iterator iterator2 = tupleSet.iterator(); iterator2.hasNext();) {
2285 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator2.next();
2286 sourceNodeSet.add(fg.getFlowNode(nTuple));
2287 System.out.println("&&&SOURCE fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
2290 sourceNodeSet.add(originalSrcNode);
2293 // System.out.println("---sourceNodeSet=" + sourceNodeSet + " from originalSrcNode="
2294 // + originalSrcNode);
2296 for (Iterator iterator3 = sourceNodeSet.iterator(); iterator3.hasNext();) {
2297 FlowNode srcNode = (FlowNode) iterator3.next();
2299 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
2300 Descriptor srcLocalDesc = srcNodeTuple.get(0);
2302 if (srcLocalDesc instanceof InterDescriptor
2303 && ((InterDescriptor) srcLocalDesc).getMethodArgIdxPair() != null) {
2305 if (srcNode.getCompositeLocation() == null) {
2310 // if the srcNode is started with the global descriptor
2311 // need to set as a skeleton node
2312 if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
2313 hasGlobalAccess = true;
2316 // Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(originalSrcNode);
2317 // for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
2318 // FlowEdge outEdge = (FlowEdge) iterator2.next();
2319 // FlowNode originalDstNode = outEdge.getDst();
2320 FlowNode originalDstNode = fg.getFlowNode(edge.getEndTuple());
2322 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
2323 if (originalDstNode instanceof FlowReturnNode) {
2324 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
2325 // System.out.println("\n-returnNode=" + rnode);
2326 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
2327 for (Iterator iterator4 = tupleSet.iterator(); iterator4.hasNext();) {
2328 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator4.next();
2329 dstNodeSet.add(fg.getFlowNode(nTuple));
2330 System.out.println("&&&DST fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
2333 dstNodeSet.add(originalDstNode);
2335 // System.out.println("---dstNodeSet=" + dstNodeSet);
2336 for (Iterator iterator4 = dstNodeSet.iterator(); iterator4.hasNext();) {
2337 FlowNode dstNode = (FlowNode) iterator4.next();
2339 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
2340 Descriptor dstLocalDesc = dstNodeTuple.get(0);
2342 if (dstLocalDesc instanceof InterDescriptor
2343 && ((InterDescriptor) dstLocalDesc).getMethodArgIdxPair() != null) {
2344 if (dstNode.getCompositeLocation() == null) {
2345 System.out.println("%%%%%%%%%%%%%SKIP=" + dstNode);
2350 // if (outEdge.getInitTuple().equals(srcNodeTuple)
2351 // && outEdge.getEndTuple().equals(dstNodeTuple)) {
2353 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
2354 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
2356 System.out.println("-srcCurTuple=" + srcCurTuple + " dstCurTuple=" + dstCurTuple
2357 + " srcNode=" + srcNode + " dstNode=" + dstNode);
2359 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
2360 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
2362 // value flows between fields
2363 Descriptor desc = srcCurTuple.get(0);
2364 ClassDescriptor classDesc;
2366 if (desc.equals(GLOBALDESC)) {
2367 classDesc = md.getClassDesc();
2369 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
2370 classDesc = varDesc.getType().getClassDesc();
2372 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
2374 } else if ((srcCurTuple.size() == 1 && dstCurTuple.size() == 1)
2375 || ((srcCurTuple.size() > 1 || dstCurTuple.size() > 1) && !srcCurTuple.get(0).equals(
2376 dstCurTuple.get(0)))) {
2378 // value flow between a primitive local var - a primitive local var or local var -
2381 Descriptor srcDesc = srcCurTuple.get(0);
2382 Descriptor dstDesc = dstCurTuple.get(0);
2384 methodGraph.addEdge(srcDesc, dstDesc);
2386 if (fg.isParamDesc(srcDesc)) {
2387 methodGraph.setParamHNode(srcDesc);
2389 if (fg.isParamDesc(dstDesc)) {
2390 methodGraph.setParamHNode(dstDesc);
2404 // If the method accesses static fields
2405 // set hasGloabalAccess true in the method summary.
2406 if (hasGlobalAccess) {
2407 getMethodSummary(md).setHasGlobalAccess();
2409 methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
2411 if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2412 // if the current method contains the event loop
2413 // we need to set all nodes of the hierarchy graph as a skeleton node
2414 Set<HNode> hnodeSet = methodGraph.getNodeSet();
2415 for (Iterator iterator = hnodeSet.iterator(); iterator.hasNext();) {
2416 HNode hnode = (HNode) iterator.next();
2417 hnode.setSkeleton(true);
2423 private MethodSummary getMethodSummary(MethodDescriptor md) {
2424 if (!mapDescToLocationSummary.containsKey(md)) {
2425 mapDescToLocationSummary.put(md, new MethodSummary(md));
2427 return (MethodSummary) mapDescToLocationSummary.get(md);
2430 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
2432 String classSymbol = cd.getSymbol();
2433 int idx = classSymbol.lastIndexOf("$");
2435 classSymbol = classSymbol.substring(idx + 1);
2438 String pattern = "class " + classSymbol + " ";
2439 if (strLine.indexOf(pattern) != -1) {
2440 mapDescToDefinitionLine.put(cd, lineNum);
2444 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
2446 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
2447 MethodDescriptor md = (MethodDescriptor) iterator.next();
2448 String pattern = md.getMethodDeclaration();
2449 if (strLine.indexOf(pattern) != -1) {
2450 mapDescToDefinitionLine.put(md, lineNum);
2451 methodSet.remove(md);
2458 private void readOriginalSourceFiles() {
2460 SymbolTable classtable = state.getClassSymbolTable();
2462 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
2463 classDescSet.addAll(classtable.getValueSet());
2466 // inefficient implement. it may re-visit the same file if the file
2467 // contains more than one class definitions.
2468 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
2469 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2471 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
2472 methodSet.addAll(cd.getMethodTable().getValueSet());
2474 String sourceFileName = cd.getSourceFileName();
2475 Vector<String> lineVec = new Vector<String>();
2477 mapFileNameToLineVector.put(sourceFileName, lineVec);
2479 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
2482 lineVec.add(""); // the index is started from 1.
2483 while ((strLine = in.readLine()) != null) {
2484 lineVec.add(lineNum, strLine);
2485 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
2486 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
2492 } catch (IOException e) {
2493 e.printStackTrace();
2498 private String generateLatticeDefinition(Descriptor desc) {
2500 Set<String> sharedLocSet = new HashSet<String>();
2502 SSJavaLattice<String> lattice = getLattice(desc);
2503 String rtr = "@LATTICE(\"";
2505 Map<String, Set<String>> map = lattice.getTable();
2506 Set<String> keySet = map.keySet();
2507 boolean first = true;
2508 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2509 String key = (String) iterator.next();
2510 if (!key.equals(lattice.getTopItem())) {
2511 Set<String> connectedSet = map.get(key);
2513 if (connectedSet.size() == 1) {
2514 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
2522 if (lattice.isSharedLoc(key)) {
2523 rtr += "," + key + "*";
2528 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
2529 String loc = (String) iterator2.next();
2530 if (!loc.equals(lattice.getBottomItem())) {
2537 rtr += loc + "<" + key;
2538 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
2539 rtr += "," + key + "*";
2540 sharedLocSet.add(key);
2542 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
2543 rtr += "," + loc + "*";
2544 sharedLocSet.add(loc);
2554 if (desc instanceof MethodDescriptor) {
2555 System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
2557 MethodDescriptor md = (MethodDescriptor) desc;
2558 MethodSummary methodSummary = getMethodSummary(md);
2560 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
2561 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
2562 if (returnType != null && (!returnType.isVoid())) {
2564 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
2566 CompositeLocation pcLoc = methodSummary.getPCLoc();
2567 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
2568 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
2572 if (!md.isStatic()) {
2573 rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
2575 rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
2582 private void generateAnnoatedCode() {
2584 readOriginalSourceFiles();
2587 while (!toAnalyzeIsEmpty()) {
2588 ClassDescriptor cd = toAnalyzeNext();
2590 setupToAnalazeMethod(cd);
2592 String sourceFileName = cd.getSourceFileName();
2594 if (cd.isInterface()) {
2598 int classDefLine = mapDescToDefinitionLine.get(cd);
2599 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
2601 LocationSummary fieldLocSummary = getLocationSummary(cd);
2603 String fieldLatticeDefStr = generateLatticeDefinition(cd);
2604 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
2605 sourceVec.set(classDefLine, annoatedSrc);
2607 // generate annotations for field declarations
2608 // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
2609 Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
2611 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2612 FieldDescriptor fd = (FieldDescriptor) iter.next();
2614 String locAnnotationStr;
2615 // CompositeLocation inferLoc = inferLocMap.get(fd);
2616 String locName = mapFieldNameToLocName.get(fd.getSymbol());
2618 if (locName != null) {
2619 // infer loc is null if the corresponding field is static and final
2620 // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2621 locAnnotationStr = "@LOC(\"" + locName + "\")";
2622 int fdLineNum = fd.getLineNum();
2623 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
2624 String fieldDeclaration = fd.toString();
2625 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
2626 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
2627 sourceVec.set(fdLineNum, annoatedStr);
2632 while (!toAnalyzeMethodIsEmpty()) {
2633 MethodDescriptor md = toAnalyzeMethodNext();
2635 if (!ssjava.needTobeAnnotated(md)) {
2639 SSJavaLattice<String> methodLattice = md2lattice.get(md);
2640 if (methodLattice != null) {
2642 int methodDefLine = md.getLineNum();
2644 // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
2645 // Map<Descriptor, CompositeLocation> methodInferLocMap =
2646 // methodLocInfo.getMapDescToInferLocation();
2648 MethodSummary methodSummary = getMethodSummary(md);
2650 Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
2651 methodSummary.getMapVarDescToInferCompositeLocation();
2652 System.out.println("-----md=" + md);
2653 System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
2655 Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
2657 Set<String> localLocElementSet = methodLattice.getElementSet();
2659 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
2660 Descriptor localVarDesc = (Descriptor) iterator.next();
2661 System.out.println("-------localVarDesc=" + localVarDesc);
2662 CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
2664 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
2665 if (!localLocElementSet.contains(localLocIdentifier)) {
2666 methodLattice.put(localLocIdentifier);
2669 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2671 if (!isParameter(md, localVarDesc)) {
2672 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
2673 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
2674 String orgSourceLine = sourceVec.get(varLineNum);
2676 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
2677 System.out.println("idx=" + idx
2678 + " generateVarDeclaration((VarDescriptor) localVarDesc)="
2679 + generateVarDeclaration((VarDescriptor) localVarDesc));
2681 String annoatedStr =
2682 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
2683 + orgSourceLine.substring(idx);
2684 sourceVec.set(varLineNum, annoatedStr);
2687 String methodDefStr = sourceVec.get(methodDefLine);
2690 getParamLocation(methodDefStr,
2691 generateVarDeclaration((VarDescriptor) localVarDesc));
2692 System.out.println("methodDefStr=" + methodDefStr + " localVarDesc=" + localVarDesc
2696 String annoatedStr =
2697 methodDefStr.substring(0, idx) + locAnnotationStr + " "
2698 + methodDefStr.substring(idx);
2699 sourceVec.set(methodDefLine, annoatedStr);
2704 // check if the lattice has to have the location type for the this
2707 // boolean needToAddthisRef = hasThisReference(md);
2708 // if (localLocElementSet.contains("this")) {
2709 // methodLattice.put("this");
2712 String methodLatticeDefStr = generateLatticeDefinition(md);
2713 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
2714 sourceVec.set(methodDefLine, annoatedStr);
2724 private boolean hasThisReference(MethodDescriptor md) {
2726 FlowGraph fg = getFlowGraph(md);
2727 Set<FlowNode> nodeSet = fg.getNodeSet();
2728 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2729 FlowNode flowNode = (FlowNode) iterator.next();
2730 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
2738 private int getParamLocation(String methodStr, String paramStr) {
2740 String pattern = paramStr + ",";
2742 int idx = methodStr.indexOf(pattern);
2746 pattern = paramStr + ")";
2747 return methodStr.indexOf(pattern);
2752 private String generateVarDeclaration(VarDescriptor varDesc) {
2754 TypeDescriptor td = varDesc.getType();
2755 String rtr = td.toString();
2757 for (int i = 0; i < td.getArrayCount(); i++) {
2761 rtr += " " + varDesc.getName();
2766 private String generateLocationAnnoatation(CompositeLocation loc) {
2769 Location methodLoc = loc.get(0);
2770 rtr += methodLoc.getLocIdentifier();
2772 for (int i = 1; i < loc.getSize(); i++) {
2773 Location element = loc.get(i);
2774 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
2780 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
2781 return getFlowGraph(md).isParamDesc(localVarDesc);
2784 private String extractFileName(String fileName) {
2785 int idx = fileName.lastIndexOf("/");
2789 return fileName.substring(idx + 1);
2794 private void codeGen() {
2796 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
2797 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
2798 String orgFileName = (String) iterator.next();
2799 String outputFileName = extractFileName(orgFileName);
2801 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
2805 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
2806 BufferedWriter out = new BufferedWriter(fileWriter);
2808 for (int i = 0; i < sourceVec.size(); i++) {
2809 out.write(sourceVec.get(i));
2813 } catch (IOException e) {
2814 e.printStackTrace();
2821 private void checkLattices() {
2823 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2825 // current descriptors to visit in fixed-point interprocedural analysis,
2827 // dependency in the call graph
2828 methodDescriptorsToVisitStack.clear();
2830 // descriptorListToAnalyze.removeFirst();
2832 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2833 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2835 while (!descriptorListToAnalyze.isEmpty()) {
2836 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2837 checkLatticesOfVirtualMethods(md);
2842 private void debug_writeLatticeDotFile() {
2843 // generate lattice dot file
2847 while (!toAnalyzeIsEmpty()) {
2848 ClassDescriptor cd = toAnalyzeNext();
2850 setupToAnalazeMethod(cd);
2852 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
2853 if (classLattice != null) {
2854 ssjava.writeLatticeDotFile(cd, null, classLattice);
2855 debug_printDescriptorToLocNameMapping(cd);
2858 while (!toAnalyzeMethodIsEmpty()) {
2859 MethodDescriptor md = toAnalyzeMethodNext();
2860 SSJavaLattice<String> methodLattice = md2lattice.get(md);
2861 if (methodLattice != null) {
2862 ssjava.writeLatticeDotFile(cd, md, methodLattice);
2863 debug_printDescriptorToLocNameMapping(md);
2870 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
2872 LocationInfo info = getLocationInfo(desc);
2873 System.out.println("## " + desc + " ##");
2874 System.out.println(info.getMapDescToInferLocation());
2875 LocationInfo locInfo = getLocationInfo(desc);
2876 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
2877 System.out.println("###################");
2881 private void calculateExtraLocations() {
2883 LinkedList<MethodDescriptor> methodDescList = ssjava.getSortedDescriptors();
2884 for (Iterator iterator = methodDescList.iterator(); iterator.hasNext();) {
2885 MethodDescriptor md = (MethodDescriptor) iterator.next();
2886 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2887 calculateExtraLocations(md);
2893 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
2895 if (!md.isStatic()) {
2896 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2897 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
2899 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
2900 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
2901 if (!md.equals(mdCallee)) {
2902 checkConsistency(md, mdCallee);
2910 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
2912 // check that two lattice have the same relations between parameters(+PC
2913 // LOC, GLOBAL_LOC RETURN LOC)
2915 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
2916 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
2918 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
2919 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
2921 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
2922 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
2924 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
2926 // add location types of paramters
2927 for (int idx = 0; idx < numParam; idx++) {
2928 list1.add(paramMap1.get(Integer.valueOf(idx)));
2929 list2.add(paramMap2.get(Integer.valueOf(idx)));
2932 // add program counter location
2933 list1.add(locInfo1.getPCLoc());
2934 list2.add(locInfo2.getPCLoc());
2936 if (!md1.getReturnType().isVoid()) {
2937 // add return value location
2938 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
2939 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
2944 // add global location type
2945 if (md1.isStatic()) {
2946 CompositeLocation globalLoc1 =
2947 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
2948 CompositeLocation globalLoc2 =
2949 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
2950 list1.add(globalLoc1);
2951 list2.add(globalLoc2);
2954 for (int i = 0; i < list1.size(); i++) {
2955 CompositeLocation locA1 = list1.get(i);
2956 CompositeLocation locA2 = list2.get(i);
2957 for (int k = 0; k < list1.size(); k++) {
2959 CompositeLocation locB1 = list1.get(k);
2960 CompositeLocation locB2 = list2.get(k);
2961 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
2963 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
2966 throw new Error("The method " + md1 + " is not consistent with the method " + md2
2967 + ".:: They have a different ordering relation between locations (" + locA1 + ","
2968 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
2976 private String getSymbol(int idx, FlowNode node) {
2977 Descriptor desc = node.getDescTuple().get(idx);
2978 return desc.getSymbol();
2981 private Descriptor getDescriptor(int idx, FlowNode node) {
2982 Descriptor desc = node.getDescTuple().get(idx);
2986 private void calculatePCLOC(MethodDescriptor md) {
2988 System.out.println("#CalculatePCLOC");
2989 MethodSummary methodSummary = getMethodSummary(md);
2990 FlowGraph fg = getFlowGraph(md);
2991 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
2993 // calculate the initial program counter location
2994 // PC location is higher than location types of parameters which has incoming flows.
2996 Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
2997 Set<Descriptor> paramDescNOTHavingInFlowSet = new HashSet<Descriptor>();
2998 // Set<FlowNode> paramNodeNOThavingInFlowSet = new HashSet<FlowNode>();
3000 int numParams = fg.getNumParameters();
3001 for (int i = 0; i < numParams; i++) {
3002 FlowNode paramFlowNode = fg.getParamFlowNode(i);
3003 Descriptor prefix = paramFlowNode.getDescTuple().get(0);
3004 NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
3005 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
3007 Set<FlowNode> inNodeToParamSet = fg.getIncomingNodeSetByPrefix(prefix);
3008 if (inNodeToParamSet.size() > 0) {
3009 // parameter has in-value flows
3011 for (Iterator iterator = inNodeToParamSet.iterator(); iterator.hasNext();) {
3012 FlowNode inNode = (FlowNode) iterator.next();
3013 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(inNode);
3014 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3015 FlowEdge flowEdge = (FlowEdge) iterator2.next();
3016 if (flowEdge.getEndTuple().startsWith(prefix)) {
3017 NTuple<Location> paramLocTupleWithIncomingFlow =
3018 translateToLocTuple(md, flowEdge.getEndTuple());
3019 paramLocTupleHavingInFlowSet.add(paramLocTupleWithIncomingFlow);
3024 // paramLocTupleHavingInFlowSet.add(paramLocTuple);
3026 // paramNodeNOThavingInFlowSet.add(fg.getFlowNode(paramDescTuple));
3027 paramDescNOTHavingInFlowSet.add(prefix);
3031 System.out.println("paramLocTupleHavingInFlowSet=" + paramLocTupleHavingInFlowSet);
3033 if (paramLocTupleHavingInFlowSet.size() > 0
3034 && !coversAllParamters(md, fg, paramLocTupleHavingInFlowSet)) {
3036 // Here, generates a location in the method lattice that is higher than the
3037 // paramLocTupleHavingInFlowSet
3038 NTuple<Location> pcLocTuple =
3039 generateLocTupleRelativeTo(md, paramLocTupleHavingInFlowSet, PCLOC);
3041 NTuple<Descriptor> pcDescTuple = translateToDescTuple(pcLocTuple);
3043 // System.out.println("pcLoc=" + pcLocTuple);
3045 CompositeLocation curPCLoc = methodSummary.getPCLoc();
3046 if (curPCLoc.get(0).isTop() || pcLocTuple.size() > curPCLoc.getSize()) {
3047 methodSummary.setPCLoc(new CompositeLocation(pcLocTuple));
3049 Set<FlowNode> flowNodeLowerthanPCLocSet = new HashSet<FlowNode>();
3050 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
3051 // add ordering relations s.t. PCLOC is higher than all flow nodes except the set of
3052 // parameters that do not have incoming flows
3053 for (Iterator iterator = fg.getNodeSet().iterator(); iterator.hasNext();) {
3054 FlowNode node = (FlowNode) iterator.next();
3056 if (!(node instanceof FlowReturnNode)) {
3057 if (!paramDescNOTHavingInFlowSet.contains(node.getCurrentDescTuple().get(0))) {
3058 flowNodeLowerthanPCLocSet.add(node);
3059 fg.addValueFlowEdge(pcDescTuple, node.getDescTuple());
3060 subGlobalFlowGraph.addValueFlowEdge(pcLocTuple,
3061 translateToLocTuple(md, node.getDescTuple()));
3064 System.out.println("***SKIP PCLOC -> RETURNLOC=" + node);
3068 fg.getFlowNode(translateToDescTuple(pcLocTuple)).setSkeleton(true);
3070 if (pcLocTuple.get(0).getLocDescriptor().equals(md.getThis())) {
3071 System.out.println("#########################################");
3072 for (Iterator iterator = flowNodeLowerthanPCLocSet.iterator(); iterator.hasNext();) {
3073 FlowNode lowerNode = (FlowNode) iterator.next();
3074 if (lowerNode.getCompositeLocation() == null) {
3075 NTuple<Location> lowerLocTuple = translateToLocTuple(md, lowerNode.getDescTuple());
3076 CompositeLocation newComp =
3077 calculateCompositeLocationFromSubGlobalGraph(md, lowerNode);
3078 if (newComp != null) {
3079 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(lowerLocTuple.get(0),
3081 lowerNode.setCompositeLocation(newComp);
3082 System.out.println("NEW COMP LOC=" + newComp + " to lowerNode=" + lowerNode);
3095 private boolean coversAllParamters(MethodDescriptor md, FlowGraph fg,
3096 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
3098 int numParam = fg.getNumParameters();
3099 int size = paramLocTupleHavingInFlowSet.size();
3101 if (!md.isStatic()) {
3103 // if the method is not static && there is a parameter composite location &&
3104 // it is started with 'this',
3105 // paramLocTupleHavingInFlowSet need to have 'this' parameter.
3107 FlowNode thisParamNode = fg.getParamFlowNode(0);
3108 NTuple<Location> thisParamLocTuple =
3109 translateToLocTuple(md, thisParamNode.getCurrentDescTuple());
3111 if (!paramLocTupleHavingInFlowSet.contains(thisParamLocTuple)) {
3113 for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
3114 NTuple<Location> paramTuple = (NTuple<Location>) iterator.next();
3115 if (paramTuple.size() > 1 && paramTuple.get(0).getLocDescriptor().equals(md.getThis())) {
3116 // paramLocTupleHavingInFlowSet.add(thisParamLocTuple);
3125 if (size == numParam) {
3133 private void calculateRETURNLOC(MethodDescriptor md) {
3135 System.out.println("#calculateRETURNLOC= " + md);
3137 // calculate a return location:
3138 // the return location type is lower than all parameters and the location of return values
3139 MethodSummary methodSummary = getMethodSummary(md);
3140 if (methodSummary.getRETURNLoc() != null) {
3143 FlowGraph fg = getFlowGraph(md);
3144 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
3145 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
3147 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
3148 // first, generate the set of return value location types that starts
3149 // with 'this' reference
3151 Set<FlowNode> paramFlowNodeFlowingToReturnValueSet = getParamNodeFlowingToReturnValue(md);
3152 // System.out.println("paramFlowNodeFlowingToReturnValueSet="
3153 // + paramFlowNodeFlowingToReturnValueSet);
3155 Set<NTuple<Location>> tupleToBeHigherThanReturnLocSet = new HashSet<NTuple<Location>>();
3156 for (Iterator iterator = paramFlowNodeFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
3157 FlowNode fn = (FlowNode) iterator.next();
3158 NTuple<Descriptor> paramDescTuple = fn.getCurrentDescTuple();
3159 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, paramDescTuple));
3162 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
3163 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
3164 FlowNode returnNode = (FlowNode) iterator.next();
3165 NTuple<Descriptor> returnDescTuple = returnNode.getCurrentDescTuple();
3166 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, returnDescTuple));
3168 System.out.println("-flow graph's returnNodeSet=" + returnNodeSet);
3169 System.out.println("tupleSetToBeHigherThanReturnLoc=" + tupleToBeHigherThanReturnLocSet);
3171 // Here, generates a return location in the method lattice that is lower than the
3172 // locFlowingToReturnValueSet
3173 NTuple<Location> returnLocTuple =
3174 generateLocTupleRelativeTo(md, tupleToBeHigherThanReturnLocSet, RLOC);
3176 // System.out.println("returnLocTuple=" + returnLocTuple);
3177 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnLocTuple);
3178 CompositeLocation curReturnLoc = methodSummary.getRETURNLoc();
3179 if (curReturnLoc == null || returnDescTuple.size() > curReturnLoc.getSize()) {
3180 methodSummary.setRETURNLoc(new CompositeLocation(returnLocTuple));
3182 for (Iterator iterator = tupleToBeHigherThanReturnLocSet.iterator(); iterator.hasNext();) {
3183 NTuple<Location> higherTuple = (NTuple<Location>) iterator.next();
3184 fg.addValueFlowEdge(translateToDescTuple(higherTuple), returnDescTuple);
3186 fg.getFlowNode(returnDescTuple).setSkeleton(true);
3194 private void calculateExtraLocations(MethodDescriptor md) {
3195 // calcualte pcloc, returnloc,...
3197 System.out.println("\nSSJAVA:Calculate PCLOC/RETURNLOC locations: " + md);
3200 calculateRETURNLOC(md);
3204 private NTuple<Location> generateLocTupleRelativeTo(MethodDescriptor md,
3205 Set<NTuple<Location>> paramLocTupleHavingInFlowSet, String locNamePrefix) {
3207 // System.out.println("-generateLocTupleRelativeTo=" + paramLocTupleHavingInFlowSet);
3209 NTuple<Location> higherLocTuple = new NTuple<Location>();
3211 VarDescriptor thisVarDesc = md.getThis();
3212 // check if all paramter loc tuple is started with 'this' reference
3213 boolean hasParamNotStartedWithThisRef = false;
3217 Set<NTuple<Location>> paramLocTupleStartedWithThis = new HashSet<NTuple<Location>>();
3219 next: for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
3220 NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
3221 Descriptor paramLocalDesc = paramLocTuple.get(0).getLocDescriptor();
3222 if (!paramLocalDesc.equals(thisVarDesc)) {
3224 Set<FlowNode> inNodeSet = getFlowGraph(md).getIncomingNodeSetByPrefix(paramLocalDesc);
3225 for (Iterator iterator2 = inNodeSet.iterator(); iterator2.hasNext();) {
3226 FlowNode flowNode = (FlowNode) iterator2.next();
3227 if (flowNode.getDescTuple().startsWith(thisVarDesc)) {
3228 // System.out.println("paramLocTuple=" + paramLocTuple + " is lower than THIS");
3232 hasParamNotStartedWithThisRef = true;
3234 } else if (paramLocTuple.size() > 1) {
3235 paramLocTupleStartedWithThis.add(paramLocTuple);
3236 if (minSize == 0 || minSize > paramLocTuple.size()) {
3237 minSize = paramLocTuple.size();
3242 // System.out.println("---paramLocTupleStartedWithThis=" + paramLocTupleStartedWithThis);
3243 Descriptor enclosingDesc = md;
3244 if (hasParamNotStartedWithThisRef) {
3245 // in this case, PCLOC will be the local location
3247 // all parameter is started with 'this', so PCLOC will be set relative to the composite
3248 // location started with 'this'.
3249 // for (int idx = 0; idx < minSize - 1; idx++) {
3250 for (int idx = 0; idx < 1; idx++) {
3251 Set<Descriptor> locDescSet = new HashSet<Descriptor>();
3252 Location curLoc = null;
3253 NTuple<Location> paramLocTuple = null;
3254 for (Iterator iterator = paramLocTupleStartedWithThis.iterator(); iterator.hasNext();) {
3255 paramLocTuple = (NTuple<Location>) iterator.next();
3256 // System.out.println("-----paramLocTuple=" + paramLocTuple + " idx=" + idx);
3257 curLoc = paramLocTuple.get(idx);
3258 Descriptor locDesc = curLoc.getLocDescriptor();
3259 locDescSet.add(locDesc);
3261 // System.out.println("-----locDescSet=" + locDescSet + " idx=" + idx);
3262 if (locDescSet.size() != 1) {
3265 Location newLocElement = new Location(curLoc.getDescriptor(), curLoc.getLocDescriptor());
3266 System.out.println("newLocElement" + newLocElement);
3267 higherLocTuple.add(newLocElement);
3268 enclosingDesc = getClassTypeDescriptor(curLoc.getLocDescriptor());
3273 String locIdentifier = locNamePrefix + (locSeed++);
3274 NameDescriptor locDesc = new NameDescriptor(locIdentifier);
3275 Location newLoc = new Location(enclosingDesc, locDesc);
3276 higherLocTuple.add(newLoc);
3277 System.out.println("---new loc tuple=" + higherLocTuple);
3279 return higherLocTuple;
3283 public ClassDescriptor getClassTypeDescriptor(Descriptor in) {
3285 if (in instanceof VarDescriptor) {
3286 return ((VarDescriptor) in).getType().getClassDesc();
3287 } else if (in instanceof FieldDescriptor) {
3288 return ((FieldDescriptor) in).getType().getClassDesc();
3290 // else if (in instanceof LocationDescriptor) {
3291 // // here is the case that the descriptor 'in' is the last element of the assigned composite
3293 // return ((VarDescriptor) locTuple.get(0).getLocDescriptor()).getType().getClassDesc();
3301 private Set<NTuple<Location>> calculateHighestLocTupleSet(
3302 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
3304 Set<NTuple<Location>> highestSet = new HashSet<NTuple<Location>>();
3306 Iterator<NTuple<Location>> iterator = paramLocTupleHavingInFlowSet.iterator();
3307 NTuple<Location> highest = iterator.next();
3309 for (; iterator.hasNext();) {
3310 NTuple<Location> curLocTuple = (NTuple<Location>) iterator.next();
3311 if (isHigherThan(curLocTuple, highest)) {
3312 // System.out.println(curLocTuple + " is greater than " + highest);
3313 highest = curLocTuple;
3317 highestSet.add(highest);
3319 MethodDescriptor md = (MethodDescriptor) highest.get(0).getDescriptor();
3320 VarDescriptor thisVarDesc = md.getThis();
3322 // System.out.println("highest=" + highest);
3324 for (Iterator<NTuple<Location>> iter = paramLocTupleHavingInFlowSet.iterator(); iter.hasNext();) {
3325 NTuple<Location> curLocTuple = iter.next();
3327 if (!curLocTuple.equals(highest) && !hasOrderingRelation(highest, curLocTuple)) {
3329 // System.out.println("add it to the highest set=" + curLocTuple);
3330 highestSet.add(curLocTuple);
3339 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
3340 Set<String> higherLocSet = new HashSet<String>();
3342 Set<String> locSet = lattice.getTable().keySet();
3343 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
3344 String element = (String) iterator.next();
3345 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
3346 higherLocSet.add(element);
3349 return higherLocSet;
3352 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
3353 Set<CompositeLocation> set) {
3355 CompositeLocation lowest = set.iterator().next();
3357 if (set.size() == 1) {
3361 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
3362 CompositeLocation loc = (CompositeLocation) iterator.next();
3364 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
3365 // if there is a case where composite locations are incomparable, just
3370 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
3377 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
3378 CompositeLocation comp2) {
3380 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
3382 for (int idx = 0; idx < size; idx++) {
3383 Location loc1 = comp1.get(idx);
3384 Location loc2 = comp2.get(idx);
3386 Descriptor desc1 = loc1.getDescriptor();
3387 Descriptor desc2 = loc2.getDescriptor();
3389 if (!desc1.equals(desc2)) {
3390 throw new Error("Fail to compare " + comp1 + " and " + comp2);
3393 String symbol1 = loc1.getLocIdentifier();
3394 String symbol2 = loc2.getLocIdentifier();
3396 SSJavaLattice<String> lattice;
3398 lattice = methodLattice;
3400 lattice = getLattice(desc1);
3403 if (symbol1.equals(symbol2)) {
3405 } else if (!lattice.isComparable(symbol1, symbol2)) {
3414 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
3415 CompositeLocation comp2) {
3417 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
3419 for (int idx = 0; idx < size; idx++) {
3420 Location loc1 = comp1.get(idx);
3421 Location loc2 = comp2.get(idx);
3423 Descriptor desc1 = loc1.getDescriptor();
3424 Descriptor desc2 = loc2.getDescriptor();
3426 if (!desc1.equals(desc2)) {
3427 throw new Error("Fail to compare " + comp1 + " and " + comp2);
3430 String symbol1 = loc1.getLocIdentifier();
3431 String symbol2 = loc2.getLocIdentifier();
3433 SSJavaLattice<String> lattice;
3435 lattice = methodLattice;
3437 lattice = getLattice(desc1);
3440 if (symbol1.equals(symbol2)) {
3442 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
3453 private GlobalFlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
3455 if (!mapMethodDescriptorToSubGlobalFlowGraph.containsKey(md)) {
3456 mapMethodDescriptorToSubGlobalFlowGraph.put(md, new GlobalFlowGraph(md));
3458 return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
3461 private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
3462 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
3464 System.out.println("-propagateFlowsToCallerWithNoCompositeLocation=" + min.printNode(0));
3465 // if the parameter A reaches to the parameter B
3466 // then, add an edge the argument A -> the argument B to the caller's flow
3469 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
3470 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
3471 int numParam = calleeFlowGraph.getNumParameters();
3473 for (int i = 0; i < numParam; i++) {
3474 for (int k = 0; k < numParam; k++) {
3478 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
3479 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
3481 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
3482 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
3484 // check if the callee propagates an ordering constraints through
3487 // Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
3488 Set<FlowNode> localReachSet =
3489 calleeFlowGraph.getReachableSetFrom(paramNode1.getDescTuple());
3491 NTuple<Descriptor> paramDescTuple1 = paramNode1.getCurrentDescTuple();
3492 NTuple<Descriptor> paramDescTuple2 = paramNode2.getCurrentDescTuple();
3494 // System.out.println("-param1CurTuple=" + paramDescTuple1 + " param2CurTuple="
3495 // + paramDescTuple2);
3496 // System.out.println("-- localReachSet from param1=" + localReachSet);
3498 if (paramDescTuple1.get(0).equals(paramDescTuple2.get(0))) {
3499 // if two parameters share the same prefix
3500 // it already has been assigned to a composite location
3501 // so we don't need to add an additional ordering relation caused by these two
3506 if (arg1Tuple.size() > 0 && arg2Tuple.size() > 0
3507 && containsPrefix(paramNode2.getDescTuple().get(0), localReachSet)) {
3508 // need to propagate an ordering relation s.t. arg1 is higher
3510 // System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
3512 // add a new flow between the corresponding arguments.
3513 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
3514 // System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
3517 // .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
3521 // System.out.println();
3526 // if a parameter has a composite location and the first element of the parameter location
3527 // matches the callee's 'this'
3528 // we have a more specific constraint: the caller's corresponding argument is higher than the
3529 // parameter location which is translated into the caller
3531 for (int idx = 0; idx < numParam; idx++) {
3532 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
3533 CompositeLocation compLoc = paramNode.getCompositeLocation();
3534 System.out.println("paramNode=" + paramNode + " compLoc=" + compLoc);
3535 if (compLoc != null && compLoc.get(0).getLocDescriptor().equals(min.getMethod().getThis())) {
3536 System.out.println("$$$COMPLOC CASE=" + compLoc + " idx=" + idx);
3538 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
3539 System.out.println("--- argTuple=" + argTuple + " current compLoc="
3540 + callerFlowGraph.getFlowNode(argTuple).getCompositeLocation());
3542 NTuple<Descriptor> translatedParamTuple =
3543 translateCompositeLocationToCaller(idx, min, compLoc);
3544 System.out.println("add a flow edge= " + argTuple + "->" + translatedParamTuple);
3545 callerFlowGraph.addValueFlowEdge(argTuple, translatedParamTuple);
3547 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
3548 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
3549 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
3550 callerFlowGraph.addValueFlowEdge(translateToDescTuple(pcLocTuple), translatedParamTuple);
3558 private boolean containsPrefix(Descriptor prefixDesc, Set<FlowNode> set) {
3560 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
3561 FlowNode flowNode = (FlowNode) iterator.next();
3562 if (flowNode.getDescTuple().startsWith(prefixDesc)) {
3563 System.out.println("FOUND=" + flowNode);
3570 private NTuple<Descriptor> translateCompositeLocationToCaller(int idx, MethodInvokeNode min,
3571 CompositeLocation compLocForParam1) {
3573 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
3575 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
3576 for (int i = 0; i < baseTuple.size(); i++) {
3577 tuple.add(baseTuple.get(i));
3580 for (int i = 1; i < compLocForParam1.getSize(); i++) {
3581 Location loc = compLocForParam1.get(i);
3582 tuple.add(loc.getLocDescriptor());
3588 private CompositeLocation generateCompositeLocation(NTuple<Location> prefixLocTuple) {
3590 System.out.println("generateCompositeLocation=" + prefixLocTuple);
3592 CompositeLocation newCompLoc = new CompositeLocation();
3593 for (int i = 0; i < prefixLocTuple.size(); i++) {
3594 newCompLoc.addLocation(prefixLocTuple.get(i));
3597 Descriptor lastDescOfPrefix = prefixLocTuple.get(prefixLocTuple.size() - 1).getLocDescriptor();
3599 ClassDescriptor enclosingDescriptor;
3600 if (lastDescOfPrefix instanceof FieldDescriptor) {
3601 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
3602 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
3603 } else if (lastDescOfPrefix.equals(GLOBALDESC)) {
3604 MethodDescriptor currentMethodDesc = (MethodDescriptor) prefixLocTuple.get(0).getDescriptor();
3605 enclosingDescriptor = currentMethodDesc.getClassDesc();
3607 // var descriptor case
3608 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
3610 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
3612 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
3613 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
3615 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
3616 newLoc.setLocDescriptor(newLocDescriptor);
3617 newCompLoc.addLocation(newLoc);
3619 // System.out.println("--newCompLoc=" + newCompLoc);
3623 private CompositeLocation generateCompositeLocation(MethodDescriptor md,
3624 NTuple<Descriptor> paramPrefix) {
3626 System.out.println("generateCompositeLocation=" + paramPrefix);
3628 CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
3630 Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
3631 // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
3632 // + lastDescOfPrefix.getClass());
3633 ClassDescriptor enclosingDescriptor;
3634 if (lastDescOfPrefix instanceof FieldDescriptor) {
3635 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
3636 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
3638 // var descriptor case
3639 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
3641 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
3643 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
3644 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
3646 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
3647 newLoc.setLocDescriptor(newLocDescriptor);
3648 newCompLoc.addLocation(newLoc);
3650 // System.out.println("--newCompLoc=" + newCompLoc);
3654 private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
3655 MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
3657 List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
3659 for (int i = 0; i < callerPrefixList.size(); i++) {
3660 NTuple<Descriptor> prefix = callerPrefixList.get(i);
3661 if (prefix.startsWith(baseRef)) {
3662 NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
3663 calleePrefix.add(mdCallee.getThis());
3664 for (int k = 1; k < prefix.size(); k++) {
3665 calleePrefix.add(prefix.get(k));
3667 calleePrefixList.add(calleePrefix);
3671 return calleePrefixList;
3675 public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
3677 CompositeLocation compLoc = new CompositeLocation();
3679 Descriptor enclosingDescriptor = md;
3681 for (int i = 0; i < tuple.size(); i++) {
3682 Descriptor curDescriptor = tuple.get(i);
3683 Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
3684 locElement.setLocDescriptor(curDescriptor);
3685 compLoc.addLocation(locElement);
3687 if (curDescriptor instanceof VarDescriptor) {
3688 enclosingDescriptor = md.getClassDesc();
3689 } else if (curDescriptor instanceof FieldDescriptor) {
3690 enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
3691 } else if (curDescriptor instanceof NameDescriptor) {
3692 // it is "GLOBAL LOC" case!
3693 enclosingDescriptor = GLOBALDESC;
3695 enclosingDescriptor = null;
3703 private LocationDescriptor generateNewLocationDescriptor() {
3704 return new LocationDescriptor("Loc" + (locSeed++));
3707 private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
3709 // return the index where the prefix shared by tuple1 and tuple2 is ended
3710 // if there is no prefix shared by both of them, return -1
3712 int minSize = tuple1.size();
3713 if (minSize > tuple2.size()) {
3714 minSize = tuple2.size();
3718 for (int i = 0; i < minSize; i++) {
3719 if (!tuple1.get(i).equals(tuple2.get(i))) {
3729 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
3730 NTuple<Location> tuple) {
3732 // first, retrieve inferred location by the local var descriptor
3733 CompositeLocation inferLoc = new CompositeLocation();
3735 CompositeLocation localVarInferLoc =
3736 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
3738 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
3740 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
3741 inferLoc.addLocation(localVarInferLoc.get(i));
3744 for (int i = 1; i < tuple.size(); i++) {
3745 Location cur = tuple.get(i);
3746 Descriptor enclosingDesc = cur.getDescriptor();
3747 Descriptor curDesc = cur.getLocDescriptor();
3749 Location inferLocElement;
3750 if (curDesc == null) {
3751 // in this case, we have a newly generated location.
3752 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
3754 String fieldLocSymbol =
3755 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
3756 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
3757 inferLocElement.setLocDescriptor(curDesc);
3760 inferLoc.addLocation(inferLocElement);
3764 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
3768 public LocationInfo getLocationInfo(Descriptor d) {
3769 if (d instanceof MethodDescriptor) {
3770 return getMethodLocationInfo((MethodDescriptor) d);
3772 return getFieldLocationInfo((ClassDescriptor) d);
3776 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
3778 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
3779 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
3782 return mapMethodDescToMethodLocationInfo.get(md);
3786 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
3788 if (!mapClassToLocationInfo.containsKey(cd)) {
3789 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
3792 return mapClassToLocationInfo.get(cd);
3796 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
3797 NTuple<Location> prefix, NTuple<Location> element) {
3799 if (!map.containsKey(prefix)) {
3800 map.put(prefix, new HashSet<NTuple<Location>>());
3802 map.get(prefix).add(element);
3805 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
3806 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
3807 Descriptor desc = (Descriptor) iterator.next();
3809 if (desc.equals(LocationInference.GLOBALDESC)) {
3811 } else if (desc instanceof VarDescriptor) {
3812 if (!((VarDescriptor) desc).getType().isPrimitive()) {
3815 } else if (desc instanceof FieldDescriptor) {
3816 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
3825 private SSJavaLattice<String> getLattice(Descriptor d) {
3826 if (d instanceof MethodDescriptor) {
3827 return getMethodLattice((MethodDescriptor) d);
3829 return getFieldLattice((ClassDescriptor) d);
3833 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
3834 if (!md2lattice.containsKey(md)) {
3835 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
3837 return md2lattice.get(md);
3840 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
3841 md2lattice.put(md, lattice);
3844 private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
3847 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
3848 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
3850 if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
3851 && dstCurTuple.size() > (idx + 1)) {
3852 // value flow between fields: we don't need to add a binary relation
3855 Descriptor desc = srcCurTuple.get(idx);
3856 ClassDescriptor classDesc;
3859 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
3861 if (desc instanceof FieldDescriptor) {
3862 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
3864 // this case is that the local variable has a composite location assignment
3865 // the following element after the composite location to the local variable
3866 // has the enclosing descriptor of the local variable
3867 Descriptor localDesc = srcNode.getDescTuple().get(0);
3868 classDesc = ((VarDescriptor) localDesc).getType().getClassDesc();
3871 extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
3875 Descriptor srcFieldDesc = srcCurTuple.get(idx);
3876 Descriptor dstFieldDesc = dstCurTuple.get(idx);
3878 System.out.println("srcFieldDesc=" + srcFieldDesc + " dstFieldDesc=" + dstFieldDesc
3880 if (!srcFieldDesc.equals(dstFieldDesc)) {
3882 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
3883 } else if (!isReference(srcFieldDesc) && !isReference(dstFieldDesc)) {
3884 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
3891 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
3892 if (!cd2lattice.containsKey(cd)) {
3893 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
3895 return cd2lattice.get(cd);
3898 public LinkedList<MethodDescriptor> computeMethodList() {
3900 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
3904 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
3905 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
3907 while (!toAnalyzeIsEmpty()) {
3908 ClassDescriptor cd = toAnalyzeNext();
3910 setupToAnalazeMethod(cd);
3911 temp_toanalyzeMethodList.removeAll(visited);
3913 while (!toAnalyzeMethodIsEmpty()) {
3914 MethodDescriptor md = toAnalyzeMethodNext();
3915 if ((!visited.contains(md))
3916 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
3918 // creates a mapping from a method descriptor to virtual methods
3919 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3920 if (md.isStatic()) {
3921 setPossibleCallees.add(md);
3923 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3926 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
3927 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
3929 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
3930 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
3931 if ((!ssjava.isTrustMethod(calleemd))
3932 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))
3933 && (!calleemd.getModifiers().isNative())) {
3934 if (!visited.contains(calleemd)) {
3935 temp_toanalyzeMethodList.add(calleemd);
3937 reachableCallee.add(calleemd);
3938 needToAnalyzeCalleeSet.add(calleemd);
3942 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
3951 return ssjava.topologicalSort(toSort);
3955 public boolean isTransitivelyCalledFrom(MethodDescriptor callee, MethodDescriptor caller) {
3956 // if the callee is transitively invoked from the caller
3959 int callerIdx = toanalyze_methodDescList.indexOf(caller);
3960 int calleeIdx = toanalyze_methodDescList.indexOf(callee);
3962 if (callerIdx < calleeIdx) {
3970 public void constructFlowGraph() {
3972 System.out.println("");
3973 toanalyze_methodDescList = computeMethodList();
3975 LinkedList<MethodDescriptor> methodDescList =
3976 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3978 System.out.println("@@@methodDescList=" + methodDescList);
3981 while (!methodDescList.isEmpty()) {
3982 MethodDescriptor md = methodDescList.removeLast();
3983 if (state.SSJAVADEBUG) {
3984 System.out.println();
3985 System.out.println("SSJAVA: Constructing a flow graph: " + md);
3987 // creates a mapping from a parameter descriptor to its index
3988 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
3990 if (!md.isStatic()) {
3992 mapParamDescToIdx.put(md.getThis(), 0);
3995 for (int i = 0; i < md.numParameters(); i++) {
3996 Descriptor paramDesc = (Descriptor) md.getParameter(i);
3997 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
4000 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
4001 mapMethodDescriptorToFlowGraph.put(md, fg);
4003 analyzeMethodBody(md.getClassDesc(), md);
4005 // System.out.println("##constructSubGlobalFlowGraph");
4006 // GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(fg);
4007 // mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
4010 // System.out.println("##addValueFlowsFromCalleeSubGlobalFlowGraph");
4011 // addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
4012 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
4014 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
4017 // _debug_printGraph();
4021 private void constructGlobalFlowGraph() {
4022 LinkedList<MethodDescriptor> methodDescList =
4023 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
4025 while (!methodDescList.isEmpty()) {
4026 MethodDescriptor md = methodDescList.removeLast();
4027 if (state.SSJAVADEBUG) {
4028 System.out.println();
4029 System.out.println("SSJAVA: Constructing a sub global flow graph: " + md);
4031 constructSubGlobalFlowGraph(getFlowGraph(md));
4034 System.out.println("-add Value Flows From CalleeSubGlobalFlowGraph");
4035 addValueFlowsFromCalleeSubGlobalFlowGraph(md);
4036 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
4038 // System.out.println("-propagate Flows From Callees With No CompositeLocation");
4039 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
4041 // mark if a parameter has incoming flows
4042 checkParamNodesInSubGlobalFlowGraph(md);
4048 private void checkParamNodesInSubGlobalFlowGraph(MethodDescriptor md) {
4049 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4050 FlowGraph flowGraph = getFlowGraph(md);
4052 Set<FlowNode> paramFlowNodeSet = flowGraph.getParamFlowNodeSet();
4053 for (Iterator iterator = paramFlowNodeSet.iterator(); iterator.hasNext();) {
4054 FlowNode paramFlowNode = (FlowNode) iterator.next();
4055 System.out.println("paramFlowNode=" + paramFlowNode);
4056 NTuple<Descriptor> paramDescTuple = paramFlowNode.getDescTuple();
4057 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
4058 GlobalFlowNode paramGlobalNode = globalFlowGraph.getFlowNode(paramLocTuple);
4060 Set<GlobalFlowNode> incomingNodeSet =
4061 globalFlowGraph.getIncomingNodeSetByPrefix(paramLocTuple.get(0));
4063 if (incomingNodeSet.size() > 0) {
4064 paramGlobalNode.setParamNodeWithIncomingFlows(true);
4070 private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
4071 if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
4072 mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
4074 return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
4077 private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
4079 // the transformation for a call site propagates flows through parameters
4080 // if the method is virtual, it also grab all relations from any possible
4083 Set<MethodInvokeNode> setMethodInvokeNode =
4084 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
4086 if (setMethodInvokeNode != null) {
4088 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
4089 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
4090 MethodDescriptor mdCallee = min.getMethod();
4091 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
4092 if (mdCallee.isStatic()) {
4093 setPossibleCallees.add(mdCallee);
4095 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
4096 // removes method descriptors that are not invoked by the caller
4097 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
4098 setPossibleCallees.addAll(calleeSet);
4101 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
4102 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
4103 propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
4111 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
4112 BlockNode bn = state.getMethodBody(md);
4113 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
4114 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
4117 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
4118 NodeTupleSet implicitFlowTupleSet) {
4120 bn.getVarTable().setParent(nametable);
4121 for (int i = 0; i < bn.size(); i++) {
4122 BlockStatementNode bsn = bn.get(i);
4123 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
4128 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
4129 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
4131 switch (bsn.kind()) {
4132 case Kind.BlockExpressionNode:
4133 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
4136 case Kind.DeclarationNode:
4137 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
4140 case Kind.IfStatementNode:
4141 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
4145 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
4148 case Kind.ReturnNode:
4149 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
4152 case Kind.SubBlockNode:
4153 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
4156 case Kind.ContinueBreakNode:
4159 case Kind.SwitchStatementNode:
4160 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
4167 private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
4168 SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
4170 analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
4174 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
4175 SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
4177 NodeTupleSet condTupleNode = new NodeTupleSet();
4178 analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
4179 implicitFlowTupleSet, false);
4181 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4183 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4184 newImplicitTupleSet.addTupleSet(condTupleNode);
4186 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4187 // need to create an intermediate node for the GLB of conditional
4188 // locations & implicit flows
4189 System.out.println("10");
4191 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4192 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4193 NTuple<Descriptor> tuple = idxIter.next();
4194 addFlowGraphEdge(md, tuple, interTuple);
4196 newImplicitTupleSet.clear();
4197 newImplicitTupleSet.addTuple(interTuple);
4200 BlockNode sbn = ssn.getSwitchBody();
4201 for (int i = 0; i < sbn.size(); i++) {
4202 analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
4207 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
4208 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
4209 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
4212 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
4213 NodeTupleSet implicitFlowTupleSet) {
4215 // System.out.println("-analyzeFlowReturnNode=" + rn.printNode(0));
4216 ExpressionNode returnExp = rn.getReturnExpression();
4218 if (returnExp != null) {
4219 NodeTupleSet nodeSet = new NodeTupleSet();
4220 // if a return expression returns a literal value, nodeSet is empty
4221 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
4222 FlowGraph fg = getFlowGraph(md);
4224 // if (implicitFlowTupleSet.size() == 1
4226 // fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate())
4229 // // since there is already an intermediate node for the GLB of implicit
4231 // // we don't need to create another intermediate node.
4232 // // just re-use the intermediate node for implicit flows.
4234 // FlowNode meetNode =
4235 // fg.getFlowNode(implicitFlowTupleSet.iterator().next());
4237 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
4238 // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>)
4240 // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
4245 NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
4247 // add tuples from return node
4248 currentFlowTupleSet.addTupleSet(nodeSet);
4250 // add tuples corresponding to the current implicit flows
4251 currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
4253 // System.out.println("---currentFlowTupleSet=" + currentFlowTupleSet);
4255 if (needToGenerateInterLoc(currentFlowTupleSet)) {
4256 System.out.println("9");
4258 FlowNode meetNode = fg.createIntermediateNode();
4259 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
4260 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
4261 fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
4263 fg.addReturnFlowNode(meetNode.getDescTuple());
4265 // currentFlowTupleSet = removeLiteralTuple(currentFlowTupleSet);
4266 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
4267 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
4268 fg.addReturnFlowNode(currentFlowTuple);
4276 private NodeTupleSet removeLiteralTuple(NodeTupleSet inSet) {
4277 NodeTupleSet tupleSet = new NodeTupleSet();
4278 for (Iterator<NTuple<Descriptor>> iter = inSet.iterator(); iter.hasNext();) {
4279 NTuple<Descriptor> tuple = iter.next();
4280 if (!tuple.get(0).equals(LITERALDESC)) {
4281 tupleSet.addTuple(tuple);
4287 private boolean needToGenerateInterLoc(NodeTupleSet tupleSet) {
4289 for (Iterator<NTuple<Descriptor>> iter = tupleSet.iterator(); iter.hasNext();) {
4290 NTuple<Descriptor> descTuple = iter.next();
4291 if (!descTuple.get(0).equals(LITERALDESC)) {
4296 System.out.println("needToGenerateInterLoc=" + tupleSet + " size=" + size);
4303 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
4304 NodeTupleSet implicitFlowTupleSet) {
4306 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
4308 NodeTupleSet condTupleNode = new NodeTupleSet();
4309 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
4310 implicitFlowTupleSet, false);
4312 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4314 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4315 newImplicitTupleSet.addTupleSet(condTupleNode);
4317 newImplicitTupleSet.addGlobalFlowTupleSet(implicitFlowTupleSet.getGlobalLocTupleSet());
4318 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
4320 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4321 // need to create an intermediate node for the GLB of conditional
4322 // locations & implicit flows
4323 System.out.println("6");
4325 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4326 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
4328 NTuple<Descriptor> tuple = idxIter.next();
4329 addFlowGraphEdge(md, tuple, interTuple);
4331 newImplicitTupleSet.clear();
4332 newImplicitTupleSet.addTuple(interTuple);
4337 // System.out.println("condTupleNode="+condTupleNode);
4338 // NTuple<Descriptor> interTuple =
4339 // getFlowGraph(md).createIntermediateNode().getDescTuple();
4341 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator();
4342 // idxIter.hasNext();) {
4343 // NTuple<Descriptor> tuple = idxIter.next();
4344 // addFlowGraphEdge(md, tuple, interTuple);
4347 // for (Iterator<NTuple<Descriptor>> idxIter =
4348 // implicitFlowTupleSet.iterator(); idxIter
4350 // NTuple<Descriptor> tuple = idxIter.next();
4351 // addFlowGraphEdge(md, tuple, interTuple);
4354 // NodeTupleSet newImplicitSet = new NodeTupleSet();
4355 // newImplicitSet.addTuple(interTuple);
4356 analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
4359 // condTupleNode.addTupleSet(implicitFlowTupleSet);
4361 // add edges from condNodeTupleSet to all nodes of conditional nodes
4362 // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
4365 // check 'for loop' case
4366 BlockNode bn = ln.getInitializer();
4367 bn.getVarTable().setParent(nametable);
4368 for (int i = 0; i < bn.size(); i++) {
4369 BlockStatementNode bsn = bn.get(i);
4370 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
4373 NodeTupleSet condTupleNode = new NodeTupleSet();
4374 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
4375 implicitFlowTupleSet, false);
4377 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4378 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4379 newImplicitTupleSet.addTupleSet(condTupleNode);
4381 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4382 // need to create an intermediate node for the GLB of conditional
4383 // locations & implicit flows
4384 System.out.println("7");
4386 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4387 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
4389 NTuple<Descriptor> tuple = idxIter.next();
4390 addFlowGraphEdge(md, tuple, interTuple);
4392 newImplicitTupleSet.clear();
4393 newImplicitTupleSet.addTuple(interTuple);
4398 // NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4400 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
4401 // NTuple<Descriptor> tuple = idxIter.next();
4402 // addFlowGraphEdge(md, tuple, interTuple);
4405 // for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
4407 // NTuple<Descriptor> tuple = idxIter.next();
4408 // addFlowGraphEdge(md, tuple, interTuple);
4411 // NodeTupleSet newImplicitSet = new NodeTupleSet();
4412 // newImplicitSet.addTuple(interTuple);
4413 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitTupleSet);
4414 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitTupleSet);
4417 // condTupleNode.addTupleSet(implicitFlowTupleSet);
4419 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
4421 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
4428 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
4429 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
4431 // System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
4433 NodeTupleSet condTupleNode = new NodeTupleSet();
4434 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
4435 implicitFlowTupleSet, false);
4437 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4439 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4440 newImplicitTupleSet.addTupleSet(condTupleNode);
4442 // System.out.println("$$$GGGcondTupleNode=" + condTupleNode.getGlobalLocTupleSet());
4443 // System.out.println("-condTupleNode=" + condTupleNode);
4444 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4445 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
4447 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4448 System.out.println("5");
4450 // need to create an intermediate node for the GLB of conditional locations & implicit flows
4451 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4452 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4453 NTuple<Descriptor> tuple = idxIter.next();
4454 addFlowGraphEdge(md, tuple, interTuple);
4456 newImplicitTupleSet.clear();
4457 newImplicitTupleSet.addTuple(interTuple);
4460 // GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4461 // for (Iterator<NTuple<Location>> iterator = condTupleNode.globalIterator();
4462 // iterator.hasNext();) {
4463 // NTuple<Location> calleeReturnLocTuple = iterator.next();
4464 // for (Iterator<NTuple<Descriptor>> iter2 = newImplicitTupleSet.iterator(); iter2.hasNext();) {
4465 // NTuple<Descriptor> callerImplicitTuple = iter2.next();
4466 // globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
4467 // translateToLocTuple(md, callerImplicitTuple));
4470 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
4472 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
4474 if (isn.getFalseBlock() != null) {
4475 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
4480 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
4481 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
4483 VarDescriptor vd = dn.getVarDescriptor();
4484 mapDescToDefinitionLine.put(vd, dn.getNumLine());
4485 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
4487 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
4488 fn.setDeclarationNode();
4490 if (dn.getExpression() != null) {
4492 NodeTupleSet nodeSetRHS = new NodeTupleSet();
4493 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
4494 implicitFlowTupleSet, false);
4496 // creates edges from RHS to LHS
4497 NTuple<Descriptor> interTuple = null;
4498 if (needToGenerateInterLoc(nodeSetRHS)) {
4499 System.out.println("3");
4500 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4503 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
4504 NTuple<Descriptor> fromTuple = iter.next();
4505 System.out.println("fromTuple=" + fromTuple + " interTuple=" + interTuple + " tupleLSH="
4507 addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
4510 // creates edges from implicitFlowTupleSet to LHS
4511 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4512 NTuple<Descriptor> implicitTuple = iter.next();
4513 addFlowGraphEdge(md, implicitTuple, tupleLHS);
4516 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4517 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
4518 NTuple<Location> calleeReturnLocTuple = iterator.next();
4519 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, tupleLHS));
4526 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
4527 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
4528 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
4532 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
4533 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
4534 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
4537 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
4538 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4539 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
4541 // note that expression node can create more than one flow node
4542 // nodeSet contains of flow nodes
4543 // base is always assigned to null except the case of a name node!
4544 NTuple<Descriptor> flowTuple;
4545 switch (en.kind()) {
4547 case Kind.AssignmentNode:
4548 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
4549 implicitFlowTupleSet);
4552 case Kind.FieldAccessNode:
4554 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
4555 implicitFlowTupleSet, isLHS);
4556 if (flowTuple != null) {
4557 nodeSet.addTuple(flowTuple);
4562 NodeTupleSet nameNodeSet = new NodeTupleSet();
4564 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
4565 if (flowTuple != null) {
4566 nodeSet.addTuple(flowTuple);
4571 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
4574 case Kind.CreateObjectNode:
4575 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
4578 case Kind.ArrayAccessNode:
4579 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
4582 case Kind.LiteralNode:
4583 analyzeFlowLiteralNode(md, nametable, (LiteralNode) en, nodeSet);
4586 case Kind.MethodInvokeNode:
4587 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
4588 implicitFlowTupleSet);
4591 case Kind.TertiaryNode:
4592 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
4596 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
4598 // case Kind.InstanceOfNode:
4599 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
4602 // case Kind.ArrayInitializerNode:
4603 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
4607 // case Kind.ClassTypeNode:
4608 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
4611 // case Kind.OffsetNode:
4612 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
4621 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
4622 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
4624 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
4625 implicitFlowTupleSet, false);
4629 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
4630 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4632 System.out.println("analyzeFlowTertiaryNode=" + tn.printNode(0));
4634 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
4635 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
4636 implicitFlowTupleSet, false);
4638 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4639 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4640 newImplicitTupleSet.addTupleSet(tertiaryTupleNode);
4642 // System.out.println("$$$GGGcondTupleNode=" + tertiaryTupleNode.getGlobalLocTupleSet());
4643 // System.out.println("-tertiaryTupleNode=" + tertiaryTupleNode);
4644 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4645 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
4647 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4648 System.out.println("15");
4649 // need to create an intermediate node for the GLB of conditional locations & implicit flows
4650 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4651 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4652 NTuple<Descriptor> tuple = idxIter.next();
4653 addFlowGraphEdge(md, tuple, interTuple);
4655 newImplicitTupleSet.clear();
4656 newImplicitTupleSet.addTuple(interTuple);
4659 newImplicitTupleSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
4661 System.out.println("---------newImplicitTupleSet=" + newImplicitTupleSet);
4662 // add edges from tertiaryTupleNode to all nodes of conditional nodes
4663 // tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
4664 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
4665 newImplicitTupleSet, false);
4667 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
4668 newImplicitTupleSet, false);
4670 nodeSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
4671 nodeSet.addTupleSet(tertiaryTupleNode);
4673 System.out.println("#tertiary node set=" + nodeSet);
4676 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
4677 MethodInvokeNode min) {
4678 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
4680 set = new HashSet<MethodInvokeNode>();
4681 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
4686 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
4688 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4689 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4691 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
4694 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
4696 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4697 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4700 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
4703 private Set<NTuple<Location>> getPCLocTupleSet(MethodInvokeNode min) {
4704 if (!mapMethodInvokeNodeToPCLocTupleSet.containsKey(min)) {
4705 mapMethodInvokeNodeToPCLocTupleSet.put(min, new HashSet<NTuple<Location>>());
4707 return mapMethodInvokeNodeToPCLocTupleSet.get(min);
4710 private void analyzeFlowMethodInvokeNode(MethodDescriptor mdCaller, SymbolTable nametable,
4711 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4713 System.out.println("analyzeFlowMethodInvokeNode=" + min.printNode(0));
4715 if (!toanalyze_methodDescList.contains(min.getMethod())) {
4719 addMapMethodDescToMethodInvokeNodeSet(min);
4721 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
4722 for (Iterator iterator = implicitFlowTupleSet.iterator(); iterator.hasNext();) {
4723 NTuple<Descriptor> pcDescTuple = (NTuple<Descriptor>) iterator.next();
4724 if (!pcDescTuple.get(0).equals(LITERALDESC)) {
4725 // here we don't need to add the literal value as a PC location
4726 pcLocTupleSet.add(translateToLocTuple(mdCaller, pcDescTuple));
4730 mapMethodInvokeNodeToArgIdxMap.put(min, new HashMap<Integer, NTuple<Descriptor>>());
4732 if (nodeSet == null) {
4733 nodeSet = new NodeTupleSet();
4736 MethodDescriptor mdCallee = min.getMethod();
4738 NameDescriptor baseName = min.getBaseName();
4739 boolean isSystemout = false;
4740 if (baseName != null) {
4741 isSystemout = baseName.getSymbol().equals("System.out");
4744 if (!ssjava.isSSJavaUtil(mdCallee.getClassDesc()) && !ssjava.isTrustMethod(mdCallee)
4747 addMapCallerMethodDescToMethodInvokeNodeSet(mdCaller, min);
4749 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
4750 System.out.println("mdCallee=" + mdCallee);
4751 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
4753 System.out.println("---calleeReturnSet=" + calleeReturnSet);
4755 NodeTupleSet tupleSet = new NodeTupleSet();
4757 if (min.getExpression() != null) {
4759 NodeTupleSet baseNodeSet = new NodeTupleSet();
4760 analyzeFlowExpressionNode(mdCaller, nametable, min.getExpression(), baseNodeSet, null,
4761 implicitFlowTupleSet, false);
4763 assert (baseNodeSet.size() == 1);
4764 NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
4765 mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
4767 if (!min.getMethod().isStatic()) {
4768 addArgIdxMap(min, 0, baseTuple);
4770 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
4771 FlowNode returnNode = (FlowNode) iterator.next();
4772 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
4773 if (returnDescTuple.startsWith(mdCallee.getThis())) {
4774 // the location type of the return value is started with 'this'
4776 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
4777 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
4778 // nodeSet.addTuple(inFlowTuple);
4779 tupleSet.addTuple(inFlowTuple);
4782 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
4783 // System.out.println("inFlowSet=" + inFlowSet + " from retrunNode=" + returnNode);
4784 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
4785 FlowNode inFlowNode = (FlowNode) iterator2.next();
4786 if (inFlowNode.getDescTuple().startsWith(mdCallee.getThis())) {
4787 // nodeSet.addTupleSet(baseNodeSet);
4788 tupleSet.addTupleSet(baseNodeSet);
4797 // analyze parameter flows
4799 if (min.numArgs() > 0) {
4802 if (min.getMethod().isStatic()) {
4808 for (int i = 0; i < min.numArgs(); i++) {
4809 ExpressionNode en = min.getArg(i);
4810 int idx = i + offset;
4811 NodeTupleSet argTupleSet = new NodeTupleSet();
4812 analyzeFlowExpressionNode(mdCaller, nametable, en, argTupleSet, false);
4813 // if argument is liternal node, argTuple is set to NULL
4814 System.out.println("argTupleSet=" + argTupleSet);
4815 NTuple<Descriptor> argTuple = generateArgTuple(mdCaller, argTupleSet);
4817 // if an argument is literal value,
4818 // we need to create an itermediate node so that we could assign a composite location to
4819 // that node if needed
4820 if (argTuple.size() > 0
4821 && (argTuple.get(0).equals(GLOBALDESC) || argTuple.get(0).equals(LITERALDESC))) {
4823 * System.out.println("***GLOBAL ARG TUPLE CASE=" + argTuple); System.out.println("8");
4825 * NTuple<Descriptor> interTuple =
4826 * getFlowGraph(mdCaller).createIntermediateNode().getDescTuple(); ((InterDescriptor)
4827 * interTuple.get(0)).setMethodArgIdxPair(min, idx); addFlowGraphEdge(mdCaller,
4828 * argTuple, interTuple); argTuple = interTuple; addArgIdxMap(min, idx, argTuple);
4829 * System.out.println("new min mapping i=" + idx + " ->" + argTuple);
4831 argTuple = new NTuple<Descriptor>();
4834 addArgIdxMap(min, idx, argTuple);
4836 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
4838 // check whether a param node in the callee graph has incoming flows
4839 // if it has incoming flows, the corresponding arg should be lower than the current PC
4840 // Descriptor prefix = paramNode.getDescTuple().get(0);
4841 // if (calleeFlowGraph.getIncomingNodeSetByPrefix(prefix).size() > 0) {
4842 // for (Iterator<NTuple<Descriptor>> iterator = implicitFlowTupleSet.iterator(); iterator
4844 // NTuple<Descriptor> pcTuple = iterator.next();
4845 // System.out.println("add edge pcTuple =" + pcTuple + " -> " + argTuple);
4846 // addFlowGraphEdge(md, pcTuple, argTuple);
4850 System.out.println("paramNode=" + paramNode + " calleeReturnSet=" + calleeReturnSet);
4851 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
4852 || mdCallee.getModifiers().isNative()) {
4853 addParamNodeFlowingToReturnValue(mdCallee, paramNode);
4854 // nodeSet.addTupleSet(argTupleSet);
4855 tupleSet.addTupleSet(argTupleSet);
4861 if (mdCallee.getReturnType() != null && !mdCallee.getReturnType().isVoid()) {
4862 FlowReturnNode setNode = getFlowGraph(mdCaller).createReturnNode(min);
4863 System.out.println("ADD TUPLESET=" + tupleSet + " to returnnode=" + setNode);
4864 setNode.addTupleSet(tupleSet);
4865 nodeSet.addTuple(setNode.getDescTuple());
4868 // propagateFlowsFromCallee(min, md, min.getMethod());
4870 // when generating the global flow graph,
4871 // we need to add ordering relations from the set of callee return loc tuple to LHS of the
4872 // caller assignment
4873 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
4874 FlowNode calleeReturnNode = (FlowNode) iterator.next();
4875 NTuple<Location> calleeReturnLocTuple =
4876 translateToLocTuple(mdCallee, calleeReturnNode.getDescTuple());
4877 System.out.println("calleeReturnLocTuple=" + calleeReturnLocTuple);
4878 nodeSet.addGlobalFlowTuple(translateToCallerLocTuple(min, mdCallee, mdCaller,
4879 calleeReturnLocTuple));
4882 System.out.println("min nodeSet=" + nodeSet);
4888 private NTuple<Descriptor> generateArgTuple(MethodDescriptor mdCaller, NodeTupleSet argTupleSet) {
4892 // if argTupleSet is empty, it comes from the top location
4893 if (argTupleSet.size() == 0) {
4894 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
4895 descTuple.add(LITERALDESC);
4899 Set<NTuple<Descriptor>> argTupleSetNonLiteral = new HashSet<NTuple<Descriptor>>();
4901 for (Iterator<NTuple<Descriptor>> iter = argTupleSet.iterator(); iter.hasNext();) {
4902 NTuple<Descriptor> descTuple = iter.next();
4903 if (!descTuple.get(0).equals(LITERALDESC)) {
4904 argTupleSetNonLiteral.add(descTuple);
4908 if (argTupleSetNonLiteral.size() > 1) {
4909 System.out.println("11");
4911 NTuple<Descriptor> interTuple =
4912 getFlowGraph(mdCaller).createIntermediateNode().getDescTuple();
4913 for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
4914 NTuple<Descriptor> tuple = idxIter.next();
4915 addFlowGraphEdge(mdCaller, tuple, interTuple);
4918 } else if (argTupleSetNonLiteral.size() == 1) {
4919 return argTupleSetNonLiteral.iterator().next();
4921 return argTupleSet.iterator().next();
4926 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
4927 // return true if inNode has in-flows to nodeSet
4929 if (nodeSet.contains(inNode)) {
4930 // in this case, the method directly returns a parameter variable.
4933 // Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
4934 Set<FlowNode> reachableSet = fg.getReachableSetFrom(inNode.getDescTuple());
4935 System.out.println("inNode=" + inNode + " reachalbeSet=" + reachableSet);
4937 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
4938 FlowNode fn = (FlowNode) iterator.next();
4939 if (nodeSet.contains(fn)) {
4946 private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
4947 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
4950 private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
4954 Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
4955 if (mapIdxToTuple == null) {
4956 mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
4957 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
4959 mapIdxToTuple.put(new Integer(idx), argTuple);
4962 private void analyzeFlowLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en,
4963 NodeTupleSet nodeSet) {
4964 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
4965 tuple.add(LITERALDESC);
4966 nodeSet.addTuple(tuple);
4969 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
4970 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
4972 // System.out.println("analyzeFlowArrayAccessNode aan=" + aan.printNode(0));
4973 String currentArrayAccessNodeExpStr = aan.printNode(0);
4974 arrayAccessNodeStack.push(aan.printNode(0));
4976 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
4977 NTuple<Descriptor> base =
4978 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
4980 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
4981 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
4983 arrayAccessNodeStack.pop();
4986 // need to create an edge from idx to array
4987 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
4988 NTuple<Descriptor> idxTuple = idxIter.next();
4989 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
4990 NTuple<Descriptor> arrTuple = arrIter.next();
4991 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
4995 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4996 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
4998 NTuple<Location> calleeReturnLocTuple = iterator.next();
4999 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
5000 NTuple<Descriptor> arrTuple = arrIter.next();
5001 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, arrTuple));
5005 nodeSet.addTupleSet(expNodeTupleSet);
5008 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
5010 nodeSetArrayAccessExp.addTupleSet(expNodeTupleSet);
5011 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
5013 if (arrayAccessNodeStack.isEmpty()
5014 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
5016 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
5017 System.out.println("1");
5018 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5020 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter.hasNext();) {
5021 NTuple<Descriptor> higherTuple = iter.next();
5022 addFlowGraphEdge(md, higherTuple, interTuple);
5024 nodeSetArrayAccessExp.clear();
5025 nodeSetArrayAccessExp.addTuple(interTuple);
5029 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
5030 nodeSet.addTupleSet(nodeSetArrayAccessExp);
5036 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
5037 CreateObjectNode en) {
5038 // TODO Auto-generated method stub
5042 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
5043 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
5045 NodeTupleSet leftOpSet = new NodeTupleSet();
5046 NodeTupleSet rightOpSet = new NodeTupleSet();
5049 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
5052 if (on.getRight() != null) {
5054 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
5055 implicitFlowTupleSet, false);
5058 Operation op = on.getOp();
5060 switch (op.getOp()) {
5062 case Operation.UNARYPLUS:
5063 case Operation.UNARYMINUS:
5064 case Operation.LOGIC_NOT:
5066 nodeSet.addTupleSet(leftOpSet);
5069 case Operation.LOGIC_OR:
5070 case Operation.LOGIC_AND:
5071 case Operation.COMP:
5072 case Operation.BIT_OR:
5073 case Operation.BIT_XOR:
5074 case Operation.BIT_AND:
5075 case Operation.ISAVAILABLE:
5076 case Operation.EQUAL:
5077 case Operation.NOTEQUAL:
5084 case Operation.MULT:
5087 case Operation.LEFTSHIFT:
5088 case Operation.RIGHTSHIFT:
5089 case Operation.URIGHTSHIFT:
5091 // there are two operands
5092 nodeSet.addTupleSet(leftOpSet);
5093 nodeSet.addTupleSet(rightOpSet);
5095 nodeSet.addGlobalFlowTupleSet(leftOpSet.getGlobalLocTupleSet());
5096 nodeSet.addGlobalFlowTupleSet(rightOpSet.getGlobalLocTupleSet());
5101 throw new Error(op.toString());
5106 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
5107 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
5110 base = new NTuple<Descriptor>();
5113 NameDescriptor nd = nn.getName();
5115 if (nd.getBase() != null) {
5117 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
5118 implicitFlowTupleSet, false);
5120 // base node has the top location
5124 String varname = nd.toString();
5125 if (varname.equals("this")) {
5127 base.add(md.getThis());
5131 Descriptor d = (Descriptor) nametable.get(varname);
5133 if (d instanceof VarDescriptor) {
5134 VarDescriptor vd = (VarDescriptor) d;
5136 } else if (d instanceof FieldDescriptor) {
5137 // the type of field descriptor has a location!
5138 FieldDescriptor fd = (FieldDescriptor) d;
5139 if (fd.isStatic()) {
5141 // if it is 'static final', no need to have flow node for the TOP
5145 // if 'static', assign the default GLOBAL LOCATION to the first
5146 // element of the tuple
5147 base.add(GLOBALDESC);
5150 // the location of field access starts from this, followed by field
5152 base.add(md.getThis());
5156 } else if (d == null) {
5157 // access static field
5158 base.add(GLOBALDESC);
5159 base.add(nn.getField());
5162 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
5164 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
5165 // String globalLocId = localLattice.getGlobalLoc();
5166 // if (globalLocId == null) {
5168 // Error("Method lattice does not define global variable location at "
5169 // + generateErrorMessage(md.getClassDesc(), nn));
5171 // loc.addLocation(new Location(md, globalLocId));
5173 // Location fieldLoc = (Location) fd.getType().getExtension();
5174 // loc.addLocation(fieldLoc);
5180 getFlowGraph(md).createNewFlowNode(base);
5186 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
5187 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
5188 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
5189 // System.out.println("analyzeFlowFieldAccessNode=" + fan.printNode(0));
5191 String currentArrayAccessNodeExpStr = null;
5192 ExpressionNode left = fan.getExpression();
5193 TypeDescriptor ltd = left.getType();
5194 FieldDescriptor fd = fan.getField();
5195 ArrayAccessNode aan = null;
5197 String varName = null;
5198 if (left.kind() == Kind.NameNode) {
5199 NameDescriptor nd = ((NameNode) left).getName();
5200 varName = nd.toString();
5203 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
5204 // using a class name directly or access using this
5205 if (fd.isStatic() && fd.isFinal()) {
5210 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
5212 boolean isArrayCase = false;
5213 if (left instanceof ArrayAccessNode) {
5216 aan = (ArrayAccessNode) left;
5218 currentArrayAccessNodeExpStr = aan.printNode(0);
5219 arrayAccessNodeStack.push(currentArrayAccessNodeExpStr);
5221 left = aan.getExpression();
5222 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
5223 implicitFlowTupleSet, isLHS);
5227 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
5230 // in this case, field is TOP location
5234 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
5236 if (!left.getType().isPrimitive()) {
5237 if (!fd.getSymbol().equals("length")) {
5238 // array.length access, just have the location of the array
5239 flowFieldTuple.add(fd);
5240 nodeSet.removeTuple(base);
5243 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
5246 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
5247 NTuple<Descriptor> idxTuple = idxIter.next();
5248 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
5251 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5252 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
5254 NTuple<Location> calleeReturnLocTuple = iterator.next();
5255 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5256 translateToLocTuple(md, flowFieldTuple));
5260 nodeSet.addTupleSet(idxNodeTupleSet);
5262 // if it is the array case and not the LHS case
5264 arrayAccessNodeStack.pop();
5266 if (arrayAccessNodeStack.isEmpty()
5267 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
5268 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
5270 nodeSetArrayAccessExp.addTuple(flowFieldTuple);
5271 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
5272 nodeSetArrayAccessExp.addTupleSet(nodeSet);
5274 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
5275 System.out.println("4");
5276 System.out.println("nodeSetArrayAccessExp=" + nodeSetArrayAccessExp);
5277 // System.out.println("idxNodeTupleSet.getGlobalLocTupleSet()="
5278 // + idxNodeTupleSet.getGlobalLocTupleSet());
5280 NTuple<Descriptor> interTuple =
5281 getFlowGraph(md).createIntermediateNode().getDescTuple();
5283 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter
5285 NTuple<Descriptor> higherTuple = iter.next();
5286 addFlowGraphEdge(md, higherTuple, interTuple);
5289 flowFieldTuple = interTuple;
5292 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
5298 return flowFieldTuple;
5303 private void debug_printTreeNode(TreeNode tn) {
5305 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
5309 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
5310 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
5311 NodeTupleSet implicitFlowTupleSet) {
5313 NodeTupleSet nodeSetRHS = new NodeTupleSet();
5314 NodeTupleSet nodeSetLHS = new NodeTupleSet();
5316 boolean postinc = true;
5317 if (an.getOperation().getBaseOp() == null
5318 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
5319 .getBaseOp().getOp() != Operation.POSTDEC)) {
5322 // if LHS is array access node, need to capture value flows between an array
5323 // and its index value
5324 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
5328 // analyze value flows of rhs expression
5329 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
5332 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
5333 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
5334 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
5335 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5336 // System.out.println("-");
5338 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
5339 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
5341 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
5342 NTuple<Descriptor> fromTuple = iter.next();
5343 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5344 NTuple<Descriptor> toTuple = iter2.next();
5345 addFlowGraphEdge(md, fromTuple, toTuple);
5350 // creates edges from RHS to LHS
5351 NTuple<Descriptor> interTuple = null;
5352 if (needToGenerateInterLoc(nodeSetRHS)) {
5353 System.out.println("2");
5354 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5357 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
5358 NTuple<Descriptor> fromTuple = iter.next();
5359 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5360 NTuple<Descriptor> toTuple = iter2.next();
5361 addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
5365 // creates edges from implicitFlowTupleSet to LHS
5366 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
5367 NTuple<Descriptor> fromTuple = iter.next();
5368 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5369 NTuple<Descriptor> toTuple = iter2.next();
5370 addFlowGraphEdge(md, fromTuple, toTuple);
5374 // create global flow edges if the callee gives return value flows to the caller
5375 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5376 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
5377 NTuple<Location> calleeReturnLocTuple = iterator.next();
5378 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5379 NTuple<Descriptor> callerLHSTuple = iter2.next();
5380 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5381 translateToLocTuple(md, callerLHSTuple));
5382 System.out.println("$$$ GLOBAL FLOW ADD=" + calleeReturnLocTuple + " -> "
5383 + translateToLocTuple(md, callerLHSTuple));
5387 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
5389 NTuple<Location> calleeReturnLocTuple = iterator.next();
5390 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5391 NTuple<Descriptor> callerLHSTuple = iter2.next();
5392 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5393 translateToLocTuple(md, callerLHSTuple));
5394 System.out.println("$$$ GLOBAL FLOW PCLOC ADD=" + calleeReturnLocTuple + " -> "
5395 + translateToLocTuple(md, callerLHSTuple));
5402 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5403 NTuple<Descriptor> tuple = iter2.next();
5404 addFlowGraphEdge(md, tuple, tuple);
5407 // creates edges from implicitFlowTupleSet to LHS
5408 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
5409 NTuple<Descriptor> fromTuple = iter.next();
5410 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5411 NTuple<Descriptor> toTuple = iter2.next();
5412 addFlowGraphEdge(md, fromTuple, toTuple);
5416 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5417 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
5419 NTuple<Location> calleeReturnLocTuple = iterator.next();
5420 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5421 NTuple<Descriptor> callerLHSTuple = iter2.next();
5422 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5423 translateToLocTuple(md, callerLHSTuple));
5424 System.out.println("$$$ GLOBAL FLOW PC ADD=" + calleeReturnLocTuple + " -> "
5425 + translateToLocTuple(md, callerLHSTuple));
5431 if (nodeSet != null) {
5432 nodeSet.addTupleSet(nodeSetLHS);
5433 nodeSet.addGlobalFlowTupleSet(nodeSetLHS.getGlobalLocTupleSet());
5437 public FlowGraph getFlowGraph(MethodDescriptor md) {
5438 return mapMethodDescriptorToFlowGraph.get(md);
5441 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
5442 NTuple<Descriptor> to) {
5443 FlowGraph graph = getFlowGraph(md);
5444 graph.addValueFlowEdge(from, to);
5448 private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
5449 NTuple<Descriptor> inter, NTuple<Descriptor> to) {
5451 FlowGraph graph = getFlowGraph(md);
5453 if (inter != null) {
5454 graph.addValueFlowEdge(from, inter);
5455 graph.addValueFlowEdge(inter, to);
5457 graph.addValueFlowEdge(from, to);
5462 public void writeInferredLatticeDotFile(ClassDescriptor cd, HierarchyGraph simpleHierarchyGraph,
5463 SSJavaLattice<String> locOrder, String nameSuffix) {
5464 System.out.println("@cd=" + cd);
5465 System.out.println("@sharedLoc=" + locOrder.getSharedLocSet());
5466 writeInferredLatticeDotFile(cd, null, simpleHierarchyGraph, locOrder, nameSuffix);
5469 public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
5470 HierarchyGraph simpleHierarchyGraph, SSJavaLattice<String> locOrder, String nameSuffix) {
5472 String fileName = "lattice_";
5475 /* cd.getSymbol().replaceAll("[\\W_]", "") + "_" + */md.toString().replaceAll("[\\W_]", "");
5477 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
5480 fileName += nameSuffix;
5482 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
5484 Set<String> addedLocSet = new HashSet<String>();
5486 if (pairSet.size() > 0) {
5488 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
5490 bw.write("digraph " + fileName + " {\n");
5492 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
5493 // pair is in the form of <higher, lower>
5494 Pair<String, String> pair = (Pair<String, String>) iterator.next();
5496 String highLocId = pair.getFirst();
5497 String lowLocId = pair.getSecond();
5498 if (!addedLocSet.contains(highLocId)) {
5499 addedLocSet.add(highLocId);
5500 drawNode(bw, locOrder, simpleHierarchyGraph, highLocId);
5503 if (!addedLocSet.contains(lowLocId)) {
5504 addedLocSet.add(lowLocId);
5505 drawNode(bw, locOrder, simpleHierarchyGraph, lowLocId);
5508 bw.write(highLocId + " -> " + lowLocId + ";\n");
5513 } catch (IOException e) {
5514 e.printStackTrace();
5521 private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
5523 for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
5524 HNode merged = (HNode) iterator.next();
5525 if (merged.isMergeNode()) {
5526 str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
5528 str += " " + merged.getName();
5534 private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, HierarchyGraph graph,
5535 String locName) throws IOException {
5538 if (lattice.isSharedLoc(locName)) {
5539 prettyStr = locName + "*";
5541 prettyStr = locName;
5543 // HNode node = graph.getHNode(locName);
5544 // if (node != null && node.isMergeNode()) {
5545 // Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
5546 // prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
5548 bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
5551 public void _debug_writeFlowGraph() {
5552 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
5554 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
5555 MethodDescriptor md = (MethodDescriptor) iterator.next();
5556 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
5557 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
5560 subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
5561 } catch (IOException e) {
5562 e.printStackTrace();
5570 class CyclicFlowException extends Exception {
5574 class InterDescriptor extends Descriptor {
5576 Pair<MethodInvokeNode, Integer> minArgIdxPair;
5578 public InterDescriptor(String name) {
5582 public void setMethodArgIdxPair(MethodInvokeNode min, int idx) {
5583 minArgIdxPair = new Pair<MethodInvokeNode, Integer>(min, new Integer(idx));
5586 public Pair<MethodInvokeNode, Integer> getMethodArgIdxPair() {
5587 return minArgIdxPair;