StrictMode.java revision fd9ddd1a40efc801dc7512950cb9336967b6f775
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; 22bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.content.Intent; 23438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.util.Log; 24cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrickimport android.util.Printer; 25438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 26438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport com.android.internal.os.RuntimeInit; 27438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 28438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard; 29fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard; 30438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter; 325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter; 335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList; 3446d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap; 3546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 36438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/** 3732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be 3832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix 3932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them. 4015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 4115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or 4215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI 4315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place. Keeping disk 4415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother, 459fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications. By keeping your application's main thread 469fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent 479fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a> 489fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users. 4915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 5015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is 5115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that 5215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency. It's often the case that 5315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be 5415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background 5515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes. If possible, it's best to assume that such 5615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p> 5715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your 5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or 6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's 6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method: 6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre> 6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() { 6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * if (DEVELOPER_MODE) { 6632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}() 6732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskReads() 6832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskWrites() 6932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectNetwork() // or .detectAll() for all detectable problems 7032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 7132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 7232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}() 7362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * .detectLeakedSqlLiteObjects() 74fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * .detectLeakedClosableObjects() 7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyDeath() 7732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 7815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 7915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * super.onCreate(); 8015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 8115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre> 8215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 8332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected. 8432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can 8532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your 8632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen. 8715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 8815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are 8915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler}, 9015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc. 9115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds. In particular, 9232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle. Use 9332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident. Network requests on the UI thread 9415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though. 9515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not 9715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses. While it does 9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing 9915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best 10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism. Notably, disk or network access from JNI calls 10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it. Future versions of Android may catch 10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode 10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * enabled in shipping applications on the Android Market. 104438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 105438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode { 106438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static final String TAG = "StrictMode"; 1075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static final boolean LOG_V = false; 108438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 10946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only log a duplicate stack trace to the logs every second. 11046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_LOG_INTERVAL_MS = 1000; 11146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 11246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only show an annoying dialog at most every 30 seconds 11346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_DIALOG_INTERVAL_MS = 30000; 11446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 115191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // How many offending stacks to keep track of (and time) per loop 116191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // of the Looper. 117191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final int MAX_OFFENSES_PER_LOOP = 10; 118191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 11932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Thread-policy: 120438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 12115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 12232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 12315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 12432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_WRITE = 0x01; // for ThreadPolicy 12515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 12615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 12732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 12815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 12932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_READ = 0x02; // for ThreadPolicy 13015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 13115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 13232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 13315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 13432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_NETWORK = 0x04; // for ThreadPolicy 135438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 13632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Process-policy: 137438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 138438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 13932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Note, a "VM_" bit, not thread. 14032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 14132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 14232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_VM_CURSOR_LEAKS = 0x200; // for ProcessPolicy 14332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 14432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 145fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Note, a "VM_" bit, not thread. 146fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 147fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 148fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400; // for ProcessPolicy 149fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 150fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 15132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 152438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 153438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_LOG = 0x10; // normal android.util.Log 154438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 15532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Used for both process and thread policy: 15632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 157438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 15832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 159438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 160438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DIALOG = 0x20; 161438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 162438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 163b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death on any detected violation. 164b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 16532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 166438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 167438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DEATH = 0x40; 168438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 169438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 170b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death just for detected network usage. 171b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 172b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 173b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 174b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static final int PENALTY_DEATH_ON_NETWORK = 0x200; 175b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 176b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 17732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 178438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 179438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DROPBOX = 0x80; 180438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 181727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 182727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Non-public penalty mode which overrides all the other penalty 183727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * bits and signals that we're in a Binder call and we should 184727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * ignore the other penalty bits and instead serialize back all 185727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * our offending stack traces to the caller to ultimately handle 186727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * in the originating process. 187727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * 188703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * This must be kept in sync with the constant in libs/binder/Parcel.cpp 189703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * 190727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * @hide 191727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 192727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick public static final int PENALTY_GATHER = 0x100; 193727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 19432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 19571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick * Mask of all the penalty bits. 19671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick */ 19771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick private static final int PENALTY_MASK = 198b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER | 199b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick PENALTY_DEATH_ON_NETWORK; 20071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 20171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick /** 20232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The current VmPolicy in effect. 20332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 20432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static volatile int sVmPolicyMask = 0; 20532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 20632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private StrictMode() {} 20732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 20832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 20932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to a certain thread. 21032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 21132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setThreadPolicy}. The current policy 21232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can be retrieved with {@link #getThreadPolicy}. 21332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 21432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note that multiple penalties may be provided and they're run 21532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * in order from least to most severe (logging before process 21632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * death, for example). There's currently no mechanism to choose 21732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * different penalties for different detected actions. 21832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 21932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class ThreadPolicy { 22032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 22132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 22232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 22332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final ThreadPolicy LAX = new ThreadPolicy(0); 22432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 22532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 22632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 22732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private ThreadPolicy(int mask) { 22832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 22932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 23032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 23132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 23232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 23332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.ThreadPolicy; mask=" + mask + "]"; 23432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 23532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 23632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 23732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates ThreadPolicy instances. Methods whose names start 23832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 23932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 24032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 24132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 24232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 24332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 24432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 24532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 24632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 24732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 24832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 24932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 25032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 25132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 25232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 25332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 25432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 25532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 25632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask = 0; 25732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 25832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 25932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Create a Builder that detects nothing and has no 26032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations. (but note that {@link #build} will default 26132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to enabling {@link #penaltyLog} if no other penalties 26232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * are specified) 26332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 26432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder() { 26532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = 0; 26632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 26732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 26832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 26932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Initialize a Builder from an existing ThreadPolicy. 27032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 27132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder(ThreadPolicy policy) { 27232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = policy.mask; 27332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 27432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 27532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 27632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 27732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 27832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>As of the Gingerbread release this includes network and 27932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * disk operations but will likely expand in future releases. 28032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 28132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 28232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); 28332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 28432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 28532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable the detection of everything. 28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitAll() { 28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); 29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of network operations. 29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectNetwork() { 29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_NETWORK); 29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of network operations. 30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitNetwork() { 30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_NETWORK); 30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk reads. 30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskReads() { 31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_READ); 31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk reads. 31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskReads() { 31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_READ); 31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk writes. 32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskWrites() { 32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE); 32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk writes. 32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskWrites() { 33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE); 33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Show an annoying dialog to the developer on detected 33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations, rate-limited to be only a little annoying. 33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDialog() { 33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DIALOG); 34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crash the whole process on violation. This penalty runs at 34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so you'll still get 34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * see logging or other violations before the process dies. 346b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 347b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies 348b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * to disk reads, disk writes, and network usage if their 349b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * corresponding detect flags are set. 35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 356b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Crash the whole process on any network usage. Unlike 357b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * {@link #penaltyDeath}, this penalty runs 358b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <em>before</em> anything else. You must still have 359b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * called {@link #detectNetwork} to enable this. 360b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 361b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>In the Honeycomb or later SDKs, this is on by default. 362b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 363b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public Builder penaltyDeathOnNetwork() { 364b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick return enable(PENALTY_DEATH_ON_NETWORK); 365b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 366b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 367b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 37832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 37932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 38632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 38732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 38832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 38932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder disable(int bit) { 39032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask &= ~bit; 39132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 39232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 39332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 39432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 39532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the ThreadPolicy instance. 39632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 39732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 39832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 39932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 40032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 40132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public ThreadPolicy build() { 40232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 40332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 40432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(mMask); 41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to all threads in the virtual machine's process. 41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 41732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setVmPolicy}. 41832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 41932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class VmPolicy { 42032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 42132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 42232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 42332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final VmPolicy LAX = new VmPolicy(0); 42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private VmPolicy(int mask) { 42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.VmPolicy; mask=" + mask + "]"; 43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates {@link VmPolicy} instances. Methods whose names start 43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 45032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 45132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 45232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 45432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 45532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 45632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask; 45732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 45832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 45932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 46032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 461fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>In the Honeycomb release this includes leaks of 462fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * SQLite cursors and other closable objects but will 463fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * likely expand in future releases. 46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 46532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 466fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS); 46732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 46832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 47032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect when an 47132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link android.database.sqlite.SQLiteCursor} or other 47232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * SQLite object is finalized without having been closed. 47332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 47432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You always want to explicitly close your SQLite 47532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * cursors to avoid unnecessary database contention and 47632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * temporary memory leaks. 47732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectLeakedSqlLiteObjects() { 47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_VM_CURSOR_LEAKS); 48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 483fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Detect when an {@link java.io.Closeable} or other 484fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * object with a explict termination method is finalized 485fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * without having been closed. 486fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * 487fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>You always want to explicitly close such objects to 488fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * avoid unnecessary resources leaks. 489fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 490fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public Builder detectLeakedClosableObjects() { 491fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CLOSABLE_LEAKS); 492fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 493fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 494fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crashes the whole process on violation. This penalty runs at 49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so yo you'll still get 49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * your logging or other violations before the process dies. 49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 52032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 52132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 52232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 52332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 52432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 52532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 52632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the VmPolicy instance. 52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 52832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 52932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public VmPolicy build() { 53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new VmPolicy(mMask); 54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 544438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 545438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 5465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Log of strict mode violation stack traces that have occurred 5475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * during a Binder call, to be serialized back later to the caller 5485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * via Parcel.writeNoException() (amusingly) where the caller can 5495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * choose how to react. 5505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 551cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations = 552cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 553cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 554703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // Starts null to avoid unnecessary allocations when 555703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // checking whether there are any violations or not in 556703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // hasGatheredViolations() below. 557703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return null; 5585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 5595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick }; 5605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 5615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions on the current thread should 56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * be detected, as well as the penalty if such actions occur. 56415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Internally this sets a thread-local variable which is 56615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagated across cross-process IPC calls, meaning you can 56715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * catch violations when a system service or another process 56815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * accesses the disk or network on your behalf. 569438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 571438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setThreadPolicy(final ThreadPolicy policy) { 57332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(policy.mask); 57432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 57532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 57632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static void setThreadPolicyMask(final int policyMask) { 577727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // In addition to the Java-level thread-local in Dalvik's 578727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // BlockGuard, we also need to keep a native thread-local in 579727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Binder in order to propagate the value across Binder calls, 580727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // even across native-only processes. The two are kept in 581727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // sync via the callback to onStrictModePolicyChange, below. 582727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(policyMask); 583727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 584727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // And set the Android native version... 585727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick Binder.setThreadStrictModePolicy(policyMask); 586727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 587727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 588727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Sets the policy in Dalvik/libcore (BlockGuard) 589727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void setBlockGuardPolicy(final int policyMask) { 59046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (policyMask == 0) { 59146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY); 59246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick return; 59346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 594438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 595438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 596438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask)); 597438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 598438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy; 599438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick androidPolicy.setPolicyMask(policyMask); 600438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 601438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 602438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 6035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeNetworkViolation extends BlockGuard.BlockGuardPolicyException { 6045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeNetworkViolation(int policyMask) { 60532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_NETWORK); 6065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 6095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeDiskReadViolation extends BlockGuard.BlockGuardPolicyException { 6105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskReadViolation(int policyMask) { 61132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_DISK_READ); 6125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 6155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeDiskWriteViolation extends BlockGuard.BlockGuardPolicyException { 6165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskWriteViolation(int policyMask) { 61732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_DISK_WRITE); 6185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 621438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 62215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * Returns the bitmask of the current thread's policy. 623438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 62432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled 62532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 62632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 627438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 62832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static int getThreadPolicyMask() { 629438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return BlockGuard.getThreadPolicy().getPolicyMask(); 630438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 631438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 6325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 63332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Returns the current thread's policy. 63432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 63532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy getThreadPolicy() { 63632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(getThreadPolicyMask()); 63732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 63832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 63932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 64032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 64132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 64232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit both disk reads & writes, and sets the new policy 64332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 64432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 64597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 64632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to {@link #setThreadPolicy} to 64732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * restore the policy at the end of a block 64897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 64932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskWrites() { 65032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 65132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ); 65232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 65332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 65497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 65532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 65697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 65797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 65897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 65932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 66032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 66132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit disk reads, and sets the new policy 66232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 66332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 66497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 66532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to setThreadPolicy to 66697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * restore the policy. 66797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 66832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskReads() { 66932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 67032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ); 67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 67232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 67397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 67432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 67597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 67697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 67797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 67850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * Enable DropBox logging for debug phone builds. 67950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * 68050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * @hide 68150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick */ 68250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick public static boolean conditionallyEnableDebugLogging() { 68350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // For debug builds, log event loop stalls to dropbox for analysis. 68450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // Similar logic also appears in ActivityThread.java for system apps. 68550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick if ("user".equals(Build.TYPE)) { 68650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return false; 68750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.setThreadPolicyMask( 68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_DISK_WRITE | 69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_DISK_READ | 69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_NETWORK | 69250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick StrictMode.PENALTY_DROPBOX); 69332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick sVmPolicyMask = StrictMode.DETECT_VM_CURSOR_LEAKS | 694fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom StrictMode.DETECT_VM_CLOSABLE_LEAKS | 69532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.PENALTY_DROPBOX | 69632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.PENALTY_LOG; 69750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return true; 69850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 69950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick 70050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick /** 701b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Used by the framework to make network usage on the main 702b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * thread a fatal error. 703b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 704b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 705b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 706b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static void enableDeathOnNetwork() { 707b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int oldPolicy = getThreadPolicyMask(); 708b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK; 709b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick setThreadPolicyMask(newPolicy); 710b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 711b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 712b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 7135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Parses the BlockGuard policy mask out from the Exception's 7145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * getMessage() String value. Kinda gross, but least 7155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * invasive. :/ 7165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 7175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Input is of form "policy=137 violation=64" 7185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 7195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Returns 0 on failure, which is a valid policy, but not a 7205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * valid policy during a violation (else there must've been 7215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * some policy in effect to violate). 7225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 7235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parsePolicyFromMessage(String message) { 7245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null || !message.startsWith("policy=")) { 7255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int spaceIndex = message.indexOf(' '); 7285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (spaceIndex == -1) { 7295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String policyString = message.substring(7, spaceIndex); 7325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 7335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(policyString).intValue(); 7345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 7355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 7395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 7405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Like parsePolicyFromMessage(), but returns the violation. 7415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 7425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parseViolationFromMessage(String message) { 7435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null) { 7445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int violationIndex = message.indexOf("violation="); 7475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (violationIndex == -1) { 7485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String violationString = message.substring(violationIndex + 10); 7515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 7525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(violationString).intValue(); 7535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 7545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 758191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed = 759191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 760191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 761191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return new ArrayList<ViolationInfo>(); 762191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 763191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick }; 764191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 765191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static boolean tooManyViolationsThisLoop() { 766191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP; 767191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 768191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 769438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static class AndroidBlockGuardPolicy implements BlockGuard.Policy { 770438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private int mPolicyMask; 77146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 77246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Map from violation stacktrace hashcode -> uptimeMillis of 77346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // last violation. No locking needed, as this is only 77446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // accessed by the same thread. 77546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>(); 776438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 777438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public AndroidBlockGuardPolicy(final int policyMask) { 778438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 779438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 780438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 7815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick @Override 7825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public String toString() { 7835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask; 7845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 786438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 787438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public int getPolicyMask() { 788438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return mPolicyMask; 789438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 790438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 791438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 792438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onWriteToDisk() { 79332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_WRITE) == 0) { 794438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 795438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 796191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 797191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 798191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 799cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask); 800cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 801cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 802438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 803438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 804438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 805438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onReadFromDisk() { 80632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_READ) == 0) { 807438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 808438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 809191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 810191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 811191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 812cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask); 813cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 814cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 815438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 816438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 817438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 818438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onNetwork() { 81932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_NETWORK) == 0) { 820438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 821438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 822b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) { 823b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick throw new NetworkOnMainThreadException(); 824b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 825191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 826191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 827191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 828cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask); 829cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 830cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 831438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 832438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 833438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void setPolicyMask(int policyMask) { 834438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 835438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 836438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 8375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Start handling a violation that just started and hasn't 8385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // actually run yet (e.g. no disk write or network operation 8395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // has yet occurred). This sees if we're in an event loop 8405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread and, if so, uses it to roughly measure how long the 8415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation took. 8425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) { 843cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ViolationInfo info = new ViolationInfo(e, e.getPolicy()); 844cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.violationUptimeMillis = SystemClock.uptimeMillis(); 845cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolationWithTimingAttempt(info); 846cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 847438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 848cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Attempts to fill in the provided ViolationInfo's 849cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // durationMillis field if this thread has a Looper we can use 850cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // to measure with. We measure from the time of violation 851cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // until the time the looper is idle again (right before 852cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // the next epoll_wait) 853cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolationWithTimingAttempt(final ViolationInfo info) { 854438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Looper looper = Looper.myLooper(); 855cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 856cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Without a Looper, we're unable to time how long the 857cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // violation takes place. This case should be rare, as 858cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // most users will care about timing violations that 859cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // happen on their main UI thread. Note that this case is 860cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // also hit when a violation takes place in a Binder 861cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // thread, in "gather" mode. In this case, the duration 862cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // of the violation is computed by the ultimate caller and 863cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // its Looper, if any. 864cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // TODO: if in gather mode, ignore Looper.myLooper() and always 865cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // go into this immediate mode? 866438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick if (looper == null) { 867cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis = -1; // unknown (redundant, already set) 868cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(info); 869cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 870438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 871438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 872cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick MessageQueue queue = Looper.myQueue(); 873cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ArrayList<ViolationInfo> records = violationsBeingTimed.get(); 874191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (records.size() >= MAX_OFFENSES_PER_LOOP) { 875cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Not worth measuring. Too many offenses in one loop. 876cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 877cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 878cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.add(info); 879cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (records.size() > 1) { 880cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // There's already been a violation this loop, so we've already 881cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // registered an idle handler to process the list of violations 882cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // at the end of this Looper's loop. 883cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 884cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 885cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 886cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick queue.addIdleHandler(new MessageQueue.IdleHandler() { 887cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public boolean queueIdle() { 888cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick long loopFinishTime = SystemClock.uptimeMillis(); 889cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (int n = 0; n < records.size(); ++n) { 890cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo v = records.get(n); 891cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.violationNumThisLoop = n + 1; 892cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.durationMillis = 893cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick (int) (loopFinishTime - v.violationUptimeMillis); 894cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(v); 895cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 896cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.clear(); 897cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return false; // remove this idle handler from the array 898cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 899cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick }); 9005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 901438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 9025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Note: It's possible (even quite likely) that the 9035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread-local policy mask has changed from the time the 9045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation fired and now (after the violating code ran) due 9055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // to people who push/pop temporary policy in regions of code, 9065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // hence the policy being passed around. 907cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolation(final ViolationInfo info) { 908cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) { 909cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.wtf(TAG, "unexpected null stacktrace"); 9105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 912438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 913cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy); 91446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 915cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_GATHER) != 0) { 916cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 917703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 918cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations = new ArrayList<ViolationInfo>(1); 919703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(violations); 920703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else if (violations.size() >= 5) { 9215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Too many. In a loop or something? Don't gather them all. 9225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 924cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (ViolationInfo previous : violations) { 925cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) { 9265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Duplicate. Don't log. 9275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 9295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 930cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations.add(info); 931727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick return; 932727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 933727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 93446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Not perfect, but fast and good enough for dup suppression. 935cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Integer crashFingerprint = info.crashInfo.stackTrace.hashCode(); 93646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long lastViolationTime = 0; 93746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (mLastViolationTime.containsKey(crashFingerprint)) { 93846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick lastViolationTime = mLastViolationTime.get(crashFingerprint); 93946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 94046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long now = SystemClock.uptimeMillis(); 94146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick mLastViolationTime.put(crashFingerprint, now); 94246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long timeSinceLastViolationMillis = lastViolationTime == 0 ? 94346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Long.MAX_VALUE : (now - lastViolationTime); 94446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 945cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_LOG) != 0 && 94646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 947cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.durationMillis != -1) { 9485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation; ~duration=" + 949cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis + " ms: " + info.crashInfo.stackTrace); 950438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 951cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace); 952438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 953438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 954438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 95571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // The violationMaskSubset, passed to ActivityManager, is a 95646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // subset of the original StrictMode policy bitmask, with 95746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // only the bit violated and penalty bits to be executed 95846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // by the ActivityManagerService remaining set. 959cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationMaskSubset = 0; 96046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 961cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DIALOG) != 0 && 96246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) { 963cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DIALOG; 96446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 96546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 966cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) { 967cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DROPBOX; 96846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 96946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 970cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationMaskSubset != 0) { 971cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage); 972cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= violationBit; 97371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick final int violationMaskSubsetFinal = violationMaskSubset; 97432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 97571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 97671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick final boolean justDropBox = (info.policy & PENALTY_MASK) == PENALTY_DROPBOX; 97771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick if (justDropBox) { 97871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // If all we're going to ask the activity manager 97971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // to do is dropbox it (the common case during 98071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // platform development), we can avoid doing this 98171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // call synchronously which Binder data suggests 98271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // isn't always super fast, despite the implementation 98371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // in the ActivityManager trying to be mostly async. 98471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick new Thread("callActivityManagerForStrictModeDropbox") { 98571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick public void run() { 98671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 98771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick try { 98871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick ActivityManagerNative.getDefault(). 98971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick handleApplicationStrictModeViolation( 99071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick RuntimeInit.getApplicationObject(), 99171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick violationMaskSubsetFinal, 99271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick info); 99371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } catch (RemoteException e) { 99471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick Log.e(TAG, "RemoteException handling StrictMode violation", e); 99571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } 99671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } 99771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick }.start(); 99871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick return; 99971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } 100071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 100171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // Normal synchronous call to the ActivityManager. 1002438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick try { 1003727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 1004727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 1005727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 1006727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // We restore the current policy below, in the finally block. 100732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 1008727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1009438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 1010438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick RuntimeInit.getApplicationObject(), 1011cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset, 1012cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info); 1013438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } catch (RemoteException e) { 101446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 1015727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } finally { 1016727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Restore the policy. 101732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 1018438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1019438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1020438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1021cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DEATH) != 0) { 1022438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick System.err.println("StrictMode policy violation with POLICY_DEATH; shutting down."); 1023438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Process.killProcess(Process.myPid()); 1024438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick System.exit(10); 1025438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1026438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1027438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1028727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1029727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 10305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 10315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 10325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static boolean hasGatheredViolations() { 1033703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return gatheredViolations.get() != null; 1034703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1035703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick 1036703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /** 1037703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Called from Parcel.writeException(), so we drop this memory and 1038703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * don't incorrectly attribute it to the wrong caller on the next 1039703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Binder call on this thread. 1040703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick */ 1041703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /* package */ static void clearGatheredViolations() { 1042703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 10435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 10455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 104632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions in the VM process (on any 104732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * thread) should be detected, as well as the penalty if such 104832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * actions occur. 104932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 105032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 105132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 105232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setVmPolicy(final VmPolicy policy) { 105332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick sVmPolicyMask = policy.mask; 1054fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom CloseGuard.setEnabled(vmClosableObjectLeaksEnabled()); 105532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 105632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 105732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 105832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Gets the current VM policy. 105932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 106032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static VmPolicy getVmPolicy() { 106132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new VmPolicy(sVmPolicyMask); 106232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 106332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 106432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 106562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * Enable the recommended StrictMode defaults, with violations just being logged. 106662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * 106762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * <p>This catches disk and network access on the main thread, as 1068fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * well as leaked SQLite cursors and unclosed resources. This is 1069fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * simply a wrapper around {@link #setVmPolicy} and {@link 1070fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * #setThreadPolicy}. 107162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick */ 107262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick public static void enableDefaults() { 107362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 107462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectAll() 107562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 107662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 107762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 107862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectLeakedSqlLiteObjects() 1079fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom .detectLeakedClosableObjects() 108062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 108162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 108262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick } 108362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick 108462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick /** 108532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 108632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 108732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static boolean vmSqliteObjectLeaksEnabled() { 108832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0; 108932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 109032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 109132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 109232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 109332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 1094fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static boolean vmClosableObjectLeaksEnabled() { 1095fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0; 1096fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 1097fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 1098fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 1099fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 1100fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 110132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void onSqliteObjectLeaked(String message, Throwable originStack) { 110232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((sVmPolicyMask & PENALTY_LOG) != 0) { 110332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, message, originStack); 110432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 110532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 110632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((sVmPolicyMask & PENALTY_DROPBOX) != 0) { 110732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask); 110832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 110932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // The violationMask, passed to ActivityManager, is a 111032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // subset of the original StrictMode policy bitmask, with 111132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // only the bit violated and penalty bits to be executed 111232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // by the ActivityManagerService remaining set. 111332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int violationMaskSubset = PENALTY_DROPBOX | DETECT_VM_CURSOR_LEAKS; 111432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 111532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick try { 111632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 111732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 111832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 111932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // We restore the current policy below, in the finally block. 112032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 112132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 112232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 112332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick RuntimeInit.getApplicationObject(), 112432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick violationMaskSubset, 112532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick info); 112632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } catch (RemoteException e) { 112732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 112832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } finally { 112932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Restore the policy. 113032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 113132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 113232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 113332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 113432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((sVmPolicyMask & PENALTY_DEATH) != 0) { 113532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down."); 113632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Process.killProcess(Process.myPid()); 113732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.exit(10); 113832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 113932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 114032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 114132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 11425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 11435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 11445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void writeGatheredViolationsToParcel(Parcel p) { 1145cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1146703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1147703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(0); 1148703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else { 1149703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(violations.size()); 1150703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick for (int i = 0; i < violations.size(); ++i) { 1151703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.get(i).writeToParcel(p, 0 /* unused flags? */); 1152703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1153703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size()); 1154703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.clear(); // somewhat redundant, as we're about to null the threadlocal 11555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1156703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 11575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 11585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 11595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class LogStackTrace extends Exception {} 11605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 11615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 11625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS, 11635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * we here read back all the encoded violations. 11645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 11655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void readAndHandleBinderCallViolations(Parcel p) { 11665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Our own stack trace to append 11675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick StringWriter sw = new StringWriter(); 1168cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new LogStackTrace().printStackTrace(new PrintWriter(sw)); 11695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String ourStack = sw.toString(); 11705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 117132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int policyMask = getThreadPolicyMask(); 1172cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0; 11735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 11745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int numViolations = p.readInt(); 11755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick for (int i = 0; i < numViolations; ++i) { 11765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call. i=" + i); 1177cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo info = new ViolationInfo(p, !currentlyGathering); 1178cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack; 11795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 11805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (policy instanceof AndroidBlockGuardPolicy) { 1181cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info); 11825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 11835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 11845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 11855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 11865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 1187727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Called from android_util_Binder.cpp's 1188727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * android_os_Parcel_enforceInterface when an incoming Binder call 1189727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * requires changing the StrictMode policy mask. The role of this 1190727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * function is to ask Binder for its current (native) thread-local 1191727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * policy value and synchronize it to libcore's (Java) 1192727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * thread-local policy value. 1193727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 1194727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void onBinderStrictModePolicyChange(int newPolicy) { 1195727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(newPolicy); 1196727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1197cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1198cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1199cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Parcelable that gets sent in Binder call headers back to callers 1200cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * to report violations that happened during a cross-process call. 1201cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 1202cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @hide 1203cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1204cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public static class ViolationInfo { 1205cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1206cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Stack and other stuff info. 1207cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1208cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final ApplicationErrorReport.CrashInfo crashInfo; 1209cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1210cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1211cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The strict mode policy mask at the time of violation. 1212cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1213cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final int policy; 1214cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1215cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1216cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The wall time duration of the violation, when known. -1 when 1217cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * not known. 1218cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1219cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int durationMillis = -1; 1220cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1221cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1222599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * The number of animations currently running. 1223599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 1224599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public int numAnimationsRunning = 0; 1225599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 1226599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 1227cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Which violation number this was (1-based) since the last Looper loop, 1228cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * from the perspective of the root caller (if it crossed any processes 1229cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * via Binder calls). The value is 0 if the root caller wasn't on a Looper 1230cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * thread. 1231cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1232cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int violationNumThisLoop; 1233cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1234cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1235cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The time (in terms of SystemClock.uptimeMillis()) that the 1236cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * violation occurred. 1237cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1238cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public long violationUptimeMillis; 1239cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1240cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1241bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * The action of the Intent being broadcast to somebody's onReceive 1242bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * on this thread right now, or null. 1243bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick */ 1244bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick public String broadcastIntentAction; 1245bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick 1246bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick /** 1247cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an uninitialized instance of ViolationInfo 1248cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1249cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo() { 1250cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = null; 1251cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = 0; 1252cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1253cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1254cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1255cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from an exception. 1256cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1257cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Throwable tr, int policy) { 1258cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(tr); 1259cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = SystemClock.uptimeMillis(); 1260cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this.policy = policy; 1261599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount(); 1262bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast(); 1263bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntent != null) { 1264bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = broadcastIntent.getAction(); 1265bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 1266cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1267cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1268cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1269cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 1270cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1271cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in) { 1272cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this(in, false); 1273cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1274cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1275cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1276cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 1277cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 1278cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @param unsetGatheringBit if true, the caller is the root caller 1279cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * and the gathering penalty should be removed. 1280cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1281cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in, boolean unsetGatheringBit) { 1282cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(in); 1283cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int rawPolicy = in.readInt(); 1284cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (unsetGatheringBit) { 1285cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy & ~PENALTY_GATHER; 1286cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } else { 1287cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy; 1288cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1289cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick durationMillis = in.readInt(); 1290cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationNumThisLoop = in.readInt(); 1291599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick numAnimationsRunning = in.readInt(); 1292cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = in.readLong(); 1293bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = in.readString(); 1294cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1295cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1296cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1297cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Save a ViolationInfo instance to a parcel. 1298cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1299cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void writeToParcel(Parcel dest, int flags) { 1300cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.writeToParcel(dest, flags); 1301cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(policy); 1302cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(durationMillis); 1303cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(violationNumThisLoop); 1304599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick dest.writeInt(numAnimationsRunning); 1305cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeLong(violationUptimeMillis); 1306bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick dest.writeString(broadcastIntentAction); 1307cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1308cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1309cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1310cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1311cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Dump a ViolationInfo instance to a Printer. 1312cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1313cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void dump(Printer pw, String prefix) { 1314cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.dump(pw, prefix); 1315cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "policy: " + policy); 1316cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (durationMillis != -1) { 1317cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "durationMillis: " + durationMillis); 1318cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1319cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationNumThisLoop != 0) { 1320cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop); 1321cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1322599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick if (numAnimationsRunning != 0) { 1323599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning); 1324599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick } 1325cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis); 1326bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntentAction != null) { 1327bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction); 1328bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 1329cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1330cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1331cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1332438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick} 1333