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