blob: 8018c2b7c27dfcbbdba1eb18ffd04f0d12af67a0 [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;
Nicolas Geoffray972b39e2018-11-15 12:59:52 +000023import android.content.pm.SharedLibraryInfo;
Calin Juravle6ae39fc2018-01-19 20:32:47 -080024import android.content.pm.dex.ArtManager;
Calin Juravlecc651942018-02-01 17:20:51 +000025import android.content.pm.dex.DexMetadataHelper;
Calin Juravleadbadd52017-03-28 18:19:15 -070026import android.os.FileUtils;
Fyodor Kupolova627c092015-05-05 18:44:39 -070027import android.os.PowerManager;
Nicolas Geoffray653356f2019-02-26 23:24:34 +000028import android.os.Process;
Fyodor Kupolov28f28552017-05-02 12:11:02 -070029import android.os.SystemClock;
Nicolas Geoffray20a894e2017-09-08 13:01:40 +010030import android.os.SystemProperties;
Fyodor Kupolov74876572015-02-23 17:14:45 -080031import android.os.UserHandle;
Fyodor Kupolova627c092015-05-05 18:44:39 -070032import android.os.WorkSource;
Nicolas Geoffray653356f2019-02-26 23:24:34 +000033import android.os.storage.StorageManager;
Fyodor Kupolov74876572015-02-23 17:14:45 -080034import android.util.Log;
35import android.util.Slog;
36
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080037import com.android.internal.annotations.GuardedBy;
Narayan Kamath88eea9e2016-05-02 14:44:31 +010038import com.android.internal.util.IndentingPrintWriter;
Jeff Sharkey740f5232016-12-09 14:31:26 -070039import com.android.server.pm.Installer.InstallerException;
Calin Juravle0610dd12018-07-20 15:51:17 -070040import com.android.server.pm.dex.ArtManagerService;
Victor Hsieh785d6182018-04-19 14:26:28 -070041import com.android.server.pm.dex.DexManager;
Calin Juravle1d0e83d2017-07-17 15:12:01 -070042import com.android.server.pm.dex.DexoptOptions;
Calin Juravle19da1cf2017-07-12 18:52:49 -070043import com.android.server.pm.dex.DexoptUtils;
Calin Juravlef1ff36f2017-07-22 12:33:41 -070044import com.android.server.pm.dex.PackageDexUsage;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070045
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080046import java.io.File;
Fyodor Kupolov74876572015-02-23 17:14:45 -080047import java.io.IOException;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080048import java.util.ArrayList;
Calin Juravle4c2b9552017-08-10 17:23:00 -070049import java.util.Arrays;
Fyodor Kupolov74876572015-02-23 17:14:45 -080050import java.util.List;
Calin Juravle41a57a62017-08-06 19:20:19 -070051import java.util.Map;
Fyodor Kupolov74876572015-02-23 17:14:45 -080052
53import dalvik.system.DexFile;
Fyodor Kupolov74876572015-02-23 17:14:45 -080054
David Brazdildf1c7dc2018-10-26 16:23:59 +010055import static android.content.pm.ApplicationInfo.HIDDEN_API_ENFORCEMENT_DISABLED;
Mathew Inwoode3299532018-02-22 13:19:53 +000056
Todd Kennedyfa54ab72015-09-25 07:46:12 -070057import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
58import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
Andreas Gampebdd30d82016-03-20 11:32:11 -070059import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED;
Todd Kennedyfa54ab72015-09-25 07:46:12 -070060import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
Calin Juravlec22c30e2017-01-16 19:18:48 -080061import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX;
62import static com.android.server.pm.Installer.DEXOPT_FORCE;
63import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE;
64import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE;
David Sehr2118ec42017-10-25 14:28:29 -070065import static com.android.server.pm.Installer.DEXOPT_IDLE_BACKGROUND_JOB;
David Brazdilf7e31c02018-02-13 17:04:26 +000066import static com.android.server.pm.Installer.DEXOPT_ENABLE_HIDDEN_API_CHECKS;
Mathieu Chartierfa3e9762018-03-06 18:28:22 -080067import static com.android.server.pm.Installer.DEXOPT_GENERATE_COMPACT_DEX;
Mathieu Chartiera9c34332018-03-12 17:11:07 -070068import static com.android.server.pm.Installer.DEXOPT_GENERATE_APP_IMAGE;
Fyodor Kupolov74876572015-02-23 17:14:45 -080069import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
70import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080071
Fyodor Kupolov28f28552017-05-02 12:11:02 -070072import static com.android.server.pm.PackageManagerService.WATCHDOG_TIMEOUT;
Nicolas Geoffrayb0818e82017-05-05 10:23:19 +010073
Calin Juravle4bc8f4d2018-02-12 12:00:44 -080074import static com.android.server.pm.PackageManagerServiceCompilerMapping.getReasonName;
75
Nicolas Geoffrayd093b202017-05-03 13:11:58 +010076import static dalvik.system.DexFile.getSafeModeCompilerFilter;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -080077import static dalvik.system.DexFile.isProfileGuidedCompilerFilter;
Fyodor Kupolov74876572015-02-23 17:14:45 -080078
79/**
80 * Helper class for running dexopt command on packages.
81 */
Calin Juravlec22c30e2017-01-16 19:18:48 -080082public class PackageDexOptimizer {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080083 private static final String TAG = "PackageManager.DexOptimizer";
84 static final String OAT_DIR_NAME = "oat";
85 // TODO b/19550105 Remove error codes and use exceptions
Calin Juravlec22c30e2017-01-16 19:18:48 -080086 public static final int DEX_OPT_SKIPPED = 0;
87 public static final int DEX_OPT_PERFORMED = 1;
88 public static final int DEX_OPT_FAILED = -1;
Fyodor Kupolov28f28552017-05-02 12:11:02 -070089 // One minute over PM WATCHDOG_TIMEOUT
90 private static final long WAKELOCK_TIMEOUT_MS = WATCHDOG_TIMEOUT + 1000 * 60;
Fyodor Kupolov74876572015-02-23 17:14:45 -080091
Fyodor Kupolov28f28552017-05-02 12:11:02 -070092 @GuardedBy("mInstallLock")
Andreas Gampea8908752015-11-10 08:58:14 -080093 private final Installer mInstaller;
94 private final Object mInstallLock;
Fyodor Kupolov74876572015-02-23 17:14:45 -080095
Fyodor Kupolov28f28552017-05-02 12:11:02 -070096 @GuardedBy("mInstallLock")
Fyodor Kupolova627c092015-05-05 18:44:39 -070097 private final PowerManager.WakeLock mDexoptWakeLock;
98 private volatile boolean mSystemReady;
99
Andreas Gampea8908752015-11-10 08:58:14 -0800100 PackageDexOptimizer(Installer installer, Object installLock, Context context,
101 String wakeLockTag) {
102 this.mInstaller = installer;
103 this.mInstallLock = installLock;
104
105 PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
106 mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
107 }
108
109 protected PackageDexOptimizer(PackageDexOptimizer from) {
110 this.mInstaller = from.mInstaller;
111 this.mInstallLock = from.mInstallLock;
112 this.mDexoptWakeLock = from.mDexoptWakeLock;
113 this.mSystemReady = from.mSystemReady;
Fyodor Kupolov74876572015-02-23 17:14:45 -0800114 }
115
Calin Juravledb4a79a2015-12-23 18:55:08 +0200116 static boolean canOptimizePackage(PackageParser.Package pkg) {
Nicolas Geoffray20a894e2017-09-08 13:01:40 +0100117 // We do not dexopt a package with no code.
118 if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) {
119 return false;
120 }
121
Nicolas Geoffray20a894e2017-09-08 13:01:40 +0100122 return true;
Calin Juravledb4a79a2015-12-23 18:55:08 +0200123 }
124
Fyodor Kupolov74876572015-02-23 17:14:45 -0800125 /**
126 * Performs dexopt on all code paths and libraries of the specified package for specified
127 * instruction sets.
128 *
Andreas Gampea8908752015-11-10 08:58:14 -0800129 * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
130 * synchronized on {@link #mInstallLock}.
Fyodor Kupolov74876572015-02-23 17:14:45 -0800131 */
Nicolas Geoffraya7f44ba2019-02-26 21:40:59 +0000132 int performDexOpt(PackageParser.Package pkg,
Calin Juravle1d0e83d2017-07-17 15:12:01 -0700133 String[] instructionSets, CompilerStats.PackageStats packageStats,
Calin Juravle3b74c412017-08-03 19:48:37 -0700134 PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) {
Calin Juravlec6540da2017-11-15 16:28:01 -0800135 if (pkg.applicationInfo.uid == -1) {
136 throw new IllegalArgumentException("Dexopt for " + pkg.packageName
137 + " has invalid uid.");
138 }
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800139 if (!canOptimizePackage(pkg)) {
140 return DEX_OPT_SKIPPED;
141 }
Andreas Gampea8908752015-11-10 08:58:14 -0800142 synchronized (mInstallLock) {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700143 final long acquireTime = acquireWakeLockLI(pkg.applicationInfo.uid);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700144 try {
Nicolas Geoffraya7f44ba2019-02-26 21:40:59 +0000145 return performDexOptLI(pkg, instructionSets,
Calin Juravle3b74c412017-08-03 19:48:37 -0700146 packageStats, packageUseInfo, options);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700147 } finally {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700148 releaseWakeLockLI(acquireTime);
Fyodor Kupolova627c092015-05-05 18:44:39 -0700149 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800150 }
151 }
152
Nicolas Geoffray653356f2019-02-26 23:24:34 +0000153 int performDexOpt(SharedLibraryInfo info, String[] instructionSets, DexoptOptions options) {
154 String classLoaderContext = DexoptUtils.getClassLoaderContext(info);
155 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
156 String compilerFilter = PackageManagerServiceCompilerMapping.getCompilerFilterForReason(
157 PackageManagerService.REASON_SHARED);
158 int result = DEX_OPT_SKIPPED;
159 for (String instructionSet : dexCodeInstructionSets) {
160 int dexoptNeeded = getDexoptNeeded(
161 info.getPath(), instructionSet, compilerFilter,
162 classLoaderContext, false /* newProfile */,
163 false /* downgrade */);
164 if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) {
165 continue;
166 }
167 // Special string recognized by installd.
168 final String packageName = "*";
169 final String outputPath = null;
170 int dexFlags = DEXOPT_PUBLIC
171 | (options.isBootComplete() ? DEXOPT_BOOTCOMPLETE : 0)
172 | (options.isDexoptIdleBackgroundJob() ? DEXOPT_IDLE_BACKGROUND_JOB : 0);
173 dexFlags = adjustDexoptFlags(dexFlags);
174 final String uuid = StorageManager.UUID_SYSTEM;
175 final String seInfo = null;
176 final int targetSdkVersion = 0; // Builtin libraries targets the system's SDK version
177 try {
178 mInstaller.dexopt(info.getPath(), Process.SYSTEM_UID, packageName,
179 instructionSet, dexoptNeeded, outputPath, dexFlags, compilerFilter,
180 uuid, classLoaderContext, seInfo, false /* downgrade */,
181 targetSdkVersion, /*profileName*/ null, /*dexMetadataPath*/ null,
182 getReasonName(options.getCompilationReason()));
183 // The end result is:
184 // - FAILED if any path failed,
185 // - PERFORMED if at least one path needed compilation,
186 // - SKIPPED when all paths are up to date
187 if (result != DEX_OPT_FAILED) {
188 result = DEX_OPT_PERFORMED;
189 }
190 } catch (InstallerException e) {
191 Slog.w(TAG, "Failed to dexopt", e);
192 result = DEX_OPT_FAILED;
193 }
194 }
195 return result;
196 }
197
Andreas Gampea8908752015-11-10 08:58:14 -0800198 /**
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800199 * Performs dexopt on all code paths of the given package.
200 * It assumes the install lock is held.
201 */
202 @GuardedBy("mInstallLock")
Nicolas Geoffray972b39e2018-11-15 12:59:52 +0000203 private int performDexOptLI(PackageParser.Package pkg,
Calin Juravle1d0e83d2017-07-17 15:12:01 -0700204 String[] targetInstructionSets, CompilerStats.PackageStats packageStats,
Calin Juravle3b74c412017-08-03 19:48:37 -0700205 PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) {
Nicolas Geoffraya7f44ba2019-02-26 21:40:59 +0000206 final List<SharedLibraryInfo> sharedLibraries = pkg.usesLibraryInfos;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800207 final String[] instructionSets = targetInstructionSets != null ?
208 targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
209 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
Jeff Hao5def5e52017-04-19 14:42:16 -0700210 final List<String> paths = pkg.getAllCodePaths();
Jeff Sharkeya73e1652017-11-15 19:07:14 -0700211
212 int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
213 if (sharedGid == -1) {
214 Slog.wtf(TAG, "Well this is awkward; package " + pkg.applicationInfo.name + " had UID "
215 + pkg.applicationInfo.uid, new Throwable());
216 sharedGid = android.os.Process.NOBODY_UID;
217 }
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800218
Calin Juravle19da1cf2017-07-12 18:52:49 -0700219 // Get the class loader context dependencies.
220 // For each code path in the package, this array contains the class loader context that
221 // needs to be passed to dexopt in order to ensure correct optimizations.
Calin Juravleda098152017-09-01 17:30:01 -0700222 boolean[] pathsWithCode = new boolean[paths.size()];
223 pathsWithCode[0] = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
224 for (int i = 1; i < paths.size(); i++) {
225 pathsWithCode[i] = (pkg.splitFlags[i - 1] & ApplicationInfo.FLAG_HAS_CODE) != 0;
226 }
Calin Juravle19da1cf2017-07-12 18:52:49 -0700227 String[] classLoaderContexts = DexoptUtils.getClassLoaderContexts(
Calin Juravleda098152017-09-01 17:30:01 -0700228 pkg.applicationInfo, sharedLibraries, pathsWithCode);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800229
Calin Juravle4c2b9552017-08-10 17:23:00 -0700230 // Sanity check that we do not call dexopt with inconsistent data.
231 if (paths.size() != classLoaderContexts.length) {
232 String[] splitCodePaths = pkg.applicationInfo.getSplitCodePaths();
233 throw new IllegalStateException("Inconsistent information "
234 + "between PackageParser.Package and its ApplicationInfo. "
235 + "pkg.getAllCodePaths=" + paths
236 + " pkg.applicationInfo.getBaseCodePath=" + pkg.applicationInfo.getBaseCodePath()
237 + " pkg.applicationInfo.getSplitCodePaths="
238 + (splitCodePaths == null ? "null" : Arrays.toString(splitCodePaths)));
239 }
240
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800241 int result = DEX_OPT_SKIPPED;
Jeff Hao5def5e52017-04-19 14:42:16 -0700242 for (int i = 0; i < paths.size(); i++) {
243 // Skip paths that have no code.
Calin Juravleda098152017-09-01 17:30:01 -0700244 if (!pathsWithCode[i]) {
Jeff Hao5def5e52017-04-19 14:42:16 -0700245 continue;
246 }
Calin Juravleda098152017-09-01 17:30:01 -0700247 if (classLoaderContexts[i] == null) {
248 throw new IllegalStateException("Inconsistent information in the "
249 + "package structure. A split is marked to contain code "
250 + "but has no dependency listed. Index=" + i + " path=" + paths.get(i));
251 }
252
Jeff Hao5def5e52017-04-19 14:42:16 -0700253 // Append shared libraries with split dependencies for this split.
254 String path = paths.get(i);
Calin Juravleb6f844d2017-07-17 15:23:21 -0700255 if (options.getSplitName() != null) {
256 // We are asked to compile only a specific split. Check that the current path is
257 // what we are looking for.
258 if (!options.getSplitName().equals(new File(path).getName())) {
259 continue;
260 }
261 }
262
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800263 String profileName = ArtManager.getProfileName(i == 0 ? null : pkg.splitNames[i - 1]);
264
Calin Juravlecc651942018-02-01 17:20:51 +0000265 String dexMetadataPath = null;
266 if (options.isDexoptInstallWithDexMetadata()) {
267 File dexMetadataFile = DexMetadataHelper.findDexMetadataForFile(new File(path));
268 dexMetadataPath = dexMetadataFile == null
269 ? null : dexMetadataFile.getAbsolutePath();
270 }
271
Calin Juravle3b74c412017-08-03 19:48:37 -0700272 final boolean isUsedByOtherApps = options.isDexoptAsSharedLibrary()
Calin Juravle52a452c2017-08-04 01:42:17 -0700273 || packageUseInfo.isUsedByOtherApps(path);
Calin Juravle3b74c412017-08-03 19:48:37 -0700274 final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo,
275 options.getCompilerFilter(), isUsedByOtherApps);
276 final boolean profileUpdated = options.isCheckForProfileUpdates() &&
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800277 isProfileUpdated(pkg, sharedGid, profileName, compilerFilter);
Calin Juravle3b74c412017-08-03 19:48:37 -0700278
279 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct
280 // flags.
Andreas Gampec041c332017-11-01 17:05:53 -0700281 final int dexoptFlags = getDexFlags(pkg, compilerFilter, options);
Calin Juravle3b74c412017-08-03 19:48:37 -0700282
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800283 for (String dexCodeIsa : dexCodeInstructionSets) {
Calin Juravle19da1cf2017-07-12 18:52:49 -0700284 int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter,
285 profileUpdated, classLoaderContexts[i], dexoptFlags, sharedGid,
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800286 packageStats, options.isDowngrade(), profileName, dexMetadataPath,
287 options.getCompilationReason());
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800288 // The end result is:
289 // - FAILED if any path failed,
290 // - PERFORMED if at least one path needed compilation,
291 // - SKIPPED when all paths are up to date
292 if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) {
293 result = newResult;
294 }
295 }
296 }
297 return result;
298 }
299
300 /**
301 * Performs dexopt on the {@code path} belonging to the package {@code pkg}.
302 *
303 * @return
304 * DEX_OPT_FAILED if there was any exception during dexopt
305 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
306 * DEX_OPT_SKIPPED if the path does not need to be deopt-ed.
307 */
308 @GuardedBy("mInstallLock")
309 private int dexOptPath(PackageParser.Package pkg, String path, String isa,
Calin Juravle576e6c02017-09-12 00:58:33 -0700310 String compilerFilter, boolean profileUpdated, String classLoaderContext,
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800311 int dexoptFlags, int uid, CompilerStats.PackageStats packageStats, boolean downgrade,
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800312 String profileName, String dexMetadataPath, int compilationReason) {
Calin Juravle576e6c02017-09-12 00:58:33 -0700313 int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, classLoaderContext,
314 profileUpdated, downgrade);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800315 if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) {
316 return DEX_OPT_SKIPPED;
317 }
318
319 // TODO(calin): there's no need to try to create the oat dir over and over again,
320 // especially since it involve an extra installd call. We should create
321 // if (if supported) on the fly during the dexopt call.
322 String oatDir = createOatDirIfSupported(pkg, isa);
323
324 Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path
325 + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa
326 + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
Calin Juravle576e6c02017-09-12 00:58:33 -0700327 + " targetFilter=" + compilerFilter + " oatDir=" + oatDir
328 + " classLoaderContext=" + classLoaderContext);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800329
330 try {
331 long startTime = System.currentTimeMillis();
332
Shubham Ajmera246dccf2017-05-24 17:46:36 -0700333 // TODO: Consider adding 2 different APIs for primary and secondary dexopt.
334 // installd only uses downgrade flag for secondary dex files and ignores it for
335 // primary dex files.
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800336 mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags,
Calin Juravle576e6c02017-09-12 00:58:33 -0700337 compilerFilter, pkg.volumeUuid, classLoaderContext, pkg.applicationInfo.seInfo,
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800338 false /* downgrade*/, pkg.applicationInfo.targetSdkVersion,
Calin Juravle0610dd12018-07-20 15:51:17 -0700339 profileName, dexMetadataPath,
340 getAugmentedReasonName(compilationReason, dexMetadataPath != null));
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800341
342 if (packageStats != null) {
343 long endTime = System.currentTimeMillis();
344 packageStats.setCompileTime(path, (int)(endTime - startTime));
345 }
346 return DEX_OPT_PERFORMED;
347 } catch (InstallerException e) {
348 Slog.w(TAG, "Failed to dexopt", e);
349 return DEX_OPT_FAILED;
350 }
351 }
352
Calin Juravle0610dd12018-07-20 15:51:17 -0700353 private String getAugmentedReasonName(int compilationReason, boolean useDexMetadata) {
354 String annotation = useDexMetadata
355 ? ArtManagerService.DEXOPT_REASON_WITH_DEX_METADATA_ANNOTATION : "";
356 return getReasonName(compilationReason) + annotation;
357 }
358
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800359 /**
Calin Juravlec22c30e2017-01-16 19:18:48 -0800360 * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}.
361 *
362 * @return
363 * DEX_OPT_FAILED if there was any exception during dexopt
364 * DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
365 * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file
366 * didn't need an update. That's because at the moment we don't get more than success/failure
367 * from installd.
368 *
369 * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than
370 * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though
371 * that seems wasteful.
372 */
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700373 public int dexOptSecondaryDexPath(ApplicationInfo info, String path,
374 PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
Calin Juravlec6540da2017-11-15 16:28:01 -0800375 if (info.uid == -1) {
376 throw new IllegalArgumentException("Dexopt for path " + path + " has invalid uid.");
377 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800378 synchronized (mInstallLock) {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700379 final long acquireTime = acquireWakeLockLI(info.uid);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800380 try {
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700381 return dexOptSecondaryDexPathLI(info, path, dexUseInfo, options);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800382 } finally {
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700383 releaseWakeLockLI(acquireTime);
Calin Juravlec22c30e2017-01-16 19:18:48 -0800384 }
385 }
386 }
387
388 @GuardedBy("mInstallLock")
Fyodor Kupolov28f28552017-05-02 12:11:02 -0700389 private long acquireWakeLockLI(final int uid) {
390 // During boot the system doesn't need to instantiate and obtain a wake lock.
391 // PowerManager might not be ready, but that doesn't mean that we can't proceed with
392 // dexopt.
393 if (!mSystemReady) {
394 return -1;
395 }
396 mDexoptWakeLock.setWorkSource(new WorkSource(uid));
397 mDexoptWakeLock.acquire(WAKELOCK_TIMEOUT_MS);
398 return SystemClock.elapsedRealtime();
399 }
400
401 @GuardedBy("mInstallLock")
402 private void releaseWakeLockLI(final long acquireTime) {
403 if (acquireTime < 0) {
404 return;
405 }
406 try {
407 if (mDexoptWakeLock.isHeld()) {
408 mDexoptWakeLock.release();
409 }
410 final long duration = SystemClock.elapsedRealtime() - acquireTime;
411 if (duration >= WAKELOCK_TIMEOUT_MS) {
412 Slog.wtf(TAG, "WakeLock " + mDexoptWakeLock.getTag()
413 + " time out. Operation took " + duration + " ms. Thread: "
414 + Thread.currentThread().getName());
415 }
416 } catch (Exception e) {
417 Slog.wtf(TAG, "Error while releasing " + mDexoptWakeLock.getTag() + " lock", e);
418 }
419 }
420
421 @GuardedBy("mInstallLock")
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700422 private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path,
423 PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
424 if (options.isDexoptOnlySharedDex() && !dexUseInfo.isUsedByOtherApps()) {
425 // We are asked to optimize only the dex files used by other apps and this is not
426 // on of them: skip it.
427 return DEX_OPT_SKIPPED;
428 }
429
430 String compilerFilter = getRealCompilerFilter(info, options.getCompilerFilter(),
431 dexUseInfo.isUsedByOtherApps());
Calin Juravleefb1c942017-04-04 20:31:44 -0700432 // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags.
Nicolas Geoffray3b4359a2017-05-25 13:53:22 +0100433 // Secondary dex files are currently not compiled at boot.
Andreas Gampec041c332017-11-01 17:05:53 -0700434 int dexoptFlags = getDexFlags(info, compilerFilter, options) | DEXOPT_SECONDARY_DEX;
Calin Juravlec22c30e2017-01-16 19:18:48 -0800435 // Check the app storage and add the appropriate flags.
Calin Juravleadbadd52017-03-28 18:19:15 -0700436 if (info.deviceProtectedDataDir != null &&
437 FileUtils.contains(info.deviceProtectedDataDir, path)) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800438 dexoptFlags |= DEXOPT_STORAGE_DE;
Calin Juravleadbadd52017-03-28 18:19:15 -0700439 } else if (info.credentialProtectedDataDir != null &&
440 FileUtils.contains(info.credentialProtectedDataDir, path)) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800441 dexoptFlags |= DEXOPT_STORAGE_CE;
442 } else {
443 Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName);
444 return DEX_OPT_FAILED;
445 }
Nicolas Geoffraybab4c2c2018-12-20 09:47:00 +0000446 String classLoaderContext = null;
David Brazdil22217f62018-12-18 13:43:55 +0000447 if (dexUseInfo.isUnknownClassLoaderContext() || dexUseInfo.isVariableClassLoaderContext()) {
Nicolas Geoffraybab4c2c2018-12-20 09:47:00 +0000448 // If we have an unknown (not yet set), or a variable class loader chain. Just extract
449 // the dex file.
450 compilerFilter = "extract";
David Brazdil22217f62018-12-18 13:43:55 +0000451 } else {
452 classLoaderContext = dexUseInfo.getClassLoaderContext();
453 }
454
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800455 int reason = options.getCompilationReason();
Nicolas Geoffraybab4c2c2018-12-20 09:47:00 +0000456 Log.d(TAG, "Running dexopt on: " + path
457 + " pkg=" + info.packageName + " isa=" + dexUseInfo.getLoaderIsas()
458 + " reason=" + getReasonName(reason)
459 + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
460 + " target-filter=" + compilerFilter
461 + " class-loader-context=" + classLoaderContext);
462
Calin Juravlec22c30e2017-01-16 19:18:48 -0800463 try {
Calin Juravlef1ff36f2017-07-22 12:33:41 -0700464 for (String isa : dexUseInfo.getLoaderIsas()) {
Calin Juravlec22c30e2017-01-16 19:18:48 -0800465 // Reuse the same dexopt path as for the primary apks. We don't need all the
466 // arguments as some (dexopNeeded and oatDir) will be computed by installd because
467 // system server cannot read untrusted app content.
468 // TODO(calin): maybe add a separate call.
469 mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0,
470 /*oatDir*/ null, dexoptFlags,
Nick Kralevich005f9592018-08-14 11:11:02 -0700471 compilerFilter, info.volumeUuid, classLoaderContext, info.seInfo,
Calin Juravlecc651942018-02-01 17:20:51 +0000472 options.isDowngrade(), info.targetSdkVersion, /*profileName*/ null,
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800473 /*dexMetadataPath*/ null, getReasonName(reason));
Calin Juravlec22c30e2017-01-16 19:18:48 -0800474 }
475
476 return DEX_OPT_PERFORMED;
477 } catch (InstallerException e) {
478 Slog.w(TAG, "Failed to dexopt", e);
479 return DEX_OPT_FAILED;
480 }
481 }
482
483 /**
Andreas Gampea8908752015-11-10 08:58:14 -0800484 * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
485 * optimize or not (and in what way).
486 */
487 protected int adjustDexoptNeeded(int dexoptNeeded) {
488 return dexoptNeeded;
489 }
490
491 /**
492 * Adjust the given dexopt flags that will be passed to the installer.
493 */
494 protected int adjustDexoptFlags(int dexoptFlags) {
495 return dexoptFlags;
496 }
497
Narayan Kamath88eea9e2016-05-02 14:44:31 +0100498 /**
499 * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}.
500 */
Calin Juravle41a57a62017-08-06 19:20:19 -0700501 void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg,
502 PackageDexUsage.PackageUseInfo useInfo) {
Narayan Kamath88eea9e2016-05-02 14:44:31 +0100503 final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo);
504 final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
505
506 final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
507
Calin Juravle41a57a62017-08-06 19:20:19 -0700508 for (String path : paths) {
509 pw.println("path: " + path);
510 pw.increaseIndent();
511
512 for (String isa : dexCodeInstructionSets) {
Calin Juravle41a57a62017-08-06 19:20:19 -0700513 try {
Calin Juravle7fc0f632018-03-30 12:38:59 -0700514 DexFile.OptimizationInfo info = DexFile.getDexFileOptimizationInfo(path, isa);
515 pw.println(isa + ": [status=" + info.getStatus()
516 +"] [reason=" + info.getReason() + "]");
Calin Juravle41a57a62017-08-06 19:20:19 -0700517 } catch (IOException ioe) {
Calin Juravleab49fc42018-03-28 19:00:18 -0700518 pw.println(isa + ": [Exception]: " + ioe.getMessage());
Calin Juravle41a57a62017-08-06 19:20:19 -0700519 }
Calin Juravle41a57a62017-08-06 19:20:19 -0700520 }
521
522 if (useInfo.isUsedByOtherApps(path)) {
Calin Juravle94837e32017-09-26 13:23:00 -0700523 pw.println("used by other apps: " + useInfo.getLoadingPackages(path));
Calin Juravle41a57a62017-08-06 19:20:19 -0700524 }
525
526 Map<String, PackageDexUsage.DexUseInfo> dexUseInfoMap = useInfo.getDexUseInfoMap();
527
528 if (!dexUseInfoMap.isEmpty()) {
529 pw.println("known secondary dex files:");
530 pw.increaseIndent();
531 for (Map.Entry<String, PackageDexUsage.DexUseInfo> e : dexUseInfoMap.entrySet()) {
532 String dex = e.getKey();
533 PackageDexUsage.DexUseInfo dexUseInfo = e.getValue();
534 pw.println(dex);
535 pw.increaseIndent();
Calin Juravlecf722222017-09-18 17:40:48 -0700536 // TODO(calin): get the status of the oat file (needs installd call)
Calin Juravle41a57a62017-08-06 19:20:19 -0700537 pw.println("class loader context: " + dexUseInfo.getClassLoaderContext());
538 if (dexUseInfo.isUsedByOtherApps()) {
Calin Juravle94837e32017-09-26 13:23:00 -0700539 pw.println("used by other apps: " + dexUseInfo.getLoadingPackages());
Calin Juravle41a57a62017-08-06 19:20:19 -0700540 }
541 pw.decreaseIndent();
542 }
543 pw.decreaseIndent();
544 }
545 pw.decreaseIndent();
Narayan Kamath88eea9e2016-05-02 14:44:31 +0100546 }
547 }
548
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800549 /**
550 * Returns the compiler filter that should be used to optimize the package code.
551 * The target filter will be updated if the package code is used by other apps
552 * or if it has the safe mode flag set.
553 */
Calin Juravlec22c30e2017-01-16 19:18:48 -0800554 private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter,
555 boolean isUsedByOtherApps) {
556 int flags = info.flags;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800557 boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
Victor Hsieh785d6182018-04-19 14:26:28 -0700558 // When a priv app is configured to run out of box, only verify it.
559 if (info.isPrivilegedApp() && DexManager.isPackageSelectedToRunOob(info.packageName)) {
560 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);
Calin Juravleff3e4a12018-02-01 17:23:13 +0000596 boolean isPublic = !info.isForwardLocked() &&
597 (!isProfileGuidedFilter || options.isDexoptInstallWithDexMetadata());
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800598 int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
David Brazdilf7e31c02018-02-13 17:04:26 +0000599 // Some apps are executed with restrictions on hidden API usage. If this app is one
600 // of them, pass a flag to dexopt to enable the same restrictions during compilation.
Mathew Inwoode3299532018-02-22 13:19:53 +0000601 // TODO we should pass the actual flag value to dexopt, rather than assuming blacklist
David Brazdildf1c7dc2018-10-26 16:23:59 +0100602 int hiddenApiFlag = info.getHiddenApiEnforcementPolicy() == HIDDEN_API_ENFORCEMENT_DISABLED
Mathew Inwoode3299532018-02-22 13:19:53 +0000603 ? 0
604 : DEXOPT_ENABLE_HIDDEN_API_CHECKS;
Mathieu Chartiera6d43e42018-03-08 13:11:40 -0800605 // Avoid generating CompactDex for modes that are latency critical.
Mathieu Chartierfa3e9762018-03-06 18:28:22 -0800606 final int compilationReason = options.getCompilationReason();
607 boolean generateCompactDex = true;
608 switch (compilationReason) {
609 case PackageManagerService.REASON_FIRST_BOOT:
610 case PackageManagerService.REASON_BOOT:
611 case PackageManagerService.REASON_INSTALL:
612 generateCompactDex = false;
613 }
Mathieu Chartiera9c34332018-03-12 17:11:07 -0700614 // Use app images only if it is enabled and we are compiling
615 // profile-guided (so the app image doesn't conservatively contain all classes).
616 // If the app didn't request for the splits to be loaded in isolation or if it does not
617 // declare inter-split dependencies, then all the splits will be loaded in the base
618 // apk class loader (in the order of their definition, otherwise disable app images
619 // because they are unsupported for multiple class loaders. b/7269679
620 boolean generateAppImage = isProfileGuidedFilter && (info.splitDependencies == null ||
621 !info.requestsIsolatedSplitLoading()) && isAppImageEnabled();
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800622 int dexFlags =
623 (isPublic ? DEXOPT_PUBLIC : 0)
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800624 | (debuggable ? DEXOPT_DEBUGGABLE : 0)
625 | profileFlag
Andreas Gampec041c332017-11-01 17:05:53 -0700626 | (options.isBootComplete() ? DEXOPT_BOOTCOMPLETE : 0)
David Brazdil464ed3d2018-01-18 15:25:18 +0000627 | (options.isDexoptIdleBackgroundJob() ? DEXOPT_IDLE_BACKGROUND_JOB : 0)
Mathieu Chartierfa3e9762018-03-06 18:28:22 -0800628 | (generateCompactDex ? DEXOPT_GENERATE_COMPACT_DEX : 0)
Mathieu Chartiera9c34332018-03-12 17:11:07 -0700629 | (generateAppImage ? DEXOPT_GENERATE_APP_IMAGE : 0)
David Brazdil464ed3d2018-01-18 15:25:18 +0000630 | hiddenApiFlag;
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800631 return adjustDexoptFlags(dexFlags);
632 }
633
634 /**
635 * Assesses if there's a need to perform dexopt on {@code path} for the given
636 * configuration (isa, compiler filter, profile).
637 */
638 private int getDexoptNeeded(String path, String isa, String compilerFilter,
Calin Juravle576e6c02017-09-12 00:58:33 -0700639 String classLoaderContext, boolean newProfile, boolean downgrade) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800640 int dexoptNeeded;
641 try {
Calin Juravle576e6c02017-09-12 00:58:33 -0700642 dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, classLoaderContext,
643 newProfile, downgrade);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800644 } catch (IOException ioe) {
645 Slog.w(TAG, "IOException reading apk: " + path, ioe);
646 return DEX_OPT_FAILED;
647 }
648 return adjustDexoptNeeded(dexoptNeeded);
649 }
650
651 /**
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800652 * Checks if there is an update on the profile information of the {@code pkg}.
653 * If the compiler filter is not profile guided the method returns false.
654 *
655 * Note that this is a "destructive" operation with side effects. Under the hood the
656 * current profile and the reference profile will be merged and subsequent calls
657 * may return a different result.
658 */
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800659 private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String profileName,
660 String compilerFilter) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800661 // Check if we are allowed to merge and if the compiler filter is profile guided.
662 if (!isProfileGuidedCompilerFilter(compilerFilter)) {
663 return false;
664 }
665 // Merge profiles. It returns whether or not there was an updated in the profile info.
666 try {
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800667 return mInstaller.mergeProfiles(uid, pkg.packageName, profileName);
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800668 } catch (InstallerException e) {
669 Slog.w(TAG, "Failed to merge profiles", e);
670 }
671 return false;
672 }
673
674 /**
675 * Creates oat dir for the specified package if needed and supported.
676 * In certain cases oat directory
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800677 * <strong>cannot</strong> be created:
678 * <ul>
679 * <li>{@code pkg} is a system app, which is not updated.</li>
680 * <li>Package location is not a directory, i.e. monolithic install.</li>
681 * </ul>
682 *
Richard Uhler7b08b352015-03-25 16:25:57 -0700683 * @return Absolute path to the oat directory or null, if oat directory
684 * cannot be created.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800685 */
686 @Nullable
Todd Kennedy27c24fb2015-09-17 16:49:25 -0700687 private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
Fyodor Kupolovebcac162015-09-09 15:56:45 -0700688 if (!pkg.canHaveOatDir()) {
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800689 return null;
690 }
691 File codePath = new File(pkg.codePath);
692 if (codePath.isDirectory()) {
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800693 // TODO(calin): why do we create this only if the codePath is a directory? (i.e for
694 // cluster packages). It seems that the logic for the folder creation is
695 // split between installd and here.
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800696 File oatDir = getOatDir(codePath);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700697 try {
Andreas Gampea8908752015-11-10 08:58:14 -0800698 mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700699 } catch (InstallerException e) {
700 Slog.w(TAG, "Failed to create oat dir", e);
701 return null;
702 }
Richard Uhler7b08b352015-03-25 16:25:57 -0700703 return oatDir.getAbsolutePath();
Fyodor Kupolovb94c1652015-03-03 12:25:30 -0800704 }
705 return null;
706 }
707
708 static File getOatDir(File codePath) {
709 return new File(codePath, OAT_DIR_NAME);
710 }
711
Fyodor Kupolova627c092015-05-05 18:44:39 -0700712 void systemReady() {
713 mSystemReady = true;
714 }
Andreas Gampea8908752015-11-10 08:58:14 -0800715
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800716 private String printDexoptFlags(int flags) {
717 ArrayList<String> flagsList = new ArrayList<>();
718
719 if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) {
720 flagsList.add("boot_complete");
721 }
722 if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) {
723 flagsList.add("debuggable");
724 }
725 if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) {
726 flagsList.add("profile_guided");
727 }
728 if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) {
729 flagsList.add("public");
730 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800731 if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) {
732 flagsList.add("secondary");
733 }
734 if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) {
735 flagsList.add("force");
736 }
737 if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) {
738 flagsList.add("storage_ce");
739 }
740 if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) {
741 flagsList.add("storage_de");
742 }
David Sehr2118ec42017-10-25 14:28:29 -0700743 if ((flags & DEXOPT_IDLE_BACKGROUND_JOB) == DEXOPT_IDLE_BACKGROUND_JOB) {
744 flagsList.add("idle_background_job");
745 }
David Brazdilf7e31c02018-02-13 17:04:26 +0000746 if ((flags & DEXOPT_ENABLE_HIDDEN_API_CHECKS) == DEXOPT_ENABLE_HIDDEN_API_CHECKS) {
747 flagsList.add("enable_hidden_api_checks");
David Brazdil464ed3d2018-01-18 15:25:18 +0000748 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800749
Calin Juravle2d4b6ad72017-01-04 13:22:14 -0800750 return String.join(",", flagsList);
751 }
752
Andreas Gampea8908752015-11-10 08:58:14 -0800753 /**
754 * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
755 * dexopt path.
756 */
757 public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
758
759 public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
760 Context context, String wakeLockTag) {
761 super(installer, installLock, context, wakeLockTag);
762 }
763
764 public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
765 super(from);
766 }
767
768 @Override
Andreas Gampea8908752015-11-10 08:58:14 -0800769 protected int adjustDexoptNeeded(int dexoptNeeded) {
Nicolas Geoffray96d12a92017-05-03 11:51:53 +0100770 if (dexoptNeeded == DexFile.NO_DEXOPT_NEEDED) {
771 // Ensure compilation by pretending a compiler filter change on the
772 // apk/odex location (the reason for the '-'. A positive value means
773 // the 'oat' location).
774 return -DexFile.DEX2OAT_FOR_FILTER;
775 }
776 return dexoptNeeded;
Andreas Gampea8908752015-11-10 08:58:14 -0800777 }
Calin Juravlec22c30e2017-01-16 19:18:48 -0800778
779 @Override
780 protected int adjustDexoptFlags(int flags) {
781 // Add DEXOPT_FORCE flag to signal installd that it should force compilation
782 // and discard dexoptanalyzer result.
783 return flags | DEXOPT_FORCE;
784 }
Andreas Gampea8908752015-11-10 08:58:14 -0800785 }
Fyodor Kupolov74876572015-02-23 17:14:45 -0800786}