blob: 4ca615dd55c9a9010066312e43e036578e1347e1 [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 Gampebdd30d82016-03-20 11:32:11 -070045import static com.android.server.pm.PackageManagerServiceCompilerMapping.getFullCompilerFilter;
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;
56 static final int DEX_OPT_DEFERRED = 2;
57 static final int DEX_OPT_FAILED = -1;
58
Andreas Gampea8908752015-11-10 08:58:14 -080059 private final Installer mInstaller;
60 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080061
Fyodor Kupolova627c092015-05-05 18:44:39 -070062 private final PowerManager.WakeLock mDexoptWakeLock;
63 private volatile boolean mSystemReady;
64
Andreas Gampea8908752015-11-10 08:58:14 -080065 PackageDexOptimizer(Installer installer, Object installLock, Context context,
66 String wakeLockTag) {
67 this.mInstaller = installer;
68 this.mInstallLock = installLock;
69
70 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
71 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
72 }
73
74 protected PackageDexOptimizer(PackageDexOptimizer from) {
75 this.mInstaller = from.mInstaller;
76 this.mInstallLock = from.mInstallLock;
77 this.mDexoptWakeLock = from.mDexoptWakeLock;
78 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -080079 }
80
Calin Juravledb4a79a2015-12-23 18:55:08 +020081 static boolean canOptimizePackage(PackageParser.Package pkg) {
Calin Juravle6dfd83d2016-01-29 18:23:57 +000082 return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
Calin Juravledb4a79a2015-12-23 18:55:08 +020083 }
84
Fyodor Kupolov74876572015-02-23 17:14:45 -080085 /**
86 * Performs dexopt on all code paths and libraries of the specified package for specified
87 * instruction sets.
88 *
Andreas Gampea8908752015-11-10 08:58:14 -080089 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
90 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -080091 */
Andreas Gampebdd30d82016-03-20 11:32:11 -070092 int performDexOpt(PackageParser.Package pkg, String[] instructionSets, boolean checkProfiles,
93 String targetCompilationFilter) {
Andreas Gampea8908752015-11-10 08:58:14 -080094 synchronized (mInstallLock) {
Fyodor Kupolova627c092015-05-05 18:44:39 -070095 final boolean useLock = mSystemReady;
96 if (useLock) {
97 mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
98 mDexoptWakeLock.acquire();
99 }
100 try {
Andreas Gampebdd30d82016-03-20 11:32:11 -0700101 return performDexOptLI(pkg, instructionSets, checkProfiles,
102 targetCompilationFilter);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700103 } finally {
104 if (useLock) {
105 mDexoptWakeLock.release();
106 }
107 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800108 }
109 }
110
Andreas Gampea8908752015-11-10 08:58:14 -0800111 /**
Andreas Gampea8908752015-11-10 08:58:14 -0800112 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
113 * optimize or not (and in what way).
114 */
115 protected int adjustDexoptNeeded(int dexoptNeeded) {
116 return dexoptNeeded;
117 }
118
119 /**
120 * Adjust the given dexopt flags that will be passed to the installer.
121 */
122 protected int adjustDexoptFlags(int dexoptFlags) {
123 return dexoptFlags;
124 }
125
Fyodor Kupolov74876572015-02-23 17:14:45 -0800126 private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
Andreas Gampebdd30d82016-03-20 11:32:11 -0700127 boolean checkProfiles, String targetCompilerFilter) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800128 final String[] instructionSets = targetInstructionSets != null ?
129 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
130
Calin Juravledb4a79a2015-12-23 18:55:08 +0200131 if (!canOptimizePackage(pkg)) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800132 return DEX_OPT_SKIPPED;
133 }
134
Andreas Gampebdd30d82016-03-20 11:32:11 -0700135 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
136 final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
137
138 boolean isProfileGuidedFilter = DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter);
139 // If any part of the app is used by other apps, we cannot use profile-guided
140 // compilation.
Calin Juravle9fa02542016-03-25 17:29:15 +0000141 // Skip the check for forward locked packages since they don't share their code.
142 if (isProfileGuidedFilter && !pkg.isForwardLocked()) {
Andreas Gampebdd30d82016-03-20 11:32:11 -0700143 for (String path : paths) {
144 if (isUsedByOtherApps(path)) {
145 checkProfiles = false;
146
147 // TODO: Should we only upgrade to the non-profile-guided version? That is,
148 // given verify-profile, should we move to interpret-only?
149 targetCompilerFilter = getFullCompilerFilter();
150 isProfileGuidedFilter = false;
151
152 break;
153 }
154 }
155 }
156
157 // If we're asked to take profile updates into account, check now.
158 boolean newProfile = false;
159 if (checkProfiles && isProfileGuidedFilter) {
160 // Merge profiles, see if we need to do anything.
161 try {
162 newProfile = mInstaller.mergeProfiles(sharedGid, pkg.packageName);
163 } catch (InstallerException e) {
164 Slog.w(TAG, "Failed to merge profiles", e);
165 }
166 }
167
Fyodor Kupolov74876572015-02-23 17:14:45 -0800168 final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
169 final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
170
Fyodor Kupolov74876572015-02-23 17:14:45 -0800171 boolean performedDexOpt = false;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800172 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
173 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
Fyodor Kupolov74876572015-02-23 17:14:45 -0800174 for (String path : paths) {
Calin Juravledb4a79a2015-12-23 18:55:08 +0200175 int dexoptNeeded;
Andreas Gampea8908752015-11-10 08:58:14 -0800176 try {
Calin Juravle693f9972016-02-25 15:26:20 +0000177 dexoptNeeded = DexFile.getDexOptNeeded(path,
Andreas Gampebdd30d82016-03-20 11:32:11 -0700178 dexCodeInstructionSet, targetCompilerFilter, newProfile);
Andreas Gampea8908752015-11-10 08:58:14 -0800179 } catch (IOException ioe) {
180 Slog.w(TAG, "IOException reading apk: " + path, ioe);
181 return DEX_OPT_FAILED;
Narayan Kamath01dcb762015-05-07 17:48:42 +0100182 }
Andreas Gampea8908752015-11-10 08:58:14 -0800183 dexoptNeeded = adjustDexoptNeeded(dexoptNeeded);
Richard Uhler7b08b352015-03-25 16:25:57 -0700184
Calin Juravledb4a79a2015-12-23 18:55:08 +0200185 final String dexoptType;
186 String oatDir = null;
Calin Juravle693f9972016-02-25 15:26:20 +0000187 switch (dexoptNeeded) {
188 case DexFile.NO_DEXOPT_NEEDED:
189 continue;
190 case DexFile.DEX2OAT_NEEDED:
191 dexoptType = "dex2oat";
192 oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
193 break;
194 case DexFile.PATCHOAT_NEEDED:
195 dexoptType = "patchoat";
196 break;
197 case DexFile.SELF_PATCHOAT_NEEDED:
198 dexoptType = "self patchoat";
199 break;
200 default:
201 throw new IllegalStateException("Invalid dexopt:" + dexoptNeeded);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200202 }
Narayan Kamath01dcb762015-05-07 17:48:42 +0100203
Calin Juravledb4a79a2015-12-23 18:55:08 +0200204 Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="
205 + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
206 + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
Andreas Gampebdd30d82016-03-20 11:32:11 -0700207 + " target-filter=" + targetCompilerFilter + " oatDir = " + oatDir);
Calin Juravled479b522016-02-24 16:22:03 +0000208 // Profile guide compiled oat files should not be public.
Andreas Gampebdd30d82016-03-20 11:32:11 -0700209 final boolean isPublic = !pkg.isForwardLocked() && !isProfileGuidedFilter;
210 final int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
Andreas Gampea8908752015-11-10 08:58:14 -0800211 final int dexFlags = adjustDexoptFlags(
Calin Juravled479b522016-02-24 16:22:03 +0000212 ( isPublic ? DEXOPT_PUBLIC : 0)
Calin Juravledb4a79a2015-12-23 18:55:08 +0200213 | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
214 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
Andreas Gampebdd30d82016-03-20 11:32:11 -0700215 | profileFlag
Andreas Gampea8908752015-11-10 08:58:14 -0800216 | DEXOPT_BOOTCOMPLETE);
217
Calin Juravledb4a79a2015-12-23 18:55:08 +0200218 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800219 mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet,
Andreas Gampebdd30d82016-03-20 11:32:11 -0700220 dexoptNeeded, oatDir, dexFlags, targetCompilerFilter, pkg.volumeUuid);
Calin Juravledb4a79a2015-12-23 18:55:08 +0200221 performedDexOpt = true;
222 } catch (InstallerException e) {
223 Slog.w(TAG, "Failed to dexopt", e);
Fyodor Kupolov74876572015-02-23 17:14:45 -0800224 }
225 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800226 }
227
228 // If we've gotten here, we're sure that no error occurred and that we haven't
229 // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
230 // we've skipped all of them because they are up to date. In both cases this
231 // package doesn't need dexopt any longer.
232 return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
233 }
234
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800235 /**
236 * Creates oat dir for the specified package. In certain cases oat directory
237 * <strong>cannot</strong> be created:
238 * <ul>
239 * <li>{@code pkg} is a system app, which is not updated.</li>
240 * <li>Package location is not a directory, i.e. monolithic install.</li>
241 * </ul>
242 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700243 * @return Absolute path to the oat directory or null, if oat directory
244 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800245 */
246 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700247 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700248 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800249 return null;
250 }
251 File codePath = new File(pkg.codePath);
252 if (codePath.isDirectory()) {
253 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700254 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800255 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700256 } catch (InstallerException e) {
257 Slog.w(TAG, "Failed to create oat dir", e);
258 return null;
259 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700260 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800261 }
262 return null;
263 }
264
265 static File getOatDir(File codePath) {
266 return new File(codePath, OAT_DIR_NAME);
267 }
268
Fyodor Kupolova627c092015-05-05 18:44:39 -0700269 void systemReady() {
270 mSystemReady = true;
271 }
Andreas Gampea8908752015-11-10 08:58:14 -0800272
Calin Juravled479b522016-02-24 16:22:03 +0000273 private boolean isUsedByOtherApps(String apkPath) {
274 try {
275 apkPath = new File(apkPath).getCanonicalPath();
276 } catch (IOException e) {
277 // Log an error but continue without it.
278 Slog.w(TAG, "Failed to get canonical path", e);
279 }
280 String useMarker = apkPath.replace('/', '@');
281 final int[] currentUserIds = UserManagerService.getInstance().getUserIds();
282 for (int i = 0; i < currentUserIds.length; i++) {
283 File profileDir = Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]);
284 File foreignUseMark = new File(profileDir, useMarker);
285 if (foreignUseMark.exists()) {
286 return true;
287 }
288 }
289 return false;
290 }
291
Andreas Gampea8908752015-11-10 08:58:14 -0800292 /**
293 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
294 * dexopt path.
295 */
296 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
297
298 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
299 Context context, String wakeLockTag) {
300 super(installer, installLock, context, wakeLockTag);
301 }
302
303 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
304 super(from);
305 }
306
307 @Override
Andreas Gampea8908752015-11-10 08:58:14 -0800308 protected int adjustDexoptNeeded(int dexoptNeeded) {
309 // Ensure compilation, no matter the current state.
310 // TODO: The return value is wrong when patchoat is needed.
311 return DexFile.DEX2OAT_NEEDED;
312 }
313 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800314}