| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.app.AppOpsManager; |
| 23 |
import android.compat.annotation.UnsupportedAppUsage; |
| 24 |
import android.util.ExceptionUtils; |
| 25 |
import android.util.Log; |
| 26 |
import android.util.Slog; |
| 27 |
|
| 28 |
import com.android.internal.annotations.VisibleForTesting; |
| 29 |
import com.android.internal.os.BinderCallHeavyHitterWatcher; |
| 30 |
import com.android.internal.os.BinderCallHeavyHitterWatcher.BinderCallHeavyHitterListener; |
| 31 |
import com.android.internal.os.BinderInternal; |
| 32 |
import com.android.internal.os.BinderInternal.CallSession; |
| 33 |
import com.android.internal.util.FastPrintWriter; |
| 34 |
import com.android.internal.util.FunctionalUtils.ThrowingRunnable; |
| 35 |
import com.android.internal.util.FunctionalUtils.ThrowingSupplier; |
| 36 |
|
| 37 |
import dalvik.annotation.optimization.CriticalNative; |
| 38 |
|
| 39 |
import libcore.io.IoUtils; |
| 40 |
import libcore.util.NativeAllocationRegistry; |
| 41 |
|
| 42 |
import java.io.FileDescriptor; |
| 43 |
import java.io.FileInputStream; |
| 44 |
import java.io.FileOutputStream; |
| 45 |
import java.io.IOException; |
| 46 |
import java.io.PrintWriter; |
| 47 |
import java.lang.reflect.Modifier; |
| 48 |
import java.util.concurrent.atomic.AtomicReferenceArray; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public class Binder implements IBinder { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private static final boolean FIND_POTENTIAL_LEAKS = false; |
| 89 |
|
| 90 |
public static final boolean CHECK_PARCEL_SIZE = false; |
| 91 |
static final String TAG = "Binder"; |
| 92 |
|
| 93 |
|
| 94 |
public static boolean LOG_RUNTIME_EXCEPTION = false; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public static final int UNSET_WORKSOURCE = -1; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
private static volatile String sDumpDisabled = null; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private static volatile TransactionTracker sTransactionTracker = null; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private static BinderInternal.Observer sObserver = null; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
private static final int NATIVE_ALLOCATION_SIZE = 500; |
| 125 |
|
| 126 |
private static native long getNativeFinalizer(); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private static class NoImagePreloadHolder { |
| 131 |
public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry( |
| 132 |
Binder.class.getClassLoader(), getNativeFinalizer(), NATIVE_ALLOCATION_SIZE); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private static volatile BinderCallHeavyHitterWatcher sHeavyHitterWatcher = null; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private static volatile boolean sStackTrackingEnabled = false; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public static void enableStackTracking() { |
| 154 |
sStackTrackingEnabled = true; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public static void disableStackTracking() { |
| 163 |
sStackTrackingEnabled = false; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static boolean isStackTrackingEnabled() { |
| 172 |
return sStackTrackingEnabled; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public synchronized static TransactionTracker getTransactionTracker() { |
| 181 |
if (sTransactionTracker == null) |
| 182 |
sTransactionTracker = new TransactionTracker(); |
| 183 |
return sTransactionTracker; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static void setObserver(@Nullable BinderInternal.Observer observer) { |
| 192 |
sObserver = observer; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
static volatile boolean sWarnOnBlocking = false; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public static void setWarnOnBlocking(boolean warnOnBlocking) { |
| 208 |
sWarnOnBlocking = warnOnBlocking; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public static IBinder allowBlocking(IBinder binder) { |
| 224 |
try { |
| 225 |
if (binder instanceof BinderProxy) { |
| 226 |
((BinderProxy) binder).mWarnOnBlocking = false; |
| 227 |
} else if (binder != null && binder.getInterfaceDescriptor() != null |
| 228 |
&& binder.queryLocalInterface(binder.getInterfaceDescriptor()) == null) { |
| 229 |
Log.w(TAG, "Unable to allow blocking on interface " + binder); |
| 230 |
} |
| 231 |
} catch (RemoteException ignored) { |
| 232 |
} |
| 233 |
return binder; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public static IBinder defaultBlocking(IBinder binder) { |
| 243 |
if (binder instanceof BinderProxy) { |
| 244 |
((BinderProxy) binder).mWarnOnBlocking = sWarnOnBlocking; |
| 245 |
} |
| 246 |
return binder; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public static void copyAllowBlocking(IBinder fromBinder, IBinder toBinder) { |
| 256 |
if (fromBinder instanceof BinderProxy && toBinder instanceof BinderProxy) { |
| 257 |
((BinderProxy) toBinder).mWarnOnBlocking = ((BinderProxy) fromBinder).mWarnOnBlocking; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
static ThreadLocal<Boolean> sWarnOnBlockingOnCurrentThread = |
| 262 |
ThreadLocal.withInitial(() -> sWarnOnBlocking); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public static void allowBlockingForCurrentThread() { |
| 272 |
sWarnOnBlockingOnCurrentThread.set(false); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public static void defaultBlockingForCurrentThread() { |
| 283 |
sWarnOnBlockingOnCurrentThread.set(sWarnOnBlocking); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
@UnsupportedAppUsage |
| 290 |
private final long mObject; |
| 291 |
|
| 292 |
private IInterface mOwner; |
| 293 |
@Nullable |
| 294 |
private String mDescriptor; |
| 295 |
private volatile AtomicReferenceArray<String> mTransactionTraceNames = null; |
| 296 |
private volatile String mSimpleDescriptor = null; |
| 297 |
private static final int TRANSACTION_TRACE_NAME_ID_LIMIT = 1024; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
@CriticalNative |
| 309 |
public static final native int getCallingPid(); |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
@CriticalNative |
| 319 |
public static final native int getCallingUid(); |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
@CriticalNative |
| 328 |
public static final native boolean isDirectlyHandlingTransaction(); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
@CriticalNative |
| 337 |
private static native boolean hasExplicitIdentity(); |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public static final int getCallingUidOrThrow() { |
| 348 |
if (!isDirectlyHandlingTransaction() && !hasExplicitIdentity()) { |
| 349 |
throw new IllegalStateException( |
| 350 |
"Thread is not in a binder transaction, " |
| 351 |
+ "and the calling identity has not been " |
| 352 |
+ "explicitly set with clearCallingIdentity"); |
| 353 |
} |
| 354 |
return getCallingUid(); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public static final int getCallingUidOrWtf(String message) { |
| 368 |
if (!isDirectlyHandlingTransaction() && !hasExplicitIdentity()) { |
| 369 |
Slog.wtf(TAG, |
| 370 |
message + ": Thread is not in a binder transaction, " |
| 371 |
+ "and the calling identity has not been " |
| 372 |
+ "explicitly set with clearCallingIdentity"); |
| 373 |
} |
| 374 |
return getCallingUid(); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public static final @NonNull UserHandle getCallingUserHandle() { |
| 388 |
return UserHandle.of(UserHandle.getUserId(getCallingUid())); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
@CriticalNative |
| 408 |
public static final native long clearCallingIdentity(); |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
@CriticalNative |
| 421 |
public static final native void restoreCallingIdentity(long token); |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public static final void withCleanCallingIdentity(@NonNull ThrowingRunnable action) { |
| 433 |
Throwable throwableToPropagate = null; |
| 434 |
final long callingIdentity = clearCallingIdentity(); |
| 435 |
try { |
| 436 |
action.runOrThrow(); |
| 437 |
} catch (Throwable throwable) { |
| 438 |
throwableToPropagate = throwable; |
| 439 |
} finally { |
| 440 |
restoreCallingIdentity(callingIdentity); |
| 441 |
if (throwableToPropagate != null) { |
| 442 |
throw ExceptionUtils.propagate(throwableToPropagate); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public static final <T> T withCleanCallingIdentity(@NonNull ThrowingSupplier<T> action) { |
| 457 |
Throwable throwableToPropagate = null; |
| 458 |
final long callingIdentity = clearCallingIdentity(); |
| 459 |
try { |
| 460 |
return action.getOrThrow(); |
| 461 |
} catch (Throwable throwable) { |
| 462 |
throwableToPropagate = throwable; |
| 463 |
return null; |
| 464 |
} finally { |
| 465 |
restoreCallingIdentity(callingIdentity); |
| 466 |
if (throwableToPropagate != null) { |
| 467 |
throw ExceptionUtils.propagate(throwableToPropagate); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
@CriticalNative |
| 487 |
public static final native void setThreadStrictModePolicy(int policyMask); |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
@CriticalNative |
| 497 |
public static final native int getThreadStrictModePolicy(); |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
@CriticalNative |
| 530 |
public static final native long setCallingWorkSourceUid(int workSource); |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
@CriticalNative |
| 542 |
public static final native int getCallingWorkSourceUid(); |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
@CriticalNative |
| 566 |
public static final native long clearCallingWorkSource(); |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
@CriticalNative |
| 583 |
public static final native void restoreCallingWorkSource(long token); |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
@SystemApi(client = SystemApi.Client.PRIVILEGED_APPS) |
| 598 |
public final native void markVintfStability(); |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
public final native void forceDowngradeToSystemStability(); |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
public static final native void flushPendingCommands(); |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
public static final void joinThreadPool() { |
| 625 |
BinderInternal.joinThreadPool(); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
public static final boolean isProxy(IInterface iface) { |
| 634 |
return iface.asBinder() != iface; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
public static final native void blockUntilThreadAvailable(); |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
public Binder() { |
| 652 |
this(null); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
public Binder(@Nullable String descriptor) { |
| 668 |
mObject = getNativeBBinderHolder(); |
| 669 |
NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mObject); |
| 670 |
|
| 671 |
if (FIND_POTENTIAL_LEAKS) { |
| 672 |
final Class<? extends Binder> klass = getClass(); |
| 673 |
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && |
| 674 |
(klass.getModifiers() & Modifier.STATIC) == 0) { |
| 675 |
Log.w(TAG, "The following Binder class should be static or leaks might occur: " + |
| 676 |
klass.getCanonicalName()); |
| 677 |
} |
| 678 |
} |
| 679 |
mDescriptor = descriptor; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
public void attachInterface(@Nullable IInterface owner, @Nullable String descriptor) { |
| 689 |
mOwner = owner; |
| 690 |
mDescriptor = descriptor; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
public @Nullable String getInterfaceDescriptor() { |
| 697 |
return mDescriptor; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
public boolean pingBinder() { |
| 705 |
return true; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
public boolean isBinderAlive() { |
| 715 |
return true; |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
public @Nullable IInterface queryLocalInterface(@NonNull String descriptor) { |
| 724 |
if (mDescriptor != null && mDescriptor.equals(descriptor)) { |
| 725 |
return mOwner; |
| 726 |
} |
| 727 |
return null; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
public static void setDumpDisabled(String msg) { |
| 742 |
sDumpDisabled = msg; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
@SystemApi |
| 753 |
public interface ProxyTransactListener { |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
@Nullable |
| 762 |
default Object onTransactStarted(@NonNull IBinder binder, int transactionCode, int flags) { |
| 763 |
return onTransactStarted(binder, transactionCode); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
@Nullable |
| 772 |
Object onTransactStarted(@NonNull IBinder binder, int transactionCode); |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
void onTransactEnded(@Nullable Object session); |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
public static class PropagateWorkSourceTransactListener implements ProxyTransactListener { |
| 792 |
@Override |
| 793 |
public Object onTransactStarted(IBinder binder, int transactionCode) { |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
int uid = ThreadLocalWorkSource.getUid(); |
| 800 |
if (uid != ThreadLocalWorkSource.UID_NONE) { |
| 801 |
return Binder.setCallingWorkSourceUid(uid); |
| 802 |
} |
| 803 |
return null; |
| 804 |
} |
| 805 |
|
| 806 |
@Override |
| 807 |
public void onTransactEnded(Object session) { |
| 808 |
if (session != null) { |
| 809 |
long token = (long) session; |
| 810 |
Binder.restoreCallingWorkSource(token); |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
@SystemApi |
| 831 |
public static void setProxyTransactListener(@Nullable ProxyTransactListener listener) { |
| 832 |
BinderProxy.setTransactListener(listener); |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply, |
| 858 |
int flags) throws RemoteException { |
| 859 |
if (code == INTERFACE_TRANSACTION) { |
| 860 |
reply.writeString(getInterfaceDescriptor()); |
| 861 |
return true; |
| 862 |
} else if (code == DUMP_TRANSACTION) { |
| 863 |
ParcelFileDescriptor fd = data.readFileDescriptor(); |
| 864 |
String[] args = data.readStringArray(); |
| 865 |
if (fd != null) { |
| 866 |
try { |
| 867 |
dump(fd.getFileDescriptor(), args); |
| 868 |
} finally { |
| 869 |
IoUtils.closeQuietly(fd); |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
if (reply != null) { |
| 874 |
reply.writeNoException(); |
| 875 |
} else { |
| 876 |
StrictMode.clearGatheredViolations(); |
| 877 |
} |
| 878 |
return true; |
| 879 |
} else if (code == SHELL_COMMAND_TRANSACTION) { |
| 880 |
ParcelFileDescriptor in = data.readFileDescriptor(); |
| 881 |
ParcelFileDescriptor out = data.readFileDescriptor(); |
| 882 |
ParcelFileDescriptor err = data.readFileDescriptor(); |
| 883 |
String[] args = data.readStringArray(); |
| 884 |
ShellCallback shellCallback = ShellCallback.CREATOR.createFromParcel(data); |
| 885 |
ResultReceiver resultReceiver = ResultReceiver.CREATOR.createFromParcel(data); |
| 886 |
try { |
| 887 |
if (out != null) { |
| 888 |
shellCommand(in != null ? in.getFileDescriptor() : null, |
| 889 |
out.getFileDescriptor(), |
| 890 |
err != null ? err.getFileDescriptor() : out.getFileDescriptor(), |
| 891 |
args, shellCallback, resultReceiver); |
| 892 |
} |
| 893 |
} finally { |
| 894 |
IoUtils.closeQuietly(in); |
| 895 |
IoUtils.closeQuietly(out); |
| 896 |
IoUtils.closeQuietly(err); |
| 897 |
|
| 898 |
if (reply != null) { |
| 899 |
reply.writeNoException(); |
| 900 |
} else { |
| 901 |
StrictMode.clearGatheredViolations(); |
| 902 |
} |
| 903 |
} |
| 904 |
return true; |
| 905 |
} |
| 906 |
return false; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
public @Nullable String getTransactionName(int transactionCode) { |
| 922 |
return null; |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
@VisibleForTesting |
| 929 |
public final @NonNull String getTransactionTraceName(int transactionCode) { |
| 930 |
if (mTransactionTraceNames == null) { |
| 931 |
final int highestId = Math.min(getMaxTransactionId(), TRANSACTION_TRACE_NAME_ID_LIMIT); |
| 932 |
mSimpleDescriptor = getSimpleDescriptor(); |
| 933 |
mTransactionTraceNames = new AtomicReferenceArray(highestId + 1); |
| 934 |
} |
| 935 |
|
| 936 |
final int index = transactionCode - FIRST_CALL_TRANSACTION; |
| 937 |
if (index < 0 || index >= mTransactionTraceNames.length()) { |
| 938 |
return mSimpleDescriptor + "#" + transactionCode; |
| 939 |
} |
| 940 |
|
| 941 |
String transactionTraceName = mTransactionTraceNames.getAcquire(index); |
| 942 |
if (transactionTraceName == null) { |
| 943 |
final String transactionName = getTransactionName(transactionCode); |
| 944 |
final StringBuffer buf = new StringBuffer(); |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
buf.append("AIDL::java::"); |
| 949 |
if (transactionName != null) { |
| 950 |
buf.append(mSimpleDescriptor).append("::").append(transactionName); |
| 951 |
} else { |
| 952 |
buf.append(mSimpleDescriptor).append("::#").append(transactionCode); |
| 953 |
} |
| 954 |
buf.append("::server"); |
| 955 |
|
| 956 |
transactionTraceName = buf.toString(); |
| 957 |
mTransactionTraceNames.setRelease(index, transactionTraceName); |
| 958 |
} |
| 959 |
|
| 960 |
return transactionTraceName; |
| 961 |
} |
| 962 |
|
| 963 |
private @NonNull String getSimpleDescriptor() { |
| 964 |
String descriptor = mDescriptor; |
| 965 |
if (descriptor == null) { |
| 966 |
|
| 967 |
return "Binder"; |
| 968 |
} |
| 969 |
|
| 970 |
final int dot = descriptor.lastIndexOf("."); |
| 971 |
if (dot > 0) { |
| 972 |
|
| 973 |
return descriptor.substring(dot + 1); |
| 974 |
} |
| 975 |
return descriptor; |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
public int getMaxTransactionId() { |
| 983 |
return 0; |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
public void dump(@NonNull FileDescriptor fd, @Nullable String[] args) { |
| 991 |
FileOutputStream fout = new FileOutputStream(fd); |
| 992 |
PrintWriter pw = new FastPrintWriter(fout); |
| 993 |
try { |
| 994 |
doDump(fd, pw, args); |
| 995 |
} finally { |
| 996 |
pw.flush(); |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
void doDump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1001 |
final String disabled = sDumpDisabled; |
| 1002 |
if (disabled == null) { |
| 1003 |
try { |
| 1004 |
dump(fd, pw, args); |
| 1005 |
} catch (SecurityException e) { |
| 1006 |
pw.println("Security exception: " + e.getMessage()); |
| 1007 |
throw e; |
| 1008 |
} catch (Throwable e) { |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
pw.println(); |
| 1014 |
pw.println("Exception occurred while dumping:"); |
| 1015 |
e.printStackTrace(pw); |
| 1016 |
} |
| 1017 |
} else { |
| 1018 |
pw.println(sDumpDisabled); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
public void dumpAsync(@NonNull final FileDescriptor fd, @Nullable final String[] args) { |
| 1027 |
final FileOutputStream fout = new FileOutputStream(fd); |
| 1028 |
final PrintWriter pw = new FastPrintWriter(fout); |
| 1029 |
Thread thr = new Thread("Binder.dumpAsync") { |
| 1030 |
public void run() { |
| 1031 |
try { |
| 1032 |
dump(fd, pw, args); |
| 1033 |
} finally { |
| 1034 |
pw.flush(); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
}; |
| 1038 |
thr.start(); |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter fout, |
| 1050 |
@Nullable String[] args) { |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
public void shellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out, |
| 1065 |
@Nullable FileDescriptor err, |
| 1066 |
@NonNull String[] args, @Nullable ShellCallback callback, |
| 1067 |
@NonNull ResultReceiver resultReceiver) throws RemoteException { |
| 1068 |
onShellCommand(in, out, err, args, callback, resultReceiver); |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out, |
| 1084 |
@Nullable FileDescriptor err, |
| 1085 |
@NonNull String[] args, @Nullable ShellCallback callback, |
| 1086 |
@NonNull ResultReceiver resultReceiver) throws RemoteException { |
| 1087 |
|
| 1088 |
final int callingUid = Binder.getCallingUid(); |
| 1089 |
if (callingUid != Process.ROOT_UID && callingUid != Process.SHELL_UID) { |
| 1090 |
resultReceiver.send(-1, null); |
| 1091 |
throw new SecurityException("Shell commands are only callable by ADB"); |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
try { |
| 1096 |
if (in == null) { |
| 1097 |
in = new FileInputStream("/dev/null").getFD(); |
| 1098 |
} |
| 1099 |
if (out == null) { |
| 1100 |
out = new FileOutputStream("/dev/null").getFD(); |
| 1101 |
} |
| 1102 |
if (err == null) { |
| 1103 |
err = out; |
| 1104 |
} |
| 1105 |
} catch (IOException e) { |
| 1106 |
PrintWriter pw = new FastPrintWriter(new FileOutputStream(err != null ? err : out)); |
| 1107 |
pw.println("Failed to open /dev/null: " + e.getMessage()); |
| 1108 |
pw.flush(); |
| 1109 |
resultReceiver.send(-1, null); |
| 1110 |
return; |
| 1111 |
} |
| 1112 |
|
| 1113 |
if (args == null) { |
| 1114 |
args = new String[0]; |
| 1115 |
} |
| 1116 |
|
| 1117 |
int result = -1; |
| 1118 |
try (ParcelFileDescriptor inPfd = ParcelFileDescriptor.dup(in); |
| 1119 |
ParcelFileDescriptor outPfd = ParcelFileDescriptor.dup(out); |
| 1120 |
ParcelFileDescriptor errPfd = ParcelFileDescriptor.dup(err)) { |
| 1121 |
result = handleShellCommand(inPfd, outPfd, errPfd, args); |
| 1122 |
} catch (IOException e) { |
| 1123 |
PrintWriter pw = new FastPrintWriter(new FileOutputStream(err)); |
| 1124 |
pw.println("dup() failed: " + e.getMessage()); |
| 1125 |
pw.flush(); |
| 1126 |
} finally { |
| 1127 |
resultReceiver.send(result, null); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
@SystemApi |
| 1150 |
public int handleShellCommand(@NonNull ParcelFileDescriptor in, |
| 1151 |
@NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err, |
| 1152 |
@NonNull String[] args) { |
| 1153 |
FileOutputStream ferr = new FileOutputStream(err.getFileDescriptor()); |
| 1154 |
PrintWriter pw = new FastPrintWriter(ferr); |
| 1155 |
pw.println("No shell command implementation."); |
| 1156 |
pw.flush(); |
| 1157 |
return 0; |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
@Override |
| 1162 |
public final native @Nullable IBinder getExtension(); |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
public final native void setExtension(@Nullable IBinder extension); |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
public final boolean transact(int code, @NonNull Parcel data, @Nullable Parcel reply, |
| 1177 |
int flags) throws RemoteException { |
| 1178 |
if (false) Log.v("Binder", "Transact: " + code + " to " + this); |
| 1179 |
|
| 1180 |
if (data != null) { |
| 1181 |
data.setDataPosition(0); |
| 1182 |
} |
| 1183 |
boolean r = onTransact(code, data, reply, flags); |
| 1184 |
if (reply != null) { |
| 1185 |
reply.setDataPosition(0); |
| 1186 |
} |
| 1187 |
return r; |
| 1188 |
} |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
public void linkToDeath(@NonNull DeathRecipient recipient, int flags) { |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
public boolean unlinkToDeath(@NonNull DeathRecipient recipient, int flags) { |
| 1200 |
return true; |
| 1201 |
} |
| 1202 |
|
| 1203 |
static void checkParcel(IBinder obj, int code, Parcel parcel, String msg) { |
| 1204 |
if (CHECK_PARCEL_SIZE && parcel.dataSize() >= 800*1024) { |
| 1205 |
|
| 1206 |
StringBuilder sb = new StringBuilder(); |
| 1207 |
sb.append(msg); |
| 1208 |
sb.append(": on "); |
| 1209 |
sb.append(obj); |
| 1210 |
sb.append(" calling "); |
| 1211 |
sb.append(code); |
| 1212 |
sb.append(" size "); |
| 1213 |
sb.append(parcel.dataSize()); |
| 1214 |
sb.append(" (data: "); |
| 1215 |
parcel.setDataPosition(0); |
| 1216 |
sb.append(parcel.readInt()); |
| 1217 |
sb.append(", "); |
| 1218 |
sb.append(parcel.readInt()); |
| 1219 |
sb.append(", "); |
| 1220 |
sb.append(parcel.readInt()); |
| 1221 |
sb.append(")"); |
| 1222 |
Slog.wtfStack(TAG, sb.toString()); |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
private static native long getNativeBBinderHolder(); |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
private static volatile BinderInternal.WorkSourceProvider sWorkSourceProvider = |
| 1232 |
(x) -> Binder.getCallingUid(); |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
public static void setWorkSourceProvider(BinderInternal.WorkSourceProvider workSourceProvider) { |
| 1248 |
if (workSourceProvider == null) { |
| 1249 |
throw new IllegalArgumentException("workSourceProvider cannot be null"); |
| 1250 |
} |
| 1251 |
sWorkSourceProvider = workSourceProvider; |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
@UnsupportedAppUsage |
| 1256 |
private boolean execTransact(int code, long dataObj, long replyObj, |
| 1257 |
int flags) { |
| 1258 |
|
| 1259 |
Parcel data = Parcel.obtain(dataObj); |
| 1260 |
Parcel reply = Parcel.obtain(replyObj); |
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
final int callingUid = data.isForRpc() ? -1 : Binder.getCallingUid(); |
| 1271 |
final long origWorkSource = callingUid == -1 |
| 1272 |
? -1 : ThreadLocalWorkSource.setUid(callingUid); |
| 1273 |
|
| 1274 |
try { |
| 1275 |
return execTransactInternal(code, data, reply, flags, callingUid); |
| 1276 |
} finally { |
| 1277 |
reply.recycle(); |
| 1278 |
data.recycle(); |
| 1279 |
|
| 1280 |
if (callingUid != -1) { |
| 1281 |
ThreadLocalWorkSource.restore(origWorkSource); |
| 1282 |
} |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
private boolean execTransactInternal(int code, Parcel data, Parcel reply, int flags, |
| 1287 |
int callingUid) { |
| 1288 |
|
| 1289 |
final BinderInternal.Observer observer = sObserver; |
| 1290 |
final CallSession callSession = |
| 1291 |
observer != null ? observer.callStarted(this, code, UNSET_WORKSOURCE) : null; |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
boolean res; |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
final boolean tagEnabled = Trace.isTagEnabled(Trace.TRACE_TAG_AIDL); |
| 1300 |
final boolean hasFullyQualifiedName = getMaxTransactionId() > 0; |
| 1301 |
final String transactionTraceName; |
| 1302 |
|
| 1303 |
if (tagEnabled && hasFullyQualifiedName) { |
| 1304 |
|
| 1305 |
transactionTraceName = getTransactionTraceName(code); |
| 1306 |
} else if (tagEnabled && isStackTrackingEnabled()) { |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
transactionTraceName = getTransactionTraceName(code); |
| 1316 |
} else { |
| 1317 |
transactionTraceName = null; |
| 1318 |
} |
| 1319 |
|
| 1320 |
final boolean tracingEnabled = tagEnabled && transactionTraceName != null; |
| 1321 |
try { |
| 1322 |
|
| 1323 |
|
| 1324 |
final BinderCallHeavyHitterWatcher heavyHitterWatcher = sHeavyHitterWatcher; |
| 1325 |
if (heavyHitterWatcher != null && callingUid != -1) { |
| 1326 |
|
| 1327 |
heavyHitterWatcher.onTransaction(callingUid, getClass(), code); |
| 1328 |
} |
| 1329 |
if (tracingEnabled) { |
| 1330 |
Trace.traceBegin(Trace.TRACE_TAG_AIDL, transactionTraceName); |
| 1331 |
} |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
if ((flags & FLAG_COLLECT_NOTED_APP_OPS) != 0 && callingUid != -1) { |
| 1337 |
AppOpsManager.startNotedAppOpsCollection(callingUid); |
| 1338 |
try { |
| 1339 |
res = onTransact(code, data, reply, flags); |
| 1340 |
} finally { |
| 1341 |
AppOpsManager.finishNotedAppOpsCollection(); |
| 1342 |
} |
| 1343 |
} else { |
| 1344 |
res = onTransact(code, data, reply, flags); |
| 1345 |
} |
| 1346 |
} catch (RemoteException|RuntimeException e) { |
| 1347 |
if (observer != null) { |
| 1348 |
observer.callThrewException(callSession, e); |
| 1349 |
} |
| 1350 |
if (LOG_RUNTIME_EXCEPTION) { |
| 1351 |
Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e); |
| 1352 |
} |
| 1353 |
if ((flags & FLAG_ONEWAY) != 0) { |
| 1354 |
if (e instanceof RemoteException) { |
| 1355 |
Log.w(TAG, "Binder call failed.", e); |
| 1356 |
} else { |
| 1357 |
Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e); |
| 1358 |
} |
| 1359 |
} else { |
| 1360 |
|
| 1361 |
reply.setDataSize(0); |
| 1362 |
reply.setDataPosition(0); |
| 1363 |
reply.writeException(e); |
| 1364 |
} |
| 1365 |
res = true; |
| 1366 |
} finally { |
| 1367 |
if (tracingEnabled) { |
| 1368 |
Trace.traceEnd(Trace.TRACE_TAG_AIDL); |
| 1369 |
} |
| 1370 |
if (observer != null) { |
| 1371 |
|
| 1372 |
|
| 1373 |
final int workSourceUid = sWorkSourceProvider.resolveWorkSourceUid( |
| 1374 |
data.readCallingWorkSourceUid()); |
| 1375 |
observer.callEnded(callSession, data.dataSize(), reply.dataSize(), workSourceUid); |
| 1376 |
} |
| 1377 |
|
| 1378 |
checkParcel(this, code, reply, "Unreasonably large binder reply buffer"); |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
StrictMode.clearGatheredViolations(); |
| 1387 |
return res; |
| 1388 |
} |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
public static synchronized void setHeavyHitterWatcherConfig(final boolean enabled, |
| 1396 |
final int batchSize, final float threshold, |
| 1397 |
@Nullable final BinderCallHeavyHitterListener listener) { |
| 1398 |
Slog.i(TAG, "Setting heavy hitter watcher config: " |
| 1399 |
+ enabled + ", " + batchSize + ", " + threshold); |
| 1400 |
BinderCallHeavyHitterWatcher watcher = sHeavyHitterWatcher; |
| 1401 |
if (enabled) { |
| 1402 |
if (listener == null) { |
| 1403 |
throw new IllegalArgumentException(); |
| 1404 |
} |
| 1405 |
boolean newWatcher = false; |
| 1406 |
if (watcher == null) { |
| 1407 |
watcher = BinderCallHeavyHitterWatcher.getInstance(); |
| 1408 |
newWatcher = true; |
| 1409 |
} |
| 1410 |
watcher.setConfig(true, batchSize, threshold, listener); |
| 1411 |
if (newWatcher) { |
| 1412 |
sHeavyHitterWatcher = watcher; |
| 1413 |
} |
| 1414 |
} else if (watcher != null) { |
| 1415 |
watcher.setConfig(false, 0, 0.0f, null); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
} |