| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.pm.SharedLibraryInfo; |
| 21 |
import android.os.Build; |
| 22 |
import android.os.GraphicsEnvironment; |
| 23 |
import android.os.Trace; |
| 24 |
import android.util.ArrayMap; |
| 25 |
import android.util.Log; |
| 26 |
|
| 27 |
import com.android.internal.os.ClassLoaderFactory; |
| 28 |
|
| 29 |
import dalvik.system.PathClassLoader; |
| 30 |
|
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.Collection; |
| 33 |
import java.util.HashMap; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Map; |
| 36 |
|
| 37 |
|
| 38 |
public class ApplicationLoaders { |
| 39 |
private static final String TAG = "ApplicationLoaders"; |
| 40 |
|
| 41 |
@UnsupportedAppUsage |
| 42 |
public static ApplicationLoaders getDefault() { |
| 43 |
return gApplicationLoaders; |
| 44 |
} |
| 45 |
|
| 46 |
ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled, |
| 47 |
String librarySearchPath, String libraryPermittedPath, |
| 48 |
ClassLoader parent, String classLoaderName) { |
| 49 |
return getClassLoaderWithSharedLibraries(zip, targetSdkVersion, isBundled, |
| 50 |
librarySearchPath, libraryPermittedPath, parent, classLoaderName, |
| 51 |
null, null, null); |
| 52 |
} |
| 53 |
|
| 54 |
ClassLoader getClassLoaderWithSharedLibraries( |
| 55 |
String zip, int targetSdkVersion, boolean isBundled, |
| 56 |
String librarySearchPath, String libraryPermittedPath, |
| 57 |
ClassLoader parent, String classLoaderName, |
| 58 |
List<ClassLoader> sharedLibraries, List<String> nativeSharedLibraries, |
| 59 |
List<ClassLoader> sharedLibrariesLoadedAfterApp) { |
| 60 |
|
| 61 |
return getClassLoader(zip, targetSdkVersion, isBundled, librarySearchPath, |
| 62 |
libraryPermittedPath, parent, zip, classLoaderName, sharedLibraries, |
| 63 |
nativeSharedLibraries, sharedLibrariesLoadedAfterApp); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
ClassLoader getSharedLibraryClassLoaderWithSharedLibraries(String zip, int targetSdkVersion, |
| 74 |
boolean isBundled, String librarySearchPath, String libraryPermittedPath, |
| 75 |
ClassLoader parent, String classLoaderName, List<ClassLoader> sharedLibraries, |
| 76 |
List<ClassLoader> sharedLibrariesAfter) { |
| 77 |
ClassLoader loader = getCachedNonBootclasspathSystemLib(zip, parent, classLoaderName, |
| 78 |
sharedLibraries); |
| 79 |
if (loader != null) { |
| 80 |
return loader; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
List<String> nativeSharedLibraries = new ArrayList<>(); |
| 88 |
nativeSharedLibraries.add("ALL"); |
| 89 |
return getClassLoaderWithSharedLibraries(zip, targetSdkVersion, isBundled, |
| 90 |
librarySearchPath, libraryPermittedPath, parent, classLoaderName, sharedLibraries, |
| 91 |
nativeSharedLibraries, sharedLibrariesAfter); |
| 92 |
} |
| 93 |
|
| 94 |
private ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled, |
| 95 |
String librarySearchPath, String libraryPermittedPath, |
| 96 |
ClassLoader parent, String cacheKey, |
| 97 |
String classLoaderName, List<ClassLoader> sharedLibraries, |
| 98 |
List<String> nativeSharedLibraries, |
| 99 |
List<ClassLoader> sharedLibrariesLoadedAfterApp) { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent(); |
| 107 |
|
| 108 |
synchronized (mLoaders) { |
| 109 |
if (parent == null) { |
| 110 |
parent = baseParent; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if (parent == baseParent) { |
| 119 |
ClassLoader loader = mLoaders.get(cacheKey); |
| 120 |
if (loader != null) { |
| 121 |
return loader; |
| 122 |
} |
| 123 |
|
| 124 |
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
| 125 |
|
| 126 |
ClassLoader classloader = ClassLoaderFactory.createClassLoader( |
| 127 |
zip, librarySearchPath, libraryPermittedPath, parent, |
| 128 |
targetSdkVersion, isBundled, classLoaderName, sharedLibraries, |
| 129 |
nativeSharedLibraries, sharedLibrariesLoadedAfterApp); |
| 130 |
|
| 131 |
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 132 |
|
| 133 |
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setLayerPaths"); |
| 134 |
GraphicsEnvironment.getInstance().setLayerPaths( |
| 135 |
classloader, librarySearchPath, libraryPermittedPath); |
| 136 |
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 137 |
|
| 138 |
if (cacheKey != null) { |
| 139 |
mLoaders.put(cacheKey, classloader); |
| 140 |
} |
| 141 |
return classloader; |
| 142 |
} |
| 143 |
|
| 144 |
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
| 145 |
ClassLoader loader = ClassLoaderFactory.createClassLoader( |
| 146 |
zip, null, parent, classLoaderName, sharedLibraries, |
| 147 |
null ); |
| 148 |
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 149 |
return loader; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public void createAndCacheNonBootclasspathSystemClassLoaders(SharedLibraryInfo[] libs) { |
| 161 |
if (mSystemLibsCacheMap != null) { |
| 162 |
throw new IllegalStateException("Already cached."); |
| 163 |
} |
| 164 |
|
| 165 |
mSystemLibsCacheMap = new HashMap<String, CachedClassLoader>(); |
| 166 |
|
| 167 |
for (SharedLibraryInfo lib : libs) { |
| 168 |
createAndCacheNonBootclasspathSystemClassLoader(lib); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private void createAndCacheNonBootclasspathSystemClassLoader(SharedLibraryInfo lib) { |
| 179 |
String path = lib.getPath(); |
| 180 |
List<SharedLibraryInfo> dependencies = lib.getDependencies(); |
| 181 |
|
| 182 |
|
| 183 |
ArrayList<ClassLoader> sharedLibraries = null; |
| 184 |
if (dependencies != null) { |
| 185 |
sharedLibraries = new ArrayList<ClassLoader>(dependencies.size()); |
| 186 |
for (SharedLibraryInfo dependency : dependencies) { |
| 187 |
String dependencyPath = dependency.getPath(); |
| 188 |
CachedClassLoader cached = mSystemLibsCacheMap.get(dependencyPath); |
| 189 |
|
| 190 |
if (cached == null) { |
| 191 |
throw new IllegalStateException("Failed to find dependency " + dependencyPath |
| 192 |
+ " of cachedlibrary " + path); |
| 193 |
} |
| 194 |
|
| 195 |
sharedLibraries.add(cached.loader); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
ClassLoader classLoader = getClassLoader(path, Build.VERSION.SDK_INT, true , |
| 201 |
null , null , null , |
| 202 |
null , null , sharedLibraries , |
| 203 |
null , null ); |
| 204 |
|
| 205 |
if (classLoader == null) { |
| 206 |
|
| 207 |
throw new IllegalStateException("Failed to cache " + path); |
| 208 |
} |
| 209 |
|
| 210 |
CachedClassLoader cached = new CachedClassLoader(); |
| 211 |
cached.loader = classLoader; |
| 212 |
cached.sharedLibraries = sharedLibraries; |
| 213 |
|
| 214 |
Log.d(TAG, "Created zygote-cached class loader: " + path); |
| 215 |
mSystemLibsCacheMap.put(path, cached); |
| 216 |
} |
| 217 |
|
| 218 |
private static boolean sharedLibrariesEquals(List<ClassLoader> lhs, List<ClassLoader> rhs) { |
| 219 |
if (lhs == null) { |
| 220 |
return rhs == null; |
| 221 |
} |
| 222 |
|
| 223 |
return lhs.equals(rhs); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public ClassLoader getCachedNonBootclasspathSystemLib(String zip, ClassLoader parent, |
| 233 |
String classLoaderName, List<ClassLoader> sharedLibraries) { |
| 234 |
if (mSystemLibsCacheMap == null) { |
| 235 |
return null; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
if (parent != null || classLoaderName != null) { |
| 240 |
return null; |
| 241 |
} |
| 242 |
|
| 243 |
CachedClassLoader cached = mSystemLibsCacheMap.get(zip); |
| 244 |
if (cached == null) { |
| 245 |
return null; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
if (!sharedLibrariesEquals(sharedLibraries, cached.sharedLibraries)) { |
| 250 |
Log.w(TAG, "Unexpected environment loading cached library " + zip + " (real|cached): (" |
| 251 |
+ sharedLibraries + "|" + cached.sharedLibraries + ")"); |
| 252 |
return null; |
| 253 |
} |
| 254 |
|
| 255 |
Log.d(TAG, "Returning zygote-cached class loader: " + zip); |
| 256 |
return cached.loader; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public ClassLoader createAndCacheWebViewClassLoader(String packagePath, String libsPath, |
| 265 |
String cacheKey) { |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
return getClassLoader(packagePath, Build.VERSION.SDK_INT, false, libsPath, null, null, |
| 273 |
cacheKey, null , null , |
| 274 |
null , |
| 275 |
null ); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
void addPath(ClassLoader classLoader, String dexPath) { |
| 283 |
if (!(classLoader instanceof PathClassLoader)) { |
| 284 |
throw new IllegalStateException("class loader is not a PathClassLoader"); |
| 285 |
} |
| 286 |
final PathClassLoader baseDexClassLoader = (PathClassLoader) classLoader; |
| 287 |
baseDexClassLoader.addDexPath(dexPath); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
void addNative(ClassLoader classLoader, Collection<String> libPaths) { |
| 294 |
if (!(classLoader instanceof PathClassLoader)) { |
| 295 |
throw new IllegalStateException("class loader is not a PathClassLoader"); |
| 296 |
} |
| 297 |
final PathClassLoader baseDexClassLoader = (PathClassLoader) classLoader; |
| 298 |
baseDexClassLoader.addNativePath(libPaths); |
| 299 |
} |
| 300 |
|
| 301 |
@UnsupportedAppUsage |
| 302 |
private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<>(); |
| 303 |
|
| 304 |
private static final ApplicationLoaders gApplicationLoaders = new ApplicationLoaders(); |
| 305 |
|
| 306 |
private static class CachedClassLoader { |
| 307 |
ClassLoader loader; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
List<ClassLoader> sharedLibraries; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
private Map<String, CachedClassLoader> mSystemLibsCacheMap = null; |
| 319 |
} |