blob: 743ef893fcbd7e505fd8f23562431effec1d510b [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>
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080027#include <binder/IResultReceiver.h>
28#include <private/android_filesystem_config.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070029
Steven Moreland901c7452018-09-04 16:17:28 -070030using DeathRecipient = ::android::IBinder::DeathRecipient;
31
Steven Moreland2e87adc2018-08-20 19:47:00 -070032using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080033using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070034using ::android::Parcel;
35using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070036using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070037using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080038using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070039using ::android::wp;
40
Steven Moreland71cddc32018-08-30 23:39:22 -070041namespace ABBinderTag {
42
43static const void* kId = "ABBinder";
44static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070045void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070046
47static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070048 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070049}
50static bool has(const sp<IBinder>& binder) {
51 return binder != nullptr && binder->findObject(kId) == kValue;
52}
53
Steven Moreland6cf66ac2018-11-02 18:14:54 -070054} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070055
Steven Moreland94968952018-09-05 14:42:59 -070056namespace ABpBinderTag {
57
58static std::mutex gLock;
59static const void* kId = "ABpBinder";
60struct Value {
61 wp<ABpBinder> binder;
62};
63void clean(const void* id, void* obj, void* cookie) {
64 CHECK(id == kId) << id << " " << obj << " " << cookie;
65
66 delete static_cast<Value*>(obj);
67};
68
Steven Moreland6cf66ac2018-11-02 18:14:54 -070069} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070070
Steven Moreland2e87adc2018-08-20 19:47:00 -070071AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
72AIBinder::~AIBinder() {}
73
Steven Moreland71cddc32018-08-30 23:39:22 -070074bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland71cddc32018-08-30 23:39:22 -070075 if (clazz == nullptr) return false;
76 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070077
78 String8 newDescriptor(clazz->getInterfaceDescriptor());
79
80 if (mClazz != nullptr) {
81 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
82 if (newDescriptor == currentDescriptor) {
83 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
84 << "' match during associateClass, but they are different class objects. "
85 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070086 } else {
87 LOG(ERROR) << __func__
88 << ": Class cannot be associated on object which already has a class. "
89 "Trying to associate to '"
90 << newDescriptor.c_str() << "' but already set to '"
91 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 }
93
Steven Moreland71cddc32018-08-30 23:39:22 -070094 // always a failure because we know mClazz != clazz
95 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 }
97
Steven Moreland6cf66ac2018-11-02 18:14:54 -070098 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -070099
Steven Moreland2e87adc2018-08-20 19:47:00 -0700100 String8 descriptor(getBinder()->getInterfaceDescriptor());
101 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000102 if (getBinder()->isBinderAlive()) {
103 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
104 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
105 } else {
106 // b/155793159
107 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor.c_str()
108 << "' to dead binder.";
109 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700110 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700111 }
112
Steven Moreland71cddc32018-08-30 23:39:22 -0700113 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700114 mClazz = clazz;
115
Steven Moreland71cddc32018-08-30 23:39:22 -0700116 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700117}
118
119ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700120 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700121 CHECK(clazz != nullptr);
122}
123ABBinder::~ABBinder() {
124 getClass()->onDestroy(mUserData);
125}
126
127const String16& ABBinder::getInterfaceDescriptor() const {
128 return getClass()->getInterfaceDescriptor();
129}
130
Steven Morelanda194c452019-03-04 16:47:07 -0800131status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
132 AIBinder_onDump onDump = getClass()->onDump;
133
134 if (onDump == nullptr) {
135 return STATUS_OK;
136 }
137
138 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
139 // null in Java
140 if (args.size() > INT32_MAX) {
141 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
142 return STATUS_BAD_VALUE;
143 }
144
145 std::vector<String8> utf8Args; // owns memory of utf8s
146 utf8Args.reserve(args.size());
147 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
148 utf8Pointers.reserve(args.size());
149
150 for (size_t i = 0; i < args.size(); i++) {
151 utf8Args.push_back(String8(args[i]));
152 utf8Pointers.push_back(utf8Args[i].c_str());
153 }
154
155 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
156}
157
Steven Moreland5d62e442018-09-13 15:01:02 -0700158status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
159 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700160 if (isUserCommand(code)) {
161 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700162 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700163 }
164
165 const AParcel in = AParcel::readOnly(this, &data);
166 AParcel out = AParcel(this, reply, false /*owns*/);
167
Steven Moreland5d62e442018-09-13 15:01:02 -0700168 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
169 return PruneStatusT(status);
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800170 } else if (code == SHELL_COMMAND_TRANSACTION) {
171 int in = data.readFileDescriptor();
172 int out = data.readFileDescriptor();
173 int err = data.readFileDescriptor();
174
175 int argc = data.readInt32();
176 std::vector<String8> utf8Args; // owns memory of utf8s
177 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
178 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
179 utf8Args.push_back(String8(data.readString16()));
180 utf8Pointers.push_back(utf8Args[i].c_str());
181 }
182
183 data.readStrongBinder(); // skip over the IShellCallback
184 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
185
186 // Shell commands should only be callable by ADB.
187 uid_t uid = AIBinder_getCallingUid();
188 if (uid != AID_ROOT && uid != AID_SHELL) {
189 if (resultReceiver != nullptr) {
190 resultReceiver->send(-1);
191 }
192 return STATUS_PERMISSION_DENIED;
193 }
194
195 // Check that the file descriptors are valid.
196 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
197 if (resultReceiver != nullptr) {
198 resultReceiver->send(-1);
199 }
200 return STATUS_BAD_VALUE;
201 }
202
203 binder_status_t status = getClass()->handleShellCommand(
204 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
205 if (resultReceiver != nullptr) {
206 resultReceiver->send(status);
207 }
208 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700209 } else {
210 return BBinder::onTransact(code, data, reply, flags);
211 }
212}
213
Steven Moreland71cddc32018-08-30 23:39:22 -0700214ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700215 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700216 CHECK(binder != nullptr);
217}
218ABpBinder::~ABpBinder() {}
219
Steven Moreland94968952018-09-05 14:42:59 -0700220void ABpBinder::onLastStrongRef(const void* id) {
221 {
222 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
223 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
224 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
225 // longer be able to exist at the time of this method call, there is no longer a need to
226 // recover it.
227
228 ABpBinderTag::Value* value =
229 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
230 if (value != nullptr) {
231 value->binder = nullptr;
232 }
233 }
234
235 BpRefBase::onLastStrongRef(id);
236}
237
238sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700239 if (binder == nullptr) {
240 return nullptr;
241 }
242 if (ABBinderTag::has(binder)) {
243 return static_cast<ABBinder*>(binder.get());
244 }
Steven Moreland94968952018-09-05 14:42:59 -0700245
246 // The following code ensures that for a given binder object (remote or local), if it is not an
247 // ABBinder then at most one ABpBinder object exists in a given process representing it.
248 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
249
250 ABpBinderTag::Value* value =
251 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
252 if (value == nullptr) {
253 value = new ABpBinderTag::Value;
254 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
255 ABpBinderTag::clean);
256 }
257
258 sp<ABpBinder> ret = value->binder.promote();
259 if (ret == nullptr) {
260 ret = new ABpBinder(binder);
261 value->binder = ret;
262 }
263
264 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700265}
266
Steven Moreland2e87adc2018-08-20 19:47:00 -0700267struct AIBinder_Weak {
268 wp<AIBinder> binder;
269};
270AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700271 if (binder == nullptr) {
272 return nullptr;
273 }
274
Steven Moreland2e87adc2018-08-20 19:47:00 -0700275 return new AIBinder_Weak{wp<AIBinder>(binder)};
276}
Steven Moreland9b80e282018-09-19 13:57:23 -0700277void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
278 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700279}
280AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700281 if (weakBinder == nullptr) {
282 return nullptr;
283 }
284
Steven Moreland2e87adc2018-08-20 19:47:00 -0700285 sp<AIBinder> binder = weakBinder->binder.promote();
286 AIBinder_incStrong(binder.get());
287 return binder.get();
288}
289
290AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
291 AIBinder_Class_onDestroy onDestroy,
292 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700293 : onCreate(onCreate),
294 onDestroy(onDestroy),
295 onTransact(onTransact),
296 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700297
298AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
299 AIBinder_Class_onCreate onCreate,
300 AIBinder_Class_onDestroy onDestroy,
301 AIBinder_Class_onTransact onTransact) {
302 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
303 onTransact == nullptr) {
304 return nullptr;
305 }
306
307 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
308}
309
Steven Morelanda194c452019-03-04 16:47:07 -0800310void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
311 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
312
313 // this is required to be called before instances are instantiated
314 clazz->onDump = onDump;
315}
316
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800317void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
318 AIBinder_handleShellCommand handleShellCommand) {
319 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
320
321 clazz->handleShellCommand = handleShellCommand;
322}
323
Steven Moreland901c7452018-09-04 16:17:28 -0700324void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
325 CHECK(who == mWho);
326
327 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700328
329 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
330 sp<IBinder> strongWho = who.promote();
331
332 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
333 if (recipient != nullptr && strongWho != nullptr) {
334 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
335 if (result != ::android::DEAD_OBJECT) {
336 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
337 }
338 }
339
Steven Moreland901c7452018-09-04 16:17:28 -0700340 mWho = nullptr;
341}
342
343AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700344 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700345 CHECK(onDied != nullptr);
346}
347
Steven Moreland64127ca2019-03-13 09:25:44 -0700348void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
349 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
350 [](const sp<TransferDeathRecipient>& tdr) {
351 return tdr->getWho() == nullptr;
352 }),
353 mDeathRecipients.end());
354}
355
356binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700357 CHECK(binder != nullptr);
358
359 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
360
361 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700362 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700363
Steven Moreland64127ca2019-03-13 09:25:44 -0700364 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700365 if (status != STATUS_OK) {
366 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700367 }
368
369 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700370
371 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700372 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700373}
374
Steven Moreland64127ca2019-03-13 09:25:44 -0700375binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700376 CHECK(binder != nullptr);
377
378 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
379
380 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
381 sp<TransferDeathRecipient> recipient = *it;
382
Steven Moreland64127ca2019-03-13 09:25:44 -0700383 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700384 mDeathRecipients.erase(it.base() - 1);
385
Steven Moreland64127ca2019-03-13 09:25:44 -0700386 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700387 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700388 LOG(ERROR) << __func__
389 << ": removed reference to death recipient but unlink failed.";
390 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700391 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700392 }
393 }
394
Steven Moreland5d62e442018-09-13 15:01:02 -0700395 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700396}
397
398// start of C-API methods
399
Steven Moreland2e87adc2018-08-20 19:47:00 -0700400AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
401 if (clazz == nullptr) {
402 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
403 return nullptr;
404 }
405
406 void* userData = clazz->onCreate(args);
407
Steven Moreland71cddc32018-08-30 23:39:22 -0700408 sp<AIBinder> ret = new ABBinder(clazz, userData);
409 ABBinderTag::attach(ret->getBinder());
410
411 AIBinder_incStrong(ret.get());
412 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700413}
414
Steven Moreland5ea54da2018-09-04 13:29:55 -0700415bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700416 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800417 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700418 }
419
420 return binder->isRemote();
421}
422
Steven Moreland65867d72018-09-04 14:22:26 -0700423bool AIBinder_isAlive(const AIBinder* binder) {
424 if (binder == nullptr) {
425 return false;
426 }
427
428 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
429}
430
431binder_status_t AIBinder_ping(AIBinder* binder) {
432 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700433 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700434 }
435
Steven Moreland5d62e442018-09-13 15:01:02 -0700436 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700437}
438
Steven Morelanda194c452019-03-04 16:47:07 -0800439binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
440 if (binder == nullptr) {
441 return STATUS_UNEXPECTED_NULL;
442 }
443
444 ABBinder* bBinder = binder->asABBinder();
445 if (bBinder != nullptr) {
446 AIBinder_onDump onDump = binder->getClass()->onDump;
447 if (onDump == nullptr) {
448 return STATUS_OK;
449 }
450 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
451 }
452
453 ::android::Vector<String16> utf16Args;
454 utf16Args.setCapacity(numArgs);
455 for (uint32_t i = 0; i < numArgs; i++) {
456 utf16Args.push(String16(String8(args[i])));
457 }
458
459 status_t status = binder->getBinder()->dump(fd, utf16Args);
460 return PruneStatusT(status);
461}
462
Steven Moreland901c7452018-09-04 16:17:28 -0700463binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
464 void* cookie) {
465 if (binder == nullptr || recipient == nullptr) {
466 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700467 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700468 }
469
Steven Moreland5d62e442018-09-13 15:01:02 -0700470 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700471 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700472}
473
474binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
475 void* cookie) {
476 if (binder == nullptr || recipient == nullptr) {
477 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700478 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700479 }
480
Steven Moreland5d62e442018-09-13 15:01:02 -0700481 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700482 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700483}
484
Steven Morelandf3034b02018-11-12 17:37:46 -0800485uid_t AIBinder_getCallingUid() {
486 return ::android::IPCThreadState::self()->getCallingUid();
487}
488
489pid_t AIBinder_getCallingPid() {
490 return ::android::IPCThreadState::self()->getCallingPid();
491}
492
Steven Moreland2e87adc2018-08-20 19:47:00 -0700493void AIBinder_incStrong(AIBinder* binder) {
494 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700495 return;
496 }
497
498 binder->incStrong(nullptr);
499}
500void AIBinder_decStrong(AIBinder* binder) {
501 if (binder == nullptr) {
502 LOG(ERROR) << __func__ << ": on null binder";
503 return;
504 }
505
506 binder->decStrong(nullptr);
507}
508int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
509 if (binder == nullptr) {
510 LOG(ERROR) << __func__ << ": on null binder";
511 return -1;
512 }
513
514 return binder->getStrongCount();
515}
516
Steven Moreland71cddc32018-08-30 23:39:22 -0700517bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
518 if (binder == nullptr) {
519 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700520 }
521
Steven Moreland71cddc32018-08-30 23:39:22 -0700522 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700523}
524
525const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
526 if (binder == nullptr) {
527 return nullptr;
528 }
529
530 return binder->getClass();
531}
532
533void* AIBinder_getUserData(AIBinder* binder) {
534 if (binder == nullptr) {
535 return nullptr;
536 }
537
538 ABBinder* bBinder = binder->asABBinder();
539 if (bBinder == nullptr) {
540 return nullptr;
541 }
542
543 return bBinder->getUserData();
544}
545
546binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
547 if (binder == nullptr || in == nullptr) {
548 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700549 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700550 }
551 const AIBinder_Class* clazz = binder->getClass();
552 if (clazz == nullptr) {
553 LOG(ERROR) << __func__
554 << ": Class must be defined for a remote binder transaction. See "
555 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700556 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700557 }
558
Steven Moreland3527c2e2018-09-05 17:07:14 -0700559 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700560 LOG(WARNING) << "A binder object at " << binder
561 << " is being transacted on, however, this object is in the same process as "
562 "its proxy. Transacting with this binder is expensive compared to just "
563 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700564 }
565
Steven Moreland2e87adc2018-08-20 19:47:00 -0700566 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700567 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700568 binder_status_t ret = PruneStatusT(status);
569
570 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700571 delete *in;
572 *in = nullptr;
573 }
574
Steven Moreland5d62e442018-09-13 15:01:02 -0700575 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700576}
577
Steven Moreland9b80e282018-09-19 13:57:23 -0700578static void DestroyParcel(AParcel** parcel) {
579 delete *parcel;
580 *parcel = nullptr;
581}
582
Steven Moreland2e87adc2018-08-20 19:47:00 -0700583binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
584 AParcel** out, binder_flags_t flags) {
585 if (in == nullptr) {
586 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700587 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700588 }
589
Steven Morelandcaa776c2018-09-04 13:48:11 -0700590 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700591 // This object is the input to the transaction. This function takes ownership of it and deletes
592 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700593 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700594
595 if (!isUserCommand(code)) {
596 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700597 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700598 }
599
Steven Moreland46b5fea2019-10-15 11:22:18 -0700600 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
601 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700602 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700603 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700604 }
605
606 if (binder == nullptr || *in == nullptr || out == nullptr) {
607 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700608 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700609 }
610
611 if ((*in)->getBinder() != binder) {
612 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
613 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700614 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700615 }
616
617 *out = new AParcel(binder);
618
Steven Morelandf18615b2018-09-14 11:43:06 -0700619 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700620 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700621
Steven Moreland5d62e442018-09-13 15:01:02 -0700622 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700623 delete *out;
624 *out = nullptr;
625 }
626
Steven Moreland5d62e442018-09-13 15:01:02 -0700627 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700628}
Steven Moreland901c7452018-09-04 16:17:28 -0700629
630AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
631 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
632 if (onBinderDied == nullptr) {
633 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
634 return nullptr;
635 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700636 auto ret = new AIBinder_DeathRecipient(onBinderDied);
637 ret->incStrong(nullptr);
638 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700639}
640
Steven Moreland9b80e282018-09-19 13:57:23 -0700641void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700642 if (recipient == nullptr) {
643 return;
644 }
645
646 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700647}
Steven Morelandea14ef22019-08-20 10:50:08 -0700648
649binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
650 if (binder == nullptr || outExt == nullptr) {
651 if (outExt != nullptr) {
652 *outExt = nullptr;
653 }
654 return STATUS_UNEXPECTED_NULL;
655 }
656
657 sp<IBinder> ext;
658 status_t res = binder->getBinder()->getExtension(&ext);
659
660 if (res != android::OK) {
661 *outExt = nullptr;
662 return PruneStatusT(res);
663 }
664
665 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
666 if (ret != nullptr) ret->incStrong(binder);
667
668 *outExt = ret.get();
669 return STATUS_OK;
670}
671
672binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
673 if (binder == nullptr || ext == nullptr) {
674 return STATUS_UNEXPECTED_NULL;
675 }
676
677 ABBinder* rawBinder = binder->asABBinder();
678 if (rawBinder == nullptr) {
679 return STATUS_INVALID_OPERATION;
680 }
681
682 rawBinder->setExtension(ext->getBinder());
683 return STATUS_OK;
684}