Merge "Stop adding large proto tombstones to dropbox." into main am: d1fb7e4a36

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/3107962

Change-Id: I5468a00e3bb1062ce84ddd7469fc5bc3f1449f0a
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/services/core/java/com/android/server/BootReceiver.java b/services/core/java/com/android/server/BootReceiver.java
index f69a521..23cee9d 100644
--- a/services/core/java/com/android/server/BootReceiver.java
+++ b/services/core/java/com/android/server/BootReceiver.java
@@ -141,6 +141,10 @@
     private static final int MAX_ERROR_REPORTS = 8;
     private static int sSentReports = 0;
 
+    // Max tombstone file size to add to dropbox.
+    private static final long MAX_TOMBSTONE_SIZE_BYTES =
+            DropBoxManagerService.DEFAULT_QUOTA_KB * 1024;
+
     @Override
     public void onReceive(final Context context, Intent intent) {
         // Log boot events in the background to avoid blocking the main thread with I/O
@@ -390,6 +394,12 @@
     private static void addAugmentedProtoToDropbox(
                 File tombstone, DropBoxManager db,
                 DropboxRateLimiter.RateLimitResult rateLimitResult) throws IOException {
+        // Do not add proto files larger than 20Mb to DropBox as they can cause OOMs when
+        // processing large tombstones. The text tombstone is still added to DropBox.
+        if (tombstone.length() > MAX_TOMBSTONE_SIZE_BYTES) {
+            Slog.w(TAG, "Tombstone too large to add to DropBox: " + tombstone.toPath());
+            return;
+        }
         // Read the proto tombstone file as bytes.
         final byte[] tombstoneBytes = Files.readAllBytes(tombstone.toPath());
 
diff --git a/services/core/java/com/android/server/DropBoxManagerService.java b/services/core/java/com/android/server/DropBoxManagerService.java
index 748253f..70b3bde 100644
--- a/services/core/java/com/android/server/DropBoxManagerService.java
+++ b/services/core/java/com/android/server/DropBoxManagerService.java
@@ -106,7 +106,7 @@
     private static final int DEFAULT_AGE_SECONDS = 3 * 86400;
     private static final int DEFAULT_MAX_FILES = 1000;
     private static final int DEFAULT_MAX_FILES_LOWRAM = 300;
-    private static final int DEFAULT_QUOTA_KB = Build.IS_USERDEBUG ? 20 * 1024 : 10 * 1024;
+    public static final int DEFAULT_QUOTA_KB = Build.IS_USERDEBUG ? 20 * 1024 : 10 * 1024;
     private static final int DEFAULT_QUOTA_PERCENT = 10;
     private static final int DEFAULT_RESERVE_PERCENT = 0;
     private static final int QUOTA_RESCAN_MILLIS = 5000;