blob: f6202742e735ea693d7b7f849f7ca1bc0c7f8ecf [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;
Calin Juravled479b522016-02-24 16:22:03 +000023import android.os.Environment;
Fyodor Kupolova627c092015-05-05 18:44:39 -070024import android.os.PowerManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080025import android.os.UserHandle;
Fyodor Kupolova627c092015-05-05 18:44:39 -070026import android.os.WorkSource;
Fyodor Kupolov74876572015-02-23 17:14:45 -080027import android.util.Log;
28import android.util.Slog;
29
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070030import com.android.internal.os.InstallerConnection.InstallerException;
31
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080032import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080033import java.io.IOException;
Fyodor Kupolov74876572015-02-23 17:14:45 -080034import java.util.List;
35
36import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080037
Todd Kennedyfa54ab72015-09-25 07:46:12 -070038import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
39import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
Andreas Gampebdd30d82016-03-20 11:32:11 -070040import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED;
Todd Kennedyfa54ab72015-09-25 07:46:12 -070041import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
42import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
Fyodor Kupolov74876572015-02-23 17:14:45 -080043import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
44import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
Andreas Gampe47c170a2016-03-30 17:21:19 -070045import static com.android.server.pm.PackageManagerServiceCompilerMapping.getNonProfileGuidedCompilerFilter;
Fyodor Kupolov74876572015-02-23 17:14:45 -080046
47/**
48 * Helper class for running dexopt command on packages.
49 */
Andreas Gampea8908752015-11-10 08:58:14 -080050class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080051 private static final String TAG = "PackageManager.DexOptimizer";
52 static final String OAT_DIR_NAME = "oat";
53 // TODO b/19550105 Remove error codes and use exceptions
Fyodor Kupolov74876572015-02-23 17:14:45 -080054 static final int DEX_OPT_SKIPPED = 0;
55 static final int DEX_OPT_PERFORMED = 1;
Fyodor Kupolov74876572015-02-23 17:14:45 -080056 static final int DEX_OPT_FAILED = -1;
57
Andreas Gampea8908752015-11-10 08:58:14 -080058 private final Installer mInstaller;
59 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080060
Fyodor Kupolova627c092015-05-05 18:44:39 -070061 private final PowerManager.WakeLock mDexoptWakeLock;
62 private volatile boolean mSystemReady;
63
Andreas Gampea8908752015-11-10 08:58:14 -080064 PackageDexOptimizer(Installer installer, Object installLock, Context context,
65 String wakeLockTag) {
66 this.mInstaller = installer;
67 this.mInstallLock = installLock;
68
69 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
70 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
71 }
72
73 protected PackageDexOptimizer(PackageDexOptimizer from) {
74 this.mInstaller = from.mInstaller;
75 this.mInstallLock = from.mInstallLock;
76 this.mDexoptWakeLock = from.mDexoptWakeLock;
77 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -080078 }
79
Calin Juravledb4a79a2015-12-23 18:55:08 +020080 static boolean canOptimizePackage(PackageParser.Package pkg) {
Calin Juravle6dfd83d2016-01-29 18:23:57 +000081 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
Calin Juravledb4a79a2015-12-23 18:55:08 +020082 }
83
Fyodor Kupolov74876572015-02-23 17:14:45 -080084 /**
85 * Performs dexopt on all code paths and libraries of the specified package for specified
86 * instruction sets.
87 *
Andreas Gampea8908752015-11-10 08:58:14 -080088 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
89 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -080090 */
Andreas Gampebdd30d82016-03-20 11:32:11 -070091 int performDexOpt(PackageParser.Package pkg, String[] instructionSets, boolean checkProfiles,
92 String targetCompilationFilter) {
Andreas Gampea8908752015-11-10 08:58:14 -080093 synchronized (mInstallLock) {
Fyodor Kupolova627c092015-05-05 18:44:39 -070094 final boolean useLock = mSystemReady;
95 if (useLock) {
96 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
97 mDexoptWakeLock.acquire();
98 }
99 try {
Andreas Gampebdd30d82016-03-20 11:32:11 -0700100 return performDexOptLI(pkg, instructionSets, checkProfiles,
101 targetCompilationFilter);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700102 } finally {
103 if (useLock) {
104 mDexoptWakeLock.release();
105 }
106 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800107 }
108 }
109
Andreas Gampea8908752015-11-10 08:58:14 -0800110 /**
Andreas Gampea8908752015-11-10 08:58:14 -0800111 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
112 * optimize or not (and in what way).
113 */
114 protected int adjustDexoptNeeded(int dexoptNeeded) {
115 return dexoptNeeded;
116 }
117
118 /**
119 * Adjust the given dexopt flags that will be passed to the installer.
120 */
121 protected int adjustDexoptFlags(int dexoptFlags) {
122 return dexoptFlags;
123 }
124
Fyodor Kupolov74876572015-02-23 17:14:45 -0800125 private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
Andreas Gampebdd30d82016-03-20 11:32:11 -0700126 boolean checkProfiles, String targetCompilerFilter) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800127 final String[] instructionSets = targetInstructionSets != null ?
128 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
129
Calin Juravledb4a79a2015-12-23 18:55:08 +0200130 if (!canOptimizePackage(pkg)) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800131 return DEX_OPT_SKIPPED;
132 }
133
Andreas Gampebdd30d82016-03-20 11:32:11 -0700134 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
135 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
136
137 boolean isProfileGuidedFilter = DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter);
138 // If any part of the app is used by other apps, we cannot use profile-guided
139 // compilation.
David Brazdil90e26992016-04-18 14:08:52 +0100140 if (isProfileGuidedFilter && isUsedByOtherApps(pkg)) {
141 checkProfiles = false;
Andreas Gampebdd30d82016-03-20 11:32:11 -0700142
David Brazdil90e26992016-04-18 14:08:52 +0100143 targetCompilerFilter = getNonProfileGuidedCompilerFilter(targetCompilerFilter);
144 if (DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter)) {
145 throw new IllegalStateException(targetCompilerFilter);
Andreas Gampebdd30d82016-03-20 11:32:11 -0700146 }
David Brazdil90e26992016-04-18 14:08:52 +0100147 isProfileGuidedFilter = false;
Andreas Gampebdd30d82016-03-20 11:32:11 -0700148 }
149
150 // If we're asked to take profile updates into account, check now.
151 boolean newProfile = false;
152 if (checkProfiles && isProfileGuidedFilter) {
153 // Merge profiles, see if we need to do anything.
154 try {
155 newProfile = mInstaller.mergeProfiles(sharedGid, pkg.packageName);
156 } catch (InstallerException e) {
157 Slog.w(TAG, "Failed to merge profiles", e);
158 }
159 }
160
Fyodor Kupolov74876572015-02-23 17:14:45 -0800161 final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
162 final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
163
Fyodor Kupolov74876572015-02-23 17:14:45 -0800164 boolean performedDexOpt = false;
David Brazdil6a3b2d22016-04-08 16:09:06 +0100165 boolean successfulDexOpt = true;
166
Fyodor Kupolov74876572015-02-23 17:14:45 -0800167 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
168 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800169 for (String path : paths) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200170 int dexoptNeeded;
Andreas Gampea8908752015-11-10 08:58:14 -0800171 try {
Calin Juravle693f9972016-02-25 15:26:20 +0000172 dexoptNeeded = DexFile.getDexOptNeeded(path,
Andreas Gampebdd30d82016-03-20 11:32:11 -0700173 dexCodeInstructionSet, targetCompilerFilter, newProfile);
Andreas Gampea8908752015-11-10 08:58:14 -0800174 } catch (IOException ioe) {
175 Slog.w(TAG, "IOException reading apk: " + path, ioe);
176 return DEX_OPT_FAILED;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100177 }
Andreas Gampea8908752015-11-10 08:58:14 -0800178 dexoptNeeded = adjustDexoptNeeded(dexoptNeeded);
Andreas Gampe47c170a2016-03-30 17:21:19 -0700179 if (PackageManagerService.DEBUG_DEXOPT) {
180 Log.i(TAG, "DexoptNeeded for " + path + "@" + targetCompilerFilter + " is " +
181 dexoptNeeded);
182 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700183
Calin Juravledb4a79a2015-12-23 18:55:08 +0200184 final String dexoptType;
185 String oatDir = null;
Calin Juravle693f9972016-02-25 15:26:20 +0000186 switch (dexoptNeeded) {
187 case DexFile.NO_DEXOPT_NEEDED:
188 continue;
189 case DexFile.DEX2OAT_NEEDED:
190 dexoptType = "dex2oat";
191 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
192 break;
193 case DexFile.PATCHOAT_NEEDED:
194 dexoptType = "patchoat";
195 break;
196 case DexFile.SELF_PATCHOAT_NEEDED:
197 dexoptType = "self patchoat";
198 break;
199 default:
200 throw new IllegalStateException("Invalid dexopt:" + dexoptNeeded);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200201 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100202
Calin Juravledb4a79a2015-12-23 18:55:08 +0200203 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
204 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
205 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
Andreas Gampebdd30d82016-03-20 11:32:11 -0700206 + " target-filter=" + targetCompilerFilter + " oatDir = " + oatDir);
Calin Juravled479b522016-02-24 16:22:03 +0000207 // Profile guide compiled oat files should not be public.
Andreas Gampebdd30d82016-03-20 11:32:11 -0700208 final boolean isPublic = !pkg.isForwardLocked() && !isProfileGuidedFilter;
209 final int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
Andreas Gampea8908752015-11-10 08:58:14 -0800210 final int dexFlags = adjustDexoptFlags(
Calin Juravled479b522016-02-24 16:22:03 +0000211 ( isPublic ? DEXOPT_PUBLIC : 0)
Calin Juravledb4a79a2015-12-23 18:55:08 +0200212 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
213 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
Andreas Gampebdd30d82016-03-20 11:32:11 -0700214 | profileFlag
Andreas Gampea8908752015-11-10 08:58:14 -0800215 | DEXOPT_BOOTCOMPLETE);
216
Calin Juravledb4a79a2015-12-23 18:55:08 +0200217 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800218 mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet,
Andreas Gampebdd30d82016-03-20 11:32:11 -0700219 dexoptNeeded, oatDir, dexFlags, targetCompilerFilter, pkg.volumeUuid);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200220 performedDexOpt = true;
221 } catch (InstallerException e) {
222 Slog.w(TAG, "Failed to dexopt", e);
David Brazdil6a3b2d22016-04-08 16:09:06 +0100223 successfulDexOpt = false;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800224 }
225 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800226 }
227
David Brazdil6a3b2d22016-04-08 16:09:06 +0100228 if (successfulDexOpt) {
229 // If we've gotten here, we're sure that no error occurred. We've either
230 // dex-opted one or more paths or instruction sets or we've skipped
231 // all of them because they are up to date. In both cases this package
232 // doesn't need dexopt any longer.
233 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
234 } else {
235 return DEX_OPT_FAILED;
236 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800237 }
238
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800239 /**
240 * Creates oat dir for the specified package. In certain cases oat directory
241 * <strong>cannot</strong> be created:
242 * <ul>
243 * <li>{@code pkg} is a system app, which is not updated.</li>
244 * <li>Package location is not a directory, i.e. monolithic install.</li>
245 * </ul>
246 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700247 * @return Absolute path to the oat directory or null, if oat directory
248 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800249 */
250 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700251 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700252 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800253 return null;
254 }
255 File codePath = new File(pkg.codePath);
256 if (codePath.isDirectory()) {
257 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700258 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800259 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700260 } catch (InstallerException e) {
261 Slog.w(TAG, "Failed to create oat dir", e);
262 return null;
263 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700264 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800265 }
266 return null;
267 }
268
269 static File getOatDir(File codePath) {
270 return new File(codePath, OAT_DIR_NAME);
271 }
272
Fyodor Kupolova627c092015-05-05 18:44:39 -0700273 void systemReady() {
274 mSystemReady = true;
275 }
Andreas Gampea8908752015-11-10 08:58:14 -0800276
David Brazdil90e26992016-04-18 14:08:52 +0100277 /**
278 * Returns true if the profiling data collected for the given app indicate
279 * that the apps's APK has been loaded by another app.
280 * Note that this returns false for all forward-locked apps and apps without
281 * any collected profiling data.
282 */
283 public static boolean isUsedByOtherApps(PackageParser.Package pkg) {
284 if (pkg.isForwardLocked()) {
285 // Skip the check for forward locked packages since they don't share their code.
286 return false;
Calin Juravled479b522016-02-24 16:22:03 +0000287 }
David Brazdil90e26992016-04-18 14:08:52 +0100288
289 for (String apkPath : pkg.getAllCodePathsExcludingResourceOnly()) {
290 try {
291 apkPath = new File(apkPath).getCanonicalPath();
292 } catch (IOException e) {
293 // Log an error but continue without it.
294 Slog.w(TAG, "Failed to get canonical path", e);
295 }
296 String useMarker = apkPath.replace('/', '@');
297 final int[] currentUserIds = UserManagerService.getInstance().getUserIds();
298 for (int i = 0; i < currentUserIds.length; i++) {
299 File profileDir =
300 Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]);
301 File foreignUseMark = new File(profileDir, useMarker);
302 if (foreignUseMark.exists()) {
303 return true;
304 }
Calin Juravled479b522016-02-24 16:22:03 +0000305 }
306 }
307 return false;
308 }
309
Andreas Gampea8908752015-11-10 08:58:14 -0800310 /**
311 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
312 * dexopt path.
313 */
314 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
315
316 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
317 Context context, String wakeLockTag) {
318 super(installer, installLock, context, wakeLockTag);
319 }
320
321 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
322 super(from);
323 }
324
325 @Override
Andreas Gampea8908752015-11-10 08:58:14 -0800326 protected int adjustDexoptNeeded(int dexoptNeeded) {
327 // Ensure compilation, no matter the current state.
328 // TODO: The return value is wrong when patchoat is needed.
329 return DexFile.DEX2OAT_NEEDED;
330 }
331 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800332}