public static boolean maybeSameLocation(Value v1, Value v2) { if (!(v1 instanceof InstanceFieldRef && v2 instanceof InstanceFieldRef) && !(v1 instanceof ArrayRef && v2 instanceof ArrayRef)) { return v1.equivTo(v2); } if (v1 instanceof InstanceFieldRef && v2 instanceof InstanceFieldRef) { InstanceFieldRef ifr1 = (InstanceFieldRef) v1; InstanceFieldRef ifr2 = (InstanceFieldRef) v2; if (!ifr1.getField().getName().equals(ifr2.getField().getName())) return false; Local base1 = (Local) ifr1.getBase(); Local base2 = (Local) ifr2.getBase(); PointsToAnalysis pta = Scene.v().getPointsToAnalysis(); PointsToSet pts1 = pta.reachingObjects(base1); PointsToSet pts2 = pta.reachingObjects(base2); return pts1.hasNonEmptyIntersection(pts2); } else { // v1 instanceof ArrayRef && v2 instanceof ArrayRef ArrayRef ar1 = (ArrayRef) v1; ArrayRef ar2 = (ArrayRef) v2; Local base1 = (Local) ar1.getBase(); Local base2 = (Local) ar2.getBase(); PointsToAnalysis pta = Scene.v().getPointsToAnalysis(); PointsToSet pts1 = pta.reachingObjects(base1); PointsToSet pts2 = pta.reachingObjects(base2); return pts1.hasNonEmptyIntersection(pts2); } }
private static SootClass makeFreshClass(String name) { // reset the testClass if (Scene.v().containsClass(name)) { Scene.v().removeClass(Scene.v().getSootClass(name)); } SootClass result = new SootClass(name); Scene.v().addClass(result); return result; }
/** * @ast method * @aspect Expressions * @declaredat * /Users/eric/Documents/workspaces/clara-soot/JastAddExtensions/JimpleBackend/Expressions.jrag:84 */ public soot.Value eval(Body b) { TypeDecl dest = getDest().type(); TypeDecl source = getSource().type(); if (dest.isString()) { Value lvalue = getDest().eval(b); Value v = asImmediate(b, lvalue); // new StringBuffer(left) Local local = b.newTemp(b.newNewExpr(lookupType("java.lang", "StringBuffer").sootRef(), this)); b.setLine(this); b.add( b.newInvokeStmt( b.newSpecialInvokeExpr( local, Scene.v() .getMethod("<java.lang.StringBuffer: void <init>(java.lang.String)>") .makeRef(), v, this), this)); // append right Local rightResult = b.newTemp( b.newVirtualInvokeExpr( local, lookupType("java.lang", "StringBuffer") .methodWithArgs("append", new TypeDecl[] {source.stringPromotion()}) .sootRef(), asImmediate(b, getSource().eval(b)), this)); // toString Local result = b.newTemp( b.newVirtualInvokeExpr( rightResult, Scene.v() .getMethod("<java.lang.StringBuffer: java.lang.String toString()>") .makeRef(), this)); Value v2 = lvalue instanceof Local ? lvalue : (Value) lvalue.clone(); getDest().emitStore(b, v2, result, this); return result; } else { return super.eval(b); } }
public Map<Unit, Set<TaintFact>> initialSeeds() { if (DEBUG) System.out.println("initial seeds"); return DefaultSeeds.make( Collections.singleton( Scene.v().getEntryPoints().get(0).getActiveBody().getUnits().getFirst()), zeroValue()); }
public class TopologicalOrderer { CallGraph cg; List<SootMethod> order = new ArrayList<SootMethod>(); NumberedSet visited = new NumberedSet(Scene.v().getMethodNumberer()); public TopologicalOrderer(CallGraph cg) { this.cg = cg; } public void go() { Iterator methods = cg.sourceMethods(); while (methods.hasNext()) { SootMethod m = (SootMethod) methods.next(); dfsVisit(m); } } private void dfsVisit(SootMethod m) { if (visited.contains(m)) return; visited.add(m); Iterator targets = new Targets(cg.edgesOutOf(m)); while (targets.hasNext()) { SootMethod target = (SootMethod) targets.next(); dfsVisit(target); } order.add(m); } public List<SootMethod> order() { return order; } }
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 static void main(String args[]) { // Set classPath String exemplePath = "/Users/gamyot/Documents/workspace/Soot_Exemples/src/"; String objectPath = "/System/Library/Frameworks/JavaVM.framework/Classes/classes.jar"; String tracePath = "/Users/gamyot/Documents/workspace/SootInstrumenter/src/"; Scene.v().setSootClassPath(".:" + objectPath + ":" + exemplePath + ":" + tracePath); Scene.v().loadClassAndSupport("java.lang.Object"); Scene.v().loadClassAndSupport("java.lang.System"); // Set up the class we’re working with SootClass c = Scene.v().loadClassAndSupport("MyExemples.ExempleBasic"); c.setApplicationClass(); // Get methods Iterator<SootMethod> methodIterator = c.methodIterator(); while (methodIterator.hasNext()) methodList.add(methodIterator.next()); // Iterate through the method list for (SootMethod m : methodList) { Body body = m.retrieveActiveBody(); PatchingChain<Unit> unitList = body.getUnits(); // get the all the "if statements" Units List<Unit> ifStmtList = searchIfStmts(body); // for each "if statement" unit, instrument and add the instrumentation code right after for (Unit ifStmtUnit : ifStmtList) { // Chain<Unit> instrumentedChain = generateInstrumentationUnits(ifStmtUnit, body); // unitList.insertAfter(instrumentedChain, ifStmtUnit); Chain<Unit> testChain = generateInstrumentationUnits(ifStmtUnit, body); unitList.insertAfter(testChain, ifStmtUnit); } // Output all the units for this method on terminal String methodName = m.getName(); System.out.println( "____Method: \"" + methodName + "\"__________________________________________________"); LoadAndGenerate.printAllUnits(body); } try { LoadAndGenerate.outputClassToBinary(c); } catch (IOException e) { e.printStackTrace(); } }
/** * Indicates whether this ThrowableSet includes some exception that might be caught by a handler * argument of the type <code>catcher</code>. * * @param catcher type of the handler parameter to be tested. * @return <code>true</code> if this set contains an exception type that might be caught by <code> * catcher</code>, false if it does not. */ public boolean catchableAs(RefType catcher) { if (INSTRUMENTING) { Manager.v().catchableAsQueries++; } FastHierarchy h = Scene.v().getOrMakeFastHierarchy(); if (exceptionsExcluded.size() > 0) { if (INSTRUMENTING) { Manager.v().catchableAsFromSearch++; } for (Iterator i = exceptionsExcluded.iterator(); i.hasNext(); ) { AnySubType exclusion = (AnySubType) i.next(); if (h.canStoreType(catcher, exclusion.getBase())) { return false; } } } if (exceptionsIncluded.contains(catcher)) { if (INSTRUMENTING) { if (exceptionsExcluded.size() == 0) { Manager.v().catchableAsFromMap++; } else { Manager.v().catchableAsFromSearch++; } } return true; } else { if (INSTRUMENTING) { if (exceptionsExcluded.size() == 0) { Manager.v().catchableAsFromSearch++; } } for (Iterator i = exceptionsIncluded.iterator(); i.hasNext(); ) { RefLikeType thrownType = (RefLikeType) i.next(); if (thrownType instanceof RefType) { if (thrownType == catcher) { // assertion failure. throw new IllegalStateException( "ThrowableSet.catchableAs(RefType): exceptions.contains() failed to match contained RefType " + catcher); } else if (h.canStoreType(thrownType, catcher)) { return true; } } else { RefType thrownBase = ((AnySubType) thrownType).getBase(); // At runtime, thrownType might be instantiated by any // of thrownBase's subtypes, so: if (h.canStoreType(thrownBase, catcher) || h.canStoreType(catcher, thrownBase)) { return true; } } } return false; } }
private void iterateAllMethods() { // Find methods Iterator<SootClass> getClassesIt = Scene.v().getApplicationClasses().iterator(); while (getClassesIt.hasNext()) { SootClass appClass = getClassesIt.next(); Iterator<SootMethod> getMethodsIt = appClass.getMethods().iterator(); while (getMethodsIt.hasNext()) { SootMethod method = getMethodsIt.next(); analyzeMethod(method); } } }
public void outAFieldSignature(AFieldSignature node) { String className, fieldName; Type t; fieldName = (String) mProductions.removeLast(); t = (Type) mProductions.removeLast(); className = (String) mProductions.removeLast(); SootClass cl = mResolver.makeClassRef(className); SootFieldRef field = Scene.v().makeFieldRef(cl, fieldName, t, false); mProductions.addLast(field); }
/* method_signature = cmplt [class_name]:class_name [first]:colon type [method_name]:name l_paren parameter_list? r_paren cmpgt; */ public void outAMethodSignature(AMethodSignature node) { String className, methodName; List parameterList = new ArrayList(); Type returnType; if (node.getParameterList() != null) parameterList = (List) mProductions.removeLast(); methodName = (String) mProductions.removeLast(); Type type = (Type) mProductions.removeLast(); className = (String) mProductions.removeLast(); SootClass sootClass = mResolver.makeClassRef(className); SootMethodRef sootMethod = Scene.v().makeMethodRef(sootClass, methodName, parameterList, type, false); mProductions.addLast(sootMethod); }
public void internalTransform(String phaseName, Map opts) { Iterator it = Scene.v().getApplicationClasses().iterator(); while (it.hasNext()) { SootClass sc = (SootClass) it.next(); // make map of first line to each method HashMap<Integer, SootMethod> lineToMeth = new HashMap<Integer, SootMethod>(); Iterator methIt = sc.getMethods().iterator(); while (methIt.hasNext()) { SootMethod meth = (SootMethod) methIt.next(); if (!meth.isConcrete()) continue; Body body = meth.retrieveActiveBody(); Stmt s = (Stmt) body.getUnits().getFirst(); while (s instanceof IdentityStmt) { s = (Stmt) body.getUnits().getSuccOf(s); } if (s.hasTag("LineNumberTag")) { LineNumberTag tag = (LineNumberTag) s.getTag("LineNumberTag"); lineToMeth.put(new Integer(tag.getLineNumber()), meth); } } Iterator methIt2 = sc.getMethods().iterator(); while (methIt2.hasNext()) { SootMethod meth = (SootMethod) methIt2.next(); if (!meth.isConcrete()) continue; Body body = meth.retrieveActiveBody(); Stmt s = (Stmt) body.getUnits().getFirst(); while (s instanceof IdentityStmt) { s = (Stmt) body.getUnits().getSuccOf(s); } if (s.hasTag("LineNumberTag")) { LineNumberTag tag = (LineNumberTag) s.getTag("LineNumberTag"); int line_num = tag.getLineNumber() - 1; // already taken if (lineToMeth.containsKey(new Integer(line_num))) { meth.addTag(new LineNumberTag(line_num + 1)); } // still available - so use it for this meth else { meth.addTag(new LineNumberTag(line_num)); } } } } }
public void outAStaticInvokeExpr(AStaticInvokeExpr node) { List args; if (node.getArgList() != null) args = (List) mProductions.removeLast(); else args = new ArrayList(); SootMethodRef method = (SootMethodRef) mProductions.removeLast(); method = Scene.v() .makeMethodRef( method.declaringClass(), method.name(), method.parameterTypes(), method.returnType(), true); mProductions.addLast(Jimple.v().newStaticInvokeExpr(method, args)); }
protected void internalTransform(String phaseName, Map options) { if (Options.v().verbose()) G.v().out.println("Transforming all classes in the Scene to Shimple..."); // *** FIXME: Add debug output to indicate which class/method is being shimplified. // *** FIXME: Is ShimpleTransformer the right solution? The call graph may deem // some classes unreachable. Iterator classesIt = Scene.v().getClasses().iterator(); while (classesIt.hasNext()) { SootClass sClass = (SootClass) classesIt.next(); if (sClass.isPhantom()) continue; Iterator methodsIt = sClass.getMethods().iterator(); while (methodsIt.hasNext()) { SootMethod method = (SootMethod) methodsIt.next(); if (!method.isConcrete()) continue; if (method.hasActiveBody()) { Body body = method.getActiveBody(); ShimpleBody sBody = null; if (body instanceof ShimpleBody) { sBody = (ShimpleBody) body; if (!sBody.isSSA()) sBody.rebuild(); } else { sBody = Shimple.v().newBody(body); } method.setActiveBody(sBody); } else { MethodSource ms = new ShimpleMethodSource(method.getSource()); method.setSource(ms); } } } }
/** Prints the given <code>JimpleBody</code> to the specified <code>PrintWriter</code>. */ private void printStatementsInBody( Body body, java.io.PrintWriter out, LabeledUnitPrinter up, UnitGraph unitGraph) { Chain units = body.getUnits(); Iterator unitIt = units.iterator(); Unit currentStmt = null, previousStmt; while (unitIt.hasNext()) { previousStmt = currentStmt; currentStmt = (Unit) unitIt.next(); // Print appropriate header. { // Put an empty line if the previous node was a branch node, the current node is a join node // or the previous statement does not have body statement as a successor, or if // body statement has a label on it if (currentStmt != units.getFirst()) { if (unitGraph.getSuccsOf(previousStmt).size() != 1 || unitGraph.getPredsOf(currentStmt).size() != 1 || up.labels().containsKey(currentStmt)) { up.newline(); } else { // Or if the previous node does not have body statement as a successor. List succs = unitGraph.getSuccsOf(previousStmt); if (succs.get(0) != currentStmt) { up.newline(); } } } if (up.labels().containsKey(currentStmt)) { up.unitRef(currentStmt, true); up.literal(":"); up.newline(); } if (up.references().containsKey(currentStmt)) { up.unitRef(currentStmt, false); } } up.startUnit(currentStmt); currentStmt.toString(up); up.endUnit(currentStmt); up.literal(";"); up.newline(); // only print them if not generating attributes files // because they mess up line number // if (!addJimpleLn()) { if (Options.v().print_tags_in_output()) { Iterator tagIterator = currentStmt.getTags().iterator(); while (tagIterator.hasNext()) { Tag t = (Tag) tagIterator.next(); up.noIndent(); up.literal("/*"); up.literal(t.toString()); up.literal("*/"); up.newline(); } /*Iterator udIt = currentStmt.getUseAndDefBoxes().iterator(); while (udIt.hasNext()) { ValueBox temp = (ValueBox)udIt.next(); Iterator vbtags = temp.getTags().iterator(); while (vbtags.hasNext()) { Tag t = (Tag) vbtags.next(); up.noIndent(); up.literal("VB Tag: "+t.toString()); up.newline(); } }*/ } } out.print(up.toString()); if (addJimpleLn()) { setJimpleLnNum(up.getPositionTagger().getEndLn()); } // Print out exceptions { Iterator trapIt = body.getTraps().iterator(); if (trapIt.hasNext()) { out.println(); incJimpleLnNum(); } while (trapIt.hasNext()) { Trap trap = (Trap) trapIt.next(); out.println( " catch " + Scene.v().quotedNameOf(trap.getException().getName()) + " from " + up.labels().get(trap.getBeginUnit()) + " to " + up.labels().get(trap.getEndUnit()) + " with " + up.labels().get(trap.getHandlerUnit()) + ";"); incJimpleLnNum(); } } }
/** * Returns a <code>ThrowableSet</code> which contains all the exceptions in <code>addedExceptions * </code> in addition to those in this <code>ThrowableSet</code>. * * @param addedExceptions a set of {@link RefLikeType} and {@link AnySubType} objects to be added * to the types included in this <code>ThrowableSet</code>. * @return a set containing all the <code>addedExceptions</code> as well as the exceptions in this * set. */ private ThrowableSet add(Set addedExceptions) { Set resultSet = new HashSet(this.exceptionsIncluded); int changes = 0; FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy(); // This algorithm is O(n m), where n and m are the sizes of the // two sets, so hope that the sets are small. for (Iterator i = addedExceptions.iterator(); i.hasNext(); ) { RefLikeType newType = (RefLikeType) i.next(); if (!resultSet.contains(newType)) { boolean addNewType = true; if (newType instanceof RefType) { for (Iterator j = resultSet.iterator(); j.hasNext(); ) { RefLikeType incumbentType = (RefLikeType) j.next(); if (incumbentType instanceof RefType) { if (newType == incumbentType) { // assertion failure. throw new IllegalStateException( "ThrowableSet.add(Set): resultSet.contains() failed to screen duplicate RefType " + newType); } } else if (incumbentType instanceof AnySubType) { RefType incumbentBase = ((AnySubType) incumbentType).getBase(); if (hierarchy.canStoreType(newType, incumbentBase)) { // No need to add this class. addNewType = false; } } else { // assertion failure. throw new IllegalStateException( "ThrowableSet.add(Set): incumbent Set element " + incumbentType + " is neither a RefType nor an AnySubType."); } } } else if (newType instanceof AnySubType) { RefType newBase = ((AnySubType) newType).getBase(); for (Iterator j = resultSet.iterator(); j.hasNext(); ) { RefLikeType incumbentType = (RefLikeType) j.next(); if (incumbentType instanceof RefType) { RefType incumbentBase = (RefType) incumbentType; if (hierarchy.canStoreType(incumbentBase, newBase)) { j.remove(); changes++; } } else if (incumbentType instanceof AnySubType) { RefType incumbentBase = ((AnySubType) incumbentType).getBase(); if (newBase == incumbentBase) { // assertion failure. throw new IllegalStateException( "ThrowableSet.add(Set): resultSet.contains() failed to screen duplicate AnySubType " + newBase); } else if (hierarchy.canStoreType(incumbentBase, newBase)) { j.remove(); changes++; } else if (hierarchy.canStoreType(newBase, incumbentBase)) { // No need to add this class. addNewType = false; } } else { // assertion failure. throw new IllegalStateException( "ThrowableSet.add(Set): old Set element " + incumbentType + " is neither a RefType nor an AnySubType."); } } } else { // assertion failure. throw new IllegalArgumentException( "ThrowableSet.add(Set): new Set element " + newType + " is neither a RefType nor an AnySubType."); } if (addNewType) { changes++; resultSet.add(newType); } } } ThrowableSet result = null; if (changes > 0) { result = Manager.v().registerSetIfNew(resultSet, this.exceptionsExcluded); } else { result = this; } return result; }
/** * Returns a <code>ThrowableSet</code> which contains <code>e</code> and all of its subclasses as * well as the exceptions in this set. * * <p><code>e</code> should be an instance of {@link AnySubType} if you know that the compile-time * type of the exception you are representing is <code>e</code>, but the exception may be * instantiated at run-time by a subclass of <code>e</code>. * * <p>For example, if you were recording the type of the exception thrown by * * <pre> * catch (IOException e) { * throw e; * } * </pre> * * you would call * * <pre> * <code>add(AnySubtype.v(Scene.v().getRefType("java.lang.Exception.IOException")))</code> * </pre> * * since the handler might rethrow any subclass of <code>IOException</code>. * * @param e represents a subtree of the exception class hierarchy to add to this set. * @return a set containing <code>e</code> and all its subclasses, as well as the exceptions * represented by this set. * @throws ThrowableSet.AlreadyHasExclusionsException if this <code>ThrowableSet</code> is the * result of a {@link #whichCatchableAs(RefType)} operation and, thus, unable to represent the * addition of <code>e</code>. */ public ThrowableSet add(AnySubType e) throws ThrowableSet.AlreadyHasExclusionsException { if (INSTRUMENTING) { Manager.v().addsOfAnySubType++; } ThrowableSet result = getMemoizedAdds(e); if (result != null) { if (INSTRUMENTING) { Manager.v().addsInclusionFromMemo++; Manager.v().addsExclusionWithoutSearch++; } return result; } else { FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy(); RefType newBase = e.getBase(); if (INSTRUMENTING) { if (exceptionsExcluded.size() != 0) { Manager.v().addsExclusionWithSearch++; } else { Manager.v().addsExclusionWithoutSearch++; } } for (Iterator i = exceptionsExcluded.iterator(); i.hasNext(); ) { RefType exclusionBase = ((AnySubType) i.next()).getBase(); if (hierarchy.canStoreType(newBase, exclusionBase) || hierarchy.canStoreType(exclusionBase, newBase)) { if (INSTRUMENTING) { // To ensure that the subcategories total properly: Manager.v().addsInclusionInterrupted++; } throw new AlreadyHasExclusionsException( "ThrowableSet.add(" + e.toString() + ") to the set [ " + this.toString() + "] where " + exclusionBase.toString() + " is excluded."); } } if (this.exceptionsIncluded.contains(e)) { if (INSTRUMENTING) { Manager.v().addsInclusionFromMap++; } return this; } else { if (INSTRUMENTING) { Manager.v().addsInclusionFromSearch++; } int changes = 0; boolean addNewException = true; Set resultSet = new HashSet(); for (Iterator i = this.exceptionsIncluded.iterator(); i.hasNext(); ) { RefLikeType incumbent = (RefLikeType) i.next(); if (incumbent instanceof RefType) { if (hierarchy.canStoreType(incumbent, newBase)) { // Omit incumbent from result. changes++; } else { resultSet.add(incumbent); } } else if (incumbent instanceof AnySubType) { RefType incumbentBase = ((AnySubType) incumbent).getBase(); // We have to use the base types in these hierarchy calls // because we want to know if _all_ possible // types represented by e can be represented by // the incumbent, or vice versa. if (hierarchy.canStoreType(newBase, incumbentBase)) { addNewException = false; resultSet.add(incumbent); } else if (hierarchy.canStoreType(incumbentBase, newBase)) { // Omit incumbent from result; changes++; } else { resultSet.add(incumbent); } } else { // assertion failure. throw new IllegalStateException( "ThrowableSet.add(AnySubType): Set element " + incumbent.toString() + " is neither a RefType nor an AnySubType."); } } if (addNewException) { resultSet.add(e); changes++; } if (changes > 0) { result = Manager.v().registerSetIfNew(resultSet, this.exceptionsExcluded); } else { result = this; } memoizedAdds.put(e, result); return result; } } }
/** * Returns a <code>ThrowableSet</code> which contains <code>e</code> in addition to the exceptions * in this <code>ThrowableSet</code>. * * <p>Add <code>e</code> as a {@link RefType} when you know that the run-time class of the * exception you are representing is necessarily <code>e</code> and cannot be a subclass of <code> * e</code>. * * <p>For example, if you were recording the type of the exception thrown by * * <pre> * throw new IOException("Permission denied"); * </pre> * * you would call * * <pre> * <code>add(Scene.v().getRefType("java.lang.Exception.IOException"))</code> * </pre> * * since the class of the exception is necessarily <code>IOException</code>. * * @param e the exception class * @return a set containing <code>e</code> as well as the exceptions in this set. * @throws {@link ThrowableSet.IllegalStateException} if this <code>ThrowableSet</code> is the * result of a {@link #whichCatchableAs(RefType)} operation and, thus, unable to represent the * addition of <code>e</code>. */ public ThrowableSet add(RefType e) throws ThrowableSet.AlreadyHasExclusionsException { if (INSTRUMENTING) { Manager.v().addsOfRefType++; } if (this.exceptionsIncluded.contains(e)) { if (INSTRUMENTING) { Manager.v().addsInclusionFromMap++; Manager.v().addsExclusionWithoutSearch++; } return this; } else { ThrowableSet result = getMemoizedAdds(e); if (result != null) { if (INSTRUMENTING) { Manager.v().addsInclusionFromMemo++; Manager.v().addsExclusionWithoutSearch++; } return result; } else { if (INSTRUMENTING) { Manager.v().addsInclusionFromSearch++; if (exceptionsExcluded.size() != 0) { Manager.v().addsExclusionWithSearch++; } else { Manager.v().addsExclusionWithoutSearch++; } } FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy(); for (Iterator i = exceptionsExcluded.iterator(); i.hasNext(); ) { RefType exclusionBase = ((AnySubType) i.next()).getBase(); if (hierarchy.canStoreType(e, exclusionBase)) { throw new AlreadyHasExclusionsException( "ThrowableSet.add(RefType): adding" + e.toString() + " to the set [ " + this.toString() + "] where " + exclusionBase.toString() + " is excluded."); } } for (Iterator i = exceptionsIncluded.iterator(); i.hasNext(); ) { RefLikeType incumbent = (RefLikeType) i.next(); if (incumbent instanceof AnySubType) { // Need to use incumbent.getBase() because // hierarchy.canStoreType() assumes that parent // is not an AnySubType. RefType incumbentBase = ((AnySubType) incumbent).getBase(); if (hierarchy.canStoreType(e, incumbentBase)) { memoizedAdds.put(e, this); return this; } } else if (!(incumbent instanceof RefType)) { // assertion failure. throw new IllegalStateException( "ThrowableSet.add(RefType): Set element " + incumbent.toString() + " is neither a RefType nor an AnySubType."); } } Set resultSet = new HashSet(this.exceptionsIncluded); resultSet.add(e); result = Manager.v().registerSetIfNew(resultSet, this.exceptionsExcluded); memoizedAdds.put(e, result); return result; } } }
/** * Constructs a <code>ThrowableSet.Manager</code> for inclusion in Soot's global variable * manager, {@link G}. * * @param g guarantees that the constructor may only be called from {@link Singletons}. */ public Manager(Singletons.Global g) { // First ensure the Exception classes are represented in Soot. // Runtime errors: RUNTIME_EXCEPTION = Scene.v().getRefType("java.lang.RuntimeException"); ARITHMETIC_EXCEPTION = Scene.v().getRefType("java.lang.ArithmeticException"); ARRAY_STORE_EXCEPTION = Scene.v().getRefType("java.lang.ArrayStoreException"); CLASS_CAST_EXCEPTION = Scene.v().getRefType("java.lang.ClassCastException"); ILLEGAL_MONITOR_STATE_EXCEPTION = Scene.v().getRefType("java.lang.IllegalMonitorStateException"); INDEX_OUT_OF_BOUNDS_EXCEPTION = Scene.v().getRefType("java.lang.IndexOutOfBoundsException"); ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION = Scene.v().getRefType("java.lang.ArrayIndexOutOfBoundsException"); NEGATIVE_ARRAY_SIZE_EXCEPTION = Scene.v().getRefType("java.lang.NegativeArraySizeException"); NULL_POINTER_EXCEPTION = Scene.v().getRefType("java.lang.NullPointerException"); INSTANTIATION_ERROR = Scene.v().getRefType("java.lang.InstantiationError"); EMPTY = registerSetIfNew(null, null); Set allThrowablesSet = new HashSet(); allThrowablesSet.add(AnySubType.v(Scene.v().getRefType("java.lang.Throwable"))); ALL_THROWABLES = registerSetIfNew(allThrowablesSet, null); Set vmErrorSet = new HashSet(); vmErrorSet.add(Scene.v().getRefType("java.lang.InternalError")); vmErrorSet.add(Scene.v().getRefType("java.lang.OutOfMemoryError")); vmErrorSet.add(Scene.v().getRefType("java.lang.StackOverflowError")); vmErrorSet.add(Scene.v().getRefType("java.lang.UnknownError")); // The Java library's deprecated Thread.stop(Throwable) method // would actually allow _any_ Throwable to be delivered // asynchronously, not just java.lang.ThreadDeath. vmErrorSet.add(Scene.v().getRefType("java.lang.ThreadDeath")); VM_ERRORS = registerSetIfNew(vmErrorSet, null); Set resolveClassErrorSet = new HashSet(); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.ClassCircularityError")); // We add AnySubType(ClassFormatError) so that we can // avoid adding its subclass, // UnsupportedClassVersionError, explicitly. This is a // hack to allow Soot to analyze older class libraries // (UnsupportedClassVersionError was added in JDK 1.2). // TODO: The class "ClassFormatError" does not exist in TakaTuka so our best guess is that // commenting the following line out won't affect the analysis in any way. But this // is a "fault point" to look at if any problems arise in the future. // resolveClassErrorSet.add(AnySubType.v(Scene.v().getRefType("java.lang.ClassFormatError"))); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.IllegalAccessError")); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.IncompatibleClassChangeError")); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.LinkageError")); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.NoClassDefFoundError")); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.VerifyError")); RESOLVE_CLASS_ERRORS = registerSetIfNew(resolveClassErrorSet, null); Set resolveFieldErrorSet = new HashSet(resolveClassErrorSet); resolveFieldErrorSet.add(Scene.v().getRefType("java.lang.NoSuchFieldError")); RESOLVE_FIELD_ERRORS = registerSetIfNew(resolveFieldErrorSet, null); Set resolveMethodErrorSet = new HashSet(resolveClassErrorSet); resolveMethodErrorSet.add(Scene.v().getRefType("java.lang.AbstractMethodError")); resolveMethodErrorSet.add(Scene.v().getRefType("java.lang.NoSuchMethodError")); resolveMethodErrorSet.add(Scene.v().getRefType("java.lang.UnsatisfiedLinkError")); RESOLVE_METHOD_ERRORS = registerSetIfNew(resolveMethodErrorSet, null); // The static initializers of a newly loaded class might // throw any Error (if they threw an Exception---even a // RuntimeException---it would be replaced by an // ExceptionInInitializerError): // Set initializationErrorSet = new HashSet(); initializationErrorSet.add(AnySubType.v(Scene.v().getRefType("java.lang.Error"))); INITIALIZATION_ERRORS = registerSetIfNew(initializationErrorSet, null); }
public static boolean mayBaseAtSameLocation(Local base1, Local base2) { PointsToAnalysis pta = Scene.v().getPointsToAnalysis(); PointsToSet pts1 = pta.reachingObjects(base1); PointsToSet pts2 = pta.reachingObjects(base2); return pts1.hasNonEmptyIntersection(pts2); }
public void printTo(SootClass cl, PrintWriter out) { // add jimple line number tags setJimpleLnNum(1); // Print class name + modifiers { StringTokenizer st = new StringTokenizer(Modifier.toString(cl.getModifiers())); while (st.hasMoreTokens()) { String tok = (String) st.nextToken(); if (cl.isInterface() && tok.equals("abstract")) continue; out.print(tok + " "); } String classPrefix = ""; if (!cl.isInterface()) { classPrefix = classPrefix + " class"; classPrefix = classPrefix.trim(); } out.print(classPrefix + " " + Scene.v().quotedNameOf(cl.getName()) + ""); } // Print extension { if (cl.hasSuperclass()) out.print(" extends " + Scene.v().quotedNameOf(cl.getSuperclass().getName()) + ""); } // Print interfaces { Iterator interfaceIt = cl.getInterfaces().iterator(); if (interfaceIt.hasNext()) { out.print(" implements "); out.print("" + Scene.v().quotedNameOf(((SootClass) interfaceIt.next()).getName()) + ""); while (interfaceIt.hasNext()) { out.print(","); out.print(" " + Scene.v().quotedNameOf(((SootClass) interfaceIt.next()).getName()) + ""); } } } out.println(); incJimpleLnNum(); /* if (!addJimpleLn()) { Iterator clTagsIt = cl.getTags().iterator(); while (clTagsIt.hasNext()) { final Tag t = (Tag)clTagsIt.next(); out.println(t); } }*/ out.println("{"); incJimpleLnNum(); if (Options.v().print_tags_in_output()) { Iterator cTagIterator = cl.getTags().iterator(); while (cTagIterator.hasNext()) { Tag t = (Tag) cTagIterator.next(); out.print("/*"); out.print(t.toString()); out.println("*/"); } } // Print fields { Iterator fieldIt = cl.getFields().iterator(); if (fieldIt.hasNext()) { while (fieldIt.hasNext()) { SootField f = (SootField) fieldIt.next(); if (f.isPhantom()) continue; if (Options.v().print_tags_in_output()) { Iterator fTagIterator = f.getTags().iterator(); while (fTagIterator.hasNext()) { Tag t = (Tag) fTagIterator.next(); out.print("/*"); out.print(t.toString()); out.println("*/"); } } out.println(" " + f.getDeclaration() + ";"); if (addJimpleLn()) { setJimpleLnNum(addJimpleLnTags(getJimpleLnNum(), f)); } // incJimpleLnNum(); } } } // Print methods { Iterator methodIt = cl.methodIterator(); if (methodIt.hasNext()) { if (cl.getMethodCount() != 0) { out.println(); incJimpleLnNum(); } while (methodIt.hasNext()) { SootMethod method = (SootMethod) methodIt.next(); if (method.isPhantom()) continue; if (!Modifier.isAbstract(method.getModifiers()) && !Modifier.isNative(method.getModifiers())) { if (!method.hasActiveBody()) throw new RuntimeException("method " + method.getName() + " has no active body!"); else if (Options.v().print_tags_in_output()) { Iterator mTagIterator = method.getTags().iterator(); while (mTagIterator.hasNext()) { Tag t = (Tag) mTagIterator.next(); out.print("/*"); out.print(t.toString()); out.println("*/"); } } printTo(method.getActiveBody(), out); if (methodIt.hasNext()) { out.println(); incJimpleLnNum(); } } else { if (Options.v().print_tags_in_output()) { Iterator mTagIterator = method.getTags().iterator(); while (mTagIterator.hasNext()) { Tag t = (Tag) mTagIterator.next(); out.print("/*"); out.print(t.toString()); out.println("*/"); } } out.print(" "); out.print(method.getDeclaration()); out.println(";"); incJimpleLnNum(); if (methodIt.hasNext()) { out.println(); incJimpleLnNum(); } } } } } out.println("}"); incJimpleLnNum(); }
public void outASigFieldRef(ASigFieldRef node) { SootFieldRef field = (SootFieldRef) mProductions.removeLast(); field = Scene.v().makeFieldRef(field.declaringClass(), field.name(), field.type(), true); mProductions.addLast(Jimple.v().newStaticFieldRef(field)); }
/* * Generates the sequence of instructions needed to instrument ifStmtUnit * * @param ifStmtUnit: the unit to be instrumented * @return A Chain of Units that represent the instrumentation of ifStmtUnit */ private static Chain<Unit> generateInstrumentationUnits(Unit ifStmtUnit, Body b) { AbstractBinopExpr expression = (AbstractBinopExpr) ((JIfStmt) ifStmtUnit).getCondition(); // implementation of AbstractBinopExpr Value operand1 = expression.getOp1(); Value operand2 = expression.getOp2(); JimpleLocal op1Local = (JimpleLocal) operand1; // Local localOperand = Jimple.v().newLocal("op1", operand1.getType()); // b.getLocals().add(localOperand); // We need to use these operand as Locals or constants /** * JimpleLocal test; if(operand1 instanceof soot.jimple.internal.JimpleLocal) test = * (JimpleLocal)operand1; else test = null; */ String op = expression.getClass().toString(); Chain<Unit> resultUnits = new HashChain<Unit>(); Local tmpRef; // Add locals directely on the top of the body, java.io.printStream tmpRef tmpRef = Jimple.v().newLocal("tmpRef", RefType.v("java.io.PrintStream")); b.getLocals().addFirst(tmpRef); // add "tmpRef = java.lang.System.out" resultUnits.add( Jimple.v() .newAssignStmt( tmpRef, Jimple.v() .newStaticFieldRef( Scene.v() .getField("<java.lang.System: java.io.PrintStream out>") .makeRef()))); { SootMethod toCall = Scene.v().getMethod("<java.io.PrintStream: void println(java.lang.String)>"); resultUnits.add( Jimple.v() .newInvokeStmt( Jimple.v() .newVirtualInvokeExpr( tmpRef, toCall.makeRef(), StringConstant.v("Operande 1: ")))); resultUnits.add( Jimple.v() .newInvokeStmt( Jimple.v() .newVirtualInvokeExpr( tmpRef, toCall.makeRef(), StringConstant.v(operand1.getClass().toString())))); resultUnits.add( Jimple.v() .newInvokeStmt( Jimple.v() .newVirtualInvokeExpr( tmpRef, toCall.makeRef(), StringConstant.v("Operande 2:")))); resultUnits.add( Jimple.v() .newInvokeStmt( Jimple.v() .newVirtualInvokeExpr( tmpRef, toCall.makeRef(), StringConstant.v(operand2.getClass().toString())))); resultUnits.add( Jimple.v() .newInvokeStmt( Jimple.v() .newVirtualInvokeExpr( tmpRef, toCall.makeRef(), StringConstant.v("Operateur: ")))); resultUnits.add( Jimple.v() .newInvokeStmt( Jimple.v().newVirtualInvokeExpr(tmpRef, toCall.makeRef(), StringConstant.v(op)))); } return resultUnits; }
/** * Partitions the exceptions in this <code>ThrowableSet</code> into those which would be caught by * a handler with the passed <code>catch</code> parameter type and those which would not. * * @param catcher type of the handler parameter to be tested. * @return a pair of <code>ThrowableSet</code>s, one containing the types in this <code> * ThrowableSet</code> which would be be caught as <code>catcher</code> and the other * containing the types in this <code>ThrowableSet</code> which would not be caught as <code> * catcher</code>. */ public Pair whichCatchableAs(RefType catcher) { if (INSTRUMENTING) { Manager.v().removesOfAnySubType++; } FastHierarchy h = Scene.v().getOrMakeFastHierarchy(); Set caughtIncluded = null; Set caughtExcluded = null; Set uncaughtIncluded = null; Set uncaughtExcluded = null; if (INSTRUMENTING) { Manager.v().removesFromSearch++; } for (Iterator i = exceptionsExcluded.iterator(); i.hasNext(); ) { AnySubType exclusion = (AnySubType) i.next(); RefType exclusionBase = exclusion.getBase(); if (h.canStoreType(catcher, exclusionBase)) { // Because the add() operations ban additions to sets // with exclusions, we can be sure no types in this are // caught by catcher. return new Pair(ThrowableSet.Manager.v().EMPTY, this); } else if (h.canStoreType(exclusionBase, catcher)) { // exclusion wouldn't be in exceptionsExcluded if one // of its supertypes were not in exceptionsIncluded, // so we know the next loop will add either that supertype // or catcher to caughtIncluded. Thus: caughtExcluded = addExceptionToSet(exclusion, caughtExcluded); } else { uncaughtExcluded = addExceptionToSet(exclusion, uncaughtExcluded); } } for (Iterator i = exceptionsIncluded.iterator(); i.hasNext(); ) { RefLikeType inclusion = (RefLikeType) i.next(); if (inclusion instanceof RefType) { if (h.canStoreType(inclusion, catcher)) { caughtIncluded = addExceptionToSet(inclusion, caughtIncluded); } else { uncaughtIncluded = addExceptionToSet(inclusion, uncaughtIncluded); } } else { RefType base = ((AnySubType) inclusion).getBase(); if (h.canStoreType(base, catcher)) { // All subtypes of base will be caught. Any exclusions // will already have been copied to caughtExcluded by // the preceding loop. caughtIncluded = addExceptionToSet(inclusion, caughtIncluded); } else if (h.canStoreType(catcher, base)) { // Some subtypes of base will be caught, and // we know that not all of those catchable subtypes // are among exceptionsExcluded, since in that case we // would already have returned from within the // preceding loop. So, remove AnySubType(catcher) // from the uncaught types. uncaughtIncluded = addExceptionToSet(inclusion, uncaughtIncluded); uncaughtExcluded = addExceptionToSet(AnySubType.v(catcher), uncaughtExcluded); caughtIncluded = addExceptionToSet(AnySubType.v(catcher), caughtIncluded); // Any already excluded subtypes of inclusion // which are subtypes of catcher will have been // added to caughtExcluded by the previous loop. } else { uncaughtIncluded = addExceptionToSet(inclusion, uncaughtIncluded); } } } ThrowableSet caughtSet = Manager.v().registerSetIfNew(caughtIncluded, caughtExcluded); ThrowableSet uncaughtSet = Manager.v().registerSetIfNew(uncaughtIncluded, uncaughtExcluded); return new Pair(caughtSet, uncaughtSet); }
/** 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))); } } }
/** Constructs a JimpleLocal of the given name and type. */ public JimpleLocal(String name, Type t) { this.name = name.intern(); this.type = t; Scene.v().getLocalNumberer().add(this); }