blob: 4c36fa6eea659107634a7e27176d5e57ec87f80f [file] [log] [blame]
Fyodor Kupolov74876572015-02-23 17:14:45 -08001/*
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
17package com.android.server.pm;
18
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080019import android.annotation.Nullable;
Fyodor Kupolov74876572015-02-23 17:14:45 -080020import android.content.pm.ApplicationInfo;
21import android.content.pm.PackageParser;
22import android.os.UserHandle;
23import android.util.ArraySet;
24import android.util.Log;
25import android.util.Slog;
26
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080027import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080028import java.io.FileNotFoundException;
29import java.io.IOException;
30import java.util.ArrayList;
31import java.util.List;
32
33import dalvik.system.DexFile;
34import dalvik.system.StaleDexCacheError;
35
36import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
37import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
38
39/**
40 * Helper class for running dexopt command on packages.
41 */
42final class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080043 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 Kupolov74876572015-02-23 17:14:45 -080046 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 Uhler7b08b352015-03-25 16:25:57 -0700116 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 Kupolov74876572015-02-23 17:14:45 -0800146 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800147 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
148 + " oatDir = " + oatDir);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800149 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
Richard Uhler7b08b352015-03-25 16:25:57 -0700150 final int ret = mPackageManagerService.mInstaller.dexopt(path, sharedGid,
151 !pkg.isForwardLocked(), pkg.packageName, dexCodeInstructionSet,
152 dexoptNeeded, vmSafeMode, debuggable, oatDir);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800153 if (ret < 0) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800154 return DEX_OPT_FAILED;
155 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800156 performedDexOpt = true;
157 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800158 } 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 Uhler7b08b352015-03-25 16:25:57 -0700174 // either have either succeeded dexopt, or have had getDexOptNeeded tell us
Fyodor Kupolov74876572015-02-23 17:14:45 -0800175 // 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 Kupolovb94c1652015-03-03 12:25:30 -0800188 /**
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 Uhler7b08b352015-03-25 16:25:57 -0700196 * @return Absolute path to the oat directory or null, if oat directory
197 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800198 */
199 @Nullable
Richard Uhler7b08b352015-03-25 16:25:57 -0700200 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet)
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800201 throws IOException {
202 if (pkg.isSystemApp() && !pkg.isUpdatedSystemApp()) {
203 return null;
204 }
205 File codePath = new File(pkg.codePath);
206 if (codePath.isDirectory()) {
207 File oatDir = getOatDir(codePath);
208 mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(),
209 dexInstructionSet);
Richard Uhler7b08b352015-03-25 16:25:57 -0700210 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800211 }
212 return null;
213 }
214
215 static File getOatDir(File codePath) {
216 return new File(codePath, OAT_DIR_NAME);
217 }
218
Fyodor Kupolov74876572015-02-23 17:14:45 -0800219 private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets,
220 boolean forceDex, boolean defer, ArraySet<String> done) {
221 for (String libName : libs) {
222 PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary(
223 libName);
224 if (libPkg != null && !done.contains(libName)) {
225 performDexOptLI(libPkg, instructionSets, forceDex, defer, done);
226 }
227 }
228 }
229
230 /**
231 * Clears set of deferred dexopt packages.
232 * @return content of dexopt set if it was not empty
233 */
234 public ArraySet<PackageParser.Package> clearDeferredDexOptPackages() {
235 ArraySet<PackageParser.Package> result = mDeferredDexOpt;
236 mDeferredDexOpt = null;
237 return result;
238 }
239
240 public void addPackageForDeferredDexopt(PackageParser.Package pkg) {
241 if (mDeferredDexOpt == null) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800242 mDeferredDexOpt = new ArraySet<>();
Fyodor Kupolov74876572015-02-23 17:14:45 -0800243 }
244 mDeferredDexOpt.add(pkg);
245 }
246}