Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package android.os; |
| 17 | |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 18 | import android.animation.ValueAnimator; |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jeff Sharkey | ddc3bfd | 2017-06-26 19:38:56 -0600 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 24 | import android.app.ActivityThread; |
Brad Fitzpatrick | 1065685 | 2010-11-23 19:01:13 -0800 | [diff] [blame] | 25 | import android.app.IActivityManager; |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.Context; |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 29 | import android.content.ServiceConnection; |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 30 | import android.content.pm.ApplicationInfo; |
Jeff Sharkey | c59a5e7 | 2018-06-21 19:14:58 -0600 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 32 | import android.net.TrafficStats; |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 33 | import android.net.Uri; |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 34 | import android.os.storage.IStorageManager; |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 35 | import android.os.strictmode.CleartextNetworkViolation; |
| 36 | import android.os.strictmode.ContentUriWithoutPermissionViolation; |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 37 | import android.os.strictmode.CredentialProtectedWhileLockedViolation; |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 38 | import android.os.strictmode.CustomViolation; |
| 39 | import android.os.strictmode.DiskReadViolation; |
| 40 | import android.os.strictmode.DiskWriteViolation; |
Pete Gillin | 22316b4 | 2018-02-15 17:17:27 +0000 | [diff] [blame] | 41 | import android.os.strictmode.ExplicitGcViolation; |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 42 | import android.os.strictmode.FileUriExposedViolation; |
Jeff Sharkey | c59a5e7 | 2018-06-21 19:14:58 -0600 | [diff] [blame] | 43 | import android.os.strictmode.ImplicitDirectBootViolation; |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 44 | import android.os.strictmode.InstanceCountViolation; |
| 45 | import android.os.strictmode.IntentReceiverLeakedViolation; |
| 46 | import android.os.strictmode.LeakedClosableViolation; |
| 47 | import android.os.strictmode.NetworkViolation; |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 48 | import android.os.strictmode.NonSdkApiUsedViolation; |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 49 | import android.os.strictmode.ResourceMismatchViolation; |
| 50 | import android.os.strictmode.ServiceConnectionLeakedViolation; |
| 51 | import android.os.strictmode.SqliteObjectLeakedViolation; |
Kurt Nelson | 0e61280 | 2017-11-09 17:25:40 -0800 | [diff] [blame] | 52 | import android.os.strictmode.UnbufferedIoViolation; |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 53 | import android.os.strictmode.UntaggedSocketViolation; |
| 54 | import android.os.strictmode.Violation; |
| 55 | import android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation; |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 56 | import android.util.ArrayMap; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 57 | import android.util.Log; |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 58 | import android.util.Printer; |
Brad Fitzpatrick | cdcb73e | 2010-11-22 22:56:23 -0800 | [diff] [blame] | 59 | import android.util.Singleton; |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 60 | import android.util.Slog; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 61 | import android.view.IWindowManager; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 62 | |
Kurt Nelson | 1981f42 | 2017-10-25 13:49:26 -0700 | [diff] [blame] | 63 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | da4e50f | 2017-11-06 15:23:03 -0700 | [diff] [blame] | 64 | import com.android.internal.os.BackgroundThread; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 65 | import com.android.internal.os.RuntimeInit; |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 66 | import com.android.internal.util.FastPrintWriter; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 67 | import com.android.internal.util.HexDump; |
| 68 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 69 | import dalvik.system.BlockGuard; |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 70 | import dalvik.system.CloseGuard; |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 71 | import dalvik.system.VMDebug; |
Jeff Sharkey | 0b55870 | 2017-01-26 16:48:01 -0700 | [diff] [blame] | 72 | import dalvik.system.VMRuntime; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 73 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 74 | import java.io.PrintWriter; |
| 75 | import java.io.StringWriter; |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 76 | import java.lang.annotation.Retention; |
| 77 | import java.lang.annotation.RetentionPolicy; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 78 | import java.net.InetAddress; |
| 79 | import java.net.UnknownHostException; |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 80 | import java.util.ArrayDeque; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 81 | import java.util.ArrayList; |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 82 | import java.util.Arrays; |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 83 | import java.util.Deque; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 84 | import java.util.HashMap; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 85 | import java.util.concurrent.Executor; |
| 86 | import java.util.concurrent.RejectedExecutionException; |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 87 | import java.util.concurrent.atomic.AtomicInteger; |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 88 | import java.util.function.Consumer; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 89 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 90 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 91 | * StrictMode is a developer tool which detects things you might be doing by accident and brings |
| 92 | * them to your attention so you can fix them. |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 93 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 94 | * <p>StrictMode is most commonly used to catch accidental disk or network access on the |
| 95 | * application's main thread, where UI operations are received and animations take place. Keeping |
| 96 | * disk and network operations off the main thread makes for much smoother, more responsive |
| 97 | * applications. By keeping your application's main thread responsive, you also prevent <a |
| 98 | * href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a> from being shown to |
| 99 | * users. |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 100 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 101 | * <p class="note">Note that even though an Android device's disk is often on flash memory, many |
| 102 | * devices run a filesystem on top of that memory with very limited concurrency. It's often the case |
| 103 | * that almost all disk accesses are fast, but may in individual cases be dramatically slower when |
| 104 | * certain I/O is happening in the background from other processes. If possible, it's best to assume |
| 105 | * that such things are not fast. |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 106 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 107 | * <p>Example code to enable from early in your {@link android.app.Application}, {@link |
| 108 | * android.app.Activity}, or other application component's {@link android.app.Application#onCreate} |
| 109 | * method: |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 110 | * |
| 111 | * <pre> |
| 112 | * public void onCreate() { |
| 113 | * if (DEVELOPER_MODE) { |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 114 | * StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}() |
| 115 | * .detectDiskReads() |
| 116 | * .detectDiskWrites() |
| 117 | * .detectNetwork() // or .detectAll() for all detectable problems |
| 118 | * .penaltyLog() |
| 119 | * .build()); |
| 120 | * StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}() |
Brad Fitzpatrick | 62a1eb5 | 2010-10-18 14:32:59 -0700 | [diff] [blame] | 121 | * .detectLeakedSqlLiteObjects() |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 122 | * .detectLeakedClosableObjects() |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 123 | * .penaltyLog() |
| 124 | * .penaltyDeath() |
| 125 | * .build()); |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 126 | * } |
| 127 | * super.onCreate(); |
| 128 | * } |
| 129 | * </pre> |
| 130 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 131 | * <p>You can decide what should happen when a violation is detected. For example, using {@link |
| 132 | * ThreadPolicy.Builder#penaltyLog} you can watch the output of <code>adb logcat</code> while you |
| 133 | * use your application to see the violations as they happen. |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 134 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 135 | * <p>If you find violations that you feel are problematic, there are a variety of tools to help |
| 136 | * solve them: threads, {@link android.os.Handler}, {@link android.os.AsyncTask}, {@link |
| 137 | * android.app.IntentService}, etc. But don't feel compelled to fix everything that StrictMode |
| 138 | * finds. In particular, many cases of disk access are often necessary during the normal activity |
| 139 | * lifecycle. Use StrictMode to find things you did by accident. Network requests on the UI thread |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 140 | * are almost always a problem, though. |
| 141 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 142 | * <p class="note">StrictMode is not a security mechanism and is not guaranteed to find all disk or |
| 143 | * network accesses. While it does propagate its state across process boundaries when doing {@link |
| 144 | * android.os.Binder} calls, it's still ultimately a best effort mechanism. Notably, disk or network |
| 145 | * access from JNI calls won't necessarily trigger it. Future versions of Android may catch more (or |
| 146 | * fewer) operations, so you should never leave StrictMode enabled in applications distributed on |
| 147 | * Google Play. |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 148 | */ |
| 149 | public final class StrictMode { |
| 150 | private static final String TAG = "StrictMode"; |
Brad Fitzpatrick | 82829ef | 2010-11-18 18:25:08 -0800 | [diff] [blame] | 151 | private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 152 | |
Brad Fitzpatrick | c1a968a | 2010-11-24 08:56:40 -0800 | [diff] [blame] | 153 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 154 | * Boolean system property to disable strict mode checks outright. Set this to 'true' to force |
| 155 | * disable; 'false' has no effect on other enable/disable policy. |
| 156 | * |
Christopher Tate | bc6f0ce | 2011-11-03 12:18:43 -0700 | [diff] [blame] | 157 | * @hide |
| 158 | */ |
| 159 | public static final String DISABLE_PROPERTY = "persist.sys.strictmode.disable"; |
| 160 | |
| 161 | /** |
Brad Fitzpatrick | c1a968a | 2010-11-24 08:56:40 -0800 | [diff] [blame] | 162 | * The boolean system property to control screen flashes on violations. |
| 163 | * |
| 164 | * @hide |
| 165 | */ |
| 166 | public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual"; |
| 167 | |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 168 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 169 | * Temporary property used to include {@link #DETECT_VM_CLEARTEXT_NETWORK} in {@link |
| 170 | * VmPolicy.Builder#detectAll()}. Apps can still always opt-into detection using {@link |
| 171 | * VmPolicy.Builder#detectCleartextNetwork()}. |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 172 | */ |
Jeff Sharkey | 2e57164 | 2015-01-22 11:51:43 -0700 | [diff] [blame] | 173 | private static final String CLEARTEXT_PROPERTY = "persist.sys.strictmode.clear"; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 174 | |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 175 | /** |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 176 | * Quick feature-flag that can be used to disable the defaults provided by {@link |
| 177 | * #initThreadDefaults(ApplicationInfo)} and {@link #initVmDefaults(ApplicationInfo)}. |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 178 | */ |
Jeff Sharkey | 617e113 | 2017-11-03 17:28:45 -0600 | [diff] [blame] | 179 | private static final boolean DISABLE = false; |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 180 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 181 | // Only apply VM penalties for the same violation at this interval. |
| 182 | private static final long MIN_VM_INTERVAL_MS = 1000; |
| 183 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 184 | // Only log a duplicate stack trace to the logs every second. |
| 185 | private static final long MIN_LOG_INTERVAL_MS = 1000; |
| 186 | |
| 187 | // Only show an annoying dialog at most every 30 seconds |
| 188 | private static final long MIN_DIALOG_INTERVAL_MS = 30000; |
| 189 | |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 190 | // How many Span tags (e.g. animations) to report. |
| 191 | private static final int MAX_SPAN_TAGS = 20; |
| 192 | |
Brad Fitzpatrick | 191cdf0 | 2010-10-11 11:31:15 -0700 | [diff] [blame] | 193 | // How many offending stacks to keep track of (and time) per loop |
| 194 | // of the Looper. |
| 195 | private static final int MAX_OFFENSES_PER_LOOP = 10; |
| 196 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 197 | /** @hide */ |
| 198 | @IntDef(flag = true, prefix = { "DETECT_THREAD_", "PENALTY_" }, value = { |
| 199 | DETECT_THREAD_DISK_WRITE, |
| 200 | DETECT_THREAD_DISK_READ, |
| 201 | DETECT_THREAD_NETWORK, |
| 202 | DETECT_THREAD_CUSTOM, |
| 203 | DETECT_THREAD_RESOURCE_MISMATCH, |
| 204 | DETECT_THREAD_UNBUFFERED_IO, |
| 205 | DETECT_THREAD_EXPLICIT_GC, |
| 206 | PENALTY_GATHER, |
| 207 | PENALTY_LOG, |
| 208 | PENALTY_DIALOG, |
| 209 | PENALTY_DEATH, |
| 210 | PENALTY_FLASH, |
| 211 | PENALTY_DROPBOX, |
| 212 | PENALTY_DEATH_ON_NETWORK, |
| 213 | PENALTY_DEATH_ON_CLEARTEXT_NETWORK, |
| 214 | PENALTY_DEATH_ON_FILE_URI_EXPOSURE, |
| 215 | }) |
| 216 | @Retention(RetentionPolicy.SOURCE) |
| 217 | public @interface ThreadPolicyMask {} |
| 218 | |
| 219 | // Thread policy: bits 0-15 |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 220 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 221 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 222 | private static final int DETECT_THREAD_DISK_WRITE = 1 << 0; |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 223 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 224 | private static final int DETECT_THREAD_DISK_READ = 1 << 1; |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 225 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 226 | private static final int DETECT_THREAD_NETWORK = 1 << 2; |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 227 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 228 | private static final int DETECT_THREAD_CUSTOM = 1 << 3; |
| 229 | /** @hide */ |
| 230 | private static final int DETECT_THREAD_RESOURCE_MISMATCH = 1 << 4; |
| 231 | /** @hide */ |
| 232 | private static final int DETECT_THREAD_UNBUFFERED_IO = 1 << 5; |
Pete Gillin | 22316b4 | 2018-02-15 17:17:27 +0000 | [diff] [blame] | 233 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 234 | private static final int DETECT_THREAD_EXPLICIT_GC = 1 << 6; |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 235 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 236 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 237 | private static final int DETECT_THREAD_ALL = 0x0000ffff; |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 238 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 239 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 240 | @IntDef(flag = true, prefix = { "DETECT_THREAD_", "PENALTY_" }, value = { |
| 241 | DETECT_VM_CURSOR_LEAKS, |
| 242 | DETECT_VM_CLOSABLE_LEAKS, |
| 243 | DETECT_VM_ACTIVITY_LEAKS, |
| 244 | DETECT_VM_INSTANCE_LEAKS, |
| 245 | DETECT_VM_REGISTRATION_LEAKS, |
| 246 | DETECT_VM_FILE_URI_EXPOSURE, |
| 247 | DETECT_VM_CLEARTEXT_NETWORK, |
| 248 | DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION, |
| 249 | DETECT_VM_UNTAGGED_SOCKET, |
| 250 | DETECT_VM_NON_SDK_API_USAGE, |
| 251 | DETECT_VM_IMPLICIT_DIRECT_BOOT, |
| 252 | PENALTY_GATHER, |
| 253 | PENALTY_LOG, |
| 254 | PENALTY_DIALOG, |
| 255 | PENALTY_DEATH, |
| 256 | PENALTY_FLASH, |
| 257 | PENALTY_DROPBOX, |
| 258 | PENALTY_DEATH_ON_NETWORK, |
| 259 | PENALTY_DEATH_ON_CLEARTEXT_NETWORK, |
| 260 | PENALTY_DEATH_ON_FILE_URI_EXPOSURE, |
| 261 | }) |
| 262 | @Retention(RetentionPolicy.SOURCE) |
| 263 | public @interface VmPolicyMask {} |
| 264 | |
| 265 | // VM policy: bits 0-15 |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 266 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 267 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 268 | private static final int DETECT_VM_CURSOR_LEAKS = 1 << 0; |
| 269 | /** @hide */ |
| 270 | private static final int DETECT_VM_CLOSABLE_LEAKS = 1 << 1; |
| 271 | /** @hide */ |
| 272 | private static final int DETECT_VM_ACTIVITY_LEAKS = 1 << 2; |
| 273 | /** @hide */ |
| 274 | private static final int DETECT_VM_INSTANCE_LEAKS = 1 << 3; |
| 275 | /** @hide */ |
| 276 | private static final int DETECT_VM_REGISTRATION_LEAKS = 1 << 4; |
| 277 | /** @hide */ |
| 278 | private static final int DETECT_VM_FILE_URI_EXPOSURE = 1 << 5; |
| 279 | /** @hide */ |
| 280 | private static final int DETECT_VM_CLEARTEXT_NETWORK = 1 << 6; |
| 281 | /** @hide */ |
| 282 | private static final int DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION = 1 << 7; |
| 283 | /** @hide */ |
| 284 | private static final int DETECT_VM_UNTAGGED_SOCKET = 1 << 8; |
| 285 | /** @hide */ |
| 286 | private static final int DETECT_VM_NON_SDK_API_USAGE = 1 << 9; |
| 287 | /** @hide */ |
| 288 | private static final int DETECT_VM_IMPLICIT_DIRECT_BOOT = 1 << 10; |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 289 | /** @hide */ |
| 290 | private static final int DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED = 1 << 11; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 291 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 292 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 293 | private static final int DETECT_VM_ALL = 0x0000ffff; |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 294 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 295 | // Penalty policy: bits 16-31 |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 296 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 297 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 298 | * Non-public penalty mode which overrides all the other penalty bits and signals that we're in |
| 299 | * a Binder call and we should ignore the other penalty bits and instead serialize back all our |
| 300 | * offending stack traces to the caller to ultimately handle in the originating process. |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 301 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 302 | * <p>This must be kept in sync with the constant in libs/binder/Parcel.cpp |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 303 | * |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 304 | * @hide |
| 305 | */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 306 | public static final int PENALTY_GATHER = 1 << 31; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 307 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 308 | /** {@hide} */ |
| 309 | public static final int PENALTY_LOG = 1 << 30; |
| 310 | /** {@hide} */ |
| 311 | public static final int PENALTY_DIALOG = 1 << 29; |
| 312 | /** {@hide} */ |
| 313 | public static final int PENALTY_DEATH = 1 << 28; |
| 314 | /** {@hide} */ |
| 315 | public static final int PENALTY_FLASH = 1 << 27; |
| 316 | /** {@hide} */ |
| 317 | public static final int PENALTY_DROPBOX = 1 << 26; |
| 318 | /** {@hide} */ |
| 319 | public static final int PENALTY_DEATH_ON_NETWORK = 1 << 25; |
| 320 | /** {@hide} */ |
| 321 | public static final int PENALTY_DEATH_ON_CLEARTEXT_NETWORK = 1 << 24; |
| 322 | /** {@hide} */ |
| 323 | public static final int PENALTY_DEATH_ON_FILE_URI_EXPOSURE = 1 << 23; |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 324 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 325 | /** @hide */ |
| 326 | public static final int PENALTY_ALL = 0xffff0000; |
Brad Fitzpatrick | c0bb0bb | 2011-01-20 16:29:52 -0800 | [diff] [blame] | 327 | |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 328 | /** {@hide} */ |
| 329 | public static final int NETWORK_POLICY_ACCEPT = 0; |
| 330 | /** {@hide} */ |
| 331 | public static final int NETWORK_POLICY_LOG = 1; |
| 332 | /** {@hide} */ |
| 333 | public static final int NETWORK_POLICY_REJECT = 2; |
Brad Fitzpatrick | c0bb0bb | 2011-01-20 16:29:52 -0800 | [diff] [blame] | 334 | |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 335 | // TODO: wrap in some ImmutableHashMap thing. |
| 336 | // Note: must be before static initialization of sVmPolicy. |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 337 | private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = |
| 338 | new HashMap<Class, Integer>(); |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 339 | |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 340 | /** The current VmPolicy in effect. */ |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 341 | private static volatile VmPolicy sVmPolicy = VmPolicy.LAX; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 342 | |
Jeff Sharkey | c985ffd | 2017-05-24 12:45:40 -0600 | [diff] [blame] | 343 | /** {@hide} */ |
Jeff Sharkey | ddc3bfd | 2017-06-26 19:38:56 -0600 | [diff] [blame] | 344 | @TestApi |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 345 | public interface ViolationLogger { |
| 346 | |
| 347 | /** Called when penaltyLog is enabled and a violation needs logging. */ |
| 348 | void log(ViolationInfo info); |
Jeff Sharkey | c985ffd | 2017-05-24 12:45:40 -0600 | [diff] [blame] | 349 | } |
| 350 | |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 351 | private static final ViolationLogger LOGCAT_LOGGER = |
| 352 | info -> { |
| 353 | String msg; |
| 354 | if (info.durationMillis != -1) { |
| 355 | msg = "StrictMode policy violation; ~duration=" + info.durationMillis + " ms:"; |
| 356 | } else { |
| 357 | msg = "StrictMode policy violation:"; |
| 358 | } |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 359 | Log.d(TAG, msg + " " + info.getStackTrace()); |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | private static volatile ViolationLogger sLogger = LOGCAT_LOGGER; |
Jeff Sharkey | c985ffd | 2017-05-24 12:45:40 -0600 | [diff] [blame] | 363 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 364 | private static final ThreadLocal<OnThreadViolationListener> sThreadViolationListener = |
| 365 | new ThreadLocal<>(); |
| 366 | private static final ThreadLocal<Executor> sThreadViolationExecutor = new ThreadLocal<>(); |
| 367 | |
| 368 | /** |
| 369 | * When #{@link ThreadPolicy.Builder#penaltyListener} is enabled, the listener is called on the |
| 370 | * provided executor when a Thread violation occurs. |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 371 | */ |
| 372 | public interface OnThreadViolationListener { |
| 373 | /** Called on a thread policy violation. */ |
| 374 | void onThreadViolation(Violation v); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * When #{@link VmPolicy.Builder#penaltyListener} is enabled, the listener is called on the |
| 379 | * provided executor when a VM violation occurs. |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 380 | */ |
| 381 | public interface OnVmViolationListener { |
| 382 | /** Called on a VM policy violation. */ |
| 383 | void onVmViolation(Violation v); |
| 384 | } |
| 385 | |
Jeff Sharkey | c985ffd | 2017-05-24 12:45:40 -0600 | [diff] [blame] | 386 | /** {@hide} */ |
Jeff Sharkey | ddc3bfd | 2017-06-26 19:38:56 -0600 | [diff] [blame] | 387 | @TestApi |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 388 | public static void setViolationLogger(ViolationLogger listener) { |
| 389 | if (listener == null) { |
| 390 | listener = LOGCAT_LOGGER; |
| 391 | } |
| 392 | sLogger = listener; |
Jeff Sharkey | c985ffd | 2017-05-24 12:45:40 -0600 | [diff] [blame] | 393 | } |
| 394 | |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 395 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 396 | * The number of threads trying to do an async dropbox write. Just to limit ourselves out of |
| 397 | * paranoia. |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 398 | */ |
| 399 | private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0); |
| 400 | |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 401 | /** |
| 402 | * Callback supplied to dalvik / libcore to get informed of usages of java API that are not |
| 403 | * a part of the public SDK. |
| 404 | */ |
| 405 | private static final Consumer<String> sNonSdkApiUsageConsumer = |
| 406 | message -> onVmPolicyViolation(new NonSdkApiUsedViolation(message)); |
| 407 | |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 408 | private StrictMode() {} |
| 409 | |
| 410 | /** |
| 411 | * {@link StrictMode} policy applied to a certain thread. |
| 412 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 413 | * <p>The policy is enabled by {@link #setThreadPolicy}. The current policy can be retrieved |
| 414 | * with {@link #getThreadPolicy}. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 415 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 416 | * <p>Note that multiple penalties may be provided and they're run in order from least to most |
| 417 | * severe (logging before process death, for example). There's currently no mechanism to choose |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 418 | * different penalties for different detected actions. |
| 419 | */ |
| 420 | public static final class ThreadPolicy { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 421 | /** The default, lax policy which doesn't catch anything. */ |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 422 | public static final ThreadPolicy LAX = new ThreadPolicy(0, null, null); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 423 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 424 | final @ThreadPolicyMask int mask; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 425 | final OnThreadViolationListener mListener; |
| 426 | final Executor mCallbackExecutor; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 427 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 428 | private ThreadPolicy(@ThreadPolicyMask int mask, OnThreadViolationListener listener, |
| 429 | Executor executor) { |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 430 | this.mask = mask; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 431 | mListener = listener; |
| 432 | mCallbackExecutor = executor; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public String toString() { |
| 437 | return "[StrictMode.ThreadPolicy; mask=" + mask + "]"; |
| 438 | } |
| 439 | |
| 440 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 441 | * Creates {@link ThreadPolicy} instances. Methods whose names start with {@code detect} |
| 442 | * specify what problems we should look for. Methods whose names start with {@code penalty} |
| 443 | * specify what we should do when we detect a problem. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 444 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 445 | * <p>You can call as many {@code detect} and {@code penalty} methods as you like. Currently |
| 446 | * order is insignificant: all penalties apply to all detected problems. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 447 | * |
| 448 | * <p>For example, detect everything and log anything that's found: |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 449 | * |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 450 | * <pre> |
Brad Fitzpatrick | 320274c | 2010-12-13 14:07:39 -0800 | [diff] [blame] | 451 | * StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 452 | * .detectAll() |
| 453 | * .penaltyLog() |
| 454 | * .build(); |
Brad Fitzpatrick | 320274c | 2010-12-13 14:07:39 -0800 | [diff] [blame] | 455 | * StrictMode.setThreadPolicy(policy); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 456 | * </pre> |
| 457 | */ |
| 458 | public static final class Builder { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 459 | private @ThreadPolicyMask int mMask = 0; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 460 | private OnThreadViolationListener mListener; |
| 461 | private Executor mExecutor; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 462 | |
| 463 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 464 | * Create a Builder that detects nothing and has no violations. (but note that {@link |
| 465 | * #build} will default to enabling {@link #penaltyLog} if no other penalties are |
| 466 | * specified) |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 467 | */ |
| 468 | public Builder() { |
| 469 | mMask = 0; |
| 470 | } |
| 471 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 472 | /** Initialize a Builder from an existing ThreadPolicy. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 473 | public Builder(ThreadPolicy policy) { |
| 474 | mMask = policy.mask; |
Kurt Nelson | 7a8804a | 2018-02-27 16:45:27 -0800 | [diff] [blame] | 475 | mListener = policy.mListener; |
| 476 | mExecutor = policy.mCallbackExecutor; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Detect everything that's potentially suspect. |
| 481 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 482 | * <p>As of the Gingerbread release this includes network and disk operations but will |
| 483 | * likely expand in future releases. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 484 | */ |
| 485 | public Builder detectAll() { |
Jeff Sharkey | 0b55870 | 2017-01-26 16:48:01 -0700 | [diff] [blame] | 486 | detectDiskReads(); |
| 487 | detectDiskWrites(); |
| 488 | detectNetwork(); |
| 489 | |
| 490 | final int targetSdk = VMRuntime.getRuntime().getTargetSdkVersion(); |
| 491 | if (targetSdk >= Build.VERSION_CODES.HONEYCOMB) { |
| 492 | detectCustomSlowCalls(); |
| 493 | } |
| 494 | if (targetSdk >= Build.VERSION_CODES.M) { |
| 495 | detectResourceMismatches(); |
| 496 | } |
| 497 | if (targetSdk >= Build.VERSION_CODES.O) { |
| 498 | detectUnbufferedIo(); |
| 499 | } |
| 500 | return this; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 503 | /** Disable the detection of everything. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 504 | public Builder permitAll() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 505 | return disable(DETECT_THREAD_ALL); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 508 | /** Enable detection of network operations. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 509 | public Builder detectNetwork() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 510 | return enable(DETECT_THREAD_NETWORK); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 513 | /** Disable detection of network operations. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 514 | public Builder permitNetwork() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 515 | return disable(DETECT_THREAD_NETWORK); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 518 | /** Enable detection of disk reads. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 519 | public Builder detectDiskReads() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 520 | return enable(DETECT_THREAD_DISK_READ); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 523 | /** Disable detection of disk reads. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 524 | public Builder permitDiskReads() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 525 | return disable(DETECT_THREAD_DISK_READ); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 528 | /** Enable detection of slow calls. */ |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 529 | public Builder detectCustomSlowCalls() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 530 | return enable(DETECT_THREAD_CUSTOM); |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 533 | /** Disable detection of slow calls. */ |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 534 | public Builder permitCustomSlowCalls() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 535 | return disable(DETECT_THREAD_CUSTOM); |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 538 | /** Disable detection of mismatches between defined resource types and getter calls. */ |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 539 | public Builder permitResourceMismatches() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 540 | return disable(DETECT_THREAD_RESOURCE_MISMATCH); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 543 | /** Detect unbuffered input/output operations. */ |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 544 | public Builder detectUnbufferedIo() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 545 | return enable(DETECT_THREAD_UNBUFFERED_IO); |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 548 | /** Disable detection of unbuffered input/output operations. */ |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 549 | public Builder permitUnbufferedIo() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 550 | return disable(DETECT_THREAD_UNBUFFERED_IO); |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 554 | * Enables detection of mismatches between defined resource types and getter calls. |
| 555 | * |
| 556 | * <p>This helps detect accidental type mismatches and potentially expensive type |
| 557 | * conversions when obtaining typed resources. |
| 558 | * |
| 559 | * <p>For example, a strict mode violation would be thrown when calling {@link |
| 560 | * android.content.res.TypedArray#getInt(int, int)} on an index that contains a |
| 561 | * String-type resource. If the string value can be parsed as an integer, this method |
| 562 | * call will return a value without crashing; however, the developer should format the |
| 563 | * resource as an integer to avoid unnecessary type conversion. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 564 | */ |
| 565 | public Builder detectResourceMismatches() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 566 | return enable(DETECT_THREAD_RESOURCE_MISMATCH); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 569 | /** Enable detection of disk writes. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 570 | public Builder detectDiskWrites() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 571 | return enable(DETECT_THREAD_DISK_WRITE); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 574 | /** Disable detection of disk writes. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 575 | public Builder permitDiskWrites() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 576 | return disable(DETECT_THREAD_DISK_WRITE); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /** |
Pete Gillin | 22316b4 | 2018-02-15 17:17:27 +0000 | [diff] [blame] | 580 | * Detect explicit GC requests, i.e. calls to Runtime.gc(). |
| 581 | * |
| 582 | * @hide |
| 583 | */ |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 584 | @TestApi |
Pete Gillin | 22316b4 | 2018-02-15 17:17:27 +0000 | [diff] [blame] | 585 | public Builder detectExplicitGc() { |
| 586 | // TODO(b/3400644): Un-hide this for next API update |
| 587 | // TODO(b/3400644): Un-hide ExplicitGcViolation for next API update |
| 588 | // TODO(b/3400644): Make DETECT_EXPLICIT_GC a @TestApi for next API update |
| 589 | // TODO(b/3400644): Call this from detectAll in next API update |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 590 | return enable(DETECT_THREAD_EXPLICIT_GC); |
Pete Gillin | 22316b4 | 2018-02-15 17:17:27 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Disable detection of explicit GC requests, i.e. calls to Runtime.gc(). |
| 595 | * |
| 596 | * @hide |
| 597 | */ |
| 598 | public Builder permitExplicitGc() { |
| 599 | // TODO(b/3400644): Un-hide this for next API update |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 600 | return disable(DETECT_THREAD_EXPLICIT_GC); |
Pete Gillin | 22316b4 | 2018-02-15 17:17:27 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 604 | * Show an annoying dialog to the developer on detected violations, rate-limited to be |
| 605 | * only a little annoying. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 606 | */ |
| 607 | public Builder penaltyDialog() { |
| 608 | return enable(PENALTY_DIALOG); |
| 609 | } |
| 610 | |
| 611 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 612 | * Crash the whole process on violation. This penalty runs at the end of all enabled |
| 613 | * penalties so you'll still get see logging or other violations before the process |
| 614 | * dies. |
Brad Fitzpatrick | b6e1841 | 2010-10-28 14:50:05 -0700 | [diff] [blame] | 615 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 616 | * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies to disk reads, disk writes, |
| 617 | * and network usage if their corresponding detect flags are set. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 618 | */ |
| 619 | public Builder penaltyDeath() { |
| 620 | return enable(PENALTY_DEATH); |
| 621 | } |
| 622 | |
| 623 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 624 | * Crash the whole process on any network usage. Unlike {@link #penaltyDeath}, this |
| 625 | * penalty runs <em>before</em> anything else. You must still have called {@link |
| 626 | * #detectNetwork} to enable this. |
Brad Fitzpatrick | b6e1841 | 2010-10-28 14:50:05 -0700 | [diff] [blame] | 627 | * |
| 628 | * <p>In the Honeycomb or later SDKs, this is on by default. |
| 629 | */ |
| 630 | public Builder penaltyDeathOnNetwork() { |
| 631 | return enable(PENALTY_DEATH_ON_NETWORK); |
| 632 | } |
| 633 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 634 | /** Flash the screen during a violation. */ |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 635 | public Builder penaltyFlashScreen() { |
| 636 | return enable(PENALTY_FLASH); |
| 637 | } |
| 638 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 639 | /** Log detected violations to the system log. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 640 | public Builder penaltyLog() { |
| 641 | return enable(PENALTY_LOG); |
| 642 | } |
| 643 | |
| 644 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 645 | * Enable detected violations log a stacktrace and timing data to the {@link |
| 646 | * android.os.DropBoxManager DropBox} on policy violation. Intended mostly for platform |
| 647 | * integrators doing beta user field data collection. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 648 | */ |
| 649 | public Builder penaltyDropBox() { |
| 650 | return enable(PENALTY_DROPBOX); |
| 651 | } |
| 652 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 653 | /** |
| 654 | * Call #{@link OnThreadViolationListener#onThreadViolation(Violation)} on specified |
| 655 | * executor every violation. |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 656 | */ |
| 657 | public Builder penaltyListener( |
Kurt Nelson | 72534b7 | 2018-02-22 11:21:15 -0800 | [diff] [blame] | 658 | @NonNull Executor executor, @NonNull OnThreadViolationListener listener) { |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 659 | if (executor == null) { |
| 660 | throw new NullPointerException("executor must not be null"); |
| 661 | } |
| 662 | mListener = listener; |
| 663 | mExecutor = executor; |
| 664 | return this; |
| 665 | } |
| 666 | |
Kurt Nelson | 72534b7 | 2018-02-22 11:21:15 -0800 | [diff] [blame] | 667 | /** @removed */ |
| 668 | public Builder penaltyListener( |
| 669 | @NonNull OnThreadViolationListener listener, @NonNull Executor executor) { |
| 670 | return penaltyListener(executor, listener); |
| 671 | } |
| 672 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 673 | private Builder enable(@ThreadPolicyMask int mask) { |
| 674 | mMask |= mask; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 675 | return this; |
| 676 | } |
| 677 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 678 | private Builder disable(@ThreadPolicyMask int mask) { |
| 679 | mMask &= ~mask; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 680 | return this; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Construct the ThreadPolicy instance. |
| 685 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 686 | * <p>Note: if no penalties are enabled before calling <code>build</code>, {@link |
| 687 | * #penaltyLog} is implicitly set. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 688 | */ |
| 689 | public ThreadPolicy build() { |
| 690 | // If there are detection bits set but no violation bits |
| 691 | // set, enable simple logging. |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 692 | if (mListener == null |
| 693 | && mMask != 0 |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 694 | && (mMask |
| 695 | & (PENALTY_DEATH |
| 696 | | PENALTY_LOG |
| 697 | | PENALTY_DROPBOX |
| 698 | | PENALTY_DIALOG)) |
| 699 | == 0) { |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 700 | penaltyLog(); |
| 701 | } |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 702 | return new ThreadPolicy(mMask, mListener, mExecutor); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * {@link StrictMode} policy applied to all threads in the virtual machine's process. |
| 709 | * |
| 710 | * <p>The policy is enabled by {@link #setVmPolicy}. |
| 711 | */ |
| 712 | public static final class VmPolicy { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 713 | /** The default, lax policy which doesn't catch anything. */ |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 714 | public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP, null, null); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 715 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 716 | final @VmPolicyMask int mask; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 717 | final OnVmViolationListener mListener; |
| 718 | final Executor mCallbackExecutor; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 719 | |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 720 | // Map from class to max number of allowed instances in memory. |
| 721 | final HashMap<Class, Integer> classInstanceLimit; |
| 722 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 723 | private VmPolicy( |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 724 | @VmPolicyMask int mask, |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 725 | HashMap<Class, Integer> classInstanceLimit, |
| 726 | OnVmViolationListener listener, |
| 727 | Executor executor) { |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 728 | if (classInstanceLimit == null) { |
| 729 | throw new NullPointerException("classInstanceLimit == null"); |
| 730 | } |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 731 | this.mask = mask; |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 732 | this.classInstanceLimit = classInstanceLimit; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 733 | mListener = listener; |
| 734 | mCallbackExecutor = executor; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | @Override |
| 738 | public String toString() { |
| 739 | return "[StrictMode.VmPolicy; mask=" + mask + "]"; |
| 740 | } |
| 741 | |
| 742 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 743 | * Creates {@link VmPolicy} instances. Methods whose names start with {@code detect} specify |
| 744 | * what problems we should look for. Methods whose names start with {@code penalty} specify |
| 745 | * what we should do when we detect a problem. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 746 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 747 | * <p>You can call as many {@code detect} and {@code penalty} methods as you like. Currently |
| 748 | * order is insignificant: all penalties apply to all detected problems. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 749 | * |
| 750 | * <p>For example, detect everything and log anything that's found: |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 751 | * |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 752 | * <pre> |
| 753 | * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() |
| 754 | * .detectAll() |
| 755 | * .penaltyLog() |
| 756 | * .build(); |
| 757 | * StrictMode.setVmPolicy(policy); |
| 758 | * </pre> |
| 759 | */ |
| 760 | public static final class Builder { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 761 | private @VmPolicyMask int mMask; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 762 | private OnVmViolationListener mListener; |
| 763 | private Executor mExecutor; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 764 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 765 | private HashMap<Class, Integer> mClassInstanceLimit; // null until needed |
| 766 | private boolean mClassInstanceLimitNeedCow = false; // need copy-on-write |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 767 | |
| 768 | public Builder() { |
| 769 | mMask = 0; |
| 770 | } |
| 771 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 772 | /** Build upon an existing VmPolicy. */ |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 773 | public Builder(VmPolicy base) { |
| 774 | mMask = base.mask; |
| 775 | mClassInstanceLimitNeedCow = true; |
| 776 | mClassInstanceLimit = base.classInstanceLimit; |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 777 | mListener = base.mListener; |
| 778 | mExecutor = base.mCallbackExecutor; |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 782 | * Set an upper bound on how many instances of a class can be in memory at once. Helps |
| 783 | * to prevent object leaks. |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 784 | */ |
| 785 | public Builder setClassInstanceLimit(Class klass, int instanceLimit) { |
| 786 | if (klass == null) { |
| 787 | throw new NullPointerException("klass == null"); |
| 788 | } |
| 789 | if (mClassInstanceLimitNeedCow) { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 790 | if (mClassInstanceLimit.containsKey(klass) |
| 791 | && mClassInstanceLimit.get(klass) == instanceLimit) { |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 792 | // no-op; don't break COW |
| 793 | return this; |
| 794 | } |
| 795 | mClassInstanceLimitNeedCow = false; |
| 796 | mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone(); |
| 797 | } else if (mClassInstanceLimit == null) { |
| 798 | mClassInstanceLimit = new HashMap<Class, Integer>(); |
| 799 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 800 | mMask |= DETECT_VM_INSTANCE_LEAKS; |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 801 | mClassInstanceLimit.put(klass, instanceLimit); |
| 802 | return this; |
| 803 | } |
| 804 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 805 | /** Detect leaks of {@link android.app.Activity} subclasses. */ |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 806 | public Builder detectActivityLeaks() { |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 807 | return enable(DETECT_VM_ACTIVITY_LEAKS); |
| 808 | } |
| 809 | |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 810 | /** @hide */ |
| 811 | public Builder permitActivityLeaks() { |
| 812 | return disable(DETECT_VM_ACTIVITY_LEAKS); |
| 813 | } |
| 814 | |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 815 | /** |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 816 | * Detect reflective usage of APIs that are not part of the public Android SDK. |
Mathew Inwood | d82a743 | 2018-04-19 14:42:58 +0100 | [diff] [blame] | 817 | * |
| 818 | * <p>Note that any non-SDK APIs that this processes accesses before this detection is |
| 819 | * enabled may not be detected. To ensure that all such API accesses are detected, |
| 820 | * you should apply this policy as early as possible after process creation. |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 821 | */ |
| 822 | public Builder detectNonSdkApiUsage() { |
| 823 | return enable(DETECT_VM_NON_SDK_API_USAGE); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Permit reflective usage of APIs that are not part of the public Android SDK. Note |
| 828 | * that this <b>only</b> affects {@code StrictMode}, the underlying runtime may |
| 829 | * continue to restrict or warn on access to methods that are not part of the |
| 830 | * public SDK. |
| 831 | */ |
| 832 | public Builder permitNonSdkApiUsage() { |
| 833 | return disable(DETECT_VM_NON_SDK_API_USAGE); |
| 834 | } |
| 835 | |
| 836 | /** |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 837 | * Detect everything that's potentially suspect. |
| 838 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 839 | * <p>In the Honeycomb release this includes leaks of SQLite cursors, Activities, and |
| 840 | * other closable objects but will likely expand in future releases. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 841 | */ |
| 842 | public Builder detectAll() { |
Jeff Sharkey | 0b55870 | 2017-01-26 16:48:01 -0700 | [diff] [blame] | 843 | detectLeakedSqlLiteObjects(); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 844 | |
Jeff Sharkey | 0b55870 | 2017-01-26 16:48:01 -0700 | [diff] [blame] | 845 | final int targetSdk = VMRuntime.getRuntime().getTargetSdkVersion(); |
| 846 | if (targetSdk >= Build.VERSION_CODES.HONEYCOMB) { |
| 847 | detectActivityLeaks(); |
| 848 | detectLeakedClosableObjects(); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 849 | } |
Jeff Sharkey | 0b55870 | 2017-01-26 16:48:01 -0700 | [diff] [blame] | 850 | if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN) { |
| 851 | detectLeakedRegistrationObjects(); |
| 852 | } |
| 853 | if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
| 854 | detectFileUriExposure(); |
| 855 | } |
| 856 | if (targetSdk >= Build.VERSION_CODES.M) { |
| 857 | // TODO: always add DETECT_VM_CLEARTEXT_NETWORK once we have |
| 858 | // facility for apps to mark sockets that should be ignored |
| 859 | if (SystemProperties.getBoolean(CLEARTEXT_PROPERTY, false)) { |
| 860 | detectCleartextNetwork(); |
| 861 | } |
| 862 | } |
| 863 | if (targetSdk >= Build.VERSION_CODES.O) { |
| 864 | detectContentUriWithoutPermission(); |
| 865 | detectUntaggedSockets(); |
| 866 | } |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 867 | if (targetSdk >= Build.VERSION_CODES.Q) { |
| 868 | detectCredentialProtectedWhileLocked(); |
| 869 | } |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 870 | |
| 871 | // TODO: Decide whether to detect non SDK API usage beyond a certain API level. |
Jeff Sharkey | c59a5e7 | 2018-06-21 19:14:58 -0600 | [diff] [blame] | 872 | // TODO: enable detectImplicitDirectBoot() once system is less noisy |
| 873 | |
Jeff Sharkey | 0b55870 | 2017-01-26 16:48:01 -0700 | [diff] [blame] | 874 | return this; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 878 | * Detect when an {@link android.database.sqlite.SQLiteCursor} or other SQLite object is |
| 879 | * finalized without having been closed. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 880 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 881 | * <p>You always want to explicitly close your SQLite cursors to avoid unnecessary |
| 882 | * database contention and temporary memory leaks. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 883 | */ |
| 884 | public Builder detectLeakedSqlLiteObjects() { |
| 885 | return enable(DETECT_VM_CURSOR_LEAKS); |
| 886 | } |
| 887 | |
| 888 | /** |
Kurt Nelson | d25bd9f | 2017-09-14 11:36:34 -0700 | [diff] [blame] | 889 | * Detect when an {@link java.io.Closeable} or other object with an explicit termination |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 890 | * method is finalized without having been closed. |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 891 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 892 | * <p>You always want to explicitly close such objects to avoid unnecessary resources |
| 893 | * leaks. |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 894 | */ |
| 895 | public Builder detectLeakedClosableObjects() { |
| 896 | return enable(DETECT_VM_CLOSABLE_LEAKS); |
| 897 | } |
| 898 | |
| 899 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 900 | * Detect when a {@link BroadcastReceiver} or {@link ServiceConnection} is leaked during |
| 901 | * {@link Context} teardown. |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 902 | */ |
| 903 | public Builder detectLeakedRegistrationObjects() { |
| 904 | return enable(DETECT_VM_REGISTRATION_LEAKS); |
| 905 | } |
| 906 | |
| 907 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 908 | * Detect when the calling application exposes a {@code file://} {@link android.net.Uri} |
| 909 | * to another app. |
| 910 | * |
| 911 | * <p>This exposure is discouraged since the receiving app may not have access to the |
| 912 | * shared path. For example, the receiving app may not have requested the {@link |
| 913 | * android.Manifest.permission#READ_EXTERNAL_STORAGE} runtime permission, or the |
| 914 | * platform may be sharing the {@link android.net.Uri} across user profile boundaries. |
| 915 | * |
| 916 | * <p>Instead, apps should use {@code content://} Uris so the platform can extend |
| 917 | * temporary permission for the receiving app to access the resource. |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 918 | * |
| 919 | * @see android.support.v4.content.FileProvider |
| 920 | * @see Intent#FLAG_GRANT_READ_URI_PERMISSION |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 921 | */ |
| 922 | public Builder detectFileUriExposure() { |
| 923 | return enable(DETECT_VM_FILE_URI_EXPOSURE); |
| 924 | } |
| 925 | |
| 926 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 927 | * Detect any network traffic from the calling app which is not wrapped in SSL/TLS. This |
| 928 | * can help you detect places that your app is inadvertently sending cleartext data |
| 929 | * across the network. |
| 930 | * |
| 931 | * <p>Using {@link #penaltyDeath()} or {@link #penaltyDeathOnCleartextNetwork()} will |
| 932 | * block further traffic on that socket to prevent accidental data leakage, in addition |
| 933 | * to crashing your process. |
| 934 | * |
| 935 | * <p>Using {@link #penaltyDropBox()} will log the raw contents of the packet that |
| 936 | * triggered the violation. |
| 937 | * |
| 938 | * <p>This inspects both IPv4/IPv6 and TCP/UDP network traffic, but it may be subject to |
| 939 | * false positives, such as when STARTTLS protocols or HTTP proxies are used. |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 940 | */ |
| 941 | public Builder detectCleartextNetwork() { |
| 942 | return enable(DETECT_VM_CLEARTEXT_NETWORK); |
| 943 | } |
| 944 | |
| 945 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 946 | * Detect when the calling application sends a {@code content://} {@link |
| 947 | * android.net.Uri} to another app without setting {@link |
| 948 | * Intent#FLAG_GRANT_READ_URI_PERMISSION} or {@link |
| 949 | * Intent#FLAG_GRANT_WRITE_URI_PERMISSION}. |
| 950 | * |
| 951 | * <p>Forgetting to include one or more of these flags when sending an intent is |
| 952 | * typically an app bug. |
Jeff Sharkey | f361f2f | 2016-12-21 10:48:42 -0700 | [diff] [blame] | 953 | * |
| 954 | * @see Intent#FLAG_GRANT_READ_URI_PERMISSION |
| 955 | * @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION |
| 956 | */ |
| 957 | public Builder detectContentUriWithoutPermission() { |
| 958 | return enable(DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION); |
| 959 | } |
| 960 | |
| 961 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 962 | * Detect any sockets in the calling app which have not been tagged using {@link |
| 963 | * TrafficStats}. Tagging sockets can help you investigate network usage inside your |
| 964 | * app, such as a narrowing down heavy usage to a specific library or component. |
| 965 | * |
| 966 | * <p>This currently does not detect sockets created in native code. |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 967 | * |
| 968 | * @see TrafficStats#setThreadStatsTag(int) |
| 969 | * @see TrafficStats#tagSocket(java.net.Socket) |
| 970 | * @see TrafficStats#tagDatagramSocket(java.net.DatagramSocket) |
| 971 | */ |
| 972 | public Builder detectUntaggedSockets() { |
| 973 | return enable(DETECT_VM_UNTAGGED_SOCKET); |
| 974 | } |
| 975 | |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 976 | /** @hide */ |
| 977 | public Builder permitUntaggedSockets() { |
| 978 | return disable(DETECT_VM_UNTAGGED_SOCKET); |
| 979 | } |
| 980 | |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 981 | /** |
Jeff Sharkey | c59a5e7 | 2018-06-21 19:14:58 -0600 | [diff] [blame] | 982 | * Detect any implicit reliance on Direct Boot automatic filtering |
| 983 | * of {@link PackageManager} values. Violations are only triggered |
| 984 | * when implicit calls are made while the user is locked. |
| 985 | * <p> |
| 986 | * Apps becoming Direct Boot aware need to carefully inspect each |
| 987 | * query site and explicitly decide which combination of flags they |
| 988 | * want to use: |
| 989 | * <ul> |
| 990 | * <li>{@link PackageManager#MATCH_DIRECT_BOOT_AWARE} |
| 991 | * <li>{@link PackageManager#MATCH_DIRECT_BOOT_UNAWARE} |
| 992 | * <li>{@link PackageManager#MATCH_DIRECT_BOOT_AUTO} |
| 993 | * </ul> |
| 994 | */ |
| 995 | public Builder detectImplicitDirectBoot() { |
| 996 | return enable(DETECT_VM_IMPLICIT_DIRECT_BOOT); |
| 997 | } |
| 998 | |
| 999 | /** @hide */ |
| 1000 | public Builder permitImplicitDirectBoot() { |
| 1001 | return disable(DETECT_VM_IMPLICIT_DIRECT_BOOT); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 1005 | * Detect access to filesystem paths stored in credential protected |
| 1006 | * storage areas while the user is locked. |
| 1007 | * <p> |
| 1008 | * When a user is locked, credential protected storage is |
| 1009 | * unavailable, and files stored in these locations appear to not |
| 1010 | * exist, which can result in subtle app bugs if they assume default |
| 1011 | * behaviors or empty states. Instead, apps should store data needed |
| 1012 | * while a user is locked under device protected storage areas. |
| 1013 | * |
| 1014 | * @see Context#createCredentialProtectedStorageContext() |
| 1015 | * @see Context#createDeviceProtectedStorageContext() |
| 1016 | */ |
| 1017 | public Builder detectCredentialProtectedWhileLocked() { |
| 1018 | return enable(DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED); |
| 1019 | } |
| 1020 | |
| 1021 | /** @hide */ |
| 1022 | public Builder permitCredentialProtectedWhileLocked() { |
| 1023 | return disable(DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1027 | * Crashes the whole process on violation. This penalty runs at the end of all enabled |
| 1028 | * penalties so you'll still get your logging or other violations before the process |
| 1029 | * dies. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1030 | */ |
| 1031 | public Builder penaltyDeath() { |
| 1032 | return enable(PENALTY_DEATH); |
| 1033 | } |
| 1034 | |
| 1035 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1036 | * Crashes the whole process when cleartext network traffic is detected. |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1037 | * |
| 1038 | * @see #detectCleartextNetwork() |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1039 | */ |
| 1040 | public Builder penaltyDeathOnCleartextNetwork() { |
| 1041 | return enable(PENALTY_DEATH_ON_CLEARTEXT_NETWORK); |
| 1042 | } |
| 1043 | |
| 1044 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1045 | * Crashes the whole process when a {@code file://} {@link android.net.Uri} is exposed |
| 1046 | * beyond this app. |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1047 | * |
| 1048 | * @see #detectFileUriExposure() |
| 1049 | */ |
| 1050 | public Builder penaltyDeathOnFileUriExposure() { |
| 1051 | return enable(PENALTY_DEATH_ON_FILE_URI_EXPOSURE); |
| 1052 | } |
| 1053 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1054 | /** Log detected violations to the system log. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1055 | public Builder penaltyLog() { |
| 1056 | return enable(PENALTY_LOG); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1060 | * Enable detected violations log a stacktrace and timing data to the {@link |
| 1061 | * android.os.DropBoxManager DropBox} on policy violation. Intended mostly for platform |
| 1062 | * integrators doing beta user field data collection. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1063 | */ |
| 1064 | public Builder penaltyDropBox() { |
| 1065 | return enable(PENALTY_DROPBOX); |
| 1066 | } |
| 1067 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1068 | /** |
| 1069 | * Call #{@link OnVmViolationListener#onVmViolation(Violation)} on every violation. |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1070 | */ |
| 1071 | public Builder penaltyListener( |
Kurt Nelson | 72534b7 | 2018-02-22 11:21:15 -0800 | [diff] [blame] | 1072 | @NonNull Executor executor, @NonNull OnVmViolationListener listener) { |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1073 | if (executor == null) { |
| 1074 | throw new NullPointerException("executor must not be null"); |
| 1075 | } |
| 1076 | mListener = listener; |
| 1077 | mExecutor = executor; |
| 1078 | return this; |
| 1079 | } |
| 1080 | |
Kurt Nelson | 72534b7 | 2018-02-22 11:21:15 -0800 | [diff] [blame] | 1081 | /** @removed */ |
| 1082 | public Builder penaltyListener( |
| 1083 | @NonNull OnVmViolationListener listener, @NonNull Executor executor) { |
| 1084 | return penaltyListener(executor, listener); |
| 1085 | } |
| 1086 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1087 | private Builder enable(@VmPolicyMask int mask) { |
| 1088 | mMask |= mask; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1089 | return this; |
| 1090 | } |
| 1091 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1092 | Builder disable(@VmPolicyMask int mask) { |
| 1093 | mMask &= ~mask; |
Mathieu Chartier | ca8c105 | 2017-06-19 18:42:57 -0700 | [diff] [blame] | 1094 | return this; |
| 1095 | } |
| 1096 | |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1097 | /** |
| 1098 | * Construct the VmPolicy instance. |
| 1099 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1100 | * <p>Note: if no penalties are enabled before calling <code>build</code>, {@link |
| 1101 | * #penaltyLog} is implicitly set. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1102 | */ |
| 1103 | public VmPolicy build() { |
| 1104 | // If there are detection bits set but no violation bits |
| 1105 | // set, enable simple logging. |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1106 | if (mListener == null |
| 1107 | && mMask != 0 |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1108 | && (mMask |
| 1109 | & (PENALTY_DEATH |
| 1110 | | PENALTY_LOG |
| 1111 | | PENALTY_DROPBOX |
| 1112 | | PENALTY_DIALOG)) |
| 1113 | == 0) { |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1114 | penaltyLog(); |
| 1115 | } |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1116 | return new VmPolicy( |
| 1117 | mMask, |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1118 | mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP, |
| 1119 | mListener, |
| 1120 | mExecutor); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1124 | |
| 1125 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1126 | * Log of strict mode violation stack traces that have occurred during a Binder call, to be |
| 1127 | * serialized back later to the caller via Parcel.writeNoException() (amusingly) where the |
| 1128 | * caller can choose how to react. |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1129 | */ |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1130 | private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations = |
| 1131 | new ThreadLocal<ArrayList<ViolationInfo>>() { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1132 | @Override |
| 1133 | protected ArrayList<ViolationInfo> initialValue() { |
| 1134 | // Starts null to avoid unnecessary allocations when |
| 1135 | // checking whether there are any violations or not in |
| 1136 | // hasGatheredViolations() below. |
| 1137 | return null; |
| 1138 | } |
| 1139 | }; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1140 | |
| 1141 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1142 | * Sets the policy for what actions on the current thread should be detected, as well as the |
| 1143 | * penalty if such actions occur. |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1144 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1145 | * <p>Internally this sets a thread-local variable which is propagated across cross-process IPC |
| 1146 | * calls, meaning you can catch violations when a system service or another process accesses the |
| 1147 | * disk or network on your behalf. |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 1148 | * |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1149 | * @param policy the policy to put into place |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1150 | */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1151 | public static void setThreadPolicy(final ThreadPolicy policy) { |
| 1152 | setThreadPolicyMask(policy.mask); |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1153 | sThreadViolationListener.set(policy.mListener); |
| 1154 | sThreadViolationExecutor.set(policy.mCallbackExecutor); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1157 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1158 | public static void setThreadPolicyMask(@ThreadPolicyMask int threadPolicyMask) { |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1159 | // In addition to the Java-level thread-local in Dalvik's |
| 1160 | // BlockGuard, we also need to keep a native thread-local in |
| 1161 | // Binder in order to propagate the value across Binder calls, |
| 1162 | // even across native-only processes. The two are kept in |
| 1163 | // sync via the callback to onStrictModePolicyChange, below. |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1164 | setBlockGuardPolicy(threadPolicyMask); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1165 | |
| 1166 | // And set the Android native version... |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1167 | Binder.setThreadStrictModePolicy(threadPolicyMask); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | // Sets the policy in Dalvik/libcore (BlockGuard) |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1171 | private static void setBlockGuardPolicy(@ThreadPolicyMask int threadPolicyMask) { |
| 1172 | if (threadPolicyMask == 0) { |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1173 | BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY); |
| 1174 | return; |
| 1175 | } |
Jeff Sharkey | a2934d5 | 2013-06-14 14:43:18 -0700 | [diff] [blame] | 1176 | final BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 1177 | final AndroidBlockGuardPolicy androidPolicy; |
| 1178 | if (policy instanceof AndroidBlockGuardPolicy) { |
| 1179 | androidPolicy = (AndroidBlockGuardPolicy) policy; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1180 | } else { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1181 | androidPolicy = THREAD_ANDROID_POLICY.get(); |
Jeff Sharkey | a2934d5 | 2013-06-14 14:43:18 -0700 | [diff] [blame] | 1182 | BlockGuard.setThreadPolicy(androidPolicy); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1183 | } |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1184 | androidPolicy.setThreadPolicyMask(threadPolicyMask); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 1187 | private static void setBlockGuardVmPolicy(@VmPolicyMask int vmPolicyMask) { |
| 1188 | // We only need to install BlockGuard for a small subset of VM policies |
| 1189 | vmPolicyMask &= DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED; |
| 1190 | if (vmPolicyMask != 0) { |
| 1191 | BlockGuard.setVmPolicy(VM_ANDROID_POLICY); |
| 1192 | } else { |
| 1193 | BlockGuard.setVmPolicy(BlockGuard.LAX_VM_POLICY); |
| 1194 | } |
| 1195 | } |
| 1196 | |
Brian Carlstrom | 4b9b7c3 | 2010-11-08 10:30:40 -0800 | [diff] [blame] | 1197 | // Sets up CloseGuard in Dalvik/libcore |
| 1198 | private static void setCloseGuardEnabled(boolean enabled) { |
Brad Fitzpatrick | 7c2ae65 | 2010-11-14 11:00:05 -0800 | [diff] [blame] | 1199 | if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) { |
Brian Carlstrom | 4b9b7c3 | 2010-11-08 10:30:40 -0800 | [diff] [blame] | 1200 | CloseGuard.setReporter(new AndroidCloseGuardReporter()); |
| 1201 | } |
| 1202 | CloseGuard.setEnabled(enabled); |
| 1203 | } |
| 1204 | |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 1205 | /** |
Brad Fitzpatrick | 15ba406 | 2010-09-22 13:53:57 -0700 | [diff] [blame] | 1206 | * Returns the bitmask of the current thread's policy. |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1207 | * |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1208 | * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1209 | * @hide |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1210 | */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1211 | public static @ThreadPolicyMask int getThreadPolicyMask() { |
| 1212 | final BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 1213 | if (policy instanceof AndroidBlockGuardPolicy) { |
| 1214 | return ((AndroidBlockGuardPolicy) policy).getThreadPolicyMask(); |
| 1215 | } else { |
| 1216 | return 0; |
| 1217 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1220 | /** Returns the current thread's policy. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1221 | public static ThreadPolicy getThreadPolicy() { |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 1222 | // TODO: this was a last minute Gingerbread API change (to |
| 1223 | // introduce VmPolicy cleanly) but this isn't particularly |
| 1224 | // optimal for users who might call this method often. This |
| 1225 | // should be in a thread-local and not allocate on each call. |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1226 | return new ThreadPolicy( |
| 1227 | getThreadPolicyMask(), |
| 1228 | sThreadViolationListener.get(), |
| 1229 | sThreadViolationExecutor.get()); |
Brad Fitzpatrick | 97461bd | 2010-08-24 11:46:47 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1233 | * A convenience wrapper that takes the current {@link ThreadPolicy} from {@link |
| 1234 | * #getThreadPolicy}, modifies it to permit both disk reads & writes, and sets the new |
| 1235 | * policy with {@link #setThreadPolicy}, returning the old policy so you can restore it at the |
| 1236 | * end of a block. |
Brad Fitzpatrick | 97461bd | 2010-08-24 11:46:47 -0700 | [diff] [blame] | 1237 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1238 | * @return the old policy, to be passed to {@link #setThreadPolicy} to restore the policy at the |
| 1239 | * end of a block |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1240 | */ |
| 1241 | public static ThreadPolicy allowThreadDiskWrites() { |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1242 | return new ThreadPolicy( |
| 1243 | allowThreadDiskWritesMask(), |
| 1244 | sThreadViolationListener.get(), |
| 1245 | sThreadViolationExecutor.get()); |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1249 | public static @ThreadPolicyMask int allowThreadDiskWritesMask() { |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1250 | int oldPolicyMask = getThreadPolicyMask(); |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1251 | int newPolicyMask = oldPolicyMask & ~(DETECT_THREAD_DISK_WRITE | DETECT_THREAD_DISK_READ); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1252 | if (newPolicyMask != oldPolicyMask) { |
| 1253 | setThreadPolicyMask(newPolicyMask); |
| 1254 | } |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1255 | return oldPolicyMask; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1259 | * A convenience wrapper that takes the current {@link ThreadPolicy} from {@link |
| 1260 | * #getThreadPolicy}, modifies it to permit disk reads, and sets the new policy with {@link |
| 1261 | * #setThreadPolicy}, returning the old policy so you can restore it at the end of a block. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1262 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1263 | * @return the old policy, to be passed to setThreadPolicy to restore the policy. |
Brad Fitzpatrick | 97461bd | 2010-08-24 11:46:47 -0700 | [diff] [blame] | 1264 | */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1265 | public static ThreadPolicy allowThreadDiskReads() { |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1266 | return new ThreadPolicy( |
| 1267 | allowThreadDiskReadsMask(), |
| 1268 | sThreadViolationListener.get(), |
| 1269 | sThreadViolationExecutor.get()); |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /** @hide */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1273 | public static @ThreadPolicyMask int allowThreadDiskReadsMask() { |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1274 | int oldPolicyMask = getThreadPolicyMask(); |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1275 | int newPolicyMask = oldPolicyMask & ~(DETECT_THREAD_DISK_READ); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1276 | if (newPolicyMask != oldPolicyMask) { |
| 1277 | setThreadPolicyMask(newPolicyMask); |
Brad Fitzpatrick | 97461bd | 2010-08-24 11:46:47 -0700 | [diff] [blame] | 1278 | } |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1279 | return oldPolicyMask; |
Brad Fitzpatrick | 97461bd | 2010-08-24 11:46:47 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1282 | private static ThreadPolicy allowThreadViolations() { |
| 1283 | ThreadPolicy oldPolicy = getThreadPolicy(); |
| 1284 | setThreadPolicyMask(0); |
| 1285 | return oldPolicy; |
| 1286 | } |
| 1287 | |
| 1288 | private static VmPolicy allowVmViolations() { |
| 1289 | VmPolicy oldPolicy = getVmPolicy(); |
| 1290 | sVmPolicy = VmPolicy.LAX; |
| 1291 | return oldPolicy; |
| 1292 | } |
| 1293 | |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1294 | /** |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1295 | * Determine if the given app is "bundled" as part of the system image. These bundled apps are |
| 1296 | * developed in lock-step with the OS, and they aren't updated outside of an OTA, so we want to |
| 1297 | * chase any {@link StrictMode} regressions by enabling detection when running on {@link |
| 1298 | * Build#IS_USERDEBUG} or {@link Build#IS_ENG} builds. |
| 1299 | * |
| 1300 | * <p>Unbundled apps included in the system image are expected to detect and triage their own |
| 1301 | * {@link StrictMode} issues separate from the OS release process, which is why we don't enable |
| 1302 | * them here. |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1303 | * |
| 1304 | * @hide |
| 1305 | */ |
| 1306 | public static boolean isBundledSystemApp(ApplicationInfo ai) { |
| 1307 | if (ai == null || ai.packageName == null) { |
| 1308 | // Probably system server |
| 1309 | return true; |
| 1310 | } else if (ai.isSystemApp()) { |
| 1311 | // Ignore unbundled apps living in the wrong namespace |
| 1312 | if (ai.packageName.equals("com.android.vending") |
| 1313 | || ai.packageName.equals("com.android.chrome")) { |
| 1314 | return false; |
| 1315 | } |
Brad Fitzpatrick | f545459 | 2010-11-24 15:27:51 -0800 | [diff] [blame] | 1316 | |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1317 | // Ignore bundled apps that are way too spammy |
| 1318 | // STOPSHIP: burn this list down to zero |
| 1319 | if (ai.packageName.equals("com.android.phone")) { |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| 1323 | if (ai.packageName.equals("android") |
| 1324 | || ai.packageName.startsWith("android.") |
| 1325 | || ai.packageName.startsWith("com.android.")) { |
Brad Fitzpatrick | f545459 | 2010-11-24 15:27:51 -0800 | [diff] [blame] | 1326 | return true; |
| 1327 | } |
| 1328 | } |
| 1329 | return false; |
| 1330 | } |
| 1331 | |
Brad Fitzpatrick | 97461bd | 2010-08-24 11:46:47 -0700 | [diff] [blame] | 1332 | /** |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1333 | * Initialize default {@link ThreadPolicy} for the current thread. |
Brad Fitzpatrick | 50d66f9 | 2010-09-13 21:29:05 -0700 | [diff] [blame] | 1334 | * |
| 1335 | * @hide |
| 1336 | */ |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1337 | public static void initThreadDefaults(ApplicationInfo ai) { |
| 1338 | final ThreadPolicy.Builder builder = new ThreadPolicy.Builder(); |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1339 | final int targetSdkVersion = |
| 1340 | (ai != null) ? ai.targetSdkVersion : Build.VERSION_CODES.CUR_DEVELOPMENT; |
Brad Fitzpatrick | c1a968a | 2010-11-24 08:56:40 -0800 | [diff] [blame] | 1341 | |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1342 | // Starting in HC, we don't allow network usage on the main thread |
| 1343 | if (targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) { |
| 1344 | builder.detectNetwork(); |
| 1345 | builder.penaltyDeathOnNetwork(); |
Brad Fitzpatrick | 50d66f9 | 2010-09-13 21:29:05 -0700 | [diff] [blame] | 1346 | } |
Brad Fitzpatrick | c1a968a | 2010-11-24 08:56:40 -0800 | [diff] [blame] | 1347 | |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1348 | if (Build.IS_USER || DISABLE || SystemProperties.getBoolean(DISABLE_PROPERTY, false)) { |
| 1349 | // Detect nothing extra |
| 1350 | } else if (Build.IS_USERDEBUG) { |
| 1351 | // Detect everything in bundled apps |
| 1352 | if (isBundledSystemApp(ai)) { |
| 1353 | builder.detectAll(); |
| 1354 | builder.penaltyDropBox(); |
| 1355 | if (SystemProperties.getBoolean(VISUAL_PROPERTY, false)) { |
| 1356 | builder.penaltyFlashScreen(); |
| 1357 | } |
Mathieu Chartier | ca8c105 | 2017-06-19 18:42:57 -0700 | [diff] [blame] | 1358 | } |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1359 | } else if (Build.IS_ENG) { |
| 1360 | // Detect everything in bundled apps |
| 1361 | if (isBundledSystemApp(ai)) { |
| 1362 | builder.detectAll(); |
| 1363 | builder.penaltyDropBox(); |
| 1364 | builder.penaltyLog(); |
| 1365 | builder.penaltyFlashScreen(); |
Jeff Brown | d5875d9 | 2011-10-09 14:59:37 -0700 | [diff] [blame] | 1366 | } |
Brad Fitzpatrick | c1a968a | 2010-11-24 08:56:40 -0800 | [diff] [blame] | 1367 | } |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1368 | |
| 1369 | setThreadPolicy(builder.build()); |
Brad Fitzpatrick | 50d66f9 | 2010-09-13 21:29:05 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /** |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1373 | * Initialize default {@link VmPolicy} for the current VM. |
Brad Fitzpatrick | b6e1841 | 2010-10-28 14:50:05 -0700 | [diff] [blame] | 1374 | * |
| 1375 | * @hide |
| 1376 | */ |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1377 | public static void initVmDefaults(ApplicationInfo ai) { |
| 1378 | final VmPolicy.Builder builder = new VmPolicy.Builder(); |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1379 | final int targetSdkVersion = |
| 1380 | (ai != null) ? ai.targetSdkVersion : Build.VERSION_CODES.CUR_DEVELOPMENT; |
Jeff Sharkey | 8918298 | 2017-11-01 19:02:56 -0600 | [diff] [blame] | 1381 | |
| 1382 | // Starting in N, we don't allow file:// Uri exposure |
| 1383 | if (targetSdkVersion >= Build.VERSION_CODES.N) { |
| 1384 | builder.detectFileUriExposure(); |
| 1385 | builder.penaltyDeathOnFileUriExposure(); |
| 1386 | } |
| 1387 | |
| 1388 | if (Build.IS_USER || DISABLE || SystemProperties.getBoolean(DISABLE_PROPERTY, false)) { |
| 1389 | // Detect nothing extra |
| 1390 | } else if (Build.IS_USERDEBUG) { |
| 1391 | // Detect everything in bundled apps (except activity leaks, which |
| 1392 | // are expensive to track) |
| 1393 | if (isBundledSystemApp(ai)) { |
| 1394 | builder.detectAll(); |
| 1395 | builder.permitActivityLeaks(); |
| 1396 | builder.penaltyDropBox(); |
| 1397 | } |
| 1398 | } else if (Build.IS_ENG) { |
| 1399 | // Detect everything in bundled apps |
| 1400 | if (isBundledSystemApp(ai)) { |
| 1401 | builder.detectAll(); |
| 1402 | builder.penaltyDropBox(); |
| 1403 | builder.penaltyLog(); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | setVmPolicy(builder.build()); |
Brad Fitzpatrick | b6e1841 | 2010-10-28 14:50:05 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /** |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1411 | * Used by the framework to make file usage a fatal error. |
| 1412 | * |
| 1413 | * @hide |
| 1414 | */ |
| 1415 | public static void enableDeathOnFileUriExposure() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 1416 | sVmPolicy = |
| 1417 | new VmPolicy( |
| 1418 | sVmPolicy.mask |
| 1419 | | DETECT_VM_FILE_URI_EXPOSURE |
| 1420 | | PENALTY_DEATH_ON_FILE_URI_EXPOSURE, |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1421 | sVmPolicy.classInstanceLimit, |
| 1422 | sVmPolicy.mListener, |
| 1423 | sVmPolicy.mCallbackExecutor); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1427 | * Used by lame internal apps that haven't done the hard work to get themselves off file:// Uris |
| 1428 | * yet. |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1429 | * |
| 1430 | * @hide |
| 1431 | */ |
| 1432 | public static void disableDeathOnFileUriExposure() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 1433 | sVmPolicy = |
| 1434 | new VmPolicy( |
| 1435 | sVmPolicy.mask |
| 1436 | & ~(DETECT_VM_FILE_URI_EXPOSURE |
| 1437 | | PENALTY_DEATH_ON_FILE_URI_EXPOSURE), |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1438 | sVmPolicy.classInstanceLimit, |
| 1439 | sVmPolicy.mListener, |
| 1440 | sVmPolicy.mCallbackExecutor); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Brad Fitzpatrick | 191cdf0 | 2010-10-11 11:31:15 -0700 | [diff] [blame] | 1443 | private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed = |
| 1444 | new ThreadLocal<ArrayList<ViolationInfo>>() { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1445 | @Override |
| 1446 | protected ArrayList<ViolationInfo> initialValue() { |
| 1447 | return new ArrayList<ViolationInfo>(); |
| 1448 | } |
| 1449 | }; |
Brad Fitzpatrick | 191cdf0 | 2010-10-11 11:31:15 -0700 | [diff] [blame] | 1450 | |
Brad Fitzpatrick | bea168c | 2010-11-30 10:56:48 -0800 | [diff] [blame] | 1451 | // Note: only access this once verifying the thread has a Looper. |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1452 | private static final ThreadLocal<Handler> THREAD_HANDLER = |
| 1453 | new ThreadLocal<Handler>() { |
| 1454 | @Override |
| 1455 | protected Handler initialValue() { |
| 1456 | return new Handler(); |
| 1457 | } |
| 1458 | }; |
Brad Fitzpatrick | bea168c | 2010-11-30 10:56:48 -0800 | [diff] [blame] | 1459 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1460 | private static final ThreadLocal<AndroidBlockGuardPolicy> THREAD_ANDROID_POLICY = |
| 1461 | new ThreadLocal<AndroidBlockGuardPolicy>() { |
| 1462 | @Override |
| 1463 | protected AndroidBlockGuardPolicy initialValue() { |
| 1464 | return new AndroidBlockGuardPolicy(0); |
| 1465 | } |
| 1466 | }; |
Jeff Sharkey | a2934d5 | 2013-06-14 14:43:18 -0700 | [diff] [blame] | 1467 | |
Brad Fitzpatrick | 191cdf0 | 2010-10-11 11:31:15 -0700 | [diff] [blame] | 1468 | private static boolean tooManyViolationsThisLoop() { |
| 1469 | return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP; |
| 1470 | } |
| 1471 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1472 | private static class AndroidBlockGuardPolicy implements BlockGuard.Policy { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1473 | private @ThreadPolicyMask int mThreadPolicyMask; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1474 | |
| 1475 | // Map from violation stacktrace hashcode -> uptimeMillis of |
| 1476 | // last violation. No locking needed, as this is only |
| 1477 | // accessed by the same thread. |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1478 | private ArrayMap<Integer, Long> mLastViolationTime; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1479 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1480 | public AndroidBlockGuardPolicy(@ThreadPolicyMask int threadPolicyMask) { |
| 1481 | mThreadPolicyMask = threadPolicyMask; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1484 | @Override |
| 1485 | public String toString() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1486 | return "AndroidBlockGuardPolicy; mPolicyMask=" + mThreadPolicyMask; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1489 | // Part of BlockGuard.Policy interface: |
| 1490 | public int getPolicyMask() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1491 | return mThreadPolicyMask; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | // Part of BlockGuard.Policy interface: |
| 1495 | public void onWriteToDisk() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1496 | if ((mThreadPolicyMask & DETECT_THREAD_DISK_WRITE) == 0) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1497 | return; |
| 1498 | } |
Brad Fitzpatrick | 191cdf0 | 2010-10-11 11:31:15 -0700 | [diff] [blame] | 1499 | if (tooManyViolationsThisLoop()) { |
| 1500 | return; |
| 1501 | } |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1502 | startHandlingViolationException(new DiskWriteViolation()); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 1505 | // Not part of BlockGuard.Policy; just part of StrictMode: |
| 1506 | void onCustomSlowCall(String name) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1507 | if ((mThreadPolicyMask & DETECT_THREAD_CUSTOM) == 0) { |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 1508 | return; |
| 1509 | } |
| 1510 | if (tooManyViolationsThisLoop()) { |
| 1511 | return; |
| 1512 | } |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1513 | startHandlingViolationException(new CustomViolation(name)); |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1516 | // Not part of BlockGuard.Policy; just part of StrictMode: |
| 1517 | void onResourceMismatch(Object tag) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1518 | if ((mThreadPolicyMask & DETECT_THREAD_RESOURCE_MISMATCH) == 0) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1519 | return; |
| 1520 | } |
| 1521 | if (tooManyViolationsThisLoop()) { |
| 1522 | return; |
| 1523 | } |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1524 | startHandlingViolationException(new ResourceMismatchViolation(tag)); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1527 | // Not part of BlockGuard.Policy; just part of StrictMode: |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 1528 | public void onUnbufferedIO() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1529 | if ((mThreadPolicyMask & DETECT_THREAD_UNBUFFERED_IO) == 0) { |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 1530 | return; |
| 1531 | } |
| 1532 | if (tooManyViolationsThisLoop()) { |
| 1533 | return; |
| 1534 | } |
Kurt Nelson | 0e61280 | 2017-11-09 17:25:40 -0800 | [diff] [blame] | 1535 | startHandlingViolationException(new UnbufferedIoViolation()); |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 1536 | } |
| 1537 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1538 | // Part of BlockGuard.Policy interface: |
| 1539 | public void onReadFromDisk() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1540 | if ((mThreadPolicyMask & DETECT_THREAD_DISK_READ) == 0) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1541 | return; |
| 1542 | } |
Brad Fitzpatrick | 191cdf0 | 2010-10-11 11:31:15 -0700 | [diff] [blame] | 1543 | if (tooManyViolationsThisLoop()) { |
| 1544 | return; |
| 1545 | } |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1546 | startHandlingViolationException(new DiskReadViolation()); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | // Part of BlockGuard.Policy interface: |
| 1550 | public void onNetwork() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1551 | if ((mThreadPolicyMask & DETECT_THREAD_NETWORK) == 0) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1552 | return; |
| 1553 | } |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1554 | if ((mThreadPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) { |
Brad Fitzpatrick | b6e1841 | 2010-10-28 14:50:05 -0700 | [diff] [blame] | 1555 | throw new NetworkOnMainThreadException(); |
| 1556 | } |
Brad Fitzpatrick | 191cdf0 | 2010-10-11 11:31:15 -0700 | [diff] [blame] | 1557 | if (tooManyViolationsThisLoop()) { |
| 1558 | return; |
| 1559 | } |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1560 | startHandlingViolationException(new NetworkViolation()); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Pete Gillin | 22316b4 | 2018-02-15 17:17:27 +0000 | [diff] [blame] | 1563 | // Part of BlockGuard.Policy interface: |
| 1564 | public void onExplicitGc() { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1565 | if ((mThreadPolicyMask & DETECT_THREAD_EXPLICIT_GC) == 0) { |
Pete Gillin | 22316b4 | 2018-02-15 17:17:27 +0000 | [diff] [blame] | 1566 | return; |
| 1567 | } |
| 1568 | if (tooManyViolationsThisLoop()) { |
| 1569 | return; |
| 1570 | } |
| 1571 | startHandlingViolationException(new ExplicitGcViolation()); |
| 1572 | } |
| 1573 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1574 | public @ThreadPolicyMask int getThreadPolicyMask() { |
| 1575 | return mThreadPolicyMask; |
| 1576 | } |
| 1577 | |
| 1578 | public void setThreadPolicyMask(@ThreadPolicyMask int threadPolicyMask) { |
| 1579 | mThreadPolicyMask = threadPolicyMask; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1582 | // Start handling a violation that just started and hasn't |
| 1583 | // actually run yet (e.g. no disk write or network operation |
| 1584 | // has yet occurred). This sees if we're in an event loop |
| 1585 | // thread and, if so, uses it to roughly measure how long the |
| 1586 | // violation took. |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1587 | void startHandlingViolationException(Violation e) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1588 | final int penaltyMask = (mThreadPolicyMask & PENALTY_ALL); |
| 1589 | final ViolationInfo info = new ViolationInfo(e, penaltyMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1590 | info.violationUptimeMillis = SystemClock.uptimeMillis(); |
| 1591 | handleViolationWithTimingAttempt(info); |
| 1592 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1593 | |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1594 | // Attempts to fill in the provided ViolationInfo's |
| 1595 | // durationMillis field if this thread has a Looper we can use |
| 1596 | // to measure with. We measure from the time of violation |
| 1597 | // until the time the looper is idle again (right before |
| 1598 | // the next epoll_wait) |
| 1599 | void handleViolationWithTimingAttempt(final ViolationInfo info) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1600 | Looper looper = Looper.myLooper(); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1601 | |
| 1602 | // Without a Looper, we're unable to time how long the |
| 1603 | // violation takes place. This case should be rare, as |
| 1604 | // most users will care about timing violations that |
| 1605 | // happen on their main UI thread. Note that this case is |
| 1606 | // also hit when a violation takes place in a Binder |
| 1607 | // thread, in "gather" mode. In this case, the duration |
| 1608 | // of the violation is computed by the ultimate caller and |
| 1609 | // its Looper, if any. |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 1610 | // |
| 1611 | // Also, as a special short-cut case when the only penalty |
| 1612 | // bit is death, we die immediately, rather than timing |
| 1613 | // the violation's duration. This makes it convenient to |
| 1614 | // use in unit tests too, rather than waiting on a Looper. |
| 1615 | // |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1616 | // TODO: if in gather mode, ignore Looper.myLooper() and always |
| 1617 | // go into this immediate mode? |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1618 | if (looper == null || (info.mPenaltyMask == PENALTY_DEATH)) { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1619 | info.durationMillis = -1; // unknown (redundant, already set) |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1620 | onThreadPolicyViolation(info); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1621 | return; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1622 | } |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1623 | |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1624 | final ArrayList<ViolationInfo> records = violationsBeingTimed.get(); |
Brad Fitzpatrick | 191cdf0 | 2010-10-11 11:31:15 -0700 | [diff] [blame] | 1625 | if (records.size() >= MAX_OFFENSES_PER_LOOP) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1626 | // Not worth measuring. Too many offenses in one loop. |
| 1627 | return; |
| 1628 | } |
| 1629 | records.add(info); |
| 1630 | if (records.size() > 1) { |
| 1631 | // There's already been a violation this loop, so we've already |
| 1632 | // registered an idle handler to process the list of violations |
| 1633 | // at the end of this Looper's loop. |
| 1634 | return; |
| 1635 | } |
| 1636 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1637 | final IWindowManager windowManager = |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1638 | info.penaltyEnabled(PENALTY_FLASH) ? sWindowManager.get() : null; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 1639 | if (windowManager != null) { |
| 1640 | try { |
| 1641 | windowManager.showStrictModeViolation(true); |
| 1642 | } catch (RemoteException unused) { |
| 1643 | } |
| 1644 | } |
| 1645 | |
Brad Fitzpatrick | bea168c | 2010-11-30 10:56:48 -0800 | [diff] [blame] | 1646 | // We post a runnable to a Handler (== delay 0 ms) for |
| 1647 | // measuring the end time of a violation instead of using |
| 1648 | // an IdleHandler (as was previously used) because an |
| 1649 | // IdleHandler may not run for quite a long period of time |
| 1650 | // if an ongoing animation is happening and continually |
| 1651 | // posting ASAP (0 ms) animation steps. Animations are |
| 1652 | // throttled back to 60fps via SurfaceFlinger/View |
| 1653 | // invalidates, _not_ by posting frame updates every 16 |
| 1654 | // milliseconds. |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1655 | THREAD_HANDLER |
| 1656 | .get() |
| 1657 | .postAtFrontOfQueue( |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1658 | () -> { |
| 1659 | long loopFinishTime = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | bea168c | 2010-11-30 10:56:48 -0800 | [diff] [blame] | 1660 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1661 | // Note: we do this early, before handling the |
| 1662 | // violation below, as handling the violation |
| 1663 | // may include PENALTY_DEATH and we don't want |
| 1664 | // to keep the red border on. |
| 1665 | if (windowManager != null) { |
| 1666 | try { |
| 1667 | windowManager.showStrictModeViolation(false); |
| 1668 | } catch (RemoteException unused) { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1669 | } |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1670 | } |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1671 | |
| 1672 | for (int n = 0; n < records.size(); ++n) { |
| 1673 | ViolationInfo v = records.get(n); |
| 1674 | v.violationNumThisLoop = n + 1; |
| 1675 | v.durationMillis = |
| 1676 | (int) (loopFinishTime - v.violationUptimeMillis); |
| 1677 | onThreadPolicyViolation(v); |
| 1678 | } |
| 1679 | records.clear(); |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1680 | }); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1683 | // Note: It's possible (even quite likely) that the |
| 1684 | // thread-local policy mask has changed from the time the |
| 1685 | // violation fired and now (after the violating code ran) due |
| 1686 | // to people who push/pop temporary policy in regions of code, |
| 1687 | // hence the policy being passed around. |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1688 | void onThreadPolicyViolation(final ViolationInfo info) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1689 | if (LOG_V) Log.d(TAG, "onThreadPolicyViolation; penalty=" + info.mPenaltyMask); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1690 | |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1691 | if (info.penaltyEnabled(PENALTY_GATHER)) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1692 | ArrayList<ViolationInfo> violations = gatheredViolations.get(); |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 1693 | if (violations == null) { |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1694 | violations = new ArrayList<>(1); |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 1695 | gatheredViolations.set(violations); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1696 | } |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1697 | for (ViolationInfo previous : violations) { |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 1698 | if (info.getStackTrace().equals(previous.getStackTrace())) { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1699 | // Duplicate. Don't log. |
| 1700 | return; |
| 1701 | } |
| 1702 | } |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 1703 | violations.add(info); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1704 | return; |
| 1705 | } |
| 1706 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1707 | // Not perfect, but fast and good enough for dup suppression. |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 1708 | Integer crashFingerprint = info.hashCode(); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1709 | long lastViolationTime = 0; |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1710 | if (mLastViolationTime != null) { |
| 1711 | Long vtime = mLastViolationTime.get(crashFingerprint); |
| 1712 | if (vtime != null) { |
| 1713 | lastViolationTime = vtime; |
| 1714 | } |
| 1715 | } else { |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 1716 | mLastViolationTime = new ArrayMap<>(1); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1717 | } |
| 1718 | long now = SystemClock.uptimeMillis(); |
| 1719 | mLastViolationTime.put(crashFingerprint, now); |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1720 | long timeSinceLastViolationMillis = |
| 1721 | lastViolationTime == 0 ? Long.MAX_VALUE : (now - lastViolationTime); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1722 | |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1723 | if (info.penaltyEnabled(PENALTY_LOG) |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1724 | && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 1725 | sLogger.log(info); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1726 | } |
| 1727 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1728 | final Violation violation = info.mViolation; |
| 1729 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1730 | // Penalties that ActivityManager should execute on our behalf. |
| 1731 | int penaltyMask = 0; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1732 | |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1733 | if (info.penaltyEnabled(PENALTY_DIALOG) |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1734 | && timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1735 | penaltyMask |= PENALTY_DIALOG; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1738 | if (info.penaltyEnabled(PENALTY_DROPBOX) && lastViolationTime == 0) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1739 | penaltyMask |= PENALTY_DROPBOX; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1742 | if (penaltyMask != 0) { |
| 1743 | final boolean justDropBox = (info.mPenaltyMask == PENALTY_DROPBOX); |
Brad Fitzpatrick | 71678dd | 2010-10-28 13:51:58 -0700 | [diff] [blame] | 1744 | if (justDropBox) { |
| 1745 | // If all we're going to ask the activity manager |
| 1746 | // to do is dropbox it (the common case during |
| 1747 | // platform development), we can avoid doing this |
| 1748 | // call synchronously which Binder data suggests |
| 1749 | // isn't always super fast, despite the implementation |
| 1750 | // in the ActivityManager trying to be mostly async. |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1751 | dropboxViolationAsync(penaltyMask, info); |
Jeff Sharkey | da4e50f | 2017-11-06 15:23:03 -0700 | [diff] [blame] | 1752 | } else { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1753 | handleApplicationStrictModeViolation(penaltyMask, info); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1757 | if (info.penaltyEnabled(PENALTY_DEATH)) { |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1758 | throw new RuntimeException("StrictMode ThreadPolicy violation", violation); |
| 1759 | } |
| 1760 | |
| 1761 | // penaltyDeath will cause penaltyCallback to no-op since we cannot guarantee the |
| 1762 | // executor finishes before crashing. |
| 1763 | final OnThreadViolationListener listener = sThreadViolationListener.get(); |
| 1764 | final Executor executor = sThreadViolationExecutor.get(); |
| 1765 | if (listener != null && executor != null) { |
| 1766 | try { |
| 1767 | executor.execute( |
| 1768 | () -> { |
| 1769 | // Lift violated policy to prevent infinite recursion. |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1770 | ThreadPolicy oldPolicy = StrictMode.allowThreadViolations(); |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1771 | try { |
| 1772 | listener.onThreadViolation(violation); |
| 1773 | } finally { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1774 | StrictMode.setThreadPolicy(oldPolicy); |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1775 | } |
| 1776 | }); |
| 1777 | } catch (RejectedExecutionException e) { |
| 1778 | Log.e(TAG, "ThreadPolicy penaltyCallback failed", e); |
| 1779 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | } |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1783 | |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 1784 | private static final BlockGuard.VmPolicy VM_ANDROID_POLICY = new BlockGuard.VmPolicy() { |
| 1785 | @Override |
| 1786 | public void onPathAccess(String path) { |
| 1787 | if (path == null) return; |
| 1788 | |
| 1789 | // NOTE: keep credential-protected paths in sync with Environment.java |
| 1790 | if (path.startsWith("/data/user/") |
| 1791 | || path.startsWith("/data/media/") |
| 1792 | || path.startsWith("/data/system_ce/") |
| 1793 | || path.startsWith("/data/misc_ce/") |
| 1794 | || path.startsWith("/data/vendor_ce/") |
| 1795 | || path.startsWith("/storage/emulated/")) { |
| 1796 | final int second = path.indexOf('/', 1); |
| 1797 | final int third = path.indexOf('/', second + 1); |
| 1798 | final int fourth = path.indexOf('/', third + 1); |
| 1799 | if (fourth == -1) return; |
| 1800 | |
| 1801 | try { |
| 1802 | final int userId = Integer.parseInt(path.substring(third + 1, fourth)); |
| 1803 | onCredentialProtectedPathAccess(path, userId); |
| 1804 | } catch (NumberFormatException ignored) { |
| 1805 | } |
| 1806 | } else if (path.startsWith("/data/data/")) { |
| 1807 | onCredentialProtectedPathAccess(path, UserHandle.USER_SYSTEM); |
| 1808 | } |
| 1809 | } |
| 1810 | }; |
| 1811 | |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 1812 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1813 | * In the common case, as set by conditionallyEnableDebugLogging, we're just dropboxing any |
| 1814 | * violations but not showing a dialog, not loggging, and not killing the process. In these |
| 1815 | * cases we don't need to do a synchronous call to the ActivityManager. This is used by both |
| 1816 | * per-thread and vm-wide violations when applicable. |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 1817 | */ |
| 1818 | private static void dropboxViolationAsync( |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1819 | final int penaltyMask, final ViolationInfo info) { |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 1820 | int outstanding = sDropboxCallsInFlight.incrementAndGet(); |
| 1821 | if (outstanding > 20) { |
| 1822 | // What's going on? Let's not make make the situation |
| 1823 | // worse and just not log. |
| 1824 | sDropboxCallsInFlight.decrementAndGet(); |
| 1825 | return; |
| 1826 | } |
| 1827 | |
| 1828 | if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding); |
| 1829 | |
Jeff Sharkey | da4e50f | 2017-11-06 15:23:03 -0700 | [diff] [blame] | 1830 | BackgroundThread.getHandler().post(() -> { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1831 | handleApplicationStrictModeViolation(penaltyMask, info); |
Jeff Sharkey | da4e50f | 2017-11-06 15:23:03 -0700 | [diff] [blame] | 1832 | int outstandingInner = sDropboxCallsInFlight.decrementAndGet(); |
| 1833 | if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstandingInner); |
| 1834 | }); |
| 1835 | } |
| 1836 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1837 | private static void handleApplicationStrictModeViolation(int penaltyMask, |
Jeff Sharkey | da4e50f | 2017-11-06 15:23:03 -0700 | [diff] [blame] | 1838 | ViolationInfo info) { |
| 1839 | final int oldMask = getThreadPolicyMask(); |
| 1840 | try { |
| 1841 | // First, remove any policy before we call into the Activity Manager, |
| 1842 | // otherwise we'll infinite recurse as we try to log policy violations |
| 1843 | // to disk, thus violating policy, thus requiring logging, etc... |
| 1844 | // We restore the current policy below, in the finally block. |
| 1845 | setThreadPolicyMask(0); |
| 1846 | |
| 1847 | IActivityManager am = ActivityManager.getService(); |
| 1848 | if (am == null) { |
| 1849 | Log.w(TAG, "No activity manager; failed to Dropbox violation."); |
| 1850 | } else { |
| 1851 | am.handleApplicationStrictModeViolation( |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1852 | RuntimeInit.getApplicationObject(), penaltyMask, info); |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 1853 | } |
Jeff Sharkey | da4e50f | 2017-11-06 15:23:03 -0700 | [diff] [blame] | 1854 | } catch (RemoteException e) { |
| 1855 | if (e instanceof DeadObjectException) { |
| 1856 | // System process is dead; ignore |
| 1857 | } else { |
| 1858 | Log.e(TAG, "RemoteException handling StrictMode violation", e); |
| 1859 | } |
| 1860 | } finally { |
| 1861 | setThreadPolicyMask(oldMask); |
| 1862 | } |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
Brian Carlstrom | 4b9b7c3 | 2010-11-08 10:30:40 -0800 | [diff] [blame] | 1865 | private static class AndroidCloseGuardReporter implements CloseGuard.Reporter { |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1866 | public void report(String message, Throwable allocationSite) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1867 | onVmPolicyViolation(new LeakedClosableViolation(message, allocationSite)); |
Brian Carlstrom | 4b9b7c3 | 2010-11-08 10:30:40 -0800 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1871 | /** Called from Parcel.writeNoException() */ |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1872 | /* package */ static boolean hasGatheredViolations() { |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 1873 | return gatheredViolations.get() != null; |
| 1874 | } |
| 1875 | |
| 1876 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1877 | * Called from Parcel.writeException(), so we drop this memory and don't incorrectly attribute |
| 1878 | * it to the wrong caller on the next Binder call on this thread. |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 1879 | */ |
| 1880 | /* package */ static void clearGatheredViolations() { |
| 1881 | gatheredViolations.set(null); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1884 | /** @hide */ |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 1885 | @TestApi |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1886 | public static void conditionallyCheckInstanceCounts() { |
| 1887 | VmPolicy policy = getVmPolicy(); |
Mathieu Chartier | d288a26 | 2015-07-10 13:44:42 -0700 | [diff] [blame] | 1888 | int policySize = policy.classInstanceLimit.size(); |
| 1889 | if (policySize == 0) { |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1890 | return; |
| 1891 | } |
Jeff Sharkey | 6f3a38f | 2014-01-16 12:46:37 -0800 | [diff] [blame] | 1892 | |
| 1893 | System.gc(); |
| 1894 | System.runFinalization(); |
| 1895 | System.gc(); |
| 1896 | |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1897 | // Note: classInstanceLimit is immutable, so this is lock-free |
Mathieu Chartier | d288a26 | 2015-07-10 13:44:42 -0700 | [diff] [blame] | 1898 | // Create the classes array. |
| 1899 | Class[] classes = policy.classInstanceLimit.keySet().toArray(new Class[policySize]); |
| 1900 | long[] instanceCounts = VMDebug.countInstancesOfClasses(classes, false); |
| 1901 | for (int i = 0; i < classes.length; ++i) { |
| 1902 | Class klass = classes[i]; |
| 1903 | int limit = policy.classInstanceLimit.get(klass); |
| 1904 | long instances = instanceCounts[i]; |
| 1905 | if (instances > limit) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 1906 | onVmPolicyViolation(new InstanceCountViolation(klass, instances, limit)); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1907 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | private static long sLastInstanceCountCheckMillis = 0; |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1912 | private static boolean sIsIdlerRegistered = false; // guarded by StrictMode.class |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1913 | private static final MessageQueue.IdleHandler sProcessIdleHandler = |
| 1914 | new MessageQueue.IdleHandler() { |
| 1915 | public boolean queueIdle() { |
| 1916 | long now = SystemClock.uptimeMillis(); |
| 1917 | if (now - sLastInstanceCountCheckMillis > 30 * 1000) { |
| 1918 | sLastInstanceCountCheckMillis = now; |
| 1919 | conditionallyCheckInstanceCounts(); |
| 1920 | } |
| 1921 | return true; |
| 1922 | } |
| 1923 | }; |
| 1924 | |
| 1925 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1926 | * Sets the policy for what actions in the VM process (on any thread) should be detected, as |
| 1927 | * well as the penalty if such actions occur. |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1928 | * |
| 1929 | * @param policy the policy to put into place |
| 1930 | */ |
| 1931 | public static void setVmPolicy(final VmPolicy policy) { |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 1932 | synchronized (StrictMode.class) { |
| 1933 | sVmPolicy = policy; |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 1934 | setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1935 | |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 1936 | Looper looper = Looper.getMainLooper(); |
| 1937 | if (looper != null) { |
| 1938 | MessageQueue mq = looper.mQueue; |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1939 | if (policy.classInstanceLimit.size() == 0 |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 1940 | || (sVmPolicy.mask & PENALTY_ALL) == 0) { |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1941 | mq.removeIdleHandler(sProcessIdleHandler); |
Brad Fitzpatrick | c0bb0bb | 2011-01-20 16:29:52 -0800 | [diff] [blame] | 1942 | sIsIdlerRegistered = false; |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1943 | } else if (!sIsIdlerRegistered) { |
| 1944 | mq.addIdleHandler(sProcessIdleHandler); |
| 1945 | sIsIdlerRegistered = true; |
| 1946 | } |
| 1947 | } |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1948 | |
| 1949 | int networkPolicy = NETWORK_POLICY_ACCEPT; |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 1950 | if ((sVmPolicy.mask & DETECT_VM_CLEARTEXT_NETWORK) != 0) { |
| 1951 | if ((sVmPolicy.mask & PENALTY_DEATH) != 0 |
| 1952 | || (sVmPolicy.mask & PENALTY_DEATH_ON_CLEARTEXT_NETWORK) != 0) { |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1953 | networkPolicy = NETWORK_POLICY_REJECT; |
| 1954 | } else { |
| 1955 | networkPolicy = NETWORK_POLICY_LOG; |
| 1956 | } |
| 1957 | } |
| 1958 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1959 | final INetworkManagementService netd = |
| 1960 | INetworkManagementService.Stub.asInterface( |
| 1961 | ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 1962 | if (netd != null) { |
| 1963 | try { |
| 1964 | netd.setUidCleartextNetworkPolicy(android.os.Process.myUid(), networkPolicy); |
| 1965 | } catch (RemoteException ignored) { |
| 1966 | } |
| 1967 | } else if (networkPolicy != NETWORK_POLICY_ACCEPT) { |
| 1968 | Log.w(TAG, "Dropping requested network policy due to missing service!"); |
| 1969 | } |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 1970 | |
| 1971 | |
| 1972 | if ((sVmPolicy.mask & DETECT_VM_NON_SDK_API_USAGE) != 0) { |
| 1973 | VMRuntime.setNonSdkApiUsageConsumer(sNonSdkApiUsageConsumer); |
Mathew Inwood | d82a743 | 2018-04-19 14:42:58 +0100 | [diff] [blame] | 1974 | VMRuntime.setDedupeHiddenApiWarnings(false); |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 1975 | } else { |
| 1976 | VMRuntime.setNonSdkApiUsageConsumer(null); |
Mathew Inwood | d82a743 | 2018-04-19 14:42:58 +0100 | [diff] [blame] | 1977 | VMRuntime.setDedupeHiddenApiWarnings(true); |
Narayan Kamath | ad1b2a9 | 2018-03-28 11:29:40 +0100 | [diff] [blame] | 1978 | } |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 1979 | |
| 1980 | setBlockGuardVmPolicy(sVmPolicy.mask); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 1981 | } |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1984 | /** Gets the current VM policy. */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1985 | public static VmPolicy getVmPolicy() { |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 1986 | synchronized (StrictMode.class) { |
| 1987 | return sVmPolicy; |
| 1988 | } |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | /** |
Brad Fitzpatrick | 62a1eb5 | 2010-10-18 14:32:59 -0700 | [diff] [blame] | 1992 | * Enable the recommended StrictMode defaults, with violations just being logged. |
| 1993 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 1994 | * <p>This catches disk and network access on the main thread, as well as leaked SQLite cursors |
| 1995 | * and unclosed resources. This is simply a wrapper around {@link #setVmPolicy} and {@link |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 1996 | * #setThreadPolicy}. |
Brad Fitzpatrick | 62a1eb5 | 2010-10-18 14:32:59 -0700 | [diff] [blame] | 1997 | */ |
| 1998 | public static void enableDefaults() { |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 1999 | setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); |
| 2000 | setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build()); |
Brad Fitzpatrick | 62a1eb5 | 2010-10-18 14:32:59 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2003 | /** @hide */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 2004 | public static boolean vmSqliteObjectLeaksEnabled() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2005 | return (sVmPolicy.mask & DETECT_VM_CURSOR_LEAKS) != 0; |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2008 | /** @hide */ |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 2009 | public static boolean vmClosableObjectLeaksEnabled() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2010 | return (sVmPolicy.mask & DETECT_VM_CLOSABLE_LEAKS) != 0; |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2013 | /** @hide */ |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 2014 | public static boolean vmRegistrationLeaksEnabled() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2015 | return (sVmPolicy.mask & DETECT_VM_REGISTRATION_LEAKS) != 0; |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 2016 | } |
| 2017 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2018 | /** @hide */ |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 2019 | public static boolean vmFileUriExposureEnabled() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2020 | return (sVmPolicy.mask & DETECT_VM_FILE_URI_EXPOSURE) != 0; |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2023 | /** @hide */ |
Jeff Sharkey | f361f2f | 2016-12-21 10:48:42 -0700 | [diff] [blame] | 2024 | public static boolean vmCleartextNetworkEnabled() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2025 | return (sVmPolicy.mask & DETECT_VM_CLEARTEXT_NETWORK) != 0; |
Jeff Sharkey | fb833f3 | 2016-12-01 14:59:59 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2028 | /** @hide */ |
Jeff Sharkey | f361f2f | 2016-12-21 10:48:42 -0700 | [diff] [blame] | 2029 | public static boolean vmContentUriWithoutPermissionEnabled() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2030 | return (sVmPolicy.mask & DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION) != 0; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2033 | /** @hide */ |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 2034 | public static boolean vmUntaggedSocketEnabled() { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2035 | return (sVmPolicy.mask & DETECT_VM_UNTAGGED_SOCKET) != 0; |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2038 | /** @hide */ |
Jeff Sharkey | c59a5e7 | 2018-06-21 19:14:58 -0600 | [diff] [blame] | 2039 | public static boolean vmImplicitDirectBootEnabled() { |
| 2040 | return (sVmPolicy.mask & DETECT_VM_IMPLICIT_DIRECT_BOOT) != 0; |
| 2041 | } |
| 2042 | |
| 2043 | /** @hide */ |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 2044 | public static boolean vmCredentialProtectedWhileLockedEnabled() { |
| 2045 | return (sVmPolicy.mask & DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED) != 0; |
| 2046 | } |
| 2047 | |
| 2048 | /** @hide */ |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 2049 | public static void onSqliteObjectLeaked(String message, Throwable originStack) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2050 | onVmPolicyViolation(new SqliteObjectLeakedViolation(message, originStack)); |
Brian Carlstrom | 4b9b7c3 | 2010-11-08 10:30:40 -0800 | [diff] [blame] | 2051 | } |
| 2052 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2053 | /** @hide */ |
Steve Block | 08d584c | 2011-05-17 19:05:03 +0100 | [diff] [blame] | 2054 | public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2055 | onVmPolicyViolation(new WebViewMethodCalledOnWrongThreadViolation(originStack)); |
Steve Block | 08d584c | 2011-05-17 19:05:03 +0100 | [diff] [blame] | 2056 | } |
| 2057 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2058 | /** @hide */ |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 2059 | public static void onIntentReceiverLeaked(Throwable originStack) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2060 | onVmPolicyViolation(new IntentReceiverLeakedViolation(originStack)); |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 2061 | } |
| 2062 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2063 | /** @hide */ |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 2064 | public static void onServiceConnectionLeaked(Throwable originStack) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2065 | onVmPolicyViolation(new ServiceConnectionLeakedViolation(originStack)); |
Jeff Sharkey | d7026f1 | 2012-03-01 20:50:32 -0800 | [diff] [blame] | 2066 | } |
| 2067 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2068 | /** @hide */ |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 2069 | public static void onFileUriExposed(Uri uri, String location) { |
| 2070 | final String message = uri + " exposed beyond app through " + location; |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2071 | if ((sVmPolicy.mask & PENALTY_DEATH_ON_FILE_URI_EXPOSURE) != 0) { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 2072 | throw new FileUriExposedException(message); |
| 2073 | } else { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2074 | onVmPolicyViolation(new FileUriExposedViolation(message)); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 2075 | } |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 2076 | } |
| 2077 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2078 | /** @hide */ |
Jeff Sharkey | fb833f3 | 2016-12-01 14:59:59 -0700 | [diff] [blame] | 2079 | public static void onContentUriWithoutPermission(Uri uri, String location) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2080 | onVmPolicyViolation(new ContentUriWithoutPermissionViolation(uri, location)); |
Jeff Sharkey | fb833f3 | 2016-12-01 14:59:59 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2083 | /** @hide */ |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 2084 | public static void onCleartextNetworkDetected(byte[] firstPacket) { |
| 2085 | byte[] rawAddr = null; |
| 2086 | if (firstPacket != null) { |
| 2087 | if (firstPacket.length >= 20 && (firstPacket[0] & 0xf0) == 0x40) { |
| 2088 | // IPv4 |
| 2089 | rawAddr = new byte[4]; |
| 2090 | System.arraycopy(firstPacket, 16, rawAddr, 0, 4); |
| 2091 | } else if (firstPacket.length >= 40 && (firstPacket[0] & 0xf0) == 0x60) { |
| 2092 | // IPv6 |
| 2093 | rawAddr = new byte[16]; |
| 2094 | System.arraycopy(firstPacket, 24, rawAddr, 0, 16); |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | final int uid = android.os.Process.myUid(); |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 2099 | String msg = "Detected cleartext network traffic from UID " + uid; |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 2100 | if (rawAddr != null) { |
| 2101 | try { |
Kurt Nelson | cf8c4fa | 2017-09-19 12:26:02 -0700 | [diff] [blame] | 2102 | msg += " to " + InetAddress.getByAddress(rawAddr); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 2103 | } catch (UnknownHostException ignored) { |
| 2104 | } |
| 2105 | } |
Kurt Nelson | 9bcd7e9 | 2017-10-25 13:38:06 -0700 | [diff] [blame] | 2106 | msg += HexDump.dumpHexString(firstPacket).trim() + " "; |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2107 | final boolean forceDeath = (sVmPolicy.mask & PENALTY_DEATH_ON_CLEARTEXT_NETWORK) != 0; |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2108 | onVmPolicyViolation(new CleartextNetworkViolation(msg), forceDeath); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2111 | /** @hide */ |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 2112 | public static void onUntaggedSocket() { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2113 | onVmPolicyViolation(new UntaggedSocketViolation()); |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
Jeff Sharkey | c59a5e7 | 2018-06-21 19:14:58 -0600 | [diff] [blame] | 2116 | /** @hide */ |
| 2117 | public static void onImplicitDirectBoot() { |
| 2118 | onVmPolicyViolation(new ImplicitDirectBootViolation()); |
| 2119 | } |
| 2120 | |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 2121 | /** Assume locked until we hear otherwise */ |
| 2122 | private static volatile boolean sUserKeyUnlocked = false; |
| 2123 | |
| 2124 | private static boolean isUserKeyUnlocked(int userId) { |
| 2125 | final IStorageManager storage = IStorageManager.Stub |
| 2126 | .asInterface(ServiceManager.getService("mount")); |
| 2127 | if (storage != null) { |
| 2128 | try { |
| 2129 | return storage.isUserKeyUnlocked(userId); |
| 2130 | } catch (RemoteException ignored) { |
| 2131 | } |
| 2132 | } |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
| 2136 | /** @hide */ |
| 2137 | private static void onCredentialProtectedPathAccess(String path, int userId) { |
| 2138 | // We can cache the unlocked state for the userId we're running as, |
| 2139 | // since any relocking of that user will always result in our |
| 2140 | // process being killed to release any CE FDs we're holding onto. |
| 2141 | if (userId == UserHandle.myUserId()) { |
| 2142 | if (sUserKeyUnlocked) { |
| 2143 | return; |
| 2144 | } else if (isUserKeyUnlocked(userId)) { |
| 2145 | sUserKeyUnlocked = true; |
| 2146 | return; |
| 2147 | } |
| 2148 | } else if (isUserKeyUnlocked(userId)) { |
| 2149 | return; |
| 2150 | } |
| 2151 | |
| 2152 | onVmPolicyViolation(new CredentialProtectedWhileLockedViolation( |
| 2153 | "Accessed credential protected path " + path + " while user " + userId |
| 2154 | + " was locked")); |
| 2155 | } |
| 2156 | |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2157 | // Map from VM violation fingerprint to uptime millis. |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2158 | private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<>(); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2159 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2160 | /** @hide */ |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2161 | public static void onVmPolicyViolation(Violation originStack) { |
Kurt Nelson | 9bcd7e9 | 2017-10-25 13:38:06 -0700 | [diff] [blame] | 2162 | onVmPolicyViolation(originStack, false); |
Jeff Sharkey | 605eb79 | 2014-11-04 13:34:06 -0800 | [diff] [blame] | 2163 | } |
| 2164 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2165 | /** @hide */ |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2166 | public static void onVmPolicyViolation(Violation violation, boolean forceDeath) { |
Kurt Nelson | 2c883f3 | 2017-09-01 10:52:45 -0700 | [diff] [blame] | 2167 | final boolean penaltyDropbox = (sVmPolicy.mask & PENALTY_DROPBOX) != 0; |
| 2168 | final boolean penaltyDeath = ((sVmPolicy.mask & PENALTY_DEATH) != 0) || forceDeath; |
| 2169 | final boolean penaltyLog = (sVmPolicy.mask & PENALTY_LOG) != 0; |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2170 | |
| 2171 | final int penaltyMask = (sVmPolicy.mask & PENALTY_ALL); |
| 2172 | final ViolationInfo info = new ViolationInfo(violation, penaltyMask); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2173 | |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2174 | // Erase stuff not relevant for process-wide violations |
| 2175 | info.numAnimationsRunning = 0; |
| 2176 | info.tags = null; |
| 2177 | info.broadcastIntentAction = null; |
| 2178 | |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2179 | final Integer fingerprint = info.hashCode(); |
| 2180 | final long now = SystemClock.uptimeMillis(); |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2181 | long lastViolationTime; |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2182 | long timeSinceLastViolationMillis = Long.MAX_VALUE; |
| 2183 | synchronized (sLastVmViolationTime) { |
| 2184 | if (sLastVmViolationTime.containsKey(fingerprint)) { |
| 2185 | lastViolationTime = sLastVmViolationTime.get(fingerprint); |
| 2186 | timeSinceLastViolationMillis = now - lastViolationTime; |
| 2187 | } |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2188 | if (timeSinceLastViolationMillis > MIN_VM_INTERVAL_MS) { |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2189 | sLastVmViolationTime.put(fingerprint, now); |
| 2190 | } |
| 2191 | } |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2192 | if (timeSinceLastViolationMillis <= MIN_VM_INTERVAL_MS) { |
| 2193 | // Rate limit all penalties. |
| 2194 | return; |
Jeff Sharkey | c985ffd | 2017-05-24 12:45:40 -0600 | [diff] [blame] | 2195 | } |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2196 | |
| 2197 | if (penaltyLog && sLogger != null && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 2198 | sLogger.log(info); |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 2199 | } |
| 2200 | |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2201 | if (penaltyDropbox) { |
| 2202 | if (penaltyDeath) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2203 | handleApplicationStrictModeViolation(PENALTY_DROPBOX, info); |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2204 | } else { |
| 2205 | // Common case for userdebug/eng builds. If no death and |
| 2206 | // just dropboxing, we can do the ActivityManager call |
| 2207 | // asynchronously. |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2208 | dropboxViolationAsync(PENALTY_DROPBOX, info); |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2209 | } |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 2210 | } |
| 2211 | |
Brad Fitzpatrick | bee2487 | 2010-11-20 12:09:10 -0800 | [diff] [blame] | 2212 | if (penaltyDeath) { |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 2213 | System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down."); |
| 2214 | Process.killProcess(Process.myPid()); |
| 2215 | System.exit(10); |
| 2216 | } |
Kurt Nelson | e6e0ebf | 2017-08-29 18:04:21 -0700 | [diff] [blame] | 2217 | |
| 2218 | // If penaltyDeath, we can't guarantee this callback finishes before the process dies for |
| 2219 | // all executors. penaltyDeath supersedes penaltyCallback. |
| 2220 | if (sVmPolicy.mListener != null && sVmPolicy.mCallbackExecutor != null) { |
| 2221 | final OnVmViolationListener listener = sVmPolicy.mListener; |
| 2222 | try { |
| 2223 | sVmPolicy.mCallbackExecutor.execute( |
| 2224 | () -> { |
| 2225 | // Lift violated policy to prevent infinite recursion. |
| 2226 | VmPolicy oldPolicy = allowVmViolations(); |
| 2227 | try { |
| 2228 | listener.onVmViolation(violation); |
| 2229 | } finally { |
| 2230 | setVmPolicy(oldPolicy); |
| 2231 | } |
| 2232 | }); |
| 2233 | } catch (RejectedExecutionException e) { |
| 2234 | Log.e(TAG, "VmPolicy penaltyCallback failed", e); |
| 2235 | } |
| 2236 | } |
Brad Fitzpatrick | 32e60c7 | 2010-09-30 16:22:36 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2239 | /** Called from Parcel.writeNoException() */ |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2240 | /* package */ static void writeGatheredViolationsToParcel(Parcel p) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2241 | ArrayList<ViolationInfo> violations = gatheredViolations.get(); |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 2242 | if (violations == null) { |
| 2243 | p.writeInt(0); |
| 2244 | } else { |
Jeff Sharkey | 58f27b5 | 2016-12-06 16:47:00 -0700 | [diff] [blame] | 2245 | // To avoid taking up too much transaction space, only include |
| 2246 | // details for the first 3 violations. Deep inside, CrashInfo |
| 2247 | // will truncate each stack trace to ~20kB. |
| 2248 | final int size = Math.min(violations.size(), 3); |
| 2249 | p.writeInt(size); |
| 2250 | for (int i = 0; i < size; i++) { |
| 2251 | violations.get(i).writeToParcel(p, 0); |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 2252 | } |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2253 | } |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 2254 | gatheredViolations.set(null); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2257 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2258 | * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS, we here |
| 2259 | * read back all the encoded violations. |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2260 | */ |
| 2261 | /* package */ static void readAndHandleBinderCallViolations(Parcel p) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2262 | Throwable localCallSite = new Throwable(); |
Jeff Sharkey | 58f27b5 | 2016-12-06 16:47:00 -0700 | [diff] [blame] | 2263 | final int policyMask = getThreadPolicyMask(); |
| 2264 | final boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2265 | |
Jeff Sharkey | 58f27b5 | 2016-12-06 16:47:00 -0700 | [diff] [blame] | 2266 | final int size = p.readInt(); |
| 2267 | for (int i = 0; i < size; i++) { |
| 2268 | final ViolationInfo info = new ViolationInfo(p, !currentlyGathering); |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2269 | info.addLocalStack(localCallSite); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2270 | BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2271 | if (policy instanceof AndroidBlockGuardPolicy) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2272 | ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2278 | * Called from android_util_Binder.cpp's android_os_Parcel_enforceInterface when an incoming |
| 2279 | * Binder call requires changing the StrictMode policy mask. The role of this function is to ask |
| 2280 | * Binder for its current (native) thread-local policy value and synchronize it to libcore's |
| 2281 | * (Java) thread-local policy value. |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 2282 | */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2283 | private static void onBinderStrictModePolicyChange(@ThreadPolicyMask int newPolicy) { |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 2284 | setBlockGuardPolicy(newPolicy); |
| 2285 | } |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2286 | |
| 2287 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2288 | * A tracked, critical time span. (e.g. during an animation.) |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2289 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2290 | * <p>The object itself is a linked list node, to avoid any allocations during rapid span |
| 2291 | * entries and exits. |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2292 | * |
| 2293 | * @hide |
| 2294 | */ |
| 2295 | public static class Span { |
| 2296 | private String mName; |
| 2297 | private long mCreateMillis; |
| 2298 | private Span mNext; |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2299 | private Span mPrev; // not used when in freeList, only active |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2300 | private final ThreadSpanState mContainerState; |
| 2301 | |
| 2302 | Span(ThreadSpanState threadState) { |
| 2303 | mContainerState = threadState; |
| 2304 | } |
| 2305 | |
Brad Fitzpatrick | 1181cbb | 2010-11-16 12:46:16 -0800 | [diff] [blame] | 2306 | // Empty constructor for the NO_OP_SPAN |
| 2307 | protected Span() { |
| 2308 | mContainerState = null; |
| 2309 | } |
| 2310 | |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2311 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2312 | * To be called when the critical span is complete (i.e. the animation is done animating). |
| 2313 | * This can be called on any thread (even a different one from where the animation was |
| 2314 | * taking place), but that's only a defensive implementation measure. It really makes no |
| 2315 | * sense for you to call this on thread other than that where you created it. |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2316 | * |
| 2317 | * @hide |
| 2318 | */ |
| 2319 | public void finish() { |
| 2320 | ThreadSpanState state = mContainerState; |
| 2321 | synchronized (state) { |
| 2322 | if (mName == null) { |
| 2323 | // Duplicate finish call. Ignore. |
| 2324 | return; |
| 2325 | } |
| 2326 | |
| 2327 | // Remove ourselves from the active list. |
| 2328 | if (mPrev != null) { |
| 2329 | mPrev.mNext = mNext; |
| 2330 | } |
| 2331 | if (mNext != null) { |
| 2332 | mNext.mPrev = mPrev; |
| 2333 | } |
| 2334 | if (state.mActiveHead == this) { |
| 2335 | state.mActiveHead = mNext; |
| 2336 | } |
| 2337 | |
Brad Fitzpatrick | 1cc13b6 | 2010-11-16 15:35:58 -0800 | [diff] [blame] | 2338 | state.mActiveSize--; |
| 2339 | |
| 2340 | if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize); |
| 2341 | |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2342 | this.mCreateMillis = -1; |
| 2343 | this.mName = null; |
| 2344 | this.mPrev = null; |
| 2345 | this.mNext = null; |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2346 | |
| 2347 | // Add ourselves to the freeList, if it's not already |
| 2348 | // too big. |
| 2349 | if (state.mFreeListSize < 5) { |
| 2350 | this.mNext = state.mFreeListHead; |
| 2351 | state.mFreeListHead = this; |
| 2352 | state.mFreeListSize++; |
| 2353 | } |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | |
Brad Fitzpatrick | 1181cbb | 2010-11-16 12:46:16 -0800 | [diff] [blame] | 2358 | // The no-op span that's used in user builds. |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2359 | private static final Span NO_OP_SPAN = |
| 2360 | new Span() { |
| 2361 | public void finish() { |
| 2362 | // Do nothing. |
| 2363 | } |
| 2364 | }; |
Brad Fitzpatrick | 1181cbb | 2010-11-16 12:46:16 -0800 | [diff] [blame] | 2365 | |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2366 | /** |
| 2367 | * Linked lists of active spans and a freelist. |
| 2368 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2369 | * <p>Locking notes: there's one of these structures per thread and all members of this |
| 2370 | * structure (as well as the Span nodes under it) are guarded by the ThreadSpanState object |
| 2371 | * instance. While in theory there'd be no locking required because it's all local per-thread, |
| 2372 | * the finish() method above is defensive against people calling it on a different thread from |
| 2373 | * where they created the Span, hence the locking. |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2374 | */ |
| 2375 | private static class ThreadSpanState { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2376 | public Span mActiveHead; // doubly-linked list. |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2377 | public int mActiveSize; |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2378 | public Span mFreeListHead; // singly-linked list. only changes at head. |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2379 | public int mFreeListSize; |
| 2380 | } |
| 2381 | |
| 2382 | private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState = |
| 2383 | new ThreadLocal<ThreadSpanState>() { |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2384 | @Override |
| 2385 | protected ThreadSpanState initialValue() { |
| 2386 | return new ThreadSpanState(); |
| 2387 | } |
| 2388 | }; |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2389 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2390 | private static Singleton<IWindowManager> sWindowManager = |
| 2391 | new Singleton<IWindowManager>() { |
| 2392 | protected IWindowManager create() { |
| 2393 | return IWindowManager.Stub.asInterface(ServiceManager.getService("window")); |
| 2394 | } |
| 2395 | }; |
Brad Fitzpatrick | cdcb73e | 2010-11-22 22:56:23 -0800 | [diff] [blame] | 2396 | |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2397 | /** |
| 2398 | * Enter a named critical span (e.g. an animation) |
| 2399 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2400 | * <p>The name is an arbitary label (or tag) that will be applied to any strictmode violation |
| 2401 | * that happens while this span is active. You must call finish() on the span when done. |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2402 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2403 | * <p>This will never return null, but on devices without debugging enabled, this may return a |
| 2404 | * dummy object on which the finish() method is a no-op. |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2405 | * |
| 2406 | * <p>TODO: add CloseGuard to this, verifying callers call finish. |
| 2407 | * |
| 2408 | * @hide |
| 2409 | */ |
| 2410 | public static Span enterCriticalSpan(String name) { |
Jeff Sharkey | 5ab0243 | 2017-06-27 11:01:36 -0600 | [diff] [blame] | 2411 | if (Build.IS_USER) { |
Brad Fitzpatrick | 1181cbb | 2010-11-16 12:46:16 -0800 | [diff] [blame] | 2412 | return NO_OP_SPAN; |
| 2413 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2414 | if (name == null || name.isEmpty()) { |
| 2415 | throw new IllegalArgumentException("name must be non-null and non-empty"); |
| 2416 | } |
| 2417 | ThreadSpanState state = sThisThreadSpanState.get(); |
| 2418 | Span span = null; |
| 2419 | synchronized (state) { |
| 2420 | if (state.mFreeListHead != null) { |
| 2421 | span = state.mFreeListHead; |
| 2422 | state.mFreeListHead = span.mNext; |
| 2423 | state.mFreeListSize--; |
| 2424 | } else { |
| 2425 | // Shouldn't have to do this often. |
| 2426 | span = new Span(state); |
| 2427 | } |
| 2428 | span.mName = name; |
| 2429 | span.mCreateMillis = SystemClock.uptimeMillis(); |
| 2430 | span.mNext = state.mActiveHead; |
| 2431 | span.mPrev = null; |
| 2432 | state.mActiveHead = span; |
| 2433 | state.mActiveSize++; |
| 2434 | if (span.mNext != null) { |
| 2435 | span.mNext.mPrev = span; |
| 2436 | } |
Brad Fitzpatrick | 1cc13b6 | 2010-11-16 15:35:58 -0800 | [diff] [blame] | 2437 | if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize); |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2438 | } |
| 2439 | return span; |
| 2440 | } |
| 2441 | |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 2442 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2443 | * For code to note that it's slow. This is a no-op unless the current thread's {@link |
| 2444 | * android.os.StrictMode.ThreadPolicy} has {@link |
| 2445 | * android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls} enabled. |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 2446 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2447 | * @param name a short string for the exception stack trace that's built if when this fires. |
Brad Fitzpatrick | e36f9bf | 2010-12-10 13:29:17 -0800 | [diff] [blame] | 2448 | */ |
| 2449 | public static void noteSlowCall(String name) { |
| 2450 | BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2451 | if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2452 | // StrictMode not enabled. |
| 2453 | return; |
| 2454 | } |
| 2455 | ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name); |
| 2456 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2457 | |
| 2458 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2459 | * For code to note that a resource was obtained using a type other than its defined type. This |
| 2460 | * is a no-op unless the current thread's {@link android.os.StrictMode.ThreadPolicy} has {@link |
| 2461 | * android.os.StrictMode.ThreadPolicy.Builder#detectResourceMismatches()} enabled. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 2462 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2463 | * @param tag an object for the exception stack trace that's built if when this fires. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 2464 | * @hide |
| 2465 | */ |
| 2466 | public static void noteResourceMismatch(Object tag) { |
| 2467 | BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2468 | if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2469 | // StrictMode not enabled. |
| 2470 | return; |
| 2471 | } |
| 2472 | ((AndroidBlockGuardPolicy) policy).onResourceMismatch(tag); |
| 2473 | } |
| 2474 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2475 | /** @hide */ |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 2476 | public static void noteUnbufferedIO() { |
| 2477 | BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2478 | if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2479 | // StrictMode not enabled. |
| 2480 | return; |
| 2481 | } |
Kurt Nelson | 9bcd7e9 | 2017-10-25 13:38:06 -0700 | [diff] [blame] | 2482 | policy.onUnbufferedIO(); |
Shubham Ajmera | 9b7184a | 2016-06-28 16:32:02 +0100 | [diff] [blame] | 2483 | } |
| 2484 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2485 | /** @hide */ |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 2486 | public static void noteDiskRead() { |
| 2487 | BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 2488 | if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2489 | // StrictMode not enabled. |
| 2490 | return; |
| 2491 | } |
Kurt Nelson | 9bcd7e9 | 2017-10-25 13:38:06 -0700 | [diff] [blame] | 2492 | policy.onReadFromDisk(); |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 2493 | } |
| 2494 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2495 | /** @hide */ |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 2496 | public static void noteDiskWrite() { |
| 2497 | BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2498 | if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2499 | // StrictMode not enabled. |
| 2500 | return; |
| 2501 | } |
Kurt Nelson | 9bcd7e9 | 2017-10-25 13:38:06 -0700 | [diff] [blame] | 2502 | policy.onWriteToDisk(); |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 2503 | } |
| 2504 | |
Kurt Nelson | 1981f42 | 2017-10-25 13:49:26 -0700 | [diff] [blame] | 2505 | @GuardedBy("StrictMode.class") |
| 2506 | private static final HashMap<Class, Integer> sExpectedActivityInstanceCount = new HashMap<>(); |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2507 | |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 2508 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2509 | * Returns an object that is used to track instances of activites. The activity should store a |
| 2510 | * reference to the tracker object in one of its fields. |
| 2511 | * |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2512 | * @hide |
| 2513 | */ |
| 2514 | public static Object trackActivity(Object instance) { |
| 2515 | return new InstanceTracker(instance); |
| 2516 | } |
| 2517 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2518 | /** @hide */ |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2519 | public static void incrementExpectedActivityCount(Class klass) { |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2520 | if (klass == null) { |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 2521 | return; |
| 2522 | } |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2523 | |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2524 | synchronized (StrictMode.class) { |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2525 | if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) { |
| 2526 | return; |
| 2527 | } |
| 2528 | |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2529 | Integer expected = sExpectedActivityInstanceCount.get(klass); |
| 2530 | Integer newExpected = expected == null ? 1 : expected + 1; |
| 2531 | sExpectedActivityInstanceCount.put(klass, newExpected); |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2535 | /** @hide */ |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2536 | public static void decrementExpectedActivityCount(Class klass) { |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2537 | if (klass == null) { |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 2538 | return; |
| 2539 | } |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2540 | |
| 2541 | final int limit; |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2542 | synchronized (StrictMode.class) { |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2543 | if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) { |
| 2544 | return; |
| 2545 | } |
| 2546 | |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2547 | Integer expected = sExpectedActivityInstanceCount.get(klass); |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2548 | int newExpected = (expected == null || expected == 0) ? 0 : expected - 1; |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2549 | if (newExpected == 0) { |
| 2550 | sExpectedActivityInstanceCount.remove(klass); |
| 2551 | } else { |
| 2552 | sExpectedActivityInstanceCount.put(klass, newExpected); |
| 2553 | } |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2554 | |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2555 | // Note: adding 1 here to give some breathing room during |
| 2556 | // orientation changes. (shouldn't be necessary, though?) |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2557 | limit = newExpected + 1; |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2558 | } |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2559 | |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2560 | // Quick check. |
| 2561 | int actual = InstanceTracker.getInstanceCount(klass); |
| 2562 | if (actual <= limit) { |
| 2563 | return; |
| 2564 | } |
| 2565 | |
| 2566 | // Do a GC and explicit count to double-check. |
| 2567 | // This is the work that we are trying to avoid by tracking the object instances |
| 2568 | // explicity. Running an explicit GC can be expensive (80ms) and so can walking |
| 2569 | // the heap to count instance (30ms). This extra work can make the system feel |
| 2570 | // noticeably less responsive during orientation changes when activities are |
| 2571 | // being restarted. Granted, it is only a problem when StrictMode is enabled |
| 2572 | // but it is annoying. |
Jeff Sharkey | 6f3a38f | 2014-01-16 12:46:37 -0800 | [diff] [blame] | 2573 | |
| 2574 | System.gc(); |
| 2575 | System.runFinalization(); |
| 2576 | System.gc(); |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2577 | |
| 2578 | long instances = VMDebug.countInstancesOfClass(klass, false); |
| 2579 | if (instances > limit) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2580 | onVmPolicyViolation(new InstanceCountViolation(klass, instances, limit)); |
Brad Fitzpatrick | 5f8b5c1 | 2011-01-20 15:12:08 -0800 | [diff] [blame] | 2581 | } |
Brad Fitzpatrick | 7580357 | 2011-01-13 14:21:03 -0800 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2585 | * Parcelable that gets sent in Binder call headers back to callers to report violations that |
| 2586 | * happened during a cross-process call. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2587 | * |
| 2588 | * @hide |
| 2589 | */ |
Kurt Nelson | 7d5c07a | 2017-09-07 16:43:45 -0700 | [diff] [blame] | 2590 | @TestApi |
| 2591 | public static final class ViolationInfo implements Parcelable { |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2592 | /** Stack and violation details. */ |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2593 | private final Violation mViolation; |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2594 | |
Kurt Nelson | 87dd8bc | 2017-10-25 15:24:15 -0700 | [diff] [blame] | 2595 | /** Path leading to a violation that occurred across binder. */ |
| 2596 | private final Deque<StackTraceElement[]> mBinderStack = new ArrayDeque<>(); |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2597 | |
| 2598 | /** Memoized stack trace of full violation. */ |
| 2599 | @Nullable private String mStackTrace; |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2600 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2601 | /** The strict mode penalty mask at the time of violation. */ |
| 2602 | private final int mPenaltyMask; |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2603 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2604 | /** The wall time duration of the violation, when known. -1 when not known. */ |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2605 | public int durationMillis = -1; |
| 2606 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2607 | /** The number of animations currently running. */ |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 2608 | public int numAnimationsRunning = 0; |
| 2609 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2610 | /** List of tags from active Span instances during this violation, or null for none. */ |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2611 | public String[] tags; |
| 2612 | |
| 2613 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2614 | * Which violation number this was (1-based) since the last Looper loop, from the |
| 2615 | * perspective of the root caller (if it crossed any processes via Binder calls). The value |
| 2616 | * is 0 if the root caller wasn't on a Looper thread. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2617 | */ |
| 2618 | public int violationNumThisLoop; |
| 2619 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2620 | /** The time (in terms of SystemClock.uptimeMillis()) that the violation occurred. */ |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2621 | public long violationUptimeMillis; |
| 2622 | |
| 2623 | /** |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2624 | * The action of the Intent being broadcast to somebody's onReceive on this thread right |
| 2625 | * now, or null. |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 2626 | */ |
| 2627 | public String broadcastIntentAction; |
| 2628 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2629 | /** If this is a instance count violation, the number of instances in memory, else -1. */ |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2630 | public long numInstances = -1; |
| 2631 | |
Kurt Nelson | 9bcd7e9 | 2017-10-25 13:38:06 -0700 | [diff] [blame] | 2632 | /** Create an instance of ViolationInfo initialized from an exception. */ |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2633 | ViolationInfo(Violation tr, int penaltyMask) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2634 | this.mViolation = tr; |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2635 | this.mPenaltyMask = penaltyMask; |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2636 | violationUptimeMillis = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 2637 | this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount(); |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 2638 | Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast(); |
| 2639 | if (broadcastIntent != null) { |
| 2640 | broadcastIntentAction = broadcastIntent.getAction(); |
| 2641 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2642 | ThreadSpanState state = sThisThreadSpanState.get(); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2643 | if (tr instanceof InstanceCountViolation) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2644 | this.numInstances = ((InstanceCountViolation) tr).getNumberOfInstances(); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2645 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2646 | synchronized (state) { |
| 2647 | int spanActiveCount = state.mActiveSize; |
| 2648 | if (spanActiveCount > MAX_SPAN_TAGS) { |
| 2649 | spanActiveCount = MAX_SPAN_TAGS; |
| 2650 | } |
| 2651 | if (spanActiveCount != 0) { |
| 2652 | this.tags = new String[spanActiveCount]; |
| 2653 | Span iter = state.mActiveHead; |
| 2654 | int index = 0; |
| 2655 | while (iter != null && index < spanActiveCount) { |
| 2656 | this.tags[index] = iter.mName; |
| 2657 | index++; |
| 2658 | iter = iter.mNext; |
| 2659 | } |
| 2660 | } |
| 2661 | } |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2662 | } |
| 2663 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2664 | /** |
| 2665 | * Equivalent output to |
| 2666 | * {@link android.app.ApplicationErrorReport.CrashInfo#stackTrace}. |
| 2667 | */ |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2668 | public String getStackTrace() { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2669 | if (mStackTrace == null) { |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2670 | StringWriter sw = new StringWriter(); |
| 2671 | PrintWriter pw = new FastPrintWriter(sw, false, 256); |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2672 | mViolation.printStackTrace(pw); |
Kurt Nelson | 87dd8bc | 2017-10-25 15:24:15 -0700 | [diff] [blame] | 2673 | for (StackTraceElement[] traces : mBinderStack) { |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2674 | pw.append("# via Binder call with stack:\n"); |
Kurt Nelson | 87dd8bc | 2017-10-25 15:24:15 -0700 | [diff] [blame] | 2675 | for (StackTraceElement traceElement : traces) { |
| 2676 | pw.append("\tat "); |
| 2677 | pw.append(traceElement.toString()); |
| 2678 | pw.append('\n'); |
| 2679 | } |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2680 | } |
| 2681 | pw.flush(); |
| 2682 | pw.close(); |
| 2683 | mStackTrace = sw.toString(); |
| 2684 | } |
| 2685 | return mStackTrace; |
| 2686 | } |
| 2687 | |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2688 | public Class<? extends Violation> getViolationClass() { |
| 2689 | return mViolation.getClass(); |
| 2690 | } |
| 2691 | |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2692 | /** |
| 2693 | * Optional message describing this violation. |
| 2694 | * |
| 2695 | * @hide |
| 2696 | */ |
| 2697 | @TestApi |
| 2698 | public String getViolationDetails() { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2699 | return mViolation.getMessage(); |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2700 | } |
| 2701 | |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2702 | boolean penaltyEnabled(int p) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2703 | return (mPenaltyMask & p) != 0; |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | /** |
Kurt Nelson | 87dd8bc | 2017-10-25 15:24:15 -0700 | [diff] [blame] | 2707 | * Add a {@link Throwable} from the current process that caused the underlying violation. We |
| 2708 | * only preserve the stack trace elements. |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2709 | * |
| 2710 | * @hide |
| 2711 | */ |
| 2712 | void addLocalStack(Throwable t) { |
Kurt Nelson | 87dd8bc | 2017-10-25 15:24:15 -0700 | [diff] [blame] | 2713 | mBinderStack.addFirst(t.getStackTrace()); |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 2716 | @Override |
| 2717 | public int hashCode() { |
| 2718 | int result = 17; |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2719 | if (mViolation != null) { |
| 2720 | result = 37 * result + mViolation.hashCode(); |
Jeff Sharkey | 58f27b5 | 2016-12-06 16:47:00 -0700 | [diff] [blame] | 2721 | } |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 2722 | if (numAnimationsRunning != 0) { |
| 2723 | result *= 37; |
| 2724 | } |
| 2725 | if (broadcastIntentAction != null) { |
| 2726 | result = 37 * result + broadcastIntentAction.hashCode(); |
| 2727 | } |
| 2728 | if (tags != null) { |
| 2729 | for (String tag : tags) { |
| 2730 | result = 37 * result + tag.hashCode(); |
| 2731 | } |
| 2732 | } |
| 2733 | return result; |
| 2734 | } |
| 2735 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2736 | /** Create an instance of ViolationInfo initialized from a Parcel. */ |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2737 | public ViolationInfo(Parcel in) { |
| 2738 | this(in, false); |
| 2739 | } |
| 2740 | |
| 2741 | /** |
| 2742 | * Create an instance of ViolationInfo initialized from a Parcel. |
| 2743 | * |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2744 | * @param unsetGatheringBit if true, the caller is the root caller and the gathering penalty |
| 2745 | * should be removed. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2746 | */ |
| 2747 | public ViolationInfo(Parcel in, boolean unsetGatheringBit) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2748 | mViolation = (Violation) in.readSerializable(); |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2749 | int binderStackSize = in.readInt(); |
| 2750 | for (int i = 0; i < binderStackSize; i++) { |
Kurt Nelson | 87dd8bc | 2017-10-25 15:24:15 -0700 | [diff] [blame] | 2751 | StackTraceElement[] traceElements = new StackTraceElement[in.readInt()]; |
| 2752 | for (int j = 0; j < traceElements.length; j++) { |
| 2753 | StackTraceElement element = |
| 2754 | new StackTraceElement( |
| 2755 | in.readString(), |
| 2756 | in.readString(), |
| 2757 | in.readString(), |
| 2758 | in.readInt()); |
| 2759 | traceElements[j] = element; |
| 2760 | } |
| 2761 | mBinderStack.add(traceElements); |
Jeff Sharkey | 58f27b5 | 2016-12-06 16:47:00 -0700 | [diff] [blame] | 2762 | } |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2763 | int rawPenaltyMask = in.readInt(); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2764 | if (unsetGatheringBit) { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2765 | mPenaltyMask = rawPenaltyMask & ~PENALTY_GATHER; |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2766 | } else { |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2767 | mPenaltyMask = rawPenaltyMask; |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2768 | } |
| 2769 | durationMillis = in.readInt(); |
| 2770 | violationNumThisLoop = in.readInt(); |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 2771 | numAnimationsRunning = in.readInt(); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2772 | violationUptimeMillis = in.readLong(); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2773 | numInstances = in.readLong(); |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 2774 | broadcastIntentAction = in.readString(); |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2775 | tags = in.readStringArray(); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2778 | /** Save a ViolationInfo instance to a parcel. */ |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 2779 | @Override |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2780 | public void writeToParcel(Parcel dest, int flags) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2781 | dest.writeSerializable(mViolation); |
Kurt Nelson | 0918c93 | 2017-09-07 10:56:56 -0700 | [diff] [blame] | 2782 | dest.writeInt(mBinderStack.size()); |
Kurt Nelson | 87dd8bc | 2017-10-25 15:24:15 -0700 | [diff] [blame] | 2783 | for (StackTraceElement[] traceElements : mBinderStack) { |
| 2784 | dest.writeInt(traceElements.length); |
| 2785 | for (StackTraceElement element : traceElements) { |
| 2786 | dest.writeString(element.getClassName()); |
| 2787 | dest.writeString(element.getMethodName()); |
| 2788 | dest.writeString(element.getFileName()); |
| 2789 | dest.writeInt(element.getLineNumber()); |
| 2790 | } |
Jeff Sharkey | 58f27b5 | 2016-12-06 16:47:00 -0700 | [diff] [blame] | 2791 | } |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 2792 | int start = dest.dataPosition(); |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2793 | dest.writeInt(mPenaltyMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2794 | dest.writeInt(durationMillis); |
| 2795 | dest.writeInt(violationNumThisLoop); |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 2796 | dest.writeInt(numAnimationsRunning); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2797 | dest.writeLong(violationUptimeMillis); |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2798 | dest.writeLong(numInstances); |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 2799 | dest.writeString(broadcastIntentAction); |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2800 | dest.writeStringArray(tags); |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2801 | int total = dest.dataPosition() - start; |
| 2802 | if (Binder.CHECK_PARCEL_SIZE && total > 10 * 1024) { |
| 2803 | Slog.d( |
| 2804 | TAG, |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2805 | "VIO: penalty=" |
| 2806 | + mPenaltyMask |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2807 | + " dur=" |
| 2808 | + durationMillis |
| 2809 | + " numLoop=" |
| 2810 | + violationNumThisLoop |
| 2811 | + " anim=" |
| 2812 | + numAnimationsRunning |
| 2813 | + " uptime=" |
| 2814 | + violationUptimeMillis |
| 2815 | + " numInst=" |
| 2816 | + numInstances); |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 2817 | Slog.d(TAG, "VIO: action=" + broadcastIntentAction); |
| 2818 | Slog.d(TAG, "VIO: tags=" + Arrays.toString(tags)); |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2819 | Slog.d(TAG, "VIO: TOTAL BYTES WRITTEN: " + (dest.dataPosition() - start)); |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 2820 | } |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
Kurt Nelson | 46974a2 | 2017-08-30 13:30:26 -0700 | [diff] [blame] | 2823 | /** Dump a ViolationInfo instance to a Printer. */ |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2824 | public void dump(Printer pw, String prefix) { |
Kurt Nelson | 1bb93bf | 2017-10-26 18:06:12 -0700 | [diff] [blame] | 2825 | pw.println(prefix + "stackTrace: " + getStackTrace()); |
Jeff Sharkey | 3ac2a43 | 2018-06-26 10:52:48 -0600 | [diff] [blame] | 2826 | pw.println(prefix + "penalty: " + mPenaltyMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2827 | if (durationMillis != -1) { |
| 2828 | pw.println(prefix + "durationMillis: " + durationMillis); |
| 2829 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2830 | if (numInstances != -1) { |
| 2831 | pw.println(prefix + "numInstances: " + numInstances); |
| 2832 | } |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2833 | if (violationNumThisLoop != 0) { |
| 2834 | pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop); |
| 2835 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 2836 | if (numAnimationsRunning != 0) { |
| 2837 | pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning); |
| 2838 | } |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2839 | pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis); |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 2840 | if (broadcastIntentAction != null) { |
| 2841 | pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction); |
| 2842 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 2843 | if (tags != null) { |
| 2844 | int index = 0; |
| 2845 | for (String tag : tags) { |
| 2846 | pw.println(prefix + "tag[" + (index++) + "]: " + tag); |
| 2847 | } |
| 2848 | } |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2849 | } |
| 2850 | |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 2851 | @Override |
| 2852 | public int describeContents() { |
| 2853 | return 0; |
| 2854 | } |
| 2855 | |
| 2856 | public static final Parcelable.Creator<ViolationInfo> CREATOR = |
| 2857 | new Parcelable.Creator<ViolationInfo>() { |
| 2858 | @Override |
| 2859 | public ViolationInfo createFromParcel(Parcel in) { |
| 2860 | return new ViolationInfo(in); |
| 2861 | } |
| 2862 | |
| 2863 | @Override |
| 2864 | public ViolationInfo[] newArray(int size) { |
| 2865 | return new ViolationInfo[size]; |
| 2866 | } |
| 2867 | }; |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 2868 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 2869 | |
Jeff Brown | 7e44283 | 2011-06-10 18:00:16 -0700 | [diff] [blame] | 2870 | private static final class InstanceTracker { |
| 2871 | private static final HashMap<Class<?>, Integer> sInstanceCounts = |
| 2872 | new HashMap<Class<?>, Integer>(); |
| 2873 | |
| 2874 | private final Class<?> mKlass; |
| 2875 | |
| 2876 | public InstanceTracker(Object instance) { |
| 2877 | mKlass = instance.getClass(); |
| 2878 | |
| 2879 | synchronized (sInstanceCounts) { |
| 2880 | final Integer value = sInstanceCounts.get(mKlass); |
| 2881 | final int newValue = value != null ? value + 1 : 1; |
| 2882 | sInstanceCounts.put(mKlass, newValue); |
| 2883 | } |
| 2884 | } |
| 2885 | |
| 2886 | @Override |
| 2887 | protected void finalize() throws Throwable { |
| 2888 | try { |
| 2889 | synchronized (sInstanceCounts) { |
| 2890 | final Integer value = sInstanceCounts.get(mKlass); |
| 2891 | if (value != null) { |
| 2892 | final int newValue = value - 1; |
| 2893 | if (newValue > 0) { |
| 2894 | sInstanceCounts.put(mKlass, newValue); |
| 2895 | } else { |
| 2896 | sInstanceCounts.remove(mKlass); |
| 2897 | } |
| 2898 | } |
| 2899 | } |
| 2900 | } finally { |
| 2901 | super.finalize(); |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | public static int getInstanceCount(Class<?> klass) { |
| 2906 | synchronized (sInstanceCounts) { |
| 2907 | final Integer value = sInstanceCounts.get(klass); |
| 2908 | return value != null ? value : 0; |
| 2909 | } |
| 2910 | } |
| 2911 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 2912 | } |