StrictMode.java revision 6f3a38f3afd79ed6dddcef5c83cb442d6749e2ff
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 358c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackbornimport com.android.internal.util.FastPrintWriter; 36438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard; 37fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard; 38bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrickimport dalvik.system.VMDebug; 39438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter; 415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter; 425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList; 4346d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap; 445f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrickimport java.util.Map; 45bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger; 4646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 47438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/** 4832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be 4932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix 5032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them. 5115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 5215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or 5315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI 5415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place. Keeping disk 5515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother, 569fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications. By keeping your application's main thread 579fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent 589fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a> 599fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users. 6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is 6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that 6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency. It's often the case that 6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be 6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background 6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes. If possible, it's best to assume that such 6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p> 6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your 7015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or 7115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's 7215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method: 7315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 7415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre> 7515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() { 7615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * if (DEVELOPER_MODE) { 7732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}() 7832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskReads() 7932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskWrites() 8032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectNetwork() // or .detectAll() for all detectable problems 8132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 8232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 8332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}() 8462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * .detectLeakedSqlLiteObjects() 85fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * .detectLeakedClosableObjects() 8632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 8732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyDeath() 8832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 8915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 9015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * super.onCreate(); 9115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 9215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre> 9315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected. 9532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can 9632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your 9732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen. 9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are 10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler}, 10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc. 10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds. In particular, 10332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle. Use 10432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident. Network requests on the UI thread 10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though. 10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not 10815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses. While it does 10915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing 11015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best 11115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism. Notably, disk or network access from JNI calls 11215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it. Future versions of Android may catch 11315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode 1144d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk Dougherty * enabled in applications distributed on Google Play. 115438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 116438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode { 117438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static final String TAG = "StrictMode"; 11882829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE); 119438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1201181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); 1216804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE); 1221181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 123c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick /** 124bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate * Boolean system property to disable strict mode checks outright. 125bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate * Set this to 'true' to force disable; 'false' has no effect on other 126bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate * enable/disable policy. 127bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate * @hide 128bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate */ 129bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate public static final String DISABLE_PROPERTY = "persist.sys.strictmode.disable"; 130bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate 131bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate /** 132c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * The boolean system property to control screen flashes on violations. 133c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * 134c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * @hide 135c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick */ 136c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual"; 137c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 13846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only log a duplicate stack trace to the logs every second. 13946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_LOG_INTERVAL_MS = 1000; 14046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 14146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only show an annoying dialog at most every 30 seconds 14246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_DIALOG_INTERVAL_MS = 30000; 14346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 144e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // How many Span tags (e.g. animations) to report. 145e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final int MAX_SPAN_TAGS = 20; 146e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 147191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // How many offending stacks to keep track of (and time) per loop 148191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // of the Looper. 149191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final int MAX_OFFENSES_PER_LOOP = 10; 150191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 15132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Thread-policy: 152438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 15315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 15432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 15515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 15632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_WRITE = 0x01; // for ThreadPolicy 15715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 15815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 15932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 16015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 16132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_READ = 0x02; // for ThreadPolicy 16215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 16315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 16432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 16515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 16632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_NETWORK = 0x04; // for ThreadPolicy 167438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 168e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick /** 169e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * For StrictMode.noteSlowCall() 170e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * 171e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * @hide 172e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick */ 173e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public static final int DETECT_CUSTOM = 0x08; // for ThreadPolicy 174e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 175e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick private static final int ALL_THREAD_DETECT_BITS = 176e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK | DETECT_CUSTOM; 177e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 17832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Process-policy: 179438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 180438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 18132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Note, a "VM_" bit, not thread. 18232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 18332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 184758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public static final int DETECT_VM_CURSOR_LEAKS = 0x200; // for VmPolicy 18532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 18632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 187fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Note, a "VM_" bit, not thread. 188fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 189fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 190758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400; // for VmPolicy 191758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 192758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick /** 193758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * Note, a "VM_" bit, not thread. 194758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * @hide 195758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick */ 196758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public static final int DETECT_VM_ACTIVITY_LEAKS = 0x800; // for VmPolicy 197fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 198fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 19932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 200438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 201bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static final int DETECT_VM_INSTANCE_LEAKS = 0x1000; // for VmPolicy 202bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 203d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 204d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * @hide 205d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 206d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public static final int DETECT_VM_REGISTRATION_LEAKS = 0x2000; // for VmPolicy 207d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 208a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey /** 209a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * @hide 210a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey */ 211a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey private static final int DETECT_VM_FILE_URI_EXPOSURE = 0x4000; // for VmPolicy 212a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey 213bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static final int ALL_VM_DETECT_BITS = 214bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS | 215d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_INSTANCE_LEAKS | 216a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey DETECT_VM_REGISTRATION_LEAKS | DETECT_VM_FILE_URI_EXPOSURE; 217bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 218bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick /** 219bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * @hide 220bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick */ 221438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_LOG = 0x10; // normal android.util.Log 222438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 22332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Used for both process and thread policy: 22432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 225438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 22632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 227438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 228438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DIALOG = 0x20; 229438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 230438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 231b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death on any detected violation. 232b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 23332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 234438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 235438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DEATH = 0x40; 236438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 237438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 238b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death just for detected network usage. 239b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 240b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 241b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 242b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static final int PENALTY_DEATH_ON_NETWORK = 0x200; 243b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 244b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 2456804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during violations. 2466804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * 2476804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * @hide 2486804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 2496804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public static final int PENALTY_FLASH = 0x800; 2506804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 2516804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 25232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 253438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 254438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DROPBOX = 0x80; 255438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 256727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 257727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Non-public penalty mode which overrides all the other penalty 258727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * bits and signals that we're in a Binder call and we should 259727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * ignore the other penalty bits and instead serialize back all 260727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * our offending stack traces to the caller to ultimately handle 261727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * in the originating process. 262727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * 263703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * This must be kept in sync with the constant in libs/binder/Parcel.cpp 264703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * 265727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * @hide 266727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 267727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick public static final int PENALTY_GATHER = 0x100; 268727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 26932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 270c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick * Mask of all the penalty bits valid for thread policies. 27171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick */ 272c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick private static final int THREAD_PENALTY_MASK = 273b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER | 2746804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH; 27571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 276758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 277c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick /** 278c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick * Mask of all the penalty bits valid for VM policies. 279c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick */ 280c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick private static final int VM_PENALTY_MASK = 281c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick PENALTY_LOG | PENALTY_DEATH | PENALTY_DROPBOX; 282c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick 283c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick 284758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick // TODO: wrap in some ImmutableHashMap thing. 285758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick // Note: must be before static initialization of sVmPolicy. 286758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = new HashMap<Class, Integer>(); 287758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 28871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick /** 28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The current VmPolicy in effect. 290758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * 291758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * TODO: these are redundant (mask is in VmPolicy). Should remove sVmPolicyMask. 29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static volatile int sVmPolicyMask = 0; 294758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private static volatile VmPolicy sVmPolicy = VmPolicy.LAX; 29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 296bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 297bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * The number of threads trying to do an async dropbox write. 298bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * Just to limit ourselves out of paranoia. 299bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 300bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0); 301bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private StrictMode() {} 30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to a certain thread. 30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setThreadPolicy}. The current policy 30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can be retrieved with {@link #getThreadPolicy}. 30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note that multiple penalties may be provided and they're run 31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * in order from least to most severe (logging before process 31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * death, for example). There's currently no mechanism to choose 31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * different penalties for different detected actions. 31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class ThreadPolicy { 31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final ThreadPolicy LAX = new ThreadPolicy(0); 32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private ThreadPolicy(int mask) { 32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.ThreadPolicy; mask=" + mask + "]"; 33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 333320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick * Creates {@link ThreadPolicy} instances. Methods whose names start 33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 344320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick * StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 348320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick * StrictMode.setThreadPolicy(policy); 34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask = 0; 35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Create a Builder that detects nothing and has no 35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations. (but note that {@link #build} will default 35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to enabling {@link #penaltyLog} if no other penalties 35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * are specified) 35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder() { 36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = 0; 36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Initialize a Builder from an existing ThreadPolicy. 36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder(ThreadPolicy policy) { 36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = policy.mask; 36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>As of the Gingerbread release this includes network and 37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * disk operations but will likely expand in future releases. 37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 378e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return enable(ALL_THREAD_DETECT_BITS); 37932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable the detection of everything. 38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitAll() { 385e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return disable(ALL_THREAD_DETECT_BITS); 38632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 38732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 38832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 38932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of network operations. 39032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 39132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectNetwork() { 39232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_NETWORK); 39332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 39432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 39532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 39632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of network operations. 39732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 39832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitNetwork() { 39932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_NETWORK); 40032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 40132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 40232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 40332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk reads. 40432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskReads() { 40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_READ); 40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk reads. 41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskReads() { 41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_READ); 41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 417097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang * Enable detection of slow calls. 418e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick */ 419e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public Builder detectCustomSlowCalls() { 420e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return enable(DETECT_CUSTOM); 421e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 422e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 423e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick /** 424097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang * Disable detection of slow calls. 425e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick */ 426e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public Builder permitCustomSlowCalls() { 427097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang return disable(DETECT_CUSTOM); 428e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 429e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 430e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick /** 43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk writes. 43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskWrites() { 43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE); 43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk writes. 43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskWrites() { 44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE); 44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Show an annoying dialog to the developer on detected 44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations, rate-limited to be only a little annoying. 44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDialog() { 44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DIALOG); 45032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 45132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 45232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crash the whole process on violation. This penalty runs at 45432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so you'll still get 45532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * see logging or other violations before the process dies. 456b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 457b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies 458b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * to disk reads, disk writes, and network usage if their 459b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * corresponding detect flags are set. 46032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 46132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 466b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Crash the whole process on any network usage. Unlike 467b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * {@link #penaltyDeath}, this penalty runs 468b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <em>before</em> anything else. You must still have 469b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * called {@link #detectNetwork} to enable this. 470b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 471b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>In the Honeycomb or later SDKs, this is on by default. 472b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 473b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public Builder penaltyDeathOnNetwork() { 474b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick return enable(PENALTY_DEATH_ON_NETWORK); 475b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 476b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 477b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 4786804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during a violation. 4796804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 4806804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public Builder penaltyFlashScreen() { 4816804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick return enable(PENALTY_FLASH); 4826804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 4836804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 4846804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder disable(int bit) { 50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask &= ~bit; 50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the ThreadPolicy instance. 51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public ThreadPolicy build() { 51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 52032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 52132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 52232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 52332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 52432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 52532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 52632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(mMask); 52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 52832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 52932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to all threads in the virtual machine's process. 53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setVmPolicy}. 53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class VmPolicy { 53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 540758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP); 54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 544758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick // Map from class to max number of allowed instances in memory. 545758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick final HashMap<Class, Integer> classInstanceLimit; 546758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 547758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private VmPolicy(int mask, HashMap<Class, Integer> classInstanceLimit) { 548758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick if (classInstanceLimit == null) { 549758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick throw new NullPointerException("classInstanceLimit == null"); 550758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 552758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick this.classInstanceLimit = classInstanceLimit; 55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.VmPolicy; mask=" + mask + "]"; 55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates {@link VmPolicy} instances. Methods whose names start 56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 56632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 56732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 56832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 56932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 57132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 57332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 57432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 57532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 57632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 57732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 57832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 57932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 58032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask; 58132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 582758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private HashMap<Class, Integer> mClassInstanceLimit; // null until needed 583758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick private boolean mClassInstanceLimitNeedCow = false; // need copy-on-write 584758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 585758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public Builder() { 586758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mMask = 0; 587758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 588758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 589758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick /** 590758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * Build upon an existing VmPolicy. 591758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick */ 592758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public Builder(VmPolicy base) { 593758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mMask = base.mask; 594758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimitNeedCow = true; 595758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit = base.classInstanceLimit; 596758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 597758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 598758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick /** 599758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * Set an upper bound on how many instances of a class can be in memory 600758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * at once. Helps to prevent object leaks. 601758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick */ 602758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick public Builder setClassInstanceLimit(Class klass, int instanceLimit) { 603758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick if (klass == null) { 604758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick throw new NullPointerException("klass == null"); 605758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 606758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick if (mClassInstanceLimitNeedCow) { 607758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick if (mClassInstanceLimit.containsKey(klass) && 608758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit.get(klass) == instanceLimit) { 609758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick // no-op; don't break COW 610758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return this; 611758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 612758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimitNeedCow = false; 613758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone(); 614758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } else if (mClassInstanceLimit == null) { 615758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit = new HashMap<Class, Integer>(); 616758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 617bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mMask |= DETECT_VM_INSTANCE_LEAKS; 618758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit.put(klass, instanceLimit); 619758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return this; 620758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 621758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 622bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick /** 623bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * Detect leaks of {@link android.app.Activity} subclasses. 624bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick */ 625bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public Builder detectActivityLeaks() { 626758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return enable(DETECT_VM_ACTIVITY_LEAKS); 627758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 628758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 62932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 63032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 63132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 632fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>In the Honeycomb release this includes leaks of 633bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * SQLite cursors, Activities, and other closable objects 634bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * but will likely expand in future releases. 63532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 63632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 637d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey return enable(DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_CURSOR_LEAKS 638a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey | DETECT_VM_CLOSABLE_LEAKS | DETECT_VM_REGISTRATION_LEAKS 639a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey | DETECT_VM_FILE_URI_EXPOSURE); 64032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 64132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 64232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 64332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect when an 64432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link android.database.sqlite.SQLiteCursor} or other 64532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * SQLite object is finalized without having been closed. 64632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 64732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You always want to explicitly close your SQLite 64832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * cursors to avoid unnecessary database contention and 64932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * temporary memory leaks. 65032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 65132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectLeakedSqlLiteObjects() { 65232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_VM_CURSOR_LEAKS); 65332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 65432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 65532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 656fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Detect when an {@link java.io.Closeable} or other 657fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * object with a explict termination method is finalized 658fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * without having been closed. 659fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * 660fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>You always want to explicitly close such objects to 661fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * avoid unnecessary resources leaks. 662fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 663fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public Builder detectLeakedClosableObjects() { 664fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CLOSABLE_LEAKS); 665fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 666fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 667fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 668d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * Detect when a {@link BroadcastReceiver} or 669d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * {@link ServiceConnection} is leaked during {@link Context} 670d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * teardown. 671d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 672d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public Builder detectLeakedRegistrationObjects() { 673d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey return enable(DETECT_VM_REGISTRATION_LEAKS); 674d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey } 675d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 676d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 6770fa30372c7768692f9deef3e33655382e8a683fdYing Wang * Detect when a {@code file://} {@link android.net.Uri} is exposed beyond this 678a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * app. The receiving app may not have access to the sent path. 679a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * Instead, when sharing files between apps, {@code content://} 680a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * should be used with permission grants. 681a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey */ 682a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey public Builder detectFileUriExposure() { 683a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey return enable(DETECT_VM_FILE_URI_EXPOSURE); 684a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey } 685a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey 686a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey /** 68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crashes the whole process on violation. This penalty runs at 68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so yo you'll still get 68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * your logging or other violations before the process dies. 69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 69332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 69432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 69532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 69632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 69732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 69932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 70132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 70232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 70332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 70432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 70532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 70632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 70732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 70832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 70932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 71032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 71132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 71232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 71332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 71432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 71532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 71632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 71732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 71832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the VmPolicy instance. 71932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 72032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 72132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 72232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 72332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 72432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public VmPolicy build() { 72532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 72632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 72732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 72832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 72932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 73032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 73132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 732758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return new VmPolicy(mMask, 733758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP); 73432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 73532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 73632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 737438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 738438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 7395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Log of strict mode violation stack traces that have occurred 7405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * during a Binder call, to be serialized back later to the caller 7415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * via Parcel.writeNoException() (amusingly) where the caller can 7425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * choose how to react. 7435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 744cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations = 745cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 746cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 747703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // Starts null to avoid unnecessary allocations when 748703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // checking whether there are any violations or not in 749703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // hasGatheredViolations() below. 750703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return null; 7515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick }; 7535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 7545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 75532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions on the current thread should 75632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * be detected, as well as the penalty if such actions occur. 75715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 75832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Internally this sets a thread-local variable which is 75915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagated across cross-process IPC calls, meaning you can 76015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * catch violations when a system service or another process 76115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * accesses the disk or network on your behalf. 762438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 76332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 764438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 76532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setThreadPolicy(final ThreadPolicy policy) { 76632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(policy.mask); 76732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 76832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 76932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static void setThreadPolicyMask(final int policyMask) { 770727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // In addition to the Java-level thread-local in Dalvik's 771727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // BlockGuard, we also need to keep a native thread-local in 772727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Binder in order to propagate the value across Binder calls, 773727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // even across native-only processes. The two are kept in 774727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // sync via the callback to onStrictModePolicyChange, below. 775727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(policyMask); 776727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 777727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // And set the Android native version... 778727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick Binder.setThreadStrictModePolicy(policyMask); 779727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 780727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 781727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Sets the policy in Dalvik/libcore (BlockGuard) 782727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void setBlockGuardPolicy(final int policyMask) { 78346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (policyMask == 0) { 78446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY); 78546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick return; 78646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 787a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey final BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 788a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey final AndroidBlockGuardPolicy androidPolicy; 789a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey if (policy instanceof AndroidBlockGuardPolicy) { 790a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey androidPolicy = (AndroidBlockGuardPolicy) policy; 791438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 792a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey androidPolicy = threadAndroidPolicy.get(); 793a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey BlockGuard.setThreadPolicy(androidPolicy); 794438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 795a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey androidPolicy.setPolicyMask(policyMask); 796438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 797438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 7984b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom // Sets up CloseGuard in Dalvik/libcore 7994b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static void setCloseGuardEnabled(boolean enabled) { 8007c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) { 8014b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setReporter(new AndroidCloseGuardReporter()); 8024b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 8034b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setEnabled(enabled); 8044b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 8054b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 8064e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8074e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8084e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public static class StrictModeViolation extends BlockGuard.BlockGuardPolicyException { 8104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public StrictModeViolation(int policyState, int policyViolated, String message) { 8114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick super(policyState, policyViolated, message); 8124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 8134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 8144e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick 8154e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8164e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8174e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8184e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public static class StrictModeNetworkViolation extends StrictModeViolation { 8195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeNetworkViolation(int policyMask) { 8204e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick super(policyMask, DETECT_NETWORK, null); 8215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 8244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick private static class StrictModeDiskReadViolation extends StrictModeViolation { 8285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskReadViolation(int policyMask) { 8294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick super(policyMask, DETECT_DISK_READ, null); 8305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 8334e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8344e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8354e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8364e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick private static class StrictModeDiskWriteViolation extends StrictModeViolation { 8375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskWriteViolation(int policyMask) { 8384e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick super(policyMask, DETECT_DISK_WRITE, null); 8395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 8424e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 8434e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 8444e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 8454e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick private static class StrictModeCustomViolation extends StrictModeViolation { 846e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public StrictModeCustomViolation(int policyMask, String name) { 847e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick super(policyMask, DETECT_CUSTOM, name); 848e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 849e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 850e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 851438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 85215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * Returns the bitmask of the current thread's policy. 853438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 85432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled 85532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 85632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 857438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 85832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static int getThreadPolicyMask() { 859438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return BlockGuard.getThreadPolicy().getPolicyMask(); 860438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 861438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 8625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 86332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Returns the current thread's policy. 86432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 86532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy getThreadPolicy() { 866e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // TODO: this was a last minute Gingerbread API change (to 867e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // introduce VmPolicy cleanly) but this isn't particularly 868e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // optimal for users who might call this method often. This 869e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // should be in a thread-local and not allocate on each call. 87032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(getThreadPolicyMask()); 87132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 87232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 87332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 87432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 87532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 87632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit both disk reads & writes, and sets the new policy 87732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 87832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 87997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 88032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to {@link #setThreadPolicy} to 88132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * restore the policy at the end of a block 88297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 88332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskWrites() { 88432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 88532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ); 88632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 88732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 88897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 88932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 89097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 89197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 89297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 89332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 89432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 89532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit disk reads, and sets the new policy 89632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 89732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 89897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 89932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to setThreadPolicy to 90097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * restore the policy. 90197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 90232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskReads() { 90332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 90432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ); 90532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 90632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 90797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 90832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 90997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 91097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 911f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // We don't want to flash the screen red in the system server 912f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // process, nor do we want to modify all the call sites of 913f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // conditionallyEnableDebugLogging() in the system server, 914f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // so instead we use this to determine if we are the system server. 915f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick private static boolean amTheSystemServerProcess() { 916f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // Fast path. Most apps don't have the system server's UID. 917f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick if (Process.myUid() != Process.SYSTEM_UID) { 918f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return false; 919f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 920f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick 921f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // The settings app, though, has the system server's UID so 922f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // look up our stack to see if we came from the system server. 923f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick Throwable stack = new Throwable(); 924f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick stack.fillInStackTrace(); 925f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick for (StackTraceElement ste : stack.getStackTrace()) { 926f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick String clsName = ste.getClassName(); 927f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick if (clsName != null && clsName.startsWith("com.android.server.")) { 928f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return true; 929f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 930f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 931f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return false; 932f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 933f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick 93497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 93550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * Enable DropBox logging for debug phone builds. 93650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * 93750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * @hide 93850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick */ 93950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick public static boolean conditionallyEnableDebugLogging() { 940bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate boolean doFlashes = SystemProperties.getBoolean(VISUAL_PROPERTY, false) 941bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate && !amTheSystemServerProcess(); 942bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate final boolean suppress = SystemProperties.getBoolean(DISABLE_PROPERTY, false); 943c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 94450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // For debug builds, log event loop stalls to dropbox for analysis. 94550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // Similar logic also appears in ActivityThread.java for system apps. 946bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate if (!doFlashes && (IS_USER_BUILD || suppress)) { 9477c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick setCloseGuardEnabled(false); 94850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return false; 94950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 950c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 951bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate // Eng builds have flashes on all the time. The suppression property 952bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate // overrides this, so we force the behavior only after the short-circuit 953bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate // check above. 954bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate if (IS_ENG_BUILD) { 955bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate doFlashes = true; 956bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate } 957bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate 958be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown // Thread policy controls BlockGuard. 959c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick int threadPolicyMask = StrictMode.DETECT_DISK_WRITE | 960c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.DETECT_DISK_READ | 961c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.DETECT_NETWORK; 962c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 963c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (!IS_USER_BUILD) { 964c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick threadPolicyMask |= StrictMode.PENALTY_DROPBOX; 965c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 966c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (doFlashes) { 967c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick threadPolicyMask |= StrictMode.PENALTY_FLASH; 968c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 969c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 970c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.setThreadPolicyMask(threadPolicyMask); 971c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 972be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown // VM Policy controls CloseGuard, detection of Activity leaks, 973be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown // and instance counting. 974c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (IS_USER_BUILD) { 975c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick setCloseGuardEnabled(false); 976c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } else { 977d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown VmPolicy.Builder policyBuilder = new VmPolicy.Builder().detectAll().penaltyDropBox(); 978d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown if (IS_ENG_BUILD) { 979d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown policyBuilder.penaltyLog(); 980d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown } 981d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown setVmPolicy(policyBuilder.build()); 982c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 983c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 98450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return true; 98550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 98650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick 98750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick /** 988b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Used by the framework to make network usage on the main 989b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * thread a fatal error. 990b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 991b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 992b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 993b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static void enableDeathOnNetwork() { 994b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int oldPolicy = getThreadPolicyMask(); 995b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK; 996b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick setThreadPolicyMask(newPolicy); 997b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 998b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 999b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 10005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Parses the BlockGuard policy mask out from the Exception's 10015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * getMessage() String value. Kinda gross, but least 10025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * invasive. :/ 10035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 1004e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * Input is of the following forms: 1005e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * "policy=137 violation=64" 1006e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * "policy=137 violation=64 msg=Arbitrary text" 10075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 10085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Returns 0 on failure, which is a valid policy, but not a 10095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * valid policy during a violation (else there must've been 10105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * some policy in effect to violate). 10115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 10125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parsePolicyFromMessage(String message) { 10135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null || !message.startsWith("policy=")) { 10145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int spaceIndex = message.indexOf(' '); 10175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (spaceIndex == -1) { 10185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String policyString = message.substring(7, spaceIndex); 10215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 10225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(policyString).intValue(); 10235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 10245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 10285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 10295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Like parsePolicyFromMessage(), but returns the violation. 10305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 10315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parseViolationFromMessage(String message) { 10325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null) { 10335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int violationIndex = message.indexOf("violation="); 10365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (violationIndex == -1) { 10375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1039e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick int numberStartIndex = violationIndex + "violation=".length(); 1040e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick int numberEndIndex = message.indexOf(' ', numberStartIndex); 1041e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick if (numberEndIndex == -1) { 1042e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick numberEndIndex = message.length(); 1043e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1044e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick String violationString = message.substring(numberStartIndex, numberEndIndex); 10455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 10465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(violationString).intValue(); 10475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 10485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 10495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 1052191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed = 1053191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 1054191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 1055191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return new ArrayList<ViolationInfo>(); 1056191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1057191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick }; 1058191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 1059bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // Note: only access this once verifying the thread has a Looper. 1060bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick private static final ThreadLocal<Handler> threadHandler = new ThreadLocal<Handler>() { 1061bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick @Override protected Handler initialValue() { 1062bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick return new Handler(); 1063bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 1064bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick }; 1065bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 1066a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey private static final ThreadLocal<AndroidBlockGuardPolicy> 1067a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey threadAndroidPolicy = new ThreadLocal<AndroidBlockGuardPolicy>() { 1068a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey @Override 1069a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey protected AndroidBlockGuardPolicy initialValue() { 1070a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey return new AndroidBlockGuardPolicy(0); 1071a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey } 1072a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey }; 1073a2934d5ff0b5c4098408cd583c2c259edf37c118Jeff Sharkey 1074191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static boolean tooManyViolationsThisLoop() { 1075191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP; 1076191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1077191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 1078438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static class AndroidBlockGuardPolicy implements BlockGuard.Policy { 1079438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private int mPolicyMask; 108046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 108146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Map from violation stacktrace hashcode -> uptimeMillis of 108246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // last violation. No locking needed, as this is only 108346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // accessed by the same thread. 1084390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn private ArrayMap<Integer, Long> mLastViolationTime; 1085438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1086438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public AndroidBlockGuardPolicy(final int policyMask) { 1087438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 1088438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1089438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 10905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick @Override 10915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public String toString() { 10925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask; 10935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 1095438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 1096438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public int getPolicyMask() { 1097438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return mPolicyMask; 1098438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1099438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1100438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 1101438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onWriteToDisk() { 110232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_WRITE) == 0) { 1103438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 1104438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1105191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 1106191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 1107191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1108cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask); 1109cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 1110cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 1111438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1113e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // Not part of BlockGuard.Policy; just part of StrictMode: 1114e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick void onCustomSlowCall(String name) { 1115e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick if ((mPolicyMask & DETECT_CUSTOM) == 0) { 1116e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return; 1117e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1118e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 1119e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return; 1120e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1121e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeCustomViolation(mPolicyMask, name); 1122e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick e.fillInStackTrace(); 1123e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick startHandlingViolationException(e); 1124e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1125e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick 1126438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 1127438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onReadFromDisk() { 112832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_READ) == 0) { 1129438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 1130438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1131191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 1132191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 1133191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1134cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask); 1135cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 1136cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 1137438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1138438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1139438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 1140438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onNetwork() { 114132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_NETWORK) == 0) { 1142438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 1143438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1144b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) { 1145b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick throw new NetworkOnMainThreadException(); 1146b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 1147191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 1148191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 1149191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 1150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask); 1151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 1152cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 1153438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1154438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1155438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void setPolicyMask(int policyMask) { 1156438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 1157438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1158438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 11595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Start handling a violation that just started and hasn't 11605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // actually run yet (e.g. no disk write or network operation 11615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // has yet occurred). This sees if we're in an event loop 11625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread and, if so, uses it to roughly measure how long the 11635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation took. 11645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) { 1165cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ViolationInfo info = new ViolationInfo(e, e.getPolicy()); 1166cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.violationUptimeMillis = SystemClock.uptimeMillis(); 1167cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolationWithTimingAttempt(info); 1168cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1169438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1170cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Attempts to fill in the provided ViolationInfo's 1171cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // durationMillis field if this thread has a Looper we can use 1172cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // to measure with. We measure from the time of violation 1173cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // until the time the looper is idle again (right before 1174cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // the next epoll_wait) 1175cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolationWithTimingAttempt(final ViolationInfo info) { 1176438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Looper looper = Looper.myLooper(); 1177cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1178cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Without a Looper, we're unable to time how long the 1179cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // violation takes place. This case should be rare, as 1180cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // most users will care about timing violations that 1181cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // happen on their main UI thread. Note that this case is 1182cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // also hit when a violation takes place in a Binder 1183cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // thread, in "gather" mode. In this case, the duration 1184cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // of the violation is computed by the ultimate caller and 1185cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // its Looper, if any. 11864e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // 11874e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // Also, as a special short-cut case when the only penalty 11884e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // bit is death, we die immediately, rather than timing 11894e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // the violation's duration. This makes it convenient to 11904e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // use in unit tests too, rather than waiting on a Looper. 11914e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // 1192cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // TODO: if in gather mode, ignore Looper.myLooper() and always 1193cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // go into this immediate mode? 11944e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick if (looper == null || 1195c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick (info.policy & THREAD_PENALTY_MASK) == PENALTY_DEATH) { 1196cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis = -1; // unknown (redundant, already set) 1197cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(info); 1198cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 1199438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1200438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1201cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ArrayList<ViolationInfo> records = violationsBeingTimed.get(); 1202191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (records.size() >= MAX_OFFENSES_PER_LOOP) { 1203cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Not worth measuring. Too many offenses in one loop. 1204cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 1205cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1206cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.add(info); 1207cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (records.size() > 1) { 1208cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // There's already been a violation this loop, so we've already 1209cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // registered an idle handler to process the list of violations 1210cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // at the end of this Looper's loop. 1211cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 1212cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1213cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 12146804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ? 1215cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick sWindowManager.get() : null; 12166804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick if (windowManager != null) { 12176804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick try { 12186804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick windowManager.showStrictModeViolation(true); 12196804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } catch (RemoteException unused) { 12206804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 12216804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 12226804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 1223bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // We post a runnable to a Handler (== delay 0 ms) for 1224bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // measuring the end time of a violation instead of using 1225bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // an IdleHandler (as was previously used) because an 1226bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // IdleHandler may not run for quite a long period of time 1227bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // if an ongoing animation is happening and continually 1228bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // posting ASAP (0 ms) animation steps. Animations are 1229bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // throttled back to 60fps via SurfaceFlinger/View 1230bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // invalidates, _not_ by posting frame updates every 16 1231bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // milliseconds. 12323761f33387f980d7a6c6e06555a5a932ba7247a2Jeff Sharkey threadHandler.get().postAtFrontOfQueue(new Runnable() { 1233bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick public void run() { 1234cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick long loopFinishTime = SystemClock.uptimeMillis(); 1235bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 1236bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // Note: we do this early, before handling the 1237bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // violation below, as handling the violation 1238bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // may include PENALTY_DEATH and we don't want 1239bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // to keep the red border on. 1240bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick if (windowManager != null) { 1241bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick try { 1242bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick windowManager.showStrictModeViolation(false); 1243bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } catch (RemoteException unused) { 1244bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 1245bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 1246bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 1247cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (int n = 0; n < records.size(); ++n) { 1248cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo v = records.get(n); 1249cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.violationNumThisLoop = n + 1; 1250cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.durationMillis = 1251cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick (int) (loopFinishTime - v.violationUptimeMillis); 1252cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(v); 1253cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1254cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.clear(); 1255cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1256cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick }); 12575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1258438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 12595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Note: It's possible (even quite likely) that the 12605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread-local policy mask has changed from the time the 12615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation fired and now (after the violating code ran) due 12625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // to people who push/pop temporary policy in regions of code, 12635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // hence the policy being passed around. 1264cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolation(final ViolationInfo info) { 1265cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) { 1266cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.wtf(TAG, "unexpected null stacktrace"); 12675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 12685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1269438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1270cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy); 127146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1272cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_GATHER) != 0) { 1273cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1274703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1275cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations = new ArrayList<ViolationInfo>(1); 1276703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(violations); 1277703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else if (violations.size() >= 5) { 12785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Too many. In a loop or something? Don't gather them all. 12795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 12805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1281cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (ViolationInfo previous : violations) { 1282cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) { 12835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Duplicate. Don't log. 12845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 12855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1287cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations.add(info); 1288727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick return; 1289727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1290727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 129146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Not perfect, but fast and good enough for dup suppression. 1292f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick Integer crashFingerprint = info.hashCode(); 129346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long lastViolationTime = 0; 1294390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn if (mLastViolationTime != null) { 1295390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn Long vtime = mLastViolationTime.get(crashFingerprint); 1296390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn if (vtime != null) { 1297390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn lastViolationTime = vtime; 1298390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn } 1299390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn } else { 1300390517be2d60dd6e6264150c190c372d89bb331aDianne Hackborn mLastViolationTime = new ArrayMap<Integer, Long>(1); 130146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 130246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long now = SystemClock.uptimeMillis(); 130346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick mLastViolationTime.put(crashFingerprint, now); 130446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long timeSinceLastViolationMillis = lastViolationTime == 0 ? 130546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Long.MAX_VALUE : (now - lastViolationTime); 130646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1307cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_LOG) != 0 && 130846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1309cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.durationMillis != -1) { 13105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation; ~duration=" + 1311cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis + " ms: " + info.crashInfo.stackTrace); 1312438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 1313cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace); 1314438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1315438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1316438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 131771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // The violationMaskSubset, passed to ActivityManager, is a 131846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // subset of the original StrictMode policy bitmask, with 131946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // only the bit violated and penalty bits to be executed 132046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // by the ActivityManagerService remaining set. 1321cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationMaskSubset = 0; 132246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1323cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DIALOG) != 0 && 132446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) { 1325cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DIALOG; 132646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 132746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1328cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) { 1329cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DROPBOX; 133046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 133146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1332cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationMaskSubset != 0) { 1333cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage); 1334cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= violationBit; 133532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 133671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 1337c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick final boolean justDropBox = (info.policy & THREAD_PENALTY_MASK) == PENALTY_DROPBOX; 133871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick if (justDropBox) { 133971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // If all we're going to ask the activity manager 134071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // to do is dropbox it (the common case during 134171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // platform development), we can avoid doing this 134271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // call synchronously which Binder data suggests 134371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // isn't always super fast, despite the implementation 134471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // in the ActivityManager trying to be mostly async. 1345bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 134671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick return; 134771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } 134871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 134971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // Normal synchronous call to the ActivityManager. 1350438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick try { 1351727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 1352727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 1353727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 1354727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // We restore the current policy below, in the finally block. 135532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 1356727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1357438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 1358438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick RuntimeInit.getApplicationObject(), 1359cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset, 1360cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info); 1361438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } catch (RemoteException e) { 136246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 1363727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } finally { 1364727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Restore the policy. 136532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 1366438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1367438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1368438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1369cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DEATH) != 0) { 13704e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick executeDeathPenalty(info); 1371438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1372438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1373438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1374727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 13754e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick private static void executeDeathPenalty(ViolationInfo info) { 13764e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage); 13774e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick throw new StrictModeViolation(info.policy, violationBit, null); 13784e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 13794e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick 1380bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 1381bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * In the common case, as set by conditionallyEnableDebugLogging, 1382bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * we're just dropboxing any violations but not showing a dialog, 1383bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * not loggging, and not killing the process. In these cases we 1384bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * don't need to do a synchronous call to the ActivityManager. 1385bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * This is used by both per-thread and vm-wide violations when 1386bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * applicable. 1387bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 1388bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static void dropboxViolationAsync( 1389bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick final int violationMaskSubset, final ViolationInfo info) { 1390bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.incrementAndGet(); 1391bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (outstanding > 20) { 1392bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // What's going on? Let's not make make the situation 1393bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // worse and just not log. 1394bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick sDropboxCallsInFlight.decrementAndGet(); 1395bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1396bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1397bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1398bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding); 1399bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1400bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick new Thread("callActivityManagerForStrictModeDropbox") { 1401bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick public void run() { 1402bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 1403bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick try { 14041065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick IActivityManager am = ActivityManagerNative.getDefault(); 14051065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick if (am == null) { 14061065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick Log.d(TAG, "No activity manager; failed to Dropbox violation."); 14071065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick } else { 14081065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick am.handleApplicationStrictModeViolation( 14091065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick RuntimeInit.getApplicationObject(), 14101065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick violationMaskSubset, 14111065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick info); 14121065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick } 1413bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } catch (RemoteException e) { 1414bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Log.e(TAG, "RemoteException handling StrictMode violation", e); 1415bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1416bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.decrementAndGet(); 1417bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding); 1418bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1419bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick }.start(); 1420bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1421bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 14224b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static class AndroidCloseGuardReporter implements CloseGuard.Reporter { 14234b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public void report (String message, Throwable allocationSite) { 14244b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, allocationSite); 14254b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 14264b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 14274b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 1428727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 14295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 14305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 14315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static boolean hasGatheredViolations() { 1432703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return gatheredViolations.get() != null; 1433703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1434703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick 1435703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /** 1436703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Called from Parcel.writeException(), so we drop this memory and 1437703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * don't incorrectly attribute it to the wrong caller on the next 1438703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Binder call on this thread. 1439703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick */ 1440703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /* package */ static void clearGatheredViolations() { 1441703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 14425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 14435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 14445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 1445bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * @hide 1446bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick */ 1447bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public static void conditionallyCheckInstanceCounts() { 1448bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick VmPolicy policy = getVmPolicy(); 1449bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (policy.classInstanceLimit.size() == 0) { 1450bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick return; 1451bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 14526f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey 14536f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey System.gc(); 14546f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey System.runFinalization(); 14556f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey System.gc(); 14566f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey 1457bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // Note: classInstanceLimit is immutable, so this is lock-free 14585f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick for (Map.Entry<Class, Integer> entry : policy.classInstanceLimit.entrySet()) { 14595f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Class klass = entry.getKey(); 14605f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick int limit = entry.getValue(); 1461bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick long instances = VMDebug.countInstancesOfClass(klass, false); 1462bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (instances <= limit) { 1463bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick continue; 1464bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1465bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick Throwable tr = new InstanceCountViolation(klass, instances, limit); 1466bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick onVmPolicyViolation(tr.getMessage(), tr); 1467bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1468bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1469bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 1470bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static long sLastInstanceCountCheckMillis = 0; 14715f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick private static boolean sIsIdlerRegistered = false; // guarded by StrictMode.class 1472bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static final MessageQueue.IdleHandler sProcessIdleHandler = 1473bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick new MessageQueue.IdleHandler() { 1474bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public boolean queueIdle() { 1475bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick long now = SystemClock.uptimeMillis(); 1476bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (now - sLastInstanceCountCheckMillis > 30 * 1000) { 1477bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick sLastInstanceCountCheckMillis = now; 1478bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick conditionallyCheckInstanceCounts(); 1479bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1480bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick return true; 1481bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1482bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick }; 1483bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 1484bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick /** 148532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions in the VM process (on any 148632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * thread) should be detected, as well as the penalty if such 148732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * actions occur. 148832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 148932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 149032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 149132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setVmPolicy(final VmPolicy policy) { 14925f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick synchronized (StrictMode.class) { 14935f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sVmPolicy = policy; 14945f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sVmPolicyMask = policy.mask; 14955f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 14965f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 14975f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Looper looper = Looper.getMainLooper(); 14985f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick if (looper != null) { 14995f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick MessageQueue mq = looper.mQueue; 1500c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick if (policy.classInstanceLimit.size() == 0 || 1501c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick (sVmPolicyMask & VM_PENALTY_MASK) == 0) { 1502bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mq.removeIdleHandler(sProcessIdleHandler); 1503c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick sIsIdlerRegistered = false; 1504bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } else if (!sIsIdlerRegistered) { 1505bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mq.addIdleHandler(sProcessIdleHandler); 1506bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick sIsIdlerRegistered = true; 1507bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1508bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1509bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 151032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 151132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 151232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 151332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Gets the current VM policy. 151432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 151532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static VmPolicy getVmPolicy() { 15165f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick synchronized (StrictMode.class) { 15175f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick return sVmPolicy; 15185f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 151932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 152032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 152132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 152262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * Enable the recommended StrictMode defaults, with violations just being logged. 152362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * 152462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * <p>This catches disk and network access on the main thread, as 1525fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * well as leaked SQLite cursors and unclosed resources. This is 1526fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * simply a wrapper around {@link #setVmPolicy} and {@link 1527fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * #setThreadPolicy}. 152862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick */ 152962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick public static void enableDefaults() { 153062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 153162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectAll() 153262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 153362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 153462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 1535e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick .detectAll() 153662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 153762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 153862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick } 153962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick 154062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick /** 154132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 154232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 154332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static boolean vmSqliteObjectLeaksEnabled() { 154432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0; 154532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 154632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 154732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 154832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 154932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 1550fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static boolean vmClosableObjectLeaksEnabled() { 1551fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0; 1552fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 1553fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 1554fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 1555fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 1556fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 1557d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public static boolean vmRegistrationLeaksEnabled() { 1558d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey return (sVmPolicyMask & DETECT_VM_REGISTRATION_LEAKS) != 0; 1559d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey } 1560d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 1561d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 1562d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * @hide 1563d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 1564a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey public static boolean vmFileUriExposureEnabled() { 1565a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey return (sVmPolicyMask & DETECT_VM_FILE_URI_EXPOSURE) != 0; 1566a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey } 1567a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey 1568a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey /** 1569a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * @hide 1570a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey */ 157132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void onSqliteObjectLeaked(String message, Throwable originStack) { 15724b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, originStack); 15734b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 15744b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 157508d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block /** 157608d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block * @hide 157708d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block */ 157808d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) { 157908d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block onVmPolicyViolation(null, originStack); 158008d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block } 158108d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block 1582d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 1583d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * @hide 1584d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 1585d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public static void onIntentReceiverLeaked(Throwable originStack) { 1586d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey onVmPolicyViolation(null, originStack); 1587d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey } 1588d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 1589d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey /** 1590d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey * @hide 1591d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey */ 1592d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey public static void onServiceConnectionLeaked(Throwable originStack) { 1593d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey onVmPolicyViolation(null, originStack); 1594d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey } 1595d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey 1596a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey /** 1597a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey * @hide 1598a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey */ 1599a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey public static void onFileUriExposed(String location) { 1600a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey final String message = "file:// Uri exposed through " + location; 1601a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey onVmPolicyViolation(message, new Throwable(message)); 1602a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey } 1603a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey 1604bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // Map from VM violation fingerprint to uptime millis. 1605bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<Integer, Long>(); 1606bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 16074b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom /** 16084b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom * @hide 16094b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom */ 16104b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public static void onVmPolicyViolation(String message, Throwable originStack) { 1611bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0; 1612bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0; 1613bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final boolean penaltyLog = (sVmPolicyMask & PENALTY_LOG) != 0; 1614bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask); 1615bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 16165f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick // Erase stuff not relevant for process-wide violations 16175f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick info.numAnimationsRunning = 0; 16185f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick info.tags = null; 16195f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick info.broadcastIntentAction = null; 16205f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 1621bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final Integer fingerprint = info.hashCode(); 1622bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final long now = SystemClock.uptimeMillis(); 1623bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick long lastViolationTime = 0; 1624bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick long timeSinceLastViolationMillis = Long.MAX_VALUE; 1625bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick synchronized (sLastVmViolationTime) { 1626bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (sLastVmViolationTime.containsKey(fingerprint)) { 1627bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick lastViolationTime = sLastVmViolationTime.get(fingerprint); 1628bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick timeSinceLastViolationMillis = now - lastViolationTime; 1629bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1630bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1631bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick sLastVmViolationTime.put(fingerprint, now); 1632bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 163332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 163432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1635bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (penaltyLog && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1636bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick Log.e(TAG, message, originStack); 1637bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 1638bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 1639bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick int violationMaskSubset = PENALTY_DROPBOX | (ALL_VM_DETECT_BITS & sVmPolicyMask); 1640bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1641bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDropbox && !penaltyDeath) { 1642bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // Common case for userdebug/eng builds. If no death and 1643bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // just dropboxing, we can do the ActivityManager call 1644bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // asynchronously. 1645bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 1646bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1647bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 164832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1649bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (penaltyDropbox && lastViolationTime == 0) { 165032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // The violationMask, passed to ActivityManager, is a 165132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // subset of the original StrictMode policy bitmask, with 165232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // only the bit violated and penalty bits to be executed 165332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // by the ActivityManagerService remaining set. 165432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 165532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick try { 165632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 165732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 165832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 165932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // We restore the current policy below, in the finally block. 166032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 166132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 166232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 166332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick RuntimeInit.getApplicationObject(), 166432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick violationMaskSubset, 166532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick info); 166632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } catch (RemoteException e) { 166732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 166832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } finally { 166932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Restore the policy. 167032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 167132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 167232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 167332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1674bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDeath) { 167532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down."); 167632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Process.killProcess(Process.myPid()); 167732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.exit(10); 167832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 167932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 168032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 168132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 16825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 16835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 16845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void writeGatheredViolationsToParcel(Parcel p) { 1685cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1686703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1687703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(0); 1688703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else { 1689703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(violations.size()); 1690703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick for (int i = 0; i < violations.size(); ++i) { 1691703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.get(i).writeToParcel(p, 0 /* unused flags? */); 1692703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1693703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size()); 1694703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.clear(); // somewhat redundant, as we're about to null the threadlocal 16955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1696703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 16975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 16985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 16995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class LogStackTrace extends Exception {} 17005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 17015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 17025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS, 17035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * we here read back all the encoded violations. 17045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 17055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void readAndHandleBinderCallViolations(Parcel p) { 17065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Our own stack trace to append 17075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick StringWriter sw = new StringWriter(); 17088c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn PrintWriter pw = new FastPrintWriter(sw, false, 256); 17098c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn new LogStackTrace().printStackTrace(pw); 17108c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn pw.flush(); 17115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String ourStack = sw.toString(); 17125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 171332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int policyMask = getThreadPolicyMask(); 1714cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0; 17155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 17165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int numViolations = p.readInt(); 17175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick for (int i = 0; i < numViolations; ++i) { 17185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call. i=" + i); 1719cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo info = new ViolationInfo(p, !currentlyGathering); 1720cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack; 17215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 17225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (policy instanceof AndroidBlockGuardPolicy) { 1723cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info); 17245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 17255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 17265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 17275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 17285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 1729727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Called from android_util_Binder.cpp's 1730727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * android_os_Parcel_enforceInterface when an incoming Binder call 1731727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * requires changing the StrictMode policy mask. The role of this 1732727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * function is to ask Binder for its current (native) thread-local 1733727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * policy value and synchronize it to libcore's (Java) 1734727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * thread-local policy value. 1735727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 1736727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void onBinderStrictModePolicyChange(int newPolicy) { 1737727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(newPolicy); 1738727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1739cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1740cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1741e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * A tracked, critical time span. (e.g. during an animation.) 1742e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1743e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * The object itself is a linked list node, to avoid any allocations 1744e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * during rapid span entries and exits. 1745e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1746e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1747e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1748e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static class Span { 1749e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private String mName; 1750e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private long mCreateMillis; 1751e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mNext; 1752e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mPrev; // not used when in freeList, only active 1753e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private final ThreadSpanState mContainerState; 1754e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1755e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span(ThreadSpanState threadState) { 1756e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mContainerState = threadState; 1757e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1758e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 17591181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Empty constructor for the NO_OP_SPAN 17601181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick protected Span() { 17611181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick mContainerState = null; 17621181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 17631181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1764e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1765e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * To be called when the critical span is complete (i.e. the 1766e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * animation is done animating). This can be called on any 1767e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread (even a different one from where the animation was 1768e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * taking place), but that's only a defensive implementation 1769e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * measure. It really makes no sense for you to call this on 1770e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread other than that where you created it. 1771e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1772e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1773e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1774e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public void finish() { 1775e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = mContainerState; 1776e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1777e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mName == null) { 1778e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Duplicate finish call. Ignore. 1779e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return; 1780e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1781e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1782e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Remove ourselves from the active list. 1783e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mPrev != null) { 1784e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mPrev.mNext = mNext; 1785e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1786e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mNext != null) { 1787e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mNext.mPrev = mPrev; 1788e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1789e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mActiveHead == this) { 1790e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = mNext; 1791e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1792e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 17931cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick state.mActiveSize--; 17941cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 17951cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize); 17961cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 1797e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mCreateMillis = -1; 1798e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mName = null; 1799e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mPrev = null; 1800e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = null; 1801e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1802e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Add ourselves to the freeList, if it's not already 1803e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // too big. 1804e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListSize < 5) { 1805e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = state.mFreeListHead; 1806e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = this; 1807e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize++; 1808e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1809e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1810e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1811e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1812e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 18131181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // The no-op span that's used in user builds. 18141181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final Span NO_OP_SPAN = new Span() { 18151181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick public void finish() { 18161181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Do nothing. 18171181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 18181181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick }; 18191181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1820e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1821e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Linked lists of active spans and a freelist. 1822e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1823e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Locking notes: there's one of these structures per thread and 1824e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * all members of this structure (as well as the Span nodes under 1825e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * it) are guarded by the ThreadSpanState object instance. While 1826e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * in theory there'd be no locking required because it's all local 1827e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * per-thread, the finish() method above is defensive against 1828e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * people calling it on a different thread from where they created 1829e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * the Span, hence the locking. 1830e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1831e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static class ThreadSpanState { 1832e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mActiveHead; // doubly-linked list. 1833e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mActiveSize; 1834e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mFreeListHead; // singly-linked list. only changes at head. 1835e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mFreeListSize; 1836e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1837e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1838e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState = 1839e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick new ThreadLocal<ThreadSpanState>() { 1840e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick @Override protected ThreadSpanState initialValue() { 1841e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return new ThreadSpanState(); 1842e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1843e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick }; 1844e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1845cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() { 1846cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick protected IWindowManager create() { 1847cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick return IWindowManager.Stub.asInterface(ServiceManager.getService("window")); 1848cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick } 1849cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick }; 1850cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick 1851e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1852e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Enter a named critical span (e.g. an animation) 1853e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1854e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>The name is an arbitary label (or tag) that will be applied 1855e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * to any strictmode violation that happens while this span is 1856e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * active. You must call finish() on the span when done. 1857e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1858e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>This will never return null, but on devices without debugging 1859e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * enabled, this may return a dummy object on which the finish() 1860e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * method is a no-op. 1861e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1862e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>TODO: add CloseGuard to this, verifying callers call finish. 1863e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1864e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1865e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1866e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static Span enterCriticalSpan(String name) { 18671181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick if (IS_USER_BUILD) { 18681181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick return NO_OP_SPAN; 18691181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 1870e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (name == null || name.isEmpty()) { 1871e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick throw new IllegalArgumentException("name must be non-null and non-empty"); 1872e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1873e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 1874e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span span = null; 1875e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1876e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListHead != null) { 1877e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = state.mFreeListHead; 1878e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = span.mNext; 1879e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize--; 1880e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } else { 1881e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Shouldn't have to do this often. 1882e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = new Span(state); 1883e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1884e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mName = name; 1885e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mCreateMillis = SystemClock.uptimeMillis(); 1886e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext = state.mActiveHead; 1887e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mPrev = null; 1888e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = span; 1889e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveSize++; 1890e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (span.mNext != null) { 1891e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext.mPrev = span; 1892e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 18931cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize); 1894e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1895e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return span; 1896e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1897e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1898e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick /** 1899e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * For code to note that it's slow. This is a no-op unless the 1900e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * current thread's {@link android.os.StrictMode.ThreadPolicy} has 1901e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls} 1902e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * enabled. 1903e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * 1904e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * @param name a short string for the exception stack trace that's 1905e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick * built if when this fires. 1906e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick */ 1907e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick public static void noteSlowCall(String name) { 1908e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 1909e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 1910e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick // StrictMode not enabled. 1911e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick return; 1912e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1913e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name); 1914e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick } 1915e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1916e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 19174e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 19184e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 19194e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public static void noteDiskRead() { 19204e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 19214e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 19224e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // StrictMode not enabled. 19234e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick return; 19244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 19254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).onReadFromDisk(); 19264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 19274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick 19284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 19294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick * @hide 19304e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick */ 19314e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick public static void noteDiskWrite() { 19324e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 19334e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 19344e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick // StrictMode not enabled. 19354e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick return; 19364e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 19374e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).onWriteToDisk(); 19384e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick } 19394e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick 19405f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick // Guarded by StrictMode.class 19415f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick private static final HashMap<Class, Integer> sExpectedActivityInstanceCount = 19425f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick new HashMap<Class, Integer>(); 19435f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 19444e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick /** 19457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown * Returns an object that is used to track instances of activites. 19467e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown * The activity should store a reference to the tracker object in one of its fields. 19477e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown * @hide 19487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown */ 19497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown public static Object trackActivity(Object instance) { 19507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return new InstanceTracker(instance); 19517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 19527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown /** 1954758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick * @hide 1955758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick */ 19565f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick public static void incrementExpectedActivityCount(Class klass) { 19577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (klass == null) { 1958758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return; 1959758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 19607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19615f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick synchronized (StrictMode.class) { 19627e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) { 19637e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return; 19647e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 19657e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19665f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Integer expected = sExpectedActivityInstanceCount.get(klass); 19675f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Integer newExpected = expected == null ? 1 : expected + 1; 19685f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sExpectedActivityInstanceCount.put(klass, newExpected); 19695f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 19705f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 19715f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 19725f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick /** 19735f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick * @hide 19745f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick */ 19755f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick public static void decrementExpectedActivityCount(Class klass) { 19767e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (klass == null) { 1977758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick return; 1978758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 19797e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19807e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final int limit; 19815f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick synchronized (StrictMode.class) { 19827e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) { 19837e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return; 19847e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 19857e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19865f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick Integer expected = sExpectedActivityInstanceCount.get(klass); 19877e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown int newExpected = (expected == null || expected == 0) ? 0 : expected - 1; 19885f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick if (newExpected == 0) { 19895f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sExpectedActivityInstanceCount.remove(klass); 19905f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } else { 19915f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick sExpectedActivityInstanceCount.put(klass, newExpected); 19925f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 19937e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 19945f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick // Note: adding 1 here to give some breathing room during 19955f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick // orientation changes. (shouldn't be necessary, though?) 19967e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown limit = newExpected + 1; 19975f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 19985f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick 19997e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // Quick check. 20007e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown int actual = InstanceTracker.getInstanceCount(klass); 20017e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (actual <= limit) { 20027e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return; 20037e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 20047e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 20057e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // Do a GC and explicit count to double-check. 20067e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // This is the work that we are trying to avoid by tracking the object instances 20077e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // explicity. Running an explicit GC can be expensive (80ms) and so can walking 20087e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // the heap to count instance (30ms). This extra work can make the system feel 20097e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // noticeably less responsive during orientation changes when activities are 20107e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // being restarted. Granted, it is only a problem when StrictMode is enabled 20117e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown // but it is annoying. 20126f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey 20136f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey System.gc(); 20146f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey System.runFinalization(); 20156f3a38f3afd79ed6dddcef5c83cb442d6749e2ffJeff Sharkey System.gc(); 20167e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 20177e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown long instances = VMDebug.countInstancesOfClass(klass, false); 20187e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (instances > limit) { 20197e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown Throwable tr = new InstanceCountViolation(klass, instances, limit); 20207e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown onVmPolicyViolation(tr.getMessage(), tr); 20215f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick } 2022758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick } 2023758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick 2024758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick /** 2025cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Parcelable that gets sent in Binder call headers back to callers 2026cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * to report violations that happened during a cross-process call. 2027cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 2028cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @hide 2029cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2030cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public static class ViolationInfo { 2031cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2032cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Stack and other stuff info. 2033cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2034cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final ApplicationErrorReport.CrashInfo crashInfo; 2035cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2036cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2037cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The strict mode policy mask at the time of violation. 2038cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2039cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final int policy; 2040cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2041cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2042cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The wall time duration of the violation, when known. -1 when 2043cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * not known. 2044cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2045cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int durationMillis = -1; 2046cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2047cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2048599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * The number of animations currently running. 2049599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 2050599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public int numAnimationsRunning = 0; 2051599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 2052599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 2053e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * List of tags from active Span instances during this 2054e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * violation, or null for none. 2055e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 2056e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public String[] tags; 2057e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 2058e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 2059cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Which violation number this was (1-based) since the last Looper loop, 2060cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * from the perspective of the root caller (if it crossed any processes 2061cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * via Binder calls). The value is 0 if the root caller wasn't on a Looper 2062cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * thread. 2063cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2064cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int violationNumThisLoop; 2065cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2066cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2067cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The time (in terms of SystemClock.uptimeMillis()) that the 2068cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * violation occurred. 2069cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2070cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public long violationUptimeMillis; 2071cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2072cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2073bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * The action of the Intent being broadcast to somebody's onReceive 2074bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * on this thread right now, or null. 2075bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick */ 2076bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick public String broadcastIntentAction; 2077bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick 2078bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick /** 2079bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * If this is a instance count violation, the number of instances in memory, 2080bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick * else -1. 2081bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick */ 2082bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public long numInstances = -1; 2083bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 2084bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick /** 2085cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an uninitialized instance of ViolationInfo 2086cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2087cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo() { 2088cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = null; 2089cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = 0; 2090cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2091cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2092cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2093cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from an exception. 2094cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2095cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Throwable tr, int policy) { 2096cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(tr); 2097cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = SystemClock.uptimeMillis(); 2098cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this.policy = policy; 2099599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount(); 2100bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast(); 2101bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntent != null) { 2102bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = broadcastIntent.getAction(); 2103bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 2104e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 2105bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (tr instanceof InstanceCountViolation) { 2106bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick this.numInstances = ((InstanceCountViolation) tr).mInstances; 2107bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 2108e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 2109e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int spanActiveCount = state.mActiveSize; 2110e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount > MAX_SPAN_TAGS) { 2111e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick spanActiveCount = MAX_SPAN_TAGS; 2112e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2113e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount != 0) { 2114e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags = new String[spanActiveCount]; 2115e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span iter = state.mActiveHead; 2116e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 2117e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick while (iter != null && index < spanActiveCount) { 2118e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags[index] = iter.mName; 2119e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick index++; 2120e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick iter = iter.mNext; 2121e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2122e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2123e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2124cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2125cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2126f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick @Override 2127f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick public int hashCode() { 2128f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick int result = 17; 2129f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + crashInfo.stackTrace.hashCode(); 2130f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (numAnimationsRunning != 0) { 2131f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result *= 37; 2132f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2133f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (broadcastIntentAction != null) { 2134f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + broadcastIntentAction.hashCode(); 2135f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2136f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (tags != null) { 2137f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick for (String tag : tags) { 2138f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + tag.hashCode(); 2139f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2140f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2141f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick return result; 2142f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 2143f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick 2144cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2145cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 2146cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2147cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in) { 2148cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this(in, false); 2149cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2152cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 2153cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 2154cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @param unsetGatheringBit if true, the caller is the root caller 2155cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * and the gathering penalty should be removed. 2156cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2157cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in, boolean unsetGatheringBit) { 2158cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(in); 2159cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int rawPolicy = in.readInt(); 2160cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (unsetGatheringBit) { 2161cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy & ~PENALTY_GATHER; 2162cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } else { 2163cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy; 2164cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2165cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick durationMillis = in.readInt(); 2166cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationNumThisLoop = in.readInt(); 2167599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick numAnimationsRunning = in.readInt(); 2168cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = in.readLong(); 2169bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick numInstances = in.readLong(); 2170bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = in.readString(); 2171e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick tags = in.readStringArray(); 2172cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2173cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2174cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2175cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Save a ViolationInfo instance to a parcel. 2176cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2177cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void writeToParcel(Parcel dest, int flags) { 2178cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.writeToParcel(dest, flags); 2179cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(policy); 2180cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(durationMillis); 2181cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(violationNumThisLoop); 2182599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick dest.writeInt(numAnimationsRunning); 2183cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeLong(violationUptimeMillis); 2184bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick dest.writeLong(numInstances); 2185bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick dest.writeString(broadcastIntentAction); 2186e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick dest.writeStringArray(tags); 2187cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2188cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2189cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2190cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 2191cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Dump a ViolationInfo instance to a Printer. 2192cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 2193cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void dump(Printer pw, String prefix) { 2194cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.dump(pw, prefix); 2195cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "policy: " + policy); 2196cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (durationMillis != -1) { 2197cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "durationMillis: " + durationMillis); 2198cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2199bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick if (numInstances != -1) { 2200bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick pw.println(prefix + "numInstances: " + numInstances); 2201bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 2202cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationNumThisLoop != 0) { 2203cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop); 2204cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2205599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick if (numAnimationsRunning != 0) { 2206599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning); 2207599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick } 2208cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis); 2209bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntentAction != null) { 2210bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction); 2211bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 2212e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (tags != null) { 2213e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 2214e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick for (String tag : tags) { 2215e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick pw.println(prefix + "tag[" + (index++) + "]: " + tag); 2216e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2217e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 2218cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2219cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 2220cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 2221bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 2222bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // Dummy throwable, for now, since we don't know when or where the 2223bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // leaked instances came from. We might in the future, but for 2224bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // now we suppress the stack trace because it's useless and/or 2225bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick // misleading. 2226bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick private static class InstanceCountViolation extends Throwable { 2227bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final Class mClass; 2228bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final long mInstances; 2229bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick final int mLimit; 2230bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 22315f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick private static final StackTraceElement[] FAKE_STACK = { 22325f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick new StackTraceElement("android.os.StrictMode", "setClassInstanceLimit", 22335f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick "StrictMode.java", 1) 22345f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick }; 2235bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick 2236bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick public InstanceCountViolation(Class klass, long instances, int limit) { 22375f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick super(klass.toString() + "; instances=" + instances + "; limit=" + limit); 2238bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick setStackTrace(FAKE_STACK); 2239bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mClass = klass; 2240bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mInstances = instances; 2241bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick mLimit = limit; 2242bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 2243bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick } 22447e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown private static final class InstanceTracker { 22467e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown private static final HashMap<Class<?>, Integer> sInstanceCounts = 22477e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown new HashMap<Class<?>, Integer>(); 22487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown private final Class<?> mKlass; 22507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown public InstanceTracker(Object instance) { 22527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown mKlass = instance.getClass(); 22537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown synchronized (sInstanceCounts) { 22557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final Integer value = sInstanceCounts.get(mKlass); 22567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final int newValue = value != null ? value + 1 : 1; 22577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown sInstanceCounts.put(mKlass, newValue); 22587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22617e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown @Override 22627e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown protected void finalize() throws Throwable { 22637e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown try { 22647e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown synchronized (sInstanceCounts) { 22657e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final Integer value = sInstanceCounts.get(mKlass); 22667e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (value != null) { 22677e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final int newValue = value - 1; 22687e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown if (newValue > 0) { 22697e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown sInstanceCounts.put(mKlass, newValue); 22707e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } else { 22717e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown sInstanceCounts.remove(mKlass); 22727e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22737e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22747e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22757e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } finally { 22767e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown super.finalize(); 22777e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22787e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22797e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown 22807e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown public static int getInstanceCount(Class<?> klass) { 22817e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown synchronized (sInstanceCounts) { 22827e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown final Integer value = sInstanceCounts.get(klass); 22837e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown return value != null ? value : 0; 22847e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22857e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 22867e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown } 2287438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick} 2288