blob: 6adbad0c61e122acff73e4b895b18f24746361e7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Dianne Hackborn4cd650c2017-07-31 17:38:53 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Eugene Suslacf00ade2017-04-10 11:51:58 -070021import android.util.ExceptionUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.util.Log;
Dianne Hackborn017c6a22014-09-25 17:41:34 -070023import android.util.Slog;
Eugene Suslacf00ade2017-04-10 11:51:58 -070024
Michael Wachenschwanz58ac2182018-01-30 15:11:19 -080025import com.android.internal.os.BinderInternal;
Olivier Gaillard289ba402018-07-24 18:50:13 +010026import com.android.internal.os.BinderInternal.CallSession;
Dianne Hackborndb4e33f2013-04-01 17:28:16 -070027import com.android.internal.util.FastPrintWriter;
Eugene Suslacf00ade2017-04-10 11:51:58 -070028import com.android.internal.util.FunctionalUtils.ThrowingRunnable;
29import com.android.internal.util.FunctionalUtils.ThrowingSupplier;
30
Olivier Gaillarde4ff3972018-08-16 14:01:58 +010031import dalvik.annotation.optimization.CriticalNative;
32
Dianne Hackborn9461b6f2015-10-07 17:33:16 -070033import libcore.io.IoUtils;
Hans Boehm5e5b13f2017-09-28 18:16:50 -070034import libcore.util.NativeAllocationRegistry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import java.io.FileDescriptor;
37import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import java.lang.reflect.Modifier;
40
41/**
42 * Base class for a remotable object, the core part of a lightweight
43 * remote procedure call mechanism defined by {@link IBinder}.
44 * This class is an implementation of IBinder that provides
Dianne Hackbornab4a81b2014-10-09 17:59:38 -070045 * standard local implementation of such an object.
46 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * <p>Most developers will not implement this class directly, instead using the
Scott Main40eee612012-08-06 17:48:37 -070048 * <a href="{@docRoot}guide/components/aidl.html">aidl</a> tool to describe the desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * interface, having it generate the appropriate Binder subclass. You can,
50 * however, derive directly from Binder to implement your own custom RPC
51 * protocol or simply instantiate a raw Binder object directly to use as a
52 * token that can be shared across processes.
Dianne Hackbornab4a81b2014-10-09 17:59:38 -070053 *
54 * <p>This class is just a basic IPC primitive; it has no impact on an application's
55 * lifecycle, and is valid only as long as the process that created it continues to run.
56 * To use this correctly, you must be doing so within the context of a top-level
57 * application component (a {@link android.app.Service}, {@link android.app.Activity},
58 * or {@link android.content.ContentProvider}) that lets the system know your process
59 * should remain running.</p>
60 *
61 * <p>You must keep in mind the situations in which your process
62 * could go away, and thus require that you later re-create a new Binder and re-attach
63 * it when the process starts again. For example, if you are using this within an
64 * {@link android.app.Activity}, your activity's process may be killed any time the
65 * activity is not started; if the activity is later re-created you will need to
66 * create a new Binder and hand it back to the correct place again; you need to be
67 * aware that your process may be started for another reason (for example to receive
68 * a broadcast) that will not involve re-creating the activity and thus run its code
69 * to create a new Binder.</p>
70 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * @see IBinder
72 */
73public class Binder implements IBinder {
74 /*
75 * Set this flag to true to detect anonymous, local or member classes
76 * that extend this Binder class and that are not static. These kind
77 * of classes can potentially create leaks.
78 */
79 private static final boolean FIND_POTENTIAL_LEAKS = false;
Jeff Sharkeyf5299f12017-03-17 10:25:07 -060080 /** @hide */
81 public static final boolean CHECK_PARCEL_SIZE = false;
Dianne Hackborn017c6a22014-09-25 17:41:34 -070082 static final String TAG = "Binder";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
Makoto Onukif9b941f2016-03-10 17:19:08 -080084 /** @hide */
85 public static boolean LOG_RUNTIME_EXCEPTION = false; // DO NOT SUBMIT WITH TRUE
86
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070087 /**
Olivier Gaillardc17d2802018-11-19 17:08:17 +000088 * Value to represents that a calling work source is not set.
89 *
90 * This constatnt needs to be kept in sync with IPCThreadState::kUnsetWorkSource.
91 *
92 * @hide
93 */
94 public static final int UNSET_WORKSOURCE = -1;
95
96 /**
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070097 * Control whether dump() calls are allowed.
98 */
Jeff Sharkey0a17db12016-11-04 11:23:46 -060099 private static volatile String sDumpDisabled = null;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700100
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400101 /**
102 * Global transaction tracker instance for this process.
103 */
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600104 private static volatile TransactionTracker sTransactionTracker = null;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400105
Hans Boehm5e5b13f2017-09-28 18:16:50 -0700106 /**
Olivier Gaillard289ba402018-07-24 18:50:13 +0100107 * Global observer for this process.
108 */
109 private static BinderInternal.Observer sObserver = null;
110
111 /**
Hans Boehm5e5b13f2017-09-28 18:16:50 -0700112 * Guestimate of native memory associated with a Binder.
113 */
114 private static final int NATIVE_ALLOCATION_SIZE = 500;
115
116 private static native long getNativeFinalizer();
117
118 // Use a Holder to allow static initialization of Binder in the boot image, and
119 // possibly to avoid some initialization ordering issues.
120 private static class NoImagePreloadHolder {
121 public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
122 Binder.class.getClassLoader(), getNativeFinalizer(), NATIVE_ALLOCATION_SIZE);
123 }
124
Olivier Gaillard289ba402018-07-24 18:50:13 +0100125
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400126 // Transaction tracking code.
127
128 /**
129 * Flag indicating whether we should be tracing transact calls.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400130 */
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600131 private static volatile boolean sTracingEnabled = false;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400132
133 /**
134 * Enable Binder IPC tracing.
135 *
136 * @hide
137 */
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600138 public static void enableTracing() {
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400139 sTracingEnabled = true;
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600140 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400141
142 /**
143 * Disable Binder IPC tracing.
144 *
145 * @hide
146 */
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600147 public static void disableTracing() {
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400148 sTracingEnabled = false;
149 }
150
151 /**
152 * Check if binder transaction tracing is enabled.
153 *
154 * @hide
155 */
156 public static boolean isTracingEnabled() {
157 return sTracingEnabled;
158 }
159
160 /**
161 * Get the binder transaction tracker for this process.
162 *
163 * @hide
164 */
165 public synchronized static TransactionTracker getTransactionTracker() {
166 if (sTransactionTracker == null)
167 sTransactionTracker = new TransactionTracker();
168 return sTransactionTracker;
169 }
170
Olivier Gaillard289ba402018-07-24 18:50:13 +0100171 /**
172 * Get the binder transaction observer for this process.
173 *
174 * @hide
175 */
176 public static void setObserver(@Nullable BinderInternal.Observer observer) {
177 sObserver = observer;
178 }
179
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600180 /** {@hide} */
181 static volatile boolean sWarnOnBlocking = false;
182
183 /**
184 * Warn if any blocking binder transactions are made out from this process.
185 * This is typically only useful for the system process, to prevent it from
186 * blocking on calls to external untrusted code. Instead, all outgoing calls
187 * that require a result must be sent as {@link IBinder#FLAG_ONEWAY} calls
188 * which deliver results through a callback interface.
189 *
190 * @hide
191 */
192 public static void setWarnOnBlocking(boolean warnOnBlocking) {
193 sWarnOnBlocking = warnOnBlocking;
194 }
195
196 /**
197 * Allow blocking calls on the given interface, overriding the requested
198 * value of {@link #setWarnOnBlocking(boolean)}.
199 * <p>
200 * This should only be rarely called when you are <em>absolutely sure</em>
201 * the remote interface is a built-in system component that can never be
202 * upgraded. In particular, this <em>must never</em> be called for
203 * interfaces hosted by package that could be upgraded or replaced,
204 * otherwise you risk system instability if that remote interface wedges.
205 *
206 * @hide
207 */
208 public static IBinder allowBlocking(IBinder binder) {
209 try {
210 if (binder instanceof BinderProxy) {
211 ((BinderProxy) binder).mWarnOnBlocking = false;
Dianne Hackbornc81983a2017-10-20 16:16:32 -0700212 } else if (binder != null && binder.getInterfaceDescriptor() != null
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600213 && binder.queryLocalInterface(binder.getInterfaceDescriptor()) == null) {
214 Log.w(TAG, "Unable to allow blocking on interface " + binder);
215 }
216 } catch (RemoteException ignored) {
217 }
218 return binder;
219 }
220
221 /**
Jeff Sharkey59189482017-11-09 18:13:43 -0700222 * Reset the given interface back to the default blocking behavior,
223 * reverting any changes made by {@link #allowBlocking(IBinder)}.
224 *
225 * @hide
226 */
227 public static IBinder defaultBlocking(IBinder binder) {
228 if (binder instanceof BinderProxy) {
229 ((BinderProxy) binder).mWarnOnBlocking = sWarnOnBlocking;
230 }
231 return binder;
232 }
233
234 /**
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600235 * Inherit the current {@link #allowBlocking(IBinder)} value from one given
236 * interface to another.
237 *
238 * @hide
239 */
240 public static void copyAllowBlocking(IBinder fromBinder, IBinder toBinder) {
241 if (fromBinder instanceof BinderProxy && toBinder instanceof BinderProxy) {
242 ((BinderProxy) toBinder).mWarnOnBlocking = ((BinderProxy) fromBinder).mWarnOnBlocking;
243 }
244 }
245
Hans Boehm5e5b13f2017-09-28 18:16:50 -0700246 /**
247 * Raw native pointer to JavaBBinderHolder object. Owned by this Java object. Not null.
248 */
249 private final long mObject;
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 private IInterface mOwner;
252 private String mDescriptor;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
255 * Return the ID of the process that sent you the current transaction
256 * that is being processed. This pid can be used with higher-level
257 * system services to determine its identity and check permissions.
258 * If the current thread is not currently executing an incoming transaction,
259 * then its own pid is returned.
260 */
Olivier Gaillardd8c3df52018-10-23 09:58:42 +0100261 @CriticalNative
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 public static final native int getCallingPid();
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
Dianne Hackborn74ee8652012-09-07 18:33:18 -0700265 * Return the Linux uid assigned to the process that sent you the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * current transaction that is being processed. This uid can be used with
267 * higher-level system services to determine its identity and check
268 * permissions. If the current thread is not currently executing an
269 * incoming transaction, then its own uid is returned.
270 */
Olivier Gaillardd8c3df52018-10-23 09:58:42 +0100271 @CriticalNative
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 public static final native int getCallingUid();
Amith Yamasani742a6712011-05-04 14:49:28 -0700273
274 /**
Dianne Hackborn74ee8652012-09-07 18:33:18 -0700275 * Return the UserHandle assigned to the process that sent you the
276 * current transaction that is being processed. This is the user
277 * of the caller. It is distinct from {@link #getCallingUid()} in that a
278 * particular user will have multiple distinct apps running under it each
279 * with their own uid. If the current thread is not currently executing an
280 * incoming transaction, then its own UserHandle is returned.
281 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700282 public static final @NonNull UserHandle getCallingUserHandle() {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700283 return UserHandle.of(UserHandle.getUserId(getCallingUid()));
Dianne Hackborn74ee8652012-09-07 18:33:18 -0700284 }
285
286 /**
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700287 * Reset the identity of the incoming IPC on the current thread. This can
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * be useful if, while handling an incoming call, you will be calling
289 * on interfaces of other objects that may be local to your process and
290 * need to do permission checks on the calls coming into them (so they
291 * will check the permission of your own local process, and not whatever
292 * process originally called you).
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700293 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 * @return Returns an opaque token that can be used to restore the
295 * original calling identity by passing it to
296 * {@link #restoreCallingIdentity(long)}.
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700297 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 * @see #getCallingPid()
299 * @see #getCallingUid()
300 * @see #restoreCallingIdentity(long)
301 */
Olivier Gaillardd8c3df52018-10-23 09:58:42 +0100302 @CriticalNative
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 public static final native long clearCallingIdentity();
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700306 * Restore the identity of the incoming IPC on the current thread
307 * back to a previously identity that was returned by {@link
308 * #clearCallingIdentity}.
309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 * @param token The opaque token that was previously returned by
311 * {@link #clearCallingIdentity}.
Brad Fitzpatricka0527f22010-03-25 20:40:34 -0700312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * @see #clearCallingIdentity
314 */
315 public static final native void restoreCallingIdentity(long token);
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700316
317 /**
Eugene Susla6a7006a2017-03-13 12:57:58 -0700318 * Convenience method for running the provided action enclosed in
319 * {@link #clearCallingIdentity}/{@link #restoreCallingIdentity}
320 *
Eugene Suslacf00ade2017-04-10 11:51:58 -0700321 * Any exception thrown by the given action will be caught and rethrown after the call to
322 * {@link #restoreCallingIdentity}
323 *
Eugene Susla6a7006a2017-03-13 12:57:58 -0700324 * @hide
325 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700326 public static final void withCleanCallingIdentity(@NonNull ThrowingRunnable action) {
Eugene Susla6a7006a2017-03-13 12:57:58 -0700327 long callingIdentity = clearCallingIdentity();
Eugene Suslacf00ade2017-04-10 11:51:58 -0700328 Throwable throwableToPropagate = null;
Eugene Susla6a7006a2017-03-13 12:57:58 -0700329 try {
Eugene Susla2f5ee712017-06-23 17:25:24 -0700330 action.runOrThrow();
Eugene Suslacf00ade2017-04-10 11:51:58 -0700331 } catch (Throwable throwable) {
332 throwableToPropagate = throwable;
Eugene Susla6a7006a2017-03-13 12:57:58 -0700333 } finally {
334 restoreCallingIdentity(callingIdentity);
Eugene Suslacf00ade2017-04-10 11:51:58 -0700335 if (throwableToPropagate != null) {
336 throw ExceptionUtils.propagate(throwableToPropagate);
337 }
Eugene Susla6a7006a2017-03-13 12:57:58 -0700338 }
339 }
340
341 /**
342 * Convenience method for running the provided action enclosed in
343 * {@link #clearCallingIdentity}/{@link #restoreCallingIdentity} returning the result
344 *
Eugene Suslacf00ade2017-04-10 11:51:58 -0700345 * Any exception thrown by the given action will be caught and rethrown after the call to
346 * {@link #restoreCallingIdentity}
347 *
Eugene Susla6a7006a2017-03-13 12:57:58 -0700348 * @hide
349 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700350 public static final <T> T withCleanCallingIdentity(@NonNull ThrowingSupplier<T> action) {
Eugene Susla6a7006a2017-03-13 12:57:58 -0700351 long callingIdentity = clearCallingIdentity();
Eugene Suslacf00ade2017-04-10 11:51:58 -0700352 Throwable throwableToPropagate = null;
Eugene Susla6a7006a2017-03-13 12:57:58 -0700353 try {
Eugene Susla2f5ee712017-06-23 17:25:24 -0700354 return action.getOrThrow();
Eugene Suslacf00ade2017-04-10 11:51:58 -0700355 } catch (Throwable throwable) {
356 throwableToPropagate = throwable;
357 return null; // overridden by throwing in finally block
Eugene Susla6a7006a2017-03-13 12:57:58 -0700358 } finally {
359 restoreCallingIdentity(callingIdentity);
Eugene Suslacf00ade2017-04-10 11:51:58 -0700360 if (throwableToPropagate != null) {
361 throw ExceptionUtils.propagate(throwableToPropagate);
362 }
Eugene Susla6a7006a2017-03-13 12:57:58 -0700363 }
364 }
365
366 /**
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700367 * Sets the native thread-local StrictMode policy mask.
368 *
369 * <p>The StrictMode settings are kept in two places: a Java-level
370 * threadlocal for libcore/Dalvik, and a native threadlocal (set
371 * here) for propagation via Binder calls. This is a little
372 * unfortunate, but necessary to break otherwise more unfortunate
373 * dependencies either of Dalvik on Android, or Android
374 * native-only code on Dalvik.
375 *
376 * @see StrictMode
377 * @hide
378 */
Olivier Gaillardd8c3df52018-10-23 09:58:42 +0100379 @CriticalNative
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700380 public static final native void setThreadStrictModePolicy(int policyMask);
381
382 /**
383 * Gets the current native thread-local StrictMode policy mask.
384 *
385 * @see #setThreadStrictModePolicy
386 * @hide
387 */
Olivier Gaillardd8c3df52018-10-23 09:58:42 +0100388 @CriticalNative
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700389 public static final native int getThreadStrictModePolicy();
390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100392 * Sets the work source for this thread.
393 *
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000394 * <p>All the following binder calls on this thread will use the provided work source. If this
395 * is called during an on-going binder transaction, all the following binder calls will use the
396 * work source until the end of the transaction.
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100397 *
398 * <p>The concept of worksource is similar to {@link WorkSource}. However, for performance
399 * reasons, we only support one UID. This UID represents the original user responsible for the
400 * binder calls.
401 *
402 * <p>A typical use case would be
403 * <pre>
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000404 * long token = Binder.setCallingWorkSourceUid(uid);
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100405 * try {
406 * // Call an API.
407 * } finally {
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000408 * Binder.restoreCallingWorkSource(token);
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100409 * }
410 * </pre>
411 *
Olivier Gaillard1d724582018-11-19 14:35:37 +0000412 * <p>The work source will be propagated for future outgoing binder transactions
413 * executed on this thread.
414 *
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100415 * @param workSource The original UID responsible for the binder call.
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000416 * @return token to restore original work source.
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100417 * @hide
418 **/
419 @CriticalNative
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000420 public static final native long setCallingWorkSourceUid(int workSource);
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100421
422 /**
423 * Returns the work source set by the caller.
424 *
425 * Unlike {@link Binder#getCallingUid()}, this result of this method cannot be trusted. The
426 * caller can set the value to whatever he wants. Only use this value if you trust the calling
427 * uid.
428 *
429 * @return The original UID responsible for the binder transaction.
430 * @hide
431 */
432 @CriticalNative
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000433 public static final native int getCallingWorkSourceUid();
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100434
435 /**
436 * Clears the work source on this thread.
437 *
Olivier Gaillard1d724582018-11-19 14:35:37 +0000438 * <p>The work source will be propagated for future outgoing binder transactions
439 * executed on this thread.
440 *
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000441 * @return token to restore original work source.
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100442 * @hide
443 **/
444 @CriticalNative
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000445 public static final native long clearCallingWorkSource();
446
447 /**
448 * Restores the work source on this thread using a token returned by
449 * {@link #setCallingWorkSourceUid(int) or {@link clearCallingWorkSource()}.
450 *
451 * <p>A typical use case would be
452 * <pre>
453 * long token = Binder.setCallingWorkSourceUid(uid);
454 * try {
455 * // Call an API.
456 * } finally {
457 * Binder.restoreCallingWorkSource(token);
458 * }
459 * </pre>
Olivier Gaillard1d724582018-11-19 14:35:37 +0000460 *
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000461 * @hide
462 **/
463 @CriticalNative
464 public static final native void restoreCallingWorkSource(long token);
Olivier Gaillarde4ff3972018-08-16 14:01:58 +0100465
466 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 * Flush any Binder commands pending in the current thread to the kernel
468 * driver. This can be
469 * useful to call before performing an operation that may block for a long
470 * time, to ensure that any pending object references have been released
471 * in order to prevent the process from holding on to objects longer than
472 * it needs to.
473 */
474 public static final native void flushPendingCommands();
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 /**
477 * Add the calling thread to the IPC thread pool. This function does
478 * not return until the current process is exiting.
479 */
Andreas Gampeacd19872018-03-16 15:58:24 -0700480 public static final void joinThreadPool() {
481 BinderInternal.joinThreadPool();
482 }
Jeff Brown1951ce82013-04-04 22:45:12 -0700483
484 /**
485 * Returns true if the specified interface is a proxy.
486 * @hide
487 */
488 public static final boolean isProxy(IInterface iface) {
489 return iface.asBinder() != iface;
490 }
491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 /**
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700493 * Call blocks until the number of executing binder threads is less
494 * than the maximum number of binder threads allowed for this process.
Wale Ogunwale8d906342015-04-15 09:10:03 -0700495 * @hide
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700496 */
497 public static final native void blockUntilThreadAvailable();
498
499 /**
Martijn Coenend2c86ab2018-07-17 16:30:40 +0200500 * Default constructor just initializes the object.
501 *
502 * If you're creating a Binder token (a Binder object without an attached interface),
503 * you should use {@link #Binder(String)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 */
505 public Binder() {
Martijn Coenend2c86ab2018-07-17 16:30:40 +0200506 this(null);
507 }
508
509 /**
510 * Constructor for creating a raw Binder object (token) along with a descriptor.
511 *
512 * The descriptor of binder objects usually specifies the interface they are implementing.
513 * In case of binder tokens, no interface is implemented, and the descriptor can be used
514 * as a sort of tag to help identify the binder token. This will help identify remote
515 * references to these objects more easily when debugging.
516 *
517 * @param descriptor Used to identify the creator of this token, for example the class name.
518 * Instead of creating multiple tokens with the same descriptor, consider adding a suffix to
519 * help identify them.
520 */
521 public Binder(@Nullable String descriptor) {
Hans Boehm5e5b13f2017-09-28 18:16:50 -0700522 mObject = getNativeBBinderHolder();
523 NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524
525 if (FIND_POTENTIAL_LEAKS) {
526 final Class<? extends Binder> klass = getClass();
527 if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
528 (klass.getModifiers() & Modifier.STATIC) == 0) {
529 Log.w(TAG, "The following Binder class should be static or leaks might occur: " +
530 klass.getCanonicalName());
531 }
532 }
Martijn Coenend2c86ab2018-07-17 16:30:40 +0200533 mDescriptor = descriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 /**
537 * Convenience method for associating a specific interface with the Binder.
538 * After calling, queryLocalInterface() will be implemented for you
539 * to return the given owner IInterface when the corresponding
540 * descriptor is requested.
541 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700542 public void attachInterface(@Nullable IInterface owner, @Nullable String descriptor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 mOwner = owner;
544 mDescriptor = descriptor;
545 }
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 /**
548 * Default implementation returns an empty interface name.
549 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700550 public @Nullable String getInterfaceDescriptor() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 return mDescriptor;
552 }
553
554 /**
555 * Default implementation always returns true -- if you got here,
556 * the object is alive.
557 */
558 public boolean pingBinder() {
559 return true;
560 }
561
562 /**
563 * {@inheritDoc}
564 *
565 * Note that if you're calling on a local binder, this always returns true
566 * because your process is alive if you're calling it.
567 */
568 public boolean isBinderAlive() {
569 return true;
570 }
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 /**
573 * Use information supplied to attachInterface() to return the
574 * associated IInterface if it matches the requested
575 * descriptor.
576 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700577 public @Nullable IInterface queryLocalInterface(@NonNull String descriptor) {
Dianne Hackbornc81983a2017-10-20 16:16:32 -0700578 if (mDescriptor != null && mDescriptor.equals(descriptor)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 return mOwner;
580 }
581 return null;
582 }
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700583
584 /**
585 * Control disabling of dump calls in this process. This is used by the system
586 * process watchdog to disable incoming dump calls while it has detecting the system
587 * is hung and is reporting that back to the activity controller. This is to
588 * prevent the controller from getting hung up on bug reports at this point.
589 * @hide
590 *
591 * @param msg The message to show instead of the dump; if null, dumps are
592 * re-enabled.
593 */
594 public static void setDumpDisabled(String msg) {
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600595 sDumpDisabled = msg;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700596 }
597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 /**
Olivier Gaillard510cdfc2018-09-17 09:35:32 +0100599 * Listener to be notified about each proxy-side binder call.
600 *
601 * See {@link setProxyTransactListener}.
602 * @hide
603 */
604 public interface ProxyTransactListener {
605 /**
606 * Called before onTransact.
607 *
608 * @return an object that will be passed back to #onTransactEnded (or null).
609 */
610 Object onTransactStarted(IBinder binder, int transactionCode);
611
612 /**
613 * Called after onTranact (even when an exception is thrown).
614 *
615 * @param session The object return by #onTransactStarted.
616 */
617 void onTransactEnded(@Nullable Object session);
618 }
619
620 /**
Olivier Gaillarddef1b902018-10-17 17:10:41 +0100621 * Propagates the work source to binder calls executed by the system server.
622 *
623 * <li>By default, this listener will propagate the worksource if the outgoing call happens on
624 * the same thread as the incoming binder call.
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000625 * <li>Custom attribution can be done by calling {@link ThreadLocalWorkSource#setUid(int)}.
Olivier Gaillarddef1b902018-10-17 17:10:41 +0100626 * @hide
627 */
628 public static class PropagateWorkSourceTransactListener implements ProxyTransactListener {
629 @Override
630 public Object onTransactStarted(IBinder binder, int transactionCode) {
631 // Note that {@link Binder#getCallingUid()} is already set to the UID of the current
632 // process when this method is called.
633 //
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000634 // We use ThreadLocalWorkSource instead. It also allows feature owners to set
635 // {@link ThreadLocalWorkSource#set(int) manually to attribute resources to a UID.
636 int uid = ThreadLocalWorkSource.getUid();
637 if (uid != ThreadLocalWorkSource.UID_NONE) {
638 return Binder.setCallingWorkSourceUid(uid);
Olivier Gaillarddef1b902018-10-17 17:10:41 +0100639 }
640 return null;
641 }
642
643 @Override
644 public void onTransactEnded(Object session) {
645 if (session != null) {
Olivier Gaillardd542b1c2018-11-14 15:24:35 +0000646 long token = (long) session;
647 Binder.restoreCallingWorkSource(token);
Olivier Gaillarddef1b902018-10-17 17:10:41 +0100648 }
649 }
650 }
651
652 /**
Olivier Gaillard510cdfc2018-09-17 09:35:32 +0100653 * Sets a listener for the transact method on the proxy-side.
654 *
655 * <li>The listener is global. Only fast operations should be done to avoid thread
656 * contentions.
657 * <li>The listener implementation needs to handle synchronization if needed. The methods on the
658 * listener can be called concurrently.
659 * <li>Listener set will be used for new transactions. On-going transaction will still use the
660 * previous listener (if already set).
661 * <li>The listener is called on the critical path of the binder transaction so be careful about
662 * performance.
663 * <li>Never execute another binder transaction inside the listener.
664 * @hide
665 */
666 public static void setProxyTransactListener(@Nullable ProxyTransactListener listener) {
667 BinderProxy.setTransactListener(listener);
668 }
669
670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * Default implementation is a stub that returns false. You will want
672 * to override this to do the appropriate unmarshalling of transactions.
673 *
674 * <p>If you want to call this, call transact().
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700675 *
Dianne Hackborn18482ae2017-08-01 17:41:00 -0700676 * <p>Implementations that are returning a result should generally use
677 * {@link Parcel#writeNoException() Parcel.writeNoException} and
678 * {@link Parcel#writeException(Exception) Parcel.writeException} to propagate
679 * exceptions back to the caller.</p>
680 *
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700681 * @param code The action to perform. This should
682 * be a number between {@link #FIRST_CALL_TRANSACTION} and
683 * {@link #LAST_CALL_TRANSACTION}.
684 * @param data Marshalled data being received from the caller.
685 * @param reply If the caller is expecting a result back, it should be marshalled
686 * in to here.
687 * @param flags Additional operation flags. Either 0 for a normal
688 * RPC, or {@link #FLAG_ONEWAY} for a one-way RPC.
689 *
690 * @return Return true on a successful call; returning false is generally used to
691 * indicate that you did not understand the transaction code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700693 protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 int flags) throws RemoteException {
695 if (code == INTERFACE_TRANSACTION) {
696 reply.writeString(getInterfaceDescriptor());
697 return true;
698 } else if (code == DUMP_TRANSACTION) {
699 ParcelFileDescriptor fd = data.readFileDescriptor();
700 String[] args = data.readStringArray();
701 if (fd != null) {
702 try {
703 dump(fd.getFileDescriptor(), args);
704 } finally {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700705 IoUtils.closeQuietly(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707 }
Brad Fitzpatrickeb75888e2010-07-26 17:47:45 -0700708 // Write the StrictMode header.
709 if (reply != null) {
710 reply.writeNoException();
711 } else {
712 StrictMode.clearGatheredViolations();
713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 return true;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700715 } else if (code == SHELL_COMMAND_TRANSACTION) {
716 ParcelFileDescriptor in = data.readFileDescriptor();
717 ParcelFileDescriptor out = data.readFileDescriptor();
718 ParcelFileDescriptor err = data.readFileDescriptor();
719 String[] args = data.readStringArray();
Dianne Hackborn354736e2016-08-22 17:00:05 -0700720 ShellCallback shellCallback = ShellCallback.CREATOR.createFromParcel(data);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700721 ResultReceiver resultReceiver = ResultReceiver.CREATOR.createFromParcel(data);
722 try {
723 if (out != null) {
724 shellCommand(in != null ? in.getFileDescriptor() : null,
725 out.getFileDescriptor(),
726 err != null ? err.getFileDescriptor() : out.getFileDescriptor(),
Dianne Hackborn354736e2016-08-22 17:00:05 -0700727 args, shellCallback, resultReceiver);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700728 }
729 } finally {
730 IoUtils.closeQuietly(in);
731 IoUtils.closeQuietly(out);
732 IoUtils.closeQuietly(err);
733 // Write the StrictMode header.
734 if (reply != null) {
735 reply.writeNoException();
736 } else {
737 StrictMode.clearGatheredViolations();
738 }
739 }
740 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742 return false;
743 }
744
745 /**
Olivier Gaillardd3d065d2018-07-05 15:07:35 +0100746 * Resolves a transaction code to a human readable name.
747 *
748 * <p>Default implementation is a stub that returns null.
749 * <p>AIDL generated code will return the original method name.
750 *
751 * @param transactionCode The code to resolve.
752 * @return A human readable name.
753 * @hide
754 */
755 public @Nullable String getTransactionName(int transactionCode) {
756 return null;
757 }
758
759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 * Implemented to call the more convenient version
761 * {@link #dump(FileDescriptor, PrintWriter, String[])}.
762 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700763 public void dump(@NonNull FileDescriptor fd, @Nullable String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborndb4e33f2013-04-01 17:28:16 -0700765 PrintWriter pw = new FastPrintWriter(fout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 try {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700767 doDump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 } finally {
769 pw.flush();
770 }
771 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700772
773 void doDump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600774 final String disabled = sDumpDisabled;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700775 if (disabled == null) {
776 try {
777 dump(fd, pw, args);
778 } catch (SecurityException e) {
779 pw.println("Security exception: " + e.getMessage());
780 throw e;
781 } catch (Throwable e) {
782 // Unlike usual calls, in this case if an exception gets thrown
783 // back to us we want to print it back in to the dump data, since
784 // that is where the caller expects all interesting information to
785 // go.
786 pw.println();
787 pw.println("Exception occurred while dumping:");
788 e.printStackTrace(pw);
789 }
790 } else {
791 pw.println(sDumpDisabled);
792 }
793 }
794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 /**
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700796 * Like {@link #dump(FileDescriptor, String[])}, but ensures the target
797 * executes asynchronously.
798 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700799 public void dumpAsync(@NonNull final FileDescriptor fd, @Nullable final String[] args) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700800 final FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborndb4e33f2013-04-01 17:28:16 -0700801 final PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700802 Thread thr = new Thread("Binder.dumpAsync") {
803 public void run() {
804 try {
805 dump(fd, pw, args);
806 } finally {
807 pw.flush();
808 }
809 }
810 };
811 thr.start();
812 }
813
814 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 * Print the object's state into the given stream.
Olivier Gaillard9429bf52018-05-15 23:25:03 +0100816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 * @param fd The raw file descriptor that the dump is being sent to.
818 * @param fout The file to which you should dump your state. This will be
819 * closed for you after you return.
820 * @param args additional arguments to the dump request.
821 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700822 protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter fout,
823 @Nullable String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
825
826 /**
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700827 * @param in The raw file descriptor that an input data stream can be read from.
828 * @param out The raw file descriptor that normal command messages should be written to.
829 * @param err The raw file descriptor that command error messages should be written to.
830 * @param args Command-line arguments.
Dianne Hackborn354736e2016-08-22 17:00:05 -0700831 * @param callback Callback through which to interact with the invoking shell.
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700832 * @param resultReceiver Called when the command has finished executing, with the result code.
833 * @throws RemoteException
834 * @hide
835 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700836 public void shellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
837 @Nullable FileDescriptor err,
838 @NonNull String[] args, @Nullable ShellCallback callback,
839 @NonNull ResultReceiver resultReceiver) throws RemoteException {
Dianne Hackborn354736e2016-08-22 17:00:05 -0700840 onShellCommand(in, out, err, args, callback, resultReceiver);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700841 }
842
843 /**
844 * Handle a call to {@link #shellCommand}. The default implementation simply prints
845 * an error message. Override and replace with your own.
Dianne Hackborn2e931f52016-01-28 12:21:17 -0800846 * <p class="caution">Note: no permission checking is done before calling this method; you must
847 * apply any security checks as appropriate for the command being executed.
848 * Consider using {@link ShellCommand} to help in the implementation.</p>
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700849 * @hide
850 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700851 public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
852 @Nullable FileDescriptor err,
853 @NonNull String[] args, @Nullable ShellCallback callback,
854 @NonNull ResultReceiver resultReceiver) throws RemoteException {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700855 FileOutputStream fout = new FileOutputStream(err != null ? err : out);
856 PrintWriter pw = new FastPrintWriter(fout);
857 pw.println("No shell command implementation.");
858 pw.flush();
859 resultReceiver.send(0, null);
860 }
861
862 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 * Default implementation rewinds the parcels and calls onTransact. On
864 * the remote side, transact calls into the binder to do the IPC.
865 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700866 public final boolean transact(int code, @NonNull Parcel data, @Nullable Parcel reply,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 int flags) throws RemoteException {
Joe Onorato43a17652011-04-06 19:22:23 -0700868 if (false) Log.v("Binder", "Transact: " + code + " to " + this);
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (data != null) {
871 data.setDataPosition(0);
872 }
873 boolean r = onTransact(code, data, reply, flags);
874 if (reply != null) {
875 reply.setDataPosition(0);
876 }
877 return r;
878 }
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 /**
881 * Local implementation is a no-op.
882 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700883 public void linkToDeath(@NonNull DeathRecipient recipient, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 }
885
886 /**
887 * Local implementation is a no-op.
888 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700889 public boolean unlinkToDeath(@NonNull DeathRecipient recipient, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 return true;
891 }
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700892
Dianne Hackbornfad079d2014-09-26 15:46:24 -0700893 static void checkParcel(IBinder obj, int code, Parcel parcel, String msg) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -0700894 if (CHECK_PARCEL_SIZE && parcel.dataSize() >= 800*1024) {
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700895 // Trying to send > 800k, this is way too much
Dianne Hackbornfad079d2014-09-26 15:46:24 -0700896 StringBuilder sb = new StringBuilder();
897 sb.append(msg);
898 sb.append(": on ");
899 sb.append(obj);
900 sb.append(" calling ");
901 sb.append(code);
902 sb.append(" size ");
903 sb.append(parcel.dataSize());
904 sb.append(" (data: ");
905 parcel.setDataPosition(0);
906 sb.append(parcel.readInt());
907 sb.append(", ");
908 sb.append(parcel.readInt());
909 sb.append(", ");
910 sb.append(parcel.readInt());
911 sb.append(")");
912 Slog.wtfStack(TAG, sb.toString());
Dianne Hackborn017c6a22014-09-25 17:41:34 -0700913 }
914 }
915
Hans Boehm5e5b13f2017-09-28 18:16:50 -0700916 private static native long getNativeBBinderHolder();
917 private static native long getFinalizer();
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700918
919 // Entry point from android_util_Binder.cpp's onTransact
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000920 private boolean execTransact(int code, long dataObj, long replyObj,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 int flags) {
Olivier Gaillard180f91f2018-12-05 15:20:02 +0000922 final long origWorkSource = ThreadLocalWorkSource.setUid(Binder.getCallingUid());
923 try {
924 return execTransactInternal(code, dataObj, replyObj, flags);
925 } finally {
926 ThreadLocalWorkSource.restore(origWorkSource);
927 }
928 }
929
930 private boolean execTransactInternal(int code, long dataObj, long replyObj,
931 int flags) {
Olivier Gaillard289ba402018-07-24 18:50:13 +0100932 // Make sure the observer won't change while processing a transaction.
933 final BinderInternal.Observer observer = sObserver;
934 final CallSession callSession =
935 observer != null ? observer.callStarted(this, code) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 Parcel data = Parcel.obtain(dataObj);
937 Parcel reply = Parcel.obtain(replyObj);
938 // theoretically, we should call transact, which will call onTransact,
939 // but all that does is rewind it, and we just got these from an IPC,
940 // so we'll just call it directly.
941 boolean res;
Igor Murashkin7eb6cfe2013-08-16 14:07:11 -0700942 // Log any exceptions as warnings, don't silently suppress them.
943 // If the call was FLAG_ONEWAY then these exceptions disappear into the ether.
Jorim Jaggi25ee0bc2016-11-01 16:17:22 -0700944 final boolean tracingEnabled = Binder.isTracingEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 try {
Jorim Jaggi25ee0bc2016-11-01 16:17:22 -0700946 if (tracingEnabled) {
Olivier Gaillard53f645062018-10-12 15:41:50 +0100947 final String transactionName = getTransactionName(code);
948 Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, getClass().getName() + ":"
949 + (transactionName != null ? transactionName : code));
Jorim Jaggi25ee0bc2016-11-01 16:17:22 -0700950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 res = onTransact(code, data, reply, flags);
Makoto Onukif9b941f2016-03-10 17:19:08 -0800952 } catch (RemoteException|RuntimeException e) {
Olivier Gaillard289ba402018-07-24 18:50:13 +0100953 if (observer != null) {
954 observer.callThrewException(callSession, e);
955 }
Makoto Onukif9b941f2016-03-10 17:19:08 -0800956 if (LOG_RUNTIME_EXCEPTION) {
Igor Murashkin7eb6cfe2013-08-16 14:07:11 -0700957 Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e);
Makoto Onukif9b941f2016-03-10 17:19:08 -0800958 }
959 if ((flags & FLAG_ONEWAY) != 0) {
960 if (e instanceof RemoteException) {
961 Log.w(TAG, "Binder call failed.", e);
962 } else {
963 Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e);
964 }
Dianne Hackbornce92b0d2014-09-30 11:28:18 -0700965 } else {
966 reply.setDataPosition(0);
967 reply.writeException(e);
Igor Murashkin7eb6cfe2013-08-16 14:07:11 -0700968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 res = true;
Jorim Jaggi25ee0bc2016-11-01 16:17:22 -0700970 } finally {
971 if (tracingEnabled) {
972 Trace.traceEnd(Trace.TRACE_TAG_ALWAYS);
973 }
Olivier Gaillard0e4d61e2018-12-05 15:30:35 +0000974 if (observer != null) {
975 observer.callEnded(callSession, data.dataSize(), reply.dataSize());
976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 }
Dianne Hackbornfad079d2014-09-26 15:46:24 -0700978 checkParcel(this, code, reply, "Unreasonably large binder reply buffer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 reply.recycle();
980 data.recycle();
Dianne Hackbornce92b0d2014-09-30 11:28:18 -0700981
982 // Just in case -- we are done with the IPC, so there should be no more strict
983 // mode violations that have gathered for this thread. Either they have been
984 // parceled and are now in transport off to the caller, or we are returning back
985 // to the main transaction loop to wait for another incoming transaction. Either
986 // way, strict mode begone!
987 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 return res;
989 }
990}