This repository was archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathSoLoader.java
More file actions
591 lines (513 loc) · 19.3 KB
/
Copy pathSoLoader.java
File metadata and controls
591 lines (513 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.soloader;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.Log;
import dalvik.system.BaseDexClassLoader;
/**
* Native code loader.
*
* <p> To load a native library, call the static method {@link #loadLibrary} from the
* static initializer of the Java class declaring the native methods. The argument
* should be the library's short name.
*
* <p>For example, if the native code is in libmy_jni_methods.so:
* <pre>
* {@code
* class MyClass {
* static {
* SoLoader.loadLibrary("my_jni_methods");
* }
* }
* }
* </pre>
*
* <p> Before any library can be loaded SoLoader needs to be initialized. The application using
* SoLoader should do that by calling SoLoader.init early on app initialization path. The call must
* happen before any class using SoLoader in its static initializer is loaded.
*/
public class SoLoader {
/* package */ static final String TAG = "SoLoader";
/* package */ static final boolean DEBUG = false;
/* package */ static final boolean SYSTRACE_LIBRARY_LOADING;
/* package */ static SoFileLoader sSoFileLoader;
/**
* Ordered list of sources to consult when trying to load a shared library or one of its
* dependencies. {@code null} indicates that SoLoader is uninitialized.
*/
@Nullable private static SoSource[] sSoSources = null;
/**
* Records the sonames (e.g., "libdistract.so") of shared libraries we've loaded.
*/
private static final Set<String> sLoadedLibraries = new HashSet<>();
/**
* Holds previous thread strict mode policy during recursive calls to loadLibraryBySoName.
*/
@Nullable private static StrictMode.ThreadPolicy sOldPolicy = null;
/**
* Wrapper for System.loadLlibrary.
*/
@Nullable private static SystemLoadLibraryWrapper sSystemLoadLibraryWrapper = null;
/**
* Name of the directory we use for extracted DSOs from built-in SO sources (APK, exopackage)
*/
private static String SO_STORE_NAME_MAIN = "lib-main";
/**
* Enable the exopackage SoSource.
*/
public static final int SOLOADER_ENABLE_EXOPACKAGE = (1<<0);
/**
* Allow deferring some initialization work to asynchronous background threads. Shared libraries
* are nevertheless ready to load as soon as init returns.
*/
public static final int SOLOADER_ALLOW_ASYNC_INIT = (1<<1);
public static final int SOLOADER_LOOK_IN_ZIP = (1<<2);
private static int sFlags;
static {
boolean shouldSystrace = false;
try {
shouldSystrace = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
} catch (NoClassDefFoundError | UnsatisfiedLinkError e) {
// In some test contexts, the Build class and/or some of its dependencies do not exist.
}
SYSTRACE_LIBRARY_LOADING = shouldSystrace;
}
public static void init(Context context, int flags) throws IOException {
init(context, flags, null);
}
/**
* Initializes native code loading for this app; this class's other static facilities cannot be
* used until this {@link #init} is called. This method is idempotent: calls after the first are
* ignored.
*
* @param context application context.
* @param flags Zero or more of the SOLOADER_* flags
* @param soFileLoader
*/
public static void init(Context context, int flags, @Nullable SoFileLoader soFileLoader)
throws IOException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
try {
initImpl(context, flags, soFileLoader);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
/**
* Backward compatibility
*/
public static void init(Context context, boolean nativeExopackage) {
try {
init(context, nativeExopackage ? SOLOADER_ENABLE_EXOPACKAGE : 0);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private static synchronized void initImpl(
Context context,
int flags,
@Nullable SoFileLoader soFileLoader)
throws IOException {
if (sSoSources == null) {
sFlags = flags;
initSoLoader(soFileLoader);
ArrayList<SoSource> soSources = new ArrayList<>();
//
// Add SoSource objects for each of the system library directories.
//
String LD_LIBRARY_PATH = System.getenv("LD_LIBRARY_PATH");
if (LD_LIBRARY_PATH == null) {
LD_LIBRARY_PATH = "/vendor/lib:/system/lib";
}
String[] systemLibraryDirectories = LD_LIBRARY_PATH.split(":");
for (int i = 0; i < systemLibraryDirectories.length; ++i) {
// Don't pass DirectorySoSource.RESOLVE_DEPENDENCIES for directories we find on
// LD_LIBRARY_PATH: Bionic's dynamic linker is capable of correctly resolving dependencies
// these libraries have on each other, so doing that ourselves would be a waste.
File systemSoDirectory = new File(systemLibraryDirectories[i]);
soSources.add(
new DirectorySoSource(
systemSoDirectory,
DirectorySoSource.ON_LD_LIBRARY_PATH));
}
//
// We can only proceed forward if we have a Context. The prominent case
// where we don't have a Context is barebones dalvikvm instantiations. In
// that case, the caller is responsible for providing a correct LD_LIBRARY_PATH.
//
if (context != null) {
//
// Prepend our own SoSource for our own DSOs.
//
if ((flags & SOLOADER_ENABLE_EXOPACKAGE) != 0) {
soSources.add(0, new ExoSoSource(context, SO_STORE_NAME_MAIN));
} else {
ApplicationInfo applicationInfo = context.getApplicationInfo();
boolean isSystemApplication =
(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 &&
(applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0;
int apkSoSourceFlags;
if (isSystemApplication) {
apkSoSourceFlags = 0;
} else {
apkSoSourceFlags = ApkSoSource.PREFER_ANDROID_LIBS_DIRECTORY;
int ourSoSourceFlags = 0;
// On old versions of Android, Bionic doesn't add our library directory to its internal
// search path, and the system doesn't resolve dependencies between modules we ship. On
// these systems, we resolve dependencies ourselves. On other systems, Bionic's built-in
// resolver suffices.
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
ourSoSourceFlags |= DirectorySoSource.RESOLVE_DEPENDENCIES;
}
SoSource ourSoSource = new DirectorySoSource(
new File(applicationInfo.nativeLibraryDir),
ourSoSourceFlags);
soSources.add(0, ourSoSource);
}
soSources.add(0, new ApkSoSource(context, SO_STORE_NAME_MAIN, apkSoSourceFlags));
}
}
SoSource[] finalSoSources = soSources.toArray(new SoSource[soSources.size()]);
int prepareFlags = makePrepareFlags();
for (int i = finalSoSources.length; i-- > 0;) {
finalSoSources[i].prepare(prepareFlags);
}
sSoSources = finalSoSources;
}
}
private static int makePrepareFlags() {
int prepareFlags = 0;
if ((sFlags & SOLOADER_ALLOW_ASYNC_INIT) != 0) {
prepareFlags |= SoSource.PREPARE_FLAG_ALLOW_ASYNC_INIT;
}
return prepareFlags;
}
private static synchronized void initSoLoader(@Nullable SoFileLoader soFileLoader) {
if (soFileLoader != null) {
sSoFileLoader = soFileLoader;
return;
}
final Runtime runtime = Runtime.getRuntime();
final Method nativeLoadRuntimeMethod = getNativeLoadRuntimeMethod();
final boolean hasNativeLoadMethod = nativeLoadRuntimeMethod != null;
final String localLdLibraryPath =
hasNativeLoadMethod ? Api14Utils.getClassLoaderLdLoadLibrary() : null;
final String localLdLibraryPathNoZips = makeNonZipPath(localLdLibraryPath);
sSoFileLoader = new SoFileLoader() {
@Override
public void load(final String pathToSoFile, final int loadFlags) {
String error = null;
if (hasNativeLoadMethod) {
final boolean inZip = (loadFlags & SOLOADER_LOOK_IN_ZIP) == SOLOADER_LOOK_IN_ZIP;
final String path = inZip ? localLdLibraryPath : localLdLibraryPathNoZips;
try {
synchronized (runtime) {
error =
// the third argument of nativeLoad method was removed in Android P API
Build.VERSION.SDK_INT <= 27
? (String)
nativeLoadRuntimeMethod.invoke(
runtime, pathToSoFile, SoLoader.class.getClassLoader(), path)
: (String)
nativeLoadRuntimeMethod.invoke(
runtime, pathToSoFile, SoLoader.class.getClassLoader());
if (error != null) {
throw new UnsatisfiedLinkError(error);
}
}
} catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException e) {
final String errMsg = "Error: Cannot load " + pathToSoFile;
Log.e(TAG, errMsg);
throw new RuntimeException(errMsg, e);
}
} else {
System.load(pathToSoFile);
}
}
};
}
@Nullable
private static Method getNativeLoadRuntimeMethod() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return null;
}
try {
final Method method;
if (Build.VERSION.SDK_INT <= 27) {
method =
Runtime.class.getDeclaredMethod(
"nativeLoad", String.class, ClassLoader.class, String.class);
} else {
method = Runtime.class.getDeclaredMethod("nativeLoad", String.class, ClassLoader.class);
}
method.setAccessible(true);
return method;
} catch (final NoSuchMethodException | SecurityException e) {
Log.w(TAG, "Cannot get nativeLoad method", e);
return null;
}
}
/**
* Turn shared-library loading into a no-op. Useful in special circumstances.
*/
public static void setInTestMode() {
sSoSources = new SoSource[]{new NoopSoSource()};
}
/**
* Make shared-library loading delegate to the system. Useful for tests.
*/
public static void deinitForTest() {
sSoSources = null;
}
/**
* Provide a wrapper object for calling {@link System#loadLibrary}.
* This is useful for controlling which ClassLoader libraries are loaded into.
*/
public static void setSystemLoadLibraryWrapper(SystemLoadLibraryWrapper wrapper) {
sSystemLoadLibraryWrapper = wrapper;
}
public static final class WrongAbiError extends UnsatisfiedLinkError {
WrongAbiError(Throwable cause) {
super("APK was built for a different platform");
initCause(cause);
}
}
public static void loadLibrary(String shortName) {
loadLibrary(shortName, 0);
}
/**
* Load a shared library, initializing any JNI binding it contains.
*
* @param shortName Name of library to find, without "lib" prefix or ".so" suffix
* @param loadFlags Control flags for the loading behavior. See available flags under
* {@link SoSource} (LOAD_FLAG_XXX).
*/
public static synchronized void loadLibrary(String shortName, int loadFlags)
throws UnsatisfiedLinkError
{
if (sSoSources == null) {
// This should never happen during normal operation,
// but if we're running in a non-Android environment,
// fall back to System.loadLibrary.
if ("http://www.android.com/".equals(System.getProperty("java.vendor.url"))) {
// This will throw.
assertInitialized();
} else {
// Not on an Android system. Ask the JVM to load for us.
if (sSystemLoadLibraryWrapper != null) {
sSystemLoadLibraryWrapper.loadLibrary(shortName);
} else {
System.loadLibrary(shortName);
}
return;
}
}
String mergedLibName = MergedSoMapping.mapLibName(shortName);
String nameToLoad = mergedLibName != null ? mergedLibName : shortName;
try {
loadLibraryBySoName(System.mapLibraryName(nameToLoad), loadFlags);
} catch (IOException ex) {
throw new RuntimeException(ex);
} catch (UnsatisfiedLinkError ex) {
String message = ex.getMessage();
if (message != null && message.contains("unexpected e_machine:")) {
throw new WrongAbiError(ex);
}
throw ex;
}
if (mergedLibName != null) {
if (SYSTRACE_LIBRARY_LOADING) {
Api18TraceUtils.beginTraceSection("MergedSoMapping.invokeJniOnload[" + shortName + "]");
}
try {
// We trust the JNI merging code to prevent us from
// invoking JNI_OnLoad more than once because
// it's more memory-efficient than tracking this in Java.
MergedSoMapping.invokeJniOnload(shortName);
} finally {
if (SYSTRACE_LIBRARY_LOADING) {
Api18TraceUtils.endSection();
}
}
}
}
/**
* Unpack library and its dependencies, returning the location of the unpacked library file. All
* non-system dependencies of the given library will either be on LD_LIBRARY_PATH or will be in
* the same directory as the returned File.
*
* @param shortName Name of library to find, without "lib" prefix or ".so" suffix
* @return Unpacked DSO location
*/
public static File unpackLibraryAndDependencies(String shortName)
throws UnsatisfiedLinkError
{
assertInitialized();
try {
return unpackLibraryBySoName(System.mapLibraryName(shortName));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public static synchronized void loadLibraryBySoName(String soName, int loadFlags)
throws IOException {
int result = sLoadedLibraries.contains(soName)
? SoSource.LOAD_RESULT_LOADED
: SoSource.LOAD_RESULT_NOT_FOUND;
if (result == SoSource.LOAD_RESULT_NOT_FOUND) {
// This way, we set the thread policy only one per loadLibrary no matter how many dependencies
// we load. Each call to StrictMode.allowThreadDiskWrites allocates.
boolean restoreOldPolicy = false;
if (sOldPolicy == null) {
sOldPolicy = StrictMode.allowThreadDiskReads();
restoreOldPolicy = true;
}
if (SYSTRACE_LIBRARY_LOADING) {
Api18TraceUtils.beginTraceSection("SoLoader.loadLibrary[" + soName + "]");
}
try {
for (int i = 0; result == SoSource.LOAD_RESULT_NOT_FOUND && i < sSoSources.length; ++i) {
result = sSoSources[i].loadLibrary(soName, loadFlags);
}
} finally {
if (SYSTRACE_LIBRARY_LOADING) {
Api18TraceUtils.endSection();
}
if (restoreOldPolicy) {
StrictMode.setThreadPolicy(sOldPolicy);
sOldPolicy = null;
}
}
}
if (result == SoSource.LOAD_RESULT_NOT_FOUND) {
throw new UnsatisfiedLinkError("couldn't find DSO to load: " + soName);
}
if (result == SoSource.LOAD_RESULT_LOADED) {
sLoadedLibraries.add(soName);
}
}
@Nullable
public static String makeNonZipPath(final String localLdLibraryPath) {
if (localLdLibraryPath == null) {
return null;
}
final String[] paths = localLdLibraryPath.split(":");
final ArrayList<String> pathsWithoutZip = new ArrayList<String>(paths.length);
for (final String path : paths) {
if (path.contains("!")) {
continue;
}
pathsWithoutZip.add(path);
}
return TextUtils.join(":", pathsWithoutZip);
}
public static Set<String> getLoadedLibrariesNames() {
return sLoadedLibraries;
}
/* package */ static File unpackLibraryBySoName(String soName) throws IOException {
for (int i = 0; i < sSoSources.length; ++i) {
File unpacked = sSoSources[i].unpackLibrary(soName);
if (unpacked != null) {
return unpacked;
}
}
throw new FileNotFoundException(soName);
}
private static void assertInitialized() {
if (sSoSources == null) {
throw new RuntimeException("SoLoader.init() not yet called");
}
}
/**
* Add a new source of native libraries. SoLoader consults the new source before any
* currently-installed source.
*
* @param extraSoSource The SoSource to install
*/
public static synchronized void prependSoSource(SoSource extraSoSource) throws IOException {
assertInitialized();
extraSoSource.prepare(makePrepareFlags());
SoSource[] newSoSources = new SoSource[sSoSources.length+1];
newSoSources[0] = extraSoSource;
System.arraycopy(sSoSources, 0, newSoSources, 1, sSoSources.length);
sSoSources = newSoSources;
}
/**
* Retrieve an LD_LIBRARY_PATH value suitable for using the native linker to resolve our
* shared libraries.
*/
public static synchronized String makeLdLibraryPath() {
assertInitialized();
ArrayList<String> pathElements = new ArrayList<>();
SoSource[] soSources = sSoSources;
for (int i = 0; i < soSources.length; ++i) {
soSources[i].addToLdLibraryPath(pathElements);
}
return TextUtils.join(":", pathElements);
}
/**
* This function ensure that every SoSources Abi is supported for at least one
* abi in SysUtil.getSupportedAbis
* @return true if all SoSources have their Abis supported
*/
public static boolean areSoSourcesAbisSupported() {
SoSource[] soSources = sSoSources;
if (soSources == null) {
return false;
}
String supportedAbis[] = SysUtil.getSupportedAbis();
for (int i = 0; i < soSources.length; ++i) {
String[] soSourceAbis = soSources[i].getSoSourceAbis();
for (int j = 0; j < soSourceAbis.length; ++j) {
boolean soSourceSupported = false;
for (int k = 0; k < supportedAbis.length && !soSourceSupported; ++k) {
soSourceSupported = soSourceAbis[j].equals(supportedAbis[k]);
}
if (!soSourceSupported) {
return false;
}
}
}
return true;
}
@DoNotOptimize
@TargetApi(14)
private static class Api14Utils {
public static String getClassLoaderLdLoadLibrary() {
final ClassLoader classLoader = SoLoader.class.getClassLoader();
if (!(classLoader instanceof BaseDexClassLoader)) {
throw new IllegalStateException(
"ClassLoader " + classLoader.getClass().getName() + " should be of type BaseDexClassLoader");
}
try {
final BaseDexClassLoader baseDexClassLoader = (BaseDexClassLoader) classLoader;
final Method getLdLibraryPathMethod = BaseDexClassLoader.class.getMethod("getLdLibraryPath");
return (String) getLdLibraryPathMethod.invoke(baseDexClassLoader);
} catch (Exception e) {
throw new RuntimeException("Cannot call getLdLibraryPath", e);
}
}
}
}