2626import soot .jimple .toolkits .scalar .NopEliminator ;
2727import soot .util .Chain ;
2828
29- //The generated implementations of this class are semantically equivalent to the AppComponentFactory in Android:
30- //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/main/core/java/android/app/AppComponentFactory.java
29+ /**
30+ * In addition to the normal JVM library classes, this class also patches
31+ * certain Android library classes.
32+ */
3133public class AndroidLibraryClassPatcher extends LibraryClassPatcher {
3234
3335 @ Override
@@ -37,6 +39,10 @@ public void patchLibraries() {
3739 patchComponentFactory ();
3840 }
3941
42+ /**
43+ * The generated implementation of this method are semantically equivalent to the AppComponentFactory in Android.
44+ * @see https://android.googlesource.com/platform/frameworks/base/+/refs/heads/main/core/java/android/app/AppComponentFactory.java
45+ */
4046 protected void patchComponentFactory () {
4147 SootClass sc = Scene .v ().forceResolve (AndroidEntryPointConstants .APPCOMPONENTFACTORYCLASS ,
4248 SootClass .SIGNATURES );
@@ -54,6 +60,11 @@ protected void patchComponentFactory() {
5460
5561 }
5662
63+ /**
64+ * Patches the instantiate classloader class.
65+ * It returns the default class loader unmodified.
66+ * @param sc the class of the app component factory
67+ */
5768 private void patchInstantiateClassLoader (SootClass sc ) {
5869 SootMethod smInstantiate = getOrCreateMethod (sc ,
5970 AndroidEntryPointConstants .APPCOMPONENTFACTORY_INSTANTIATECLASSLOADER );
@@ -64,6 +75,12 @@ private void patchInstantiateClassLoader(SootClass sc) {
6475
6576 }
6677
78+ /**
79+ * Returns all class names that could be instantiated when
80+ * instantiating a class with the given class name, i.e. all subclasses/implementers.
81+ * @param className the class name (could also represent an interface)
82+ * @return a string array of all possible names.
83+ */
6784 protected String [] getAllNames (String className ) {
6885 List <String > names = new ArrayList <>();
6986 SootClass sc = Scene .v ().getSootClassUnsafe (className );
@@ -84,6 +101,28 @@ protected String[] getAllNames(String className) {
84101 return names .toArray (new String [names .size ()]);
85102 }
86103
104+ /**
105+ * Patches an instantiate method. Generates code equivalent to the following:
106+ *
107+ * <code>
108+ * public void instantiateActivity(ClassLoader cl, String className, Intent intent)
109+ * {
110+ *
111+ * if (className.equals("foo.bar.MainActivity"))
112+ * return new foo.bar.MainActivity(); //(1)
113+ * if (className.equals("foo.bar.FooActivity"))
114+ * return new foo.bar.FooActivity(); //(2)
115+ * return cl.loadClass(className).newInstance(); //(3)
116+ *
117+ * }
118+ * </code>
119+ * The instantiation statements (1) and (2) are used to help SPARK and other static algorithms to find
120+ * allocation sites. (3) is the fallback that would normally be the implementation when using Android's default
121+ * app component factory.
122+ * @param sc the class of the app component factory
123+ * @param subsig the sub signature of the method, in our example case instantiateActivity
124+ * @param names the names for each possible class instantiation, in our example case "foo.bar.MainActivity", "foo.bar.FooActivity"
125+ */
87126 protected void patchInstantiate (SootClass sc , String subsig , String ... names ) {
88127
89128 if (!sc .isLibraryClass ())
@@ -154,6 +193,12 @@ protected void patchInstantiate(SootClass sc, String subsig, String... names) {
154193
155194 }
156195
196+ /**
197+ * Creates a method if it doesn't exist. Otherwise, it returns the existing method
198+ * @param sc the class where the method is being looked for
199+ * @param subsig the sub signature of the method
200+ * @return the method
201+ */
157202 private static SootMethod getOrCreateMethod (SootClass sc , String subsig ) {
158203 SootMethod p = sc .getMethodUnsafe (subsig );
159204 if (p != null )
0 commit comments