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: If79073a9a40c70e8223acbc47a3da394eb54fb43
Merged-In: I9abec5d2401af95ecb095fa3c45960d2f15d4e74
(cherry picked from commit 1c3e99b6c5a13ec2d7a8d3e618ce459fec0de7b7)
diff --git a/services/core/java/com/android/server/pm/StagingManager.java b/services/core/java/com/android/server/pm/StagingManager.java
index 96dd001..5adab3e 100644
--- a/services/core/java/com/android/server/pm/StagingManager.java
+++ b/services/core/java/com/android/server/pm/StagingManager.java
@@ -353,22 +353,9 @@
         }
         // The APEX part of the session is activated, proceed with the installation of APKs.
         if (!installApksInSession(session, /* preReboot */ false)) {
-            session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED,
-                    "Staged installation of APKs failed. Check logcat messages for"
-                        + "more information.");
-
-            if (!hasApex) {
-                return;
-            }
-
-            if (!mApexManager.abortActiveSession()) {
-                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);
-            }
+            onInstallationFailure(session, new PackageManagerException(
+                    SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, "Staged installation of APKs "
+                    + "failed. Check logcat messages for more information."));
             return;
         }
 
@@ -378,6 +365,21 @@
         }
     }
 
+    void onInstallationFailure(PackageInstallerSession session, PackageManagerException e) {
+        session.setStagedSessionFailed(e.error, e.getMessage());
+        if (!sessionContainsApex(session)) {
+            return;
+        }
+        if (!mApexManager.abortActiveSession()) {
+            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);
+        }
+    }
+
     private List<String> findAPKsInDir(File stageDir) {
         List<String> ret = new ArrayList<>();
         if (stageDir != null && stageDir.exists()) {
@@ -671,7 +673,15 @@
         } else {
             // Session had already being marked ready. Start the checks to verify if there is any
             // follow-up work.
-            resumeSession(session);
+            try {
+                resumeSession(session);
+            } 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));
+
+            }
         }
     }