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

Commit 883ee642 authored by Xiang Wang's avatar Xiang Wang
Browse files

Set max number of sessions to 20 for non-system process

Bug: b/296160319
Test: atest HintManagerServiceTest
Change-Id: I1cdf6dc7f1b4047d141a686e01130a1876484587
parent 36ef5e5d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -144,6 +144,8 @@ APerformanceHintSession* APerformanceHintManager::createSession(
    binder::Status ret =
            mHintManager->createHintSession(mToken, tids, initialTargetWorkDurationNanos, &session);
    if (!ret.isOk() || !session) {
        ALOGE("%s: PerformanceHint cannot create hint session. %s", __FUNCTION__,
              ret.exceptionMessage().c_str());
        return nullptr;
    }
    return new APerformanceHintSession(mHintManager, std::move(session), mPreferredRateNanos,
+18 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ import java.util.Objects;
public final class HintManagerService extends SystemService {
    private static final String TAG = "HintManagerService";
    private static final boolean DEBUG = false;
    private static final int MAX_HINT_SESSION_COUNT_PER_UID = 20;
    @VisibleForTesting final long mHintSessionPreferredRate;

    // Multi-level map storing all active AppHintSessions.
@@ -367,6 +368,23 @@ public final class HintManagerService extends SystemService {
                    + " not be empty.");

            final int callingUid = Binder.getCallingUid();
            if (callingUid != Process.SYSTEM_UID) {
                int sessionCount = 0;
                synchronized (mLock) {
                    ArrayMap<IBinder, ArraySet<AppHintSession>> tokenMap =
                            mActiveSessions.get(callingUid);
                    if (tokenMap != null) {
                        for (ArraySet<AppHintSession> arr : tokenMap.values()) {
                            sessionCount += arr.size();
                        }
                    }
                }
                if (sessionCount >= MAX_HINT_SESSION_COUNT_PER_UID) {
                    throw new IllegalStateException(
                            "Max session count limit reached: " + sessionCount);
                }
            }

            final int callingTgid = Process.getThreadGroupLeader(Binder.getCallingPid());
            final long identity = Binder.clearCallingIdentity();
            try {
+26 −0
Original line number Diff line number Diff line
@@ -148,6 +148,32 @@ public class HintManagerServiceTest {
        assertNotNull(c);
    }

    @Test
    public void testCreateHintSession_exceedsLimit() throws Exception {
        HintManagerService service = createService();
        IBinder token1 = new Binder();
        IBinder token2 = new Binder();

        for (int i = 0; i < 10; i++) {
            IHintSession a = service.getBinderServiceInstance().createHintSession(token1,
                    SESSION_TIDS_A, DEFAULT_TARGET_DURATION);
            assertNotNull(a);
        }

        for (int i = 0; i < 10; i++) {
            IHintSession b = service.getBinderServiceInstance().createHintSession(token2,
                    SESSION_TIDS_B, DEFAULT_TARGET_DURATION);
            assertNotNull(b);
        }

        assertThrows(IllegalStateException.class,
                () -> service.getBinderServiceInstance().createHintSession(token1, SESSION_TIDS_A,
                        DEFAULT_TARGET_DURATION));
        assertThrows(IllegalStateException.class,
                () -> service.getBinderServiceInstance().createHintSession(token2, SESSION_TIDS_B,
                        DEFAULT_TARGET_DURATION));
    }

    @Test
    public void testPauseResumeHintSession() throws Exception {
        HintManagerService service = createService();