From 5733be54b2d8d21336f2efe71d1cc13778779dd0 Mon Sep 17 00:00:00 2001 From: Songchun Fan Date: Fri, 28 Oct 2022 16:09:42 -0700 Subject: [PATCH] [pm] use list caches instead of real lists when snapshotting The actual lists/maps can be changed when snapshotting, causing system server crashes. BUG: 255912832 Test: builds Change-Id: Ia10b4bd47fb3c1d3de577bc4b1a4a70d0d5adc9f --- .../java/com/android/server/pm/Settings.java | 94 ++++++++++++++----- 1 file changed, 72 insertions(+), 22 deletions(-) diff --git a/services/core/java/com/android/server/pm/Settings.java b/services/core/java/com/android/server/pm/Settings.java index 12c9d4b37eed4..45c0d6ea6c11d 100644 --- a/services/core/java/com/android/server/pm/Settings.java +++ b/services/core/java/com/android/server/pm/Settings.java @@ -455,19 +455,24 @@ public final class Settings implements Watchable, Snappable { // The user's preferred activities associated with particular intent // filters. @Watched - private final WatchedSparseArray - mPreferredActivities = new WatchedSparseArray<>(); + private final WatchedSparseArray mPreferredActivities; + private final SnapshotCache> + mPreferredActivitiesSnapshot; // The persistent preferred activities of the user's profile/device owner // associated with particular intent filters. @Watched private final WatchedSparseArray - mPersistentPreferredActivities = new WatchedSparseArray<>(); + mPersistentPreferredActivities; + private final SnapshotCache> + mPersistentPreferredActivitiesSnapshot; + // For every user, it is used to find to which other users the intent can be forwarded. @Watched - private final WatchedSparseArray - mCrossProfileIntentResolvers = new WatchedSparseArray<>(); + private final WatchedSparseArray mCrossProfileIntentResolvers; + private final SnapshotCache> + mCrossProfileIntentResolversSnapshot; @Watched final WatchedArrayMap mSharedUsers = new WatchedArrayMap<>(); @@ -476,11 +481,12 @@ public final class Settings implements Watchable, Snappable { // For reading/writing settings file. @Watched - private final WatchedArrayList mPastSignatures = - new WatchedArrayList(); + private final WatchedArrayList mPastSignatures; + private final SnapshotCache> mPastSignaturesSnapshot; + @Watched - private final WatchedArrayMap mKeySetRefs = - new WatchedArrayMap(); + private final WatchedArrayMap mKeySetRefs; + private final SnapshotCache> mKeySetRefsSnapshot; // Packages that have been renamed since they were first installed. // Keys are the new names of the packages, values are the original @@ -511,7 +517,8 @@ public final class Settings implements Watchable, Snappable { * scanning to make it less confusing. */ @Watched - private final WatchedArrayList mPendingPackages = new WatchedArrayList<>(); + private final WatchedArrayList mPendingPackages; + private final SnapshotCache> mPendingPackagesSnapshot; private final File mSystemDir; @@ -583,6 +590,26 @@ public final class Settings implements Watchable, Snappable { mInstallerPackagesSnapshot = new SnapshotCache.Auto<>(mInstallerPackages, mInstallerPackages, "Settings.mInstallerPackages"); + mPreferredActivities = new WatchedSparseArray<>(); + mPreferredActivitiesSnapshot = new SnapshotCache.Auto<>(mPreferredActivities, + mPreferredActivities, "Settings.mPreferredActivities"); + mPersistentPreferredActivities = new WatchedSparseArray<>(); + mPersistentPreferredActivitiesSnapshot = new SnapshotCache.Auto<>( + mPersistentPreferredActivities, mPersistentPreferredActivities, + "Settings.mPersistentPreferredActivities"); + mCrossProfileIntentResolvers = new WatchedSparseArray<>(); + mCrossProfileIntentResolversSnapshot = new SnapshotCache.Auto<>( + mCrossProfileIntentResolvers, mCrossProfileIntentResolvers, + "Settings.mCrossProfileIntentResolvers"); + mPastSignatures = new WatchedArrayList<>(); + mPastSignaturesSnapshot = new SnapshotCache.Auto<>(mPastSignatures, mPastSignatures, + "Settings.mPastSignatures"); + mKeySetRefs = new WatchedArrayMap<>(); + mKeySetRefsSnapshot = new SnapshotCache.Auto<>(mKeySetRefs, mKeySetRefs, + "Settings.mKeySetRefs"); + mPendingPackages = new WatchedArrayList<>(); + mPendingPackagesSnapshot = new SnapshotCache.Auto<>(mPendingPackages, mPendingPackages, + "Settings.mPendingPackages"); mKeySetManagerService = new KeySetManagerService(mPackages); // Test-only handler working on background thread. @@ -623,6 +650,26 @@ public final class Settings implements Watchable, Snappable { mInstallerPackagesSnapshot = new SnapshotCache.Auto<>(mInstallerPackages, mInstallerPackages, "Settings.mInstallerPackages"); + mPreferredActivities = new WatchedSparseArray<>(); + mPreferredActivitiesSnapshot = new SnapshotCache.Auto<>(mPreferredActivities, + mPreferredActivities, "Settings.mPreferredActivities"); + mPersistentPreferredActivities = new WatchedSparseArray<>(); + mPersistentPreferredActivitiesSnapshot = new SnapshotCache.Auto<>( + mPersistentPreferredActivities, mPersistentPreferredActivities, + "Settings.mPersistentPreferredActivities"); + mCrossProfileIntentResolvers = new WatchedSparseArray<>(); + mCrossProfileIntentResolversSnapshot = new SnapshotCache.Auto<>( + mCrossProfileIntentResolvers, mCrossProfileIntentResolvers, + "Settings.mCrossProfileIntentResolvers"); + mPastSignatures = new WatchedArrayList<>(); + mPastSignaturesSnapshot = new SnapshotCache.Auto<>(mPastSignatures, mPastSignatures, + "Settings.mPastSignatures"); + mKeySetRefs = new WatchedArrayMap<>(); + mKeySetRefsSnapshot = new SnapshotCache.Auto<>(mKeySetRefs, mKeySetRefs, + "Settings.mKeySetRefs"); + mPendingPackages = new WatchedArrayList<>(); + mPendingPackagesSnapshot = new SnapshotCache.Auto<>(mPendingPackages, mPendingPackages, + "Settings.mPendingPackages"); mKeySetManagerService = new KeySetManagerService(mPackages); mHandler = handler; @@ -699,24 +746,27 @@ public final class Settings implements Watchable, Snappable { mBlockUninstallPackages.snapshot(r.mBlockUninstallPackages); mVersion.putAll(r.mVersion); mVerifierDeviceIdentity = r.mVerifierDeviceIdentity; - WatchedSparseArray.snapshot( - mPreferredActivities, r.mPreferredActivities); - WatchedSparseArray.snapshot( - mPersistentPreferredActivities, r.mPersistentPreferredActivities); - WatchedSparseArray.snapshot( - mCrossProfileIntentResolvers, r.mCrossProfileIntentResolvers); + mPreferredActivities = r.mPreferredActivitiesSnapshot.snapshot(); + mPreferredActivitiesSnapshot = new SnapshotCache.Sealed<>(); + mPersistentPreferredActivities = r.mPersistentPreferredActivitiesSnapshot.snapshot(); + mPersistentPreferredActivitiesSnapshot = new SnapshotCache.Sealed<>(); + mCrossProfileIntentResolvers = r.mCrossProfileIntentResolversSnapshot.snapshot(); + mCrossProfileIntentResolversSnapshot = new SnapshotCache.Sealed<>(); + mSharedUsers.snapshot(r.mSharedUsers); mAppIds = r.mAppIds.snapshot(); - WatchedArrayList.snapshot( - mPastSignatures, r.mPastSignatures); - WatchedArrayMap.snapshot( - mKeySetRefs, r.mKeySetRefs); + + mPastSignatures = r.mPastSignaturesSnapshot.snapshot(); + mPastSignaturesSnapshot = new SnapshotCache.Sealed<>(); + mKeySetRefs = r.mKeySetRefsSnapshot.snapshot(); + mKeySetRefsSnapshot = new SnapshotCache.Sealed<>(); + mRenamedPackages.snapshot(r.mRenamedPackages); mNextAppLinkGeneration.snapshot(r.mNextAppLinkGeneration); mDefaultBrowserApp.snapshot(r.mDefaultBrowserApp); // mReadMessages - WatchedArrayList.snapshot( - mPendingPackages, r.mPendingPackages); + mPendingPackages = r.mPendingPackagesSnapshot.snapshot(); + mPendingPackagesSnapshot = new SnapshotCache.Sealed<>(); mSystemDir = null; // mKeySetManagerService; mPermissions = r.mPermissions;