blob: 4b4637488016e6ac96b8c8827bc7e5b64978dd08 [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
Patrick Baumannfc2851e2018-11-13 15:23:22 -080019import static android.content.pm.ApplicationInfo.HIDDEN_API_ENFORCEMENT_DISABLED;
20
21import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
22import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
23import static com.android.server.pm.Installer.DEXOPT_ENABLE_HIDDEN_API_CHECKS;
24import static com.android.server.pm.Installer.DEXOPT_FORCE;
25import static com.android.server.pm.Installer.DEXOPT_GENERATE_APP_IMAGE;
26import static com.android.server.pm.Installer.DEXOPT_GENERATE_COMPACT_DEX;
27import static com.android.server.pm.Installer.DEXOPT_IDLE_BACKGROUND_JOB;
28import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED;
29import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
30import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX;
31import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE;
32import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE;
33import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
34import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
35import static com.android.server.pm.PackageManagerService.WATCHDOG_TIMEOUT;
36import static com.android.server.pm.PackageManagerServiceCompilerMapping.getReasonName;
37
38import static dalvik.system.DexFile.getSafeModeCompilerFilter;
39import static dalvik.system.DexFile.isProfileGuidedCompilerFilter;
40
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080041import android.annotation.Nullable;
Fyodor Kupolova627c092015-05-05 18:44:39 -070042import android.content.Context;
Fyodor Kupolov74876572015-02-23 17:14:45 -080043import android.content.pm.ApplicationInfo;
44import android.content.pm.PackageParser;
Nicolas Geoffray8d144eb2018-11-15 12:59:52 +000045import android.content.pm.SharedLibraryInfo;
Calin Juravle6ae39fc2018-01-19 20:32:47 -080046import android.content.pm.dex.ArtManager;
Calin Juravlecc651942018-02-01 17:20:51 +000047import android.content.pm.dex.DexMetadataHelper;
Calin Juravleadbadd52017-03-28 18:19:15 -070048import android.os.FileUtils;
Fyodor Kupolova627c092015-05-05 18:44:39 -070049import android.os.PowerManager;
Nicolas Geoffray653356f2019-02-26 23:24:34 +000050import android.os.Process;
Fyodor Kupolov28f28552017-05-02 12:11:02 -070051import android.os.SystemClock;
Nicolas Geoffray20a894e2017-09-08 13:01:40 +010052import android.os.SystemProperties;
Fyodor Kupolov74876572015-02-23 17:14:45 -080053import android.os.UserHandle;
Fyodor Kupolova627c092015-05-05 18:44:39 -070054import android.os.WorkSource;
Nicolas Geoffray653356f2019-02-26 23:24:34 +000055import android.os.storage.StorageManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080056import android.util.Log;
57import android.util.Slog;
58
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080059import com.android.internal.annotations.GuardedBy;
Narayan Kamath88eea9e2016-05-02 14:44:31 +010060import com.android.internal.util.IndentingPrintWriter;
Jeff Sharkey740f5232016-12-09 14:31:26 -070061import com.android.server.pm.Installer.InstallerException;
Calin Juravle0610dd12018-07-20 15:51:17 -070062import com.android.server.pm.dex.ArtManagerService;
Victor Hsieh785d6182018-04-19 14:26:28 -070063import com.android.server.pm.dex.DexManager;
Calin Juravle1d0e83d2017-07-17 15:12:01 -070064import com.android.server.pm.dex.DexoptOptions;
Calin Juravle19da1cf2017-07-12 18:52:49 -070065import com.android.server.pm.dex.DexoptUtils;
Calin Juravlef1ff36f2017-07-22 12:33:41 -070066import com.android.server.pm.dex.PackageDexUsage;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070067
Patrick Baumannfc2851e2018-11-13 15:23:22 -080068import dalvik.system.DexFile;
69
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080070import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080071import java.io.IOException;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080072import java.util.ArrayList;
Calin Juravle4c2b9552017-08-10 17:23:00 -070073import java.util.Arrays;
Fyodor Kupolov74876572015-02-23 17:14:45 -080074import java.util.List;
Calin Juravle41a57a62017-08-06 19:20:19 -070075import java.util.Map;
Fyodor Kupolov74876572015-02-23 17:14:45 -080076
Fyodor Kupolov74876572015-02-23 17:14:45 -080077/**
78 * Helper class for running dexopt command on packages.
79 */
Calin Juravlec22c30e2017-01-16 19:18:48 -080080public class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080081 private static final String TAG = "PackageManager.DexOptimizer";
82 static final String OAT_DIR_NAME = "oat";
83 // TODO b/19550105 Remove error codes and use exceptions
Calin Juravlec22c30e2017-01-16 19:18:48 -080084 public static final int DEX_OPT_SKIPPED = 0;
85 public static final int DEX_OPT_PERFORMED = 1;
86 public static final int DEX_OPT_FAILED = -1;
Fyodor Kupolov28f28552017-05-02 12:11:02 -070087 // One minute over PM WATCHDOG_TIMEOUT
88 private static final long WAKELOCK_TIMEOUT_MS = WATCHDOG_TIMEOUT + 1000 * 60;
Fyodor Kupolov74876572015-02-23 17:14:45 -080089
Fyodor Kupolov28f28552017-05-02 12:11:02 -070090 @GuardedBy("mInstallLock")
Andreas Gampea8908752015-11-10 08:58:14 -080091 private final Installer mInstaller;
92 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080093
Fyodor Kupolov28f28552017-05-02 12:11:02 -070094 @GuardedBy("mInstallLock")
Fyodor Kupolova627c092015-05-05 18:44:39 -070095 private final PowerManager.WakeLock mDexoptWakeLock;
96 private volatile boolean mSystemReady;
97
Andreas Gampea8908752015-11-10 08:58:14 -080098 PackageDexOptimizer(Installer installer, Object installLock, Context context,
99 String wakeLockTag) {
100 this.mInstaller = installer;
101 this.mInstallLock = installLock;
102
103 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
104 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
105 }
106
107 protected PackageDexOptimizer(PackageDexOptimizer from) {
108 this.mInstaller = from.mInstaller;
109 this.mInstallLock = from.mInstallLock;
110 this.mDexoptWakeLock = from.mDexoptWakeLock;
111 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800112 }
113
Calin Juravledb4a79a2015-12-23 18:55:08 +0200114 static boolean canOptimizePackage(PackageParser.Package pkg) {
Nicolas Geoffray20a894e2017-09-08 13:01:40 +0100115 // We do not dexopt a package with no code.
116 if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) {
117 return false;
118 }
119
Nicolas Geoffray20a894e2017-09-08 13:01:40 +0100120 return true;
Calin Juravledb4a79a2015-12-23 18:55:08 +0200121 }
122
Fyodor Kupolov74876572015-02-23 17:14:45 -0800123 /**
124 * Performs dexopt on all code paths and libraries of the specified package for specified
125 * instruction sets.
126 *
Andreas Gampea8908752015-11-10 08:58:14 -0800127 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
128 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -0800129 */
Nicolas Geoffray1158b872019-02-26 21:40:59 +0000130 int performDexOpt(PackageParser.Package pkg,
Calin Juravle1d0e83d2017-07-17 15:12:01 -0700131 String[] instructionSets, CompilerStats.PackageStats packageStats,
Calin Juravle3b74c412017-08-03 19:48:37 -0700132 PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) {
Calin Juravlec6540da2017-11-15 16:28:01 -0800133 if (pkg.applicationInfo.uid == -1) {
134 throw new IllegalArgumentException("Dexopt for " + pkg.packageName
135 + " has invalid uid.");
136 }
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800137 if (!canOptimizePackage(pkg)) {
138 return DEX_OPT_SKIPPED;
139 }
Andreas Gampea8908752015-11-10 08:58:14 -0800140 synchronized (mInstallLock) {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700141 final long acquireTime = acquireWakeLockLI(pkg.applicationInfo.uid);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700142 try {
Nicolas Geoffray1158b872019-02-26 21:40:59 +0000143 return performDexOptLI(pkg, instructionSets,
Calin Juravle3b74c412017-08-03 19:48:37 -0700144 packageStats, packageUseInfo, options);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700145 } finally {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700146 releaseWakeLockLI(acquireTime);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700147 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800148 }
149 }
150
Nicolas Geoffray653356f2019-02-26 23:24:34 +0000151 int performDexOpt(SharedLibraryInfo info, String[] instructionSets, DexoptOptions options) {
152 String classLoaderContext = DexoptUtils.getClassLoaderContext(info);
153 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
154 String compilerFilter = PackageManagerServiceCompilerMapping.getCompilerFilterForReason(
155 PackageManagerService.REASON_SHARED);
156 int result = DEX_OPT_SKIPPED;
157 for (String instructionSet : dexCodeInstructionSets) {
158 int dexoptNeeded = getDexoptNeeded(
159 info.getPath(), instructionSet, compilerFilter,
160 classLoaderContext, false /* newProfile */,
161 false /* downgrade */);
162 if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) {
163 continue;
164 }
165 // Special string recognized by installd.
166 final String packageName = "*";
167 final String outputPath = null;
168 int dexFlags = DEXOPT_PUBLIC
169 | (options.isBootComplete() ? DEXOPT_BOOTCOMPLETE : 0)
170 | (options.isDexoptIdleBackgroundJob() ? DEXOPT_IDLE_BACKGROUND_JOB : 0);
171 dexFlags = adjustDexoptFlags(dexFlags);
172 final String uuid = StorageManager.UUID_SYSTEM;
173 final String seInfo = null;
174 final int targetSdkVersion = 0; // Builtin libraries targets the system's SDK version
175 try {
176 mInstaller.dexopt(info.getPath(), Process.SYSTEM_UID, packageName,
177 instructionSet, dexoptNeeded, outputPath, dexFlags, compilerFilter,
178 uuid, classLoaderContext, seInfo, false /* downgrade */,
179 targetSdkVersion, /*profileName*/ null, /*dexMetadataPath*/ null,
180 getReasonName(options.getCompilationReason()));
181 // The end result is:
182 // - FAILED if any path failed,
183 // - PERFORMED if at least one path needed compilation,
184 // - SKIPPED when all paths are up to date
185 if (result != DEX_OPT_FAILED) {
186 result = DEX_OPT_PERFORMED;
187 }
188 } catch (InstallerException e) {
189 Slog.w(TAG, "Failed to dexopt", e);
190 result = DEX_OPT_FAILED;
191 }
192 }
193 return result;
194 }
195
Andreas Gampea8908752015-11-10 08:58:14 -0800196 /**
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800197 * Performs dexopt on all code paths of the given package.
198 * It assumes the install lock is held.
199 */
200 @GuardedBy("mInstallLock")
Nicolas Geoffray8d144eb2018-11-15 12:59:52 +0000201 private int performDexOptLI(PackageParser.Package pkg,
Calin Juravle1d0e83d2017-07-17 15:12:01 -0700202 String[] targetInstructionSets, CompilerStats.PackageStats packageStats,
Calin Juravle3b74c412017-08-03 19:48:37 -0700203 PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) {
Nicolas Geoffray1158b872019-02-26 21:40:59 +0000204 final List<SharedLibraryInfo> sharedLibraries = pkg.usesLibraryInfos;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800205 final String[] instructionSets = targetInstructionSets != null ?
206 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
207 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
Jeff Hao5def5e52017-04-19 14:42:16 -0700208 final List<String> paths = pkg.getAllCodePaths();
Jeff Sharkeya73e1652017-11-15 19:07:14 -0700209
210 int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
211 if (sharedGid == -1) {
212 Slog.wtf(TAG, "Well this is awkward; package " + pkg.applicationInfo.name + " had UID "
213 + pkg.applicationInfo.uid, new Throwable());
214 sharedGid = android.os.Process.NOBODY_UID;
215 }
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800216
Calin Juravle19da1cf2017-07-12 18:52:49 -0700217 // Get the class loader context dependencies.
218 // For each code path in the package, this array contains the class loader context that
219 // needs to be passed to dexopt in order to ensure correct optimizations.
Calin Juravleda098152017-09-01 17:30:01 -0700220 boolean[] pathsWithCode = new boolean[paths.size()];
221 pathsWithCode[0] = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
222 for (int i = 1; i < paths.size(); i++) {
223 pathsWithCode[i] = (pkg.splitFlags[i - 1] & ApplicationInfo.FLAG_HAS_CODE) != 0;
224 }
Calin Juravle19da1cf2017-07-12 18:52:49 -0700225 String[] classLoaderContexts = DexoptUtils.getClassLoaderContexts(
Calin Juravleda098152017-09-01 17:30:01 -0700226 pkg.applicationInfo, sharedLibraries, pathsWithCode);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800227
Calin Juravle4c2b9552017-08-10 17:23:00 -0700228 // Sanity check that we do not call dexopt with inconsistent data.
229 if (paths.size() != classLoaderContexts.length) {
230 String[] splitCodePaths = pkg.applicationInfo.getSplitCodePaths();
231 throw new IllegalStateException("Inconsistent information "
232 + "between PackageParser.Package and its ApplicationInfo. "
233 + "pkg.getAllCodePaths=" + paths
234 + " pkg.applicationInfo.getBaseCodePath=" + pkg.applicationInfo.getBaseCodePath()
235 + " pkg.applicationInfo.getSplitCodePaths="
236 + (splitCodePaths == null ? "null" : Arrays.toString(splitCodePaths)));
237 }
238
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800239 int result = DEX_OPT_SKIPPED;
Jeff Hao5def5e52017-04-19 14:42:16 -0700240 for (int i = 0; i < paths.size(); i++) {
241 // Skip paths that have no code.
Calin Juravleda098152017-09-01 17:30:01 -0700242 if (!pathsWithCode[i]) {
Jeff Hao5def5e52017-04-19 14:42:16 -0700243 continue;
244 }
Calin Juravleda098152017-09-01 17:30:01 -0700245 if (classLoaderContexts[i] == null) {
246 throw new IllegalStateException("Inconsistent information in the "
247 + "package structure. A split is marked to contain code "
248 + "but has no dependency listed. Index=" + i + " path=" + paths.get(i));
249 }
250
Jeff Hao5def5e52017-04-19 14:42:16 -0700251 // Append shared libraries with split dependencies for this split.
252 String path = paths.get(i);
Calin Juravleb6f844d2017-07-17 15:23:21 -0700253 if (options.getSplitName() != null) {
254 // We are asked to compile only a specific split. Check that the current path is
255 // what we are looking for.
256 if (!options.getSplitName().equals(new File(path).getName())) {
257 continue;
258 }
259 }
260
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800261 String profileName = ArtManager.getProfileName(i == 0 ? null : pkg.splitNames[i - 1]);
262
Calin Juravlecc651942018-02-01 17:20:51 +0000263 String dexMetadataPath = null;
264 if (options.isDexoptInstallWithDexMetadata()) {
265 File dexMetadataFile = DexMetadataHelper.findDexMetadataForFile(new File(path));
266 dexMetadataPath = dexMetadataFile == null
267 ? null : dexMetadataFile.getAbsolutePath();
268 }
269
Calin Juravle3b74c412017-08-03 19:48:37 -0700270 final boolean isUsedByOtherApps = options.isDexoptAsSharedLibrary()
Calin Juravle52a452c2017-08-04 01:42:17 -0700271 || packageUseInfo.isUsedByOtherApps(path);
Calin Juravle3b74c412017-08-03 19:48:37 -0700272 final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo,
273 options.getCompilerFilter(), isUsedByOtherApps);
274 final boolean profileUpdated = options.isCheckForProfileUpdates() &&
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800275 isProfileUpdated(pkg, sharedGid, profileName, compilerFilter);
Calin Juravle3b74c412017-08-03 19:48:37 -0700276
277 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct
278 // flags.
Andreas Gampec041c332017-11-01 17:05:53 -0700279 final int dexoptFlags = getDexFlags(pkg, compilerFilter, options);
Calin Juravle3b74c412017-08-03 19:48:37 -0700280
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800281 for (String dexCodeIsa : dexCodeInstructionSets) {
Calin Juravle19da1cf2017-07-12 18:52:49 -0700282 int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter,
283 profileUpdated, classLoaderContexts[i], dexoptFlags, sharedGid,
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800284 packageStats, options.isDowngrade(), profileName, dexMetadataPath,
285 options.getCompilationReason());
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800286 // The end result is:
287 // - FAILED if any path failed,
288 // - PERFORMED if at least one path needed compilation,
289 // - SKIPPED when all paths are up to date
290 if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) {
291 result = newResult;
292 }
293 }
294 }
295 return result;
296 }
297
298 /**
299 * Performs dexopt on the {@code path} belonging to the package {@code pkg}.
300 *
301 * @return
302 * DEX_OPT_FAILED if there was any exception during dexopt
303 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
304 * DEX_OPT_SKIPPED if the path does not need to be deopt-ed.
305 */
306 @GuardedBy("mInstallLock")
307 private int dexOptPath(PackageParser.Package pkg, String path, String isa,
Calin Juravle576e6c02017-09-12 00:58:33 -0700308 String compilerFilter, boolean profileUpdated, String classLoaderContext,
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800309 int dexoptFlags, int uid, CompilerStats.PackageStats packageStats, boolean downgrade,
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800310 String profileName, String dexMetadataPath, int compilationReason) {
Calin Juravle576e6c02017-09-12 00:58:33 -0700311 int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, classLoaderContext,
312 profileUpdated, downgrade);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800313 if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) {
314 return DEX_OPT_SKIPPED;
315 }
316
317 // TODO(calin): there's no need to try to create the oat dir over and over again,
318 // especially since it involve an extra installd call. We should create
319 // if (if supported) on the fly during the dexopt call.
320 String oatDir = createOatDirIfSupported(pkg, isa);
321
322 Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path
323 + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa
324 + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
Calin Juravle576e6c02017-09-12 00:58:33 -0700325 + " targetFilter=" + compilerFilter + " oatDir=" + oatDir
326 + " classLoaderContext=" + classLoaderContext);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800327
328 try {
329 long startTime = System.currentTimeMillis();
330
Shubham Ajmera246dccf2017-05-24 17:46:36 -0700331 // TODO: Consider adding 2 different APIs for primary and secondary dexopt.
332 // installd only uses downgrade flag for secondary dex files and ignores it for
333 // primary dex files.
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800334 mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags,
Calin Juravle576e6c02017-09-12 00:58:33 -0700335 compilerFilter, pkg.volumeUuid, classLoaderContext, pkg.applicationInfo.seInfo,
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800336 false /* downgrade*/, pkg.applicationInfo.targetSdkVersion,
Calin Juravle0610dd12018-07-20 15:51:17 -0700337 profileName, dexMetadataPath,
338 getAugmentedReasonName(compilationReason, dexMetadataPath != null));
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800339
340 if (packageStats != null) {
341 long endTime = System.currentTimeMillis();
342 packageStats.setCompileTime(path, (int)(endTime - startTime));
343 }
344 return DEX_OPT_PERFORMED;
345 } catch (InstallerException e) {
346 Slog.w(TAG, "Failed to dexopt", e);
347 return DEX_OPT_FAILED;
348 }
349 }
350
Calin Juravle0610dd12018-07-20 15:51:17 -0700351 private String getAugmentedReasonName(int compilationReason, boolean useDexMetadata) {
352 String annotation = useDexMetadata
353 ? ArtManagerService.DEXOPT_REASON_WITH_DEX_METADATA_ANNOTATION : "";
354 return getReasonName(compilationReason) + annotation;
355 }
356
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800357 /**
Calin Juravlec22c30e2017-01-16 19:18:48 -0800358 * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}.
359 *
360 * @return
361 * DEX_OPT_FAILED if there was any exception during dexopt
362 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
363 * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file
364 * didn't need an update. That's because at the moment we don't get more than success/failure
365 * from installd.
366 *
367 * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than
368 * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though
369 * that seems wasteful.
370 */
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700371 public int dexOptSecondaryDexPath(ApplicationInfo info, String path,
372 PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
Calin Juravlec6540da2017-11-15 16:28:01 -0800373 if (info.uid == -1) {
374 throw new IllegalArgumentException("Dexopt for path " + path + " has invalid uid.");
375 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800376 synchronized (mInstallLock) {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700377 final long acquireTime = acquireWakeLockLI(info.uid);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800378 try {
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700379 return dexOptSecondaryDexPathLI(info, path, dexUseInfo, options);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800380 } finally {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700381 releaseWakeLockLI(acquireTime);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800382 }
383 }
384 }
385
386 @GuardedBy("mInstallLock")
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700387 private long acquireWakeLockLI(final int uid) {
388 // During boot the system doesn't need to instantiate and obtain a wake lock.
389 // PowerManager might not be ready, but that doesn't mean that we can't proceed with
390 // dexopt.
391 if (!mSystemReady) {
392 return -1;
393 }
394 mDexoptWakeLock.setWorkSource(new WorkSource(uid));
395 mDexoptWakeLock.acquire(WAKELOCK_TIMEOUT_MS);
396 return SystemClock.elapsedRealtime();
397 }
398
399 @GuardedBy("mInstallLock")
400 private void releaseWakeLockLI(final long acquireTime) {
401 if (acquireTime < 0) {
402 return;
403 }
404 try {
405 if (mDexoptWakeLock.isHeld()) {
406 mDexoptWakeLock.release();
407 }
408 final long duration = SystemClock.elapsedRealtime() - acquireTime;
409 if (duration >= WAKELOCK_TIMEOUT_MS) {
410 Slog.wtf(TAG, "WakeLock " + mDexoptWakeLock.getTag()
411 + " time out. Operation took " + duration + " ms. Thread: "
412 + Thread.currentThread().getName());
413 }
414 } catch (Exception e) {
415 Slog.wtf(TAG, "Error while releasing " + mDexoptWakeLock.getTag() + " lock", e);
416 }
417 }
418
419 @GuardedBy("mInstallLock")
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700420 private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path,
421 PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
422 if (options.isDexoptOnlySharedDex() && !dexUseInfo.isUsedByOtherApps()) {
423 // We are asked to optimize only the dex files used by other apps and this is not
424 // on of them: skip it.
425 return DEX_OPT_SKIPPED;
426 }
427
428 String compilerFilter = getRealCompilerFilter(info, options.getCompilerFilter(),
429 dexUseInfo.isUsedByOtherApps());
Calin Juravleefb1c942017-04-04 20:31:44 -0700430 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags.
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100431 // Secondary dex files are currently not compiled at boot.
Andreas Gampec041c332017-11-01 17:05:53 -0700432 int dexoptFlags = getDexFlags(info, compilerFilter, options) | DEXOPT_SECONDARY_DEX;
Calin Juravlec22c30e2017-01-16 19:18:48 -0800433 // Check the app storage and add the appropriate flags.
Calin Juravleadbadd52017-03-28 18:19:15 -0700434 if (info.deviceProtectedDataDir != null &&
435 FileUtils.contains(info.deviceProtectedDataDir, path)) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800436 dexoptFlags |= DEXOPT_STORAGE_DE;
Calin Juravleadbadd52017-03-28 18:19:15 -0700437 } else if (info.credentialProtectedDataDir != null &&
438 FileUtils.contains(info.credentialProtectedDataDir, path)) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800439 dexoptFlags |= DEXOPT_STORAGE_CE;
440 } else {
441 Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName);
442 return DEX_OPT_FAILED;
443 }
Nicolas Geoffray03dba3b2018-12-20 09:47:00 +0000444 String classLoaderContext = null;
David Brazdil22217f62018-12-18 13:43:55 +0000445 if (dexUseInfo.isUnknownClassLoaderContext() || dexUseInfo.isVariableClassLoaderContext()) {
Nicolas Geoffray03dba3b2018-12-20 09:47:00 +0000446 // If we have an unknown (not yet set), or a variable class loader chain. Just extract
447 // the dex file.
448 compilerFilter = "extract";
David Brazdil22217f62018-12-18 13:43:55 +0000449 } else {
450 classLoaderContext = dexUseInfo.getClassLoaderContext();
451 }
452
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800453 int reason = options.getCompilationReason();
Nicolas Geoffray03dba3b2018-12-20 09:47:00 +0000454 Log.d(TAG, "Running dexopt on: " + path
455 + " pkg=" + info.packageName + " isa=" + dexUseInfo.getLoaderIsas()
456 + " reason=" + getReasonName(reason)
457 + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
458 + " target-filter=" + compilerFilter
459 + " class-loader-context=" + classLoaderContext);
460
Calin Juravlec22c30e2017-01-16 19:18:48 -0800461 try {
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700462 for (String isa : dexUseInfo.getLoaderIsas()) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800463 // Reuse the same dexopt path as for the primary apks. We don't need all the
464 // arguments as some (dexopNeeded and oatDir) will be computed by installd because
465 // system server cannot read untrusted app content.
466 // TODO(calin): maybe add a separate call.
467 mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0,
468 /*oatDir*/ null, dexoptFlags,
Nick Kralevich005f9592018-08-14 11:11:02 -0700469 compilerFilter, info.volumeUuid, classLoaderContext, info.seInfo,
Calin Juravlecc651942018-02-01 17:20:51 +0000470 options.isDowngrade(), info.targetSdkVersion, /*profileName*/ null,
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800471 /*dexMetadataPath*/ null, getReasonName(reason));
Calin Juravlec22c30e2017-01-16 19:18:48 -0800472 }
473
474 return DEX_OPT_PERFORMED;
475 } catch (InstallerException e) {
476 Slog.w(TAG, "Failed to dexopt", e);
477 return DEX_OPT_FAILED;
478 }
479 }
480
481 /**
Andreas Gampea8908752015-11-10 08:58:14 -0800482 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
483 * optimize or not (and in what way).
484 */
485 protected int adjustDexoptNeeded(int dexoptNeeded) {
486 return dexoptNeeded;
487 }
488
489 /**
490 * Adjust the given dexopt flags that will be passed to the installer.
491 */
492 protected int adjustDexoptFlags(int dexoptFlags) {
493 return dexoptFlags;
494 }
495
Narayan Kamath88eea9e2016-05-02 14:44:31 +0100496 /**
497 * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}.
498 */
Calin Juravle41a57a62017-08-06 19:20:19 -0700499 void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg,
500 PackageDexUsage.PackageUseInfo useInfo) {
Narayan Kamath88eea9e2016-05-02 14:44:31 +0100501 final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo);
502 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
503
504 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
505
Calin Juravle41a57a62017-08-06 19:20:19 -0700506 for (String path : paths) {
507 pw.println("path: " + path);
508 pw.increaseIndent();
509
510 for (String isa : dexCodeInstructionSets) {
Calin Juravle41a57a62017-08-06 19:20:19 -0700511 try {
Calin Juravle7fc0f632018-03-30 12:38:59 -0700512 DexFile.OptimizationInfo info = DexFile.getDexFileOptimizationInfo(path, isa);
513 pw.println(isa + ": [status=" + info.getStatus()
514 +"] [reason=" + info.getReason() + "]");
Calin Juravle41a57a62017-08-06 19:20:19 -0700515 } catch (IOException ioe) {
Calin Juravleab49fc42018-03-28 19:00:18 -0700516 pw.println(isa + ": [Exception]: " + ioe.getMessage());
Calin Juravle41a57a62017-08-06 19:20:19 -0700517 }
Calin Juravle41a57a62017-08-06 19:20:19 -0700518 }
519
520 if (useInfo.isUsedByOtherApps(path)) {
Calin Juravle94837e32017-09-26 13:23:00 -0700521 pw.println("used by other apps: " + useInfo.getLoadingPackages(path));
Calin Juravle41a57a62017-08-06 19:20:19 -0700522 }
523
524 Map<String, PackageDexUsage.DexUseInfo> dexUseInfoMap = useInfo.getDexUseInfoMap();
525
526 if (!dexUseInfoMap.isEmpty()) {
527 pw.println("known secondary dex files:");
528 pw.increaseIndent();
529 for (Map.Entry<String, PackageDexUsage.DexUseInfo> e : dexUseInfoMap.entrySet()) {
530 String dex = e.getKey();
531 PackageDexUsage.DexUseInfo dexUseInfo = e.getValue();
532 pw.println(dex);
533 pw.increaseIndent();
Calin Juravlecf722222017-09-18 17:40:48 -0700534 // TODO(calin): get the status of the oat file (needs installd call)
Calin Juravle41a57a62017-08-06 19:20:19 -0700535 pw.println("class loader context: " + dexUseInfo.getClassLoaderContext());
536 if (dexUseInfo.isUsedByOtherApps()) {
Calin Juravle94837e32017-09-26 13:23:00 -0700537 pw.println("used by other apps: " + dexUseInfo.getLoadingPackages());
Calin Juravle41a57a62017-08-06 19:20:19 -0700538 }
539 pw.decreaseIndent();
540 }
541 pw.decreaseIndent();
542 }
543 pw.decreaseIndent();
Narayan Kamath88eea9e2016-05-02 14:44:31 +0100544 }
545 }
546
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800547 /**
548 * Returns the compiler filter that should be used to optimize the package code.
549 * The target filter will be updated if the package code is used by other apps
550 * or if it has the safe mode flag set.
551 */
Calin Juravlec22c30e2017-01-16 19:18:48 -0800552 private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter,
553 boolean isUsedByOtherApps) {
554 int flags = info.flags;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800555 boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
Victor Hsiehf12af2f2019-01-03 10:11:03 -0800556 // When an app or priv app is configured to run out of box, only verify it.
Victor Hsiehfa9df0b2019-01-29 12:48:36 -0800557 if (info.isEmbeddedDexUsed()
Victor Hsiehf12af2f2019-01-03 10:11:03 -0800558 || (info.isPrivilegedApp()
559 && DexManager.isPackageSelectedToRunOob(info.packageName))) {
Victor Hsieh785d6182018-04-19 14:26:28 -0700560 return "verify";
Nicolas Geoffray11d21a292018-01-24 14:07:14 +0000561 }
Mathieu Chartier41e4a372016-09-02 16:36:42 -0700562 if (vmSafeMode) {
Nicolas Geoffrayd093b202017-05-03 13:11:58 +0100563 return getSafeModeCompilerFilter(targetCompilerFilter);
Mathieu Chartier41e4a372016-09-02 16:36:42 -0700564 }
565
Calin Juravlec22c30e2017-01-16 19:18:48 -0800566 if (isProfileGuidedCompilerFilter(targetCompilerFilter) && isUsedByOtherApps) {
Calin Juravlef53201f2017-09-15 11:09:29 -0700567 // If the dex files is used by other apps, apply the shared filter.
568 return PackageManagerServiceCompilerMapping.getCompilerFilterForReason(
569 PackageManagerService.REASON_SHARED);
Andreas Gampebdd30d82016-03-20 11:32:11 -0700570 }
571
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800572 return targetCompilerFilter;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800573 }
574
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800575 /**
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800576 * Computes the dex flags that needs to be pass to installd for the given package and compiler
577 * filter.
578 */
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100579 private int getDexFlags(PackageParser.Package pkg, String compilerFilter,
Andreas Gampec041c332017-11-01 17:05:53 -0700580 DexoptOptions options) {
581 return getDexFlags(pkg.applicationInfo, compilerFilter, options);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800582 }
583
Mathieu Chartiera9c34332018-03-12 17:11:07 -0700584 private boolean isAppImageEnabled() {
585 return SystemProperties.get("dalvik.vm.appimageformat", "").length() > 0;
586 }
587
Andreas Gampec041c332017-11-01 17:05:53 -0700588 private int getDexFlags(ApplicationInfo info, String compilerFilter, DexoptOptions options) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800589 int flags = info.flags;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800590 boolean debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
Calin Juravleff3e4a12018-02-01 17:23:13 +0000591 // Profile guide compiled oat files should not be public unles they are based
592 // on profiles from dex metadata archives.
593 // The flag isDexoptInstallWithDexMetadata applies only on installs when we know that
594 // the user does not have an existing profile.
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800595 boolean isProfileGuidedFilter = isProfileGuidedCompilerFilter(compilerFilter);
Patrick Baumannfc2851e2018-11-13 15:23:22 -0800596 boolean isPublic = !isProfileGuidedFilter || options.isDexoptInstallWithDexMetadata();
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800597 int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
David Brazdilf7e31c02018-02-13 17:04:26 +0000598 // Some apps are executed with restrictions on hidden API usage. If this app is one
599 // of them, pass a flag to dexopt to enable the same restrictions during compilation.
Mathew Inwoode3299532018-02-22 13:19:53 +0000600 // TODO we should pass the actual flag value to dexopt, rather than assuming blacklist
David Brazdil06ae4b82018-11-02 18:01:45 +0000601 int hiddenApiFlag = info.getHiddenApiEnforcementPolicy() == HIDDEN_API_ENFORCEMENT_DISABLED
Mathew Inwoode3299532018-02-22 13:19:53 +0000602 ? 0
603 : DEXOPT_ENABLE_HIDDEN_API_CHECKS;
Mathieu Chartiera6d43e42018-03-08 13:11:40 -0800604 // Avoid generating CompactDex for modes that are latency critical.
Mathieu Chartierfa3e9762018-03-06 18:28:22 -0800605 final int compilationReason = options.getCompilationReason();
606 boolean generateCompactDex = true;
607 switch (compilationReason) {
608 case PackageManagerService.REASON_FIRST_BOOT:
609 case PackageManagerService.REASON_BOOT:
610 case PackageManagerService.REASON_INSTALL:
611 generateCompactDex = false;
612 }
Mathieu Chartiera9c34332018-03-12 17:11:07 -0700613 // Use app images only if it is enabled and we are compiling
614 // profile-guided (so the app image doesn't conservatively contain all classes).
615 // If the app didn't request for the splits to be loaded in isolation or if it does not
616 // declare inter-split dependencies, then all the splits will be loaded in the base
617 // apk class loader (in the order of their definition, otherwise disable app images
618 // because they are unsupported for multiple class loaders. b/7269679
619 boolean generateAppImage = isProfileGuidedFilter && (info.splitDependencies == null ||
620 !info.requestsIsolatedSplitLoading()) && isAppImageEnabled();
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800621 int dexFlags =
622 (isPublic ? DEXOPT_PUBLIC : 0)
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800623 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
624 | profileFlag
Andreas Gampec041c332017-11-01 17:05:53 -0700625 | (options.isBootComplete() ? DEXOPT_BOOTCOMPLETE : 0)
David Brazdil464ed3d2018-01-18 15:25:18 +0000626 | (options.isDexoptIdleBackgroundJob() ? DEXOPT_IDLE_BACKGROUND_JOB : 0)
Mathieu Chartierfa3e9762018-03-06 18:28:22 -0800627 | (generateCompactDex ? DEXOPT_GENERATE_COMPACT_DEX : 0)
Mathieu Chartiera9c34332018-03-12 17:11:07 -0700628 | (generateAppImage ? DEXOPT_GENERATE_APP_IMAGE : 0)
David Brazdil464ed3d2018-01-18 15:25:18 +0000629 | hiddenApiFlag;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800630 return adjustDexoptFlags(dexFlags);
631 }
632
633 /**
634 * Assesses if there's a need to perform dexopt on {@code path} for the given
635 * configuration (isa, compiler filter, profile).
636 */
637 private int getDexoptNeeded(String path, String isa, String compilerFilter,
Calin Juravle576e6c02017-09-12 00:58:33 -0700638 String classLoaderContext, boolean newProfile, boolean downgrade) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800639 int dexoptNeeded;
640 try {
Calin Juravle576e6c02017-09-12 00:58:33 -0700641 dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, classLoaderContext,
642 newProfile, downgrade);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800643 } catch (IOException ioe) {
644 Slog.w(TAG, "IOException reading apk: " + path, ioe);
645 return DEX_OPT_FAILED;
646 }
647 return adjustDexoptNeeded(dexoptNeeded);
648 }
649
650 /**
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800651 * Checks if there is an update on the profile information of the {@code pkg}.
652 * If the compiler filter is not profile guided the method returns false.
653 *
654 * Note that this is a "destructive" operation with side effects. Under the hood the
655 * current profile and the reference profile will be merged and subsequent calls
656 * may return a different result.
657 */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800658 private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String profileName,
659 String compilerFilter) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800660 // Check if we are allowed to merge and if the compiler filter is profile guided.
661 if (!isProfileGuidedCompilerFilter(compilerFilter)) {
662 return false;
663 }
664 // Merge profiles. It returns whether or not there was an updated in the profile info.
665 try {
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800666 return mInstaller.mergeProfiles(uid, pkg.packageName, profileName);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800667 } catch (InstallerException e) {
668 Slog.w(TAG, "Failed to merge profiles", e);
669 }
670 return false;
671 }
672
673 /**
674 * Creates oat dir for the specified package if needed and supported.
675 * In certain cases oat directory
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800676 * <strong>cannot</strong> be created:
677 * <ul>
678 * <li>{@code pkg} is a system app, which is not updated.</li>
679 * <li>Package location is not a directory, i.e. monolithic install.</li>
680 * </ul>
681 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700682 * @return Absolute path to the oat directory or null, if oat directory
683 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800684 */
685 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700686 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700687 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800688 return null;
689 }
690 File codePath = new File(pkg.codePath);
691 if (codePath.isDirectory()) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800692 // TODO(calin): why do we create this only if the codePath is a directory? (i.e for
693 // cluster packages). It seems that the logic for the folder creation is
694 // split between installd and here.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800695 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700696 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800697 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700698 } catch (InstallerException e) {
699 Slog.w(TAG, "Failed to create oat dir", e);
700 return null;
701 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700702 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800703 }
704 return null;
705 }
706
707 static File getOatDir(File codePath) {
708 return new File(codePath, OAT_DIR_NAME);
709 }
710
Fyodor Kupolova627c092015-05-05 18:44:39 -0700711 void systemReady() {
712 mSystemReady = true;
713 }
Andreas Gampea8908752015-11-10 08:58:14 -0800714
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800715 private String printDexoptFlags(int flags) {
716 ArrayList<String> flagsList = new ArrayList<>();
717
718 if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) {
719 flagsList.add("boot_complete");
720 }
721 if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) {
722 flagsList.add("debuggable");
723 }
724 if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) {
725 flagsList.add("profile_guided");
726 }
727 if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) {
728 flagsList.add("public");
729 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800730 if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) {
731 flagsList.add("secondary");
732 }
733 if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) {
734 flagsList.add("force");
735 }
736 if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) {
737 flagsList.add("storage_ce");
738 }
739 if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) {
740 flagsList.add("storage_de");
741 }
David Sehr2118ec42017-10-25 14:28:29 -0700742 if ((flags & DEXOPT_IDLE_BACKGROUND_JOB) == DEXOPT_IDLE_BACKGROUND_JOB) {
743 flagsList.add("idle_background_job");
744 }
David Brazdilf7e31c02018-02-13 17:04:26 +0000745 if ((flags & DEXOPT_ENABLE_HIDDEN_API_CHECKS) == DEXOPT_ENABLE_HIDDEN_API_CHECKS) {
746 flagsList.add("enable_hidden_api_checks");
David Brazdil464ed3d2018-01-18 15:25:18 +0000747 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800748
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800749 return String.join(",", flagsList);
750 }
751
Andreas Gampea8908752015-11-10 08:58:14 -0800752 /**
753 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
754 * dexopt path.
755 */
756 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
757
758 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
759 Context context, String wakeLockTag) {
760 super(installer, installLock, context, wakeLockTag);
761 }
762
763 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
764 super(from);
765 }
766
767 @Override
Andreas Gampea8908752015-11-10 08:58:14 -0800768 protected int adjustDexoptNeeded(int dexoptNeeded) {
Nicolas Geoffray96d12a92017-05-03 11:51:53 +0100769 if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) {
770 // Ensure compilation by pretending a compiler filter change on the
771 // apk/odex location (the reason for the '-'. A positive value means
772 // the 'oat' location).
773 return -DexFile.DEX2OAT_FOR_FILTER;
774 }
775 return dexoptNeeded;
Andreas Gampea8908752015-11-10 08:58:14 -0800776 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800777
778 @Override
779 protected int adjustDexoptFlags(int flags) {
780 // Add DEXOPT_FORCE flag to signal installd that it should force compilation
781 // and discard dexoptanalyzer result.
782 return flags | DEXOPT_FORCE;
783 }
Andreas Gampea8908752015-11-10 08:58:14 -0800784 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800785}