1 package Analysis.SSJava;
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
12 import IR.ClassDescriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.VarDescriptor;
18 public class FlowGraph {
22 Set<FlowNode> returnNodeSet;
25 Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
27 Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
28 Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
30 // maps the composite representation of field/var descriptors to infer nodes
31 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
33 // maps a paramter descriptor to its index
34 Map<Descriptor, Integer> mapParamDescToIdx;
36 // DS for the lattice generation
37 Map<Integer, FlowNode> mapIdxToFlowNode;
39 public static int interseed = 0;
43 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
45 this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
46 this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
47 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
48 this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
49 this.mapParamDescToIdx.putAll(mapParamDescToIdx);
50 this.returnNodeSet = new HashSet<FlowNode>();
51 this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
52 this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
55 // create a node for 'this' varialbe
56 // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
57 // thisDescTuple.add(md.getThis());
59 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
60 thisVarTuple.add(md.getThis());
61 FlowNode thisNode = createNewFlowNode(thisVarTuple);
62 thisNode.setSkeleton(true);
63 thisVarNode = thisNode;
70 public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
71 return mapDescTupleToInferNode;
74 public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
75 this.mapDescTupleToInferNode.putAll(in);
78 public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
79 return mapLocTupleToFlowNode;
82 public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
83 this.mapLocTupleToFlowNode.putAll(in);
86 public void setReturnNodeSet(Set<FlowNode> in) {
87 this.returnNodeSet.addAll(in);
90 public void setThisVarNode(FlowNode thisVarNode) {
91 this.thisVarNode = thisVarNode;
94 public Map<Descriptor, Integer> getMapParamDescToIdx() {
95 return mapParamDescToIdx;
98 public FlowNode createIntermediateNode() {
99 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
100 Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
101 tuple.add(interDesc);
104 FlowNode newNode = new FlowNode(tuple);
105 newNode.setIntermediate(true);
107 mapDescTupleToInferNode.put(tuple, newNode);
108 // nodeSet.add(newNode);
110 System.out.println("create new intermediate node= " + newNode);
115 private void setupMapIdxToDesc() {
117 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
118 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
119 Descriptor paramDesc = (Descriptor) iterator.next();
120 int idx = mapParamDescToIdx.get(paramDesc);
121 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
122 descTuple.add(paramDesc);
123 FlowNode paramNode = getFlowNode(descTuple);
124 mapIdxToFlowNode.put(idx, paramNode);
125 paramNode.setSkeleton(true);
130 public int getNumParameters() {
131 return mapIdxToFlowNode.keySet().size();
134 public FlowNode getParamFlowNode(int idx) {
135 return mapIdxToFlowNode.get(idx);
138 public Set<FlowNode> getNodeSet() {
139 Set<FlowNode> set = new HashSet<FlowNode>();
140 set.addAll(mapDescTupleToInferNode.values());
144 public MethodDescriptor getMethodDescriptor() {
148 public boolean isParamDesc(Descriptor desc) {
150 if (mapParamDescToIdx.containsKey(desc)) {
151 int idx = mapParamDescToIdx.get(desc);
152 if (!md.isStatic() && idx == 0) {
161 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
163 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
164 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
166 Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
167 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
168 FlowEdge flowEdge = (FlowEdge) iterator.next();
169 if (flowEdge.getDst().equals(toNode)) {
172 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
181 public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
183 Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
185 // returns the set of edges that share the same prefix of startNode
186 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
188 for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
189 Set<FlowEdge> nodeEdgeSet = iter.next();
190 for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
191 FlowEdge edge = iter2.next();
192 if (edge.getInitTuple().get(0).equals(prefixDesc)) {
201 public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
202 if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
203 mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
205 return mapFlowNodeToOutEdgeSet.get(node);
208 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
210 FlowNode fromNode = getFlowNode(fromDescTuple);
211 FlowNode toNode = getFlowNode(toDescTuple);
213 // System.out.println("create an edge from " + fromNode + " to " + toNode);
215 int fromTupleSize = fromDescTuple.size();
216 NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
217 for (int i = 0; i < fromTupleSize; i++) {
218 Descriptor desc = fromDescTuple.get(i);
219 curFromTuple.add(desc);
220 int toTupleSize = toDescTuple.size();
221 NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
222 for (int k = 0; k < toTupleSize; k++) {
223 Descriptor toDesc = toDescTuple.get(k);
224 curToTuple.add(toDesc);
225 addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
229 // int fromTupleSize = fromDescTuple.size();
230 // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
231 // for (int i = 0; i < fromTupleSize; i++) {
232 // Descriptor desc = fromDescTuple.get(i);
233 // curTuple.add(desc);
234 // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
237 // int toTupleSize = toDescTuple.size();
238 // curTuple = new NTuple<Descriptor>();
239 // for (int i = 0; i < toTupleSize; i++) {
240 // Descriptor desc = toDescTuple.get(i);
241 // curTuple.add(desc);
242 // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
247 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
248 NTuple<Descriptor> endTuple) {
250 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
251 addOutEdge(fromNode, edge);
253 // System.out.println("add a new edge=" + edge);
256 private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
257 if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
258 mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
260 mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
263 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
264 if (!mapDescTupleToInferNode.containsKey(descTuple)) {
265 FlowNode node = createNewFlowNode(descTuple);
266 mapDescTupleToInferNode.put(descTuple, node);
268 return mapDescTupleToInferNode.get(descTuple);
271 public FlowNode getThisVarNode() {
275 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
277 if (!mapDescTupleToInferNode.containsKey(tuple)) {
278 FlowNode node = new FlowNode(tuple);
279 mapDescTupleToInferNode.put(tuple, node);
280 // nodeSet.add(node);
282 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
284 if (tuple.size() > 1) {
285 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
286 getFlowNode(baseTuple).addFieldNode(node);
289 // System.out.println("Creating new node=" + node);
292 return mapDescTupleToInferNode.get(tuple);
297 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
299 if (!mapDescTupleToInferNode.containsKey(tuple)) {
300 createNewFlowNode(tuple);
303 FlowNode node = mapDescTupleToInferNode.get(tuple);
304 node.setReturn(true);
306 returnNodeSet.add(node);
309 public Set<FlowNode> getReturnNodeSet() {
310 return returnNodeSet;
313 public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
314 Set<FlowNode> set = new HashSet<FlowNode>();
315 recurLocalReachFlowNodeSet(fn, set);
319 private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
321 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
322 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
323 FlowEdge edge = (FlowEdge) iterator.next();
324 FlowNode dstNode = edge.getDst();
326 if (!visited.contains(dstNode)) {
327 visited.add(dstNode);
328 recurLocalReachFlowNodeSet(dstNode, visited);
334 private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
336 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
337 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
338 FlowEdge edge = (FlowEdge) iterator.next();
340 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
341 FlowNode dstNode = getFlowNode(edge.getEndTuple());
342 if (!visited.contains(dstNode)) {
343 visited.add(dstNode);
344 getReachFlowNodeSetFrom(dstNode, visited);
351 public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
352 Set<FlowNode> set = new HashSet<FlowNode>();
353 getReachFlowNodeSetFrom(fn, set);
357 // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
359 // for (Iterator iterator = fn.getOutEdgeSet().iterator();
360 // iterator.hasNext();) {
361 // FlowEdge edge = (FlowEdge) iterator.next();
363 // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
365 // FlowNode dstNode = getFlowNode(edge.getEndTuple());
367 // if (!visited.contains(dstNode)) {
368 // visited.add(dstNode);
369 // getReachFlowNodeSetFrom(dstNode, visited);
376 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
378 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
380 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
381 FlowEdge edge = (FlowEdge) iterator.next();
383 if (fn.getDescTuple().equals(edge.getInitTuple())) {
384 FlowNode dstNode = getFlowNode(edge.getEndTuple());
385 NTuple<Location> dstTuple = getLocationTuple(dstNode);
387 if (!visited.contains(dstTuple)) {
388 visited.add(dstTuple);
389 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
397 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
398 return getLocationTuple(getFlowNode(descTuple));
401 public NTuple<Location> getLocationTuple(FlowNode fn) {
403 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
404 NTuple<Descriptor> descTuple = fn.getDescTuple();
405 NTuple<Location> locTuple = new NTuple<Location>();
406 ClassDescriptor cd = null;
408 Descriptor localDesc = fn.getDescTuple().get(0);
410 if (fn.isIntermediate()) {
411 Location interLoc = new Location(md, localDesc.getSymbol());
412 interLoc.setLocDescriptor(localDesc);
413 locTuple.add(interLoc);
414 } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
415 Location topLoc = new Location(md, Location.TOP);
416 topLoc.setLocDescriptor(LocationInference.TOPDESC);
417 locTuple.add(topLoc);
418 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
419 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
420 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
421 locTuple.add(globalLoc);
424 for (int i = 0; i < descTuple.size(); i++) {
425 Descriptor curDesc = descTuple.get(i);
428 loc = new Location(md, curDesc.getSymbol());
429 loc.setLocDescriptor(curDesc);
430 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
432 loc = new Location(cd, curDesc.getSymbol());
433 loc.setLocDescriptor(curDesc);
435 if (curDesc instanceof FieldDescriptor) {
436 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
438 cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
446 mapFlowNodeToLocTuple.put(fn, locTuple);
448 return mapFlowNodeToLocTuple.get(fn);
451 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
452 Set<FlowNode> set = new HashSet<FlowNode>();
453 getIncomingFlowNodeSet(node, set);
457 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
459 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
460 FlowNode curNode = (FlowNode) iterator.next();
461 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
463 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
464 FlowEdge flowEdge = (FlowEdge) iterator2.next();
466 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
467 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
469 if (!visited.contains(incomingNode)) {
470 visited.add(incomingNode);
471 getIncomingFlowNodeSet(incomingNode, visited);
479 public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
481 NTuple<Descriptor> dstTuple = fn.getDescTuple();
483 Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
485 ClassDescriptor cd = null;
487 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
488 FlowNode node = (FlowNode) iterator.next();
490 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
491 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
492 FlowEdge flowEdge = (FlowEdge) iterator2.next();
493 if (dstTuple.equals(flowEdge.getEndTuple())) {
494 NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
495 NTuple<Location> locTuple = new NTuple<Location>();
496 for (int i = 0; i < initTuple.size(); i++) {
497 Descriptor d = initTuple.get(i);
500 loc = new Location(md, d.getSymbol());
501 cd = ((VarDescriptor) d).getType().getClassDesc();
503 loc = new Location(cd, d.getSymbol());
504 cd = ((FieldDescriptor) d).getType().getClassDesc();
515 public boolean isParameter(NTuple<Descriptor> tuple) {
516 // return true if a descriptor tuple is started with a parameter descriptor
517 Descriptor firstIdxDesc = tuple.get(0);
518 return mapParamDescToIdx.containsKey(firstIdxDesc);
521 public int getParamIdx(NTuple<Descriptor> tuple) {
522 Descriptor firstIdxDesc = tuple.get(0);
523 return mapParamDescToIdx.get(firstIdxDesc);
526 public FlowGraph clone() {
527 FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
529 // clone.setNodeSet(getNodeSet());
530 clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
531 clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
532 clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
534 clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
535 clone.setReturnNodeSet(getReturnNodeSet());
536 clone.setThisVarNode(getThisVarNode());
541 public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
542 return mapFlowNodeToLocTuple;
545 public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
546 this.mapFlowNodeToLocTuple.putAll(in);
549 public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
550 return mapFlowNodeToOutEdgeSet;
553 public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
554 Set<FlowNode> keySet = inMap.keySet();
555 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
556 FlowNode key = (FlowNode) iterator.next();
557 Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
558 newEdgeSet.addAll(inMap.get(key));
559 mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
563 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
564 Set<FlowEdge> addedEdgeSet) throws IOException {
566 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
568 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
569 FlowEdge flowEdge = iterator.next();
571 FlowNode u = flowEdge.getSrc();
572 FlowNode v = flowEdge.getDst();
574 if (u.getDescTuple().equals(flowEdge.getInitTuple())
575 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
576 // only draw an edge of the actual value flow
578 if (!addedEdgeSet.contains(flowEdge)) {
580 if (!addedNodeSet.contains(u)) {
584 if (!addedNodeSet.contains(v)) {
589 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
590 addedEdgeSet.add(flowEdge);
598 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
599 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
602 public void writeGraph() throws java.io.IOException {
606 public void writeGraph(String suffix) throws java.io.IOException {
608 String graphName = "flowgraph_" + md.toString() + suffix;
609 graphName = graphName.replaceAll("[\\W]", "");
611 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
612 bw.write("digraph " + graphName + " {\n");
613 bw.write("compound=true;\n");
615 // then visit every flow node
617 // Iterator<FlowNode> iter = nodeSet.iterator();
618 Iterator<FlowNode> iter = getNodeSet().iterator();
620 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
621 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
623 while (iter.hasNext()) {
624 FlowNode node = iter.next();
626 if (node.getDescTuple().size() == 1) {
627 // here, we just care about the local variable
628 if (node.getFieldNodeSet().size() > 0) {
629 drawSubgraph(node, bw, addedEdgeSet);
632 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
641 public boolean constainsNode(FlowNode node) {
642 return getNodeSet().contains(node);
645 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
648 bw.write("subgraph cluster_" + node.getID() + "{\n");
649 bw.write("label=\"" + node.getPrettyID() + "\";\n");
651 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
652 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
653 FlowNode fieldNode = (FlowNode) iterator.next();
654 if (fieldNode.getFieldNodeSet().size() > 0) {
655 drawSubgraph(fieldNode, bw, addedSet);
657 Descriptor desc = fieldNode.getDescTuple().getLastElement();
658 if (desc instanceof VarDescriptor) {
659 VarDescriptor varDesc = (VarDescriptor) desc;
660 if (varDesc.getType().isPrimitive()) {
661 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
663 } else if (desc instanceof FieldDescriptor) {
664 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
665 if (fieldDesc.getType().isPrimitive()) {
666 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");