Calin Juravle | 0318162 | 2016-12-01 17:53:07 +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 | |
| 19 | import android.util.AtomicFile; |
| 20 | import android.util.Slog; |
| 21 | import android.os.Build; |
| 22 | |
| 23 | import com.android.internal.annotations.GuardedBy; |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 24 | import com.android.internal.annotations.VisibleForTesting; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 25 | import com.android.internal.util.FastPrintWriter; |
| 26 | import com.android.server.pm.AbstractStatsBase; |
| 27 | import com.android.server.pm.PackageManagerServiceUtils; |
| 28 | |
| 29 | import java.io.BufferedReader; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 30 | import java.io.FileNotFoundException; |
| 31 | import java.io.FileOutputStream; |
| 32 | import java.io.InputStreamReader; |
| 33 | import java.io.IOException; |
| 34 | import java.io.OutputStreamWriter; |
| 35 | import java.io.Reader; |
| 36 | import java.io.StringWriter; |
| 37 | import java.io.Writer; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 38 | import java.util.Arrays; |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 39 | import java.util.Collection; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 40 | import java.util.Collections; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 41 | import java.util.Iterator; |
| 42 | import java.util.HashMap; |
| 43 | import java.util.HashSet; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 44 | import java.util.List; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 45 | import java.util.Map; |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 46 | import java.util.Objects; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 47 | import java.util.Set; |
| 48 | |
| 49 | import dalvik.system.VMRuntime; |
| 50 | import libcore.io.IoUtils; |
| 51 | |
| 52 | /** |
| 53 | * Stat file which store usage information about dex files. |
| 54 | */ |
| 55 | public class PackageDexUsage extends AbstractStatsBase<Void> { |
| 56 | private final static String TAG = "PackageDexUsage"; |
| 57 | |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 58 | // We support previous version to ensure that the usage list remains valid cross OTAs. |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 59 | private final static int PACKAGE_DEX_USAGE_SUPPORTED_VERSION_1 = 1; |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 60 | // Version 2 added: |
| 61 | // - the list of packages that load the dex files |
| 62 | // - class loader contexts for secondary dex files |
| 63 | // - usage for all code paths (including splits) |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 64 | private final static int PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2 = 2; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 65 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 66 | private final static int PACKAGE_DEX_USAGE_VERSION = PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2; |
| 67 | |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 68 | private final static String PACKAGE_DEX_USAGE_VERSION_HEADER = |
| 69 | "PACKAGE_MANAGER__PACKAGE_DEX_USAGE__"; |
| 70 | |
| 71 | private final static String SPLIT_CHAR = ","; |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 72 | private final static String CODE_PATH_LINE_CHAR = "+"; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 73 | private final static String DEX_LINE_CHAR = "#"; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 74 | private final static String LOADING_PACKAGE_CHAR = "@"; |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 75 | |
| 76 | // One of the things we record about dex files is the class loader context that was used to |
| 77 | // load them. That should be stable but if it changes we don't keep track of variable contexts. |
| 78 | // Instead we put a special marker in the dex usage file in order to recognize the case and |
| 79 | // skip optimizations on that dex files. |
| 80 | /*package*/ static final String VARIABLE_CLASS_LOADER_CONTEXT = |
| 81 | "=VariableClassLoaderContext="; |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 82 | // The markers used for unknown class loader contexts. This can happen if the dex file was |
| 83 | // recorded in a previous version and we didn't have a chance to update its usage. |
| 84 | /*package*/ static final String UNKNOWN_CLASS_LOADER_CONTEXT = |
| 85 | "=UnknownClassLoaderContext="; |
| 86 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 87 | // The marker used for unsupported class loader contexts (no longer written, may occur in old |
| 88 | // files so discarded on read). |
| 89 | private static final String UNSUPPORTED_CLASS_LOADER_CONTEXT = |
| 90 | "=UnsupportedClassLoaderContext="; |
| 91 | |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 92 | // Map which structures the information we have on a package. |
| 93 | // Maps package name to package data (which stores info about UsedByOtherApps and |
| 94 | // secondary dex files.). |
| 95 | // Access to this map needs synchronized. |
| 96 | @GuardedBy("mPackageUseInfoMap") |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 97 | private final Map<String, PackageUseInfo> mPackageUseInfoMap; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 98 | |
Alan Stokes | 58aea51 | 2018-09-21 17:26:43 +0100 | [diff] [blame] | 99 | /* package */ PackageDexUsage() { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 100 | super("package-dex-usage.list", "PackageDexUsage_DiskWriter", /*lock*/ false); |
| 101 | mPackageUseInfoMap = new HashMap<>(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Record a dex file load. |
| 106 | * |
| 107 | * Note this is called when apps load dex files and as such it should return |
| 108 | * as fast as possible. |
| 109 | * |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 110 | * @param owningPackageName the package owning the dex path |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 111 | * @param dexPath the path of the dex files being loaded |
| 112 | * @param ownerUserId the user id which runs the code loading the dex files |
| 113 | * @param loaderIsa the ISA of the app loading the dex files |
| 114 | * @param isUsedByOtherApps whether or not this dex file was not loaded by its owning package |
| 115 | * @param primaryOrSplit whether or not the dex file is a primary/split dex. True indicates |
| 116 | * the file is either primary or a split. False indicates the file is secondary dex. |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 117 | * @param loadingPackageName the package performing the load. Recorded only if it is different |
| 118 | * than {@param owningPackageName}. |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 119 | * @return true if the dex load constitutes new information, or false if this information |
| 120 | * has been seen before. |
| 121 | */ |
Alan Stokes | 58aea51 | 2018-09-21 17:26:43 +0100 | [diff] [blame] | 122 | /* package */ boolean record(String owningPackageName, String dexPath, int ownerUserId, |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 123 | String loaderIsa, boolean isUsedByOtherApps, boolean primaryOrSplit, |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 124 | String loadingPackageName, String classLoaderContext) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 125 | if (!PackageManagerServiceUtils.checkISA(loaderIsa)) { |
| 126 | throw new IllegalArgumentException("loaderIsa " + loaderIsa + " is unsupported"); |
| 127 | } |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 128 | if (classLoaderContext == null) { |
| 129 | throw new IllegalArgumentException("Null classLoaderContext"); |
| 130 | } |
| 131 | |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 132 | synchronized (mPackageUseInfoMap) { |
| 133 | PackageUseInfo packageUseInfo = mPackageUseInfoMap.get(owningPackageName); |
| 134 | if (packageUseInfo == null) { |
| 135 | // This is the first time we see the package. |
| 136 | packageUseInfo = new PackageUseInfo(); |
| 137 | if (primaryOrSplit) { |
| 138 | // If we have a primary or a split apk, set isUsedByOtherApps. |
| 139 | // We do not need to record the loaderIsa or the owner because we compile |
| 140 | // primaries for all users and all ISAs. |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 141 | packageUseInfo.mergeCodePathUsedByOtherApps(dexPath, isUsedByOtherApps, |
| 142 | owningPackageName, loadingPackageName); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 143 | } else { |
| 144 | // For secondary dex files record the loaderISA and the owner. We'll need |
| 145 | // to know under which user to compile and for what ISA. |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 146 | DexUseInfo newData = new DexUseInfo(isUsedByOtherApps, ownerUserId, |
| 147 | classLoaderContext, loaderIsa); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 148 | packageUseInfo.mDexUseInfoMap.put(dexPath, newData); |
| 149 | maybeAddLoadingPackage(owningPackageName, loadingPackageName, |
| 150 | newData.mLoadingPackages); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 151 | } |
| 152 | mPackageUseInfoMap.put(owningPackageName, packageUseInfo); |
| 153 | return true; |
| 154 | } else { |
| 155 | // We already have data on this package. Amend it. |
| 156 | if (primaryOrSplit) { |
| 157 | // We have a possible update on the primary apk usage. Merge |
| 158 | // isUsedByOtherApps information and return if there was an update. |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 159 | return packageUseInfo.mergeCodePathUsedByOtherApps( |
| 160 | dexPath, isUsedByOtherApps, owningPackageName, loadingPackageName); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 161 | } else { |
| 162 | DexUseInfo newData = new DexUseInfo( |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 163 | isUsedByOtherApps, ownerUserId, classLoaderContext, loaderIsa); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 164 | boolean updateLoadingPackages = maybeAddLoadingPackage(owningPackageName, |
| 165 | loadingPackageName, newData.mLoadingPackages); |
| 166 | |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 167 | DexUseInfo existingData = packageUseInfo.mDexUseInfoMap.get(dexPath); |
| 168 | if (existingData == null) { |
| 169 | // It's the first time we see this dex file. |
| 170 | packageUseInfo.mDexUseInfoMap.put(dexPath, newData); |
| 171 | return true; |
| 172 | } else { |
| 173 | if (ownerUserId != existingData.mOwnerUserId) { |
| 174 | // Oups, this should never happen, the DexManager who calls this should |
| 175 | // do the proper checks and not call record if the user does not own the |
| 176 | // dex path. |
| 177 | // Secondary dex files are stored in the app user directory. A change in |
| 178 | // owningUser for the same path means that something went wrong at some |
| 179 | // higher level, and the loaderUser was allowed to cross |
| 180 | // user-boundaries and access data from what we know to be the owner |
| 181 | // user. |
| 182 | throw new IllegalArgumentException("Trying to change ownerUserId for " |
| 183 | + " dex path " + dexPath + " from " + existingData.mOwnerUserId |
| 184 | + " to " + ownerUserId); |
| 185 | } |
| 186 | // Merge the information into the existing data. |
| 187 | // Returns true if there was an update. |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 188 | return existingData.merge(newData) || updateLoadingPackages; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Convenience method for sync reads which does not force the user to pass a useless |
| 197 | * (Void) null. |
| 198 | */ |
Alan Stokes | 58aea51 | 2018-09-21 17:26:43 +0100 | [diff] [blame] | 199 | /* package */ void read() { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 200 | read((Void) null); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Convenience method for async writes which does not force the user to pass a useless |
| 205 | * (Void) null. |
| 206 | */ |
Calin Juravle | 14876bd | 2017-07-28 16:13:35 -0700 | [diff] [blame] | 207 | /*package*/ void maybeWriteAsync() { |
| 208 | maybeWriteAsync(null); |
| 209 | } |
| 210 | |
| 211 | /*package*/ void writeNow() { |
| 212 | writeInternal(null); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | protected void writeInternal(Void data) { |
| 217 | AtomicFile file = getFile(); |
| 218 | FileOutputStream f = null; |
| 219 | |
| 220 | try { |
| 221 | f = file.startWrite(); |
| 222 | OutputStreamWriter osw = new OutputStreamWriter(f); |
| 223 | write(osw); |
| 224 | osw.flush(); |
| 225 | file.finishWrite(f); |
| 226 | } catch (IOException e) { |
| 227 | if (f != null) { |
| 228 | file.failWrite(f); |
| 229 | } |
| 230 | Slog.e(TAG, "Failed to write usage for dex files", e); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * File format: |
| 236 | * |
| 237 | * file_magic_version |
| 238 | * package_name_1 |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 239 | * +code_path1 |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 240 | * @ loading_package_1_1, loading_package_1_2... |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 241 | * +code_path2 |
| 242 | * @ loading_package_2_1, loading_package_2_2... |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 243 | * #dex_file_path_1_1 |
| 244 | * user_1_1, used_by_other_app_1_1, user_isa_1_1_1, user_isa_1_1_2 |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 245 | * @ loading_package_1_1_1, loading_package_1_1_2... |
| 246 | * class_loader_context_1_1 |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 247 | * #dex_file_path_1_2 |
| 248 | * user_1_2, used_by_other_app_1_2, user_isa_1_2_1, user_isa_1_2_2 |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 249 | * @ loading_package_1_2_1, loading_package_1_2_2... |
| 250 | * class_loader_context_1_2 |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 251 | * ... |
| 252 | */ |
| 253 | /* package */ void write(Writer out) { |
| 254 | // Make a clone to avoid locking while writing to disk. |
| 255 | Map<String, PackageUseInfo> packageUseInfoMapClone = clonePackageUseInfoMap(); |
| 256 | |
| 257 | FastPrintWriter fpw = new FastPrintWriter(out); |
| 258 | |
| 259 | // Write the header. |
| 260 | fpw.print(PACKAGE_DEX_USAGE_VERSION_HEADER); |
| 261 | fpw.println(PACKAGE_DEX_USAGE_VERSION); |
| 262 | |
| 263 | for (Map.Entry<String, PackageUseInfo> pEntry : packageUseInfoMapClone.entrySet()) { |
| 264 | // Write the package line. |
| 265 | String packageName = pEntry.getKey(); |
| 266 | PackageUseInfo packageUseInfo = pEntry.getValue(); |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 267 | fpw.println(packageName); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 268 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 269 | // Write the code paths used by other apps. |
| 270 | for (Map.Entry<String, Set<String>> codeEntry : |
| 271 | packageUseInfo.mCodePathsUsedByOtherApps.entrySet()) { |
| 272 | String codePath = codeEntry.getKey(); |
| 273 | Set<String> loadingPackages = codeEntry.getValue(); |
| 274 | fpw.println(CODE_PATH_LINE_CHAR + codePath); |
| 275 | fpw.println(LOADING_PACKAGE_CHAR + String.join(SPLIT_CHAR, loadingPackages)); |
| 276 | } |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 277 | |
| 278 | // Write dex file lines. |
| 279 | for (Map.Entry<String, DexUseInfo> dEntry : packageUseInfo.mDexUseInfoMap.entrySet()) { |
| 280 | String dexPath = dEntry.getKey(); |
| 281 | DexUseInfo dexUseInfo = dEntry.getValue(); |
| 282 | fpw.println(DEX_LINE_CHAR + dexPath); |
| 283 | fpw.print(String.join(SPLIT_CHAR, Integer.toString(dexUseInfo.mOwnerUserId), |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 284 | writeBoolean(dexUseInfo.mIsUsedByOtherApps))); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 285 | for (String isa : dexUseInfo.mLoaderIsas) { |
| 286 | fpw.print(SPLIT_CHAR + isa); |
| 287 | } |
| 288 | fpw.println(); |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 289 | fpw.println(LOADING_PACKAGE_CHAR |
| 290 | + String.join(SPLIT_CHAR, dexUseInfo.mLoadingPackages)); |
| 291 | fpw.println(dexUseInfo.getClassLoaderContext()); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | fpw.flush(); |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | protected void readInternal(Void data) { |
| 299 | AtomicFile file = getFile(); |
| 300 | BufferedReader in = null; |
| 301 | try { |
| 302 | in = new BufferedReader(new InputStreamReader(file.openRead())); |
| 303 | read(in); |
| 304 | } catch (FileNotFoundException expected) { |
| 305 | // The file may not be there. E.g. When we first take the OTA with this feature. |
| 306 | } catch (IOException e) { |
| 307 | Slog.w(TAG, "Failed to parse package dex usage.", e); |
| 308 | } finally { |
| 309 | IoUtils.closeQuietly(in); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /* package */ void read(Reader reader) throws IOException { |
| 314 | Map<String, PackageUseInfo> data = new HashMap<>(); |
| 315 | BufferedReader in = new BufferedReader(reader); |
| 316 | // Read header, do version check. |
| 317 | String versionLine = in.readLine(); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 318 | int version; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 319 | if (versionLine == null) { |
| 320 | throw new IllegalStateException("No version line found."); |
| 321 | } else { |
| 322 | if (!versionLine.startsWith(PACKAGE_DEX_USAGE_VERSION_HEADER)) { |
| 323 | // TODO(calin): the caller is responsible to clear the file. |
| 324 | throw new IllegalStateException("Invalid version line: " + versionLine); |
| 325 | } |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 326 | version = Integer.parseInt( |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 327 | versionLine.substring(PACKAGE_DEX_USAGE_VERSION_HEADER.length())); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 328 | if (!isSupportedVersion(version)) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 329 | throw new IllegalStateException("Unexpected version: " + version); |
| 330 | } |
| 331 | } |
| 332 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 333 | String line; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 334 | String currentPackage = null; |
| 335 | PackageUseInfo currentPackageData = null; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 336 | |
| 337 | Set<String> supportedIsas = new HashSet<>(); |
| 338 | for (String abi : Build.SUPPORTED_ABIS) { |
| 339 | supportedIsas.add(VMRuntime.getInstructionSet(abi)); |
| 340 | } |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 341 | while ((line = in.readLine()) != null) { |
| 342 | if (line.startsWith(DEX_LINE_CHAR)) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 343 | // This is the start of the the dex lines. |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 344 | // We expect 4 lines for each dex entry: |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 345 | // #dexPaths |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 346 | // @loading_package_1,loading_package_2,... |
| 347 | // class_loader_context |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 348 | // onwerUserId,isUsedByOtherApps,isa1,isa2 |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 349 | if (currentPackage == null) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 350 | throw new IllegalStateException( |
| 351 | "Malformed PackageDexUsage file. Expected package line before dex line."); |
| 352 | } |
| 353 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 354 | // Line 1 is the dex path. |
| 355 | String dexPath = line.substring(DEX_LINE_CHAR.length()); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 356 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 357 | // Line 2 is the dex data: (userId, isUsedByOtherApps, isa). |
| 358 | line = in.readLine(); |
| 359 | if (line == null) { |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 360 | throw new IllegalStateException("Could not find dexUseInfo line"); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 361 | } |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 362 | String[] elems = line.split(SPLIT_CHAR); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 363 | if (elems.length < 3) { |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 364 | throw new IllegalStateException("Invalid PackageDexUsage line: " + line); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 365 | } |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 366 | |
| 367 | // In version 2 we added the loading packages and class loader context. |
| 368 | Set<String> loadingPackages = maybeReadLoadingPackages(in, version); |
| 369 | String classLoaderContext = maybeReadClassLoaderContext(in, version); |
| 370 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 371 | if (UNSUPPORTED_CLASS_LOADER_CONTEXT.equals(classLoaderContext)) { |
| 372 | // We used to record use of unsupported class loaders, but we no longer do. |
| 373 | // Discard such entries; they will be deleted when we next write the file. |
| 374 | continue; |
| 375 | } |
| 376 | |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 377 | int ownerUserId = Integer.parseInt(elems[0]); |
| 378 | boolean isUsedByOtherApps = readBoolean(elems[1]); |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 379 | DexUseInfo dexUseInfo = new DexUseInfo(isUsedByOtherApps, ownerUserId, |
| 380 | classLoaderContext, /*isa*/ null); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 381 | dexUseInfo.mLoadingPackages.addAll(loadingPackages); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 382 | for (int i = 2; i < elems.length; i++) { |
| 383 | String isa = elems[i]; |
| 384 | if (supportedIsas.contains(isa)) { |
| 385 | dexUseInfo.mLoaderIsas.add(elems[i]); |
| 386 | } else { |
| 387 | // Should never happen unless someone crafts the file manually. |
| 388 | // In theory it could if we drop a supported ISA after an OTA but we don't |
| 389 | // do that. |
| 390 | Slog.wtf(TAG, "Unsupported ISA when parsing PackageDexUsage: " + isa); |
| 391 | } |
| 392 | } |
| 393 | if (supportedIsas.isEmpty()) { |
| 394 | Slog.wtf(TAG, "Ignore dexPath when parsing PackageDexUsage because of " + |
| 395 | "unsupported isas. dexPath=" + dexPath); |
| 396 | continue; |
| 397 | } |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 398 | currentPackageData.mDexUseInfoMap.put(dexPath, dexUseInfo); |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 399 | } else if (line.startsWith(CODE_PATH_LINE_CHAR)) { |
| 400 | // This is a code path used by other apps line. |
| 401 | if (version < PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2) { |
| 402 | throw new IllegalArgumentException("Unexpected code path line when parsing " + |
| 403 | "PackageDexUseData: " + line); |
| 404 | } |
| 405 | |
| 406 | // Expects 2 lines: |
| 407 | // +code_paths |
| 408 | // @loading_packages |
| 409 | String codePath = line.substring(CODE_PATH_LINE_CHAR.length()); |
| 410 | Set<String> loadingPackages = maybeReadLoadingPackages(in, version); |
| 411 | currentPackageData.mCodePathsUsedByOtherApps.put(codePath, loadingPackages); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 412 | } else { |
| 413 | // This is a package line. |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 414 | if (version >= PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2) { |
| 415 | currentPackage = line; |
| 416 | currentPackageData = new PackageUseInfo(); |
| 417 | } else { |
| 418 | // Old version (<2) |
| 419 | // We expect it to be: `packageName,isUsedByOtherApps`. |
| 420 | String[] elems = line.split(SPLIT_CHAR); |
| 421 | if (elems.length != 2) { |
| 422 | throw new IllegalStateException("Invalid PackageDexUsage line: " + line); |
| 423 | } |
| 424 | currentPackage = elems[0]; |
| 425 | currentPackageData = new PackageUseInfo(); |
| 426 | currentPackageData.mUsedByOtherAppsBeforeUpgrade = readBoolean(elems[1]); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 427 | } |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 428 | data.put(currentPackage, currentPackageData); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | synchronized (mPackageUseInfoMap) { |
| 433 | mPackageUseInfoMap.clear(); |
| 434 | mPackageUseInfoMap.putAll(data); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 439 | * Reads the class loader context encoding from the buffer {@code in} if |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 440 | * {@code version} is at least {PACKAGE_DEX_USAGE_VERSION}. |
| 441 | */ |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 442 | private String maybeReadClassLoaderContext(BufferedReader in, int version) throws IOException { |
| 443 | String context = null; |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 444 | if (version >= PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2) { |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 445 | context = in.readLine(); |
| 446 | if (context == null) { |
| 447 | throw new IllegalStateException("Could not find the classLoaderContext line."); |
| 448 | } |
| 449 | } |
| 450 | // The context might be empty if we didn't have the chance to update it after a version |
| 451 | // upgrade. In this case return the special marker so that we recognize this is an unknown |
| 452 | // context. |
| 453 | return context == null ? UNKNOWN_CLASS_LOADER_CONTEXT : context; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Reads the list of loading packages from the buffer {@code in} if |
| 458 | * {@code version} is at least {PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2}. |
| 459 | */ |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 460 | private Set<String> maybeReadLoadingPackages(BufferedReader in, int version) |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 461 | throws IOException { |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 462 | if (version >= PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2) { |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 463 | String line = in.readLine(); |
| 464 | if (line == null) { |
| 465 | throw new IllegalStateException("Could not find the loadingPackages line."); |
| 466 | } |
| 467 | // We expect that most of the times the list of loading packages will be empty. |
| 468 | if (line.length() == LOADING_PACKAGE_CHAR.length()) { |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 469 | return Collections.emptySet(); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 470 | } else { |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 471 | Set<String> result = new HashSet<>(); |
| 472 | Collections.addAll(result, |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 473 | line.substring(LOADING_PACKAGE_CHAR.length()).split(SPLIT_CHAR)); |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 474 | return result; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 475 | } |
| 476 | } else { |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 477 | return Collections.emptySet(); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Utility method which adds {@param loadingPackage} to {@param loadingPackages} only if it's |
| 483 | * not equal to {@param owningPackage} |
| 484 | */ |
| 485 | private boolean maybeAddLoadingPackage(String owningPackage, String loadingPackage, |
| 486 | Set<String> loadingPackages) { |
| 487 | return !owningPackage.equals(loadingPackage) && loadingPackages.add(loadingPackage); |
| 488 | } |
| 489 | |
| 490 | private boolean isSupportedVersion(int version) { |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 491 | return version == PACKAGE_DEX_USAGE_SUPPORTED_VERSION_1 |
| 492 | || version == PACKAGE_DEX_USAGE_SUPPORTED_VERSION_2; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /** |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 496 | * Syncs the existing data with the set of available packages by removing obsolete entries. |
| 497 | */ |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 498 | /*package*/ void syncData(Map<String, Set<Integer>> packageToUsersMap, |
| 499 | Map<String, Set<String>> packageToCodePaths) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 500 | synchronized (mPackageUseInfoMap) { |
| 501 | Iterator<Map.Entry<String, PackageUseInfo>> pIt = |
| 502 | mPackageUseInfoMap.entrySet().iterator(); |
| 503 | while (pIt.hasNext()) { |
| 504 | Map.Entry<String, PackageUseInfo> pEntry = pIt.next(); |
| 505 | String packageName = pEntry.getKey(); |
| 506 | PackageUseInfo packageUseInfo = pEntry.getValue(); |
| 507 | Set<Integer> users = packageToUsersMap.get(packageName); |
| 508 | if (users == null) { |
| 509 | // The package doesn't exist anymore, remove the record. |
| 510 | pIt.remove(); |
| 511 | } else { |
| 512 | // The package exists but we can prune the entries associated with non existing |
| 513 | // users. |
| 514 | Iterator<Map.Entry<String, DexUseInfo>> dIt = |
| 515 | packageUseInfo.mDexUseInfoMap.entrySet().iterator(); |
| 516 | while (dIt.hasNext()) { |
| 517 | DexUseInfo dexUseInfo = dIt.next().getValue(); |
| 518 | if (!users.contains(dexUseInfo.mOwnerUserId)) { |
| 519 | // User was probably removed. Delete its dex usage info. |
| 520 | dIt.remove(); |
| 521 | } |
| 522 | } |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 523 | |
| 524 | // Sync the code paths. |
| 525 | Set<String> codePaths = packageToCodePaths.get(packageName); |
| 526 | Iterator<Map.Entry<String, Set<String>>> codeIt = |
| 527 | packageUseInfo.mCodePathsUsedByOtherApps.entrySet().iterator(); |
| 528 | while (codeIt.hasNext()) { |
| 529 | if (!codePaths.contains(codeIt.next().getKey())) { |
| 530 | codeIt.remove(); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // In case the package was marked as used by other apps in a previous version |
| 535 | // propagate the flag to all the code paths. |
| 536 | // See mUsedByOtherAppsBeforeUpgrade docs on why it is important to do it. |
| 537 | if (packageUseInfo.mUsedByOtherAppsBeforeUpgrade) { |
| 538 | for (String codePath : codePaths) { |
| 539 | packageUseInfo.mergeCodePathUsedByOtherApps(codePath, true, null, null); |
| 540 | } |
| 541 | } else if (!packageUseInfo.isAnyCodePathUsedByOtherApps() |
| 542 | && packageUseInfo.mDexUseInfoMap.isEmpty()) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 543 | // The package is not used by other apps and we removed all its dex files |
| 544 | // records. Remove the entire package record as well. |
| 545 | pIt.remove(); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 552 | /** |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 553 | * Clears the {@code usesByOtherApps} marker for the package {@code packageName}. |
| 554 | * @return true if the package usage info was updated. |
| 555 | */ |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 556 | /*package*/ boolean clearUsedByOtherApps(String packageName) { |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 557 | synchronized (mPackageUseInfoMap) { |
| 558 | PackageUseInfo packageUseInfo = mPackageUseInfoMap.get(packageName); |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 559 | if (packageUseInfo == null) { |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 560 | return false; |
| 561 | } |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 562 | return packageUseInfo.clearCodePathUsedByOtherApps(); |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Remove the usage data associated with package {@code packageName}. |
| 568 | * @return true if the package usage was found and removed successfully. |
| 569 | */ |
Alan Stokes | 58aea51 | 2018-09-21 17:26:43 +0100 | [diff] [blame] | 570 | /* package */ boolean removePackage(String packageName) { |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 571 | synchronized (mPackageUseInfoMap) { |
| 572 | return mPackageUseInfoMap.remove(packageName) != null; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /** |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 577 | * Remove all the records about package {@code packageName} belonging to user {@code userId}. |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 578 | * If the package is left with no records of secondary dex usage and is not used by other |
| 579 | * apps it will be removed as well. |
Calin Juravle | b109741 | 2017-01-26 18:53:23 -0800 | [diff] [blame] | 580 | * @return true if the record was found and actually deleted, |
| 581 | * false if the record doesn't exist |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 582 | */ |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 583 | /*package*/ boolean removeUserPackage(String packageName, int userId) { |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 584 | synchronized (mPackageUseInfoMap) { |
| 585 | PackageUseInfo packageUseInfo = mPackageUseInfoMap.get(packageName); |
| 586 | if (packageUseInfo == null) { |
| 587 | return false; |
| 588 | } |
| 589 | boolean updated = false; |
| 590 | Iterator<Map.Entry<String, DexUseInfo>> dIt = |
| 591 | packageUseInfo.mDexUseInfoMap.entrySet().iterator(); |
| 592 | while (dIt.hasNext()) { |
| 593 | DexUseInfo dexUseInfo = dIt.next().getValue(); |
| 594 | if (dexUseInfo.mOwnerUserId == userId) { |
| 595 | dIt.remove(); |
| 596 | updated = true; |
| 597 | } |
| 598 | } |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 599 | // If no secondary dex info is left and the package is not used by other apps |
| 600 | // remove the data since it is now useless. |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 601 | if (packageUseInfo.mDexUseInfoMap.isEmpty() |
| 602 | && !packageUseInfo.isAnyCodePathUsedByOtherApps()) { |
Calin Juravle | 99dd37b | 2017-02-22 19:05:06 -0800 | [diff] [blame] | 603 | mPackageUseInfoMap.remove(packageName); |
| 604 | updated = true; |
| 605 | } |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 606 | return updated; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Remove the secondary dex file record belonging to the package {@code packageName} |
| 612 | * and user {@code userId}. |
Calin Juravle | b109741 | 2017-01-26 18:53:23 -0800 | [diff] [blame] | 613 | * @return true if the record was found and actually deleted, |
| 614 | * false if the record doesn't exist |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 615 | */ |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 616 | /*package*/ boolean removeDexFile(String packageName, String dexFile, int userId) { |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 617 | synchronized (mPackageUseInfoMap) { |
| 618 | PackageUseInfo packageUseInfo = mPackageUseInfoMap.get(packageName); |
| 619 | if (packageUseInfo == null) { |
| 620 | return false; |
| 621 | } |
| 622 | return removeDexFile(packageUseInfo, dexFile, userId); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | private boolean removeDexFile(PackageUseInfo packageUseInfo, String dexFile, int userId) { |
| 627 | DexUseInfo dexUseInfo = packageUseInfo.mDexUseInfoMap.get(dexFile); |
| 628 | if (dexUseInfo == null) { |
| 629 | return false; |
| 630 | } |
| 631 | if (dexUseInfo.mOwnerUserId == userId) { |
| 632 | packageUseInfo.mDexUseInfoMap.remove(dexFile); |
| 633 | return true; |
| 634 | } |
| 635 | return false; |
| 636 | } |
| 637 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 638 | /*package*/ PackageUseInfo getPackageUseInfo(String packageName) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 639 | synchronized (mPackageUseInfoMap) { |
Calin Juravle | 1aa5f88 | 2017-01-25 01:05:50 -0800 | [diff] [blame] | 640 | PackageUseInfo useInfo = mPackageUseInfoMap.get(packageName); |
| 641 | // The useInfo contains a map for secondary dex files which could be modified |
| 642 | // concurrently after this method returns and thus outside the locking we do here. |
| 643 | // (i.e. the map is updated when new class loaders are created, which can happen anytime |
| 644 | // after this method returns) |
| 645 | // Make a defensive copy to be sure we don't get concurrent modifications. |
| 646 | return useInfo == null ? null : new PackageUseInfo(useInfo); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Calin Juravle | 51f521c | 2017-01-25 18:00:05 -0800 | [diff] [blame] | 650 | /** |
| 651 | * Return all packages that contain records of secondary dex files. |
| 652 | */ |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 653 | /*package*/ Set<String> getAllPackagesWithSecondaryDexFiles() { |
Calin Juravle | 51f521c | 2017-01-25 18:00:05 -0800 | [diff] [blame] | 654 | Set<String> packages = new HashSet<>(); |
| 655 | synchronized (mPackageUseInfoMap) { |
| 656 | for (Map.Entry<String, PackageUseInfo> entry : mPackageUseInfoMap.entrySet()) { |
| 657 | if (!entry.getValue().mDexUseInfoMap.isEmpty()) { |
| 658 | packages.add(entry.getKey()); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | return packages; |
| 663 | } |
| 664 | |
Alan Stokes | 58aea51 | 2018-09-21 17:26:43 +0100 | [diff] [blame] | 665 | /* package */ void clear() { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 666 | synchronized (mPackageUseInfoMap) { |
| 667 | mPackageUseInfoMap.clear(); |
| 668 | } |
| 669 | } |
Alan Stokes | 58aea51 | 2018-09-21 17:26:43 +0100 | [diff] [blame] | 670 | |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 671 | // Creates a deep copy of the class' mPackageUseInfoMap. |
| 672 | private Map<String, PackageUseInfo> clonePackageUseInfoMap() { |
| 673 | Map<String, PackageUseInfo> clone = new HashMap<>(); |
| 674 | synchronized (mPackageUseInfoMap) { |
| 675 | for (Map.Entry<String, PackageUseInfo> e : mPackageUseInfoMap.entrySet()) { |
| 676 | clone.put(e.getKey(), new PackageUseInfo(e.getValue())); |
| 677 | } |
| 678 | } |
| 679 | return clone; |
| 680 | } |
| 681 | |
| 682 | private String writeBoolean(boolean bool) { |
| 683 | return bool ? "1" : "0"; |
| 684 | } |
| 685 | |
| 686 | private boolean readBoolean(String bool) { |
| 687 | if ("0".equals(bool)) return false; |
| 688 | if ("1".equals(bool)) return true; |
| 689 | throw new IllegalArgumentException("Unknown bool encoding: " + bool); |
| 690 | } |
| 691 | |
Alan Stokes | 58aea51 | 2018-09-21 17:26:43 +0100 | [diff] [blame] | 692 | /* package */ String dump() { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 693 | StringWriter sw = new StringWriter(); |
| 694 | write(sw); |
| 695 | return sw.toString(); |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Stores data on how a package and its dex files are used. |
| 700 | */ |
| 701 | public static class PackageUseInfo { |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 702 | // The app's code paths that are used by other apps. |
| 703 | // The key is the code path and the value is the set of loading packages. |
| 704 | private final Map<String, Set<String>> mCodePathsUsedByOtherApps; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 705 | // Map dex paths to their data (isUsedByOtherApps, owner id, loader isa). |
| 706 | private final Map<String, DexUseInfo> mDexUseInfoMap; |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 707 | |
| 708 | // Keeps track of whether or not this package was used by other apps before |
| 709 | // we upgraded to VERSION 4 which records the info for each code path separately. |
| 710 | // This is unwanted complexity but without it we risk to profile guide compile |
| 711 | // something that supposed to be shared. For example: |
| 712 | // 1) we determine that chrome is used by another app |
| 713 | // 2) we take an OTA which upgrades the way we keep track of usage data |
| 714 | // 3) chrome doesn't get used until the background job executes |
| 715 | // 4) as part of the backgound job we now think that chrome is not used by others |
| 716 | // and we speed-profile. |
| 717 | // 5) as a result the next time someone uses chrome it will extract from apk since |
| 718 | // the compiled code will be private. |
| 719 | private boolean mUsedByOtherAppsBeforeUpgrade; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 720 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 721 | /*package*/ PackageUseInfo() { |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 722 | mCodePathsUsedByOtherApps = new HashMap<>(); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 723 | mDexUseInfoMap = new HashMap<>(); |
| 724 | } |
| 725 | |
| 726 | // Creates a deep copy of the `other`. |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 727 | private PackageUseInfo(PackageUseInfo other) { |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 728 | mCodePathsUsedByOtherApps = new HashMap<>(); |
| 729 | for (Map.Entry<String, Set<String>> e : other.mCodePathsUsedByOtherApps.entrySet()) { |
| 730 | mCodePathsUsedByOtherApps.put(e.getKey(), new HashSet<>(e.getValue())); |
| 731 | } |
| 732 | |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 733 | mDexUseInfoMap = new HashMap<>(); |
| 734 | for (Map.Entry<String, DexUseInfo> e : other.mDexUseInfoMap.entrySet()) { |
| 735 | mDexUseInfoMap.put(e.getKey(), new DexUseInfo(e.getValue())); |
| 736 | } |
| 737 | } |
| 738 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 739 | private boolean mergeCodePathUsedByOtherApps(String codePath, boolean isUsedByOtherApps, |
| 740 | String owningPackageName, String loadingPackage) { |
| 741 | if (!isUsedByOtherApps) { |
| 742 | // Nothing to update if the the code path is not used by other apps. |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | boolean newCodePath = false; |
| 747 | Set<String> loadingPackages = mCodePathsUsedByOtherApps.get(codePath); |
| 748 | if (loadingPackages == null) { |
| 749 | loadingPackages = new HashSet<>(); |
| 750 | mCodePathsUsedByOtherApps.put(codePath, loadingPackages); |
| 751 | newCodePath = true; |
| 752 | } |
| 753 | boolean newLoadingPackage = loadingPackage != null |
| 754 | && !loadingPackage.equals(owningPackageName) |
| 755 | && loadingPackages.add(loadingPackage); |
| 756 | return newCodePath || newLoadingPackage; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 759 | public boolean isUsedByOtherApps(String codePath) { |
| 760 | return mCodePathsUsedByOtherApps.containsKey(codePath); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | public Map<String, DexUseInfo> getDexUseInfoMap() { |
| 764 | return mDexUseInfoMap; |
| 765 | } |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 766 | |
Calin Juravle | 52a452c | 2017-08-04 01:42:17 -0700 | [diff] [blame] | 767 | public Set<String> getLoadingPackages(String codePath) { |
| 768 | return mCodePathsUsedByOtherApps.getOrDefault(codePath, null); |
| 769 | } |
| 770 | |
| 771 | public boolean isAnyCodePathUsedByOtherApps() { |
| 772 | return !mCodePathsUsedByOtherApps.isEmpty(); |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * Clears the usedByOtherApps markers from all code paths. |
| 777 | * Returns whether or not there was an update. |
| 778 | */ |
| 779 | /*package*/ boolean clearCodePathUsedByOtherApps() { |
| 780 | // Update mUsedByOtherAppsBeforeUpgrade as well to be consistent with |
| 781 | // the new data. This is not saved to disk so we don't need to return it. |
| 782 | mUsedByOtherAppsBeforeUpgrade = true; |
| 783 | |
| 784 | if (mCodePathsUsedByOtherApps.isEmpty()) { |
| 785 | return false; |
| 786 | } else { |
| 787 | mCodePathsUsedByOtherApps.clear(); |
| 788 | return true; |
| 789 | } |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 790 | } |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /** |
| 794 | * Stores data about a loaded dex files. |
| 795 | */ |
| 796 | public static class DexUseInfo { |
| 797 | private boolean mIsUsedByOtherApps; |
| 798 | private final int mOwnerUserId; |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 799 | // The class loader context for the dex file. This encodes the class loader chain |
| 800 | // (class loader type + class path) in a format compatible to dex2oat. |
| 801 | // See {@code DexoptUtils.processContextForDexLoad}. |
| 802 | private String mClassLoaderContext; |
| 803 | // The instructions sets of the applications loading the dex file. |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 804 | private final Set<String> mLoaderIsas; |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 805 | // Packages who load this dex file. |
| 806 | private final Set<String> mLoadingPackages; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 807 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 808 | @VisibleForTesting |
| 809 | /* package */ DexUseInfo(boolean isUsedByOtherApps, int ownerUserId, |
| 810 | String classLoaderContext, String loaderIsa) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 811 | mIsUsedByOtherApps = isUsedByOtherApps; |
| 812 | mOwnerUserId = ownerUserId; |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 813 | mClassLoaderContext = classLoaderContext; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 814 | mLoaderIsas = new HashSet<>(); |
| 815 | if (loaderIsa != null) { |
| 816 | mLoaderIsas.add(loaderIsa); |
| 817 | } |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 818 | mLoadingPackages = new HashSet<>(); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | // Creates a deep copy of the `other`. |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 822 | private DexUseInfo(DexUseInfo other) { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 823 | mIsUsedByOtherApps = other.mIsUsedByOtherApps; |
| 824 | mOwnerUserId = other.mOwnerUserId; |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 825 | mClassLoaderContext = other.mClassLoaderContext; |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 826 | mLoaderIsas = new HashSet<>(other.mLoaderIsas); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 827 | mLoadingPackages = new HashSet<>(other.mLoadingPackages); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | private boolean merge(DexUseInfo dexUseInfo) { |
| 831 | boolean oldIsUsedByOtherApps = mIsUsedByOtherApps; |
| 832 | mIsUsedByOtherApps = mIsUsedByOtherApps || dexUseInfo.mIsUsedByOtherApps; |
| 833 | boolean updateIsas = mLoaderIsas.addAll(dexUseInfo.mLoaderIsas); |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 834 | boolean updateLoadingPackages = mLoadingPackages.addAll(dexUseInfo.mLoadingPackages); |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 835 | |
| 836 | String oldClassLoaderContext = mClassLoaderContext; |
| 837 | if (UNKNOWN_CLASS_LOADER_CONTEXT.equals(mClassLoaderContext)) { |
| 838 | // Can happen if we read a previous version. |
| 839 | mClassLoaderContext = dexUseInfo.mClassLoaderContext; |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 840 | } else if (!Objects.equals(mClassLoaderContext, dexUseInfo.mClassLoaderContext)) { |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 841 | // We detected a context change. |
| 842 | mClassLoaderContext = VARIABLE_CLASS_LOADER_CONTEXT; |
| 843 | } |
| 844 | |
| 845 | return updateIsas || |
| 846 | (oldIsUsedByOtherApps != mIsUsedByOtherApps) || |
| 847 | updateLoadingPackages |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 848 | || !Objects.equals(oldClassLoaderContext, mClassLoaderContext); |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | public boolean isUsedByOtherApps() { |
| 852 | return mIsUsedByOtherApps; |
| 853 | } |
| 854 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 855 | /* package */ int getOwnerUserId() { |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 856 | return mOwnerUserId; |
| 857 | } |
| 858 | |
| 859 | public Set<String> getLoaderIsas() { |
| 860 | return mLoaderIsas; |
| 861 | } |
Calin Juravle | 535a475 | 2017-02-03 16:55:49 -0800 | [diff] [blame] | 862 | |
| 863 | public Set<String> getLoadingPackages() { |
| 864 | return mLoadingPackages; |
| 865 | } |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 866 | |
| 867 | public String getClassLoaderContext() { return mClassLoaderContext; } |
| 868 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 869 | @VisibleForTesting |
| 870 | /* package */ boolean isUnknownClassLoaderContext() { |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 871 | // The class loader context may be unknown if we loaded the data from a previous version |
| 872 | // which didn't save the context. |
| 873 | return UNKNOWN_CLASS_LOADER_CONTEXT.equals(mClassLoaderContext); |
| 874 | } |
| 875 | |
Alan Stokes | 6dba50d | 2018-10-30 15:05:36 +0000 | [diff] [blame] | 876 | @VisibleForTesting |
| 877 | /* package */ boolean isVariableClassLoaderContext() { |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 878 | return VARIABLE_CLASS_LOADER_CONTEXT.equals(mClassLoaderContext); |
| 879 | } |
Calin Juravle | 0318162 | 2016-12-01 17:53:07 +0000 | [diff] [blame] | 880 | } |
| 881 | } |