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

Commit 0a108229 authored by gang.huang's avatar gang.huang Committed by Yurii Zubrytskyi
Browse files

Add lock for OverlayManagerService.commit

+ add GuardedBy annotations

Bug: 383043458
Test: manual.
Flag: EXEMPT bugfix
Change-Id: I241af7bf583c589bcccede95167aca077ecdf287
parent 53471fa4
Loading
Loading
Loading
Loading
+64 −55
Original line number Diff line number Diff line
@@ -251,14 +251,17 @@ public final class OverlayManagerService extends SystemService {

    private final Object mLock = new Object();

    @GuardedBy("mLock")
    private final AtomicFile mSettingsFile;

    private final PackageManagerHelperImpl mPackageManager;

    private final UserManagerService mUserManager;

    @GuardedBy("mLock")
    private final OverlayManagerSettings mSettings;

    @GuardedBy("mLock")
    private final OverlayManagerServiceImpl mImpl;

    private final OverlayActorEnforcer mActorEnforcer;
@@ -296,7 +299,9 @@ public final class OverlayManagerService extends SystemService {
            UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class);
            umi.addUserLifecycleListener(new UserLifecycleListener());

            restoreSettings();
            // No async stuff happening in the constructor yet, so it's OK to call a ...Locked()
            // method without a lock here.
            restoreSettingsLocked();

            // Wipe all shell overlays on boot, to recover from a potentially broken device
            String shellPkgName = TextUtils.emptyIfNull(
@@ -902,12 +907,13 @@ public final class OverlayManagerService extends SystemService {
                throws RemoteException {
            try {
                traceBegin(TRACE_TAG_RRO, "OMS#commit " + transaction);
                synchronized (mLock) {
                    try {
                    executeAllRequests(transaction);
                        executeAllRequestsLocked(transaction);
                    } catch (Exception e) {
                        final long ident = Binder.clearCallingIdentity();
                        try {
                        restoreSettings();
                            restoreSettingsLocked();
                        } finally {
                            Binder.restoreCallingIdentity(ident);
                        }
@@ -915,12 +921,14 @@ public final class OverlayManagerService extends SystemService {
                        throw new SecurityException("commit failed"
                                + (DEBUG || Build.IS_DEBUGGABLE ? ": " + e.getMessage() : ""));
                    }
                }
            } finally {
                traceEnd(TRACE_TAG_RRO);
            }
        }

        private Set<UserPackage> executeRequest(
        @GuardedBy("mLock")
        private Set<UserPackage> executeRequestLocked(
                @NonNull final OverlayManagerTransaction.Request request)
                throws OperationFailedException {
            Objects.requireNonNull(request, "Transaction contains a null request");
@@ -995,22 +1003,19 @@ public final class OverlayManagerService extends SystemService {
            }
        }

        private void executeAllRequests(@NonNull final OverlayManagerTransaction transaction)
        @GuardedBy("mLock")
        private void executeAllRequestsLocked(@NonNull final OverlayManagerTransaction transaction)
                throws OperationFailedException {
            if (DEBUG) {
                Slog.d(TAG, "commit " + transaction);
            }
            if (transaction == null) {
                throw new IllegalArgumentException("null transaction");
            }

            synchronized (mLock) {
            // execute the requests (as calling user)
            Set<UserPackage> affectedPackagesToUpdate = null;
            for (Iterator<Request> it = transaction.getRequests(); it.hasNext(); ) {
                Request request = it.next();
                affectedPackagesToUpdate = CollectionUtils.addAll(affectedPackagesToUpdate,
                            executeRequest(request));
                        executeRequestLocked(request));
            }

            // past the point of no return: the entire transaction has been
@@ -1023,7 +1028,6 @@ public final class OverlayManagerService extends SystemService {
                Binder.restoreCallingIdentity(ident);
            }
        }
        }

        @Override
        public void onShellCommand(@NonNull final FileDescriptor in,
@@ -1447,12 +1451,14 @@ public final class OverlayManagerService extends SystemService {
        }
    }

    @GuardedBy("mLock")
    private void updateTargetPackagesLocked(@Nullable UserPackage updatedTarget) {
        if (updatedTarget != null) {
            updateTargetPackagesLocked(Set.of(updatedTarget));
        }
    }

    @GuardedBy("mLock")
    private void updateTargetPackagesLocked(@Nullable Set<UserPackage> updatedTargets) {
        if (CollectionUtils.isEmpty(updatedTargets)) {
            return;
@@ -1548,6 +1554,7 @@ public final class OverlayManagerService extends SystemService {
    }

    @NonNull
    @GuardedBy("mLock")
    private SparseArray<List<String>> updatePackageManagerLocked(
            @Nullable Set<UserPackage> targets) {
        if (CollectionUtils.isEmpty(targets)) {
@@ -1568,6 +1575,7 @@ public final class OverlayManagerService extends SystemService {
     *         targetPackageNames: the target themselves and shared libraries)
     */
    @NonNull
    @GuardedBy("mLock")
    private List<String> updatePackageManagerLocked(@NonNull Collection<String> targetPackageNames,
            final int userId) {
        try {
@@ -1623,6 +1631,7 @@ public final class OverlayManagerService extends SystemService {
        }
    }

    @GuardedBy("mLock")
    private void persistSettingsLocked() {
        if (DEBUG) {
            Slog.d(TAG, "Writing overlay settings");
@@ -1638,10 +1647,11 @@ public final class OverlayManagerService extends SystemService {
        }
    }

    private void restoreSettings() {
    @GuardedBy("mLock")
    private void restoreSettingsLocked() {
        try {
            traceBegin(TRACE_TAG_RRO, "OMS#restoreSettings");
            synchronized (mLock) {

            if (!mSettingsFile.getBaseFile().exists()) {
                return;
            }
@@ -1667,7 +1677,6 @@ public final class OverlayManagerService extends SystemService {
            } catch (IOException | XmlPullParserException e) {
                Slog.e(TAG, "failed to restore overlay state", e);
            }
            }
        } finally {
            traceEnd(TRACE_TAG_RRO);
        }