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 {
// report.
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
* before calling System.loadLibrary, this only applies when not using the chromium linker.
......@@ -186,7 +221,7 @@ public class LibraryLoader {
*/
public void preloadNowOverrideApplicationContext(Context appContext) {
synchronized (mLock) {
if (!Linker.isUsed()) {
if (!useCrazyLinker()) {
preloadAlreadyLocked(appContext);
}
}
......@@ -195,7 +230,7 @@ public class LibraryLoader {
private void preloadAlreadyLocked(Context appContext) {
try (TraceEvent te = TraceEvent.scoped("LibraryLoader.preloadAlreadyLocked")) {
// Preloader uses system linker, we shouldn't preload if Chromium linker is used.
assert !Linker.isUsed();
assert !useCrazyLinker();
if (mLibraryPreloader != null && !mLibraryPreloaderCalled) {
mLibraryPreloaderStatus = mLibraryPreloader.loadLibrary(appContext);
mLibraryPreloaderCalled = true;
......@@ -396,12 +431,12 @@ public class LibraryLoader {
long startTime = SystemClock.uptimeMillis();
if (Linker.isUsed()) {
if (useCrazyLinker()) {
// Load libraries using the Chromium linker.
Linker linker = Linker.getInstance();
String apkFilePath =
Linker.isInZipFile() ? appContext.getApplicationInfo().sourceDir : null;
isInZipFile() ? appContext.getApplicationInfo().sourceDir : null;
linker.prepareLibraryLoad(apkFilePath);
for (String library : NativeLibraries.LIBRARIES) {
......@@ -427,7 +462,7 @@ public class LibraryLoader {
linker, apkFilePath, libFilePath);
incrementRelinkerCountNotHitHistogram();
} catch (UnsatisfiedLinkError e) {
if (!Linker.isInZipFile()
if (!isInZipFile()
&& PLATFORM_REQUIRES_NATIVE_FALLBACK_EXTRACTION) {
loadLibraryWithCustomLinkerAlreadyLocked(
linker, null, getExtractedLibraryPath(appContext, library));
......@@ -447,12 +482,12 @@ public class LibraryLoader {
// 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
// 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.
for (String library : NativeLibraries.LIBRARIES) {
try {
if (!Linker.isInZipFile()) {
if (!isInZipFile()) {
// The extract and retry logic isn't needed because this path is
// used only for local development.
System.loadLibrary(library);
......@@ -632,7 +667,7 @@ public class LibraryLoader {
// onNativeInitializationComplete().
private void recordBrowserProcessHistogramAlreadyLocked() {
assert Thread.holdsLock(mLock);
if (Linker.isUsed()) {
if (useCrazyLinker()) {
nativeRecordChromiumAndroidLinkerBrowserHistogram(mIsUsingBrowserSharedRelros,
mLoadAtFixedAddressFailed,
mLibraryWasLoadedFromApk ? LibraryLoadFromApkStatusCodes.SUCCESSFUL
......@@ -651,7 +686,7 @@ public class LibraryLoader {
public void registerRendererProcessHistogram(boolean requestedSharedRelro,
boolean loadAtFixedAddressFailed) {
synchronized (mLock) {
if (Linker.isUsed()) {
if (useCrazyLinker()) {
nativeRegisterChromiumAndroidLinkerRendererHistogram(
requestedSharedRelro, loadAtFixedAddressFailed, mLibraryLoadTimeMs);
}
......
......@@ -83,10 +83,11 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe
mCpuFeatures = connectionBundle.getLong(ContentChildProcessConstants.EXTRA_CPU_FEATURES);
assert mCpuCount > 0;
Bundle sharedRelros = connectionBundle.getBundle(Linker.EXTRA_LINKER_SHARED_RELROS);
if (sharedRelros != null) {
getLinker().useSharedRelros(sharedRelros);
sharedRelros = null;
if (LibraryLoader.useCrazyLinker()) {
Bundle sharedRelros = connectionBundle.getBundle(Linker.EXTRA_LINKER_SHARED_RELROS);
if (sharedRelros != null) {
getLinker().useSharedRelros(sharedRelros);
}
}
}
......@@ -109,7 +110,7 @@ public class ContentChildProcessServiceDelegate implements ChildProcessServiceDe
Linker linker = null;
boolean requestedSharedRelro = false;
if (Linker.isUsed()) {
if (LibraryLoader.useCrazyLinker()) {
assert mLinkerParams != null;
linker = getLinker();
if (mLinkerParams.mWaitForSharedRelro) {
......
......@@ -23,6 +23,7 @@ import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.Linker;
import org.chromium.base.process_launcher.ChildConnectionAllocator;
import org.chromium.base.process_launcher.ChildProcessConnection;
......@@ -122,7 +123,7 @@ public final class ChildProcessLauncherHelperImpl {
ContentChildProcessConstants.EXTRA_CPU_COUNT, CpuFeatures.getCount());
connectionBundle.putLong(
ContentChildProcessConstants.EXTRA_CPU_FEATURES, CpuFeatures.getMask());
if (Linker.isUsed()) {
if (LibraryLoader.useCrazyLinker()) {
connectionBundle.putBundle(Linker.EXTRA_LINKER_SHARED_RELROS,
Linker.getInstance().getSharedRelros());
}
......@@ -541,7 +542,7 @@ public final class ChildProcessLauncherHelperImpl {
private static void initLinker() {
assert LauncherThread.runningOnLauncherThread();
if (sLinkerInitialized) return;
if (Linker.isUsed()) {
if (LibraryLoader.useCrazyLinker()) {
sLinkerLoadAddress = Linker.getInstance().getBaseLoadAddress();
if (sLinkerLoadAddress == 0) {
Log.i(TAG, "Shared RELRO support disabled!");
......
......@@ -81,8 +81,7 @@ public class ChromiumLinkerTestActivity extends Activity {
linker.setMemoryDeviceConfigForTesting(memoryDeviceConfig);
// Setup the TestRunner class name.
linker.setTestRunnerClassNameForTesting(
"org.chromium.chromium_linker_test_apk.LinkerTests");
Linker.setupForTesting("org.chromium.chromium_linker_test_apk.LinkerTests");
// Load the library in the browser process, this will also run the test
// 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