private void buildSuccessor(Chain pegChain) { // Add regular successors { HashMap unitToPeg = (HashMap) unitToPegMap.get(pegChain); Iterator pegIt = pegChain.iterator(); JPegStmt currentNode, nextNode; currentNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; // June 19 add for begin node if (currentNode != null) { // System.out.println("currentNode: "+currentNode); // if the unit is "begin" node nextNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; if (currentNode.getName().equals("begin")) { List<JPegStmt> successors = new ArrayList<JPegStmt>(); successors.add(nextNode); unitToSuccs.put(currentNode, successors); currentNode = nextNode; } // end June 19 add for begin node while (currentNode != null) { // System.out.println("currentNode: "+currentNode); /* If unitToSuccs contains currentNode, it is the point to inline methods, * we need not compute its successors again */ if (unitToSuccs.containsKey(currentNode) && !currentNode.getName().equals("wait")) { currentNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; continue; } List<JPegStmt> successors = new ArrayList<JPegStmt>(); Unit unit = currentNode.getUnit(); UnitGraph unitGraph = currentNode.getUnitGraph(); List unitSucc = unitGraph.getSuccsOf(unit); Iterator succIt = unitSucc.iterator(); while (succIt.hasNext()) { Unit un = (Unit) succIt.next(); // Don't build the edge from "monitor exit" to exception handler if (unit instanceof ExitMonitorStmt && exceHandlers.contains(un)) { // System.out.println("====find it! unit: "+unit+"\n un: "+un); continue; } else if (unitToPeg.containsKey(un)) { JPegStmt pp = (JPegStmt) (unitToPeg.get(un)); if (pp != null && !successors.contains(pp)) successors.add(pp); } } // end while if (currentNode.getName().equals("wait")) { while (!(currentNode.getName().equals("notified-entry"))) { currentNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; } unitToSuccs.put(currentNode, successors); // System.out.println("put key: "+currentNode+" into unitToSucc"); } else { unitToSuccs.put(currentNode, successors); } if (currentNode.getName().equals("start")) { // System.out.println("-----build succ for start----"); if (startToThread.containsKey(currentNode)) { List runMethodChainList = startToThread.get(currentNode); Iterator possibleMethodIt = runMethodChainList.iterator(); while (possibleMethodIt.hasNext()) { Chain subChain = (Chain) possibleMethodIt.next(); if (subChain != null) { // System.out.println("build succ for subChain"); // buildSuccessor(subGraph, subChain, addExceptionEdges); buildSuccessor(subChain); } else System.out.println("*********subgraph is null!!!"); } } } currentNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; } // while // June 19 add for begin node } // end June 19 add for begin node } }
/** * Constructs a graph for the units found in the provided Body instance. Each node in the graph * corresponds to a unit. The edges are derived from the control flow. * * @param body The underlying body we want to make a graph for. * @param addExceptionEdges If true then the control flow edges associated with exceptions are * added. * @param dontAddEdgeFromStmtBeforeAreaOfProtectionToCatchBlock This was added for Dava. If true, * edges are not added from statement before area of protection to catch. If false, edges ARE * added. For Dava, it should be true. For flow analyses, it should be false. * @param Hierarchy Using class hierarchy analysis to find the run method of started thread * @param PointsToAnalysis Using point to analysis (SPARK package) to improve the precision of * results */ public PegGraph( CallGraph callGraph, Hierarchy hierarchy, PAG pag, Set methodsNeedingInlining, Set allocNodes, List<List> inlineSites, Map<SootMethod, String> synchObj, Set multiRunAllocNodes, Map<AllocNode, String> allocNodeToObj, Body unitBody, String threadName, SootMethod sm, boolean addExceEdge, boolean dontAddEdgeFromStmtBeforeAreaOfProtectionToCatchBlock) { this.allocNodeToObj = allocNodeToObj; this.multiRunAllocNodes = multiRunAllocNodes; this.synchObj = synchObj; this.inlineSites = inlineSites; this.allocNodes = allocNodes; this.methodsNeedingInlining = methodsNeedingInlining; logFile = new File("log.txt"); try { fileWriter = new FileWriter(logFile); } catch (IOException io) { System.err.println("Errors occur during create FileWriter !"); // throw io; } body = unitBody; synch = new HashSet<List>(); exceHandlers = new HashSet<Unit>(); needInlining = true; monitorObjs = new HashSet<Object>(); startToBeginNodes = new HashMap(); unitChain = body.getUnits(); int size = unitChain.size(); // initial unitToSuccs, unitToPreds, unitToPegMap, and startToThread unitToSuccs = new HashMap(size * 2 + 1, 0.7f); unitToPreds = new HashMap(size * 2 + 1, 0.7f); // unitToPegMap is the map of a chain to its corresponding (cfg node --> peg node ) map. unitToPegMap = new HashMap(size * 2 + 1, 0.7f); startToThread = new HashMap(size * 2 + 1, 0.7f); startToAllocNodes = new HashMap(size * 2 + 1, 0.7f); waitingNodes = new HashMap<String, FlowSet>(size * 2 + 1, 0.7f); joinStmtToThread = new HashMap<JPegStmt, Chain>(); threadNo = new HashMap(); threadNameToStart = new HashMap(); this.allocNodeToObj = new HashMap<AllocNode, String>(size * 2 + 1, 0.7f); allocNodeToThread = new HashMap<AllocNode, PegChain>(size * 2 + 1, 0.7f); notifyAll = new HashMap<String, Set<JPegStmt>>(size * 2 + 1, 0.7f); methodsNeedingInlining = new HashSet(); allNodes = new ArraySparseSet(); canNotBeCompacted = new HashSet(); threadAllocSites = new HashSet(); specialJoin = new HashSet<JPegStmt>(); // if(Main.isVerbose) // System.out.println(" Constructing PegGraph..."); // if(Main.isProfilingOptimization) // Main.graphTimer.start(); // make a peg for debug /* mainPegChain = new HashChain(); specialTreatment1(); */ // end make a peg UnitGraph mainUnitGraph = new CompleteUnitGraph(body); // mainPegChain = new HashChain(); mainPegChain = new PegChain( callGraph, hierarchy, pag, threadAllocSites, methodsNeedingInlining, allocNodes, inlineSites, synchObj, multiRunAllocNodes, allocNodeToObj, body, sm, threadName, true, this); // testPegChain(); // System.out.println("finish building chain"); // testStartToThread(); // buildSuccessor(mainUnitGraph, mainPegChain,addExceptionEdges); // buildSuccessorForExtendingMethod(mainPegChain); // testSet(exceHandlers, "exceHandlers"); buildSuccessor(mainPegChain); // System.out.println("finish building successors"); // unmodifiableSuccs(mainPegChain); // testUnitToSucc ); buildPredecessor(mainPegChain); // System.out.println("finish building predcessors"); // unmodifiablePreds(mainPegChain); // testSynch(); addMonitorStmt(); addTag(); // System.out.println(this.toString()); buildHeadsAndTails(); // testIterator(); // testWaitingNodes(); // System.out.println("finish building heads and tails"); // testSet(canNotBeCompacted, "canNotBeCompacted"); // computeEdgeAndThreadNo(); // testExtendingPoints(); // testUnitToSucc(); // testPegChain(); /* if (print) { PegToDotFile printer1 = new PegToDotFile(this, false, sm.getName()); } */ try { fileWriter.flush(); fileWriter.close(); } catch (IOException io) { System.err.println("Errors occur during close file " + logFile.getName()); // throw io; } // System.out.println("==threadAllocaSits==\n"+threadAllocSites.toString()); }