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