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

Side by Side Diff: content/public/android/java/src/org/chromium/content/app/LibraryLoader.java

Issue 23717023: Android: Add chrome-specific dynamic linker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add new 'content_linker_unittests_apk' target + ensure ashmem regions are forced read-only 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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.content.app; 5 package org.chromium.content.app;
6 6
7 import android.text.TextUtils; 7 import android.text.TextUtils;
8 import android.util.Log; 8 import android.util.Log;
9 9
10 import org.chromium.base.Linker;
10 import org.chromium.base.JNINamespace; 11 import org.chromium.base.JNINamespace;
12 import org.chromium.content.browser.ChildProcessLauncher;
11 import org.chromium.content.common.CommandLine; 13 import org.chromium.content.common.CommandLine;
12 import org.chromium.content.common.ProcessInitException; 14 import org.chromium.content.common.ProcessInitException;
13 import org.chromium.content.common.ResultCodes; 15 import org.chromium.content.common.ResultCodes;
14 import org.chromium.content.common.TraceEvent; 16 import org.chromium.content.common.TraceEvent;
15 17
16 /** 18 /**
17 * This class provides functionality to load and register the native libraries. 19 * This class provides functionality to load and register the native libraries.
18 * Callers are allowed to separate loading the libraries from initializing them. 20 * Callers are allowed to separate loading the libraries from initializing them.
19 * This may be an advantage for Android Webview, where the libraries can be load ed 21 * This may be an advantage for Android Webview, where the libraries can be load ed
20 * by the zygote process, but then needs per process initialization after the 22 * by the zygote process, but then needs per process initialization after the
(...skipping 14 matching lines...) Expand all
35 private static final Object sLock = new Object(); 37 private static final Object sLock = new Object();
36 38
37 // One-way switch becomes true when the libraries are loaded. 39 // One-way switch becomes true when the libraries are loaded.
38 private static boolean sLoaded = false; 40 private static boolean sLoaded = false;
39 41
40 // One-way switch becomes true when the libraries are initialized ( 42 // One-way switch becomes true when the libraries are initialized (
41 // by calling nativeLibraryLoaded, which forwards to LibraryLoaded(...) in 43 // by calling nativeLibraryLoaded, which forwards to LibraryLoaded(...) in
42 // library_loader_hooks.cc). 44 // library_loader_hooks.cc).
43 private static boolean sInitialized = false; 45 private static boolean sInitialized = false;
44 46
47 public static boolean useCrazyLinker() {
48 return NativeLibraries.USE_CRAZY_LINKER;
49 }
50
45 // TODO(cjhopman): Remove this once it's unused. 51 // TODO(cjhopman): Remove this once it's unused.
46 /** 52 /**
47 * Doesn't do anything. 53 * Doesn't do anything.
48 */ 54 */
49 @Deprecated 55 @Deprecated
50 public static void setLibraryToLoad(String library) { 56 public static void setLibraryToLoad(String library) {
51 } 57 }
52 58
53 /** 59 /**
54 * This method blocks until the library is fully loaded and initialized. 60 * This method blocks until the library is fully loaded and initialized.
55 */ 61 */
56 public static void ensureInitialized() throws ProcessInitException { 62 public static void ensureInitialized() throws ProcessInitException {
57 synchronized (sLock) { 63 synchronized (sLock) {
58 if (sInitialized) { 64 if (sInitialized) {
59 // Already initialized, nothing to do. 65 // Already initialized, nothing to do.
60 return; 66 return;
61 } 67 }
62 loadAlreadyLocked(); 68 loadAlreadyLocked();
63 initializeAlreadyLocked(CommandLine.getJavaSwitchesOrNull()); 69 initializeAlreadyLocked(CommandLine.getJavaSwitchesOrNull());
70 if (useCrazyLinker())
71 ChildProcessLauncher.setRelroBundle(Linker.getRelroBundle());
64 } 72 }
65 } 73 }
66 74
67 /** 75 /**
68 * Checks if library is fully loaded and initialized. 76 * Checks if library is fully loaded and initialized.
69 */ 77 */
70 public static boolean isInitialized() { 78 public static boolean isInitialized() {
71 synchronized (sLock) { 79 synchronized (sLock) {
72 return sInitialized; 80 return sInitialized;
73 } 81 }
(...skipping 28 matching lines...) Expand all
102 } 110 }
103 } 111 }
104 112
105 113
106 // Invoke System.loadLibrary(...), triggering JNI_OnLoad in native code 114 // Invoke System.loadLibrary(...), triggering JNI_OnLoad in native code
107 private static void loadAlreadyLocked() throws ProcessInitException { 115 private static void loadAlreadyLocked() throws ProcessInitException {
108 try { 116 try {
109 if (!sLoaded) { 117 if (!sLoaded) {
110 assert !sInitialized; 118 assert !sInitialized;
111 for (String sLibrary : NativeLibraries.libraries) { 119 for (String sLibrary : NativeLibraries.libraries) {
112 Log.i(TAG, "loading: " + sLibrary); 120 if (useCrazyLinker()) {
113 System.loadLibrary(sLibrary); 121 Linker.loadLibrary(sLibrary);
114 Log.i(TAG, "loaded: " + sLibrary); 122 } else if (!sLibrary.equals("crazy_linker")) {
123 System.loadLibrary(sLibrary);
124 }
115 } 125 }
116 sLoaded = true; 126 sLoaded = true;
117 } 127 }
118 } catch (UnsatisfiedLinkError e) { 128 } catch (UnsatisfiedLinkError e) {
119 throw new ProcessInitException(ResultCodes.RESULT_CODE_NATIVE_LIBRAR Y_LOAD_FAILED, e); 129 throw new ProcessInitException(ResultCodes.RESULT_CODE_NATIVE_LIBRAR Y_LOAD_FAILED, e);
120 } 130 }
121 } 131 }
122 132
123 133
124 // Invoke content::LibraryLoaded in library_loader_hooks.cc 134 // Invoke content::LibraryLoaded in library_loader_hooks.cc
125 private static void initializeAlreadyLocked(String[] initCommandLine) 135 private static void initializeAlreadyLocked(String[] initCommandLine)
126 throws ProcessInitException { 136 throws ProcessInitException {
127 if (sInitialized) { 137 if (sInitialized) {
128 return; 138 return;
129 } 139 }
130 int resultCode = nativeLibraryLoaded(initCommandLine); 140 int resultCode = nativeLibraryLoaded(initCommandLine);
131 if (resultCode != 0) { 141 if (resultCode != 0) {
132 Log.e(TAG, "error calling nativeLibraryLoaded"); 142 Log.e(TAG, "error calling nativeLibraryLoaded");
133 throw new ProcessInitException(resultCode); 143 throw new ProcessInitException(resultCode);
134 } 144 }
135 // From this point on, native code is ready to use and checkIsReady() 145 // From this point on, native code is ready to use and checkIsReady()
136 // shouldn't complain from now on (and in fact, it's used by the 146 // shouldn't complain from now on (and in fact, it's used by the
137 // following calls). 147 // following calls).
138 sInitialized = true; 148 sInitialized = true;
139 CommandLine.enableNativeProxy(); 149 CommandLine.enableNativeProxy();
140 TraceEvent.setEnabledToMatchNative(); 150 TraceEvent.setEnabledToMatchNative();
151 // Record histogram for crazy linker.
152 if (useCrazyLinker())
153 nativeRecordCrazyLinkerHistogram(Linker.loadAtFixedAddressFailed());
141 } 154 }
142 155
143 // This is the only method that is registered during System.loadLibrary. We then call it 156 // This is the only method that is registered during System.loadLibrary. We then call it
144 // to register everything else. This process is called "initialization". 157 // to register everything else. This process is called "initialization".
145 // This method will be mapped (by generated code) to the LibraryLoaded 158 // This method will be mapped (by generated code) to the LibraryLoaded
146 // definition in content/app/android/library_loader_hooks.cc. 159 // definition in content/app/android/library_loader_hooks.cc.
147 // 160 //
148 // Return 0 on success, otherwise return the error code from 161 // Return 0 on success, otherwise return the error code from
149 // content/public/common/result_codes.h. 162 // content/public/common/result_codes.h.
150 private static native int nativeLibraryLoaded(String[] initCommandLine); 163 private static native int nativeLibraryLoaded(String[] initCommandLine);
164
165 // Method called to register if the crazy linker was able to load library at a fixed address.
166 // This is called once the library loading is done and successful.
167 private static native void nativeRecordCrazyLinkerHistogram(
168 boolean loadedAtFixedAddressFailed);
151 } 169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698