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

Commit 9b36f3fd authored by Jared Duke's avatar Jared Duke
Browse files

Fix a minor data race in PropertyInvalidatedCache

Ensure that the nonce value is set as part of its initialiation, within
the same lock scope. This ensures any pending reads that are waiting on
that initialization get the initial value.

Bug: 411728421
Test: presubmit
Flag: EXEMPT minor bugfix
Change-Id: Ibae17e14b762ca547e6ed321a903c07755078c95
parent 62e8a512
Loading
Loading
Loading
Loading
+30 −23
Original line number Diff line number Diff line
@@ -974,8 +974,8 @@ public class PropertyInvalidatedCache<Query, Result> {
        //
        // If the "update" boolean is true, then the property is registered with the nonce store
        // before the associated handle is fetched.
        private int initialize(boolean update) {
            synchronized (mLock) {
        @GuardedBy("mLock")
        private int initializeLocked(boolean update) {
            int handle = mHandle;
            if (handle == NonceStore.INVALID_NONCE_INDEX) {
                if (mStore == null) {
@@ -996,7 +996,6 @@ public class PropertyInvalidatedCache<Query, Result> {
            }
            return handle;
        }
        }

        // Fetch the nonce from shared memory.  If the shared memory is not available, return
        // UNSET.  If the shared memory is available but the nonce name is not known (it may not
@@ -1005,11 +1004,13 @@ public class PropertyInvalidatedCache<Query, Result> {
        long getNonceInternal() {
            int handle = mHandle;
            if (handle == NonceStore.INVALID_NONCE_INDEX) {
                handle = initialize(false);
                synchronized (mLock) {
                    handle = initializeLocked(false);
                    if (handle == NonceStore.INVALID_NONCE_INDEX) {
                        return NONCE_UNSET;
                    }
                }
            }
            return mStore.getNonce(handle);
        }

@@ -1019,10 +1020,16 @@ public class PropertyInvalidatedCache<Query, Result> {
        void setNonceInternal(long value) {
            int handle = mHandle;
            if (handle == NonceStore.INVALID_NONCE_INDEX) {
                handle = initialize(true);
                synchronized (mLock) {
                    handle = initializeLocked(true);
                    if (handle == NonceStore.INVALID_NONCE_INDEX) {
                        throw new IllegalStateException("unable to assign nonce handle: " + mName);
                    }
                    // Note that we set the value within the lock, ensuring it takes effect
                    // immediately upon initialization, before any pending getNonce reads.
                    mStore.setNonce(handle, value);
                    return;
                }
            }
            mStore.setNonce(handle, value);
        }