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

Commit ed2cd670 authored by Mohammad Samiul Islam's avatar Mohammad Samiul Islam
Browse files

Prevent exceptions during staged install from crashing system server

An unhandled exception during staged install at boot time  will cause
system server to crash and restart. Upon restart, staged install will
resume again and it will cause system server to crash again. Thus
creating a loop.

Instead of allowing the exception to crash the system server, we now
catch it and fail the corresponding staged session with appropriate
message.

Bug: 170784748
Test: manual
Test: verified that without this fix, any unhandled exception sends
system server into a crash loop

Change-Id: I9abec5d2401af95ecb095fa3c45960d2f15d4e74
Merged-In: I9abec5d2401af95ecb095fa3c45960d2f15d4e74
(cherry picked from commit 1c3e99b6c5a13ec2d7a8d3e618ce459fec0de7b7)
parent fdf525f5
Loading
Loading
Loading
Loading
+45 −42
Original line number Original line Diff line number Diff line
@@ -564,7 +564,8 @@ public class StagingManager {
        }
        }
    }
    }


    private void resumeSession(@NonNull PackageInstallerSession session) {
    private void resumeSession(@NonNull PackageInstallerSession session)
            throws PackageManagerException {
        Slog.d(TAG, "Resuming session " + session.sessionId);
        Slog.d(TAG, "Resuming session " + session.sessionId);


        final boolean hasApex = sessionContainsApex(session);
        final boolean hasApex = sessionContainsApex(session);
@@ -628,10 +629,8 @@ public class StagingManager {
            if (apexSessionInfo == null) {
            if (apexSessionInfo == null) {
                final String errorMsg = "apexd did not know anything about a staged session "
                final String errorMsg = "apexd did not know anything about a staged session "
                        + "supposed to be activated";
                        + "supposed to be activated";
                session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED,
                throw new PackageManagerException(
                        errorMsg);
                        SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, errorMsg);
                abortCheckpoint(session.sessionId, errorMsg);
                return;
            }
            }
            if (isApexSessionFailed(apexSessionInfo)) {
            if (isApexSessionFailed(apexSessionInfo)) {
                String errorMsg = "APEX activation failed. Check logcat messages from apexd "
                String errorMsg = "APEX activation failed. Check logcat messages from apexd "
@@ -640,10 +639,8 @@ public class StagingManager {
                    errorMsg = "Session reverted due to crashing native process: "
                    errorMsg = "Session reverted due to crashing native process: "
                            + mNativeFailureReason;
                            + mNativeFailureReason;
                }
                }
                session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED,
                throw new PackageManagerException(
                        errorMsg);
                        SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, errorMsg);
                abortCheckpoint(session.sessionId, errorMsg);
                return;
            }
            }
            if (!apexSessionInfo.isActivated && !apexSessionInfo.isSuccess) {
            if (!apexSessionInfo.isActivated && !apexSessionInfo.isSuccess) {
                // Apexd did not apply the session for some unknown reason. There is no
                // Apexd did not apply the session for some unknown reason. There is no
@@ -651,14 +648,11 @@ public class StagingManager {
                // it as failed.
                // it as failed.
                final String errorMsg = "Staged session " + session.sessionId + "at boot "
                final String errorMsg = "Staged session " + session.sessionId + "at boot "
                        + "didn't activate nor fail. Marking it as failed anyway.";
                        + "didn't activate nor fail. Marking it as failed anyway.";
                session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED,
                throw new PackageManagerException(
                        errorMsg);
                        SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, errorMsg);
                abortCheckpoint(session.sessionId, errorMsg);
                return;
            }
            }
        }
        }
        // Handle apk and apk-in-apex installation
        // Handle apk and apk-in-apex installation
        try {
        if (hasApex) {
        if (hasApex) {
            checkInstallationOfApkInApexSuccessful(session);
            checkInstallationOfApkInApexSuccessful(session);
            snapshotAndRestoreForApexSession(session);
            snapshotAndRestoreForApexSession(session);
@@ -668,25 +662,6 @@ public class StagingManager {
        // The APEX part of the session is activated, proceed with the installation of APKs.
        // The APEX part of the session is activated, proceed with the installation of APKs.
        Slog.d(TAG, "Installing APK packages in session " + session.sessionId);
        Slog.d(TAG, "Installing APK packages in session " + session.sessionId);
        installApksInSession(session);
        installApksInSession(session);
        } catch (PackageManagerException e) {
            session.setStagedSessionFailed(e.error, e.getMessage());
            abortCheckpoint(session.sessionId, e.getMessage());

            // If checkpoint is not supported, we have to handle failure for one staged session.
            if (!hasApex) {
                return;
            }

            if (!mApexManager.revertActiveSessions()) {
                Slog.e(TAG, "Failed to abort APEXd session");
            } else {
                Slog.e(TAG,
                        "Successfully aborted apexd session. Rebooting device in order to revert "
                                + "to the previous state of APEXd.");
                mPowerManager.reboot(null);
            }
            return;
        }


        Slog.d(TAG, "Marking session " + session.sessionId + " as applied");
        Slog.d(TAG, "Marking session " + session.sessionId + " as applied");
        session.setStagedSessionApplied();
        session.setStagedSessionApplied();
@@ -722,6 +697,25 @@ public class StagingManager {
        return ret;
        return ret;
    }
    }


    void onInstallationFailure(PackageInstallerSession session, PackageManagerException e) {
        session.setStagedSessionFailed(e.error, e.getMessage());
        abortCheckpoint(session.sessionId, e.getMessage());

        // If checkpoint is not supported, we have to handle failure for one staged session.
        if (!sessionContainsApex(session)) {
            return;
        }

        if (!mApexManager.revertActiveSessions()) {
            Slog.e(TAG, "Failed to abort APEXd session");
        } else {
            Slog.e(TAG,
                    "Successfully aborted apexd session. Rebooting device in order to revert "
                            + "to the previous state of APEXd.");
            mPowerManager.reboot(null);
        }
    }

    @NonNull
    @NonNull
    private PackageInstallerSession createAndWriteApkSession(
    private PackageInstallerSession createAndWriteApkSession(
            @NonNull PackageInstallerSession originalSession, boolean preReboot)
            @NonNull PackageInstallerSession originalSession, boolean preReboot)
@@ -1185,7 +1179,16 @@ public class StagingManager {
        } else {
        } else {
            // Session had already being marked ready. Start the checks to verify if there is any
            // Session had already being marked ready. Start the checks to verify if there is any
            // follow-up work.
            // follow-up work.
            try {
                resumeSession(session);
                resumeSession(session);
            } catch (PackageManagerException e) {
                onInstallationFailure(session, e);
            } catch (Exception e) {
                Slog.e(TAG, "Staged install failed due to unhandled exception", e);
                onInstallationFailure(session, new PackageManagerException(
                        SessionInfo.STAGED_SESSION_ACTIVATION_FAILED,
                        "Staged install failed due to unhandled exception: " + e));
            }
        }
        }
    }
    }