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 | |
| 19 | import android.content.pm.ApplicationInfo; |
| 20 | import android.content.pm.PackageParser; |
| 21 | import android.os.UserHandle; |
| 22 | import android.util.ArraySet; |
| 23 | import android.util.Log; |
| 24 | import android.util.Slog; |
| 25 | |
| 26 | import java.io.FileNotFoundException; |
| 27 | import java.io.IOException; |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import dalvik.system.DexFile; |
| 32 | import dalvik.system.StaleDexCacheError; |
| 33 | |
| 34 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 35 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
| 36 | |
| 37 | /** |
| 38 | * Helper class for running dexopt command on packages. |
| 39 | */ |
| 40 | final class PackageDexOptimizer { |
| 41 | static final String TAG = "PackageManager.DexOptimizer"; |
| 42 | static final int DEX_OPT_SKIPPED = 0; |
| 43 | static final int DEX_OPT_PERFORMED = 1; |
| 44 | static final int DEX_OPT_DEFERRED = 2; |
| 45 | static final int DEX_OPT_FAILED = -1; |
| 46 | |
| 47 | private final PackageManagerService mPackageManagerService; |
| 48 | private ArraySet<PackageParser.Package> mDeferredDexOpt; |
| 49 | |
| 50 | PackageDexOptimizer(PackageManagerService packageManagerService) { |
| 51 | this.mPackageManagerService = packageManagerService; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 56 | * instruction sets. |
| 57 | * |
| 58 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} are synchronized on |
| 59 | * {@link PackageManagerService#mInstallLock}. |
| 60 | */ |
| 61 | int performDexOpt(PackageParser.Package pkg, String[] instructionSets, |
| 62 | boolean forceDex, boolean defer, boolean inclDependencies) { |
| 63 | ArraySet<String> done; |
| 64 | if (inclDependencies && (pkg.usesLibraries != null || pkg.usesOptionalLibraries != null)) { |
| 65 | done = new ArraySet<String>(); |
| 66 | done.add(pkg.packageName); |
| 67 | } else { |
| 68 | done = null; |
| 69 | } |
| 70 | synchronized (mPackageManagerService.mInstallLock) { |
| 71 | return performDexOptLI(pkg, instructionSets, forceDex, defer, done); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets, |
| 76 | boolean forceDex, boolean defer, ArraySet<String> done) { |
| 77 | final String[] instructionSets = targetInstructionSets != null ? |
| 78 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 79 | |
| 80 | if (done != null) { |
| 81 | done.add(pkg.packageName); |
| 82 | if (pkg.usesLibraries != null) { |
| 83 | performDexOptLibsLI(pkg.usesLibraries, instructionSets, forceDex, defer, done); |
| 84 | } |
| 85 | if (pkg.usesOptionalLibraries != null) { |
| 86 | performDexOptLibsLI(pkg.usesOptionalLibraries, instructionSets, forceDex, defer, |
| 87 | done); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) { |
| 92 | return DEX_OPT_SKIPPED; |
| 93 | } |
| 94 | |
| 95 | final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
| 96 | final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 97 | |
| 98 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 99 | boolean performedDexOpt = false; |
| 100 | // There are three basic cases here: |
| 101 | // 1.) we need to dexopt, either because we are forced or it is needed |
| 102 | // 2.) we are deferring a needed dexopt |
| 103 | // 3.) we are skipping an unneeded dexopt |
| 104 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 105 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
| 106 | if (!forceDex && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) { |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | for (String path : paths) { |
| 111 | try { |
| 112 | // This will return DEXOPT_NEEDED if we either cannot find any odex file for this |
| 113 | // package or the one we find does not match the image checksum (i.e. it was |
| 114 | // compiled against an old image). It will return PATCHOAT_NEEDED if we can find a |
| 115 | // odex file and it matches the checksum of the image but not its base address, |
| 116 | // meaning we need to move it. |
| 117 | final byte isDexOptNeeded = DexFile.isDexOptNeededInternal(path, |
| 118 | pkg.packageName, dexCodeInstructionSet, defer); |
| 119 | if (forceDex || (!defer && isDexOptNeeded == DexFile.DEXOPT_NEEDED)) { |
| 120 | Log.i(TAG, "Running dexopt on: " + path + " pkg=" |
| 121 | + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet |
| 122 | + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable); |
| 123 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 124 | final int ret = mPackageManagerService.mInstaller.dexopt(path, sharedGid, |
| 125 | !pkg.isForwardLocked(), pkg.packageName, dexCodeInstructionSet, |
| 126 | vmSafeMode, debuggable); |
| 127 | |
| 128 | if (ret < 0) { |
| 129 | // Don't bother running dexopt again if we failed, it will probably |
| 130 | // just result in an error again. Also, don't bother dexopting for other |
| 131 | // paths & ISAs. |
| 132 | return DEX_OPT_FAILED; |
| 133 | } |
| 134 | |
| 135 | performedDexOpt = true; |
| 136 | } else if (!defer && isDexOptNeeded == DexFile.PATCHOAT_NEEDED) { |
| 137 | Log.i(TAG, "Running patchoat on: " + pkg.applicationInfo.packageName); |
| 138 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 139 | final int ret = mPackageManagerService.mInstaller.patchoat(path, sharedGid, |
| 140 | !pkg.isForwardLocked(), pkg.packageName, dexCodeInstructionSet); |
| 141 | |
| 142 | if (ret < 0) { |
| 143 | // Don't bother running patchoat again if we failed, it will probably |
| 144 | // just result in an error again. Also, don't bother dexopting for other |
| 145 | // paths & ISAs. |
| 146 | return DEX_OPT_FAILED; |
| 147 | } |
| 148 | |
| 149 | performedDexOpt = true; |
| 150 | } |
| 151 | |
| 152 | // We're deciding to defer a needed dexopt. Don't bother dexopting for other |
| 153 | // paths and instruction sets. We'll deal with them all together when we process |
| 154 | // our list of deferred dexopts. |
| 155 | if (defer && isDexOptNeeded != DexFile.UP_TO_DATE) { |
| 156 | addPackageForDeferredDexopt(pkg); |
| 157 | return DEX_OPT_DEFERRED; |
| 158 | } |
| 159 | } catch (FileNotFoundException e) { |
| 160 | Slog.w(TAG, "Apk not found for dexopt: " + path); |
| 161 | return DEX_OPT_FAILED; |
| 162 | } catch (IOException e) { |
| 163 | Slog.w(TAG, "IOException reading apk: " + path, e); |
| 164 | return DEX_OPT_FAILED; |
| 165 | } catch (StaleDexCacheError e) { |
| 166 | Slog.w(TAG, "StaleDexCacheError when reading apk: " + path, e); |
| 167 | return DEX_OPT_FAILED; |
| 168 | } catch (Exception e) { |
| 169 | Slog.w(TAG, "Exception when doing dexopt : ", e); |
| 170 | return DEX_OPT_FAILED; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // At this point we haven't failed dexopt and we haven't deferred dexopt. We must |
| 175 | // either have either succeeded dexopt, or have had isDexOptNeededInternal tell us |
| 176 | // it isn't required. We therefore mark that this package doesn't need dexopt unless |
| 177 | // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped |
| 178 | // it. |
| 179 | pkg.mDexOptPerformed.add(dexCodeInstructionSet); |
| 180 | } |
| 181 | |
| 182 | // If we've gotten here, we're sure that no error occurred and that we haven't |
| 183 | // deferred dex-opt. We've either dex-opted one more paths or instruction sets or |
| 184 | // we've skipped all of them because they are up to date. In both cases this |
| 185 | // package doesn't need dexopt any longer. |
| 186 | return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 187 | } |
| 188 | |
| 189 | private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets, |
| 190 | boolean forceDex, boolean defer, ArraySet<String> done) { |
| 191 | for (String libName : libs) { |
| 192 | PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary( |
| 193 | libName); |
| 194 | if (libPkg != null && !done.contains(libName)) { |
| 195 | performDexOptLI(libPkg, instructionSets, forceDex, defer, done); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Clears set of deferred dexopt packages. |
| 202 | * @return content of dexopt set if it was not empty |
| 203 | */ |
| 204 | public ArraySet<PackageParser.Package> clearDeferredDexOptPackages() { |
| 205 | ArraySet<PackageParser.Package> result = mDeferredDexOpt; |
| 206 | mDeferredDexOpt = null; |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | public void addPackageForDeferredDexopt(PackageParser.Package pkg) { |
| 211 | if (mDeferredDexOpt == null) { |
| 212 | mDeferredDexOpt = new ArraySet<PackageParser.Package>(); |
| 213 | } |
| 214 | mDeferredDexOpt.add(pkg); |
| 215 | } |
| 216 | } |