StrictMode.java revision a2934d5ff0b5c4098408cd583c2c259edf37c118
1438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/* 2438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Copyright (C) 2010 The Android Open Source Project 3438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 4438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Licensed under the Apache License, Version 2.0 (the "License"); 5438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * you may not use this file except in compliance with the License. 6438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * You may obtain a copy of the License at 7438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 8438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * http://www.apache.org/licenses/LICENSE-2.0 9438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 10438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Unless required by applicable law or agreed to in writing, software 11438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * distributed under the License is distributed on an "AS IS" BASIS, 12438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * See the License for the specific language governing permissions and 14438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * limitations under the License. 15438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 16438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpackage android.os; 17438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 18599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrickimport android.animation.ValueAnimator; 19438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ActivityManagerNative; 20bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.app.ActivityThread; 21438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ApplicationErrorReport; 221065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrickimport android.app.IActivityManager; 23d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkeyimport android.content.BroadcastReceiver; 24d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkeyimport android.content.Context; 25bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.content.Intent; 26d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkeyimport android.content.ServiceConnection; 27390517be2d60dd6e6264150c190c372d89bb331aDianne Hackbornimport android.util.ArrayMap; 28438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.util.Log; 29cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrickimport android.util.Printer; 30cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrickimport android.util.Singleton; 316804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrickimport android.view.IWindowManager; 32438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 33438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport com.android.internal.os.RuntimeInit; 34438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 35438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard; 36fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard; 37bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrickimport dalvik.system.VMDebug; 38438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter; 405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter; 415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList; 4246d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap; 435f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrickimport java.util.Map; 44bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger; 4546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 46438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/** 4732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be 4832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix 4932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them. 5015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 5115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or 5215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI 5315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place. Keeping disk 5415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother, 559fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications. By keeping your application's main thread 569fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent 579fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a> 589fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users. 5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is 6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that 6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency. It's often the case that 6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be 6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background 6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes. If possible, it's best to assume that such 6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p> 6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your 6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or 7015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's 7115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method: 7215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 7315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre> 7415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() { 7515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * if (DEVELOPER_MODE) { 7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}() 7732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskReads() 7832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskWrites() 7932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectNetwork() // or .detectAll() for all detectable problems 8032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 8132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 8232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}() 8362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * .detectLeakedSqlLiteObjects() 84fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * .detectLeakedClosableObjects() 8532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 8632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyDeath() 8732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 8815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 8915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * super.onCreate(); 9015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 9115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre> 9215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected. 9432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can 9532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your 9632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen. 9715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are 9915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler}, 10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc. 10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds. In particular, 10232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle. Use 10332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident. Network requests on the UI thread 10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though. 10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not 10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses. While it does 10815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing 10915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best 11015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism. Notably, disk or network access from JNI calls 11115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it. Future versions of Android may catch 11215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode 1134d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk Dougherty * enabled in applications distributed on Google Play. 114438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 115438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode { 116438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static final String TAG = "StrictMode"; 11782829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE); 118438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1191181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); 1206804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE); 1211181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 122c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick /** 123bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate * Boolean system property to disable strict mode checks outright. 124bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate * Set this to 'true' to force disable; 'false' has no effect on other 125bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate * enable/disable policy. 126bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate * @hide 127bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate */ 128bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate public static final String DISABLE_PROPERTY = "persist.sys.strictmode.disable"; 129bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate 130bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate /** 131c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * The boolean system property to control screen flashes on violations. 132c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * 133c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * @hide 134c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick */ 135c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual"; 136c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 13746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only log a duplicate stack trace to the logs every second. 13846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_LOG_INTERVAL_MS = 1000; 13946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 14046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only show an annoying dialog at most every 30 seconds 14146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_DIALOG_INTERVAL_MS = 30000; 14246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 143e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // How many Span tags (e.g. animations) to report. 144e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final int MAX_SPAN_TAGS = 20; 145e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 146191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // How many offending stacks to keep track of (and time) per loop 147191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // of the Looper. 148191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final int MAX_OFFENSES_PER_LOOP = 10; 149191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 15032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Thread-policy: 151438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 15215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 15332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 15415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 15532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_WRITE = 0x01; // for ThreadPolicy 15615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 15715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 15832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 15915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 16032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_READ = 0x02; // for ThreadPolicy 16115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 16215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 16332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 16415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 16532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_NETWORK = 0x04; // for ThreadPolicy 166438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 167e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick /** 168e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * For StrictMode.noteSlowCall() 169e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * 170e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * @hide 171e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick */ 172e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public static final int DETECT_CUSTOM = 0x08; // for ThreadPolicy 173e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 174e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick private static final int ALL_THREAD_DETECT_BITS = 175e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK | DETECT_CUSTOM; 176e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 17732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Process-policy: 178438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 179438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 18032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Note, a "VM_" bit, not thread. 18132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 18232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 183758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public static final int DETECT_VM_CURSOR_LEAKS = 0x200; // for VmPolicy 18432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 18532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 186fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Note, a "VM_" bit, not thread. 187fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 188fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 189758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400; // for VmPolicy 190758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 191758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick /** 192758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * Note, a "VM_" bit, not thread. 193758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * @hide 194758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick */ 195758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public static final int DETECT_VM_ACTIVITY_LEAKS = 0x800; // for VmPolicy 196fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 197fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 19832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 199438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 200bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static final int DETECT_VM_INSTANCE_LEAKS = 0x1000; // for VmPolicy 201bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 202d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 203d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * @hide 204d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 205d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public static final int DETECT_VM_REGISTRATION_LEAKS = 0x2000; // for VmPolicy 206d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 207a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey /** 208a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * @hide 209a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey */ 210a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey private static final int DETECT_VM_FILE_URI_EXPOSURE = 0x4000; // for VmPolicy 211a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey 212bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static final int ALL_VM_DETECT_BITS = 213bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS | 214d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_INSTANCE_LEAKS | 215a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey DETECT_VM_REGISTRATION_LEAKS | DETECT_VM_FILE_URI_EXPOSURE; 216bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 217bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick /** 218bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * @hide 219bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick */ 220438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_LOG = 0x10; // normal android.util.Log 221438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 22232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Used for both process and thread policy: 22332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 224438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 22532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 226438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 227438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DIALOG = 0x20; 228438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 229438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 230b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death on any detected violation. 231b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 23232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 233438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 234438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DEATH = 0x40; 235438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 236438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 237b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death just for detected network usage. 238b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 239b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 240b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 241b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static final int PENALTY_DEATH_ON_NETWORK = 0x200; 242b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 243b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 2446804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during violations. 2456804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * 2466804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * @hide 2476804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 2486804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public static final int PENALTY_FLASH = 0x800; 2496804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 2506804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 25132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 252438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 253438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DROPBOX = 0x80; 254438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 255727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 256727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Non-public penalty mode which overrides all the other penalty 257727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * bits and signals that we're in a Binder call and we should 258727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * ignore the other penalty bits and instead serialize back all 259727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * our offending stack traces to the caller to ultimately handle 260727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * in the originating process. 261727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * 262703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * This must be kept in sync with the constant in libs/binder/Parcel.cpp 263703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * 264727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * @hide 265727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 266727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick public static final int PENALTY_GATHER = 0x100; 267727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 26832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 269c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick * Mask of all the penalty bits valid for thread policies. 27071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick */ 271c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick private static final int THREAD_PENALTY_MASK = 272b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER | 2736804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH; 27471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 275758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 276c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick /** 277c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick * Mask of all the penalty bits valid for VM policies. 278c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick */ 279c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick private static final int VM_PENALTY_MASK = 280c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick PENALTY_LOG | PENALTY_DEATH | PENALTY_DROPBOX; 281c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick 282c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick 283758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick // TODO: wrap in some ImmutableHashMap thing. 284758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick // Note: must be before static initialization of sVmPolicy. 285758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = new HashMap<Class, Integer>(); 286758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 28771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick /** 28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The current VmPolicy in effect. 289758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * 290758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * TODO: these are redundant (mask is in VmPolicy). Should remove sVmPolicyMask. 29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static volatile int sVmPolicyMask = 0; 293758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private static volatile VmPolicy sVmPolicy = VmPolicy.LAX; 29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 295bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 296bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * The number of threads trying to do an async dropbox write. 297bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * Just to limit ourselves out of paranoia. 298bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 299bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0); 300bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private StrictMode() {} 30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to a certain thread. 30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setThreadPolicy}. The current policy 30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can be retrieved with {@link #getThreadPolicy}. 30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note that multiple penalties may be provided and they're run 31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * in order from least to most severe (logging before process 31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * death, for example). There's currently no mechanism to choose 31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * different penalties for different detected actions. 31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class ThreadPolicy { 31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final ThreadPolicy LAX = new ThreadPolicy(0); 31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private ThreadPolicy(int mask) { 32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.ThreadPolicy; mask=" + mask + "]"; 32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 332320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick * Creates {@link ThreadPolicy} instances. Methods whose names start 33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 343320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick * StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 347320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick * StrictMode.setThreadPolicy(policy); 34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask = 0; 35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Create a Builder that detects nothing and has no 35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations. (but note that {@link #build} will default 35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to enabling {@link #penaltyLog} if no other penalties 35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * are specified) 35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder() { 36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = 0; 36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Initialize a Builder from an existing ThreadPolicy. 36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder(ThreadPolicy policy) { 36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = policy.mask; 36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>As of the Gingerbread release this includes network and 37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * disk operations but will likely expand in future releases. 37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 377e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return enable(ALL_THREAD_DETECT_BITS); 37832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 37932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable the detection of everything. 38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitAll() { 384e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return disable(ALL_THREAD_DETECT_BITS); 38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 38632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 38732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 38832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of network operations. 38932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 39032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectNetwork() { 39132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_NETWORK); 39232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 39332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 39432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 39532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of network operations. 39632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 39732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitNetwork() { 39832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_NETWORK); 39932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 40032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 40132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 40232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk reads. 40332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 40432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskReads() { 40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_READ); 40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk reads. 41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskReads() { 41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_READ); 41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 416097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang * Enable detection of slow calls. 417e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick */ 418e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public Builder detectCustomSlowCalls() { 419e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return enable(DETECT_CUSTOM); 420e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 421e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 422e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick /** 423097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang * Disable detection of slow calls. 424e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick */ 425e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public Builder permitCustomSlowCalls() { 426097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang return disable(DETECT_CUSTOM); 427e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 428e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 429e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick /** 43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk writes. 43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskWrites() { 43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE); 43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk writes. 43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskWrites() { 44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE); 44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Show an annoying dialog to the developer on detected 44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations, rate-limited to be only a little annoying. 44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDialog() { 44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DIALOG); 44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 45032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 45132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 45232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crash the whole process on violation. This penalty runs at 45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so you'll still get 45432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * see logging or other violations before the process dies. 455b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 456b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies 457b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * to disk reads, disk writes, and network usage if their 458b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * corresponding detect flags are set. 45932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 46032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 46132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 465b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Crash the whole process on any network usage. Unlike 466b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * {@link #penaltyDeath}, this penalty runs 467b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <em>before</em> anything else. You must still have 468b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * called {@link #detectNetwork} to enable this. 469b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 470b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>In the Honeycomb or later SDKs, this is on by default. 471b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 472b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public Builder penaltyDeathOnNetwork() { 473b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick return enable(PENALTY_DEATH_ON_NETWORK); 474b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 475b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 476b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 4776804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during a violation. 4786804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 4796804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public Builder penaltyFlashScreen() { 4806804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick return enable(PENALTY_FLASH); 4816804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 4826804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 4836804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder disable(int bit) { 50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask &= ~bit; 50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the ThreadPolicy instance. 51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public ThreadPolicy build() { 51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 52032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 52132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 52232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 52332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 52432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 52532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(mMask); 52632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 52832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 52932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to all threads in the virtual machine's process. 53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setVmPolicy}. 53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class VmPolicy { 53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 539758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP); 54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 543758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick // Map from class to max number of allowed instances in memory. 544758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick final HashMap<Class, Integer> classInstanceLimit; 545758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 546758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private VmPolicy(int mask, HashMap<Class, Integer> classInstanceLimit) { 547758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick if (classInstanceLimit == null) { 548758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick throw new NullPointerException("classInstanceLimit == null"); 549758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 551758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick this.classInstanceLimit = classInstanceLimit; 55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.VmPolicy; mask=" + mask + "]"; 55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates {@link VmPolicy} instances. Methods whose names start 56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 56632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 56732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 56832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 56932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 57132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 57332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 57432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 57532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 57632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 57732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 57832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 57932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask; 58032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 581758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private HashMap<Class, Integer> mClassInstanceLimit; // null until needed 582758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private boolean mClassInstanceLimitNeedCow = false; // need copy-on-write 583758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 584758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public Builder() { 585758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mMask = 0; 586758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 587758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 588758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick /** 589758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * Build upon an existing VmPolicy. 590758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick */ 591758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public Builder(VmPolicy base) { 592758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mMask = base.mask; 593758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimitNeedCow = true; 594758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit = base.classInstanceLimit; 595758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 596758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 597758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick /** 598758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * Set an upper bound on how many instances of a class can be in memory 599758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * at once. Helps to prevent object leaks. 600758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick */ 601758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public Builder setClassInstanceLimit(Class klass, int instanceLimit) { 602758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick if (klass == null) { 603758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick throw new NullPointerException("klass == null"); 604758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 605758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick if (mClassInstanceLimitNeedCow) { 606758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick if (mClassInstanceLimit.containsKey(klass) && 607758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit.get(klass) == instanceLimit) { 608758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick // no-op; don't break COW 609758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return this; 610758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 611758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimitNeedCow = false; 612758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone(); 613758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } else if (mClassInstanceLimit == null) { 614758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit = new HashMap<Class, Integer>(); 615758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 616bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mMask |= DETECT_VM_INSTANCE_LEAKS; 617758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit.put(klass, instanceLimit); 618758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return this; 619758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 620758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 621bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick /** 622bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * Detect leaks of {@link android.app.Activity} subclasses. 623bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick */ 624bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public Builder detectActivityLeaks() { 625758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return enable(DETECT_VM_ACTIVITY_LEAKS); 626758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 627758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 62832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 62932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 63032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 631fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>In the Honeycomb release this includes leaks of 632bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * SQLite cursors, Activities, and other closable objects 633bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * but will likely expand in future releases. 63432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 63532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 636d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey return enable(DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_CURSOR_LEAKS 637a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey | DETECT_VM_CLOSABLE_LEAKS | DETECT_VM_REGISTRATION_LEAKS 638a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey | DETECT_VM_FILE_URI_EXPOSURE); 63932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 64032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 64132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 64232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect when an 64332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link android.database.sqlite.SQLiteCursor} or other 64432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * SQLite object is finalized without having been closed. 64532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 64632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You always want to explicitly close your SQLite 64732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * cursors to avoid unnecessary database contention and 64832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * temporary memory leaks. 64932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 65032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectLeakedSqlLiteObjects() { 65132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_VM_CURSOR_LEAKS); 65232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 65332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 65432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 655fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Detect when an {@link java.io.Closeable} or other 656fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * object with a explict termination method is finalized 657fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * without having been closed. 658fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * 659fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>You always want to explicitly close such objects to 660fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * avoid unnecessary resources leaks. 661fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 662fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public Builder detectLeakedClosableObjects() { 663fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CLOSABLE_LEAKS); 664fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 665fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 666fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 667d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * Detect when a {@link BroadcastReceiver} or 668d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * {@link ServiceConnection} is leaked during {@link Context} 669d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * teardown. 670d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 671d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public Builder detectLeakedRegistrationObjects() { 672d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey return enable(DETECT_VM_REGISTRATION_LEAKS); 673d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey } 674d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 675d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 6760fa30372c7768692f9deef3e33655382e8a683fdYing Wang * Detect when a {@code file://} {@link android.net.Uri} is exposed beyond this 677a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * app. The receiving app may not have access to the sent path. 678a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * Instead, when sharing files between apps, {@code content://} 679a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * should be used with permission grants. 680a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey */ 681a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey public Builder detectFileUriExposure() { 682a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey return enable(DETECT_VM_FILE_URI_EXPOSURE); 683a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey } 684a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey 685a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey /** 68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crashes the whole process on violation. This penalty runs at 68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so yo you'll still get 68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * your logging or other violations before the process dies. 68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 69332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 69432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 69532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 69632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 69732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 69932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 70132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 70232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 70332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 70432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 70532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 70632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 70732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 70832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 70932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 71032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 71132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 71232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 71332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 71432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 71532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 71632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 71732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the VmPolicy instance. 71832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 71932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 72032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 72132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 72232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 72332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public VmPolicy build() { 72432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 72532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 72632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 72732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 72832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 72932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 73032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 731758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return new VmPolicy(mMask, 732758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP); 73332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 73432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 73532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 736438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 737438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 7385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Log of strict mode violation stack traces that have occurred 7395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * during a Binder call, to be serialized back later to the caller 7405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * via Parcel.writeNoException() (amusingly) where the caller can 7415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * choose how to react. 7425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 743cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations = 744cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 745cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 746703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // Starts null to avoid unnecessary allocations when 747703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // checking whether there are any violations or not in 748703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // hasGatheredViolations() below. 749703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return null; 7505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick }; 7525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 7535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 75432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions on the current thread should 75532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * be detected, as well as the penalty if such actions occur. 75615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 75732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Internally this sets a thread-local variable which is 75815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagated across cross-process IPC calls, meaning you can 75915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * catch violations when a system service or another process 76015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * accesses the disk or network on your behalf. 761438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 76232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 763438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 76432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setThreadPolicy(final ThreadPolicy policy) { 76532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(policy.mask); 76632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 76732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 76832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static void setThreadPolicyMask(final int policyMask) { 769727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // In addition to the Java-level thread-local in Dalvik's 770727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // BlockGuard, we also need to keep a native thread-local in 771727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Binder in order to propagate the value across Binder calls, 772727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // even across native-only processes. The two are kept in 773727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // sync via the callback to onStrictModePolicyChange, below. 774727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(policyMask); 775727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 776727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // And set the Android native version... 777727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick Binder.setThreadStrictModePolicy(policyMask); 778727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 779727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 780727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Sets the policy in Dalvik/libcore (BlockGuard) 781727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void setBlockGuardPolicy(final int policyMask) { 78246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (policyMask == 0) { 78346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY); 78446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick return; 78546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 786a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey final BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 787a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey final AndroidBlockGuardPolicy androidPolicy; 788a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey if (policy instanceof AndroidBlockGuardPolicy) { 789a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey androidPolicy = (AndroidBlockGuardPolicy) policy; 790438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 791a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey androidPolicy = threadAndroidPolicy.get(); 792a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey BlockGuard.setThreadPolicy(androidPolicy); 793438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 794a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey androidPolicy.setPolicyMask(policyMask); 795438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 796438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 7974b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom // Sets up CloseGuard in Dalvik/libcore 7984b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static void setCloseGuardEnabled(boolean enabled) { 7997c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) { 8004b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setReporter(new AndroidCloseGuardReporter()); 8014b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 8024b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setEnabled(enabled); 8034b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 8044b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 8054e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8064e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8074e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8084e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public static class StrictModeViolation extends BlockGuard.BlockGuardPolicyException { 8094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public StrictModeViolation(int policyState, int policyViolated, String message) { 8104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick super(policyState, policyViolated, message); 8114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 8124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 8134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick 8144e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8154e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8164e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8174e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public static class StrictModeNetworkViolation extends StrictModeViolation { 8185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeNetworkViolation(int policyMask) { 8194e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick super(policyMask, DETECT_NETWORK, null); 8205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 8234e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick private static class StrictModeDiskReadViolation extends StrictModeViolation { 8275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskReadViolation(int policyMask) { 8284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick super(policyMask, DETECT_DISK_READ, null); 8295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 8324e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8334e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8344e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8354e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick private static class StrictModeDiskWriteViolation extends StrictModeViolation { 8365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskWriteViolation(int policyMask) { 8374e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick super(policyMask, DETECT_DISK_WRITE, null); 8385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 8414e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8424e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8434e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8444e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick private static class StrictModeCustomViolation extends StrictModeViolation { 845e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public StrictModeCustomViolation(int policyMask, String name) { 846e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick super(policyMask, DETECT_CUSTOM, name); 847e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 848e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 849e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 850438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 85115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * Returns the bitmask of the current thread's policy. 852438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 85332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled 85432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 85532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 856438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 85732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static int getThreadPolicyMask() { 858438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return BlockGuard.getThreadPolicy().getPolicyMask(); 859438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 860438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 8615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 86232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Returns the current thread's policy. 86332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 86432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy getThreadPolicy() { 865e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // TODO: this was a last minute Gingerbread API change (to 866e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // introduce VmPolicy cleanly) but this isn't particularly 867e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // optimal for users who might call this method often. This 868e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // should be in a thread-local and not allocate on each call. 86932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(getThreadPolicyMask()); 87032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 87132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 87232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 87332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 87432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 87532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit both disk reads & writes, and sets the new policy 87632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 87732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 87897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 87932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to {@link #setThreadPolicy} to 88032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * restore the policy at the end of a block 88197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 88232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskWrites() { 88332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 88432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ); 88532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 88632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 88797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 88832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 88997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 89097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 89197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 89232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 89332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 89432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit disk reads, and sets the new policy 89532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 89632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 89797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 89832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to setThreadPolicy to 89997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * restore the policy. 90097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 90132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskReads() { 90232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 90332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ); 90432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 90532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 90697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 90732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 90897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 90997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 910f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // We don't want to flash the screen red in the system server 911f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // process, nor do we want to modify all the call sites of 912f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // conditionallyEnableDebugLogging() in the system server, 913f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // so instead we use this to determine if we are the system server. 914f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick private static boolean amTheSystemServerProcess() { 915f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // Fast path. Most apps don't have the system server's UID. 916f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick if (Process.myUid() != Process.SYSTEM_UID) { 917f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return false; 918f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 919f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick 920f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // The settings app, though, has the system server's UID so 921f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // look up our stack to see if we came from the system server. 922f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick Throwable stack = new Throwable(); 923f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick stack.fillInStackTrace(); 924f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick for (StackTraceElement ste : stack.getStackTrace()) { 925f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick String clsName = ste.getClassName(); 926f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick if (clsName != null && clsName.startsWith("com.android.server.")) { 927f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return true; 928f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 929f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 930f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return false; 931f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 932f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick 93397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 93450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * Enable DropBox logging for debug phone builds. 93550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * 93650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * @hide 93750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick */ 93850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick public static boolean conditionallyEnableDebugLogging() { 939bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate boolean doFlashes = SystemProperties.getBoolean(VISUAL_PROPERTY, false) 940bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate && !amTheSystemServerProcess(); 941bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate final boolean suppress = SystemProperties.getBoolean(DISABLE_PROPERTY, false); 942c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 94350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // For debug builds, log event loop stalls to dropbox for analysis. 94450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // Similar logic also appears in ActivityThread.java for system apps. 945bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate if (!doFlashes && (IS_USER_BUILD || suppress)) { 9467c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick setCloseGuardEnabled(false); 94750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return false; 94850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 949c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 950bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate // Eng builds have flashes on all the time. The suppression property 951bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate // overrides this, so we force the behavior only after the short-circuit 952bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate // check above. 953bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate if (IS_ENG_BUILD) { 954bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate doFlashes = true; 955bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate } 956bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate 957be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown // Thread policy controls BlockGuard. 958c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick int threadPolicyMask = StrictMode.DETECT_DISK_WRITE | 959c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.DETECT_DISK_READ | 960c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.DETECT_NETWORK; 961c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 962c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (!IS_USER_BUILD) { 963c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick threadPolicyMask |= StrictMode.PENALTY_DROPBOX; 964c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 965c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (doFlashes) { 966c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick threadPolicyMask |= StrictMode.PENALTY_FLASH; 967c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 968c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 969c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.setThreadPolicyMask(threadPolicyMask); 970c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 971be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown // VM Policy controls CloseGuard, detection of Activity leaks, 972be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown // and instance counting. 973c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (IS_USER_BUILD) { 974c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick setCloseGuardEnabled(false); 975c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } else { 976d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown VmPolicy.Builder policyBuilder = new VmPolicy.Builder().detectAll().penaltyDropBox(); 977d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown if (IS_ENG_BUILD) { 978d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown policyBuilder.penaltyLog(); 979d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown } 980d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown setVmPolicy(policyBuilder.build()); 981c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 982c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 98350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return true; 98450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 98550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick 98650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick /** 987b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Used by the framework to make network usage on the main 988b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * thread a fatal error. 989b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 990b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 991b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 992b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static void enableDeathOnNetwork() { 993b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int oldPolicy = getThreadPolicyMask(); 994b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK; 995b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick setThreadPolicyMask(newPolicy); 996b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 997b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 998b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 9995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Parses the BlockGuard policy mask out from the Exception's 10005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * getMessage() String value. Kinda gross, but least 10015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * invasive. :/ 10025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 1003e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * Input is of the following forms: 1004e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * "policy=137 violation=64" 1005e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * "policy=137 violation=64 msg=Arbitrary text" 10065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 10075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Returns 0 on failure, which is a valid policy, but not a 10085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * valid policy during a violation (else there must've been 10095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * some policy in effect to violate). 10105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 10115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parsePolicyFromMessage(String message) { 10125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null || !message.startsWith("policy=")) { 10135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int spaceIndex = message.indexOf(' '); 10165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (spaceIndex == -1) { 10175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String policyString = message.substring(7, spaceIndex); 10205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 10215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(policyString).intValue(); 10225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 10235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 10275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 10285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Like parsePolicyFromMessage(), but returns the violation. 10295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 10305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parseViolationFromMessage(String message) { 10315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null) { 10325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int violationIndex = message.indexOf("violation="); 10355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (violationIndex == -1) { 10365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1038e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick int numberStartIndex = violationIndex + "violation=".length(); 1039e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick int numberEndIndex = message.indexOf(' ', numberStartIndex); 1040e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick if (numberEndIndex == -1) { 1041e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick numberEndIndex = message.length(); 1042e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1043e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick String violationString = message.substring(numberStartIndex, numberEndIndex); 10445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 10455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(violationString).intValue(); 10465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 10475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 1051191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed = 1052191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 1053191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 1054191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return new ArrayList<ViolationInfo>(); 1055191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1056191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick }; 1057191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 1058bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // Note: only access this once verifying the thread has a Looper. 1059bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick private static final ThreadLocal<Handler> threadHandler = new ThreadLocal<Handler>() { 1060bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick @Override protected Handler initialValue() { 1061bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick return new Handler(); 1062bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 1063bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick }; 1064bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 1065a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey private static final ThreadLocal<AndroidBlockGuardPolicy> 1066a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey threadAndroidPolicy = new ThreadLocal<AndroidBlockGuardPolicy>() { 1067a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey @Override 1068a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey protected AndroidBlockGuardPolicy initialValue() { 1069a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey return new AndroidBlockGuardPolicy(0); 1070a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey } 1071a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey }; 1072a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey 1073191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static boolean tooManyViolationsThisLoop() { 1074191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP; 1075191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1076191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 1077438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static class AndroidBlockGuardPolicy implements BlockGuard.Policy { 1078438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private int mPolicyMask; 107946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 108046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Map from violation stacktrace hashcode -> uptimeMillis of 108146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // last violation. No locking needed, as this is only 108246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // accessed by the same thread. 1083390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn private ArrayMap<Integer, Long> mLastViolationTime; 1084438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1085438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public AndroidBlockGuardPolicy(final int policyMask) { 1086438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 1087438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1088438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 10895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick @Override 10905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public String toString() { 10915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask; 10925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 1094438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 1095438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public int getPolicyMask() { 1096438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return mPolicyMask; 1097438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1098438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1099438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 1100438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onWriteToDisk() { 110132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_WRITE) == 0) { 1102438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 1103438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1104191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 1105191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 1106191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1107cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask); 1108cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 1109cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 1110438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1111438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1112e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // Not part of BlockGuard.Policy; just part of StrictMode: 1113e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick void onCustomSlowCall(String name) { 1114e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick if ((mPolicyMask & DETECT_CUSTOM) == 0) { 1115e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return; 1116e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1117e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 1118e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return; 1119e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1120e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeCustomViolation(mPolicyMask, name); 1121e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick e.fillInStackTrace(); 1122e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick startHandlingViolationException(e); 1123e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1124e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 1125438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 1126438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onReadFromDisk() { 112732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_READ) == 0) { 1128438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 1129438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1130191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 1131191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 1132191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1133cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask); 1134cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 1135cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 1136438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1137438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1138438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 1139438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onNetwork() { 114032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_NETWORK) == 0) { 1141438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 1142438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1143b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) { 1144b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick throw new NetworkOnMainThreadException(); 1145b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 1146191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 1147191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 1148191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1149cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask); 1150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 1151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 1152438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1153438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1154438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void setPolicyMask(int policyMask) { 1155438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 1156438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1157438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 11585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Start handling a violation that just started and hasn't 11595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // actually run yet (e.g. no disk write or network operation 11605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // has yet occurred). This sees if we're in an event loop 11615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread and, if so, uses it to roughly measure how long the 11625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation took. 11635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) { 1164cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ViolationInfo info = new ViolationInfo(e, e.getPolicy()); 1165cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.violationUptimeMillis = SystemClock.uptimeMillis(); 1166cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolationWithTimingAttempt(info); 1167cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1168438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1169cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Attempts to fill in the provided ViolationInfo's 1170cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // durationMillis field if this thread has a Looper we can use 1171cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // to measure with. We measure from the time of violation 1172cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // until the time the looper is idle again (right before 1173cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // the next epoll_wait) 1174cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolationWithTimingAttempt(final ViolationInfo info) { 1175438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Looper looper = Looper.myLooper(); 1176cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1177cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Without a Looper, we're unable to time how long the 1178cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // violation takes place. This case should be rare, as 1179cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // most users will care about timing violations that 1180cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // happen on their main UI thread. Note that this case is 1181cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // also hit when a violation takes place in a Binder 1182cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // thread, in "gather" mode. In this case, the duration 1183cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // of the violation is computed by the ultimate caller and 1184cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // its Looper, if any. 11854e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // 11864e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // Also, as a special short-cut case when the only penalty 11874e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // bit is death, we die immediately, rather than timing 11884e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // the violation's duration. This makes it convenient to 11894e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // use in unit tests too, rather than waiting on a Looper. 11904e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // 1191cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // TODO: if in gather mode, ignore Looper.myLooper() and always 1192cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // go into this immediate mode? 11934e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick if (looper == null || 1194c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick (info.policy & THREAD_PENALTY_MASK) == PENALTY_DEATH) { 1195cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis = -1; // unknown (redundant, already set) 1196cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(info); 1197cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 1198438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1199438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1200cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ArrayList<ViolationInfo> records = violationsBeingTimed.get(); 1201191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (records.size() >= MAX_OFFENSES_PER_LOOP) { 1202cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Not worth measuring. Too many offenses in one loop. 1203cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 1204cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1205cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.add(info); 1206cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (records.size() > 1) { 1207cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // There's already been a violation this loop, so we've already 1208cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // registered an idle handler to process the list of violations 1209cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // at the end of this Looper's loop. 1210cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 1211cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1212cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 12136804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ? 1214cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick sWindowManager.get() : null; 12156804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick if (windowManager != null) { 12166804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick try { 12176804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick windowManager.showStrictModeViolation(true); 12186804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } catch (RemoteException unused) { 12196804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 12206804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 12216804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 1222bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // We post a runnable to a Handler (== delay 0 ms) for 1223bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // measuring the end time of a violation instead of using 1224bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // an IdleHandler (as was previously used) because an 1225bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // IdleHandler may not run for quite a long period of time 1226bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // if an ongoing animation is happening and continually 1227bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // posting ASAP (0 ms) animation steps. Animations are 1228bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // throttled back to 60fps via SurfaceFlinger/View 1229bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // invalidates, _not_ by posting frame updates every 16 1230bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // milliseconds. 12313761f33387f980d7a6c6e06555a5a932ba7247a2Jeff Sharkey threadHandler.get().postAtFrontOfQueue(new Runnable() { 1232bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick public void run() { 1233cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick long loopFinishTime = SystemClock.uptimeMillis(); 1234bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 1235bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // Note: we do this early, before handling the 1236bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // violation below, as handling the violation 1237bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // may include PENALTY_DEATH and we don't want 1238bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // to keep the red border on. 1239bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick if (windowManager != null) { 1240bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick try { 1241bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick windowManager.showStrictModeViolation(false); 1242bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } catch (RemoteException unused) { 1243bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 1244bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 1245bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 1246cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (int n = 0; n < records.size(); ++n) { 1247cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo v = records.get(n); 1248cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.violationNumThisLoop = n + 1; 1249cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.durationMillis = 1250cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick (int) (loopFinishTime - v.violationUptimeMillis); 1251cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(v); 1252cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1253cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.clear(); 1254cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1255cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick }); 12565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1257438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 12585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Note: It's possible (even quite likely) that the 12595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread-local policy mask has changed from the time the 12605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation fired and now (after the violating code ran) due 12615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // to people who push/pop temporary policy in regions of code, 12625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // hence the policy being passed around. 1263cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolation(final ViolationInfo info) { 1264cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) { 1265cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.wtf(TAG, "unexpected null stacktrace"); 12665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 12675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1268438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1269cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy); 127046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1271cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_GATHER) != 0) { 1272cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1273703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1274cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations = new ArrayList<ViolationInfo>(1); 1275703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(violations); 1276703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else if (violations.size() >= 5) { 12775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Too many. In a loop or something? Don't gather them all. 12785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 12795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1280cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (ViolationInfo previous : violations) { 1281cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) { 12825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Duplicate. Don't log. 12835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 12845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1286cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations.add(info); 1287727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick return; 1288727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1289727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 129046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Not perfect, but fast and good enough for dup suppression. 1291f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick Integer crashFingerprint = info.hashCode(); 129246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long lastViolationTime = 0; 1293390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn if (mLastViolationTime != null) { 1294390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn Long vtime = mLastViolationTime.get(crashFingerprint); 1295390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn if (vtime != null) { 1296390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn lastViolationTime = vtime; 1297390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn } 1298390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn } else { 1299390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn mLastViolationTime = new ArrayMap<Integer, Long>(1); 130046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 130146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long now = SystemClock.uptimeMillis(); 130246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick mLastViolationTime.put(crashFingerprint, now); 130346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long timeSinceLastViolationMillis = lastViolationTime == 0 ? 130446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Long.MAX_VALUE : (now - lastViolationTime); 130546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1306cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_LOG) != 0 && 130746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1308cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.durationMillis != -1) { 13095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation; ~duration=" + 1310cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis + " ms: " + info.crashInfo.stackTrace); 1311438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 1312cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace); 1313438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1314438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1315438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 131671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // The violationMaskSubset, passed to ActivityManager, is a 131746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // subset of the original StrictMode policy bitmask, with 131846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // only the bit violated and penalty bits to be executed 131946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // by the ActivityManagerService remaining set. 1320cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationMaskSubset = 0; 132146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1322cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DIALOG) != 0 && 132346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) { 1324cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DIALOG; 132546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 132646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1327cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) { 1328cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DROPBOX; 132946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 133046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1331cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationMaskSubset != 0) { 1332cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage); 1333cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= violationBit; 133432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 133571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 1336c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick final boolean justDropBox = (info.policy & THREAD_PENALTY_MASK) == PENALTY_DROPBOX; 133771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick if (justDropBox) { 133871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // If all we're going to ask the activity manager 133971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // to do is dropbox it (the common case during 134071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // platform development), we can avoid doing this 134171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // call synchronously which Binder data suggests 134271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // isn't always super fast, despite the implementation 134371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // in the ActivityManager trying to be mostly async. 1344bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 134571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick return; 134671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } 134771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 134871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // Normal synchronous call to the ActivityManager. 1349438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick try { 1350727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 1351727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 1352727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 1353727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // We restore the current policy below, in the finally block. 135432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 1355727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1356438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 1357438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick RuntimeInit.getApplicationObject(), 1358cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset, 1359cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info); 1360438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } catch (RemoteException e) { 136146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 1362727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } finally { 1363727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Restore the policy. 136432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 1365438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1366438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1367438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1368cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DEATH) != 0) { 13694e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick executeDeathPenalty(info); 1370438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1371438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1372438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1373727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 13744e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick private static void executeDeathPenalty(ViolationInfo info) { 13754e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage); 13764e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick throw new StrictModeViolation(info.policy, violationBit, null); 13774e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 13784e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick 1379bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 1380bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * In the common case, as set by conditionallyEnableDebugLogging, 1381bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * we're just dropboxing any violations but not showing a dialog, 1382bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * not loggging, and not killing the process. In these cases we 1383bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * don't need to do a synchronous call to the ActivityManager. 1384bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * This is used by both per-thread and vm-wide violations when 1385bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * applicable. 1386bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 1387bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static void dropboxViolationAsync( 1388bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick final int violationMaskSubset, final ViolationInfo info) { 1389bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.incrementAndGet(); 1390bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (outstanding > 20) { 1391bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // What's going on? Let's not make make the situation 1392bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // worse and just not log. 1393bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick sDropboxCallsInFlight.decrementAndGet(); 1394bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1395bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1396bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1397bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding); 1398bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1399bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick new Thread("callActivityManagerForStrictModeDropbox") { 1400bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick public void run() { 1401bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 1402bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick try { 14031065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick IActivityManager am = ActivityManagerNative.getDefault(); 14041065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick if (am == null) { 14051065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick Log.d(TAG, "No activity manager; failed to Dropbox violation."); 14061065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick } else { 14071065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick am.handleApplicationStrictModeViolation( 14081065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick RuntimeInit.getApplicationObject(), 14091065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick violationMaskSubset, 14101065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick info); 14111065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick } 1412bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } catch (RemoteException e) { 1413bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Log.e(TAG, "RemoteException handling StrictMode violation", e); 1414bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1415bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.decrementAndGet(); 1416bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding); 1417bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1418bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick }.start(); 1419bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1420bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 14214b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static class AndroidCloseGuardReporter implements CloseGuard.Reporter { 14224b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public void report (String message, Throwable allocationSite) { 14234b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, allocationSite); 14244b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 14254b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 14264b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 1427727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 14285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 14295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 14305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static boolean hasGatheredViolations() { 1431703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return gatheredViolations.get() != null; 1432703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1433703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick 1434703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /** 1435703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Called from Parcel.writeException(), so we drop this memory and 1436703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * don't incorrectly attribute it to the wrong caller on the next 1437703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Binder call on this thread. 1438703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick */ 1439703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /* package */ static void clearGatheredViolations() { 1440703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 14415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 14425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 14435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 1444bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * @hide 1445bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick */ 1446bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public static void conditionallyCheckInstanceCounts() { 1447bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick VmPolicy policy = getVmPolicy(); 1448bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (policy.classInstanceLimit.size() == 0) { 1449bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick return; 1450bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1451bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick Runtime.getRuntime().gc(); 1452bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // Note: classInstanceLimit is immutable, so this is lock-free 14535f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick for (Map.Entry<Class, Integer> entry : policy.classInstanceLimit.entrySet()) { 14545f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Class klass = entry.getKey(); 14555f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick int limit = entry.getValue(); 1456bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick long instances = VMDebug.countInstancesOfClass(klass, false); 1457bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (instances <= limit) { 1458bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick continue; 1459bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1460bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick Throwable tr = new InstanceCountViolation(klass, instances, limit); 1461bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick onVmPolicyViolation(tr.getMessage(), tr); 1462bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1463bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1464bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 1465bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static long sLastInstanceCountCheckMillis = 0; 14665f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick private static boolean sIsIdlerRegistered = false; // guarded by StrictMode.class 1467bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static final MessageQueue.IdleHandler sProcessIdleHandler = 1468bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick new MessageQueue.IdleHandler() { 1469bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public boolean queueIdle() { 1470bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick long now = SystemClock.uptimeMillis(); 1471bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (now - sLastInstanceCountCheckMillis > 30 * 1000) { 1472bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick sLastInstanceCountCheckMillis = now; 1473bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick conditionallyCheckInstanceCounts(); 1474bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1475bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick return true; 1476bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1477bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick }; 1478bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 1479bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick /** 148032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions in the VM process (on any 148132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * thread) should be detected, as well as the penalty if such 148232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * actions occur. 148332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 148432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 148532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 148632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setVmPolicy(final VmPolicy policy) { 14875f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick synchronized (StrictMode.class) { 14885f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sVmPolicy = policy; 14895f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sVmPolicyMask = policy.mask; 14905f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 14915f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 14925f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Looper looper = Looper.getMainLooper(); 14935f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick if (looper != null) { 14945f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick MessageQueue mq = looper.mQueue; 1495c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick if (policy.classInstanceLimit.size() == 0 || 1496c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick (sVmPolicyMask & VM_PENALTY_MASK) == 0) { 1497bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mq.removeIdleHandler(sProcessIdleHandler); 1498c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick sIsIdlerRegistered = false; 1499bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } else if (!sIsIdlerRegistered) { 1500bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mq.addIdleHandler(sProcessIdleHandler); 1501bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick sIsIdlerRegistered = true; 1502bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1503bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1504bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 150532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 150632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 150732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 150832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Gets the current VM policy. 150932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 151032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static VmPolicy getVmPolicy() { 15115f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick synchronized (StrictMode.class) { 15125f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick return sVmPolicy; 15135f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 151432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 151532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 151632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 151762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * Enable the recommended StrictMode defaults, with violations just being logged. 151862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * 151962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * <p>This catches disk and network access on the main thread, as 1520fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * well as leaked SQLite cursors and unclosed resources. This is 1521fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * simply a wrapper around {@link #setVmPolicy} and {@link 1522fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * #setThreadPolicy}. 152362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick */ 152462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick public static void enableDefaults() { 152562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 152662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectAll() 152762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 152862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 152962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 1530e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick .detectAll() 153162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 153262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 153362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick } 153462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick 153562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick /** 153632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 153732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 153832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static boolean vmSqliteObjectLeaksEnabled() { 153932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0; 154032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 154132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 154232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 154332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 154432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 1545fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static boolean vmClosableObjectLeaksEnabled() { 1546fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0; 1547fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 1548fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 1549fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 1550fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 1551fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 1552d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public static boolean vmRegistrationLeaksEnabled() { 1553d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey return (sVmPolicyMask & DETECT_VM_REGISTRATION_LEAKS) != 0; 1554d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey } 1555d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 1556d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 1557d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * @hide 1558d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 1559a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey public static boolean vmFileUriExposureEnabled() { 1560a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey return (sVmPolicyMask & DETECT_VM_FILE_URI_EXPOSURE) != 0; 1561a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey } 1562a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey 1563a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey /** 1564a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * @hide 1565a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey */ 156632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void onSqliteObjectLeaked(String message, Throwable originStack) { 15674b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, originStack); 15684b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 15694b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 157008d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block /** 157108d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block * @hide 157208d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block */ 157308d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) { 157408d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block onVmPolicyViolation(null, originStack); 157508d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block } 157608d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block 1577d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 1578d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * @hide 1579d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 1580d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public static void onIntentReceiverLeaked(Throwable originStack) { 1581d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey onVmPolicyViolation(null, originStack); 1582d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey } 1583d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 1584d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 1585d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * @hide 1586d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 1587d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public static void onServiceConnectionLeaked(Throwable originStack) { 1588d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey onVmPolicyViolation(null, originStack); 1589d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey } 1590d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 1591a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey /** 1592a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * @hide 1593a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey */ 1594a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey public static void onFileUriExposed(String location) { 1595a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey final String message = "file:// Uri exposed through " + location; 1596a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey onVmPolicyViolation(message, new Throwable(message)); 1597a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey } 1598a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey 1599bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // Map from VM violation fingerprint to uptime millis. 1600bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<Integer, Long>(); 1601bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 16024b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom /** 16034b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom * @hide 16044b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom */ 16054b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public static void onVmPolicyViolation(String message, Throwable originStack) { 1606bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0; 1607bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0; 1608bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final boolean penaltyLog = (sVmPolicyMask & PENALTY_LOG) != 0; 1609bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask); 1610bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 16115f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick // Erase stuff not relevant for process-wide violations 16125f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick info.numAnimationsRunning = 0; 16135f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick info.tags = null; 16145f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick info.broadcastIntentAction = null; 16155f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 1616bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final Integer fingerprint = info.hashCode(); 1617bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final long now = SystemClock.uptimeMillis(); 1618bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick long lastViolationTime = 0; 1619bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick long timeSinceLastViolationMillis = Long.MAX_VALUE; 1620bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick synchronized (sLastVmViolationTime) { 1621bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (sLastVmViolationTime.containsKey(fingerprint)) { 1622bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick lastViolationTime = sLastVmViolationTime.get(fingerprint); 1623bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick timeSinceLastViolationMillis = now - lastViolationTime; 1624bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1625bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1626bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick sLastVmViolationTime.put(fingerprint, now); 1627bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 162832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 162932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1630bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (penaltyLog && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1631bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick Log.e(TAG, message, originStack); 1632bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1633bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 1634bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick int violationMaskSubset = PENALTY_DROPBOX | (ALL_VM_DETECT_BITS & sVmPolicyMask); 1635bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1636bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDropbox && !penaltyDeath) { 1637bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // Common case for userdebug/eng builds. If no death and 1638bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // just dropboxing, we can do the ActivityManager call 1639bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // asynchronously. 1640bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 1641bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1642bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 164332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1644bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (penaltyDropbox && lastViolationTime == 0) { 164532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // The violationMask, passed to ActivityManager, is a 164632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // subset of the original StrictMode policy bitmask, with 164732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // only the bit violated and penalty bits to be executed 164832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // by the ActivityManagerService remaining set. 164932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 165032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick try { 165132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 165232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 165332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 165432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // We restore the current policy below, in the finally block. 165532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 165632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 165732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 165832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick RuntimeInit.getApplicationObject(), 165932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick violationMaskSubset, 166032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick info); 166132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } catch (RemoteException e) { 166232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 166332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } finally { 166432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Restore the policy. 166532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 166632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 166732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 166832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1669bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDeath) { 167032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down."); 167132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Process.killProcess(Process.myPid()); 167232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.exit(10); 167332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 167432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 167532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 167632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 16775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 16785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 16795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void writeGatheredViolationsToParcel(Parcel p) { 1680cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1681703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1682703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(0); 1683703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else { 1684703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(violations.size()); 1685703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick for (int i = 0; i < violations.size(); ++i) { 1686703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.get(i).writeToParcel(p, 0 /* unused flags? */); 1687703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1688703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size()); 1689703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.clear(); // somewhat redundant, as we're about to null the threadlocal 16905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1691703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 16925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 16935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 16945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class LogStackTrace extends Exception {} 16955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 16965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 16975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS, 16985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * we here read back all the encoded violations. 16995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 17005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void readAndHandleBinderCallViolations(Parcel p) { 17015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Our own stack trace to append 17025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick StringWriter sw = new StringWriter(); 1703cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new LogStackTrace().printStackTrace(new PrintWriter(sw)); 17045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String ourStack = sw.toString(); 17055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 170632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int policyMask = getThreadPolicyMask(); 1707cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0; 17085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 17095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int numViolations = p.readInt(); 17105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick for (int i = 0; i < numViolations; ++i) { 17115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call. i=" + i); 1712cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo info = new ViolationInfo(p, !currentlyGathering); 1713cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack; 17145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 17155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (policy instanceof AndroidBlockGuardPolicy) { 1716cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info); 17175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 17185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 17195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 17205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 17215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 1722727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Called from android_util_Binder.cpp's 1723727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * android_os_Parcel_enforceInterface when an incoming Binder call 1724727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * requires changing the StrictMode policy mask. The role of this 1725727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * function is to ask Binder for its current (native) thread-local 1726727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * policy value and synchronize it to libcore's (Java) 1727727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * thread-local policy value. 1728727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 1729727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void onBinderStrictModePolicyChange(int newPolicy) { 1730727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(newPolicy); 1731727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1732cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1733cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1734e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * A tracked, critical time span. (e.g. during an animation.) 1735e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1736e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * The object itself is a linked list node, to avoid any allocations 1737e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * during rapid span entries and exits. 1738e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1739e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1740e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1741e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static class Span { 1742e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private String mName; 1743e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private long mCreateMillis; 1744e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mNext; 1745e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mPrev; // not used when in freeList, only active 1746e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private final ThreadSpanState mContainerState; 1747e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1748e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span(ThreadSpanState threadState) { 1749e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mContainerState = threadState; 1750e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1751e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 17521181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Empty constructor for the NO_OP_SPAN 17531181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick protected Span() { 17541181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick mContainerState = null; 17551181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 17561181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1757e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1758e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * To be called when the critical span is complete (i.e. the 1759e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * animation is done animating). This can be called on any 1760e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread (even a different one from where the animation was 1761e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * taking place), but that's only a defensive implementation 1762e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * measure. It really makes no sense for you to call this on 1763e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread other than that where you created it. 1764e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1765e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1766e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1767e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public void finish() { 1768e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = mContainerState; 1769e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1770e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mName == null) { 1771e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Duplicate finish call. Ignore. 1772e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return; 1773e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1774e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1775e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Remove ourselves from the active list. 1776e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mPrev != null) { 1777e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mPrev.mNext = mNext; 1778e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1779e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mNext != null) { 1780e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mNext.mPrev = mPrev; 1781e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1782e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mActiveHead == this) { 1783e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = mNext; 1784e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1785e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 17861cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick state.mActiveSize--; 17871cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 17881cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize); 17891cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 1790e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mCreateMillis = -1; 1791e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mName = null; 1792e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mPrev = null; 1793e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = null; 1794e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1795e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Add ourselves to the freeList, if it's not already 1796e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // too big. 1797e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListSize < 5) { 1798e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = state.mFreeListHead; 1799e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = this; 1800e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize++; 1801e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1802e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1803e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1804e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1805e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 18061181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // The no-op span that's used in user builds. 18071181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final Span NO_OP_SPAN = new Span() { 18081181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick public void finish() { 18091181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Do nothing. 18101181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 18111181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick }; 18121181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1813e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1814e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Linked lists of active spans and a freelist. 1815e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1816e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Locking notes: there's one of these structures per thread and 1817e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * all members of this structure (as well as the Span nodes under 1818e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * it) are guarded by the ThreadSpanState object instance. While 1819e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * in theory there'd be no locking required because it's all local 1820e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * per-thread, the finish() method above is defensive against 1821e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * people calling it on a different thread from where they created 1822e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * the Span, hence the locking. 1823e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1824e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static class ThreadSpanState { 1825e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mActiveHead; // doubly-linked list. 1826e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mActiveSize; 1827e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mFreeListHead; // singly-linked list. only changes at head. 1828e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mFreeListSize; 1829e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1830e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1831e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState = 1832e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick new ThreadLocal<ThreadSpanState>() { 1833e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick @Override protected ThreadSpanState initialValue() { 1834e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return new ThreadSpanState(); 1835e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1836e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick }; 1837e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1838cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() { 1839cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick protected IWindowManager create() { 1840cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick return IWindowManager.Stub.asInterface(ServiceManager.getService("window")); 1841cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick } 1842cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick }; 1843cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick 1844e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1845e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Enter a named critical span (e.g. an animation) 1846e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1847e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>The name is an arbitary label (or tag) that will be applied 1848e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * to any strictmode violation that happens while this span is 1849e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * active. You must call finish() on the span when done. 1850e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1851e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>This will never return null, but on devices without debugging 1852e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * enabled, this may return a dummy object on which the finish() 1853e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * method is a no-op. 1854e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1855e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>TODO: add CloseGuard to this, verifying callers call finish. 1856e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1857e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1858e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1859e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static Span enterCriticalSpan(String name) { 18601181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick if (IS_USER_BUILD) { 18611181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick return NO_OP_SPAN; 18621181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 1863e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (name == null || name.isEmpty()) { 1864e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick throw new IllegalArgumentException("name must be non-null and non-empty"); 1865e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1866e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 1867e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span span = null; 1868e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1869e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListHead != null) { 1870e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = state.mFreeListHead; 1871e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = span.mNext; 1872e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize--; 1873e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } else { 1874e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Shouldn't have to do this often. 1875e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = new Span(state); 1876e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1877e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mName = name; 1878e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mCreateMillis = SystemClock.uptimeMillis(); 1879e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext = state.mActiveHead; 1880e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mPrev = null; 1881e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = span; 1882e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveSize++; 1883e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (span.mNext != null) { 1884e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext.mPrev = span; 1885e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 18861cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize); 1887e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1888e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return span; 1889e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1890e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1891e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick /** 1892e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * For code to note that it's slow. This is a no-op unless the 1893e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * current thread's {@link android.os.StrictMode.ThreadPolicy} has 1894e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls} 1895e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * enabled. 1896e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * 1897e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * @param name a short string for the exception stack trace that's 1898e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * built if when this fires. 1899e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick */ 1900e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public static void noteSlowCall(String name) { 1901e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 1902e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 1903e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // StrictMode not enabled. 1904e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return; 1905e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1906e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name); 1907e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1908e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1909e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 19104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 19114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 19124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public static void noteDiskRead() { 19134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 19144e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 19154e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // StrictMode not enabled. 19164e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick return; 19174e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 19184e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).onReadFromDisk(); 19194e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 19204e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick 19214e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 19224e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 19234e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 19244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public static void noteDiskWrite() { 19254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 19264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 19274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // StrictMode not enabled. 19284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick return; 19294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 19304e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).onWriteToDisk(); 19314e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 19324e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick 19335f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick // Guarded by StrictMode.class 19345f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick private static final HashMap<Class, Integer> sExpectedActivityInstanceCount = 19355f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick new HashMap<Class, Integer>(); 19365f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 19374e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 19387e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown * Returns an object that is used to track instances of activites. 19397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown * The activity should store a reference to the tracker object in one of its fields. 19407e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown * @hide 19417e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown */ 19427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown public static Object trackActivity(Object instance) { 19437e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return new InstanceTracker(instance); 19447e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 19457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19467e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown /** 1947758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * @hide 1948758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick */ 19495f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick public static void incrementExpectedActivityCount(Class klass) { 19507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (klass == null) { 1951758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return; 1952758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 19537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19545f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick synchronized (StrictMode.class) { 19557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) { 19567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return; 19577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 19587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19595f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Integer expected = sExpectedActivityInstanceCount.get(klass); 19605f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Integer newExpected = expected == null ? 1 : expected + 1; 19615f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sExpectedActivityInstanceCount.put(klass, newExpected); 19625f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 19635f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 19645f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 19655f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick /** 19665f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick * @hide 19675f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick */ 19685f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick public static void decrementExpectedActivityCount(Class klass) { 19697e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (klass == null) { 1970758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return; 1971758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 19727e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19737e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final int limit; 19745f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick synchronized (StrictMode.class) { 19757e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) { 19767e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return; 19777e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 19787e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19795f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Integer expected = sExpectedActivityInstanceCount.get(klass); 19807e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown int newExpected = (expected == null || expected == 0) ? 0 : expected - 1; 19815f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick if (newExpected == 0) { 19825f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sExpectedActivityInstanceCount.remove(klass); 19835f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } else { 19845f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sExpectedActivityInstanceCount.put(klass, newExpected); 19855f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 19867e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19875f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick // Note: adding 1 here to give some breathing room during 19885f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick // orientation changes. (shouldn't be necessary, though?) 19897e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown limit = newExpected + 1; 19905f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 19915f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 19927e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // Quick check. 19937e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown int actual = InstanceTracker.getInstanceCount(klass); 19947e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (actual <= limit) { 19957e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return; 19967e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 19977e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19987e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // Do a GC and explicit count to double-check. 19997e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // This is the work that we are trying to avoid by tracking the object instances 20007e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // explicity. Running an explicit GC can be expensive (80ms) and so can walking 20017e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // the heap to count instance (30ms). This extra work can make the system feel 20027e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // noticeably less responsive during orientation changes when activities are 20037e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // being restarted. Granted, it is only a problem when StrictMode is enabled 20047e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // but it is annoying. 20057e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown Runtime.getRuntime().gc(); 20067e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 20077e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown long instances = VMDebug.countInstancesOfClass(klass, false); 20087e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (instances > limit) { 20097e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown Throwable tr = new InstanceCountViolation(klass, instances, limit); 20107e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown onVmPolicyViolation(tr.getMessage(), tr); 20115f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 2012758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 2013758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 2014758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick /** 2015cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Parcelable that gets sent in Binder call headers back to callers 2016cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * to report violations that happened during a cross-process call. 2017cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 2018cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @hide 2019cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2020cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public static class ViolationInfo { 2021cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2022cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Stack and other stuff info. 2023cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2024cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final ApplicationErrorReport.CrashInfo crashInfo; 2025cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2026cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2027cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The strict mode policy mask at the time of violation. 2028cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2029cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final int policy; 2030cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2031cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2032cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The wall time duration of the violation, when known. -1 when 2033cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * not known. 2034cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2035cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int durationMillis = -1; 2036cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2037cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2038599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * The number of animations currently running. 2039599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 2040599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public int numAnimationsRunning = 0; 2041599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 2042599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 2043e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * List of tags from active Span instances during this 2044e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * violation, or null for none. 2045e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 2046e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public String[] tags; 2047e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 2048e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 2049cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Which violation number this was (1-based) since the last Looper loop, 2050cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * from the perspective of the root caller (if it crossed any processes 2051cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * via Binder calls). The value is 0 if the root caller wasn't on a Looper 2052cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * thread. 2053cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2054cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int violationNumThisLoop; 2055cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2056cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2057cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The time (in terms of SystemClock.uptimeMillis()) that the 2058cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * violation occurred. 2059cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2060cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public long violationUptimeMillis; 2061cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2062cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2063bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * The action of the Intent being broadcast to somebody's onReceive 2064bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * on this thread right now, or null. 2065bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick */ 2066bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick public String broadcastIntentAction; 2067bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick 2068bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick /** 2069bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * If this is a instance count violation, the number of instances in memory, 2070bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * else -1. 2071bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick */ 2072bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public long numInstances = -1; 2073bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 2074bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick /** 2075cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an uninitialized instance of ViolationInfo 2076cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2077cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo() { 2078cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = null; 2079cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = 0; 2080cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2081cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2082cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2083cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from an exception. 2084cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2085cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Throwable tr, int policy) { 2086cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(tr); 2087cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = SystemClock.uptimeMillis(); 2088cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this.policy = policy; 2089599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount(); 2090bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast(); 2091bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntent != null) { 2092bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = broadcastIntent.getAction(); 2093bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 2094e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 2095bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (tr instanceof InstanceCountViolation) { 2096bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick this.numInstances = ((InstanceCountViolation) tr).mInstances; 2097bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 2098e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 2099e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int spanActiveCount = state.mActiveSize; 2100e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount > MAX_SPAN_TAGS) { 2101e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick spanActiveCount = MAX_SPAN_TAGS; 2102e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2103e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount != 0) { 2104e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags = new String[spanActiveCount]; 2105e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span iter = state.mActiveHead; 2106e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 2107e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick while (iter != null && index < spanActiveCount) { 2108e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags[index] = iter.mName; 2109e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick index++; 2110e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick iter = iter.mNext; 2111e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2112e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2113e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2114cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2115cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2116f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick @Override 2117f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick public int hashCode() { 2118f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick int result = 17; 2119f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + crashInfo.stackTrace.hashCode(); 2120f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (numAnimationsRunning != 0) { 2121f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result *= 37; 2122f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2123f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (broadcastIntentAction != null) { 2124f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + broadcastIntentAction.hashCode(); 2125f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2126f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (tags != null) { 2127f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick for (String tag : tags) { 2128f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + tag.hashCode(); 2129f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2130f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2131f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick return result; 2132f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2133f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick 2134cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2135cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 2136cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2137cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in) { 2138cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this(in, false); 2139cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2140cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2141cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2142cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 2143cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 2144cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @param unsetGatheringBit if true, the caller is the root caller 2145cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * and the gathering penalty should be removed. 2146cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2147cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in, boolean unsetGatheringBit) { 2148cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(in); 2149cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int rawPolicy = in.readInt(); 2150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (unsetGatheringBit) { 2151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy & ~PENALTY_GATHER; 2152cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } else { 2153cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy; 2154cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2155cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick durationMillis = in.readInt(); 2156cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationNumThisLoop = in.readInt(); 2157599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick numAnimationsRunning = in.readInt(); 2158cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = in.readLong(); 2159bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick numInstances = in.readLong(); 2160bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = in.readString(); 2161e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick tags = in.readStringArray(); 2162cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2163cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2164cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2165cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Save a ViolationInfo instance to a parcel. 2166cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2167cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void writeToParcel(Parcel dest, int flags) { 2168cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.writeToParcel(dest, flags); 2169cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(policy); 2170cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(durationMillis); 2171cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(violationNumThisLoop); 2172599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick dest.writeInt(numAnimationsRunning); 2173cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeLong(violationUptimeMillis); 2174bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick dest.writeLong(numInstances); 2175bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick dest.writeString(broadcastIntentAction); 2176e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick dest.writeStringArray(tags); 2177cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2178cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2179cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2180cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2181cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Dump a ViolationInfo instance to a Printer. 2182cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2183cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void dump(Printer pw, String prefix) { 2184cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.dump(pw, prefix); 2185cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "policy: " + policy); 2186cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (durationMillis != -1) { 2187cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "durationMillis: " + durationMillis); 2188cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2189bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (numInstances != -1) { 2190bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick pw.println(prefix + "numInstances: " + numInstances); 2191bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 2192cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationNumThisLoop != 0) { 2193cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop); 2194cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2195599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick if (numAnimationsRunning != 0) { 2196599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning); 2197599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick } 2198cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis); 2199bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntentAction != null) { 2200bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction); 2201bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 2202e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (tags != null) { 2203e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 2204e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick for (String tag : tags) { 2205e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick pw.println(prefix + "tag[" + (index++) + "]: " + tag); 2206e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2207e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2208cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2209cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2210cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2211bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 2212bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // Dummy throwable, for now, since we don't know when or where the 2213bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // leaked instances came from. We might in the future, but for 2214bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // now we suppress the stack trace because it's useless and/or 2215bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // misleading. 2216bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static class InstanceCountViolation extends Throwable { 2217bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final Class mClass; 2218bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final long mInstances; 2219bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final int mLimit; 2220bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 22215f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick private static final StackTraceElement[] FAKE_STACK = { 22225f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick new StackTraceElement("android.os.StrictMode", "setClassInstanceLimit", 22235f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick "StrictMode.java", 1) 22245f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick }; 2225bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 2226bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public InstanceCountViolation(Class klass, long instances, int limit) { 22275f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick super(klass.toString() + "; instances=" + instances + "; limit=" + limit); 2228bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick setStackTrace(FAKE_STACK); 2229bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mClass = klass; 2230bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mInstances = instances; 2231bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mLimit = limit; 2232bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 2233bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 22347e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22357e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown private static final class InstanceTracker { 22367e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown private static final HashMap<Class<?>, Integer> sInstanceCounts = 22377e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown new HashMap<Class<?>, Integer>(); 22387e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown private final Class<?> mKlass; 22407e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22417e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown public InstanceTracker(Object instance) { 22427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown mKlass = instance.getClass(); 22437e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22447e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown synchronized (sInstanceCounts) { 22457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final Integer value = sInstanceCounts.get(mKlass); 22467e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final int newValue = value != null ? value + 1 : 1; 22477e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown sInstanceCounts.put(mKlass, newValue); 22487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown @Override 22527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown protected void finalize() throws Throwable { 22537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown try { 22547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown synchronized (sInstanceCounts) { 22557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final Integer value = sInstanceCounts.get(mKlass); 22567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (value != null) { 22577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final int newValue = value - 1; 22587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (newValue > 0) { 22597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown sInstanceCounts.put(mKlass, newValue); 22607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } else { 22617e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown sInstanceCounts.remove(mKlass); 22627e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22637e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22647e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22657e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } finally { 22667e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown super.finalize(); 22677e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22687e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22697e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22707e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown public static int getInstanceCount(Class<?> klass) { 22717e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown synchronized (sInstanceCounts) { 22727e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final Integer value = sInstanceCounts.get(klass); 22737e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return value != null ? value : 0; 22747e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22757e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22767e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 2277438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick} 2278