DexManager.java revision a70b1b120365976f5eeb7cb3f577f5f6ff7ad18e
1/*
2 * Copyright (C) 2016 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.dex;
18
19import android.content.pm.ApplicationInfo;
20import android.content.pm.IPackageManager;
21import android.content.pm.PackageInfo;
22import android.content.pm.PackageParser;
23import android.os.RemoteException;
24import android.os.storage.StorageManager;
25import android.os.UserHandle;
26
27import android.util.Slog;
28
29import com.android.internal.annotations.GuardedBy;
30import com.android.server.pm.Installer;
31import com.android.server.pm.Installer.InstallerException;
32import com.android.server.pm.PackageDexOptimizer;
33import com.android.server.pm.PackageManagerServiceUtils;
34import com.android.server.pm.PackageManagerServiceCompilerMapping;
35
36import java.io.File;
37import java.io.IOException;
38import java.util.List;
39import java.util.HashMap;
40import java.util.HashSet;
41import java.util.Map;
42import java.util.Set;
43
44import static com.android.server.pm.dex.PackageDexUsage.PackageUseInfo;
45import static com.android.server.pm.dex.PackageDexUsage.DexUseInfo;
46
47/**
48 * This class keeps track of how dex files are used.
49 * Every time it gets a notification about a dex file being loaded it tracks
50 * its owning package and records it in PackageDexUsage (package-dex-usage.list).
51 *
52 * TODO(calin): Extract related dexopt functionality from PackageManagerService
53 * into this class.
54 */
55public class DexManager {
56    private static final String TAG = "DexManager";
57
58    private static final boolean DEBUG = false;
59
60    // Maps package name to code locations.
61    // It caches the code locations for the installed packages. This allows for
62    // faster lookups (no locks) when finding what package owns the dex file.
63    private final Map<String, PackageCodeLocations> mPackageCodeLocationsCache;
64
65    // PackageDexUsage handles the actual I/O operations. It is responsible to
66    // encode and save the dex usage data.
67    private final PackageDexUsage mPackageDexUsage;
68
69    private final IPackageManager mPackageManager;
70    private final PackageDexOptimizer mPackageDexOptimizer;
71    private final Object mInstallLock;
72    @GuardedBy("mInstallLock")
73    private final Installer mInstaller;
74
75    // Possible outcomes of a dex search.
76    private static int DEX_SEARCH_NOT_FOUND = 0;  // dex file not found
77    private static int DEX_SEARCH_FOUND_PRIMARY = 1;  // dex file is the primary/base apk
78    private static int DEX_SEARCH_FOUND_SPLIT = 2;  // dex file is a split apk
79    private static int DEX_SEARCH_FOUND_SECONDARY = 3;  // dex file is a secondary dex
80
81    public DexManager(IPackageManager pms, PackageDexOptimizer pdo,
82            Installer installer, Object installLock) {
83      mPackageCodeLocationsCache = new HashMap<>();
84      mPackageDexUsage = new PackageDexUsage();
85      mPackageManager = pms;
86      mPackageDexOptimizer = pdo;
87      mInstaller = installer;
88      mInstallLock = installLock;
89    }
90
91    /**
92     * Notify about dex files loads.
93     * Note that this method is invoked when apps load dex files and it should
94     * return as fast as possible.
95     *
96     * @param loadingPackage the package performing the load
97     * @param dexPaths the list of dex files being loaded
98     * @param loaderIsa the ISA of the app loading the dex files
99     * @param loaderUserId the user id which runs the code loading the dex files
100     */
101    public void notifyDexLoad(ApplicationInfo loadingAppInfo, List<String> dexPaths,
102            String loaderIsa, int loaderUserId) {
103        try {
104            notifyDexLoadInternal(loadingAppInfo, dexPaths, loaderIsa, loaderUserId);
105        } catch (Exception e) {
106            Slog.w(TAG, "Exception while notifying dex load for package " +
107                    loadingAppInfo.packageName, e);
108        }
109    }
110
111    private void notifyDexLoadInternal(ApplicationInfo loadingAppInfo, List<String> dexPaths,
112            String loaderIsa, int loaderUserId) {
113        if (!PackageManagerServiceUtils.checkISA(loaderIsa)) {
114            Slog.w(TAG, "Loading dex files " + dexPaths + " in unsupported ISA: " +
115                    loaderIsa + "?");
116            return;
117        }
118
119        for (String dexPath : dexPaths) {
120            // Find the owning package name.
121            DexSearchResult searchResult = getDexPackage(loadingAppInfo, dexPath, loaderUserId);
122
123            if (DEBUG) {
124                Slog.i(TAG, loadingAppInfo.packageName
125                    + " loads from " + searchResult + " : " + loaderUserId + " : " + dexPath);
126            }
127
128            if (searchResult.mOutcome != DEX_SEARCH_NOT_FOUND) {
129                // TODO(calin): extend isUsedByOtherApps check to detect the cases where
130                // different apps share the same runtime. In that case we should not mark the dex
131                // file as isUsedByOtherApps. Currently this is a safe approximation.
132                boolean isUsedByOtherApps = !loadingAppInfo.packageName.equals(
133                        searchResult.mOwningPackageName);
134                boolean primaryOrSplit = searchResult.mOutcome == DEX_SEARCH_FOUND_PRIMARY ||
135                        searchResult.mOutcome == DEX_SEARCH_FOUND_SPLIT;
136
137                if (primaryOrSplit && !isUsedByOtherApps) {
138                    // If the dex file is the primary apk (or a split) and not isUsedByOtherApps
139                    // do not record it. This case does not bring any new usable information
140                    // and can be safely skipped.
141                    continue;
142                }
143
144                // Record dex file usage. If the current usage is a new pattern (e.g. new secondary,
145                // or UsedBytOtherApps), record will return true and we trigger an async write
146                // to disk to make sure we don't loose the data in case of a reboot.
147                if (mPackageDexUsage.record(searchResult.mOwningPackageName,
148                        dexPath, loaderUserId, loaderIsa, isUsedByOtherApps, primaryOrSplit)) {
149                    mPackageDexUsage.maybeWriteAsync();
150                }
151            } else {
152                // This can happen in a few situations:
153                // - bogus dex loads
154                // - recent installs/uninstalls that we didn't detect.
155                // - new installed splits
156                // If we can't find the owner of the dex we simply do not track it. The impact is
157                // that the dex file will not be considered for offline optimizations.
158                // TODO(calin): add hooks for move/uninstall notifications to
159                // capture package moves or obsolete packages.
160                if (DEBUG) {
161                    Slog.i(TAG, "Could not find owning package for dex file: " + dexPath);
162                }
163            }
164        }
165    }
166
167    /**
168     * Read the dex usage from disk and populate the code cache locations.
169     * @param existingPackages a map containing information about what packages
170     *          are available to what users. Only packages in this list will be
171     *          recognized during notifyDexLoad().
172     */
173    public void load(Map<Integer, List<PackageInfo>> existingPackages) {
174        try {
175            loadInternal(existingPackages);
176        } catch (Exception e) {
177            mPackageDexUsage.clear();
178            Slog.w(TAG, "Exception while loading package dex usage. " +
179                    "Starting with a fresh state.", e);
180        }
181    }
182
183    /**
184     * Notifies that a new package was installed for {@code userId}.
185     * {@code userId} must not be {@code UserHandle.USER_ALL}.
186     *
187     * @throws IllegalArgumentException if {@code userId} is {@code UserHandle.USER_ALL}.
188     */
189    public void notifyPackageInstalled(PackageInfo pi, int userId) {
190        if (userId == UserHandle.USER_ALL) {
191            throw new IllegalArgumentException(
192                "notifyPackageInstalled called with USER_ALL");
193        }
194        cachePackageCodeLocation(pi.packageName, pi.applicationInfo.sourceDir,
195                pi.applicationInfo.splitSourceDirs, pi.applicationInfo.dataDir, userId);
196    }
197
198    /**
199     * Notifies that package {@code packageName} was updated.
200     * This will clear the UsedByOtherApps mark if it exists.
201     */
202    public void notifyPackageUpdated(String packageName, String baseCodePath,
203            String[] splitCodePaths) {
204        cachePackageCodeLocation(packageName, baseCodePath, splitCodePaths, null, /*userId*/ -1);
205        // In case there was an update, write the package use info to disk async.
206        // Note that we do the writing here and not in PackageDexUsage in order to be
207        // consistent with other methods in DexManager (e.g. reconcileSecondaryDexFiles performs
208        // multiple updates in PackaeDexUsage before writing it).
209        if (mPackageDexUsage.clearUsedByOtherApps(packageName)) {
210            mPackageDexUsage.maybeWriteAsync();
211        }
212    }
213
214    /**
215     * Notifies that the user {@code userId} data for package {@code packageName}
216     * was destroyed. This will remove all usage info associated with the package
217     * for the given user.
218     * {@code userId} is allowed to be {@code UserHandle.USER_ALL} in which case
219     * all usage information for the package will be removed.
220     */
221    public void notifyPackageDataDestroyed(String packageName, int userId) {
222        boolean updated = userId == UserHandle.USER_ALL
223            ? mPackageDexUsage.removePackage(packageName)
224            : mPackageDexUsage.removeUserPackage(packageName, userId);
225        // In case there was an update, write the package use info to disk async.
226        // Note that we do the writing here and not in PackageDexUsage in order to be
227        // consistent with other methods in DexManager (e.g. reconcileSecondaryDexFiles performs
228        // multiple updates in PackaeDexUsage before writing it).
229        if (updated) {
230            mPackageDexUsage.maybeWriteAsync();
231        }
232    }
233
234    public void cachePackageCodeLocation(String packageName, String baseCodePath,
235            String[] splitCodePaths, String dataDir, int userId) {
236        PackageCodeLocations pcl = putIfAbsent(mPackageCodeLocationsCache, packageName,
237                new PackageCodeLocations(packageName, baseCodePath, splitCodePaths));
238        pcl.updateCodeLocation(baseCodePath, splitCodePaths);
239        if (dataDir != null) {
240            pcl.mergeAppDataDirs(dataDir, userId);
241        }
242    }
243
244    private void loadInternal(Map<Integer, List<PackageInfo>> existingPackages) {
245        Map<String, Set<Integer>> packageToUsersMap = new HashMap<>();
246        // Cache the code locations for the installed packages. This allows for
247        // faster lookups (no locks) when finding what package owns the dex file.
248        for (Map.Entry<Integer, List<PackageInfo>> entry : existingPackages.entrySet()) {
249            List<PackageInfo> packageInfoList = entry.getValue();
250            int userId = entry.getKey();
251            for (PackageInfo pi : packageInfoList) {
252                // Cache the code locations.
253                cachePackageCodeLocation(pi.packageName, pi.applicationInfo.sourceDir,
254                        pi.applicationInfo.splitSourceDirs, pi.applicationInfo.dataDir, userId);
255
256                // Cache a map from package name to the set of user ids who installed the package.
257                // We will use it to sync the data and remove obsolete entries from
258                // mPackageDexUsage.
259                Set<Integer> users = putIfAbsent(
260                        packageToUsersMap, pi.packageName, new HashSet<>());
261                users.add(userId);
262            }
263        }
264
265        mPackageDexUsage.read();
266        mPackageDexUsage.syncData(packageToUsersMap);
267    }
268
269    /**
270     * Get the package dex usage for the given package name.
271     * @return the package data or null if there is no data available for this package.
272     */
273    public PackageUseInfo getPackageUseInfo(String packageName) {
274        return mPackageDexUsage.getPackageUseInfo(packageName);
275    }
276
277    /**
278     * Perform dexopt on the package {@code packageName} secondary dex files.
279     * @return true if all secondary dex files were processed successfully (compiled or skipped
280     *         because they don't need to be compiled)..
281     */
282    public boolean dexoptSecondaryDex(String packageName, int compilerReason, boolean force) {
283        return dexoptSecondaryDex(packageName,
284                PackageManagerServiceCompilerMapping.getCompilerFilterForReason(compilerReason),
285                force);
286    }
287
288    /**
289     * Perform dexopt on the package {@code packageName} secondary dex files.
290     * @return true if all secondary dex files were processed successfully (compiled or skipped
291     *         because they don't need to be compiled)..
292     */
293    public boolean dexoptSecondaryDex(String packageName, String compilerFilter, boolean force) {
294        // Select the dex optimizer based on the force parameter.
295        // Forced compilation is done through ForcedUpdatePackageDexOptimizer which will adjust
296        // the necessary dexopt flags to make sure that compilation is not skipped. This avoid
297        // passing the force flag through the multitude of layers.
298        // Note: The force option is rarely used (cmdline input for testing, mostly), so it's OK to
299        //       allocate an object here.
300        PackageDexOptimizer pdo = force
301                ? new PackageDexOptimizer.ForcedUpdatePackageDexOptimizer(mPackageDexOptimizer)
302                : mPackageDexOptimizer;
303        PackageUseInfo useInfo = getPackageUseInfo(packageName);
304        if (useInfo == null || useInfo.getDexUseInfoMap().isEmpty()) {
305            if (DEBUG) {
306                Slog.d(TAG, "No secondary dex use for package:" + packageName);
307            }
308            // Nothing to compile, return true.
309            return true;
310        }
311        boolean success = true;
312        for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) {
313            String dexPath = entry.getKey();
314            DexUseInfo dexUseInfo = entry.getValue();
315            PackageInfo pkg = null;
316            try {
317                pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0,
318                    dexUseInfo.getOwnerUserId());
319            } catch (RemoteException e) {
320                throw new AssertionError(e);
321            }
322            // It may be that the package gets uninstalled while we try to compile its
323            // secondary dex files. If that's the case, just ignore.
324            // Note that we don't break the entire loop because the package might still be
325            // installed for other users.
326            if (pkg == null) {
327                Slog.d(TAG, "Could not find package when compiling secondary dex " + packageName
328                        + " for user " + dexUseInfo.getOwnerUserId());
329                mPackageDexUsage.removeUserPackage(packageName, dexUseInfo.getOwnerUserId());
330                continue;
331            }
332            int result = pdo.dexOptSecondaryDexPath(pkg.applicationInfo, dexPath,
333                    dexUseInfo.getLoaderIsas(), compilerFilter, dexUseInfo.isUsedByOtherApps());
334            success = success && (result != PackageDexOptimizer.DEX_OPT_FAILED);
335        }
336        return success;
337    }
338
339    /**
340     * Reconcile the information we have about the secondary dex files belonging to
341     * {@code packagName} and the actual dex files. For all dex files that were
342     * deleted, update the internal records and delete any generated oat files.
343     */
344    public void reconcileSecondaryDexFiles(String packageName) {
345        PackageUseInfo useInfo = getPackageUseInfo(packageName);
346        if (useInfo == null || useInfo.getDexUseInfoMap().isEmpty()) {
347            if (DEBUG) {
348                Slog.d(TAG, "No secondary dex use for package:" + packageName);
349            }
350            // Nothing to reconcile.
351            return;
352        }
353        Set<String> dexFilesToRemove = new HashSet<>();
354        boolean updated = false;
355        for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) {
356            String dexPath = entry.getKey();
357            DexUseInfo dexUseInfo = entry.getValue();
358            PackageInfo pkg = null;
359            try {
360                // Note that we look for the package in the PackageManager just to be able
361                // to get back the real app uid and its storage kind. These are only used
362                // to perform extra validation in installd.
363                // TODO(calin): maybe a bit overkill.
364                pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0,
365                    dexUseInfo.getOwnerUserId());
366            } catch (RemoteException ignore) {
367                // Can't happen, DexManager is local.
368            }
369            if (pkg == null) {
370                // It may be that the package was uninstalled while we process the secondary
371                // dex files.
372                Slog.d(TAG, "Could not find package when compiling secondary dex " + packageName
373                        + " for user " + dexUseInfo.getOwnerUserId());
374                // Update the usage and continue, another user might still have the package.
375                updated = mPackageDexUsage.removeUserPackage(
376                        packageName, dexUseInfo.getOwnerUserId()) || updated;
377                continue;
378            }
379            ApplicationInfo info = pkg.applicationInfo;
380            int flags = 0;
381            if (info.dataDir.equals(info.deviceProtectedDataDir)) {
382                flags |= StorageManager.FLAG_STORAGE_DE;
383            } else if (info.dataDir.equals(info.credentialProtectedDataDir)) {
384                flags |= StorageManager.FLAG_STORAGE_CE;
385            } else {
386                Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName);
387                updated = mPackageDexUsage.removeUserPackage(
388                        packageName, dexUseInfo.getOwnerUserId()) || updated;
389                continue;
390            }
391
392            boolean dexStillExists = true;
393            synchronized(mInstallLock) {
394                try {
395                    String[] isas = dexUseInfo.getLoaderIsas().toArray(new String[0]);
396                    dexStillExists = mInstaller.reconcileSecondaryDexFile(dexPath, packageName,
397                            pkg.applicationInfo.uid, isas, pkg.applicationInfo.volumeUuid, flags);
398                } catch (InstallerException e) {
399                    Slog.e(TAG, "Got InstallerException when reconciling dex " + dexPath +
400                            " : " + e.getMessage());
401                }
402            }
403            if (!dexStillExists) {
404                updated = mPackageDexUsage.removeDexFile(
405                        packageName, dexPath, dexUseInfo.getOwnerUserId()) || updated;
406            }
407
408        }
409        if (updated) {
410            mPackageDexUsage.maybeWriteAsync();
411        }
412    }
413
414    /**
415     * Return all packages that contain records of secondary dex files.
416     */
417    public Set<String> getAllPackagesWithSecondaryDexFiles() {
418        return mPackageDexUsage.getAllPackagesWithSecondaryDexFiles();
419    }
420
421    /**
422     * Return true if the profiling data collected for the given app indicate
423     * that the apps's APK has been loaded by another app.
424     * Note that this returns false for all apps without any collected profiling data.
425    */
426    public boolean isUsedByOtherApps(String packageName) {
427        PackageUseInfo useInfo = getPackageUseInfo(packageName);
428        if (useInfo == null) {
429            // No use info, means the package was not used or it was used but not by other apps.
430            // Note that right now we might prune packages which are not used by other apps.
431            // TODO(calin): maybe we should not (prune) so we can have an accurate view when we try
432            // to access the package use.
433            return false;
434        }
435        return useInfo.isUsedByOtherApps();
436    }
437
438    /**
439     * Retrieves the package which owns the given dexPath.
440     */
441    private DexSearchResult getDexPackage(
442            ApplicationInfo loadingAppInfo, String dexPath, int userId) {
443        // Ignore framework code.
444        // TODO(calin): is there a better way to detect it?
445        if (dexPath.startsWith("/system/framework/")) {
446            return new DexSearchResult("framework", DEX_SEARCH_NOT_FOUND);
447        }
448
449        // First, check if the package which loads the dex file actually owns it.
450        // Most of the time this will be true and we can return early.
451        PackageCodeLocations loadingPackageCodeLocations =
452                new PackageCodeLocations(loadingAppInfo, userId);
453        int outcome = loadingPackageCodeLocations.searchDex(dexPath, userId);
454        if (outcome != DEX_SEARCH_NOT_FOUND) {
455            // TODO(calin): evaluate if we bother to detect symlinks at the dexPath level.
456            return new DexSearchResult(loadingPackageCodeLocations.mPackageName, outcome);
457        }
458
459        // The loadingPackage does not own the dex file.
460        // Perform a reverse look-up in the cache to detect if any package has ownership.
461        // Note that we can have false negatives if the cache falls out of date.
462        for (PackageCodeLocations pcl : mPackageCodeLocationsCache.values()) {
463            outcome = pcl.searchDex(dexPath, userId);
464            if (outcome != DEX_SEARCH_NOT_FOUND) {
465                return new DexSearchResult(pcl.mPackageName, outcome);
466            }
467        }
468
469        // Cache miss. Return not found for the moment.
470        //
471        // TODO(calin): this may be because of a newly installed package, an update
472        // or a new added user. We can either perform a full look up again or register
473        // observers to be notified of package/user updates.
474        return new DexSearchResult(null, DEX_SEARCH_NOT_FOUND);
475    }
476
477    private static <K,V> V putIfAbsent(Map<K,V> map, K key, V newValue) {
478        V existingValue = map.putIfAbsent(key, newValue);
479        return existingValue == null ? newValue : existingValue;
480    }
481
482    /**
483     * Convenience class to store the different locations where a package might
484     * own code.
485     */
486    private static class PackageCodeLocations {
487        private final String mPackageName;
488        private String mBaseCodePath;
489        private final Set<String> mSplitCodePaths;
490        // Maps user id to the application private directory.
491        private final Map<Integer, Set<String>> mAppDataDirs;
492
493        public PackageCodeLocations(ApplicationInfo ai, int userId) {
494            this(ai.packageName, ai.sourceDir, ai.splitSourceDirs);
495            mergeAppDataDirs(ai.dataDir, userId);
496        }
497        public PackageCodeLocations(String packageName, String baseCodePath,
498                String[] splitCodePaths) {
499            mPackageName = packageName;
500            mSplitCodePaths = new HashSet<>();
501            mAppDataDirs = new HashMap<>();
502            updateCodeLocation(baseCodePath, splitCodePaths);
503        }
504
505        public void updateCodeLocation(String baseCodePath, String[] splitCodePaths) {
506            mBaseCodePath = baseCodePath;
507            mSplitCodePaths.clear();
508            if (splitCodePaths != null) {
509                for (String split : splitCodePaths) {
510                    mSplitCodePaths.add(split);
511                }
512            }
513        }
514
515        public void mergeAppDataDirs(String dataDir, int userId) {
516            Set<String> dataDirs = putIfAbsent(mAppDataDirs, userId, new HashSet<>());
517            dataDirs.add(dataDir);
518        }
519
520        public int searchDex(String dexPath, int userId) {
521            // First check that this package is installed or active for the given user.
522            // If we don't have a data dir it means this user is trying to load something
523            // unavailable for them.
524            Set<String> userDataDirs = mAppDataDirs.get(userId);
525            if (userDataDirs == null) {
526                Slog.w(TAG, "Trying to load a dex path which does not exist for the current " +
527                        "user. dexPath=" + dexPath + ", userId=" + userId);
528                return DEX_SEARCH_NOT_FOUND;
529            }
530
531            if (mBaseCodePath.equals(dexPath)) {
532                return DEX_SEARCH_FOUND_PRIMARY;
533            }
534            if (mSplitCodePaths.contains(dexPath)) {
535                return DEX_SEARCH_FOUND_SPLIT;
536            }
537            for (String dataDir : userDataDirs) {
538                if (dexPath.startsWith(dataDir)) {
539                    return DEX_SEARCH_FOUND_SECONDARY;
540                }
541            }
542
543            // TODO(calin): What if we get a symlink? e.g. data dir may be a symlink,
544            // /data/data/ -> /data/user/0/.
545            if (DEBUG) {
546                try {
547                    String dexPathReal = PackageManagerServiceUtils.realpath(new File(dexPath));
548                    if (dexPathReal != dexPath) {
549                        Slog.d(TAG, "Dex loaded with symlink. dexPath=" +
550                                dexPath + " dexPathReal=" + dexPathReal);
551                    }
552                } catch (IOException e) {
553                    // Ignore
554                }
555            }
556            return DEX_SEARCH_NOT_FOUND;
557        }
558    }
559
560    /**
561     * Convenience class to store ownership search results.
562     */
563    private class DexSearchResult {
564        private String mOwningPackageName;
565        private int mOutcome;
566
567        public DexSearchResult(String owningPackageName, int outcome) {
568            this.mOwningPackageName = owningPackageName;
569            this.mOutcome = outcome;
570        }
571
572        @Override
573        public String toString() {
574            return mOwningPackageName + "-" + mOutcome;
575        }
576    }
577
578
579}
580