| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base.library_loader; | 5 package org.chromium.base.library_loader; |
| 6 | 6 |
| 7 import android.os.Bundle; | 7 import android.os.Bundle; |
| 8 import android.os.Parcel; | 8 import android.os.Parcel; |
| 9 import android.os.ParcelFileDescriptor; | 9 import android.os.ParcelFileDescriptor; |
| 10 import android.os.Parcelable; | 10 import android.os.Parcelable; |
| (...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 loadLibraryMaybeInZipFile(null, library); | 720 loadLibraryMaybeInZipFile(null, library); |
| 721 } | 721 } |
| 722 | 722 |
| 723 private static void loadLibraryMaybeInZipFile( | 723 private static void loadLibraryMaybeInZipFile( |
| 724 @Nullable String zipFile, String library) { | 724 @Nullable String zipFile, String library) { |
| 725 if (DEBUG) Log.i(TAG, "loadLibrary: " + library); | 725 if (DEBUG) Log.i(TAG, "loadLibrary: " + library); |
| 726 | 726 |
| 727 // Don't self-load the linker. This is because the build system is | 727 // Don't self-load the linker. This is because the build system is |
| 728 // not clever enough to understand that all the libraries packaged | 728 // not clever enough to understand that all the libraries packaged |
| 729 // in the final .apk don't need to be explicitly loaded. | 729 // in the final .apk don't need to be explicitly loaded. |
| 730 // Also deal with the component build that adds a .cr suffix to the name
. | 730 if (isLinkerLibrary(library)) { |
| 731 if (library.equals(TAG) || library.equals(TAG + ".cr")) { | |
| 732 if (DEBUG) Log.i(TAG, "ignoring self-linker load"); | 731 if (DEBUG) Log.i(TAG, "ignoring self-linker load"); |
| 733 return; | 732 return; |
| 734 } | 733 } |
| 735 | 734 |
| 736 synchronized (Linker.class) { | 735 synchronized (Linker.class) { |
| 737 ensureInitializedLocked(); | 736 ensureInitializedLocked(); |
| 738 | 737 |
| 739 // Security: Ensure prepareLibraryLoad() was called before. | 738 // Security: Ensure prepareLibraryLoad() was called before. |
| 740 // In theory, this can be done lazily here, but it's more consistent | 739 // In theory, this can be done lazily here, but it's more consistent |
| 741 // to use a pair of functions (i.e. prepareLibraryLoad() + finishLib
raryLoad()) | 740 // to use a pair of functions (i.e. prepareLibraryLoad() + finishLib
raryLoad()) |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 // only. | 813 // only. |
| 815 sCurrentLoadAddress = libInfo.mLoadAddress + libInfo.mLoadSize; | 814 sCurrentLoadAddress = libInfo.mLoadAddress + libInfo.mLoadSize; |
| 816 } | 815 } |
| 817 | 816 |
| 818 sLoadedLibraries.put(sharedRelRoName, libInfo); | 817 sLoadedLibraries.put(sharedRelRoName, libInfo); |
| 819 if (DEBUG) Log.i(TAG, "Library details " + libInfo.toString()); | 818 if (DEBUG) Log.i(TAG, "Library details " + libInfo.toString()); |
| 820 } | 819 } |
| 821 } | 820 } |
| 822 | 821 |
| 823 /** | 822 /** |
| 823 * Determine whether a library is the linker library. Also deal with the |
| 824 * component build that adds a .cr suffix to the name. |
| 825 */ |
| 826 public static boolean isLinkerLibrary(String library) { |
| 827 return library.equals(TAG) || library.equals(TAG + ".cr"); |
| 828 } |
| 829 |
| 830 /** |
| 824 * Check whether the device supports loading a library directly from the APK
file. | 831 * Check whether the device supports loading a library directly from the APK
file. |
| 825 * | 832 * |
| 826 * @param apkFile Filename of the APK. | 833 * @param apkFile Filename of the APK. |
| 827 * @return true if supported. | 834 * @return true if supported. |
| 828 */ | 835 */ |
| 829 public static boolean checkLibraryLoadFromApkSupport(String apkFile) { | 836 public static boolean checkLibraryLoadFromApkSupport(String apkFile) { |
| 830 assert apkFile != null; | 837 assert apkFile != null; |
| 831 synchronized (Linker.class) { | 838 synchronized (Linker.class) { |
| 832 ensureInitializedLocked(); | 839 ensureInitializedLocked(); |
| 833 | 840 |
| 834 if (DEBUG) Log.i(TAG, "checkLibraryLoadFromApkSupported: " + apkFile
); | 841 if (DEBUG) Log.i(TAG, "checkLibraryLoadFromApkSupported: " + apkFile
); |
| 835 boolean supported = nativeCheckLibraryLoadFromApkSupport(apkFile); | 842 boolean supported = nativeCheckLibraryLoadFromApkSupport(apkFile); |
| 836 if (DEBUG) Log.i(TAG, "Loading a library directly from the APK file
" + | 843 if (DEBUG) Log.i(TAG, "Loading a library directly from the APK file
" + |
| 837 (supported ? "" : "NOT ") + "supported"); | 844 (supported ? "" : "NOT ") + "supported"); |
| 838 return supported; | 845 return supported; |
| 839 } | 846 } |
| 840 } | 847 } |
| 841 | 848 |
| 842 /** | 849 /** |
| 850 * Check whether a library is page aligned in the APK file. |
| 851 * |
| 852 * @param apkFile Filename of the APK. |
| 853 * @param library The library's base name. |
| 854 * @return true if page aligned. |
| 855 */ |
| 856 public static boolean checkLibraryAlignedInApk(String apkFile, String librar
y) { |
| 857 synchronized (Linker.class) { |
| 858 ensureInitializedLocked(); |
| 859 |
| 860 if (DEBUG) Log.i(TAG, "checkLibraryAlignedInApk: " + apkFile + ", "
+ library); |
| 861 boolean aligned = nativeCheckLibraryAlignedInApk(apkFile, library); |
| 862 if (DEBUG) Log.i(TAG, library + " is " + (aligned ? "" : "NOT ") + |
| 863 "page aligned in " + apkFile); |
| 864 return aligned; |
| 865 } |
| 866 } |
| 867 |
| 868 /** |
| 843 * Move activity from the native thread to the main UI thread. | 869 * Move activity from the native thread to the main UI thread. |
| 844 * Called from native code on its own thread. Posts a callback from | 870 * Called from native code on its own thread. Posts a callback from |
| 845 * the UI thread back to native code. | 871 * the UI thread back to native code. |
| 846 * | 872 * |
| 847 * @param opaque Opaque argument. | 873 * @param opaque Opaque argument. |
| 848 */ | 874 */ |
| 849 @CalledByNative | 875 @CalledByNative |
| 850 public static void postCallbackOnMainThread(final long opaque) { | 876 public static void postCallbackOnMainThread(final long opaque) { |
| 851 ThreadUtils.postOnUiThread(new Runnable() { | 877 ThreadUtils.postOnUiThread(new Runnable() { |
| 852 @Override | 878 @Override |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 935 * @return address to pass to future mmap, or 0 on error. | 961 * @return address to pass to future mmap, or 0 on error. |
| 936 */ | 962 */ |
| 937 private static native long nativeGetRandomBaseLoadAddress(long sizeBytes); | 963 private static native long nativeGetRandomBaseLoadAddress(long sizeBytes); |
| 938 | 964 |
| 939 /** | 965 /** |
| 940 * Native method which checks whether the device supports loading a library | 966 * Native method which checks whether the device supports loading a library |
| 941 * directly from the APK file. | 967 * directly from the APK file. |
| 942 * | 968 * |
| 943 * @param apkFile Filename of the APK. | 969 * @param apkFile Filename of the APK. |
| 944 * @return true if supported. | 970 * @return true if supported. |
| 945 * | |
| 946 */ | 971 */ |
| 947 private static native boolean nativeCheckLibraryLoadFromApkSupport(String ap
kFile); | 972 private static native boolean nativeCheckLibraryLoadFromApkSupport(String ap
kFile); |
| 948 | 973 |
| 949 /** | 974 /** |
| 975 * Native method which checks whether a library is page aligned in the APK f
ile. |
| 976 * |
| 977 * @param apkFile Filename of the APK. |
| 978 * @param library The library's base name. |
| 979 * @return true if page aligned. |
| 980 */ |
| 981 private static native boolean nativeCheckLibraryAlignedInApk(String apkFile,
String library); |
| 982 |
| 983 /** |
| 950 * Record information for a given library. | 984 * Record information for a given library. |
| 951 * IMPORTANT: Native code knows about this class's fields, so | 985 * IMPORTANT: Native code knows about this class's fields, so |
| 952 * don't change them without modifying the corresponding C++ sources. | 986 * don't change them without modifying the corresponding C++ sources. |
| 953 * Also, the LibInfo instance owns the ashmem file descriptor. | 987 * Also, the LibInfo instance owns the ashmem file descriptor. |
| 954 */ | 988 */ |
| 955 public static class LibInfo implements Parcelable { | 989 public static class LibInfo implements Parcelable { |
| 956 | 990 |
| 957 public LibInfo() { | 991 public LibInfo() { |
| 958 mLoadAddress = 0; | 992 mLoadAddress = 0; |
| 959 mLoadSize = 0; | 993 mLoadSize = 0; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1074 } | 1108 } |
| 1075 } | 1109 } |
| 1076 | 1110 |
| 1077 // The map of libraries that are currently loaded in this process. | 1111 // The map of libraries that are currently loaded in this process. |
| 1078 private static HashMap<String, LibInfo> sLoadedLibraries = null; | 1112 private static HashMap<String, LibInfo> sLoadedLibraries = null; |
| 1079 | 1113 |
| 1080 // Used to pass the shared RELRO Bundle through Binder. | 1114 // Used to pass the shared RELRO Bundle through Binder. |
| 1081 public static final String EXTRA_LINKER_SHARED_RELROS = | 1115 public static final String EXTRA_LINKER_SHARED_RELROS = |
| 1082 "org.chromium.base.android.linker.shared_relros"; | 1116 "org.chromium.base.android.linker.shared_relros"; |
| 1083 } | 1117 } |
| OLD | NEW |