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; |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 21 | import android.util.ExceptionUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.util.Log; |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 23 | import android.util.Slog; |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 24 | |
Michael Wachenschwanz | 58ac218 | 2018-01-30 15:11:19 -0800 | [diff] [blame] | 25 | import com.android.internal.os.BinderInternal; |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 26 | import com.android.internal.os.BinderInternal.CallSession; |
Dianne Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 27 | import com.android.internal.util.FastPrintWriter; |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 28 | import com.android.internal.util.FunctionalUtils.ThrowingRunnable; |
| 29 | import com.android.internal.util.FunctionalUtils.ThrowingSupplier; |
| 30 | |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 31 | import dalvik.annotation.optimization.CriticalNative; |
| 32 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 33 | import libcore.io.IoUtils; |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 34 | import libcore.util.NativeAllocationRegistry; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | import java.io.FileDescriptor; |
| 37 | import java.io.FileOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import java.io.PrintWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import 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 Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 45 | * standard local implementation of such an object. |
| 46 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | * <p>Most developers will not implement this class directly, instead using the |
Scott Main | 40eee61 | 2012-08-06 17:48:37 -0700 | [diff] [blame] | 48 | * <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] | 49 | * 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 Hackborn | ab4a81b | 2014-10-09 17:59:38 -0700 | [diff] [blame] | 53 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | * @see IBinder |
| 72 | */ |
| 73 | public 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 Sharkey | f5299f1 | 2017-03-17 10:25:07 -0600 | [diff] [blame] | 80 | /** @hide */ |
| 81 | public static final boolean CHECK_PARCEL_SIZE = false; |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 82 | static final String TAG = "Binder"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 84 | /** @hide */ |
| 85 | public static boolean LOG_RUNTIME_EXCEPTION = false; // DO NOT SUBMIT WITH TRUE |
| 86 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 87 | /** |
Olivier Gaillard | c17d280 | 2018-11-19 17:08:17 +0000 | [diff] [blame] | 88 | * 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 Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 97 | * Control whether dump() calls are allowed. |
| 98 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 99 | private static volatile String sDumpDisabled = null; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 100 | |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 101 | /** |
| 102 | * Global transaction tracker instance for this process. |
| 103 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 104 | private static volatile TransactionTracker sTransactionTracker = null; |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 105 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 106 | /** |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 107 | * Global observer for this process. |
| 108 | */ |
| 109 | private static BinderInternal.Observer sObserver = null; |
| 110 | |
| 111 | /** |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 112 | * 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 Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 125 | |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 126 | // Transaction tracking code. |
| 127 | |
| 128 | /** |
| 129 | * Flag indicating whether we should be tracing transact calls. |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 130 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 131 | private static volatile boolean sTracingEnabled = false; |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Enable Binder IPC tracing. |
| 135 | * |
| 136 | * @hide |
| 137 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 138 | public static void enableTracing() { |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 139 | sTracingEnabled = true; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 140 | } |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Disable Binder IPC tracing. |
| 144 | * |
| 145 | * @hide |
| 146 | */ |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 147 | public static void disableTracing() { |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 148 | 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 Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 171 | /** |
| 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 Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 180 | /** {@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 Hackborn | c81983a | 2017-10-20 16:16:32 -0700 | [diff] [blame] | 212 | } else if (binder != null && binder.getInterfaceDescriptor() != null |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 213 | && 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 Sharkey | 5918948 | 2017-11-09 18:13:43 -0700 | [diff] [blame] | 222 | * 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 Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 235 | * 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 Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 246 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | private IInterface mOwner; |
| 252 | private String mDescriptor; |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | /** |
| 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 Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 261 | @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | public static final native int getCallingPid(); |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | /** |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 265 | * 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] | 266 | * 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 Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 271 | @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | public static final native int getCallingUid(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 275 | * 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 Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 282 | public static final @NonNull UserHandle getCallingUserHandle() { |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 283 | return UserHandle.of(UserHandle.getUserId(getCallingUid())); |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 287 | * 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] | 288 | * 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 Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 293 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | * @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 Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 297 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | * @see #getCallingPid() |
| 299 | * @see #getCallingUid() |
| 300 | * @see #restoreCallingIdentity(long) |
| 301 | */ |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 302 | @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | public static final native long clearCallingIdentity(); |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 304 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | /** |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 306 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | * @param token The opaque token that was previously returned by |
| 311 | * {@link #clearCallingIdentity}. |
Brad Fitzpatrick | a0527f2 | 2010-03-25 20:40:34 -0700 | [diff] [blame] | 312 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | * @see #clearCallingIdentity |
| 314 | */ |
| 315 | public static final native void restoreCallingIdentity(long token); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 318 | * Convenience method for running the provided action enclosed in |
| 319 | * {@link #clearCallingIdentity}/{@link #restoreCallingIdentity} |
| 320 | * |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 321 | * Any exception thrown by the given action will be caught and rethrown after the call to |
| 322 | * {@link #restoreCallingIdentity} |
| 323 | * |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 324 | * @hide |
| 325 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 326 | public static final void withCleanCallingIdentity(@NonNull ThrowingRunnable action) { |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 327 | long callingIdentity = clearCallingIdentity(); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 328 | Throwable throwableToPropagate = null; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 329 | try { |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 330 | action.runOrThrow(); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 331 | } catch (Throwable throwable) { |
| 332 | throwableToPropagate = throwable; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 333 | } finally { |
| 334 | restoreCallingIdentity(callingIdentity); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 335 | if (throwableToPropagate != null) { |
| 336 | throw ExceptionUtils.propagate(throwableToPropagate); |
| 337 | } |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Convenience method for running the provided action enclosed in |
| 343 | * {@link #clearCallingIdentity}/{@link #restoreCallingIdentity} returning the result |
| 344 | * |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 345 | * Any exception thrown by the given action will be caught and rethrown after the call to |
| 346 | * {@link #restoreCallingIdentity} |
| 347 | * |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 348 | * @hide |
| 349 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 350 | public static final <T> T withCleanCallingIdentity(@NonNull ThrowingSupplier<T> action) { |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 351 | long callingIdentity = clearCallingIdentity(); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 352 | Throwable throwableToPropagate = null; |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 353 | try { |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 354 | return action.getOrThrow(); |
Eugene Susla | cf00ade | 2017-04-10 11:51:58 -0700 | [diff] [blame] | 355 | } catch (Throwable throwable) { |
| 356 | throwableToPropagate = throwable; |
| 357 | return null; // overridden by throwing in finally block |
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 | /** |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 367 | * 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 Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 379 | @CriticalNative |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 380 | 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 Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 388 | @CriticalNative |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 389 | public static final native int getThreadStrictModePolicy(); |
| 390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | /** |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 392 | * Sets the work source for this thread. |
| 393 | * |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 394 | * <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 Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 397 | * |
| 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 Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 404 | * long token = Binder.setCallingWorkSourceUid(uid); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 405 | * try { |
| 406 | * // Call an API. |
| 407 | * } finally { |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 408 | * Binder.restoreCallingWorkSource(token); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 409 | * } |
| 410 | * </pre> |
| 411 | * |
Olivier Gaillard | 1d72458 | 2018-11-19 14:35:37 +0000 | [diff] [blame] | 412 | * <p>The work source will be propagated for future outgoing binder transactions |
| 413 | * executed on this thread. |
| 414 | * |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 415 | * @param workSource The original UID responsible for the binder call. |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 416 | * @return token to restore original work source. |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 417 | * @hide |
| 418 | **/ |
| 419 | @CriticalNative |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 420 | public static final native long setCallingWorkSourceUid(int workSource); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 421 | |
| 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 Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 433 | public static final native int getCallingWorkSourceUid(); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 434 | |
| 435 | /** |
| 436 | * Clears the work source on this thread. |
| 437 | * |
Olivier Gaillard | 1d72458 | 2018-11-19 14:35:37 +0000 | [diff] [blame] | 438 | * <p>The work source will be propagated for future outgoing binder transactions |
| 439 | * executed on this thread. |
| 440 | * |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 441 | * @return token to restore original work source. |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 442 | * @hide |
| 443 | **/ |
| 444 | @CriticalNative |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 445 | 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 Gaillard | 1d72458 | 2018-11-19 14:35:37 +0000 | [diff] [blame] | 460 | * |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 461 | * @hide |
| 462 | **/ |
| 463 | @CriticalNative |
| 464 | public static final native void restoreCallingWorkSource(long token); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 465 | |
| 466 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | * 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 Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | /** |
| 477 | * Add the calling thread to the IPC thread pool. This function does |
| 478 | * not return until the current process is exiting. |
| 479 | */ |
Andreas Gampe | acd1987 | 2018-03-16 15:58:24 -0700 | [diff] [blame] | 480 | public static final void joinThreadPool() { |
| 481 | BinderInternal.joinThreadPool(); |
| 482 | } |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame] | 483 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | /** |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 493 | * Call blocks until the number of executing binder threads is less |
| 494 | * than the maximum number of binder threads allowed for this process. |
Wale Ogunwale | 8d90634 | 2015-04-15 09:10:03 -0700 | [diff] [blame] | 495 | * @hide |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 496 | */ |
| 497 | public static final native void blockUntilThreadAvailable(); |
| 498 | |
| 499 | /** |
Martijn Coenen | d2c86ab | 2018-07-17 16:30:40 +0200 | [diff] [blame] | 500 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | */ |
| 505 | public Binder() { |
Martijn Coenen | d2c86ab | 2018-07-17 16:30:40 +0200 | [diff] [blame] | 506 | 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 Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 522 | mObject = getNativeBBinderHolder(); |
| 523 | NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | |
| 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 Coenen | d2c86ab | 2018-07-17 16:30:40 +0200 | [diff] [blame] | 533 | mDescriptor = descriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | } |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 535 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | /** |
| 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 Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 542 | public void attachInterface(@Nullable IInterface owner, @Nullable String descriptor) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | mOwner = owner; |
| 544 | mDescriptor = descriptor; |
| 545 | } |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | /** |
| 548 | * Default implementation returns an empty interface name. |
| 549 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 550 | public @Nullable String getInterfaceDescriptor() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | 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 Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | /** |
| 573 | * Use information supplied to attachInterface() to return the |
| 574 | * associated IInterface if it matches the requested |
| 575 | * descriptor. |
| 576 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 577 | public @Nullable IInterface queryLocalInterface(@NonNull String descriptor) { |
Dianne Hackborn | c81983a | 2017-10-20 16:16:32 -0700 | [diff] [blame] | 578 | if (mDescriptor != null && mDescriptor.equals(descriptor)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | return mOwner; |
| 580 | } |
| 581 | return null; |
| 582 | } |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 583 | |
| 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 Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 595 | sDumpDisabled = msg; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 596 | } |
| 597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | /** |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 599 | * 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 Gaillard | def1b90 | 2018-10-17 17:10:41 +0100 | [diff] [blame] | 621 | * 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 Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 625 | * <li>Custom attribution can be done by calling {@link ThreadLocalWorkSource#setUid(int)}. |
Olivier Gaillard | def1b90 | 2018-10-17 17:10:41 +0100 | [diff] [blame] | 626 | * @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 Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 634 | // 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 Gaillard | def1b90 | 2018-10-17 17:10:41 +0100 | [diff] [blame] | 639 | } |
| 640 | return null; |
| 641 | } |
| 642 | |
| 643 | @Override |
| 644 | public void onTransactEnded(Object session) { |
| 645 | if (session != null) { |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 646 | long token = (long) session; |
| 647 | Binder.restoreCallingWorkSource(token); |
Olivier Gaillard | def1b90 | 2018-10-17 17:10:41 +0100 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /** |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 653 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | * 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 Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 675 | * |
Dianne Hackborn | 18482ae | 2017-08-01 17:41:00 -0700 | [diff] [blame] | 676 | * <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 Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 681 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 693 | 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] | 694 | 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 Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 705 | IoUtils.closeQuietly(fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | } |
| 707 | } |
Brad Fitzpatrick | eb75888e | 2010-07-26 17:47:45 -0700 | [diff] [blame] | 708 | // Write the StrictMode header. |
| 709 | if (reply != null) { |
| 710 | reply.writeNoException(); |
| 711 | } else { |
| 712 | StrictMode.clearGatheredViolations(); |
| 713 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | return true; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 715 | } 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 Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 720 | ShellCallback shellCallback = ShellCallback.CREATOR.createFromParcel(data); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 721 | 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 Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 727 | args, shellCallback, resultReceiver); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 728 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | } |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | /** |
Olivier Gaillard | d3d065d | 2018-07-05 15:07:35 +0100 | [diff] [blame] | 746 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | * Implemented to call the more convenient version |
| 761 | * {@link #dump(FileDescriptor, PrintWriter, String[])}. |
| 762 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 763 | public void dump(@NonNull FileDescriptor fd, @Nullable String[] args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | FileOutputStream fout = new FileOutputStream(fd); |
Dianne Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 765 | PrintWriter pw = new FastPrintWriter(fout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | try { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 767 | doDump(fd, pw, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | } finally { |
| 769 | pw.flush(); |
| 770 | } |
| 771 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 772 | |
| 773 | void doDump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 774 | final String disabled = sDumpDisabled; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 775 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | /** |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 796 | * Like {@link #dump(FileDescriptor, String[])}, but ensures the target |
| 797 | * executes asynchronously. |
| 798 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 799 | public void dumpAsync(@NonNull final FileDescriptor fd, @Nullable final String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 800 | final FileOutputStream fout = new FileOutputStream(fd); |
Dianne Hackborn | db4e33f | 2013-04-01 17:28:16 -0700 | [diff] [blame] | 801 | final PrintWriter pw = new FastPrintWriter(fout); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 802 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | * Print the object's state into the given stream. |
Olivier Gaillard | 9429bf5 | 2018-05-15 23:25:03 +0100 | [diff] [blame] | 816 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | * @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 Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 822 | protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter fout, |
| 823 | @Nullable String[] args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /** |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 827 | * @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 Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 831 | * @param callback Callback through which to interact with the invoking shell. |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 832 | * @param resultReceiver Called when the command has finished executing, with the result code. |
| 833 | * @throws RemoteException |
| 834 | * @hide |
| 835 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 836 | 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 Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 840 | onShellCommand(in, out, err, args, callback, resultReceiver); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 841 | } |
| 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 Hackborn | 2e931f5 | 2016-01-28 12:21:17 -0800 | [diff] [blame] | 846 | * <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 Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 849 | * @hide |
| 850 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 851 | 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 Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 855 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | * Default implementation rewinds the parcels and calls onTransact. On |
| 864 | * the remote side, transact calls into the binder to do the IPC. |
| 865 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 866 | 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] | 867 | int flags) throws RemoteException { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 868 | if (false) Log.v("Binder", "Transact: " + code + " to " + this); |
Rahul Chaturvedi | 52613f9 | 2015-06-17 23:54:08 -0400 | [diff] [blame] | 869 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | 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 Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | /** |
| 881 | * Local implementation is a no-op. |
| 882 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 883 | public void linkToDeath(@NonNull DeathRecipient recipient, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Local implementation is a no-op. |
| 888 | */ |
Dianne Hackborn | 4cd650c | 2017-07-31 17:38:53 -0700 | [diff] [blame] | 889 | public boolean unlinkToDeath(@NonNull DeathRecipient recipient, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | return true; |
| 891 | } |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 892 | |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 893 | static void checkParcel(IBinder obj, int code, Parcel parcel, String msg) { |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 894 | if (CHECK_PARCEL_SIZE && parcel.dataSize() >= 800*1024) { |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 895 | // Trying to send > 800k, this is way too much |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 896 | 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 Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 916 | private static native long getNativeBBinderHolder(); |
| 917 | private static native long getFinalizer(); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 918 | |
| 919 | // Entry point from android_util_Binder.cpp's onTransact |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 920 | private boolean execTransact(int code, long dataObj, long replyObj, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | int flags) { |
Olivier Gaillard | 180f91f | 2018-12-05 15:20:02 +0000 | [diff] [blame] | 922 | 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 Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 932 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | 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 Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 942 | // 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 Jaggi | 25ee0bc | 2016-11-01 16:17:22 -0700 | [diff] [blame] | 944 | final boolean tracingEnabled = Binder.isTracingEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | try { |
Jorim Jaggi | 25ee0bc | 2016-11-01 16:17:22 -0700 | [diff] [blame] | 946 | if (tracingEnabled) { |
Olivier Gaillard | 53f64506 | 2018-10-12 15:41:50 +0100 | [diff] [blame] | 947 | final String transactionName = getTransactionName(code); |
| 948 | Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, getClass().getName() + ":" |
| 949 | + (transactionName != null ? transactionName : code)); |
Jorim Jaggi | 25ee0bc | 2016-11-01 16:17:22 -0700 | [diff] [blame] | 950 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | res = onTransact(code, data, reply, flags); |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 952 | } catch (RemoteException|RuntimeException e) { |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 953 | if (observer != null) { |
| 954 | observer.callThrewException(callSession, e); |
| 955 | } |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 956 | if (LOG_RUNTIME_EXCEPTION) { |
Igor Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 957 | Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e); |
Makoto Onuki | f9b941f | 2016-03-10 17:19:08 -0800 | [diff] [blame] | 958 | } |
| 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 Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 965 | } else { |
| 966 | reply.setDataPosition(0); |
| 967 | reply.writeException(e); |
Igor Murashkin | 7eb6cfe | 2013-08-16 14:07:11 -0700 | [diff] [blame] | 968 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | res = true; |
Jorim Jaggi | 25ee0bc | 2016-11-01 16:17:22 -0700 | [diff] [blame] | 970 | } finally { |
| 971 | if (tracingEnabled) { |
| 972 | Trace.traceEnd(Trace.TRACE_TAG_ALWAYS); |
| 973 | } |
Olivier Gaillard | 0e4d61e | 2018-12-05 15:30:35 +0000 | [diff] [blame^] | 974 | if (observer != null) { |
| 975 | observer.callEnded(callSession, data.dataSize(), reply.dataSize()); |
| 976 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | } |
Dianne Hackborn | fad079d | 2014-09-26 15:46:24 -0700 | [diff] [blame] | 978 | checkParcel(this, code, reply, "Unreasonably large binder reply buffer"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | reply.recycle(); |
| 980 | data.recycle(); |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 981 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | return res; |
| 989 | } |
| 990 | } |