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 | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 20 | import android.content.pm.ApplicationInfo; |
| 21 | import android.content.pm.PackageParser; |
| 22 | import android.os.UserHandle; |
| 23 | import android.util.ArraySet; |
| 24 | import android.util.Log; |
| 25 | import android.util.Slog; |
| 26 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 27 | import java.io.File; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 28 | import java.io.FileNotFoundException; |
| 29 | import java.io.IOException; |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | import dalvik.system.DexFile; |
| 34 | import dalvik.system.StaleDexCacheError; |
| 35 | |
| 36 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 37 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
| 38 | |
| 39 | /** |
| 40 | * Helper class for running dexopt command on packages. |
| 41 | */ |
| 42 | final class PackageDexOptimizer { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 43 | private static final String TAG = "PackageManager.DexOptimizer"; |
| 44 | static final String OAT_DIR_NAME = "oat"; |
| 45 | // TODO b/19550105 Remove error codes and use exceptions |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 46 | static final int DEX_OPT_SKIPPED = 0; |
| 47 | static final int DEX_OPT_PERFORMED = 1; |
| 48 | static final int DEX_OPT_DEFERRED = 2; |
| 49 | static final int DEX_OPT_FAILED = -1; |
| 50 | |
| 51 | private final PackageManagerService mPackageManagerService; |
| 52 | private ArraySet<PackageParser.Package> mDeferredDexOpt; |
| 53 | |
| 54 | PackageDexOptimizer(PackageManagerService packageManagerService) { |
| 55 | this.mPackageManagerService = packageManagerService; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 60 | * instruction sets. |
| 61 | * |
| 62 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} are synchronized on |
| 63 | * {@link PackageManagerService#mInstallLock}. |
| 64 | */ |
| 65 | int performDexOpt(PackageParser.Package pkg, String[] instructionSets, |
| 66 | boolean forceDex, boolean defer, boolean inclDependencies) { |
| 67 | ArraySet<String> done; |
| 68 | if (inclDependencies && (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null)) { |
| 69 | done = new ArraySet<String>(); |
| 70 | done.add(pkg.packageName); |
| 71 | } else { |
| 72 | done = null; |
| 73 | } |
| 74 | synchronized (mPackageManagerService.mInstallLock) { |
| 75 | return performDexOptLI(pkg, instructionSets, forceDex, defer, done); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets, |
| 80 | boolean forceDex, boolean defer, ArraySet<String> done) { |
| 81 | final String[] instructionSets = targetInstructionSets != null ? |
| 82 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 83 | |
| 84 | if (done != null) { |
| 85 | done.add(pkg.packageName); |
| 86 | if (pkg.usesLibraries != null) { |
| 87 | performDexOptLibsLI(pkg.usesLibraries, instructionSets, forceDex, defer, done); |
| 88 | } |
| 89 | if (pkg.usesOptionalLibraries != null) { |
| 90 | performDexOptLibsLI(pkg.usesOptionalLibraries, instructionSets, forceDex, defer, |
| 91 | done); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) { |
| 96 | return DEX_OPT_SKIPPED; |
| 97 | } |
| 98 | |
| 99 | final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
| 100 | final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 101 | |
| 102 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 103 | boolean performedDexOpt = false; |
| 104 | // There are three basic cases here: |
| 105 | // 1.) we need to dexopt, either because we are forced or it is needed |
| 106 | // 2.) we are deferring a needed dexopt |
| 107 | // 3.) we are skipping an unneeded dexopt |
| 108 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 109 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
| 110 | if (!forceDex && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | for (String path : paths) { |
| 115 | try { |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 116 | final int dexoptNeeded; |
| 117 | if (forceDex) { |
| 118 | dexoptNeeded = DexFile.DEX2OAT_NEEDED; |
| 119 | } else { |
| 120 | dexoptNeeded = DexFile.getDexOptNeeded(path, |
| 121 | pkg.packageName, dexCodeInstructionSet, defer); |
| 122 | } |
| 123 | |
| 124 | if (!forceDex && defer && dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) { |
| 125 | // We're deciding to defer a needed dexopt. Don't bother dexopting for other |
| 126 | // paths and instruction sets. We'll deal with them all together when we process |
| 127 | // our list of deferred dexopts. |
| 128 | addPackageForDeferredDexopt(pkg); |
| 129 | return DEX_OPT_DEFERRED; |
| 130 | } |
| 131 | |
| 132 | if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) { |
| 133 | final String dexoptType; |
| 134 | String oatDir = null; |
| 135 | if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) { |
| 136 | dexoptType = "dex2oat"; |
| 137 | oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet); |
| 138 | } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) { |
| 139 | dexoptType = "patchoat"; |
| 140 | } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) { |
| 141 | dexoptType = "self patchoat"; |
| 142 | } else { |
| 143 | throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded); |
| 144 | } |
| 145 | Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg=" |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 146 | + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 147 | + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable |
| 148 | + " oatDir = " + oatDir); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 149 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 150 | final int ret = mPackageManagerService.mInstaller.dexopt(path, sharedGid, |
| 151 | !pkg.isForwardLocked(), pkg.packageName, dexCodeInstructionSet, |
| 152 | dexoptNeeded, vmSafeMode, debuggable, oatDir); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 153 | if (ret < 0) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 154 | return DEX_OPT_FAILED; |
| 155 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 156 | performedDexOpt = true; |
| 157 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 158 | } catch (FileNotFoundException e) { |
| 159 | Slog.w(TAG, "Apk not found for dexopt: " + path); |
| 160 | return DEX_OPT_FAILED; |
| 161 | } catch (IOException e) { |
| 162 | Slog.w(TAG, "IOException reading apk: " + path, e); |
| 163 | return DEX_OPT_FAILED; |
| 164 | } catch (StaleDexCacheError e) { |
| 165 | Slog.w(TAG, "StaleDexCacheError when reading apk: " + path, e); |
| 166 | return DEX_OPT_FAILED; |
| 167 | } catch (Exception e) { |
| 168 | Slog.w(TAG, "Exception when doing dexopt : ", e); |
| 169 | return DEX_OPT_FAILED; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // At this point we haven't failed dexopt and we haven't deferred dexopt. We must |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 174 | // either have either succeeded dexopt, or have had getDexOptNeeded tell us |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 175 | // it isn't required. We therefore mark that this package doesn't need dexopt unless |
| 176 | // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped |
| 177 | // it. |
| 178 | pkg.mDexOptPerformed.add(dexCodeInstructionSet); |
| 179 | } |
| 180 | |
| 181 | // If we've gotten here, we're sure that no error occurred and that we haven't |
| 182 | // deferred dex-opt. We've either dex-opted one more paths or instruction sets or |
| 183 | // we've skipped all of them because they are up to date. In both cases this |
| 184 | // package doesn't need dexopt any longer. |
| 185 | return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 186 | } |
| 187 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Creates oat dir for the specified package. In certain cases oat directory |
| 190 | * <strong>cannot</strong> be created: |
| 191 | * <ul> |
| 192 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 193 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 194 | * </ul> |
| 195 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 196 | * @return Absolute path to the oat directory or null, if oat directory |
| 197 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 198 | */ |
| 199 | @Nullable |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 200 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 201 | throws IOException { |
Fyodor Kupolov | 94056d1 | 2015-04-22 16:03:48 -0700 | [diff] [blame^] | 202 | if ((pkg.isSystemApp() && !pkg.isUpdatedSystemApp()) || pkg.isForwardLocked() |
| 203 | || pkg.applicationInfo.isExternalAsec()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 204 | return null; |
| 205 | } |
| 206 | File codePath = new File(pkg.codePath); |
| 207 | if (codePath.isDirectory()) { |
| 208 | File oatDir = getOatDir(codePath); |
| 209 | mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(), |
| 210 | dexInstructionSet); |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 211 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 212 | } |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | static File getOatDir(File codePath) { |
| 217 | return new File(codePath, OAT_DIR_NAME); |
| 218 | } |
| 219 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 220 | private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets, |
| 221 | boolean forceDex, boolean defer, ArraySet<String> done) { |
| 222 | for (String libName : libs) { |
| 223 | PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary( |
| 224 | libName); |
| 225 | if (libPkg != null && !done.contains(libName)) { |
| 226 | performDexOptLI(libPkg, instructionSets, forceDex, defer, done); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Clears set of deferred dexopt packages. |
| 233 | * @return content of dexopt set if it was not empty |
| 234 | */ |
| 235 | public ArraySet<PackageParser.Package> clearDeferredDexOptPackages() { |
| 236 | ArraySet<PackageParser.Package> result = mDeferredDexOpt; |
| 237 | mDeferredDexOpt = null; |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | public void addPackageForDeferredDexopt(PackageParser.Package pkg) { |
| 242 | if (mDeferredDexOpt == null) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 243 | mDeferredDexOpt = new ArraySet<>(); |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 244 | } |
| 245 | mDeferredDexOpt.add(pkg); |
| 246 | } |
| 247 | } |