PackageDexOptimizer.java revision c22c30ed1c05c5c24185dc4d380d1c5026923d46
1/*
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
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageInfo;
23import android.content.pm.PackageParser;
24import android.os.Environment;
25import android.os.PowerManager;
26import android.os.UserHandle;
27import android.os.WorkSource;
28import android.util.Log;
29import android.util.Slog;
30
31import com.android.internal.annotations.GuardedBy;
32import com.android.internal.util.IndentingPrintWriter;
33import com.android.server.pm.Installer.InstallerException;
34
35import java.io.File;
36import java.io.IOException;
37import java.util.ArrayList;
38import java.util.List;
39import java.util.Set;
40
41import dalvik.system.DexFile;
42
43import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE;
44import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE;
45import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED;
46import static com.android.server.pm.Installer.DEXOPT_PUBLIC;
47import static com.android.server.pm.Installer.DEXOPT_SAFEMODE;
48import static com.android.server.pm.Installer.DEXOPT_SECONDARY_DEX;
49import static com.android.server.pm.Installer.DEXOPT_FORCE;
50import static com.android.server.pm.Installer.DEXOPT_STORAGE_CE;
51import static com.android.server.pm.Installer.DEXOPT_STORAGE_DE;
52import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
53import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets;
54
55import static com.android.server.pm.PackageManagerServiceCompilerMapping.getNonProfileGuidedCompilerFilter;
56import static dalvik.system.DexFile.isProfileGuidedCompilerFilter;
57
58/**
59 * Helper class for running dexopt command on packages.
60 */
61public class PackageDexOptimizer {
62    private static final String TAG = "PackageManager.DexOptimizer";
63    static final String OAT_DIR_NAME = "oat";
64    // TODO b/19550105 Remove error codes and use exceptions
65    public static final int DEX_OPT_SKIPPED = 0;
66    public static final int DEX_OPT_PERFORMED = 1;
67    public static final int DEX_OPT_FAILED = -1;
68
69    private final Installer mInstaller;
70    private final Object mInstallLock;
71
72    private final PowerManager.WakeLock mDexoptWakeLock;
73    private volatile boolean mSystemReady;
74
75    PackageDexOptimizer(Installer installer, Object installLock, Context context,
76            String wakeLockTag) {
77        this.mInstaller = installer;
78        this.mInstallLock = installLock;
79
80        PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
81        mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
82    }
83
84    protected PackageDexOptimizer(PackageDexOptimizer from) {
85        this.mInstaller = from.mInstaller;
86        this.mInstallLock = from.mInstallLock;
87        this.mDexoptWakeLock = from.mDexoptWakeLock;
88        this.mSystemReady = from.mSystemReady;
89    }
90
91    static boolean canOptimizePackage(PackageParser.Package pkg) {
92        return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0;
93    }
94
95    /**
96     * Performs dexopt on all code paths and libraries of the specified package for specified
97     * instruction sets.
98     *
99     * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
100     * synchronized on {@link #mInstallLock}.
101     */
102    int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries,
103            String[] instructionSets, boolean checkProfiles, String targetCompilationFilter,
104            CompilerStats.PackageStats packageStats) {
105        if (!canOptimizePackage(pkg)) {
106            return DEX_OPT_SKIPPED;
107        }
108        synchronized (mInstallLock) {
109            // During boot the system doesn't need to instantiate and obtain a wake lock.
110            // PowerManager might not be ready, but that doesn't mean that we can't proceed with
111            // dexopt.
112            final boolean useLock = mSystemReady;
113            if (useLock) {
114                mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid));
115                mDexoptWakeLock.acquire();
116            }
117            try {
118                return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles,
119                        targetCompilationFilter, packageStats);
120            } finally {
121                if (useLock) {
122                    mDexoptWakeLock.release();
123                }
124            }
125        }
126    }
127
128    /**
129     * Performs dexopt on all code paths of the given package.
130     * It assumes the install lock is held.
131     */
132    @GuardedBy("mInstallLock")
133    private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries,
134            String[] targetInstructionSets, boolean checkForProfileUpdates,
135            String targetCompilerFilter, CompilerStats.PackageStats packageStats) {
136        final String[] instructionSets = targetInstructionSets != null ?
137                targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo);
138        final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
139        final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
140        final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
141
142        final String compilerFilter = getRealCompilerFilter(pkg.applicationInfo,
143                targetCompilerFilter, isUsedByOtherApps(pkg));
144        final boolean profileUpdated = checkForProfileUpdates &&
145                isProfileUpdated(pkg, sharedGid, compilerFilter);
146
147        // TODO(calin,jeffhao): shared library paths should be adjusted to include previous code
148        // paths (b/34169257).
149        final String sharedLibrariesPath = getSharedLibrariesPath(sharedLibraries);
150        final int dexoptFlags = getDexFlags(pkg, compilerFilter);
151
152        int result = DEX_OPT_SKIPPED;
153        for (String path : paths) {
154            for (String dexCodeIsa : dexCodeInstructionSets) {
155                int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, profileUpdated,
156                        sharedLibrariesPath, dexoptFlags, sharedGid, packageStats);
157                // The end result is:
158                //  - FAILED if any path failed,
159                //  - PERFORMED if at least one path needed compilation,
160                //  - SKIPPED when all paths are up to date
161                if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) {
162                    result = newResult;
163                }
164            }
165        }
166        return result;
167    }
168
169    /**
170     * Performs dexopt on the {@code path} belonging to the package {@code pkg}.
171     *
172     * @return
173     *      DEX_OPT_FAILED if there was any exception during dexopt
174     *      DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
175     *      DEX_OPT_SKIPPED if the path does not need to be deopt-ed.
176     */
177    @GuardedBy("mInstallLock")
178    private int dexOptPath(PackageParser.Package pkg, String path, String isa,
179            String compilerFilter, boolean profileUpdated, String sharedLibrariesPath,
180            int dexoptFlags, int uid, CompilerStats.PackageStats packageStats) {
181        int dexoptNeeded = getDexoptNeeded(path, isa, compilerFilter, profileUpdated);
182        if (Math.abs(dexoptNeeded) == DexFile.NO_DEXOPT_NEEDED) {
183            return DEX_OPT_SKIPPED;
184        }
185
186        // TODO(calin): there's no need to try to create the oat dir over and over again,
187        //              especially since it involve an extra installd call. We should create
188        //              if (if supported) on the fly during the dexopt call.
189        String oatDir = createOatDirIfSupported(pkg, isa);
190
191        Log.i(TAG, "Running dexopt (dexoptNeeded=" + dexoptNeeded + ") on: " + path
192                + " pkg=" + pkg.applicationInfo.packageName + " isa=" + isa
193                + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
194                + " target-filter=" + compilerFilter + " oatDir=" + oatDir
195                + " sharedLibraries=" + sharedLibrariesPath);
196
197        try {
198            long startTime = System.currentTimeMillis();
199
200            mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags,
201                    compilerFilter, pkg.volumeUuid, sharedLibrariesPath);
202
203            if (packageStats != null) {
204                long endTime = System.currentTimeMillis();
205                packageStats.setCompileTime(path, (int)(endTime - startTime));
206            }
207            return DEX_OPT_PERFORMED;
208        } catch (InstallerException e) {
209            Slog.w(TAG, "Failed to dexopt", e);
210            return DEX_OPT_FAILED;
211        }
212    }
213
214    /**
215     * Performs dexopt on the secondary dex {@code path} belonging to the app {@code info}.
216     *
217     * @return
218     *      DEX_OPT_FAILED if there was any exception during dexopt
219     *      DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
220     * NOTE that DEX_OPT_PERFORMED for secondary dex files includes the case when the dex file
221     * didn't need an update. That's because at the moment we don't get more than success/failure
222     * from installd.
223     *
224     * TODO(calin): Consider adding return codes to installd dexopt invocation (rather than
225     * throwing exceptions). Or maybe make a separate call to installd to get DexOptNeeded, though
226     * that seems wasteful.
227     */
228    public int dexOptSecondaryDexPath(ApplicationInfo info, String path, Set<String> isas,
229            String compilerFilter, boolean isUsedByOtherApps) {
230        synchronized (mInstallLock) {
231            // During boot the system doesn't need to instantiate and obtain a wake lock.
232            // PowerManager might not be ready, but that doesn't mean that we can't proceed with
233            // dexopt.
234            final boolean useLock = mSystemReady;
235            if (useLock) {
236                mDexoptWakeLock.setWorkSource(new WorkSource(info.uid));
237                mDexoptWakeLock.acquire();
238            }
239            try {
240                return dexOptSecondaryDexPathLI(info, path, isas, compilerFilter,
241                        isUsedByOtherApps);
242            } finally {
243                if (useLock) {
244                    mDexoptWakeLock.release();
245                }
246            }
247        }
248    }
249
250    @GuardedBy("mInstallLock")
251    private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path, Set<String> isas,
252            String compilerFilter, boolean isUsedByOtherApps) {
253        int dexoptFlags = getDexFlags(info, compilerFilter) | DEXOPT_SECONDARY_DEX;
254        // Check the app storage and add the appropriate flags.
255        if (info.dataDir.equals(info.deviceProtectedDataDir)) {
256            dexoptFlags |= DEXOPT_STORAGE_DE;
257        } else if (info.dataDir.equals(info.credentialProtectedDataDir)) {
258            dexoptFlags |= DEXOPT_STORAGE_CE;
259        } else {
260            Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName);
261            return DEX_OPT_FAILED;
262        }
263        compilerFilter = getRealCompilerFilter(info, compilerFilter, isUsedByOtherApps);
264        Log.d(TAG, "Running dexopt on: " + path
265                + " pkg=" + info.packageName + " isa=" + isas
266                + " dexoptFlags=" + printDexoptFlags(dexoptFlags)
267                + " target-filter=" + compilerFilter);
268
269        try {
270            for (String isa : isas) {
271                // Reuse the same dexopt path as for the primary apks. We don't need all the
272                // arguments as some (dexopNeeded and oatDir) will be computed by installd because
273                // system server cannot read untrusted app content.
274                // TODO(calin): maybe add a separate call.
275                mInstaller.dexopt(path, info.uid, info.packageName, isa, /*dexoptNeeded*/ 0,
276                        /*oatDir*/ null, dexoptFlags,
277                        compilerFilter, info.volumeUuid, /*sharedLibrariesPath*/ null);
278            }
279
280            return DEX_OPT_PERFORMED;
281        } catch (InstallerException e) {
282            Slog.w(TAG, "Failed to dexopt", e);
283            return DEX_OPT_FAILED;
284        }
285    }
286
287    /**
288     * Adjust the given dexopt-needed value. Can be overridden to influence the decision to
289     * optimize or not (and in what way).
290     */
291    protected int adjustDexoptNeeded(int dexoptNeeded) {
292        return dexoptNeeded;
293    }
294
295    /**
296     * Adjust the given dexopt flags that will be passed to the installer.
297     */
298    protected int adjustDexoptFlags(int dexoptFlags) {
299        return dexoptFlags;
300    }
301
302    /**
303     * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}.
304     */
305    void dumpDexoptState(IndentingPrintWriter pw, PackageParser.Package pkg) {
306        final String[] instructionSets = getAppDexInstructionSets(pkg.applicationInfo);
307        final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
308
309        final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
310
311        for (String instructionSet : dexCodeInstructionSets) {
312             pw.println("Instruction Set: " + instructionSet);
313             pw.increaseIndent();
314             for (String path : paths) {
315                  String status = null;
316                  try {
317                      status = DexFile.getDexFileStatus(path, instructionSet);
318                  } catch (IOException ioe) {
319                      status = "[Exception]: " + ioe.getMessage();
320                  }
321                  pw.println("path: " + path);
322                  pw.println("status: " + status);
323             }
324             pw.decreaseIndent();
325        }
326    }
327
328    /**
329     * Returns the compiler filter that should be used to optimize the package code.
330     * The target filter will be updated if the package code is used by other apps
331     * or if it has the safe mode flag set.
332     */
333    private String getRealCompilerFilter(ApplicationInfo info, String targetCompilerFilter,
334            boolean isUsedByOtherApps) {
335        int flags = info.flags;
336        boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
337        if (vmSafeMode) {
338            // For the compilation, it doesn't really matter what we return here because installd
339            // will replace the filter with interpret-only anyway.
340            // However, we return a non profile guided filter so that we simplify the logic of
341            // merging profiles.
342            // TODO(calin): safe mode path could be simplified if we pass interpret-only from
343            //              here rather than letting installd decide on the filter.
344            return getNonProfileGuidedCompilerFilter(targetCompilerFilter);
345        }
346
347        if (isProfileGuidedCompilerFilter(targetCompilerFilter) && isUsedByOtherApps) {
348            // If the dex files is used by other apps, we cannot use profile-guided compilation.
349            return getNonProfileGuidedCompilerFilter(targetCompilerFilter);
350        }
351
352        return targetCompilerFilter;
353    }
354
355    /**
356     * Computes the dex flags that needs to be pass to installd for the given package and compiler
357     * filter.
358     */
359    private int getDexFlags(PackageParser.Package pkg, String compilerFilter) {
360        return getDexFlags(pkg.applicationInfo, compilerFilter);
361    }
362
363    private int getDexFlags(ApplicationInfo info, String compilerFilter) {
364        int flags = info.flags;
365        boolean vmSafeMode = (flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
366        boolean debuggable = (flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
367        // Profile guide compiled oat files should not be public.
368        boolean isProfileGuidedFilter = isProfileGuidedCompilerFilter(compilerFilter);
369        boolean isPublic = !info.isForwardLocked() && !isProfileGuidedFilter;
370        int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0;
371        int dexFlags =
372                (isPublic ? DEXOPT_PUBLIC : 0)
373                | (vmSafeMode ? DEXOPT_SAFEMODE : 0)
374                | (debuggable ? DEXOPT_DEBUGGABLE : 0)
375                | profileFlag
376                | DEXOPT_BOOTCOMPLETE;
377        return adjustDexoptFlags(dexFlags);
378    }
379
380    /**
381     * Assesses if there's a need to perform dexopt on {@code path} for the given
382     * configuration (isa, compiler filter, profile).
383     */
384    private int getDexoptNeeded(String path, String isa, String compilerFilter,
385            boolean newProfile) {
386        int dexoptNeeded;
387        try {
388            dexoptNeeded = DexFile.getDexOptNeeded(path, isa, compilerFilter, newProfile);
389        } catch (IOException ioe) {
390            Slog.w(TAG, "IOException reading apk: " + path, ioe);
391            return DEX_OPT_FAILED;
392        }
393        return adjustDexoptNeeded(dexoptNeeded);
394    }
395
396    /**
397     * Computes the shared libraries path that should be passed to dexopt.
398     */
399    private String getSharedLibrariesPath(String[] sharedLibraries) {
400        if (sharedLibraries == null || sharedLibraries.length == 0) {
401            return null;
402        }
403        StringBuilder sb = new StringBuilder();
404        for (String lib : sharedLibraries) {
405            if (sb.length() != 0) {
406                sb.append(":");
407            }
408            sb.append(lib);
409        }
410        return sb.toString();
411    }
412
413    /**
414     * Checks if there is an update on the profile information of the {@code pkg}.
415     * If the compiler filter is not profile guided the method returns false.
416     *
417     * Note that this is a "destructive" operation with side effects. Under the hood the
418     * current profile and the reference profile will be merged and subsequent calls
419     * may return a different result.
420     */
421    private boolean isProfileUpdated(PackageParser.Package pkg, int uid, String compilerFilter) {
422        // Check if we are allowed to merge and if the compiler filter is profile guided.
423        if (!isProfileGuidedCompilerFilter(compilerFilter)) {
424            return false;
425        }
426        // Merge profiles. It returns whether or not there was an updated in the profile info.
427        try {
428            return mInstaller.mergeProfiles(uid, pkg.packageName);
429        } catch (InstallerException e) {
430            Slog.w(TAG, "Failed to merge profiles", e);
431        }
432        return false;
433    }
434
435    /**
436     * Creates oat dir for the specified package if needed and supported.
437     * In certain cases oat directory
438     * <strong>cannot</strong> be created:
439     * <ul>
440     *      <li>{@code pkg} is a system app, which is not updated.</li>
441     *      <li>Package location is not a directory, i.e. monolithic install.</li>
442     * </ul>
443     *
444     * @return Absolute path to the oat directory or null, if oat directory
445     * cannot be created.
446     */
447    @Nullable
448    private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) {
449        if (!pkg.canHaveOatDir()) {
450            return null;
451        }
452        File codePath = new File(pkg.codePath);
453        if (codePath.isDirectory()) {
454            // TODO(calin): why do we create this only if the codePath is a directory? (i.e for
455            //              cluster packages). It seems that the logic for the folder creation is
456            //              split between installd and here.
457            File oatDir = getOatDir(codePath);
458            try {
459                mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet);
460            } catch (InstallerException e) {
461                Slog.w(TAG, "Failed to create oat dir", e);
462                return null;
463            }
464            return oatDir.getAbsolutePath();
465        }
466        return null;
467    }
468
469    static File getOatDir(File codePath) {
470        return new File(codePath, OAT_DIR_NAME);
471    }
472
473    void systemReady() {
474        mSystemReady = true;
475    }
476
477    /**
478     * Returns true if the profiling data collected for the given app indicate
479     * that the apps's APK has been loaded by another app.
480     * Note that this returns false for all forward-locked apps and apps without
481     * any collected profiling data.
482     */
483    public static boolean isUsedByOtherApps(PackageParser.Package pkg) {
484        if (pkg.isForwardLocked()) {
485            // Skip the check for forward locked packages since they don't share their code.
486            return false;
487        }
488
489        for (String apkPath : pkg.getAllCodePathsExcludingResourceOnly()) {
490            try {
491                apkPath = PackageManagerServiceUtils.realpath(new File(apkPath));
492            } catch (IOException e) {
493                // Log an error but continue without it.
494                Slog.w(TAG, "Failed to get canonical path", e);
495                continue;
496            }
497            String useMarker = apkPath.replace('/', '@');
498            final int[] currentUserIds = UserManagerService.getInstance().getUserIds();
499            for (int i = 0; i < currentUserIds.length; i++) {
500                File profileDir =
501                        Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]);
502                File foreignUseMark = new File(profileDir, useMarker);
503                if (foreignUseMark.exists()) {
504                    return true;
505                }
506            }
507        }
508        return false;
509    }
510
511    private String printDexoptFlags(int flags) {
512        ArrayList<String> flagsList = new ArrayList<>();
513
514        if ((flags & DEXOPT_BOOTCOMPLETE) == DEXOPT_BOOTCOMPLETE) {
515            flagsList.add("boot_complete");
516        }
517        if ((flags & DEXOPT_DEBUGGABLE) == DEXOPT_DEBUGGABLE) {
518            flagsList.add("debuggable");
519        }
520        if ((flags & DEXOPT_PROFILE_GUIDED) == DEXOPT_PROFILE_GUIDED) {
521            flagsList.add("profile_guided");
522        }
523        if ((flags & DEXOPT_PUBLIC) == DEXOPT_PUBLIC) {
524            flagsList.add("public");
525        }
526        if ((flags & DEXOPT_SAFEMODE) == DEXOPT_SAFEMODE) {
527            flagsList.add("safemode");
528        }
529        if ((flags & DEXOPT_SECONDARY_DEX) == DEXOPT_SECONDARY_DEX) {
530            flagsList.add("secondary");
531        }
532        if ((flags & DEXOPT_FORCE) == DEXOPT_FORCE) {
533            flagsList.add("force");
534        }
535        if ((flags & DEXOPT_STORAGE_CE) == DEXOPT_STORAGE_CE) {
536            flagsList.add("storage_ce");
537        }
538        if ((flags & DEXOPT_STORAGE_DE) == DEXOPT_STORAGE_DE) {
539            flagsList.add("storage_de");
540        }
541
542        return String.join(",", flagsList);
543    }
544
545    /**
546     * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a
547     * dexopt path.
548     */
549    public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer {
550
551        public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock,
552                Context context, String wakeLockTag) {
553            super(installer, installLock, context, wakeLockTag);
554        }
555
556        public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) {
557            super(from);
558        }
559
560        @Override
561        protected int adjustDexoptNeeded(int dexoptNeeded) {
562            // Ensure compilation, no matter the current state.
563            // TODO: The return value is wrong when patchoat is needed.
564            return DexFile.DEX2OAT_FROM_SCRATCH;
565        }
566
567        @Override
568        protected int adjustDexoptFlags(int flags) {
569            // Add DEXOPT_FORCE flag to signal installd that it should force compilation
570            // and discard dexoptanalyzer result.
571            return flags | DEXOPT_FORCE;
572        }
573    }
574}
575