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