| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.os; |
| 17 |
|
| 18 |
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; |
| 19 |
|
| 20 |
import android.animation.ValueAnimator; |
| 21 |
import android.annotation.IntDef; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.annotation.SystemApi; |
| 25 |
import android.annotation.TestApi; |
| 26 |
import android.app.ActivityManager; |
| 27 |
import android.app.ActivityThread; |
| 28 |
import android.app.IActivityManager; |
| 29 |
import android.app.compat.CompatChanges; |
| 30 |
import android.compat.annotation.ChangeId; |
| 31 |
import android.compat.annotation.EnabledSince; |
| 32 |
import android.compat.annotation.UnsupportedAppUsage; |
| 33 |
import android.content.BroadcastReceiver; |
| 34 |
import android.content.Context; |
| 35 |
import android.content.Intent; |
| 36 |
import android.content.ServiceConnection; |
| 37 |
import android.content.pm.ApplicationInfo; |
| 38 |
import android.content.pm.PackageManager; |
| 39 |
import android.content.res.Configuration; |
| 40 |
import android.net.TrafficStats; |
| 41 |
import android.net.Uri; |
| 42 |
import android.os.storage.IStorageManager; |
| 43 |
import android.os.strictmode.CleartextNetworkViolation; |
| 44 |
import android.os.strictmode.ContentUriWithoutPermissionViolation; |
| 45 |
import android.os.strictmode.CredentialProtectedWhileLockedViolation; |
| 46 |
import android.os.strictmode.CustomViolation; |
| 47 |
import android.os.strictmode.DiskReadViolation; |
| 48 |
import android.os.strictmode.DiskWriteViolation; |
| 49 |
import android.os.strictmode.ExplicitGcViolation; |
| 50 |
import android.os.strictmode.FileUriExposedViolation; |
| 51 |
import android.os.strictmode.ImplicitDirectBootViolation; |
| 52 |
import android.os.strictmode.IncorrectContextUseViolation; |
| 53 |
import android.os.strictmode.InstanceCountViolation; |
| 54 |
import android.os.strictmode.IntentReceiverLeakedViolation; |
| 55 |
import android.os.strictmode.LeakedClosableViolation; |
| 56 |
import android.os.strictmode.NetworkViolation; |
| 57 |
import android.os.strictmode.NonSdkApiUsedViolation; |
| 58 |
import android.os.strictmode.ResourceMismatchViolation; |
| 59 |
import android.os.strictmode.ServiceConnectionLeakedViolation; |
| 60 |
import android.os.strictmode.SqliteObjectLeakedViolation; |
| 61 |
import android.os.strictmode.UnbufferedIoViolation; |
| 62 |
import android.os.strictmode.UnsafeIntentLaunchViolation; |
| 63 |
import android.os.strictmode.UntaggedSocketViolation; |
| 64 |
import android.os.strictmode.Violation; |
| 65 |
import android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation; |
| 66 |
import android.util.ArrayMap; |
| 67 |
import android.util.Log; |
| 68 |
import android.util.Printer; |
| 69 |
import android.util.Singleton; |
| 70 |
import android.util.Slog; |
| 71 |
import android.util.SparseLongArray; |
| 72 |
import android.view.IWindowManager; |
| 73 |
|
| 74 |
import com.android.internal.annotations.GuardedBy; |
| 75 |
import com.android.internal.os.BackgroundThread; |
| 76 |
import com.android.internal.os.RuntimeInit; |
| 77 |
import com.android.internal.util.FastPrintWriter; |
| 78 |
import com.android.internal.util.HexDump; |
| 79 |
|
| 80 |
import dalvik.system.BlockGuard; |
| 81 |
import dalvik.system.CloseGuard; |
| 82 |
import dalvik.system.VMDebug; |
| 83 |
import dalvik.system.VMRuntime; |
| 84 |
|
| 85 |
import java.io.PrintWriter; |
| 86 |
import java.io.StringWriter; |
| 87 |
import java.lang.annotation.Retention; |
| 88 |
import java.lang.annotation.RetentionPolicy; |
| 89 |
import java.net.InetAddress; |
| 90 |
import java.net.UnknownHostException; |
| 91 |
import java.util.ArrayDeque; |
| 92 |
import java.util.ArrayList; |
| 93 |
import java.util.Arrays; |
| 94 |
import java.util.Deque; |
| 95 |
import java.util.HashMap; |
| 96 |
import java.util.concurrent.Executor; |
| 97 |
import java.util.concurrent.RejectedExecutionException; |
| 98 |
import java.util.concurrent.atomic.AtomicInteger; |
| 99 |
import java.util.function.Consumer; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public final class StrictMode { |
| 157 |
private static final String TAG = "StrictMode"; |
| 158 |
private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public static final String DISABLE_PROPERTY = "persist.sys.strictmode.disable"; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual"; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private static final String CLEARTEXT_PROPERTY = "persist.sys.strictmode.clear"; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
private static final boolean DISABLE = false; |
| 187 |
|
| 188 |
|
| 189 |
private static final long MIN_VM_INTERVAL_MS = 1000; |
| 190 |
|
| 191 |
|
| 192 |
private static final long MIN_LOG_INTERVAL_MS = 1000; |
| 193 |
|
| 194 |
|
| 195 |
private static final long MIN_DIALOG_INTERVAL_MS = 30000; |
| 196 |
|
| 197 |
|
| 198 |
private static final long MIN_DROPBOX_INTERVAL_MS = 3000; |
| 199 |
|
| 200 |
|
| 201 |
private static final int MAX_SPAN_TAGS = 20; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private static final int MAX_OFFENSES_PER_LOOP = 10; |
| 206 |
|
| 207 |
|
| 208 |
@IntDef(flag = true, prefix = { "DETECT_THREAD_", "PENALTY_" }, value = { |
| 209 |
DETECT_THREAD_DISK_WRITE, |
| 210 |
DETECT_THREAD_DISK_READ, |
| 211 |
DETECT_THREAD_NETWORK, |
| 212 |
DETECT_THREAD_CUSTOM, |
| 213 |
DETECT_THREAD_RESOURCE_MISMATCH, |
| 214 |
DETECT_THREAD_UNBUFFERED_IO, |
| 215 |
DETECT_THREAD_EXPLICIT_GC, |
| 216 |
PENALTY_GATHER, |
| 217 |
PENALTY_LOG, |
| 218 |
PENALTY_DIALOG, |
| 219 |
PENALTY_DEATH, |
| 220 |
PENALTY_FLASH, |
| 221 |
PENALTY_DROPBOX, |
| 222 |
PENALTY_DEATH_ON_NETWORK, |
| 223 |
PENALTY_DEATH_ON_CLEARTEXT_NETWORK, |
| 224 |
PENALTY_DEATH_ON_FILE_URI_EXPOSURE, |
| 225 |
}) |
| 226 |
@Retention(RetentionPolicy.SOURCE) |
| 227 |
public @interface ThreadPolicyMask {} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private static final int DETECT_THREAD_DISK_WRITE = 1 << 0; |
| 233 |
|
| 234 |
private static final int DETECT_THREAD_DISK_READ = 1 << 1; |
| 235 |
|
| 236 |
private static final int DETECT_THREAD_NETWORK = 1 << 2; |
| 237 |
|
| 238 |
private static final int DETECT_THREAD_CUSTOM = 1 << 3; |
| 239 |
|
| 240 |
private static final int DETECT_THREAD_RESOURCE_MISMATCH = 1 << 4; |
| 241 |
|
| 242 |
private static final int DETECT_THREAD_UNBUFFERED_IO = 1 << 5; |
| 243 |
|
| 244 |
private static final int DETECT_THREAD_EXPLICIT_GC = 1 << 6; |
| 245 |
|
| 246 |
|
| 247 |
private static final int DETECT_THREAD_ALL = 0x0000ffff; |
| 248 |
|
| 249 |
|
| 250 |
@IntDef(flag = true, prefix = { "DETECT_THREAD_", "PENALTY_" }, value = { |
| 251 |
DETECT_VM_CURSOR_LEAKS, |
| 252 |
DETECT_VM_CLOSABLE_LEAKS, |
| 253 |
DETECT_VM_ACTIVITY_LEAKS, |
| 254 |
DETECT_VM_INSTANCE_LEAKS, |
| 255 |
DETECT_VM_REGISTRATION_LEAKS, |
| 256 |
DETECT_VM_FILE_URI_EXPOSURE, |
| 257 |
DETECT_VM_CLEARTEXT_NETWORK, |
| 258 |
DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION, |
| 259 |
DETECT_VM_UNTAGGED_SOCKET, |
| 260 |
DETECT_VM_NON_SDK_API_USAGE, |
| 261 |
DETECT_VM_IMPLICIT_DIRECT_BOOT, |
| 262 |
DETECT_VM_INCORRECT_CONTEXT_USE, |
| 263 |
DETECT_VM_UNSAFE_INTENT_LAUNCH, |
| 264 |
PENALTY_GATHER, |
| 265 |
PENALTY_LOG, |
| 266 |
PENALTY_DIALOG, |
| 267 |
PENALTY_DEATH, |
| 268 |
PENALTY_FLASH, |
| 269 |
PENALTY_DROPBOX, |
| 270 |
PENALTY_DEATH_ON_NETWORK, |
| 271 |
PENALTY_DEATH_ON_CLEARTEXT_NETWORK, |
| 272 |
PENALTY_DEATH_ON_FILE_URI_EXPOSURE, |
| 273 |
}) |
| 274 |
@Retention(RetentionPolicy.SOURCE) |
| 275 |
public @interface VmPolicyMask {} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
private static final int DETECT_VM_CURSOR_LEAKS = 1 << 0; |
| 281 |
|
| 282 |
private static final int DETECT_VM_CLOSABLE_LEAKS = 1 << 1; |
| 283 |
|
| 284 |
private static final int DETECT_VM_ACTIVITY_LEAKS = 1 << 2; |
| 285 |
|
| 286 |
private static final int DETECT_VM_INSTANCE_LEAKS = 1 << 3; |
| 287 |
|
| 288 |
private static final int DETECT_VM_REGISTRATION_LEAKS = 1 << 4; |
| 289 |
|
| 290 |
private static final int DETECT_VM_FILE_URI_EXPOSURE = 1 << 5; |
| 291 |
|
| 292 |
private static final int DETECT_VM_CLEARTEXT_NETWORK = 1 << 6; |
| 293 |
|
| 294 |
private static final int DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION = 1 << 7; |
| 295 |
|
| 296 |
private static final int DETECT_VM_UNTAGGED_SOCKET = 1 << 8; |
| 297 |
|
| 298 |
private static final int DETECT_VM_NON_SDK_API_USAGE = 1 << 9; |
| 299 |
|
| 300 |
private static final int DETECT_VM_IMPLICIT_DIRECT_BOOT = 1 << 10; |
| 301 |
|
| 302 |
private static final int DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED = 1 << 11; |
| 303 |
|
| 304 |
private static final int DETECT_VM_INCORRECT_CONTEXT_USE = 1 << 12; |
| 305 |
|
| 306 |
private static final int DETECT_VM_UNSAFE_INTENT_LAUNCH = 1 << 13; |
| 307 |
|
| 308 |
|
| 309 |
private static final int DETECT_VM_ALL = 0x0000ffff; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public static final int PENALTY_GATHER = 1 << 31; |
| 323 |
|
| 324 |
|
| 325 |
public static final int PENALTY_LOG = 1 << 30; |
| 326 |
|
| 327 |
public static final int PENALTY_DIALOG = 1 << 29; |
| 328 |
|
| 329 |
public static final int PENALTY_DEATH = 1 << 28; |
| 330 |
|
| 331 |
public static final int PENALTY_FLASH = 1 << 27; |
| 332 |
|
| 333 |
public static final int PENALTY_DROPBOX = 1 << 26; |
| 334 |
|
| 335 |
public static final int PENALTY_DEATH_ON_NETWORK = 1 << 25; |
| 336 |
|
| 337 |
public static final int PENALTY_DEATH_ON_CLEARTEXT_NETWORK = 1 << 24; |
| 338 |
|
| 339 |
public static final int PENALTY_DEATH_ON_FILE_URI_EXPOSURE = 1 << 23; |
| 340 |
|
| 341 |
|
| 342 |
public static final int PENALTY_ALL = 0xffff0000; |
| 343 |
|
| 344 |
|
| 345 |
public static final int NETWORK_POLICY_ACCEPT = 0; |
| 346 |
|
| 347 |
public static final int NETWORK_POLICY_LOG = 1; |
| 348 |
|
| 349 |
public static final int NETWORK_POLICY_REJECT = 2; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
@ChangeId |
| 355 |
@EnabledSince(targetSdkVersion = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 356 |
static final long DETECT_EXPLICIT_GC = 3400644L; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = |
| 361 |
new HashMap<Class, Integer>(); |
| 362 |
|
| 363 |
|
| 364 |
private static volatile VmPolicy sVmPolicy = VmPolicy.LAX; |
| 365 |
|
| 366 |
|
| 367 |
@TestApi |
| 368 |
public interface ViolationLogger { |
| 369 |
|
| 370 |
|
| 371 |
void log(ViolationInfo info); |
| 372 |
} |
| 373 |
|
| 374 |
private static final ViolationLogger LOGCAT_LOGGER = |
| 375 |
info -> { |
| 376 |
String msg; |
| 377 |
if (info.durationMillis != -1) { |
| 378 |
msg = "StrictMode policy violation; ~duration=" + info.durationMillis + " ms:"; |
| 379 |
} else { |
| 380 |
msg = "StrictMode policy violation:"; |
| 381 |
} |
| 382 |
Log.d(TAG, msg + " " + info.getStackTrace()); |
| 383 |
}; |
| 384 |
|
| 385 |
private static volatile ViolationLogger sLogger = LOGCAT_LOGGER; |
| 386 |
|
| 387 |
private static final ThreadLocal<OnThreadViolationListener> sThreadViolationListener = |
| 388 |
new ThreadLocal<>(); |
| 389 |
private static final ThreadLocal<Executor> sThreadViolationExecutor = new ThreadLocal<>(); |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public interface OnThreadViolationListener { |
| 396 |
|
| 397 |
void onThreadViolation(Violation v); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public interface OnVmViolationListener { |
| 405 |
|
| 406 |
void onVmViolation(Violation v); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
@TestApi |
| 411 |
public static void setViolationLogger(ViolationLogger listener) { |
| 412 |
if (listener == null) { |
| 413 |
listener = LOGCAT_LOGGER; |
| 414 |
} |
| 415 |
sLogger = listener; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0); |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
private static final Consumer<String> sNonSdkApiUsageConsumer = |
| 429 |
message -> onVmPolicyViolation(new NonSdkApiUsedViolation(message)); |
| 430 |
|
| 431 |
private StrictMode() {} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public static final class ThreadPolicy { |
| 444 |
|
| 445 |
public static final ThreadPolicy LAX = new ThreadPolicy(0, null, null); |
| 446 |
|
| 447 |
@UnsupportedAppUsage |
| 448 |
final @ThreadPolicyMask int mask; |
| 449 |
final OnThreadViolationListener mListener; |
| 450 |
final Executor mCallbackExecutor; |
| 451 |
|
| 452 |
private ThreadPolicy(@ThreadPolicyMask int mask, OnThreadViolationListener listener, |
| 453 |
Executor executor) { |
| 454 |
this.mask = mask; |
| 455 |
mListener = listener; |
| 456 |
mCallbackExecutor = executor; |
| 457 |
} |
| 458 |
|
| 459 |
@Override |
| 460 |
public String toString() { |
| 461 |
return "[StrictMode.ThreadPolicy; mask=" + mask + "]"; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public static final class Builder { |
| 483 |
private @ThreadPolicyMask int mMask = 0; |
| 484 |
private OnThreadViolationListener mListener; |
| 485 |
private Executor mExecutor; |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public Builder() { |
| 493 |
mMask = 0; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
public Builder(ThreadPolicy policy) { |
| 498 |
mMask = policy.mask; |
| 499 |
mListener = policy.mListener; |
| 500 |
mExecutor = policy.mCallbackExecutor; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
@SuppressWarnings("AndroidFrameworkCompatChange") |
| 510 |
public @NonNull Builder detectAll() { |
| 511 |
detectDiskReads(); |
| 512 |
detectDiskWrites(); |
| 513 |
detectNetwork(); |
| 514 |
|
| 515 |
final int targetSdk = VMRuntime.getRuntime().getTargetSdkVersion(); |
| 516 |
if (targetSdk >= Build.VERSION_CODES.HONEYCOMB) { |
| 517 |
detectCustomSlowCalls(); |
| 518 |
} |
| 519 |
if (targetSdk >= Build.VERSION_CODES.M) { |
| 520 |
detectResourceMismatches(); |
| 521 |
} |
| 522 |
if (targetSdk >= Build.VERSION_CODES.O) { |
| 523 |
detectUnbufferedIo(); |
| 524 |
} |
| 525 |
if (CompatChanges.isChangeEnabled(DETECT_EXPLICIT_GC)) { |
| 526 |
detectExplicitGc(); |
| 527 |
} |
| 528 |
return this; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
public @NonNull Builder permitAll() { |
| 533 |
return disable(DETECT_THREAD_ALL); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
public @NonNull Builder detectNetwork() { |
| 538 |
return enable(DETECT_THREAD_NETWORK); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
public @NonNull Builder permitNetwork() { |
| 543 |
return disable(DETECT_THREAD_NETWORK); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
public @NonNull Builder detectDiskReads() { |
| 548 |
return enable(DETECT_THREAD_DISK_READ); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
public @NonNull Builder permitDiskReads() { |
| 553 |
return disable(DETECT_THREAD_DISK_READ); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
public @NonNull Builder detectCustomSlowCalls() { |
| 558 |
return enable(DETECT_THREAD_CUSTOM); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
public @NonNull Builder permitCustomSlowCalls() { |
| 563 |
return disable(DETECT_THREAD_CUSTOM); |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
public @NonNull Builder permitResourceMismatches() { |
| 568 |
return disable(DETECT_THREAD_RESOURCE_MISMATCH); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
public @NonNull Builder detectUnbufferedIo() { |
| 573 |
return enable(DETECT_THREAD_UNBUFFERED_IO); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
public @NonNull Builder permitUnbufferedIo() { |
| 578 |
return disable(DETECT_THREAD_UNBUFFERED_IO); |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public @NonNull Builder detectResourceMismatches() { |
| 594 |
return enable(DETECT_THREAD_RESOURCE_MISMATCH); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
public @NonNull Builder detectDiskWrites() { |
| 599 |
return enable(DETECT_THREAD_DISK_WRITE); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
public @NonNull Builder permitDiskWrites() { |
| 604 |
return disable(DETECT_THREAD_DISK_WRITE); |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
public @NonNull Builder detectExplicitGc() { |
| 611 |
return enable(DETECT_THREAD_EXPLICIT_GC); |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
public @NonNull Builder permitExplicitGc() { |
| 618 |
return disable(DETECT_THREAD_EXPLICIT_GC); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
public @NonNull Builder penaltyDialog() { |
| 626 |
return enable(PENALTY_DIALOG); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
public @NonNull Builder penaltyDeath() { |
| 638 |
return enable(PENALTY_DEATH); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
public @NonNull Builder penaltyDeathOnNetwork() { |
| 649 |
return enable(PENALTY_DEATH_ON_NETWORK); |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
public @NonNull Builder penaltyFlashScreen() { |
| 654 |
return enable(PENALTY_FLASH); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
public @NonNull Builder penaltyLog() { |
| 659 |
return enable(PENALTY_LOG); |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
public @NonNull Builder penaltyDropBox() { |
| 668 |
return enable(PENALTY_DROPBOX); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
public @NonNull Builder penaltyListener( |
| 676 |
@NonNull Executor executor, @NonNull OnThreadViolationListener listener) { |
| 677 |
if (executor == null) { |
| 678 |
throw new NullPointerException("executor must not be null"); |
| 679 |
} |
| 680 |
mListener = listener; |
| 681 |
mExecutor = executor; |
| 682 |
return this; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
public @NonNull Builder penaltyListener( |
| 687 |
@NonNull OnThreadViolationListener listener, @NonNull Executor executor) { |
| 688 |
return penaltyListener(executor, listener); |
| 689 |
} |
| 690 |
|
| 691 |
private Builder enable(@ThreadPolicyMask int mask) { |
| 692 |
mMask |= mask; |
| 693 |
return this; |
| 694 |
} |
| 695 |
|
| 696 |
private Builder disable(@ThreadPolicyMask int mask) { |
| 697 |
mMask &= ~mask; |
| 698 |
return this; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
public ThreadPolicy build() { |
| 708 |
|
| 709 |
|
| 710 |
if (mListener == null |
| 711 |
&& mMask != 0 |
| 712 |
&& (mMask |
| 713 |
& (PENALTY_DEATH |
| 714 |
| PENALTY_LOG |
| 715 |
| PENALTY_DROPBOX |
| 716 |
| PENALTY_DIALOG)) |
| 717 |
== 0) { |
| 718 |
penaltyLog(); |
| 719 |
} |
| 720 |
return new ThreadPolicy(mMask, mListener, mExecutor); |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
public static final class VmPolicy { |
| 731 |
|
| 732 |
public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP, null, null); |
| 733 |
|
| 734 |
@UnsupportedAppUsage |
| 735 |
final @VmPolicyMask int mask; |
| 736 |
final OnVmViolationListener mListener; |
| 737 |
final Executor mCallbackExecutor; |
| 738 |
|
| 739 |
|
| 740 |
final HashMap<Class, Integer> classInstanceLimit; |
| 741 |
|
| 742 |
private VmPolicy( |
| 743 |
@VmPolicyMask int mask, |
| 744 |
HashMap<Class, Integer> classInstanceLimit, |
| 745 |
OnVmViolationListener listener, |
| 746 |
Executor executor) { |
| 747 |
if (classInstanceLimit == null) { |
| 748 |
throw new NullPointerException("classInstanceLimit == null"); |
| 749 |
} |
| 750 |
this.mask = mask; |
| 751 |
this.classInstanceLimit = classInstanceLimit; |
| 752 |
mListener = listener; |
| 753 |
mCallbackExecutor = executor; |
| 754 |
} |
| 755 |
|
| 756 |
@Override |
| 757 |
public String toString() { |
| 758 |
return "[StrictMode.VmPolicy; mask=" + mask + "]"; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
public static final class Builder { |
| 780 |
@UnsupportedAppUsage |
| 781 |
private @VmPolicyMask int mMask; |
| 782 |
private OnVmViolationListener mListener; |
| 783 |
private Executor mExecutor; |
| 784 |
|
| 785 |
private HashMap<Class, Integer> mClassInstanceLimit; |
| 786 |
private boolean mClassInstanceLimitNeedCow = false; |
| 787 |
|
| 788 |
public Builder() { |
| 789 |
mMask = 0; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
public Builder(VmPolicy base) { |
| 794 |
mMask = base.mask; |
| 795 |
mClassInstanceLimitNeedCow = true; |
| 796 |
mClassInstanceLimit = base.classInstanceLimit; |
| 797 |
mListener = base.mListener; |
| 798 |
mExecutor = base.mCallbackExecutor; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
public @NonNull Builder setClassInstanceLimit(Class klass, int instanceLimit) { |
| 806 |
if (klass == null) { |
| 807 |
throw new NullPointerException("klass == null"); |
| 808 |
} |
| 809 |
if (mClassInstanceLimitNeedCow) { |
| 810 |
if (mClassInstanceLimit.containsKey(klass) |
| 811 |
&& mClassInstanceLimit.get(klass) == instanceLimit) { |
| 812 |
|
| 813 |
return this; |
| 814 |
} |
| 815 |
mClassInstanceLimitNeedCow = false; |
| 816 |
mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone(); |
| 817 |
} else if (mClassInstanceLimit == null) { |
| 818 |
mClassInstanceLimit = new HashMap<Class, Integer>(); |
| 819 |
} |
| 820 |
mMask |= DETECT_VM_INSTANCE_LEAKS; |
| 821 |
mClassInstanceLimit.put(klass, instanceLimit); |
| 822 |
return this; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
public @NonNull Builder detectActivityLeaks() { |
| 827 |
return enable(DETECT_VM_ACTIVITY_LEAKS); |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
public @NonNull Builder permitActivityLeaks() { |
| 832 |
synchronized (StrictMode.class) { |
| 833 |
sExpectedActivityInstanceCount.clear(); |
| 834 |
} |
| 835 |
return disable(DETECT_VM_ACTIVITY_LEAKS); |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
public @NonNull Builder detectNonSdkApiUsage() { |
| 846 |
return enable(DETECT_VM_NON_SDK_API_USAGE); |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
public @NonNull Builder permitNonSdkApiUsage() { |
| 856 |
return disable(DETECT_VM_NON_SDK_API_USAGE); |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
@SuppressWarnings("AndroidFrameworkCompatChange") |
| 866 |
public @NonNull Builder detectAll() { |
| 867 |
detectLeakedSqlLiteObjects(); |
| 868 |
|
| 869 |
final int targetSdk = VMRuntime.getRuntime().getTargetSdkVersion(); |
| 870 |
if (targetSdk >= Build.VERSION_CODES.HONEYCOMB) { |
| 871 |
detectActivityLeaks(); |
| 872 |
detectLeakedClosableObjects(); |
| 873 |
} |
| 874 |
if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN) { |
| 875 |
detectLeakedRegistrationObjects(); |
| 876 |
} |
| 877 |
if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
| 878 |
detectFileUriExposure(); |
| 879 |
} |
| 880 |
if (targetSdk >= Build.VERSION_CODES.M) { |
| 881 |
|
| 882 |
|
| 883 |
if (SystemProperties.getBoolean(CLEARTEXT_PROPERTY, false)) { |
| 884 |
detectCleartextNetwork(); |
| 885 |
} |
| 886 |
} |
| 887 |
if (targetSdk >= Build.VERSION_CODES.O) { |
| 888 |
detectContentUriWithoutPermission(); |
| 889 |
detectUntaggedSockets(); |
| 890 |
} |
| 891 |
if (targetSdk >= Build.VERSION_CODES.Q) { |
| 892 |
detectCredentialProtectedWhileLocked(); |
| 893 |
} |
| 894 |
if (targetSdk >= Build.VERSION_CODES.R) { |
| 895 |
detectIncorrectContextUse(); |
| 896 |
} |
| 897 |
if (targetSdk >= Build.VERSION_CODES.S) { |
| 898 |
detectUnsafeIntentLaunch(); |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
return this; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
public @NonNull Builder detectLeakedSqlLiteObjects() { |
| 915 |
return enable(DETECT_VM_CURSOR_LEAKS); |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
public @NonNull Builder detectLeakedClosableObjects() { |
| 926 |
return enable(DETECT_VM_CLOSABLE_LEAKS); |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
public @NonNull Builder detectLeakedRegistrationObjects() { |
| 934 |
return enable(DETECT_VM_REGISTRATION_LEAKS); |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
public @NonNull Builder detectFileUriExposure() { |
| 953 |
return enable(DETECT_VM_FILE_URI_EXPOSURE); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
public @NonNull Builder detectCleartextNetwork() { |
| 972 |
return enable(DETECT_VM_CLEARTEXT_NETWORK); |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
public @NonNull Builder detectContentUriWithoutPermission() { |
| 988 |
return enable(DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION); |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
public @NonNull Builder detectUntaggedSockets() { |
| 1003 |
return enable(DETECT_VM_UNTAGGED_SOCKET); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
public @NonNull Builder permitUntaggedSockets() { |
| 1008 |
return disable(DETECT_VM_UNTAGGED_SOCKET); |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
public @NonNull Builder detectImplicitDirectBoot() { |
| 1026 |
return enable(DETECT_VM_IMPLICIT_DIRECT_BOOT); |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
public @NonNull Builder permitImplicitDirectBoot() { |
| 1031 |
return disable(DETECT_VM_IMPLICIT_DIRECT_BOOT); |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
public @NonNull Builder detectCredentialProtectedWhileLocked() { |
| 1047 |
return enable(DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED); |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
public @NonNull Builder permitCredentialProtectedWhileLocked() { |
| 1052 |
return disable(DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED); |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
public @NonNull Builder detectIncorrectContextUse() { |
| 1066 |
return enable(DETECT_VM_INCORRECT_CONTEXT_USE); |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
@TestApi |
| 1077 |
public @NonNull Builder permitIncorrectContextUse() { |
| 1078 |
return disable(DETECT_VM_INCORRECT_CONTEXT_USE); |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
public @NonNull Builder detectUnsafeIntentLaunch() { |
| 1116 |
return enable(DETECT_VM_UNSAFE_INTENT_LAUNCH); |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
public @NonNull Builder permitUnsafeIntentLaunch() { |
| 1131 |
return disable(DETECT_VM_UNSAFE_INTENT_LAUNCH); |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
public @NonNull Builder penaltyDeath() { |
| 1140 |
return enable(PENALTY_DEATH); |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
public @NonNull Builder penaltyDeathOnCleartextNetwork() { |
| 1149 |
return enable(PENALTY_DEATH_ON_CLEARTEXT_NETWORK); |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
public @NonNull Builder penaltyDeathOnFileUriExposure() { |
| 1159 |
return enable(PENALTY_DEATH_ON_FILE_URI_EXPOSURE); |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
public @NonNull Builder penaltyLog() { |
| 1164 |
return enable(PENALTY_LOG); |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
public @NonNull Builder penaltyDropBox() { |
| 1173 |
return enable(PENALTY_DROPBOX); |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
public @NonNull Builder penaltyListener( |
| 1180 |
@NonNull Executor executor, @NonNull OnVmViolationListener listener) { |
| 1181 |
if (executor == null) { |
| 1182 |
throw new NullPointerException("executor must not be null"); |
| 1183 |
} |
| 1184 |
mListener = listener; |
| 1185 |
mExecutor = executor; |
| 1186 |
return this; |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
public @NonNull Builder penaltyListener( |
| 1191 |
@NonNull OnVmViolationListener listener, @NonNull Executor executor) { |
| 1192 |
return penaltyListener(executor, listener); |
| 1193 |
} |
| 1194 |
|
| 1195 |
private Builder enable(@VmPolicyMask int mask) { |
| 1196 |
mMask |= mask; |
| 1197 |
return this; |
| 1198 |
} |
| 1199 |
|
| 1200 |
Builder disable(@VmPolicyMask int mask) { |
| 1201 |
mMask &= ~mask; |
| 1202 |
return this; |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
public VmPolicy build() { |
| 1212 |
|
| 1213 |
|
| 1214 |
if (mListener == null |
| 1215 |
&& mMask != 0 |
| 1216 |
&& (mMask |
| 1217 |
& (PENALTY_DEATH |
| 1218 |
| PENALTY_LOG |
| 1219 |
| PENALTY_DROPBOX |
| 1220 |
| PENALTY_DIALOG)) |
| 1221 |
== 0) { |
| 1222 |
penaltyLog(); |
| 1223 |
} |
| 1224 |
return new VmPolicy( |
| 1225 |
mMask, |
| 1226 |
mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP, |
| 1227 |
mListener, |
| 1228 |
mExecutor); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations = |
| 1239 |
new ThreadLocal<ArrayList<ViolationInfo>>() { |
| 1240 |
@Override |
| 1241 |
protected ArrayList<ViolationInfo> initialValue() { |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
return null; |
| 1246 |
} |
| 1247 |
}; |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
public static void setThreadPolicy(final ThreadPolicy policy) { |
| 1260 |
setThreadPolicyMask(policy.mask); |
| 1261 |
sThreadViolationListener.set(policy.mListener); |
| 1262 |
sThreadViolationExecutor.set(policy.mCallbackExecutor); |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
public static void setThreadPolicyMask(@ThreadPolicyMask int threadPolicyMask) { |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
setBlockGuardPolicy(threadPolicyMask); |
| 1273 |
|
| 1274 |
|
| 1275 |
Binder.setThreadStrictModePolicy(threadPolicyMask); |
| 1276 |
} |
| 1277 |
|
| 1278 |
|
| 1279 |
private static void setBlockGuardPolicy(@ThreadPolicyMask int threadPolicyMask) { |
| 1280 |
if (threadPolicyMask == 0) { |
| 1281 |
BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY); |
| 1282 |
return; |
| 1283 |
} |
| 1284 |
final BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 1285 |
final AndroidBlockGuardPolicy androidPolicy; |
| 1286 |
if (policy instanceof AndroidBlockGuardPolicy) { |
| 1287 |
androidPolicy = (AndroidBlockGuardPolicy) policy; |
| 1288 |
} else { |
| 1289 |
androidPolicy = THREAD_ANDROID_POLICY.get(); |
| 1290 |
BlockGuard.setThreadPolicy(androidPolicy); |
| 1291 |
} |
| 1292 |
androidPolicy.setThreadPolicyMask(threadPolicyMask); |
| 1293 |
} |
| 1294 |
|
| 1295 |
private static void setBlockGuardVmPolicy(@VmPolicyMask int vmPolicyMask) { |
| 1296 |
|
| 1297 |
vmPolicyMask &= DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED; |
| 1298 |
if (vmPolicyMask != 0) { |
| 1299 |
BlockGuard.setVmPolicy(VM_ANDROID_POLICY); |
| 1300 |
} else { |
| 1301 |
BlockGuard.setVmPolicy(BlockGuard.LAX_VM_POLICY); |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
private static void setCloseGuardEnabled(boolean enabled) { |
| 1307 |
if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) { |
| 1308 |
CloseGuard.setReporter(new AndroidCloseGuardReporter()); |
| 1309 |
} |
| 1310 |
CloseGuard.setEnabled(enabled); |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
@UnsupportedAppUsage |
| 1320 |
public static @ThreadPolicyMask int getThreadPolicyMask() { |
| 1321 |
final BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 1322 |
if (policy instanceof AndroidBlockGuardPolicy) { |
| 1323 |
return ((AndroidBlockGuardPolicy) policy).getThreadPolicyMask(); |
| 1324 |
} else { |
| 1325 |
return 0; |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
|
| 1330 |
public static ThreadPolicy getThreadPolicy() { |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
return new ThreadPolicy( |
| 1336 |
getThreadPolicyMask(), |
| 1337 |
sThreadViolationListener.get(), |
| 1338 |
sThreadViolationExecutor.get()); |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
public static ThreadPolicy allowThreadDiskWrites() { |
| 1351 |
return new ThreadPolicy( |
| 1352 |
allowThreadDiskWritesMask(), |
| 1353 |
sThreadViolationListener.get(), |
| 1354 |
sThreadViolationExecutor.get()); |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
public static @ThreadPolicyMask int allowThreadDiskWritesMask() { |
| 1359 |
int oldPolicyMask = getThreadPolicyMask(); |
| 1360 |
int newPolicyMask = oldPolicyMask & ~(DETECT_THREAD_DISK_WRITE | DETECT_THREAD_DISK_READ); |
| 1361 |
if (newPolicyMask != oldPolicyMask) { |
| 1362 |
setThreadPolicyMask(newPolicyMask); |
| 1363 |
} |
| 1364 |
return oldPolicyMask; |
| 1365 |
} |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
public static ThreadPolicy allowThreadDiskReads() { |
| 1375 |
return new ThreadPolicy( |
| 1376 |
allowThreadDiskReadsMask(), |
| 1377 |
sThreadViolationListener.get(), |
| 1378 |
sThreadViolationExecutor.get()); |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
public static @ThreadPolicyMask int allowThreadDiskReadsMask() { |
| 1383 |
int oldPolicyMask = getThreadPolicyMask(); |
| 1384 |
int newPolicyMask = oldPolicyMask & ~(DETECT_THREAD_DISK_READ); |
| 1385 |
if (newPolicyMask != oldPolicyMask) { |
| 1386 |
setThreadPolicyMask(newPolicyMask); |
| 1387 |
} |
| 1388 |
return oldPolicyMask; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
public static ThreadPolicy allowThreadViolations() { |
| 1393 |
ThreadPolicy oldPolicy = getThreadPolicy(); |
| 1394 |
setThreadPolicyMask(0); |
| 1395 |
return oldPolicy; |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
public static VmPolicy allowVmViolations() { |
| 1400 |
VmPolicy oldPolicy = getVmPolicy(); |
| 1401 |
sVmPolicy = VmPolicy.LAX; |
| 1402 |
return oldPolicy; |
| 1403 |
} |
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
public static boolean isBundledSystemApp(ApplicationInfo ai) { |
| 1418 |
if (ai == null || ai.packageName == null) { |
| 1419 |
|
| 1420 |
return true; |
| 1421 |
} else if (ai.isSystemApp()) { |
| 1422 |
|
| 1423 |
if (ai.packageName.equals("com.android.vending") |
| 1424 |
|| ai.packageName.equals("com.android.chrome")) { |
| 1425 |
return false; |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
if (ai.packageName.equals("com.android.phone")) { |
| 1431 |
return false; |
| 1432 |
} |
| 1433 |
|
| 1434 |
if (ai.packageName.equals("android") |
| 1435 |
|| ai.packageName.startsWith("android.") |
| 1436 |
|| ai.packageName.startsWith("com.android.")) { |
| 1437 |
return true; |
| 1438 |
} |
| 1439 |
} |
| 1440 |
return false; |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
public static void initThreadDefaults(ApplicationInfo ai) { |
| 1449 |
final ThreadPolicy.Builder builder = new ThreadPolicy.Builder(); |
| 1450 |
final int targetSdkVersion = |
| 1451 |
(ai != null) ? ai.targetSdkVersion : Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 1452 |
|
| 1453 |
|
| 1454 |
if (targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) { |
| 1455 |
builder.detectNetwork(); |
| 1456 |
builder.penaltyDeathOnNetwork(); |
| 1457 |
} |
| 1458 |
|
| 1459 |
if (Build.IS_USER || DISABLE || SystemProperties.getBoolean(DISABLE_PROPERTY, false)) { |
| 1460 |
|
| 1461 |
} else if (Build.IS_USERDEBUG) { |
| 1462 |
|
| 1463 |
if (isBundledSystemApp(ai)) { |
| 1464 |
builder.detectAll(); |
| 1465 |
builder.penaltyDropBox(); |
| 1466 |
if (SystemProperties.getBoolean(VISUAL_PROPERTY, false)) { |
| 1467 |
builder.penaltyFlashScreen(); |
| 1468 |
} |
| 1469 |
} |
| 1470 |
} else if (Build.IS_ENG) { |
| 1471 |
|
| 1472 |
if (isBundledSystemApp(ai)) { |
| 1473 |
builder.detectAll(); |
| 1474 |
builder.penaltyDropBox(); |
| 1475 |
builder.penaltyLog(); |
| 1476 |
builder.penaltyFlashScreen(); |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
setThreadPolicy(builder.build()); |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
public static void initVmDefaults(ApplicationInfo ai) { |
| 1489 |
final VmPolicy.Builder builder = new VmPolicy.Builder(); |
| 1490 |
final int targetSdkVersion = |
| 1491 |
(ai != null) ? ai.targetSdkVersion : Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 1492 |
|
| 1493 |
|
| 1494 |
if (targetSdkVersion >= Build.VERSION_CODES.N) { |
| 1495 |
builder.detectFileUriExposure(); |
| 1496 |
builder.penaltyDeathOnFileUriExposure(); |
| 1497 |
} |
| 1498 |
|
| 1499 |
if (Build.IS_USER || DISABLE || SystemProperties.getBoolean(DISABLE_PROPERTY, false)) { |
| 1500 |
|
| 1501 |
} else if (Build.IS_USERDEBUG) { |
| 1502 |
|
| 1503 |
|
| 1504 |
if (isBundledSystemApp(ai)) { |
| 1505 |
builder.detectAll(); |
| 1506 |
builder.permitActivityLeaks(); |
| 1507 |
builder.penaltyDropBox(); |
| 1508 |
} |
| 1509 |
} else if (Build.IS_ENG) { |
| 1510 |
|
| 1511 |
if (isBundledSystemApp(ai)) { |
| 1512 |
builder.detectAll(); |
| 1513 |
builder.penaltyDropBox(); |
| 1514 |
builder.penaltyLog(); |
| 1515 |
} |
| 1516 |
} |
| 1517 |
|
| 1518 |
setVmPolicy(builder.build()); |
| 1519 |
} |
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
@UnsupportedAppUsage |
| 1527 |
public static void enableDeathOnFileUriExposure() { |
| 1528 |
sVmPolicy = |
| 1529 |
new VmPolicy( |
| 1530 |
sVmPolicy.mask |
| 1531 |
| DETECT_VM_FILE_URI_EXPOSURE |
| 1532 |
| PENALTY_DEATH_ON_FILE_URI_EXPOSURE, |
| 1533 |
sVmPolicy.classInstanceLimit, |
| 1534 |
sVmPolicy.mListener, |
| 1535 |
sVmPolicy.mCallbackExecutor); |
| 1536 |
} |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
@UnsupportedAppUsage |
| 1545 |
public static void disableDeathOnFileUriExposure() { |
| 1546 |
sVmPolicy = |
| 1547 |
new VmPolicy( |
| 1548 |
sVmPolicy.mask |
| 1549 |
& ~(DETECT_VM_FILE_URI_EXPOSURE |
| 1550 |
| PENALTY_DEATH_ON_FILE_URI_EXPOSURE), |
| 1551 |
sVmPolicy.classInstanceLimit, |
| 1552 |
sVmPolicy.mListener, |
| 1553 |
sVmPolicy.mCallbackExecutor); |
| 1554 |
} |
| 1555 |
|
| 1556 |
@UnsupportedAppUsage |
| 1557 |
private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed = |
| 1558 |
new ThreadLocal<ArrayList<ViolationInfo>>() { |
| 1559 |
@Override |
| 1560 |
protected ArrayList<ViolationInfo> initialValue() { |
| 1561 |
return new ArrayList<ViolationInfo>(); |
| 1562 |
} |
| 1563 |
}; |
| 1564 |
|
| 1565 |
|
| 1566 |
private static final ThreadLocal<Handler> THREAD_HANDLER = |
| 1567 |
new ThreadLocal<Handler>() { |
| 1568 |
@Override |
| 1569 |
protected Handler initialValue() { |
| 1570 |
return new Handler(); |
| 1571 |
} |
| 1572 |
}; |
| 1573 |
|
| 1574 |
private static final ThreadLocal<AndroidBlockGuardPolicy> THREAD_ANDROID_POLICY = |
| 1575 |
new ThreadLocal<AndroidBlockGuardPolicy>() { |
| 1576 |
@Override |
| 1577 |
protected AndroidBlockGuardPolicy initialValue() { |
| 1578 |
return new AndroidBlockGuardPolicy(0); |
| 1579 |
} |
| 1580 |
}; |
| 1581 |
|
| 1582 |
private static boolean tooManyViolationsThisLoop() { |
| 1583 |
return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP; |
| 1584 |
} |
| 1585 |
|
| 1586 |
private static class AndroidBlockGuardPolicy implements BlockGuard.Policy { |
| 1587 |
private @ThreadPolicyMask int mThreadPolicyMask; |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
private ArrayMap<Integer, Long> mLastViolationTime; |
| 1594 |
private SparseLongArray mRealLastViolationTime; |
| 1595 |
|
| 1596 |
public AndroidBlockGuardPolicy(@ThreadPolicyMask int threadPolicyMask) { |
| 1597 |
mThreadPolicyMask = threadPolicyMask; |
| 1598 |
} |
| 1599 |
|
| 1600 |
@Override |
| 1601 |
public String toString() { |
| 1602 |
return "AndroidBlockGuardPolicy; mPolicyMask=" + mThreadPolicyMask; |
| 1603 |
} |
| 1604 |
|
| 1605 |
|
| 1606 |
public int getPolicyMask() { |
| 1607 |
return mThreadPolicyMask; |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
public void onWriteToDisk() { |
| 1612 |
if ((mThreadPolicyMask & DETECT_THREAD_DISK_WRITE) == 0) { |
| 1613 |
return; |
| 1614 |
} |
| 1615 |
if (tooManyViolationsThisLoop()) { |
| 1616 |
return; |
| 1617 |
} |
| 1618 |
startHandlingViolationException(new DiskWriteViolation()); |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
void onCustomSlowCall(String name) { |
| 1623 |
if ((mThreadPolicyMask & DETECT_THREAD_CUSTOM) == 0) { |
| 1624 |
return; |
| 1625 |
} |
| 1626 |
if (tooManyViolationsThisLoop()) { |
| 1627 |
return; |
| 1628 |
} |
| 1629 |
startHandlingViolationException(new CustomViolation(name)); |
| 1630 |
} |
| 1631 |
|
| 1632 |
|
| 1633 |
void onResourceMismatch(Object tag) { |
| 1634 |
if ((mThreadPolicyMask & DETECT_THREAD_RESOURCE_MISMATCH) == 0) { |
| 1635 |
return; |
| 1636 |
} |
| 1637 |
if (tooManyViolationsThisLoop()) { |
| 1638 |
return; |
| 1639 |
} |
| 1640 |
startHandlingViolationException(new ResourceMismatchViolation(tag)); |
| 1641 |
} |
| 1642 |
|
| 1643 |
|
| 1644 |
public void onUnbufferedIO() { |
| 1645 |
if ((mThreadPolicyMask & DETECT_THREAD_UNBUFFERED_IO) == 0) { |
| 1646 |
return; |
| 1647 |
} |
| 1648 |
if (tooManyViolationsThisLoop()) { |
| 1649 |
return; |
| 1650 |
} |
| 1651 |
startHandlingViolationException(new UnbufferedIoViolation()); |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
public void onReadFromDisk() { |
| 1656 |
if ((mThreadPolicyMask & DETECT_THREAD_DISK_READ) == 0) { |
| 1657 |
return; |
| 1658 |
} |
| 1659 |
if (tooManyViolationsThisLoop()) { |
| 1660 |
return; |
| 1661 |
} |
| 1662 |
startHandlingViolationException(new DiskReadViolation()); |
| 1663 |
} |
| 1664 |
|
| 1665 |
|
| 1666 |
public void onNetwork() { |
| 1667 |
if ((mThreadPolicyMask & DETECT_THREAD_NETWORK) == 0) { |
| 1668 |
return; |
| 1669 |
} |
| 1670 |
if ((mThreadPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) { |
| 1671 |
throw new NetworkOnMainThreadException(); |
| 1672 |
} |
| 1673 |
if (tooManyViolationsThisLoop()) { |
| 1674 |
return; |
| 1675 |
} |
| 1676 |
startHandlingViolationException(new NetworkViolation()); |
| 1677 |
} |
| 1678 |
|
| 1679 |
|
| 1680 |
public void onExplicitGc() { |
| 1681 |
if ((mThreadPolicyMask & DETECT_THREAD_EXPLICIT_GC) == 0) { |
| 1682 |
return; |
| 1683 |
} |
| 1684 |
if (tooManyViolationsThisLoop()) { |
| 1685 |
return; |
| 1686 |
} |
| 1687 |
startHandlingViolationException(new ExplicitGcViolation()); |
| 1688 |
} |
| 1689 |
|
| 1690 |
public @ThreadPolicyMask int getThreadPolicyMask() { |
| 1691 |
return mThreadPolicyMask; |
| 1692 |
} |
| 1693 |
|
| 1694 |
public void setThreadPolicyMask(@ThreadPolicyMask int threadPolicyMask) { |
| 1695 |
mThreadPolicyMask = threadPolicyMask; |
| 1696 |
} |
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
void startHandlingViolationException(Violation e) { |
| 1704 |
final int penaltyMask = (mThreadPolicyMask & PENALTY_ALL); |
| 1705 |
final ViolationInfo info = new ViolationInfo(e, penaltyMask); |
| 1706 |
info.violationUptimeMillis = SystemClock.uptimeMillis(); |
| 1707 |
handleViolationWithTimingAttempt(info); |
| 1708 |
} |
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
void handleViolationWithTimingAttempt(final ViolationInfo info) { |
| 1716 |
Looper looper = Looper.myLooper(); |
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
if (looper == null || (info.mPenaltyMask == PENALTY_DEATH)) { |
| 1735 |
info.durationMillis = -1; |
| 1736 |
onThreadPolicyViolation(info); |
| 1737 |
return; |
| 1738 |
} |
| 1739 |
|
| 1740 |
final ArrayList<ViolationInfo> records = violationsBeingTimed.get(); |
| 1741 |
if (records.size() >= MAX_OFFENSES_PER_LOOP) { |
| 1742 |
|
| 1743 |
return; |
| 1744 |
} |
| 1745 |
records.add(info); |
| 1746 |
if (records.size() > 1) { |
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
return; |
| 1751 |
} |
| 1752 |
|
| 1753 |
final IWindowManager windowManager = |
| 1754 |
info.penaltyEnabled(PENALTY_FLASH) ? sWindowManager.get() : null; |
| 1755 |
if (windowManager != null) { |
| 1756 |
try { |
| 1757 |
windowManager.showStrictModeViolation(true); |
| 1758 |
} catch (RemoteException unused) { |
| 1759 |
} |
| 1760 |
} |
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
THREAD_HANDLER |
| 1772 |
.get() |
| 1773 |
.postAtFrontOfQueue( |
| 1774 |
() -> { |
| 1775 |
long loopFinishTime = SystemClock.uptimeMillis(); |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
if (windowManager != null) { |
| 1782 |
try { |
| 1783 |
windowManager.showStrictModeViolation(false); |
| 1784 |
} catch (RemoteException unused) { |
| 1785 |
} |
| 1786 |
} |
| 1787 |
|
| 1788 |
for (int n = 0; n < records.size(); ++n) { |
| 1789 |
ViolationInfo v = records.get(n); |
| 1790 |
v.violationNumThisLoop = n + 1; |
| 1791 |
v.durationMillis = |
| 1792 |
(int) (loopFinishTime - v.violationUptimeMillis); |
| 1793 |
onThreadPolicyViolation(v); |
| 1794 |
} |
| 1795 |
records.clear(); |
| 1796 |
}); |
| 1797 |
} |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
void onThreadPolicyViolation(final ViolationInfo info) { |
| 1805 |
if (LOG_V) Log.d(TAG, "onThreadPolicyViolation; penalty=" + info.mPenaltyMask); |
| 1806 |
|
| 1807 |
if (info.penaltyEnabled(PENALTY_GATHER)) { |
| 1808 |
ArrayList<ViolationInfo> violations = gatheredViolations.get(); |
| 1809 |
if (violations == null) { |
| 1810 |
violations = new ArrayList<>(1); |
| 1811 |
gatheredViolations.set(violations); |
| 1812 |
} |
| 1813 |
for (ViolationInfo previous : violations) { |
| 1814 |
if (info.getStackTrace().equals(previous.getStackTrace())) { |
| 1815 |
|
| 1816 |
return; |
| 1817 |
} |
| 1818 |
} |
| 1819 |
violations.add(info); |
| 1820 |
return; |
| 1821 |
} |
| 1822 |
|
| 1823 |
|
| 1824 |
Integer crashFingerprint = info.hashCode(); |
| 1825 |
long lastViolationTime = 0; |
| 1826 |
long now = SystemClock.uptimeMillis(); |
| 1827 |
if (sLogger == LOGCAT_LOGGER) { |
| 1828 |
if (mRealLastViolationTime != null) { |
| 1829 |
Long vtime = mRealLastViolationTime.get(crashFingerprint); |
| 1830 |
if (vtime != null) { |
| 1831 |
lastViolationTime = vtime; |
| 1832 |
} |
| 1833 |
clampViolationTimeMap(mRealLastViolationTime, Math.max(MIN_LOG_INTERVAL_MS, |
| 1834 |
Math.max(MIN_DIALOG_INTERVAL_MS, MIN_DROPBOX_INTERVAL_MS))); |
| 1835 |
} else { |
| 1836 |
mRealLastViolationTime = new SparseLongArray(1); |
| 1837 |
} |
| 1838 |
mRealLastViolationTime.put(crashFingerprint, now); |
| 1839 |
} |
| 1840 |
long timeSinceLastViolationMillis = |
| 1841 |
lastViolationTime == 0 ? Long.MAX_VALUE : (now - lastViolationTime); |
| 1842 |
|
| 1843 |
if (info.penaltyEnabled(PENALTY_LOG) |
| 1844 |
&& timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { |
| 1845 |
sLogger.log(info); |
| 1846 |
} |
| 1847 |
|
| 1848 |
final Violation violation = info.mViolation; |
| 1849 |
|
| 1850 |
|
| 1851 |
int penaltyMask = 0; |
| 1852 |
|
| 1853 |
if (info.penaltyEnabled(PENALTY_DIALOG) |
| 1854 |
&& timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) { |
| 1855 |
penaltyMask |= PENALTY_DIALOG; |
| 1856 |
} |
| 1857 |
|
| 1858 |
if (info.penaltyEnabled(PENALTY_DROPBOX) |
| 1859 |
&& timeSinceLastViolationMillis > MIN_DROPBOX_INTERVAL_MS) { |
| 1860 |
penaltyMask |= PENALTY_DROPBOX; |
| 1861 |
} |
| 1862 |
|
| 1863 |
if (penaltyMask != 0) { |
| 1864 |
final boolean justDropBox = (info.mPenaltyMask == PENALTY_DROPBOX); |
| 1865 |
if (justDropBox) { |
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
dropboxViolationAsync(penaltyMask, info); |
| 1873 |
} else { |
| 1874 |
handleApplicationStrictModeViolation(penaltyMask, info); |
| 1875 |
} |
| 1876 |
} |
| 1877 |
|
| 1878 |
if (info.penaltyEnabled(PENALTY_DEATH)) { |
| 1879 |
throw new RuntimeException("StrictMode ThreadPolicy violation", violation); |
| 1880 |
} |
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
final OnThreadViolationListener listener = sThreadViolationListener.get(); |
| 1885 |
final Executor executor = sThreadViolationExecutor.get(); |
| 1886 |
if (listener != null && executor != null) { |
| 1887 |
try { |
| 1888 |
executor.execute( |
| 1889 |
() -> { |
| 1890 |
|
| 1891 |
ThreadPolicy oldPolicy = StrictMode.allowThreadViolations(); |
| 1892 |
try { |
| 1893 |
listener.onThreadViolation(violation); |
| 1894 |
} finally { |
| 1895 |
StrictMode.setThreadPolicy(oldPolicy); |
| 1896 |
} |
| 1897 |
}); |
| 1898 |
} catch (RejectedExecutionException e) { |
| 1899 |
Log.e(TAG, "ThreadPolicy penaltyCallback failed", e); |
| 1900 |
} |
| 1901 |
} |
| 1902 |
} |
| 1903 |
} |
| 1904 |
|
| 1905 |
private static final BlockGuard.VmPolicy VM_ANDROID_POLICY = new BlockGuard.VmPolicy() { |
| 1906 |
@Override |
| 1907 |
public void onPathAccess(String path) { |
| 1908 |
if (path == null) return; |
| 1909 |
|
| 1910 |
|
| 1911 |
if (path.startsWith("/data/user/") |
| 1912 |
|| path.startsWith("/data/media/") |
| 1913 |
|| path.startsWith("/data/system_ce/") |
| 1914 |
|| path.startsWith("/data/misc_ce/") |
| 1915 |
|| path.startsWith("/data/vendor_ce/") |
| 1916 |
|| path.startsWith("/storage/emulated/")) { |
| 1917 |
final int second = path.indexOf('/', 1); |
| 1918 |
final int third = path.indexOf('/', second + 1); |
| 1919 |
final int fourth = path.indexOf('/', third + 1); |
| 1920 |
if (fourth == -1) return; |
| 1921 |
|
| 1922 |
try { |
| 1923 |
final int userId = Integer.parseInt(path.substring(third + 1, fourth)); |
| 1924 |
onCredentialProtectedPathAccess(path, userId); |
| 1925 |
} catch (NumberFormatException ignored) { |
| 1926 |
} |
| 1927 |
} else if (path.startsWith("/data/data/")) { |
| 1928 |
onCredentialProtectedPathAccess(path, UserHandle.USER_SYSTEM); |
| 1929 |
} |
| 1930 |
} |
| 1931 |
}; |
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
private static void dropboxViolationAsync( |
| 1940 |
final int penaltyMask, final ViolationInfo info) { |
| 1941 |
int outstanding = sDropboxCallsInFlight.incrementAndGet(); |
| 1942 |
if (outstanding > 20) { |
| 1943 |
|
| 1944 |
|
| 1945 |
sDropboxCallsInFlight.decrementAndGet(); |
| 1946 |
return; |
| 1947 |
} |
| 1948 |
|
| 1949 |
if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding); |
| 1950 |
|
| 1951 |
BackgroundThread.getHandler().post(() -> { |
| 1952 |
handleApplicationStrictModeViolation(penaltyMask, info); |
| 1953 |
int outstandingInner = sDropboxCallsInFlight.decrementAndGet(); |
| 1954 |
if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstandingInner); |
| 1955 |
}); |
| 1956 |
} |
| 1957 |
|
| 1958 |
private static void handleApplicationStrictModeViolation(int penaltyMask, |
| 1959 |
ViolationInfo info) { |
| 1960 |
final int oldMask = getThreadPolicyMask(); |
| 1961 |
try { |
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
setThreadPolicyMask(0); |
| 1967 |
|
| 1968 |
IActivityManager am = ActivityManager.getService(); |
| 1969 |
if (am == null) { |
| 1970 |
Log.w(TAG, "No activity manager; failed to Dropbox violation."); |
| 1971 |
} else { |
| 1972 |
am.handleApplicationStrictModeViolation( |
| 1973 |
RuntimeInit.getApplicationObject(), penaltyMask, info); |
| 1974 |
} |
| 1975 |
} catch (RemoteException e) { |
| 1976 |
if (e instanceof DeadObjectException) { |
| 1977 |
|
| 1978 |
} else { |
| 1979 |
Log.e(TAG, "RemoteException handling StrictMode violation", e); |
| 1980 |
} |
| 1981 |
} finally { |
| 1982 |
setThreadPolicyMask(oldMask); |
| 1983 |
} |
| 1984 |
} |
| 1985 |
|
| 1986 |
private static class AndroidCloseGuardReporter implements CloseGuard.Reporter { |
| 1987 |
|
| 1988 |
@Override |
| 1989 |
public void report(String message, Throwable allocationSite) { |
| 1990 |
onVmPolicyViolation(new LeakedClosableViolation(message, allocationSite)); |
| 1991 |
} |
| 1992 |
|
| 1993 |
@Override |
| 1994 |
public void report(String message) { |
| 1995 |
onVmPolicyViolation(new LeakedClosableViolation(message)); |
| 1996 |
} |
| 1997 |
} |
| 1998 |
|
| 1999 |
|
| 2000 |
static boolean hasGatheredViolations() { |
| 2001 |
return gatheredViolations.get() != null; |
| 2002 |
} |
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
static void clearGatheredViolations() { |
| 2009 |
gatheredViolations.set(null); |
| 2010 |
} |
| 2011 |
|
| 2012 |
|
| 2013 |
@UnsupportedAppUsage |
| 2014 |
@TestApi |
| 2015 |
public static void conditionallyCheckInstanceCounts() { |
| 2016 |
VmPolicy policy = getVmPolicy(); |
| 2017 |
int policySize = policy.classInstanceLimit.size(); |
| 2018 |
if (policySize == 0) { |
| 2019 |
return; |
| 2020 |
} |
| 2021 |
|
| 2022 |
System.gc(); |
| 2023 |
System.runFinalization(); |
| 2024 |
System.gc(); |
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
Class[] classes = policy.classInstanceLimit.keySet().toArray(new Class[policySize]); |
| 2029 |
long[] instanceCounts = VMDebug.countInstancesOfClasses(classes, false); |
| 2030 |
for (int i = 0; i < classes.length; ++i) { |
| 2031 |
Class klass = classes[i]; |
| 2032 |
int limit = policy.classInstanceLimit.get(klass); |
| 2033 |
long instances = instanceCounts[i]; |
| 2034 |
if (instances > limit) { |
| 2035 |
onVmPolicyViolation(new InstanceCountViolation(klass, instances, limit)); |
| 2036 |
} |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
private static long sLastInstanceCountCheckMillis = 0; |
| 2041 |
private static boolean sIsIdlerRegistered = false; |
| 2042 |
private static final MessageQueue.IdleHandler sProcessIdleHandler = |
| 2043 |
new MessageQueue.IdleHandler() { |
| 2044 |
public boolean queueIdle() { |
| 2045 |
long now = SystemClock.uptimeMillis(); |
| 2046 |
if (now - sLastInstanceCountCheckMillis > 30 * 1000) { |
| 2047 |
sLastInstanceCountCheckMillis = now; |
| 2048 |
conditionallyCheckInstanceCounts(); |
| 2049 |
} |
| 2050 |
return true; |
| 2051 |
} |
| 2052 |
}; |
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
public static void setVmPolicy(final VmPolicy policy) { |
| 2061 |
synchronized (StrictMode.class) { |
| 2062 |
sVmPolicy = policy; |
| 2063 |
setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); |
| 2064 |
|
| 2065 |
Looper looper = Looper.getMainLooper(); |
| 2066 |
if (looper != null) { |
| 2067 |
MessageQueue mq = looper.mQueue; |
| 2068 |
if (policy.classInstanceLimit.size() == 0 |
| 2069 |
|| (sVmPolicy.mask & PENALTY_ALL) == 0) { |
| 2070 |
mq.removeIdleHandler(sProcessIdleHandler); |
| 2071 |
sIsIdlerRegistered = false; |
| 2072 |
} else if (!sIsIdlerRegistered) { |
| 2073 |
mq.addIdleHandler(sProcessIdleHandler); |
| 2074 |
sIsIdlerRegistered = true; |
| 2075 |
} |
| 2076 |
} |
| 2077 |
|
| 2078 |
int networkPolicy = NETWORK_POLICY_ACCEPT; |
| 2079 |
if ((sVmPolicy.mask & DETECT_VM_CLEARTEXT_NETWORK) != 0) { |
| 2080 |
if ((sVmPolicy.mask & PENALTY_DEATH) != 0 |
| 2081 |
|| (sVmPolicy.mask & PENALTY_DEATH_ON_CLEARTEXT_NETWORK) != 0) { |
| 2082 |
networkPolicy = NETWORK_POLICY_REJECT; |
| 2083 |
} else { |
| 2084 |
networkPolicy = NETWORK_POLICY_LOG; |
| 2085 |
} |
| 2086 |
} |
| 2087 |
|
| 2088 |
final INetworkManagementService netd = |
| 2089 |
INetworkManagementService.Stub.asInterface( |
| 2090 |
ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); |
| 2091 |
if (netd != null) { |
| 2092 |
try { |
| 2093 |
netd.setUidCleartextNetworkPolicy(android.os.Process.myUid(), networkPolicy); |
| 2094 |
} catch (RemoteException ignored) { |
| 2095 |
} |
| 2096 |
} else if (networkPolicy != NETWORK_POLICY_ACCEPT) { |
| 2097 |
Log.w(TAG, "Dropping requested network policy due to missing service!"); |
| 2098 |
} |
| 2099 |
|
| 2100 |
|
| 2101 |
if ((sVmPolicy.mask & DETECT_VM_NON_SDK_API_USAGE) != 0) { |
| 2102 |
VMRuntime.setNonSdkApiUsageConsumer(sNonSdkApiUsageConsumer); |
| 2103 |
VMRuntime.setDedupeHiddenApiWarnings(false); |
| 2104 |
} else { |
| 2105 |
VMRuntime.setNonSdkApiUsageConsumer(null); |
| 2106 |
VMRuntime.setDedupeHiddenApiWarnings(true); |
| 2107 |
} |
| 2108 |
|
| 2109 |
setBlockGuardVmPolicy(sVmPolicy.mask); |
| 2110 |
} |
| 2111 |
} |
| 2112 |
|
| 2113 |
|
| 2114 |
public static VmPolicy getVmPolicy() { |
| 2115 |
synchronized (StrictMode.class) { |
| 2116 |
return sVmPolicy; |
| 2117 |
} |
| 2118 |
} |
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
public static void enableDefaults() { |
| 2128 |
setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); |
| 2129 |
setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build()); |
| 2130 |
} |
| 2131 |
|
| 2132 |
|
| 2133 |
public static boolean vmSqliteObjectLeaksEnabled() { |
| 2134 |
return (sVmPolicy.mask & DETECT_VM_CURSOR_LEAKS) != 0; |
| 2135 |
} |
| 2136 |
|
| 2137 |
|
| 2138 |
public static boolean vmClosableObjectLeaksEnabled() { |
| 2139 |
return (sVmPolicy.mask & DETECT_VM_CLOSABLE_LEAKS) != 0; |
| 2140 |
} |
| 2141 |
|
| 2142 |
|
| 2143 |
public static boolean vmRegistrationLeaksEnabled() { |
| 2144 |
return (sVmPolicy.mask & DETECT_VM_REGISTRATION_LEAKS) != 0; |
| 2145 |
} |
| 2146 |
|
| 2147 |
|
| 2148 |
public static boolean vmFileUriExposureEnabled() { |
| 2149 |
return (sVmPolicy.mask & DETECT_VM_FILE_URI_EXPOSURE) != 0; |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
public static boolean vmCleartextNetworkEnabled() { |
| 2154 |
return (sVmPolicy.mask & DETECT_VM_CLEARTEXT_NETWORK) != 0; |
| 2155 |
} |
| 2156 |
|
| 2157 |
|
| 2158 |
public static boolean vmContentUriWithoutPermissionEnabled() { |
| 2159 |
return (sVmPolicy.mask & DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION) != 0; |
| 2160 |
} |
| 2161 |
|
| 2162 |
|
| 2163 |
public static boolean vmUntaggedSocketEnabled() { |
| 2164 |
return (sVmPolicy.mask & DETECT_VM_UNTAGGED_SOCKET) != 0; |
| 2165 |
} |
| 2166 |
|
| 2167 |
|
| 2168 |
public static boolean vmImplicitDirectBootEnabled() { |
| 2169 |
return (sVmPolicy.mask & DETECT_VM_IMPLICIT_DIRECT_BOOT) != 0; |
| 2170 |
} |
| 2171 |
|
| 2172 |
|
| 2173 |
public static boolean vmCredentialProtectedWhileLockedEnabled() { |
| 2174 |
return (sVmPolicy.mask & DETECT_VM_CREDENTIAL_PROTECTED_WHILE_LOCKED) != 0; |
| 2175 |
} |
| 2176 |
|
| 2177 |
|
| 2178 |
public static boolean vmIncorrectContextUseEnabled() { |
| 2179 |
return (sVmPolicy.mask & DETECT_VM_INCORRECT_CONTEXT_USE) != 0; |
| 2180 |
} |
| 2181 |
|
| 2182 |
|
| 2183 |
public static boolean vmUnsafeIntentLaunchEnabled() { |
| 2184 |
return (sVmPolicy.mask & DETECT_VM_UNSAFE_INTENT_LAUNCH) != 0; |
| 2185 |
} |
| 2186 |
|
| 2187 |
|
| 2188 |
public static void onSqliteObjectLeaked(String message, Throwable originStack) { |
| 2189 |
onVmPolicyViolation(new SqliteObjectLeakedViolation(message, originStack)); |
| 2190 |
} |
| 2191 |
|
| 2192 |
|
| 2193 |
@UnsupportedAppUsage |
| 2194 |
public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) { |
| 2195 |
onVmPolicyViolation(new WebViewMethodCalledOnWrongThreadViolation(originStack)); |
| 2196 |
} |
| 2197 |
|
| 2198 |
|
| 2199 |
public static void onIntentReceiverLeaked(Throwable originStack) { |
| 2200 |
onVmPolicyViolation(new IntentReceiverLeakedViolation(originStack)); |
| 2201 |
} |
| 2202 |
|
| 2203 |
|
| 2204 |
public static void onServiceConnectionLeaked(Throwable originStack) { |
| 2205 |
onVmPolicyViolation(new ServiceConnectionLeakedViolation(originStack)); |
| 2206 |
} |
| 2207 |
|
| 2208 |
|
| 2209 |
public static void onFileUriExposed(Uri uri, String location) { |
| 2210 |
final String message = uri + " exposed beyond app through " + location; |
| 2211 |
if ((sVmPolicy.mask & PENALTY_DEATH_ON_FILE_URI_EXPOSURE) != 0) { |
| 2212 |
throw new FileUriExposedException(message); |
| 2213 |
} else { |
| 2214 |
onVmPolicyViolation(new FileUriExposedViolation(message)); |
| 2215 |
} |
| 2216 |
} |
| 2217 |
|
| 2218 |
|
| 2219 |
public static void onContentUriWithoutPermission(Uri uri, String location) { |
| 2220 |
onVmPolicyViolation(new ContentUriWithoutPermissionViolation(uri, location)); |
| 2221 |
} |
| 2222 |
|
| 2223 |
|
| 2224 |
public static void onCleartextNetworkDetected(byte[] firstPacket) { |
| 2225 |
byte[] rawAddr = null; |
| 2226 |
if (firstPacket != null) { |
| 2227 |
if (firstPacket.length >= 20 && (firstPacket[0] & 0xf0) == 0x40) { |
| 2228 |
|
| 2229 |
rawAddr = new byte[4]; |
| 2230 |
System.arraycopy(firstPacket, 16, rawAddr, 0, 4); |
| 2231 |
} else if (firstPacket.length >= 40 && (firstPacket[0] & 0xf0) == 0x60) { |
| 2232 |
|
| 2233 |
rawAddr = new byte[16]; |
| 2234 |
System.arraycopy(firstPacket, 24, rawAddr, 0, 16); |
| 2235 |
} |
| 2236 |
} |
| 2237 |
|
| 2238 |
final int uid = android.os.Process.myUid(); |
| 2239 |
final StringBuilder msg = new StringBuilder("Detected cleartext network traffic from UID ") |
| 2240 |
.append(uid); |
| 2241 |
if (rawAddr != null) { |
| 2242 |
try { |
| 2243 |
msg.append(" to ").append(InetAddress.getByAddress(rawAddr)); |
| 2244 |
} catch (UnknownHostException ignored) { |
| 2245 |
} |
| 2246 |
} |
| 2247 |
msg.append(HexDump.dumpHexString(firstPacket).trim()).append(' '); |
| 2248 |
final boolean forceDeath = (sVmPolicy.mask & PENALTY_DEATH_ON_CLEARTEXT_NETWORK) != 0; |
| 2249 |
onVmPolicyViolation(new CleartextNetworkViolation(msg.toString()), forceDeath); |
| 2250 |
} |
| 2251 |
|
| 2252 |
|
| 2253 |
public static void onUntaggedSocket() { |
| 2254 |
onVmPolicyViolation(new UntaggedSocketViolation()); |
| 2255 |
} |
| 2256 |
|
| 2257 |
|
| 2258 |
public static void onImplicitDirectBoot() { |
| 2259 |
onVmPolicyViolation(new ImplicitDirectBootViolation()); |
| 2260 |
} |
| 2261 |
|
| 2262 |
|
| 2263 |
public static void onIncorrectContextUsed(String message, Throwable originStack) { |
| 2264 |
onVmPolicyViolation(new IncorrectContextUseViolation(message, originStack)); |
| 2265 |
} |
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
public static void assertConfigurationContext(@NonNull Context context, |
| 2291 |
@NonNull String methodName) { |
| 2292 |
if (vmIncorrectContextUseEnabled() && !context.isConfigurationContext()) { |
| 2293 |
final String errorMessage = "Tried to access the API:" + methodName + " which needs to" |
| 2294 |
+ " have proper configuration from a non-UI Context:" + context; |
| 2295 |
final String message = "The API:" + methodName + " needs a proper configuration." |
| 2296 |
+ " Use UI contexts such as an activity or a context created" |
| 2297 |
+ " via createWindowContext(Display, int, Bundle) or " |
| 2298 |
+ " createConfigurationContext(Configuration) with a proper configuration."; |
| 2299 |
final Exception exception = new IllegalAccessException(errorMessage); |
| 2300 |
StrictMode.onIncorrectContextUsed(message, exception); |
| 2301 |
Log.e(TAG, errorMessage + " " + message, exception); |
| 2302 |
} |
| 2303 |
} |
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
public static void assertUiContext(@NonNull Context context, @NonNull String methodName) { |
| 2318 |
if (vmIncorrectContextUseEnabled() && !context.isUiContext()) { |
| 2319 |
final String errorMessage = "Tried to access UI related API:" + methodName |
| 2320 |
+ " from a non-UI Context:" + context; |
| 2321 |
final String message = methodName + " should be accessed from Activity or other UI " |
| 2322 |
+ "Contexts. Use an Activity or a Context created with " |
| 2323 |
+ "Context#createWindowContext(int, Bundle), which are adjusted to " |
| 2324 |
+ "the configuration and visual bounds of an area on screen."; |
| 2325 |
final Exception exception = new IllegalAccessException(errorMessage); |
| 2326 |
StrictMode.onIncorrectContextUsed(message, exception); |
| 2327 |
Log.e(TAG, errorMessage + " " + message, exception); |
| 2328 |
} |
| 2329 |
} |
| 2330 |
|
| 2331 |
|
| 2332 |
public static void onUnsafeIntentLaunch(Intent intent) { |
| 2333 |
onVmPolicyViolation(new UnsafeIntentLaunchViolation(intent)); |
| 2334 |
} |
| 2335 |
|
| 2336 |
|
| 2337 |
private static volatile boolean sUserKeyUnlocked = false; |
| 2338 |
|
| 2339 |
private static boolean isUserKeyUnlocked(int userId) { |
| 2340 |
final IStorageManager storage = IStorageManager.Stub |
| 2341 |
.asInterface(ServiceManager.getService("mount")); |
| 2342 |
if (storage != null) { |
| 2343 |
try { |
| 2344 |
return storage.isUserKeyUnlocked(userId); |
| 2345 |
} catch (RemoteException ignored) { |
| 2346 |
} |
| 2347 |
} |
| 2348 |
return false; |
| 2349 |
} |
| 2350 |
|
| 2351 |
|
| 2352 |
private static void onCredentialProtectedPathAccess(String path, int userId) { |
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
if (userId == UserHandle.myUserId()) { |
| 2357 |
if (sUserKeyUnlocked) { |
| 2358 |
return; |
| 2359 |
} else if (isUserKeyUnlocked(userId)) { |
| 2360 |
sUserKeyUnlocked = true; |
| 2361 |
return; |
| 2362 |
} |
| 2363 |
} else if (isUserKeyUnlocked(userId)) { |
| 2364 |
return; |
| 2365 |
} |
| 2366 |
|
| 2367 |
onVmPolicyViolation(new CredentialProtectedWhileLockedViolation( |
| 2368 |
"Accessed credential protected path " + path + " while user " + userId |
| 2369 |
+ " was locked")); |
| 2370 |
} |
| 2371 |
|
| 2372 |
|
| 2373 |
@UnsupportedAppUsage |
| 2374 |
private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<>(); |
| 2375 |
private static final SparseLongArray sRealLastVmViolationTime = new SparseLongArray(); |
| 2376 |
|
| 2377 |
|
| 2378 |
|
| 2379 |
|
| 2380 |
private static void clampViolationTimeMap(final @NonNull SparseLongArray violationTime, |
| 2381 |
final long retainSince) { |
| 2382 |
for (int i = 0; i < violationTime.size(); ) { |
| 2383 |
if (violationTime.valueAt(i) < retainSince) { |
| 2384 |
|
| 2385 |
violationTime.removeAt(i); |
| 2386 |
} else { |
| 2387 |
i++; |
| 2388 |
} |
| 2389 |
} |
| 2390 |
|
| 2391 |
|
| 2392 |
} |
| 2393 |
|
| 2394 |
|
| 2395 |
public static void onVmPolicyViolation(Violation originStack) { |
| 2396 |
onVmPolicyViolation(originStack, false); |
| 2397 |
} |
| 2398 |
|
| 2399 |
|
| 2400 |
public static void onVmPolicyViolation(Violation violation, boolean forceDeath) { |
| 2401 |
final boolean penaltyDropbox = (sVmPolicy.mask & PENALTY_DROPBOX) != 0; |
| 2402 |
final boolean penaltyDeath = ((sVmPolicy.mask & PENALTY_DEATH) != 0) || forceDeath; |
| 2403 |
final boolean penaltyLog = (sVmPolicy.mask & PENALTY_LOG) != 0; |
| 2404 |
|
| 2405 |
final int penaltyMask = (sVmPolicy.mask & PENALTY_ALL); |
| 2406 |
final ViolationInfo info = new ViolationInfo(violation, penaltyMask); |
| 2407 |
|
| 2408 |
|
| 2409 |
info.numAnimationsRunning = 0; |
| 2410 |
info.tags = null; |
| 2411 |
info.broadcastIntentAction = null; |
| 2412 |
|
| 2413 |
final Integer fingerprint = info.hashCode(); |
| 2414 |
final long now = SystemClock.uptimeMillis(); |
| 2415 |
long lastViolationTime; |
| 2416 |
long timeSinceLastViolationMillis = Long.MAX_VALUE; |
| 2417 |
if (sLogger == LOGCAT_LOGGER) { |
| 2418 |
synchronized (sRealLastVmViolationTime) { |
| 2419 |
if (sRealLastVmViolationTime.indexOfKey(fingerprint) >= 0) { |
| 2420 |
lastViolationTime = sRealLastVmViolationTime.get(fingerprint); |
| 2421 |
timeSinceLastViolationMillis = now - lastViolationTime; |
| 2422 |
} |
| 2423 |
if (timeSinceLastViolationMillis > MIN_VM_INTERVAL_MS) { |
| 2424 |
sRealLastVmViolationTime.put(fingerprint, now); |
| 2425 |
} |
| 2426 |
clampViolationTimeMap(sRealLastVmViolationTime, |
| 2427 |
now - Math.max(MIN_VM_INTERVAL_MS, MIN_LOG_INTERVAL_MS)); |
| 2428 |
} |
| 2429 |
} |
| 2430 |
if (timeSinceLastViolationMillis <= MIN_VM_INTERVAL_MS) { |
| 2431 |
|
| 2432 |
return; |
| 2433 |
} |
| 2434 |
|
| 2435 |
if (penaltyLog && sLogger != null && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { |
| 2436 |
sLogger.log(info); |
| 2437 |
} |
| 2438 |
|
| 2439 |
if (penaltyDropbox) { |
| 2440 |
if (penaltyDeath) { |
| 2441 |
handleApplicationStrictModeViolation(PENALTY_DROPBOX, info); |
| 2442 |
} else { |
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
dropboxViolationAsync(PENALTY_DROPBOX, info); |
| 2447 |
} |
| 2448 |
} |
| 2449 |
|
| 2450 |
if (penaltyDeath) { |
| 2451 |
System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down."); |
| 2452 |
Process.killProcess(Process.myPid()); |
| 2453 |
System.exit(10); |
| 2454 |
} |
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
if (sVmPolicy.mListener != null && sVmPolicy.mCallbackExecutor != null) { |
| 2459 |
final OnVmViolationListener listener = sVmPolicy.mListener; |
| 2460 |
try { |
| 2461 |
sVmPolicy.mCallbackExecutor.execute( |
| 2462 |
() -> { |
| 2463 |
|
| 2464 |
VmPolicy oldPolicy = allowVmViolations(); |
| 2465 |
try { |
| 2466 |
listener.onVmViolation(violation); |
| 2467 |
} finally { |
| 2468 |
setVmPolicy(oldPolicy); |
| 2469 |
} |
| 2470 |
}); |
| 2471 |
} catch (RejectedExecutionException e) { |
| 2472 |
Log.e(TAG, "VmPolicy penaltyCallback failed", e); |
| 2473 |
} |
| 2474 |
} |
| 2475 |
} |
| 2476 |
|
| 2477 |
|
| 2478 |
static void writeGatheredViolationsToParcel(Parcel p) { |
| 2479 |
ArrayList<ViolationInfo> violations = gatheredViolations.get(); |
| 2480 |
if (violations == null) { |
| 2481 |
p.writeInt(0); |
| 2482 |
} else { |
| 2483 |
|
| 2484 |
|
| 2485 |
|
| 2486 |
final int size = Math.min(violations.size(), 3); |
| 2487 |
p.writeInt(size); |
| 2488 |
for (int i = 0; i < size; i++) { |
| 2489 |
violations.get(i).writeToParcel(p, 0); |
| 2490 |
} |
| 2491 |
} |
| 2492 |
gatheredViolations.set(null); |
| 2493 |
} |
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
static void readAndHandleBinderCallViolations(Parcel p) { |
| 2500 |
Throwable localCallSite = new Throwable(); |
| 2501 |
final int policyMask = getThreadPolicyMask(); |
| 2502 |
final boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0; |
| 2503 |
|
| 2504 |
final int size = p.readInt(); |
| 2505 |
for (int i = 0; i < size; i++) { |
| 2506 |
final ViolationInfo info = new ViolationInfo(p, !currentlyGathering); |
| 2507 |
info.addLocalStack(localCallSite); |
| 2508 |
BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2509 |
if (policy instanceof AndroidBlockGuardPolicy) { |
| 2510 |
((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info); |
| 2511 |
} |
| 2512 |
} |
| 2513 |
} |
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2522 |
private static void onBinderStrictModePolicyChange(@ThreadPolicyMask int newPolicy) { |
| 2523 |
setBlockGuardPolicy(newPolicy); |
| 2524 |
} |
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
|
| 2534 |
public static class Span { |
| 2535 |
private String mName; |
| 2536 |
private long mCreateMillis; |
| 2537 |
private Span mNext; |
| 2538 |
private Span mPrev; |
| 2539 |
private final ThreadSpanState mContainerState; |
| 2540 |
|
| 2541 |
Span(ThreadSpanState threadState) { |
| 2542 |
mContainerState = threadState; |
| 2543 |
} |
| 2544 |
|
| 2545 |
|
| 2546 |
protected Span() { |
| 2547 |
mContainerState = null; |
| 2548 |
} |
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
@UnsupportedAppUsage |
| 2559 |
public void finish() { |
| 2560 |
ThreadSpanState state = mContainerState; |
| 2561 |
synchronized (state) { |
| 2562 |
if (mName == null) { |
| 2563 |
|
| 2564 |
return; |
| 2565 |
} |
| 2566 |
|
| 2567 |
|
| 2568 |
if (mPrev != null) { |
| 2569 |
mPrev.mNext = mNext; |
| 2570 |
} |
| 2571 |
if (mNext != null) { |
| 2572 |
mNext.mPrev = mPrev; |
| 2573 |
} |
| 2574 |
if (state.mActiveHead == this) { |
| 2575 |
state.mActiveHead = mNext; |
| 2576 |
} |
| 2577 |
|
| 2578 |
state.mActiveSize--; |
| 2579 |
|
| 2580 |
if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize); |
| 2581 |
|
| 2582 |
this.mCreateMillis = -1; |
| 2583 |
this.mName = null; |
| 2584 |
this.mPrev = null; |
| 2585 |
this.mNext = null; |
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
if (state.mFreeListSize < 5) { |
| 2590 |
this.mNext = state.mFreeListHead; |
| 2591 |
state.mFreeListHead = this; |
| 2592 |
state.mFreeListSize++; |
| 2593 |
} |
| 2594 |
} |
| 2595 |
} |
| 2596 |
} |
| 2597 |
|
| 2598 |
|
| 2599 |
private static final Span NO_OP_SPAN = |
| 2600 |
new Span() { |
| 2601 |
public void finish() { |
| 2602 |
|
| 2603 |
} |
| 2604 |
}; |
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
|
| 2615 |
private static class ThreadSpanState { |
| 2616 |
public Span mActiveHead; |
| 2617 |
public int mActiveSize; |
| 2618 |
public Span mFreeListHead; |
| 2619 |
public int mFreeListSize; |
| 2620 |
} |
| 2621 |
|
| 2622 |
private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState = |
| 2623 |
new ThreadLocal<ThreadSpanState>() { |
| 2624 |
@Override |
| 2625 |
protected ThreadSpanState initialValue() { |
| 2626 |
return new ThreadSpanState(); |
| 2627 |
} |
| 2628 |
}; |
| 2629 |
|
| 2630 |
@UnsupportedAppUsage |
| 2631 |
private static Singleton<IWindowManager> sWindowManager = |
| 2632 |
new Singleton<IWindowManager>() { |
| 2633 |
protected IWindowManager create() { |
| 2634 |
return IWindowManager.Stub.asInterface(ServiceManager.getService("window")); |
| 2635 |
} |
| 2636 |
}; |
| 2637 |
|
| 2638 |
|
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
|
| 2645 |
|
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
@UnsupportedAppUsage |
| 2652 |
public static Span enterCriticalSpan(String name) { |
| 2653 |
if (Build.IS_USER) { |
| 2654 |
return NO_OP_SPAN; |
| 2655 |
} |
| 2656 |
if (name == null || name.isEmpty()) { |
| 2657 |
throw new IllegalArgumentException("name must be non-null and non-empty"); |
| 2658 |
} |
| 2659 |
ThreadSpanState state = sThisThreadSpanState.get(); |
| 2660 |
Span span = null; |
| 2661 |
synchronized (state) { |
| 2662 |
if (state.mFreeListHead != null) { |
| 2663 |
span = state.mFreeListHead; |
| 2664 |
state.mFreeListHead = span.mNext; |
| 2665 |
state.mFreeListSize--; |
| 2666 |
} else { |
| 2667 |
|
| 2668 |
span = new Span(state); |
| 2669 |
} |
| 2670 |
span.mName = name; |
| 2671 |
span.mCreateMillis = SystemClock.uptimeMillis(); |
| 2672 |
span.mNext = state.mActiveHead; |
| 2673 |
span.mPrev = null; |
| 2674 |
state.mActiveHead = span; |
| 2675 |
state.mActiveSize++; |
| 2676 |
if (span.mNext != null) { |
| 2677 |
span.mNext.mPrev = span; |
| 2678 |
} |
| 2679 |
if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize); |
| 2680 |
} |
| 2681 |
return span; |
| 2682 |
} |
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
public static void noteSlowCall(String name) { |
| 2692 |
BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2693 |
if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2694 |
|
| 2695 |
return; |
| 2696 |
} |
| 2697 |
((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name); |
| 2698 |
} |
| 2699 |
|
| 2700 |
|
| 2701 |
@SystemApi(client = MODULE_LIBRARIES) |
| 2702 |
public static void noteUntaggedSocket() { |
| 2703 |
if (vmUntaggedSocketEnabled()) onUntaggedSocket(); |
| 2704 |
} |
| 2705 |
|
| 2706 |
|
| 2707 |
|
| 2708 |
|
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
public static void noteResourceMismatch(Object tag) { |
| 2715 |
BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2716 |
if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2717 |
|
| 2718 |
return; |
| 2719 |
} |
| 2720 |
((AndroidBlockGuardPolicy) policy).onResourceMismatch(tag); |
| 2721 |
} |
| 2722 |
|
| 2723 |
|
| 2724 |
public static void noteUnbufferedIO() { |
| 2725 |
BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2726 |
if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2727 |
|
| 2728 |
return; |
| 2729 |
} |
| 2730 |
policy.onUnbufferedIO(); |
| 2731 |
} |
| 2732 |
|
| 2733 |
|
| 2734 |
public static void noteDiskRead() { |
| 2735 |
BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2736 |
if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2737 |
|
| 2738 |
return; |
| 2739 |
} |
| 2740 |
policy.onReadFromDisk(); |
| 2741 |
} |
| 2742 |
|
| 2743 |
|
| 2744 |
public static void noteDiskWrite() { |
| 2745 |
BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); |
| 2746 |
if (!(policy instanceof AndroidBlockGuardPolicy)) { |
| 2747 |
|
| 2748 |
return; |
| 2749 |
} |
| 2750 |
policy.onWriteToDisk(); |
| 2751 |
} |
| 2752 |
|
| 2753 |
@GuardedBy("StrictMode.class") |
| 2754 |
private static final HashMap<Class, Integer> sExpectedActivityInstanceCount = new HashMap<>(); |
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
public static Object trackActivity(Object instance) { |
| 2763 |
return new InstanceTracker(instance); |
| 2764 |
} |
| 2765 |
|
| 2766 |
|
| 2767 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2768 |
public static void incrementExpectedActivityCount(Class klass) { |
| 2769 |
if (klass == null) { |
| 2770 |
return; |
| 2771 |
} |
| 2772 |
|
| 2773 |
synchronized (StrictMode.class) { |
| 2774 |
if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) { |
| 2775 |
return; |
| 2776 |
} |
| 2777 |
|
| 2778 |
|
| 2779 |
Integer expected = sExpectedActivityInstanceCount.get(klass); |
| 2780 |
Integer newExpected = |
| 2781 |
expected == null ? InstanceTracker.getInstanceCount(klass) + 1 : expected + 1; |
| 2782 |
sExpectedActivityInstanceCount.put(klass, newExpected); |
| 2783 |
} |
| 2784 |
} |
| 2785 |
|
| 2786 |
|
| 2787 |
public static void decrementExpectedActivityCount(Class klass) { |
| 2788 |
if (klass == null) { |
| 2789 |
return; |
| 2790 |
} |
| 2791 |
|
| 2792 |
final int limit; |
| 2793 |
synchronized (StrictMode.class) { |
| 2794 |
if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) { |
| 2795 |
return; |
| 2796 |
} |
| 2797 |
|
| 2798 |
Integer expected = sExpectedActivityInstanceCount.get(klass); |
| 2799 |
int newExpected = (expected == null || expected == 0) ? 0 : expected - 1; |
| 2800 |
if (newExpected == 0) { |
| 2801 |
sExpectedActivityInstanceCount.remove(klass); |
| 2802 |
} else { |
| 2803 |
sExpectedActivityInstanceCount.put(klass, newExpected); |
| 2804 |
} |
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
limit = newExpected + 1; |
| 2809 |
} |
| 2810 |
|
| 2811 |
|
| 2812 |
int actual = InstanceTracker.getInstanceCount(klass); |
| 2813 |
if (actual <= limit) { |
| 2814 |
return; |
| 2815 |
} |
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
|
| 2820 |
|
| 2821 |
|
| 2822 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
System.gc(); |
| 2826 |
System.runFinalization(); |
| 2827 |
System.gc(); |
| 2828 |
|
| 2829 |
long instances = VMDebug.countInstancesOfClass(klass, false); |
| 2830 |
if (instances > limit) { |
| 2831 |
onVmPolicyViolation(new InstanceCountViolation(klass, instances, limit)); |
| 2832 |
} |
| 2833 |
} |
| 2834 |
|
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
|
| 2839 |
|
| 2840 |
|
| 2841 |
@TestApi |
| 2842 |
public static final class ViolationInfo implements Parcelable { |
| 2843 |
|
| 2844 |
private final Violation mViolation; |
| 2845 |
|
| 2846 |
|
| 2847 |
private final Deque<StackTraceElement[]> mBinderStack = new ArrayDeque<>(); |
| 2848 |
|
| 2849 |
|
| 2850 |
@Nullable private String mStackTrace; |
| 2851 |
|
| 2852 |
|
| 2853 |
private final int mPenaltyMask; |
| 2854 |
|
| 2855 |
|
| 2856 |
public int durationMillis = -1; |
| 2857 |
|
| 2858 |
|
| 2859 |
public int numAnimationsRunning = 0; |
| 2860 |
|
| 2861 |
|
| 2862 |
public String[] tags; |
| 2863 |
|
| 2864 |
|
| 2865 |
|
| 2866 |
|
| 2867 |
|
| 2868 |
|
| 2869 |
public int violationNumThisLoop; |
| 2870 |
|
| 2871 |
|
| 2872 |
public long violationUptimeMillis; |
| 2873 |
|
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
|
| 2878 |
public String broadcastIntentAction; |
| 2879 |
|
| 2880 |
|
| 2881 |
public long numInstances = -1; |
| 2882 |
|
| 2883 |
|
| 2884 |
ViolationInfo(Violation tr, int penaltyMask) { |
| 2885 |
this.mViolation = tr; |
| 2886 |
this.mPenaltyMask = penaltyMask; |
| 2887 |
violationUptimeMillis = SystemClock.uptimeMillis(); |
| 2888 |
this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount(); |
| 2889 |
Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast(); |
| 2890 |
if (broadcastIntent != null) { |
| 2891 |
broadcastIntentAction = broadcastIntent.getAction(); |
| 2892 |
} |
| 2893 |
ThreadSpanState state = sThisThreadSpanState.get(); |
| 2894 |
if (tr instanceof InstanceCountViolation) { |
| 2895 |
this.numInstances = ((InstanceCountViolation) tr).getNumberOfInstances(); |
| 2896 |
} |
| 2897 |
synchronized (state) { |
| 2898 |
int spanActiveCount = state.mActiveSize; |
| 2899 |
if (spanActiveCount > MAX_SPAN_TAGS) { |
| 2900 |
spanActiveCount = MAX_SPAN_TAGS; |
| 2901 |
} |
| 2902 |
if (spanActiveCount != 0) { |
| 2903 |
this.tags = new String[spanActiveCount]; |
| 2904 |
Span iter = state.mActiveHead; |
| 2905 |
int index = 0; |
| 2906 |
while (iter != null && index < spanActiveCount) { |
| 2907 |
this.tags[index] = iter.mName; |
| 2908 |
index++; |
| 2909 |
iter = iter.mNext; |
| 2910 |
} |
| 2911 |
} |
| 2912 |
} |
| 2913 |
} |
| 2914 |
|
| 2915 |
|
| 2916 |
|
| 2917 |
|
| 2918 |
|
| 2919 |
public String getStackTrace() { |
| 2920 |
if (mStackTrace == null) { |
| 2921 |
StringWriter sw = new StringWriter(); |
| 2922 |
PrintWriter pw = new FastPrintWriter(sw, false, 256); |
| 2923 |
mViolation.printStackTrace(pw); |
| 2924 |
for (StackTraceElement[] traces : mBinderStack) { |
| 2925 |
pw.append("# via Binder call with stack:\n"); |
| 2926 |
for (StackTraceElement traceElement : traces) { |
| 2927 |
pw.append("\tat "); |
| 2928 |
pw.append(traceElement.toString()); |
| 2929 |
pw.append('\n'); |
| 2930 |
} |
| 2931 |
} |
| 2932 |
pw.flush(); |
| 2933 |
pw.close(); |
| 2934 |
mStackTrace = sw.toString(); |
| 2935 |
} |
| 2936 |
return mStackTrace; |
| 2937 |
} |
| 2938 |
|
| 2939 |
public Class<? extends Violation> getViolationClass() { |
| 2940 |
return mViolation.getClass(); |
| 2941 |
} |
| 2942 |
|
| 2943 |
|
| 2944 |
|
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
@TestApi |
| 2949 |
public String getViolationDetails() { |
| 2950 |
return mViolation.getMessage(); |
| 2951 |
} |
| 2952 |
|
| 2953 |
boolean penaltyEnabled(int p) { |
| 2954 |
return (mPenaltyMask & p) != 0; |
| 2955 |
} |
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
void addLocalStack(Throwable t) { |
| 2964 |
mBinderStack.addFirst(t.getStackTrace()); |
| 2965 |
} |
| 2966 |
|
| 2967 |
@Override |
| 2968 |
public int hashCode() { |
| 2969 |
int result = 17; |
| 2970 |
if (mViolation != null) { |
| 2971 |
result = 37 * result + mViolation.hashCode(); |
| 2972 |
} |
| 2973 |
if (numAnimationsRunning != 0) { |
| 2974 |
result *= 37; |
| 2975 |
} |
| 2976 |
if (broadcastIntentAction != null) { |
| 2977 |
result = 37 * result + broadcastIntentAction.hashCode(); |
| 2978 |
} |
| 2979 |
if (tags != null) { |
| 2980 |
for (String tag : tags) { |
| 2981 |
result = 37 * result + tag.hashCode(); |
| 2982 |
} |
| 2983 |
} |
| 2984 |
return result; |
| 2985 |
} |
| 2986 |
|
| 2987 |
|
| 2988 |
@UnsupportedAppUsage |
| 2989 |
public ViolationInfo(Parcel in) { |
| 2990 |
this(in, false); |
| 2991 |
} |
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
|
| 2998 |
|
| 2999 |
public ViolationInfo(Parcel in, boolean unsetGatheringBit) { |
| 3000 |
mViolation = (Violation) in.readSerializable(android.os.strictmode.Violation.class.getClassLoader(), android.os.strictmode.Violation.class); |
| 3001 |
int binderStackSize = in.readInt(); |
| 3002 |
for (int i = 0; i < binderStackSize; i++) { |
| 3003 |
StackTraceElement[] traceElements = new StackTraceElement[in.readInt()]; |
| 3004 |
for (int j = 0; j < traceElements.length; j++) { |
| 3005 |
StackTraceElement element = |
| 3006 |
new StackTraceElement( |
| 3007 |
in.readString(), |
| 3008 |
in.readString(), |
| 3009 |
in.readString(), |
| 3010 |
in.readInt()); |
| 3011 |
traceElements[j] = element; |
| 3012 |
} |
| 3013 |
mBinderStack.add(traceElements); |
| 3014 |
} |
| 3015 |
int rawPenaltyMask = in.readInt(); |
| 3016 |
if (unsetGatheringBit) { |
| 3017 |
mPenaltyMask = rawPenaltyMask & ~PENALTY_GATHER; |
| 3018 |
} else { |
| 3019 |
mPenaltyMask = rawPenaltyMask; |
| 3020 |
} |
| 3021 |
durationMillis = in.readInt(); |
| 3022 |
violationNumThisLoop = in.readInt(); |
| 3023 |
numAnimationsRunning = in.readInt(); |
| 3024 |
violationUptimeMillis = in.readLong(); |
| 3025 |
numInstances = in.readLong(); |
| 3026 |
broadcastIntentAction = in.readString(); |
| 3027 |
tags = in.readStringArray(); |
| 3028 |
} |
| 3029 |
|
| 3030 |
|
| 3031 |
@Override |
| 3032 |
public void writeToParcel(Parcel dest, int flags) { |
| 3033 |
dest.writeSerializable(mViolation); |
| 3034 |
dest.writeInt(mBinderStack.size()); |
| 3035 |
for (StackTraceElement[] traceElements : mBinderStack) { |
| 3036 |
dest.writeInt(traceElements.length); |
| 3037 |
for (StackTraceElement element : traceElements) { |
| 3038 |
dest.writeString(element.getClassName()); |
| 3039 |
dest.writeString(element.getMethodName()); |
| 3040 |
dest.writeString(element.getFileName()); |
| 3041 |
dest.writeInt(element.getLineNumber()); |
| 3042 |
} |
| 3043 |
} |
| 3044 |
int start = dest.dataPosition(); |
| 3045 |
dest.writeInt(mPenaltyMask); |
| 3046 |
dest.writeInt(durationMillis); |
| 3047 |
dest.writeInt(violationNumThisLoop); |
| 3048 |
dest.writeInt(numAnimationsRunning); |
| 3049 |
dest.writeLong(violationUptimeMillis); |
| 3050 |
dest.writeLong(numInstances); |
| 3051 |
dest.writeString(broadcastIntentAction); |
| 3052 |
dest.writeStringArray(tags); |
| 3053 |
int total = dest.dataPosition() - start; |
| 3054 |
if (Binder.CHECK_PARCEL_SIZE && total > 10 * 1024) { |
| 3055 |
Slog.d( |
| 3056 |
TAG, |
| 3057 |
"VIO: penalty=" |
| 3058 |
+ mPenaltyMask |
| 3059 |
+ " dur=" |
| 3060 |
+ durationMillis |
| 3061 |
+ " numLoop=" |
| 3062 |
+ violationNumThisLoop |
| 3063 |
+ " anim=" |
| 3064 |
+ numAnimationsRunning |
| 3065 |
+ " uptime=" |
| 3066 |
+ violationUptimeMillis |
| 3067 |
+ " numInst=" |
| 3068 |
+ numInstances); |
| 3069 |
Slog.d(TAG, "VIO: action=" + broadcastIntentAction); |
| 3070 |
Slog.d(TAG, "VIO: tags=" + Arrays.toString(tags)); |
| 3071 |
Slog.d(TAG, "VIO: TOTAL BYTES WRITTEN: " + (dest.dataPosition() - start)); |
| 3072 |
} |
| 3073 |
} |
| 3074 |
|
| 3075 |
|
| 3076 |
public void dump(Printer pw, String prefix) { |
| 3077 |
pw.println(prefix + "stackTrace: " + getStackTrace()); |
| 3078 |
pw.println(prefix + "penalty: " + mPenaltyMask); |
| 3079 |
if (durationMillis != -1) { |
| 3080 |
pw.println(prefix + "durationMillis: " + durationMillis); |
| 3081 |
} |
| 3082 |
if (numInstances != -1) { |
| 3083 |
pw.println(prefix + "numInstances: " + numInstances); |
| 3084 |
} |
| 3085 |
if (violationNumThisLoop != 0) { |
| 3086 |
pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop); |
| 3087 |
} |
| 3088 |
if (numAnimationsRunning != 0) { |
| 3089 |
pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning); |
| 3090 |
} |
| 3091 |
pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis); |
| 3092 |
if (broadcastIntentAction != null) { |
| 3093 |
pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction); |
| 3094 |
} |
| 3095 |
if (tags != null) { |
| 3096 |
int index = 0; |
| 3097 |
for (String tag : tags) { |
| 3098 |
pw.println(prefix + "tag[" + (index++) + "]: " + tag); |
| 3099 |
} |
| 3100 |
} |
| 3101 |
} |
| 3102 |
|
| 3103 |
@Override |
| 3104 |
public int describeContents() { |
| 3105 |
return 0; |
| 3106 |
} |
| 3107 |
|
| 3108 |
public static final @android.annotation.NonNull Parcelable.Creator<ViolationInfo> CREATOR = |
| 3109 |
new Parcelable.Creator<ViolationInfo>() { |
| 3110 |
@Override |
| 3111 |
public ViolationInfo createFromParcel(Parcel in) { |
| 3112 |
return new ViolationInfo(in); |
| 3113 |
} |
| 3114 |
|
| 3115 |
@Override |
| 3116 |
public ViolationInfo[] newArray(int size) { |
| 3117 |
return new ViolationInfo[size]; |
| 3118 |
} |
| 3119 |
}; |
| 3120 |
} |
| 3121 |
|
| 3122 |
private static final class InstanceTracker { |
| 3123 |
private static final HashMap<Class<?>, Integer> sInstanceCounts = |
| 3124 |
new HashMap<Class<?>, Integer>(); |
| 3125 |
|
| 3126 |
private final Class<?> mKlass; |
| 3127 |
|
| 3128 |
public InstanceTracker(Object instance) { |
| 3129 |
mKlass = instance.getClass(); |
| 3130 |
|
| 3131 |
synchronized (sInstanceCounts) { |
| 3132 |
final Integer value = sInstanceCounts.get(mKlass); |
| 3133 |
final int newValue = value != null ? value + 1 : 1; |
| 3134 |
sInstanceCounts.put(mKlass, newValue); |
| 3135 |
} |
| 3136 |
} |
| 3137 |
|
| 3138 |
@Override |
| 3139 |
protected void finalize() throws Throwable { |
| 3140 |
try { |
| 3141 |
synchronized (sInstanceCounts) { |
| 3142 |
final Integer value = sInstanceCounts.get(mKlass); |
| 3143 |
if (value != null) { |
| 3144 |
final int newValue = value - 1; |
| 3145 |
if (newValue > 0) { |
| 3146 |
sInstanceCounts.put(mKlass, newValue); |
| 3147 |
} else { |
| 3148 |
sInstanceCounts.remove(mKlass); |
| 3149 |
} |
| 3150 |
} |
| 3151 |
} |
| 3152 |
} finally { |
| 3153 |
super.finalize(); |
| 3154 |
} |
| 3155 |
} |
| 3156 |
|
| 3157 |
public static int getInstanceCount(Class<?> klass) { |
| 3158 |
synchronized (sInstanceCounts) { |
| 3159 |
final Integer value = sInstanceCounts.get(klass); |
| 3160 |
return value != null ? value : 0; |
| 3161 |
} |
| 3162 |
} |
| 3163 |
} |
| 3164 |
} |