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