public static void cleanClass(SootClass s) { List methods = s.getMethods(); Iterator it = methods.iterator(); while (it.hasNext()) { removeReturn((SootMethod) it.next()); } }
/** Returns a list of all concrete methods of all application classes. */ public List<SootMethod> methodsOfApplicationClasses() { List<SootMethod> ret = new ArrayList<SootMethod>(); for (Iterator<SootClass> clIt = Scene.v().getApplicationClasses().iterator(); clIt.hasNext();) { final SootClass cl = clIt.next(); for (Iterator<SootMethod> mIt = cl.getMethods().iterator(); mIt.hasNext();) { final SootMethod m = mIt.next(); if (m.isConcrete()) { ret.add(m); } } } return ret; }
/** Returns a list of all constructors. */ public List<SootMethod> allInits() { List<SootMethod> ret = new ArrayList<SootMethod>(); for (Iterator<SootClass> clIt = Scene.v().getClasses().iterator(); clIt.hasNext();) { final SootClass cl = clIt.next(); for (SootMethod m : cl.getMethods()) { if (m.getName().equals("<init>")) { ret.add(m); } } } return ret; }
private void handleFieldInits() { if ((fieldInits != null) || (initializerBlocks != null)) { Iterator methodsIt = sootClass.getMethods().iterator(); while (methodsIt.hasNext()) { soot.SootMethod next = (soot.SootMethod) methodsIt.next(); if (next.getName().equals("<init>")) { soot.javaToJimple.PolyglotMethodSource src = (soot.javaToJimple.PolyglotMethodSource) next.getSource(); src.setInitializerBlocks(initializerBlocks); src.setFieldInits(fieldInits); } } } }
/** * Checks that method is declared in class. We assume that method is declared in class if class contains method with the * same name and the same number of arguments. The exact types are not compared. * * @param sootClass * the class to search in * @param methodName * the searching method name * @param parameterTypes * the searching method parameters * @return {@code true} if passed class contains similar method; {@code false} otherwise */ private boolean isDeclared(SootClass sootClass, String methodName, List<Type> parameterTypes) { for (SootMethod declared : sootClass.getMethods()) { if (declared.getName().equals(methodName) && declared.getParameterCount() == parameterTypes.size()) { return true; } } return false; }
private SootMethod getFirstMethod(SootClass sc) { ArrayList paramTypes = new ArrayList(); paramTypes.add(soot.ArrayType.v(soot.RefType.v("java.lang.String"), 1)); SootMethod sm = sc.getMethodUnsafe("main", paramTypes, soot.VoidType.v()); if (sm != null) { return sm; } else { return (SootMethod) sc.getMethods().get(0); } }
private void addQualifierRefToInit(polyglot.types.Type type) { soot.Type sootType = Util.getSootType(type); Iterator it = sootClass.getMethods().iterator(); while (it.hasNext()) { soot.SootMethod meth = (soot.SootMethod) it.next(); if (meth.getName().equals("<init>")) { List newParams = new ArrayList(); newParams.add(sootType); newParams.addAll(meth.getParameterTypes()); meth.setParameterTypes(newParams); meth.addTag(new soot.tagkit.QualifyingTag()); } } }
/** * Returns true if clients can instantiate the given class. This holds if the given class is actually an interface, or if * it contains a visible constructor. If the class is an inner class, then the enclosing classes must be instantiable as * well. */ private static boolean clientCanInstantiate(SootClass cPrime) { // subtypes of interface types can always be instantiated if (cPrime.isInterface()) { return true; } for (SootMethod m : cPrime.getMethods()) { if (m.getName().equals(SootMethod.constructorName)) { if (visible(m)) { return true; } } } return false; }
private void dumpAllBodies(String baseName, boolean deleteGraphFiles) { List<SootClass> classes = Scene.v().getClasses(SootClass.BODIES); for (SootClass cls : classes) { for (Iterator m = cls.getMethods().iterator(); m.hasNext();) { SootMethod method = (SootMethod) m.next(); if (method.hasActiveBody()) { Body body = method.getActiveBody(); if (deleteGraphFiles) { deleteOldGraphFiles(body, baseName); } dumpBody(body, baseName); } } } }
public static void retrieveAllNames() { if (namesHaveBeenRetrieved) { return; } // iterate through application classes, rename fields with junk for (SootClass c : soot.Scene.v().getApplicationClasses()) { nameList.add(c.getName()); for (SootMethod m : c.getMethods()) { nameList.add(m.getName()); } for (SootField m : c.getFields()) { nameList.add(m.getName()); } } namesHaveBeenRetrieved = true; }
@Override public void validate(SootClass sc, List<ValidationException> exceptions) { if (sc.isConcrete()) { for (SootMethod sm : sc.getMethods()) { for (Type tp : sm.getParameterTypes()) { if (tp == null) { exceptions.add(new ValidationException(sm, "Null parameter types are invalid")); } if (tp instanceof VoidType) { exceptions.add(new ValidationException(sm, "Void parameter types are invalid")); } if (!tp.isAllowedInFinalCode()) { exceptions.add(new ValidationException(sm, "Parameter type not allowed in final code")); } } } } }
private void addOuterClassThisRefToInit(polyglot.types.Type outerType) { soot.Type outerSootType = Util.getSootType(outerType); Iterator it = sootClass.getMethods().iterator(); while (it.hasNext()) { soot.SootMethod meth = (soot.SootMethod) it.next(); if (meth.getName().equals("<init>")) { List newParams = new ArrayList(); newParams.add(outerSootType); newParams.addAll(meth.getParameterTypes()); meth.setParameterTypes(newParams); meth.addTag(new soot.tagkit.EnclosingTag()); if (InitialResolver.v().getHasOuterRefInInit() == null) { InitialResolver.v().setHasOuterRefInInit(new ArrayList()); } InitialResolver.v().getHasOuterRefInInit().add(meth.getDeclaringClass().getType()); } } }
public void preJimplify() { boolean change = true; while (change) { change = false; for (Iterator<SootClass> cIt = new ArrayList<>(Scene.v().getClasses()).iterator(); cIt.hasNext();) { final SootClass c = cIt.next(); for (final SootMethod m : c.getMethods()) { if (!m.isConcrete()) { continue; } if (m.isNative()) { continue; } if (m.isPhantom()) { continue; } if (!m.hasActiveBody()) { change = true; m.retrieveActiveBody(); } } } } }
protected void handleClass(PrintWriter out, SootClass c) { for (SootMethod m : c.getMethods()) { if (!m.isConcrete()) { continue; } Body b = m.retrieveActiveBody(); Local[] sortedLocals = b.getLocals().toArray(new Local[b.getLocalCount()]); Arrays.sort(sortedLocals, new StringComparator<Local>()); for (Local l : sortedLocals) { out.println("V " + m + l); if (l.getType() instanceof RefLikeType) { Set<Type> types = pa.reachingObjects(l).possibleTypes(); Type[] sortedTypes = types.toArray(new Type[types.size()]); Arrays.sort(sortedTypes, new StringComparator<Type>()); for (Type type : sortedTypes) { out.println("T " + type); } } } } } }
@Override public void contructorNewInstance(SootMethod source, Stmt s) { if (options.safe_newinstance()) { for (SootMethod tgt : EntryPoints.v().allInits()) { addEdge(source, s, tgt, Kind.NEWINSTANCE); } } else { for (SootClass cls : Scene.v().dynamicClasses()) { for (SootMethod m : cls.getMethods()) { if (m.getName().equals("<init>")) { addEdge(source, s, m, Kind.NEWINSTANCE); } } } if (options.verbose()) { logger.warn("Method " + source + " is reachable, and calls Constructor.newInstance;" + " graph will be incomplete!" + " Use safe-newinstance option for a conservative result."); } } }
private void addFinals(polyglot.types.LocalInstance li, ArrayList<SootField> finalFields) { // add as param for init for (SootMethod meth : sootClass.getMethods()) { if (meth.getName().equals("<init>")) { List<soot.Type> newParams = new ArrayList<soot.Type>(); newParams.addAll(meth.getParameterTypes()); newParams.add(Util.getSootType(li.type())); meth.setParameterTypes(newParams); } } // add field soot.SootField sf = Scene.v().makeSootField("val$" + li.name(), Util.getSootType(li.type()), soot.Modifier.FINAL | soot.Modifier.PRIVATE); sootClass.addField(sf); finalFields.add(sf); sf.addTag(new soot.tagkit.SyntheticTag()); }
public static void retrieveAllBodies() { if (bodiesHaveBeenBuilt) { return; } // iterate through application classes, rename fields with junk for (SootClass c : soot.Scene.v().getApplicationClasses()) { for (SootMethod m : c.getMethods()) { if (!m.isConcrete()) { continue; } if (!m.hasActiveBody()) { m.retrieveActiveBody(); } } } bodiesHaveBeenBuilt = true; }
protected void handleClass(SootClass c) { boolean incedClasses = false; if (c.isConcrete()) { for (SootMethod m : c.getMethods()) { if (!m.isConcrete() && !m.isNative()) { continue; } totalMethods++; if (reachables.contains(m)) { MethodPAG mpag = MethodPAG.v(pag, m); mpag.build(); mpag.addToPAG(null); analyzedMethods++; if (!incedClasses) { incedClasses = true; classes++; } } } } }
/** * Collect tags from all fields and methods of <code>sc</code>. If <code>includeBodies</code> is true, then tags are also * collected from method bodies. * * @param sc * The class from which to collect the tags. */ public void collectTags(SootClass sc, boolean includeBodies) { // tag the class collectClassTags(sc); // tag fields for (SootField sf : sc.getFields()) { collectFieldTags(sf); } // tag methods for (SootMethod sm : sc.getMethods()) { collectMethodTags(sm); if (!includeBodies || !sm.hasActiveBody()) { continue; } Body b = sm.getActiveBody(); collectBodyTags(b); } }
protected void bringToSignaturesUnchecked(SootClass sc) { for (SootField f : sc.getFields()) { addToResolveWorklist(f.getType(), SootClass.HIERARCHY); } for (SootMethod m : sc.getMethods()) { addToResolveWorklist(m.getReturnType(), SootClass.HIERARCHY); for (Type ptype : m.getParameterTypes()) { addToResolveWorklist(ptype, SootClass.HIERARCHY); } List<SootClass> exceptions = m.getExceptionsUnsafe(); if (exceptions != null) { for (SootClass exception : exceptions) { addToResolveWorklist(exception, SootClass.HIERARCHY); } } } // Bring superclasses to signatures reResolveHierarchy(sc, SootClass.SIGNATURES); }