Merge "[pm] minor refactoring"

This commit is contained in:
TreeHugger Robot
2020-07-08 23:47:37 +00:00
committed by Android (Google) Code Review
5 changed files with 41 additions and 30 deletions

View File

@@ -358,6 +358,8 @@ public class NativeLibraryHelper {
createNativeLibrarySubdir(subDir);
}
// Even if extractNativeLibs is false, we still need to check if the native libs in the APK
// are valid. This is done in the native code.
int copyRet = copyNativeBinaries(handle, subDir, supportedAbi);
if (copyRet != PackageManager.INSTALL_SUCCEEDED) {
return copyRet;

View File

@@ -16,6 +16,7 @@
package com.android.server.pm;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.util.Pair;
@@ -27,6 +28,8 @@ import com.android.server.pm.parsing.pkg.ParsedPackage;
import java.io.File;
import java.util.Set;
// TODO: Move to .parsing sub-package
@VisibleForTesting
public interface PackageAbiHelper {
@@ -34,7 +37,8 @@ public interface PackageAbiHelper {
* Derive and get the location of native libraries for the given package,
* which varies depending on where and how the package was installed.
*/
NativeLibraryPaths getNativeLibraryPaths(AndroidPackage pkg, PackageSetting pkgSetting,
@NonNull
NativeLibraryPaths deriveNativeLibraryPaths(AndroidPackage pkg, boolean isUpdatedSystemApp,
File appLib32InstallDir);
/**
@@ -51,7 +55,7 @@ public interface PackageAbiHelper {
* If {@code extractLibs} is true, native libraries are extracted from the app if required.
*/
Pair<Abis, NativeLibraryPaths> derivePackageAbi(AndroidPackage pkg, boolean isUpdatedSystemApp,
String cpuAbiOverride, boolean extractLibs) throws PackageManagerException;
String cpuAbiOverride) throws PackageManagerException;
/**
* Calculates adjusted ABIs for a set of packages belonging to a shared user so that they all

View File

@@ -131,16 +131,16 @@ final class PackageAbiHelperImpl implements PackageAbiHelper {
}
@Override
public NativeLibraryPaths getNativeLibraryPaths(AndroidPackage pkg, PackageSetting pkgSetting,
File appLib32InstallDir) {
public NativeLibraryPaths deriveNativeLibraryPaths(AndroidPackage pkg,
boolean isUpdatedSystemApp, File appLib32InstallDir) {
// Trying to derive the paths, thus need the raw ABI info from the parsed package, and the
// current state in PackageSetting is irrelevant.
return getNativeLibraryPaths(new Abis(pkg.getPrimaryCpuAbi(), pkg.getSecondaryCpuAbi()),
return deriveNativeLibraryPaths(new Abis(pkg.getPrimaryCpuAbi(), pkg.getSecondaryCpuAbi()),
appLib32InstallDir, pkg.getCodePath(), pkg.getBaseCodePath(), pkg.isSystem(),
pkgSetting.getPkgState().isUpdatedSystemApp());
isUpdatedSystemApp);
}
private static NativeLibraryPaths getNativeLibraryPaths(final Abis abis,
private static NativeLibraryPaths deriveNativeLibraryPaths(final Abis abis,
final File appLib32InstallDir, final String codePath, final String sourceDir,
final boolean isSystemApp, final boolean isUpdatedSystemApp) {
final File codeFile = new File(codePath);
@@ -296,22 +296,19 @@ final class PackageAbiHelperImpl implements PackageAbiHelper {
@Override
public Pair<Abis, NativeLibraryPaths> derivePackageAbi(AndroidPackage pkg,
boolean isUpdatedSystemApp, String cpuAbiOverride, boolean extractLibs)
boolean isUpdatedSystemApp, String cpuAbiOverride)
throws PackageManagerException {
// Give ourselves some initial paths; we'll come back for another
// pass once we've determined ABI below.
String pkgRawPrimaryCpuAbi = AndroidPackageUtils.getRawPrimaryCpuAbi(pkg);
String pkgRawSecondaryCpuAbi = AndroidPackageUtils.getRawSecondaryCpuAbi(pkg);
final NativeLibraryPaths initialLibraryPaths = getNativeLibraryPaths(
final NativeLibraryPaths initialLibraryPaths = deriveNativeLibraryPaths(
new Abis(pkgRawPrimaryCpuAbi, pkgRawSecondaryCpuAbi),
PackageManagerService.sAppLib32InstallDir, pkg.getCodePath(),
pkg.getBaseCodePath(), pkg.isSystem(),
isUpdatedSystemApp);
// We shouldn't attempt to extract libs from system app when it was not updated.
if (pkg.isSystem() && !isUpdatedSystemApp) {
extractLibs = false;
}
final boolean extractLibs = shouldExtractLibs(pkg, isUpdatedSystemApp);
final String nativeLibraryRootStr = initialLibraryPaths.nativeLibraryRootDir;
final boolean useIsaSpecificSubdirs = initialLibraryPaths.nativeLibraryRootRequiresIsa;
@@ -455,11 +452,21 @@ final class PackageAbiHelperImpl implements PackageAbiHelper {
final Abis abis = new Abis(primaryCpuAbi, secondaryCpuAbi);
return new Pair<>(abis,
getNativeLibraryPaths(abis, PackageManagerService.sAppLib32InstallDir,
deriveNativeLibraryPaths(abis, PackageManagerService.sAppLib32InstallDir,
pkg.getCodePath(), pkg.getBaseCodePath(), pkg.isSystem(),
isUpdatedSystemApp));
}
private boolean shouldExtractLibs(AndroidPackage pkg, boolean isUpdatedSystemApp) {
// We shouldn't extract libs if the package is a library or if extractNativeLibs=false
boolean extractLibs = !AndroidPackageUtils.isLibrary(pkg) && pkg.isExtractNativeLibs();
// We shouldn't attempt to extract libs from system app when it was not updated.
if (pkg.isSystem() && !isUpdatedSystemApp) {
extractLibs = false;
}
return extractLibs;
}
/**
* Adjusts ABIs for a set of packages belonging to a shared user so that they all match.
* i.e, so that all packages can be run inside a single process if required.

View File

@@ -11492,15 +11492,14 @@ public class PackageManagerService extends IPackageManager.Stub
}
final String cpuAbiOverride = deriveAbiOverride(request.cpuAbiOverride, pkgSetting);
final boolean isUpdatedSystemApp = pkgSetting.getPkgState().isUpdatedSystemApp();
if ((scanFlags & SCAN_NEW_INSTALL) == 0) {
if (needToDeriveAbi) {
Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "derivePackageAbi");
final boolean extractNativeLibs = !AndroidPackageUtils.isLibrary(parsedPackage);
final Pair<PackageAbiHelper.Abis, PackageAbiHelper.NativeLibraryPaths> derivedAbi =
packageAbiHelper.derivePackageAbi(parsedPackage,
pkgSetting.getPkgState().isUpdatedSystemApp(), cpuAbiOverride,
extractNativeLibs);
packageAbiHelper.derivePackageAbi(parsedPackage, isUpdatedSystemApp,
cpuAbiOverride);
derivedAbi.first.applyTo(parsedPackage);
derivedAbi.second.applyTo(parsedPackage);
Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
@@ -11510,15 +11509,15 @@ public class PackageManagerService extends IPackageManager.Stub
// structure. Try to detect abi based on directory structure.
String pkgRawPrimaryCpuAbi = AndroidPackageUtils.getRawPrimaryCpuAbi(parsedPackage);
if (parsedPackage.isSystem() && !pkgSetting.getPkgState().isUpdatedSystemApp() &&
pkgRawPrimaryCpuAbi == null) {
if (parsedPackage.isSystem() && !isUpdatedSystemApp
&& pkgRawPrimaryCpuAbi == null) {
final PackageAbiHelper.Abis abis = packageAbiHelper.getBundledAppAbis(
parsedPackage);
abis.applyTo(parsedPackage);
abis.applyTo(pkgSetting);
final PackageAbiHelper.NativeLibraryPaths nativeLibraryPaths =
packageAbiHelper.getNativeLibraryPaths(parsedPackage, pkgSetting,
sAppLib32InstallDir);
packageAbiHelper.deriveNativeLibraryPaths(parsedPackage,
isUpdatedSystemApp, sAppLib32InstallDir);
nativeLibraryPaths.applyTo(parsedPackage);
}
} else {
@@ -11529,8 +11528,8 @@ public class PackageManagerService extends IPackageManager.Stub
.setSecondaryCpuAbi(secondaryCpuAbiFromSettings);
final PackageAbiHelper.NativeLibraryPaths nativeLibraryPaths =
packageAbiHelper.getNativeLibraryPaths(parsedPackage,
pkgSetting, sAppLib32InstallDir);
packageAbiHelper.deriveNativeLibraryPaths(parsedPackage,
isUpdatedSystemApp, sAppLib32InstallDir);
nativeLibraryPaths.applyTo(parsedPackage);
if (DEBUG_ABI_SELECTION) {
@@ -11555,7 +11554,7 @@ public class PackageManagerService extends IPackageManager.Stub
// ABIs we determined during compilation, but the path will depend on the final
// package path (after the rename away from the stage path).
final PackageAbiHelper.NativeLibraryPaths nativeLibraryPaths =
packageAbiHelper.getNativeLibraryPaths(parsedPackage, pkgSetting,
packageAbiHelper.deriveNativeLibraryPaths(parsedPackage, isUpdatedSystemApp,
sAppLib32InstallDir);
nativeLibraryPaths.applyTo(parsedPackage);
}
@@ -17485,7 +17484,6 @@ public class PackageManagerService extends IPackageManager.Stub
scanFlags |= SCAN_NO_DEX;
try {
final boolean extractNativeLibs = !AndroidPackageUtils.isLibrary(parsedPackage);
PackageSetting pkgSetting;
synchronized (mLock) {
pkgSetting = mSettings.getPackageLPr(pkgName);
@@ -17500,7 +17498,7 @@ public class PackageManagerService extends IPackageManager.Stub
final Pair<PackageAbiHelper.Abis, PackageAbiHelper.NativeLibraryPaths>
derivedAbi = mInjector.getAbiHelper().derivePackageAbi(parsedPackage,
isUpdatedSystemAppFromExistingSetting || isUpdatedSystemAppInferred,
abiOverride, extractNativeLibs);
abiOverride);
derivedAbi.first.applyTo(parsedPackage);
derivedAbi.second.applyTo(parsedPackage);
} catch (PackageManagerException pme) {

View File

@@ -102,13 +102,13 @@ public class ScanTests {
@Before
public void setupDefaultAbiBehavior() throws Exception {
when(mMockPackageAbiHelper.derivePackageAbi(
any(AndroidPackage.class), anyBoolean(), nullable(String.class), anyBoolean()))
any(AndroidPackage.class), anyBoolean(), nullable(String.class)))
.thenReturn(new Pair<>(
new PackageAbiHelper.Abis("derivedPrimary", "derivedSecondary"),
new PackageAbiHelper.NativeLibraryPaths(
"derivedRootDir", true, "derivedNativeDir", "derivedNativeDir2")));
when(mMockPackageAbiHelper.getNativeLibraryPaths(
any(AndroidPackage.class), any(PackageSetting.class), any(File.class)))
when(mMockPackageAbiHelper.deriveNativeLibraryPaths(
any(AndroidPackage.class), anyBoolean(), any(File.class)))
.thenReturn(new PackageAbiHelper.NativeLibraryPaths(
"getRootDir", true, "getNativeDir", "getNativeDir2"
));