Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a5960576 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Disable SystemFeaturesCache in cmd-like system processes" into main

parents 14a2512c a12bb0b3
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -200,6 +200,8 @@ public class ApplicationPackageManager extends PackageManager {
    @GuardedBy("mPackageMonitorCallbacks")
    private final ArraySet<IRemoteCallback> mPackageMonitorCallbacks = new ArraySet<>();

    private final boolean mUseSystemFeaturesCache;

    UserManager getUserManager() {
        if (mUserManager == null) {
            mUserManager = UserManager.get(mContext);
@@ -824,8 +826,7 @@ public class ApplicationPackageManager extends PackageManager {
        if (maybeHasSystemFeature != null) {
            return maybeHasSystemFeature;
        }
        if (com.android.internal.os.Flags.applicationSharedMemoryEnabled()
                && android.content.pm.Flags.cacheSdkSystemFeatures()) {
        if (mUseSystemFeaturesCache) {
            maybeHasSystemFeature =
                    SystemFeaturesCache.getInstance().maybeHasFeature(name, version);
            if (maybeHasSystemFeature != null) {
@@ -2221,6 +2222,25 @@ public class ApplicationPackageManager extends PackageManager {
    protected ApplicationPackageManager(ContextImpl context, IPackageManager pm) {
        mContext = context;
        mPM = pm;
        mUseSystemFeaturesCache = isSystemFeaturesCacheEnabledAndAvailable();
    }

    private static boolean isSystemFeaturesCacheEnabledAndAvailable() {
        if (!android.content.pm.Flags.cacheSdkSystemFeatures()) {
            return false;
        }
        if (!com.android.internal.os.Flags.applicationSharedMemoryEnabled()) {
            return false;
        }
        if (ActivityThread.isSystem() && !SystemFeaturesCache.hasInstance()) {
            // There are a handful of utility "system" processes that are neither system_server nor
            // bound as applications. For these processes, we don't have access to application
            // shared memory or the dependent system features cache.
            // TODO(b/400713460): Revisit this exception after deprecating these command-like
            // system processes.
            return false;
        }
        return true;
    }

    /**
+5 −0
Original line number Diff line number Diff line
@@ -74,6 +74,11 @@ public final class SystemFeaturesCache {
        return instance;
    }

    /** Checks for existence of the process-global instance. */
    public static boolean hasInstance() {
        return sInstance != null;
    }

    /** Clears the process-global cache instance for testing. */
    @VisibleForTesting
    public static void clearInstance() {
+3 −0
Original line number Diff line number Diff line
@@ -136,9 +136,11 @@ public class SystemFeaturesCacheTest {
        SystemFeaturesCache cache = new SystemFeaturesCache(features);

        SystemFeaturesCache.clearInstance();
        assertThat(SystemFeaturesCache.hasInstance()).isFalse();
        assertThrows(IllegalStateException.class, () -> SystemFeaturesCache.getInstance());

        SystemFeaturesCache.setInstance(cache);
        assertThat(SystemFeaturesCache.hasInstance()).isTrue();
        assertThat(SystemFeaturesCache.getInstance()).isEqualTo(cache);

        assertThrows(
@@ -149,6 +151,7 @@ public class SystemFeaturesCacheTest {
    @Test
    public void testSingletonAutomaticallySetWithFeatureEnabled() {
        assumeTrue(android.content.pm.Flags.cacheSdkSystemFeatures());
        assertThat(SystemFeaturesCache.hasInstance()).isTrue();
        assertThat(SystemFeaturesCache.getInstance()).isNotNull();
    }