Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 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 | |
| 17 | package com.android.server.pm.dex; |
| 18 | |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 19 | import static android.provider.DeviceConfig.FsiBoot; |
| 20 | |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 21 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 22 | import static com.android.server.pm.dex.PackageDexUsage.DexUseInfo; |
| 23 | import static com.android.server.pm.dex.PackageDexUsage.PackageUseInfo; |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 24 | |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 26 | import android.content.pm.ApplicationInfo; |
| 27 | import android.content.pm.IPackageManager; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 28 | import android.content.pm.PackageInfo; |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 29 | import android.os.FileUtils; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 30 | import android.os.RemoteException; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 31 | import android.os.SystemProperties; |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 32 | import android.os.UserHandle; |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 33 | import android.os.storage.StorageManager; |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 34 | import android.provider.DeviceConfig; |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 35 | import android.util.Log; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 36 | import android.util.Slog; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 37 | import android.util.jar.StrictJarFile; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 38 | |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 39 | import com.android.internal.annotations.GuardedBy; |
Alan Stokes | b6c3a60 | 2018-11-02 12:10:42 +0000 | [diff] [blame] | 40 | import com.android.internal.annotations.VisibleForTesting; |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 41 | import com.android.server.pm.Installer; |
| 42 | import com.android.server.pm.Installer.InstallerException; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 43 | import com.android.server.pm.PackageDexOptimizer; |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 44 | import com.android.server.pm.PackageManagerService; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 45 | import com.android.server.pm.PackageManagerServiceUtils; |
| 46 | |
| 47 | import java.io.File; |
| 48 | import java.io.IOException; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 49 | import java.util.Arrays; |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 50 | import java.util.Collection; |
| 51 | import java.util.Collections; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 52 | import java.util.HashMap; |
| 53 | import java.util.HashSet; |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 54 | import java.util.Iterator; |
| 55 | import java.util.List; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 56 | import java.util.Map; |
| 57 | import java.util.Set; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 58 | import java.util.zip.ZipEntry; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * This class keeps track of how dex files are used. |
| 62 | * Every time it gets a notification about a dex file being loaded it tracks |
| 63 | * its owning package and records it in PackageDexUsage (package-dex-usage.list). |
| 64 | * |
| 65 | * TODO(calin): Extract related dexopt functionality from PackageManagerService |
| 66 | * into this class. |
| 67 | */ |
| 68 | public class DexManager { |
| 69 | private static final String TAG = "DexManager"; |
| 70 | |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 71 | private static final String PROPERTY_NAME_PM_DEXOPT_PRIV_APPS_OOB = "pm.dexopt.priv-apps-oob"; |
| 72 | private static final String PROPERTY_NAME_PM_DEXOPT_PRIV_APPS_OOB_LIST = |
| 73 | "pm.dexopt.priv-apps-oob-list"; |
| 74 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 75 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 76 | |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 77 | private final Context mContext; |
| 78 | |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 79 | // Maps package name to code locations. |
| 80 | // It caches the code locations for the installed packages. This allows for |
| 81 | // faster lookups (no locks) when finding what package owns the dex file. |
Calin Juravle | 1fade2f | 2017-05-02 18:50:56 -0700 | [diff] [blame] | 82 | @GuardedBy("mPackageCodeLocationsCache") |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 83 | private final Map<String, PackageCodeLocations> mPackageCodeLocationsCache; |
| 84 | |
| 85 | // PackageDexUsage handles the actual I/O operations. It is responsible to |
| 86 | // encode and save the dex usage data. |
| 87 | private final PackageDexUsage mPackageDexUsage; |
| 88 | |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 89 | // DexLogger handles recording of dynamic code loading - which is similar to PackageDexUsage |
| 90 | // but records a different aspect of the data. |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 91 | // (It additionally includes DEX files loaded with unsupported class loaders, and doesn't |
| 92 | // record class loaders or ISAs.) |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 93 | private final DexLogger mDexLogger; |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 94 | |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 95 | private final IPackageManager mPackageManager; |
| 96 | private final PackageDexOptimizer mPackageDexOptimizer; |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 97 | private final Object mInstallLock; |
| 98 | @GuardedBy("mInstallLock") |
| 99 | private final Installer mInstaller; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 100 | |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 101 | // Possible outcomes of a dex search. |
| 102 | private static int DEX_SEARCH_NOT_FOUND = 0; // dex file not found |
| 103 | private static int DEX_SEARCH_FOUND_PRIMARY = 1; // dex file is the primary/base apk |
| 104 | private static int DEX_SEARCH_FOUND_SPLIT = 2; // dex file is a split apk |
| 105 | private static int DEX_SEARCH_FOUND_SECONDARY = 3; // dex file is a secondary dex |
| 106 | |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 107 | /** |
| 108 | * We do not record packages that have no secondary dex files or that are not used by other |
| 109 | * apps. This is an optimization to reduce the amount of data that needs to be written to |
| 110 | * disk (apps will not usually be shared so this trims quite a bit the number we record). |
| 111 | * |
| 112 | * To make this behaviour transparent to the callers which need use information on packages, |
| 113 | * DexManager will return this DEFAULT instance from |
| 114 | * {@link DexManager#getPackageUseInfoOrDefault}. It has no data about secondary dex files and |
| 115 | * is marked as not being used by other apps. This reflects the intended behaviour when we don't |
| 116 | * find the package in the underlying data file. |
| 117 | */ |
| 118 | private final static PackageUseInfo DEFAULT_USE_INFO = new PackageUseInfo(); |
| 119 | |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 120 | public DexManager(Context context, IPackageManager pms, PackageDexOptimizer pdo, |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 121 | Installer installer, Object installLock) { |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 122 | mContext = context; |
| 123 | mPackageCodeLocationsCache = new HashMap<>(); |
| 124 | mPackageDexUsage = new PackageDexUsage(); |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 125 | mPackageManager = pms; |
| 126 | mPackageDexOptimizer = pdo; |
| 127 | mInstaller = installer; |
| 128 | mInstallLock = installLock; |
Alan Stokes | 6087638 | 2019-01-15 15:01:35 +0000 | [diff] [blame] | 129 | mDexLogger = new DexLogger(pms, installer); |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | public DexLogger getDexLogger() { |
| 133 | return mDexLogger; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Notify about dex files loads. |
| 138 | * Note that this method is invoked when apps load dex files and it should |
| 139 | * return as fast as possible. |
| 140 | * |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 141 | * @param loadingAppInfo the package performing the load |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 142 | * @param classLoadersNames the names of the class loaders present in the loading chain. The |
| 143 | * list encodes the class loader chain in the natural order. The first class loader has |
| 144 | * the second one as its parent and so on. The dex files present in the class path of the |
| 145 | * first class loader will be recorded in the usage file. |
| 146 | * @param classPaths the class paths corresponding to the class loaders names from |
| 147 | * {@param classLoadersNames}. The the first element corresponds to the first class loader |
| 148 | * and so on. A classpath is represented as a list of dex files separated by |
Alan Stokes | b6c3a60 | 2018-11-02 12:10:42 +0000 | [diff] [blame] | 149 | * {@code File.pathSeparator}, or null if the class loader's classpath is not known. |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 150 | * The dex files found in the first class path will be recorded in the usage file. |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 151 | * @param loaderIsa the ISA of the app loading the dex files |
| 152 | * @param loaderUserId the user id which runs the code loading the dex files |
| 153 | */ |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 154 | public void notifyDexLoad(ApplicationInfo loadingAppInfo, List<String> classLoadersNames, |
| 155 | List<String> classPaths, String loaderIsa, int loaderUserId) { |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 156 | try { |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 157 | notifyDexLoadInternal(loadingAppInfo, classLoadersNames, classPaths, loaderIsa, |
| 158 | loaderUserId); |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 159 | } catch (Exception e) { |
| 160 | Slog.w(TAG, "Exception while notifying dex load for package " + |
| 161 | loadingAppInfo.packageName, e); |
| 162 | } |
| 163 | } |
| 164 | |
Alan Stokes | b6c3a60 | 2018-11-02 12:10:42 +0000 | [diff] [blame] | 165 | @VisibleForTesting |
| 166 | /*package*/ void notifyDexLoadInternal(ApplicationInfo loadingAppInfo, |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 167 | List<String> classLoaderNames, List<String> classPaths, String loaderIsa, |
| 168 | int loaderUserId) { |
| 169 | if (classLoaderNames.size() != classPaths.size()) { |
| 170 | Slog.wtf(TAG, "Bad call to noitfyDexLoad: args have different size"); |
| 171 | return; |
| 172 | } |
| 173 | if (classLoaderNames.isEmpty()) { |
| 174 | Slog.wtf(TAG, "Bad call to notifyDexLoad: class loaders list is empty"); |
| 175 | return; |
| 176 | } |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 177 | if (!PackageManagerServiceUtils.checkISA(loaderIsa)) { |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 178 | Slog.w(TAG, "Loading dex files " + classPaths + " in unsupported ISA: " + |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 179 | loaderIsa + "?"); |
| 180 | return; |
| 181 | } |
| 182 | |
Alan Stokes | b6c3a60 | 2018-11-02 12:10:42 +0000 | [diff] [blame] | 183 | // The first classpath should never be null because the first classloader |
| 184 | // should always be an instance of BaseDexClassLoader. |
| 185 | String firstClassPath = classPaths.get(0); |
| 186 | if (firstClassPath == null) { |
| 187 | return; |
| 188 | } |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 189 | // The classpath is represented as a list of dex files separated by File.pathSeparator. |
Alan Stokes | b6c3a60 | 2018-11-02 12:10:42 +0000 | [diff] [blame] | 190 | String[] dexPathsToRegister = firstClassPath.split(File.pathSeparator); |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 191 | |
| 192 | // Encode the class loader contexts for the dexPathsToRegister. |
| 193 | String[] classLoaderContexts = DexoptUtils.processContextForDexLoad( |
| 194 | classLoaderNames, classPaths); |
| 195 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 196 | // A null classLoaderContexts means that there are unsupported class loaders in the |
| 197 | // chain. |
| 198 | if (classLoaderContexts == null) { |
| 199 | if (DEBUG) { |
| 200 | Slog.i(TAG, loadingAppInfo.packageName + |
| 201 | " uses unsupported class loader in " + classLoaderNames); |
| 202 | } |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 205 | int dexPathIndex = 0; |
| 206 | for (String dexPath : dexPathsToRegister) { |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 207 | // Find the owning package name. |
| 208 | DexSearchResult searchResult = getDexPackage(loadingAppInfo, dexPath, loaderUserId); |
| 209 | |
| 210 | if (DEBUG) { |
| 211 | Slog.i(TAG, loadingAppInfo.packageName |
| 212 | + " loads from " + searchResult + " : " + loaderUserId + " : " + dexPath); |
| 213 | } |
| 214 | |
| 215 | if (searchResult.mOutcome != DEX_SEARCH_NOT_FOUND) { |
| 216 | // TODO(calin): extend isUsedByOtherApps check to detect the cases where |
| 217 | // different apps share the same runtime. In that case we should not mark the dex |
| 218 | // file as isUsedByOtherApps. Currently this is a safe approximation. |
| 219 | boolean isUsedByOtherApps = !loadingAppInfo.packageName.equals( |
| 220 | searchResult.mOwningPackageName); |
| 221 | boolean primaryOrSplit = searchResult.mOutcome == DEX_SEARCH_FOUND_PRIMARY || |
| 222 | searchResult.mOutcome == DEX_SEARCH_FOUND_SPLIT; |
| 223 | |
| 224 | if (primaryOrSplit && !isUsedByOtherApps) { |
| 225 | // If the dex file is the primary apk (or a split) and not isUsedByOtherApps |
| 226 | // do not record it. This case does not bring any new usable information |
| 227 | // and can be safely skipped. |
| 228 | continue; |
| 229 | } |
| 230 | |
Alan Stokes | e9d3314 | 2018-12-19 09:31:21 +0000 | [diff] [blame] | 231 | mDexLogger.recordDex(loaderUserId, dexPath, searchResult.mOwningPackageName, |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 232 | loadingAppInfo.packageName); |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 233 | |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 234 | if (classLoaderContexts != null) { |
| 235 | |
| 236 | // Record dex file usage. If the current usage is a new pattern (e.g. new |
| 237 | // secondary, or UsedByOtherApps), record will return true and we trigger an |
| 238 | // async write to disk to make sure we don't loose the data in case of a reboot. |
| 239 | |
| 240 | String classLoaderContext = classLoaderContexts[dexPathIndex]; |
| 241 | if (mPackageDexUsage.record(searchResult.mOwningPackageName, |
| 242 | dexPath, loaderUserId, loaderIsa, isUsedByOtherApps, primaryOrSplit, |
| 243 | loadingAppInfo.packageName, classLoaderContext)) { |
| 244 | mPackageDexUsage.maybeWriteAsync(); |
| 245 | } |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 246 | } |
| 247 | } else { |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 248 | // If we can't find the owner of the dex we simply do not track it. The impact is |
| 249 | // that the dex file will not be considered for offline optimizations. |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 250 | if (DEBUG) { |
| 251 | Slog.i(TAG, "Could not find owning package for dex file: " + dexPath); |
| 252 | } |
| 253 | } |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 254 | dexPathIndex++; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Read the dex usage from disk and populate the code cache locations. |
| 260 | * @param existingPackages a map containing information about what packages |
| 261 | * are available to what users. Only packages in this list will be |
| 262 | * recognized during notifyDexLoad(). |
| 263 | */ |
| 264 | public void load(Map<Integer, List<PackageInfo>> existingPackages) { |
| 265 | try { |
| 266 | loadInternal(existingPackages); |
| 267 | } catch (Exception e) { |
| 268 | mPackageDexUsage.clear(); |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 269 | mDexLogger.clear(); |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 270 | Slog.w(TAG, "Exception while loading. Starting with a fresh state.", e); |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 274 | /** |
| 275 | * Notifies that a new package was installed for {@code userId}. |
| 276 | * {@code userId} must not be {@code UserHandle.USER_ALL}. |
| 277 | * |
| 278 | * @throws IllegalArgumentException if {@code userId} is {@code UserHandle.USER_ALL}. |
| 279 | */ |
| 280 | public void notifyPackageInstalled(PackageInfo pi, int userId) { |
| 281 | if (userId == UserHandle.USER_ALL) { |
| 282 | throw new IllegalArgumentException( |
| 283 | "notifyPackageInstalled called with USER_ALL"); |
| 284 | } |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 285 | cachePackageInfo(pi, userId); |
Calin Juravle | 0d4b8f8 | 2017-01-23 23:34:25 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 288 | /** |
| 289 | * Notifies that package {@code packageName} was updated. |
| 290 | * This will clear the UsedByOtherApps mark if it exists. |
| 291 | */ |
| 292 | public void notifyPackageUpdated(String packageName, String baseCodePath, |
| 293 | String[] splitCodePaths) { |
| 294 | cachePackageCodeLocation(packageName, baseCodePath, splitCodePaths, null, /*userId*/ -1); |
| 295 | // In case there was an update, write the package use info to disk async. |
| 296 | // Note that we do the writing here and not in PackageDexUsage in order to be |
| 297 | // consistent with other methods in DexManager (e.g. reconcileSecondaryDexFiles performs |
Calin Juravle | 1fade2f | 2017-05-02 18:50:56 -0700 | [diff] [blame] | 298 | // multiple updates in PackageDexUsage before writing it). |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 299 | if (mPackageDexUsage.clearUsedByOtherApps(packageName)) { |
| 300 | mPackageDexUsage.maybeWriteAsync(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Notifies that the user {@code userId} data for package {@code packageName} |
| 306 | * was destroyed. This will remove all usage info associated with the package |
| 307 | * for the given user. |
| 308 | * {@code userId} is allowed to be {@code UserHandle.USER_ALL} in which case |
| 309 | * all usage information for the package will be removed. |
| 310 | */ |
| 311 | public void notifyPackageDataDestroyed(String packageName, int userId) { |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 312 | // In case there was an update, write the package use info to disk async. |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 313 | // Note that we do the writing here and not in the lower level classes in order to be |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 314 | // consistent with other methods in DexManager (e.g. reconcileSecondaryDexFiles performs |
Calin Juravle | 1fade2f | 2017-05-02 18:50:56 -0700 | [diff] [blame] | 315 | // multiple updates in PackageDexUsage before writing it). |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 316 | if (userId == UserHandle.USER_ALL) { |
| 317 | if (mPackageDexUsage.removePackage(packageName)) { |
| 318 | mPackageDexUsage.maybeWriteAsync(); |
| 319 | } |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 320 | mDexLogger.removePackage(packageName); |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 321 | } else { |
| 322 | if (mPackageDexUsage.removeUserPackage(packageName, userId)) { |
| 323 | mPackageDexUsage.maybeWriteAsync(); |
| 324 | } |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 325 | mDexLogger.removeUserPackage(packageName, userId); |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Caches the code location from the given package info. |
| 331 | */ |
| 332 | private void cachePackageInfo(PackageInfo pi, int userId) { |
| 333 | ApplicationInfo ai = pi.applicationInfo; |
| 334 | String[] dataDirs = new String[] {ai.dataDir, ai.deviceProtectedDataDir, |
| 335 | ai.credentialProtectedDataDir}; |
| 336 | cachePackageCodeLocation(pi.packageName, ai.sourceDir, ai.splitSourceDirs, |
| 337 | dataDirs, userId); |
| 338 | } |
| 339 | |
| 340 | private void cachePackageCodeLocation(String packageName, String baseCodePath, |
| 341 | String[] splitCodePaths, String[] dataDirs, int userId) { |
Calin Juravle | 1fade2f | 2017-05-02 18:50:56 -0700 | [diff] [blame] | 342 | synchronized (mPackageCodeLocationsCache) { |
| 343 | PackageCodeLocations pcl = putIfAbsent(mPackageCodeLocationsCache, packageName, |
| 344 | new PackageCodeLocations(packageName, baseCodePath, splitCodePaths)); |
| 345 | // TODO(calin): We are forced to extend the scope of this synchronization because |
| 346 | // the values of the cache (PackageCodeLocations) are updated in place. |
| 347 | // Make PackageCodeLocations immutable to simplify the synchronization reasoning. |
| 348 | pcl.updateCodeLocation(baseCodePath, splitCodePaths); |
| 349 | if (dataDirs != null) { |
| 350 | for (String dataDir : dataDirs) { |
| 351 | // The set of data dirs includes deviceProtectedDataDir and |
| 352 | // credentialProtectedDataDir which might be null for shared |
| 353 | // libraries. Currently we don't track these but be lenient |
| 354 | // and check in case we ever decide to store their usage data. |
| 355 | if (dataDir != null) { |
| 356 | pcl.mergeAppDataDirs(dataDir, userId); |
| 357 | } |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
Calin Juravle | 0d4b8f8 | 2017-01-23 23:34:25 -0800 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 363 | private void loadInternal(Map<Integer, List<PackageInfo>> existingPackages) { |
| 364 | Map<String, Set<Integer>> packageToUsersMap = new HashMap<>(); |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 365 | Map<String, Set<String>> packageToCodePaths = new HashMap<>(); |
| 366 | |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 367 | // Cache the code locations for the installed packages. This allows for |
| 368 | // faster lookups (no locks) when finding what package owns the dex file. |
| 369 | for (Map.Entry<Integer, List<PackageInfo>> entry : existingPackages.entrySet()) { |
| 370 | List<PackageInfo> packageInfoList = entry.getValue(); |
| 371 | int userId = entry.getKey(); |
| 372 | for (PackageInfo pi : packageInfoList) { |
| 373 | // Cache the code locations. |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 374 | cachePackageInfo(pi, userId); |
Calin Juravle | 0d4b8f8 | 2017-01-23 23:34:25 -0800 | [diff] [blame] | 375 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 376 | // Cache two maps: |
| 377 | // - from package name to the set of user ids who installed the package. |
| 378 | // - from package name to the set of code paths. |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 379 | // We will use it to sync the data and remove obsolete entries from |
| 380 | // mPackageDexUsage. |
| 381 | Set<Integer> users = putIfAbsent( |
| 382 | packageToUsersMap, pi.packageName, new HashSet<>()); |
| 383 | users.add(userId); |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 384 | |
| 385 | Set<String> codePaths = putIfAbsent( |
| 386 | packageToCodePaths, pi.packageName, new HashSet<>()); |
| 387 | codePaths.add(pi.applicationInfo.sourceDir); |
| 388 | if (pi.applicationInfo.splitSourceDirs != null) { |
| 389 | Collections.addAll(codePaths, pi.applicationInfo.splitSourceDirs); |
| 390 | } |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 394 | try { |
| 395 | mPackageDexUsage.read(); |
| 396 | mPackageDexUsage.syncData(packageToUsersMap, packageToCodePaths); |
| 397 | } catch (Exception e) { |
| 398 | mPackageDexUsage.clear(); |
| 399 | Slog.w(TAG, "Exception while loading package dex usage. " |
| 400 | + "Starting with a fresh state.", e); |
| 401 | } |
| 402 | |
| 403 | try { |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 404 | mDexLogger.readAndSync(packageToUsersMap); |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 405 | } catch (Exception e) { |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 406 | mDexLogger.clear(); |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 407 | Slog.w(TAG, "Exception while loading package dynamic code usage. " |
| 408 | + "Starting with a fresh state.", e); |
| 409 | } |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Get the package dex usage for the given package name. |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 414 | * If there is no usage info the method will return a default {@code PackageUseInfo} with |
| 415 | * no data about secondary dex files and marked as not being used by other apps. |
| 416 | * |
| 417 | * Note that no use info means the package was not used or it was used but not by other apps. |
| 418 | * Also, note that right now we might prune packages which are not used by other apps. |
| 419 | * TODO(calin): maybe we should not (prune) so we can have an accurate view when we try |
| 420 | * to access the package use. |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 421 | */ |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 422 | public PackageUseInfo getPackageUseInfoOrDefault(String packageName) { |
| 423 | PackageUseInfo useInfo = mPackageDexUsage.getPackageUseInfo(packageName); |
| 424 | return useInfo == null ? DEFAULT_USE_INFO : useInfo; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Return whether or not the manager has usage information on the give package. |
| 429 | * |
| 430 | * Note that no use info means the package was not used or it was used but not by other apps. |
| 431 | * Also, note that right now we might prune packages which are not used by other apps. |
| 432 | * TODO(calin): maybe we should not (prune) so we can have an accurate view when we try |
| 433 | * to access the package use. |
| 434 | */ |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 435 | @VisibleForTesting |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 436 | /*package*/ boolean hasInfoOnPackage(String packageName) { |
| 437 | return mPackageDexUsage.getPackageUseInfo(packageName) != null; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 441 | * Perform dexopt on with the given {@code options} on the secondary dex files. |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 442 | * @return true if all secondary dex files were processed successfully (compiled or skipped |
| 443 | * because they don't need to be compiled).. |
| 444 | */ |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 445 | public boolean dexoptSecondaryDex(DexoptOptions options) { |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 446 | // Select the dex optimizer based on the force parameter. |
| 447 | // Forced compilation is done through ForcedUpdatePackageDexOptimizer which will adjust |
| 448 | // the necessary dexopt flags to make sure that compilation is not skipped. This avoid |
| 449 | // passing the force flag through the multitude of layers. |
| 450 | // Note: The force option is rarely used (cmdline input for testing, mostly), so it's OK to |
| 451 | // allocate an object here. |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 452 | PackageDexOptimizer pdo = options.isForce() |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 453 | ? new PackageDexOptimizer.ForcedUpdatePackageDexOptimizer(mPackageDexOptimizer) |
| 454 | : mPackageDexOptimizer; |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 455 | String packageName = options.getPackageName(); |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 456 | PackageUseInfo useInfo = getPackageUseInfoOrDefault(packageName); |
Alan Stokes | a002360 | 2017-10-16 12:31:44 +0100 | [diff] [blame] | 457 | if (useInfo.getDexUseInfoMap().isEmpty()) { |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 458 | if (DEBUG) { |
| 459 | Slog.d(TAG, "No secondary dex use for package:" + packageName); |
| 460 | } |
| 461 | // Nothing to compile, return true. |
| 462 | return true; |
| 463 | } |
| 464 | boolean success = true; |
| 465 | for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) { |
| 466 | String dexPath = entry.getKey(); |
| 467 | DexUseInfo dexUseInfo = entry.getValue(); |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 468 | |
| 469 | PackageInfo pkg; |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 470 | try { |
| 471 | pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0, |
| 472 | dexUseInfo.getOwnerUserId()); |
| 473 | } catch (RemoteException e) { |
| 474 | throw new AssertionError(e); |
| 475 | } |
| 476 | // It may be that the package gets uninstalled while we try to compile its |
| 477 | // secondary dex files. If that's the case, just ignore. |
| 478 | // Note that we don't break the entire loop because the package might still be |
| 479 | // installed for other users. |
| 480 | if (pkg == null) { |
| 481 | Slog.d(TAG, "Could not find package when compiling secondary dex " + packageName |
| 482 | + " for user " + dexUseInfo.getOwnerUserId()); |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 483 | mPackageDexUsage.removeUserPackage(packageName, dexUseInfo.getOwnerUserId()); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 484 | continue; |
| 485 | } |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 486 | |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 487 | int result = pdo.dexOptSecondaryDexPath(pkg.applicationInfo, dexPath, |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 488 | dexUseInfo, options); |
Calin Juravle | c22c30e | 2017-01-16 19:18:48 -0800 | [diff] [blame] | 489 | success = success && (result != PackageDexOptimizer.DEX_OPT_FAILED); |
| 490 | } |
| 491 | return success; |
| 492 | } |
| 493 | |
| 494 | /** |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 495 | * Reconcile the information we have about the secondary dex files belonging to |
| 496 | * {@code packagName} and the actual dex files. For all dex files that were |
| 497 | * deleted, update the internal records and delete any generated oat files. |
| 498 | */ |
| 499 | public void reconcileSecondaryDexFiles(String packageName) { |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 500 | PackageUseInfo useInfo = getPackageUseInfoOrDefault(packageName); |
Alan Stokes | a002360 | 2017-10-16 12:31:44 +0100 | [diff] [blame] | 501 | if (useInfo.getDexUseInfoMap().isEmpty()) { |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 502 | if (DEBUG) { |
| 503 | Slog.d(TAG, "No secondary dex use for package:" + packageName); |
| 504 | } |
| 505 | // Nothing to reconcile. |
| 506 | return; |
| 507 | } |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 508 | |
Calin Juravle | b109741 | 2017-01-26 18:53:23 -0800 | [diff] [blame] | 509 | boolean updated = false; |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 510 | for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) { |
| 511 | String dexPath = entry.getKey(); |
| 512 | DexUseInfo dexUseInfo = entry.getValue(); |
| 513 | PackageInfo pkg = null; |
| 514 | try { |
| 515 | // Note that we look for the package in the PackageManager just to be able |
| 516 | // to get back the real app uid and its storage kind. These are only used |
| 517 | // to perform extra validation in installd. |
| 518 | // TODO(calin): maybe a bit overkill. |
| 519 | pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0, |
| 520 | dexUseInfo.getOwnerUserId()); |
| 521 | } catch (RemoteException ignore) { |
| 522 | // Can't happen, DexManager is local. |
| 523 | } |
| 524 | if (pkg == null) { |
| 525 | // It may be that the package was uninstalled while we process the secondary |
| 526 | // dex files. |
| 527 | Slog.d(TAG, "Could not find package when compiling secondary dex " + packageName |
| 528 | + " for user " + dexUseInfo.getOwnerUserId()); |
| 529 | // Update the usage and continue, another user might still have the package. |
Calin Juravle | b109741 | 2017-01-26 18:53:23 -0800 | [diff] [blame] | 530 | updated = mPackageDexUsage.removeUserPackage( |
| 531 | packageName, dexUseInfo.getOwnerUserId()) || updated; |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 532 | continue; |
| 533 | } |
| 534 | ApplicationInfo info = pkg.applicationInfo; |
| 535 | int flags = 0; |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 536 | if (info.deviceProtectedDataDir != null && |
| 537 | FileUtils.contains(info.deviceProtectedDataDir, dexPath)) { |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 538 | flags |= StorageManager.FLAG_STORAGE_DE; |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 539 | } else if (info.credentialProtectedDataDir!= null && |
| 540 | FileUtils.contains(info.credentialProtectedDataDir, dexPath)) { |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 541 | flags |= StorageManager.FLAG_STORAGE_CE; |
| 542 | } else { |
Calin Juravle | adbadd5 | 2017-03-28 18:19:15 -0700 | [diff] [blame] | 543 | Slog.e(TAG, "Could not infer CE/DE storage for path " + dexPath); |
| 544 | updated = mPackageDexUsage.removeDexFile( |
| 545 | packageName, dexPath, dexUseInfo.getOwnerUserId()) || updated; |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 546 | continue; |
| 547 | } |
| 548 | |
| 549 | boolean dexStillExists = true; |
| 550 | synchronized(mInstallLock) { |
| 551 | try { |
| 552 | String[] isas = dexUseInfo.getLoaderIsas().toArray(new String[0]); |
| 553 | dexStillExists = mInstaller.reconcileSecondaryDexFile(dexPath, packageName, |
Alan Stokes | a002360 | 2017-10-16 12:31:44 +0100 | [diff] [blame] | 554 | info.uid, isas, info.volumeUuid, flags); |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 555 | } catch (InstallerException e) { |
| 556 | Slog.e(TAG, "Got InstallerException when reconciling dex " + dexPath + |
| 557 | " : " + e.getMessage()); |
| 558 | } |
| 559 | } |
| 560 | if (!dexStillExists) { |
Calin Juravle | b109741 | 2017-01-26 18:53:23 -0800 | [diff] [blame] | 561 | updated = mPackageDexUsage.removeDexFile( |
| 562 | packageName, dexPath, dexUseInfo.getOwnerUserId()) || updated; |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 563 | } |
Calin Juravle | b109741 | 2017-01-26 18:53:23 -0800 | [diff] [blame] | 564 | |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 565 | } |
Calin Juravle | b109741 | 2017-01-26 18:53:23 -0800 | [diff] [blame] | 566 | if (updated) { |
| 567 | mPackageDexUsage.maybeWriteAsync(); |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 571 | // TODO(calin): questionable API in the presence of class loaders context. Needs amends as the |
| 572 | // compilation happening here will use a pessimistic context. |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 573 | public RegisterDexModuleResult registerDexModule(ApplicationInfo info, String dexPath, |
| 574 | boolean isUsedByOtherApps, int userId) { |
| 575 | // Find the owning package record. |
| 576 | DexSearchResult searchResult = getDexPackage(info, dexPath, userId); |
| 577 | |
| 578 | if (searchResult.mOutcome == DEX_SEARCH_NOT_FOUND) { |
| 579 | return new RegisterDexModuleResult(false, "Package not found"); |
| 580 | } |
| 581 | if (!info.packageName.equals(searchResult.mOwningPackageName)) { |
| 582 | return new RegisterDexModuleResult(false, "Dex path does not belong to package"); |
| 583 | } |
| 584 | if (searchResult.mOutcome == DEX_SEARCH_FOUND_PRIMARY || |
| 585 | searchResult.mOutcome == DEX_SEARCH_FOUND_SPLIT) { |
| 586 | return new RegisterDexModuleResult(false, "Main apks cannot be registered"); |
| 587 | } |
| 588 | |
| 589 | // We found the package. Now record the usage for all declared ISAs. |
| 590 | boolean update = false; |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 591 | for (String isa : getAppDexInstructionSets(info)) { |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 592 | boolean newUpdate = mPackageDexUsage.record(searchResult.mOwningPackageName, |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 593 | dexPath, userId, isa, isUsedByOtherApps, /*primaryOrSplit*/ false, |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 594 | searchResult.mOwningPackageName, |
| 595 | PackageDexUsage.UNKNOWN_CLASS_LOADER_CONTEXT); |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 596 | update |= newUpdate; |
| 597 | } |
| 598 | if (update) { |
| 599 | mPackageDexUsage.maybeWriteAsync(); |
| 600 | } |
| 601 | |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 602 | DexUseInfo dexUseInfo = mPackageDexUsage.getPackageUseInfo(searchResult.mOwningPackageName) |
| 603 | .getDexUseInfoMap().get(dexPath); |
| 604 | |
Calin Juravle | 4bc8f4d | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 605 | // Try to optimize the package according to the install reason. |
| 606 | DexoptOptions options = new DexoptOptions(info.packageName, |
| 607 | PackageManagerService.REASON_INSTALL, /*flags*/0); |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 608 | |
| 609 | int result = mPackageDexOptimizer.dexOptSecondaryDexPath(info, dexPath, dexUseInfo, |
| 610 | options); |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 611 | |
| 612 | // If we fail to optimize the package log an error but don't propagate the error |
| 613 | // back to the app. The app cannot do much about it and the background job |
| 614 | // will rety again when it executes. |
| 615 | // TODO(calin): there might be some value to return the error here but it may |
| 616 | // cause red herrings since that doesn't mean the app cannot use the module. |
| 617 | if (result != PackageDexOptimizer.DEX_OPT_FAILED) { |
| 618 | Slog.e(TAG, "Failed to optimize dex module " + dexPath); |
| 619 | } |
| 620 | return new RegisterDexModuleResult(true, "Dex module registered successfully"); |
| 621 | } |
| 622 | |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 623 | /** |
Calin Juravle | 51f521c | 2017-01-25 18:00:05 -0800 | [diff] [blame] | 624 | * Return all packages that contain records of secondary dex files. |
| 625 | */ |
| 626 | public Set<String> getAllPackagesWithSecondaryDexFiles() { |
| 627 | return mPackageDexUsage.getAllPackagesWithSecondaryDexFiles(); |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 631 | * Retrieves the package which owns the given dexPath. |
| 632 | */ |
| 633 | private DexSearchResult getDexPackage( |
| 634 | ApplicationInfo loadingAppInfo, String dexPath, int userId) { |
| 635 | // Ignore framework code. |
| 636 | // TODO(calin): is there a better way to detect it? |
| 637 | if (dexPath.startsWith("/system/framework/")) { |
Calin Juravle | 2dfc1b3 | 2017-03-10 18:24:33 -0800 | [diff] [blame] | 638 | return new DexSearchResult("framework", DEX_SEARCH_NOT_FOUND); |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // First, check if the package which loads the dex file actually owns it. |
| 642 | // Most of the time this will be true and we can return early. |
| 643 | PackageCodeLocations loadingPackageCodeLocations = |
| 644 | new PackageCodeLocations(loadingAppInfo, userId); |
| 645 | int outcome = loadingPackageCodeLocations.searchDex(dexPath, userId); |
| 646 | if (outcome != DEX_SEARCH_NOT_FOUND) { |
| 647 | // TODO(calin): evaluate if we bother to detect symlinks at the dexPath level. |
| 648 | return new DexSearchResult(loadingPackageCodeLocations.mPackageName, outcome); |
| 649 | } |
| 650 | |
| 651 | // The loadingPackage does not own the dex file. |
| 652 | // Perform a reverse look-up in the cache to detect if any package has ownership. |
| 653 | // Note that we can have false negatives if the cache falls out of date. |
Calin Juravle | 1fade2f | 2017-05-02 18:50:56 -0700 | [diff] [blame] | 654 | synchronized (mPackageCodeLocationsCache) { |
| 655 | for (PackageCodeLocations pcl : mPackageCodeLocationsCache.values()) { |
| 656 | outcome = pcl.searchDex(dexPath, userId); |
| 657 | if (outcome != DEX_SEARCH_NOT_FOUND) { |
| 658 | return new DexSearchResult(pcl.mPackageName, outcome); |
| 659 | } |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
Calin Juravle | 6304ec3 | 2017-03-20 23:46:44 -0700 | [diff] [blame] | 663 | if (DEBUG) { |
| 664 | // TODO(calin): Consider checking for /data/data symlink. |
| 665 | // /data/data/ symlinks /data/user/0/ and there's nothing stopping apps |
| 666 | // to load dex files through it. |
| 667 | try { |
| 668 | String dexPathReal = PackageManagerServiceUtils.realpath(new File(dexPath)); |
Alan Stokes | 6778291 | 2018-11-27 10:45:38 +0000 | [diff] [blame] | 669 | if (!dexPath.equals(dexPathReal)) { |
Calin Juravle | 6304ec3 | 2017-03-20 23:46:44 -0700 | [diff] [blame] | 670 | Slog.d(TAG, "Dex loaded with symlink. dexPath=" + |
| 671 | dexPath + " dexPathReal=" + dexPathReal); |
| 672 | } |
| 673 | } catch (IOException e) { |
| 674 | // Ignore |
| 675 | } |
| 676 | } |
| 677 | // Cache miss. The cache is updated during installs and uninstalls, |
| 678 | // so if we get here we're pretty sure the dex path does not exist. |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 679 | return new DexSearchResult(null, DEX_SEARCH_NOT_FOUND); |
| 680 | } |
| 681 | |
| 682 | private static <K,V> V putIfAbsent(Map<K,V> map, K key, V newValue) { |
| 683 | V existingValue = map.putIfAbsent(key, newValue); |
| 684 | return existingValue == null ? newValue : existingValue; |
| 685 | } |
| 686 | |
Calin Juravle | 14876bd | 2017-07-28 16:13:35 -0700 | [diff] [blame] | 687 | /** |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 688 | * Writes the in-memory package dex usage to disk right away. |
Calin Juravle | 14876bd | 2017-07-28 16:13:35 -0700 | [diff] [blame] | 689 | */ |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 690 | public void writePackageDexUsageNow() { |
Calin Juravle | 14876bd | 2017-07-28 16:13:35 -0700 | [diff] [blame] | 691 | mPackageDexUsage.writeNow(); |
Alan Stokes | be9ec97 | 2018-12-10 14:07:47 +0000 | [diff] [blame] | 692 | mDexLogger.writeNow(); |
Calin Juravle | 14876bd | 2017-07-28 16:13:35 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 695 | /** |
| 696 | * Returns whether the given package is in the list of privilaged apps that should run out of |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 697 | * box. This only makes sense if the feature is enabled. Note that when the the OOB list is |
| 698 | * empty, all priv apps will run in OOB mode. |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 699 | */ |
| 700 | public static boolean isPackageSelectedToRunOob(String packageName) { |
| 701 | return isPackageSelectedToRunOob(Arrays.asList(packageName)); |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Returns whether any of the given packages are in the list of privilaged apps that should run |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 706 | * out of box. This only makes sense if the feature is enabled. Note that when the the OOB list |
| 707 | * is empty, all priv apps will run in OOB mode. |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 708 | */ |
| 709 | public static boolean isPackageSelectedToRunOob(Collection<String> packageNamesInSameProcess) { |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 710 | return isPackageSelectedToRunOobInternal( |
| 711 | SystemProperties.getBoolean(PROPERTY_NAME_PM_DEXOPT_PRIV_APPS_OOB, false), |
| 712 | SystemProperties.get(PROPERTY_NAME_PM_DEXOPT_PRIV_APPS_OOB_LIST, "ALL"), |
| 713 | DeviceConfig.getProperty(FsiBoot.NAMESPACE, FsiBoot.OOB_ENABLED), |
| 714 | DeviceConfig.getProperty(FsiBoot.NAMESPACE, FsiBoot.OOB_WHITELIST), |
| 715 | packageNamesInSameProcess); |
| 716 | } |
| 717 | |
| 718 | @VisibleForTesting |
| 719 | /* package */ static boolean isPackageSelectedToRunOobInternal( |
| 720 | boolean isDefaultEnabled, String defaultWhitelist, String overrideEnabled, |
| 721 | String overrideWhitelist, Collection<String> packageNamesInSameProcess) { |
| 722 | // Allow experiment (if exists) to override device configuration. |
| 723 | boolean enabled = overrideEnabled != null ? overrideEnabled.equals("true") |
| 724 | : isDefaultEnabled; |
| 725 | if (!enabled) { |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 726 | return false; |
| 727 | } |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 728 | |
| 729 | // Similarly, experiment flag can override the whitelist. |
| 730 | String whitelist = overrideWhitelist != null ? overrideWhitelist : defaultWhitelist; |
| 731 | if ("ALL".equals(whitelist)) { |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 732 | return true; |
| 733 | } |
Victor Hsieh | 293595f | 2019-01-10 09:33:21 -0800 | [diff] [blame] | 734 | for (String oobPkgName : whitelist.split(",")) { |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 735 | if (packageNamesInSameProcess.contains(oobPkgName)) { |
| 736 | return true; |
| 737 | } |
| 738 | } |
| 739 | return false; |
| 740 | } |
| 741 | |
| 742 | /** |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame^] | 743 | * Generates log if the archive located at {@code fileName} has uncompressed dex file that can |
| 744 | * be direclty mapped. |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 745 | */ |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame^] | 746 | public static boolean auditUncompressedDexInApk(String fileName) { |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 747 | StrictJarFile jarFile = null; |
| 748 | try { |
| 749 | jarFile = new StrictJarFile(fileName, |
| 750 | false /*verify*/, false /*signatureSchemeRollbackProtectionsEnforced*/); |
| 751 | Iterator<ZipEntry> it = jarFile.iterator(); |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 752 | boolean allCorrect = true; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 753 | while (it.hasNext()) { |
| 754 | ZipEntry entry = it.next(); |
| 755 | if (entry.getName().endsWith(".dex")) { |
| 756 | if (entry.getMethod() != ZipEntry.STORED) { |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 757 | allCorrect = false; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 758 | Slog.w(TAG, "APK " + fileName + " has compressed dex code " + |
| 759 | entry.getName()); |
| 760 | } else if ((entry.getDataOffset() & 0x3) != 0) { |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 761 | allCorrect = false; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 762 | Slog.w(TAG, "APK " + fileName + " has unaligned dex code " + |
| 763 | entry.getName()); |
| 764 | } |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 765 | } |
| 766 | } |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 767 | return allCorrect; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 768 | } catch (IOException ignore) { |
| 769 | Slog.wtf(TAG, "Error when parsing APK " + fileName); |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 770 | return false; |
Victor Hsieh | 785d618 | 2018-04-19 14:26:28 -0700 | [diff] [blame] | 771 | } finally { |
| 772 | try { |
| 773 | if (jarFile != null) { |
| 774 | jarFile.close(); |
| 775 | } |
| 776 | } catch (IOException ignore) {} |
| 777 | } |
| 778 | } |
| 779 | |
Calin Juravle | 3d2af7f | 2017-04-19 19:56:21 -0700 | [diff] [blame] | 780 | public static class RegisterDexModuleResult { |
| 781 | public RegisterDexModuleResult() { |
| 782 | this(false, null); |
| 783 | } |
| 784 | |
| 785 | public RegisterDexModuleResult(boolean success, String message) { |
| 786 | this.success = success; |
| 787 | this.message = message; |
| 788 | } |
| 789 | |
| 790 | public final boolean success; |
| 791 | public final String message; |
| 792 | } |
| 793 | |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 794 | /** |
| 795 | * Convenience class to store the different locations where a package might |
| 796 | * own code. |
| 797 | */ |
| 798 | private static class PackageCodeLocations { |
| 799 | private final String mPackageName; |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 800 | private String mBaseCodePath; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 801 | private final Set<String> mSplitCodePaths; |
| 802 | // Maps user id to the application private directory. |
| 803 | private final Map<Integer, Set<String>> mAppDataDirs; |
| 804 | |
| 805 | public PackageCodeLocations(ApplicationInfo ai, int userId) { |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 806 | this(ai.packageName, ai.sourceDir, ai.splitSourceDirs); |
| 807 | mergeAppDataDirs(ai.dataDir, userId); |
| 808 | } |
| 809 | public PackageCodeLocations(String packageName, String baseCodePath, |
| 810 | String[] splitCodePaths) { |
| 811 | mPackageName = packageName; |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 812 | mSplitCodePaths = new HashSet<>(); |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 813 | mAppDataDirs = new HashMap<>(); |
| 814 | updateCodeLocation(baseCodePath, splitCodePaths); |
| 815 | } |
| 816 | |
| 817 | public void updateCodeLocation(String baseCodePath, String[] splitCodePaths) { |
| 818 | mBaseCodePath = baseCodePath; |
| 819 | mSplitCodePaths.clear(); |
| 820 | if (splitCodePaths != null) { |
| 821 | for (String split : splitCodePaths) { |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 822 | mSplitCodePaths.add(split); |
| 823 | } |
| 824 | } |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 827 | public void mergeAppDataDirs(String dataDir, int userId) { |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 828 | Set<String> dataDirs = putIfAbsent(mAppDataDirs, userId, new HashSet<>()); |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 829 | dataDirs.add(dataDir); |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | public int searchDex(String dexPath, int userId) { |
| 833 | // First check that this package is installed or active for the given user. |
Calin Juravle | 6304ec3 | 2017-03-20 23:46:44 -0700 | [diff] [blame] | 834 | // A missing data dir means the package is not installed. |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 835 | Set<String> userDataDirs = mAppDataDirs.get(userId); |
| 836 | if (userDataDirs == null) { |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 837 | return DEX_SEARCH_NOT_FOUND; |
| 838 | } |
| 839 | |
| 840 | if (mBaseCodePath.equals(dexPath)) { |
| 841 | return DEX_SEARCH_FOUND_PRIMARY; |
| 842 | } |
| 843 | if (mSplitCodePaths.contains(dexPath)) { |
| 844 | return DEX_SEARCH_FOUND_SPLIT; |
| 845 | } |
| 846 | for (String dataDir : userDataDirs) { |
| 847 | if (dexPath.startsWith(dataDir)) { |
| 848 | return DEX_SEARCH_FOUND_SECONDARY; |
| 849 | } |
| 850 | } |
Calin Juravle | c066205 | 2016-12-22 18:47:05 +0200 | [diff] [blame] | 851 | |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 852 | return DEX_SEARCH_NOT_FOUND; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * Convenience class to store ownership search results. |
| 858 | */ |
| 859 | private class DexSearchResult { |
| 860 | private String mOwningPackageName; |
| 861 | private int mOutcome; |
| 862 | |
| 863 | public DexSearchResult(String owningPackageName, int outcome) { |
| 864 | this.mOwningPackageName = owningPackageName; |
| 865 | this.mOutcome = outcome; |
| 866 | } |
| 867 | |
| 868 | @Override |
| 869 | public String toString() { |
| 870 | return mOwningPackageName + "-" + mOutcome; |
| 871 | } |
| 872 | } |
Calin Juravle | b8976d8 | 2016-12-16 16:22:00 +0000 | [diff] [blame] | 873 | } |