private void insertAfter(JPegStmt node, JPegStmt after) { // System.out.println("node: "+node); // System.out.println("after: "+after); // System.out.println("succs of node: "+getSuccsOf(node)); // this must be done first because the succs of node will be chanaged lately List succOfAfter = new ArrayList(); succOfAfter.addAll(getSuccsOf(node)); unitToSuccs.put(after, succOfAfter); Iterator succsIt = getSuccsOf(node).iterator(); while (succsIt.hasNext()) { Object succ = succsIt.next(); List pred = getPredsOf(succ); pred.remove(node); pred.add(after); } List succOfNode = new ArrayList(); succOfNode.add(after); unitToSuccs.put(node, succOfNode); List predOfAfter = new ArrayList(); predOfAfter.add(node); unitToPreds.put(after, predOfAfter); // buildPredecessor(Chain pegChain); }
/** * Given an abstract dispatch to an object of type c and a method m, gives a list of possible * receiver methods. */ public List resolveAbstractDispatch(SootClass c, SootMethod m) { c.checkLevel(SootClass.HIERARCHY); m.getDeclaringClass().checkLevel(SootClass.HIERARCHY); checkState(); Iterator<SootClass> classesIt = null; if (c.isInterface()) { classesIt = getImplementersOf(c).iterator(); HashSet<SootClass> classes = new HashSet<SootClass>(); while (classesIt.hasNext()) classes.addAll(getSubclassesOfIncluding(classesIt.next())); classesIt = classes.iterator(); } else classesIt = getSubclassesOfIncluding(c).iterator(); ArraySet s = new ArraySet(); while (classesIt.hasNext()) { SootClass cl = classesIt.next(); if (Modifier.isAbstract(cl.getModifiers())) continue; s.add(resolveConcreteDispatch(cl, m)); } List l = new ArrayList(); l.addAll(s); return Collections.unmodifiableList(l); }
/* The purpose of this method is to replace the ASTIfElseNode given by the var nodeNumber with the new ASTIfNode and to add the remianing list of bodies after this ASTIfNode The new body is then returned; */ public List<Object> createNewBody(List<Object> oldSubBody, int nodeNumber) { if (newIfNode == null) return null; List<Object> newSubBody = new ArrayList<Object>(); if (oldSubBody.size() <= nodeNumber) { // something is wrong since the oldSubBody has lesser nodes than nodeNumber return null; } Iterator<Object> oldIt = oldSubBody.iterator(); int index = 0; while (index != nodeNumber) { newSubBody.add(oldIt.next()); index++; } // check to see that the next is an ASTIfElseNode ASTNode temp = (ASTNode) oldIt.next(); if (!(temp instanceof ASTIfElseNode)) return null; newSubBody.add(newIfNode); newSubBody.addAll(remainingBody); // copy any remaining nodes while (oldIt.hasNext()) { newSubBody.add(oldIt.next()); } return newSubBody; }
public String toString() { Iterator it = iterator(); StringBuffer buf = new StringBuffer(); while (it.hasNext()) { JPegStmt u = (JPegStmt) it.next(); buf.append("u is: " + u + "\n"); List l = new ArrayList(); l.addAll(getPredsOf(u)); buf.append("preds: " + l + "\n"); // buf.append(u.toString() + '\n'); l = new ArrayList(); l.addAll(getSuccsOf(u)); buf.append("succs: " + l + "\n"); } return buf.toString(); }
public OnFlyCallGraph(PAG pag) { this.pag = pag; CGOptions options = new CGOptions(PhaseOptions.v().getPhaseOptions("cg")); if (options.all_reachable()) { List entryPoints = new ArrayList(); entryPoints.addAll(EntryPoints.v().all()); entryPoints.addAll(EntryPoints.v().methodsOfApplicationClasses()); Scene.v().setEntryPoints(entryPoints); } callGraph = new CallGraph(); Scene.v().setCallGraph(callGraph); ContextManager cm = CallGraphBuilder.makeContextManager(callGraph); reachableMethods = Scene.v().getReachableMethods(); ofcgb = new OnFlyCallGraphBuilder(cm, reachableMethods); reachablesReader = reachableMethods.listener(); callEdges = cm.callGraph().listener(); }
public List getUseBoxes() { List useBoxes = new ArrayList(); useBoxes.addAll(opBox.getValue().getUseBoxes()); useBoxes.add(opBox); return useBoxes; }
public List getUseBoxes() { List list = new ArrayList(); for (int i = 0; i < argBoxes.size(); i++) { list.addAll(getArg(i).getUseBoxes()); list.add(getArgBox(i)); } return list; }
/** Returns a list of subinterfaces of c, including itself. */ public List<SootClass> getSubinterfacesOfIncluding(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (!c.isInterface()) throw new RuntimeException("interface needed!"); List<SootClass> l = new ArrayList<SootClass>(); l.addAll(getSubinterfacesOf(c)); l.add(c); return Collections.unmodifiableList(l); }
/** Returns a list of possible targets for the given method and set of receiver types. */ public List resolveAbstractDispatch(List classes, SootMethod m) { m.getDeclaringClass().checkLevel(SootClass.HIERARCHY); ArraySet s = new ArraySet(); Iterator classesIt = classes.iterator(); while (classesIt.hasNext()) s.addAll(resolveAbstractDispatch((SootClass) classesIt.next(), m)); List l = new ArrayList(); l.addAll(s); return Collections.unmodifiableList(l); }
/** Returns a list of direct subclasses of c, including c. */ public List<SootClass> getDirectSubclassesOfIncluding(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (c.isInterface()) throw new RuntimeException("class needed!"); checkState(); List<SootClass> l = new ArrayList<SootClass>(); l.addAll(classToDirSubclasses.get(c)); l.add(c); return Collections.unmodifiableList(l); }
/** Given a set of definite receiver types, returns a list of possible targets. */ public List resolveConcreteDispatch(List classes, SootMethod m) { m.getDeclaringClass().checkLevel(SootClass.HIERARCHY); checkState(); ArraySet s = new ArraySet(); Iterator classesIt = classes.iterator(); while (classesIt.hasNext()) { Object cls = classesIt.next(); if (cls instanceof RefType) s.add(resolveConcreteDispatch(((RefType) cls).getSootClass(), m)); else if (cls instanceof ArrayType) { s.add(resolveConcreteDispatch((RefType.v("java.lang.Object")).getSootClass(), m)); } else throw new RuntimeException("Unable to resolve concrete dispatch of type " + cls); } List l = new ArrayList(); l.addAll(s); return Collections.unmodifiableList(l); }
/** Returns a list of subinterfaces of c, excluding itself. */ public List<SootClass> getSubinterfacesOf(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (!c.isInterface()) throw new RuntimeException("interface needed!"); checkState(); // If already cached, return the value. if (interfaceToSubinterfaces.get(c) != null) return interfaceToSubinterfaces.get(c); // Otherwise, build up the hashmap. List<SootClass> l = new ArrayList<SootClass>(); ListIterator it = interfaceToDirSubinterfaces.get(c).listIterator(); while (it.hasNext()) { l.addAll(getSubinterfacesOfIncluding((SootClass) it.next())); } interfaceToSubinterfaces.put(c, Collections.unmodifiableList(l)); return Collections.unmodifiableList(l); }
private void insertBefore(JPegStmt node, JPegStmt enter) { // build preds of before List predOfBefore = new ArrayList(); predOfBefore.addAll(getPredsOf(node)); unitToPreds.put(enter, predOfBefore); // System.out.println("put into unitToPreds enter: "+enter); // System.out.println("put into unitToPreds value: "+predOfBefore); Iterator predsIt = getPredsOf(node).iterator(); // build succs of former preds of node while (predsIt.hasNext()) { Object pred = predsIt.next(); List succ = getSuccsOf(pred); succ.remove(node); succ.add(enter); // System.out.println("in unitToPred pred: "+pred); // System.out.println("in unitToPred value is: "+succ); } List succOfBefore = new ArrayList(); succOfBefore.add(node); unitToSuccs.put(enter, succOfBefore); // System.out.println("put into unitToSuccs enter: "+enter); // System.out.println("put into unitToSuccs value: "+succOfBefore); List predOfNode = new ArrayList(); predOfNode.add(enter); unitToPreds.put(node, predOfNode); // System.out.println("put into unitToPreds enter: "+node); // System.out.println("put into unitToPreds value: "+predOfNode); // buildPreds(); }
/** Returns a list of subclasses of c, excluding itself. */ public List<SootClass> getSubclassesOf(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (c.isInterface()) throw new RuntimeException("class needed!"); checkState(); // If already cached, return the value. if (classToSubclasses.get(c) != null) return classToSubclasses.get(c); // Otherwise, build up the hashmap. List<SootClass> l = new ArrayList<SootClass>(); ListIterator it = classToDirSubclasses.get(c).listIterator(); while (it.hasNext()) { SootClass cls = (SootClass) it.next(); if (cls.resolvingLevel() < SootClass.HIERARCHY) continue; l.addAll(getSubclassesOfIncluding(cls)); } l = Collections.unmodifiableList(l); classToSubclasses.put(c, l); return l; }
/** Constructs a hierarchy from the current scene. */ public Hierarchy() { this.sc = Scene.v(); state = sc.getState(); // Well, this used to be describable by 'Duh'. // Construct the subclasses hierarchy and the subinterfaces hierarchy. { Chain allClasses = sc.getClasses(); classToSubclasses = new HashMap<SootClass, List<SootClass>>(allClasses.size() * 2 + 1, 0.7f); interfaceToSubinterfaces = new HashMap<SootClass, List<SootClass>>(allClasses.size() * 2 + 1, 0.7f); classToDirSubclasses = new HashMap<SootClass, List>(allClasses.size() * 2 + 1, 0.7f); interfaceToDirSubinterfaces = new HashMap<SootClass, List>(allClasses.size() * 2 + 1, 0.7f); interfaceToDirImplementers = new HashMap<SootClass, List>(allClasses.size() * 2 + 1, 0.7f); Iterator classesIt = allClasses.iterator(); while (classesIt.hasNext()) { SootClass c = (SootClass) classesIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; if (c.isInterface()) { interfaceToDirSubinterfaces.put(c, new ArrayList()); interfaceToDirImplementers.put(c, new ArrayList()); } else classToDirSubclasses.put(c, new ArrayList()); } classesIt = allClasses.iterator(); while (classesIt.hasNext()) { SootClass c = (SootClass) classesIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; if (c.hasSuperclass()) { if (c.isInterface()) { Iterator subIt = c.getInterfaces().iterator(); while (subIt.hasNext()) { SootClass i = (SootClass) subIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; List<SootClass> l = interfaceToDirSubinterfaces.get(i); l.add(c); } } else { List<SootClass> l = classToDirSubclasses.get(c.getSuperclass()); l.add(c); Iterator subIt = c.getInterfaces().iterator(); while (subIt.hasNext()) { SootClass i = (SootClass) subIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; l = interfaceToDirImplementers.get(i); l.add(c); } } } } // Fill the directImplementers lists with subclasses. { classesIt = allClasses.iterator(); while (classesIt.hasNext()) { SootClass c = (SootClass) classesIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; if (c.isInterface()) { List<SootClass> imp = interfaceToDirImplementers.get(c); Set<SootClass> s = new ArraySet(); Iterator<SootClass> impIt = imp.iterator(); while (impIt.hasNext()) { SootClass c0 = impIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; s.addAll(getSubclassesOfIncluding(c0)); } imp.clear(); imp.addAll(s); } } } classesIt = allClasses.iterator(); while (classesIt.hasNext()) { SootClass c = (SootClass) classesIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; if (c.isInterface()) { interfaceToDirSubinterfaces.put( c, Collections.unmodifiableList(interfaceToDirSubinterfaces.get(c))); interfaceToDirImplementers.put( c, Collections.unmodifiableList(interfaceToDirImplementers.get(c))); } else classToDirSubclasses.put(c, Collections.unmodifiableList(classToDirSubclasses.get(c))); } } }
protected void buildSuccsForInlining(JPegStmt stmt, Chain chain, PegGraph inlinee) { // System.out.println("entering buildSuccsForInlining..."); Tag tag = (Tag) stmt.getTags().get(0); // System.out.println("stmt is: "+tag+" "+stmt); /*connect heads of inlinee with the preds of invokeStmt and * delete stmt from the succs list from the preds */ Iterator predIt = getPredsOf(stmt).iterator(); // System.out.println("preds list: "+getPredsOf(stmt)); // System.out.println("preds size: "+getPredsOf(stmt).size()); Iterator headsIt = inlinee.getHeads().iterator(); { // System.out.println("heads: "+inlinee.getHeads()); while (predIt.hasNext()) { JPegStmt pred = (JPegStmt) predIt.next(); // System.out.println("pred: "+pred); List succList = (List) getSuccsOf(pred); // System.out.println("succList of pred: "+succList); int pos = succList.indexOf(stmt); // System.out.println("remove : "+stmt + " from succList: \n"+succList+ "\n of pred" ); // remove invokeStmt succList.remove(pos); while (headsIt.hasNext()) { succList.add(headsIt.next()); } unitToSuccs.put(pred, succList); } { while (headsIt.hasNext()) { Object head = headsIt.next(); List predsOfHeads = new ArrayList(); predsOfHeads.addAll(getPredsOf(head)); unitToPreds.put(head, predsOfHeads); } } /* { predIt = getPredsOf(stmt).iterator(); while (predIt.hasNext()){ JPegStmt s = (JPegStmt)predIt.next(); if (unitToSuccs.containsKey(s)){ Iterator succIt = ((List) unitToSuccs.get(s)).iterator(); while(succIt.hasNext()){ //Object successor = succIt.next(); JPegStmt successor = (JPegStmt)succIt.next(); List predList = (List) unitToPreds.get(successor); if (predList != null) { try { predList.add(s); } catch(NullPointerException e) { System.out.println(s + "successor: " + successor); throw e; } } } } } }*/ } /*connect tails of inlinee with the succ of invokeStmt and * delete stmt from the */ Iterator tailsIt = inlinee.getTails().iterator(); { // System.out.println("tails: "+inlinee.getTails()); while (tailsIt.hasNext()) { Iterator succIt = getSuccsOf(stmt).iterator(); JPegStmt tail = (JPegStmt) tailsIt.next(); List succList = null; if (unitToSuccs.containsKey(tail)) { // System.out.println("error: unitToSucc containsKey: "+tail); succList = (List) getSuccsOf(tail); // System.out.println("succList: "+succList); } else { succList = new ArrayList(); } while (succIt.hasNext()) { JPegStmt succ = (JPegStmt) succIt.next(); succList.add(succ); // System.out.println("succ: "+succ); // remove stmt from the preds list of the succs of itself. List predListOfSucc = getPredsOf(succ); if (predListOfSucc == null) { System.err.println("Error: predListOfSucc is null!"); System.exit(1); } else { if (predListOfSucc.size() != 0) { int pos = predListOfSucc.indexOf(stmt); if (pos > 0 || pos == 0) { // System.out.println("remove stmt: "+stmt+" from the preds list"+predListOfSucc+" // of the succ"); predListOfSucc.remove(pos); } // System.out.println("remove(from PRED): "); } } unitToPreds.put(succ, predListOfSucc); } unitToSuccs.put(tail, succList); // System.out.println("put: "+tail); // System.out.println("succList: "+succList+ "into unitToSucc"); } } // add Nov 1 { tailsIt = inlinee.getTails().iterator(); while (tailsIt.hasNext()) { JPegStmt s = (JPegStmt) tailsIt.next(); if (unitToSuccs.containsKey(s)) { Iterator succIt = unitToSuccs.get(s).iterator(); while (succIt.hasNext()) { // Object successor = succIt.next(); JPegStmt successor = (JPegStmt) succIt.next(); List<JPegStmt> predList = unitToPreds.get(successor); if (predList != null && !predList.contains(s)) { try { predList.add(s); /* Tag tag = (Tag)successor.getTags().get(0); System.out.println("add "+s+" to predlist of "+tag+" "+successor); */ } catch (NullPointerException e) { System.out.println(s + "successor: " + successor); throw e; } } } } } } // end add Nov 1 // System.out.println("stmt: "+stmt); // remove stmt from allNodes and mainPegChain // System.out.println("mainPegChain contains stmt: "+mainPegChain.contains(stmt)); // testPegChain(); if (!allNodes.contains(stmt)) { System.err.println("fail to find begin node in allNodes!"); System.exit(1); } else { allNodes.remove(stmt); // System.out.println("remove from allNode: "+stmt); } if (!chain.contains(stmt)) { System.err.println("Error! Chain does not contains stmt (extending point)!"); System.exit(1); } else { if (!chain.remove(stmt)) { System.err.println("fail to remove invoke stmt in from Chain!"); System.exit(1); } } /* if (!mainPegChain.contains(stmt)){ boolean find = false; //System.out.println("main chain does not contain AFTER"); Set maps = startToThread.entrySet(); for(Iterator iter=maps.iterator(); iter.hasNext();){ Map.Entry entry = (Map.Entry)iter.next(); Object startNode = entry.getKey(); Iterator runIt = ((List)entry.getValue()).iterator(); while (runIt.hasNext()){ Chain chain=(Chain)runIt.next(); if (chain.contains(stmt)) { find = true; if (!chain.remove(stmt)){ System.err.println("fail to remove begin node in from mainPegChain!"); System.exit(1); } break; } } if (find == false){ System.err.println("fail to find stmt: "+stmt+" in chains!"); System.exit(1); } } //this.toString(); } else{ if (!mainPegChain.remove(stmt)) { System.err.println("fail to remove begin node in from mainPegChain!"); System.exit(1); } else{ // System.out.println("remove(from mainchain): "+stmt); } } */ // remove stmt from unitToSuccs and unitToPreds if (unitToSuccs.containsKey(stmt)) { unitToSuccs.remove(stmt); } if (unitToPreds.containsKey(stmt)) { unitToPreds.remove(stmt); } }
/** * This method pushes all newExpr down to be the stmt directly before every invoke of the init * only if they are in the types list */ public void internalTransform(Body b, String phaseName, Map options) { JimpleBody body = (JimpleBody) b; if (Options.v().verbose()) G.v().out.println("[" + body.getMethod().getName() + "] Folding Jimple constructors..."); Chain units = body.getUnits(); List<Unit> stmtList = new ArrayList<Unit>(); stmtList.addAll(units); Iterator<Unit> it = stmtList.iterator(); Iterator<Unit> nextStmtIt = stmtList.iterator(); // start ahead one nextStmtIt.next(); SmartLocalDefs localDefs = SmartLocalDefsPool.v().getSmartLocalDefsFor(body); UnitGraph graph = localDefs.getGraph(); LocalUses localUses = new SimpleLocalUses(graph, localDefs); /* fold in NewExpr's with specialinvoke's */ while (it.hasNext()) { Stmt s = (Stmt) it.next(); if (!(s instanceof AssignStmt)) continue; /* this should be generalized to ArrayRefs */ // only deal with stmts that are an local = newExpr Value lhs = ((AssignStmt) s).getLeftOp(); if (!(lhs instanceof Local)) continue; Value rhs = ((AssignStmt) s).getRightOp(); if (!(rhs instanceof NewExpr)) continue; // check if very next statement is invoke --> // this indicates there is no control flow between // new and invoke and should do nothing if (nextStmtIt.hasNext()) { Stmt next = (Stmt) nextStmtIt.next(); if (next instanceof InvokeStmt) { InvokeStmt invoke = (InvokeStmt) next; if (invoke.getInvokeExpr() instanceof SpecialInvokeExpr) { SpecialInvokeExpr invokeExpr = (SpecialInvokeExpr) invoke.getInvokeExpr(); if (invokeExpr.getBase() == lhs) { break; } } } } // check if new is in the types list - only process these if (!types.contains(((NewExpr) rhs).getType())) continue; List lu = localUses.getUsesOf(s); Iterator luIter = lu.iterator(); boolean MadeNewInvokeExpr = false; while (luIter.hasNext()) { Unit use = ((UnitValueBoxPair) (luIter.next())).unit; if (!(use instanceof InvokeStmt)) continue; InvokeStmt is = (InvokeStmt) use; if (!(is.getInvokeExpr() instanceof SpecialInvokeExpr) || lhs != ((SpecialInvokeExpr) is.getInvokeExpr()).getBase()) continue; // make a new one here AssignStmt constructStmt = Jimple.v() .newAssignStmt(((DefinitionStmt) s).getLeftOp(), ((DefinitionStmt) s).getRightOp()); constructStmt.setRightOp(Jimple.v().newNewExpr(((NewExpr) rhs).getBaseType())); MadeNewInvokeExpr = true; // redirect jumps use.redirectJumpsToThisTo(constructStmt); // insert new one here units.insertBefore(constructStmt, use); constructStmt.addTag(s.getTag("SourceLnPosTag")); } if (MadeNewInvokeExpr) { units.remove(s); } } }