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);
373 // calculateReturnHolderLocation();
376 private void calculateReturnHolderLocation() {
377 LinkedList<MethodDescriptor> methodDescList =
378 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
380 while (!methodDescList.isEmpty()) {
381 MethodDescriptor md = methodDescList.removeLast();
383 FlowGraph fg = getFlowGraph(md);
384 Set<FlowNode> nodeSet = fg.getNodeSet();
385 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
386 FlowNode flowNode = (FlowNode) iterator.next();
387 if (flowNode.isFromHolder()) {
388 calculateCompositeLocationFromFlowGraph(md, flowNode);
395 private void updateCompositeLocationAssignments() {
397 LinkedList<MethodDescriptor> methodDescList =
398 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
400 while (!methodDescList.isEmpty()) {
401 MethodDescriptor md = methodDescList.removeLast();
403 System.out.println("\n#updateCompositeLocationAssignments=" + md);
405 FlowGraph flowGraph = getFlowGraph(md);
407 MethodSummary methodSummary = getMethodSummary(md);
409 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
410 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
411 FlowNode node = (FlowNode) iterator.next();
412 System.out.println("-node=" + node + " node.getDescTuple=" + node.getDescTuple());
413 if (node.getCompositeLocation() != null) {
414 CompositeLocation compLoc = node.getCompositeLocation();
415 CompositeLocation updatedCompLoc = updateCompositeLocation(compLoc);
416 node.setCompositeLocation(updatedCompLoc);
417 System.out.println("---updatedCompLoc1=" + updatedCompLoc);
419 NTuple<Descriptor> descTuple = node.getDescTuple();
420 System.out.println("update desc=" + descTuple);
421 CompositeLocation compLoc = convertToCompositeLocation(md, descTuple);
422 compLoc = updateCompositeLocation(compLoc);
423 node.setCompositeLocation(compLoc);
424 System.out.println("---updatedCompLoc2=" + compLoc);
427 if (node.isDeclaratonNode()) {
428 Descriptor localVarDesc = node.getDescTuple().get(0);
429 CompositeLocation compLoc = updateCompositeLocation(node.getCompositeLocation());
430 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, compLoc);
434 // update PCLOC and RETURNLOC if they have a composite location assignment
435 if (methodSummary.getRETURNLoc() != null) {
436 methodSummary.setRETURNLoc(updateCompositeLocation(methodSummary.getRETURNLoc()));
438 if (methodSummary.getPCLoc() != null) {
439 methodSummary.setPCLoc(updateCompositeLocation(methodSummary.getPCLoc()));
446 private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
447 CompositeLocation updatedCompLoc = new CompositeLocation();
448 for (int i = 0; i < compLoc.getSize(); i++) {
449 Location loc = compLoc.get(i);
450 String nodeIdentifier = loc.getLocIdentifier();
451 Descriptor enclosingDesc = loc.getDescriptor();
453 if (!enclosingDesc.equals(GLOBALDESC)) {
454 LocationSummary locSummary = getLocationSummary(enclosingDesc);
455 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(enclosingDesc);
456 if (scGraph != null) {
457 HNode curNode = scGraph.getCurrentHNode(nodeIdentifier);
458 if (curNode != null) {
459 nodeIdentifier = curNode.getName();
462 locName = locSummary.getLocationName(nodeIdentifier);
464 locName = nodeIdentifier;
466 Location updatedLoc = new Location(enclosingDesc, locName);
467 updatedCompLoc.addLocation(updatedLoc);
470 return updatedCompLoc;
473 private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
475 System.out.println("\n\n###translateCompositeLocationAssignmentToFlowGraph mdCaller="
478 // First, assign a composite location to a node in the flow graph
479 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
481 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
482 Map<Location, CompositeLocation> callerMapLocToCompLoc =
483 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
485 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
486 for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
487 Location methodLoc = (Location) iterator.next();
488 if (methodLoc.getDescriptor().equals(mdCaller)) {
489 CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
490 assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
494 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
496 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
497 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
498 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
499 // need to translate a composite location that is started with the base
501 translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
502 MethodDescriptor mdCallee = min.getMethod();
503 calleeSet.add(mdCallee);
507 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
508 MethodDescriptor callee = (MethodDescriptor) iterator.next();
509 translateCompositeLocationAssignmentToFlowGraph(callee);
514 private CompositeLocation translateArgCompLocToParamCompLoc(MethodInvokeNode min,
515 CompositeLocation argCompLoc) {
517 System.out.println("--------translateArgCompLocToParamCompLoc argCompLoc=" + argCompLoc);
518 MethodDescriptor mdCallee = min.getMethod();
519 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
521 NTuple<Location> argLocTuple = argCompLoc.getTuple();
522 Location argLocalLoc = argLocTuple.get(0);
524 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
525 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
526 for (Iterator iterator2 = idxSet.iterator(); iterator2.hasNext();) {
527 Integer idx = (Integer) iterator2.next();
529 if (idx == 0 && !min.getMethod().isStatic()) {
533 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
534 if (argTuple.size() > 0 && argTuple.get(0).equals(argLocalLoc.getLocDescriptor())) {
535 // it matches with the current argument composite location
536 // so what is the corresponding parameter local descriptor?
537 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
538 System.out.println("----------found paramNode=" + paramNode);
539 NTuple<Descriptor> paramDescTuple = paramNode.getCurrentDescTuple();
541 NTuple<Location> newParamTupleFromArgTuple = translateToLocTuple(mdCallee, paramDescTuple);
542 for (int i = 1; i < argLocTuple.size(); i++) {
543 newParamTupleFromArgTuple.add(argLocTuple.get(i));
546 System.out.println("-----------newParamTuple=" + newParamTupleFromArgTuple);
547 return new CompositeLocation(newParamTupleFromArgTuple);
554 private void addAddtionalOrderingConstraints(MethodDescriptor mdCaller) {
556 // First, assign a composite location to a node in the flow graph
557 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
559 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
560 Map<Location, CompositeLocation> callerMapLocToCompLoc =
561 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
562 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
564 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
566 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
567 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
568 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
569 MethodDescriptor mdCallee = min.getMethod();
570 calleeSet.add(mdCallee);
573 // add an additional ordering constraint
574 // if the first element of a parameter composite location matches 'this' reference,
575 // the corresponding argument in the caller is required to be higher than the translated
576 // parameter location in the caller lattice
578 // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
581 // update return flow nodes in the caller
582 CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc();
583 System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc);
584 if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis())
585 && returnLoc.getSize() > 1) {
586 System.out.println("###RETURN COMP LOC=" + returnLoc);
587 NTuple<Location> returnLocTuple = returnLoc.getTuple();
588 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
589 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
590 for (int i = 1; i < returnLocTuple.size(); i++) {
591 newReturnTuple.add(returnLocTuple.get(i).getLocDescriptor());
593 System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple);
594 callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple);
596 // if the return loc set was empty and later pcloc was connected to the return loc
597 // need to make sure that return loc reflects to this changes.
598 FlowReturnNode flowReturnNode = callerFlowGraph.getFlowReturnNode(min);
599 if (flowReturnNode != null && flowReturnNode.getReturnTupleSet().isEmpty()) {
601 if (needToUpdateReturnLocHolder(min.getMethod(), flowReturnNode)) {
602 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
603 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
604 flowReturnNode.addTuple(newReturnTuple);
613 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
614 MethodDescriptor callee = (MethodDescriptor) iterator.next();
615 addAddtionalOrderingConstraints(callee);
620 private boolean needToUpdateReturnLocHolder(MethodDescriptor mdCallee,
621 FlowReturnNode flowReturnNode) {
622 FlowGraph fg = getFlowGraph(mdCallee);
623 MethodSummary summary = getMethodSummary(mdCallee);
624 CompositeLocation returnCompLoc = summary.getRETURNLoc();
625 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnCompLoc.getTuple());
626 Set<FlowNode> incomingNodeToReturnNode =
627 fg.getIncomingFlowNodeSet(fg.getFlowNode(returnDescTuple));
628 for (Iterator iterator = incomingNodeToReturnNode.iterator(); iterator.hasNext();) {
629 FlowNode inNode = (FlowNode) iterator.next();
630 if (inNode.getDescTuple().get(0).equals(mdCallee.getThis())) {
637 private void addMapMethodDescToMethodInvokeNodeSet(MethodInvokeNode min) {
638 MethodDescriptor md = min.getMethod();
639 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
640 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
642 mapMethodDescToMethodInvokeNodeSet.get(md).add(min);
645 private Set<MethodInvokeNode> getMethodInvokeNodeSetByMethodDesc(MethodDescriptor md) {
646 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
647 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
649 return mapMethodDescToMethodInvokeNodeSet.get(md);
652 private void addOrderingConstraintFromCompLocParamToArg(MethodDescriptor mdCaller,
653 MethodInvokeNode min) {
654 System.out.println("-addOrderingConstraintFromCompLocParamToArg=" + min.printNode(0));
656 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(ssjava.getMethodContainingSSJavaLoop());
658 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
660 MethodDescriptor mdCallee = min.getMethod();
662 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
663 for (int idx = 0; idx < calleeFlowGraph.getNumParameters(); idx++) {
664 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
665 NTuple<Location> globalParamLocTuple =
666 translateToLocTuple(mdCallee, paramNode.getDescTuple());
667 translateToLocTuple(mdCallee, paramNode.getDescTuple());
668 CompositeLocation compLoc = paramNode.getCompositeLocation();
669 System.out.println("---paramNode=" + paramNode + " compLoc=" + compLoc);
670 if (compLoc != null) {
671 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
672 NTuple<Location> globalArgLocTuple = translateToLocTuple(mdCaller, argTuple);
674 if (!isLiteralValueLocTuple(globalArgLocTuple)
675 && !isLiteralValueLocTuple(globalParamLocTuple)) {
676 if (!globalGraph.hasValueFlowEdge(globalArgLocTuple, globalParamLocTuple)) {
677 System.out.println("----- add global flow globalArgLocTuple=" + globalArgLocTuple
678 + "-> globalParamLocTuple=" + globalParamLocTuple);
680 globalGraph.addValueFlowEdge(globalArgLocTuple, globalParamLocTuple);
684 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
685 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
687 if (!isLiteralValueLocTuple(pcLocTuple) && !isLiteralValueLocTuple(globalParamLocTuple)) {
688 if (!globalGraph.hasValueFlowEdge(pcLocTuple, globalParamLocTuple)) {
690 .println("----- add global flow PCLOC="
692 + "-> globalParamLocTu!globalArgLocTuple.get(0).getLocDescriptor().equals(LITERALDESC)ple="
693 + globalParamLocTuple);
695 globalGraph.addValueFlowEdge(pcLocTuple, globalParamLocTuple);
704 private boolean isLiteralValueLocTuple(NTuple<Location> locTuple) {
705 return locTuple.get(0).getLocDescriptor().equals(LITERALDESC);
708 public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
709 CompositeLocation inferCompLoc) {
710 Descriptor localDesc = loc.getLocDescriptor();
712 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
713 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
714 FlowNode node = (FlowNode) iterator.next();
715 if (node.getDescTuple().startsWith(localDesc)
716 && !node.getDescTuple().get(0).equals(LITERALDESC)) {
717 // need to assign the inferred composite location to this node
718 CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
719 node.setCompositeLocation(newCompLoc);
720 System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
725 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
726 CompositeLocation inferCompLoc) {
728 System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
731 MethodDescriptor md = (MethodDescriptor) inferCompLoc.get(0).getDescriptor();
733 CompositeLocation newCompLoc = new CompositeLocation();
734 for (int i = 0; i < inferCompLoc.getSize(); i++) {
735 newCompLoc.addLocation(inferCompLoc.get(i));
738 Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
739 Descriptor enclosingDescriptor;
740 if (lastDescOfPrefix instanceof InterDescriptor) {
741 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(lastDescOfPrefix);
743 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
746 for (int i = 1; i < nodeDescTuple.size(); i++) {
747 Descriptor desc = nodeDescTuple.get(i);
748 Location locElement = new Location(enclosingDescriptor, desc);
749 newCompLoc.addLocation(locElement);
751 enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
757 private void translateMapLocationToInferCompositeLocationToCalleeGraph(
758 GlobalFlowGraph callerGraph, MethodInvokeNode min) {
760 MethodDescriptor mdCallee = min.getMethod();
761 MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
762 Map<Location, CompositeLocation> callerMapLocToCompLoc =
763 callerGraph.getMapLocationToInferCompositeLocation();
765 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
767 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
768 GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
770 NTuple<Location> baseLocTuple = null;
771 if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
772 baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
775 System.out.println("\n-#translate caller=" + mdCaller + " infer composite loc to callee="
776 + mdCallee + " baseLocTuple=" + baseLocTuple);
777 // System.out.println("-mapIdxToArgTuple=" + mapIdxToArgTuple);
778 // System.out.println("-callerMapLocToCompLoc=" + callerMapLocToCompLoc);
780 Set<Location> keySet = callerMapLocToCompLoc.keySet();
781 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
782 Location key = (Location) iterator.next();
783 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
785 if (!key.getDescriptor().equals(mdCaller)) {
787 CompositeLocation newCalleeCompLoc;
788 if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
789 // System.out.println("-----need to translate callerCompLoc=" + callerCompLoc
790 // + " with baseTuple=" + baseLocTuple);
792 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
794 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
795 System.out.println("1---key=" + key + " callerCompLoc=" + callerCompLoc
796 + " newCalleeCompLoc=" + newCalleeCompLoc);
797 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
798 if (!newCalleeCompLoc.get(0).getDescriptor().equals(mdCallee)) {
802 // System.out.println("-----baseLoctuple=" + baseLocTuple);
804 // check if it is the global access
805 Location compLocFirstElement = callerCompLoc.getTuple().get(0);
806 if (compLocFirstElement.getDescriptor().equals(mdCallee)
807 && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
809 newCalleeCompLoc = new CompositeLocation();
810 Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
812 newCalleeCompLoc.addLocation(newMethodLoc);
813 for (int i = 1; i < callerCompLoc.getSize(); i++) {
814 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
816 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
817 System.out.println("2---key=" + key + " callerCompLoc=" + callerCompLoc
818 + " newCalleeCompLoc=" + newCalleeCompLoc);
819 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
822 int paramIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
823 if (paramIdx == -1) {
824 // here, the first element of the current composite location comes from the current
826 // so transfer the same composite location to the callee
827 if (!calleeGlobalGraph.contrainsInferCompositeLocationMapKey(key)) {
828 if (callerCompLoc.get(0).getDescriptor().equals(mdCallee)) {
829 System.out.println("3---key=" + key + " callerCompLoc=" + callerCompLoc
830 + " newCalleeCompLoc=" + callerCompLoc);
831 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
832 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, callerCompLoc);
834 System.out.println("3---SKIP key=" + key + " callerCompLoc=" + callerCompLoc);
840 // It is the case where two parameters have relative orderings between them by having
841 // composite locations
842 // if we found the param idx, it means that the first part of the caller composite
843 // location corresponds to the one of arguments.
844 // for example, if the caller argument is <<caller.this>,<Decoder.br>>
845 // and the current caller composite location mapping
846 // <<caller.this>,<Decoder.br>,<Br.value>>
847 // and the parameter which matches with the caller argument is 'Br brParam'
848 // then, the translated callee composite location will be <<callee.brParam>,<Br.value>>
849 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(paramIdx);
851 FlowNode paramFlowNode = calleeFlowGraph.getParamFlowNode(paramIdx);
852 NTuple<Location> paramLocTuple =
853 translateToLocTuple(mdCallee, paramFlowNode.getDescTuple());
854 newCalleeCompLoc = new CompositeLocation();
855 for (int i = 0; i < paramLocTuple.size(); i++) {
856 newCalleeCompLoc.addLocation(paramLocTuple.get(i));
858 for (int i = argTuple.size(); i < callerCompLoc.getSize(); i++) {
859 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
861 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
862 System.out.println("4---key=" + key + " callerCompLoc=" + callerCompLoc
863 + " newCalleeCompLoc=" + newCalleeCompLoc);
864 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
866 // System.out.println("-----argTuple=" + argTuple + " caller=" + mdCaller +
869 // System.out.println("-----paramIdx=" + paramIdx + " paramFlowNode=" + paramFlowNode);
878 // System.out.println("-----*AFTER TRANSLATING COMP LOC MAPPING, CALLEE MAPPING="
879 // + calleeGlobalGraph.getMapLocationToInferCompositeLocation());
881 System.out.println("#ASSIGN COMP LOC TO CALLEE PARAMS: callee=" + mdCallee + " caller="
883 // If the location of an argument has a composite location
884 // need to assign a proper composite location to the corresponding callee parameter
885 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
886 for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
887 Integer idx = (Integer) iterator.next();
889 if (idx == 0 && !min.getMethod().isStatic()) {
893 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
894 System.out.println("-argTuple=" + argTuple + " idx=" + idx);
895 if (argTuple.size() > 0) {
896 // check if an arg tuple has been already assigned to a composite location
897 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
898 Location argLocalLoc = argLocTuple.get(0);
900 // if (!isPrimitiveType(argTuple)) {
901 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
903 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
904 for (int i = 1; i < argLocTuple.size(); i++) {
905 callerCompLoc.addLocation(argLocTuple.get(i));
908 System.out.println("---callerCompLoc=" + callerCompLoc);
910 // if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
912 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
914 NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
915 NTuple<Location> calleeParamLocTuple =
916 translateToLocTuple(mdCallee, calleeParamDescTuple);
918 int refParamIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
919 System.out.println("-----paramIdx=" + refParamIdx);
920 if (refParamIdx == 0 && !mdCallee.isStatic()) {
922 System.out.println("-------need to translate callerCompLoc=" + callerCompLoc
923 + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
924 + calleeParamLocTuple);
926 CompositeLocation newCalleeCompLoc =
927 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
929 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
932 System.out.println("---------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
933 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
935 } else if (refParamIdx != -1) {
936 // the first element of an argument composite location matches with one of paramtere
937 // composite locations
939 System.out.println("-------param match case=");
941 NTuple<Descriptor> argTupleRef = mapIdxToArgTuple.get(refParamIdx);
942 FlowNode refParamFlowNode = calleeFlowGraph.getParamFlowNode(refParamIdx);
943 NTuple<Location> refParamLocTuple =
944 translateToLocTuple(mdCallee, refParamFlowNode.getDescTuple());
946 System.out.println("---------refParamLocTuple=" + refParamLocTuple
947 + " from argTupleRef=" + argTupleRef);
949 CompositeLocation newCalleeCompLoc = new CompositeLocation();
950 for (int i = 0; i < refParamLocTuple.size(); i++) {
951 newCalleeCompLoc.addLocation(refParamLocTuple.get(i));
953 for (int i = argTupleRef.size(); i < callerCompLoc.getSize(); i++) {
954 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
957 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
960 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
961 System.out.println("-----------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
962 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
965 CompositeLocation newCalleeCompLoc =
966 calculateCompositeLocationFromSubGlobalGraph(mdCallee, calleeParamFlowNode);
967 if (newCalleeCompLoc != null) {
968 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
970 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
974 System.out.println("-----------------calleeParamFlowNode="
975 + calleeParamFlowNode.getCompositeLocation());
986 private CompositeLocation calculateCompositeLocationFromSubGlobalGraph(MethodDescriptor md,
987 FlowNode paramNode) {
989 System.out.println("#############################################################");
990 System.out.println("calculateCompositeLocationFromSubGlobalGraph=" + paramNode);
992 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
993 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
994 GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
996 List<NTuple<Location>> prefixList = calculatePrefixList(subGlobalFlowGraph, paramGlobalNode);
998 Location prefixLoc = paramLocTuple.get(0);
1000 Set<GlobalFlowNode> reachableNodeSet =
1001 subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
1002 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1004 // System.out.println("node=" + node + " prefixList=" + prefixList);
1006 for (int i = 0; i < prefixList.size(); i++) {
1007 NTuple<Location> curPrefix = prefixList.get(i);
1008 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1010 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1011 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1012 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1013 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1016 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1018 if (!reachableCommonPrefixSet.isEmpty()) {
1020 MethodDescriptor curPrefixFirstElementMethodDesc =
1021 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1023 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1024 (MethodDescriptor) prefixLoc.getDescriptor();
1026 // System.out.println("curPrefixFirstElementMethodDesc=" +
1027 // curPrefixFirstElementMethodDesc);
1028 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1029 // + nodePrefixLocFirstElementMethodDesc);
1031 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1032 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1033 curPrefixFirstElementMethodDesc)) {
1036 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1038 Location curPrefixLocalLoc = curPrefix.get(0);
1039 if (subGlobalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1040 // in this case, the local variable of the current prefix has already got a composite
1042 // so we just ignore the current composite location.
1044 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1045 // + " DUE TO " + curPrefix);
1049 if (!needToGenerateCompositeLocation(paramGlobalNode, curPrefix)) {
1050 System.out.println("NO NEED TO GENERATE COMP LOC to " + paramGlobalNode
1051 + " with prefix=" + curPrefix);
1052 // System.out.println("prefixList=" + prefixList);
1053 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1057 Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
1058 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1059 System.out.println("NEED TO ASSIGN COMP LOC TO " + paramGlobalNode + " with prefix="
1061 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
1063 // makes sure that a newly generated location appears in the hierarchy graph
1064 for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
1065 Location curLoc = newCompLoc.get(compIdx);
1066 getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
1069 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1081 private int getParamIdx(CompositeLocation compLoc,
1082 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple) {
1084 // if the composite location is started with the argument descriptor
1085 // return the argument's index. o.t. return -1
1087 Set<Integer> keySet = mapIdxToArgTuple.keySet();
1088 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1089 Integer key = (Integer) iterator.next();
1090 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(key);
1091 if (argTuple.size() > 0 && translateToDescTuple(compLoc.getTuple()).startsWith(argTuple)) {
1092 System.out.println("compLoc.getTuple=" + compLoc + " is started with " + argTuple);
1093 return key.intValue();
1100 private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
1102 Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
1104 if (lastDesc instanceof FieldDescriptor) {
1105 return ((FieldDescriptor) lastDesc).getType().isPrimitive();
1106 } else if (lastDesc instanceof VarDescriptor) {
1107 return ((VarDescriptor) lastDesc).getType().isPrimitive();
1108 } else if (lastDesc instanceof InterDescriptor) {
1115 private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
1116 NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
1118 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1120 Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
1121 newCalleeCompLoc.addLocation(calleeThisLoc);
1123 // remove the base tuple from the caller
1124 // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
1125 // ,which is relative to the 'this' variable, <THIS,...>
1126 for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
1127 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1130 return newCalleeCompLoc;
1134 private void calculateGlobalValueFlowCompositeLocation() {
1136 System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
1137 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1138 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1140 Set<Location> calculatedPrefixSet = new HashSet<Location>();
1142 Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
1144 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1145 GlobalFlowNode node = (GlobalFlowNode) iterator.next();
1147 Location prefixLoc = node.getLocTuple().get(0);
1149 if (calculatedPrefixSet.contains(prefixLoc)) {
1150 // the prefix loc has been already assigned to a composite location
1154 calculatedPrefixSet.add(prefixLoc);
1156 // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
1157 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
1159 Set<GlobalFlowNode> reachableNodeSet =
1160 globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1161 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1163 // System.out.println("node=" + node + " prefixList=" + prefixList);
1164 System.out.println("---prefixList=" + prefixList);
1166 nextprefix: for (int i = 0; i < prefixList.size(); i++) {
1167 NTuple<Location> curPrefix = prefixList.get(i);
1168 System.out.println("---curPrefix=" + curPrefix);
1169 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1171 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1172 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1173 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1174 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1177 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1179 if (!reachableCommonPrefixSet.isEmpty()) {
1181 MethodDescriptor curPrefixFirstElementMethodDesc =
1182 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1184 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1185 (MethodDescriptor) prefixLoc.getDescriptor();
1187 // System.out.println("curPrefixFirstElementMethodDesc=" +
1188 // curPrefixFirstElementMethodDesc);
1189 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1190 // + nodePrefixLocFirstElementMethodDesc);
1192 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1193 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1194 curPrefixFirstElementMethodDesc)) {
1197 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1199 Location curPrefixLocalLoc = curPrefix.get(0);
1200 if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1201 // in this case, the local variable of the current prefix has already got a composite
1203 // so we just ignore the current composite location.
1205 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1206 // + " DUE TO " + curPrefix);
1211 if (!needToGenerateCompositeLocation(node, curPrefix)) {
1212 System.out.println("NO NEED TO GENERATE COMP LOC to " + node + " with prefix="
1214 // System.out.println("prefixList=" + prefixList);
1215 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1216 continue nextprefix;
1219 Location targetLocalLoc = node.getLocTuple().get(0);
1220 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1221 System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
1222 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc="
1224 globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1239 private boolean checkFlowNodeReturnThisField(MethodDescriptor md) {
1241 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1242 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1244 FlowGraph flowGraph = getFlowGraph(md);
1246 ClassDescriptor enclosingDesc = getClassTypeDescriptor(md.getThis());
1247 if (enclosingDesc == null) {
1252 Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
1253 Set<GlobalFlowNode> globalReturnNodeSet = new HashSet<GlobalFlowNode>();
1254 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1255 FlowNode flowNode = (FlowNode) iterator.next();
1256 NTuple<Location> locTuple = translateToLocTuple(md, flowNode.getDescTuple());
1257 GlobalFlowNode globalReturnNode = globalFlowGraph.getFlowNode(locTuple);
1258 globalReturnNodeSet.add(globalReturnNode);
1260 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, globalReturnNode);
1261 for (int i = 0; i < prefixList.size(); i++) {
1262 NTuple<Location> curPrefix = prefixList.get(i);
1263 ClassDescriptor cd =
1264 getClassTypeDescriptor(curPrefix.get(curPrefix.size() - 1).getLocDescriptor());
1265 if (cd != null && cd.equals(enclosingDesc)) {
1273 if (count == returnNodeSet.size()) {
1274 // in this case, all return nodes in the method returns values coming from a location that
1275 // starts with "this"
1277 System.out.println("$$$SET RETURN LOC TRUE=" + md);
1278 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.TRUE);
1280 // NameDescriptor returnLocDesc = new NameDescriptor("RLOC" + (locSeed++));
1281 // NTuple<Descriptor> rDescTuple = new NTuple<Descriptor>();
1282 // rDescTuple.add(md.getThis());
1283 // rDescTuple.add(returnLocDesc);
1285 // for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1286 // FlowNode rnode = (FlowNode) iterator.next();
1287 // flowGraph.addValueFlowEdge(rnode.getDescTuple(), rDescTuple);
1290 // getMethodSummary(md).setRETURNLoc(new CompositeLocation(translateToLocTuple(md,
1294 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.FALSE);
1297 return mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1301 private boolean needToGenerateCompositeLocation(GlobalFlowNode node, NTuple<Location> curPrefix) {
1302 // return true if there is a path between a node to which we want to give a composite location
1303 // and nodes which start with curPrefix
1305 System.out.println("---needToGenerateCompositeLocation curPrefix=" + curPrefix);
1307 Location targetLocalLoc = node.getLocTuple().get(0);
1309 MethodDescriptor md = (MethodDescriptor) targetLocalLoc.getDescriptor();
1310 FlowGraph flowGraph = getFlowGraph(md);
1311 FlowNode flowNode = flowGraph.getFlowNode(node.getDescTuple());
1312 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
1314 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
1315 for (Iterator iterator = paramNodeSet.iterator(); iterator.hasNext();) {
1316 FlowNode paramFlowNode = (FlowNode) iterator.next();
1317 if (curPrefix.startsWith(translateToLocTuple(md, paramFlowNode.getDescTuple()))) {
1318 System.out.println("here1?!");
1323 if (targetLocalLoc.getLocDescriptor() instanceof InterDescriptor) {
1324 Pair<MethodInvokeNode, Integer> pair =
1325 ((InterDescriptor) targetLocalLoc.getLocDescriptor()).getMethodArgIdxPair();
1328 System.out.println("$$$TARGETLOCALLOC HOLDER=" + targetLocalLoc);
1330 MethodInvokeNode min = pair.getFirst();
1331 Integer paramIdx = pair.getSecond();
1332 MethodDescriptor mdCallee = min.getMethod();
1334 FlowNode paramNode = getFlowGraph(mdCallee).getParamFlowNode(paramIdx);
1335 if (checkNodeReachToReturnNode(mdCallee, paramNode)) {
1336 System.out.println("here2?!");
1344 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1345 Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1347 if (!md.isStatic()) {
1348 ClassDescriptor currentMethodThisType = getClassTypeDescriptor(md.getThis());
1349 for (int i = 0; i < curPrefix.size(); i++) {
1350 ClassDescriptor prefixType = getClassTypeDescriptor(curPrefix.get(i).getLocDescriptor());
1351 if (prefixType != null && prefixType.equals(currentMethodThisType)) {
1352 System.out.println("PREFIX TYPE MATCHES WITH=" + currentMethodThisType);
1354 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1355 boolean hasCompReturnLocWithThis =
1356 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1357 if (hasCompReturnLocWithThis) {
1358 if (checkNodeReachToReturnNode(md, flowNode)) {
1359 System.out.println("here3?!");
1365 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1366 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1367 if (subGlobalReachalbeNode.getLocTuple().get(0).getLocDescriptor().equals(md.getThis())) {
1368 System.out.println("PREFIX FOUND=" + subGlobalReachalbeNode);
1369 System.out.println("here4?!");
1377 // System.out.println("flowGraph.getReturnNodeSet()=" + flowGraph.getReturnNodeSet());
1378 // System.out.println("flowGraph.contains(node.getDescTuple())="
1379 // + flowGraph.contains(node.getDescTuple()) + " flowGraph.getFlowNode(node.getDescTuple())="
1380 // + flowGraph.getFlowNode(node.getDescTuple()));reachableSet
1382 // if (flowGraph.contains(node.getDescTuple())
1383 // && flowGraph.getReturnNodeSet().contains(flowGraph.getFlowNode(node.getDescTuple()))) {
1384 // // return checkFlowNodeReturnThisField(flowGraph);
1387 Location lastLocationOfPrefix = curPrefix.get(curPrefix.size() - 1);
1388 // check whether prefix appears in the list of parameters
1389 Set<MethodInvokeNode> minSet = mapMethodDescToMethodInvokeNodeSet.get(md);
1390 found: for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1391 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1392 Map<Integer, NTuple<Descriptor>> map = mapMethodInvokeNodeToArgIdxMap.get(min);
1393 Set<Integer> keySet = map.keySet();
1394 // System.out.println("min=" + min.printNode(0));
1396 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1397 Integer argIdx = (Integer) iterator2.next();
1398 NTuple<Descriptor> argTuple = map.get(argIdx);
1400 if (!(!md.isStatic() && argIdx == 0)) {
1401 // if the argTuple is empty, we don't need to do with anything(LITERAL CASE).
1402 if (argTuple.size() > 0
1403 && argTuple.get(argTuple.size() - 1).equals(lastLocationOfPrefix.getLocDescriptor())) {
1404 NTuple<Location> locTuple =
1405 translateToLocTuple(md, flowGraph.getParamFlowNode(argIdx).getDescTuple());
1406 lastLocationOfPrefix = locTuple.get(0);
1407 System.out.println("ARG CASE=" + locTuple);
1408 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1409 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1410 // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
1411 NTuple<Location> globalReachlocTuple = subGlobalReachalbeNode.getLocTuple();
1412 for (int i = 0; i < globalReachlocTuple.size(); i++) {
1413 if (globalReachlocTuple.get(i).equals(lastLocationOfPrefix)) {
1414 System.out.println("ARG " + argTuple + " IS MATCHED WITH="
1415 + lastLocationOfPrefix);
1416 System.out.println("here5?!");
1427 // ClassDescriptor cd;
1428 // if (lastLocationOfPrefix.getLocDescriptor() instanceof VarDescriptor) {
1429 // cd = ((VarDescriptor) lastLocationOfPrefix.getLocDescriptor()).getType().getClassDesc();
1431 // // it is a field descriptor
1432 // cd = ((FieldDescriptor) lastLocationOfPrefix.getLocDescriptor()).getType().getClassDesc();
1435 // GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1436 // Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1438 // System.out.println("TRY TO FIND lastLocationOfPrefix=" + lastLocationOfPrefix);
1439 // for (Iterator iterator2 = subGlobalReachableSet.iterator(); iterator2.hasNext();) {
1440 // GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator2.next();
1441 // // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
1442 // NTuple<Location> locTuple = subGlobalReachalbeNode.getLocTuple();
1444 // for (int i = 0; i < locTuple.size(); i++) {
1445 // if (locTuple.get(i).equals(lastLocationOfPrefix)) {
1450 // Location lastLoc = locTuple.get(locTuple.size() - 1);
1451 // Descriptor enclosingDescriptor = lastLoc.getDescriptor();
1453 // if (enclosingDescriptor != null && enclosingDescriptor.equals(cd)) {
1454 // System.out.println("# WHY HERE?");
1455 // System.out.println("subGlobalReachalbeNode=" + subGlobalReachalbeNode);
1463 private boolean checkNodeReachToReturnNode(MethodDescriptor md, FlowNode node) {
1465 FlowGraph flowGraph = getFlowGraph(md);
1466 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(node);
1467 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1468 boolean hasCompReturnLocWithThis =
1469 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1471 if (hasCompReturnLocWithThis) {
1472 for (Iterator iterator = flowGraph.getReturnNodeSet().iterator(); iterator.hasNext();) {
1473 FlowNode returnFlowNode = (FlowNode) iterator.next();
1474 if (reachableSet.contains(returnFlowNode)) {
1483 private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
1484 CompositeLocation newCompLoc = compLocPrefix.clone();
1485 NTuple<Location> locTuple = node.getLocTuple();
1486 for (int i = 1; i < locTuple.size(); i++) {
1487 newCompLoc.addLocation(locTuple.get(i));
1489 node.setInferCompositeLocation(newCompLoc);
1492 private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
1494 System.out.println("\n##### calculatePrefixList node=" + node);
1496 Set<GlobalFlowNode> incomingNodeSetPrefix =
1497 graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
1498 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
1500 Set<GlobalFlowNode> reachableNodeSetPrefix =
1501 graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1502 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
1504 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1506 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
1507 GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
1508 NTuple<Location> inNodeTuple = inNode.getLocTuple();
1510 if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
1511 || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
1515 for (int i = 1; i < inNodeTuple.size(); i++) {
1516 NTuple<Location> prefix = inNodeTuple.subList(0, i);
1517 if (!prefixList.contains(prefix)) {
1518 prefixList.add(prefix);
1523 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1524 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1525 int s0 = arg0.size();
1526 int s1 = arg1.size();
1529 } else if (s0 == s1) {
1537 // remove a prefix which is not suitable for generating composite location
1538 Location localVarLoc = node.getLocTuple().get(0);
1539 MethodDescriptor md = (MethodDescriptor) localVarLoc.getDescriptor();
1540 ClassDescriptor cd = md.getClassDesc();
1543 Set<NTuple<Location>> toberemoved = new HashSet<NTuple<Location>>();
1544 // for (int i = 0; i < prefixList.size(); i++) {
1545 // NTuple<Location> prefixLocTuple = prefixList.get(i);
1546 // if (!containsClassDesc(cd, prefixLocTuple)) {
1547 // toberemoved.add(prefixLocTuple);
1551 // System.out.println("method class=" + cd + " toberemoved=" + toberemoved);
1553 prefixList.removeAll(toberemoved);
1559 private CompositeLocation calculateCompositeLocationFromFlowGraph(MethodDescriptor md,
1562 System.out.println("#############################################################");
1563 System.out.println("calculateCompositeLocationFromFlowGraph=" + node);
1565 FlowGraph flowGraph = getFlowGraph(md);
1566 // NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
1567 // GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
1569 List<NTuple<Location>> prefixList = calculatePrefixListFlowGraph(flowGraph, node);
1571 // Set<GlobalFlowNode> reachableNodeSet =
1572 // subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
1574 Set<FlowNode> reachableNodeSet =
1575 flowGraph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
1577 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1579 // System.out.println("node=" + node + " prefixList=" + prefixList);
1581 for (int i = 0; i < prefixList.size(); i++) {
1582 NTuple<Location> curPrefix = prefixList.get(i);
1583 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1585 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1586 FlowNode reachNode = (FlowNode) iterator2.next();
1587 NTuple<Location> reachLocTuple = translateToLocTuple(md, reachNode.getCurrentDescTuple());
1588 if (reachLocTuple.startsWith(curPrefix)) {
1589 reachableCommonPrefixSet.add(reachLocTuple);
1592 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1594 if (!reachableCommonPrefixSet.isEmpty()) {
1596 MethodDescriptor curPrefixFirstElementMethodDesc =
1597 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1599 // MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1600 // (MethodDescriptor) prefixLoc.getDescriptor();
1602 // System.out.println("curPrefixFirstElementMethodDesc=" +
1603 // curPrefixFirstElementMethodDesc);
1604 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1605 // + nodePrefixLocFirstElementMethodDesc);
1608 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1610 Location curPrefixLocalLoc = curPrefix.get(0);
1612 Location targetLocalLoc = new Location(md, node.getDescTuple().get(0));
1613 // Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
1615 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1616 System.out.println("NEED2ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
1617 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
1619 // // makes sure that a newly generated location appears in the hierarchy graph
1620 // for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
1621 // Location curLoc = newCompLoc.get(compIdx);
1622 // getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
1624 // subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1625 node.setCompositeLocation(newCompLoc);
1635 private List<NTuple<Location>> calculatePrefixListFlowGraph(FlowGraph graph, FlowNode node) {
1637 System.out.println("\n##### calculatePrefixList node=" + node);
1639 MethodDescriptor md = graph.getMethodDescriptor();
1640 Set<FlowNode> incomingNodeSetPrefix =
1641 graph.getIncomingNodeSetByPrefix(node.getDescTuple().get(0));
1642 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
1644 Set<FlowNode> reachableNodeSetPrefix =
1645 graph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
1646 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
1648 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1650 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
1651 FlowNode inNode = (FlowNode) iterator.next();
1652 NTuple<Location> inNodeTuple = translateToLocTuple(md, inNode.getCurrentDescTuple());
1654 // if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
1655 // || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
1659 for (int i = 1; i < inNodeTuple.size(); i++) {
1660 NTuple<Location> prefix = inNodeTuple.subList(0, i);
1661 if (!prefixList.contains(prefix)) {
1662 prefixList.add(prefix);
1667 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1668 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1669 int s0 = arg0.size();
1670 int s1 = arg1.size();
1673 } else if (s0 == s1) {
1685 private boolean containsClassDesc(ClassDescriptor cd, NTuple<Location> prefixLocTuple) {
1686 for (int i = 0; i < prefixLocTuple.size(); i++) {
1687 Location loc = prefixLocTuple.get(i);
1688 Descriptor locDesc = loc.getLocDescriptor();
1689 if (locDesc != null) {
1690 ClassDescriptor type = getClassTypeDescriptor(locDesc);
1691 if (type != null && type.equals(cd)) {
1699 private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
1701 MethodDescriptor md = flowGraph.getMethodDescriptor();
1703 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(md);
1705 // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1706 Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
1708 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
1710 FlowEdge edge = (FlowEdge) iterator.next();
1711 NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
1712 NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
1714 if (flowGraph.getFlowNode(srcDescTuple) instanceof FlowReturnNode
1715 || flowGraph.getFlowNode(dstDescTuple) instanceof FlowReturnNode) {
1719 // here only keep the first element(method location) of the descriptor
1721 NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
1722 // Location srcMethodLoc = srcLocTuple.get(0);
1723 // Descriptor srcVarDesc = srcMethodLoc.getLocDescriptor();
1724 // // if (flowGraph.isParamDesc(srcVarDesc) &&
1725 // (!srcVarDesc.equals(md.getThis()))) {
1726 // if (!srcVarDesc.equals(md.getThis())) {
1727 // srcLocTuple = new NTuple<Location>();
1728 // Location loc = new Location(md, srcVarDesc);
1729 // srcLocTuple.add(loc);
1732 NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
1733 // Location dstMethodLoc = dstLocTuple.get(0);
1734 // Descriptor dstVarDesc = dstMethodLoc.getLocDescriptor();
1735 // if (!dstVarDesc.equals(md.getThis())) {
1736 // dstLocTuple = new NTuple<Location>();
1737 // Location loc = new Location(md, dstVarDesc);
1738 // dstLocTuple.add(loc);
1741 globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
1748 private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
1750 NTuple<Location> locTuple = new NTuple<Location>();
1752 Descriptor enclosingDesc = md;
1753 for (int i = 0; i < descTuple.size(); i++) {
1754 Descriptor desc = descTuple.get(i);
1756 Location loc = new Location(enclosingDesc, desc);
1759 if (desc instanceof VarDescriptor) {
1760 enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
1761 } else if (desc instanceof FieldDescriptor) {
1762 enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1764 // TODO: inter descriptor case
1765 enclosingDesc = desc;
1774 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller) {
1776 // the transformation for a call site propagates flows through parameters
1777 // if the method is virtual, it also grab all relations from any possible
1780 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
1782 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
1783 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1784 MethodDescriptor mdCallee = min.getMethod();
1785 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1786 if (mdCallee.isStatic()) {
1787 setPossibleCallees.add(mdCallee);
1789 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
1790 // removes method descriptors that are not invoked by the caller
1791 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
1792 setPossibleCallees.addAll(calleeSet);
1795 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
1796 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
1797 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
1804 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
1805 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
1807 System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
1808 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
1809 Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
1811 System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
1812 + mapMethodInvokeNodeToArgIdxMap.get(min));
1814 Set<Integer> keySet = mapIdxToArg.keySet();
1815 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1816 Integer idx = (Integer) iterator.next();
1817 NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
1818 if (argDescTuple.size() > 0) {
1819 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1820 NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
1821 NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
1822 System.out.println("-------paramDescTuple=" + paramDescTuple + "->argDescTuple="
1824 addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
1828 // addValueFlowBetweenParametersToCaller(min, mdCaller, possibleMdCallee);
1830 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1831 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
1832 Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
1833 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
1834 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
1835 addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
1838 System.out.println("$$$GLOBAL PC LOC ADD=" + mdCaller);
1839 Set<NTuple<Location>> pcLocTupleSet = mapMethodInvokeNodeToPCLocTupleSet.get(min);
1840 System.out.println("---pcLocTupleSet=" + pcLocTupleSet);
1841 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
1842 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
1843 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
1844 if (calleeNode.isParamNodeWithIncomingFlows()) {
1845 System.out.println("calleeNode.getLocTuple()" + calleeNode.getLocTuple());
1846 NTuple<Location> callerSrcNodeLocTuple =
1847 translateToCallerLocTuple(min, possibleMdCallee, mdCaller, calleeNode.getLocTuple());
1848 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
1849 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
1850 for (Iterator iterator2 = pcLocTupleSet.iterator(); iterator2.hasNext();) {
1851 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator2.next();
1852 callerSubGlobalGraph.addValueFlowEdge(pcLocTuple, callerSrcNodeLocTuple);
1861 private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
1862 MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
1864 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1865 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
1867 // System.out.println("$addValueFlowFromCalleeNode calleeSrcNode=" + calleeSrcNode);
1869 NTuple<Location> callerSrcNodeLocTuple =
1870 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
1871 // System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
1873 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
1875 Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
1877 for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
1878 GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
1879 NTuple<Location> callerDstNodeLocTuple =
1880 translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
1881 // System.out.println("outNode=" + outNode + " callerDstNodeLocTuple="
1882 // + callerDstNodeLocTuple);
1883 if (callerDstNodeLocTuple != null) {
1884 callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
1891 private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
1892 MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
1893 // this method will return the same nodeLocTuple if the corresponding argument is literal
1896 // System.out.println("translateToCallerLocTuple=" + nodeLocTuple);
1898 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1899 NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
1900 if (calleeFlowGraph.isParameter(nodeDescTuple)) {
1901 int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
1902 NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
1904 // if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
1905 // // the type of argument is primitive.
1906 // return nodeLocTuple.clone();
1908 // System.out.println("paramIdx=" + paramIdx + " argDescTuple=" + argDescTuple + " from min="
1909 // + min.printNode(0));
1910 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1912 NTuple<Location> callerLocTuple = new NTuple<Location>();
1914 callerLocTuple.addAll(argLocTuple);
1915 for (int i = 1; i < nodeLocTuple.size(); i++) {
1916 callerLocTuple.add(nodeLocTuple.get(i));
1918 return callerLocTuple;
1920 return nodeLocTuple.clone();
1925 public static boolean isPrimitive(Descriptor desc) {
1927 if (desc instanceof FieldDescriptor) {
1928 return ((FieldDescriptor) desc).getType().isPrimitive();
1929 } else if (desc instanceof VarDescriptor) {
1930 return ((VarDescriptor) desc).getType().isPrimitive();
1931 } else if (desc instanceof InterDescriptor) {
1938 public static boolean isReference(Descriptor desc) {
1940 if (desc instanceof FieldDescriptor) {
1942 TypeDescriptor type = ((FieldDescriptor) desc).getType();
1943 if (type.isArray()) {
1946 return type.isPtr();
1949 } else if (desc instanceof VarDescriptor) {
1950 TypeDescriptor type = ((VarDescriptor) desc).getType();
1951 if (type.isArray()) {
1954 return type.isPtr();
1961 private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
1963 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
1964 for (int i = 0; i < locTuple.size(); i++) {
1965 descTuple.add(locTuple.get(i).getLocDescriptor());
1971 public LocationSummary getLocationSummary(Descriptor d) {
1972 if (!mapDescToLocationSummary.containsKey(d)) {
1973 if (d instanceof MethodDescriptor) {
1974 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
1975 } else if (d instanceof ClassDescriptor) {
1976 mapDescToLocationSummary.put(d, new FieldSummary());
1979 return mapDescToLocationSummary.get(d);
1982 private void generateMethodSummary() {
1984 Set<MethodDescriptor> keySet = md2lattice.keySet();
1985 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1986 MethodDescriptor md = (MethodDescriptor) iterator.next();
1988 System.out.println("\nSSJAVA: generate method summary: " + md);
1990 FlowGraph flowGraph = getFlowGraph(md);
1991 MethodSummary methodSummary = getMethodSummary(md);
1993 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
1995 // set the 'this' reference location
1996 if (!md.isStatic()) {
1997 System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
1998 methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
2001 // set the 'global' reference location if needed
2002 if (methodSummary.hasGlobalAccess()) {
2003 methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
2006 // construct a parameter mapping that maps a parameter descriptor to an
2007 // inferred composite location
2008 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
2009 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
2010 CompositeLocation inferredCompLoc =
2011 updateCompositeLocation(flowNode.getCompositeLocation());
2012 // NTuple<Descriptor> descTuple = flowNode.getDescTuple();
2014 // CompositeLocation assignedCompLoc = flowNode.getCompositeLocation();
2015 // CompositeLocation inferredCompLoc;
2016 // if (assignedCompLoc != null) {
2017 // inferredCompLoc = translateCompositeLocation(assignedCompLoc);
2019 // Descriptor locDesc = descTuple.get(0);
2020 // Location loc = new Location(md, locDesc.getSymbol());
2021 // loc.setLocDescriptor(locDesc);
2022 // inferredCompLoc = new CompositeLocation(loc);
2024 System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc + " original="
2025 + flowNode.getCompositeLocation());
2027 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
2028 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
2029 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
2036 private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2038 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2040 for (int idx = 0; idx < size; idx++) {
2041 Location loc1 = locTuple1.get(idx);
2042 Location loc2 = locTuple2.get(idx);
2044 Descriptor desc1 = loc1.getDescriptor();
2045 Descriptor desc2 = loc2.getDescriptor();
2047 if (!desc1.equals(desc2)) {
2048 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2051 Descriptor locDesc1 = loc1.getLocDescriptor();
2052 Descriptor locDesc2 = loc2.getLocDescriptor();
2054 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2056 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2057 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2059 System.out.println("---node1=" + node1 + " node2=" + node2);
2060 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2061 + hierarchyGraph.getIncomingNodeSet(node2));
2063 if (locDesc1.equals(locDesc2)) {
2065 } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
2066 && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
2078 private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2080 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2082 for (int idx = 0; idx < size; idx++) {
2083 Location loc1 = locTuple1.get(idx);
2084 Location loc2 = locTuple2.get(idx);
2086 Descriptor desc1 = loc1.getDescriptor();
2087 Descriptor desc2 = loc2.getDescriptor();
2089 if (!desc1.equals(desc2)) {
2090 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2093 Descriptor locDesc1 = loc1.getLocDescriptor();
2094 Descriptor locDesc2 = loc2.getLocDescriptor();
2096 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2098 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2099 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2101 System.out.println("---node1=" + node1 + " node2=" + node2);
2102 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2103 + hierarchyGraph.getIncomingNodeSet(node2));
2105 if (locDesc1.equals(locDesc2)) {
2107 } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
2118 private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
2119 CompositeLocation newCompLoc = new CompositeLocation();
2121 // System.out.println("compLoc=" + compLoc);
2122 for (int i = 0; i < compLoc.getSize(); i++) {
2123 Location loc = compLoc.get(i);
2124 Descriptor enclosingDescriptor = loc.getDescriptor();
2125 Descriptor locDescriptor = loc.getLocDescriptor();
2127 HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
2128 // System.out.println("-hnode=" + hnode + " from=" + locDescriptor +
2129 // " enclosingDescriptor="
2130 // + enclosingDescriptor);
2131 // System.out.println("-getLocationSummary(enclosingDescriptor)="
2132 // + getLocationSummary(enclosingDescriptor));
2133 String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
2134 // System.out.println("-locName=" + locName);
2135 Location newLoc = new Location(enclosingDescriptor, locName);
2136 newLoc.setLocDescriptor(locDescriptor);
2137 newCompLoc.addLocation(newLoc);
2143 private void debug_writeLattices() {
2145 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
2146 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2147 Descriptor key = (Descriptor) iterator.next();
2148 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
2149 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
2150 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
2151 if (key instanceof ClassDescriptor) {
2152 writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
2154 } else if (key instanceof MethodDescriptor) {
2155 MethodDescriptor md = (MethodDescriptor) key;
2156 writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
2160 LocationSummary ls = getLocationSummary(key);
2161 System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
2164 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
2165 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
2166 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2167 writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
2168 cd2lattice.get(cd), "");
2171 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
2172 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
2173 MethodDescriptor md = (MethodDescriptor) iterator.next();
2174 writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
2175 md2lattice.get(md), "");
2180 private void buildLattice() {
2182 BuildLattice buildLattice = new BuildLattice(this);
2184 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
2185 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2186 Descriptor desc = (Descriptor) iterator.next();
2188 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
2190 addMapDescToSimpleLattice(desc, simpleLattice);
2192 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2193 System.out.println("\n## insertIntermediateNodesToStraightLine:"
2194 + simpleHierarchyGraph.getName());
2195 SSJavaLattice<String> lattice =
2196 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2197 lattice.removeRedundantEdges();
2199 if (desc instanceof ClassDescriptor) {
2201 cd2lattice.put((ClassDescriptor) desc, lattice);
2202 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2203 } else if (desc instanceof MethodDescriptor) {
2205 md2lattice.put((MethodDescriptor) desc, lattice);
2206 MethodDescriptor md = (MethodDescriptor) desc;
2207 ClassDescriptor cd = md.getClassDesc();
2208 // ssjava.writeLatticeDotFile(cd, md, lattice);
2211 // System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
2212 // HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2213 // HierarchyGraph skeletonGraphWithCombinationNode =
2214 // skeletonGraph.clone();
2215 // skeletonGraphWithCombinationNode.setName(desc + "_SC");
2217 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2218 // System.out.println("Identifying Combination Nodes:");
2219 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2220 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2221 // mapDescriptorToCombineSkeletonHierarchyGraph.put(desc,
2222 // skeletonGraphWithCombinationNode);
2227 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
2228 mapDescriptorToSimpleLattice.put(desc, lattice);
2231 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
2232 return mapDescriptorToSimpleLattice.get(desc);
2235 private void simplifyHierarchyGraph() {
2236 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2237 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2238 Descriptor desc = (Descriptor) iterator.next();
2239 // System.out.println("SSJAVA: remove redundant edges: " + desc);
2240 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
2241 simpleHierarchyGraph.setName(desc + "_SIMPLE");
2242 simpleHierarchyGraph.removeRedundantEdges();
2243 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
2247 private void insertCombinationNodes() {
2248 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
2249 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2250 Descriptor desc = (Descriptor) iterator.next();
2251 System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
2252 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2253 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
2254 skeletonGraphWithCombinationNode.setName(desc + "_SC");
2256 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2257 System.out.println("Identifying Combination Nodes:");
2258 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2259 skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2260 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
2264 private void constructSkeletonHierarchyGraph() {
2265 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2266 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2267 Descriptor desc = (Descriptor) iterator.next();
2268 System.out.println("SSJAVA: Constructing Skeleton Hierarchy Graph: " + desc);
2269 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
2270 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
2271 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
2272 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
2273 skeletonGraph.simplifyHierarchyGraph();
2274 // skeletonGraph.combineRedundantNodes(false);
2275 // skeletonGraph.removeRedundantEdges();
2276 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
2280 private void debug_writeHierarchyDotFiles() {
2282 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2283 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2284 Descriptor desc = (Descriptor) iterator.next();
2285 getHierarchyGraph(desc).writeGraph();
2290 private void debug_writeSimpleHierarchyDotFiles() {
2292 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2293 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2294 Descriptor desc = (Descriptor) iterator.next();
2295 getHierarchyGraph(desc).writeGraph();
2296 getSimpleHierarchyGraph(desc).writeGraph();
2301 private void debug_writeSkeletonHierarchyDotFiles() {
2303 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2304 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2305 Descriptor desc = (Descriptor) iterator.next();
2306 getSkeletonHierarchyGraph(desc).writeGraph();
2311 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
2313 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2314 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2315 Descriptor desc = (Descriptor) iterator.next();
2316 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
2321 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
2322 return mapDescriptorToSimpleHierarchyGraph.get(d);
2325 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
2326 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
2327 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2329 return mapDescriptorToSkeletonHierarchyGraph.get(d);
2332 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
2333 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
2334 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2336 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
2339 private void constructHierarchyGraph() {
2341 // do fixed-point analysis
2343 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2345 // Collections.sort(descriptorListToAnalyze, new
2346 // Comparator<MethodDescriptor>() {
2347 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
2348 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
2352 // current descriptors to visit in fixed-point interprocedural analysis,
2353 // prioritized by dependency in the call graph
2354 methodDescriptorsToVisitStack.clear();
2356 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2357 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2359 while (!descriptorListToAnalyze.isEmpty()) {
2360 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2361 methodDescriptorsToVisitStack.add(md);
2364 // analyze scheduled methods until there are no more to visit
2365 while (!methodDescriptorsToVisitStack.isEmpty()) {
2366 // start to analyze leaf node
2367 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2369 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
2370 // MethodSummary methodSummary = new MethodSummary(md);
2372 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
2373 // curMethodInfo = methodInfo;
2375 System.out.println();
2376 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
2378 constructHierarchyGraph(md, hierarchyGraph);
2380 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
2381 // MethodSummary prevMethodSummary = getMethodSummary(md);
2383 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
2385 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
2386 // mapDescToLocationSummary.put(md, methodSummary);
2388 // results for callee changed, so enqueue dependents caller for
2390 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
2391 while (depsItr.hasNext()) {
2392 MethodDescriptor methodNext = depsItr.next();
2393 if (!methodDescriptorsToVisitStack.contains(methodNext)
2394 && methodDescriptorToVistSet.contains(methodNext)) {
2395 methodDescriptorsToVisitStack.add(methodNext);
2404 while (!toAnalyzeIsEmpty()) {
2405 ClassDescriptor cd = toAnalyzeNext();
2406 HierarchyGraph graph = getHierarchyGraph(cd);
2407 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2408 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
2409 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
2410 graph.getHNode(fieldDesc);
2415 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2416 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2417 Descriptor key = (Descriptor) iterator.next();
2418 HierarchyGraph graph = getHierarchyGraph(key);
2420 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
2421 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
2422 HNode node = (HNode) iterator2.next();
2423 if (!node.isSkeleton() && !node.isCombinationNode()) {
2424 if (graph.getIncomingNodeSet(node).size() == 0) {
2425 nodeToBeConnected.add(node);
2430 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
2431 HNode node = (HNode) iterator2.next();
2432 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
2433 graph.addEdge(graph.getHNode(TOPDESC), node);
2440 private HierarchyGraph getHierarchyGraph(Descriptor d) {
2441 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
2442 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
2444 return mapDescriptorToHierarchyGraph.get(d);
2447 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
2449 // visit each node of method flow graph
2450 FlowGraph fg = getFlowGraph(md);
2451 // Set<FlowNode> nodeSet = fg.getNodeSet();
2453 Set<FlowEdge> edgeSet = fg.getEdgeSet();
2455 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
2456 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
2457 Descriptor desc = (Descriptor) iterator.next();
2458 methodGraph.getHNode(desc).setSkeleton(true);
2461 // for the method lattice, we need to look at the first element of
2462 // NTuple<Descriptor>
2463 boolean hasGlobalAccess = false;
2464 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2465 // FlowNode originalSrcNode = (FlowNode) iterator.next();
2466 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
2467 FlowEdge edge = (FlowEdge) iterator.next();
2469 FlowNode originalSrcNode = fg.getFlowNode(edge.getInitTuple());
2470 Set<FlowNode> sourceNodeSet = new HashSet<FlowNode>();
2471 if (originalSrcNode instanceof FlowReturnNode) {
2472 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
2473 System.out.println("rnode=" + rnode);
2474 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
2475 for (Iterator iterator2 = tupleSet.iterator(); iterator2.hasNext();) {
2476 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator2.next();
2477 sourceNodeSet.add(fg.getFlowNode(nTuple));
2478 System.out.println("&&&SOURCE fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
2481 sourceNodeSet.add(originalSrcNode);
2484 // System.out.println("---sourceNodeSet=" + sourceNodeSet + " from originalSrcNode="
2485 // + originalSrcNode);
2487 for (Iterator iterator3 = sourceNodeSet.iterator(); iterator3.hasNext();) {
2488 FlowNode srcNode = (FlowNode) iterator3.next();
2490 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
2491 Descriptor srcLocalDesc = srcNodeTuple.get(0);
2493 if (srcLocalDesc instanceof InterDescriptor
2494 && ((InterDescriptor) srcLocalDesc).getMethodArgIdxPair() != null) {
2496 if (srcNode.getCompositeLocation() == null) {
2501 // if the srcNode is started with the global descriptor
2502 // need to set as a skeleton node
2503 if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
2504 hasGlobalAccess = true;
2507 // Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(originalSrcNode);
2508 // for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
2509 // FlowEdge outEdge = (FlowEdge) iterator2.next();
2510 // FlowNode originalDstNode = outEdge.getDst();
2511 FlowNode originalDstNode = fg.getFlowNode(edge.getEndTuple());
2513 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
2514 if (originalDstNode instanceof FlowReturnNode) {
2515 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
2516 // System.out.println("\n-returnNode=" + rnode);
2517 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
2518 for (Iterator iterator4 = tupleSet.iterator(); iterator4.hasNext();) {
2519 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator4.next();
2520 dstNodeSet.add(fg.getFlowNode(nTuple));
2521 System.out.println("&&&DST fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
2524 dstNodeSet.add(originalDstNode);
2526 // System.out.println("---dstNodeSet=" + dstNodeSet);
2527 for (Iterator iterator4 = dstNodeSet.iterator(); iterator4.hasNext();) {
2528 FlowNode dstNode = (FlowNode) iterator4.next();
2530 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
2531 Descriptor dstLocalDesc = dstNodeTuple.get(0);
2533 if (dstLocalDesc instanceof InterDescriptor
2534 && ((InterDescriptor) dstLocalDesc).getMethodArgIdxPair() != null) {
2535 if (dstNode.getCompositeLocation() == null) {
2536 System.out.println("%%%%%%%%%%%%%SKIP=" + dstNode);
2541 // if (outEdge.getInitTuple().equals(srcNodeTuple)
2542 // && outEdge.getEndTuple().equals(dstNodeTuple)) {
2544 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
2545 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
2547 System.out.println("-srcCurTuple=" + srcCurTuple + " dstCurTuple=" + dstCurTuple
2548 + " srcNode=" + srcNode + " dstNode=" + dstNode);
2550 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
2551 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
2553 // value flows between fields
2554 Descriptor desc = srcCurTuple.get(0);
2555 ClassDescriptor classDesc;
2557 if (desc.equals(GLOBALDESC)) {
2558 classDesc = md.getClassDesc();
2560 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
2561 classDesc = varDesc.getType().getClassDesc();
2563 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
2565 } else if ((srcCurTuple.size() == 1 && dstCurTuple.size() == 1)
2566 || ((srcCurTuple.size() > 1 || dstCurTuple.size() > 1) && !srcCurTuple.get(0).equals(
2567 dstCurTuple.get(0)))) {
2569 // value flow between a primitive local var - a primitive local var or local var -
2572 Descriptor srcDesc = srcCurTuple.get(0);
2573 Descriptor dstDesc = dstCurTuple.get(0);
2575 methodGraph.addEdge(srcDesc, dstDesc);
2577 if (fg.isParamDesc(srcDesc)) {
2578 methodGraph.setParamHNode(srcDesc);
2580 if (fg.isParamDesc(dstDesc)) {
2581 methodGraph.setParamHNode(dstDesc);
2595 // If the method accesses static fields
2596 // set hasGloabalAccess true in the method summary.
2597 if (hasGlobalAccess) {
2598 getMethodSummary(md).setHasGlobalAccess();
2600 methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
2602 if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2603 // if the current method contains the event loop
2604 // we need to set all nodes of the hierarchy graph as a skeleton node
2605 Set<HNode> hnodeSet = methodGraph.getNodeSet();
2606 for (Iterator iterator = hnodeSet.iterator(); iterator.hasNext();) {
2607 HNode hnode = (HNode) iterator.next();
2608 hnode.setSkeleton(true);
2614 private MethodSummary getMethodSummary(MethodDescriptor md) {
2615 if (!mapDescToLocationSummary.containsKey(md)) {
2616 mapDescToLocationSummary.put(md, new MethodSummary(md));
2618 return (MethodSummary) mapDescToLocationSummary.get(md);
2621 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
2623 String classSymbol = cd.getSymbol();
2624 int idx = classSymbol.lastIndexOf("$");
2626 classSymbol = classSymbol.substring(idx + 1);
2629 String pattern = "class " + classSymbol + " ";
2630 if (strLine.indexOf(pattern) != -1) {
2631 mapDescToDefinitionLine.put(cd, lineNum);
2635 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
2637 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
2638 MethodDescriptor md = (MethodDescriptor) iterator.next();
2639 String pattern = md.getMethodDeclaration();
2640 if (strLine.indexOf(pattern) != -1) {
2641 mapDescToDefinitionLine.put(md, lineNum);
2642 methodSet.remove(md);
2649 private void readOriginalSourceFiles() {
2651 SymbolTable classtable = state.getClassSymbolTable();
2653 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
2654 classDescSet.addAll(classtable.getValueSet());
2657 // inefficient implement. it may re-visit the same file if the file
2658 // contains more than one class definitions.
2659 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
2660 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2662 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
2663 methodSet.addAll(cd.getMethodTable().getValueSet());
2665 String sourceFileName = cd.getSourceFileName();
2666 Vector<String> lineVec = new Vector<String>();
2668 mapFileNameToLineVector.put(sourceFileName, lineVec);
2670 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
2673 lineVec.add(""); // the index is started from 1.
2674 while ((strLine = in.readLine()) != null) {
2675 lineVec.add(lineNum, strLine);
2676 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
2677 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
2683 } catch (IOException e) {
2684 e.printStackTrace();
2689 private String generateLatticeDefinition(Descriptor desc) {
2691 Set<String> sharedLocSet = new HashSet<String>();
2693 SSJavaLattice<String> lattice = getLattice(desc);
2694 String rtr = "@LATTICE(\"";
2696 Map<String, Set<String>> map = lattice.getTable();
2697 Set<String> keySet = map.keySet();
2698 boolean first = true;
2699 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2700 String key = (String) iterator.next();
2701 if (!key.equals(lattice.getTopItem())) {
2702 Set<String> connectedSet = map.get(key);
2704 if (connectedSet.size() == 1) {
2705 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
2713 if (lattice.isSharedLoc(key)) {
2714 rtr += "," + key + "*";
2719 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
2720 String loc = (String) iterator2.next();
2721 if (!loc.equals(lattice.getBottomItem())) {
2728 rtr += loc + "<" + key;
2729 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
2730 rtr += "," + key + "*";
2731 sharedLocSet.add(key);
2733 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
2734 rtr += "," + loc + "*";
2735 sharedLocSet.add(loc);
2743 if (desc instanceof MethodDescriptor) {
2744 System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
2746 MethodDescriptor md = (MethodDescriptor) desc;
2747 MethodSummary methodSummary = getMethodSummary(md);
2749 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
2750 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc) && returnType != null
2751 && (!returnType.isVoid())) {
2752 CompositeLocation returnLoc = methodSummary.getRETURNLoc();
2753 if (returnLoc.getSize() == 1) {
2754 String returnLocStr = generateLocationAnnoatation(methodSummary.getRETURNLoc());
2755 if (rtr.indexOf(returnLocStr) == -1) {
2756 rtr += "," + returnLocStr;
2762 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
2763 if (returnType != null && (!returnType.isVoid())) {
2765 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
2768 CompositeLocation pcLoc = methodSummary.getPCLoc();
2769 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
2770 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
2774 if (!md.isStatic()) {
2775 rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
2777 rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
2786 private void generateAnnoatedCode() {
2788 readOriginalSourceFiles();
2791 while (!toAnalyzeIsEmpty()) {
2792 ClassDescriptor cd = toAnalyzeNext();
2794 setupToAnalazeMethod(cd);
2796 String sourceFileName = cd.getSourceFileName();
2798 if (cd.isInterface()) {
2802 int classDefLine = mapDescToDefinitionLine.get(cd);
2803 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
2805 LocationSummary fieldLocSummary = getLocationSummary(cd);
2807 String fieldLatticeDefStr = generateLatticeDefinition(cd);
2808 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
2809 sourceVec.set(classDefLine, annoatedSrc);
2811 // generate annotations for field declarations
2812 // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
2813 Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
2815 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2816 FieldDescriptor fd = (FieldDescriptor) iter.next();
2818 String locAnnotationStr;
2819 // CompositeLocation inferLoc = inferLocMap.get(fd);
2820 String locName = mapFieldNameToLocName.get(fd.getSymbol());
2822 if (locName != null) {
2823 // infer loc is null if the corresponding field is static and final
2824 // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2825 locAnnotationStr = "@LOC(\"" + locName + "\")";
2826 int fdLineNum = fd.getLineNum();
2827 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
2828 String fieldDeclaration = fd.toString();
2829 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
2830 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
2831 sourceVec.set(fdLineNum, annoatedStr);
2836 while (!toAnalyzeMethodIsEmpty()) {
2837 MethodDescriptor md = toAnalyzeMethodNext();
2839 if (!ssjava.needTobeAnnotated(md)) {
2843 SSJavaLattice<String> methodLattice = md2lattice.get(md);
2844 if (methodLattice != null) {
2846 int methodDefLine = md.getLineNum();
2848 // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
2849 // Map<Descriptor, CompositeLocation> methodInferLocMap =
2850 // methodLocInfo.getMapDescToInferLocation();
2852 MethodSummary methodSummary = getMethodSummary(md);
2854 Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
2855 methodSummary.getMapVarDescToInferCompositeLocation();
2856 System.out.println("-----md=" + md);
2857 System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
2859 Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
2861 Set<String> localLocElementSet = methodLattice.getElementSet();
2863 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
2864 Descriptor localVarDesc = (Descriptor) iterator.next();
2865 System.out.println("-------localVarDesc=" + localVarDesc);
2866 CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
2868 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
2869 if (!localLocElementSet.contains(localLocIdentifier)) {
2870 methodLattice.put(localLocIdentifier);
2873 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2875 if (!isParameter(md, localVarDesc)) {
2876 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
2877 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
2878 String orgSourceLine = sourceVec.get(varLineNum);
2879 System.out.println("varLineNum=" + varLineNum + " org src=" + orgSourceLine);
2881 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
2882 System.out.println("idx=" + idx
2883 + " generateVarDeclaration((VarDescriptor) localVarDesc)="
2884 + generateVarDeclaration((VarDescriptor) localVarDesc));
2886 String annoatedStr =
2887 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
2888 + orgSourceLine.substring(idx);
2889 sourceVec.set(varLineNum, annoatedStr);
2892 String methodDefStr = sourceVec.get(methodDefLine);
2895 getParamLocation(methodDefStr,
2896 generateVarDeclaration((VarDescriptor) localVarDesc));
2897 System.out.println("methodDefStr=" + methodDefStr + " localVarDesc=" + localVarDesc
2901 String annoatedStr =
2902 methodDefStr.substring(0, idx) + locAnnotationStr + " "
2903 + methodDefStr.substring(idx);
2904 sourceVec.set(methodDefLine, annoatedStr);
2909 // check if the lattice has to have the location type for the this
2912 // boolean needToAddthisRef = hasThisReference(md);
2913 // if (localLocElementSet.contains("this")) {
2914 // methodLattice.put("this");
2917 String methodLatticeDefStr = generateLatticeDefinition(md);
2918 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
2919 sourceVec.set(methodDefLine, annoatedStr);
2929 private boolean hasThisReference(MethodDescriptor md) {
2931 FlowGraph fg = getFlowGraph(md);
2932 Set<FlowNode> nodeSet = fg.getNodeSet();
2933 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2934 FlowNode flowNode = (FlowNode) iterator.next();
2935 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
2943 private int getParamLocation(String methodStr, String paramStr) {
2945 String pattern = paramStr + ",";
2947 int idx = methodStr.indexOf(pattern);
2951 pattern = paramStr + ")";
2952 return methodStr.indexOf(pattern);
2957 private String generateVarDeclaration(VarDescriptor varDesc) {
2959 TypeDescriptor td = varDesc.getType();
2960 String rtr = td.toString();
2962 for (int i = 0; i < td.getArrayCount(); i++) {
2966 rtr += " " + varDesc.getName();
2971 private String generateLocationAnnoatation(CompositeLocation loc) {
2974 Location methodLoc = loc.get(0);
2975 rtr += methodLoc.getLocIdentifier();
2977 for (int i = 1; i < loc.getSize(); i++) {
2978 Location element = loc.get(i);
2979 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
2985 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
2986 return getFlowGraph(md).isParamDesc(localVarDesc);
2989 private String extractFileName(String fileName) {
2990 int idx = fileName.lastIndexOf("/");
2994 return fileName.substring(idx + 1);
2999 private void codeGen() {
3001 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
3002 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
3003 String orgFileName = (String) iterator.next();
3004 String outputFileName = extractFileName(orgFileName);
3006 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
3010 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
3011 BufferedWriter out = new BufferedWriter(fileWriter);
3013 for (int i = 0; i < sourceVec.size(); i++) {
3014 out.write(sourceVec.get(i));
3018 } catch (IOException e) {
3019 e.printStackTrace();
3026 private void checkLattices() {
3028 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
3030 // current descriptors to visit in fixed-point interprocedural analysis,
3032 // dependency in the call graph
3033 methodDescriptorsToVisitStack.clear();
3035 // descriptorListToAnalyze.removeFirst();
3037 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
3038 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
3040 while (!descriptorListToAnalyze.isEmpty()) {
3041 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
3042 checkLatticesOfVirtualMethods(md);
3047 private void debug_writeLatticeDotFile() {
3048 // generate lattice dot file
3052 while (!toAnalyzeIsEmpty()) {
3053 ClassDescriptor cd = toAnalyzeNext();
3055 setupToAnalazeMethod(cd);
3057 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
3058 if (classLattice != null) {
3059 ssjava.writeLatticeDotFile(cd, null, classLattice);
3060 debug_printDescriptorToLocNameMapping(cd);
3063 while (!toAnalyzeMethodIsEmpty()) {
3064 MethodDescriptor md = toAnalyzeMethodNext();
3065 SSJavaLattice<String> methodLattice = md2lattice.get(md);
3066 if (methodLattice != null) {
3067 ssjava.writeLatticeDotFile(cd, md, methodLattice);
3068 debug_printDescriptorToLocNameMapping(md);
3075 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
3077 LocationInfo info = getLocationInfo(desc);
3078 System.out.println("## " + desc + " ##");
3079 System.out.println(info.getMapDescToInferLocation());
3080 LocationInfo locInfo = getLocationInfo(desc);
3081 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
3082 System.out.println("###################");
3086 private void calculateExtraLocations() {
3088 LinkedList<MethodDescriptor> methodDescList = ssjava.getSortedDescriptors();
3089 for (Iterator iterator = methodDescList.iterator(); iterator.hasNext();) {
3090 MethodDescriptor md = (MethodDescriptor) iterator.next();
3091 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
3092 calculateExtraLocations(md);
3098 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
3100 if (!md.isStatic()) {
3101 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3102 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3104 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
3105 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
3106 if (!md.equals(mdCallee)) {
3107 checkConsistency(md, mdCallee);
3115 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
3117 // check that two lattice have the same relations between parameters(+PC
3118 // LOC, GLOBAL_LOC RETURN LOC)
3120 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
3121 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
3123 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
3124 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
3126 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
3127 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
3129 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
3131 // add location types of paramters
3132 for (int idx = 0; idx < numParam; idx++) {
3133 list1.add(paramMap1.get(Integer.valueOf(idx)));
3134 list2.add(paramMap2.get(Integer.valueOf(idx)));
3137 // add program counter location
3138 list1.add(locInfo1.getPCLoc());
3139 list2.add(locInfo2.getPCLoc());
3141 if (!md1.getReturnType().isVoid()) {
3142 // add return value location
3143 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
3144 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
3149 // add global location type
3150 if (md1.isStatic()) {
3151 CompositeLocation globalLoc1 =
3152 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
3153 CompositeLocation globalLoc2 =
3154 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
3155 list1.add(globalLoc1);
3156 list2.add(globalLoc2);
3159 for (int i = 0; i < list1.size(); i++) {
3160 CompositeLocation locA1 = list1.get(i);
3161 CompositeLocation locA2 = list2.get(i);
3162 for (int k = 0; k < list1.size(); k++) {
3164 CompositeLocation locB1 = list1.get(k);
3165 CompositeLocation locB2 = list2.get(k);
3166 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
3168 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
3171 throw new Error("The method " + md1 + " is not consistent with the method " + md2
3172 + ".:: They have a different ordering relation between locations (" + locA1 + ","
3173 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
3181 private String getSymbol(int idx, FlowNode node) {
3182 Descriptor desc = node.getDescTuple().get(idx);
3183 return desc.getSymbol();
3186 private Descriptor getDescriptor(int idx, FlowNode node) {
3187 Descriptor desc = node.getDescTuple().get(idx);
3191 private void calculatePCLOC(MethodDescriptor md) {
3193 System.out.println("#CalculatePCLOC");
3194 MethodSummary methodSummary = getMethodSummary(md);
3195 FlowGraph fg = getFlowGraph(md);
3196 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
3198 // calculate the initial program counter location
3199 // PC location is higher than location types of parameters which has incoming flows.
3201 Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
3202 Set<Descriptor> paramDescNOTHavingInFlowSet = new HashSet<Descriptor>();
3203 // Set<FlowNode> paramNodeNOThavingInFlowSet = new HashSet<FlowNode>();
3205 int numParams = fg.getNumParameters();
3206 for (int i = 0; i < numParams; i++) {
3207 FlowNode paramFlowNode = fg.getParamFlowNode(i);
3208 Descriptor prefix = paramFlowNode.getDescTuple().get(0);
3209 NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
3210 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
3212 Set<FlowNode> inNodeToParamSet = fg.getIncomingNodeSetByPrefix(prefix);
3213 if (inNodeToParamSet.size() > 0) {
3214 // parameter has in-value flows
3216 for (Iterator iterator = inNodeToParamSet.iterator(); iterator.hasNext();) {
3217 FlowNode inNode = (FlowNode) iterator.next();
3218 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(inNode);
3219 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3220 FlowEdge flowEdge = (FlowEdge) iterator2.next();
3221 if (flowEdge.getEndTuple().startsWith(prefix)) {
3222 NTuple<Location> paramLocTupleWithIncomingFlow =
3223 translateToLocTuple(md, flowEdge.getEndTuple());
3224 paramLocTupleHavingInFlowSet.add(paramLocTupleWithIncomingFlow);
3229 // paramLocTupleHavingInFlowSet.add(paramLocTuple);
3231 // paramNodeNOThavingInFlowSet.add(fg.getFlowNode(paramDescTuple));
3232 paramDescNOTHavingInFlowSet.add(prefix);
3236 System.out.println("paramLocTupleHavingInFlowSet=" + paramLocTupleHavingInFlowSet);
3238 if (paramLocTupleHavingInFlowSet.size() > 0
3239 && !coversAllParamters(md, fg, paramLocTupleHavingInFlowSet)) {
3241 // Here, generates a location in the method lattice that is higher than the
3242 // paramLocTupleHavingInFlowSet
3243 NTuple<Location> pcLocTuple =
3244 generateLocTupleRelativeTo(md, paramLocTupleHavingInFlowSet, PCLOC);
3246 NTuple<Descriptor> pcDescTuple = translateToDescTuple(pcLocTuple);
3248 // System.out.println("pcLoc=" + pcLocTuple);
3250 CompositeLocation curPCLoc = methodSummary.getPCLoc();
3251 if (curPCLoc.get(0).isTop() || pcLocTuple.size() > curPCLoc.getSize()) {
3252 methodSummary.setPCLoc(new CompositeLocation(pcLocTuple));
3254 Set<FlowNode> flowNodeLowerthanPCLocSet = new HashSet<FlowNode>();
3255 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
3256 // add ordering relations s.t. PCLOC is higher than all flow nodes except the set of
3257 // parameters that do not have incoming flows
3258 for (Iterator iterator = fg.getNodeSet().iterator(); iterator.hasNext();) {
3259 FlowNode node = (FlowNode) iterator.next();
3261 if (!(node instanceof FlowReturnNode)) {
3262 if (!paramDescNOTHavingInFlowSet.contains(node.getCurrentDescTuple().get(0))) {
3263 flowNodeLowerthanPCLocSet.add(node);
3264 fg.addValueFlowEdge(pcDescTuple, node.getDescTuple());
3265 subGlobalFlowGraph.addValueFlowEdge(pcLocTuple,
3266 translateToLocTuple(md, node.getDescTuple()));
3269 System.out.println("***SKIP PCLOC -> RETURNLOC=" + node);
3273 fg.getFlowNode(translateToDescTuple(pcLocTuple)).setSkeleton(true);
3275 if (pcLocTuple.get(0).getLocDescriptor().equals(md.getThis())) {
3276 System.out.println("#########################################");
3277 for (Iterator iterator = flowNodeLowerthanPCLocSet.iterator(); iterator.hasNext();) {
3278 FlowNode lowerNode = (FlowNode) iterator.next();
3279 if (lowerNode.getCompositeLocation() == null) {
3280 NTuple<Location> lowerLocTuple = translateToLocTuple(md, lowerNode.getDescTuple());
3281 CompositeLocation newComp =
3282 calculateCompositeLocationFromSubGlobalGraph(md, lowerNode);
3283 if (newComp != null) {
3284 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(lowerLocTuple.get(0),
3286 lowerNode.setCompositeLocation(newComp);
3287 System.out.println("NEW COMP LOC=" + newComp + " to lowerNode=" + lowerNode);
3300 private boolean coversAllParamters(MethodDescriptor md, FlowGraph fg,
3301 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
3303 int numParam = fg.getNumParameters();
3304 int size = paramLocTupleHavingInFlowSet.size();
3306 if (!md.isStatic()) {
3308 // if the method is not static && there is a parameter composite location &&
3309 // it is started with 'this',
3310 // paramLocTupleHavingInFlowSet need to have 'this' parameter.
3312 FlowNode thisParamNode = fg.getParamFlowNode(0);
3313 NTuple<Location> thisParamLocTuple =
3314 translateToLocTuple(md, thisParamNode.getCurrentDescTuple());
3316 if (!paramLocTupleHavingInFlowSet.contains(thisParamLocTuple)) {
3318 for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
3319 NTuple<Location> paramTuple = (NTuple<Location>) iterator.next();
3320 if (paramTuple.size() > 1 && paramTuple.get(0).getLocDescriptor().equals(md.getThis())) {
3321 // paramLocTupleHavingInFlowSet.add(thisParamLocTuple);
3330 if (size == numParam) {
3338 private void calculateRETURNLOC(MethodDescriptor md) {
3340 System.out.println("#calculateRETURNLOC= " + md);
3342 // calculate a return location:
3343 // the return location type is lower than all parameters and the location of return values
3344 MethodSummary methodSummary = getMethodSummary(md);
3345 // if (methodSummary.getRETURNLoc() != null) {
3346 // System.out.println("$HERE?");
3350 FlowGraph fg = getFlowGraph(md);
3351 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
3352 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
3354 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
3355 // first, generate the set of return value location types that starts
3356 // with 'this' reference
3358 Set<FlowNode> paramFlowNodeFlowingToReturnValueSet = getParamNodeFlowingToReturnValue(md);
3359 // System.out.println("paramFlowNodeFlowingToReturnValueSet="
3360 // + paramFlowNodeFlowingToReturnValueSet);
3362 Set<NTuple<Location>> tupleToBeHigherThanReturnLocSet = new HashSet<NTuple<Location>>();
3363 for (Iterator iterator = paramFlowNodeFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
3364 FlowNode fn = (FlowNode) iterator.next();
3365 NTuple<Descriptor> paramDescTuple = fn.getCurrentDescTuple();
3366 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, paramDescTuple));
3369 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
3370 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
3371 FlowNode returnNode = (FlowNode) iterator.next();
3372 NTuple<Descriptor> returnDescTuple = returnNode.getCurrentDescTuple();
3373 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, returnDescTuple));
3375 System.out.println("-flow graph's returnNodeSet=" + returnNodeSet);
3376 System.out.println("tupleSetToBeHigherThanReturnLoc=" + tupleToBeHigherThanReturnLocSet);
3378 // Here, generates a return location in the method lattice that is lower than the
3379 // locFlowingToReturnValueSet
3380 NTuple<Location> returnLocTuple =
3381 generateLocTupleRelativeTo(md, tupleToBeHigherThanReturnLocSet, RLOC);
3383 // System.out.println("returnLocTuple=" + returnLocTuple);
3384 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnLocTuple);
3385 CompositeLocation curReturnLoc = methodSummary.getRETURNLoc();
3386 if (curReturnLoc == null || returnDescTuple.size() > curReturnLoc.getSize()) {
3387 methodSummary.setRETURNLoc(new CompositeLocation(returnLocTuple));
3389 for (Iterator iterator = tupleToBeHigherThanReturnLocSet.iterator(); iterator.hasNext();) {
3390 NTuple<Location> higherTuple = (NTuple<Location>) iterator.next();
3391 fg.addValueFlowEdge(translateToDescTuple(higherTuple), returnDescTuple);
3393 fg.getFlowNode(returnDescTuple).setSkeleton(true);
3397 // makes sure that PCLOC is higher than RETURNLOC
3398 CompositeLocation pcLoc = methodSummary.getPCLoc();
3399 if (!pcLoc.get(0).isTop()) {
3400 NTuple<Descriptor> pcLocDescTuple = translateToDescTuple(pcLoc.getTuple());
3401 fg.addValueFlowEdge(pcLocDescTuple, returnDescTuple);
3408 private void calculateExtraLocations(MethodDescriptor md) {
3409 // calcualte pcloc, returnloc,...
3411 System.out.println("\nSSJAVA:Calculate PCLOC/RETURNLOC locations: " + md);
3414 calculateRETURNLOC(md);
3418 private NTuple<Location> generateLocTupleRelativeTo(MethodDescriptor md,
3419 Set<NTuple<Location>> paramLocTupleHavingInFlowSet, String locNamePrefix) {
3421 // System.out.println("-generateLocTupleRelativeTo=" + paramLocTupleHavingInFlowSet);
3423 NTuple<Location> higherLocTuple = new NTuple<Location>();
3425 VarDescriptor thisVarDesc = md.getThis();
3426 // check if all paramter loc tuple is started with 'this' reference
3427 boolean hasParamNotStartedWithThisRef = false;
3431 Set<NTuple<Location>> paramLocTupleStartedWithThis = new HashSet<NTuple<Location>>();
3433 next: for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
3434 NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
3435 Descriptor paramLocalDesc = paramLocTuple.get(0).getLocDescriptor();
3436 if (!paramLocalDesc.equals(thisVarDesc)) {
3438 Set<FlowNode> inNodeSet = getFlowGraph(md).getIncomingNodeSetByPrefix(paramLocalDesc);
3439 for (Iterator iterator2 = inNodeSet.iterator(); iterator2.hasNext();) {
3440 FlowNode flowNode = (FlowNode) iterator2.next();
3441 if (flowNode.getDescTuple().startsWith(thisVarDesc)) {
3442 // System.out.println("paramLocTuple=" + paramLocTuple + " is lower than THIS");
3446 hasParamNotStartedWithThisRef = true;
3448 } else if (paramLocTuple.size() > 1) {
3449 paramLocTupleStartedWithThis.add(paramLocTuple);
3450 if (minSize == 0 || minSize > paramLocTuple.size()) {
3451 minSize = paramLocTuple.size();
3456 // System.out.println("---paramLocTupleStartedWithThis=" + paramLocTupleStartedWithThis);
3457 Descriptor enclosingDesc = md;
3458 if (hasParamNotStartedWithThisRef) {
3459 // in this case, PCLOC will be the local location
3461 // all parameter is started with 'this', so PCLOC will be set relative to the composite
3462 // location started with 'this'.
3463 // for (int idx = 0; idx < minSize - 1; idx++) {
3464 for (int idx = 0; idx < 1; idx++) {
3465 Set<Descriptor> locDescSet = new HashSet<Descriptor>();
3466 Location curLoc = null;
3467 NTuple<Location> paramLocTuple = null;
3468 for (Iterator iterator = paramLocTupleStartedWithThis.iterator(); iterator.hasNext();) {
3469 paramLocTuple = (NTuple<Location>) iterator.next();
3470 // System.out.println("-----paramLocTuple=" + paramLocTuple + " idx=" + idx);
3471 curLoc = paramLocTuple.get(idx);
3472 Descriptor locDesc = curLoc.getLocDescriptor();
3473 locDescSet.add(locDesc);
3475 // System.out.println("-----locDescSet=" + locDescSet + " idx=" + idx);
3476 if (locDescSet.size() != 1) {
3479 Location newLocElement = new Location(curLoc.getDescriptor(), curLoc.getLocDescriptor());
3480 System.out.println("newLocElement" + newLocElement);
3481 higherLocTuple.add(newLocElement);
3482 enclosingDesc = getClassTypeDescriptor(curLoc.getLocDescriptor());
3487 String locIdentifier = locNamePrefix + (locSeed++);
3488 NameDescriptor locDesc = new NameDescriptor(locIdentifier);
3489 Location newLoc = new Location(enclosingDesc, locDesc);
3490 higherLocTuple.add(newLoc);
3491 System.out.println("---new loc tuple=" + higherLocTuple);
3493 return higherLocTuple;
3497 public ClassDescriptor getClassTypeDescriptor(Descriptor in) {
3499 if (in instanceof VarDescriptor) {
3500 return ((VarDescriptor) in).getType().getClassDesc();
3501 } else if (in instanceof FieldDescriptor) {
3502 return ((FieldDescriptor) in).getType().getClassDesc();
3504 // else if (in instanceof LocationDescriptor) {
3505 // // here is the case that the descriptor 'in' is the last element of the assigned composite
3507 // return ((VarDescriptor) locTuple.get(0).getLocDescriptor()).getType().getClassDesc();
3515 private Set<NTuple<Location>> calculateHighestLocTupleSet(
3516 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
3518 Set<NTuple<Location>> highestSet = new HashSet<NTuple<Location>>();
3520 Iterator<NTuple<Location>> iterator = paramLocTupleHavingInFlowSet.iterator();
3521 NTuple<Location> highest = iterator.next();
3523 for (; iterator.hasNext();) {
3524 NTuple<Location> curLocTuple = (NTuple<Location>) iterator.next();
3525 if (isHigherThan(curLocTuple, highest)) {
3526 // System.out.println(curLocTuple + " is greater than " + highest);
3527 highest = curLocTuple;
3531 highestSet.add(highest);
3533 MethodDescriptor md = (MethodDescriptor) highest.get(0).getDescriptor();
3534 VarDescriptor thisVarDesc = md.getThis();
3536 // System.out.println("highest=" + highest);
3538 for (Iterator<NTuple<Location>> iter = paramLocTupleHavingInFlowSet.iterator(); iter.hasNext();) {
3539 NTuple<Location> curLocTuple = iter.next();
3541 if (!curLocTuple.equals(highest) && !hasOrderingRelation(highest, curLocTuple)) {
3543 // System.out.println("add it to the highest set=" + curLocTuple);
3544 highestSet.add(curLocTuple);
3553 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
3554 Set<String> higherLocSet = new HashSet<String>();
3556 Set<String> locSet = lattice.getTable().keySet();
3557 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
3558 String element = (String) iterator.next();
3559 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
3560 higherLocSet.add(element);
3563 return higherLocSet;
3566 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
3567 Set<CompositeLocation> set) {
3569 CompositeLocation lowest = set.iterator().next();
3571 if (set.size() == 1) {
3575 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
3576 CompositeLocation loc = (CompositeLocation) iterator.next();
3578 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
3579 // if there is a case where composite locations are incomparable, just
3584 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
3591 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
3592 CompositeLocation comp2) {
3594 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
3596 for (int idx = 0; idx < size; idx++) {
3597 Location loc1 = comp1.get(idx);
3598 Location loc2 = comp2.get(idx);
3600 Descriptor desc1 = loc1.getDescriptor();
3601 Descriptor desc2 = loc2.getDescriptor();
3603 if (!desc1.equals(desc2)) {
3604 throw new Error("Fail to compare " + comp1 + " and " + comp2);
3607 String symbol1 = loc1.getLocIdentifier();
3608 String symbol2 = loc2.getLocIdentifier();
3610 SSJavaLattice<String> lattice;
3612 lattice = methodLattice;
3614 lattice = getLattice(desc1);
3617 if (symbol1.equals(symbol2)) {
3619 } else if (!lattice.isComparable(symbol1, symbol2)) {
3628 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
3629 CompositeLocation comp2) {
3631 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
3633 for (int idx = 0; idx < size; idx++) {
3634 Location loc1 = comp1.get(idx);
3635 Location loc2 = comp2.get(idx);
3637 Descriptor desc1 = loc1.getDescriptor();
3638 Descriptor desc2 = loc2.getDescriptor();
3640 if (!desc1.equals(desc2)) {
3641 throw new Error("Fail to compare " + comp1 + " and " + comp2);
3644 String symbol1 = loc1.getLocIdentifier();
3645 String symbol2 = loc2.getLocIdentifier();
3647 SSJavaLattice<String> lattice;
3649 lattice = methodLattice;
3651 lattice = getLattice(desc1);
3654 if (symbol1.equals(symbol2)) {
3656 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
3667 private GlobalFlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
3669 if (!mapMethodDescriptorToSubGlobalFlowGraph.containsKey(md)) {
3670 mapMethodDescriptorToSubGlobalFlowGraph.put(md, new GlobalFlowGraph(md));
3672 return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
3675 private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
3676 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
3678 System.out.println("-propagateFlowsToCallerWithNoCompositeLocation=" + min.printNode(0));
3679 // if the parameter A reaches to the parameter B
3680 // then, add an edge the argument A -> the argument B to the caller's flow
3683 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
3684 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
3685 int numParam = calleeFlowGraph.getNumParameters();
3687 for (int i = 0; i < numParam; i++) {
3688 for (int k = 0; k < numParam; k++) {
3692 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
3693 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
3695 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
3696 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
3698 // check if the callee propagates an ordering constraints through
3701 // Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
3702 Set<FlowNode> localReachSet =
3703 calleeFlowGraph.getReachableSetFrom(paramNode1.getDescTuple());
3705 NTuple<Descriptor> paramDescTuple1 = paramNode1.getCurrentDescTuple();
3706 NTuple<Descriptor> paramDescTuple2 = paramNode2.getCurrentDescTuple();
3708 // System.out.println("-param1CurTuple=" + paramDescTuple1 + " param2CurTuple="
3709 // + paramDescTuple2);
3710 // System.out.println("-- localReachSet from param1=" + localReachSet);
3712 if (paramDescTuple1.get(0).equals(paramDescTuple2.get(0))) {
3713 // if two parameters share the same prefix
3714 // it already has been assigned to a composite location
3715 // so we don't need to add an additional ordering relation caused by these two
3720 if (arg1Tuple.size() > 0 && arg2Tuple.size() > 0
3721 && containsPrefix(paramNode2.getDescTuple().get(0), localReachSet)) {
3722 // need to propagate an ordering relation s.t. arg1 is higher
3724 // System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
3726 // add a new flow between the corresponding arguments.
3727 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
3728 // System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
3731 // .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
3735 // System.out.println();
3740 // if a parameter has a composite location and the first element of the parameter location
3741 // matches the callee's 'this'
3742 // we have a more specific constraint: the caller's corresponding argument is higher than the
3743 // parameter location which is translated into the caller
3745 for (int idx = 0; idx < numParam; idx++) {
3746 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
3747 CompositeLocation compLoc = paramNode.getCompositeLocation();
3748 System.out.println("paramNode=" + paramNode + " compLoc=" + compLoc);
3749 if (compLoc != null && compLoc.get(0).getLocDescriptor().equals(min.getMethod().getThis())) {
3750 System.out.println("$$$COMPLOC CASE=" + compLoc + " idx=" + idx);
3752 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
3753 System.out.println("--- argTuple=" + argTuple + " current compLoc="
3754 + callerFlowGraph.getFlowNode(argTuple).getCompositeLocation());
3756 NTuple<Descriptor> translatedParamTuple =
3757 translateCompositeLocationToCaller(idx, min, compLoc);
3758 System.out.println("add a flow edge= " + argTuple + "->" + translatedParamTuple);
3759 callerFlowGraph.addValueFlowEdge(argTuple, translatedParamTuple);
3761 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
3762 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
3763 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
3764 callerFlowGraph.addValueFlowEdge(translateToDescTuple(pcLocTuple), translatedParamTuple);
3772 private boolean containsPrefix(Descriptor prefixDesc, Set<FlowNode> set) {
3774 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
3775 FlowNode flowNode = (FlowNode) iterator.next();
3776 if (flowNode.getDescTuple().startsWith(prefixDesc)) {
3777 System.out.println("FOUND=" + flowNode);
3784 private NTuple<Descriptor> translateCompositeLocationToCaller(int idx, MethodInvokeNode min,
3785 CompositeLocation compLocForParam1) {
3787 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
3789 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
3790 for (int i = 0; i < baseTuple.size(); i++) {
3791 tuple.add(baseTuple.get(i));
3794 for (int i = 1; i < compLocForParam1.getSize(); i++) {
3795 Location loc = compLocForParam1.get(i);
3796 tuple.add(loc.getLocDescriptor());
3802 private CompositeLocation generateCompositeLocation(NTuple<Location> prefixLocTuple) {
3804 System.out.println("generateCompositeLocation=" + prefixLocTuple);
3806 CompositeLocation newCompLoc = new CompositeLocation();
3807 for (int i = 0; i < prefixLocTuple.size(); i++) {
3808 newCompLoc.addLocation(prefixLocTuple.get(i));
3811 Descriptor lastDescOfPrefix = prefixLocTuple.get(prefixLocTuple.size() - 1).getLocDescriptor();
3813 ClassDescriptor enclosingDescriptor;
3814 if (lastDescOfPrefix instanceof FieldDescriptor) {
3815 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
3816 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
3817 } else if (lastDescOfPrefix.equals(GLOBALDESC)) {
3818 MethodDescriptor currentMethodDesc = (MethodDescriptor) prefixLocTuple.get(0).getDescriptor();
3819 enclosingDescriptor = currentMethodDesc.getClassDesc();
3821 // var descriptor case
3822 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
3824 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
3826 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
3827 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
3829 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
3830 newLoc.setLocDescriptor(newLocDescriptor);
3831 newCompLoc.addLocation(newLoc);
3833 // System.out.println("--newCompLoc=" + newCompLoc);
3837 private CompositeLocation generateCompositeLocation(MethodDescriptor md,
3838 NTuple<Descriptor> paramPrefix) {
3840 System.out.println("generateCompositeLocation=" + paramPrefix);
3842 CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
3844 Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
3845 // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
3846 // + lastDescOfPrefix.getClass());
3847 ClassDescriptor enclosingDescriptor;
3848 if (lastDescOfPrefix instanceof FieldDescriptor) {
3849 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
3850 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
3852 // var descriptor case
3853 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
3855 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
3857 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
3858 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
3860 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
3861 newLoc.setLocDescriptor(newLocDescriptor);
3862 newCompLoc.addLocation(newLoc);
3864 // System.out.println("--newCompLoc=" + newCompLoc);
3868 private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
3869 MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
3871 List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
3873 for (int i = 0; i < callerPrefixList.size(); i++) {
3874 NTuple<Descriptor> prefix = callerPrefixList.get(i);
3875 if (prefix.startsWith(baseRef)) {
3876 NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
3877 calleePrefix.add(mdCallee.getThis());
3878 for (int k = 1; k < prefix.size(); k++) {
3879 calleePrefix.add(prefix.get(k));
3881 calleePrefixList.add(calleePrefix);
3885 return calleePrefixList;
3889 public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
3891 CompositeLocation compLoc = new CompositeLocation();
3893 Descriptor enclosingDescriptor = md;
3895 for (int i = 0; i < tuple.size(); i++) {
3896 Descriptor curDescriptor = tuple.get(i);
3897 Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
3898 locElement.setLocDescriptor(curDescriptor);
3899 compLoc.addLocation(locElement);
3901 if (curDescriptor instanceof VarDescriptor) {
3902 enclosingDescriptor = md.getClassDesc();
3903 } else if (curDescriptor instanceof FieldDescriptor) {
3904 enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
3905 } else if (curDescriptor instanceof NameDescriptor) {
3906 // it is "GLOBAL LOC" case!
3907 enclosingDescriptor = GLOBALDESC;
3908 } else if (curDescriptor instanceof InterDescriptor) {
3909 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(curDescriptor);
3911 enclosingDescriptor = null;
3919 private LocationDescriptor generateNewLocationDescriptor() {
3920 return new LocationDescriptor("Loc" + (locSeed++));
3923 private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
3925 // return the index where the prefix shared by tuple1 and tuple2 is ended
3926 // if there is no prefix shared by both of them, return -1
3928 int minSize = tuple1.size();
3929 if (minSize > tuple2.size()) {
3930 minSize = tuple2.size();
3934 for (int i = 0; i < minSize; i++) {
3935 if (!tuple1.get(i).equals(tuple2.get(i))) {
3945 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
3946 NTuple<Location> tuple) {
3948 // first, retrieve inferred location by the local var descriptor
3949 CompositeLocation inferLoc = new CompositeLocation();
3951 CompositeLocation localVarInferLoc =
3952 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
3954 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
3956 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
3957 inferLoc.addLocation(localVarInferLoc.get(i));
3960 for (int i = 1; i < tuple.size(); i++) {
3961 Location cur = tuple.get(i);
3962 Descriptor enclosingDesc = cur.getDescriptor();
3963 Descriptor curDesc = cur.getLocDescriptor();
3965 Location inferLocElement;
3966 if (curDesc == null) {
3967 // in this case, we have a newly generated location.
3968 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
3970 String fieldLocSymbol =
3971 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
3972 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
3973 inferLocElement.setLocDescriptor(curDesc);
3976 inferLoc.addLocation(inferLocElement);
3980 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
3984 public LocationInfo getLocationInfo(Descriptor d) {
3985 if (d instanceof MethodDescriptor) {
3986 return getMethodLocationInfo((MethodDescriptor) d);
3988 return getFieldLocationInfo((ClassDescriptor) d);
3992 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
3994 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
3995 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
3998 return mapMethodDescToMethodLocationInfo.get(md);
4002 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
4004 if (!mapClassToLocationInfo.containsKey(cd)) {
4005 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
4008 return mapClassToLocationInfo.get(cd);
4012 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
4013 NTuple<Location> prefix, NTuple<Location> element) {
4015 if (!map.containsKey(prefix)) {
4016 map.put(prefix, new HashSet<NTuple<Location>>());
4018 map.get(prefix).add(element);
4021 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
4022 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
4023 Descriptor desc = (Descriptor) iterator.next();
4025 if (desc.equals(LocationInference.GLOBALDESC)) {
4027 } else if (desc instanceof VarDescriptor) {
4028 if (!((VarDescriptor) desc).getType().isPrimitive()) {
4031 } else if (desc instanceof FieldDescriptor) {
4032 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
4041 private SSJavaLattice<String> getLattice(Descriptor d) {
4042 if (d instanceof MethodDescriptor) {
4043 return getMethodLattice((MethodDescriptor) d);
4045 return getFieldLattice((ClassDescriptor) d);
4049 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
4050 if (!md2lattice.containsKey(md)) {
4051 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
4053 return md2lattice.get(md);
4056 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
4057 md2lattice.put(md, lattice);
4060 private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
4063 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
4064 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
4066 if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
4067 && dstCurTuple.size() > (idx + 1)) {
4068 // value flow between fields: we don't need to add a binary relation
4071 Descriptor desc = srcCurTuple.get(idx);
4072 ClassDescriptor classDesc;
4075 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
4077 if (desc instanceof FieldDescriptor) {
4078 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
4080 // this case is that the local variable has a composite location assignment
4081 // the following element after the composite location to the local variable
4082 // has the enclosing descriptor of the local variable
4083 Descriptor localDesc = srcNode.getDescTuple().get(0);
4084 classDesc = ((VarDescriptor) localDesc).getType().getClassDesc();
4087 extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
4091 Descriptor srcFieldDesc = srcCurTuple.get(idx);
4092 Descriptor dstFieldDesc = dstCurTuple.get(idx);
4094 System.out.println("srcFieldDesc=" + srcFieldDesc + " dstFieldDesc=" + dstFieldDesc
4096 if (!srcFieldDesc.equals(dstFieldDesc)) {
4098 System.out.println("-ADD EDGE");
4099 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
4100 } else if (!isReference(srcFieldDesc) && !isReference(dstFieldDesc)) {
4101 System.out.println("-ADD EDGE");
4102 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
4109 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
4110 if (!cd2lattice.containsKey(cd)) {
4111 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
4113 return cd2lattice.get(cd);
4116 public LinkedList<MethodDescriptor> computeMethodList() {
4118 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
4122 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
4123 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
4125 while (!toAnalyzeIsEmpty()) {
4126 ClassDescriptor cd = toAnalyzeNext();
4128 setupToAnalazeMethod(cd);
4129 temp_toanalyzeMethodList.removeAll(visited);
4131 while (!toAnalyzeMethodIsEmpty()) {
4132 MethodDescriptor md = toAnalyzeMethodNext();
4133 if ((!visited.contains(md))
4134 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
4136 // creates a mapping from a method descriptor to virtual methods
4137 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
4138 if (md.isStatic()) {
4139 setPossibleCallees.add(md);
4141 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
4144 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
4145 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
4147 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
4148 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
4149 if ((!ssjava.isTrustMethod(calleemd))
4150 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))
4151 && (!calleemd.getModifiers().isNative())) {
4152 if (!visited.contains(calleemd)) {
4153 temp_toanalyzeMethodList.add(calleemd);
4155 reachableCallee.add(calleemd);
4156 needToAnalyzeCalleeSet.add(calleemd);
4160 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
4169 return ssjava.topologicalSort(toSort);
4173 public boolean isTransitivelyCalledFrom(MethodDescriptor callee, MethodDescriptor caller) {
4174 // if the callee is transitively invoked from the caller
4177 int callerIdx = toanalyze_methodDescList.indexOf(caller);
4178 int calleeIdx = toanalyze_methodDescList.indexOf(callee);
4180 if (callerIdx < calleeIdx) {
4188 public void constructFlowGraph() {
4190 System.out.println("");
4191 toanalyze_methodDescList = computeMethodList();
4193 // hack... it seems that there is a problem with topological sorting.
4194 // so String.toString(Object o) is appeared too higher in the call chain.
4195 MethodDescriptor mdToString = null;
4196 for (Iterator iterator = toanalyze_methodDescList.iterator(); iterator.hasNext();) {
4197 MethodDescriptor md = (MethodDescriptor) iterator.next();
4198 if (md.toString().equals("public static String String.valueOf(Object o)")) {
4203 if (mdToString != null) {
4204 toanalyze_methodDescList.remove(mdToString);
4205 toanalyze_methodDescList.addLast(mdToString);
4208 LinkedList<MethodDescriptor> methodDescList =
4209 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
4211 System.out.println("@@@methodDescList=" + methodDescList);
4214 while (!methodDescList.isEmpty()) {
4215 MethodDescriptor md = methodDescList.removeLast();
4216 if (state.SSJAVADEBUG) {
4217 System.out.println();
4218 System.out.println("SSJAVA: Constructing a flow graph: " + md);
4220 // creates a mapping from a parameter descriptor to its index
4221 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
4223 if (!md.isStatic()) {
4225 mapParamDescToIdx.put(md.getThis(), 0);
4228 for (int i = 0; i < md.numParameters(); i++) {
4229 Descriptor paramDesc = (Descriptor) md.getParameter(i);
4230 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
4233 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
4234 mapMethodDescriptorToFlowGraph.put(md, fg);
4236 analyzeMethodBody(md.getClassDesc(), md);
4238 // System.out.println("##constructSubGlobalFlowGraph");
4239 // GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(fg);
4240 // mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
4243 // System.out.println("##addValueFlowsFromCalleeSubGlobalFlowGraph");
4244 // addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
4245 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
4247 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
4250 // _debug_printGraph();
4254 private void constructGlobalFlowGraph() {
4255 LinkedList<MethodDescriptor> methodDescList =
4256 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
4258 while (!methodDescList.isEmpty()) {
4259 MethodDescriptor md = methodDescList.removeLast();
4260 if (state.SSJAVADEBUG) {
4261 System.out.println();
4262 System.out.println("SSJAVA: Constructing a sub global flow graph: " + md);
4264 constructSubGlobalFlowGraph(getFlowGraph(md));
4267 System.out.println("-add Value Flows From CalleeSubGlobalFlowGraph");
4268 addValueFlowsFromCalleeSubGlobalFlowGraph(md);
4269 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
4271 // System.out.println("-propagate Flows From Callees With No CompositeLocation");
4272 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
4274 // mark if a parameter has incoming flows
4275 checkParamNodesInSubGlobalFlowGraph(md);
4281 private void checkParamNodesInSubGlobalFlowGraph(MethodDescriptor md) {
4282 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4283 FlowGraph flowGraph = getFlowGraph(md);
4285 Set<FlowNode> paramFlowNodeSet = flowGraph.getParamFlowNodeSet();
4286 for (Iterator iterator = paramFlowNodeSet.iterator(); iterator.hasNext();) {
4287 FlowNode paramFlowNode = (FlowNode) iterator.next();
4288 System.out.println("paramFlowNode=" + paramFlowNode);
4289 NTuple<Descriptor> paramDescTuple = paramFlowNode.getDescTuple();
4290 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
4291 GlobalFlowNode paramGlobalNode = globalFlowGraph.getFlowNode(paramLocTuple);
4293 Set<GlobalFlowNode> incomingNodeSet =
4294 globalFlowGraph.getIncomingNodeSetByPrefix(paramLocTuple.get(0));
4296 if (incomingNodeSet.size() > 0) {
4297 paramGlobalNode.setParamNodeWithIncomingFlows(true);
4303 private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
4304 if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
4305 mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
4307 return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
4310 private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
4312 // the transformation for a call site propagates flows through parameters
4313 // if the method is virtual, it also grab all relations from any possible
4316 Set<MethodInvokeNode> setMethodInvokeNode =
4317 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
4319 if (setMethodInvokeNode != null) {
4321 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
4322 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
4323 MethodDescriptor mdCallee = min.getMethod();
4324 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
4325 if (mdCallee.isStatic()) {
4326 setPossibleCallees.add(mdCallee);
4328 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
4329 // removes method descriptors that are not invoked by the caller
4330 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
4331 setPossibleCallees.addAll(calleeSet);
4334 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
4335 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
4336 propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
4344 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
4345 BlockNode bn = state.getMethodBody(md);
4346 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
4347 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
4350 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
4351 NodeTupleSet implicitFlowTupleSet) {
4353 bn.getVarTable().setParent(nametable);
4354 for (int i = 0; i < bn.size(); i++) {
4355 BlockStatementNode bsn = bn.get(i);
4356 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
4361 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
4362 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
4364 switch (bsn.kind()) {
4365 case Kind.BlockExpressionNode:
4366 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
4369 case Kind.DeclarationNode:
4370 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
4373 case Kind.IfStatementNode:
4374 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
4378 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
4381 case Kind.ReturnNode:
4382 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
4385 case Kind.SubBlockNode:
4386 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
4389 case Kind.ContinueBreakNode:
4392 case Kind.SwitchStatementNode:
4393 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
4400 private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
4401 SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
4403 analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
4407 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
4408 SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
4410 NodeTupleSet condTupleNode = new NodeTupleSet();
4411 analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
4412 implicitFlowTupleSet, false);
4414 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4416 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4417 newImplicitTupleSet.addTupleSet(condTupleNode);
4419 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4420 // need to create an intermediate node for the GLB of conditional
4421 // locations & implicit flows
4422 System.out.println("10");
4424 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4425 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4426 NTuple<Descriptor> tuple = idxIter.next();
4427 addFlowGraphEdge(md, tuple, interTuple);
4429 newImplicitTupleSet.clear();
4430 newImplicitTupleSet.addTuple(interTuple);
4433 BlockNode sbn = ssn.getSwitchBody();
4434 for (int i = 0; i < sbn.size(); i++) {
4435 analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
4440 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
4441 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
4442 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
4445 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
4446 NodeTupleSet implicitFlowTupleSet) {
4448 // System.out.println("-analyzeFlowReturnNode=" + rn.printNode(0));
4449 ExpressionNode returnExp = rn.getReturnExpression();
4451 if (returnExp != null) {
4452 NodeTupleSet nodeSet = new NodeTupleSet();
4453 // if a return expression returns a literal value, nodeSet is empty
4454 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
4455 FlowGraph fg = getFlowGraph(md);
4457 // if (implicitFlowTupleSet.size() == 1
4459 // fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate())
4462 // // since there is already an intermediate node for the GLB of implicit
4464 // // we don't need to create another intermediate node.
4465 // // just re-use the intermediate node for implicit flows.
4467 // FlowNode meetNode =
4468 // fg.getFlowNode(implicitFlowTupleSet.iterator().next());
4470 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
4471 // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>)
4473 // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
4478 NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
4480 // add tuples from return node
4481 currentFlowTupleSet.addTupleSet(nodeSet);
4483 // add tuples corresponding to the current implicit flows
4484 currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
4486 // System.out.println("---currentFlowTupleSet=" + currentFlowTupleSet);
4488 if (needToGenerateInterLoc(currentFlowTupleSet)) {
4489 System.out.println("9");
4491 FlowNode meetNode = fg.createIntermediateNode();
4492 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
4493 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
4494 fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
4496 fg.addReturnFlowNode(meetNode.getDescTuple());
4498 // currentFlowTupleSet = removeLiteralTuple(currentFlowTupleSet);
4499 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
4500 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
4501 fg.addReturnFlowNode(currentFlowTuple);
4509 private NodeTupleSet removeLiteralTuple(NodeTupleSet inSet) {
4510 NodeTupleSet tupleSet = new NodeTupleSet();
4511 for (Iterator<NTuple<Descriptor>> iter = inSet.iterator(); iter.hasNext();) {
4512 NTuple<Descriptor> tuple = iter.next();
4513 if (!tuple.get(0).equals(LITERALDESC)) {
4514 tupleSet.addTuple(tuple);
4520 private boolean needToGenerateInterLoc(NodeTupleSet tupleSet) {
4522 for (Iterator<NTuple<Descriptor>> iter = tupleSet.iterator(); iter.hasNext();) {
4523 NTuple<Descriptor> descTuple = iter.next();
4524 if (!descTuple.get(0).equals(LITERALDESC)) {
4529 System.out.println("needToGenerateInterLoc=" + tupleSet + " size=" + size);
4536 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
4537 NodeTupleSet implicitFlowTupleSet) {
4539 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
4541 NodeTupleSet condTupleNode = new NodeTupleSet();
4542 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
4543 implicitFlowTupleSet, false);
4545 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4547 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4548 newImplicitTupleSet.addTupleSet(condTupleNode);
4550 System.out.println("A5");
4551 newImplicitTupleSet.addGlobalFlowTupleSet(implicitFlowTupleSet.getGlobalLocTupleSet());
4552 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
4554 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4555 // need to create an intermediate node for the GLB of conditional
4556 // locations & implicit flows
4557 System.out.println("6");
4559 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4560 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
4562 NTuple<Descriptor> tuple = idxIter.next();
4563 addFlowGraphEdge(md, tuple, interTuple);
4565 newImplicitTupleSet.clear();
4566 newImplicitTupleSet.addTuple(interTuple);
4571 // System.out.println("condTupleNode="+condTupleNode);
4572 // NTuple<Descriptor> interTuple =
4573 // getFlowGraph(md).createIntermediateNode().getDescTuple();
4575 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator();
4576 // idxIter.hasNext();) {
4577 // NTuple<Descriptor> tuple = idxIter.next();
4578 // addFlowGraphEdge(md, tuple, interTuple);
4581 // for (Iterator<NTuple<Descriptor>> idxIter =
4582 // implicitFlowTupleSet.iterator(); idxIter
4584 // NTuple<Descriptor> tuple = idxIter.next();
4585 // addFlowGraphEdge(md, tuple, interTuple);
4588 // NodeTupleSet newImplicitSet = new NodeTupleSet();
4589 // newImplicitSet.addTuple(interTuple);
4590 analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
4593 // condTupleNode.addTupleSet(implicitFlowTupleSet);
4595 // add edges from condNodeTupleSet to all nodes of conditional nodes
4596 // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
4599 // check 'for loop' case
4600 BlockNode bn = ln.getInitializer();
4601 bn.getVarTable().setParent(nametable);
4602 for (int i = 0; i < bn.size(); i++) {
4603 BlockStatementNode bsn = bn.get(i);
4604 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
4607 NodeTupleSet condTupleNode = new NodeTupleSet();
4608 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
4609 implicitFlowTupleSet, false);
4611 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4612 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4613 newImplicitTupleSet.addTupleSet(condTupleNode);
4615 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4616 // need to create an intermediate node for the GLB of conditional
4617 // locations & implicit flows
4618 System.out.println("7");
4620 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4621 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
4623 NTuple<Descriptor> tuple = idxIter.next();
4624 addFlowGraphEdge(md, tuple, interTuple);
4626 newImplicitTupleSet.clear();
4627 newImplicitTupleSet.addTuple(interTuple);
4632 // NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4634 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
4635 // NTuple<Descriptor> tuple = idxIter.next();
4636 // addFlowGraphEdge(md, tuple, interTuple);
4639 // for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
4641 // NTuple<Descriptor> tuple = idxIter.next();
4642 // addFlowGraphEdge(md, tuple, interTuple);
4645 // NodeTupleSet newImplicitSet = new NodeTupleSet();
4646 // newImplicitSet.addTuple(interTuple);
4647 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitTupleSet);
4648 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitTupleSet);
4651 // condTupleNode.addTupleSet(implicitFlowTupleSet);
4653 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
4655 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
4662 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
4663 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
4665 System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
4667 NodeTupleSet condTupleNode = new NodeTupleSet();
4668 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
4669 implicitFlowTupleSet, false);
4671 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4673 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4674 newImplicitTupleSet.addTupleSet(condTupleNode);
4676 // System.out.println("$$$GGGcondTupleNode=" + condTupleNode.getGlobalLocTupleSet());
4677 // System.out.println("-condTupleNode=" + condTupleNode);
4678 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4679 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
4681 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4682 System.out.println("5");
4684 // need to create an intermediate node for the GLB of conditional locations & implicit flows
4685 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4686 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4687 NTuple<Descriptor> tuple = idxIter.next();
4688 addFlowGraphEdge(md, tuple, interTuple);
4690 newImplicitTupleSet.clear();
4691 newImplicitTupleSet.addTuple(interTuple);
4694 // GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4695 // for (Iterator<NTuple<Location>> iterator = condTupleNode.globalIterator();
4696 // iterator.hasNext();) {
4697 // NTuple<Location> calleeReturnLocTuple = iterator.next();
4698 // for (Iterator<NTuple<Descriptor>> iter2 = newImplicitTupleSet.iterator(); iter2.hasNext();) {
4699 // NTuple<Descriptor> callerImplicitTuple = iter2.next();
4700 // globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
4701 // translateToLocTuple(md, callerImplicitTuple));
4704 System.out.println("A4");
4705 newImplicitTupleSet.addGlobalFlowTupleSet(condTupleNode.getGlobalLocTupleSet());
4707 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
4709 if (isn.getFalseBlock() != null) {
4710 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
4715 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
4716 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
4718 VarDescriptor vd = dn.getVarDescriptor();
4719 mapDescToDefinitionLine.put(vd, dn.getNumLine());
4720 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
4722 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
4723 fn.setDeclarationNode();
4725 if (dn.getExpression() != null) {
4727 NodeTupleSet nodeSetRHS = new NodeTupleSet();
4728 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
4729 implicitFlowTupleSet, false);
4731 // creates edges from RHS to LHS
4732 NTuple<Descriptor> interTuple = null;
4733 if (needToGenerateInterLoc(nodeSetRHS)) {
4734 System.out.println("3");
4735 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4738 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
4739 NTuple<Descriptor> fromTuple = iter.next();
4740 System.out.println("fromTuple=" + fromTuple + " interTuple=" + interTuple + " tupleLSH="
4742 addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
4745 // creates edges from implicitFlowTupleSet to LHS
4746 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4747 NTuple<Descriptor> implicitTuple = iter.next();
4748 addFlowGraphEdge(md, implicitTuple, tupleLHS);
4751 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4752 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
4753 NTuple<Location> calleeReturnLocTuple = iterator.next();
4754 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, tupleLHS));
4761 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
4762 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
4763 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
4767 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
4768 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
4769 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
4772 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
4773 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4774 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
4776 // System.out.println("en=" + en.printNode(0) + " class=" + en.getClass());
4778 // note that expression node can create more than one flow node
4779 // nodeSet contains of flow nodes
4780 // base is always assigned to null except the case of a name node!
4781 NTuple<Descriptor> flowTuple;
4782 switch (en.kind()) {
4783 case Kind.AssignmentNode:
4784 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
4785 implicitFlowTupleSet);
4788 case Kind.FieldAccessNode:
4790 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
4791 implicitFlowTupleSet, isLHS);
4792 if (flowTuple != null) {
4793 nodeSet.addTuple(flowTuple);
4798 NodeTupleSet nameNodeSet = new NodeTupleSet();
4800 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
4801 if (flowTuple != null) {
4802 nodeSet.addTuple(flowTuple);
4807 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
4810 case Kind.CreateObjectNode:
4811 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
4814 case Kind.ArrayAccessNode:
4815 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
4818 case Kind.LiteralNode:
4819 analyzeFlowLiteralNode(md, nametable, (LiteralNode) en, nodeSet);
4822 case Kind.MethodInvokeNode:
4823 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
4824 implicitFlowTupleSet);
4827 case Kind.TertiaryNode:
4828 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
4832 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
4834 // case Kind.InstanceOfNode:
4835 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
4838 // case Kind.ArrayInitializerNode:
4839 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
4843 // case Kind.ClassTypeNode:
4844 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
4847 // case Kind.OffsetNode:
4848 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
4857 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
4858 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
4860 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
4861 implicitFlowTupleSet, false);
4865 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
4866 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4868 System.out.println("analyzeFlowTertiaryNode=" + tn.printNode(0));
4870 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
4871 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
4872 implicitFlowTupleSet, false);
4874 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4875 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4876 newImplicitTupleSet.addTupleSet(tertiaryTupleNode);
4878 // System.out.println("$$$GGGcondTupleNode=" + tertiaryTupleNode.getGlobalLocTupleSet());
4879 // System.out.println("-tertiaryTupleNode=" + tertiaryTupleNode);
4880 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4881 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
4883 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4884 System.out.println("15");
4885 // need to create an intermediate node for the GLB of conditional locations & implicit flows
4886 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4887 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4888 NTuple<Descriptor> tuple = idxIter.next();
4889 addFlowGraphEdge(md, tuple, interTuple);
4891 newImplicitTupleSet.clear();
4892 newImplicitTupleSet.addTuple(interTuple);
4895 System.out.println("A7");
4896 newImplicitTupleSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
4898 System.out.println("---------newImplicitTupleSet=" + newImplicitTupleSet);
4899 // add edges from tertiaryTupleNode to all nodes of conditional nodes
4900 // tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
4901 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
4902 newImplicitTupleSet, false);
4904 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
4905 newImplicitTupleSet, false);
4907 System.out.println("A8");
4908 nodeSet.addGlobalFlowTupleSet(tertiaryTupleNode.getGlobalLocTupleSet());
4909 nodeSet.addTupleSet(tertiaryTupleNode);
4911 System.out.println("#tertiary node set=" + nodeSet);
4914 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
4915 MethodInvokeNode min) {
4916 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
4918 set = new HashSet<MethodInvokeNode>();
4919 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
4924 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
4926 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4927 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4929 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
4932 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
4934 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4935 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4938 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
4941 private Set<NTuple<Location>> getPCLocTupleSet(MethodInvokeNode min) {
4942 if (!mapMethodInvokeNodeToPCLocTupleSet.containsKey(min)) {
4943 mapMethodInvokeNodeToPCLocTupleSet.put(min, new HashSet<NTuple<Location>>());
4945 return mapMethodInvokeNodeToPCLocTupleSet.get(min);
4948 private void analyzeFlowMethodInvokeNode(MethodDescriptor mdCaller, SymbolTable nametable,
4949 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4951 System.out.println("analyzeFlowMethodInvokeNode=" + min.printNode(0));
4953 if (!toanalyze_methodDescList.contains(min.getMethod())) {
4957 addMapMethodDescToMethodInvokeNodeSet(min);
4959 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
4960 for (Iterator iterator = implicitFlowTupleSet.iterator(); iterator.hasNext();) {
4961 NTuple<Descriptor> pcDescTuple = (NTuple<Descriptor>) iterator.next();
4962 if (!pcDescTuple.get(0).equals(LITERALDESC)) {
4963 // here we don't need to add the literal value as a PC location
4964 pcLocTupleSet.add(translateToLocTuple(mdCaller, pcDescTuple));
4968 mapMethodInvokeNodeToArgIdxMap.put(min, new HashMap<Integer, NTuple<Descriptor>>());
4970 if (nodeSet == null) {
4971 nodeSet = new NodeTupleSet();
4974 MethodDescriptor mdCallee = min.getMethod();
4976 NameDescriptor baseName = min.getBaseName();
4977 boolean isSystemout = false;
4978 if (baseName != null) {
4979 isSystemout = baseName.getSymbol().equals("System.out");
4982 if (!ssjava.isSSJavaUtil(mdCallee.getClassDesc()) && !ssjava.isTrustMethod(mdCallee)
4985 addMapCallerMethodDescToMethodInvokeNodeSet(mdCaller, min);
4987 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
4988 System.out.println("mdCallee=" + mdCallee + " calleeFlowGraph=" + calleeFlowGraph);
4989 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
4991 System.out.println("---calleeReturnSet=" + calleeReturnSet);
4993 NodeTupleSet tupleSet = new NodeTupleSet();
4995 if (min.getExpression() != null) {
4997 NodeTupleSet baseNodeSet = new NodeTupleSet();
4998 analyzeFlowExpressionNode(mdCaller, nametable, min.getExpression(), baseNodeSet, null,
4999 implicitFlowTupleSet, false);
5000 System.out.println("baseNodeSet=" + baseNodeSet);
5002 assert (baseNodeSet.size() == 1);
5003 NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
5004 mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
5006 if (!min.getMethod().isStatic()) {
5007 addArgIdxMap(min, 0, baseTuple);
5009 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
5010 FlowNode returnNode = (FlowNode) iterator.next();
5011 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
5012 if (returnDescTuple.startsWith(mdCallee.getThis())) {
5013 // the location type of the return value is started with 'this'
5015 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
5017 if (inFlowTuple.get(0) instanceof InterDescriptor) {
5018 // min.getExpression()
5023 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
5024 // nodeSet.addTuple(inFlowTuple);
5025 System.out.println("1CREATE A NEW TUPLE=" + inFlowTuple + " from="
5026 + mdCallee.getThis());
5027 tupleSet.addTuple(inFlowTuple);
5030 System.out.println("returnNode=" + returnNode);
5031 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
5032 // System.out.println("inFlowSet=" + inFlowSet + " from retrunNode=" + returnNode);
5033 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
5034 FlowNode inFlowNode = (FlowNode) iterator2.next();
5035 if (inFlowNode.getDescTuple().startsWith(mdCallee.getThis())) {
5036 // nodeSet.addTupleSet(baseNodeSet);
5037 System.out.println("2CREATE A NEW TUPLE=" + baseNodeSet + " from="
5038 + mdCallee.getThis());
5039 tupleSet.addTupleSet(baseNodeSet);
5047 // analyze parameter flows
5049 if (min.numArgs() > 0) {
5052 if (min.getMethod().isStatic()) {
5058 for (int i = 0; i < min.numArgs(); i++) {
5059 ExpressionNode en = min.getArg(i);
5060 int idx = i + offset;
5061 NodeTupleSet argTupleSet = new NodeTupleSet();
5062 analyzeFlowExpressionNode(mdCaller, nametable, en, argTupleSet, false);
5063 // if argument is liternal node, argTuple is set to NULL
5064 System.out.println("---arg idx=" + idx + " argTupleSet=" + argTupleSet);
5065 NTuple<Descriptor> argTuple = generateArgTuple(mdCaller, argTupleSet);
5067 // if an argument is literal value,
5068 // we need to create an itermediate node so that we could assign a composite location to
5069 // that node if needed
5070 if (argTuple.size() > 0
5071 && (argTuple.get(0).equals(GLOBALDESC) || argTuple.get(0).equals(LITERALDESC))) {
5073 * System.out.println("***GLOBAL ARG TUPLE CASE=" + argTuple); System.out.println("8");
5075 * NTuple<Descriptor> interTuple =
5076 * getFlowGraph(mdCaller).createIntermediateNode().getDescTuple(); ((InterDescriptor)
5077 * interTuple.get(0)).setMethodArgIdxPair(min, idx); addFlowGraphEdge(mdCaller,
5078 * argTuple, interTuple); argTuple = interTuple; addArgIdxMap(min, idx, argTuple);
5079 * System.out.println("new min mapping i=" + idx + " ->" + argTuple);
5081 argTuple = new NTuple<Descriptor>();
5084 addArgIdxMap(min, idx, argTuple);
5086 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
5088 // check whether a param node in the callee graph has incoming flows
5089 // if it has incoming flows, the corresponding arg should be lower than the current PC
5090 // Descriptor prefix = paramNode.getDescTuple().get(0);
5091 // if (calleeFlowGraph.getIncomingNodeSetByPrefix(prefix).size() > 0) {
5092 // for (Iterator<NTuple<Descriptor>> iterator = implicitFlowTupleSet.iterator(); iterator
5094 // NTuple<Descriptor> pcTuple = iterator.next();
5095 // System.out.println("add edge pcTuple =" + pcTuple + " -> " + argTuple);
5096 // addFlowGraphEdge(md, pcTuple, argTuple);
5100 System.out.println("paramNode=" + paramNode + " calleeReturnSet=" + calleeReturnSet);
5101 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
5102 || mdCallee.getModifiers().isNative()) {
5103 addParamNodeFlowingToReturnValue(mdCallee, paramNode);
5104 // nodeSet.addTupleSet(argTupleSet);
5105 System.out.println("3CREATE A NEW TUPLE=" + argTupleSet + " from=" + paramNode);
5106 tupleSet.addTupleSet(argTupleSet);
5112 if (mdCallee.getReturnType() != null && !mdCallee.getReturnType().isVoid()) {
5113 FlowReturnNode setNode = getFlowGraph(mdCaller).createReturnNode(min);
5115 if (needToGenerateInterLoc(tupleSet)) {
5116 System.out.println("20");
5117 FlowGraph fg = getFlowGraph(mdCaller);
5118 FlowNode interNode = fg.createIntermediateNode();
5119 interNode.setFormHolder(true);
5121 NTuple<Descriptor> interTuple = interNode.getDescTuple();
5123 for (Iterator iterator = tupleSet.iterator(); iterator.hasNext();) {
5124 NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator.next();
5126 Set<NTuple<Descriptor>> addSet = new HashSet<NTuple<Descriptor>>();
5127 FlowNode node = fg.getFlowNode(tuple);
5128 if (node instanceof FlowReturnNode) {
5129 addSet.addAll(fg.getReturnTupleSet(((FlowReturnNode) node).getReturnTupleSet()));
5133 for (Iterator iterator2 = addSet.iterator(); iterator2.hasNext();) {
5134 NTuple<Descriptor> higher = (NTuple<Descriptor>) iterator2.next();
5135 addFlowGraphEdge(mdCaller, higher, interTuple);
5139 setNode.addTuple(interTuple);
5140 System.out.println("ADD TUPLESET=" + interTuple + " to returnnode=" + setNode);
5143 setNode.addTupleSet(tupleSet);
5144 System.out.println("ADD TUPLESET=" + tupleSet + " to returnnode=" + setNode);
5147 // setNode.addTupleSet(tupleSet);
5148 nodeSet.addTuple(setNode.getDescTuple());
5152 // propagateFlowsFromCallee(min, md, min.getMethod());
5154 // when generating the global flow graph,
5155 // we need to add ordering relations from the set of callee return loc tuple to LHS of the
5156 // caller assignment
5157 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
5158 FlowNode calleeReturnNode = (FlowNode) iterator.next();
5159 NTuple<Location> calleeReturnLocTuple =
5160 translateToLocTuple(mdCallee, calleeReturnNode.getDescTuple());
5161 System.out.println("calleeReturnLocTuple=" + calleeReturnLocTuple);
5162 NTuple<Location> transaltedToCaller =
5163 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeReturnLocTuple);
5164 // System.out.println("translateToCallerLocTuple="
5165 // + translateToCallerLocTuple(min, mdCallee, mdCaller, calleeReturnLocTuple));
5166 if (transaltedToCaller.size() > 0) {
5167 nodeSet.addGlobalFlowTuple(translateToCallerLocTuple(min, mdCallee, mdCaller,
5168 calleeReturnLocTuple));
5172 System.out.println("min nodeSet=" + nodeSet);
5178 private NTuple<Descriptor> generateArgTuple(MethodDescriptor mdCaller, NodeTupleSet argTupleSet) {
5182 // if argTupleSet is empty, it comes from the top location
5183 if (argTupleSet.size() == 0) {
5184 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
5185 descTuple.add(LITERALDESC);
5189 Set<NTuple<Descriptor>> argTupleSetNonLiteral = new HashSet<NTuple<Descriptor>>();
5191 for (Iterator<NTuple<Descriptor>> iter = argTupleSet.iterator(); iter.hasNext();) {
5192 NTuple<Descriptor> descTuple = iter.next();
5193 if (!descTuple.get(0).equals(LITERALDESC)) {
5194 argTupleSetNonLiteral.add(descTuple);
5198 if (argTupleSetNonLiteral.size() > 1) {
5199 System.out.println("11");
5201 NTuple<Descriptor> interTuple =
5202 getFlowGraph(mdCaller).createIntermediateNode().getDescTuple();
5203 for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
5204 NTuple<Descriptor> tuple = idxIter.next();
5205 addFlowGraphEdge(mdCaller, tuple, interTuple);
5208 } else if (argTupleSetNonLiteral.size() == 1) {
5209 return argTupleSetNonLiteral.iterator().next();
5211 return argTupleSet.iterator().next();
5216 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
5217 // return true if inNode has in-flows to nodeSet
5219 if (nodeSet.contains(inNode)) {
5220 // in this case, the method directly returns a parameter variable.
5223 // Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
5224 Set<FlowNode> reachableSet = fg.getReachableSetFrom(inNode.getDescTuple());
5225 System.out.println("inNode=" + inNode + " reachalbeSet=" + reachableSet);
5227 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
5228 FlowNode fn = (FlowNode) iterator.next();
5229 if (nodeSet.contains(fn)) {
5236 private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
5237 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
5240 private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
5244 Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
5245 if (mapIdxToTuple == null) {
5246 mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
5247 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
5249 mapIdxToTuple.put(new Integer(idx), argTuple);
5252 private void analyzeFlowLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en,
5253 NodeTupleSet nodeSet) {
5254 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
5255 tuple.add(LITERALDESC);
5256 nodeSet.addTuple(tuple);
5259 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
5260 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
5262 System.out.println("analyzeFlowArrayAccessNode aan=" + aan.printNode(0));
5263 String currentArrayAccessNodeExpStr = aan.printNode(0);
5264 arrayAccessNodeStack.push(aan.printNode(0));
5266 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
5267 NTuple<Descriptor> base =
5268 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
5269 System.out.println("-base=" + base);
5271 nodeSet.setMethodInvokeBaseDescTuple(base);
5272 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
5273 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
5275 arrayAccessNodeStack.pop();
5278 // need to create an edge from idx to array
5279 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
5280 NTuple<Descriptor> idxTuple = idxIter.next();
5281 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
5282 NTuple<Descriptor> arrTuple = arrIter.next();
5283 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
5287 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5288 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
5290 NTuple<Location> calleeReturnLocTuple = iterator.next();
5291 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
5292 NTuple<Descriptor> arrTuple = arrIter.next();
5293 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, arrTuple));
5297 nodeSet.addTupleSet(expNodeTupleSet);
5300 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
5302 nodeSetArrayAccessExp.addTupleSet(expNodeTupleSet);
5303 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
5305 if (arrayAccessNodeStack.isEmpty()
5306 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
5308 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
5309 System.out.println("1");
5310 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5312 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter.hasNext();) {
5313 NTuple<Descriptor> higherTuple = iter.next();
5314 addFlowGraphEdge(md, higherTuple, interTuple);
5316 nodeSetArrayAccessExp.clear();
5317 nodeSetArrayAccessExp.addTuple(interTuple);
5318 FlowGraph fg = getFlowGraph(md);
5320 System.out.println("base=" + base);
5322 fg.addMapInterLocNodeToEnclosingDescriptor(interTuple.get(0),
5323 getClassTypeDescriptor(base.get(base.size() - 1)));
5328 System.out.println("A1");
5329 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
5330 nodeSet.addTupleSet(nodeSetArrayAccessExp);
5336 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
5337 CreateObjectNode en) {
5338 // TODO Auto-generated method stub
5342 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
5343 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
5345 NodeTupleSet leftOpSet = new NodeTupleSet();
5346 NodeTupleSet rightOpSet = new NodeTupleSet();
5348 System.out.println("analyzeFlowOpNode=" + on.printNode(0));
5351 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
5353 System.out.println("--leftOpSet=" + leftOpSet);
5355 if (on.getRight() != null) {
5357 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
5358 implicitFlowTupleSet, false);
5360 System.out.println("--rightOpSet=" + rightOpSet);
5362 Operation op = on.getOp();
5364 switch (op.getOp()) {
5366 case Operation.UNARYPLUS:
5367 case Operation.UNARYMINUS:
5368 case Operation.LOGIC_NOT:
5370 nodeSet.addTupleSet(leftOpSet);
5373 case Operation.LOGIC_OR:
5374 case Operation.LOGIC_AND:
5375 case Operation.COMP:
5376 case Operation.BIT_OR:
5377 case Operation.BIT_XOR:
5378 case Operation.BIT_AND:
5379 case Operation.ISAVAILABLE:
5380 case Operation.EQUAL:
5381 case Operation.NOTEQUAL:
5388 case Operation.MULT:
5391 case Operation.LEFTSHIFT:
5392 case Operation.RIGHTSHIFT:
5393 case Operation.URIGHTSHIFT:
5395 // there are two operands
5396 nodeSet.addTupleSet(leftOpSet);
5397 nodeSet.addTupleSet(rightOpSet);
5399 System.out.println("A6");
5400 nodeSet.addGlobalFlowTupleSet(leftOpSet.getGlobalLocTupleSet());
5401 nodeSet.addGlobalFlowTupleSet(rightOpSet.getGlobalLocTupleSet());
5406 throw new Error(op.toString());
5411 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
5412 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
5415 base = new NTuple<Descriptor>();
5418 NameDescriptor nd = nn.getName();
5420 if (nd.getBase() != null) {
5422 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
5423 implicitFlowTupleSet, false);
5425 // base node has the top location
5429 String varname = nd.toString();
5430 if (varname.equals("this")) {
5432 base.add(md.getThis());
5436 Descriptor d = (Descriptor) nametable.get(varname);
5438 if (d instanceof VarDescriptor) {
5439 VarDescriptor vd = (VarDescriptor) d;
5441 } else if (d instanceof FieldDescriptor) {
5442 // the type of field descriptor has a location!
5443 FieldDescriptor fd = (FieldDescriptor) d;
5444 if (fd.isStatic()) {
5446 // if it is 'static final', no need to have flow node for the TOP
5450 // if 'static', assign the default GLOBAL LOCATION to the first
5451 // element of the tuple
5452 base.add(GLOBALDESC);
5455 // the location of field access starts from this, followed by field
5457 base.add(md.getThis());
5461 } else if (d == null) {
5462 // access static field
5463 base.add(GLOBALDESC);
5464 base.add(nn.getField());
5467 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
5469 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
5470 // String globalLocId = localLattice.getGlobalLoc();
5471 // if (globalLocId == null) {
5473 // Error("Method lattice does not define global variable location at "
5474 // + generateErrorMessage(md.getClassDesc(), nn));
5476 // loc.addLocation(new Location(md, globalLocId));
5478 // Location fieldLoc = (Location) fd.getType().getExtension();
5479 // loc.addLocation(fieldLoc);
5485 getFlowGraph(md).createNewFlowNode(base);
5491 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
5492 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
5493 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
5494 // System.out.println("analyzeFlowFieldAccessNode=" + fan.printNode(0));
5496 String currentArrayAccessNodeExpStr = null;
5497 ExpressionNode left = fan.getExpression();
5498 TypeDescriptor ltd = left.getType();
5499 FieldDescriptor fd = fan.getField();
5500 ArrayAccessNode aan = null;
5502 String varName = null;
5503 if (left.kind() == Kind.NameNode) {
5504 NameDescriptor nd = ((NameNode) left).getName();
5505 varName = nd.toString();
5508 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
5509 // using a class name directly or access using this
5510 if (fd.isStatic() && fd.isFinal()) {
5515 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
5517 boolean isArrayCase = false;
5518 if (left instanceof ArrayAccessNode) {
5521 aan = (ArrayAccessNode) left;
5523 currentArrayAccessNodeExpStr = aan.printNode(0);
5524 arrayAccessNodeStack.push(currentArrayAccessNodeExpStr);
5526 left = aan.getExpression();
5527 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
5528 implicitFlowTupleSet, isLHS);
5532 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
5535 // in this case, field is TOP location
5539 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
5541 if (!left.getType().isPrimitive()) {
5542 if (!fd.getSymbol().equals("length")) {
5543 // array.length access, just have the location of the array
5544 flowFieldTuple.add(fd);
5545 nodeSet.removeTuple(base);
5548 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
5551 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
5552 NTuple<Descriptor> idxTuple = idxIter.next();
5553 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
5556 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5557 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
5559 NTuple<Location> calleeReturnLocTuple = iterator.next();
5560 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5561 translateToLocTuple(md, flowFieldTuple));
5565 nodeSet.addTupleSet(idxNodeTupleSet);
5567 // if it is the array case and not the LHS case
5569 arrayAccessNodeStack.pop();
5571 if (arrayAccessNodeStack.isEmpty()
5572 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
5573 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
5575 nodeSetArrayAccessExp.addTuple(flowFieldTuple);
5576 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
5577 nodeSetArrayAccessExp.addTupleSet(nodeSet);
5579 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
5580 System.out.println("4");
5581 System.out.println("nodeSetArrayAccessExp=" + nodeSetArrayAccessExp);
5582 // System.out.println("idxNodeTupleSet.getGlobalLocTupleSet()="
5583 // + idxNodeTupleSet.getGlobalLocTupleSet());
5585 NTuple<Descriptor> interTuple =
5586 getFlowGraph(md).createIntermediateNode().getDescTuple();
5588 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter
5590 NTuple<Descriptor> higherTuple = iter.next();
5591 addFlowGraphEdge(md, higherTuple, interTuple);
5594 FlowGraph fg = getFlowGraph(md);
5595 fg.addMapInterLocNodeToEnclosingDescriptor(interTuple.get(0),
5596 getClassTypeDescriptor(base.get(base.size() - 1)));
5599 flowFieldTuple = interTuple;
5601 System.out.println("A3");
5602 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
5608 return flowFieldTuple;
5613 private void debug_printTreeNode(TreeNode tn) {
5615 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
5619 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
5620 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
5621 NodeTupleSet implicitFlowTupleSet) {
5623 NodeTupleSet nodeSetRHS = new NodeTupleSet();
5624 NodeTupleSet nodeSetLHS = new NodeTupleSet();
5626 boolean postinc = true;
5627 if (an.getOperation().getBaseOp() == null
5628 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
5629 .getBaseOp().getOp() != Operation.POSTDEC)) {
5632 // if LHS is array access node, need to capture value flows between an array
5633 // and its index value
5634 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
5638 // analyze value flows of rhs expression
5639 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
5642 System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
5643 System.out.println("-nodeSetLHS=" + nodeSetLHS);
5644 System.out.println("-nodeSetRHS=" + nodeSetRHS);
5645 System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
5646 // System.out.println("-");
5648 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
5649 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
5651 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
5652 NTuple<Descriptor> fromTuple = iter.next();
5653 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5654 NTuple<Descriptor> toTuple = iter2.next();
5655 addFlowGraphEdge(md, fromTuple, toTuple);
5660 // creates edges from RHS to LHS
5661 NTuple<Descriptor> interTuple = null;
5662 if (needToGenerateInterLoc(nodeSetRHS)) {
5663 System.out.println("2");
5664 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
5667 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
5668 NTuple<Descriptor> fromTuple = iter.next();
5669 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5670 NTuple<Descriptor> toTuple = iter2.next();
5671 addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
5675 // creates edges from implicitFlowTupleSet to LHS
5676 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
5677 NTuple<Descriptor> fromTuple = iter.next();
5678 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5679 NTuple<Descriptor> toTuple = iter2.next();
5680 addFlowGraphEdge(md, fromTuple, toTuple);
5684 // create global flow edges if the callee gives return value flows to the caller
5685 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5686 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
5687 NTuple<Location> calleeReturnLocTuple = iterator.next();
5688 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5689 NTuple<Descriptor> callerLHSTuple = iter2.next();
5690 System.out.println("$$$ GLOBAL FLOW ADD=" + calleeReturnLocTuple + " -> "
5691 + translateToLocTuple(md, callerLHSTuple));
5692 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5693 translateToLocTuple(md, callerLHSTuple));
5697 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
5699 NTuple<Location> calleeReturnLocTuple = iterator.next();
5700 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5701 NTuple<Descriptor> callerLHSTuple = iter2.next();
5702 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5703 translateToLocTuple(md, callerLHSTuple));
5704 System.out.println("$$$ GLOBAL FLOW PCLOC ADD=" + calleeReturnLocTuple + " -> "
5705 + translateToLocTuple(md, callerLHSTuple));
5712 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5713 NTuple<Descriptor> tuple = iter2.next();
5714 addFlowGraphEdge(md, tuple, tuple);
5717 // creates edges from implicitFlowTupleSet to LHS
5718 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
5719 NTuple<Descriptor> fromTuple = iter.next();
5720 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5721 NTuple<Descriptor> toTuple = iter2.next();
5722 addFlowGraphEdge(md, fromTuple, toTuple);
5726 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
5727 for (Iterator<NTuple<Location>> iterator = implicitFlowTupleSet.globalIterator(); iterator
5729 NTuple<Location> calleeReturnLocTuple = iterator.next();
5730 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
5731 NTuple<Descriptor> callerLHSTuple = iter2.next();
5732 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
5733 translateToLocTuple(md, callerLHSTuple));
5734 System.out.println("$$$ GLOBAL FLOW PC ADD=" + calleeReturnLocTuple + " -> "
5735 + translateToLocTuple(md, callerLHSTuple));
5741 if (nodeSet != null) {
5742 nodeSet.addTupleSet(nodeSetLHS);
5743 System.out.println("A2");
5744 nodeSet.addGlobalFlowTupleSet(nodeSetLHS.getGlobalLocTupleSet());
5748 public FlowGraph getFlowGraph(MethodDescriptor md) {
5749 return mapMethodDescriptorToFlowGraph.get(md);
5752 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
5753 NTuple<Descriptor> to) {
5754 FlowGraph graph = getFlowGraph(md);
5755 graph.addValueFlowEdge(from, to);
5759 private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
5760 NTuple<Descriptor> inter, NTuple<Descriptor> to) {
5762 FlowGraph graph = getFlowGraph(md);
5764 if (inter != null) {
5765 graph.addValueFlowEdge(from, inter);
5766 graph.addValueFlowEdge(inter, to);
5768 graph.addValueFlowEdge(from, to);
5773 public void writeInferredLatticeDotFile(ClassDescriptor cd, HierarchyGraph simpleHierarchyGraph,
5774 SSJavaLattice<String> locOrder, String nameSuffix) {
5775 System.out.println("@cd=" + cd);
5776 System.out.println("@sharedLoc=" + locOrder.getSharedLocSet());
5777 writeInferredLatticeDotFile(cd, null, simpleHierarchyGraph, locOrder, nameSuffix);
5780 public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
5781 HierarchyGraph simpleHierarchyGraph, SSJavaLattice<String> locOrder, String nameSuffix) {
5783 String fileName = "lattice_";
5786 /* cd.getSymbol().replaceAll("[\\W_]", "") + "_" + */md.toString().replaceAll("[\\W_]", "");
5788 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
5791 fileName += nameSuffix;
5793 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
5795 Set<String> addedLocSet = new HashSet<String>();
5797 if (pairSet.size() > 0) {
5799 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
5801 bw.write("digraph " + fileName + " {\n");
5803 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
5804 // pair is in the form of <higher, lower>
5805 Pair<String, String> pair = (Pair<String, String>) iterator.next();
5807 String highLocId = pair.getFirst();
5808 String lowLocId = pair.getSecond();
5809 if (!addedLocSet.contains(highLocId)) {
5810 addedLocSet.add(highLocId);
5811 drawNode(bw, locOrder, simpleHierarchyGraph, highLocId);
5814 if (!addedLocSet.contains(lowLocId)) {
5815 addedLocSet.add(lowLocId);
5816 drawNode(bw, locOrder, simpleHierarchyGraph, lowLocId);
5819 bw.write(highLocId + " -> " + lowLocId + ";\n");
5824 } catch (IOException e) {
5825 e.printStackTrace();
5832 private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
5834 for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
5835 HNode merged = (HNode) iterator.next();
5836 if (merged.isMergeNode()) {
5837 str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
5839 str += " " + merged.getName();
5845 private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, HierarchyGraph graph,
5846 String locName) throws IOException {
5849 if (lattice.isSharedLoc(locName)) {
5850 prettyStr = locName + "*";
5852 prettyStr = locName;
5854 // HNode node = graph.getHNode(locName);
5855 // if (node != null && node.isMergeNode()) {
5856 // Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
5857 // prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
5859 bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
5862 public void _debug_writeFlowGraph() {
5863 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
5865 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
5866 MethodDescriptor md = (MethodDescriptor) iterator.next();
5867 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
5868 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
5871 subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
5872 } catch (IOException e) {
5873 e.printStackTrace();
5881 class CyclicFlowException extends Exception {
5885 class InterDescriptor extends Descriptor {
5887 Pair<MethodInvokeNode, Integer> minArgIdxPair;
5889 public InterDescriptor(String name) {
5893 public void setMethodArgIdxPair(MethodInvokeNode min, int idx) {
5894 minArgIdxPair = new Pair<MethodInvokeNode, Integer>(min, new Integer(idx));
5897 public Pair<MethodInvokeNode, Integer> getMethodArgIdxPair() {
5898 return minArgIdxPair;