From 36d42a5545621d9b995f8a187b101e54939cca50 Mon Sep 17 00:00:00 2001 From: Lee Shombert Date: Thu, 4 Mar 2021 10:34:50 -0800 Subject: [PATCH] Update IntentResolver snapshots Bug: 179652745 Update IntentResolver snapshots to deep-copy the arrays that are part of the maps. This change makes copies of the IntentFilter arrays but the IntentFilter elements are still copied by reference. Therefore, this change addresses the race condition of the bug but does not address dynamic mime types as per the comment. With this change, the steady-state overhead of a snapshot is approximately 1.4Mb relative to not using snapshots at all. This was measured by comparing the rss (mean of five runs) and pss of three images: * disabled - snapshots disabled * baseline - snapshots enabled, without this CL * proposed - snapshots enabled, with this CL Test: atest * FrameworksServicesTests:AppsFilterTest * FrameworksServicesTests:PackageInstallerSessionTest * FrameworksServicesTests:PackageManagerServiceTest * FrameworksServicesTests:PackageManagerSettingsTests * FrameworksServicesTests:ScanTests * FrameworksServicesTests:UserSystemPackageInstallerTest * PackageManagerServiceBootTest * UserLifecycleTests#startUser * UserLifecycleTests#stopUser * UserLifecycleTests#switchUser * android.appsecurity.cts.EphemeralTest * android.appsecurity.cts.InstantAppUserTest Change-Id: I704bf8e6c0888c2ffb9563e188ebd8ac91c2f597 --- .../com/android/server/IntentResolver.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/IntentResolver.java b/services/core/java/com/android/server/IntentResolver.java index 2906ceebca58e..9067028668895 100644 --- a/services/core/java/com/android/server/IntentResolver.java +++ b/services/core/java/com/android/server/IntentResolver.java @@ -839,23 +839,34 @@ public abstract class IntentResolver { } }; + // Helper method to copy some of the maps. + private static void copyInto(ArrayMap l, ArrayMap r) { + final int end = r.size(); + l.ensureCapacity(end); + for (int i = 0; i < end; i++) { + final E[] val = r.valueAt(i); + final String key = r.keyAt(i); + l.put(key, Arrays.copyOf(val, val.length)); + } + } + // Make a copy of . The presumption is that is empty but all // arrays are cleared out explicitly, just to be sure. protected void copyFrom(IntentResolver orig) { mFilters.clear(); mFilters.addAll(orig.mFilters); mTypeToFilter.clear(); - mTypeToFilter.putAll(orig.mTypeToFilter); + copyInto(mTypeToFilter, orig.mTypeToFilter); mBaseTypeToFilter.clear(); - mBaseTypeToFilter.putAll(orig.mBaseTypeToFilter); + copyInto(mBaseTypeToFilter, orig.mBaseTypeToFilter); mWildTypeToFilter.clear(); - mWildTypeToFilter.putAll(orig.mWildTypeToFilter); + copyInto(mWildTypeToFilter, orig.mWildTypeToFilter); mSchemeToFilter.clear(); - mSchemeToFilter.putAll(orig.mSchemeToFilter); + copyInto(mSchemeToFilter, orig.mSchemeToFilter); mActionToFilter.clear(); - mActionToFilter.putAll(orig.mActionToFilter); + copyInto(mActionToFilter, orig.mActionToFilter); mTypedActionToFilter.clear(); - mTypedActionToFilter.putAll(orig.mTypedActionToFilter); + copyInto(mTypedActionToFilter, orig.mTypedActionToFilter); } /**