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, GlobalFlowGraph> mapMethodDescriptorToSubGlobalFlowGraph;
119 private Map<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>> mapMethodInvokeNodeToMapCallerArgToCalleeArg;
121 public static final String GLOBALLOC = "GLOBALLOC";
123 public static final String TOPLOC = "TOPLOC";
125 public static final String INTERLOC = "INTERLOC";
127 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
129 public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
131 public static String newline = System.getProperty("line.separator");
133 LocationInfo curMethodInfo;
135 boolean debug = true;
137 private static int locSeed = 0;
139 public LocationInference(SSJavaAnalysis ssjava, State state) {
140 this.ssjava = ssjava;
142 this.temp_toanalyzeList = new ArrayList<ClassDescriptor>();
143 this.temp_toanalyzeMethodList = new ArrayList<MethodDescriptor>();
144 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
145 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
146 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
147 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
148 this.mapMethodDescriptorToMethodInvokeNodeSet =
149 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
150 this.mapMethodInvokeNodeToArgIdxMap =
151 new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
152 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
153 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
154 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
156 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
157 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
158 this.mapMethodDescToParamNodeFlowsToReturnValue =
159 new HashMap<MethodDescriptor, Set<FlowNode>>();
161 this.mapDescriptorToHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
162 this.mapMethodInvokeNodeToBaseTuple = new HashMap<MethodInvokeNode, NTuple<Descriptor>>();
164 this.mapDescriptorToSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
165 this.mapDescriptorToCombineSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
166 this.mapDescriptorToSimpleHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
168 this.mapDescriptorToSimpleLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
170 this.mapDescToLocationSummary = new HashMap<Descriptor, LocationSummary>();
172 this.mapMethodDescriptorToSubGlobalFlowGraph = new HashMap<MethodDescriptor, GlobalFlowGraph>();
174 this.mapMethodInvokeNodeToMapCallerArgToCalleeArg =
175 new HashMap<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>>();
179 public void setupToAnalyze() {
180 SymbolTable classtable = state.getClassSymbolTable();
181 temp_toanalyzeList.clear();
182 temp_toanalyzeList.addAll(classtable.getValueSet());
183 // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
184 // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
185 // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
190 public void setupToAnalazeMethod(ClassDescriptor cd) {
192 SymbolTable methodtable = cd.getMethodTable();
193 temp_toanalyzeMethodList.clear();
194 temp_toanalyzeMethodList.addAll(methodtable.getValueSet());
195 Collections.sort(temp_toanalyzeMethodList, new Comparator<MethodDescriptor>() {
196 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
197 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
202 public boolean toAnalyzeMethodIsEmpty() {
203 return temp_toanalyzeMethodList.isEmpty();
206 public boolean toAnalyzeIsEmpty() {
207 return temp_toanalyzeList.isEmpty();
210 public ClassDescriptor toAnalyzeNext() {
211 return temp_toanalyzeList.remove(0);
214 public MethodDescriptor toAnalyzeMethodNext() {
215 return temp_toanalyzeMethodList.remove(0);
218 public void inference() {
220 // 1) construct value flow graph
221 constructFlowGraph();
223 assignCompositeLocation();
225 // constructGlobalFlowGraph();
229 constructHierarchyGraph();
231 debug_writeHierarchyDotFiles();
233 simplifyHierarchyGraph();
235 debug_writeSimpleHierarchyDotFiles();
237 constructSkeletonHierarchyGraph();
239 debug_writeSkeletonHierarchyDotFiles();
241 insertCombinationNodes();
243 debug_writeSkeletonCombinationHierarchyDotFiles();
247 debug_writeLattices();
249 generateMethodSummary();
253 // 2) construct lattices
256 // simplifyLattices();
258 // 3) check properties
261 // calculate RETURNLOC,PCLOC
262 calculateExtraLocations();
264 debug_writeLatticeDotFile();
266 // 4) generate annotated source codes
267 generateAnnoatedCode();
271 public Map<NTuple<Descriptor>, NTuple<Descriptor>> getMapCallerArgToCalleeParam(
272 MethodInvokeNode min) {
274 if (!mapMethodInvokeNodeToMapCallerArgToCalleeArg.containsKey(min)) {
275 mapMethodInvokeNodeToMapCallerArgToCalleeArg.put(min,
276 new HashMap<NTuple<Descriptor>, NTuple<Descriptor>>());
279 return mapMethodInvokeNodeToMapCallerArgToCalleeArg.get(min);
282 public void addMapCallerArgToCalleeParam(MethodInvokeNode min, NTuple<Descriptor> callerArg,
283 NTuple<Descriptor> calleeParam) {
284 // System.out.println("min=" + min + " arg=" + callerArg + " param=" + calleeParam);
285 getMapCallerArgToCalleeParam(min).put(callerArg, calleeParam);
288 private void assignCompositeLocation() {
289 calculateGlobalValueFlowCompositeLocation();
290 translateCompositeLocationAssignmentToFlowGraph();
294 private void translateCompositeLocationAssignmentToFlowGraph() {
296 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
297 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
301 private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
303 System.out.println("\n#translateCompositeLocationAssignmentToFlowGraph=" + mdCaller);
305 // First, assign a composite location to a node in the flow graph
306 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
308 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
309 Map<Location, CompositeLocation> callerMapLocToCompLoc =
310 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
311 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
312 for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
313 Location methodLoc = (Location) iterator.next();
314 if (methodLoc.getDescriptor().equals(mdCaller)) {
315 CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
316 assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
320 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
322 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
323 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
324 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
325 // need to translate a composite location that is started with the base tuple of 'min'.
326 translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
327 calleeSet.add(min.getMethod());
330 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
331 MethodDescriptor callee = (MethodDescriptor) iterator.next();
332 translateCompositeLocationAssignmentToFlowGraph(callee);
337 public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
338 CompositeLocation inferCompLoc) {
339 Descriptor localDesc = loc.getLocDescriptor();
341 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
342 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
343 FlowNode node = (FlowNode) iterator.next();
344 if (node.getDescTuple().startsWith(localDesc)) {
345 // need to assign the inferred composite location to this node
346 CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
347 node.setCompositeLocation(newCompLoc);
348 System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
353 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
354 CompositeLocation inferCompLoc) {
356 System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
359 CompositeLocation newCompLoc = new CompositeLocation();
360 for (int i = 0; i < inferCompLoc.getSize(); i++) {
361 newCompLoc.addLocation(inferCompLoc.get(i));
364 Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
365 Descriptor enclosingDescriptor;
366 if (lastDescOfPrefix instanceof InterDescriptor) {
367 enclosingDescriptor = null;
369 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
372 for (int i = 1; i < nodeDescTuple.size(); i++) {
373 Descriptor desc = nodeDescTuple.get(i);
374 Location locElement = new Location(enclosingDescriptor, desc);
375 newCompLoc.addLocation(locElement);
377 enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
383 private void translateMapLocationToInferCompositeLocationToCalleeGraph(
384 GlobalFlowGraph callerGraph, MethodInvokeNode min) {
386 MethodDescriptor mdCallee = min.getMethod();
387 MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
388 Map<Location, CompositeLocation> callerMapLocToCompLoc =
389 callerGraph.getMapLocationToInferCompositeLocation();
391 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
392 GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
394 NTuple<Location> baseLocTuple =
395 translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
397 System.out.println("-translate caller infer composite loc to callee=" + mdCallee);
398 Set<Location> keySet = callerMapLocToCompLoc.keySet();
399 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
400 Location key = (Location) iterator.next();
401 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
403 if (!key.getDescriptor().equals(mdCaller)
404 && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
405 // need to translate to the callee side
406 // System.out.println("need to translate callerCompLoc=" + callerCompLoc +
407 // " with baseTuple="
410 CompositeLocation newCalleeCompLoc =
411 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
412 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
413 System.out.println("---callee loc=" + key + " newCalleeCompLoc=" + newCalleeCompLoc);
417 // If the location of an argument has a composite location
418 // need to assign a proper composite location to the corresponding callee parameter
419 System.out.println("-translate arg composite location to callee param:");
420 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
421 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
422 for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
423 Integer idx = (Integer) iterator.next();
425 if (idx == 0 && !min.getMethod().isStatic()) {
429 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
431 // check if an arg tuple has been already assigned to a composite location
432 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
433 Location argLocalLoc = argLocTuple.get(0);
435 // if (!isPrimitiveType(argTuple)) {
436 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
438 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
439 for (int i = 1; i < argLocTuple.size(); i++) {
440 callerCompLoc.addLocation(argLocTuple.get(i));
443 if (callerCompLoc.getTuple().startsWith(baseLocTuple)) {
445 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
446 NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
447 NTuple<Location> calleeParamLocTuple =
448 translateToLocTuple(mdCallee, calleeParamDescTuple);
450 CompositeLocation newCalleeCompLoc =
451 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
453 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
456 System.out.println("###need to assign composite location to=" + calleeParamDescTuple
457 + " with baseTuple=" + baseLocTuple);
458 System.out.println("---newCalleeCompLoc=" + newCalleeCompLoc);
467 private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
469 Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
471 if (lastDesc instanceof FieldDescriptor) {
472 return ((FieldDescriptor) lastDesc).getType().isPrimitive();
473 } else if (lastDesc instanceof VarDescriptor) {
474 return ((VarDescriptor) lastDesc).getType().isPrimitive();
480 private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
481 NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
483 CompositeLocation newCalleeCompLoc = new CompositeLocation();
485 // replace the last element of the caller compLoc with the 'this' location of the callee
486 for (int i = 0; i < baseLocTuple.size() - 1; i++) {
487 newCalleeCompLoc.addLocation(baseLocTuple.get(i));
490 Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
491 newCalleeCompLoc.addLocation(calleeThisLoc);
493 for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
494 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
497 return newCalleeCompLoc;
501 private void constructGlobalFlowGraph() {
503 System.out.println("");
504 LinkedList<MethodDescriptor> methodDescList =
505 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
507 System.out.println("@@@methodDescList=" + methodDescList);
510 while (!methodDescList.isEmpty()) {
511 MethodDescriptor md = methodDescList.removeLast();
512 if (state.SSJAVADEBUG) {
513 System.out.println();
514 System.out.println("SSJAVA: Constructing a global flow graph: " + md);
516 FlowGraph flowGraph = getFlowGraph(md);
517 FlowGraph subGlobalFlowGraph = flowGraph.clone();
519 // mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
521 // addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
524 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
525 // } catch (IOException e) {
526 // e.printStackTrace();
528 // FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
529 // mapMethodDescriptorToFlowGraph.put(md, fg);
530 // analyzeMethodBody(md.getClassDesc(), md);
535 // DEBUG: write a global flow graph
536 MethodDescriptor mdContainingSSJavaLoop = ssjava.getMethodContainingSSJavaLoop();
537 // FlowGraph globalFlowGraph = getSubGlobalFlowGraph(mdContainingSSJavaLoop);
538 // System.out.println("GLOBAL NODE SET=" + globalFlowGraph.getNodeSet());
539 // assignCompositeLocation(globalFlowGraph);
541 // globalFlowGraph.writeGraph("_GLOBAL");
542 // } catch (IOException e) {
543 // e.printStackTrace();
545 // _debug_printGraph();
549 private void calculateGlobalValueFlowCompositeLocation() {
551 System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
552 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
553 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
555 Set<NTuple<Location>> prefixSet = new HashSet<NTuple<Location>>();
557 Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
559 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
560 GlobalFlowNode node = (GlobalFlowNode) iterator.next();
561 Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
562 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
563 Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
565 // System.out.println("node=" + node + " inNodeSet=" + incomingNodeSet
566 // + " reachableNodeSet=" + reachNodeSet);
568 for (int i = 0; i < prefixList.size(); i++) {
569 NTuple<Location> curPrefix = prefixList.get(i);
570 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
572 for (Iterator iterator2 = reachNodeSet.iterator(); iterator2.hasNext();) {
573 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
574 if (reachNode.getLocTuple().startsWith(curPrefix)) {
575 reachableCommonPrefixSet.add(reachNode.getLocTuple());
579 if (!reachableCommonPrefixSet.isEmpty()) {
580 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
581 System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
582 System.out.println("- newCompLoc=" + newCompLoc);
584 Location targetLocalLoc = node.getLocTuple().get(0);
585 globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
593 // Set<GlobalFlowNode> inNodeSet = graph.getIncomingNodeSetWithPrefix(prefix);
594 // System.out.println("inNodeSet=" + inNodeSet + " from=" + node);
597 private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
598 CompositeLocation newCompLoc = compLocPrefix.clone();
599 NTuple<Location> locTuple = node.getLocTuple();
600 for (int i = 1; i < locTuple.size(); i++) {
601 newCompLoc.addLocation(locTuple.get(i));
603 node.setInferCompositeLocation(newCompLoc);
606 private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
608 System.out.println("\n##### calculatePrefixList=" + node);
610 Set<GlobalFlowNode> incomingNodeSet = graph.getIncomingNodeSet(node);
612 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
614 for (Iterator iterator = incomingNodeSet.iterator(); iterator.hasNext();) {
615 GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
616 NTuple<Location> inNodeTuple = inNode.getLocTuple();
618 for (int i = 1; i < inNodeTuple.size(); i++) {
619 NTuple<Location> prefix = inNodeTuple.subList(0, i);
620 if (!prefixList.contains(prefix)) {
621 prefixList.add(prefix);
626 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
627 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
628 int s0 = arg0.size();
629 int s1 = arg1.size();
632 } else if (s0 == s1) {
642 private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
644 MethodDescriptor md = flowGraph.getMethodDescriptor();
646 GlobalFlowGraph globalGraph = new GlobalFlowGraph(md);
648 // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
649 Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
651 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
653 FlowEdge edge = (FlowEdge) iterator.next();
654 NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
655 NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
657 // here only keep the first element(method location) of the descriptor tuple
658 NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
659 // Location srcMethodLoc = srcLocTuple.get(0);
660 // Descriptor srcVarDesc = srcMethodLoc.getLocDescriptor();
661 // // if (flowGraph.isParamDesc(srcVarDesc) && (!srcVarDesc.equals(md.getThis()))) {
662 // if (!srcVarDesc.equals(md.getThis())) {
663 // srcLocTuple = new NTuple<Location>();
664 // Location loc = new Location(md, srcVarDesc);
665 // srcLocTuple.add(loc);
668 NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
669 // Location dstMethodLoc = dstLocTuple.get(0);
670 // Descriptor dstVarDesc = dstMethodLoc.getLocDescriptor();
671 // if (!dstVarDesc.equals(md.getThis())) {
672 // dstLocTuple = new NTuple<Location>();
673 // Location loc = new Location(md, dstVarDesc);
674 // dstLocTuple.add(loc);
677 globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
684 private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
686 NTuple<Location> locTuple = new NTuple<Location>();
688 Descriptor enclosingDesc = md;
689 System.out.println("md=" + md + " descTuple=" + descTuple);
690 for (int i = 0; i < descTuple.size(); i++) {
691 Descriptor desc = descTuple.get(i);
693 Location loc = new Location(enclosingDesc, desc);
696 if (desc instanceof VarDescriptor) {
697 enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
698 } else if (desc instanceof FieldDescriptor) {
699 enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
701 // TODO: inter descriptor case
702 enclosingDesc = desc;
711 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller,
712 GlobalFlowGraph subGlobalFlowGraph) {
714 // the transformation for a call site propagates flows through parameters
715 // if the method is virtual, it also grab all relations from any possible
718 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
720 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
721 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
722 MethodDescriptor mdCallee = min.getMethod();
723 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
724 if (mdCallee.isStatic()) {
725 setPossibleCallees.add(mdCallee);
727 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
728 // removes method descriptors that are not invoked by the caller
729 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
730 setPossibleCallees.addAll(calleeSet);
733 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
734 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
735 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
742 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
743 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
745 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
746 Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
748 Set<Integer> keySet = mapIdxToArg.keySet();
749 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
750 Integer idx = (Integer) iterator.next();
751 NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
752 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
754 NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
755 NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
756 addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
759 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
760 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
761 Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
762 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
763 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
764 addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
767 // int numParam = calleeFlowGraph.getNumParameters();
768 // for (int idx = 0; idx < numParam; idx++) {
770 // FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
772 // NTuple<Location> paramLocTuple =
773 // translateToLocTuple(possibleMdCallee, paramNode.getCurrentDescTuple());
775 // GlobalFlowNode globalParamNode = calleeSubGlobalGraph.getFlowNode(paramLocTuple);
777 // NTuple<Descriptor> argTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(idx);
779 // NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
781 // System.out.println("argTupleSet=" + argLocTuple + " param=" + paramLocTuple);
782 // // here, it adds all value flows reachable from the paramNode in the callee's flow graph
784 // addValueFlowsFromCalleeParam(mdCaller, argLocTuple, baseLocTuple, possibleMdCallee,
789 // // FlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
790 // // FlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
792 // // int numParam = calleeSubGlobalGraph.getNumParameters();
793 // // for (int idx = 0; idx < numParam; idx++) {
794 // // FlowNode paramNode = calleeSubGlobalGraph.getParamFlowNode(idx);
795 // // NTuple<Descriptor> argTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(idx);
796 // // System.out.println("argTupleSet=" + argTuple + " param=" + paramNode);
797 // // // here, it adds all value flows reachable from the paramNode in the callee's flow graph
798 // // addValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, paramNode, callerSubGlobalGraph,
799 // // argTuple, baseTuple);
804 private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
805 MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
807 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
808 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
810 NTuple<Location> srcNodeLocTuple =
811 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
813 Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
815 for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
816 GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
817 NTuple<Location> dstNodeLocTuple =
818 translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
819 callerSubGlobalGraph.addValueFlowEdge(srcNodeLocTuple, dstNodeLocTuple);
824 private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
825 MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
827 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
829 NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
831 if (calleeFlowGraph.isParameter(nodeDescTuple)) {
832 int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
833 NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
834 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
836 NTuple<Location> callerLocTuple = new NTuple<Location>();
838 callerLocTuple.addAll(argLocTuple);
839 for (int i = 1; i < nodeLocTuple.size(); i++) {
840 callerLocTuple.add(nodeLocTuple.get(i));
842 return callerLocTuple;
849 private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
851 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
852 for (int i = 0; i < locTuple.size(); i++) {
853 descTuple.add(locTuple.get(i).getLocDescriptor());
859 private void addValueFlowsFromCalleeParam(MethodDescriptor mdCaller,
860 NTuple<Location> argLocTuple, NTuple<Location> baseLocTuple, MethodDescriptor mdCallee,
861 GlobalFlowNode globalParamNode) {
863 Set<GlobalFlowNode> visited = new HashSet<GlobalFlowNode>();
864 visited.add(globalParamNode);
865 recurAddValueFlowsFromCalleeParam(mdCaller, argLocTuple, baseLocTuple, mdCallee,
870 private void recurAddValueFlowsFromCalleeParam(MethodDescriptor mdCaller,
871 NTuple<Location> argLocTuple, NTuple<Location> baseLocTuple, MethodDescriptor mdCallee,
872 GlobalFlowNode calleeCurNode) {
874 // FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
875 // GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
877 // NTuple<Location> curNodeLocTuple = calleeCurNode.getLocTuple();
878 // NTuple<Descriptor> curNodeDescTuple = calleeCurNode.getDescTuple();
879 // if (calleeFlowGraph.isParameter(curNodeDescTuple)) {
880 // curNodeLocTuple = translateToCaller(argLocTuple, curNodeLocTuple);
883 // Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeCurNode);
884 // for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
885 // GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
887 // NTuple<Location> curNodeLocTuple = calleeCurNode.getLocTuple();
888 // NTuple<Descriptor> curNodeDescTuple = calleeCurNode.getDescTuple();
889 // if (calleeFlowGraph.isParameter(curNodeDescTuple)) {
890 // curNodeLocTuple = translateToCaller(argLocTuple, curNodeLocTuple);
893 // outNode.getDescTuple();
895 // if (calleeFlowGraph.is)
897 // if (calleeSubGlobalGraph.isParameter(srcDescTuple)) {
898 // // destination node is started with 'parameter'
899 // // need to translate it in terms of the caller's a node
901 // translateToCaller(min, calleeSubGlobalGraph.getParamIdx(srcDescTuple), srcDescTuple);
906 // Set<FlowEdge> edgeSet = calleeSubGlobalGraph.getOutEdgeSetStartingFrom(calleeSrcNode);
907 // for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
908 // FlowEdge flowEdge = (FlowEdge) iterator.next();
910 // NTuple<Descriptor> srcDescTuple = flowEdge.getInitTuple();
911 // NTuple<Descriptor> dstDescTuple = flowEdge.getEndTuple();
913 // FlowNode dstNode = calleeSubGlobalGraph.getFlowNode(dstDescTuple);
915 // if (calleeSubGlobalGraph.isParameter(srcDescTuple)) {
916 // // destination node is started with 'parameter'
917 // // need to translate it in terms of the caller's a node
919 // translateToCaller(min, calleeSubGlobalGraph.getParamIdx(srcDescTuple), srcDescTuple);
922 // if (calleeSubGlobalGraph.isParameter(dstDescTuple)) {
923 // // destination node is started with 'parameter'
924 // // need to translate it in terms of the caller's a node
926 // translateToCaller(min, calleeSubGlobalGraph.getParamIdx(dstDescTuple), dstDescTuple);
929 // callerSubGlobalGraph.addValueFlowEdge(srcDescTuple, dstDescTuple);
931 // if (!visited.contains(dstNode)) {
932 // visited.add(dstNode);
933 // recurAddValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, dstNode, callerSubGlobalGraph,
934 // dstDescTuple, visited, baseTuple);
941 private NTuple<Location> translateToCaller(NTuple<Location> argLocTuple,
942 NTuple<Location> curNodeLocTuple) {
944 NTuple<Location> callerLocTuple = new NTuple<Location>();
946 callerLocTuple.addAll(argLocTuple);
947 for (int i = 1; i < curNodeLocTuple.size(); i++) {
948 callerLocTuple.add(curNodeLocTuple.get(i));
951 return callerLocTuple;
954 private void recurAddValueFlowsFromCalleeParam(MethodInvokeNode min,
955 FlowGraph calleeSubGlobalGraph, FlowNode calleeSrcNode, FlowGraph callerSubGlobalGraph,
956 NTuple<Descriptor> callerSrcTuple, Set<FlowNode> visited, NTuple<Descriptor> baseTuple) {
958 MethodDescriptor mdCallee = calleeSubGlobalGraph.getMethodDescriptor();
960 // Set<FlowEdge> edgeSet = calleeSubGlobalGraph.getOutEdgeSet(calleeSrcNode);
961 Set<FlowEdge> edgeSet = calleeSubGlobalGraph.getOutEdgeSetStartingFrom(calleeSrcNode);
962 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
963 FlowEdge flowEdge = (FlowEdge) iterator.next();
965 NTuple<Descriptor> srcDescTuple = flowEdge.getInitTuple();
966 NTuple<Descriptor> dstDescTuple = flowEdge.getEndTuple();
968 FlowNode dstNode = calleeSubGlobalGraph.getFlowNode(dstDescTuple);
970 if (calleeSubGlobalGraph.isParameter(srcDescTuple)) {
971 // destination node is started with 'parameter'
972 // need to translate it in terms of the caller's a node
974 translateToCaller(min, calleeSubGlobalGraph.getParamIdx(srcDescTuple), srcDescTuple);
977 if (calleeSubGlobalGraph.isParameter(dstDescTuple)) {
978 // destination node is started with 'parameter'
979 // need to translate it in terms of the caller's a node
981 translateToCaller(min, calleeSubGlobalGraph.getParamIdx(dstDescTuple), dstDescTuple);
984 callerSubGlobalGraph.addValueFlowEdge(srcDescTuple, dstDescTuple);
986 if (!visited.contains(dstNode)) {
987 visited.add(dstNode);
988 recurAddValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, dstNode, callerSubGlobalGraph,
989 dstDescTuple, visited, baseTuple);
996 private NTuple<Descriptor> translateToCaller(MethodInvokeNode min, int paramIdx,
997 NTuple<Descriptor> srcDescTuple) {
999 NTuple<Descriptor> callerTuple = new NTuple<Descriptor>();
1001 NTuple<Descriptor> argTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
1003 for (int i = 0; i < argTuple.size(); i++) {
1004 callerTuple.add(argTuple.get(i));
1007 for (int i = 1; i < srcDescTuple.size(); i++) {
1008 callerTuple.add(srcDescTuple.get(i));
1014 private NTuple<Descriptor> traslateToCalleeParamTupleToCallerArgTuple(
1015 NTuple<Descriptor> calleeInitTuple, NTuple<Descriptor> callerSrcTuple) {
1017 NTuple<Descriptor> callerInitTuple = new NTuple<Descriptor>();
1019 for (int i = 0; i < callerSrcTuple.size(); i++) {
1020 callerInitTuple.add(callerSrcTuple.get(i));
1023 for (int i = 1; i < calleeInitTuple.size(); i++) {
1024 callerInitTuple.add(calleeInitTuple.get(i));
1027 return callerInitTuple;
1030 public LocationSummary getLocationSummary(Descriptor d) {
1031 if (!mapDescToLocationSummary.containsKey(d)) {
1032 if (d instanceof MethodDescriptor) {
1033 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
1034 } else if (d instanceof ClassDescriptor) {
1035 mapDescToLocationSummary.put(d, new FieldSummary());
1038 return mapDescToLocationSummary.get(d);
1041 private void generateMethodSummary() {
1043 Set<MethodDescriptor> keySet = md2lattice.keySet();
1044 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1045 MethodDescriptor md = (MethodDescriptor) iterator.next();
1047 System.out.println("\nSSJAVA: generate method summary: " + md);
1049 FlowGraph flowGraph = getFlowGraph(md);
1050 MethodSummary methodSummary = getMethodSummary(md);
1052 // construct a parameter mapping that maps a parameter descriptor to an inferred composite
1055 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
1056 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
1057 NTuple<Descriptor> descTuple = flowNode.getDescTuple();
1059 CompositeLocation assignedCompLoc = flowNode.getCompositeLocation();
1060 CompositeLocation inferredCompLoc;
1061 if (assignedCompLoc != null) {
1062 inferredCompLoc = translateCompositeLocation(assignedCompLoc);
1064 Descriptor locDesc = descTuple.get(0);
1065 Location loc = new Location(md, locDesc.getSymbol());
1066 loc.setLocDescriptor(locDesc);
1067 inferredCompLoc = new CompositeLocation(loc);
1069 System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc);
1070 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
1077 private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
1078 CompositeLocation newCompLoc = new CompositeLocation();
1080 // System.out.println("compLoc=" + compLoc);
1081 for (int i = 0; i < compLoc.getSize(); i++) {
1082 Location loc = compLoc.get(i);
1083 Descriptor enclosingDescriptor = loc.getDescriptor();
1084 Descriptor locDescriptor = loc.getLocDescriptor();
1086 HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
1087 // System.out.println("-hnode=" + hnode + " from=" + locDescriptor +
1088 // " enclosingDescriptor="
1089 // + enclosingDescriptor);
1090 // System.out.println("-getLocationSummary(enclosingDescriptor)="
1091 // + getLocationSummary(enclosingDescriptor));
1092 String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
1093 // System.out.println("-locName=" + locName);
1094 Location newLoc = new Location(enclosingDescriptor, locName);
1095 newLoc.setLocDescriptor(locDescriptor);
1096 newCompLoc.addLocation(newLoc);
1102 private void debug_writeLattices() {
1104 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
1105 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1106 Descriptor key = (Descriptor) iterator.next();
1107 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
1108 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
1109 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
1110 if (key instanceof ClassDescriptor) {
1111 writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
1113 } else if (key instanceof MethodDescriptor) {
1114 MethodDescriptor md = (MethodDescriptor) key;
1115 writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
1119 LocationSummary ls = getLocationSummary(key);
1120 System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
1123 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
1124 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
1125 ClassDescriptor cd = (ClassDescriptor) iterator.next();
1126 writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
1127 cd2lattice.get(cd), "");
1130 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
1131 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
1132 MethodDescriptor md = (MethodDescriptor) iterator.next();
1133 writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
1134 md2lattice.get(md), "");
1139 private void buildLattice() {
1141 BuildLattice buildLattice = new BuildLattice(this);
1143 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
1144 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1145 Descriptor desc = (Descriptor) iterator.next();
1147 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
1149 addMapDescToSimpleLattice(desc, simpleLattice);
1151 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1152 System.out.println("## insertIntermediateNodesToStraightLine:"
1153 + simpleHierarchyGraph.getName());
1154 SSJavaLattice<String> lattice =
1155 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
1156 lattice.removeRedundantEdges();
1158 if (desc instanceof ClassDescriptor) {
1160 cd2lattice.put((ClassDescriptor) desc, lattice);
1161 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
1162 } else if (desc instanceof MethodDescriptor) {
1164 md2lattice.put((MethodDescriptor) desc, lattice);
1165 MethodDescriptor md = (MethodDescriptor) desc;
1166 ClassDescriptor cd = md.getClassDesc();
1167 // ssjava.writeLatticeDotFile(cd, md, lattice);
1170 // System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
1171 // HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
1172 // HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
1173 // skeletonGraphWithCombinationNode.setName(desc + "_SC");
1175 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1176 // System.out.println("Identifying Combination Nodes:");
1177 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
1178 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
1179 // mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
1184 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
1185 mapDescriptorToSimpleLattice.put(desc, lattice);
1188 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
1189 return mapDescriptorToSimpleLattice.get(desc);
1192 private void simplifyHierarchyGraph() {
1193 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1194 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1195 Descriptor desc = (Descriptor) iterator.next();
1196 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
1197 simpleHierarchyGraph.setName(desc + "_SIMPLE");
1198 simpleHierarchyGraph.removeRedundantEdges();
1199 // simpleHierarchyGraph.simplifyHierarchyGraph();
1200 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
1204 private void insertCombinationNodes() {
1205 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
1206 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1207 Descriptor desc = (Descriptor) iterator.next();
1208 System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
1209 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
1210 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
1211 skeletonGraphWithCombinationNode.setName(desc + "_SC");
1213 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1214 System.out.println("Identifying Combination Nodes:");
1215 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
1216 skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
1217 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
1221 private void constructSkeletonHierarchyGraph() {
1222 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1223 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1224 Descriptor desc = (Descriptor) iterator.next();
1225 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
1226 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
1227 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
1228 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
1229 skeletonGraph.simplifyHierarchyGraph();
1230 // skeletonGraph.combineRedundantNodes(false);
1231 // skeletonGraph.removeRedundantEdges();
1232 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
1236 private void debug_writeHierarchyDotFiles() {
1238 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1239 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1240 Descriptor desc = (Descriptor) iterator.next();
1241 getHierarchyGraph(desc).writeGraph();
1246 private void debug_writeSimpleHierarchyDotFiles() {
1248 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1249 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1250 Descriptor desc = (Descriptor) iterator.next();
1251 getHierarchyGraph(desc).writeGraph();
1252 getSimpleHierarchyGraph(desc).writeGraph();
1257 private void debug_writeSkeletonHierarchyDotFiles() {
1259 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1260 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1261 Descriptor desc = (Descriptor) iterator.next();
1262 getSkeletonHierarchyGraph(desc).writeGraph();
1267 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
1269 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1270 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1271 Descriptor desc = (Descriptor) iterator.next();
1272 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
1277 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
1278 return mapDescriptorToSimpleHierarchyGraph.get(d);
1281 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
1282 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
1283 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
1285 return mapDescriptorToSkeletonHierarchyGraph.get(d);
1288 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
1289 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
1290 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
1292 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
1295 private void constructHierarchyGraph() {
1297 // do fixed-point analysis
1300 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
1302 // Collections.sort(descriptorListToAnalyze, new
1303 // Comparator<MethodDescriptor>() {
1304 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
1305 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
1309 // current descriptors to visit in fixed-point interprocedural analysis,
1310 // prioritized by dependency in the call graph
1311 methodDescriptorsToVisitStack.clear();
1313 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1314 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1316 while (!descriptorListToAnalyze.isEmpty()) {
1317 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1318 methodDescriptorsToVisitStack.add(md);
1321 // analyze scheduled methods until there are no more to visit
1322 while (!methodDescriptorsToVisitStack.isEmpty()) {
1323 // start to analyze leaf node
1324 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
1326 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
1327 // MethodSummary methodSummary = new MethodSummary(md);
1329 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
1330 // curMethodInfo = methodInfo;
1332 System.out.println();
1333 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
1335 constructHierarchyGraph(md, hierarchyGraph);
1337 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
1338 // MethodSummary prevMethodSummary = getMethodSummary(md);
1340 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
1342 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
1343 // mapDescToLocationSummary.put(md, methodSummary);
1345 // results for callee changed, so enqueue dependents caller for
1347 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
1348 while (depsItr.hasNext()) {
1349 MethodDescriptor methodNext = depsItr.next();
1350 if (!methodDescriptorsToVisitStack.contains(methodNext)
1351 && methodDescriptorToVistSet.contains(methodNext)) {
1352 methodDescriptorsToVisitStack.add(methodNext);
1362 private HierarchyGraph getHierarchyGraph(Descriptor d) {
1363 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
1364 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
1366 return mapDescriptorToHierarchyGraph.get(d);
1369 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
1371 // visit each node of method flow graph
1372 FlowGraph fg = getFlowGraph(md);
1373 Set<FlowNode> nodeSet = fg.getNodeSet();
1375 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
1376 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
1377 Descriptor desc = (Descriptor) iterator.next();
1378 methodGraph.getHNode(desc).setSkeleton(true);
1381 // for the method lattice, we need to look at the first element of
1382 // NTuple<Descriptor>
1383 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1384 FlowNode srcNode = (FlowNode) iterator.next();
1386 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(srcNode);
1387 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
1388 FlowEdge outEdge = (FlowEdge) iterator2.next();
1389 FlowNode dstNode = outEdge.getDst();
1391 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
1392 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
1394 if (outEdge.getInitTuple().equals(srcNodeTuple)
1395 && outEdge.getEndTuple().equals(dstNodeTuple)) {
1397 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
1398 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
1400 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
1401 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
1403 // value flows between fields
1404 Descriptor desc = srcCurTuple.get(0);
1405 ClassDescriptor classDesc;
1407 if (desc.equals(GLOBALDESC)) {
1408 classDesc = md.getClassDesc();
1410 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
1411 classDesc = varDesc.getType().getClassDesc();
1413 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
1416 // value flow between local var - local var or local var - field
1418 Descriptor srcDesc = srcCurTuple.get(0);
1419 Descriptor dstDesc = dstCurTuple.get(0);
1421 methodGraph.addEdge(srcDesc, dstDesc);
1423 if (fg.isParamDesc(srcDesc)) {
1424 methodGraph.setParamHNode(srcDesc);
1426 if (fg.isParamDesc(dstDesc)) {
1427 methodGraph.setParamHNode(dstDesc);
1438 private MethodSummary getMethodSummary(MethodDescriptor md) {
1439 if (!mapDescToLocationSummary.containsKey(md)) {
1440 mapDescToLocationSummary.put(md, new MethodSummary(md));
1442 return (MethodSummary) mapDescToLocationSummary.get(md);
1445 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
1447 String classSymbol = cd.getSymbol();
1448 int idx = classSymbol.lastIndexOf("$");
1450 classSymbol = classSymbol.substring(idx + 1);
1453 String pattern = "class " + classSymbol + " ";
1454 if (strLine.indexOf(pattern) != -1) {
1455 mapDescToDefinitionLine.put(cd, lineNum);
1459 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
1461 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
1462 MethodDescriptor md = (MethodDescriptor) iterator.next();
1463 String pattern = md.getMethodDeclaration();
1464 if (strLine.indexOf(pattern) != -1) {
1465 mapDescToDefinitionLine.put(md, lineNum);
1466 methodSet.remove(md);
1473 private void readOriginalSourceFiles() {
1475 SymbolTable classtable = state.getClassSymbolTable();
1477 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
1478 classDescSet.addAll(classtable.getValueSet());
1481 // inefficient implement. it may re-visit the same file if the file
1482 // contains more than one class definitions.
1483 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
1484 ClassDescriptor cd = (ClassDescriptor) iterator.next();
1486 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
1487 methodSet.addAll(cd.getMethodTable().getValueSet());
1489 String sourceFileName = cd.getSourceFileName();
1490 Vector<String> lineVec = new Vector<String>();
1492 mapFileNameToLineVector.put(sourceFileName, lineVec);
1494 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
1497 lineVec.add(""); // the index is started from 1.
1498 while ((strLine = in.readLine()) != null) {
1499 lineVec.add(lineNum, strLine);
1500 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
1501 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
1507 } catch (IOException e) {
1508 e.printStackTrace();
1513 private String generateLatticeDefinition(Descriptor desc) {
1515 Set<String> sharedLocSet = new HashSet<String>();
1517 SSJavaLattice<String> lattice = getLattice(desc);
1518 String rtr = "@LATTICE(\"";
1520 Map<String, Set<String>> map = lattice.getTable();
1521 Set<String> keySet = map.keySet();
1522 boolean first = true;
1523 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1524 String key = (String) iterator.next();
1525 if (!key.equals(lattice.getTopItem())) {
1526 Set<String> connectedSet = map.get(key);
1528 if (connectedSet.size() == 1) {
1529 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
1537 if (lattice.isSharedLoc(key)) {
1538 rtr += "," + key + "*";
1543 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
1544 String loc = (String) iterator2.next();
1545 if (!loc.equals(lattice.getBottomItem())) {
1552 rtr += loc + "<" + key;
1553 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
1554 rtr += "," + key + "*";
1555 sharedLocSet.add(key);
1557 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
1558 rtr += "," + loc + "*";
1559 sharedLocSet.add(loc);
1569 if (desc instanceof MethodDescriptor) {
1570 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
1572 MethodLocationInfo methodLocInfo = getMethodLocationInfo((MethodDescriptor) desc);
1574 if (returnType != null && (!returnType.isVoid())) {
1576 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodLocInfo.getReturnLoc()) + "\")";
1579 rtr += "\n@THISLOC(\"this\")";
1580 rtr += "\n@GLOBALLOC(\"GLOBALLOC\")";
1582 CompositeLocation pcLoc = methodLocInfo.getPCLoc();
1583 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
1584 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
1592 private void generateAnnoatedCode() {
1594 readOriginalSourceFiles();
1597 while (!toAnalyzeIsEmpty()) {
1598 ClassDescriptor cd = toAnalyzeNext();
1600 setupToAnalazeMethod(cd);
1602 LocationInfo locInfo = mapClassToLocationInfo.get(cd);
1603 String sourceFileName = cd.getSourceFileName();
1605 if (cd.isInterface()) {
1609 int classDefLine = mapDescToDefinitionLine.get(cd);
1610 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
1612 if (locInfo == null) {
1613 locInfo = getLocationInfo(cd);
1616 for (Iterator iter = cd.getFields(); iter.hasNext();) {
1617 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
1618 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
1619 String locIdentifier = locInfo.getFieldInferLocation(fieldDesc).getLocIdentifier();
1620 if (!getLattice(cd).getElementSet().contains(locIdentifier)) {
1621 getLattice(cd).put(locIdentifier);
1626 String fieldLatticeDefStr = generateLatticeDefinition(cd);
1627 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
1628 sourceVec.set(classDefLine, annoatedSrc);
1630 // generate annotations for field declarations
1631 LocationInfo fieldLocInfo = getLocationInfo(cd);
1632 Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
1634 for (Iterator iter = cd.getFields(); iter.hasNext();) {
1635 FieldDescriptor fd = (FieldDescriptor) iter.next();
1637 String locAnnotationStr;
1638 CompositeLocation inferLoc = inferLocMap.get(fd);
1640 if (inferLoc != null) {
1641 // infer loc is null if the corresponding field is static and final
1642 locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
1643 int fdLineNum = fd.getLineNum();
1644 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
1645 String fieldDeclaration = fd.toString();
1646 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
1647 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
1648 sourceVec.set(fdLineNum, annoatedStr);
1653 while (!toAnalyzeMethodIsEmpty()) {
1654 MethodDescriptor md = toAnalyzeMethodNext();
1656 if (!ssjava.needTobeAnnotated(md)) {
1660 SSJavaLattice<String> methodLattice = md2lattice.get(md);
1661 if (methodLattice != null) {
1663 int methodDefLine = md.getLineNum();
1665 MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
1667 Map<Descriptor, CompositeLocation> methodInferLocMap =
1668 methodLocInfo.getMapDescToInferLocation();
1669 Set<Descriptor> localVarDescSet = methodInferLocMap.keySet();
1671 Set<String> localLocElementSet = methodLattice.getElementSet();
1673 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
1674 Descriptor localVarDesc = (Descriptor) iterator.next();
1675 CompositeLocation inferLoc = methodInferLocMap.get(localVarDesc);
1677 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
1678 if (!localLocElementSet.contains(localLocIdentifier)) {
1679 methodLattice.put(localLocIdentifier);
1682 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
1684 if (!isParameter(md, localVarDesc)) {
1685 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
1686 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
1687 String orgSourceLine = sourceVec.get(varLineNum);
1689 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
1691 String annoatedStr =
1692 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
1693 + orgSourceLine.substring(idx);
1694 sourceVec.set(varLineNum, annoatedStr);
1697 String methodDefStr = sourceVec.get(methodDefLine);
1700 getParamLocation(methodDefStr,
1701 generateVarDeclaration((VarDescriptor) localVarDesc));
1705 String annoatedStr =
1706 methodDefStr.substring(0, idx) + locAnnotationStr + " "
1707 + methodDefStr.substring(idx);
1708 sourceVec.set(methodDefLine, annoatedStr);
1713 // check if the lattice has to have the location type for the this
1716 // boolean needToAddthisRef = hasThisReference(md);
1717 if (localLocElementSet.contains("this")) {
1718 methodLattice.put("this");
1721 String methodLatticeDefStr = generateLatticeDefinition(md);
1722 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
1723 sourceVec.set(methodDefLine, annoatedStr);
1733 private boolean hasThisReference(MethodDescriptor md) {
1735 FlowGraph fg = getFlowGraph(md);
1736 Set<FlowNode> nodeSet = fg.getNodeSet();
1737 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1738 FlowNode flowNode = (FlowNode) iterator.next();
1739 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
1747 private int getParamLocation(String methodStr, String paramStr) {
1749 String pattern = paramStr + ",";
1751 int idx = methodStr.indexOf(pattern);
1755 pattern = paramStr + ")";
1756 return methodStr.indexOf(pattern);
1761 private String generateVarDeclaration(VarDescriptor varDesc) {
1763 TypeDescriptor td = varDesc.getType();
1764 String rtr = td.toString();
1766 for (int i = 0; i < td.getArrayCount(); i++) {
1770 rtr += " " + varDesc.getName();
1775 private String generateLocationAnnoatation(CompositeLocation loc) {
1778 Location methodLoc = loc.get(0);
1779 rtr += methodLoc.getLocIdentifier();
1781 for (int i = 1; i < loc.getSize(); i++) {
1782 Location element = loc.get(i);
1783 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
1789 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
1790 return getFlowGraph(md).isParamDesc(localVarDesc);
1793 private String extractFileName(String fileName) {
1794 int idx = fileName.lastIndexOf("/");
1798 return fileName.substring(idx + 1);
1803 private void codeGen() {
1805 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
1806 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
1807 String orgFileName = (String) iterator.next();
1808 String outputFileName = extractFileName(orgFileName);
1810 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
1814 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
1815 BufferedWriter out = new BufferedWriter(fileWriter);
1817 for (int i = 0; i < sourceVec.size(); i++) {
1818 out.write(sourceVec.get(i));
1822 } catch (IOException e) {
1823 e.printStackTrace();
1830 private void checkLattices() {
1832 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
1834 // current descriptors to visit in fixed-point interprocedural analysis,
1836 // dependency in the call graph
1837 methodDescriptorsToVisitStack.clear();
1839 // descriptorListToAnalyze.removeFirst();
1841 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1842 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1844 while (!descriptorListToAnalyze.isEmpty()) {
1845 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1846 checkLatticesOfVirtualMethods(md);
1851 private void debug_writeLatticeDotFile() {
1852 // generate lattice dot file
1856 while (!toAnalyzeIsEmpty()) {
1857 ClassDescriptor cd = toAnalyzeNext();
1859 setupToAnalazeMethod(cd);
1861 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
1862 if (classLattice != null) {
1863 ssjava.writeLatticeDotFile(cd, null, classLattice);
1864 debug_printDescriptorToLocNameMapping(cd);
1867 while (!toAnalyzeMethodIsEmpty()) {
1868 MethodDescriptor md = toAnalyzeMethodNext();
1869 SSJavaLattice<String> methodLattice = md2lattice.get(md);
1870 if (methodLattice != null) {
1871 ssjava.writeLatticeDotFile(cd, md, methodLattice);
1872 debug_printDescriptorToLocNameMapping(md);
1879 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
1881 LocationInfo info = getLocationInfo(desc);
1882 System.out.println("## " + desc + " ##");
1883 System.out.println(info.getMapDescToInferLocation());
1884 LocationInfo locInfo = getLocationInfo(desc);
1885 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
1886 System.out.println("###################");
1890 private void inferLattices() {
1893 private void calculateExtraLocations() {
1894 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
1895 for (Iterator iterator = descriptorListToAnalyze.iterator(); iterator.hasNext();) {
1896 MethodDescriptor md = (MethodDescriptor) iterator.next();
1897 calculateExtraLocations(md);
1901 private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
1902 mapMethodDescToMethodLocationInfo.put(md, methodInfo);
1905 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
1907 if (!md.isStatic()) {
1908 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1909 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
1911 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
1912 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
1913 if (!md.equals(mdCallee)) {
1914 checkConsistency(md, mdCallee);
1922 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
1924 // check that two lattice have the same relations between parameters(+PC
1925 // LOC, GLOBAL_LOC RETURN LOC)
1927 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
1928 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
1930 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
1931 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
1933 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
1934 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
1936 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
1938 // add location types of paramters
1939 for (int idx = 0; idx < numParam; idx++) {
1940 list1.add(paramMap1.get(Integer.valueOf(idx)));
1941 list2.add(paramMap2.get(Integer.valueOf(idx)));
1944 // add program counter location
1945 list1.add(locInfo1.getPCLoc());
1946 list2.add(locInfo2.getPCLoc());
1948 if (!md1.getReturnType().isVoid()) {
1949 // add return value location
1950 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
1951 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
1956 // add global location type
1957 if (md1.isStatic()) {
1958 CompositeLocation globalLoc1 =
1959 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
1960 CompositeLocation globalLoc2 =
1961 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
1962 list1.add(globalLoc1);
1963 list2.add(globalLoc2);
1966 for (int i = 0; i < list1.size(); i++) {
1967 CompositeLocation locA1 = list1.get(i);
1968 CompositeLocation locA2 = list2.get(i);
1969 for (int k = 0; k < list1.size(); k++) {
1971 CompositeLocation locB1 = list1.get(k);
1972 CompositeLocation locB2 = list2.get(k);
1973 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
1975 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
1978 throw new Error("The method " + md1 + " is not consistent with the method " + md2
1979 + ".:: They have a different ordering relation between locations (" + locA1 + ","
1980 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
1988 private String getSymbol(int idx, FlowNode node) {
1989 Descriptor desc = node.getDescTuple().get(idx);
1990 return desc.getSymbol();
1993 private Descriptor getDescriptor(int idx, FlowNode node) {
1994 Descriptor desc = node.getDescTuple().get(idx);
1998 private void calculateExtraLocations(MethodDescriptor md) {
1999 // calcualte pcloc, returnloc,...
2001 SSJavaLattice<String> methodLattice = getMethodLattice(md);
2002 MethodLocationInfo methodInfo = getMethodLocationInfo(md);
2003 FlowGraph fg = getFlowGraph(md);
2004 Set<FlowNode> nodeSet = fg.getNodeSet();
2006 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2007 FlowNode flowNode = (FlowNode) iterator.next();
2008 if (flowNode.isDeclaratonNode()) {
2009 CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
2010 String locIdentifier = inferLoc.get(0).getLocIdentifier();
2011 if (!methodLattice.containsKey(locIdentifier)) {
2012 methodLattice.put(locIdentifier);
2018 Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
2019 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
2021 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2022 // calculate the initial program counter location
2023 // PC location is higher than location types of all parameters
2024 String pcLocSymbol = "PCLOC";
2026 Set<CompositeLocation> paramInFlowSet = new HashSet<CompositeLocation>();
2028 for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
2029 Integer paramIdx = (Integer) iterator.next();
2031 FlowNode paramFlowNode = fg.getParamFlowNode(paramIdx);
2033 if (fg.getIncomingFlowNodeSet(paramFlowNode).size() > 0) {
2034 // parameter has in-value flows
2035 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
2036 paramInFlowSet.add(inferLoc);
2040 if (paramInFlowSet.size() > 0) {
2041 CompositeLocation lowestLoc = getLowest(methodLattice, paramInFlowSet);
2042 assert (lowestLoc != null);
2043 methodInfo.setPCLoc(lowestLoc);
2048 // calculate a return location
2049 // the return location type is lower than all parameters and location
2052 if (!md.getReturnType().isVoid()) {
2053 // first, generate the set of return value location types that starts
2057 Set<CompositeLocation> inferFieldReturnLocSet = new HashSet<CompositeLocation>();
2059 Set<FlowNode> paramFlowNode = getParamNodeFlowingToReturnValue(md);
2060 Set<CompositeLocation> inferParamLocSet = new HashSet<CompositeLocation>();
2061 if (paramFlowNode != null) {
2062 for (Iterator iterator = paramFlowNode.iterator(); iterator.hasNext();) {
2063 FlowNode fn = (FlowNode) iterator.next();
2064 CompositeLocation inferLoc =
2065 generateInferredCompositeLocation(methodInfo, getFlowGraph(md).getLocationTuple(fn));
2066 inferParamLocSet.add(inferLoc);
2070 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
2072 skip: for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
2073 FlowNode returnNode = (FlowNode) iterator.next();
2074 CompositeLocation inferReturnLoc =
2075 generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
2076 if (inferReturnLoc.get(0).getLocIdentifier().equals("this")) {
2077 // if the location type of the return value matches "this" reference
2078 // then, check whether this return value is equal to/lower than all
2080 // parameters that possibly flow into the return values
2081 for (Iterator iterator2 = inferParamLocSet.iterator(); iterator2.hasNext();) {
2082 CompositeLocation paramInferLoc = (CompositeLocation) iterator2.next();
2084 if ((!paramInferLoc.equals(inferReturnLoc))
2085 && !isGreaterThan(methodLattice, paramInferLoc, inferReturnLoc)) {
2089 inferFieldReturnLocSet.add(inferReturnLoc);
2094 if (inferFieldReturnLocSet.size() > 0) {
2096 CompositeLocation returnLoc = getLowest(methodLattice, inferFieldReturnLocSet);
2097 if (returnLoc == null) {
2098 // in this case, assign <'this',bottom> to the RETURNLOC
2099 returnLoc = new CompositeLocation(new Location(md, md.getThis().getSymbol()));
2100 returnLoc.addLocation(new Location(md.getClassDesc(), getLattice(md.getClassDesc())
2103 methodInfo.setReturnLoc(returnLoc);
2106 String returnLocSymbol = "RETURNLOC";
2107 CompositeLocation returnLocInferLoc =
2108 new CompositeLocation(new Location(md, returnLocSymbol));
2109 methodInfo.setReturnLoc(returnLocInferLoc);
2111 for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
2112 Integer paramIdx = (Integer) iterator.next();
2113 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
2114 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
2115 if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
2117 // addRelationHigherToLower(methodLattice, methodInfo, paramLocLocalSymbol,
2118 // returnLocSymbol);
2122 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
2123 FlowNode returnNode = (FlowNode) iterator.next();
2124 CompositeLocation inferLoc =
2125 generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
2126 if (!isGreaterThan(methodLattice, inferLoc, returnLocInferLoc)) {
2128 // addRelation(methodLattice, methodInfo, inferLoc, returnLocInferLoc);
2137 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
2138 Set<String> higherLocSet = new HashSet<String>();
2140 Set<String> locSet = lattice.getTable().keySet();
2141 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
2142 String element = (String) iterator.next();
2143 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
2144 higherLocSet.add(element);
2147 return higherLocSet;
2150 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
2151 Set<CompositeLocation> set) {
2153 CompositeLocation lowest = set.iterator().next();
2155 if (set.size() == 1) {
2159 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
2160 CompositeLocation loc = (CompositeLocation) iterator.next();
2162 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
2163 // if there is a case where composite locations are incomparable, just
2168 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
2175 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
2176 CompositeLocation comp2) {
2178 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
2180 for (int idx = 0; idx < size; idx++) {
2181 Location loc1 = comp1.get(idx);
2182 Location loc2 = comp2.get(idx);
2184 Descriptor desc1 = loc1.getDescriptor();
2185 Descriptor desc2 = loc2.getDescriptor();
2187 if (!desc1.equals(desc2)) {
2188 throw new Error("Fail to compare " + comp1 + " and " + comp2);
2191 String symbol1 = loc1.getLocIdentifier();
2192 String symbol2 = loc2.getLocIdentifier();
2194 SSJavaLattice<String> lattice;
2196 lattice = methodLattice;
2198 lattice = getLattice(desc1);
2201 if (symbol1.equals(symbol2)) {
2203 } else if (!lattice.isComparable(symbol1, symbol2)) {
2212 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
2213 CompositeLocation comp2) {
2215 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
2217 for (int idx = 0; idx < size; idx++) {
2218 Location loc1 = comp1.get(idx);
2219 Location loc2 = comp2.get(idx);
2221 Descriptor desc1 = loc1.getDescriptor();
2222 Descriptor desc2 = loc2.getDescriptor();
2224 if (!desc1.equals(desc2)) {
2225 throw new Error("Fail to compare " + comp1 + " and " + comp2);
2228 String symbol1 = loc1.getLocIdentifier();
2229 String symbol2 = loc2.getLocIdentifier();
2231 SSJavaLattice<String> lattice;
2233 lattice = methodLattice;
2235 lattice = getLattice(desc1);
2238 if (symbol1.equals(symbol2)) {
2240 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
2251 private void contributeCalleeFlows(MethodInvokeNode min, MethodDescriptor mdCaller,
2252 MethodDescriptor mdCallee) {
2254 System.out.println("\n##contributeCalleeFlows callee=" + mdCallee + "TO caller=" + mdCaller);
2256 getSubGlobalFlowGraph(mdCallee);
2260 private GlobalFlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
2261 return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
2264 private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
2265 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
2267 System.out.println("\n##PROPAGATE callee=" + mdCallee + "TO caller=" + mdCaller);
2269 // if the parameter A reaches to the parameter B
2270 // then, add an edge the argument A -> the argument B to the caller's flow
2273 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
2274 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
2275 int numParam = calleeFlowGraph.getNumParameters();
2277 for (int i = 0; i < numParam; i++) {
2278 for (int k = 0; k < numParam; k++) {
2282 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
2283 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
2285 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
2286 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
2288 // check if the callee propagates an ordering constraints through
2291 Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
2293 if (localReachSet.contains(paramNode2)) {
2294 // need to propagate an ordering relation s.t. arg1 is higher
2297 System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
2299 .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
2301 // otherwise, flows between method/field locations...
2302 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
2303 System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
2307 System.out.println();
2311 System.out.println("##\n");
2315 private void propagateFlowsToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
2316 MethodDescriptor mdCallee) {
2318 System.out.println("\n##PROPAGATE callee=" + mdCallee + "TO caller=" + mdCaller);
2320 // if the parameter A reaches to the parameter B
2321 // then, add an edge the argument A -> the argument B to the caller's flow
2325 // also if a parameter is a composite location and is started with "this" reference,
2326 // need to make sure that the corresponding argument is higher than the translated location of
2329 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
2330 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
2331 int numParam = calleeFlowGraph.getNumParameters();
2333 for (int i = 0; i < numParam; i++) {
2334 for (int k = 0; k < numParam; k++) {
2338 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
2339 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
2341 System.out.println("param1=" + paramNode1 + " curDescTuple="
2342 + paramNode1.getCurrentDescTuple());
2343 System.out.println("param2=" + paramNode2 + " curDescTuple="
2344 + paramNode2.getCurrentDescTuple());
2346 // TODO: deprecated method
2347 // NodeTupleSet tupleSetArg1 = getNodeTupleSetByArgIdx(min, i);
2348 // NodeTupleSet tupleSetArg2 = getNodeTupleSetByArgIdx(min, k);
2349 NodeTupleSet tupleSetArg1 = null;
2350 NodeTupleSet tupleSetArg2 = null;
2352 for (Iterator<NTuple<Descriptor>> iter1 = tupleSetArg1.iterator(); iter1.hasNext();) {
2353 NTuple<Descriptor> arg1Tuple = iter1.next();
2355 for (Iterator<NTuple<Descriptor>> iter2 = tupleSetArg2.iterator(); iter2.hasNext();) {
2356 NTuple<Descriptor> arg2Tuple = iter2.next();
2358 // check if the callee propagates an ordering constraints through
2361 Set<FlowNode> localReachSet =
2362 calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
2364 if (localReachSet.contains(paramNode2)) {
2365 // need to propagate an ordering relation s.t. arg1 is higher
2369 .println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
2370 System.out.println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple="
2373 if (!min.getMethod().isStatic()) {
2374 // check if this is the case that values flow to/from the
2375 // current object reference 'this'
2377 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2378 Descriptor baseRef = baseTuple.get(baseTuple.size() - 1);
2380 System.out.println("paramNode1.getCurrentDescTuple()="
2381 + paramNode1.getCurrentDescTuple());
2382 // calculate the prefix of the argument
2384 if (arg2Tuple.size() == 1 && arg2Tuple.get(0).equals(baseRef)) {
2385 // in this case, the callee flow causes a caller flow to the object whose method
2388 if (!paramNode1.getCurrentDescTuple().startsWith(mdCallee.getThis())) {
2389 // check whether ???
2391 NTuple<Descriptor> param1Prefix =
2392 calculatePrefixForParam(callerFlowGraph, calleeFlowGraph, min, arg1Tuple,
2395 if (param1Prefix != null && param1Prefix.startsWith(mdCallee.getThis())) {
2396 // in this case, we need to create a new edge 'this.FIELD'->'this'
2397 // but we couldn't... instead we assign a new composite location started
2398 // with 'this' reference to the corresponding parameter
2400 CompositeLocation compLocForParam1 =
2401 generateCompositeLocation(mdCallee, param1Prefix);
2404 .println("set comp loc=" + compLocForParam1 + " to " + paramNode1);
2405 paramNode1.setCompositeLocation(compLocForParam1);
2407 // then, we need to make sure that the corresponding argument in the caller
2408 // is required to be higher than or equal to the translated parameter
2411 NTuple<Descriptor> translatedParamTuple =
2412 translateCompositeLocationToCaller(min, compLocForParam1);
2414 // TODO : check if the arg >= the tranlated parameter
2416 System.out.println("add a flow edge= " + arg1Tuple + "->"
2417 + translatedParamTuple);
2418 callerFlowGraph.addValueFlowEdge(arg1Tuple, translatedParamTuple);
2425 // param1 has already been assigned a composite location
2427 System.out.println("--param1 has already been assigned a composite location");
2428 CompositeLocation compLocForParam1 = paramNode1.getCompositeLocation();
2429 NTuple<Descriptor> translatedParamTuple =
2430 translateCompositeLocationToCaller(min, compLocForParam1);
2432 // TODO : check if the arg >= the tranlated parameter
2434 System.out.println("add a flow edge= " + arg1Tuple + "->"
2435 + translatedParamTuple);
2436 callerFlowGraph.addValueFlowEdge(arg1Tuple, translatedParamTuple);
2442 } else if (arg1Tuple.size() == 1 && arg1Tuple.get(0).equals(baseRef)) {
2443 // in this case, the callee flow causes a caller flow originated from the object
2445 // method is invoked.
2447 System.out.println("###FROM CASE");
2449 if (!paramNode2.getCurrentDescTuple().startsWith(mdCallee.getThis())) {
2451 NTuple<Descriptor> param2Prefix =
2452 calculatePrefixForParam(callerFlowGraph, calleeFlowGraph, min, arg2Tuple,
2455 if (param2Prefix != null && param2Prefix.startsWith(mdCallee.getThis())) {
2456 // in this case, we need to create a new edge 'this' ->
2457 // 'this.FIELD' but we couldn't... instead we assign the corresponding
2458 // parameter a new composite location started with 'this' reference
2460 CompositeLocation compLocForParam2 =
2461 generateCompositeLocation(mdCallee, param2Prefix);
2463 // System.out.println("set comp loc=" + compLocForParam2
2465 // " to " + paramNode2);
2466 paramNode1.setCompositeLocation(compLocForParam2);
2474 // otherwise, flows between method/field locations...
2475 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
2476 System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
2483 System.out.println();
2487 System.out.println("##\n");
2490 private NTuple<Descriptor> translateCompositeLocationToCaller(MethodInvokeNode min,
2491 CompositeLocation compLocForParam1) {
2492 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2494 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
2496 for (int i = 0; i < baseTuple.size(); i++) {
2497 tuple.add(baseTuple.get(i));
2500 for (int i = 1; i < compLocForParam1.getSize(); i++) {
2501 Location loc = compLocForParam1.get(i);
2502 tuple.add(loc.getLocDescriptor());
2508 private CompositeLocation generateCompositeLocation(NTuple<Location> prefixLocTuple) {
2510 System.out.println("generateCompositeLocation=" + prefixLocTuple);
2512 CompositeLocation newCompLoc = new CompositeLocation();
2513 for (int i = 0; i < prefixLocTuple.size(); i++) {
2514 newCompLoc.addLocation(prefixLocTuple.get(i));
2517 Descriptor lastDescOfPrefix = prefixLocTuple.get(prefixLocTuple.size() - 1).getLocDescriptor();
2519 ClassDescriptor enclosingDescriptor;
2520 if (lastDescOfPrefix instanceof FieldDescriptor) {
2521 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
2522 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
2524 // var descriptor case
2525 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
2527 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
2529 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
2530 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
2532 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
2533 newLoc.setLocDescriptor(newLocDescriptor);
2534 newCompLoc.addLocation(newLoc);
2536 // System.out.println("--newCompLoc=" + newCompLoc);
2540 private CompositeLocation generateCompositeLocation(MethodDescriptor md,
2541 NTuple<Descriptor> paramPrefix) {
2543 System.out.println("generateCompositeLocation=" + paramPrefix);
2545 CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
2547 Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
2548 // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
2549 // + lastDescOfPrefix.getClass());
2550 ClassDescriptor enclosingDescriptor;
2551 if (lastDescOfPrefix instanceof FieldDescriptor) {
2552 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
2553 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
2555 // var descriptor case
2556 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
2558 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
2560 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
2561 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
2563 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
2564 newLoc.setLocDescriptor(newLocDescriptor);
2565 newCompLoc.addLocation(newLoc);
2567 // System.out.println("--newCompLoc=" + newCompLoc);
2571 private NTuple<Descriptor> calculatePrefixForParam(FlowGraph callerFlowGraph,
2572 FlowGraph calleeFlowGraph, MethodInvokeNode min, NTuple<Descriptor> arg1Tuple,
2573 FlowNode paramNode1) {
2575 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
2576 Descriptor baseRef = baseTuple.get(baseTuple.size() - 1);
2577 System.out.println("baseRef=" + baseRef);
2579 FlowNode flowNodeArg1 = callerFlowGraph.getFlowNode(arg1Tuple);
2580 List<NTuple<Descriptor>> callerPrefixList = calculatePrefixList(callerFlowGraph, flowNodeArg1);
2581 System.out.println("callerPrefixList=" + callerPrefixList);
2583 List<NTuple<Descriptor>> prefixList = calculatePrefixList(calleeFlowGraph, paramNode1);
2584 System.out.println("###prefixList from node=" + paramNode1 + " =" + prefixList);
2586 List<NTuple<Descriptor>> calleePrefixList =
2587 translatePrefixListToCallee(baseRef, min.getMethod(), callerPrefixList);
2589 System.out.println("calleePrefixList=" + calleePrefixList);
2591 Set<FlowNode> reachNodeSetFromParam1 = calleeFlowGraph.getReachFlowNodeSetFrom(paramNode1);
2592 System.out.println("reachNodeSetFromParam1=" + reachNodeSetFromParam1);
2594 for (int i = 0; i < calleePrefixList.size(); i++) {
2595 NTuple<Descriptor> curPrefix = calleePrefixList.get(i);
2596 Set<NTuple<Descriptor>> reachableCommonPrefixSet = new HashSet<NTuple<Descriptor>>();
2598 for (Iterator iterator2 = reachNodeSetFromParam1.iterator(); iterator2.hasNext();) {
2599 FlowNode reachNode = (FlowNode) iterator2.next();
2600 if (reachNode.getCurrentDescTuple().startsWith(curPrefix)) {
2601 reachableCommonPrefixSet.add(reachNode.getCurrentDescTuple());
2605 if (!reachableCommonPrefixSet.isEmpty()) {
2606 System.out.println("###REACHABLECOMONPREFIX=" + reachableCommonPrefixSet
2607 + " with curPreFix=" + curPrefix);
2616 private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
2617 MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
2619 List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
2621 for (int i = 0; i < callerPrefixList.size(); i++) {
2622 NTuple<Descriptor> prefix = callerPrefixList.get(i);
2623 if (prefix.startsWith(baseRef)) {
2624 NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
2625 calleePrefix.add(mdCallee.getThis());
2626 for (int k = 1; k < prefix.size(); k++) {
2627 calleePrefix.add(prefix.get(k));
2629 calleePrefixList.add(calleePrefix);
2633 return calleePrefixList;
2637 private List<NTuple<Descriptor>> calculatePrefixList(FlowGraph flowGraph, FlowNode flowNode) {
2639 System.out.println("\n##### calculatePrefixList=" + flowNode);
2641 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
2642 inNodeSet.add(flowNode);
2644 System.out.println("inNodeSet=" + inNodeSet);
2646 List<NTuple<Descriptor>> prefixList = new ArrayList<NTuple<Descriptor>>();
2648 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
2649 FlowNode inNode = (FlowNode) iterator.next();
2651 NTuple<Descriptor> inNodeTuple = inNode.getCurrentDescTuple();
2653 // CompositeLocation inNodeInferredLoc =
2654 // generateInferredCompositeLocation(methodInfo, inNodeTuple);
2655 // NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
2657 for (int i = 1; i < inNodeTuple.size(); i++) {
2658 NTuple<Descriptor> prefix = inNodeTuple.subList(0, i);
2659 if (!prefixList.contains(prefix)) {
2660 prefixList.add(prefix);
2665 Collections.sort(prefixList, new Comparator<NTuple<Descriptor>>() {
2666 public int compare(NTuple<Descriptor> arg0, NTuple<Descriptor> arg1) {
2667 int s0 = arg0.size();
2668 int s1 = arg1.size();
2671 } else if (s0 == s1) {
2683 public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
2685 CompositeLocation compLoc = new CompositeLocation();
2687 Descriptor enclosingDescriptor = md;
2689 for (int i = 0; i < tuple.size(); i++) {
2690 Descriptor curDescriptor = tuple.get(i);
2691 Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
2692 locElement.setLocDescriptor(curDescriptor);
2693 compLoc.addLocation(locElement);
2695 if (curDescriptor instanceof VarDescriptor) {
2696 enclosingDescriptor = md.getClassDesc();
2697 } else if (curDescriptor instanceof NameDescriptor) {
2698 // it is "GLOBAL LOC" case!
2699 enclosingDescriptor = GLOBALDESC;
2701 enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
2706 System.out.println("-convertToCompositeLocation from=" + tuple + " to " + compLoc);
2711 private LocationDescriptor generateNewLocationDescriptor() {
2712 return new LocationDescriptor("Loc" + (locSeed++));
2715 private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
2717 // return the index where the prefix shared by tuple1 and tuple2 is ended
2718 // if there is no prefix shared by both of them, return -1
2720 int minSize = tuple1.size();
2721 if (minSize > tuple2.size()) {
2722 minSize = tuple2.size();
2726 for (int i = 0; i < minSize; i++) {
2727 if (!tuple1.get(i).equals(tuple2.get(i))) {
2737 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
2738 NTuple<Location> tuple) {
2740 // first, retrieve inferred location by the local var descriptor
2741 CompositeLocation inferLoc = new CompositeLocation();
2743 CompositeLocation localVarInferLoc =
2744 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
2746 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
2748 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
2749 inferLoc.addLocation(localVarInferLoc.get(i));
2752 for (int i = 1; i < tuple.size(); i++) {
2753 Location cur = tuple.get(i);
2754 Descriptor enclosingDesc = cur.getDescriptor();
2755 Descriptor curDesc = cur.getLocDescriptor();
2757 Location inferLocElement;
2758 if (curDesc == null) {
2759 // in this case, we have a newly generated location.
2760 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
2762 String fieldLocSymbol =
2763 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
2764 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
2765 inferLocElement.setLocDescriptor(curDesc);
2768 inferLoc.addLocation(inferLocElement);
2772 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
2776 public LocationInfo getLocationInfo(Descriptor d) {
2777 if (d instanceof MethodDescriptor) {
2778 return getMethodLocationInfo((MethodDescriptor) d);
2780 return getFieldLocationInfo((ClassDescriptor) d);
2784 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
2786 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
2787 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
2790 return mapMethodDescToMethodLocationInfo.get(md);
2794 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
2796 if (!mapClassToLocationInfo.containsKey(cd)) {
2797 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
2800 return mapClassToLocationInfo.get(cd);
2804 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
2805 NTuple<Location> prefix, NTuple<Location> element) {
2807 if (!map.containsKey(prefix)) {
2808 map.put(prefix, new HashSet<NTuple<Location>>());
2810 map.get(prefix).add(element);
2813 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
2814 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
2815 Descriptor desc = (Descriptor) iterator.next();
2817 if (desc.equals(LocationInference.GLOBALDESC)) {
2819 } else if (desc instanceof VarDescriptor) {
2820 if (!((VarDescriptor) desc).getType().isPrimitive()) {
2823 } else if (desc instanceof FieldDescriptor) {
2824 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
2833 private SSJavaLattice<String> getLattice(Descriptor d) {
2834 if (d instanceof MethodDescriptor) {
2835 return getMethodLattice((MethodDescriptor) d);
2837 return getFieldLattice((ClassDescriptor) d);
2841 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
2842 if (!md2lattice.containsKey(md)) {
2843 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
2845 return md2lattice.get(md);
2848 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
2849 md2lattice.put(md, lattice);
2852 private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
2855 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
2856 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
2858 if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
2859 && dstCurTuple.size() > (idx + 1)) {
2860 // value flow between fields: we don't need to add a binary relation
2863 Descriptor desc = srcCurTuple.get(idx);
2864 ClassDescriptor classDesc;
2867 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
2869 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
2872 extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
2876 Descriptor srcFieldDesc = srcCurTuple.get(idx);
2877 Descriptor dstFieldDesc = dstCurTuple.get(idx);
2880 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
2886 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
2887 if (!cd2lattice.containsKey(cd)) {
2888 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
2890 return cd2lattice.get(cd);
2893 public LinkedList<MethodDescriptor> computeMethodList() {
2895 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
2899 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
2900 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
2902 while (!toAnalyzeIsEmpty()) {
2903 ClassDescriptor cd = toAnalyzeNext();
2905 setupToAnalazeMethod(cd);
2906 temp_toanalyzeMethodList.removeAll(visited);
2908 while (!toAnalyzeMethodIsEmpty()) {
2909 MethodDescriptor md = toAnalyzeMethodNext();
2910 if ((!visited.contains(md))
2911 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
2913 // creates a mapping from a method descriptor to virtual methods
2914 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2915 if (md.isStatic()) {
2916 setPossibleCallees.add(md);
2918 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
2921 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
2922 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
2924 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
2925 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
2926 if ((!ssjava.isTrustMethod(calleemd))
2927 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
2928 if (!visited.contains(calleemd)) {
2929 temp_toanalyzeMethodList.add(calleemd);
2931 reachableCallee.add(calleemd);
2932 needToAnalyzeCalleeSet.add(calleemd);
2936 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
2945 return ssjava.topologicalSort(toSort);
2949 public void constructFlowGraph() {
2951 System.out.println("");
2952 toanalyze_methodDescList = computeMethodList();
2954 LinkedList<MethodDescriptor> methodDescList =
2955 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
2957 System.out.println("@@@methodDescList=" + methodDescList);
2960 while (!methodDescList.isEmpty()) {
2961 MethodDescriptor md = methodDescList.removeLast();
2962 if (state.SSJAVADEBUG) {
2963 System.out.println();
2964 System.out.println("SSJAVA: Constructing a flow graph: " + md);
2966 // creates a mapping from a parameter descriptor to its index
2967 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
2969 if (!md.isStatic()) {
2971 mapParamDescToIdx.put(md.getThis(), 0);
2974 for (int i = 0; i < md.numParameters(); i++) {
2975 Descriptor paramDesc = (Descriptor) md.getParameter(i);
2976 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
2979 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
2980 mapMethodDescriptorToFlowGraph.put(md, fg);
2982 analyzeMethodBody(md.getClassDesc(), md);
2984 System.out.println("##constructSubGlobalFlowGraph");
2985 GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(fg);
2986 mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
2989 System.out.println("##addValueFlowsFromCalleeSubGlobalFlowGraph");
2990 addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
2991 subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
2993 propagateFlowsFromCalleesWithNoCompositeLocation(md);
2994 // assignCompositeLocation(md);
2998 // _debug_printGraph();
3002 private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
3003 if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
3004 mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
3006 return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
3009 private void constructSubGlobalFlowGraph(MethodDescriptor md) {
3011 FlowGraph flowGraph = getFlowGraph(md);
3013 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(md);
3015 for (Iterator<MethodInvokeNode> iter = setMethodInvokeNode.iterator(); iter.hasNext();) {
3016 MethodInvokeNode min = iter.next();
3017 propagateFlowsFromMethodInvokeNode(md, min);
3022 private void propagateFlowsFromMethodInvokeNode(MethodDescriptor mdCaller, MethodInvokeNode min) {
3023 // the transformation for a call site propagates flows through parameters
3024 // if the method is virtual, it also grab all relations from any possible
3027 MethodDescriptor mdCallee = min.getMethod();
3028 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3029 if (mdCallee.isStatic()) {
3030 setPossibleCallees.add(mdCallee);
3032 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3033 // removes method descriptors that are not invoked by the caller
3034 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3035 setPossibleCallees.addAll(calleeSet);
3038 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3039 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3040 contributeCalleeFlows(min, mdCaller, possibleMdCallee);
3045 private void assignCompositeLocation(MethodDescriptor md) {
3047 FlowGraph flowGraph = getFlowGraph(md);
3049 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
3051 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3052 FlowNode flowNode = (FlowNode) iterator.next();
3054 // assign a composite location only to the local variable
3055 if (flowNode.getCurrentDescTuple().size() == 1) {
3057 List<NTuple<Descriptor>> prefixList = calculatePrefixList(flowGraph, flowNode);
3058 Set<FlowNode> reachSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
3060 for (int i = 0; i < prefixList.size(); i++) {
3061 NTuple<Descriptor> curPrefix = prefixList.get(i);
3062 Set<NTuple<Descriptor>> reachableCommonPrefixSet = new HashSet<NTuple<Descriptor>>();
3064 for (Iterator iterator2 = reachSet.iterator(); iterator2.hasNext();) {
3065 FlowNode reachNode = (FlowNode) iterator2.next();
3066 if (reachNode.getCurrentDescTuple().startsWith(curPrefix)) {
3067 reachableCommonPrefixSet.add(reachNode.getCurrentDescTuple());
3071 if (!reachableCommonPrefixSet.isEmpty()) {
3072 System.out.println("NEED TO ASSIGN COMP LOC TO " + flowNode + " with prefix="
3074 CompositeLocation newCompLoc = generateCompositeLocation(md, curPrefix);
3075 flowNode.setCompositeLocation(newCompLoc);
3086 private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
3088 // the transformation for a call site propagates flows through parameters
3089 // if the method is virtual, it also grab all relations from any possible
3092 Set<MethodInvokeNode> setMethodInvokeNode =
3093 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
3095 if (setMethodInvokeNode != null) {
3097 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
3098 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
3099 MethodDescriptor mdCallee = min.getMethod();
3100 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3101 if (mdCallee.isStatic()) {
3102 setPossibleCallees.add(mdCallee);
3104 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3105 // removes method descriptors that are not invoked by the caller
3106 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3107 setPossibleCallees.addAll(calleeSet);
3110 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3111 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3112 propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
3120 private void propagateFlowsFromCallees(MethodDescriptor mdCaller) {
3122 // the transformation for a call site propagates flows through parameters
3123 // if the method is virtual, it also grab all relations from any possible
3126 Set<MethodInvokeNode> setMethodInvokeNode =
3127 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
3129 if (setMethodInvokeNode != null) {
3131 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
3132 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
3133 MethodDescriptor mdCallee = min.getMethod();
3134 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3135 if (mdCallee.isStatic()) {
3136 setPossibleCallees.add(mdCallee);
3138 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3139 // removes method descriptors that are not invoked by the caller
3140 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3141 setPossibleCallees.addAll(calleeSet);
3144 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3145 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3146 propagateFlowsToCaller(min, mdCaller, possibleMdCallee);
3154 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
3155 BlockNode bn = state.getMethodBody(md);
3156 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
3157 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
3160 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
3161 NodeTupleSet implicitFlowTupleSet) {
3163 bn.getVarTable().setParent(nametable);
3164 for (int i = 0; i < bn.size(); i++) {
3165 BlockStatementNode bsn = bn.get(i);
3166 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
3171 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
3172 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
3174 switch (bsn.kind()) {
3175 case Kind.BlockExpressionNode:
3176 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
3179 case Kind.DeclarationNode:
3180 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
3183 case Kind.IfStatementNode:
3184 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
3188 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
3191 case Kind.ReturnNode:
3192 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
3195 case Kind.SubBlockNode:
3196 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
3199 case Kind.ContinueBreakNode:
3202 case Kind.SwitchStatementNode:
3203 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
3210 private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
3211 SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
3213 analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
3217 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
3218 SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
3220 NodeTupleSet condTupleNode = new NodeTupleSet();
3221 analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
3222 implicitFlowTupleSet, false);
3224 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3226 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3227 newImplicitTupleSet.addTupleSet(condTupleNode);
3229 if (newImplicitTupleSet.size() > 1) {
3230 // need to create an intermediate node for the GLB of conditional locations & implicit flows
3231 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3232 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
3233 NTuple<Descriptor> tuple = idxIter.next();
3234 addFlowGraphEdge(md, tuple, interTuple);
3236 newImplicitTupleSet.clear();
3237 newImplicitTupleSet.addTuple(interTuple);
3240 BlockNode sbn = ssn.getSwitchBody();
3241 for (int i = 0; i < sbn.size(); i++) {
3242 analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
3247 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
3248 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
3249 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
3252 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
3253 NodeTupleSet implicitFlowTupleSet) {
3255 ExpressionNode returnExp = rn.getReturnExpression();
3257 if (returnExp != null) {
3258 NodeTupleSet nodeSet = new NodeTupleSet();
3259 // if a return expression returns a literal value, nodeSet is empty
3260 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
3261 FlowGraph fg = getFlowGraph(md);
3263 // if (implicitFlowTupleSet.size() == 1
3264 // && fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate()) {
3266 // // since there is already an intermediate node for the GLB of implicit flows
3267 // // we don't need to create another intermediate node.
3268 // // just re-use the intermediate node for implicit flows.
3270 // FlowNode meetNode = fg.getFlowNode(implicitFlowTupleSet.iterator().next());
3272 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3273 // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>) iterator.next();
3274 // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
3279 NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
3281 // add tuples from return node
3282 currentFlowTupleSet.addTupleSet(nodeSet);
3284 // add tuples corresponding to the current implicit flows
3285 currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
3287 if (currentFlowTupleSet.size() > 1) {
3288 FlowNode meetNode = fg.createIntermediateNode();
3289 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
3290 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
3291 fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
3293 fg.addReturnFlowNode(meetNode.getDescTuple());
3294 } else if (currentFlowTupleSet.size() == 1) {
3295 NTuple<Descriptor> tuple = currentFlowTupleSet.iterator().next();
3296 fg.addReturnFlowNode(tuple);
3303 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
3304 NodeTupleSet implicitFlowTupleSet) {
3306 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
3308 NodeTupleSet condTupleNode = new NodeTupleSet();
3309 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
3310 implicitFlowTupleSet, false);
3312 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3314 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3315 newImplicitTupleSet.addTupleSet(condTupleNode);
3317 if (newImplicitTupleSet.size() > 1) {
3318 // need to create an intermediate node for the GLB of conditional locations & implicit flows
3319 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3320 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
3322 NTuple<Descriptor> tuple = idxIter.next();
3323 addFlowGraphEdge(md, tuple, interTuple);
3325 newImplicitTupleSet.clear();
3326 newImplicitTupleSet.addTuple(interTuple);
3331 // System.out.println("condTupleNode="+condTupleNode);
3332 // NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3334 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
3335 // NTuple<Descriptor> tuple = idxIter.next();
3336 // addFlowGraphEdge(md, tuple, interTuple);
3339 // for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
3341 // NTuple<Descriptor> tuple = idxIter.next();
3342 // addFlowGraphEdge(md, tuple, interTuple);
3345 // NodeTupleSet newImplicitSet = new NodeTupleSet();
3346 // newImplicitSet.addTuple(interTuple);
3347 analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
3350 // condTupleNode.addTupleSet(implicitFlowTupleSet);
3352 // add edges from condNodeTupleSet to all nodes of conditional nodes
3353 // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
3356 // check 'for loop' case
3357 BlockNode bn = ln.getInitializer();
3358 bn.getVarTable().setParent(nametable);
3359 for (int i = 0; i < bn.size(); i++) {
3360 BlockStatementNode bsn = bn.get(i);
3361 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
3364 NodeTupleSet condTupleNode = new NodeTupleSet();
3365 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
3366 implicitFlowTupleSet, false);
3369 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3371 for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
3372 NTuple<Descriptor> tuple = idxIter.next();
3373 addFlowGraphEdge(md, tuple, interTuple);
3376 for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
3378 NTuple<Descriptor> tuple = idxIter.next();
3379 addFlowGraphEdge(md, tuple, interTuple);
3382 NodeTupleSet newImplicitSet = new NodeTupleSet();
3383 newImplicitSet.addTuple(interTuple);
3384 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitSet);
3385 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitSet);
3388 // condTupleNode.addTupleSet(implicitFlowTupleSet);
3390 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
3392 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
3399 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
3400 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
3402 System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
3404 NodeTupleSet condTupleNode = new NodeTupleSet();
3405 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
3406 implicitFlowTupleSet, false);
3408 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3410 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3411 newImplicitTupleSet.addTupleSet(condTupleNode);
3413 System.out.println("condTupleNode=" + condTupleNode);
3414 System.out.println("implicitFlowTupleSet=" + implicitFlowTupleSet);
3415 System.out.println("newImplicitTupleSet=" + newImplicitTupleSet);
3417 if (newImplicitTupleSet.size() > 1) {
3419 // need to create an intermediate node for the GLB of conditional locations & implicit flows
3420 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3421 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
3422 NTuple<Descriptor> tuple = idxIter.next();
3423 addFlowGraphEdge(md, tuple, interTuple);
3425 newImplicitTupleSet.clear();
3426 newImplicitTupleSet.addTuple(interTuple);
3429 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
3431 if (isn.getFalseBlock() != null) {
3432 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
3437 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
3438 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
3440 VarDescriptor vd = dn.getVarDescriptor();
3441 mapDescToDefinitionLine.put(vd, dn.getNumLine());
3442 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
3444 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
3445 fn.setDeclarationNode();
3447 if (dn.getExpression() != null) {
3449 NodeTupleSet nodeSetRHS = new NodeTupleSet();
3450 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
3451 implicitFlowTupleSet, false);
3453 // creates edges from RHS to LHS
3454 NTuple<Descriptor> interTuple = null;
3455 if (nodeSetRHS.size() > 1) {
3456 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3459 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
3460 NTuple<Descriptor> fromTuple = iter.next();
3461 addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
3464 // creates edges from implicitFlowTupleSet to LHS
3465 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
3466 NTuple<Descriptor> implicitTuple = iter.next();
3467 addFlowGraphEdge(md, implicitTuple, tupleLHS);
3474 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
3475 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
3476 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
3480 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
3481 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
3482 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
3485 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
3486 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
3487 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
3489 // note that expression node can create more than one flow node
3490 // nodeSet contains of flow nodes
3491 // base is always assigned to null except the case of a name node!
3493 NTuple<Descriptor> flowTuple;
3495 switch (en.kind()) {
3497 case Kind.AssignmentNode:
3498 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
3499 implicitFlowTupleSet);
3502 case Kind.FieldAccessNode:
3504 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
3505 implicitFlowTupleSet, isLHS);
3506 if (flowTuple != null) {
3507 nodeSet.addTuple(flowTuple);
3512 NodeTupleSet nameNodeSet = new NodeTupleSet();
3514 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
3515 if (flowTuple != null) {
3516 nodeSet.addTuple(flowTuple);
3521 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
3524 case Kind.CreateObjectNode:
3525 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
3528 case Kind.ArrayAccessNode:
3529 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
3532 case Kind.LiteralNode:
3533 analyzeLiteralNode(md, nametable, (LiteralNode) en);
3536 case Kind.MethodInvokeNode:
3537 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
3538 implicitFlowTupleSet);
3541 case Kind.TertiaryNode:
3542 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
3546 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
3548 // case Kind.InstanceOfNode:
3549 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
3552 // case Kind.ArrayInitializerNode:
3553 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
3557 // case Kind.ClassTypeNode:
3558 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
3561 // case Kind.OffsetNode:
3562 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
3570 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
3571 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
3573 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
3574 implicitFlowTupleSet, false);
3578 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
3579 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
3581 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
3582 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
3583 implicitFlowTupleSet, false);
3585 // add edges from tertiaryTupleNode to all nodes of conditional nodes
3586 tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
3587 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
3588 implicitFlowTupleSet, false);
3590 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
3591 implicitFlowTupleSet, false);
3593 nodeSet.addTupleSet(tertiaryTupleNode);
3597 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
3598 MethodInvokeNode min) {
3599 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
3601 set = new HashSet<MethodInvokeNode>();
3602 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
3607 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
3609 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
3610 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
3612 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
3615 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
3616 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
3619 private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
3620 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
3622 System.out.println("analyzeFlowMethodInvokeNode=" + min.printNode(0));
3624 if (nodeSet == null) {
3625 nodeSet = new NodeTupleSet();
3628 MethodDescriptor calleeMethodDesc = min.getMethod();
3630 NameDescriptor baseName = min.getBaseName();
3631 boolean isSystemout = false;
3632 if (baseName != null) {
3633 isSystemout = baseName.getSymbol().equals("System.out");
3636 if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
3637 && !ssjava.isTrustMethod(calleeMethodDesc) && !isSystemout) {
3639 addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
3641 FlowGraph calleeFlowGraph = getFlowGraph(calleeMethodDesc);
3642 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
3644 System.out.println("#calleeReturnSet=" + calleeReturnSet);
3646 if (min.getExpression() != null) {
3648 NodeTupleSet baseNodeSet = new NodeTupleSet();
3649 analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
3650 implicitFlowTupleSet, false);
3652 assert (baseNodeSet.size() == 1);
3653 NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
3654 mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
3656 if (!min.getMethod().isStatic()) {
3657 addArgIdxMap(min, 0, baseTuple);
3659 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
3660 FlowNode returnNode = (FlowNode) iterator.next();
3661 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
3662 if (returnDescTuple.startsWith(calleeMethodDesc.getThis())) {
3663 // the location type of the return value is started with 'this'
3665 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
3666 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
3667 nodeSet.addTuple(inFlowTuple);
3670 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
3671 System.out.println("inFlowSet=" + inFlowSet + " from retrunNode=" + returnNode);
3672 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
3673 FlowNode inFlowNode = (FlowNode) iterator2.next();
3674 if (inFlowNode.getDescTuple().startsWith(calleeMethodDesc.getThis())) {
3675 nodeSet.addTupleSet(baseNodeSet);
3684 // analyze parameter flows
3686 if (min.numArgs() > 0) {
3689 if (min.getMethod().isStatic()) {
3695 for (int i = 0; i < min.numArgs(); i++) {
3696 ExpressionNode en = min.getArg(i);
3697 int idx = i + offset;
3698 NodeTupleSet argTupleSet = new NodeTupleSet();
3699 analyzeFlowExpressionNode(md, nametable, en, argTupleSet, false);
3700 // if argument is liternal node, argTuple is set to NULL
3702 NTuple<Descriptor> argTuple = new NTuple<Descriptor>();
3703 System.out.println("-argTupleSet=" + argTupleSet + " from en=" + en.printNode(0));
3704 if (argTupleSet.size() > 1) {
3705 NTuple<Descriptor> interTuple =
3706 getFlowGraph(md).createIntermediateNode().getDescTuple();
3707 for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
3708 NTuple<Descriptor> tuple = idxIter.next();
3709 addFlowGraphEdge(md, tuple, interTuple);
3711 argTuple = interTuple;
3712 } else if (argTupleSet.size() == 1) {
3713 argTuple = argTupleSet.iterator().next();
3715 argTuple = new NTuple<Descriptor>();
3718 if (argTuple.size() != 0) {
3719 addArgIdxMap(min, idx, argTuple);
3721 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
3722 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
3723 || calleeMethodDesc.getModifiers().isNative()) {
3724 addParamNodeFlowingToReturnValue(calleeMethodDesc, paramNode);
3725 nodeSet.addTupleSet(argTupleSet);
3731 // propagateFlowsFromCallee(min, md, min.getMethod());
3733 System.out.println("min nodeSet=" + nodeSet);
3738 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
3739 // return true if inNode has in-flows to nodeSet
3741 // Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
3742 Set<FlowNode> reachableSet = fg.getReachableSetFrom(inNode.getDescTuple());
3743 System.out.println("inNode=" + inNode + " reachalbeSet=" + reachableSet);
3745 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
3746 FlowNode fn = (FlowNode) iterator.next();
3747 if (nodeSet.contains(fn)) {
3754 private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
3755 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
3758 private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
3762 Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
3763 if (mapIdxToTuple == null) {
3764 mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
3765 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
3767 mapIdxToTuple.put(new Integer(idx), argTuple);
3770 private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
3774 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
3775 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
3777 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
3778 NTuple<Descriptor> base =
3779 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
3781 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
3782 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
3785 // need to create an edge from idx to array
3786 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
3787 NTuple<Descriptor> idxTuple = idxIter.next();
3788 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
3789 NTuple<Descriptor> arrTuple = arrIter.next();
3790 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
3794 nodeSet.addTupleSet(expNodeTupleSet);
3796 nodeSet.addTupleSet(expNodeTupleSet);
3797 nodeSet.addTupleSet(idxNodeTupleSet);
3801 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
3802 CreateObjectNode en) {
3803 // TODO Auto-generated method stub
3807 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
3808 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
3810 NodeTupleSet leftOpSet = new NodeTupleSet();
3811 NodeTupleSet rightOpSet = new NodeTupleSet();
3814 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
3817 if (on.getRight() != null) {
3819 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
3820 implicitFlowTupleSet, false);
3823 Operation op = on.getOp();
3825 switch (op.getOp()) {
3827 case Operation.UNARYPLUS:
3828 case Operation.UNARYMINUS:
3829 case Operation.LOGIC_NOT:
3831 nodeSet.addTupleSet(leftOpSet);
3834 case Operation.LOGIC_OR:
3835 case Operation.LOGIC_AND:
3836 case Operation.COMP:
3837 case Operation.BIT_OR:
3838 case Operation.BIT_XOR:
3839 case Operation.BIT_AND:
3840 case Operation.ISAVAILABLE:
3841 case Operation.EQUAL:
3842 case Operation.NOTEQUAL:
3849 case Operation.MULT:
3852 case Operation.LEFTSHIFT:
3853 case Operation.RIGHTSHIFT:
3854 case Operation.URIGHTSHIFT:
3856 // there are two operands
3857 nodeSet.addTupleSet(leftOpSet);
3858 nodeSet.addTupleSet(rightOpSet);
3862 throw new Error(op.toString());
3867 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
3868 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
3870 // System.out.println("analyzeFlowNameNode=" + nn.printNode(0));
3873 base = new NTuple<Descriptor>();
3876 NameDescriptor nd = nn.getName();
3878 if (nd.getBase() != null) {
3880 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
3881 implicitFlowTupleSet, false);
3883 // base node has the top location
3887 String varname = nd.toString();
3888 if (varname.equals("this")) {
3890 base.add(md.getThis());
3894 Descriptor d = (Descriptor) nametable.get(varname);
3896 if (d instanceof VarDescriptor) {
3897 VarDescriptor vd = (VarDescriptor) d;
3899 } else if (d instanceof FieldDescriptor) {
3900 // the type of field descriptor has a location!
3901 FieldDescriptor fd = (FieldDescriptor) d;
3902 if (fd.isStatic()) {
3904 // if it is 'static final', no need to have flow node for the TOP
3908 // if 'static', assign the default GLOBAL LOCATION to the first
3909 // element of the tuple
3910 base.add(GLOBALDESC);
3913 // the location of field access starts from this, followed by field
3915 base.add(md.getThis());
3919 } else if (d == null) {
3920 // access static field
3921 base.add(GLOBALDESC);
3922 base.add(nn.getField());
3925 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
3927 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
3928 // String globalLocId = localLattice.getGlobalLoc();
3929 // if (globalLocId == null) {
3931 // Error("Method lattice does not define global variable location at "
3932 // + generateErrorMessage(md.getClassDesc(), nn));
3934 // loc.addLocation(new Location(md, globalLocId));
3936 // Location fieldLoc = (Location) fd.getType().getExtension();
3937 // loc.addLocation(fieldLoc);
3943 getFlowGraph(md).createNewFlowNode(base);
3949 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
3950 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
3951 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
3953 ExpressionNode left = fan.getExpression();
3954 TypeDescriptor ltd = left.getType();
3955 FieldDescriptor fd = fan.getField();
3957 String varName = null;
3958 if (left.kind() == Kind.NameNode) {
3959 NameDescriptor nd = ((NameNode) left).getName();
3960 varName = nd.toString();
3963 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
3964 // using a class name directly or access using this
3965 if (fd.isStatic() && fd.isFinal()) {
3970 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
3972 if (left instanceof ArrayAccessNode) {
3974 ArrayAccessNode aan = (ArrayAccessNode) left;
3975 left = aan.getExpression();
3976 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
3977 implicitFlowTupleSet, isLHS);
3979 nodeSet.addTupleSet(idxNodeTupleSet);
3982 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
3985 // in this case, field is TOP location
3989 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
3991 if (!left.getType().isPrimitive()) {
3993 if (!fd.getSymbol().equals("length")) {
3994 // array.length access, just have the location of the array
3995 flowFieldTuple.add(fd);
3996 nodeSet.removeTuple(base);
4000 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
4003 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
4004 NTuple<Descriptor> idxTuple = idxIter.next();
4005 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
4008 return flowFieldTuple;
4014 private void debug_printTreeNode(TreeNode tn) {
4016 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
4020 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
4021 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4022 NodeTupleSet implicitFlowTupleSet) {
4024 NodeTupleSet nodeSetRHS = new NodeTupleSet();
4025 NodeTupleSet nodeSetLHS = new NodeTupleSet();
4027 boolean postinc = true;
4028 if (an.getOperation().getBaseOp() == null
4029 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
4030 .getBaseOp().getOp() != Operation.POSTDEC)) {
4033 // if LHS is array access node, need to capture value flows between an array
4034 // and its index value
4035 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
4039 // analyze value flows of rhs expression
4040 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
4043 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
4044 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
4045 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
4046 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4047 // System.out.println("-");
4049 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
4050 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
4052 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
4053 NTuple<Descriptor> fromTuple = iter.next();
4054 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4055 NTuple<Descriptor> toTuple = iter2.next();
4056 addFlowGraphEdge(md, fromTuple, toTuple);
4061 // creates edges from RHS to LHS
4062 NTuple<Descriptor> interTuple = null;
4063 if (nodeSetRHS.size() > 1) {
4064 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4067 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
4068 NTuple<Descriptor> fromTuple = iter.next();
4069 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4070 NTuple<Descriptor> toTuple = iter2.next();
4071 addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
4075 // creates edges from implicitFlowTupleSet to LHS
4076 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4077 NTuple<Descriptor> fromTuple = iter.next();
4078 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4079 NTuple<Descriptor> toTuple = iter2.next();
4080 addFlowGraphEdge(md, fromTuple, toTuple);
4087 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4088 NTuple<Descriptor> tuple = iter2.next();
4089 addFlowGraphEdge(md, tuple, tuple);
4092 // creates edges from implicitFlowTupleSet to LHS
4093 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4094 NTuple<Descriptor> fromTuple = iter.next();
4095 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4096 NTuple<Descriptor> toTuple = iter2.next();
4097 addFlowGraphEdge(md, fromTuple, toTuple);
4103 if (nodeSet != null) {
4104 nodeSet.addTupleSet(nodeSetLHS);
4108 public FlowGraph getFlowGraph(MethodDescriptor md) {
4109 return mapMethodDescriptorToFlowGraph.get(md);
4112 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
4113 NTuple<Descriptor> to) {
4114 FlowGraph graph = getFlowGraph(md);
4115 graph.addValueFlowEdge(from, to);
4119 private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
4120 NTuple<Descriptor> inter, NTuple<Descriptor> to) {
4122 FlowGraph graph = getFlowGraph(md);
4124 if (inter != null) {
4125 graph.addValueFlowEdge(from, inter);
4126 graph.addValueFlowEdge(inter, to);
4128 graph.addValueFlowEdge(from, to);
4133 public void writeInferredLatticeDotFile(ClassDescriptor cd, HierarchyGraph simpleHierarchyGraph,
4134 SSJavaLattice<String> locOrder, String nameSuffix) {
4135 writeInferredLatticeDotFile(cd, null, simpleHierarchyGraph, locOrder, nameSuffix);
4138 public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
4139 HierarchyGraph simpleHierarchyGraph, SSJavaLattice<String> locOrder, String nameSuffix) {
4141 String fileName = "lattice_";
4144 cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
4146 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
4149 fileName += nameSuffix;
4151 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
4153 Set<String> addedLocSet = new HashSet<String>();
4155 if (pairSet.size() > 0) {
4157 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
4159 bw.write("digraph " + fileName + " {\n");
4161 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
4162 // pair is in the form of <higher, lower>
4163 Pair<String, String> pair = (Pair<String, String>) iterator.next();
4165 String highLocId = pair.getFirst();
4166 String lowLocId = pair.getSecond();
4168 if (!addedLocSet.contains(highLocId)) {
4169 addedLocSet.add(highLocId);
4170 drawNode(bw, locOrder, simpleHierarchyGraph, highLocId);
4173 if (!addedLocSet.contains(lowLocId)) {
4174 addedLocSet.add(lowLocId);
4175 drawNode(bw, locOrder, simpleHierarchyGraph, lowLocId);
4178 bw.write(highLocId + " -> " + lowLocId + ";\n");
4183 } catch (IOException e) {
4184 e.printStackTrace();
4191 private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
4193 for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
4194 HNode merged = (HNode) iterator.next();
4195 if (merged.isMergeNode()) {
4196 str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
4198 str += " " + merged.getName();
4204 private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, HierarchyGraph graph,
4205 String locName) throws IOException {
4207 HNode node = graph.getHNode(locName);
4214 if (lattice.isSharedLoc(locName)) {
4215 prettyStr = locName + "*";
4217 prettyStr = locName;
4220 if (node.isMergeNode()) {
4221 Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
4222 prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
4224 bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
4227 public void _debug_printGraph() {
4228 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
4230 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
4231 MethodDescriptor md = (MethodDescriptor) iterator.next();
4232 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
4235 } catch (IOException e) {
4236 e.printStackTrace();
4244 class CyclicFlowException extends Exception {
4248 class InterDescriptor extends Descriptor {
4250 public InterDescriptor(String name) {