blob: c9613b4313c6d822196e6f45f377ec171cae8b36 [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 Kupolova627c092015-05-05 18:44:39 -070020import android.content.Context;
Fyodor Kupolov74876572015-02-23 17:14:45 -080021import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageParser;
Andreas Gampea8908752015-11-10 08:58:14 -080023import android.content.pm.PackageParser.Package;
Calin Juravled479b522016-02-24 16:22:03 +000024import android.os.Environment;
Fyodor Kupolova627c092015-05-05 18:44:39 -070025import android.os.PowerManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080026import android.os.UserHandle;
Fyodor Kupolova627c092015-05-05 18:44:39 -070027import android.os.WorkSource;
Calin Juravledb4a79a2015-12-23 18:55:08 +020028import android.os.storage.StorageManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080029import android.util.ArraySet;
30import android.util.Log;
31import android.util.Slog;
32
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070033import com.android.internal.os.InstallerConnection.InstallerException;
34
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080035import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080036import java.io.IOException;
37import java.util.ArrayList;
38import java.util.List;
39
40import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080041
Todd Kennedyfa54ab72015-09-25 07:46:12 -070042import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
43import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
44import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
45import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
David Brazdila0e10432016-01-20 14:04:40 +000046import static com.android.server.pm.Installer.DEXOPT_EXTRACTONLY;
Fyodor Kupolov74876572015-02-23 17:14:45 -080047import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
48import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
49
50/**
51 * Helper class for running dexopt command on packages.
52 */
Andreas Gampea8908752015-11-10 08:58:14 -080053class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080054 private static final String TAG = "PackageManager.DexOptimizer";
55 static final String OAT_DIR_NAME = "oat";
56 // TODO b/19550105 Remove error codes and use exceptions
Fyodor Kupolov74876572015-02-23 17:14:45 -080057 static final int DEX_OPT_SKIPPED = 0;
58 static final int DEX_OPT_PERFORMED = 1;
59 static final int DEX_OPT_DEFERRED = 2;
60 static final int DEX_OPT_FAILED = -1;
61
Andreas Gampea8908752015-11-10 08:58:14 -080062 private static final boolean DEBUG_DEXOPT = PackageManagerService.DEBUG_DEXOPT;
63
64 private final Installer mInstaller;
65 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080066
Fyodor Kupolova627c092015-05-05 18:44:39 -070067 private final PowerManager.WakeLock mDexoptWakeLock;
68 private volatile boolean mSystemReady;
69
Andreas Gampea8908752015-11-10 08:58:14 -080070 PackageDexOptimizer(Installer installer, Object installLock, Context context,
71 String wakeLockTag) {
72 this.mInstaller = installer;
73 this.mInstallLock = installLock;
74
75 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
76 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
77 }
78
79 protected PackageDexOptimizer(PackageDexOptimizer from) {
80 this.mInstaller = from.mInstaller;
81 this.mInstallLock = from.mInstallLock;
82 this.mDexoptWakeLock = from.mDexoptWakeLock;
83 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -080084 }
85
Calin Juravledb4a79a2015-12-23 18:55:08 +020086 static boolean canOptimizePackage(PackageParser.Package pkg) {
Calin Juravle6dfd83d2016-01-29 18:23:57 +000087 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
Calin Juravledb4a79a2015-12-23 18:55:08 +020088 }
89
Fyodor Kupolov74876572015-02-23 17:14:45 -080090 /**
91 * Performs dexopt on all code paths and libraries of the specified package for specified
92 * instruction sets.
93 *
Andreas Gampea8908752015-11-10 08:58:14 -080094 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
95 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -080096 */
Andreas Gampea8908752015-11-10 08:58:14 -080097 int performDexOpt(PackageParser.Package pkg, String[] instructionSets, boolean useProfiles,
98 boolean extractOnly) {
99 synchronized (mInstallLock) {
Fyodor Kupolova627c092015-05-05 18:44:39 -0700100 final boolean useLock = mSystemReady;
101 if (useLock) {
102 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
103 mDexoptWakeLock.acquire();
104 }
105 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800106 return performDexOptLI(pkg, instructionSets, useProfiles, extractOnly);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700107 } finally {
108 if (useLock) {
109 mDexoptWakeLock.release();
110 }
111 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800112 }
113 }
114
Andreas Gampea8908752015-11-10 08:58:14 -0800115 /**
Andreas Gampea8908752015-11-10 08:58:14 -0800116 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
117 * optimize or not (and in what way).
118 */
119 protected int adjustDexoptNeeded(int dexoptNeeded) {
120 return dexoptNeeded;
121 }
122
123 /**
124 * Adjust the given dexopt flags that will be passed to the installer.
125 */
126 protected int adjustDexoptFlags(int dexoptFlags) {
127 return dexoptFlags;
128 }
129
Fyodor Kupolov74876572015-02-23 17:14:45 -0800130 private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
Andreas Gampea8908752015-11-10 08:58:14 -0800131 boolean useProfiles, boolean extractOnly) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800132 final String[] instructionSets = targetInstructionSets != null ?
133 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
134
Calin Juravledb4a79a2015-12-23 18:55:08 +0200135 if (!canOptimizePackage(pkg)) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800136 return DEX_OPT_SKIPPED;
137 }
138
139 final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
140 final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
141
142 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
143 boolean performedDexOpt = false;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800144 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
145 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800146 for (String path : paths) {
Calin Juravled479b522016-02-24 16:22:03 +0000147 if (useProfiles && isUsedByOtherApps(path)) {
148 // We cannot use profile guided compilation if the apk was used by another app.
149 useProfiles = false;
150 }
Calin Juravledb4a79a2015-12-23 18:55:08 +0200151 int dexoptNeeded;
David Brazdil493411a2016-02-01 13:48:46 +0000152
Andreas Gampea8908752015-11-10 08:58:14 -0800153 try {
Calin Juravle693f9972016-02-25 15:26:20 +0000154 int compilationTypeMask = 0;
155 if (extractOnly) {
156 // For extract only, any type of compilation is good.
157 compilationTypeMask = DexFile.COMPILATION_TYPE_FULL
158 | DexFile.COMPILATION_TYPE_PROFILE_GUIDE
159 | DexFile.COMPILATION_TYPE_EXTRACT_ONLY;
160 } else {
161 // Branch taken for profile guide and full compilation.
162 // Profile guide compilation should only recompile a previous
163 // profile compiled/extract only file and should not be attempted if the
164 // apk is already fully compiled. So test against a full compilation type.
165 compilationTypeMask = DexFile.COMPILATION_TYPE_FULL;
166 }
167 dexoptNeeded = DexFile.getDexOptNeeded(path,
168 dexCodeInstructionSet, compilationTypeMask);
Andreas Gampea8908752015-11-10 08:58:14 -0800169 } catch (IOException ioe) {
170 Slog.w(TAG, "IOException reading apk: " + path, ioe);
171 return DEX_OPT_FAILED;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100172 }
Andreas Gampea8908752015-11-10 08:58:14 -0800173 dexoptNeeded = adjustDexoptNeeded(dexoptNeeded);
Richard Uhler7b08b352015-03-25 16:25:57 -0700174
Calin Juravledb4a79a2015-12-23 18:55:08 +0200175 final String dexoptType;
176 String oatDir = null;
Calin Juravle693f9972016-02-25 15:26:20 +0000177 switch (dexoptNeeded) {
178 case DexFile.NO_DEXOPT_NEEDED:
179 continue;
180 case DexFile.DEX2OAT_NEEDED:
181 dexoptType = "dex2oat";
182 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
183 break;
184 case DexFile.PATCHOAT_NEEDED:
185 dexoptType = "patchoat";
186 break;
187 case DexFile.SELF_PATCHOAT_NEEDED:
188 dexoptType = "self patchoat";
189 break;
190 default:
191 throw new IllegalStateException("Invalid dexopt:" + dexoptNeeded);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200192 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100193
Andreas Gampea8908752015-11-10 08:58:14 -0800194
Calin Juravledb4a79a2015-12-23 18:55:08 +0200195 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
196 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
197 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
David Brazdila0e10432016-01-20 14:04:40 +0000198 + " extractOnly=" + extractOnly + " oatDir = " + oatDir);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200199 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
Calin Juravled479b522016-02-24 16:22:03 +0000200 // Profile guide compiled oat files should not be public.
201 final boolean isPublic = !pkg.isForwardLocked() && !useProfiles;
Andreas Gampea8908752015-11-10 08:58:14 -0800202 final int dexFlags = adjustDexoptFlags(
Calin Juravled479b522016-02-24 16:22:03 +0000203 ( isPublic ? DEXOPT_PUBLIC : 0)
Calin Juravledb4a79a2015-12-23 18:55:08 +0200204 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
205 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
David Brazdila0e10432016-01-20 14:04:40 +0000206 | (extractOnly ? DEXOPT_EXTRACTONLY : 0)
Andreas Gampea8908752015-11-10 08:58:14 -0800207 | DEXOPT_BOOTCOMPLETE);
208
Calin Juravledb4a79a2015-12-23 18:55:08 +0200209 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800210 mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet,
211 dexoptNeeded, oatDir, dexFlags, pkg.volumeUuid, useProfiles);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200212 performedDexOpt = true;
213 } catch (InstallerException e) {
214 Slog.w(TAG, "Failed to dexopt", e);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800215 }
216 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800217 }
218
219 // If we've gotten here, we're sure that no error occurred and that we haven't
220 // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
221 // we've skipped all of them because they are up to date. In both cases this
222 // package doesn't need dexopt any longer.
223 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
224 }
225
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800226 /**
227 * Creates oat dir for the specified package. In certain cases oat directory
228 * <strong>cannot</strong> be created:
229 * <ul>
230 * <li>{@code pkg} is a system app, which is not updated.</li>
231 * <li>Package location is not a directory, i.e. monolithic install.</li>
232 * </ul>
233 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700234 * @return Absolute path to the oat directory or null, if oat directory
235 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800236 */
237 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700238 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700239 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800240 return null;
241 }
242 File codePath = new File(pkg.codePath);
243 if (codePath.isDirectory()) {
244 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700245 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800246 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700247 } catch (InstallerException e) {
248 Slog.w(TAG, "Failed to create oat dir", e);
249 return null;
250 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700251 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800252 }
253 return null;
254 }
255
256 static File getOatDir(File codePath) {
257 return new File(codePath, OAT_DIR_NAME);
258 }
259
Fyodor Kupolova627c092015-05-05 18:44:39 -0700260 void systemReady() {
261 mSystemReady = true;
262 }
Andreas Gampea8908752015-11-10 08:58:14 -0800263
Calin Juravled479b522016-02-24 16:22:03 +0000264 private boolean isUsedByOtherApps(String apkPath) {
265 try {
266 apkPath = new File(apkPath).getCanonicalPath();
267 } catch (IOException e) {
268 // Log an error but continue without it.
269 Slog.w(TAG, "Failed to get canonical path", e);
270 }
271 String useMarker = apkPath.replace('/', '@');
272 final int[] currentUserIds = UserManagerService.getInstance().getUserIds();
273 for (int i = 0; i < currentUserIds.length; i++) {
274 File profileDir = Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]);
275 File foreignUseMark = new File(profileDir, useMarker);
276 if (foreignUseMark.exists()) {
277 return true;
278 }
279 }
280 return false;
281 }
282
Andreas Gampea8908752015-11-10 08:58:14 -0800283 /**
284 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
285 * dexopt path.
286 */
287 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
288
289 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
290 Context context, String wakeLockTag) {
291 super(installer, installLock, context, wakeLockTag);
292 }
293
294 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
295 super(from);
296 }
297
298 @Override
Andreas Gampea8908752015-11-10 08:58:14 -0800299 protected int adjustDexoptNeeded(int dexoptNeeded) {
300 // Ensure compilation, no matter the current state.
301 // TODO: The return value is wrong when patchoat is needed.
302 return DexFile.DEX2OAT_NEEDED;
303 }
304 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800305}