The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 22 | import android.util.ExceptionUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.util.Log; |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 24 | import android.util.Slog; |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 25 | |
Michael Wachenschwanz | 58ac218 | 2018-01-30 15:11:19 -0800 | [diff] [blame] | 26 | import com.android.internal.os.BinderInternal; |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 27 | import com.android.internal.os.BinderInternal.CallSession; |
Dianne Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 28 | import com.android.internal.util.FastPrintWriter; |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 29 | import com.android.internal.util.FunctionalUtils.ThrowingRunnable; |
| 30 | import com.android.internal.util.FunctionalUtils.ThrowingSupplier; |
| 31 | |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 32 | import dalvik.annotation.optimization.CriticalNative; |
| 33 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 34 | import libcore.io.IoUtils; |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 35 | import libcore.util.NativeAllocationRegistry; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | import java.io.FileDescriptor; |
| 38 | import java.io.FileOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import java.io.PrintWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import java.lang.reflect.Modifier; |
| 41 | |
| 42 | /** |
| 43 | * Base class for a remotable object, the core part of a lightweight |
| 44 | * remote procedure call mechanism defined by {@link IBinder}. |
| 45 | * This class is an implementation of IBinder that provides |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 46 | * standard local implementation of such an object. |
| 47 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | * <p>Most developers will not implement this class directly, instead using the |
Scott Main | 40eee61 | 2012-08-06 17:48:37 -0700 | [diff] [blame] | 49 | * <a href="{@docRoot}guide/components/aidl.html">aidl</a> tool to describe the desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * interface, having it generate the appropriate Binder subclass. You can, |
| 51 | * however, derive directly from Binder to implement your own custom RPC |
| 52 | * protocol or simply instantiate a raw Binder object directly to use as a |
| 53 | * token that can be shared across processes. |
Dianne Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 54 | * |
| 55 | * <p>This class is just a basic IPC primitive; it has no impact on an application's |
| 56 | * lifecycle, and is valid only as long as the process that created it continues to run. |
| 57 | * To use this correctly, you must be doing so within the context of a top-level |
| 58 | * application component (a {@link android.app.Service}, {@link android.app.Activity}, |
| 59 | * or {@link android.content.ContentProvider}) that lets the system know your process |
| 60 | * should remain running.</p> |
| 61 | * |
| 62 | * <p>You must keep in mind the situations in which your process |
| 63 | * could go away, and thus require that you later re-create a new Binder and re-attach |
| 64 | * it when the process starts again. For example, if you are using this within an |
| 65 | * {@link android.app.Activity}, your activity's process may be killed any time the |
| 66 | * activity is not started; if the activity is later re-created you will need to |
| 67 | * create a new Binder and hand it back to the correct place again; you need to be |
| 68 | * aware that your process may be started for another reason (for example to receive |
| 69 | * a broadcast) that will not involve re-creating the activity and thus run its code |
| 70 | * to create a new Binder.</p> |
| 71 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | * @see IBinder |
| 73 | */ |
| 74 | public class Binder implements IBinder { |
| 75 | /* |
| 76 | * Set this flag to true to detect anonymous, local or member classes |
| 77 | * that extend this Binder class and that are not static. These kind |
| 78 | * of classes can potentially create leaks. |
| 79 | */ |
| 80 | private static final boolean FIND_POTENTIAL_LEAKS = false; |
Jeff Sharkey | f5299f1 | 2017-03-17 10:25:07 -0600 | [diff] [blame] | 81 | /** @hide */ |
| 82 | public static final boolean CHECK_PARCEL_SIZE = false; |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 83 | static final String TAG = "Binder"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 85 | /** @hide */ |
| 86 | public static boolean LOG_RUNTIME_EXCEPTION = false; // DO NOT SUBMIT WITH TRUE |
| 87 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 88 | /** |
Olivier Gaillard | c17d280 | 2018-11-19 17:08:17 +0000 | [diff] [blame] | 89 | * Value to represents that a calling work source is not set. |
| 90 | * |
| 91 | * This constatnt needs to be kept in sync with IPCThreadState::kUnsetWorkSource. |
| 92 | * |
| 93 | * @hide |
| 94 | */ |
| 95 | public static final int UNSET_WORKSOURCE = -1; |
| 96 | |
| 97 | /** |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 98 | * Control whether dump() calls are allowed. |
| 99 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 100 | private static volatile String sDumpDisabled = null; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 101 | |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 102 | /** |
| 103 | * Global transaction tracker instance for this process. |
| 104 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 105 | private static volatile TransactionTracker sTransactionTracker = null; |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 106 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 107 | /** |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 108 | * Global observer for this process. |
| 109 | */ |
| 110 | private static BinderInternal.Observer sObserver = null; |
| 111 | |
| 112 | /** |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 113 | * Guestimate of native memory associated with a Binder. |
| 114 | */ |
| 115 | private static final int NATIVE_ALLOCATION_SIZE = 500; |
| 116 | |
| 117 | private static native long getNativeFinalizer(); |
| 118 | |
| 119 | // Use a Holder to allow static initialization of Binder in the boot image, and |
| 120 | // possibly to avoid some initialization ordering issues. |
| 121 | private static class NoImagePreloadHolder { |
| 122 | public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry( |
| 123 | Binder.class.getClassLoader(), getNativeFinalizer(), NATIVE_ALLOCATION_SIZE); |
| 124 | } |
| 125 | |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 126 | |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 127 | // Transaction tracking code. |
| 128 | |
| 129 | /** |
| 130 | * Flag indicating whether we should be tracing transact calls. |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 131 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 132 | private static volatile boolean sTracingEnabled = false; |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Enable Binder IPC tracing. |
| 136 | * |
| 137 | * @hide |
| 138 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 139 | public static void enableTracing() { |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 140 | sTracingEnabled = true; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 141 | } |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Disable Binder IPC tracing. |
| 145 | * |
| 146 | * @hide |
| 147 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 148 | public static void disableTracing() { |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 149 | sTracingEnabled = false; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Check if binder transaction tracing is enabled. |
| 154 | * |
| 155 | * @hide |
| 156 | */ |
| 157 | public static boolean isTracingEnabled() { |
| 158 | return sTracingEnabled; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the binder transaction tracker for this process. |
| 163 | * |
| 164 | * @hide |
| 165 | */ |
| 166 | public synchronized static TransactionTracker getTransactionTracker() { |
| 167 | if (sTransactionTracker == null) |
| 168 | sTransactionTracker = new TransactionTracker(); |
| 169 | return sTransactionTracker; |
| 170 | } |
| 171 | |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 172 | /** |
| 173 | * Get the binder transaction observer for this process. |
| 174 | * |
| 175 | * @hide |
| 176 | */ |
| 177 | public static void setObserver(@Nullable BinderInternal.Observer observer) { |
| 178 | sObserver = observer; |
| 179 | } |
| 180 | |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 181 | /** {@hide} */ |
| 182 | static volatile boolean sWarnOnBlocking = false; |
| 183 | |
| 184 | /** |
| 185 | * Warn if any blocking binder transactions are made out from this process. |
| 186 | * This is typically only useful for the system process, to prevent it from |
| 187 | * blocking on calls to external untrusted code. Instead, all outgoing calls |
| 188 | * that require a result must be sent as {@link IBinder#FLAG_ONEWAY} calls |
| 189 | * which deliver results through a callback interface. |
| 190 | * |
| 191 | * @hide |
| 192 | */ |
| 193 | public static void setWarnOnBlocking(boolean warnOnBlocking) { |
| 194 | sWarnOnBlocking = warnOnBlocking; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Allow blocking calls on the given interface, overriding the requested |
| 199 | * value of {@link #setWarnOnBlocking(boolean)}. |
| 200 | * <p> |
| 201 | * This should only be rarely called when you are <em>absolutely sure</em> |
| 202 | * the remote interface is a built-in system component that can never be |
| 203 | * upgraded. In particular, this <em>must never</em> be called for |
| 204 | * interfaces hosted by package that could be upgraded or replaced, |
| 205 | * otherwise you risk system instability if that remote interface wedges. |
| 206 | * |
| 207 | * @hide |
| 208 | */ |
| 209 | public static IBinder allowBlocking(IBinder binder) { |
| 210 | try { |
| 211 | if (binder instanceof BinderProxy) { |
| 212 | ((BinderProxy) binder).mWarnOnBlocking = false; |
Dianne Hackborn | c81983a | 2017-10-20 16:16:32 -0700 | [diff] [blame] | 213 | } else if (binder != null && binder.getInterfaceDescriptor() != null |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 214 | && binder.queryLocalInterface(binder.getInterfaceDescriptor()) == null) { |
| 215 | Log.w(TAG, "Unable to allow blocking on interface " + binder); |
| 216 | } |
| 217 | } catch (RemoteException ignored) { |
| 218 | } |
| 219 | return binder; |
| 220 | } |
| 221 | |
| 222 | /** |
Jeff Sharkey | 5918948 | 2017-11-09 18:13:43 -0700 | [diff] [blame] | 223 | * Reset the given interface back to the default blocking behavior, |
| 224 | * reverting any changes made by {@link #allowBlocking(IBinder)}. |
| 225 | * |
| 226 | * @hide |
| 227 | */ |
| 228 | public static IBinder defaultBlocking(IBinder binder) { |
| 229 | if (binder instanceof BinderProxy) { |
| 230 | ((BinderProxy) binder).mWarnOnBlocking = sWarnOnBlocking; |
| 231 | } |
| 232 | return binder; |
| 233 | } |
| 234 | |
| 235 | /** |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 236 | * Inherit the current {@link #allowBlocking(IBinder)} value from one given |
| 237 | * interface to another. |
| 238 | * |
| 239 | * @hide |
| 240 | */ |
| 241 | public static void copyAllowBlocking(IBinder fromBinder, IBinder toBinder) { |
| 242 | if (fromBinder instanceof BinderProxy && toBinder instanceof BinderProxy) { |
| 243 | ((BinderProxy) toBinder).mWarnOnBlocking = ((BinderProxy) fromBinder).mWarnOnBlocking; |
| 244 | } |
| 245 | } |
| 246 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Raw native pointer to JavaBBinderHolder object. Owned by this Java object. Not null. |
| 249 | */ |
| 250 | private final long mObject; |
| 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | private IInterface mOwner; |
| 253 | private String mDescriptor; |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Return the ID of the process that sent you the current transaction |
| 257 | * that is being processed. This pid can be used with higher-level |
| 258 | * system services to determine its identity and check permissions. |
| 259 | * If the current thread is not currently executing an incoming transaction, |
| 260 | * then its own pid is returned. |
| 261 | */ |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 262 | @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | public static final native int getCallingPid(); |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | /** |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 266 | * Return the Linux uid assigned to the process that sent you the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | * current transaction that is being processed. This uid can be used with |
| 268 | * higher-level system services to determine its identity and check |
| 269 | * permissions. If the current thread is not currently executing an |
| 270 | * incoming transaction, then its own uid is returned. |
| 271 | */ |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 272 | @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | public static final native int getCallingUid(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 274 | |
| 275 | /** |
Nikita Ioffe | a929cf0 | 2019-01-03 13:35:22 +0000 | [diff] [blame] | 276 | * Returns {@code true} if the current thread is currently executing an |
| 277 | * incoming transaction. |
| 278 | * |
| 279 | * @hide |
| 280 | */ |
| 281 | @CriticalNative |
| 282 | public static final native boolean isHandlingTransaction(); |
| 283 | |
| 284 | /** |
| 285 | * Return the Linux uid assigned to the process that sent the transaction |
| 286 | * currently being processed. |
| 287 | * |
| 288 | * @throws IllegalStateException if the current thread is not currently |
| 289 | * executing an incoming transaction. |
| 290 | */ |
| 291 | public static final int getCallingUidOrThrow() { |
| 292 | if (!isHandlingTransaction()) { |
| 293 | throw new IllegalStateException( |
| 294 | "Thread is not in a binder transcation"); |
| 295 | } |
| 296 | return getCallingUid(); |
| 297 | } |
| 298 | |
| 299 | /** |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 300 | * Return the UserHandle assigned to the process that sent you the |
| 301 | * current transaction that is being processed. This is the user |
| 302 | * of the caller. It is distinct from {@link #getCallingUid()} in that a |
| 303 | * particular user will have multiple distinct apps running under it each |
| 304 | * with their own uid. If the current thread is not currently executing an |
| 305 | * incoming transaction, then its own UserHandle is returned. |
| 306 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 307 | public static final @NonNull UserHandle getCallingUserHandle() { |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 308 | return UserHandle.of(UserHandle.getUserId(getCallingUid())); |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 312 | * Reset the identity of the incoming IPC on the current thread. This can |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | * be useful if, while handling an incoming call, you will be calling |
| 314 | * on interfaces of other objects that may be local to your process and |
| 315 | * need to do permission checks on the calls coming into them (so they |
| 316 | * will check the permission of your own local process, and not whatever |
| 317 | * process originally called you). |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 318 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | * @return Returns an opaque token that can be used to restore the |
| 320 | * original calling identity by passing it to |
| 321 | * {@link #restoreCallingIdentity(long)}. |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 322 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | * @see #getCallingPid() |
| 324 | * @see #getCallingUid() |
| 325 | * @see #restoreCallingIdentity(long) |
| 326 | */ |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 327 | @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | public static final native long clearCallingIdentity(); |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 329 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | /** |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 331 | * Restore the identity of the incoming IPC on the current thread |
| 332 | * back to a previously identity that was returned by {@link |
| 333 | * #clearCallingIdentity}. |
| 334 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | * @param token The opaque token that was previously returned by |
| 336 | * {@link #clearCallingIdentity}. |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 337 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | * @see #clearCallingIdentity |
| 339 | */ |
| 340 | public static final native void restoreCallingIdentity(long token); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 341 | |
| 342 | /** |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 343 | * Convenience method for running the provided action enclosed in |
| 344 | * {@link #clearCallingIdentity}/{@link #restoreCallingIdentity} |
| 345 | * |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 346 | * Any exception thrown by the given action will be caught and rethrown after the call to |
| 347 | * {@link #restoreCallingIdentity} |
| 348 | * |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 349 | * @hide |
| 350 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 351 | public static final void withCleanCallingIdentity(@NonNull ThrowingRunnable action) { |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 352 | long callingIdentity = clearCallingIdentity(); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 353 | Throwable throwableToPropagate = null; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 354 | try { |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 355 | action.runOrThrow(); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 356 | } catch (Throwable throwable) { |
| 357 | throwableToPropagate = throwable; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 358 | } finally { |
| 359 | restoreCallingIdentity(callingIdentity); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 360 | if (throwableToPropagate != null) { |
| 361 | throw ExceptionUtils.propagate(throwableToPropagate); |
| 362 | } |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Convenience method for running the provided action enclosed in |
| 368 | * {@link #clearCallingIdentity}/{@link #restoreCallingIdentity} returning the result |
| 369 | * |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 370 | * Any exception thrown by the given action will be caught and rethrown after the call to |
| 371 | * {@link #restoreCallingIdentity} |
| 372 | * |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 373 | * @hide |
| 374 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 375 | public static final <T> T withCleanCallingIdentity(@NonNull ThrowingSupplier<T> action) { |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 376 | long callingIdentity = clearCallingIdentity(); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 377 | Throwable throwableToPropagate = null; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 378 | try { |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 379 | return action.getOrThrow(); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 380 | } catch (Throwable throwable) { |
| 381 | throwableToPropagate = throwable; |
| 382 | return null; // overridden by throwing in finally block |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 383 | } finally { |
| 384 | restoreCallingIdentity(callingIdentity); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 385 | if (throwableToPropagate != null) { |
| 386 | throw ExceptionUtils.propagate(throwableToPropagate); |
| 387 | } |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 392 | * Sets the native thread-local StrictMode policy mask. |
| 393 | * |
| 394 | * <p>The StrictMode settings are kept in two places: a Java-level |
| 395 | * threadlocal for libcore/Dalvik, and a native threadlocal (set |
| 396 | * here) for propagation via Binder calls. This is a little |
| 397 | * unfortunate, but necessary to break otherwise more unfortunate |
| 398 | * dependencies either of Dalvik on Android, or Android |
| 399 | * native-only code on Dalvik. |
| 400 | * |
| 401 | * @see StrictMode |
| 402 | * @hide |
| 403 | */ |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 404 | @CriticalNative |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 405 | public static final native void setThreadStrictModePolicy(int policyMask); |
| 406 | |
| 407 | /** |
| 408 | * Gets the current native thread-local StrictMode policy mask. |
| 409 | * |
| 410 | * @see #setThreadStrictModePolicy |
| 411 | * @hide |
| 412 | */ |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 413 | @CriticalNative |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 414 | public static final native int getThreadStrictModePolicy(); |
| 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | /** |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 417 | * Sets the work source for this thread. |
| 418 | * |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 419 | * <p>All the following binder calls on this thread will use the provided work source. If this |
| 420 | * is called during an on-going binder transaction, all the following binder calls will use the |
| 421 | * work source until the end of the transaction. |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 422 | * |
| 423 | * <p>The concept of worksource is similar to {@link WorkSource}. However, for performance |
| 424 | * reasons, we only support one UID. This UID represents the original user responsible for the |
| 425 | * binder calls. |
| 426 | * |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 427 | * <p>{@link Binder#restoreCallingWorkSource(long)} must always be called after setting the |
| 428 | * worksource. |
| 429 | * |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 430 | * <p>A typical use case would be |
| 431 | * <pre> |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 432 | * long token = Binder.setCallingWorkSourceUid(uid); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 433 | * try { |
| 434 | * // Call an API. |
| 435 | * } finally { |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 436 | * Binder.restoreCallingWorkSource(token); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 437 | * } |
| 438 | * </pre> |
| 439 | * |
Olivier Gaillard | 1d72458 | 2018-11-19 14:35:37 +0000 | [diff] [blame] | 440 | * <p>The work source will be propagated for future outgoing binder transactions |
| 441 | * executed on this thread. |
| 442 | * |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 443 | * @param workSource The original UID responsible for the binder call. |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 444 | * @return token to restore original work source. |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 445 | * @hide |
| 446 | **/ |
| 447 | @CriticalNative |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 448 | @SystemApi |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 449 | public static final native long setCallingWorkSourceUid(int workSource); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 450 | |
| 451 | /** |
| 452 | * Returns the work source set by the caller. |
| 453 | * |
| 454 | * Unlike {@link Binder#getCallingUid()}, this result of this method cannot be trusted. The |
| 455 | * caller can set the value to whatever he wants. Only use this value if you trust the calling |
| 456 | * uid. |
| 457 | * |
| 458 | * @return The original UID responsible for the binder transaction. |
| 459 | * @hide |
| 460 | */ |
| 461 | @CriticalNative |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 462 | @SystemApi |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 463 | public static final native int getCallingWorkSourceUid(); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 464 | |
| 465 | /** |
| 466 | * Clears the work source on this thread. |
| 467 | * |
Olivier Gaillard | 1d72458 | 2018-11-19 14:35:37 +0000 | [diff] [blame] | 468 | * <p>The work source will be propagated for future outgoing binder transactions |
| 469 | * executed on this thread. |
| 470 | * |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 471 | * <p>{@link Binder#restoreCallingWorkSource(long)} must always be called after clearing the |
| 472 | * worksource. |
| 473 | * |
| 474 | * <p>A typical use case would be |
| 475 | * <pre> |
| 476 | * long token = Binder.clearCallingWorkSource(); |
| 477 | * try { |
| 478 | * // Call an API. |
| 479 | * } finally { |
| 480 | * Binder.restoreCallingWorkSource(token); |
| 481 | * } |
| 482 | * </pre> |
| 483 | * |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 484 | * @return token to restore original work source. |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 485 | * @hide |
| 486 | **/ |
| 487 | @CriticalNative |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 488 | @SystemApi |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 489 | public static final native long clearCallingWorkSource(); |
| 490 | |
| 491 | /** |
| 492 | * Restores the work source on this thread using a token returned by |
| 493 | * {@link #setCallingWorkSourceUid(int) or {@link clearCallingWorkSource()}. |
| 494 | * |
| 495 | * <p>A typical use case would be |
| 496 | * <pre> |
| 497 | * long token = Binder.setCallingWorkSourceUid(uid); |
| 498 | * try { |
| 499 | * // Call an API. |
| 500 | * } finally { |
| 501 | * Binder.restoreCallingWorkSource(token); |
| 502 | * } |
| 503 | * </pre> |
Olivier Gaillard | 1d72458 | 2018-11-19 14:35:37 +0000 | [diff] [blame] | 504 | * |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 505 | * @hide |
| 506 | **/ |
| 507 | @CriticalNative |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 508 | @SystemApi |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 509 | public static final native void restoreCallingWorkSource(long token); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 510 | |
| 511 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | * Flush any Binder commands pending in the current thread to the kernel |
| 513 | * driver. This can be |
| 514 | * useful to call before performing an operation that may block for a long |
| 515 | * time, to ensure that any pending object references have been released |
| 516 | * in order to prevent the process from holding on to objects longer than |
| 517 | * it needs to. |
| 518 | */ |
| 519 | public static final native void flushPendingCommands(); |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 520 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | /** |
| 522 | * Add the calling thread to the IPC thread pool. This function does |
| 523 | * not return until the current process is exiting. |
| 524 | */ |
Andreas Gampe | acd1987 | 2018-03-16 15:58:24 -0700 | [diff] [blame] | 525 | public static final void joinThreadPool() { |
| 526 | BinderInternal.joinThreadPool(); |
| 527 | } |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * Returns true if the specified interface is a proxy. |
| 531 | * @hide |
| 532 | */ |
| 533 | public static final boolean isProxy(IInterface iface) { |
| 534 | return iface.asBinder() != iface; |
| 535 | } |
| 536 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | /** |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 538 | * Call blocks until the number of executing binder threads is less |
| 539 | * than the maximum number of binder threads allowed for this process. |
Wale Ogunwale | 8d90634 | 2015-04-15 09:10:03 -0700 | [diff] [blame] | 540 | * @hide |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 541 | */ |
| 542 | public static final native void blockUntilThreadAvailable(); |
| 543 | |
| 544 | /** |
Martijn Coenen | d2c86ab | 2018-07-17 16:30:40 +0200 | [diff] [blame] | 545 | * Default constructor just initializes the object. |
| 546 | * |
| 547 | * If you're creating a Binder token (a Binder object without an attached interface), |
| 548 | * you should use {@link #Binder(String)} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | */ |
| 550 | public Binder() { |
Martijn Coenen | d2c86ab | 2018-07-17 16:30:40 +0200 | [diff] [blame] | 551 | this(null); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Constructor for creating a raw Binder object (token) along with a descriptor. |
| 556 | * |
| 557 | * The descriptor of binder objects usually specifies the interface they are implementing. |
| 558 | * In case of binder tokens, no interface is implemented, and the descriptor can be used |
| 559 | * as a sort of tag to help identify the binder token. This will help identify remote |
| 560 | * references to these objects more easily when debugging. |
| 561 | * |
| 562 | * @param descriptor Used to identify the creator of this token, for example the class name. |
| 563 | * Instead of creating multiple tokens with the same descriptor, consider adding a suffix to |
| 564 | * help identify them. |
| 565 | */ |
| 566 | public Binder(@Nullable String descriptor) { |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 567 | mObject = getNativeBBinderHolder(); |
| 568 | NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | |
| 570 | if (FIND_POTENTIAL_LEAKS) { |
| 571 | final Class<? extends Binder> klass = getClass(); |
| 572 | if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && |
| 573 | (klass.getModifiers() & Modifier.STATIC) == 0) { |
| 574 | Log.w(TAG, "The following Binder class should be static or leaks might occur: " + |
| 575 | klass.getCanonicalName()); |
| 576 | } |
| 577 | } |
Martijn Coenen | d2c86ab | 2018-07-17 16:30:40 +0200 | [diff] [blame] | 578 | mDescriptor = descriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | } |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 580 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | /** |
| 582 | * Convenience method for associating a specific interface with the Binder. |
| 583 | * After calling, queryLocalInterface() will be implemented for you |
| 584 | * to return the given owner IInterface when the corresponding |
| 585 | * descriptor is requested. |
| 586 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 587 | public void attachInterface(@Nullable IInterface owner, @Nullable String descriptor) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | mOwner = owner; |
| 589 | mDescriptor = descriptor; |
| 590 | } |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | /** |
| 593 | * Default implementation returns an empty interface name. |
| 594 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 595 | public @Nullable String getInterfaceDescriptor() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | return mDescriptor; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Default implementation always returns true -- if you got here, |
| 601 | * the object is alive. |
| 602 | */ |
| 603 | public boolean pingBinder() { |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * {@inheritDoc} |
| 609 | * |
| 610 | * Note that if you're calling on a local binder, this always returns true |
| 611 | * because your process is alive if you're calling it. |
| 612 | */ |
| 613 | public boolean isBinderAlive() { |
| 614 | return true; |
| 615 | } |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | /** |
| 618 | * Use information supplied to attachInterface() to return the |
| 619 | * associated IInterface if it matches the requested |
| 620 | * descriptor. |
| 621 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 622 | public @Nullable IInterface queryLocalInterface(@NonNull String descriptor) { |
Dianne Hackborn | c81983a | 2017-10-20 16:16:32 -0700 | [diff] [blame] | 623 | if (mDescriptor != null && mDescriptor.equals(descriptor)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | return mOwner; |
| 625 | } |
| 626 | return null; |
| 627 | } |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * Control disabling of dump calls in this process. This is used by the system |
| 631 | * process watchdog to disable incoming dump calls while it has detecting the system |
| 632 | * is hung and is reporting that back to the activity controller. This is to |
| 633 | * prevent the controller from getting hung up on bug reports at this point. |
| 634 | * @hide |
| 635 | * |
| 636 | * @param msg The message to show instead of the dump; if null, dumps are |
| 637 | * re-enabled. |
| 638 | */ |
| 639 | public static void setDumpDisabled(String msg) { |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 640 | sDumpDisabled = msg; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 641 | } |
| 642 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | /** |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 644 | * Listener to be notified about each proxy-side binder call. |
| 645 | * |
| 646 | * See {@link setProxyTransactListener}. |
| 647 | * @hide |
| 648 | */ |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 649 | @SystemApi |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 650 | public interface ProxyTransactListener { |
| 651 | /** |
| 652 | * Called before onTransact. |
| 653 | * |
| 654 | * @return an object that will be passed back to #onTransactEnded (or null). |
| 655 | */ |
Olivier Gaillard | 5a73a51 | 2019-03-05 16:32:35 +0000 | [diff] [blame] | 656 | @Nullable |
| 657 | Object onTransactStarted(@NonNull IBinder binder, int transactionCode); |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 658 | |
| 659 | /** |
| 660 | * Called after onTranact (even when an exception is thrown). |
| 661 | * |
| 662 | * @param session The object return by #onTransactStarted. |
| 663 | */ |
| 664 | void onTransactEnded(@Nullable Object session); |
| 665 | } |
| 666 | |
| 667 | /** |
Olivier Gaillard | def1b90 | 2018-10-17 17:10:41 +0100 | [diff] [blame] | 668 | * Propagates the work source to binder calls executed by the system server. |
| 669 | * |
| 670 | * <li>By default, this listener will propagate the worksource if the outgoing call happens on |
| 671 | * the same thread as the incoming binder call. |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 672 | * <li>Custom attribution can be done by calling {@link ThreadLocalWorkSource#setUid(int)}. |
Olivier Gaillard | def1b90 | 2018-10-17 17:10:41 +0100 | [diff] [blame] | 673 | * @hide |
| 674 | */ |
| 675 | public static class PropagateWorkSourceTransactListener implements ProxyTransactListener { |
| 676 | @Override |
| 677 | public Object onTransactStarted(IBinder binder, int transactionCode) { |
| 678 | // Note that {@link Binder#getCallingUid()} is already set to the UID of the current |
| 679 | // process when this method is called. |
| 680 | // |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 681 | // We use ThreadLocalWorkSource instead. It also allows feature owners to set |
| 682 | // {@link ThreadLocalWorkSource#set(int) manually to attribute resources to a UID. |
| 683 | int uid = ThreadLocalWorkSource.getUid(); |
| 684 | if (uid != ThreadLocalWorkSource.UID_NONE) { |
| 685 | return Binder.setCallingWorkSourceUid(uid); |
Olivier Gaillard | def1b90 | 2018-10-17 17:10:41 +0100 | [diff] [blame] | 686 | } |
| 687 | return null; |
| 688 | } |
| 689 | |
| 690 | @Override |
| 691 | public void onTransactEnded(Object session) { |
| 692 | if (session != null) { |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 693 | long token = (long) session; |
| 694 | Binder.restoreCallingWorkSource(token); |
Olivier Gaillard | def1b90 | 2018-10-17 17:10:41 +0100 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 700 | * Sets a listener for the transact method on the proxy-side. |
| 701 | * |
| 702 | * <li>The listener is global. Only fast operations should be done to avoid thread |
| 703 | * contentions. |
| 704 | * <li>The listener implementation needs to handle synchronization if needed. The methods on the |
| 705 | * listener can be called concurrently. |
| 706 | * <li>Listener set will be used for new transactions. On-going transaction will still use the |
| 707 | * previous listener (if already set). |
| 708 | * <li>The listener is called on the critical path of the binder transaction so be careful about |
| 709 | * performance. |
| 710 | * <li>Never execute another binder transaction inside the listener. |
| 711 | * @hide |
| 712 | */ |
Olivier Gaillard | 6dfdcf4 | 2018-12-03 17:51:46 +0000 | [diff] [blame] | 713 | @SystemApi |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 714 | public static void setProxyTransactListener(@Nullable ProxyTransactListener listener) { |
| 715 | BinderProxy.setTransactListener(listener); |
| 716 | } |
| 717 | |
| 718 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | * Default implementation is a stub that returns false. You will want |
| 720 | * to override this to do the appropriate unmarshalling of transactions. |
| 721 | * |
| 722 | * <p>If you want to call this, call transact(). |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 723 | * |
Dianne Hackborn | 18482ae | 2017-08-01 17:41:00 -0700 | [diff] [blame] | 724 | * <p>Implementations that are returning a result should generally use |
| 725 | * {@link Parcel#writeNoException() Parcel.writeNoException} and |
| 726 | * {@link Parcel#writeException(Exception) Parcel.writeException} to propagate |
| 727 | * exceptions back to the caller.</p> |
| 728 | * |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 729 | * @param code The action to perform. This should |
| 730 | * be a number between {@link #FIRST_CALL_TRANSACTION} and |
| 731 | * {@link #LAST_CALL_TRANSACTION}. |
| 732 | * @param data Marshalled data being received from the caller. |
| 733 | * @param reply If the caller is expecting a result back, it should be marshalled |
| 734 | * in to here. |
| 735 | * @param flags Additional operation flags. Either 0 for a normal |
| 736 | * RPC, or {@link #FLAG_ONEWAY} for a one-way RPC. |
| 737 | * |
| 738 | * @return Return true on a successful call; returning false is generally used to |
| 739 | * indicate that you did not understand the transaction code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 741 | protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | int flags) throws RemoteException { |
| 743 | if (code == INTERFACE_TRANSACTION) { |
| 744 | reply.writeString(getInterfaceDescriptor()); |
| 745 | return true; |
| 746 | } else if (code == DUMP_TRANSACTION) { |
| 747 | ParcelFileDescriptor fd = data.readFileDescriptor(); |
| 748 | String[] args = data.readStringArray(); |
| 749 | if (fd != null) { |
| 750 | try { |
| 751 | dump(fd.getFileDescriptor(), args); |
| 752 | } finally { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 753 | IoUtils.closeQuietly(fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
Brad Fitzpatrick | eb75888e | 2010-07-26 17:47:45 -0700 | [diff] [blame] | 756 | // Write the StrictMode header. |
| 757 | if (reply != null) { |
| 758 | reply.writeNoException(); |
| 759 | } else { |
| 760 | StrictMode.clearGatheredViolations(); |
| 761 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | return true; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 763 | } else if (code == SHELL_COMMAND_TRANSACTION) { |
| 764 | ParcelFileDescriptor in = data.readFileDescriptor(); |
| 765 | ParcelFileDescriptor out = data.readFileDescriptor(); |
| 766 | ParcelFileDescriptor err = data.readFileDescriptor(); |
| 767 | String[] args = data.readStringArray(); |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 768 | ShellCallback shellCallback = ShellCallback.CREATOR.createFromParcel(data); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 769 | ResultReceiver resultReceiver = ResultReceiver.CREATOR.createFromParcel(data); |
| 770 | try { |
| 771 | if (out != null) { |
| 772 | shellCommand(in != null ? in.getFileDescriptor() : null, |
| 773 | out.getFileDescriptor(), |
| 774 | err != null ? err.getFileDescriptor() : out.getFileDescriptor(), |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 775 | args, shellCallback, resultReceiver); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 776 | } |
| 777 | } finally { |
| 778 | IoUtils.closeQuietly(in); |
| 779 | IoUtils.closeQuietly(out); |
| 780 | IoUtils.closeQuietly(err); |
| 781 | // Write the StrictMode header. |
| 782 | if (reply != null) { |
| 783 | reply.writeNoException(); |
| 784 | } else { |
| 785 | StrictMode.clearGatheredViolations(); |
| 786 | } |
| 787 | } |
| 788 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | } |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | /** |
Olivier Gaillard | d3d065d | 2018-07-05 15:07:35 +0100 | [diff] [blame] | 794 | * Resolves a transaction code to a human readable name. |
| 795 | * |
| 796 | * <p>Default implementation is a stub that returns null. |
| 797 | * <p>AIDL generated code will return the original method name. |
| 798 | * |
| 799 | * @param transactionCode The code to resolve. |
| 800 | * @return A human readable name. |
| 801 | * @hide |
| 802 | */ |
| 803 | public @Nullable String getTransactionName(int transactionCode) { |
| 804 | return null; |
| 805 | } |
| 806 | |
| 807 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | * Implemented to call the more convenient version |
| 809 | * {@link #dump(FileDescriptor, PrintWriter, String[])}. |
| 810 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 811 | public void dump(@NonNull FileDescriptor fd, @Nullable String[] args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | FileOutputStream fout = new FileOutputStream(fd); |
Dianne Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 813 | PrintWriter pw = new FastPrintWriter(fout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | try { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 815 | doDump(fd, pw, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | } finally { |
| 817 | pw.flush(); |
| 818 | } |
| 819 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 820 | |
| 821 | void doDump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 822 | final String disabled = sDumpDisabled; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 823 | if (disabled == null) { |
| 824 | try { |
| 825 | dump(fd, pw, args); |
| 826 | } catch (SecurityException e) { |
| 827 | pw.println("Security exception: " + e.getMessage()); |
| 828 | throw e; |
| 829 | } catch (Throwable e) { |
| 830 | // Unlike usual calls, in this case if an exception gets thrown |
| 831 | // back to us we want to print it back in to the dump data, since |
| 832 | // that is where the caller expects all interesting information to |
| 833 | // go. |
| 834 | pw.println(); |
| 835 | pw.println("Exception occurred while dumping:"); |
| 836 | e.printStackTrace(pw); |
| 837 | } |
| 838 | } else { |
| 839 | pw.println(sDumpDisabled); |
| 840 | } |
| 841 | } |
| 842 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | /** |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 844 | * Like {@link #dump(FileDescriptor, String[])}, but ensures the target |
| 845 | * executes asynchronously. |
| 846 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 847 | public void dumpAsync(@NonNull final FileDescriptor fd, @Nullable final String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 848 | final FileOutputStream fout = new FileOutputStream(fd); |
Dianne Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 849 | final PrintWriter pw = new FastPrintWriter(fout); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 850 | Thread thr = new Thread("Binder.dumpAsync") { |
| 851 | public void run() { |
| 852 | try { |
| 853 | dump(fd, pw, args); |
| 854 | } finally { |
| 855 | pw.flush(); |
| 856 | } |
| 857 | } |
| 858 | }; |
| 859 | thr.start(); |
| 860 | } |
| 861 | |
| 862 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | * Print the object's state into the given stream. |
Olivier Gaillard | 9429bf5 | 2018-05-15 23:25:03 +0100 | [diff] [blame] | 864 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | * @param fd The raw file descriptor that the dump is being sent to. |
| 866 | * @param fout The file to which you should dump your state. This will be |
| 867 | * closed for you after you return. |
| 868 | * @param args additional arguments to the dump request. |
| 869 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 870 | protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter fout, |
| 871 | @Nullable String[] args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /** |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 875 | * @param in The raw file descriptor that an input data stream can be read from. |
| 876 | * @param out The raw file descriptor that normal command messages should be written to. |
| 877 | * @param err The raw file descriptor that command error messages should be written to. |
| 878 | * @param args Command-line arguments. |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 879 | * @param callback Callback through which to interact with the invoking shell. |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 880 | * @param resultReceiver Called when the command has finished executing, with the result code. |
| 881 | * @throws RemoteException |
| 882 | * @hide |
| 883 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 884 | public void shellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out, |
| 885 | @Nullable FileDescriptor err, |
| 886 | @NonNull String[] args, @Nullable ShellCallback callback, |
| 887 | @NonNull ResultReceiver resultReceiver) throws RemoteException { |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 888 | onShellCommand(in, out, err, args, callback, resultReceiver); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /** |
| 892 | * Handle a call to {@link #shellCommand}. The default implementation simply prints |
| 893 | * an error message. Override and replace with your own. |
Dianne Hackborn | 2e931f5 | 2016-01-28 12:21:17 -0800 | [diff] [blame] | 894 | * <p class="caution">Note: no permission checking is done before calling this method; you must |
| 895 | * apply any security checks as appropriate for the command being executed. |
| 896 | * Consider using {@link ShellCommand} to help in the implementation.</p> |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 897 | * @hide |
| 898 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 899 | public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out, |
| 900 | @Nullable FileDescriptor err, |
| 901 | @NonNull String[] args, @Nullable ShellCallback callback, |
| 902 | @NonNull ResultReceiver resultReceiver) throws RemoteException { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 903 | FileOutputStream fout = new FileOutputStream(err != null ? err : out); |
| 904 | PrintWriter pw = new FastPrintWriter(fout); |
| 905 | pw.println("No shell command implementation."); |
| 906 | pw.flush(); |
| 907 | resultReceiver.send(0, null); |
| 908 | } |
| 909 | |
| 910 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | * Default implementation rewinds the parcels and calls onTransact. On |
| 912 | * the remote side, transact calls into the binder to do the IPC. |
| 913 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 914 | public final boolean transact(int code, @NonNull Parcel data, @Nullable Parcel reply, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | int flags) throws RemoteException { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 916 | if (false) Log.v("Binder", "Transact: " + code + " to " + this); |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | if (data != null) { |
| 919 | data.setDataPosition(0); |
| 920 | } |
| 921 | boolean r = onTransact(code, data, reply, flags); |
| 922 | if (reply != null) { |
| 923 | reply.setDataPosition(0); |
| 924 | } |
| 925 | return r; |
| 926 | } |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 927 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | /** |
| 929 | * Local implementation is a no-op. |
| 930 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 931 | public void linkToDeath(@NonNull DeathRecipient recipient, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Local implementation is a no-op. |
| 936 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 937 | public boolean unlinkToDeath(@NonNull DeathRecipient recipient, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | return true; |
| 939 | } |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 940 | |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 941 | static void checkParcel(IBinder obj, int code, Parcel parcel, String msg) { |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 942 | if (CHECK_PARCEL_SIZE && parcel.dataSize() >= 800*1024) { |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 943 | // Trying to send > 800k, this is way too much |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 944 | StringBuilder sb = new StringBuilder(); |
| 945 | sb.append(msg); |
| 946 | sb.append(": on "); |
| 947 | sb.append(obj); |
| 948 | sb.append(" calling "); |
| 949 | sb.append(code); |
| 950 | sb.append(" size "); |
| 951 | sb.append(parcel.dataSize()); |
| 952 | sb.append(" (data: "); |
| 953 | parcel.setDataPosition(0); |
| 954 | sb.append(parcel.readInt()); |
| 955 | sb.append(", "); |
| 956 | sb.append(parcel.readInt()); |
| 957 | sb.append(", "); |
| 958 | sb.append(parcel.readInt()); |
| 959 | sb.append(")"); |
| 960 | Slog.wtfStack(TAG, sb.toString()); |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 964 | private static native long getNativeBBinderHolder(); |
| 965 | private static native long getFinalizer(); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 966 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 967 | /** |
| 968 | * By default, we use the calling uid since we can always trust it. |
| 969 | */ |
| 970 | private static volatile BinderInternal.WorkSourceProvider sWorkSourceProvider = |
Olivier Gaillard | bab444a | 2019-01-30 17:11:40 +0000 | [diff] [blame] | 971 | (x) -> Binder.getCallingUid(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 972 | |
| 973 | /** |
| 974 | * Sets the work source provider. |
| 975 | * |
| 976 | * <li>The callback is global. Only fast operations should be done to avoid thread |
| 977 | * contentions. |
| 978 | * <li>The callback implementation needs to handle synchronization if needed. The methods on the |
| 979 | * callback can be called concurrently. |
| 980 | * <li>The callback is called on the critical path of the binder transaction so be careful about |
| 981 | * performance. |
| 982 | * <li>Never execute another binder transaction inside the callback. |
| 983 | * @hide |
| 984 | */ |
| 985 | public static void setWorkSourceProvider(BinderInternal.WorkSourceProvider workSourceProvider) { |
| 986 | if (workSourceProvider == null) { |
| 987 | throw new IllegalArgumentException("workSourceProvider cannot be null"); |
| 988 | } |
| 989 | sWorkSourceProvider = workSourceProvider; |
| 990 | } |
| 991 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 992 | // Entry point from android_util_Binder.cpp's onTransact |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 993 | private boolean execTransact(int code, long dataObj, long replyObj, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | int flags) { |
Olivier Gaillard | bab444a | 2019-01-30 17:11:40 +0000 | [diff] [blame] | 995 | // At that point, the parcel request headers haven't been parsed so we do not know what |
| 996 | // WorkSource the caller has set. Use calling uid as the default. |
| 997 | final int callingUid = Binder.getCallingUid(); |
| 998 | final long origWorkSource = ThreadLocalWorkSource.setUid(callingUid); |
Olivier Gaillard | 180f91f | 2018-12-05 15:20:02 +0000 | [diff] [blame] | 999 | try { |
Olivier Gaillard | bab444a | 2019-01-30 17:11:40 +0000 | [diff] [blame] | 1000 | return execTransactInternal(code, dataObj, replyObj, flags, callingUid); |
Olivier Gaillard | 180f91f | 2018-12-05 15:20:02 +0000 | [diff] [blame] | 1001 | } finally { |
| 1002 | ThreadLocalWorkSource.restore(origWorkSource); |
| 1003 | } |
| 1004 | } |
| 1005 | |
Olivier Gaillard | bab444a | 2019-01-30 17:11:40 +0000 | [diff] [blame] | 1006 | private boolean execTransactInternal(int code, long dataObj, long replyObj, int flags, |
| 1007 | int callingUid) { |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 1008 | // Make sure the observer won't change while processing a transaction. |
| 1009 | final BinderInternal.Observer observer = sObserver; |
| 1010 | final CallSession callSession = |
Olivier Gaillard | bab444a | 2019-01-30 17:11:40 +0000 | [diff] [blame] | 1011 | observer != null ? observer.callStarted(this, code, UNSET_WORKSOURCE) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | Parcel data = Parcel.obtain(dataObj); |
| 1013 | Parcel reply = Parcel.obtain(replyObj); |
| 1014 | // theoretically, we should call transact, which will call onTransact, |
| 1015 | // but all that does is rewind it, and we just got these from an IPC, |
| 1016 | // so we'll just call it directly. |
| 1017 | boolean res; |
Igor Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 1018 | // Log any exceptions as warnings, don't silently suppress them. |
| 1019 | // If the call was FLAG_ONEWAY then these exceptions disappear into the ether. |
Jorim Jaggi | 25ee0bc | 2016-11-01 16:17:22 -0700 | [diff] [blame] | 1020 | final boolean tracingEnabled = Binder.isTracingEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | try { |
Jorim Jaggi | 25ee0bc | 2016-11-01 16:17:22 -0700 | [diff] [blame] | 1022 | if (tracingEnabled) { |
Olivier Gaillard | 53f64506 | 2018-10-12 15:41:50 +0100 | [diff] [blame] | 1023 | final String transactionName = getTransactionName(code); |
| 1024 | Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, getClass().getName() + ":" |
| 1025 | + (transactionName != null ? transactionName : code)); |
Jorim Jaggi | 25ee0bc | 2016-11-01 16:17:22 -0700 | [diff] [blame] | 1026 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | res = onTransact(code, data, reply, flags); |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 1028 | } catch (RemoteException|RuntimeException e) { |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 1029 | if (observer != null) { |
| 1030 | observer.callThrewException(callSession, e); |
| 1031 | } |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 1032 | if (LOG_RUNTIME_EXCEPTION) { |
Igor Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 1033 | Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e); |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 1034 | } |
| 1035 | if ((flags & FLAG_ONEWAY) != 0) { |
| 1036 | if (e instanceof RemoteException) { |
| 1037 | Log.w(TAG, "Binder call failed.", e); |
| 1038 | } else { |
| 1039 | Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e); |
| 1040 | } |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 1041 | } else { |
| 1042 | reply.setDataPosition(0); |
| 1043 | reply.writeException(e); |
Igor Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 1044 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | res = true; |
Jorim Jaggi | 25ee0bc | 2016-11-01 16:17:22 -0700 | [diff] [blame] | 1046 | } finally { |
| 1047 | if (tracingEnabled) { |
| 1048 | Trace.traceEnd(Trace.TRACE_TAG_ALWAYS); |
| 1049 | } |
Olivier Gaillard | 0e4d61e | 2018-12-05 15:30:35 +0000 | [diff] [blame] | 1050 | if (observer != null) { |
Olivier Gaillard | bab444a | 2019-01-30 17:11:40 +0000 | [diff] [blame] | 1051 | // The parcel RPC headers have been called during onTransact so we can now access |
| 1052 | // the worksource uid from the parcel. |
| 1053 | final int workSourceUid = sWorkSourceProvider.resolveWorkSourceUid( |
| 1054 | data.readCallingWorkSourceUid()); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 1055 | observer.callEnded(callSession, data.dataSize(), reply.dataSize(), workSourceUid); |
Olivier Gaillard | 0e4d61e | 2018-12-05 15:30:35 +0000 | [diff] [blame] | 1056 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | } |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 1058 | checkParcel(this, code, reply, "Unreasonably large binder reply buffer"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | reply.recycle(); |
| 1060 | data.recycle(); |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 1061 | |
| 1062 | // Just in case -- we are done with the IPC, so there should be no more strict |
| 1063 | // mode violations that have gathered for this thread. Either they have been |
| 1064 | // parceled and are now in transport off to the caller, or we are returning back |
| 1065 | // to the main transaction loop to wait for another incoming transaction. Either |
| 1066 | // way, strict mode begone! |
| 1067 | StrictMode.clearGatheredViolations(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | return res; |
| 1069 | } |
| 1070 | } |