Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.pm; |
| 18 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 22 | import android.content.pm.PackageInfo; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 23 | import android.content.pm.PackageParser; |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 24 | import android.os.Environment; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 25 | import android.os.PowerManager; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 26 | import android.os.UserHandle; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 27 | import android.os.WorkSource; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import android.util.Slog; |
| 30 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 31 | import com.android.internal.annotations.GuardedBy; |
Narayan Kamath | 88eea9e | 2016-05-02 14:44:31 +0100 | [diff] [blame] | 32 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 33 | import com.android.server.pm.Installer.InstallerException; |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 34 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 35 | import java.io.File; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 36 | import java.io.IOException; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 37 | import java.util.ArrayList; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 38 | import java.util.List; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 39 | import java.util.Set; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 40 | |
| 41 | import dalvik.system.DexFile; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 42 | |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 43 | import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; |
| 44 | import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 45 | import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED; |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 46 | import static com.android.server.pm.Installer.DEXOPT_PUBLIC; |
| 47 | import static com.android.server.pm.Installer.DEXOPT_SAFEMODE; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 48 | import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX; |
| 49 | import static com.android.server.pm.Installer.DEXOPT_FORCE; |
| 50 | import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE; |
| 51 | import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 52 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 53 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 54 | |
Andreas Gampe | 47c170a | 2016-03-30 17:21:19 -0700 | [diff] [blame] | 55 | import static com.android.server.pm.PackageManagerServiceCompilerMapping.getNonProfileGuidedCompilerFilter; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 56 | import static dalvik.system.DexFile.isProfileGuidedCompilerFilter; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Helper class for running dexopt command on packages. |
| 60 | */ |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 61 | public class PackageDexOptimizer { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 62 | private static final String TAG = "PackageManager.DexOptimizer"; |
| 63 | static final String OAT_DIR_NAME = "oat"; |
| 64 | // TODO b/19550105 Remove error codes and use exceptions |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 65 | public static final int DEX_OPT_SKIPPED = 0; |
| 66 | public static final int DEX_OPT_PERFORMED = 1; |
| 67 | public static final int DEX_OPT_FAILED = -1; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 68 | |
Calin Juravle | aae3576 | 2017-02-14 17:53:13 -0800 | [diff] [blame] | 69 | /** Special library name that skips shared libraries check during compilation. */ |
| 70 | public static final String SKIP_SHARED_LIBRARY_CHECK = "&"; |
| 71 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 72 | private final Installer mInstaller; |
| 73 | private final Object mInstallLock; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 74 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 75 | private final PowerManager.WakeLock mDexoptWakeLock; |
| 76 | private volatile boolean mSystemReady; |
| 77 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 78 | PackageDexOptimizer(Installer installer, Object installLock, Context context, |
| 79 | String wakeLockTag) { |
| 80 | this.mInstaller = installer; |
| 81 | this.mInstallLock = installLock; |
| 82 | |
| 83 | PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 84 | mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag); |
| 85 | } |
| 86 | |
| 87 | protected PackageDexOptimizer(PackageDexOptimizer from) { |
| 88 | this.mInstaller = from.mInstaller; |
| 89 | this.mInstallLock = from.mInstallLock; |
| 90 | this.mDexoptWakeLock = from.mDexoptWakeLock; |
| 91 | this.mSystemReady = from.mSystemReady; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 94 | static boolean canOptimizePackage(PackageParser.Package pkg) { |
Calin Juravle | 6dfd83d | 2016-01-29 18:23:57 +0000 | [diff] [blame] | 95 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0; |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 98 | /** |
| 99 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 100 | * instruction sets. |
| 101 | * |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 102 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are |
| 103 | * synchronized on {@link #mInstallLock}. |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 104 | */ |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 105 | int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries, |
Andreas Gampe | 7e619a9 | 2016-07-12 22:42:41 -0700 | [diff] [blame] | 106 | String[] instructionSets, boolean checkProfiles, String targetCompilationFilter, |
Calin Juravle | 07b6eab | 2017-03-01 19:55:35 -0800 | [diff] [blame] | 107 | CompilerStats.PackageStats packageStats, boolean isUsedByOtherApps) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 108 | if (!canOptimizePackage(pkg)) { |
| 109 | return DEX_OPT_SKIPPED; |
| 110 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 111 | synchronized (mInstallLock) { |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 112 | // During boot the system doesn't need to instantiate and obtain a wake lock. |
| 113 | // PowerManager might not be ready, but that doesn't mean that we can't proceed with |
| 114 | // dexopt. |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 115 | final boolean useLock = mSystemReady; |
| 116 | if (useLock) { |
| 117 | mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid)); |
| 118 | mDexoptWakeLock.acquire(); |
| 119 | } |
| 120 | try { |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame] | 121 | return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles, |
Calin Juravle | 07b6eab | 2017-03-01 19:55:35 -0800 | [diff] [blame] | 122 | targetCompilationFilter, packageStats, isUsedByOtherApps); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 123 | } finally { |
| 124 | if (useLock) { |
| 125 | mDexoptWakeLock.release(); |
| 126 | } |
| 127 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 131 | /** |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 132 | * Performs dexopt on all code paths of the given package. |
| 133 | * It assumes the install lock is held. |
| 134 | */ |
| 135 | @GuardedBy("mInstallLock") |
| 136 | private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries, |
| 137 | String[] targetInstructionSets, boolean checkForProfileUpdates, |
Calin Juravle | 07b6eab | 2017-03-01 19:55:35 -0800 | [diff] [blame] | 138 | String targetCompilerFilter, CompilerStats.PackageStats packageStats, |
| 139 | boolean isUsedByOtherApps) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 140 | final String[] instructionSets = targetInstructionSets != null ? |
| 141 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 142 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 143 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 144 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 145 | |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 146 | final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo, |
Calin Juravle | 07b6eab | 2017-03-01 19:55:35 -0800 | [diff] [blame] | 147 | targetCompilerFilter, isUsedByOtherApps); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 148 | final boolean profileUpdated = checkForProfileUpdates && |
| 149 | isProfileUpdated(pkg, sharedGid, compilerFilter); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 150 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 151 | // TODO(calin,jeffhao): shared library paths should be adjusted to include previous code |
| 152 | // paths (b/34169257). |
| 153 | final String sharedLibrariesPath = getSharedLibrariesPath(sharedLibraries); |
Calin Juravle | efb1c94 | 2017-04-04 20:31:44 -0700 | [diff] [blame] | 154 | // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags. |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 155 | final int dexoptFlags = getDexFlags(pkg, compilerFilter); |
| 156 | |
| 157 | int result = DEX_OPT_SKIPPED; |
| 158 | for (String path : paths) { |
| 159 | for (String dexCodeIsa : dexCodeInstructionSets) { |
| 160 | int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, profileUpdated, |
| 161 | sharedLibrariesPath, dexoptFlags, sharedGid, packageStats); |
| 162 | // The end result is: |
| 163 | // - FAILED if any path failed, |
| 164 | // - PERFORMED if at least one path needed compilation, |
| 165 | // - SKIPPED when all paths are up to date |
| 166 | if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) { |
| 167 | result = newResult; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Performs dexopt on the {@code path} belonging to the package {@code pkg}. |
| 176 | * |
| 177 | * @return |
| 178 | * DEX_OPT_FAILED if there was any exception during dexopt |
| 179 | * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path. |
| 180 | * DEX_OPT_SKIPPED if the path does not need to be deopt-ed. |
| 181 | */ |
| 182 | @GuardedBy("mInstallLock") |
| 183 | private int dexOptPath(PackageParser.Package pkg, String path, String isa, |
| 184 | String compilerFilter, boolean profileUpdated, String sharedLibrariesPath, |
| 185 | int dexoptFlags, int uid, CompilerStats.PackageStats packageStats) { |
| 186 | int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, profileUpdated); |
| 187 | if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) { |
| 188 | return DEX_OPT_SKIPPED; |
| 189 | } |
| 190 | |
| 191 | // TODO(calin): there's no need to try to create the oat dir over and over again, |
| 192 | // especially since it involve an extra installd call. We should create |
| 193 | // if (if supported) on the fly during the dexopt call. |
| 194 | String oatDir = createOatDirIfSupported(pkg, isa); |
| 195 | |
| 196 | Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path |
| 197 | + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa |
| 198 | + " dexoptFlags=" + printDexoptFlags(dexoptFlags) |
| 199 | + " target-filter=" + compilerFilter + " oatDir=" + oatDir |
| 200 | + " sharedLibraries=" + sharedLibrariesPath); |
| 201 | |
| 202 | try { |
| 203 | long startTime = System.currentTimeMillis(); |
| 204 | |
| 205 | mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags, |
Calin Juravle | 811a75a | 2017-04-05 22:49:38 -0700 | [diff] [blame^] | 206 | compilerFilter, pkg.volumeUuid, sharedLibrariesPath, pkg.applicationInfo.seInfo); |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 207 | |
| 208 | if (packageStats != null) { |
| 209 | long endTime = System.currentTimeMillis(); |
| 210 | packageStats.setCompileTime(path, (int)(endTime - startTime)); |
| 211 | } |
| 212 | return DEX_OPT_PERFORMED; |
| 213 | } catch (InstallerException e) { |
| 214 | Slog.w(TAG, "Failed to dexopt", e); |
| 215 | return DEX_OPT_FAILED; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 220 | * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}. |
| 221 | * |
| 222 | * @return |
| 223 | * DEX_OPT_FAILED if there was any exception during dexopt |
| 224 | * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path. |
| 225 | * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file |
| 226 | * didn't need an update. That's because at the moment we don't get more than success/failure |
| 227 | * from installd. |
| 228 | * |
| 229 | * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than |
| 230 | * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though |
| 231 | * that seems wasteful. |
| 232 | */ |
| 233 | public int dexOptSecondaryDexPath(ApplicationInfo info, String path, Set<String> isas, |
| 234 | String compilerFilter, boolean isUsedByOtherApps) { |
| 235 | synchronized (mInstallLock) { |
| 236 | // During boot the system doesn't need to instantiate and obtain a wake lock. |
| 237 | // PowerManager might not be ready, but that doesn't mean that we can't proceed with |
| 238 | // dexopt. |
| 239 | final boolean useLock = mSystemReady; |
| 240 | if (useLock) { |
| 241 | mDexoptWakeLock.setWorkSource(new WorkSource(info.uid)); |
| 242 | mDexoptWakeLock.acquire(); |
| 243 | } |
| 244 | try { |
| 245 | return dexOptSecondaryDexPathLI(info, path, isas, compilerFilter, |
| 246 | isUsedByOtherApps); |
| 247 | } finally { |
| 248 | if (useLock) { |
| 249 | mDexoptWakeLock.release(); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | @GuardedBy("mInstallLock") |
| 256 | private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path, Set<String> isas, |
| 257 | String compilerFilter, boolean isUsedByOtherApps) { |
Calin Juravle | efb1c94 | 2017-04-04 20:31:44 -0700 | [diff] [blame] | 258 | compilerFilter = getRealCompilerFilter(info, compilerFilter, isUsedByOtherApps); |
| 259 | // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags. |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 260 | int dexoptFlags = getDexFlags(info, compilerFilter) | DEXOPT_SECONDARY_DEX; |
| 261 | // Check the app storage and add the appropriate flags. |
| 262 | if (info.dataDir.equals(info.deviceProtectedDataDir)) { |
| 263 | dexoptFlags |= DEXOPT_STORAGE_DE; |
| 264 | } else if (info.dataDir.equals(info.credentialProtectedDataDir)) { |
| 265 | dexoptFlags |= DEXOPT_STORAGE_CE; |
| 266 | } else { |
| 267 | Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName); |
| 268 | return DEX_OPT_FAILED; |
| 269 | } |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 270 | Log.d(TAG, "Running dexopt on: " + path |
| 271 | + " pkg=" + info.packageName + " isa=" + isas |
| 272 | + " dexoptFlags=" + printDexoptFlags(dexoptFlags) |
| 273 | + " target-filter=" + compilerFilter); |
| 274 | |
| 275 | try { |
| 276 | for (String isa : isas) { |
| 277 | // Reuse the same dexopt path as for the primary apks. We don't need all the |
| 278 | // arguments as some (dexopNeeded and oatDir) will be computed by installd because |
| 279 | // system server cannot read untrusted app content. |
| 280 | // TODO(calin): maybe add a separate call. |
| 281 | mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0, |
| 282 | /*oatDir*/ null, dexoptFlags, |
Calin Juravle | 811a75a | 2017-04-05 22:49:38 -0700 | [diff] [blame^] | 283 | compilerFilter, info.volumeUuid, SKIP_SHARED_LIBRARY_CHECK, info.seInfoUser); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return DEX_OPT_PERFORMED; |
| 287 | } catch (InstallerException e) { |
| 288 | Slog.w(TAG, "Failed to dexopt", e); |
| 289 | return DEX_OPT_FAILED; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 294 | * Adjust the given dexopt-needed value. Can be overridden to influence the decision to |
| 295 | * optimize or not (and in what way). |
| 296 | */ |
| 297 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 298 | return dexoptNeeded; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Adjust the given dexopt flags that will be passed to the installer. |
| 303 | */ |
| 304 | protected int adjustDexoptFlags(int dexoptFlags) { |
| 305 | return dexoptFlags; |
| 306 | } |
| 307 | |
Narayan Kamath | 88eea9e | 2016-05-02 14:44:31 +0100 | [diff] [blame] | 308 | /** |
| 309 | * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}. |
| 310 | */ |
| 311 | void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) { |
| 312 | final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo); |
| 313 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 314 | |
| 315 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 316 | |
| 317 | for (String instructionSet : dexCodeInstructionSets) { |
| 318 | pw.println("Instruction Set: " + instructionSet); |
| 319 | pw.increaseIndent(); |
| 320 | for (String path : paths) { |
| 321 | String status = null; |
| 322 | try { |
| 323 | status = DexFile.getDexFileStatus(path, instructionSet); |
| 324 | } catch (IOException ioe) { |
| 325 | status = "[Exception]: " + ioe.getMessage(); |
| 326 | } |
| 327 | pw.println("path: " + path); |
| 328 | pw.println("status: " + status); |
| 329 | } |
| 330 | pw.decreaseIndent(); |
| 331 | } |
| 332 | } |
| 333 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 334 | /** |
| 335 | * Returns the compiler filter that should be used to optimize the package code. |
| 336 | * The target filter will be updated if the package code is used by other apps |
| 337 | * or if it has the safe mode flag set. |
| 338 | */ |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 339 | private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter, |
| 340 | boolean isUsedByOtherApps) { |
| 341 | int flags = info.flags; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 342 | boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
Mathieu Chartier | 41e4a37 | 2016-09-02 16:36:42 -0700 | [diff] [blame] | 343 | if (vmSafeMode) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 344 | // For the compilation, it doesn't really matter what we return here because installd |
| 345 | // will replace the filter with interpret-only anyway. |
| 346 | // However, we return a non profile guided filter so that we simplify the logic of |
| 347 | // merging profiles. |
| 348 | // TODO(calin): safe mode path could be simplified if we pass interpret-only from |
| 349 | // here rather than letting installd decide on the filter. |
| 350 | return getNonProfileGuidedCompilerFilter(targetCompilerFilter); |
Mathieu Chartier | 41e4a37 | 2016-09-02 16:36:42 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 353 | if (isProfileGuidedCompilerFilter(targetCompilerFilter) && isUsedByOtherApps) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 354 | // If the dex files is used by other apps, we cannot use profile-guided compilation. |
| 355 | return getNonProfileGuidedCompilerFilter(targetCompilerFilter); |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 358 | return targetCompilerFilter; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 361 | /** |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 362 | * Computes the dex flags that needs to be pass to installd for the given package and compiler |
| 363 | * filter. |
| 364 | */ |
| 365 | private int getDexFlags(PackageParser.Package pkg, String compilerFilter) { |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 366 | return getDexFlags(pkg.applicationInfo, compilerFilter); |
| 367 | } |
| 368 | |
| 369 | private int getDexFlags(ApplicationInfo info, String compilerFilter) { |
| 370 | int flags = info.flags; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 371 | boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
| 372 | boolean debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 373 | // Profile guide compiled oat files should not be public. |
| 374 | boolean isProfileGuidedFilter = isProfileGuidedCompilerFilter(compilerFilter); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 375 | boolean isPublic = !info.isForwardLocked() && !isProfileGuidedFilter; |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 376 | int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0; |
| 377 | int dexFlags = |
| 378 | (isPublic ? DEXOPT_PUBLIC : 0) |
| 379 | | (vmSafeMode ? DEXOPT_SAFEMODE : 0) |
| 380 | | (debuggable ? DEXOPT_DEBUGGABLE : 0) |
| 381 | | profileFlag |
| 382 | | DEXOPT_BOOTCOMPLETE; |
| 383 | return adjustDexoptFlags(dexFlags); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Assesses if there's a need to perform dexopt on {@code path} for the given |
| 388 | * configuration (isa, compiler filter, profile). |
| 389 | */ |
| 390 | private int getDexoptNeeded(String path, String isa, String compilerFilter, |
| 391 | boolean newProfile) { |
| 392 | int dexoptNeeded; |
| 393 | try { |
| 394 | dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, newProfile); |
| 395 | } catch (IOException ioe) { |
| 396 | Slog.w(TAG, "IOException reading apk: " + path, ioe); |
| 397 | return DEX_OPT_FAILED; |
| 398 | } |
| 399 | return adjustDexoptNeeded(dexoptNeeded); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Computes the shared libraries path that should be passed to dexopt. |
| 404 | */ |
| 405 | private String getSharedLibrariesPath(String[] sharedLibraries) { |
| 406 | if (sharedLibraries == null || sharedLibraries.length == 0) { |
| 407 | return null; |
| 408 | } |
| 409 | StringBuilder sb = new StringBuilder(); |
| 410 | for (String lib : sharedLibraries) { |
| 411 | if (sb.length() != 0) { |
| 412 | sb.append(":"); |
| 413 | } |
| 414 | sb.append(lib); |
| 415 | } |
| 416 | return sb.toString(); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Checks if there is an update on the profile information of the {@code pkg}. |
| 421 | * If the compiler filter is not profile guided the method returns false. |
| 422 | * |
| 423 | * Note that this is a "destructive" operation with side effects. Under the hood the |
| 424 | * current profile and the reference profile will be merged and subsequent calls |
| 425 | * may return a different result. |
| 426 | */ |
| 427 | private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String compilerFilter) { |
| 428 | // Check if we are allowed to merge and if the compiler filter is profile guided. |
| 429 | if (!isProfileGuidedCompilerFilter(compilerFilter)) { |
| 430 | return false; |
| 431 | } |
| 432 | // Merge profiles. It returns whether or not there was an updated in the profile info. |
| 433 | try { |
| 434 | return mInstaller.mergeProfiles(uid, pkg.packageName); |
| 435 | } catch (InstallerException e) { |
| 436 | Slog.w(TAG, "Failed to merge profiles", e); |
| 437 | } |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Creates oat dir for the specified package if needed and supported. |
| 443 | * In certain cases oat directory |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 444 | * <strong>cannot</strong> be created: |
| 445 | * <ul> |
| 446 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 447 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 448 | * </ul> |
| 449 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 450 | * @return Absolute path to the oat directory or null, if oat directory |
| 451 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 452 | */ |
| 453 | @Nullable |
Todd Kennedy | 27c24fb | 2015-09-17 16:49:25 -0700 | [diff] [blame] | 454 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) { |
Fyodor Kupolov | ebcac16 | 2015-09-09 15:56:45 -0700 | [diff] [blame] | 455 | if (!pkg.canHaveOatDir()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 456 | return null; |
| 457 | } |
| 458 | File codePath = new File(pkg.codePath); |
| 459 | if (codePath.isDirectory()) { |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 460 | // TODO(calin): why do we create this only if the codePath is a directory? (i.e for |
| 461 | // cluster packages). It seems that the logic for the folder creation is |
| 462 | // split between installd and here. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 463 | File oatDir = getOatDir(codePath); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 464 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 465 | mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 466 | } catch (InstallerException e) { |
| 467 | Slog.w(TAG, "Failed to create oat dir", e); |
| 468 | return null; |
| 469 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 470 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 471 | } |
| 472 | return null; |
| 473 | } |
| 474 | |
| 475 | static File getOatDir(File codePath) { |
| 476 | return new File(codePath, OAT_DIR_NAME); |
| 477 | } |
| 478 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 479 | void systemReady() { |
| 480 | mSystemReady = true; |
| 481 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 482 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 483 | private String printDexoptFlags(int flags) { |
| 484 | ArrayList<String> flagsList = new ArrayList<>(); |
| 485 | |
| 486 | if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) { |
| 487 | flagsList.add("boot_complete"); |
| 488 | } |
| 489 | if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) { |
| 490 | flagsList.add("debuggable"); |
| 491 | } |
| 492 | if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) { |
| 493 | flagsList.add("profile_guided"); |
| 494 | } |
| 495 | if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) { |
| 496 | flagsList.add("public"); |
| 497 | } |
| 498 | if ((flags & DEXOPT_SAFEMODE) == DEXOPT_SAFEMODE) { |
| 499 | flagsList.add("safemode"); |
| 500 | } |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 501 | if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) { |
| 502 | flagsList.add("secondary"); |
| 503 | } |
| 504 | if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) { |
| 505 | flagsList.add("force"); |
| 506 | } |
| 507 | if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) { |
| 508 | flagsList.add("storage_ce"); |
| 509 | } |
| 510 | if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) { |
| 511 | flagsList.add("storage_de"); |
| 512 | } |
| 513 | |
Calin Juravle | 2d4b6ad7 | 2017-01-04 13:22:14 -0800 | [diff] [blame] | 514 | return String.join(",", flagsList); |
| 515 | } |
| 516 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 517 | /** |
| 518 | * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a |
| 519 | * dexopt path. |
| 520 | */ |
| 521 | public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer { |
| 522 | |
| 523 | public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock, |
| 524 | Context context, String wakeLockTag) { |
| 525 | super(installer, installLock, context, wakeLockTag); |
| 526 | } |
| 527 | |
| 528 | public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) { |
| 529 | super(from); |
| 530 | } |
| 531 | |
| 532 | @Override |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 533 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 534 | // Ensure compilation, no matter the current state. |
| 535 | // TODO: The return value is wrong when patchoat is needed. |
Nicolas Geoffray | 79c9e06 | 2016-11-25 09:50:29 +0000 | [diff] [blame] | 536 | return DexFile.DEX2OAT_FROM_SCRATCH; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 537 | } |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 538 | |
| 539 | @Override |
| 540 | protected int adjustDexoptFlags(int flags) { |
| 541 | // Add DEXOPT_FORCE flag to signal installd that it should force compilation |
| 542 | // and discard dexoptanalyzer result. |
| 543 | return flags | DEXOPT_FORCE; |
| 544 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 545 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 546 | } |