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