/** * Get astubx style method signature. {FullyQualifiedEnclosingType}: {UnqualifiedMethodReturnType} * {methodName} ([{UnqualifiedArgumentType}*]) * * @param mtd Method reference. * @return String Method signature. */ // TODO: handle generics and inner classes private static String getSignature(IMethod mtd) { String classType = mtd.getDeclaringClass().getName().toString().replaceAll("/", "\\.").substring(1); classType = classType.replaceAll("\\$", "\\."); // handle inner class String returnType = mtd.isInit() ? null : getSimpleTypeName(mtd.getReturnType()); String strArgTypes = ""; int argi = mtd.isStatic() ? 0 : 1; // Skip 'this' parameter for (; argi < mtd.getNumberOfParameters(); argi++) { strArgTypes += getSimpleTypeName(mtd.getParameterType(argi)); if (argi < mtd.getNumberOfParameters() - 1) strArgTypes += ", "; } return classType + ":" + (returnType == null ? "void " : returnType + " ") + mtd.getName().toString() + "(" + strArgTypes + ")"; } /**
if (!mtd.isPrivate() && !mtd.isAbstract() && !mtd.isNative() && !isAllPrimitiveTypes(mtd) && !mtd.getDeclaringClass() .getClassLoader() .getName() String sign = ""; if (mtd.getNumberOfParameters() > (mtd.isStatic() ? 0 : 1)) { if (!mtd.getReturnType().isPrimitiveType()) { if (analysisDriver == null) { analysisDriver = getAnalysisDriver(mtd, options, cache, cha);
/** * Checks if all parameters and return value of a method have primitive types. * * @param mtd Method. * @return boolean True if all parameters and return value are of primitive type, otherwise false. */ private static boolean isAllPrimitiveTypes(IMethod mtd) { if (!mtd.getReturnType().isPrimitiveType()) return false; for (int i = (mtd.isStatic() ? 0 : 1); i < mtd.getNumberOfParameters(); i++) { if (!mtd.getParameterType(i).isPrimitiveType()) return false; } return true; }
/** * The method this accessor reads the parameters from. */ public MethodReference forMethod() { if (this.mRef != null) { return this.mRef; } else { return this.method.getReference(); } }
/** * Return the method that m overrides, or null if none */ public static IMethod getOverriden(IMethod m) { IClass c = m.getDeclaringClass(); IClass parent = c.getSuperclass(); if (parent == null) { return null; } else { MethodReference ref = MethodReference.findOrCreate(parent.getReference(), m.getSelector()); IMethod m2 = m.getClassHierarchy().resolveMethod(ref); if (m2 != null && !m2.equals(m)) { return m2; } return null; } }
logger.trace("- signature: " + m.getSignature()); logger.trace("- descriptor: " + m.getDescriptor().toString()); for (int i = (m.isStatic()? 0 : 1) ; i < m.getNumberOfParameters(); i++) { boolean isAppClazz = false; if (m.getParameterType(i).getClassLoader().equals(ClassLoaderReference.Application)) { IClass ct = m.getClassHierarchy().lookupClass(m.getParameterType(i)); isAppClazz = ct == null || WalaUtils.isAppClass(ct); sb.append(isAppClazz? customTypeReplacement : m.getParameterType(i).getName().toString()); } else sb.append(m.getParameterType(i).getName().toString()); if (m.getReturnType().getClassLoader().equals(ClassLoaderReference.Application)) { IClass ct = m.getClassHierarchy().lookupClass(m.getReturnType()); sb.append(ct == null || WalaUtils.isAppClass(ct)? customTypeReplacement : m.getReturnType().getName().toString()); } else sb.append(m.getReturnType().getName().toString());
@Override @SuppressWarnings("deprecation") public CGNode findOrCreateNode(IMethod method, Context C) throws CancelException { assert C.equals(Everywhere.EVERYWHERE); assert !method.isAbstract(); CGNode n = getNode(method, C); if (n == null) { assert !isInitialized; n = makeNewNode(method, C); IMethod clinit = method.getDeclaringClass().getClassInitializer(); if (clinit != null && getNode(clinit, Everywhere.EVERYWHERE) == null) { CGNode cln = makeNewNode(clinit, Everywhere.EVERYWHERE); CGNode clinits = getFakeWorldClinitNode(); clinits.addTarget(CallSiteReference.make(clinitPC++, clinit.getReference(), IInvokeInstruction.Dispatch.STATIC), cln); } } return n; }
private static IMethod getCallTargetMethod(IMethod method) { if(method.getName().equals(JavaScriptMethods.ctorAtom)) { method = method.getDeclaringClass().getMethod(AstMethodReference.fnSelector); if(method != null) return method; } return method; }
private Collection<IMethod> lookupMethods(IClass c) { Collection<IMethod> matching = new LinkedList<>(); Atom atom = Atom.findOrCreateUnicodeAtom(memberName); Descriptor desc = descriptor == null ? null : Descriptor.findOrCreateUTF8(descriptor); Collection<? extends IMethod> allMethods = c.getAllMethods(); for(IMethod m: allMethods) { if(m.getName().equals(atom) && (desc == null || m.getDescriptor().equals(desc))) { matching.add(m); } } return matching; }
@Override public TypeReference[] getParameterTypes(int i) { assert i == 0; if (getMethod().isStatic()) { return new TypeReference[0]; } else { return new TypeReference[] { getMethod().getDeclaringClass().getReference() }; } }
public static CGNode findInstanceMethod(CallGraph cg, IClass declaringClass, Atom name, Descriptor args) { for (CGNode n : cg) { // System.err.println(n.getMethod().getDeclaringClass() + " " + // n.getMethod().getName() + " " + n.getMethod().getDescriptor()); if (n.getMethod().getDeclaringClass().equals(declaringClass) && n.getMethod().getName().equals(name) && n.getMethod().getDescriptor().equals(args)) { return n; } } Assertions.UNREACHABLE("failed to find method"); return null; }
private boolean isRelevantMethod(IMethod target) { return !target.isAbstract() && (!applicationOnly || cha.getScope().isApplicationLoader(target.getDeclaringClass().getClassLoader())); }
public static Set<IMethod> assertUserCodeReachable(CallGraph cg, Set<MethodReference> uncalled) { Set<IMethod> result = HashSetFactory.make(); for(IClass cls : Iterator2Iterable.make(cg.getClassHierarchy().getLoader(ClassLoaderReference.Application).iterateAllClasses())) { if (cls.isInterface()) { continue; } if (! cls.getName().toString().startsWith("Landroid") && ! cls.getName().toString().equals("Lde/ecspride/R$styleable")) { for(IMethod m : cls.getDeclaredMethods()) { if (!m.isInit() && !m.isAbstract() && !uncalled.contains(m.getReference())) { if (! cg.getNodes(m.getReference()).isEmpty()) { System.err.println("found " + m); } else { result.add(m); } } } } } return result; }
/** * @param method the method to be called for this entrypoint */ protected Entrypoint(IMethod method) { if (method == null) { throw new IllegalArgumentException("method is null"); } this.method = method; assert method.getDeclaringClass() != null : "null declaring class"; }
public static int getMethodAccessCode(IMethod m) { int res = 0x0; if (m == null) return res; if (m.isPublic()) { res |= JvmMethodAccessFlags.PUBLIC.getValue(); } else if (m.isProtected()) { res |= JvmMethodAccessFlags.PROTECTED.getValue(); } else if (m.isPrivate()) { res |= JvmMethodAccessFlags.PRIVATE.getValue(); } else { res |= JvmMethodAccessFlags.PACKAGE_PROTECTED.getValue(); } if (m.isStatic()) res |= JvmMethodAccessFlags.STATIC.getValue(); if (m.isFinal()) res |= JvmMethodAccessFlags.FINAL.getValue(); if (m.isAbstract()) res |= JvmMethodAccessFlags.ABSTRACT.getValue(); return res; }
/** * @return number of parameters to this method, including "this" */ public int getNumberOfParameters() { return method.getNumberOfParameters(); }
/** * @return the receiver class for this method. */ private IClass getReceiverClass(IMethod method) { TypeReference formalType = method.getParameterType(0); IClass C = cha.lookupClass(formalType); if (method.isStatic()) { Assertions.UNREACHABLE("asked for receiver of static method " + method); } if (C == null) { Assertions.UNREACHABLE("no class found for " + formalType + " recv of " + method); } return C; }
/** * Get all non-constructor, non-class-initializer methods declared by a class * if their name is equal to the specified name. * @param cls the class * @param name the name */ private static Collection<IMethod> getDeclaredNormalMethods(IClass cls,Atom name) { Collection<IMethod> result = HashSetFactory.make(); for (IMethod m : cls.getDeclaredMethods()) { if (!m.isInit() && !m.isClinit() && m.getSelector().getName().equals(name)) { result.add(m); } } return result; }
public static Iterable<Entrypoint> makePrimordialPublicEntrypoints(ClassHierarchy cha, String pkg) { final HashSet<Entrypoint> result = HashSetFactory.make(); for (IClass clazz : cha) { if (clazz.getName().toString().indexOf(pkg) != -1 && !clazz.isInterface() && !clazz.isAbstract()) { for (IMethod method : clazz.getDeclaredMethods()) { if (method.isPublic() && !method.isAbstract()) { System.out.println("Entry:" + method.getReference()); result.add(new DefaultEntrypoint(method, cha)); } } } } return result::iterator; }
/** * Get the first paramNo of a given type. * * @see com.ibm.wala.util.ssa.ParameterAccessor */ private static int firstOf(TypeName type, IMethod inCallTo) { for (int i = 0; i < inCallTo.getNumberOfParameters(); ++i) { if (inCallTo.getParameterType(i).getName().equals(type)) { return i; } } throw new IllegalArgumentException(type.toString() + " is not a parameter to " + inCallTo); }