blob: e752c45d60e3498ad8a59d0f5aebd140ba25db30 [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
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>
Steven Moreland4d5ad492018-09-13 12:49:16 -070018#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070019
Steven Moreland46b5fea2019-10-15 11:22:18 -070020#include <android/binder_stability.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070021#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070022#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070023#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070024
25#include <android-base/logging.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080026#include <binder/IPCThreadState.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070027
Steven Moreland901c7452018-09-04 16:17:28 -070028using DeathRecipient = ::android::IBinder::DeathRecipient;
29
Steven Moreland2e87adc2018-08-20 19:47:00 -070030using ::android::IBinder;
31using ::android::Parcel;
32using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070033using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070034using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080035using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070036using ::android::wp;
37
Steven Moreland71cddc32018-08-30 23:39:22 -070038namespace ABBinderTag {
39
40static const void* kId = "ABBinder";
41static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070042void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070043
44static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070045 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070046}
47static bool has(const sp<IBinder>& binder) {
48 return binder != nullptr && binder->findObject(kId) == kValue;
49}
50
Steven Moreland6cf66ac2018-11-02 18:14:54 -070051} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070052
Steven Moreland94968952018-09-05 14:42:59 -070053namespace ABpBinderTag {
54
55static std::mutex gLock;
56static const void* kId = "ABpBinder";
57struct Value {
58 wp<ABpBinder> binder;
59};
60void clean(const void* id, void* obj, void* cookie) {
61 CHECK(id == kId) << id << " " << obj << " " << cookie;
62
63 delete static_cast<Value*>(obj);
64};
65
Steven Moreland6cf66ac2018-11-02 18:14:54 -070066} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070067
Steven Moreland2e87adc2018-08-20 19:47:00 -070068AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
69AIBinder::~AIBinder() {}
70
Steven Moreland71cddc32018-08-30 23:39:22 -070071bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland71cddc32018-08-30 23:39:22 -070072 if (clazz == nullptr) return false;
73 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070074
75 String8 newDescriptor(clazz->getInterfaceDescriptor());
76
77 if (mClazz != nullptr) {
78 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
79 if (newDescriptor == currentDescriptor) {
80 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
81 << "' match during associateClass, but they are different class objects. "
82 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070083 } else {
84 LOG(ERROR) << __func__
85 << ": Class cannot be associated on object which already has a class. "
86 "Trying to associate to '"
87 << newDescriptor.c_str() << "' but already set to '"
88 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070089 }
90
Steven Moreland71cddc32018-08-30 23:39:22 -070091 // always a failure because we know mClazz != clazz
92 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070093 }
94
Steven Moreland6cf66ac2018-11-02 18:14:54 -070095 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -070096
Steven Moreland2e87adc2018-08-20 19:47:00 -070097 String8 descriptor(getBinder()->getInterfaceDescriptor());
98 if (descriptor != newDescriptor) {
99 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
100 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -0700101 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700102 }
103
Steven Moreland71cddc32018-08-30 23:39:22 -0700104 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700105 mClazz = clazz;
106
Steven Moreland71cddc32018-08-30 23:39:22 -0700107 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700108}
109
110ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700111 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700112 CHECK(clazz != nullptr);
113}
114ABBinder::~ABBinder() {
115 getClass()->onDestroy(mUserData);
116}
117
118const String16& ABBinder::getInterfaceDescriptor() const {
119 return getClass()->getInterfaceDescriptor();
120}
121
Steven Morelanda194c452019-03-04 16:47:07 -0800122status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
123 AIBinder_onDump onDump = getClass()->onDump;
124
125 if (onDump == nullptr) {
126 return STATUS_OK;
127 }
128
129 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
130 // null in Java
131 if (args.size() > INT32_MAX) {
132 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
133 return STATUS_BAD_VALUE;
134 }
135
136 std::vector<String8> utf8Args; // owns memory of utf8s
137 utf8Args.reserve(args.size());
138 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
139 utf8Pointers.reserve(args.size());
140
141 for (size_t i = 0; i < args.size(); i++) {
142 utf8Args.push_back(String8(args[i]));
143 utf8Pointers.push_back(utf8Args[i].c_str());
144 }
145
146 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
147}
148
Steven Moreland5d62e442018-09-13 15:01:02 -0700149status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
150 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700151 if (isUserCommand(code)) {
152 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700153 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700154 }
155
156 const AParcel in = AParcel::readOnly(this, &data);
157 AParcel out = AParcel(this, reply, false /*owns*/);
158
Steven Moreland5d62e442018-09-13 15:01:02 -0700159 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
160 return PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700161 } else {
162 return BBinder::onTransact(code, data, reply, flags);
163 }
164}
165
Steven Moreland71cddc32018-08-30 23:39:22 -0700166ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700167 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700168 CHECK(binder != nullptr);
169}
170ABpBinder::~ABpBinder() {}
171
Steven Moreland94968952018-09-05 14:42:59 -0700172void ABpBinder::onLastStrongRef(const void* id) {
173 {
174 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
175 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
176 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
177 // longer be able to exist at the time of this method call, there is no longer a need to
178 // recover it.
179
180 ABpBinderTag::Value* value =
181 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
182 if (value != nullptr) {
183 value->binder = nullptr;
184 }
185 }
186
187 BpRefBase::onLastStrongRef(id);
188}
189
190sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700191 if (binder == nullptr) {
192 return nullptr;
193 }
194 if (ABBinderTag::has(binder)) {
195 return static_cast<ABBinder*>(binder.get());
196 }
Steven Moreland94968952018-09-05 14:42:59 -0700197
198 // The following code ensures that for a given binder object (remote or local), if it is not an
199 // ABBinder then at most one ABpBinder object exists in a given process representing it.
200 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
201
202 ABpBinderTag::Value* value =
203 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
204 if (value == nullptr) {
205 value = new ABpBinderTag::Value;
206 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
207 ABpBinderTag::clean);
208 }
209
210 sp<ABpBinder> ret = value->binder.promote();
211 if (ret == nullptr) {
212 ret = new ABpBinder(binder);
213 value->binder = ret;
214 }
215
216 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700217}
218
Steven Moreland2e87adc2018-08-20 19:47:00 -0700219struct AIBinder_Weak {
220 wp<AIBinder> binder;
221};
222AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700223 if (binder == nullptr) {
224 return nullptr;
225 }
226
Steven Moreland2e87adc2018-08-20 19:47:00 -0700227 return new AIBinder_Weak{wp<AIBinder>(binder)};
228}
Steven Moreland9b80e282018-09-19 13:57:23 -0700229void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
230 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700231}
232AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700233 if (weakBinder == nullptr) {
234 return nullptr;
235 }
236
Steven Moreland2e87adc2018-08-20 19:47:00 -0700237 sp<AIBinder> binder = weakBinder->binder.promote();
238 AIBinder_incStrong(binder.get());
239 return binder.get();
240}
241
242AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
243 AIBinder_Class_onDestroy onDestroy,
244 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700245 : onCreate(onCreate),
246 onDestroy(onDestroy),
247 onTransact(onTransact),
248 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700249
250AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
251 AIBinder_Class_onCreate onCreate,
252 AIBinder_Class_onDestroy onDestroy,
253 AIBinder_Class_onTransact onTransact) {
254 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
255 onTransact == nullptr) {
256 return nullptr;
257 }
258
259 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
260}
261
Steven Morelanda194c452019-03-04 16:47:07 -0800262void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
263 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
264
265 // this is required to be called before instances are instantiated
266 clazz->onDump = onDump;
267}
268
Steven Moreland901c7452018-09-04 16:17:28 -0700269void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
270 CHECK(who == mWho);
271
272 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700273
274 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
275 sp<IBinder> strongWho = who.promote();
276
277 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
278 if (recipient != nullptr && strongWho != nullptr) {
279 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
280 if (result != ::android::DEAD_OBJECT) {
281 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
282 }
283 }
284
Steven Moreland901c7452018-09-04 16:17:28 -0700285 mWho = nullptr;
286}
287
288AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700289 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700290 CHECK(onDied != nullptr);
291}
292
Steven Moreland64127ca2019-03-13 09:25:44 -0700293void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
294 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
295 [](const sp<TransferDeathRecipient>& tdr) {
296 return tdr->getWho() == nullptr;
297 }),
298 mDeathRecipients.end());
299}
300
301binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700302 CHECK(binder != nullptr);
303
304 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
305
306 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700307 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700308
Steven Moreland64127ca2019-03-13 09:25:44 -0700309 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700310 if (status != STATUS_OK) {
311 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700312 }
313
314 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700315
316 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700317 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700318}
319
Steven Moreland64127ca2019-03-13 09:25:44 -0700320binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700321 CHECK(binder != nullptr);
322
323 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
324
325 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
326 sp<TransferDeathRecipient> recipient = *it;
327
Steven Moreland64127ca2019-03-13 09:25:44 -0700328 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700329 mDeathRecipients.erase(it.base() - 1);
330
Steven Moreland64127ca2019-03-13 09:25:44 -0700331 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700332 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700333 LOG(ERROR) << __func__
334 << ": removed reference to death recipient but unlink failed.";
335 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700336 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700337 }
338 }
339
Steven Moreland5d62e442018-09-13 15:01:02 -0700340 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700341}
342
343// start of C-API methods
344
Steven Moreland2e87adc2018-08-20 19:47:00 -0700345AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
346 if (clazz == nullptr) {
347 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
348 return nullptr;
349 }
350
351 void* userData = clazz->onCreate(args);
352
Steven Moreland71cddc32018-08-30 23:39:22 -0700353 sp<AIBinder> ret = new ABBinder(clazz, userData);
354 ABBinderTag::attach(ret->getBinder());
355
356 AIBinder_incStrong(ret.get());
357 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700358}
359
Steven Moreland5ea54da2018-09-04 13:29:55 -0700360bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700361 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800362 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700363 }
364
365 return binder->isRemote();
366}
367
Steven Moreland65867d72018-09-04 14:22:26 -0700368bool AIBinder_isAlive(const AIBinder* binder) {
369 if (binder == nullptr) {
370 return false;
371 }
372
373 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
374}
375
376binder_status_t AIBinder_ping(AIBinder* binder) {
377 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700378 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700379 }
380
Steven Moreland5d62e442018-09-13 15:01:02 -0700381 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700382}
383
Steven Morelanda194c452019-03-04 16:47:07 -0800384binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
385 if (binder == nullptr) {
386 return STATUS_UNEXPECTED_NULL;
387 }
388
389 ABBinder* bBinder = binder->asABBinder();
390 if (bBinder != nullptr) {
391 AIBinder_onDump onDump = binder->getClass()->onDump;
392 if (onDump == nullptr) {
393 return STATUS_OK;
394 }
395 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
396 }
397
398 ::android::Vector<String16> utf16Args;
399 utf16Args.setCapacity(numArgs);
400 for (uint32_t i = 0; i < numArgs; i++) {
401 utf16Args.push(String16(String8(args[i])));
402 }
403
404 status_t status = binder->getBinder()->dump(fd, utf16Args);
405 return PruneStatusT(status);
406}
407
Steven Moreland901c7452018-09-04 16:17:28 -0700408binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
409 void* cookie) {
410 if (binder == nullptr || recipient == nullptr) {
411 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700412 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700413 }
414
Steven Moreland5d62e442018-09-13 15:01:02 -0700415 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700416 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700417}
418
419binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
420 void* cookie) {
421 if (binder == nullptr || recipient == nullptr) {
422 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700423 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700424 }
425
Steven Moreland5d62e442018-09-13 15:01:02 -0700426 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700427 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700428}
429
Steven Morelandf3034b02018-11-12 17:37:46 -0800430uid_t AIBinder_getCallingUid() {
431 return ::android::IPCThreadState::self()->getCallingUid();
432}
433
434pid_t AIBinder_getCallingPid() {
435 return ::android::IPCThreadState::self()->getCallingPid();
436}
437
Steven Moreland2e87adc2018-08-20 19:47:00 -0700438void AIBinder_incStrong(AIBinder* binder) {
439 if (binder == nullptr) {
440 LOG(ERROR) << __func__ << ": on null binder";
441 return;
442 }
443
444 binder->incStrong(nullptr);
445}
446void AIBinder_decStrong(AIBinder* binder) {
447 if (binder == nullptr) {
448 LOG(ERROR) << __func__ << ": on null binder";
449 return;
450 }
451
452 binder->decStrong(nullptr);
453}
454int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
455 if (binder == nullptr) {
456 LOG(ERROR) << __func__ << ": on null binder";
457 return -1;
458 }
459
460 return binder->getStrongCount();
461}
462
Steven Moreland71cddc32018-08-30 23:39:22 -0700463bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
464 if (binder == nullptr) {
465 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700466 }
467
Steven Moreland71cddc32018-08-30 23:39:22 -0700468 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700469}
470
471const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
472 if (binder == nullptr) {
473 return nullptr;
474 }
475
476 return binder->getClass();
477}
478
479void* AIBinder_getUserData(AIBinder* binder) {
480 if (binder == nullptr) {
481 return nullptr;
482 }
483
484 ABBinder* bBinder = binder->asABBinder();
485 if (bBinder == nullptr) {
486 return nullptr;
487 }
488
489 return bBinder->getUserData();
490}
491
492binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
493 if (binder == nullptr || in == nullptr) {
494 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700495 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700496 }
497 const AIBinder_Class* clazz = binder->getClass();
498 if (clazz == nullptr) {
499 LOG(ERROR) << __func__
500 << ": Class must be defined for a remote binder transaction. See "
501 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700502 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700503 }
504
Steven Moreland3527c2e2018-09-05 17:07:14 -0700505 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700506 LOG(WARNING) << "A binder object at " << binder
507 << " is being transacted on, however, this object is in the same process as "
508 "its proxy. Transacting with this binder is expensive compared to just "
509 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700510 }
511
Steven Moreland2e87adc2018-08-20 19:47:00 -0700512 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700513 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700514 binder_status_t ret = PruneStatusT(status);
515
516 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700517 delete *in;
518 *in = nullptr;
519 }
520
Steven Moreland5d62e442018-09-13 15:01:02 -0700521 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700522}
523
Steven Moreland9b80e282018-09-19 13:57:23 -0700524static void DestroyParcel(AParcel** parcel) {
525 delete *parcel;
526 *parcel = nullptr;
527}
528
Steven Moreland2e87adc2018-08-20 19:47:00 -0700529binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
530 AParcel** out, binder_flags_t flags) {
531 if (in == nullptr) {
532 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700533 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700534 }
535
Steven Morelandcaa776c2018-09-04 13:48:11 -0700536 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700537 // This object is the input to the transaction. This function takes ownership of it and deletes
538 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700539 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700540
541 if (!isUserCommand(code)) {
542 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700543 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700544 }
545
Steven Moreland46b5fea2019-10-15 11:22:18 -0700546 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
547 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700548 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700549 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700550 }
551
552 if (binder == nullptr || *in == nullptr || out == nullptr) {
553 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700554 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700555 }
556
557 if ((*in)->getBinder() != binder) {
558 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
559 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700560 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700561 }
562
563 *out = new AParcel(binder);
564
Steven Morelandf18615b2018-09-14 11:43:06 -0700565 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700566 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700567
Steven Moreland5d62e442018-09-13 15:01:02 -0700568 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700569 delete *out;
570 *out = nullptr;
571 }
572
Steven Moreland5d62e442018-09-13 15:01:02 -0700573 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700574}
Steven Moreland901c7452018-09-04 16:17:28 -0700575
576AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
577 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
578 if (onBinderDied == nullptr) {
579 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
580 return nullptr;
581 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700582 auto ret = new AIBinder_DeathRecipient(onBinderDied);
583 ret->incStrong(nullptr);
584 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700585}
586
Steven Moreland9b80e282018-09-19 13:57:23 -0700587void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700588 if (recipient == nullptr) {
589 return;
590 }
591
592 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700593}
Steven Morelandea14ef22019-08-20 10:50:08 -0700594
595binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
596 if (binder == nullptr || outExt == nullptr) {
597 if (outExt != nullptr) {
598 *outExt = nullptr;
599 }
600 return STATUS_UNEXPECTED_NULL;
601 }
602
603 sp<IBinder> ext;
604 status_t res = binder->getBinder()->getExtension(&ext);
605
606 if (res != android::OK) {
607 *outExt = nullptr;
608 return PruneStatusT(res);
609 }
610
611 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
612 if (ret != nullptr) ret->incStrong(binder);
613
614 *outExt = ret.get();
615 return STATUS_OK;
616}
617
618binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
619 if (binder == nullptr || ext == nullptr) {
620 return STATUS_UNEXPECTED_NULL;
621 }
622
623 ABBinder* rawBinder = binder->asABBinder();
624 if (rawBinder == nullptr) {
625 return STATUS_INVALID_OPERATION;
626 }
627
628 rawBinder->setExtension(ext->getBinder());
629 return STATUS_OK;
630}