From dace93abb3cd5bf5cf7bd722d688b0ae302a4722 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Mon, 11 Jun 2018 23:02:40 -0700 Subject: [PATCH] Upgrade SQLCipher to respect a max window size. Previously, SQLCipher's memory usage would grow indefinitely, up until it hit the end of the cursor. We've now switched to a release where the memory used by the cursor can be bounded. --- build.gradle | 4 ++-- .../database/helpers/SQLCipherOpenHelper.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index c6346dc9c5..f0ec8c4e37 100644 --- a/build.gradle +++ b/build.gradle @@ -125,7 +125,7 @@ dependencies { } compile 'com.codewaves.stickyheadergrid:stickyheadergrid:0.9.4' compile 'com.github.dmytrodanylyk.circular-progress-button:library:1.1.3-S2' - compile 'org.signal:android-database-sqlcipher:3.5.9-S1' + compile 'org.signal:android-database-sqlcipher:3.5.9-S2' compile ('com.googlecode.ez-vcard:ez-vcard:0.9.11') { exclude group: 'com.fasterxml.jackson.core' exclude group: 'org.freemarker' @@ -198,7 +198,7 @@ dependencyVerification { 'com.annimon:stream:5da6e2e3e0551d61a3ea7014f04312276549e3dd739cf637996e4cf43c5535b9', 'com.takisoft.fix:colorpicker:f5d0dbabe406a1800498ca9c1faf34db36e021d8488bf10360f29961fe3ab0d1', 'com.github.dmytrodanylyk.circular-progress-button:library:8dc6a29a5a8db7b2ad5a9a7fda1dc9ae0893f4c8f0545732b2c63854ea693e8e', - 'org.signal:android-database-sqlcipher:4302551df258883cc5dc5d62ddb141a6b5b8f113d77d70322dc2648c0856ccef', + 'org.signal:android-database-sqlcipher:4d682fccefb21cdeb10f856a55c86ede381f91137eae8a7a5746c9df415a6aed', 'com.googlecode.ez-vcard:ez-vcard:7e24ad50b222d2f70ac91bdccfa3c0f6200b078d797cb784837f75e77bb4210f', 'com.google.android.gms:play-services-iid:54e919f9957b8b7820da7ee9b83471d00d0cac1cf08ddea8b5b41aea80bb1a70', 'com.google.android.gms:play-services-base:0ca636a8fc9a5af45e607cdcd61783bf5d561cbbb0f862021ce69606eee5ad49', diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 4338952e4c..a776e758cd 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -8,6 +8,9 @@ import android.os.SystemClock; import android.support.annotation.NonNull; import android.util.Log; +import net.sqlcipher.CursorWindow; +import net.sqlcipher.CursorWindowAllocation; +import net.sqlcipher.CustomCursorWindowAllocation; import net.sqlcipher.database.SQLiteDatabase; import net.sqlcipher.database.SQLiteDatabaseHook; import net.sqlcipher.database.SQLiteOpenHelper; @@ -52,6 +55,14 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 9; private static final String DATABASE_NAME = "signal.db"; + private static final int MEMORY_INITIAL = 128 * 1024; + private static final int MEMORY_GROW = 1024 * 1024; + private static final int MEMORY_MAX = 4 * 1024 * 1024; + private static final CursorWindowAllocation CURSOR_ALLOCATION = new CustomCursorWindowAllocation(MEMORY_INITIAL, MEMORY_GROW, MEMORY_MAX); + static { + CursorWindow.setCursorWindowAllocation(CURSOR_ALLOCATION); + } + private final Context context; private final DatabaseSecret databaseSecret;