Merge "[AppsFilter] make mShouldFilterCache non null" into tm-dev

This commit is contained in:
Songchun Fan
2022-04-04 20:10:52 +00:00
committed by Android (Google) Code Review

View File

@@ -52,7 +52,6 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils; import com.android.internal.util.ArrayUtils;
import com.android.internal.util.function.QuadFunction; import com.android.internal.util.function.QuadFunction;
import com.android.server.FgThread; import com.android.server.FgThread;
import com.android.server.LocalServices;
import com.android.server.compat.CompatChange; import com.android.server.compat.CompatChange;
import com.android.server.om.OverlayReferenceMapper; import com.android.server.om.OverlayReferenceMapper;
import com.android.server.pm.parsing.pkg.AndroidPackage; import com.android.server.pm.parsing.pkg.AndroidPackage;
@@ -171,10 +170,13 @@ public class AppsFilter implements Watchable, Snappable {
* filtered to the second. It's essentially a cache of the * filtered to the second. It's essentially a cache of the
* {@link #shouldFilterApplicationInternal(int, Object, PackageStateInternal, int)} call. * {@link #shouldFilterApplicationInternal(int, Object, PackageStateInternal, int)} call.
* NOTE: It can only be relied upon after the system is ready to avoid unnecessary update on * NOTE: It can only be relied upon after the system is ready to avoid unnecessary update on
* initial scam and is null until {@link #onSystemReady()} is called. * initial scam and is empty until {@link #mSystemReady} is true.
*/ */
@GuardedBy("mCacheLock") @GuardedBy("mCacheLock")
private volatile WatchedSparseBooleanMatrix mShouldFilterCache; @NonNull
private final WatchedSparseBooleanMatrix mShouldFilterCache;
private volatile boolean mSystemReady = false;
/** /**
* A cached snapshot. * A cached snapshot.
@@ -187,7 +189,8 @@ public class AppsFilter implements Watchable, Snappable {
public AppsFilter createSnapshot() { public AppsFilter createSnapshot() {
AppsFilter s = new AppsFilter(mSource); AppsFilter s = new AppsFilter(mSource);
return s; return s;
}}; }
};
} }
/** /**
@@ -219,6 +222,7 @@ public class AppsFilter implements Watchable, Snappable {
/** /**
* Return true if the {@link Watcher) is a registered observer. * Return true if the {@link Watcher) is a registered observer.
*
* @param observer A {@link Watcher} that might be registered * @param observer A {@link Watcher} that might be registered
* @return true if the observer is registered with this {@link Watchable}. * @return true if the observer is registered with this {@link Watchable}.
*/ */
@@ -262,6 +266,7 @@ public class AppsFilter implements Watchable, Snappable {
mStateProvider = stateProvider; mStateProvider = stateProvider;
mPmInternal = pmInternal; mPmInternal = pmInternal;
mBackgroundExecutor = backgroundExecutor; mBackgroundExecutor = backgroundExecutor;
mShouldFilterCache = new WatchedSparseBooleanMatrix();
mSnapshot = makeCache(); mSnapshot = makeCache();
} }
@@ -285,16 +290,14 @@ public class AppsFilter implements Watchable, Snappable {
mStateProvider = orig.mStateProvider; mStateProvider = orig.mStateProvider;
mSystemSigningDetails = orig.mSystemSigningDetails; mSystemSigningDetails = orig.mSystemSigningDetails;
mProtectedBroadcasts = orig.mProtectedBroadcasts; mProtectedBroadcasts = orig.mProtectedBroadcasts;
mShouldFilterCache = orig.mShouldFilterCache;
if (mShouldFilterCache != null) {
synchronized (orig.mCacheLock) { synchronized (orig.mCacheLock) {
mShouldFilterCache = mShouldFilterCache.snapshot(); mShouldFilterCache = orig.mShouldFilterCache.snapshot();
}
} }
mBackgroundExecutor = null; mBackgroundExecutor = null;
mPmInternal = null; mPmInternal = null;
mSnapshot = new SnapshotCache.Sealed<>(); mSnapshot = new SnapshotCache.Sealed<>();
mSystemReady = true;
} }
/** /**
@@ -669,8 +672,9 @@ public class AppsFilter implements Watchable, Snappable {
Slog.i(TAG, (retainOnUpdate ? "retained " : "") + "implicit access granted: " Slog.i(TAG, (retainOnUpdate ? "retained " : "") + "implicit access granted: "
+ recipientUid + " -> " + visibleUid); + recipientUid + " -> " + visibleUid);
} }
if (mSystemReady) {
synchronized (mCacheLock) { synchronized (mCacheLock) {
if (mShouldFilterCache != null) {
// update the cache in a one-off manner since we've got all the information we // update the cache in a one-off manner since we've got all the information we
// need. // need.
mShouldFilterCache.put(recipientUid, visibleUid, false); mShouldFilterCache.put(recipientUid, visibleUid, false);
@@ -688,6 +692,7 @@ public class AppsFilter implements Watchable, Snappable {
updateEntireShouldFilterCacheAsync(); updateEntireShouldFilterCacheAsync();
onChanged(); onChanged();
mSystemReady = true;
} }
/** /**
@@ -708,9 +713,8 @@ public class AppsFilter implements Watchable, Snappable {
mStateProvider.runWithState((settings, users) -> { mStateProvider.runWithState((settings, users) -> {
ArraySet<String> additionalChangedPackages = ArraySet<String> additionalChangedPackages =
addPackageInternal(newPkgSetting, settings); addPackageInternal(newPkgSetting, settings);
synchronized (mCacheLock) { if (mSystemReady) {
if (mShouldFilterCache != null) { updateShouldFilterCacheForPackage(null, newPkgSetting,
updateShouldFilterCacheForPackage(mShouldFilterCache, null, newPkgSetting,
settings, users, USER_ALL, settings.size()); settings, users, USER_ALL, settings.size());
if (additionalChangedPackages != null) { if (additionalChangedPackages != null) {
for (int index = 0; index < additionalChangedPackages.size(); index++) { for (int index = 0; index < additionalChangedPackages.size(); index++) {
@@ -724,13 +728,12 @@ public class AppsFilter implements Watchable, Snappable {
continue; continue;
} }
updateShouldFilterCacheForPackage(mShouldFilterCache, null, updateShouldFilterCacheForPackage(null,
changedPkgSetting, settings, users, USER_ALL, changedPkgSetting, settings, users, USER_ALL,
settings.size()); settings.size());
} }
} }
} // else, rebuild entire cache when system is ready } // else, rebuild entire cache when system is ready
}
}); });
} finally { } finally {
onChanged(); onChanged();
@@ -845,11 +848,11 @@ public class AppsFilter implements Watchable, Snappable {
return changedPackages; return changedPackages;
} }
@GuardedBy("mCacheLock")
private void removeAppIdFromVisibilityCache(int appId) { private void removeAppIdFromVisibilityCache(int appId) {
if (mShouldFilterCache == null) { if (!mSystemReady) {
return; return;
} }
synchronized (mCacheLock) {
for (int i = 0; i < mShouldFilterCache.size(); i++) { for (int i = 0; i < mShouldFilterCache.size(); i++) {
if (UserHandle.getAppId(mShouldFilterCache.keyAt(i)) == appId) { if (UserHandle.getAppId(mShouldFilterCache.keyAt(i)) == appId) {
mShouldFilterCache.removeAt(i); mShouldFilterCache.removeAt(i);
@@ -861,6 +864,7 @@ public class AppsFilter implements Watchable, Snappable {
} }
} }
} }
}
private void updateEntireShouldFilterCache() { private void updateEntireShouldFilterCache() {
updateEntireShouldFilterCache(USER_ALL); updateEntireShouldFilterCache(USER_ALL);
@@ -880,31 +884,23 @@ public class AppsFilter implements Watchable, Snappable {
+ "updating the whole cache"); + "updating the whole cache");
userId = USER_ALL; userId = USER_ALL;
} }
WatchedSparseBooleanMatrix cache =
updateEntireShouldFilterCacheInner(settings, users, userId); updateEntireShouldFilterCacheInner(settings, users, userId);
synchronized (mCacheLock) {
mShouldFilterCache = cache;
}
}); });
} }
private WatchedSparseBooleanMatrix updateEntireShouldFilterCacheInner( private void updateEntireShouldFilterCacheInner(
ArrayMap<String, ? extends PackageStateInternal> settings, UserInfo[] users, ArrayMap<String, ? extends PackageStateInternal> settings, UserInfo[] users,
int subjectUserId) { int subjectUserId) {
final WatchedSparseBooleanMatrix cache;
if (subjectUserId == USER_ALL) {
cache = new WatchedSparseBooleanMatrix(users.length * settings.size());
} else {
synchronized (mCacheLock) { synchronized (mCacheLock) {
cache = mShouldFilterCache.snapshot(); if (subjectUserId == USER_ALL) {
mShouldFilterCache.clear();
} }
cache.setCapacity(users.length * settings.size()); mShouldFilterCache.setCapacity(users.length * settings.size());
} }
for (int i = settings.size() - 1; i >= 0; i--) { for (int i = settings.size() - 1; i >= 0; i--) {
updateShouldFilterCacheForPackage(cache, updateShouldFilterCacheForPackage(
null /*skipPackage*/, settings.valueAt(i), settings, users, subjectUserId, i); null /*skipPackage*/, settings.valueAt(i), settings, users, subjectUserId, i);
} }
return cache;
} }
private void updateEntireShouldFilterCacheAsync() { private void updateEntireShouldFilterCacheAsync() {
@@ -923,8 +919,7 @@ public class AppsFilter implements Watchable, Snappable {
packagesCache.put(settings.keyAt(i), pkg); packagesCache.put(settings.keyAt(i), pkg);
} }
}); });
WatchedSparseBooleanMatrix cache = updateEntireShouldFilterCacheInner(
settingsCopy, usersRef[0], USER_ALL);
boolean[] changed = new boolean[1]; boolean[] changed = new boolean[1];
// We have a cache, let's make sure the world hasn't changed out from under us. // We have a cache, let's make sure the world hasn't changed out from under us.
mStateProvider.runWithState((settings, users) -> { mStateProvider.runWithState((settings, users) -> {
@@ -947,45 +942,39 @@ public class AppsFilter implements Watchable, Snappable {
Slog.i(TAG, "Rebuilding cache with lock due to package change."); Slog.i(TAG, "Rebuilding cache with lock due to package change.");
} }
} else { } else {
synchronized (mCacheLock) { updateEntireShouldFilterCacheInner(settingsCopy, usersRef[0], USER_ALL);
mShouldFilterCache = cache;
}
} }
}); });
} }
public void onUserCreated(int newUserId) { public void onUserCreated(int newUserId) {
synchronized (mCacheLock) { if (!mSystemReady) {
if (mShouldFilterCache != null) { return;
}
updateEntireShouldFilterCache(newUserId); updateEntireShouldFilterCache(newUserId);
onChanged(); onChanged();
} }
}
}
public void onUserDeleted(@UserIdInt int userId) { public void onUserDeleted(@UserIdInt int userId) {
synchronized (mCacheLock) { if (!mSystemReady) {
if (mShouldFilterCache != null) { return;
}
removeShouldFilterCacheForUser(userId); removeShouldFilterCacheForUser(userId);
onChanged(); onChanged();
} }
}
}
private void updateShouldFilterCacheForPackage(String packageName) { private void updateShouldFilterCacheForPackage(String packageName) {
mStateProvider.runWithState((settings, users) -> { mStateProvider.runWithState((settings, users) -> {
synchronized (mCacheLock) { if (!mSystemReady) {
if (mShouldFilterCache == null) {
return; return;
} }
updateShouldFilterCacheForPackage(mShouldFilterCache, null /* skipPackage */, updateShouldFilterCacheForPackage(null /* skipPackage */,
settings.get(packageName), settings, users, USER_ALL, settings.get(packageName), settings, users, USER_ALL,
settings.size() /*maxIndex*/); settings.size() /*maxIndex*/);
}
}); });
} }
private void updateShouldFilterCacheForPackage(WatchedSparseBooleanMatrix cache, private void updateShouldFilterCacheForPackage(
@Nullable String skipPackageName, PackageStateInternal subjectSetting, ArrayMap<String, @Nullable String skipPackageName, PackageStateInternal subjectSetting, ArrayMap<String,
? extends PackageStateInternal> allSettings, UserInfo[] allUsers, int subjectUserId, ? extends PackageStateInternal> allSettings, UserInfo[] allUsers, int subjectUserId,
int maxIndex) { int maxIndex) {
@@ -1001,34 +990,36 @@ public class AppsFilter implements Watchable, Snappable {
} }
if (subjectUserId == USER_ALL) { if (subjectUserId == USER_ALL) {
for (int su = 0; su < allUsers.length; su++) { for (int su = 0; su < allUsers.length; su++) {
updateShouldFilterCacheForUser(cache, subjectSetting, allUsers, otherSetting, updateShouldFilterCacheForUser(subjectSetting, allUsers, otherSetting,
allUsers[su].id); allUsers[su].id);
} }
} else { } else {
updateShouldFilterCacheForUser(cache, subjectSetting, allUsers, otherSetting, updateShouldFilterCacheForUser(subjectSetting, allUsers, otherSetting,
subjectUserId); subjectUserId);
} }
} }
} }
private void updateShouldFilterCacheForUser(WatchedSparseBooleanMatrix cache, private void updateShouldFilterCacheForUser(
PackageStateInternal subjectSetting, UserInfo[] allUsers, PackageStateInternal subjectSetting, UserInfo[] allUsers,
PackageStateInternal otherSetting, int subjectUserId) { PackageStateInternal otherSetting, int subjectUserId) {
for (int ou = 0; ou < allUsers.length; ou++) { for (int ou = 0; ou < allUsers.length; ou++) {
int otherUser = allUsers[ou].id; int otherUser = allUsers[ou].id;
int subjectUid = UserHandle.getUid(subjectUserId, subjectSetting.getAppId()); int subjectUid = UserHandle.getUid(subjectUserId, subjectSetting.getAppId());
int otherUid = UserHandle.getUid(otherUser, otherSetting.getAppId()); int otherUid = UserHandle.getUid(otherUser, otherSetting.getAppId());
cache.put(subjectUid, otherUid, final boolean shouldFilterSubjectToOther = shouldFilterApplicationInternal(
shouldFilterApplicationInternal( subjectUid, subjectSetting, otherSetting, otherUser);
subjectUid, subjectSetting, otherSetting, otherUser)); final boolean shouldFilterOtherToSubject = shouldFilterApplicationInternal(
cache.put(otherUid, subjectUid, otherUid, otherSetting, subjectSetting, subjectUserId);
shouldFilterApplicationInternal( synchronized (mCacheLock) {
otherUid, otherSetting, subjectSetting, subjectUserId)); mShouldFilterCache.put(subjectUid, otherUid, shouldFilterSubjectToOther);
mShouldFilterCache.put(otherUid, subjectUid, shouldFilterOtherToSubject);
}
} }
} }
@GuardedBy("mCacheLock")
private void removeShouldFilterCacheForUser(int userId) { private void removeShouldFilterCacheForUser(int userId) {
synchronized (mCacheLock) {
// Sorted uids with the ascending order // Sorted uids with the ascending order
final int[] cacheUids = mShouldFilterCache.keys(); final int[] cacheUids = mShouldFilterCache.keys();
final int size = cacheUids.length; final int size = cacheUids.length;
@@ -1049,6 +1040,7 @@ public class AppsFilter implements Watchable, Snappable {
mShouldFilterCache.removeRange(fromIndex, toIndex); mShouldFilterCache.removeRange(fromIndex, toIndex);
mShouldFilterCache.compact(); mShouldFilterCache.compact();
} }
}
private static boolean isSystemSigned(@NonNull SigningDetails sysSigningDetails, private static boolean isSystemSigned(@NonNull SigningDetails sysSigningDetails,
PackageStateInternal pkgSetting) { PackageStateInternal pkgSetting) {
@@ -1171,6 +1163,7 @@ public class AppsFilter implements Watchable, Snappable {
/** /**
* Equivalent to calling {@link #addPackage(PackageStateInternal, boolean)} with * Equivalent to calling {@link #addPackage(PackageStateInternal, boolean)} with
* {@code isReplace} equal to {@code false}. * {@code isReplace} equal to {@code false}.
*
* @see AppsFilter#addPackage(PackageStateInternal, boolean) * @see AppsFilter#addPackage(PackageStateInternal, boolean)
*/ */
public void addPackage(PackageStateInternal newPkgSetting) { public void addPackage(PackageStateInternal newPkgSetting) {
@@ -1253,9 +1246,8 @@ public class AppsFilter implements Watchable, Snappable {
} }
} }
synchronized (mCacheLock) {
removeAppIdFromVisibilityCache(setting.getAppId()); removeAppIdFromVisibilityCache(setting.getAppId());
if (mShouldFilterCache != null && setting.hasSharedUser()) { if (mSystemReady && setting.hasSharedUser()) {
final ArraySet<PackageStateInternal> sharedUserPackages = final ArraySet<PackageStateInternal> sharedUserPackages =
mPmInternal.getSharedUserPackages(setting.getSharedUserAppId()); mPmInternal.getSharedUserPackages(setting.getSharedUserAppId());
for (int i = sharedUserPackages.size() - 1; i >= 0; i--) { for (int i = sharedUserPackages.size() - 1; i >= 0; i--) {
@@ -1264,13 +1256,13 @@ public class AppsFilter implements Watchable, Snappable {
if (siblingSetting == setting) { if (siblingSetting == setting) {
continue; continue;
} }
updateShouldFilterCacheForPackage(mShouldFilterCache, updateShouldFilterCacheForPackage(
setting.getPackageName(), siblingSetting, settings, users, setting.getPackageName(), siblingSetting, settings, users,
USER_ALL, settings.size()); USER_ALL, settings.size());
} }
} }
if (mShouldFilterCache != null) { if (mSystemReady) {
if (additionalChangedPackages != null) { if (additionalChangedPackages != null) {
for (int index = 0; index < additionalChangedPackages.size(); index++) { for (int index = 0; index < additionalChangedPackages.size(); index++) {
String changedPackage = additionalChangedPackages.valueAt(index); String changedPackage = additionalChangedPackages.valueAt(index);
@@ -1282,14 +1274,13 @@ public class AppsFilter implements Watchable, Snappable {
continue; continue;
} }
updateShouldFilterCacheForPackage(mShouldFilterCache, null, updateShouldFilterCacheForPackage(null,
changedPkgSetting, settings, users, USER_ALL, settings.size()); changedPkgSetting, settings, users, USER_ALL, settings.size());
} }
} }
} }
onChanged(); onChanged();
}
}); });
} }
@@ -1315,22 +1306,10 @@ public class AppsFilter implements Watchable, Snappable {
|| callingAppId == targetPkgSetting.getAppId()) { || callingAppId == targetPkgSetting.getAppId()) {
return false; return false;
} }
synchronized (mCacheLock) { if (mSystemReady) { // use cache
if (mShouldFilterCache != null) { // use cache if (!shouldFilterApplicationUsingCache(callingUid,
final int callingIndex = mShouldFilterCache.indexOfKey(callingUid); targetPkgSetting.getAppId(),
if (callingIndex < 0) { userId)) {
Slog.wtf(TAG, "Encountered calling uid with no cached rules: "
+ callingUid);
return true;
}
final int targetUid = UserHandle.getUid(userId, targetPkgSetting.getAppId());
final int targetIndex = mShouldFilterCache.indexOfKey(targetUid);
if (targetIndex < 0) {
Slog.w(TAG, "Encountered calling -> target with no cached rules: "
+ callingUid + " -> " + targetUid);
return true;
}
if (!mShouldFilterCache.valueAt(callingIndex, targetIndex)) {
return false; return false;
} }
} else { } else {
@@ -1339,7 +1318,6 @@ public class AppsFilter implements Watchable, Snappable {
return false; return false;
} }
} }
}
if (DEBUG_LOGGING || mFeatureConfig.isLoggingEnabled(callingAppId)) { if (DEBUG_LOGGING || mFeatureConfig.isLoggingEnabled(callingAppId)) {
log(callingSetting, targetPkgSetting, "BLOCKED"); log(callingSetting, targetPkgSetting, "BLOCKED");
} }
@@ -1351,6 +1329,25 @@ public class AppsFilter implements Watchable, Snappable {
} }
} }
private boolean shouldFilterApplicationUsingCache(int callingUid, int appId, int userId) {
synchronized (mCacheLock) {
final int callingIndex = mShouldFilterCache.indexOfKey(callingUid);
if (callingIndex < 0) {
Slog.wtf(TAG, "Encountered calling uid with no cached rules: "
+ callingUid);
return true;
}
final int targetUid = UserHandle.getUid(userId, appId);
final int targetIndex = mShouldFilterCache.indexOfKey(targetUid);
if (targetIndex < 0) {
Slog.w(TAG, "Encountered calling -> target with no cached rules: "
+ callingUid + " -> " + targetUid);
return true;
}
return mShouldFilterCache.valueAt(callingIndex, targetIndex);
}
}
private boolean shouldFilterApplicationInternal(int callingUid, Object callingSetting, private boolean shouldFilterApplicationInternal(int callingUid, Object callingSetting,
PackageStateInternal targetPkgSetting, int targetUserId) { PackageStateInternal targetPkgSetting, int targetUserId) {
if (DEBUG_TRACING) { if (DEBUG_TRACING) {
@@ -1747,6 +1744,7 @@ public class AppsFilter implements Watchable, Snappable {
private interface ToString<T> { private interface ToString<T> {
String toString(T input); String toString(T input);
} }
private static <T> void dumpPackageSet(PrintWriter pw, @Nullable T filteringId, private static <T> void dumpPackageSet(PrintWriter pw, @Nullable T filteringId,