Restrict the cases when we profile guided compile an apk
Do not use profile guided compilation if the apk is loaded by another
app. The decision if an apk was used or not by another app is done
by looking into the foreign profile directory. Apks which where
loaded in others apps will have a file marker in the profile directory.
The marker is named after the canonical location of the apk file where
'/' is replaced by '@'.
Also, refactor the profile paths to the Environment.
Bug: 27334750
Bug: 26080105
Change-Id: Ic2ac5a7a231670ecb4462166c34fdd5b4c631178
diff --git a/services/core/java/com/android/server/pm/PackageDexOptimizer.java b/services/core/java/com/android/server/pm/PackageDexOptimizer.java
index a3af561..a084d86 100644
--- a/services/core/java/com/android/server/pm/PackageDexOptimizer.java
+++ b/services/core/java/com/android/server/pm/PackageDexOptimizer.java
@@ -21,6 +21,7 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageParser;
import android.content.pm.PackageParser.Package;
+import android.os.Environment;
import android.os.PowerManager;
import android.os.UserHandle;
import android.os.WorkSource;
@@ -164,6 +165,10 @@
}
for (String path : paths) {
+ if (useProfiles && isUsedByOtherApps(path)) {
+ // We cannot use profile guided compilation if the apk was used by another app.
+ useProfiles = false;
+ }
int dexoptNeeded;
try {
@@ -204,8 +209,10 @@
+ " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
+ " extractOnly=" + extractOnly + " oatDir = " + oatDir);
final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
+ // Profile guide compiled oat files should not be public.
+ final boolean isPublic = !pkg.isForwardLocked() && !useProfiles;
final int dexFlags = adjustDexoptFlags(
- (!pkg.isForwardLocked() ? DEXOPT_PUBLIC : 0)
+ ( isPublic ? DEXOPT_PUBLIC : 0)
| (vmSafeMode ? DEXOPT_SAFEMODE : 0)
| (debuggable ? DEXOPT_DEBUGGABLE : 0)
| (extractOnly ? DEXOPT_EXTRACTONLY : 0)
@@ -275,6 +282,25 @@
mSystemReady = true;
}
+ private boolean isUsedByOtherApps(String apkPath) {
+ try {
+ apkPath = new File(apkPath).getCanonicalPath();
+ } catch (IOException e) {
+ // Log an error but continue without it.
+ Slog.w(TAG, "Failed to get canonical path", e);
+ }
+ String useMarker = apkPath.replace('/', '@');
+ final int[] currentUserIds = UserManagerService.getInstance().getUserIds();
+ for (int i = 0; i < currentUserIds.length; i++) {
+ File profileDir = Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]);
+ File foreignUseMark = new File(profileDir, useMarker);
+ if (foreignUseMark.exists()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
/**
* A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
* dexopt path.