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<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
103 private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
105 private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
107 private Map<MethodDescriptor, Set<FlowNode>> mapMethodDescToParamNodeFlowsToReturnValue;
109 private Map<String, Vector<String>> mapFileNameToLineVector;
111 private Map<Descriptor, Integer> mapDescToDefinitionLine;
113 private Map<Descriptor, LocationSummary> mapDescToLocationSummary;
115 // maps a method descriptor to a sub global flow graph that captures all value flows caused by the
116 // set of callees reachable from the method
117 private Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToSubGlobalFlowGraph;
119 public static final String GLOBALLOC = "GLOBALLOC";
121 public static final String TOPLOC = "TOPLOC";
123 public static final String INTERLOC = "INTERLOC";
125 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
127 public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
129 public static String newline = System.getProperty("line.separator");
131 LocationInfo curMethodInfo;
133 boolean debug = true;
135 private static int locSeed = 0;
137 public LocationInference(SSJavaAnalysis ssjava, State state) {
138 this.ssjava = ssjava;
140 this.temp_toanalyzeList = new ArrayList<ClassDescriptor>();
141 this.temp_toanalyzeMethodList = new ArrayList<MethodDescriptor>();
142 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
143 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
144 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
145 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
146 this.mapMethodDescriptorToMethodInvokeNodeSet =
147 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
148 this.mapMethodInvokeNodeToArgIdxMap =
149 new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
150 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
151 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
152 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
154 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
155 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
156 this.mapMethodDescToParamNodeFlowsToReturnValue =
157 new HashMap<MethodDescriptor, Set<FlowNode>>();
159 this.mapDescriptorToHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
160 this.mapMethodInvokeNodeToBaseTuple = new HashMap<MethodInvokeNode, NTuple<Descriptor>>();
162 this.mapDescriptorToSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
163 this.mapDescriptorToCombineSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
164 this.mapDescriptorToSimpleHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
166 this.mapDescriptorToSimpleLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
168 this.mapDescToLocationSummary = new HashMap<Descriptor, LocationSummary>();
170 mapMethodDescriptorToSubGlobalFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
174 public void setupToAnalyze() {
175 SymbolTable classtable = state.getClassSymbolTable();
176 temp_toanalyzeList.clear();
177 temp_toanalyzeList.addAll(classtable.getValueSet());
178 // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
179 // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
180 // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
185 public void setupToAnalazeMethod(ClassDescriptor cd) {
187 SymbolTable methodtable = cd.getMethodTable();
188 temp_toanalyzeMethodList.clear();
189 temp_toanalyzeMethodList.addAll(methodtable.getValueSet());
190 Collections.sort(temp_toanalyzeMethodList, new Comparator<MethodDescriptor>() {
191 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
192 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
197 public boolean toAnalyzeMethodIsEmpty() {
198 return temp_toanalyzeMethodList.isEmpty();
201 public boolean toAnalyzeIsEmpty() {
202 return temp_toanalyzeList.isEmpty();
205 public ClassDescriptor toAnalyzeNext() {
206 return temp_toanalyzeList.remove(0);
209 public MethodDescriptor toAnalyzeMethodNext() {
210 return temp_toanalyzeMethodList.remove(0);
213 public void inference() {
215 // 1) construct value flow graph
216 constructFlowGraph();
218 constructGlobalFlowGraph();
222 constructHierarchyGraph();
224 debug_writeHierarchyDotFiles();
226 simplifyHierarchyGraph();
228 debug_writeSimpleHierarchyDotFiles();
230 constructSkeletonHierarchyGraph();
232 debug_writeSkeletonHierarchyDotFiles();
234 insertCombinationNodes();
236 debug_writeSkeletonCombinationHierarchyDotFiles();
240 debug_writeLattices();
242 generateMethodSummary();
246 // 2) construct lattices
251 // 3) check properties
254 // calculate RETURNLOC,PCLOC
255 calculateExtraLocations();
257 debug_writeLatticeDotFile();
259 // 4) generate annotated source codes
260 generateAnnoatedCode();
264 private void constructGlobalFlowGraph() {
266 System.out.println("");
267 LinkedList<MethodDescriptor> methodDescList =
268 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
270 System.out.println("@@@methodDescList=" + methodDescList);
273 while (!methodDescList.isEmpty()) {
274 MethodDescriptor md = methodDescList.removeLast();
275 if (state.SSJAVADEBUG) {
276 System.out.println();
277 System.out.println("SSJAVA: Constructing a global flow graph: " + md);
279 FlowGraph flowGraph = getFlowGraph(md);
280 FlowGraph subGlobalFlowGraph = flowGraph.clone();
281 mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
283 addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
286 subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
287 } catch (IOException e) {
290 // FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
291 // mapMethodDescriptorToFlowGraph.put(md, fg);
292 // analyzeMethodBody(md.getClassDesc(), md);
297 // DEBUG: write a global flow graph
298 MethodDescriptor mdContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
299 FlowGraph globalFlowGraph = getSubGlobalFlowGraph(mdContainingSSJavaLoop);
300 // System.out.println("GLOBAL NODE SET=" + globalFlowGraph.getNodeSet());
301 assignCompositeLocation(globalFlowGraph);
303 globalFlowGraph.writeGraph("_GLOBAL");
304 } catch (IOException e) {
307 // _debug_printGraph();
311 private void assignCompositeLocation(FlowGraph globalFlowGraph) {
312 Set<FlowNode> nodeSet = globalFlowGraph.getNodeSet();
314 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
315 FlowNode flowNode = (FlowNode) iterator.next();
316 Set<FlowNode> inNodeSet = globalFlowGraph.getIncomingFlowNodeSet(flowNode);
317 Set<FlowNode> reachableNodeSet = globalFlowGraph.getReachFlowNodeSetFrom(flowNode);
319 // System.out.println("flowNode=" + flowNode + " incoming=" + inNodeSet);
320 // System.out.println("reachableNodeSet=" + reachableNodeSet);
322 Map<NTuple<Location>, Set<NTuple<Descriptor>>> mapPrefixToIncomingLocTupleSet =
323 new HashMap<NTuple<Location>, Set<NTuple<Descriptor>>>();
325 List<NTuple<Descriptor>> prefixList = new ArrayList<NTuple<Descriptor>>();
327 for (Iterator iterator2 = inNodeSet.iterator(); iterator2.hasNext();) {
328 FlowNode inNode = (FlowNode) iterator2.next();
330 NTuple<Descriptor> inNodeTuple = inNode.getCurrentDescTuple();
332 // CompositeLocation inNodeInferredLoc =
333 // generateInferredCompositeLocation(methodInfo, inNodeTuple);
334 // NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
336 for (int i = 1; i < inNodeTuple.size(); i++) {
337 NTuple<Descriptor> prefix = inNodeTuple.subList(0, i);
338 if (!prefixList.contains(prefix)) {
339 prefixList.add(prefix);
344 Collections.sort(prefixList, new Comparator<NTuple<Descriptor>>() {
345 public int compare(NTuple<Descriptor> arg0, NTuple<Descriptor> arg1) {
346 int s0 = arg0.size();
347 int s1 = arg1.size();
350 } else if (s0 == s1) {
358 // find out reachable nodes that have the longest common prefix
359 for (int i = 0; i < prefixList.size(); i++) {
360 NTuple<Descriptor> curPrefix = prefixList.get(i);
361 Set<NTuple<Descriptor>> reachableCommonPrefixSet = new HashSet<NTuple<Descriptor>>();
363 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
364 FlowNode reachableNode = (FlowNode) iterator2.next();
365 NTuple<Descriptor> reachLocTuple = reachableNode.getCurrentDescTuple();
366 if (reachLocTuple.startsWith(curPrefix)) {
367 reachableCommonPrefixSet.add(reachLocTuple);
371 if (!reachableCommonPrefixSet.isEmpty()) {
372 // found reachable nodes that start with the prefix curPrefix
373 // need to assign a composite location
374 // System.out.println("-prefixList=" + prefixList);
375 // System.out.println("-reachableCommonPrefixSet=" + reachableCommonPrefixSet);
376 // System.out.println("-curPrefix=" + curPrefix);
378 // first, check if there are more than one the set of locations that has
379 // the same length of the longest reachable prefix, no way to assign
380 // a composite location to the input local var
381 prefixSanityCheck(prefixList, i, globalFlowGraph, reachableNodeSet);
383 MethodDescriptor topMethodDesc = globalFlowGraph.getMethodDescriptor();
384 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix, topMethodDesc);
386 System.out.println("SET COMPOSITE LOCATION=" + newCompLoc + " to " + flowNode);
387 flowNode.setCompositeLocation(newCompLoc);
395 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> curPrefix,
396 MethodDescriptor md) {
397 CompositeLocation newCompLoc = new CompositeLocation();
399 Descriptor enclosingDesc = md;
400 for (int i = 0; i < curPrefix.size(); i++) {
401 Descriptor curDesc = curPrefix.get(i);
402 Location loc = new Location(enclosingDesc, curDesc.getSymbol());
403 newCompLoc.addLocation(loc);
405 VarDescriptor varDesc = (VarDescriptor) curDesc;
406 enclosingDesc = varDesc.getType().getClassDesc();
408 FieldDescriptor fieldDesc = (FieldDescriptor) curDesc;
409 enclosingDesc = fieldDesc.getType().getClassDesc();
413 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
414 newLocDescriptor.setEnclosingClassDesc((ClassDescriptor) enclosingDesc);
416 Location newLoc = new Location(enclosingDesc, newLocDescriptor.getSymbol());
417 newLoc.setLocDescriptor(newLocDescriptor);
418 newCompLoc.addLocation(newLoc);
423 private void prefixSanityCheck(List<NTuple<Descriptor>> prefixList, int curIdx,
424 FlowGraph globalFlowGraph, Set<FlowNode> reachableNodeSet) {
426 NTuple<Descriptor> curPrefix = prefixList.get(curIdx);
428 for (int i = curIdx + 1; i < prefixList.size(); i++) {
429 NTuple<Descriptor> prefixTuple = prefixList.get(i);
431 if (curPrefix.startsWith(prefixTuple)) {
435 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
436 FlowNode reachableNode = (FlowNode) iterator2.next();
437 NTuple<Descriptor> reachLocTuple = reachableNode.getCurrentDescTuple();
438 if (reachLocTuple.startsWith(prefixTuple)) {
440 "Failed to generate a composite location because there is more than one prefix which is reach to the current node.");
447 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller,
448 FlowGraph subGlobalFlowGraph) {
450 // the transformation for a call site propagates flows through parameters
451 // if the method is virtual, it also grab all relations from any possible
454 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
456 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
457 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
458 MethodDescriptor mdCallee = min.getMethod();
459 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
460 if (mdCallee.isStatic()) {
461 setPossibleCallees.add(mdCallee);
463 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
464 // removes method descriptors that are not invoked by the caller
465 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
466 setPossibleCallees.addAll(calleeSet);
469 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
470 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
471 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
472 // propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
479 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
480 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
482 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
484 FlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
485 FlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
487 int numParam = calleeSubGlobalGraph.getNumParameters();
488 for (int idx = 0; idx < numParam; idx++) {
489 FlowNode paramNode = calleeSubGlobalGraph.getParamFlowNode(idx);
490 NTuple<Descriptor> argTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(idx);
491 System.out.println("argTupleSet=" + argTuple + " param=" + paramNode);
492 // here, it adds all value flows reachable from the paramNode in the callee's flow graph
493 addValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, paramNode, callerSubGlobalGraph,
494 argTuple, baseTuple);
499 private void addValueFlowsFromCalleeParam(MethodInvokeNode min, FlowGraph calleeSubGlobalGraph,
500 FlowNode paramNode, FlowGraph callerSubGlobalGraph, NTuple<Descriptor> argTuple,
501 NTuple<Descriptor> baseTuple) {
503 Set<FlowNode> visited = new HashSet<FlowNode>();
505 visited.add(paramNode);
506 recurAddValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, paramNode, callerSubGlobalGraph,
507 argTuple, visited, baseTuple);
510 private void recurAddValueFlowsFromCalleeParam(MethodInvokeNode min,
511 FlowGraph calleeSubGlobalGraph, FlowNode calleeSrcNode, FlowGraph callerSubGlobalGraph,
512 NTuple<Descriptor> callerSrcTuple, Set<FlowNode> visited, NTuple<Descriptor> baseTuple) {
514 MethodDescriptor mdCallee = calleeSubGlobalGraph.getMethodDescriptor();
516 // Set<FlowEdge> edgeSet = calleeSubGlobalGraph.getOutEdgeSet(calleeSrcNode);
517 Set<FlowEdge> edgeSet = calleeSubGlobalGraph.getOutEdgeSetStartingFrom(calleeSrcNode);
518 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
519 FlowEdge flowEdge = (FlowEdge) iterator.next();
521 NTuple<Descriptor> srcDescTuple = flowEdge.getInitTuple();
522 NTuple<Descriptor> dstDescTuple = flowEdge.getEndTuple();
524 FlowNode dstNode = calleeSubGlobalGraph.getFlowNode(dstDescTuple);
526 if (calleeSubGlobalGraph.isParameter(srcDescTuple)) {
527 // destination node is started with 'parameter'
528 // need to translate it in terms of the caller's a node
530 translateToCaller(min, calleeSubGlobalGraph.getParamIdx(srcDescTuple), srcDescTuple);
533 if (calleeSubGlobalGraph.isParameter(dstDescTuple)) {
534 // destination node is started with 'parameter'
535 // need to translate it in terms of the caller's a node
537 translateToCaller(min, calleeSubGlobalGraph.getParamIdx(dstDescTuple), dstDescTuple);
540 callerSubGlobalGraph.addValueFlowEdge(srcDescTuple, dstDescTuple);
542 if (!visited.contains(dstNode)) {
543 visited.add(dstNode);
544 recurAddValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, dstNode, callerSubGlobalGraph,
545 dstDescTuple, visited, baseTuple);
552 private NTuple<Descriptor> translateToCaller(MethodInvokeNode min, int paramIdx,
553 NTuple<Descriptor> srcDescTuple) {
555 NTuple<Descriptor> callerTuple = new NTuple<Descriptor>();
557 NTuple<Descriptor> argTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
559 for (int i = 0; i < argTuple.size(); i++) {
560 callerTuple.add(argTuple.get(i));
563 for (int i = 1; i < srcDescTuple.size(); i++) {
564 callerTuple.add(srcDescTuple.get(i));
570 private NTuple<Descriptor> translateToCaller(NTuple<Descriptor> dstDescTuple,
571 NTuple<Descriptor> baseTuple) {
572 NTuple<Descriptor> callerDescTuple = new NTuple<Descriptor>();
574 callerDescTuple.addAll(baseTuple);
575 for (int i = 1; i < dstDescTuple.size(); i++) {
576 callerDescTuple.add(dstDescTuple.get(i));
579 return callerDescTuple;
582 public LocationSummary getLocationSummary(Descriptor d) {
583 if (!mapDescToLocationSummary.containsKey(d)) {
584 if (d instanceof MethodDescriptor) {
585 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
586 } else if (d instanceof ClassDescriptor) {
587 mapDescToLocationSummary.put(d, new FieldSummary());
590 return mapDescToLocationSummary.get(d);
593 private void generateMethodSummary() {
595 Set<MethodDescriptor> keySet = md2lattice.keySet();
596 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
597 MethodDescriptor md = (MethodDescriptor) iterator.next();
599 System.out.println("\nSSJAVA: generate method summary: " + md);
601 FlowGraph flowGraph = getFlowGraph(md);
602 MethodSummary methodSummary = getMethodSummary(md);
604 // construct a parameter mapping that maps a parameter descriptor to an inferred composite
607 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
608 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
609 NTuple<Location> locTuple = flowNode.getLocTuple();
611 CompositeLocation assignedCompLoc = flowNode.getCompositeLocation();
612 CompositeLocation inferredCompLoc;
613 if (assignedCompLoc != null) {
614 inferredCompLoc = translateCompositeLocation(assignedCompLoc);
616 Location loc = locTuple.get(0);
617 inferredCompLoc = new CompositeLocation(loc);
619 System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc);
620 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
627 private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
628 CompositeLocation newCompLoc = new CompositeLocation();
630 // System.out.println("compLoc=" + compLoc);
631 for (int i = 0; i < compLoc.getSize(); i++) {
632 Location loc = compLoc.get(i);
633 Descriptor enclosingDescriptor = loc.getDescriptor();
634 Descriptor locDescriptor = loc.getLocDescriptor();
636 HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
637 // System.out.println("-hnode=" + hnode + " from=" + locDescriptor +
638 // " enclosingDescriptor="
639 // + enclosingDescriptor);
640 // System.out.println("-getLocationSummary(enclosingDescriptor)="
641 // + getLocationSummary(enclosingDescriptor));
642 String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
643 // System.out.println("-locName=" + locName);
644 Location newLoc = new Location(enclosingDescriptor, locName);
645 newLoc.setLocDescriptor(locDescriptor);
646 newCompLoc.addLocation(newLoc);
652 private void debug_writeLattices() {
654 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
655 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
656 Descriptor key = (Descriptor) iterator.next();
657 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
658 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
659 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
660 if (key instanceof ClassDescriptor) {
661 writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
663 } else if (key instanceof MethodDescriptor) {
664 MethodDescriptor md = (MethodDescriptor) key;
665 writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
669 LocationSummary ls = getLocationSummary(key);
670 System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
673 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
674 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
675 ClassDescriptor cd = (ClassDescriptor) iterator.next();
676 writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
677 cd2lattice.get(cd), "");
680 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
681 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
682 MethodDescriptor md = (MethodDescriptor) iterator.next();
683 writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
684 md2lattice.get(md), "");
689 private void buildLattice() {
691 BuildLattice buildLattice = new BuildLattice(this);
693 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
694 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
695 Descriptor desc = (Descriptor) iterator.next();
697 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
699 addMapDescToSimpleLattice(desc, simpleLattice);
701 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
702 System.out.println("## insertIntermediateNodesToStraightLine:"
703 + simpleHierarchyGraph.getName());
704 SSJavaLattice<String> lattice =
705 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
706 lattice.removeRedundantEdges();
708 if (desc instanceof ClassDescriptor) {
710 cd2lattice.put((ClassDescriptor) desc, lattice);
711 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
712 } else if (desc instanceof MethodDescriptor) {
714 md2lattice.put((MethodDescriptor) desc, lattice);
715 MethodDescriptor md = (MethodDescriptor) desc;
716 ClassDescriptor cd = md.getClassDesc();
717 // ssjava.writeLatticeDotFile(cd, md, lattice);
720 // System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
721 // HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
722 // HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
723 // skeletonGraphWithCombinationNode.setName(desc + "_SC");
725 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
726 // System.out.println("Identifying Combination Nodes:");
727 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
728 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
729 // mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
734 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
735 mapDescriptorToSimpleLattice.put(desc, lattice);
738 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
739 return mapDescriptorToSimpleLattice.get(desc);
742 private void simplifyHierarchyGraph() {
743 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
744 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
745 Descriptor desc = (Descriptor) iterator.next();
746 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
747 simpleHierarchyGraph.setName(desc + "_SIMPLE");
748 simpleHierarchyGraph.removeRedundantEdges();
749 // simpleHierarchyGraph.simplifyHierarchyGraph();
750 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
754 private void insertCombinationNodes() {
755 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
756 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
757 Descriptor desc = (Descriptor) iterator.next();
758 System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
759 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
760 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
761 skeletonGraphWithCombinationNode.setName(desc + "_SC");
763 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
764 System.out.println("Identifying Combination Nodes:");
765 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
766 skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
767 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
771 private void constructSkeletonHierarchyGraph() {
772 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
773 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
774 Descriptor desc = (Descriptor) iterator.next();
775 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
776 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
777 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
778 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
779 skeletonGraph.simplifyHierarchyGraph();
780 // skeletonGraph.combineRedundantNodes(false);
781 // skeletonGraph.removeRedundantEdges();
782 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
786 private void debug_writeHierarchyDotFiles() {
788 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
789 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
790 Descriptor desc = (Descriptor) iterator.next();
791 getHierarchyGraph(desc).writeGraph();
796 private void debug_writeSimpleHierarchyDotFiles() {
798 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
799 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
800 Descriptor desc = (Descriptor) iterator.next();
801 getHierarchyGraph(desc).writeGraph();
802 getSimpleHierarchyGraph(desc).writeGraph();
807 private void debug_writeSkeletonHierarchyDotFiles() {
809 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
810 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
811 Descriptor desc = (Descriptor) iterator.next();
812 getSkeletonHierarchyGraph(desc).writeGraph();
817 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
819 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
820 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
821 Descriptor desc = (Descriptor) iterator.next();
822 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
827 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
828 return mapDescriptorToSimpleHierarchyGraph.get(d);
831 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
832 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
833 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
835 return mapDescriptorToSkeletonHierarchyGraph.get(d);
838 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
839 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
840 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
842 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
845 private void constructHierarchyGraph() {
847 // do fixed-point analysis
850 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
852 // Collections.sort(descriptorListToAnalyze, new
853 // Comparator<MethodDescriptor>() {
854 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
855 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
859 // current descriptors to visit in fixed-point interprocedural analysis,
860 // prioritized by dependency in the call graph
861 methodDescriptorsToVisitStack.clear();
863 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
864 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
866 while (!descriptorListToAnalyze.isEmpty()) {
867 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
868 methodDescriptorsToVisitStack.add(md);
871 // analyze scheduled methods until there are no more to visit
872 while (!methodDescriptorsToVisitStack.isEmpty()) {
873 // start to analyze leaf node
874 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
876 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
877 // MethodSummary methodSummary = new MethodSummary(md);
879 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
880 // curMethodInfo = methodInfo;
882 System.out.println();
883 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
885 constructHierarchyGraph(md, hierarchyGraph);
887 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
888 // MethodSummary prevMethodSummary = getMethodSummary(md);
890 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
892 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
893 // mapDescToLocationSummary.put(md, methodSummary);
895 // results for callee changed, so enqueue dependents caller for
897 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
898 while (depsItr.hasNext()) {
899 MethodDescriptor methodNext = depsItr.next();
900 if (!methodDescriptorsToVisitStack.contains(methodNext)
901 && methodDescriptorToVistSet.contains(methodNext)) {
902 methodDescriptorsToVisitStack.add(methodNext);
912 private HierarchyGraph getHierarchyGraph(Descriptor d) {
913 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
914 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
916 return mapDescriptorToHierarchyGraph.get(d);
919 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
921 // visit each node of method flow graph
922 FlowGraph fg = getFlowGraph(md);
923 Set<FlowNode> nodeSet = fg.getNodeSet();
925 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
926 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
927 Descriptor desc = (Descriptor) iterator.next();
928 methodGraph.getHNode(desc).setSkeleton(true);
931 // for the method lattice, we need to look at the first element of
932 // NTuple<Descriptor>
933 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
934 FlowNode srcNode = (FlowNode) iterator.next();
936 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(srcNode);
937 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
938 FlowEdge outEdge = (FlowEdge) iterator2.next();
939 FlowNode dstNode = outEdge.getDst();
941 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
942 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
944 if (outEdge.getInitTuple().equals(srcNodeTuple)
945 && outEdge.getEndTuple().equals(dstNodeTuple)) {
947 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
948 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
950 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
951 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
953 // value flows between fields
954 Descriptor desc = srcCurTuple.get(0);
955 ClassDescriptor classDesc;
957 if (desc.equals(GLOBALDESC)) {
958 classDesc = md.getClassDesc();
960 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
961 classDesc = varDesc.getType().getClassDesc();
963 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
966 // value flow between local var - local var or local var - field
968 Descriptor srcDesc = srcCurTuple.get(0);
969 Descriptor dstDesc = dstCurTuple.get(0);
971 methodGraph.addEdge(srcDesc, dstDesc);
973 if (fg.isParamDesc(srcDesc)) {
974 methodGraph.setParamHNode(srcDesc);
976 if (fg.isParamDesc(dstDesc)) {
977 methodGraph.setParamHNode(dstDesc);
988 private MethodSummary getMethodSummary(MethodDescriptor md) {
989 if (!mapDescToLocationSummary.containsKey(md)) {
990 mapDescToLocationSummary.put(md, new MethodSummary(md));
992 return (MethodSummary) mapDescToLocationSummary.get(md);
995 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
997 String classSymbol = cd.getSymbol();
998 int idx = classSymbol.lastIndexOf("$");
1000 classSymbol = classSymbol.substring(idx + 1);
1003 String pattern = "class " + classSymbol + " ";
1004 if (strLine.indexOf(pattern) != -1) {
1005 mapDescToDefinitionLine.put(cd, lineNum);
1009 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
1011 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
1012 MethodDescriptor md = (MethodDescriptor) iterator.next();
1013 String pattern = md.getMethodDeclaration();
1014 if (strLine.indexOf(pattern) != -1) {
1015 mapDescToDefinitionLine.put(md, lineNum);
1016 methodSet.remove(md);
1023 private void readOriginalSourceFiles() {
1025 SymbolTable classtable = state.getClassSymbolTable();
1027 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
1028 classDescSet.addAll(classtable.getValueSet());
1031 // inefficient implement. it may re-visit the same file if the file
1032 // contains more than one class definitions.
1033 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
1034 ClassDescriptor cd = (ClassDescriptor) iterator.next();
1036 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
1037 methodSet.addAll(cd.getMethodTable().getValueSet());
1039 String sourceFileName = cd.getSourceFileName();
1040 Vector<String> lineVec = new Vector<String>();
1042 mapFileNameToLineVector.put(sourceFileName, lineVec);
1044 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
1047 lineVec.add(""); // the index is started from 1.
1048 while ((strLine = in.readLine()) != null) {
1049 lineVec.add(lineNum, strLine);
1050 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
1051 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
1057 } catch (IOException e) {
1058 e.printStackTrace();
1063 private String generateLatticeDefinition(Descriptor desc) {
1065 Set<String> sharedLocSet = new HashSet<String>();
1067 SSJavaLattice<String> lattice = getLattice(desc);
1068 String rtr = "@LATTICE(\"";
1070 Map<String, Set<String>> map = lattice.getTable();
1071 Set<String> keySet = map.keySet();
1072 boolean first = true;
1073 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1074 String key = (String) iterator.next();
1075 if (!key.equals(lattice.getTopItem())) {
1076 Set<String> connectedSet = map.get(key);
1078 if (connectedSet.size() == 1) {
1079 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
1087 if (lattice.isSharedLoc(key)) {
1088 rtr += "," + key + "*";
1093 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
1094 String loc = (String) iterator2.next();
1095 if (!loc.equals(lattice.getBottomItem())) {
1102 rtr += loc + "<" + key;
1103 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
1104 rtr += "," + key + "*";
1105 sharedLocSet.add(key);
1107 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
1108 rtr += "," + loc + "*";
1109 sharedLocSet.add(loc);
1119 if (desc instanceof MethodDescriptor) {
1120 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
1122 MethodLocationInfo methodLocInfo = getMethodLocationInfo((MethodDescriptor) desc);
1124 if (returnType != null && (!returnType.isVoid())) {
1126 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodLocInfo.getReturnLoc()) + "\")";
1129 rtr += "\n@THISLOC(\"this\")";
1130 rtr += "\n@GLOBALLOC(\"GLOBALLOC\")";
1132 CompositeLocation pcLoc = methodLocInfo.getPCLoc();
1133 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
1134 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
1142 private void generateAnnoatedCode() {
1144 readOriginalSourceFiles();
1147 while (!toAnalyzeIsEmpty()) {
1148 ClassDescriptor cd = toAnalyzeNext();
1150 setupToAnalazeMethod(cd);
1152 LocationInfo locInfo = mapClassToLocationInfo.get(cd);
1153 String sourceFileName = cd.getSourceFileName();
1155 if (cd.isInterface()) {
1159 int classDefLine = mapDescToDefinitionLine.get(cd);
1160 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
1162 if (locInfo == null) {
1163 locInfo = getLocationInfo(cd);
1166 for (Iterator iter = cd.getFields(); iter.hasNext();) {
1167 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
1168 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
1169 String locIdentifier = locInfo.getFieldInferLocation(fieldDesc).getLocIdentifier();
1170 if (!getLattice(cd).getElementSet().contains(locIdentifier)) {
1171 getLattice(cd).put(locIdentifier);
1176 String fieldLatticeDefStr = generateLatticeDefinition(cd);
1177 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
1178 sourceVec.set(classDefLine, annoatedSrc);
1180 // generate annotations for field declarations
1181 LocationInfo fieldLocInfo = getLocationInfo(cd);
1182 Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
1184 for (Iterator iter = cd.getFields(); iter.hasNext();) {
1185 FieldDescriptor fd = (FieldDescriptor) iter.next();
1187 String locAnnotationStr;
1188 CompositeLocation inferLoc = inferLocMap.get(fd);
1190 if (inferLoc != null) {
1191 // infer loc is null if the corresponding field is static and final
1192 locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
1193 int fdLineNum = fd.getLineNum();
1194 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
1195 String fieldDeclaration = fd.toString();
1196 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
1197 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
1198 sourceVec.set(fdLineNum, annoatedStr);
1203 while (!toAnalyzeMethodIsEmpty()) {
1204 MethodDescriptor md = toAnalyzeMethodNext();
1206 if (!ssjava.needTobeAnnotated(md)) {
1210 SSJavaLattice<String> methodLattice = md2lattice.get(md);
1211 if (methodLattice != null) {
1213 int methodDefLine = md.getLineNum();
1215 MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
1217 Map<Descriptor, CompositeLocation> methodInferLocMap =
1218 methodLocInfo.getMapDescToInferLocation();
1219 Set<Descriptor> localVarDescSet = methodInferLocMap.keySet();
1221 Set<String> localLocElementSet = methodLattice.getElementSet();
1223 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
1224 Descriptor localVarDesc = (Descriptor) iterator.next();
1225 CompositeLocation inferLoc = methodInferLocMap.get(localVarDesc);
1227 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
1228 if (!localLocElementSet.contains(localLocIdentifier)) {
1229 methodLattice.put(localLocIdentifier);
1232 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
1234 if (!isParameter(md, localVarDesc)) {
1235 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
1236 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
1237 String orgSourceLine = sourceVec.get(varLineNum);
1239 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
1241 String annoatedStr =
1242 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
1243 + orgSourceLine.substring(idx);
1244 sourceVec.set(varLineNum, annoatedStr);
1247 String methodDefStr = sourceVec.get(methodDefLine);
1250 getParamLocation(methodDefStr,
1251 generateVarDeclaration((VarDescriptor) localVarDesc));
1255 String annoatedStr =
1256 methodDefStr.substring(0, idx) + locAnnotationStr + " "
1257 + methodDefStr.substring(idx);
1258 sourceVec.set(methodDefLine, annoatedStr);
1263 // check if the lattice has to have the location type for the this
1266 // boolean needToAddthisRef = hasThisReference(md);
1267 if (localLocElementSet.contains("this")) {
1268 methodLattice.put("this");
1271 String methodLatticeDefStr = generateLatticeDefinition(md);
1272 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
1273 sourceVec.set(methodDefLine, annoatedStr);
1283 private boolean hasThisReference(MethodDescriptor md) {
1285 FlowGraph fg = getFlowGraph(md);
1286 Set<FlowNode> nodeSet = fg.getNodeSet();
1287 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1288 FlowNode flowNode = (FlowNode) iterator.next();
1289 if (flowNode.getLocTuple().get(0).equals(md.getThis())) {
1297 private int getParamLocation(String methodStr, String paramStr) {
1299 String pattern = paramStr + ",";
1301 int idx = methodStr.indexOf(pattern);
1305 pattern = paramStr + ")";
1306 return methodStr.indexOf(pattern);
1311 private String generateVarDeclaration(VarDescriptor varDesc) {
1313 TypeDescriptor td = varDesc.getType();
1314 String rtr = td.toString();
1316 for (int i = 0; i < td.getArrayCount(); i++) {
1320 rtr += " " + varDesc.getName();
1325 private String generateLocationAnnoatation(CompositeLocation loc) {
1328 Location methodLoc = loc.get(0);
1329 rtr += methodLoc.getLocIdentifier();
1331 for (int i = 1; i < loc.getSize(); i++) {
1332 Location element = loc.get(i);
1333 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
1339 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
1340 return getFlowGraph(md).isParamDesc(localVarDesc);
1343 private String extractFileName(String fileName) {
1344 int idx = fileName.lastIndexOf("/");
1348 return fileName.substring(idx + 1);
1353 private void codeGen() {
1355 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
1356 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
1357 String orgFileName = (String) iterator.next();
1358 String outputFileName = extractFileName(orgFileName);
1360 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
1364 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
1365 BufferedWriter out = new BufferedWriter(fileWriter);
1367 for (int i = 0; i < sourceVec.size(); i++) {
1368 out.write(sourceVec.get(i));
1372 } catch (IOException e) {
1373 e.printStackTrace();
1380 private void simplifyLattices() {
1384 while (!toAnalyzeIsEmpty()) {
1385 ClassDescriptor cd = toAnalyzeNext();
1386 setupToAnalazeMethod(cd);
1388 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
1389 if (classLattice != null) {
1390 System.out.println("@@@check lattice=" + cd);
1391 checkLatticeProperty(cd, classLattice);
1394 while (!toAnalyzeMethodIsEmpty()) {
1395 MethodDescriptor md = toAnalyzeMethodNext();
1396 SSJavaLattice<String> methodLattice = md2lattice.get(md);
1397 if (methodLattice != null) {
1398 System.out.println("@@@check lattice=" + md);
1399 checkLatticeProperty(md, methodLattice);
1406 while (!toAnalyzeIsEmpty()) {
1407 ClassDescriptor cd = toAnalyzeNext();
1409 setupToAnalazeMethod(cd);
1411 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
1412 if (classLattice != null) {
1413 classLattice.removeRedundantEdges();
1416 while (!toAnalyzeMethodIsEmpty()) {
1417 MethodDescriptor md = toAnalyzeMethodNext();
1418 SSJavaLattice<String> methodLattice = md2lattice.get(md);
1419 if (methodLattice != null) {
1420 methodLattice.removeRedundantEdges();
1427 private boolean checkLatticeProperty(Descriptor d, SSJavaLattice<String> lattice) {
1428 // if two elements has the same incoming node set,
1429 // we need to merge two elements ...
1432 boolean isModified = false;
1434 isUpdated = removeNodeSharingSameIncomingNodes(d, lattice);
1435 if (!isModified && isUpdated) {
1438 } while (isUpdated);
1443 private boolean removeNodeSharingSameIncomingNodes(Descriptor d, SSJavaLattice<String> lattice) {
1444 LocationInfo locInfo = getLocationInfo(d);
1445 Map<String, Set<String>> map = lattice.getIncomingElementMap();
1446 Set<String> keySet = map.keySet();
1447 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1448 String key = (String) iterator.next();
1449 Set<String> incomingSetKey = map.get(key);
1451 // System.out.println("key=" + key + " incomingSetKey=" +
1453 if (incomingSetKey.size() > 0) {
1454 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1455 String cur = (String) iterator2.next();
1456 if (!cur.equals(key)) {
1457 Set<String> incomingSetCur = map.get(cur);
1458 if (incomingSetCur.equals(incomingSetKey)) {
1459 if (!(incomingSetCur.size() == 1 && incomingSetCur.contains(lattice.getTopItem()))) {
1460 // NEED TO MERGE HERE!!!!
1461 System.out.println("@@@Try merge=" + cur + " " + key);
1463 Set<String> mergeSet = new HashSet<String>();
1467 String newMergeLoc = "MLoc" + (SSJavaLattice.seed++);
1469 System.out.println("---ASSIGN NEW MERGE LOC=" + newMergeLoc + " to " + mergeSet);
1470 lattice.mergeIntoNewLocation(mergeSet, newMergeLoc);
1472 for (Iterator miterator = mergeSet.iterator(); miterator.hasNext();) {
1473 String oldLocSymbol = (String) miterator.next();
1475 Set<Pair<Descriptor, Descriptor>> inferLocSet =
1476 locInfo.getRelatedInferLocSet(oldLocSymbol);
1477 System.out.println("---update related locations=" + inferLocSet
1478 + " oldLocSymbol=" + oldLocSymbol);
1480 for (Iterator miterator2 = inferLocSet.iterator(); miterator2.hasNext();) {
1481 Pair<Descriptor, Descriptor> pair =
1482 (Pair<Descriptor, Descriptor>) miterator2.next();
1483 Descriptor enclosingDesc = pair.getFirst();
1484 Descriptor desc = pair.getSecond();
1486 System.out.println("---inferLoc pair=" + pair);
1488 CompositeLocation inferLoc =
1489 getLocationInfo(enclosingDesc).getInferLocation(desc);
1490 System.out.println("oldLoc=" + inferLoc);
1491 // if (curMethodInfo.md.equals(enclosingDesc)) {
1492 // inferLoc = curMethodInfo.getInferLocation(desc);
1495 // getLocationInfo(enclosingDesc).getInferLocation(desc);
1498 Location locElement = inferLoc.get(inferLoc.getSize() - 1);
1500 locElement.setLocIdentifier(newMergeLoc);
1501 locInfo.addMapLocSymbolToRelatedInferLoc(newMergeLoc, enclosingDesc, desc);
1503 // if (curMethodInfo.md.equals(enclosingDesc)) {
1504 // inferLoc = curMethodInfo.getInferLocation(desc);
1507 // getLocationInfo(enclosingDesc).getInferLocation(desc);
1510 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1511 System.out.println("---New Infer Loc=" + inferLoc);
1515 locInfo.removeRelatedInferLocSet(oldLocSymbol, newMergeLoc);
1519 for (Iterator iterator3 = mergeSet.iterator(); iterator3.hasNext();) {
1520 String oldLoc = (String) iterator3.next();
1521 lattice.remove(oldLoc);
1534 private void checkLattices() {
1536 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
1538 // current descriptors to visit in fixed-point interprocedural analysis,
1540 // dependency in the call graph
1541 methodDescriptorsToVisitStack.clear();
1543 // descriptorListToAnalyze.removeFirst();
1545 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1546 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1548 while (!descriptorListToAnalyze.isEmpty()) {
1549 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1550 checkLatticesOfVirtualMethods(md);
1555 private void debug_writeLatticeDotFile() {
1556 // generate lattice dot file
1560 while (!toAnalyzeIsEmpty()) {
1561 ClassDescriptor cd = toAnalyzeNext();
1563 setupToAnalazeMethod(cd);
1565 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
1566 if (classLattice != null) {
1567 ssjava.writeLatticeDotFile(cd, null, classLattice);
1568 debug_printDescriptorToLocNameMapping(cd);
1571 while (!toAnalyzeMethodIsEmpty()) {
1572 MethodDescriptor md = toAnalyzeMethodNext();
1573 SSJavaLattice<String> methodLattice = md2lattice.get(md);
1574 if (methodLattice != null) {
1575 ssjava.writeLatticeDotFile(cd, md, methodLattice);
1576 debug_printDescriptorToLocNameMapping(md);
1583 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
1585 LocationInfo info = getLocationInfo(desc);
1586 System.out.println("## " + desc + " ##");
1587 System.out.println(info.getMapDescToInferLocation());
1588 LocationInfo locInfo = getLocationInfo(desc);
1589 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
1590 System.out.println("###################");
1594 private void inferLattices() {
1596 // do fixed-point analysis
1599 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
1601 // Collections.sort(descriptorListToAnalyze, new
1602 // Comparator<MethodDescriptor>() {
1603 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
1604 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
1608 // current descriptors to visit in fixed-point interprocedural analysis,
1610 // dependency in the call graph
1611 methodDescriptorsToVisitStack.clear();
1613 // descriptorListToAnalyze.removeFirst();
1615 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1616 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1618 while (!descriptorListToAnalyze.isEmpty()) {
1619 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1620 methodDescriptorsToVisitStack.add(md);
1623 // analyze scheduled methods until there are no more to visit
1624 while (!methodDescriptorsToVisitStack.isEmpty()) {
1625 // start to analyze leaf node
1626 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
1628 SSJavaLattice<String> methodLattice =
1629 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
1631 MethodLocationInfo methodInfo = new MethodLocationInfo(md);
1632 curMethodInfo = methodInfo;
1634 System.out.println();
1635 System.out.println("SSJAVA: Inferencing the lattice from " + md);
1638 analyzeMethodLattice(md, methodLattice, methodInfo);
1639 } catch (CyclicFlowException e) {
1640 throw new Error("Fail to generate the method lattice for " + md);
1643 SSJavaLattice<String> prevMethodLattice = getMethodLattice(md);
1644 MethodLocationInfo prevMethodInfo = getMethodLocationInfo(md);
1646 if ((!methodLattice.equals(prevMethodLattice)) || (!methodInfo.equals(prevMethodInfo))) {
1648 setMethodLattice(md, methodLattice);
1649 setMethodLocInfo(md, methodInfo);
1651 // results for callee changed, so enqueue dependents caller for
1653 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
1654 while (depsItr.hasNext()) {
1655 MethodDescriptor methodNext = depsItr.next();
1656 if (!methodDescriptorsToVisitStack.contains(methodNext)
1657 && methodDescriptorToVistSet.contains(methodNext)) {
1658 methodDescriptorsToVisitStack.add(methodNext);
1668 private void calculateExtraLocations() {
1669 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
1670 for (Iterator iterator = descriptorListToAnalyze.iterator(); iterator.hasNext();) {
1671 MethodDescriptor md = (MethodDescriptor) iterator.next();
1672 calculateExtraLocations(md);
1676 private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
1677 mapMethodDescToMethodLocationInfo.put(md, methodInfo);
1680 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
1682 if (!md.isStatic()) {
1683 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1684 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
1686 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1687 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
1688 if (!md.equals(mdCallee)) {
1689 checkConsistency(md, mdCallee);
1697 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
1699 // check that two lattice have the same relations between parameters(+PC
1700 // LOC, GLOBAL_LOC RETURN LOC)
1702 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
1703 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
1705 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
1706 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
1708 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
1709 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
1711 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
1713 // add location types of paramters
1714 for (int idx = 0; idx < numParam; idx++) {
1715 list1.add(paramMap1.get(Integer.valueOf(idx)));
1716 list2.add(paramMap2.get(Integer.valueOf(idx)));
1719 // add program counter location
1720 list1.add(locInfo1.getPCLoc());
1721 list2.add(locInfo2.getPCLoc());
1723 if (!md1.getReturnType().isVoid()) {
1724 // add return value location
1725 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
1726 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
1731 // add global location type
1732 if (md1.isStatic()) {
1733 CompositeLocation globalLoc1 =
1734 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
1735 CompositeLocation globalLoc2 =
1736 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
1737 list1.add(globalLoc1);
1738 list2.add(globalLoc2);
1741 for (int i = 0; i < list1.size(); i++) {
1742 CompositeLocation locA1 = list1.get(i);
1743 CompositeLocation locA2 = list2.get(i);
1744 for (int k = 0; k < list1.size(); k++) {
1746 CompositeLocation locB1 = list1.get(k);
1747 CompositeLocation locB2 = list2.get(k);
1748 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
1750 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
1753 throw new Error("The method " + md1 + " is not consistent with the method " + md2
1754 + ".:: They have a different ordering relation between locations (" + locA1 + ","
1755 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
1763 private String getSymbol(int idx, FlowNode node) {
1764 Descriptor desc = node.getDescTuple().get(idx);
1765 return desc.getSymbol();
1768 private Descriptor getDescriptor(int idx, FlowNode node) {
1769 Descriptor desc = node.getDescTuple().get(idx);
1773 private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
1774 MethodLocationInfo methodInfo) throws CyclicFlowException {
1776 // first take a look at method invocation nodes to newly added relations
1778 analyzeLatticeMethodInvocationNode(md, methodLattice, methodInfo);
1780 if (!md.isStatic()) {
1781 // set the this location
1782 String thisLocSymbol = md.getThis().getSymbol();
1783 methodInfo.setThisLocName(thisLocSymbol);
1786 // set the global location
1787 methodInfo.setGlobalLocName(LocationInference.GLOBALLOC);
1788 methodInfo.mapDescriptorToLocation(GLOBALDESC, new CompositeLocation(
1789 new Location(md, GLOBALLOC)));
1791 // visit each node of method flow graph
1792 FlowGraph fg = getFlowGraph(md);
1793 Set<FlowNode> nodeSet = fg.getNodeSet();
1795 // for the method lattice, we need to look at the first element of
1796 // NTuple<Descriptor>
1797 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1798 FlowNode srcNode = (FlowNode) iterator.next();
1800 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(srcNode);
1801 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
1802 FlowEdge outEdge = (FlowEdge) iterator2.next();
1803 FlowNode dstNode = outEdge.getDst();
1805 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
1806 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
1808 if (outEdge.getInitTuple().equals(srcNodeTuple)
1809 && outEdge.getEndTuple().equals(dstNodeTuple)) {
1811 if ((srcNodeTuple.size() > 1 && dstNodeTuple.size() > 1)
1812 && srcNodeTuple.get(0).equals(dstNodeTuple.get(0))) {
1814 // value flows between fields
1815 Descriptor desc = srcNodeTuple.get(0);
1816 ClassDescriptor classDesc;
1818 if (desc.equals(GLOBALDESC)) {
1819 classDesc = md.getClassDesc();
1821 VarDescriptor varDesc = (VarDescriptor) srcNodeTuple.get(0);
1822 classDesc = varDesc.getType().getClassDesc();
1824 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, 1);
1827 // value flow between local var - local var or local var - field
1828 addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
1834 // create mapping from param idx to inferred composite location
1837 if (!md.isStatic()) {
1838 // add 'this' reference location
1840 methodInfo.addMapParamIdxToInferLoc(0, methodInfo.getInferLocation(md.getThis()));
1845 for (int idx = 0; idx < md.numParameters(); idx++) {
1846 Descriptor paramDesc = md.getParameter(idx);
1847 CompositeLocation inferParamLoc = methodInfo.getInferLocation(paramDesc);
1848 methodInfo.addMapParamIdxToInferLoc(idx + offset, inferParamLoc);
1853 private void calculateExtraLocations(MethodDescriptor md) {
1854 // calcualte pcloc, returnloc,...
1856 SSJavaLattice<String> methodLattice = getMethodLattice(md);
1857 MethodLocationInfo methodInfo = getMethodLocationInfo(md);
1858 FlowGraph fg = getFlowGraph(md);
1859 Set<FlowNode> nodeSet = fg.getNodeSet();
1861 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1862 FlowNode flowNode = (FlowNode) iterator.next();
1863 if (flowNode.isDeclaratonNode()) {
1864 CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
1865 String locIdentifier = inferLoc.get(0).getLocIdentifier();
1866 if (!methodLattice.containsKey(locIdentifier)) {
1867 methodLattice.put(locIdentifier);
1873 Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
1874 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
1877 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
1878 // calculate the initial program counter location
1879 // PC location is higher than location types of all parameters
1880 String pcLocSymbol = "PCLOC";
1882 Set<CompositeLocation> paramInFlowSet = new HashSet<CompositeLocation>();
1884 for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
1885 Integer paramIdx = (Integer) iterator.next();
1887 FlowNode paramFlowNode = fg.getParamFlowNode(paramIdx);
1889 if (fg.getIncomingFlowNodeSet(paramFlowNode).size() > 0) {
1890 // parameter has in-value flows
1891 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
1892 paramInFlowSet.add(inferLoc);
1896 if (paramInFlowSet.size() > 0) {
1897 CompositeLocation lowestLoc = getLowest(methodLattice, paramInFlowSet);
1898 assert (lowestLoc != null);
1899 methodInfo.setPCLoc(lowestLoc);
1904 // calculate a return location
1905 // the return location type is lower than all parameters and location
1908 if (!md.getReturnType().isVoid()) {
1909 // first, generate the set of return value location types that starts
1913 Set<CompositeLocation> inferFieldReturnLocSet = new HashSet<CompositeLocation>();
1915 Set<FlowNode> paramFlowNode = getParamNodeFlowingToReturnValue(md);
1916 Set<CompositeLocation> inferParamLocSet = new HashSet<CompositeLocation>();
1917 if (paramFlowNode != null) {
1918 for (Iterator iterator = paramFlowNode.iterator(); iterator.hasNext();) {
1919 FlowNode fn = (FlowNode) iterator.next();
1920 CompositeLocation inferLoc =
1921 generateInferredCompositeLocation(methodInfo, getFlowGraph(md).getLocationTuple(fn));
1922 inferParamLocSet.add(inferLoc);
1926 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
1928 skip: for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1929 FlowNode returnNode = (FlowNode) iterator.next();
1930 CompositeLocation inferReturnLoc =
1931 generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
1932 if (inferReturnLoc.get(0).getLocIdentifier().equals("this")) {
1933 // if the location type of the return value matches "this" reference
1934 // then, check whether this return value is equal to/lower than all
1936 // parameters that possibly flow into the return values
1937 for (Iterator iterator2 = inferParamLocSet.iterator(); iterator2.hasNext();) {
1938 CompositeLocation paramInferLoc = (CompositeLocation) iterator2.next();
1940 if ((!paramInferLoc.equals(inferReturnLoc))
1941 && !isGreaterThan(methodLattice, paramInferLoc, inferReturnLoc)) {
1945 inferFieldReturnLocSet.add(inferReturnLoc);
1950 if (inferFieldReturnLocSet.size() > 0) {
1952 CompositeLocation returnLoc = getLowest(methodLattice, inferFieldReturnLocSet);
1953 if (returnLoc == null) {
1954 // in this case, assign <'this',bottom> to the RETURNLOC
1955 returnLoc = new CompositeLocation(new Location(md, md.getThis().getSymbol()));
1956 returnLoc.addLocation(new Location(md.getClassDesc(), getLattice(md.getClassDesc())
1959 methodInfo.setReturnLoc(returnLoc);
1962 String returnLocSymbol = "RETURNLOC";
1963 CompositeLocation returnLocInferLoc =
1964 new CompositeLocation(new Location(md, returnLocSymbol));
1965 methodInfo.setReturnLoc(returnLocInferLoc);
1967 for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
1968 Integer paramIdx = (Integer) iterator.next();
1969 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
1970 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
1971 if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
1972 addRelationHigherToLower(methodLattice, methodInfo, paramLocLocalSymbol,
1977 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1978 FlowNode returnNode = (FlowNode) iterator.next();
1979 CompositeLocation inferLoc =
1980 generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
1981 if (!isGreaterThan(methodLattice, inferLoc, returnLocInferLoc)) {
1982 addRelation(methodLattice, methodInfo, inferLoc, returnLocInferLoc);
1989 } catch (CyclicFlowException e) {
1990 e.printStackTrace();
1995 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
1996 Set<String> higherLocSet = new HashSet<String>();
1998 Set<String> locSet = lattice.getTable().keySet();
1999 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
2000 String element = (String) iterator.next();
2001 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
2002 higherLocSet.add(element);
2005 return higherLocSet;
2008 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
2009 Set<CompositeLocation> set) {
2011 CompositeLocation lowest = set.iterator().next();
2013 if (set.size() == 1) {
2017 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
2018 CompositeLocation loc = (CompositeLocation) iterator.next();
2020 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
2021 // if there is a case where composite locations are incomparable, just
2026 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
2033 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
2034 CompositeLocation comp2) {
2036 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
2038 for (int idx = 0; idx < size; idx++) {
2039 Location loc1 = comp1.get(idx);
2040 Location loc2 = comp2.get(idx);
2042 Descriptor desc1 = loc1.getDescriptor();
2043 Descriptor desc2 = loc2.getDescriptor();
2045 if (!desc1.equals(desc2)) {
2046 throw new Error("Fail to compare " + comp1 + " and " + comp2);
2049 String symbol1 = loc1.getLocIdentifier();
2050 String symbol2 = loc2.getLocIdentifier();
2052 SSJavaLattice<String> lattice;
2054 lattice = methodLattice;
2056 lattice = getLattice(desc1);
2059 if (symbol1.equals(symbol2)) {
2061 } else if (!lattice.isComparable(symbol1, symbol2)) {
2070 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
2071 CompositeLocation comp2) {
2073 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
2075 for (int idx = 0; idx < size; idx++) {
2076 Location loc1 = comp1.get(idx);
2077 Location loc2 = comp2.get(idx);
2079 Descriptor desc1 = loc1.getDescriptor();
2080 Descriptor desc2 = loc2.getDescriptor();
2082 if (!desc1.equals(desc2)) {
2083 throw new Error("Fail to compare " + comp1 + " and " + comp2);
2086 String symbol1 = loc1.getLocIdentifier();
2087 String symbol2 = loc2.getLocIdentifier();
2089 SSJavaLattice<String> lattice;
2091 lattice = methodLattice;
2093 lattice = getLattice(desc1);
2096 if (symbol1.equals(symbol2)) {
2098 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
2109 private void recursiveAddRelationToLattice(int idx, MethodDescriptor md,
2110 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
2112 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
2113 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
2115 if (srcLocSymbol.equals(dstLocSymbol)) {
2116 recursiveAddRelationToLattice(idx + 1, md, srcInferLoc, dstInferLoc);
2119 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
2120 LocationInfo locInfo = getLocationInfo(parentDesc);
2122 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
2128 // private void propagateFlowsFromCallee(MethodInvokeNode min, MethodDescriptor mdCaller,
2129 // MethodDescriptor mdCallee) {
2131 // // the transformation for a call site propagates all relations between
2132 // // parameters from the callee
2133 // // if the method is virtual, it also grab all relations from any possible
2136 // Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2137 // if (mdCallee.isStatic()) {
2138 // setPossibleCallees.add(mdCallee);
2140 // Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
2141 // // removes method descriptors that are not invoked by the caller
2142 // calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
2143 // setPossibleCallees.addAll(calleeSet);
2146 // for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
2147 // MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
2148 // propagateFlowsToCaller(min, mdCaller, possibleMdCallee);
2153 private void contributeCalleeFlows(MethodInvokeNode min, MethodDescriptor mdCaller,
2154 MethodDescriptor mdCallee) {
2156 System.out.println("\n##contributeCalleeFlows callee=" + mdCallee + "TO caller=" + mdCaller);
2158 getSubGlobalFlowGraph(mdCallee);
2162 private FlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
2163 return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
2166 private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
2167 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
2169 System.out.println("\n##PROPAGATE callee=" + mdCallee + "TO caller=" + mdCaller);
2171 // if the parameter A reaches to the parameter B
2172 // then, add an edge the argument A -> the argument B to the caller's flow
2175 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
2176 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
2177 int numParam = calleeFlowGraph.getNumParameters();
2179 for (int i = 0; i < numParam; i++) {
2180 for (int k = 0; k < numParam; k++) {
2184 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
2185 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
2187 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
2188 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
2190 // check if the callee propagates an ordering constraints through
2193 Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
2195 if (localReachSet.contains(paramNode2)) {
2196 // need to propagate an ordering relation s.t. arg1 is higher
2199 System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
2201 .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
2203 // otherwise, flows between method/field locations...
2204 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
2205 System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
2209 System.out.println();
2213 System.out.println("##\n");
2217 private void propagateFlowsToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
2218 MethodDescriptor mdCallee) {
2220 System.out.println("\n##PROPAGATE callee=" + mdCallee + "TO caller=" + mdCaller);
2222 // if the parameter A reaches to the parameter B
2223 // then, add an edge the argument A -> the argument B to the caller's flow
2227 // also if a parameter is a composite location and is started with "this" reference,
2228 // need to make sure that the corresponding argument is higher than the translated location of
2231 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
2232 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
2233 int numParam = calleeFlowGraph.getNumParameters();
2235 for (int i = 0; i < numParam; i++) {
2236 for (int k = 0; k < numParam; k++) {
2240 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
2241 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
2243 System.out.println("param1=" + paramNode1 + " curDescTuple="
2244 + paramNode1.getCurrentDescTuple());
2245 System.out.println("param2=" + paramNode2 + " curDescTuple="
2246 + paramNode2.getCurrentDescTuple());
2248 // TODO: deprecated method
2249 // NodeTupleSet tupleSetArg1 = getNodeTupleSetByArgIdx(min, i);
2250 // NodeTupleSet tupleSetArg2 = getNodeTupleSetByArgIdx(min, k);
2251 NodeTupleSet tupleSetArg1 = null;
2252 NodeTupleSet tupleSetArg2 = null;
2254 for (Iterator<NTuple<Descriptor>> iter1 = tupleSetArg1.iterator(); iter1.hasNext();) {
2255 NTuple<Descriptor> arg1Tuple = iter1.next();
2257 for (Iterator<NTuple<Descriptor>> iter2 = tupleSetArg2.iterator(); iter2.hasNext();) {
2258 NTuple<Descriptor> arg2Tuple = iter2.next();
2260 // check if the callee propagates an ordering constraints through
2263 Set<FlowNode> localReachSet =
2264 calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
2266 if (localReachSet.contains(paramNode2)) {
2267 // need to propagate an ordering relation s.t. arg1 is higher
2271 .println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
2272 System.out.println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple="
2275 if (!min.getMethod().isStatic()) {
2276 // check if this is the case that values flow to/from the
2277 // current object reference 'this'
2279 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2280 Descriptor baseRef = baseTuple.get(baseTuple.size() - 1);
2282 System.out.println("paramNode1.getCurrentDescTuple()="
2283 + paramNode1.getCurrentDescTuple());
2284 // calculate the prefix of the argument
2286 if (arg2Tuple.size() == 1 && arg2Tuple.get(0).equals(baseRef)) {
2287 // in this case, the callee flow causes a caller flow to the object whose method
2290 if (!paramNode1.getCurrentDescTuple().startsWith(mdCallee.getThis())) {
2291 // check whether ???
2293 NTuple<Descriptor> param1Prefix =
2294 calculatePrefixForParam(callerFlowGraph, calleeFlowGraph, min, arg1Tuple,
2297 if (param1Prefix != null && param1Prefix.startsWith(mdCallee.getThis())) {
2298 // in this case, we need to create a new edge 'this.FIELD'->'this'
2299 // but we couldn't... instead we assign a new composite location started
2300 // with 'this' reference to the corresponding parameter
2302 CompositeLocation compLocForParam1 =
2303 generateCompositeLocation(mdCallee, param1Prefix);
2306 .println("set comp loc=" + compLocForParam1 + " to " + paramNode1);
2307 paramNode1.setCompositeLocation(compLocForParam1);
2309 // then, we need to make sure that the corresponding argument in the caller
2310 // is required to be higher than or equal to the translated parameter
2313 NTuple<Descriptor> translatedParamTuple =
2314 translateCompositeLocationToCaller(min, compLocForParam1);
2316 // TODO : check if the arg >= the tranlated parameter
2318 System.out.println("add a flow edge= " + arg1Tuple + "->"
2319 + translatedParamTuple);
2320 callerFlowGraph.addValueFlowEdge(arg1Tuple, translatedParamTuple);
2327 // param1 has already been assigned a composite location
2329 System.out.println("--param1 has already been assigned a composite location");
2330 CompositeLocation compLocForParam1 = paramNode1.getCompositeLocation();
2331 NTuple<Descriptor> translatedParamTuple =
2332 translateCompositeLocationToCaller(min, compLocForParam1);
2334 // TODO : check if the arg >= the tranlated parameter
2336 System.out.println("add a flow edge= " + arg1Tuple + "->"
2337 + translatedParamTuple);
2338 callerFlowGraph.addValueFlowEdge(arg1Tuple, translatedParamTuple);
2344 } else if (arg1Tuple.size() == 1 && arg1Tuple.get(0).equals(baseRef)) {
2345 // in this case, the callee flow causes a caller flow originated from the object
2347 // method is invoked.
2349 System.out.println("###FROM CASE");
2351 if (!paramNode2.getCurrentDescTuple().startsWith(mdCallee.getThis())) {
2353 NTuple<Descriptor> param2Prefix =
2354 calculatePrefixForParam(callerFlowGraph, calleeFlowGraph, min, arg2Tuple,
2357 if (param2Prefix != null && param2Prefix.startsWith(mdCallee.getThis())) {
2358 // in this case, we need to create a new edge 'this' ->
2359 // 'this.FIELD' but we couldn't... instead we assign the corresponding
2360 // parameter a new composite location started with 'this' reference
2362 CompositeLocation compLocForParam2 =
2363 generateCompositeLocation(mdCallee, param2Prefix);
2365 // System.out.println("set comp loc=" + compLocForParam2
2367 // " to " + paramNode2);
2368 paramNode1.setCompositeLocation(compLocForParam2);
2376 // otherwise, flows between method/field locations...
2377 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
2378 System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
2385 System.out.println();
2389 System.out.println("##\n");
2392 private NTuple<Descriptor> translateCompositeLocationToCaller(MethodInvokeNode min,
2393 CompositeLocation compLocForParam1) {
2394 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2396 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
2398 for (int i = 0; i < baseTuple.size(); i++) {
2399 tuple.add(baseTuple.get(i));
2402 for (int i = 1; i < compLocForParam1.getSize(); i++) {
2403 Location loc = compLocForParam1.get(i);
2404 tuple.add(loc.getLocDescriptor());
2410 private CompositeLocation generateCompositeLocation(MethodDescriptor md,
2411 NTuple<Descriptor> paramPrefix) {
2413 System.out.println("generateCompositeLocation=" + paramPrefix);
2415 CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
2417 Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
2418 // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
2419 // + lastDescOfPrefix.getClass());
2420 ClassDescriptor enclosingDescriptor;
2421 if (lastDescOfPrefix instanceof FieldDescriptor) {
2422 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
2423 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
2425 // var descriptor case
2426 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
2428 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
2430 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
2431 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
2433 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
2434 newLoc.setLocDescriptor(newLocDescriptor);
2435 newCompLoc.addLocation(newLoc);
2437 // System.out.println("--newCompLoc=" + newCompLoc);
2441 private NTuple<Descriptor> calculatePrefixForParam(FlowGraph callerFlowGraph,
2442 FlowGraph calleeFlowGraph, MethodInvokeNode min, NTuple<Descriptor> arg1Tuple,
2443 FlowNode paramNode1) {
2445 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2446 Descriptor baseRef = baseTuple.get(baseTuple.size() - 1);
2447 System.out.println("baseRef=" + baseRef);
2449 FlowNode flowNodeArg1 = callerFlowGraph.getFlowNode(arg1Tuple);
2450 List<NTuple<Descriptor>> callerPrefixList = calculatePrefixList(callerFlowGraph, flowNodeArg1);
2451 System.out.println("callerPrefixList=" + callerPrefixList);
2453 List<NTuple<Descriptor>> prefixList = calculatePrefixList(calleeFlowGraph, paramNode1);
2454 System.out.println("###prefixList from node=" + paramNode1 + " =" + prefixList);
2456 List<NTuple<Descriptor>> calleePrefixList =
2457 translatePrefixListToCallee(baseRef, min.getMethod(), callerPrefixList);
2459 System.out.println("calleePrefixList=" + calleePrefixList);
2461 Set<FlowNode> reachNodeSetFromParam1 = calleeFlowGraph.getReachFlowNodeSetFrom(paramNode1);
2462 System.out.println("reachNodeSetFromParam1=" + reachNodeSetFromParam1);
2464 for (int i = 0; i < calleePrefixList.size(); i++) {
2465 NTuple<Descriptor> curPrefix = calleePrefixList.get(i);
2466 Set<NTuple<Descriptor>> reachableCommonPrefixSet = new HashSet<NTuple<Descriptor>>();
2468 for (Iterator iterator2 = reachNodeSetFromParam1.iterator(); iterator2.hasNext();) {
2469 FlowNode reachNode = (FlowNode) iterator2.next();
2470 if (reachNode.getCurrentDescTuple().startsWith(curPrefix)) {
2471 reachableCommonPrefixSet.add(reachNode.getCurrentDescTuple());
2475 if (!reachableCommonPrefixSet.isEmpty()) {
2476 System.out.println("###REACHABLECOMONPREFIX=" + reachableCommonPrefixSet
2477 + " with curPreFix=" + curPrefix);
2486 private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
2487 MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
2489 List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
2491 for (int i = 0; i < callerPrefixList.size(); i++) {
2492 NTuple<Descriptor> prefix = callerPrefixList.get(i);
2493 if (prefix.startsWith(baseRef)) {
2494 NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
2495 calleePrefix.add(mdCallee.getThis());
2496 for (int k = 1; k < prefix.size(); k++) {
2497 calleePrefix.add(prefix.get(k));
2499 calleePrefixList.add(calleePrefix);
2503 return calleePrefixList;
2507 private List<NTuple<Descriptor>> calculatePrefixList(FlowGraph flowGraph, FlowNode flowNode) {
2509 System.out.println("\n##### calculatePrefixList=" + flowNode);
2511 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
2512 inNodeSet.add(flowNode);
2514 System.out.println("inNodeSet=" + inNodeSet);
2516 List<NTuple<Descriptor>> prefixList = new ArrayList<NTuple<Descriptor>>();
2518 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
2519 FlowNode inNode = (FlowNode) iterator.next();
2521 NTuple<Descriptor> inNodeTuple = inNode.getCurrentDescTuple();
2523 // CompositeLocation inNodeInferredLoc =
2524 // generateInferredCompositeLocation(methodInfo, inNodeTuple);
2525 // NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
2527 for (int i = 1; i < inNodeTuple.size(); i++) {
2528 NTuple<Descriptor> prefix = inNodeTuple.subList(0, i);
2529 if (!prefixList.contains(prefix)) {
2530 prefixList.add(prefix);
2535 Collections.sort(prefixList, new Comparator<NTuple<Descriptor>>() {
2536 public int compare(NTuple<Descriptor> arg0, NTuple<Descriptor> arg1) {
2537 int s0 = arg0.size();
2538 int s1 = arg1.size();
2541 } else if (s0 == s1) {
2553 public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
2555 CompositeLocation compLoc = new CompositeLocation();
2557 Descriptor enclosingDescriptor = md;
2559 for (int i = 0; i < tuple.size(); i++) {
2560 Descriptor curDescriptor = tuple.get(i);
2561 Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
2562 locElement.setLocDescriptor(curDescriptor);
2563 compLoc.addLocation(locElement);
2565 if (curDescriptor instanceof VarDescriptor) {
2566 enclosingDescriptor = md.getClassDesc();
2567 } else if (curDescriptor instanceof NameDescriptor) {
2568 // it is "GLOBAL LOC" case!
2569 enclosingDescriptor = GLOBALDESC;
2571 enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
2576 System.out.println("-convertToCompositeLocation from=" + tuple + " to " + compLoc);
2581 private LocationDescriptor generateNewLocationDescriptor() {
2582 return new LocationDescriptor("Loc" + (locSeed++));
2585 private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
2587 // return the index where the prefix shared by tuple1 and tuple2 is ended
2588 // if there is no prefix shared by both of them, return -1
2590 int minSize = tuple1.size();
2591 if (minSize > tuple2.size()) {
2592 minSize = tuple2.size();
2596 for (int i = 0; i < minSize; i++) {
2597 if (!tuple1.get(i).equals(tuple2.get(i))) {
2607 private void analyzeLatticeMethodInvocationNode(MethodDescriptor mdCaller,
2608 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo)
2609 throws CyclicFlowException {
2611 // the transformation for a call site propagates all relations between
2612 // parameters from the callee
2613 // if the method is virtual, it also grab all relations from any possible
2616 Set<MethodInvokeNode> setMethodInvokeNode =
2617 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
2619 if (setMethodInvokeNode != null) {
2621 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
2622 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
2623 MethodDescriptor mdCallee = min.getMethod();
2624 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2625 if (mdCallee.isStatic()) {
2626 setPossibleCallees.add(mdCallee);
2628 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
2629 // removes method descriptors that are not invoked by the caller
2630 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
2631 setPossibleCallees.addAll(calleeSet);
2634 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
2635 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
2636 propagateRelationToCaller(min, mdCaller, possibleMdCallee, methodLattice, methodInfo);
2644 private void propagateRelationToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
2645 MethodDescriptor possibleMdCallee, SSJavaLattice<String> methodLattice,
2646 MethodLocationInfo methodInfo) throws CyclicFlowException {
2648 SSJavaLattice<String> calleeLattice = getMethodLattice(possibleMdCallee);
2649 MethodLocationInfo calleeLocInfo = getMethodLocationInfo(possibleMdCallee);
2650 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
2652 int numParam = calleeLocInfo.getNumParam();
2653 for (int i = 0; i < numParam; i++) {
2654 CompositeLocation param1 = calleeLocInfo.getParamCompositeLocation(i);
2655 for (int k = 0; k < numParam; k++) {
2657 CompositeLocation param2 = calleeLocInfo.getParamCompositeLocation(k);
2659 if (isGreaterThan(getLattice(possibleMdCallee), param1, param2)) {
2660 // NodeTupleSet argDescTupleSet1 = getNodeTupleSetByArgIdx(min, i);
2661 // NodeTupleSet argDescTupleSet2 = getNodeTupleSetByArgIdx(min, k);
2662 NodeTupleSet argDescTupleSet1 = null;
2663 NodeTupleSet argDescTupleSet2 = null;
2665 // the callee has the relation in which param1 is higher than param2
2666 // therefore, the caller has to have the relation in which arg1 is
2669 for (Iterator<NTuple<Descriptor>> iterator = argDescTupleSet1.iterator(); iterator
2671 NTuple<Descriptor> argDescTuple1 = iterator.next();
2673 for (Iterator<NTuple<Descriptor>> iterator2 = argDescTupleSet2.iterator(); iterator2
2675 NTuple<Descriptor> argDescTuple2 = iterator2.next();
2677 // retreive inferred location by the local var descriptor
2679 NTuple<Location> tuple1 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple1);
2680 NTuple<Location> tuple2 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple2);
2682 // CompositeLocation higherInferLoc =
2683 // methodInfo.getInferLocation(argTuple1.get(0));
2684 // CompositeLocation lowerInferLoc =
2685 // methodInfo.getInferLocation(argTuple2.get(0));
2687 CompositeLocation inferLoc1 = generateInferredCompositeLocation(methodInfo, tuple1);
2688 CompositeLocation inferLoc2 = generateInferredCompositeLocation(methodInfo, tuple2);
2690 // addRelation(methodLattice, methodInfo, inferLoc1, inferLoc2);
2692 addFlowGraphEdge(mdCaller, argDescTuple1, argDescTuple2);
2705 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
2706 NTuple<Location> tuple) {
2708 // first, retrieve inferred location by the local var descriptor
2709 CompositeLocation inferLoc = new CompositeLocation();
2711 CompositeLocation localVarInferLoc =
2712 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
2714 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
2716 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
2717 inferLoc.addLocation(localVarInferLoc.get(i));
2720 for (int i = 1; i < tuple.size(); i++) {
2721 Location cur = tuple.get(i);
2722 Descriptor enclosingDesc = cur.getDescriptor();
2723 Descriptor curDesc = cur.getLocDescriptor();
2725 Location inferLocElement;
2726 if (curDesc == null) {
2727 // in this case, we have a newly generated location.
2728 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
2730 String fieldLocSymbol =
2731 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
2732 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
2733 inferLocElement.setLocDescriptor(curDesc);
2736 inferLoc.addLocation(inferLocElement);
2740 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
2744 private void addRelation(SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo,
2745 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
2747 System.out.println("addRelation --- srcInferLoc=" + srcInferLoc + " dstInferLoc="
2749 String srcLocalLocSymbol = srcInferLoc.get(0).getLocIdentifier();
2750 String dstLocalLocSymbol = dstInferLoc.get(0).getLocIdentifier();
2752 if (srcInferLoc.getSize() == 1 && dstInferLoc.getSize() == 1) {
2753 // add a new relation to the local lattice
2754 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
2755 } else if (srcInferLoc.getSize() > 1 && dstInferLoc.getSize() > 1) {
2756 // both src and dst have assigned to a composite location
2758 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
2759 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
2761 recursivelyAddRelation(1, srcInferLoc, dstInferLoc);
2764 // either src or dst has assigned to a composite location
2765 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
2766 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
2770 System.out.println();
2774 public LocationInfo getLocationInfo(Descriptor d) {
2775 if (d instanceof MethodDescriptor) {
2776 return getMethodLocationInfo((MethodDescriptor) d);
2778 return getFieldLocationInfo((ClassDescriptor) d);
2782 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
2784 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
2785 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
2788 return mapMethodDescToMethodLocationInfo.get(md);
2792 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
2794 if (!mapClassToLocationInfo.containsKey(cd)) {
2795 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
2798 return mapClassToLocationInfo.get(cd);
2802 private void addRelationToLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
2803 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode) throws CyclicFlowException {
2805 System.out.println();
2806 System.out.println("### addRelationToLattice src=" + srcNode + " dst=" + dstNode);
2808 // add a new binary relation of dstNode < srcNode
2809 FlowGraph flowGraph = getFlowGraph(md);
2811 System.out.println("***** src composite case::");
2812 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, srcNode, null);
2814 CompositeLocation srcInferLoc =
2815 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
2816 CompositeLocation dstInferLoc =
2817 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
2818 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
2819 } catch (CyclicFlowException e) {
2820 // there is a cyclic value flow... try to calculate a composite location
2821 // for the destination node
2822 System.out.println("***** dst composite case::");
2823 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, dstNode, srcNode);
2824 CompositeLocation srcInferLoc =
2825 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
2826 CompositeLocation dstInferLoc =
2827 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
2829 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
2830 } catch (CyclicFlowException e1) {
2831 throw new Error("Failed to merge cyclic value flows into a shared location.");
2837 private void recursivelyAddRelation(int idx, CompositeLocation srcInferLoc,
2838 CompositeLocation dstInferLoc) throws CyclicFlowException {
2840 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
2841 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
2843 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
2845 if (srcLocSymbol.equals(dstLocSymbol)) {
2846 // check if it is the case of shared location
2847 if (srcInferLoc.getSize() == (idx + 1) && dstInferLoc.getSize() == (idx + 1)) {
2848 Location inferLocElement = srcInferLoc.get(idx);
2849 System.out.println("SET SHARED LOCATION=" + inferLocElement);
2850 getLattice(inferLocElement.getDescriptor())
2851 .addSharedLoc(inferLocElement.getLocIdentifier());
2852 } else if (srcInferLoc.getSize() > (idx + 1) && dstInferLoc.getSize() > (idx + 1)) {
2853 recursivelyAddRelation(idx + 1, srcInferLoc, dstInferLoc);
2856 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
2861 private void recursivelyAddCompositeRelation(MethodDescriptor md, FlowGraph flowGraph,
2862 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode, Descriptor srcDesc,
2863 Descriptor dstDesc) throws CyclicFlowException {
2865 CompositeLocation inferSrcLoc;
2866 CompositeLocation inferDstLoc = methodInfo.getInferLocation(dstDesc);
2868 if (srcNode.getLocTuple().size() > 1) {
2870 inferSrcLoc = new CompositeLocation();
2872 NTuple<Location> locTuple = flowGraph.getLocationTuple(srcNode);
2873 for (int i = 0; i < locTuple.size(); i++) {
2874 inferSrcLoc.addLocation(locTuple.get(i));
2878 inferSrcLoc = methodInfo.getInferLocation(srcDesc);
2881 if (dstNode.getLocTuple().size() > 1) {
2883 inferDstLoc = new CompositeLocation();
2885 NTuple<Location> locTuple = flowGraph.getLocationTuple(dstNode);
2886 for (int i = 0; i < locTuple.size(); i++) {
2887 inferDstLoc.addLocation(locTuple.get(i));
2891 inferDstLoc = methodInfo.getInferLocation(dstDesc);
2894 recursiveAddRelationToLattice(1, md, inferSrcLoc, inferDstLoc);
2897 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
2898 NTuple<Location> prefix, NTuple<Location> element) {
2900 if (!map.containsKey(prefix)) {
2901 map.put(prefix, new HashSet<NTuple<Location>>());
2903 map.get(prefix).add(element);
2906 private boolean calculateCompositeLocation(FlowGraph flowGraph,
2907 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo, FlowNode flowNode,
2908 FlowNode srcNode) throws CyclicFlowException {
2910 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
2911 NTuple<Location> flowNodelocTuple = flowGraph.getLocationTuple(flowNode);
2913 if (localVarDesc.equals(methodInfo.getMethodDesc())) {
2917 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
2918 Set<FlowNode> reachableNodeSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
2920 Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
2921 new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
2923 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
2925 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
2926 FlowNode inNode = (FlowNode) iterator.next();
2927 NTuple<Location> inNodeTuple = flowGraph.getLocationTuple(inNode);
2929 CompositeLocation inNodeInferredLoc =
2930 generateInferredCompositeLocation(methodInfo, inNodeTuple);
2932 NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
2934 for (int i = 1; i < inNodeInferredLocTuple.size(); i++) {
2935 NTuple<Location> prefix = inNodeInferredLocTuple.subList(0, i);
2936 if (!prefixList.contains(prefix)) {
2937 prefixList.add(prefix);
2939 addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inNodeInferredLocTuple);
2943 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
2944 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
2945 int s0 = arg0.size();
2946 int s1 = arg1.size();
2949 } else if (s0 == s1) {
2957 // System.out.println("prefixList=" + prefixList);
2958 // System.out.println("reachableNodeSet=" + reachableNodeSet);
2960 // find out reachable nodes that have the longest common prefix
2961 for (int i = 0; i < prefixList.size(); i++) {
2962 NTuple<Location> curPrefix = prefixList.get(i);
2963 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
2965 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
2966 FlowNode reachableNode = (FlowNode) iterator2.next();
2967 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
2968 CompositeLocation reachLocInferLoc =
2969 generateInferredCompositeLocation(methodInfo, reachLocTuple);
2970 if (reachLocInferLoc.getTuple().startsWith(curPrefix)) {
2971 reachableCommonPrefixSet.add(reachLocTuple);
2975 if (!reachableCommonPrefixSet.isEmpty()) {
2976 // found reachable nodes that start with the prefix curPrefix
2977 // need to assign a composite location
2979 // first, check if there are more than one the set of locations that has
2980 // the same length of the longest reachable prefix, no way to assign
2981 // a composite location to the input local var
2982 // prefixSanityCheck(prefixList, i, flowGraph, reachableNodeSet);
2984 Set<NTuple<Location>> incomingCommonPrefixSet =
2985 mapPrefixToIncomingLocTupleSet.get(curPrefix);
2987 int idx = curPrefix.size();
2988 NTuple<Location> element = incomingCommonPrefixSet.iterator().next();
2989 Descriptor desc = element.get(idx).getDescriptor();
2991 SSJavaLattice<String> lattice = getLattice(desc);
2992 LocationInfo locInfo = getLocationInfo(desc);
2994 CompositeLocation inferLocation =
2995 generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
2997 // methodInfo.getInferLocation(localVarDesc);
2998 CompositeLocation newInferLocation = new CompositeLocation();
3000 if (inferLocation.getTuple().startsWith(curPrefix)) {
3001 // the same infer location is already existed. no need to do
3003 System.out.println("NO ATTEMPT TO MAKE A COMPOSITE LOCATION curPrefix=" + curPrefix);
3005 // TODO: refactoring!
3006 if (srcNode != null) {
3007 CompositeLocation newLoc = new CompositeLocation();
3008 String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
3009 for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
3010 newLoc.addLocation(curPrefix.get(locIdx));
3012 Location newLocationElement = new Location(desc, newLocSymbol);
3013 newLoc.addLocation(newLocationElement);
3015 Descriptor srcLocalVar = srcNode.getDescTuple().get(0);
3016 methodInfo.mapDescriptorToLocation(srcLocalVar, newLoc.clone());
3017 addMapLocSymbolToInferredLocation(methodInfo.getMethodDesc(), srcLocalVar, newLoc);
3018 methodInfo.removeMaplocalVarToLocSet(srcLocalVar);
3020 // add the field/var descriptor to the set of the location symbol
3021 int lastIdx = srcNode.getLocTuple().size() - 1;
3022 Descriptor lastFlowNodeDesc = srcNode.getDescTuple().get(lastIdx);
3023 NTuple<Location> srcNodelocTuple = flowGraph.getLocationTuple(srcNode);
3024 Descriptor enclosinglastLastFlowNodeDesc = srcNodelocTuple.get(lastIdx).getDescriptor();
3026 CompositeLocation newlyInferredLocForFlowNode =
3027 generateInferredCompositeLocation(methodInfo, srcNodelocTuple);
3028 Location lastInferLocElement =
3029 newlyInferredLocForFlowNode.get(newlyInferredLocForFlowNode.getSize() - 1);
3030 Descriptor enclosingLastInferLocElement = lastInferLocElement.getDescriptor();
3032 // getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToDescSet(
3033 // lastInferLocElement.getLocIdentifier(), lastFlowNodeDesc);
3034 getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToRelatedInferLoc(
3035 lastInferLocElement.getLocIdentifier(), enclosinglastLastFlowNodeDesc,
3038 System.out.println("@@@@@@@ ASSIGN " + newLoc + " to SRC=" + srcNode);
3043 // assign a new composite location
3045 // String oldMethodLocationSymbol =
3046 // inferLocation.get(0).getLocIdentifier();
3047 String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
3048 for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
3049 newInferLocation.addLocation(curPrefix.get(locIdx));
3051 Location newLocationElement = new Location(desc, newLocSymbol);
3052 newInferLocation.addLocation(newLocationElement);
3054 // maps local variable to location types of the common prefix
3055 methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation.clone());
3057 // methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation);
3058 addMapLocSymbolToInferredLocation(methodInfo.getMethodDesc(), localVarDesc,
3060 methodInfo.removeMaplocalVarToLocSet(localVarDesc);
3062 // add the field/var descriptor to the set of the location symbol
3063 int lastIdx = flowNode.getLocTuple().size() - 1;
3064 Descriptor lastFlowNodeDesc = flowNode.getDescTuple().get(lastIdx);
3065 Descriptor enclosinglastLastFlowNodeDesc = flowNodelocTuple.get(lastIdx).getDescriptor();
3067 CompositeLocation newlyInferredLocForFlowNode =
3068 generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
3069 Location lastInferLocElement =
3070 newlyInferredLocForFlowNode.get(newlyInferredLocForFlowNode.getSize() - 1);
3071 Descriptor enclosingLastInferLocElement = lastInferLocElement.getDescriptor();
3073 // getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToDescSet(
3074 // lastInferLocElement.getLocIdentifier(), lastFlowNodeDesc);
3075 getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToRelatedInferLoc(
3076 lastInferLocElement.getLocIdentifier(), enclosinglastLastFlowNodeDesc,
3079 // clean up the previous location
3080 // Location prevInferLocElement =
3081 // inferLocation.get(inferLocation.getSize() - 1);
3082 // Descriptor prevEnclosingDesc = prevInferLocElement.getDescriptor();
3084 // SSJavaLattice<String> targetLattice;
3085 // LocationInfo targetInfo;
3086 // if (prevEnclosingDesc.equals(methodInfo.getMethodDesc())) {
3087 // targetLattice = methodLattice;
3088 // targetInfo = methodInfo;
3090 // targetLattice = getLattice(prevInferLocElement.getDescriptor());
3091 // targetInfo = getLocationInfo(prevInferLocElement.getDescriptor());
3094 // Set<Pair<Descriptor, Descriptor>> associstedDescSet =
3095 // targetInfo.getRelatedInferLocSet(prevInferLocElement.getLocIdentifier());
3097 // if (associstedDescSet.size() == 1) {
3098 // targetLattice.remove(prevInferLocElement.getLocIdentifier());
3100 // associstedDescSet.remove(lastFlowNodeDesc);
3105 System.out.println("curPrefix=" + curPrefix);
3106 System.out.println("ASSIGN NEW COMPOSITE LOCATION =" + newInferLocation + " to "
3109 String newlyInsertedLocName =
3110 newInferLocation.get(newInferLocation.getSize() - 1).getLocIdentifier();
3112 System.out.println("-- add in-flow");
3113 for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
3114 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
3115 Location loc = tuple.get(idx);
3116 String higher = loc.getLocIdentifier();
3117 addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
3120 System.out.println("-- add out flow");
3121 for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
3122 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
3123 if (tuple.size() > idx) {
3124 Location loc = tuple.get(idx);
3125 String lower = loc.getLocIdentifier();
3127 // locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
3128 addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
3141 private void addMapLocSymbolToInferredLocation(MethodDescriptor md, Descriptor localVar,
3142 CompositeLocation inferLoc) {
3144 Location locElement = inferLoc.get((inferLoc.getSize() - 1));
3145 Descriptor enclosingDesc = locElement.getDescriptor();
3146 LocationInfo locInfo = getLocationInfo(enclosingDesc);
3147 locInfo.addMapLocSymbolToRelatedInferLoc(locElement.getLocIdentifier(), md, localVar);
3150 private boolean isCompositeLocation(CompositeLocation cl) {
3151 return cl.getSize() > 1;
3154 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
3155 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
3156 Descriptor desc = (Descriptor) iterator.next();
3158 if (desc.equals(LocationInference.GLOBALDESC)) {
3160 } else if (desc instanceof VarDescriptor) {
3161 if (!((VarDescriptor) desc).getType().isPrimitive()) {
3164 } else if (desc instanceof FieldDescriptor) {
3165 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
3174 private void addRelationHigherToLower(SSJavaLattice<String> lattice, LocationInfo locInfo,
3175 String higher, String lower) throws CyclicFlowException {
3177 System.out.println("---addRelationHigherToLower " + higher + " -> " + lower
3178 + " to the lattice of " + locInfo.getDescIdentifier());
3179 // if (higher.equals(lower) && lattice.isSharedLoc(higher)) {
3182 Set<String> cycleElementSet = lattice.getPossibleCycleElements(higher, lower);
3184 boolean hasNonPrimitiveElement = false;
3185 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
3186 String cycleElementLocSymbol = (String) iterator.next();
3188 Set<Descriptor> descSet = locInfo.getDescSet(cycleElementLocSymbol);
3189 if (containsNonPrimitiveElement(descSet)) {
3190 hasNonPrimitiveElement = true;
3195 if (hasNonPrimitiveElement) {
3196 System.out.println("#Check cycle= " + lower + " < " + higher + " cycleElementSet="
3198 // if there is non-primitive element in the cycle, no way to merge cyclic
3199 // elements into the shared location
3200 throw new CyclicFlowException();
3203 if (cycleElementSet.size() > 0) {
3205 String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
3207 System.out.println("---ASSIGN NEW SHARED LOC=" + newSharedLoc + " to " + cycleElementSet);
3208 lattice.mergeIntoNewLocation(cycleElementSet, newSharedLoc);
3209 lattice.addSharedLoc(newSharedLoc);
3211 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
3212 String oldLocSymbol = (String) iterator.next();
3214 Set<Pair<Descriptor, Descriptor>> inferLocSet = locInfo.getRelatedInferLocSet(oldLocSymbol);
3215 System.out.println("---update related locations=" + inferLocSet);
3216 for (Iterator iterator2 = inferLocSet.iterator(); iterator2.hasNext();) {
3217 Pair<Descriptor, Descriptor> pair = (Pair<Descriptor, Descriptor>) iterator2.next();
3218 Descriptor enclosingDesc = pair.getFirst();
3219 Descriptor desc = pair.getSecond();
3221 CompositeLocation inferLoc;
3222 if (curMethodInfo.md.equals(enclosingDesc)) {
3223 inferLoc = curMethodInfo.getInferLocation(desc);
3225 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
3228 Location locElement = inferLoc.get(inferLoc.getSize() - 1);
3230 locElement.setLocIdentifier(newSharedLoc);
3231 locInfo.addMapLocSymbolToRelatedInferLoc(newSharedLoc, enclosingDesc, desc);
3233 if (curMethodInfo.md.equals(enclosingDesc)) {
3234 inferLoc = curMethodInfo.getInferLocation(desc);
3236 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
3238 System.out.println("---New Infer Loc=" + inferLoc);
3241 locInfo.removeRelatedInferLocSet(oldLocSymbol, newSharedLoc);
3245 lattice.addSharedLoc(newSharedLoc);
3247 } else if (!lattice.isGreaterThan(higher, lower)) {
3248 lattice.addRelationHigherToLower(higher, lower);
3252 private void replaceOldLocWithNewLoc(SSJavaLattice<String> methodLattice, String oldLocSymbol,
3253 String newLocSymbol) {
3255 if (methodLattice.containsKey(oldLocSymbol)) {
3256 methodLattice.substituteLocation(oldLocSymbol, newLocSymbol);
3261 public boolean isPrimitiveLocalVariable(FlowNode node) {
3262 VarDescriptor varDesc = (VarDescriptor) node.getDescTuple().get(0);
3263 return varDesc.getType().isPrimitive();
3266 private SSJavaLattice<String> getLattice(Descriptor d) {
3267 if (d instanceof MethodDescriptor) {
3268 return getMethodLattice((MethodDescriptor) d);
3270 return getFieldLattice((ClassDescriptor) d);
3274 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
3275 if (!md2lattice.containsKey(md)) {
3276 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
3278 return md2lattice.get(md);
3281 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
3282 md2lattice.put(md, lattice);
3285 private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
3288 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
3289 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
3291 if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
3292 && dstCurTuple.size() > (idx + 1)) {
3293 // value flow between fields: we don't need to add a binary relation
3296 Descriptor desc = srcCurTuple.get(idx);
3297 ClassDescriptor classDesc;
3300 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
3302 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
3305 extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
3309 Descriptor srcFieldDesc = srcCurTuple.get(idx);
3310 Descriptor dstFieldDesc = dstCurTuple.get(idx);
3313 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
3319 private void extractRelationFromFieldFlows(ClassDescriptor cd, FlowNode srcNode,
3320 FlowNode dstNode, int idx) throws CyclicFlowException {
3322 if (srcNode.getLocTuple().get(idx).equals(dstNode.getLocTuple().get(idx))
3323 && srcNode.getLocTuple().size() > (idx + 1) && dstNode.getLocTuple().size() > (idx + 1)) {
3324 // value flow between fields: we don't need to add a binary relation
3327 Descriptor desc = srcNode.getDescTuple().get(idx);
3328 ClassDescriptor classDesc;
3331 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
3333 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
3336 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, idx + 1);
3340 Descriptor srcFieldDesc = srcNode.getDescTuple().get(idx);
3341 Descriptor dstFieldDesc = dstNode.getDescTuple().get(idx);
3343 // add a new binary relation of dstNode < srcNode
3344 SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
3345 LocationInfo fieldInfo = getFieldLocationInfo(cd);
3347 String srcSymbol = fieldInfo.getFieldInferLocation(srcFieldDesc).getLocIdentifier();
3348 String dstSymbol = fieldInfo.getFieldInferLocation(dstFieldDesc).getLocIdentifier();
3350 addRelationHigherToLower(fieldLattice, fieldInfo, srcSymbol, dstSymbol);
3356 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
3357 if (!cd2lattice.containsKey(cd)) {
3358 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
3360 return cd2lattice.get(cd);
3363 public LinkedList<MethodDescriptor> computeMethodList() {
3365 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
3369 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
3370 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
3372 while (!toAnalyzeIsEmpty()) {
3373 ClassDescriptor cd = toAnalyzeNext();
3375 setupToAnalazeMethod(cd);
3376 temp_toanalyzeMethodList.removeAll(visited);
3378 while (!toAnalyzeMethodIsEmpty()) {
3379 MethodDescriptor md = toAnalyzeMethodNext();
3380 if ((!visited.contains(md))
3381 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
3383 // creates a mapping from a method descriptor to virtual methods
3384 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3385 if (md.isStatic()) {
3386 setPossibleCallees.add(md);
3388 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3391 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
3392 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
3394 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
3395 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
3396 if ((!ssjava.isTrustMethod(calleemd))
3397 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
3398 if (!visited.contains(calleemd)) {
3399 temp_toanalyzeMethodList.add(calleemd);
3401 reachableCallee.add(calleemd);
3402 needToAnalyzeCalleeSet.add(calleemd);
3406 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
3415 return ssjava.topologicalSort(toSort);
3419 public void constructFlowGraph() {
3421 System.out.println("");
3422 toanalyze_methodDescList = computeMethodList();
3424 LinkedList<MethodDescriptor> methodDescList =
3425 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3427 System.out.println("@@@methodDescList=" + methodDescList);
3430 while (!methodDescList.isEmpty()) {
3431 MethodDescriptor md = methodDescList.removeLast();
3432 if (state.SSJAVADEBUG) {
3433 System.out.println();
3434 System.out.println("SSJAVA: Constructing a flow graph: " + md);
3436 // creates a mapping from a parameter descriptor to its index
3437 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
3439 if (!md.isStatic()) {
3441 mapParamDescToIdx.put(md.getThis(), 0);
3444 for (int i = 0; i < md.numParameters(); i++) {
3445 Descriptor paramDesc = (Descriptor) md.getParameter(i);
3446 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
3449 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
3450 mapMethodDescriptorToFlowGraph.put(md, fg);
3452 analyzeMethodBody(md.getClassDesc(), md);
3453 propagateFlowsFromCalleesWithNoCompositeLocation(md);
3454 // assignCompositeLocation(md);
3458 _debug_printGraph();
3462 private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
3463 if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
3464 mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
3466 return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
3469 private void constructSubGlobalFlowGraph(MethodDescriptor md) {
3471 FlowGraph flowGraph = getFlowGraph(md);
3473 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(md);
3475 for (Iterator<MethodInvokeNode> iter = setMethodInvokeNode.iterator(); iter.hasNext();) {
3476 MethodInvokeNode min = iter.next();
3477 propagateFlowsFromMethodInvokeNode(md, min);
3482 private void propagateFlowsFromMethodInvokeNode(MethodDescriptor mdCaller, MethodInvokeNode min) {
3483 // the transformation for a call site propagates flows through parameters
3484 // if the method is virtual, it also grab all relations from any possible
3487 MethodDescriptor mdCallee = min.getMethod();
3488 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3489 if (mdCallee.isStatic()) {
3490 setPossibleCallees.add(mdCallee);
3492 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3493 // removes method descriptors that are not invoked by the caller
3494 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3495 setPossibleCallees.addAll(calleeSet);
3498 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3499 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3500 contributeCalleeFlows(min, mdCaller, possibleMdCallee);
3505 private void assignCompositeLocation(MethodDescriptor md) {
3507 FlowGraph flowGraph = getFlowGraph(md);
3509 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
3511 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3512 FlowNode flowNode = (FlowNode) iterator.next();
3514 // assign a composite location only to the local variable
3515 if (flowNode.getCurrentDescTuple().size() == 1) {
3517 List<NTuple<Descriptor>> prefixList = calculatePrefixList(flowGraph, flowNode);
3518 Set<FlowNode> reachSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
3520 for (int i = 0; i < prefixList.size(); i++) {
3521 NTuple<Descriptor> curPrefix = prefixList.get(i);
3522 Set<NTuple<Descriptor>> reachableCommonPrefixSet = new HashSet<NTuple<Descriptor>>();
3524 for (Iterator iterator2 = reachSet.iterator(); iterator2.hasNext();) {
3525 FlowNode reachNode = (FlowNode) iterator2.next();
3526 if (reachNode.getCurrentDescTuple().startsWith(curPrefix)) {
3527 reachableCommonPrefixSet.add(reachNode.getCurrentDescTuple());
3531 if (!reachableCommonPrefixSet.isEmpty()) {
3532 System.out.println("NEED TO ASSIGN COMP LOC TO " + flowNode + " with prefix="
3534 CompositeLocation newCompLoc = generateCompositeLocation(md, curPrefix);
3535 flowNode.setCompositeLocation(newCompLoc);
3546 private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
3548 // the transformation for a call site propagates flows through parameters
3549 // if the method is virtual, it also grab all relations from any possible
3552 Set<MethodInvokeNode> setMethodInvokeNode =
3553 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
3555 if (setMethodInvokeNode != null) {
3557 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
3558 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
3559 MethodDescriptor mdCallee = min.getMethod();
3560 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3561 if (mdCallee.isStatic()) {
3562 setPossibleCallees.add(mdCallee);
3564 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3565 // removes method descriptors that are not invoked by the caller
3566 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3567 setPossibleCallees.addAll(calleeSet);
3570 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3571 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3572 propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
3580 private void propagateFlowsFromCallees(MethodDescriptor mdCaller) {
3582 // the transformation for a call site propagates flows through parameters
3583 // if the method is virtual, it also grab all relations from any possible
3586 Set<MethodInvokeNode> setMethodInvokeNode =
3587 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
3589 if (setMethodInvokeNode != null) {
3591 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
3592 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
3593 MethodDescriptor mdCallee = min.getMethod();
3594 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3595 if (mdCallee.isStatic()) {
3596 setPossibleCallees.add(mdCallee);
3598 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3599 // removes method descriptors that are not invoked by the caller
3600 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3601 setPossibleCallees.addAll(calleeSet);
3604 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3605 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3606 propagateFlowsToCaller(min, mdCaller, possibleMdCallee);
3614 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
3615 BlockNode bn = state.getMethodBody(md);
3616 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
3617 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
3620 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
3621 NodeTupleSet implicitFlowTupleSet) {
3623 bn.getVarTable().setParent(nametable);
3624 for (int i = 0; i < bn.size(); i++) {
3625 BlockStatementNode bsn = bn.get(i);
3626 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
3631 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
3632 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
3634 switch (bsn.kind()) {
3635 case Kind.BlockExpressionNode:
3636 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
3639 case Kind.DeclarationNode:
3640 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
3643 case Kind.IfStatementNode:
3644 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
3648 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
3651 case Kind.ReturnNode:
3652 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
3655 case Kind.SubBlockNode:
3656 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
3659 case Kind.ContinueBreakNode:
3662 case Kind.SwitchStatementNode:
3663 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
3670 private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
3671 SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
3673 analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
3677 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
3678 SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
3680 NodeTupleSet condTupleNode = new NodeTupleSet();
3681 analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
3682 implicitFlowTupleSet, false);
3684 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3686 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3687 newImplicitTupleSet.addTupleSet(condTupleNode);
3689 if (newImplicitTupleSet.size() > 1) {
3690 // need to create an intermediate node for the GLB of conditional locations & implicit flows
3691 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode(md).getDescTuple();
3692 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
3693 NTuple<Descriptor> tuple = idxIter.next();
3694 addFlowGraphEdge(md, tuple, interTuple);
3696 newImplicitTupleSet.clear();
3697 newImplicitTupleSet.addTuple(interTuple);
3700 BlockNode sbn = ssn.getSwitchBody();
3701 for (int i = 0; i < sbn.size(); i++) {
3702 analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
3707 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
3708 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
3709 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
3712 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
3713 NodeTupleSet implicitFlowTupleSet) {
3715 ExpressionNode returnExp = rn.getReturnExpression();
3717 if (returnExp != null) {
3718 NodeTupleSet nodeSet = new NodeTupleSet();
3719 // if a return expression returns a literal value, nodeSet is empty
3720 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
3721 FlowGraph fg = getFlowGraph(md);
3723 // if (implicitFlowTupleSet.size() == 1
3724 // && fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate()) {
3726 // // since there is already an intermediate node for the GLB of implicit flows
3727 // // we don't need to create another intermediate node.
3728 // // just re-use the intermediate node for implicit flows.
3730 // FlowNode meetNode = fg.getFlowNode(implicitFlowTupleSet.iterator().next());
3732 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3733 // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>) iterator.next();
3734 // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
3739 NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
3741 // add tuples from return node
3742 currentFlowTupleSet.addTupleSet(nodeSet);
3744 // add tuples corresponding to the current implicit flows
3745 currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
3747 if (currentFlowTupleSet.size() > 1) {
3748 FlowNode meetNode = fg.createIntermediateNode(md);
3749 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
3750 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
3751 fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
3753 fg.addReturnFlowNode(meetNode.getDescTuple());
3754 } else if (currentFlowTupleSet.size() == 1) {
3755 NTuple<Descriptor> tuple = currentFlowTupleSet.iterator().next();
3756 fg.addReturnFlowNode(tuple);
3763 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
3764 NodeTupleSet implicitFlowTupleSet) {
3766 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
3768 NodeTupleSet condTupleNode = new NodeTupleSet();
3769 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
3770 implicitFlowTupleSet, false);
3772 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3774 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3775 newImplicitTupleSet.addTupleSet(condTupleNode);
3777 if (newImplicitTupleSet.size() > 1) {
3778 // need to create an intermediate node for the GLB of conditional locations & implicit flows
3779 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode(md).getDescTuple();
3780 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
3782 NTuple<Descriptor> tuple = idxIter.next();
3783 addFlowGraphEdge(md, tuple, interTuple);
3785 newImplicitTupleSet.clear();
3786 newImplicitTupleSet.addTuple(interTuple);
3791 // System.out.println("condTupleNode="+condTupleNode);
3792 // NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3794 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
3795 // NTuple<Descriptor> tuple = idxIter.next();
3796 // addFlowGraphEdge(md, tuple, interTuple);
3799 // for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
3801 // NTuple<Descriptor> tuple = idxIter.next();
3802 // addFlowGraphEdge(md, tuple, interTuple);
3805 // NodeTupleSet newImplicitSet = new NodeTupleSet();
3806 // newImplicitSet.addTuple(interTuple);
3807 analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
3810 // condTupleNode.addTupleSet(implicitFlowTupleSet);
3812 // add edges from condNodeTupleSet to all nodes of conditional nodes
3813 // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
3816 // check 'for loop' case
3817 BlockNode bn = ln.getInitializer();
3818 bn.getVarTable().setParent(nametable);
3819 for (int i = 0; i < bn.size(); i++) {
3820 BlockStatementNode bsn = bn.get(i);
3821 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
3824 NodeTupleSet condTupleNode = new NodeTupleSet();
3825 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
3826 implicitFlowTupleSet, false);
3829 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode(md).getDescTuple();
3831 for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
3832 NTuple<Descriptor> tuple = idxIter.next();
3833 addFlowGraphEdge(md, tuple, interTuple);
3836 for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
3838 NTuple<Descriptor> tuple = idxIter.next();
3839 addFlowGraphEdge(md, tuple, interTuple);
3842 NodeTupleSet newImplicitSet = new NodeTupleSet();
3843 newImplicitSet.addTuple(interTuple);
3844 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitSet);
3845 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitSet);
3848 // condTupleNode.addTupleSet(implicitFlowTupleSet);
3850 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
3852 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
3859 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
3860 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
3862 System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
3864 NodeTupleSet condTupleNode = new NodeTupleSet();
3865 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
3866 implicitFlowTupleSet, false);
3868 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3870 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3871 newImplicitTupleSet.addTupleSet(condTupleNode);
3873 System.out.println("condTupleNode=" + condTupleNode);
3874 System.out.println("implicitFlowTupleSet=" + implicitFlowTupleSet);
3875 System.out.println("newImplicitTupleSet=" + newImplicitTupleSet);
3877 if (newImplicitTupleSet.size() > 1) {
3879 // need to create an intermediate node for the GLB of conditional locations & implicit flows
3880 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode(md).getDescTuple();
3881 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
3882 NTuple<Descriptor> tuple = idxIter.next();
3883 addFlowGraphEdge(md, tuple, interTuple);
3885 newImplicitTupleSet.clear();
3886 newImplicitTupleSet.addTuple(interTuple);
3889 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
3891 if (isn.getFalseBlock() != null) {
3892 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
3897 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
3898 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
3900 VarDescriptor vd = dn.getVarDescriptor();
3901 mapDescToDefinitionLine.put(vd, dn.getNumLine());
3902 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
3904 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
3905 fn.setDeclarationNode();
3907 if (dn.getExpression() != null) {
3909 NodeTupleSet nodeSetRHS = new NodeTupleSet();
3910 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
3911 implicitFlowTupleSet, false);
3913 // creates edges from RHS to LHS
3914 NTuple<Descriptor> interTuple = null;
3915 if (nodeSetRHS.size() > 1) {
3916 interTuple = getFlowGraph(md).createIntermediateNode(md).getDescTuple();
3919 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
3920 NTuple<Descriptor> fromTuple = iter.next();
3921 addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
3924 // creates edges from implicitFlowTupleSet to LHS
3925 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
3926 NTuple<Descriptor> implicitTuple = iter.next();
3927 addFlowGraphEdge(md, implicitTuple, tupleLHS);
3934 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
3935 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
3936 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
3940 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
3941 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
3942 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
3945 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
3946 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
3947 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
3949 // note that expression node can create more than one flow node
3950 // nodeSet contains of flow nodes
3951 // base is always assigned to null except the case of a name node!
3953 NTuple<Descriptor> flowTuple;
3955 switch (en.kind()) {
3957 case Kind.AssignmentNode:
3958 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
3959 implicitFlowTupleSet);
3962 case Kind.FieldAccessNode:
3964 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
3965 implicitFlowTupleSet, isLHS);
3966 if (flowTuple != null) {
3967 nodeSet.addTuple(flowTuple);
3972 NodeTupleSet nameNodeSet = new NodeTupleSet();
3974 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
3975 if (flowTuple != null) {
3976 nodeSet.addTuple(flowTuple);
3981 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
3984 case Kind.CreateObjectNode:
3985 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
3988 case Kind.ArrayAccessNode:
3989 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
3992 case Kind.LiteralNode:
3993 analyzeLiteralNode(md, nametable, (LiteralNode) en);
3996 case Kind.MethodInvokeNode:
3997 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
3998 implicitFlowTupleSet);
4001 case Kind.TertiaryNode:
4002 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
4006 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
4008 // case Kind.InstanceOfNode:
4009 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
4012 // case Kind.ArrayInitializerNode:
4013 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
4017 // case Kind.ClassTypeNode:
4018 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
4021 // case Kind.OffsetNode:
4022 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
4030 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
4031 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
4033 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
4034 implicitFlowTupleSet, false);
4038 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
4039 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4041 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
4042 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
4043 implicitFlowTupleSet, false);
4045 // add edges from tertiaryTupleNode to all nodes of conditional nodes
4046 tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
4047 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
4048 implicitFlowTupleSet, false);
4050 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
4051 implicitFlowTupleSet, false);
4053 nodeSet.addTupleSet(tertiaryTupleNode);
4057 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
4058 MethodInvokeNode min) {
4059 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
4061 set = new HashSet<MethodInvokeNode>();
4062 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
4067 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
4069 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4070 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4072 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
4075 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
4076 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
4079 private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
4080 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4082 if (nodeSet == null) {
4083 nodeSet = new NodeTupleSet();
4086 MethodDescriptor calleeMethodDesc = min.getMethod();
4088 NameDescriptor baseName = min.getBaseName();
4089 boolean isSystemout = false;
4090 if (baseName != null) {
4091 isSystemout = baseName.getSymbol().equals("System.out");
4094 if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
4095 && !ssjava.isTrustMethod(calleeMethodDesc) && !isSystemout) {
4097 addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
4099 FlowGraph calleeFlowGraph = getFlowGraph(calleeMethodDesc);
4100 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
4102 System.out.println("#calleeReturnSet=" + calleeReturnSet);
4104 if (min.getExpression() != null) {
4106 NodeTupleSet baseNodeSet = new NodeTupleSet();
4107 analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
4108 implicitFlowTupleSet, false);
4110 assert (baseNodeSet.size() == 1);
4111 NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
4112 mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
4114 if (!min.getMethod().isStatic()) {
4115 addArgIdxMap(min, 0, baseTuple);
4117 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
4118 FlowNode returnNode = (FlowNode) iterator.next();
4119 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
4120 if (returnDescTuple.startsWith(calleeMethodDesc.getThis())) {
4121 // the location type of the return value is started with 'this'
4123 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
4124 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
4125 nodeSet.addTuple(inFlowTuple);
4128 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
4129 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
4130 FlowNode inFlowNode = (FlowNode) iterator2.next();
4131 if (inFlowNode.getDescTuple().startsWith(calleeMethodDesc.getThis())) {
4132 nodeSet.addTupleSet(baseNodeSet);
4141 // analyze parameter flows
4143 if (min.numArgs() > 0) {
4146 if (min.getMethod().isStatic()) {
4152 for (int i = 0; i < min.numArgs(); i++) {
4153 ExpressionNode en = min.getArg(i);
4154 int idx = i + offset;
4155 NodeTupleSet argTupleSet = new NodeTupleSet();
4156 analyzeFlowExpressionNode(md, nametable, en, argTupleSet, false);
4157 // if argument is liternal node, argTuple is set to NULL.
4159 NTuple<Descriptor> argTuple = new NTuple<Descriptor>();
4160 if (argTupleSet.size() > 1) {
4161 NTuple<Descriptor> interTuple =
4162 getFlowGraph(md).createIntermediateNode(md).getDescTuple();
4163 for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
4164 NTuple<Descriptor> tuple = idxIter.next();
4165 addFlowGraphEdge(md, tuple, interTuple);
4167 argTuple = interTuple;
4169 argTuple = argTupleSet.iterator().next();
4172 addArgIdxMap(min, idx, argTuple);
4173 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
4174 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
4175 || calleeMethodDesc.getModifiers().isNative()) {
4176 addParamNodeFlowingToReturnValue(calleeMethodDesc, paramNode);
4177 nodeSet.addTupleSet(argTupleSet);
4183 // propagateFlowsFromCallee(min, md, min.getMethod());
4189 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
4190 // return true if inNode has in-flows to nodeSet
4191 Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
4192 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
4193 FlowNode fn = (FlowNode) iterator.next();
4194 if (nodeSet.contains(fn)) {
4201 private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
4202 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
4205 private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
4209 Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
4210 if (mapIdxToTuple == null) {
4211 mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
4212 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
4214 mapIdxToTuple.put(new Integer(idx), argTuple);
4217 private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
4221 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
4222 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
4224 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
4225 NTuple<Descriptor> base =
4226 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
4228 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
4229 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
4232 // need to create an edge from idx to array
4233 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
4234 NTuple<Descriptor> idxTuple = idxIter.next();
4235 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
4236 NTuple<Descriptor> arrTuple = arrIter.next();
4237 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
4241 nodeSet.addTupleSet(expNodeTupleSet);
4243 nodeSet.addTupleSet(expNodeTupleSet);
4244 nodeSet.addTupleSet(idxNodeTupleSet);
4248 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
4249 CreateObjectNode en) {
4250 // TODO Auto-generated method stub
4254 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
4255 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4257 NodeTupleSet leftOpSet = new NodeTupleSet();
4258 NodeTupleSet rightOpSet = new NodeTupleSet();
4261 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
4264 if (on.getRight() != null) {
4266 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
4267 implicitFlowTupleSet, false);
4270 Operation op = on.getOp();
4272 switch (op.getOp()) {
4274 case Operation.UNARYPLUS:
4275 case Operation.UNARYMINUS:
4276 case Operation.LOGIC_NOT:
4278 nodeSet.addTupleSet(leftOpSet);
4281 case Operation.LOGIC_OR:
4282 case Operation.LOGIC_AND:
4283 case Operation.COMP:
4284 case Operation.BIT_OR:
4285 case Operation.BIT_XOR:
4286 case Operation.BIT_AND:
4287 case Operation.ISAVAILABLE:
4288 case Operation.EQUAL:
4289 case Operation.NOTEQUAL:
4296 case Operation.MULT:
4299 case Operation.LEFTSHIFT:
4300 case Operation.RIGHTSHIFT:
4301 case Operation.URIGHTSHIFT:
4303 // there are two operands
4304 nodeSet.addTupleSet(leftOpSet);
4305 nodeSet.addTupleSet(rightOpSet);
4309 throw new Error(op.toString());
4314 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
4315 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
4317 // System.out.println("analyzeFlowNameNode=" + nn.printNode(0));
4320 base = new NTuple<Descriptor>();
4323 NameDescriptor nd = nn.getName();
4325 if (nd.getBase() != null) {
4327 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
4328 implicitFlowTupleSet, false);
4330 // base node has the top location
4334 String varname = nd.toString();
4335 if (varname.equals("this")) {
4337 base.add(md.getThis());
4341 Descriptor d = (Descriptor) nametable.get(varname);
4343 if (d instanceof VarDescriptor) {
4344 VarDescriptor vd = (VarDescriptor) d;
4346 } else if (d instanceof FieldDescriptor) {
4347 // the type of field descriptor has a location!
4348 FieldDescriptor fd = (FieldDescriptor) d;
4349 if (fd.isStatic()) {
4351 // if it is 'static final', no need to have flow node for the TOP
4355 // if 'static', assign the default GLOBAL LOCATION to the first
4356 // element of the tuple
4357 base.add(GLOBALDESC);
4360 // the location of field access starts from this, followed by field
4362 base.add(md.getThis());
4366 } else if (d == null) {
4367 // access static field
4368 base.add(GLOBALDESC);
4369 base.add(nn.getField());
4372 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
4374 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
4375 // String globalLocId = localLattice.getGlobalLoc();
4376 // if (globalLocId == null) {
4378 // Error("Method lattice does not define global variable location at "
4379 // + generateErrorMessage(md.getClassDesc(), nn));
4381 // loc.addLocation(new Location(md, globalLocId));
4383 // Location fieldLoc = (Location) fd.getType().getExtension();
4384 // loc.addLocation(fieldLoc);
4390 getFlowGraph(md).createNewFlowNode(base);
4396 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
4397 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4398 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
4400 ExpressionNode left = fan.getExpression();
4401 TypeDescriptor ltd = left.getType();
4402 FieldDescriptor fd = fan.getField();
4404 String varName = null;
4405 if (left.kind() == Kind.NameNode) {
4406 NameDescriptor nd = ((NameNode) left).getName();
4407 varName = nd.toString();
4410 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
4411 // using a class name directly or access using this
4412 if (fd.isStatic() && fd.isFinal()) {
4417 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
4419 if (left instanceof ArrayAccessNode) {
4421 ArrayAccessNode aan = (ArrayAccessNode) left;
4422 left = aan.getExpression();
4423 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
4424 implicitFlowTupleSet, isLHS);
4426 nodeSet.addTupleSet(idxNodeTupleSet);
4429 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
4432 // in this case, field is TOP location
4436 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
4438 if (!left.getType().isPrimitive()) {
4440 if (!fd.getSymbol().equals("length")) {
4441 // array.length access, just have the location of the array
4442 flowFieldTuple.add(fd);
4443 nodeSet.removeTuple(base);
4447 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
4450 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
4451 NTuple<Descriptor> idxTuple = idxIter.next();
4452 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
4455 return flowFieldTuple;
4461 private void debug_printTreeNode(TreeNode tn) {
4463 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
4467 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
4468 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4469 NodeTupleSet implicitFlowTupleSet) {
4471 NodeTupleSet nodeSetRHS = new NodeTupleSet();
4472 NodeTupleSet nodeSetLHS = new NodeTupleSet();
4474 boolean postinc = true;
4475 if (an.getOperation().getBaseOp() == null
4476 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
4477 .getBaseOp().getOp() != Operation.POSTDEC)) {
4480 // if LHS is array access node, need to capture value flows between an array
4481 // and its index value
4482 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
4486 // analyze value flows of rhs expression
4487 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
4490 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
4491 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
4492 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
4493 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4494 // System.out.println("-");
4496 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
4497 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
4499 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
4500 NTuple<Descriptor> fromTuple = iter.next();
4501 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4502 NTuple<Descriptor> toTuple = iter2.next();
4503 addFlowGraphEdge(md, fromTuple, toTuple);
4508 // creates edges from RHS to LHS
4509 NTuple<Descriptor> interTuple = null;
4510 if (nodeSetRHS.size() > 1) {
4511 interTuple = getFlowGraph(md).createIntermediateNode(md).getDescTuple();
4514 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
4515 NTuple<Descriptor> fromTuple = iter.next();
4516 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4517 NTuple<Descriptor> toTuple = iter2.next();
4518 addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
4522 // creates edges from implicitFlowTupleSet to LHS
4523 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4524 NTuple<Descriptor> fromTuple = iter.next();
4525 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4526 NTuple<Descriptor> toTuple = iter2.next();
4527 addFlowGraphEdge(md, fromTuple, toTuple);
4534 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4535 NTuple<Descriptor> tuple = iter2.next();
4536 addFlowGraphEdge(md, tuple, tuple);
4539 // creates edges from implicitFlowTupleSet to LHS
4540 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4541 NTuple<Descriptor> fromTuple = iter.next();
4542 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4543 NTuple<Descriptor> toTuple = iter2.next();
4544 addFlowGraphEdge(md, fromTuple, toTuple);
4550 if (nodeSet != null) {
4551 nodeSet.addTupleSet(nodeSetLHS);
4555 public FlowGraph getFlowGraph(MethodDescriptor md) {
4556 return mapMethodDescriptorToFlowGraph.get(md);
4559 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
4560 NTuple<Descriptor> to) {
4561 FlowGraph graph = getFlowGraph(md);
4562 graph.addValueFlowEdge(from, to);
4566 private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
4567 NTuple<Descriptor> inter, NTuple<Descriptor> to) {
4569 FlowGraph graph = getFlowGraph(md);
4571 if (inter != null) {
4572 graph.addValueFlowEdge(from, inter);
4573 graph.addValueFlowEdge(inter, to);
4575 graph.addValueFlowEdge(from, to);
4580 public void writeInferredLatticeDotFile(ClassDescriptor cd, HierarchyGraph simpleHierarchyGraph,
4581 SSJavaLattice<String> locOrder, String nameSuffix) {
4582 writeInferredLatticeDotFile(cd, null, simpleHierarchyGraph, locOrder, nameSuffix);
4585 public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
4586 HierarchyGraph simpleHierarchyGraph, SSJavaLattice<String> locOrder, String nameSuffix) {
4588 String fileName = "lattice_";
4591 cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
4593 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
4596 fileName += nameSuffix;
4598 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
4600 Set<String> addedLocSet = new HashSet<String>();
4602 if (pairSet.size() > 0) {
4604 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
4606 bw.write("digraph " + fileName + " {\n");
4608 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
4609 // pair is in the form of <higher, lower>
4610 Pair<String, String> pair = (Pair<String, String>) iterator.next();
4612 String highLocId = pair.getFirst();
4613 String lowLocId = pair.getSecond();
4615 if (!addedLocSet.contains(highLocId)) {
4616 addedLocSet.add(highLocId);
4617 drawNode(bw, locOrder, simpleHierarchyGraph, highLocId);
4620 if (!addedLocSet.contains(lowLocId)) {
4621 addedLocSet.add(lowLocId);
4622 drawNode(bw, locOrder, simpleHierarchyGraph, lowLocId);
4625 bw.write(highLocId + " -> " + lowLocId + ";\n");
4630 } catch (IOException e) {
4631 e.printStackTrace();
4638 private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
4640 for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
4641 HNode merged = (HNode) iterator.next();
4642 if (merged.isMergeNode()) {
4643 str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
4645 str += " " + merged.getName();
4651 private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, HierarchyGraph graph,
4652 String locName) throws IOException {
4654 HNode node = graph.getHNode(locName);
4661 if (lattice.isSharedLoc(locName)) {
4662 prettyStr = locName + "*";
4664 prettyStr = locName;
4667 if (node.isMergeNode()) {
4668 Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
4669 prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
4671 bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
4674 public void _debug_printGraph() {
4675 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
4677 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
4678 MethodDescriptor md = (MethodDescriptor) iterator.next();
4679 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
4682 } catch (IOException e) {
4683 e.printStackTrace();
4691 class CyclicFlowException extends Exception {
4695 class InterDescriptor extends Descriptor {
4697 public InterDescriptor(String name) {