From 6b32012abd003790cea7252fd3e272b144027835 Mon Sep 17 00:00:00 2001 From: Adrian Catana Date: Tue, 17 Oct 2023 04:12:45 -0700 Subject: [PATCH] Rename ApkSoSource to BackupSoSource Summary: As in the title. Rename to better show its purpose. Also, add more documentation around it. Part of a larger effort to unify all ApkSoSources into one abstraction that's easier to handle. Reviewed By: simpleton Differential Revision: D50302416 fbshipit-source-id: d359a0c1500cd70a2da49cda9db6a33160fc0bf6 --- .../{ApkSoSource.java => BackupSoSource.java} | 18 +++++++++------ java/com/facebook/soloader/SoLoader.java | 18 +++++++-------- .../DefaultRecoveryStrategyFactory.java | 4 ++-- ...rces.java => ReunpackBackupSoSources.java} | 22 +++++++++---------- ...s.java => ReunpackNonBackupSoSources.java} | 8 +++---- 5 files changed, 37 insertions(+), 33 deletions(-) rename java/com/facebook/soloader/{ApkSoSource.java => BackupSoSource.java} (91%) rename java/com/facebook/soloader/recovery/{ReunpackApkSoSources.java => ReunpackBackupSoSources.java} (77%) rename java/com/facebook/soloader/recovery/{ReunpackNonApkSoSources.java => ReunpackNonBackupSoSources.java} (90%) diff --git a/java/com/facebook/soloader/ApkSoSource.java b/java/com/facebook/soloader/BackupSoSource.java similarity index 91% rename from java/com/facebook/soloader/ApkSoSource.java rename to java/com/facebook/soloader/BackupSoSource.java index b7d267b..8671d25 100644 --- a/java/com/facebook/soloader/ApkSoSource.java +++ b/java/com/facebook/soloader/BackupSoSource.java @@ -22,10 +22,14 @@ import java.io.IOException; import java.util.zip.ZipEntry; -/** {@link SoSource} that extracts libraries from an APK to the filesystem. */ -public class ApkSoSource extends ExtractFromZipSoSource { +/** + * {@link SoSource} that extracts zipped libraries from an APK to the filesystem. This is a + * workaround for a known OS bug where the unpacking of non-asset zipped libraries at install time + * results in corrupted libraries (e.g. bad elf magic, or truncated files). + */ +public class BackupSoSource extends ExtractFromZipSoSource { - private static final String TAG = "ApkSoSource"; + private static final String TAG = "BackupSoSource"; /** * If this flag is given, do not extract libraries that appear to be correctly extracted to the @@ -40,11 +44,11 @@ public class ApkSoSource extends ExtractFromZipSoSource { private final int mFlags; - public ApkSoSource(Context context, String name, int flags) { + public BackupSoSource(Context context, String name, int flags) { this(context, new File(context.getApplicationInfo().sourceDir), name, flags); } - public ApkSoSource(Context context, File apkPath, String name, int flags) { + public BackupSoSource(Context context, File apkPath, String name, int flags) { super( context, name, @@ -57,7 +61,7 @@ public ApkSoSource(Context context, File apkPath, String name, int flags) { @Override public String getName() { - return "ApkSoSource"; + return "BackupSoSource"; } public boolean hasZippedLibs() throws IOException { @@ -81,7 +85,7 @@ protected class ApkUnpacker extends ZipUnpacker { super(soSource); mForceUnpacking = forceUnpacking; mLibDir = new File(mContext.getApplicationInfo().nativeLibraryDir); - mFlags = ApkSoSource.this.mFlags; + mFlags = BackupSoSource.this.mFlags; } @Override diff --git a/java/com/facebook/soloader/SoLoader.java b/java/com/facebook/soloader/SoLoader.java index f947c1a..02f8681 100644 --- a/java/com/facebook/soloader/SoLoader.java +++ b/java/com/facebook/soloader/SoLoader.java @@ -358,7 +358,7 @@ private static void initSoSources(Context context, int flags) throws IOException addDirectApkSoSource(context, soSources); } addApplicationSoSource(soSources, getApplicationSoSourceFlags()); - addBackupSoSource(context, soSources, ApkSoSource.PREFER_ANDROID_LIBS_DIRECTORY); + addBackupSoSource(context, soSources, BackupSoSource.PREFER_ANDROID_LIBS_DIRECTORY); } } @@ -440,7 +440,7 @@ private static void addApplicationSoSource(ArrayList soSources, int fl /** Add the SoSources for recovering the dso if the file is corrupted or missed */ private static void addBackupSoSource( - Context context, ArrayList soSources, int apkSoSourceFlags) throws IOException { + Context context, ArrayList soSources, int backupSoSourceFlags) throws IOException { if ((sFlags & SOLOADER_DISABLE_BACKUP_SOSOURCE) != 0) { // Clean up backups final File backupDir = UnpackingSoSource.getSoStorePath(context, SO_STORE_NAME_MAIN); @@ -454,30 +454,30 @@ private static void addBackupSoSource( final File mainApkDir = new File(context.getApplicationInfo().sourceDir); ArrayList backupSources = new ArrayList<>(); - ApkSoSource mainApkSource = - new ApkSoSource(context, mainApkDir, SO_STORE_NAME_MAIN, apkSoSourceFlags); + BackupSoSource mainApkSource = + new BackupSoSource(context, mainApkDir, SO_STORE_NAME_MAIN, backupSoSourceFlags); backupSources.add(mainApkSource); LogUtil.d(TAG, "adding backup source from : " + mainApkSource.toString()); - addBackupSoSourceFromSplitApk(context, apkSoSourceFlags, backupSources); + addBackupSoSourceFromSplitApk(context, backupSoSourceFlags, backupSources); soSources.addAll(0, backupSources); } private static void addBackupSoSourceFromSplitApk( - Context context, int apkSoSourceFlags, ArrayList backupSources) + Context context, int backupSoSourceFlags, ArrayList backupSources) throws IOException { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && context.getApplicationInfo().splitSourceDirs != null) { LogUtil.d(TAG, "adding backup sources from split apks"); int splitIndex = 0; for (String splitApkDir : context.getApplicationInfo().splitSourceDirs) { - ApkSoSource splitApkSource = - new ApkSoSource( + BackupSoSource splitApkSource = + new BackupSoSource( context, new File(splitApkDir), SO_STORE_NAME_SPLIT + (splitIndex++), - apkSoSourceFlags); + backupSoSourceFlags); LogUtil.d(TAG, "adding backup source: " + splitApkSource.toString()); if (splitApkSource.hasZippedLibs()) { backupSources.add(splitApkSource); diff --git a/java/com/facebook/soloader/recovery/DefaultRecoveryStrategyFactory.java b/java/com/facebook/soloader/recovery/DefaultRecoveryStrategyFactory.java index 4d24811..aec1c73 100644 --- a/java/com/facebook/soloader/recovery/DefaultRecoveryStrategyFactory.java +++ b/java/com/facebook/soloader/recovery/DefaultRecoveryStrategyFactory.java @@ -38,8 +38,8 @@ public RecoveryStrategy get() { // explicit SimpleRetry. new WaitForAsyncInit(), new DetectDataAppMove(mContext, mBaseApkPathHistory), - new ReunpackApkSoSources(), - new ReunpackNonApkSoSources(), + new ReunpackBackupSoSources(), + new ReunpackNonBackupSoSources(), new WaitForAsyncInit(), new CheckBaseApkExists(mContext, mBaseApkPathHistory)); } diff --git a/java/com/facebook/soloader/recovery/ReunpackApkSoSources.java b/java/com/facebook/soloader/recovery/ReunpackBackupSoSources.java similarity index 77% rename from java/com/facebook/soloader/recovery/ReunpackApkSoSources.java rename to java/com/facebook/soloader/recovery/ReunpackBackupSoSources.java index 8160561..e95aef2 100644 --- a/java/com/facebook/soloader/recovery/ReunpackApkSoSources.java +++ b/java/com/facebook/soloader/recovery/ReunpackBackupSoSources.java @@ -16,7 +16,7 @@ package com.facebook.soloader.recovery; -import com.facebook.soloader.ApkSoSource; +import com.facebook.soloader.BackupSoSource; import com.facebook.soloader.LogUtil; import com.facebook.soloader.SoLoader; import com.facebook.soloader.SoLoaderDSONotFoundError; @@ -25,12 +25,12 @@ /** * RecoveryStrategy that detects cases when SoLoader failed to load a corrupted library, case in - * which we try to re-unpack the libraries. Only for ApkSoSources + * which we try to re-unpack the libraries. Only for BackupSoSources * *

