Commit e7571198 authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Misc code clean-up in Linker.java (no behavior change)

* Use assert statements
* Reduce visibility of methods where possible
* Create singleton during class initialization

Change-Id: I60c2da7d8cdb1065eba47c45a3369b8ba0e34780
Reviewed-on: https://chromium-review.googlesource.com/1116281Reviewed-by: default avatarDavid Turner <digit@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: agrieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#572423}
parent 6ef62268
...@@ -135,6 +135,41 @@ public class LibraryLoader { ...@@ -135,6 +135,41 @@ public class LibraryLoader {
// report. // report.
private int mLibraryPreloaderStatus = -1; private int mLibraryPreloaderStatus = -1;
/**
* Call this method to determine if this chromium project must
* use this linker. If not, System.loadLibrary() should be used to load
* libraries instead.
*/
public static boolean useCrazyLinker() {
// TODO(digit): Remove this early return GVR is loadable.
// A non-monochrome APK (such as ChromePublic.apk or ChromeModernPublic.apk) on N+ cannot
// use the Linker because the latter is incompatible with the GVR library. Fall back
// to using System.loadLibrary() or System.load() at the cost of no RELRO sharing.
//
// A non-monochrome APK (such as ChromePublic.apk) can be installed on N+ in these
// circumstances:
// * installing APK manually
// * after OTA from M to N
// * side-installing Chrome (possibly from another release channel)
// * Play Store bugs leading to incorrect APK flavor being installed
//
if (Build.VERSION.SDK_INT >= VERSION_CODES.N) return false;
// The auto-generated NativeLibraries.sUseLinker variable will be true if the
// build has not explicitly disabled Linker features.
return NativeLibraries.sUseLinker;
}
/**
* Call this method to determine if the chromium project must load the library
* directly from a zip file.
*/
private static boolean isInZipFile() {
// The auto-generated NativeLibraries.sUseLibraryInZipFile variable will be true
// iff the library remains embedded in the APK zip file on the target.
return NativeLibraries.sUseLibraryInZipFile;
}
/** /**
* Set native library preloader, if set, the NativeLibraryPreloader.loadLibrary will be invoked * Set native library preloader, if set, the NativeLibraryPreloader.loadLibrary will be invoked
* before calling System.loadLibrary, this only applies when not using the chromium linker. * before calling System.loadLibrary, this only applies when not using the chromium linker.
...@@ -186,7 +221,7 @@ public class LibraryLoader { ...@@ -186,7 +221,7 @@ public class LibraryLoader {
*/ */
public void preloadNowOverrideApplicationContext(Context appContext) { public void preloadNowOverrideApplicationContext(Context appContext) {
synchronized (mLock) { synchronized (mLock) {
if (!Linker.isUsed()) { if (!useCrazyLinker()) {
preloadAlreadyLocked(appContext); preloadAlreadyLocked(appContext);
} }
} }
...@@ -195,7 +230,7 @@ public class LibraryLoader { ...@@ -195,7 +230,7 @@ public class LibraryLoader {
private void preloadAlreadyLocked(Context appContext) { private void preloadAlreadyLocked(Context appContext) {
try (TraceEvent te = TraceEvent.scoped("LibraryLoader.preloadAlreadyLocked")) { try (TraceEvent te = TraceEvent.scoped("LibraryLoader.preloadAlreadyLocked")) {
// Preloader uses system linker, we shouldn't preload if Chromium linker is used. // Preloader uses system linker, we shouldn't preload if Chromium linker is used.
assert !Linker.isUsed(); assert !useCrazyLinker();
if (mLibraryPreloader != null && !mLibraryPreloaderCalled) { if (mLibraryPreloader != null && !mLibraryPreloaderCalled) {
mLibraryPreloaderStatus = mLibraryPreloader.loadLibrary(appContext); mLibraryPreloaderStatus = mLibraryPreloader.loadLibrary(appContext);
mLibraryPreloaderCalled = true; mLibraryPreloaderCalled = true;
...@@ -396,12 +431,12 @@ public class LibraryLoader { ...@@ -396,12 +431,12 @@ public class LibraryLoader {
long startTime = SystemClock.uptimeMillis(); long startTime = SystemClock.uptimeMillis();
if (Linker.isUsed()) { if (useCrazyLinker()) {
// Load libraries using the Chromium linker. // Load libraries using the Chromium linker.
Linker linker = Linker.getInstance(); Linker linker = Linker.getInstance();
String apkFilePath = String apkFilePath =
Linker.isInZipFile() ? appContext.getApplicationInfo().sourceDir : null; isInZipFile() ? appContext.getApplicationInfo().sourceDir : null;
linker.prepareLibraryLoad(apkFilePath); linker.prepareLibraryLoad(apkFilePath);
for (String library : NativeLibraries.LIBRARIES) { for (String library : NativeLibraries.LIBRARIES) {
...@@ -427,7 +462,7 @@ public class LibraryLoader { ...@@ -427,7 +462,7 @@ public class LibraryLoader {
linker, apkFilePath, libFilePath); linker, apkFilePath, libFilePath);
incrementRelinkerCountNotHitHistogram(); incrementRelinkerCountNotHitHistogram();
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
if (!Linker.isInZipFile() if (!isInZipFile()
&& PLATFORM_REQUIRES_NATIVE_FALLBACK_EXTRACTION) { && PLATFORM_REQUIRES_NATIVE_FALLBACK_EXTRACTION) {
loadLibraryWithCustomLinkerAlreadyLocked( loadLibraryWithCustomLinkerAlreadyLocked(
linker, null, getExtractedLibraryPath(appContext, library)); linker, null, getExtractedLibraryPath(appContext, library));
...@@ -447,12 +482,12 @@ public class LibraryLoader { ...@@ -447,12 +482,12 @@ public class LibraryLoader {
// If the libraries are located in the zip file, assert that the device API // If the libraries are located in the zip file, assert that the device API
// level is M or higher. On devices lower than M, the libraries should // level is M or higher. On devices lower than M, the libraries should
// always be loaded by Linker. // always be loaded by Linker.
assert !Linker.isInZipFile() || Build.VERSION.SDK_INT >= VERSION_CODES.M; assert !isInZipFile() || Build.VERSION.SDK_INT >= VERSION_CODES.M;
// Load libraries using the system linker. // Load libraries using the system linker.
for (String library : NativeLibraries.LIBRARIES) { for (String library : NativeLibraries.LIBRARIES) {
try { try {
if (!Linker.isInZipFile()) { if (!isInZipFile()) {
// The extract and retry logic isn't needed because this path is // The extract and retry logic isn't needed because this path is
// used only for local development. // used only for local development.
System.loadLibrary(library); System.loadLibrary(library);
...@@ -632,7 +667,7 @@ public class LibraryLoader { ...@@ -632,7 +667,7 @@ public class LibraryLoader {
// onNativeInitializationComplete(). // onNativeInitializationComplete().
private void recordBrowserProcessHistogramAlreadyLocked() { private void recordBrowserProcessHistogramAlreadyLocked() {
assert Thread.holdsLock(mLock); assert Thread.holdsLock(mLock);
if (Linker.isUsed()) { if (useCrazyLinker()) {
nativeRecordChromiumAndroidLinkerBrowserHistogram(mIsUsingBrowserSharedRelros, nativeRecordChromiumAndroidLinkerBrowserHistogram(mIsUsingBrowserSharedRelros,
mLoadAtFixedAddressFailed, mLoadAtFixedAddressFailed,
mLibraryWasLoadedFromApk ? LibraryLoadFromApkStatusCodes.SUCCESSFUL mLibraryWasLoadedFromApk ? LibraryLoadFromApkStatusCodes.SUCCESSFUL
...@@ -651,7 +686,7 @@ public class LibraryLoader { ...@@ -651,7 +686,7 @@ public class LibraryLoader {
public void registerRendererProcessHistogram(boolean requestedSharedRelro, public void registerRendererProcessHistogram(boolean requestedSharedRelro,
boolean loadAtFixedAddressFailed) { boolean loadAtFixedAddressFailed) {
synchronized (mLock) { synchronized (mLock) {
if (Linker.isUsed()) { if (useCrazyLinker()) {
nativeRegisterChromiumAndroidLinkerRendererHistogram( nativeRegisterChromiumAndroidLinkerRendererHistogram(
requestedSharedRelro, loadAtFixedAddressFailed, mLibraryLoadTimeMs); requestedSharedRelro, loadAtFixedAddressFailed, mLibraryLoadTimeMs);
} }
......
...@@ -83,10 +83,11 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe ...@@ -83,10 +83,11 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe
mCpuFeatures = connectionBundle.getLong(ContentChildProcessConstants.EXTRA_CPU_FEATURES); mCpuFeatures = connectionBundle.getLong(ContentChildProcessConstants.EXTRA_CPU_FEATURES);
assert mCpuCount > 0; assert mCpuCount > 0;
if (LibraryLoader.useCrazyLinker()) {
Bundle sharedRelros = connectionBundle.getBundle(Linker.EXTRA_LINKER_SHARED_RELROS); Bundle sharedRelros = connectionBundle.getBundle(Linker.EXTRA_LINKER_SHARED_RELROS);
if (sharedRelros != null) { if (sharedRelros != null) {
getLinker().useSharedRelros(sharedRelros); getLinker().useSharedRelros(sharedRelros);
sharedRelros = null; }
} }
} }
...@@ -109,7 +110,7 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe ...@@ -109,7 +110,7 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe
Linker linker = null; Linker linker = null;
boolean requestedSharedRelro = false; boolean requestedSharedRelro = false;
if (Linker.isUsed()) { if (LibraryLoader.useCrazyLinker()) {
assert mLinkerParams != null; assert mLinkerParams != null;
linker = getLinker(); linker = getLinker();
if (mLinkerParams.mWaitForSharedRelro) { if (mLinkerParams.mWaitForSharedRelro) {
......
...@@ -23,6 +23,7 @@ import org.chromium.base.ThreadUtils; ...@@ -23,6 +23,7 @@ import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.Linker; import org.chromium.base.library_loader.Linker;
import org.chromium.base.process_launcher.ChildConnectionAllocator; import org.chromium.base.process_launcher.ChildConnectionAllocator;
import org.chromium.base.process_launcher.ChildProcessConnection; import org.chromium.base.process_launcher.ChildProcessConnection;
...@@ -122,7 +123,7 @@ public final class ChildProcessLauncherHelperImpl { ...@@ -122,7 +123,7 @@ public final class ChildProcessLauncherHelperImpl {
ContentChildProcessConstants.EXTRA_CPU_COUNT, CpuFeatures.getCount()); ContentChildProcessConstants.EXTRA_CPU_COUNT, CpuFeatures.getCount());
connectionBundle.putLong( connectionBundle.putLong(
ContentChildProcessConstants.EXTRA_CPU_FEATURES, CpuFeatures.getMask()); ContentChildProcessConstants.EXTRA_CPU_FEATURES, CpuFeatures.getMask());
if (Linker.isUsed()) { if (LibraryLoader.useCrazyLinker()) {
connectionBundle.putBundle(Linker.EXTRA_LINKER_SHARED_RELROS, connectionBundle.putBundle(Linker.EXTRA_LINKER_SHARED_RELROS,
Linker.getInstance().getSharedRelros()); Linker.getInstance().getSharedRelros());
} }
...@@ -541,7 +542,7 @@ public final class ChildProcessLauncherHelperImpl { ...@@ -541,7 +542,7 @@ public final class ChildProcessLauncherHelperImpl {
private static void initLinker() { private static void initLinker() {
assert LauncherThread.runningOnLauncherThread(); assert LauncherThread.runningOnLauncherThread();
if (sLinkerInitialized) return; if (sLinkerInitialized) return;
if (Linker.isUsed()) { if (LibraryLoader.useCrazyLinker()) {
sLinkerLoadAddress = Linker.getInstance().getBaseLoadAddress(); sLinkerLoadAddress = Linker.getInstance().getBaseLoadAddress();
if (sLinkerLoadAddress == 0) { if (sLinkerLoadAddress == 0) {
Log.i(TAG, "Shared RELRO support disabled!"); Log.i(TAG, "Shared RELRO support disabled!");
......
...@@ -81,8 +81,7 @@ public class ChromiumLinkerTestActivity extends Activity { ...@@ -81,8 +81,7 @@ public class ChromiumLinkerTestActivity extends Activity {
linker.setMemoryDeviceConfigForTesting(memoryDeviceConfig); linker.setMemoryDeviceConfigForTesting(memoryDeviceConfig);
// Setup the TestRunner class name. // Setup the TestRunner class name.
linker.setTestRunnerClassNameForTesting( Linker.setupForTesting("org.chromium.chromium_linker_test_apk.LinkerTests");
"org.chromium.chromium_linker_test_apk.LinkerTests");
// Load the library in the browser process, this will also run the test // Load the library in the browser process, this will also run the test
// runner in this process. // runner in this process.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment