DexManager.java revision 2dfc1b3e125860221bc67835c2d5c99198a12f8a
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, String compilerFilter, boolean force) {
283        // Select the dex optimizer based on the force parameter.
284        // Forced compilation is done through ForcedUpdatePackageDexOptimizer which will adjust
285        // the necessary dexopt flags to make sure that compilation is not skipped. This avoid
286        // passing the force flag through the multitude of layers.
287        // Note: The force option is rarely used (cmdline input for testing, mostly), so it's OK to
288        //       allocate an object here.
289        PackageDexOptimizer pdo = force
290                ? new PackageDexOptimizer.ForcedUpdatePackageDexOptimizer(mPackageDexOptimizer)
291                : mPackageDexOptimizer;
292        PackageUseInfo useInfo = getPackageUseInfo(packageName);
293        if (useInfo == null || useInfo.getDexUseInfoMap().isEmpty()) {
294            if (DEBUG) {
295                Slog.d(TAG, "No secondary dex use for package:" + packageName);
296            }
297            // Nothing to compile, return true.
298            return true;
299        }
300        boolean success = true;
301        for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) {
302            String dexPath = entry.getKey();
303            DexUseInfo dexUseInfo = entry.getValue();
304            PackageInfo pkg = null;
305            try {
306                pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0,
307                    dexUseInfo.getOwnerUserId());
308            } catch (RemoteException e) {
309                throw new AssertionError(e);
310            }
311            // It may be that the package gets uninstalled while we try to compile its
312            // secondary dex files. If that's the case, just ignore.
313            // Note that we don't break the entire loop because the package might still be
314            // installed for other users.
315            if (pkg == null) {
316                Slog.d(TAG, "Could not find package when compiling secondary dex " + packageName
317                        + " for user " + dexUseInfo.getOwnerUserId());
318                mPackageDexUsage.removeUserPackage(packageName, dexUseInfo.getOwnerUserId());
319                continue;
320            }
321            int result = pdo.dexOptSecondaryDexPath(pkg.applicationInfo, dexPath,
322                    dexUseInfo.getLoaderIsas(), compilerFilter, dexUseInfo.isUsedByOtherApps());
323            success = success && (result != PackageDexOptimizer.DEX_OPT_FAILED);
324        }
325        return success;
326    }
327
328    /**
329     * Reconcile the information we have about the secondary dex files belonging to
330     * {@code packagName} and the actual dex files. For all dex files that were
331     * deleted, update the internal records and delete any generated oat files.
332     */
333    public void reconcileSecondaryDexFiles(String packageName) {
334        PackageUseInfo useInfo = getPackageUseInfo(packageName);
335        if (useInfo == null || useInfo.getDexUseInfoMap().isEmpty()) {
336            if (DEBUG) {
337                Slog.d(TAG, "No secondary dex use for package:" + packageName);
338            }
339            // Nothing to reconcile.
340            return;
341        }
342        Set<String> dexFilesToRemove = new HashSet<>();
343        boolean updated = false;
344        for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) {
345            String dexPath = entry.getKey();
346            DexUseInfo dexUseInfo = entry.getValue();
347            PackageInfo pkg = null;
348            try {
349                // Note that we look for the package in the PackageManager just to be able
350                // to get back the real app uid and its storage kind. These are only used
351                // to perform extra validation in installd.
352                // TODO(calin): maybe a bit overkill.
353                pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0,
354                    dexUseInfo.getOwnerUserId());
355            } catch (RemoteException ignore) {
356                // Can't happen, DexManager is local.
357            }
358            if (pkg == null) {
359                // It may be that the package was uninstalled while we process the secondary
360                // dex files.
361                Slog.d(TAG, "Could not find package when compiling secondary dex " + packageName
362                        + " for user " + dexUseInfo.getOwnerUserId());
363                // Update the usage and continue, another user might still have the package.
364                updated = mPackageDexUsage.removeUserPackage(
365                        packageName, dexUseInfo.getOwnerUserId()) || updated;
366                continue;
367            }
368            ApplicationInfo info = pkg.applicationInfo;
369            int flags = 0;
370            if (info.dataDir.equals(info.deviceProtectedDataDir)) {
371                flags |= StorageManager.FLAG_STORAGE_DE;
372            } else if (info.dataDir.equals(info.credentialProtectedDataDir)) {
373                flags |= StorageManager.FLAG_STORAGE_CE;
374            } else {
375                Slog.e(TAG, "Could not infer CE/DE storage for package " + info.packageName);
376                updated = mPackageDexUsage.removeUserPackage(
377                        packageName, dexUseInfo.getOwnerUserId()) || updated;
378                continue;
379            }
380
381            boolean dexStillExists = true;
382            synchronized(mInstallLock) {
383                try {
384                    String[] isas = dexUseInfo.getLoaderIsas().toArray(new String[0]);
385                    dexStillExists = mInstaller.reconcileSecondaryDexFile(dexPath, packageName,
386                            pkg.applicationInfo.uid, isas, pkg.applicationInfo.volumeUuid, flags);
387                } catch (InstallerException e) {
388                    Slog.e(TAG, "Got InstallerException when reconciling dex " + dexPath +
389                            " : " + e.getMessage());
390                }
391            }
392            if (!dexStillExists) {
393                updated = mPackageDexUsage.removeDexFile(
394                        packageName, dexPath, dexUseInfo.getOwnerUserId()) || updated;
395            }
396
397        }
398        if (updated) {
399            mPackageDexUsage.maybeWriteAsync();
400        }
401    }
402
403    /**
404     * Return all packages that contain records of secondary dex files.
405     */
406    public Set<String> getAllPackagesWithSecondaryDexFiles() {
407        return mPackageDexUsage.getAllPackagesWithSecondaryDexFiles();
408    }
409
410    /**
411     * Return true if the profiling data collected for the given app indicate
412     * that the apps's APK has been loaded by another app.
413     * Note that this returns false for all apps without any collected profiling data.
414    */
415    public boolean isUsedByOtherApps(String packageName) {
416        PackageUseInfo useInfo = getPackageUseInfo(packageName);
417        if (useInfo == null) {
418            // No use info, means the package was not used or it was used but not by other apps.
419            // Note that right now we might prune packages which are not used by other apps.
420            // TODO(calin): maybe we should not (prune) so we can have an accurate view when we try
421            // to access the package use.
422            return false;
423        }
424        return useInfo.isUsedByOtherApps();
425    }
426
427    /**
428     * Retrieves the package which owns the given dexPath.
429     */
430    private DexSearchResult getDexPackage(
431            ApplicationInfo loadingAppInfo, String dexPath, int userId) {
432        // Ignore framework code.
433        // TODO(calin): is there a better way to detect it?
434        if (dexPath.startsWith("/system/framework/")) {
435            return new DexSearchResult("framework", DEX_SEARCH_NOT_FOUND);
436        }
437
438        // First, check if the package which loads the dex file actually owns it.
439        // Most of the time this will be true and we can return early.
440        PackageCodeLocations loadingPackageCodeLocations =
441                new PackageCodeLocations(loadingAppInfo, userId);
442        int outcome = loadingPackageCodeLocations.searchDex(dexPath, userId);
443        if (outcome != DEX_SEARCH_NOT_FOUND) {
444            // TODO(calin): evaluate if we bother to detect symlinks at the dexPath level.
445            return new DexSearchResult(loadingPackageCodeLocations.mPackageName, outcome);
446        }
447
448        // The loadingPackage does not own the dex file.
449        // Perform a reverse look-up in the cache to detect if any package has ownership.
450        // Note that we can have false negatives if the cache falls out of date.
451        for (PackageCodeLocations pcl : mPackageCodeLocationsCache.values()) {
452            outcome = pcl.searchDex(dexPath, userId);
453            if (outcome != DEX_SEARCH_NOT_FOUND) {
454                return new DexSearchResult(pcl.mPackageName, outcome);
455            }
456        }
457
458        // Cache miss. Return not found for the moment.
459        //
460        // TODO(calin): this may be because of a newly installed package, an update
461        // or a new added user. We can either perform a full look up again or register
462        // observers to be notified of package/user updates.
463        return new DexSearchResult(null, DEX_SEARCH_NOT_FOUND);
464    }
465
466    private static <K,V> V putIfAbsent(Map<K,V> map, K key, V newValue) {
467        V existingValue = map.putIfAbsent(key, newValue);
468        return existingValue == null ? newValue : existingValue;
469    }
470
471    /**
472     * Convenience class to store the different locations where a package might
473     * own code.
474     */
475    private static class PackageCodeLocations {
476        private final String mPackageName;
477        private String mBaseCodePath;
478        private final Set<String> mSplitCodePaths;
479        // Maps user id to the application private directory.
480        private final Map<Integer, Set<String>> mAppDataDirs;
481
482        public PackageCodeLocations(ApplicationInfo ai, int userId) {
483            this(ai.packageName, ai.sourceDir, ai.splitSourceDirs);
484            mergeAppDataDirs(ai.dataDir, userId);
485        }
486        public PackageCodeLocations(String packageName, String baseCodePath,
487                String[] splitCodePaths) {
488            mPackageName = packageName;
489            mSplitCodePaths = new HashSet<>();
490            mAppDataDirs = new HashMap<>();
491            updateCodeLocation(baseCodePath, splitCodePaths);
492        }
493
494        public void updateCodeLocation(String baseCodePath, String[] splitCodePaths) {
495            mBaseCodePath = baseCodePath;
496            mSplitCodePaths.clear();
497            if (splitCodePaths != null) {
498                for (String split : splitCodePaths) {
499                    mSplitCodePaths.add(split);
500                }
501            }
502        }
503
504        public void mergeAppDataDirs(String dataDir, int userId) {
505            Set<String> dataDirs = putIfAbsent(mAppDataDirs, userId, new HashSet<>());
506            dataDirs.add(dataDir);
507        }
508
509        public int searchDex(String dexPath, int userId) {
510            // First check that this package is installed or active for the given user.
511            // If we don't have a data dir it means this user is trying to load something
512            // unavailable for them.
513            Set<String> userDataDirs = mAppDataDirs.get(userId);
514            if (userDataDirs == null) {
515                Slog.w(TAG, "Trying to load a dex path which does not exist for the current " +
516                        "user. dexPath=" + dexPath + ", userId=" + userId);
517                return DEX_SEARCH_NOT_FOUND;
518            }
519
520            if (mBaseCodePath.equals(dexPath)) {
521                return DEX_SEARCH_FOUND_PRIMARY;
522            }
523            if (mSplitCodePaths.contains(dexPath)) {
524                return DEX_SEARCH_FOUND_SPLIT;
525            }
526            for (String dataDir : userDataDirs) {
527                if (dexPath.startsWith(dataDir)) {
528                    return DEX_SEARCH_FOUND_SECONDARY;
529                }
530            }
531
532            // TODO(calin): What if we get a symlink? e.g. data dir may be a symlink,
533            // /data/data/ -> /data/user/0/.
534            if (DEBUG) {
535                try {
536                    String dexPathReal = PackageManagerServiceUtils.realpath(new File(dexPath));
537                    if (dexPathReal != dexPath) {
538                        Slog.d(TAG, "Dex loaded with symlink. dexPath=" +
539                                dexPath + " dexPathReal=" + dexPathReal);
540                    }
541                } catch (IOException e) {
542                    // Ignore
543                }
544            }
545            return DEX_SEARCH_NOT_FOUND;
546        }
547    }
548
549    /**
550     * Convenience class to store ownership search results.
551     */
552    private class DexSearchResult {
553        private String mOwningPackageName;
554        private int mOutcome;
555
556        public DexSearchResult(String owningPackageName, int outcome) {
557            this.mOwningPackageName = owningPackageName;
558            this.mOutcome = outcome;
559        }
560
561        @Override
562        public String toString() {
563            return mOwningPackageName + "-" + mOutcome;
564        }
565    }
566
567
568}
569