Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include <android/binder_ibinder.h> |
| 18 | #include "AIBinder_internal.h" |
| 19 | |
| 20 | #include <android/binder_status.h> |
| 21 | #include "AParcel_internal.h" |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | |
| 25 | using ::android::IBinder; |
| 26 | using ::android::Parcel; |
| 27 | using ::android::sp; |
| 28 | using ::android::String16; |
| 29 | using ::android::wp; |
| 30 | |
| 31 | AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {} |
| 32 | AIBinder::~AIBinder() {} |
| 33 | |
| 34 | sp<AIBinder> AIBinder::associateClass(const AIBinder_Class* clazz) { |
| 35 | using ::android::String8; |
| 36 | |
| 37 | if (clazz == nullptr) return nullptr; |
| 38 | if (mClazz == clazz) return this; |
| 39 | |
| 40 | String8 newDescriptor(clazz->getInterfaceDescriptor()); |
| 41 | |
| 42 | if (mClazz != nullptr) { |
| 43 | String8 currentDescriptor(mClazz->getInterfaceDescriptor()); |
| 44 | if (newDescriptor == currentDescriptor) { |
| 45 | LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor |
| 46 | << "' match during associateClass, but they are different class objects. " |
| 47 | "Class descriptor collision?"; |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
| 51 | LOG(ERROR) << __func__ |
| 52 | << ": Class cannot be associated on object which already has a class. Trying to " |
| 53 | "associate to '" |
| 54 | << newDescriptor.c_str() << "' but already set to '" << currentDescriptor.c_str() |
| 55 | << "'."; |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
| 59 | String8 descriptor(getBinder()->getInterfaceDescriptor()); |
| 60 | if (descriptor != newDescriptor) { |
| 61 | LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str() |
| 62 | << "' but descriptor is actually '" << descriptor.c_str() << "'."; |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
| 66 | // The descriptor matches, so if it is local, this is guaranteed to be the libbinder_ndk class. |
| 67 | // An error here can occur if there is a conflict between descriptors (two unrelated classes |
| 68 | // define the same descriptor), but this should never happen. |
| 69 | |
| 70 | // if this is a local ABBinder, mClazz should be non-null |
| 71 | CHECK(asABBinder() == nullptr); |
| 72 | CHECK(asABpBinder() != nullptr); |
| 73 | |
| 74 | if (!isRemote()) { |
| 75 | // ABpBinder but proxy to a local object. Therefore that local object must be an ABBinder. |
| 76 | ABBinder* binder = static_cast<ABBinder*>(getBinder().get()); |
| 77 | return binder; |
| 78 | } |
| 79 | |
| 80 | // This is a remote object |
| 81 | mClazz = clazz; |
| 82 | |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData) |
| 87 | : AIBinder(clazz), BBinder(), mUserData(userData) { |
| 88 | CHECK(clazz != nullptr); |
| 89 | } |
| 90 | ABBinder::~ABBinder() { |
| 91 | getClass()->onDestroy(mUserData); |
| 92 | } |
| 93 | |
| 94 | const String16& ABBinder::getInterfaceDescriptor() const { |
| 95 | return getClass()->getInterfaceDescriptor(); |
| 96 | } |
| 97 | |
| 98 | binder_status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply, |
| 99 | binder_flags_t flags) { |
| 100 | if (isUserCommand(code)) { |
| 101 | if (!data.checkInterface(this)) { |
| 102 | return EX_ILLEGAL_STATE; |
| 103 | } |
| 104 | |
| 105 | const AParcel in = AParcel::readOnly(this, &data); |
| 106 | AParcel out = AParcel(this, reply, false /*owns*/); |
| 107 | |
| 108 | return getClass()->onTransact(this, code, &in, &out); |
| 109 | } else { |
| 110 | return BBinder::onTransact(code, data, reply, flags); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | ABpBinder::ABpBinder(::android::sp<::android::IBinder> binder) |
| 115 | : AIBinder(nullptr /*clazz*/), BpRefBase(binder) { |
| 116 | CHECK(binder != nullptr); |
| 117 | } |
| 118 | ABpBinder::~ABpBinder() {} |
| 119 | |
| 120 | struct AIBinder_Weak { |
| 121 | wp<AIBinder> binder; |
| 122 | }; |
| 123 | AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) { |
| 124 | if (binder == nullptr) return nullptr; |
| 125 | return new AIBinder_Weak{wp<AIBinder>(binder)}; |
| 126 | } |
| 127 | void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) { |
| 128 | delete weakBinder; |
| 129 | } |
| 130 | AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) { |
| 131 | if (weakBinder == nullptr) return nullptr; |
| 132 | sp<AIBinder> binder = weakBinder->binder.promote(); |
| 133 | AIBinder_incStrong(binder.get()); |
| 134 | return binder.get(); |
| 135 | } |
| 136 | |
| 137 | AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, |
| 138 | AIBinder_Class_onDestroy onDestroy, |
| 139 | AIBinder_Class_onTransact onTransact) |
| 140 | : onCreate(onCreate), |
| 141 | onDestroy(onDestroy), |
| 142 | onTransact(onTransact), |
| 143 | mInterfaceDescriptor(interfaceDescriptor) {} |
| 144 | |
| 145 | AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor, |
| 146 | AIBinder_Class_onCreate onCreate, |
| 147 | AIBinder_Class_onDestroy onDestroy, |
| 148 | AIBinder_Class_onTransact onTransact) { |
| 149 | if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr || |
| 150 | onTransact == nullptr) { |
| 151 | return nullptr; |
| 152 | } |
| 153 | |
| 154 | return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact); |
| 155 | } |
| 156 | |
| 157 | AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) { |
| 158 | if (clazz == nullptr) { |
| 159 | LOG(ERROR) << __func__ << ": Must provide class to construct local binder."; |
| 160 | return nullptr; |
| 161 | } |
| 162 | |
| 163 | void* userData = clazz->onCreate(args); |
| 164 | |
| 165 | AIBinder* ret = new ABBinder(clazz, userData); |
| 166 | AIBinder_incStrong(ret); |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | bool AIBinder_isRemote(AIBinder* binder) { |
| 171 | if (binder == nullptr) { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | return binder->isRemote(); |
| 176 | } |
| 177 | |
| 178 | void AIBinder_incStrong(AIBinder* binder) { |
| 179 | if (binder == nullptr) { |
| 180 | LOG(ERROR) << __func__ << ": on null binder"; |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | binder->incStrong(nullptr); |
| 185 | } |
| 186 | void AIBinder_decStrong(AIBinder* binder) { |
| 187 | if (binder == nullptr) { |
| 188 | LOG(ERROR) << __func__ << ": on null binder"; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | binder->decStrong(nullptr); |
| 193 | } |
| 194 | int32_t AIBinder_debugGetRefCount(AIBinder* binder) { |
| 195 | if (binder == nullptr) { |
| 196 | LOG(ERROR) << __func__ << ": on null binder"; |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | return binder->getStrongCount(); |
| 201 | } |
| 202 | |
| 203 | void AIBinder_associateClass(AIBinder** binder, const AIBinder_Class* clazz) { |
| 204 | if (binder == nullptr || *binder == nullptr) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | sp<AIBinder> result = (*binder)->associateClass(clazz); |
| 209 | |
| 210 | // This function takes one refcount of 'binder' and delivers one refcount of 'result' to the |
| 211 | // callee. First we give the callee their refcount and then take it away from binder. This is |
| 212 | // done in this order in order to handle the case that the result and the binder are the same |
| 213 | // object. |
| 214 | if (result != nullptr) { |
| 215 | AIBinder_incStrong(result.get()); |
| 216 | } |
| 217 | AIBinder_decStrong(*binder); |
| 218 | |
| 219 | *binder = result.get(); // Maybe no-op |
| 220 | } |
| 221 | |
| 222 | const AIBinder_Class* AIBinder_getClass(AIBinder* binder) { |
| 223 | if (binder == nullptr) { |
| 224 | return nullptr; |
| 225 | } |
| 226 | |
| 227 | return binder->getClass(); |
| 228 | } |
| 229 | |
| 230 | void* AIBinder_getUserData(AIBinder* binder) { |
| 231 | if (binder == nullptr) { |
| 232 | return nullptr; |
| 233 | } |
| 234 | |
| 235 | ABBinder* bBinder = binder->asABBinder(); |
| 236 | if (bBinder == nullptr) { |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
| 240 | return bBinder->getUserData(); |
| 241 | } |
| 242 | |
| 243 | binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) { |
| 244 | if (binder == nullptr || in == nullptr) { |
| 245 | LOG(ERROR) << __func__ << ": requires non-null parameters."; |
| 246 | return EX_NULL_POINTER; |
| 247 | } |
| 248 | const AIBinder_Class* clazz = binder->getClass(); |
| 249 | if (clazz == nullptr) { |
| 250 | LOG(ERROR) << __func__ |
| 251 | << ": Class must be defined for a remote binder transaction. See " |
| 252 | "AIBinder_associateClass."; |
| 253 | return EX_ILLEGAL_STATE; |
| 254 | } |
| 255 | |
| 256 | *in = new AParcel(binder); |
| 257 | binder_status_t status = (**in)->writeInterfaceToken(clazz->getInterfaceDescriptor()); |
| 258 | if (status != EX_NONE) { |
| 259 | delete *in; |
| 260 | *in = nullptr; |
| 261 | } |
| 262 | |
| 263 | return status; |
| 264 | } |
| 265 | |
| 266 | using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>; |
| 267 | static void destroy_parcel(AParcel** parcel) { |
| 268 | delete *parcel; |
| 269 | *parcel = nullptr; |
| 270 | } |
| 271 | |
| 272 | binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in, |
| 273 | AParcel** out, binder_flags_t flags) { |
| 274 | if (in == nullptr) { |
| 275 | LOG(ERROR) << __func__ << ": requires non-null in parameter"; |
| 276 | return EX_NULL_POINTER; |
| 277 | } |
| 278 | |
| 279 | // This object is the input to the transaction. This function takes ownership of it and deletes |
| 280 | // it. |
| 281 | AutoParcelDestroyer forIn(in, destroy_parcel); |
| 282 | |
| 283 | if (!isUserCommand(code)) { |
| 284 | LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK."; |
| 285 | return EX_UNSUPPORTED_OPERATION; |
| 286 | } |
| 287 | |
| 288 | if ((flags & ~FLAG_ONEWAY) != 0) { |
| 289 | LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags; |
| 290 | return EX_ILLEGAL_ARGUMENT; |
| 291 | } |
| 292 | |
| 293 | if (binder == nullptr || *in == nullptr || out == nullptr) { |
| 294 | LOG(ERROR) << __func__ << ": requires non-null parameters."; |
| 295 | return EX_NULL_POINTER; |
| 296 | } |
| 297 | |
| 298 | if ((*in)->getBinder() != binder) { |
| 299 | LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder |
| 300 | << " but called with " << (*in)->getBinder(); |
| 301 | return EX_ILLEGAL_STATE; |
| 302 | } |
| 303 | |
| 304 | *out = new AParcel(binder); |
| 305 | |
| 306 | binder_status_t parcelStatus = |
| 307 | binder->getBinder()->transact(code, *(*in)->operator->(), (*out)->operator->(), flags); |
| 308 | |
| 309 | if (parcelStatus != EX_NONE) { |
| 310 | delete *out; |
| 311 | *out = nullptr; |
| 312 | } |
| 313 | |
| 314 | return parcelStatus; |
| 315 | } |
| 316 | |
| 317 | binder_status_t AIBinder_finalizeTransaction(AIBinder* binder, AParcel** out) { |
| 318 | if (out == nullptr) { |
| 319 | LOG(ERROR) << __func__ << ": requires non-null out parameter"; |
| 320 | return EX_NULL_POINTER; |
| 321 | } |
| 322 | |
| 323 | // This object is the input to the transaction. This function takes ownership of it and deletes |
| 324 | // it. |
| 325 | AutoParcelDestroyer forOut(out, destroy_parcel); |
| 326 | |
| 327 | if (binder == nullptr || *out == nullptr) { |
| 328 | LOG(ERROR) << __func__ << ": requires non-null parameters."; |
| 329 | return EX_NULL_POINTER; |
| 330 | } |
| 331 | |
| 332 | if ((*out)->getBinder() != binder) { |
| 333 | LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder |
| 334 | << " but called with " << (*out)->getBinder(); |
| 335 | return EX_ILLEGAL_STATE; |
| 336 | } |
| 337 | |
| 338 | if ((**out)->dataAvail() != 0) { |
| 339 | LOG(ERROR) << __func__ |
| 340 | << ": Only part of this transaction was read. There is remaining data left."; |
| 341 | return EX_ILLEGAL_STATE; |
| 342 | } |
| 343 | |
| 344 | return EX_NONE; |
| 345 | } |