Commit 2f2b401b authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Android: Make Linker.java optimized out when use_chromium_linker=false

Two fixes:
1) Move static getters to a separate LibraryLoaderConfig.java
   * Required because of the non-trivial class initializer in
     LibraryLoader preventing inlining.
2) Change JNI proguard rules to not -keep natives when the linker
   is not used.

TBR=agrieve  # Renames

Change-Id: I0f4a40af6168cbe7243f462c24d230d277e642ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1703155
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarSam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#692146}
parent 8e3fb2e2
......@@ -39,10 +39,6 @@
-keep class com.android.webview.chromium.DrawGLFunctor
-keep class com.android.webview.chromium.GraphicsUtils
# Linker dynamically casts to $TestRunner when running tests. We don't run these
# tests in WebView.
-dontnote org.chromium.base.library_loader.Linker$TestRunner
# Don't note about the API 21 compatibility code which references various
# hidden APIs via reflection.
-dontnote com.android.webview.chromium.WebViewDelegateFactory$Api21CompatibilityDelegate
......
......@@ -3258,6 +3258,7 @@ if (is_android) {
"android/java/src/org/chromium/base/compat/ApiHelperForP.java",
"android/java/src/org/chromium/base/library_loader/LegacyLinker.java",
"android/java/src/org/chromium/base/library_loader/LibraryLoader.java",
"android/java/src/org/chromium/base/library_loader/LibraryLoaderConfig.java",
"android/java/src/org/chromium/base/library_loader/LibraryPrefetcher.java",
"android/java/src/org/chromium/base/library_loader/Linker.java",
"android/java/src/org/chromium/base/library_loader/LoaderErrors.java",
......
......@@ -191,7 +191,7 @@ public class LibraryLoader {
*/
public void preloadNowOverrideApplicationContext(Context appContext) {
synchronized (mLock) {
if (useChromiumLinker()) return;
if (LibraryLoaderConfig.useChromiumLinker()) return;
preloadAlreadyLocked(appContext.getApplicationInfo());
}
}
......@@ -199,7 +199,7 @@ public class LibraryLoader {
private void preloadAlreadyLocked(ApplicationInfo appInfo) {
try (TraceEvent te = TraceEvent.scoped("LibraryLoader.preloadAlreadyLocked")) {
// Preloader uses system linker, we shouldn't preload if Chromium linker is used.
assert !useChromiumLinker();
assert !LibraryLoaderConfig.useChromiumLinker();
if (mLibraryPreloader != null && !mLibraryPreloaderCalled) {
mLibraryPreloader.loadLibrary(appInfo);
mLibraryPreloaderCalled = true;
......@@ -340,7 +340,7 @@ public class LibraryLoader {
long startTime = SystemClock.uptimeMillis();
if (useChromiumLinker() && !inZygote) {
if (LibraryLoaderConfig.useChromiumLinker() && !inZygote) {
Linker linker = Linker.getInstance();
// See base/android/linker/config.gni, the chromium linker is only enabled when we
......@@ -559,7 +559,7 @@ public class LibraryLoader {
// Called after all native initializations are complete.
public void onBrowserNativeInitializationComplete() {
synchronized (mLock) {
if (useChromiumLinker()) {
if (LibraryLoaderConfig.useChromiumLinker()) {
RecordHistogram.recordTimesHistogram(
"ChromiumAndroidLinker.BrowserLoadTime", mLibraryLoadTimeMs);
}
......@@ -572,21 +572,12 @@ public class LibraryLoader {
// RecordChromiumAndroidLinkerRendererHistogram() will record it correctly.
public void registerRendererProcessHistogram() {
synchronized (mLock) {
if (useChromiumLinker()) {
if (LibraryLoaderConfig.useChromiumLinker()) {
LibraryLoaderJni.get().recordRendererLibraryLoadTime(mLibraryLoadTimeMs);
}
}
}
/**
* 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 useChromiumLinker() {
return NativeLibraries.sUseLinker;
}
/**
* Override the library loader (normally with a mock) for testing.
* @param loader the mock library loader.
......
// Copyright 2014 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.library_loader;
/**
* Build-time configuration of LibraryLoader.
* These are in a separate class from LibraryLoader to ensure that they are inlined.
*/
public class LibraryLoaderConfig {
private LibraryLoaderConfig() {}
/**
* Check that native library linker tests are enabled.
* If not enabled, calls to testing functions will fail with an assertion
* error.
*
* @return true if native library linker tests are enabled.
*/
public static boolean areTestsEnabled() {
return NativeLibraries.sEnableLinkerTests;
}
/**
* 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 useChromiumLinker() {
return NativeLibraries.sUseLinker;
}
}
......@@ -600,16 +600,6 @@ public abstract class Linker {
}
/* ---------------------- Testing support methods. ---------------------- */
/**
* Check that native library linker tests are enabled.
* If not enabled, calls to testing functions will fail with an assertion
* error.
*
* @return true if native library linker tests are enabled.
*/
public static boolean areTestsEnabled() {
return NativeLibraries.sEnableLinkerTests;
}
/**
* Get Linker implementation type.
......
......@@ -38,7 +38,7 @@
-keepclasseswithmembers class ** {
@org.chromium.base.annotations.UsedByReflection <fields>;
}
-keepclasseswithmembers,includedescriptorclasses class ** {
-keepclasseswithmembers,includedescriptorclasses class !org.chromium.base.library_loader.**,** {
native <methods>;
}
......
# Copyright 2019 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.
-keepclasseswithmembers,includedescriptorclasses class org.chromium.base.library_loader.** {
native <methods>;
}
# Copyright 2019 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.
# All references to Linker.getInstance() should be guarded by:
# LibraryLoaderConfig.useCrazyLinker().
-checkdiscard class org.chromium.base.library_loader.Linker {
*;
}
......@@ -2813,6 +2813,13 @@ if (enable_java_templates) {
if (defined(invoker.proguard_configs)) {
proguard_configs += invoker.proguard_configs
}
if (_use_chromium_linker) {
proguard_configs +=
[ "//base/android/proguard/chromium_linker.flags" ]
} else {
proguard_configs +=
[ "//base/android/proguard/no_chromium_linker.flags" ]
}
if (_enable_main_dex_list) {
proguard_configs += [ "//build/android/multidex.flags" ]
}
......
......@@ -241,6 +241,20 @@ template("chrome_public_common_apk_or_module_tmpl") {
srcjar_deps = [ "//components/module_installer/android:module_installer_apk_build_config" ]
}
if (!defined(use_chromium_linker)) {
use_chromium_linker = chromium_linker_supported
}
if (use_chromium_linker) {
if (!defined(load_library_from_apk)) {
# Whether native libraries should be loaded from within the apk.
# Only attempt loading the library from the APK for 64 bit devices
# until the number of 32 bit devices which don't support this
# approach falls to a minimal level - http://crbug.com/390618.
load_library_from_apk = chromium_linker_supported &&
(current_cpu == "arm64" || current_cpu == "x64")
}
}
if (!is_java_debug) {
proguard_enabled = true
if (!defined(proguard_configs)) {
......@@ -260,20 +274,6 @@ template("chrome_public_common_apk_or_module_tmpl") {
}
}
if (!defined(use_chromium_linker)) {
use_chromium_linker = chromium_linker_supported
}
if (use_chromium_linker) {
if (!defined(load_library_from_apk)) {
# Whether native libraries should be loaded from within the apk.
# Only attempt loading the library from the APK for 64 bit devices
# until the number of 32 bit devices which don't support this
# approach falls to a minimal level - http://crbug.com/390618.
load_library_from_apk = chromium_linker_supported &&
(current_cpu == "arm64" || current_cpu == "x64")
}
}
if (_target_type == "android_apk") {
command_line_flags_file = "chrome-command-line"
} else {
......
......@@ -42,10 +42,6 @@
-keep class com.android.webview.chromium.DrawGLFunctor
-keep class com.android.webview.chromium.GraphicsUtils
# Linker dynamically casts to $TestRunner when running tests. We don't run these
# tests in WebView.
-dontnote org.chromium.base.library_loader.Linker$TestRunner
# Don't note about the API 21 compatibility code which references various
# hidden APIs via reflection.
-dontnote com.android.webview.chromium.WebViewDelegateFactory$Api21CompatibilityDelegate
......@@ -211,7 +207,7 @@
-keepclasseswithmembers class ** {
@org.chromium.base.annotations.UsedByReflection <fields>;
}
-keepclasseswithmembers,includedescriptorclasses class ** {
-keepclasseswithmembers,includedescriptorclasses class !org.chromium.base.library_loader.**,** {
native <methods>;
}
......@@ -273,6 +269,19 @@
-renamesourcefileattribute PG
-repackageclasses ''
################################################################################
# ../../base/android/proguard/no_chromium_linker.flags
################################################################################
# Copyright 2019 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.
# All references to Linker.getInstance() should be guarded by:
# LibraryLoaderConfig.useCrazyLinker().
-checkdiscard class org.chromium.base.library_loader.Linker {
*;
}
################################################################################
# ../../build/android/buildhooks/proguard/build_hooks_android_impl.flags
################################################################################
......
......@@ -19,7 +19,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.Log;
import org.chromium.base.compat.ApiHelperForM;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.LibraryLoaderConfig;
import org.chromium.base.library_loader.LoadStatusRecorder.LoadLibraryStatus;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.CommandLineFlags;
......@@ -159,7 +159,7 @@ public class StartupLoadingMetricsTest {
assertHistogramsRecorded(1, TABBED_SUFFIX);
// LibraryLoader checks.
if (!LibraryLoader.useChromiumLinker()) {
if (!LibraryLoaderConfig.useChromiumLinker()) {
Log.w(TAG, "Skipping test because not using ChromiumLinker.");
return;
}
......
......@@ -20,6 +20,7 @@ import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.MainDex;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.LibraryLoaderConfig;
import org.chromium.base.library_loader.Linker;
import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.base.memory.MemoryPressureUma;
......@@ -82,7 +83,8 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe
mCpuFeatures = connectionBundle.getLong(ContentChildProcessConstants.EXTRA_CPU_FEATURES);
assert mCpuCount > 0;
if (LibraryLoader.useChromiumLinker() && !LibraryLoader.getInstance().isLoadedByZygote()) {
if (LibraryLoaderConfig.useChromiumLinker()
&& !LibraryLoader.getInstance().isLoadedByZygote()) {
Bundle sharedRelros = connectionBundle.getBundle(Linker.EXTRA_LINKER_SHARED_RELROS);
if (sharedRelros != null) getLinker().provideSharedRelros(sharedRelros);
}
......@@ -106,7 +108,7 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe
Linker linker = null;
boolean requestedSharedRelro = false;
if (LibraryLoader.useChromiumLinker()) {
if (LibraryLoaderConfig.useChromiumLinker()) {
assert mLinkerParams != null;
linker = getLinker();
if (mLinkerParams.mWaitForSharedRelro) {
......@@ -184,7 +186,7 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe
// Return a Linker instance. If testing, the Linker needs special setup.
private Linker getLinker() {
if (Linker.areTestsEnabled()) {
if (LibraryLoaderConfig.areTestsEnabled()) {
// For testing, set the Linker implementation and the test runner
// class name to match those used by the parent.
assert mLinkerParams != null;
......
......@@ -24,7 +24,7 @@ import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.LibraryLoaderConfig;
import org.chromium.base.library_loader.Linker;
import org.chromium.base.process_launcher.ChildConnectionAllocator;
import org.chromium.base.process_launcher.ChildProcessConnection;
......@@ -133,7 +133,7 @@ public final class ChildProcessLauncherHelperImpl {
ContentChildProcessConstants.EXTRA_CPU_COUNT, CpuFeatures.getCount());
connectionBundle.putLong(
ContentChildProcessConstants.EXTRA_CPU_FEATURES, CpuFeatures.getMask());
if (LibraryLoader.useChromiumLinker()) {
if (LibraryLoaderConfig.useChromiumLinker()) {
connectionBundle.putBundle(Linker.EXTRA_LINKER_SHARED_RELROS,
Linker.getInstance().getSharedRelros());
}
......@@ -594,7 +594,7 @@ public final class ChildProcessLauncherHelperImpl {
private static void initLinker() {
assert LauncherThread.runningOnLauncherThread();
if (sLinkerInitialized) return;
if (LibraryLoader.useChromiumLinker()) {
if (LibraryLoaderConfig.useChromiumLinker()) {
sLinkerLoadAddress = Linker.getInstance().getBaseLoadAddress();
if (sLinkerLoadAddress == 0) {
Log.i(TAG, "Shared RELRO support disabled!");
......@@ -612,7 +612,7 @@ public final class ChildProcessLauncherHelperImpl {
// Always wait for the shared RELROs in service processes.
final boolean waitForSharedRelros = true;
if (Linker.areTestsEnabled()) {
if (LibraryLoaderConfig.areTestsEnabled()) {
Linker linker = Linker.getInstance();
return new ChromiumLinkerParams(sLinkerLoadAddress, waitForSharedRelros,
linker.getTestRunnerClassNameForTesting(),
......
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