| Index: base/android/java/src/org/chromium/base/library_loader/Linker.java
|
| diff --git a/base/android/java/src/org/chromium/base/library_loader/Linker.java b/base/android/java/src/org/chromium/base/library_loader/Linker.java
|
| index 9a64a3fddbfd3b9837326daa6aaabc9edbdceb22..c769339620df9bae97d72735f05b030d064bb57a 100644
|
| --- a/base/android/java/src/org/chromium/base/library_loader/Linker.java
|
| +++ b/base/android/java/src/org/chromium/base/library_loader/Linker.java
|
| @@ -1,4 +1,4 @@
|
| -// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| @@ -8,14 +8,10 @@ import android.os.Bundle;
|
| import android.os.Parcel;
|
| import android.os.ParcelFileDescriptor;
|
| import android.os.Parcelable;
|
| -import android.util.Log;
|
|
|
| -import org.chromium.base.CalledByNative;
|
| -import org.chromium.base.SysUtils;
|
| -import org.chromium.base.ThreadUtils;
|
| +import org.chromium.base.Log;
|
| import org.chromium.base.annotations.AccessedByNative;
|
|
|
| -import java.io.FileNotFoundException;
|
| import java.util.HashMap;
|
| import java.util.Locale;
|
| import java.util.Map;
|
| @@ -153,13 +149,19 @@ import javax.annotation.Nullable;
|
| *
|
| * This method also ensures the process uses the shared RELROs.
|
| */
|
| -public class Linker {
|
| -
|
| - // Log tag for this class. This must match the name of the linker's native library.
|
| - private static final String TAG = "chromium_android_linker";
|
| +public abstract class Linker {
|
| + // Log tag for this class.
|
| + private static final String TAG = "cr.library_loader";
|
|
|
| // Set to true to enable debug logs.
|
| - private static final boolean DEBUG = false;
|
| + protected static final boolean DEBUG = false;
|
| +
|
| + // Used to pass the shared RELRO Bundle through Binder.
|
| + public static final String EXTRA_LINKER_SHARED_RELROS =
|
| + "org.chromium.base.android.linker.shared_relros";
|
| +
|
| + // Guards all access to the linker.
|
| + protected final Object mLock = new Object();
|
|
|
| // Constants used to control the behaviour of the browser process with
|
| // regards to the shared RELRO section.
|
| @@ -188,96 +190,24 @@ public class Linker {
|
| // Indicates if this is a low-memory device or not. The default is to
|
| // determine this by probing the system at runtime, but this can be forced
|
| // for testing by calling setMemoryDeviceConfig().
|
| - private static int sMemoryDeviceConfig = MEMORY_DEVICE_CONFIG_INIT;
|
| -
|
| - // Becomes true after linker initialization.
|
| - private static boolean sInitialized = false;
|
| -
|
| - // Set to true to indicate that the system supports safe sharing of RELRO sections.
|
| - private static boolean sRelroSharingSupported = false;
|
| -
|
| - // Set to true if this runs in the browser process. Disabled by initServiceProcess().
|
| - // TODO(petrcermak): This flag can be incorrectly set to false (even though this might run in
|
| - // the browser process) on low-memory devices.
|
| - private static boolean sInBrowserProcess = true;
|
| -
|
| - // Becomes true to indicate this process needs to wait for a shared RELRO in
|
| - // finishLibraryLoad().
|
| - private static boolean sWaitForSharedRelros = false;
|
| -
|
| - // Becomes true when initialization determines that the browser process can use the
|
| - // shared RELRO.
|
| - private static boolean sBrowserUsesSharedRelro = false;
|
| -
|
| - // The map of all RELRO sections either created or used in this process.
|
| - private static Bundle sSharedRelros = null;
|
| -
|
| - // Current common random base load address.
|
| - private static long sBaseLoadAddress = 0;
|
| -
|
| - // Current fixed-location load address for the next library called by loadLibrary().
|
| - private static long sCurrentLoadAddress = 0;
|
| -
|
| - // Becomes true once prepareLibraryLoad() has been called.
|
| - private static boolean sPrepareLibraryLoadCalled = false;
|
| -
|
| - // Used internally to initialize the linker's static data. Assume lock is held.
|
| - private static void ensureInitializedLocked() {
|
| - assert Thread.holdsLock(Linker.class);
|
| -
|
| - if (!sInitialized) {
|
| - sRelroSharingSupported = false;
|
| - if (NativeLibraries.sUseLinker) {
|
| - if (DEBUG) Log.i(TAG, "Loading lib" + TAG + ".so");
|
| - try {
|
| - System.loadLibrary(TAG);
|
| - } catch (UnsatisfiedLinkError e) {
|
| - // In a component build, the ".cr" suffix is added to each library name.
|
| - Log.w(TAG, "Couldn't load lib" + TAG + ".so, trying lib" + TAG + ".cr.so");
|
| - System.loadLibrary(TAG + ".cr");
|
| - }
|
| - sRelroSharingSupported = nativeCanUseSharedRelro();
|
| - if (!sRelroSharingSupported) {
|
| - Log.w(TAG, "This system cannot safely share RELRO sections");
|
| - } else {
|
| - if (DEBUG) Log.i(TAG, "This system supports safe shared RELRO sections");
|
| - }
|
| -
|
| - if (sMemoryDeviceConfig == MEMORY_DEVICE_CONFIG_INIT) {
|
| - sMemoryDeviceConfig = SysUtils.isLowEndDevice()
|
| - ? MEMORY_DEVICE_CONFIG_LOW : MEMORY_DEVICE_CONFIG_NORMAL;
|
| - }
|
| -
|
| - switch (BROWSER_SHARED_RELRO_CONFIG) {
|
| - case BROWSER_SHARED_RELRO_CONFIG_NEVER:
|
| - sBrowserUsesSharedRelro = false;
|
| - break;
|
| - case BROWSER_SHARED_RELRO_CONFIG_LOW_RAM_ONLY:
|
| - sBrowserUsesSharedRelro =
|
| - (sMemoryDeviceConfig == MEMORY_DEVICE_CONFIG_LOW);
|
| - if (sBrowserUsesSharedRelro) {
|
| - Log.w(TAG, "Low-memory device: shared RELROs used in all processes");
|
| - }
|
| - break;
|
| - case BROWSER_SHARED_RELRO_CONFIG_ALWAYS:
|
| - Log.w(TAG, "Beware: shared RELROs used in all processes!");
|
| - sBrowserUsesSharedRelro = true;
|
| - break;
|
| - default:
|
| - assert false : "Unreached";
|
| - break;
|
| - }
|
| - } else {
|
| - if (DEBUG) Log.i(TAG, "Linker disabled");
|
| + protected int mMemoryDeviceConfig = MEMORY_DEVICE_CONFIG_INIT;
|
| +
|
| + // Singleton.
|
| + private static Linker sSingleton = null;
|
| + private static Object sSingletonLock = new Object();
|
| +
|
| + // Protected singleton constructor.
|
| + protected Linker() {}
|
| +
|
| + // Get singleton instance.
|
| + public static final Linker getInstance() {
|
| + synchronized (sSingletonLock) {
|
| + if (sSingleton == null) {
|
| + // TODO(simonb): Extend later to return either a LegacyLinker
|
| + // or a ModernLinker instance.
|
| + sSingleton = new LegacyLinker();
|
| }
|
| -
|
| - if (!sRelroSharingSupported) {
|
| - // Sanity.
|
| - sBrowserUsesSharedRelro = false;
|
| - sWaitForSharedRelros = false;
|
| - }
|
| -
|
| - sInitialized = true;
|
| + return sSingleton;
|
| }
|
| }
|
|
|
| @@ -297,7 +227,7 @@ public class Linker {
|
| }
|
|
|
| // The name of a class that implements TestRunner.
|
| - static String sTestRunnerClassName = null;
|
| + String mTestRunnerClassName = null;
|
|
|
| /**
|
| * Set the TestRunner by its class name. It will be instantiated at
|
| @@ -305,17 +235,18 @@ public class Linker {
|
| * @param testRunnerClassName null or a String for the class name of the
|
| * TestRunner to use.
|
| */
|
| - public static void setTestRunnerClassName(String testRunnerClassName) {
|
| - if (DEBUG) Log.i(TAG, "setTestRunnerByClassName(" + testRunnerClassName + ") called");
|
| -
|
| + public void setTestRunnerClassName(String testRunnerClassName) {
|
| + if (DEBUG) {
|
| + Log.i(TAG, "setTestRunnerByClassName(" + testRunnerClassName + ") called");
|
| + }
|
| if (!NativeLibraries.sEnableLinkerTests) {
|
| - // Ignore this in production code to prevent malvolent runtime injection.
|
| + // Ignore this in production code to prevent malevolent runtime injection.
|
| return;
|
| }
|
|
|
| - synchronized (Linker.class) {
|
| - assert sTestRunnerClassName == null;
|
| - sTestRunnerClassName = testRunnerClassName;
|
| + synchronized (mLock) {
|
| + assert mTestRunnerClassName == null;
|
| + mTestRunnerClassName = testRunnerClassName;
|
| }
|
| }
|
|
|
| @@ -326,9 +257,9 @@ public class Linker {
|
| * @return null or a String holding the name of the class implementing
|
| * the TestRunner set by calling setTestRunnerClassName() previously.
|
| */
|
| - public static String getTestRunnerClassName() {
|
| - synchronized (Linker.class) {
|
| - return sTestRunnerClassName;
|
| + public String getTestRunnerClassName() {
|
| + synchronized (mLock) {
|
| + return mTestRunnerClassName;
|
| }
|
| }
|
|
|
| @@ -337,12 +268,14 @@ public class Linker {
|
| * memory device configuration. Should only be used for testing.
|
| * @param memoryDeviceConfig either MEMORY_DEVICE_CONFIG_LOW or MEMORY_DEVICE_CONFIG_NORMAL.
|
| */
|
| - public static void setMemoryDeviceConfig(int memoryDeviceConfig) {
|
| - if (DEBUG) Log.i(TAG, "setMemoryDeviceConfig(" + memoryDeviceConfig + ") called");
|
| + public void setMemoryDeviceConfig(int memoryDeviceConfig) {
|
| + if (DEBUG) {
|
| + Log.i(TAG, "setMemoryDeviceConfig(" + memoryDeviceConfig + ") called");
|
| + }
|
| // Sanity check. This method should only be called during tests.
|
| assert NativeLibraries.sEnableLinkerTests;
|
| - synchronized (Linker.class) {
|
| - assert sMemoryDeviceConfig == MEMORY_DEVICE_CONFIG_INIT;
|
| + synchronized (mLock) {
|
| + assert mMemoryDeviceConfig == MEMORY_DEVICE_CONFIG_INIT;
|
| assert memoryDeviceConfig == MEMORY_DEVICE_CONFIG_LOW
|
| || memoryDeviceConfig == MEMORY_DEVICE_CONFIG_NORMAL;
|
| if (DEBUG) {
|
| @@ -352,7 +285,7 @@ public class Linker {
|
| Log.i(TAG, "Simulating a regular-memory device");
|
| }
|
| }
|
| - sMemoryDeviceConfig = memoryDeviceConfig;
|
| + mMemoryDeviceConfig = memoryDeviceConfig;
|
| }
|
| }
|
|
|
| @@ -361,130 +294,31 @@ public class Linker {
|
| * use this linker. If not, System.loadLibrary() should be used to load
|
| * libraries instead.
|
| */
|
| - public static boolean isUsed() {
|
| - // Only GYP targets that are APKs and have the 'use_chromium_linker' variable
|
| - // defined as 1 will use this linker. For all others (the default), the
|
| - // auto-generated NativeLibraries.sUseLinker variable will be false.
|
| - if (!NativeLibraries.sUseLinker) return false;
|
| -
|
| - synchronized (Linker.class) {
|
| - ensureInitializedLocked();
|
| - // At the moment, there is also no point in using this linker if the
|
| - // system does not support RELRO sharing safely.
|
| - return sRelroSharingSupported;
|
| - }
|
| - }
|
| + public abstract boolean isUsed();
|
|
|
| /**
|
| * Call this method to determine if the linker will try to use shared RELROs
|
| * for the browser process.
|
| */
|
| - public static boolean isUsingBrowserSharedRelros() {
|
| - synchronized (Linker.class) {
|
| - ensureInitializedLocked();
|
| - return sBrowserUsesSharedRelro;
|
| - }
|
| - }
|
| + public abstract boolean isUsingBrowserSharedRelros();
|
|
|
| /**
|
| * Call this method to determine if the chromium project must load
|
| * the library directly from the zip file.
|
| */
|
| - public static boolean isInZipFile() {
|
| - return NativeLibraries.sUseLibraryInZipFile;
|
| - }
|
| + public abstract boolean isInZipFile();
|
|
|
| /**
|
| * Call this method just before loading any native shared libraries in this process.
|
| */
|
| - public static void prepareLibraryLoad() {
|
| - if (DEBUG) Log.i(TAG, "prepareLibraryLoad() called");
|
| - synchronized (Linker.class) {
|
| - sPrepareLibraryLoadCalled = true;
|
| -
|
| - if (sInBrowserProcess) {
|
| - // Force generation of random base load address, as well
|
| - // as creation of shared RELRO sections in this process.
|
| - setupBaseLoadAddressLocked();
|
| - }
|
| - }
|
| - }
|
| + public abstract void prepareLibraryLoad();
|
|
|
| /**
|
| * Call this method just after loading all native shared libraries in this process.
|
| * Note that when in a service process, this will block until the RELRO bundle is
|
| * received, i.e. when another thread calls useSharedRelros().
|
| */
|
| - public static void finishLibraryLoad() {
|
| - if (DEBUG) Log.i(TAG, "finishLibraryLoad() called");
|
| - synchronized (Linker.class) {
|
| - if (DEBUG) Log.i(TAG, String.format(
|
| - Locale.US,
|
| - "sInBrowserProcess=%s sBrowserUsesSharedRelro=%s sWaitForSharedRelros=%s",
|
| - sInBrowserProcess ? "true" : "false",
|
| - sBrowserUsesSharedRelro ? "true" : "false",
|
| - sWaitForSharedRelros ? "true" : "false"));
|
| -
|
| - if (sLoadedLibraries == null) {
|
| - if (DEBUG) Log.i(TAG, "No libraries loaded");
|
| - } else {
|
| - if (sInBrowserProcess) {
|
| - // Create new Bundle containing RELRO section information
|
| - // for all loaded libraries. Make it available to getSharedRelros().
|
| - sSharedRelros = createBundleFromLibInfoMap(sLoadedLibraries);
|
| - if (DEBUG) {
|
| - Log.i(TAG, "Shared RELRO created");
|
| - dumpBundle(sSharedRelros);
|
| - }
|
| -
|
| - if (sBrowserUsesSharedRelro) {
|
| - useSharedRelrosLocked(sSharedRelros);
|
| - }
|
| - }
|
| -
|
| - if (sWaitForSharedRelros) {
|
| - assert !sInBrowserProcess;
|
| -
|
| - // Wait until the shared relro bundle is received from useSharedRelros().
|
| - while (sSharedRelros == null) {
|
| - try {
|
| - Linker.class.wait();
|
| - } catch (InterruptedException ie) {
|
| - // no-op
|
| - }
|
| - }
|
| - useSharedRelrosLocked(sSharedRelros);
|
| - // Clear the Bundle to ensure its file descriptor references can't be reused.
|
| - sSharedRelros.clear();
|
| - sSharedRelros = null;
|
| - }
|
| - }
|
| -
|
| - if (NativeLibraries.sEnableLinkerTests && sTestRunnerClassName != null) {
|
| - // The TestRunner implementation must be instantiated _after_
|
| - // all libraries are loaded to ensure that its native methods
|
| - // are properly registered.
|
| - if (DEBUG) Log.i(TAG, "Instantiating " + sTestRunnerClassName);
|
| - TestRunner testRunner = null;
|
| - try {
|
| - testRunner = (TestRunner)
|
| - Class.forName(sTestRunnerClassName).newInstance();
|
| - } catch (Exception e) {
|
| - Log.e(TAG, "Could not extract test runner class name", e);
|
| - testRunner = null;
|
| - }
|
| - if (testRunner != null) {
|
| - if (!testRunner.runChecks(sMemoryDeviceConfig, sInBrowserProcess)) {
|
| - Log.wtf(TAG, "Linker runtime tests failed in this process!!");
|
| - assert false;
|
| - } else {
|
| - Log.i(TAG, "All linker tests passed!");
|
| - }
|
| - }
|
| - }
|
| - }
|
| - if (DEBUG) Log.i(TAG, "finishLibraryLoad() exiting");
|
| - }
|
| + public abstract void finishLibraryLoad();
|
|
|
| /**
|
| * Call this to send a Bundle containing the shared RELRO sections to be
|
| @@ -494,32 +328,7 @@ public class Linker {
|
| * @param bundle The Bundle instance containing a map of shared RELRO sections
|
| * to use in this process.
|
| */
|
| - public static void useSharedRelros(Bundle bundle) {
|
| - // Ensure the bundle uses the application's class loader, not the framework
|
| - // one which doesn't know anything about LibInfo.
|
| - // Also, hold a fresh copy of it so the caller can't recycle it.
|
| - Bundle clonedBundle = null;
|
| - if (bundle != null) {
|
| - bundle.setClassLoader(LibInfo.class.getClassLoader());
|
| - clonedBundle = new Bundle(LibInfo.class.getClassLoader());
|
| - Parcel parcel = Parcel.obtain();
|
| - bundle.writeToParcel(parcel, 0);
|
| - parcel.setDataPosition(0);
|
| - clonedBundle.readFromParcel(parcel);
|
| - parcel.recycle();
|
| - }
|
| - if (DEBUG) {
|
| - Log.i(TAG, "useSharedRelros() called with " + bundle
|
| - + ", cloned " + clonedBundle);
|
| - }
|
| - synchronized (Linker.class) {
|
| - // Note that in certain cases, this can be called before
|
| - // initServiceProcess() in service processes.
|
| - sSharedRelros = clonedBundle;
|
| - // Tell any listener blocked in finishLibraryLoad() about it.
|
| - Linker.class.notifyAll();
|
| - }
|
| - }
|
| + public abstract void useSharedRelros(Bundle bundle);
|
|
|
| /**
|
| * Call this to retrieve the shared RELRO sections created in this process,
|
| @@ -527,33 +336,13 @@ public class Linker {
|
| * @return a new Bundle instance, or null if RELRO sharing is disabled on
|
| * this system, or if initServiceProcess() was called previously.
|
| */
|
| - public static Bundle getSharedRelros() {
|
| - if (DEBUG) Log.i(TAG, "getSharedRelros() called");
|
| - synchronized (Linker.class) {
|
| - if (!sInBrowserProcess) {
|
| - if (DEBUG) Log.i(TAG, "... returning null Bundle");
|
| - return null;
|
| - }
|
| -
|
| - // Return the Bundle created in finishLibraryLoad().
|
| - if (DEBUG) Log.i(TAG, "... returning " + sSharedRelros);
|
| - return sSharedRelros;
|
| - }
|
| - }
|
| -
|
| + public abstract Bundle getSharedRelros();
|
|
|
| /**
|
| * Call this method before loading any libraries to indicate that this
|
| * process shall neither create or reuse shared RELRO sections.
|
| */
|
| - public static void disableSharedRelros() {
|
| - if (DEBUG) Log.i(TAG, "disableSharedRelros() called");
|
| - synchronized (Linker.class) {
|
| - sInBrowserProcess = false;
|
| - sWaitForSharedRelros = false;
|
| - sBrowserUsesSharedRelro = false;
|
| - }
|
| - }
|
| + public abstract void disableSharedRelros();
|
|
|
| /**
|
| * Call this method before loading any libraries to indicate that this
|
| @@ -561,22 +350,7 @@ public class Linker {
|
| * Typically used when starting service processes.
|
| * @param baseLoadAddress the base library load address to use.
|
| */
|
| - public static void initServiceProcess(long baseLoadAddress) {
|
| - if (DEBUG) {
|
| - Log.i(TAG, String.format(
|
| - Locale.US, "initServiceProcess(0x%x) called", baseLoadAddress));
|
| - }
|
| - synchronized (Linker.class) {
|
| - ensureInitializedLocked();
|
| - sInBrowserProcess = false;
|
| - sBrowserUsesSharedRelro = false;
|
| - if (sRelroSharingSupported) {
|
| - sWaitForSharedRelros = true;
|
| - sBaseLoadAddress = baseLoadAddress;
|
| - sCurrentLoadAddress = baseLoadAddress;
|
| - }
|
| - }
|
| - }
|
| + public abstract void initServiceProcess(long baseLoadAddress);
|
|
|
| /**
|
| * Retrieve the base load address of all shared RELRO sections.
|
| @@ -585,119 +359,7 @@ public class Linker {
|
| * @return a common, random base load address, or 0 if RELRO sharing is
|
| * disabled.
|
| */
|
| - public static long getBaseLoadAddress() {
|
| - synchronized (Linker.class) {
|
| - ensureInitializedLocked();
|
| - if (!sInBrowserProcess) {
|
| - Log.w(TAG, "Shared RELRO sections are disabled in this process!");
|
| - return 0;
|
| - }
|
| -
|
| - setupBaseLoadAddressLocked();
|
| - if (DEBUG) Log.i(TAG, String.format(Locale.US, "getBaseLoadAddress() returns 0x%x",
|
| - sBaseLoadAddress));
|
| - return sBaseLoadAddress;
|
| - }
|
| - }
|
| -
|
| - // Used internally to lazily setup the common random base load address.
|
| - private static void setupBaseLoadAddressLocked() {
|
| - assert Thread.holdsLock(Linker.class);
|
| - if (sBaseLoadAddress == 0) {
|
| - long address = computeRandomBaseLoadAddress();
|
| - sBaseLoadAddress = address;
|
| - sCurrentLoadAddress = address;
|
| - if (address == 0) {
|
| - // If the computed address is 0, there are issues with finding enough
|
| - // free address space, so disable RELRO shared / fixed load addresses.
|
| - Log.w(TAG, "Disabling shared RELROs due address space pressure");
|
| - sBrowserUsesSharedRelro = false;
|
| - sWaitForSharedRelros = false;
|
| - }
|
| - }
|
| - }
|
| -
|
| -
|
| - /**
|
| - * Compute a random base load address at which to place loaded libraries.
|
| - * @return new base load address, or 0 if the system does not support
|
| - * RELRO sharing.
|
| - */
|
| - private static long computeRandomBaseLoadAddress() {
|
| - // nativeGetRandomBaseLoadAddress() returns an address at which it has previously
|
| - // successfully mapped an area of the given size, on the basis that we will be
|
| - // able, with high probability, to map our library into it.
|
| - //
|
| - // One issue with this is that we do not yet know the size of the library that
|
| - // we will load is. So here we pass a value that we expect will always be larger
|
| - // than that needed. If it is smaller the library mapping may still succeed. The
|
| - // other issue is that although highly unlikely, there is no guarantee that
|
| - // something else does not map into the area we are going to use between here and
|
| - // when we try to map into it.
|
| - //
|
| - // The above notes mean that all of this is probablistic. It is however okay to do
|
| - // because if, worst case and unlikely, we get unlucky in our choice of address,
|
| - // the back-out and retry without the shared RELRO in the ChildProcessService will
|
| - // keep things running.
|
| - final long maxExpectedBytes = 192 * 1024 * 1024;
|
| - final long address = nativeGetRandomBaseLoadAddress(maxExpectedBytes);
|
| - if (DEBUG) {
|
| - Log.i(TAG, String.format(Locale.US, "Random native base load address: 0x%x", address));
|
| - }
|
| - return address;
|
| - }
|
| -
|
| - // Used for debugging only.
|
| - private static void dumpBundle(Bundle bundle) {
|
| - if (DEBUG) Log.i(TAG, "Bundle has " + bundle.size() + " items: " + bundle);
|
| - }
|
| -
|
| - /**
|
| - * Use the shared RELRO section from a Bundle received form another process.
|
| - * Call this after calling setBaseLoadAddress() then loading all libraries
|
| - * with loadLibrary().
|
| - * @param bundle Bundle instance generated with createSharedRelroBundle() in
|
| - * another process.
|
| - */
|
| - private static void useSharedRelrosLocked(Bundle bundle) {
|
| - assert Thread.holdsLock(Linker.class);
|
| -
|
| - if (DEBUG) Log.i(TAG, "Linker.useSharedRelrosLocked() called");
|
| -
|
| - if (bundle == null) {
|
| - if (DEBUG) Log.i(TAG, "null bundle!");
|
| - return;
|
| - }
|
| -
|
| - if (!sRelroSharingSupported) {
|
| - if (DEBUG) Log.i(TAG, "System does not support RELRO sharing");
|
| - return;
|
| - }
|
| -
|
| - if (sLoadedLibraries == null) {
|
| - if (DEBUG) Log.i(TAG, "No libraries loaded!");
|
| - return;
|
| - }
|
| -
|
| - if (DEBUG) dumpBundle(bundle);
|
| - HashMap<String, LibInfo> relroMap = createLibInfoMapFromBundle(bundle);
|
| -
|
| - // Apply the RELRO section to all libraries that were already loaded.
|
| - for (Map.Entry<String, LibInfo> entry : relroMap.entrySet()) {
|
| - String libName = entry.getKey();
|
| - LibInfo libInfo = entry.getValue();
|
| - if (!nativeUseSharedRelro(libName, libInfo)) {
|
| - Log.w(TAG, "Could not use shared RELRO section for " + libName);
|
| - } else {
|
| - if (DEBUG) Log.i(TAG, "Using shared RELRO section for " + libName);
|
| - }
|
| - }
|
| -
|
| - // In service processes, close all file descriptors from the map now.
|
| - if (!sInBrowserProcess) closeLibInfoMap(relroMap);
|
| -
|
| - if (DEBUG) Log.i(TAG, "Linker.useSharedRelrosLocked() exiting");
|
| - }
|
| + public abstract long getBaseLoadAddress();
|
|
|
| /**
|
| * Load a native shared library with the Chromium linker. If the zip file
|
| @@ -709,254 +371,13 @@ public class Linker {
|
| * @param zipFilePath The path of the zip file containing the library (or null).
|
| * @param libFilePath The path of the library (possibly in the zip file).
|
| */
|
| - public static void loadLibrary(@Nullable String zipFilePath, String libFilePath) {
|
| - if (DEBUG) Log.i(TAG, "loadLibrary: " + zipFilePath + ", " + libFilePath);
|
| -
|
| - synchronized (Linker.class) {
|
| - ensureInitializedLocked();
|
| -
|
| - // Security: Ensure prepareLibraryLoad() was called before.
|
| - // In theory, this can be done lazily here, but it's more consistent
|
| - // to use a pair of functions (i.e. prepareLibraryLoad() + finishLibraryLoad())
|
| - // that wrap all calls to loadLibrary() in the library loader.
|
| - assert sPrepareLibraryLoadCalled;
|
| -
|
| - if (sLoadedLibraries == null) sLoadedLibraries = new HashMap<String, LibInfo>();
|
| -
|
| - if (sLoadedLibraries.containsKey(libFilePath)) {
|
| - if (DEBUG) Log.i(TAG, "Not loading " + libFilePath + " twice");
|
| - return;
|
| - }
|
| -
|
| - LibInfo libInfo = new LibInfo();
|
| - long loadAddress = 0;
|
| - if ((sInBrowserProcess && sBrowserUsesSharedRelro) || sWaitForSharedRelros) {
|
| - // Load the library at a fixed address.
|
| - loadAddress = sCurrentLoadAddress;
|
| - }
|
| -
|
| - String sharedRelRoName = libFilePath;
|
| - if (zipFilePath != null) {
|
| - if (!nativeLoadLibraryInZipFile(zipFilePath, libFilePath, loadAddress, libInfo)) {
|
| - String errorMessage = "Unable to load library: " + libFilePath
|
| - + ", in: " + zipFilePath;
|
| - Log.e(TAG, errorMessage);
|
| - throw new UnsatisfiedLinkError(errorMessage);
|
| - }
|
| - sharedRelRoName = zipFilePath;
|
| - } else {
|
| - if (!nativeLoadLibrary(libFilePath, loadAddress, libInfo)) {
|
| - String errorMessage = "Unable to load library: " + libFilePath;
|
| - Log.e(TAG, errorMessage);
|
| - throw new UnsatisfiedLinkError(errorMessage);
|
| - }
|
| - }
|
| -
|
| - // Print the load address to the logcat when testing the linker. The format
|
| - // of the string is expected by the Python test_runner script as one of:
|
| - // BROWSER_LIBRARY_ADDRESS: <library-name> <address>
|
| - // RENDERER_LIBRARY_ADDRESS: <library-name> <address>
|
| - // Where <library-name> is the library name, and <address> is the hexadecimal load
|
| - // address.
|
| - if (NativeLibraries.sEnableLinkerTests) {
|
| - Log.i(TAG, String.format(
|
| - Locale.US,
|
| - "%s_LIBRARY_ADDRESS: %s %x",
|
| - sInBrowserProcess ? "BROWSER" : "RENDERER",
|
| - libFilePath,
|
| - libInfo.mLoadAddress));
|
| - }
|
| -
|
| - if (sInBrowserProcess) {
|
| - // Create a new shared RELRO section at the 'current' fixed load address.
|
| - if (!nativeCreateSharedRelro(sharedRelRoName, sCurrentLoadAddress, libInfo)) {
|
| - Log.w(TAG, String.format(Locale.US,
|
| - "Could not create shared RELRO for %s at %x", libFilePath,
|
| - sCurrentLoadAddress));
|
| - } else {
|
| - if (DEBUG) Log.i(TAG,
|
| - String.format(
|
| - Locale.US,
|
| - "Created shared RELRO for %s at %x: %s",
|
| - sharedRelRoName,
|
| - sCurrentLoadAddress,
|
| - libInfo.toString()));
|
| - }
|
| - }
|
| -
|
| - if (sCurrentLoadAddress != 0) {
|
| - // Compute the next current load address. If sBaseLoadAddress
|
| - // is not 0, this is an explicit library load address. Otherwise,
|
| - // this is an explicit load address for relocated RELRO sections
|
| - // only.
|
| - sCurrentLoadAddress = libInfo.mLoadAddress + libInfo.mLoadSize;
|
| - }
|
| -
|
| - sLoadedLibraries.put(sharedRelRoName, libInfo);
|
| - if (DEBUG) Log.i(TAG, "Library details " + libInfo.toString());
|
| - }
|
| - }
|
| + public abstract void loadLibrary(@Nullable String zipFilePath, String libFilePath);
|
|
|
| /**
|
| * Determine whether a library is the linker library. Also deal with the
|
| * component build that adds a .cr suffix to the name.
|
| */
|
| - public static boolean isChromiumLinkerLibrary(String library) {
|
| - return library.equals(TAG) || library.equals(TAG + ".cr");
|
| - }
|
| -
|
| - /**
|
| - * Get the full library path in zip file (lib/<abi>/crazy.<lib_name>).
|
| - *
|
| - * @param library The library's base name.
|
| - * @return the library path.
|
| - */
|
| - public static String getLibraryFilePathInZipFile(String library) throws FileNotFoundException {
|
| - synchronized (Linker.class) {
|
| - ensureInitializedLocked();
|
| -
|
| - String path = nativeGetLibraryFilePathInZipFile(library);
|
| - if (path.equals("")) {
|
| - throw new FileNotFoundException(
|
| - "Failed to retrieve path in zip file for library " + library);
|
| - }
|
| - return path;
|
| - }
|
| - }
|
| -
|
| - /**
|
| - * Check whether a library is page aligned and uncompressed in the APK file.
|
| - *
|
| - * @param apkFile Filename of the APK.
|
| - * @param library The library's base name.
|
| - * @return true if page aligned and uncompressed.
|
| - */
|
| - public static boolean checkLibraryIsMappableInApk(String apkFile, String library) {
|
| - synchronized (Linker.class) {
|
| - ensureInitializedLocked();
|
| -
|
| - if (DEBUG) Log.i(TAG, "checkLibraryIsMappableInApk: " + apkFile + ", " + library);
|
| - boolean aligned = nativeCheckLibraryIsMappableInApk(apkFile, library);
|
| - if (DEBUG) Log.i(TAG, library + " is " + (aligned ? "" : "NOT ")
|
| - + "page aligned in " + apkFile);
|
| - return aligned;
|
| - }
|
| - }
|
| -
|
| - /**
|
| - * Move activity from the native thread to the main UI thread.
|
| - * Called from native code on its own thread. Posts a callback from
|
| - * the UI thread back to native code.
|
| - *
|
| - * @param opaque Opaque argument.
|
| - */
|
| - @CalledByNative
|
| - public static void postCallbackOnMainThread(final long opaque) {
|
| - ThreadUtils.postOnUiThread(new Runnable() {
|
| - @Override
|
| - public void run() {
|
| - nativeRunCallbackOnUiThread(opaque);
|
| - }
|
| - });
|
| - }
|
| -
|
| - /**
|
| - * Native method to run callbacks on the main UI thread.
|
| - * Supplied by the crazy linker and called by postCallbackOnMainThread.
|
| - * @param opaque Opaque crazy linker arguments.
|
| - */
|
| - private static native void nativeRunCallbackOnUiThread(long opaque);
|
| -
|
| - /**
|
| - * Native method used to load a library.
|
| - * @param library Platform specific library name (e.g. libfoo.so)
|
| - * @param loadAddress Explicit load address, or 0 for randomized one.
|
| - * @param libInfo If not null, the mLoadAddress and mLoadSize fields
|
| - * of this LibInfo instance will set on success.
|
| - * @return true for success, false otherwise.
|
| - */
|
| - private static native boolean nativeLoadLibrary(String library,
|
| - long loadAddress,
|
| - LibInfo libInfo);
|
| -
|
| - /**
|
| - * Native method used to load a library which is inside a zipfile.
|
| - * @param zipfileName Filename of the zip file containing the library.
|
| - * @param library Platform specific library name (e.g. libfoo.so)
|
| - * @param loadAddress Explicit load address, or 0 for randomized one.
|
| - * @param libInfo If not null, the mLoadAddress and mLoadSize fields
|
| - * of this LibInfo instance will set on success.
|
| - * @return true for success, false otherwise.
|
| - */
|
| - private static native boolean nativeLoadLibraryInZipFile(String zipfileName,
|
| - String libraryName,
|
| - long loadAddress,
|
| - LibInfo libInfo);
|
| -
|
| - /**
|
| - * Native method used to create a shared RELRO section.
|
| - * If the library was already loaded at the same address using
|
| - * nativeLoadLibrary(), this creates the RELRO for it. Otherwise,
|
| - * this loads a new temporary library at the specified address,
|
| - * creates and extracts the RELRO section from it, then unloads it.
|
| - * @param library Library name.
|
| - * @param loadAddress load address, which can be different from the one
|
| - * used to load the library in the current process!
|
| - * @param libInfo libInfo instance. On success, the mRelroStart, mRelroSize
|
| - * and mRelroFd will be set.
|
| - * @return true on success, false otherwise.
|
| - */
|
| - private static native boolean nativeCreateSharedRelro(String library,
|
| - long loadAddress,
|
| - LibInfo libInfo);
|
| -
|
| - /**
|
| - * Native method used to use a shared RELRO section.
|
| - * @param library Library name.
|
| - * @param libInfo A LibInfo instance containing valid RELRO information
|
| - * @return true on success.
|
| - */
|
| - private static native boolean nativeUseSharedRelro(String library,
|
| - LibInfo libInfo);
|
| -
|
| - /**
|
| - * Checks that the system supports shared RELROs. Old Android kernels
|
| - * have a bug in the way they check Ashmem region protection flags, which
|
| - * makes using shared RELROs unsafe. This method performs a simple runtime
|
| - * check for this misfeature, even though nativeEnableSharedRelro() will
|
| - * always fail if this returns false.
|
| - */
|
| - private static native boolean nativeCanUseSharedRelro();
|
| -
|
| - /**
|
| - * Return a random address that should be free to be mapped with the given size.
|
| - * Maps an area of size bytes, and if successful then unmaps it and returns
|
| - * the address of the area allocated by the system (with ASLR). The idea is
|
| - * that this area should remain free of other mappings until we map our library
|
| - * into it.
|
| - * @param sizeBytes Size of area in bytes to search for.
|
| - * @return address to pass to future mmap, or 0 on error.
|
| - */
|
| - private static native long nativeGetRandomBaseLoadAddress(long sizeBytes);
|
| -
|
| - /**
|
| - * Native method used to get the full library path in zip file
|
| - * (lib/<abi>/crazy.<lib_name>).
|
| - *
|
| - * @param library The library's base name.
|
| - * @return the library path (or empty string on failure).
|
| - */
|
| - private static native String nativeGetLibraryFilePathInZipFile(String library);
|
| -
|
| - /**
|
| - * Native method which checks whether a library is page aligned and
|
| - * uncompressed in the APK file.
|
| - *
|
| - * @param apkFile Filename of the APK.
|
| - * @param library The library's base name.
|
| - * @return true if page aligned and uncompressed.
|
| - */
|
| - private static native boolean nativeCheckLibraryIsMappableInApk(String apkFile, String library);
|
| + public abstract boolean isChromiumLinkerLibrary(String library);
|
|
|
| /**
|
| * Record information for a given library.
|
| @@ -979,7 +400,9 @@ public class Linker {
|
| try {
|
| ParcelFileDescriptor.adoptFd(mRelroFd).close();
|
| } catch (java.io.IOException e) {
|
| - if (DEBUG) Log.e(TAG, "Failed to close fd: " + mRelroFd);
|
| + if (DEBUG) {
|
| + Log.e(TAG, "Failed to close fd: " + mRelroFd);
|
| + }
|
| }
|
| mRelroFd = -1;
|
| }
|
| @@ -1060,7 +483,7 @@ public class Linker {
|
| }
|
|
|
| // Create a Bundle from a map of LibInfo objects.
|
| - private static Bundle createBundleFromLibInfoMap(HashMap<String, LibInfo> map) {
|
| + protected Bundle createBundleFromLibInfoMap(HashMap<String, LibInfo> map) {
|
| Bundle bundle = new Bundle(map.size());
|
| for (Map.Entry<String, LibInfo> entry : map.entrySet()) {
|
| bundle.putParcelable(entry.getKey(), entry.getValue());
|
| @@ -1070,7 +493,7 @@ public class Linker {
|
| }
|
|
|
| // Create a new LibInfo map from a Bundle.
|
| - private static HashMap<String, LibInfo> createLibInfoMapFromBundle(Bundle bundle) {
|
| + protected HashMap<String, LibInfo> createLibInfoMapFromBundle(Bundle bundle) {
|
| HashMap<String, LibInfo> map = new HashMap<String, LibInfo>();
|
| for (String library : bundle.keySet()) {
|
| LibInfo libInfo = bundle.getParcelable(library);
|
| @@ -1080,16 +503,9 @@ public class Linker {
|
| }
|
|
|
| // Call the close() method on all values of a LibInfo map.
|
| - private static void closeLibInfoMap(HashMap<String, LibInfo> map) {
|
| + protected void closeLibInfoMap(HashMap<String, LibInfo> map) {
|
| for (Map.Entry<String, LibInfo> entry : map.entrySet()) {
|
| entry.getValue().close();
|
| }
|
| }
|
| -
|
| - // The map of libraries that are currently loaded in this process.
|
| - private static HashMap<String, LibInfo> sLoadedLibraries = null;
|
| -
|
| - // Used to pass the shared RELRO Bundle through Binder.
|
| - public static final String EXTRA_LINKER_SHARED_RELROS =
|
| - "org.chromium.base.android.linker.shared_relros";
|
| }
|
|
|