Guards against a known Android OS bug where the installer incorrectly unpacks libraries under * /data/app */ -public class ReunpackApkSoSources implements RecoveryStrategy { +public class ReunpackBackupSoSources implements RecoveryStrategy { @Override public boolean recover(UnsatisfiedLinkError error, SoSource[] soSources) { @@ -54,27 +54,27 @@ public boolean recover(UnsatisfiedLinkError error, SoSource[] soSources) { String soName = err.getSoName(); LogUtil.e( SoLoader.TAG, - "Reunpacking ApkSoSources due to " + "Reunpacking BackupSoSources due to " + error + ((soName == null) ? "" : (", retrying for specific library " + soName))); for (SoSource soSource : soSources) { - if (!(soSource instanceof ApkSoSource)) { - // NonApk SoSources get reunpacked in ReunpackNonApkSoSource recovery strategy + if (!(soSource instanceof BackupSoSource)) { + // NonApk SoSources get reunpacked in ReunpackNonBackupSoSource recovery strategy continue; } - ApkSoSource apkSource = (ApkSoSource) soSource; + BackupSoSource backupSoSource = (BackupSoSource) soSource; try { - LogUtil.e(SoLoader.TAG, "Runpacking ApkSoSource " + apkSource.getName()); - apkSource.prepareForceRefresh(); + LogUtil.e(SoLoader.TAG, "Runpacking BackupSoSource " + backupSoSource.getName()); + backupSoSource.prepareForceRefresh(); } catch (Exception e) { // Catch a general error and log it, rather than failing during recovery and crashing the // app // in a different way. LogUtil.e( SoLoader.TAG, - "Encountered an exception while reunpacking ApkSoSource " - + apkSource.getName() + "Encountered an exception while reunpacking BackupSoSource " + + backupSoSource.getName() + " for library " + soName + ": ", diff --git a/java/com/facebook/soloader/recovery/ReunpackNonApkSoSources.java b/java/com/facebook/soloader/recovery/ReunpackNonBackupSoSources.java similarity index 90% rename from java/com/facebook/soloader/recovery/ReunpackNonApkSoSources.java rename to java/com/facebook/soloader/recovery/ReunpackNonBackupSoSources.java index 9b0fb52..98b982b 100644 --- a/java/com/facebook/soloader/recovery/ReunpackNonApkSoSources.java +++ b/java/com/facebook/soloader/recovery/ReunpackNonBackupSoSources.java @@ -16,7 +16,7 @@ package com.facebook.soloader.recovery; -import com.facebook.soloader.ApkSoSource; +import com.facebook.soloader.BackupSoSource; import com.facebook.soloader.LogUtil; import com.facebook.soloader.SoLoader; import com.facebook.soloader.SoLoaderDSONotFoundError; @@ -28,7 +28,7 @@ * RecoveryStrategy that detects cases when SoLoader failed to load a corrupted library, case in * which we try to re-unpack the libraries. */ -public class ReunpackNonApkSoSources implements RecoveryStrategy { +public class ReunpackNonBackupSoSources implements RecoveryStrategy { @Override public boolean recover(UnsatisfiedLinkError error, SoSource[] soSources) { @@ -56,8 +56,8 @@ public boolean recover(UnsatisfiedLinkError error, SoSource[] soSources) { continue; } UnpackingSoSource uss = (UnpackingSoSource) soSource; - if (uss instanceof ApkSoSource) { - // Assume ReunpackApkSoSources has already attempted to reunpack ApkSoSources + if (uss instanceof BackupSoSource) { + // Assume ReunpackBackupSoSources has already attempted to reunpack BackupSoSources continue; } try {