Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1989)

Unified Diff: base/android/java/src/org/chromium/base/Linker.java

Issue 23717023: Android: Add chrome-specific dynamic linker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename library Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/android/linker/crazy_linker.cpp » ('j') | base/android/linker/crazy_linker.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/Linker.java
diff --git a/base/android/java/src/org/chromium/base/Linker.java b/base/android/java/src/org/chromium/base/Linker.java
new file mode 100644
index 0000000000000000000000000000000000000000..6589670852cbb326319b315ad61ba6da85e7583f
--- /dev/null
+++ b/base/android/java/src/org/chromium/base/Linker.java
@@ -0,0 +1,470 @@
+// Copyright (c) 2012 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.
+
+package org.chromium.base;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+// This is support code for the custom dynamic linker used in Android
+// to reduce RAM by sharing the RELRO section between the browser and
+// renderer processes.
+//
+// To make this feature work, native shared libraries must be loaded
+// at exactly the same addresses in all Chromium processes. On the other
+// hand, these load addresses must be randomized for security. This is
+// thus implemented with the following scheme:
+//
+// - The browser process shall call Linker.randomizeBaseLoadAddress()
+// as soon as possible (i.e. before trying to load any library, and
+// before starting any service process).
+//
+// It shall also call Linker.enableRelroSharing() to enable RELRO
+// section sharing at this point.
+//
+// - When starting any service process, the browser process shall
+// pass the randomized address, retrieved with
+// Linker.getBaseLoadAddress(), as an extra in the startup Intent.
+//
+// - When the service process binds() for the first time, it must
+// read the base address from the intent and call
+// Linker.setBaseLoadAddress() before trying to load any library.
+//
+// - The browser and service processes should always load the same
+// set of libraries, in the same order.
+//
+// If all these steps are followed, all libraries are guaranteed to be
+// loaded at the same address in all processes.
+//
+// IMPORTANT: Currently, Chromium libraries are loaded between 0x30000000
+// and 0x40000000, this is to avoid any conflicts with other allocations
+// performed by Dalvik, which use the kernel's ASLR feature, which always
+// allocate stuff, starting from 0x40000000.
+//
+// Once the libraries are loaded, the following can happen:
+//
+// - The browser process calls Linker.enableRelroSharing().
+//
+// - It sends the result of Linker.getRelroBundle() to any service
+// process it knows about.
+//
+// - Each service process then calls Linker.applyRelroBundle() with
+// the data received from the browser process.
+//
+// Once this is completed, the RELRO section will be shared, saving
+// about 1.3 MB per service process (as of M31).
+
+public class Linker {
+
+ // Log tag for this class.
+ private static final String TAG = "crazy_linker";
+
+ // Set to true to enable debug logs.
+ private static final boolean DEBUG = true;
+
+ // Becomes true after linker initialization.
+ private static boolean sInitialized = false;
+
+ // Set to true to enable RelroSharing from the browser process.
+ private static boolean sRelroSharingEnabled = false;
+
+ private static Object sLock = new Object();
+
+ /**
+ * Call this in the browser process to randomize the load address
+ * of Chromium libraries. In one hand, RELRO section sharing requires
+ * that all libraries be loaded at the same addresses in all processes,
+ * on the other hand, it's better for these addresses to be unpredictable
+ * for security reasons.
+ *
+ * The randomized base address can be retrieved with getBaseLoadAddress(),
+ * and shall be passed to service processes. The latter should call
+ * setBaseLoadAddress() with the appropriate value before loading any
+ * library.
+ */
+ public static void randomizeBaseLoadAddress() {
+
+ if (DEBUG)
+ Log.i(TAG, "Linker.randomizeBaseLoadAddress() called");
+
+ // Ensure offset is between 0 and 64 MB exclusive.
+ Random r = new Random();
+
+ // The kernel ASLR feature will place randomized mappings starting
+ // from this address. Never try to load anything above this
+ // explicitely to avoid random conflicts.
+ final long baseAddressLimit = 0x40000000;
+
+ // Start loading libraries from this base address.
+ final long baseAddress = 0x20000000;
+
+ // Maximum randomized base address value. Used to ensure a margin
+ // of 192 MB below baseAddressLimit.
+ final long baseAddressMax = baseAddressLimit - 192*1024*1024;
+
+ // There is no way to get this from Java APIs, and it's not
+ // possible to call JNI here since no shared library has been
+ // loaded yet. All current Android platforms have a 4 KB page
+ // size at the moment. Correct this in the future when this is
+ // no longer true.
+ final long pageSize = 4096;
+
+ // Get a proper random page-aligned offset.
+ final int offset = r.nextInt((int)((baseAddressMax - baseAddress) / pageSize));
+ setBaseLoadAddress(baseAddress + offset * pageSize);
+ }
+
+ /**
+ * Retrieve the base library load address. If randomizedBaseLoadAddress()
+ * or setBaseLoadAddress() was called, this retrieves the result. Otherwise,
+ * returns 0, which means that addresses will be randomized by the
+ * kernel ASLR feature instead.
+ * @return The current base load address.
+ */
+ public static long getBaseLoadAddress() {
+ if (DEBUG) Log.i(TAG, "Linker.getBaseLoadAddress() called => " + sBaseLoadAddress);
+
+ synchronized (TAG) {
+ return sBaseLoadAddress;
+ }
+ }
+
+ /**
+ * Set the base library load address. This is call to enforce a base load
+ * address, typically in a service process. Will only take effect when
+ * loading libraries after this call.
+ * @param baseLoadAddress The new base load address.
+ */
+ public static void setBaseLoadAddress(long baseLoadAddress) {
+ if (DEBUG) Log.i(TAG, "Linker.setBaseLoadAddress(" + baseLoadAddress + ") called");
+
+ synchronized (TAG) {
+ sBaseLoadAddress = baseLoadAddress;
+ sCurrentLoadAddress = baseLoadAddress;
+ }
+ }
+
+ private static long sBaseLoadAddress = 0;
+ private static long sCurrentLoadAddress = 0;
+
+ /**
+ * Call this method in the browser process to enable RELRO section
+ * sharing. This can be called before or after loading libraries.
+ */
+ public static void enableRelroSharing() {
+ if (DEBUG) Log.i(TAG, "Linker.enableRelroSharing() called");
+
+ synchronized (TAG) {
+ if (!sRelroSharingEnabled) {
+ if (sLoadedLibraries != null) {
+ // If some libraries were already loaded, enable RELRO
+ // sharing for them too.
+ for (Map.Entry<String, LibInfo> entry : sLoadedLibraries.entrySet()) {
+ String libName = entry.getKey();
+ LibInfo libInfo = entry.getValue();
+ if (!nativeEnableSharedRelro(libName, libInfo))
+ Log.w(TAG, "Could not enable RELRO sharing for " + libName);
+ }
+ }
+ sRelroSharingEnabled = true;
+ }
+ }
+ }
+
+ /**
+ * Call this method to retrieve a Bundle containing RELRO sharing
+ * information about the libraries currently loaded. Should be used
+ * in the browser process only.
+ * @return null if enableRelroSharing() was not called, or no libraries
+ * were loaded. Otherwise a new Bundle that must be passed to any
+ * service process, which in turn should call applyRelroBundle() with
+ * it. Note that the Bundle contains file descriptor and thus cannot
+ * be written to disk or added to an Intent.
+ */
+ public static Bundle getRelroBundle() {
+ if (DEBUG) Log.i(TAG, "Linker.getRelroBundle() called");
+
+ synchronized (TAG) {
+
+ if (!sRelroSharingEnabled || sLoadedLibraries == null) {
+ if (DEBUG) Log.i(TAG, "Nothing to share!?");
+ return null;
+ }
+
+ Bundle bundle = sLoadedLibraries.toBundle();
+ if (DEBUG) Log.i(TAG, "Bundle has " + bundle.size() + " items.");
+ return bundle;
+ }
+ }
+
+ /**
+ * Apply a Bundle generated by getRelroBundle() to the libraries
+ * loaded in this process. Should be used in service processes only.
+ * @param bundle A Bundle reference. Can be null.
+ */
+ public static void applyRelroBundle(Bundle bundle) {
+ if (DEBUG) Log.i(TAG, "Linker.applyRelroBundle() called (REALLY)");
+
+ synchronized (TAG) {
+ if (bundle == null) {
+ if (DEBUG) Log.i(TAG, "null Bundle!?");
+ return;
+ }
+
+ sRelroLibraries = new LibInfoMap(bundle);
+ if (DEBUG) {
+ Log.i(TAG, "Bundle has " + sRelroLibraries.size() + " items");
+ for (Map.Entry<String, LibInfo> entry : sRelroLibraries.entrySet()) {
+ String libName = entry.getKey();
+ LibInfo libInfo = entry.getValue();
+ Log.i(TAG, " library " + libName + ": " + libInfo.toString());
+ }
+ }
+
+ if (sLoadedLibraries != null) {
+ // Apply the RELRO section to all libraries that were already
+ // loaded, if any.
+ for (Map.Entry<String, LibInfo> entry : sRelroLibraries.entrySet()) {
+ String libName = entry.getKey();
+ LibInfo libInfo = entry.getValue();
+
+ if (sLoadedLibraries.get(libName) != null) {
+ sLoadedLibraries.put(libName, libInfo);
+ if (!nativeUseSharedRelro(libName, libInfo))
+ Log.w(TAG, "Could not use shared RELRO section for " + libName);
+ }
+ }
+ }
+
+ if (DEBUG) Log.i(TAG, "Linker.applyRelroBundle() exiting");
+ }
+ }
+
+ /**
+ * Load a native shared library with the Chromium linker.
+ * If neither initSharedRelro() or readFromBundle() were called
+ * previously, this uses the standard linker (i.e. System.loadLibrary()).
+ *
+ * @param library The library's base name.
+ * @throws UnsatisfiedLinkError if the library does not exist.
+ */
+ public static void loadLibrary(String library) {
+ if (DEBUG) Log.i(TAG, "loadLibrary: " + library);
+
+ synchronized (TAG) {
+ // Don't self-load the linker. This is because the build system is
+ // not clever enough to understand that all the libraries packaged
+ // in the final .apk don't need to be explicitely loaded.
+ if (library.startsWith("crazy_linker")) {
+ if (DEBUG) Log.i(TAG, "ignoring self-linker load");
+ return;
+ }
+
+ if (!sInitialized) {
+ if (DEBUG) Log.i(TAG, "Initializing Linker");
+ Linker.randomizeBaseLoadAddress();
+ Linker.enableRelroSharing();
+ if (DEBUG) Log.i(TAG, "Loading libcrazy_linker.so");
+ try {
+ System.loadLibrary("crazy_linker");
+ } catch (UnsatisfiedLinkError e) {
+ // In a component build, the library name is crazy_linker.cr
+ System.loadLibrary("crazy_linker.cr");
+ }
+ sInitialized = true;
+ }
+
+ String libName = System.mapLibraryName(library);
+
+ if (sLoadedLibraries == null)
+ sLoadedLibraries = new LibInfoMap();
+
+ if (sLoadedLibraries.containsKey(libName)) {
+ if (DEBUG) Log.i(TAG, "Not loading " + libName + " twice");
+ return;
+ }
+
+ LibInfo libInfo = new LibInfo();
+ LibInfo relroLibInfo = null;
+ long loadAddress = sCurrentLoadAddress;
+
+ if (sRelroLibraries != null) {
+ relroLibInfo = sRelroLibraries.get(libName);
+ if (relroLibInfo != null) {
+ loadAddress = relroLibInfo.mLoadAddress;
+ if (DEBUG) Log.i(TAG, "Loading library " + libName + " at " + loadAddress);
+ }
+ }
+
+ if (!nativeLoadLibrary(libName, loadAddress, libInfo)) {
+ Log.e(TAG, "Unable to load library: " + libName);
+ throw new UnsatisfiedLinkError("Unable to load library: " + libName);
+ }
+ // Ensure next library will be loaded at an appropriate address.
+ sCurrentLoadAddress = libInfo.mLoadAddress + libInfo.mLoadSize;
+
+ if (sRelroSharingEnabled) {
+ if (!nativeEnableSharedRelro(libName, libInfo))
+ Log.w(TAG, "Could not enable RELRO sharing for " + libName);
+ } else if (relroLibInfo != null) {
+ if (!nativeUseSharedRelro(libName, relroLibInfo))
+ Log.w(TAG, "Could not use RELRO sharing for " + libName);
+ else {
+ // Replace libInfo with relroLibInfo since it contains
+ // actual RELRO information.
+ libInfo = relroLibInfo;
+ }
+ }
+
+ sLoadedLibraries.put(libName, libInfo);
+ if (DEBUG) Log.i(TAG, "Library details " + libInfo.toString());
+ }
+ }
+
+ // 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 relro_info If not null, this LibInfo instance will be populated
+ // with the library's information.
+ private static native boolean nativeLoadLibrary(String library,
+ long loadAddress,
+ LibInfo libInfo);
+
+ // Native method used to enable RELRO section sharing. To be called
+ // in the browser process only.
+ // @param library Library name.
+ // @param libInfo LibInfo instance populated on success.
+ // @return true on success.
+ private static native boolean nativeEnableSharedRelro(String library,
+ LibInfo libInfo);
+
+ // Native method used to use RELRO section sharing. To be called
+ // in service processes only.
+ // @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);
+
+ // Record information for a given library.
+ // IMPORTANT: Native code knows anout this class's fields, so
+ // don't change them without modifying base/android/linker/crazy_linker.cpp too.
+ public static class LibInfo implements Parcelable {
+
+ public LibInfo() {
+ mLoadAddress = 0;
+ mLoadSize = 0;
+ mRelroStart = 0;
+ mRelroSize = 0;
+ mRelroFd = -1;
+ }
+
+ // from Parcelable
+ public LibInfo(Parcel in) {
+ mLoadAddress = in.readLong();
+ mLoadSize = in.readLong();
+ mRelroStart = in.readLong();
+ mRelroSize = in.readLong();
+ mRelroFd = in.readFileDescriptor().detachFd();
+ }
+
+ // from Parcelable
+ @Override
+ public void writeToParcel(Parcel out, int flags) {
+ if (mRelroFd >= 0) {
+ out.writeLong(mLoadAddress);
+ out.writeLong(mLoadSize);
+ out.writeLong(mRelroStart);
+ out.writeLong(mRelroSize);
+ ParcelFileDescriptor fd = ParcelFileDescriptor.adoptFd(mRelroFd);
+ fd.writeToParcel(out, 0);
+ fd.detachFd();
+ }
+ }
+
+ // from Parcelable
+ @Override
+ public int describeContents() {
+ return Parcelable.CONTENTS_FILE_DESCRIPTOR;
+ }
+
+ // from Parcelable
+ public static final Parcelable.Creator<LibInfo> CREATOR
+ = new Parcelable.Creator<LibInfo>() {
+ public LibInfo createFromParcel(Parcel in) {
+ return new LibInfo(in);
+ }
+
+ public LibInfo[] newArray(int size) {
+ return new LibInfo[size];
+ }
+ };
+
+ public String toString() {
+ return String.format("[load=%x-%x relro=%x-%x fd=%d]",
+ mLoadAddress,
+ mLoadAddress + mLoadSize,
+ mRelroStart,
+ mRelroStart + mRelroSize,
+ mRelroFd);
+ }
+
+ public long mLoadAddress; // page-aligned load address.
+ public long mLoadSize; // page-aligned load size.
+ public long mRelroStart; // page-aligned address in memory, or 0 if none.
+ public long mRelroSize; // page-aligned size in memory, or 0.
+ public int mRelroFd; // ashmem file descriptor, or -1
+ }
+
+ // A class that map library names to LibInfo objects, with support
+ // for reading and writing from/to parcels.
+ private static class LibInfoMap extends HashMap<String, LibInfo> {
+ public LibInfoMap() {
+ super();
+ }
+
+ // Serialize into a Bundle that can be passed to a different
+ // process through Binder. Note that this includes file descriptors,
+ // this it can't be serialized to disk or added to an Intent.
+ public Bundle toBundle() {
+ Bundle bundle = new Bundle(size());
+ for (Map.Entry<String, LibInfo> entry : entrySet())
+ bundle.putParcelable(entry.getKey(), entry.getValue());
+
+ return bundle;
+ }
+
+ // Initialized from a Bundle like the one generated from
+ // toBundle().
+ // @param bundle Input bundle.
+ public LibInfoMap(Bundle bundle) {
+ super();
+
+ // Ensure the bundle uses the application's class loader,
+ // not the framework one which doesn't know anything about
+ // LibInfo.
+ bundle.setClassLoader(getClass().getClassLoader());
+
+ for (String library : bundle.keySet()) {
+ LibInfo libInfo = bundle.getParcelable(library);
+ put(library, libInfo);
+ }
+ }
+ }
+
+ // The list of libraries that are currently loaded in this process.
+ private static LibInfoMap sLoadedLibraries = null;
+
+ // The list of libraries that can share their RELRO section.
+ private static LibInfoMap sRelroLibraries = null;
+}
« no previous file with comments | « no previous file | base/android/linker/crazy_linker.cpp » ('j') | base/android/linker/crazy_linker.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698