blob: 7d9fd51cc26e3155c5d01de0695c25537056b21a [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 Moreland2f405f52020-07-08 22:24:29 +000018#include <android/binder_ibinder_platform.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070019#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070020
Steven Moreland46b5fea2019-10-15 11:22:18 -070021#include <android/binder_stability.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070022#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070023#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070024#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070025
26#include <android-base/logging.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080027#include <binder/IPCThreadState.h>
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080028#include <binder/IResultReceiver.h>
29#include <private/android_filesystem_config.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070030
Steven Moreland901c7452018-09-04 16:17:28 -070031using DeathRecipient = ::android::IBinder::DeathRecipient;
32
Steven Moreland2e87adc2018-08-20 19:47:00 -070033using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080034using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070035using ::android::Parcel;
36using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070037using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070038using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080039using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070040using ::android::wp;
41
Steven Moreland71cddc32018-08-30 23:39:22 -070042namespace ABBinderTag {
43
44static const void* kId = "ABBinder";
45static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070046void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070047
48static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070049 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070050}
51static bool has(const sp<IBinder>& binder) {
52 return binder != nullptr && binder->findObject(kId) == kValue;
53}
54
Steven Moreland6cf66ac2018-11-02 18:14:54 -070055} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070056
Steven Moreland94968952018-09-05 14:42:59 -070057namespace ABpBinderTag {
58
59static std::mutex gLock;
60static const void* kId = "ABpBinder";
61struct Value {
62 wp<ABpBinder> binder;
63};
64void clean(const void* id, void* obj, void* cookie) {
65 CHECK(id == kId) << id << " " << obj << " " << cookie;
66
67 delete static_cast<Value*>(obj);
68};
69
Steven Moreland6cf66ac2018-11-02 18:14:54 -070070} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070071
Steven Moreland2e87adc2018-08-20 19:47:00 -070072AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
73AIBinder::~AIBinder() {}
74
Steven Moreland71cddc32018-08-30 23:39:22 -070075bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland71cddc32018-08-30 23:39:22 -070076 if (clazz == nullptr) return false;
77 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070078
79 String8 newDescriptor(clazz->getInterfaceDescriptor());
80
81 if (mClazz != nullptr) {
82 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
83 if (newDescriptor == currentDescriptor) {
84 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
85 << "' match during associateClass, but they are different class objects. "
86 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070087 } else {
88 LOG(ERROR) << __func__
89 << ": Class cannot be associated on object which already has a class. "
90 "Trying to associate to '"
91 << newDescriptor.c_str() << "' but already set to '"
92 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070093 }
94
Steven Moreland71cddc32018-08-30 23:39:22 -070095 // always a failure because we know mClazz != clazz
96 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070097 }
98
Steven Moreland6cf66ac2018-11-02 18:14:54 -070099 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700100
Steven Moreland2e87adc2018-08-20 19:47:00 -0700101 String8 descriptor(getBinder()->getInterfaceDescriptor());
102 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000103 if (getBinder()->isBinderAlive()) {
104 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
105 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
106 } else {
107 // b/155793159
108 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor.c_str()
109 << "' to dead binder.";
110 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700111 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700112 }
113
Steven Moreland71cddc32018-08-30 23:39:22 -0700114 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700115 mClazz = clazz;
116
Steven Moreland71cddc32018-08-30 23:39:22 -0700117 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700118}
119
120ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700121 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700122 CHECK(clazz != nullptr);
123}
124ABBinder::~ABBinder() {
125 getClass()->onDestroy(mUserData);
126}
127
128const String16& ABBinder::getInterfaceDescriptor() const {
129 return getClass()->getInterfaceDescriptor();
130}
131
Steven Morelanda194c452019-03-04 16:47:07 -0800132status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
133 AIBinder_onDump onDump = getClass()->onDump;
134
135 if (onDump == nullptr) {
136 return STATUS_OK;
137 }
138
139 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
140 // null in Java
141 if (args.size() > INT32_MAX) {
142 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
143 return STATUS_BAD_VALUE;
144 }
145
146 std::vector<String8> utf8Args; // owns memory of utf8s
147 utf8Args.reserve(args.size());
148 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
149 utf8Pointers.reserve(args.size());
150
151 for (size_t i = 0; i < args.size(); i++) {
152 utf8Args.push_back(String8(args[i]));
153 utf8Pointers.push_back(utf8Args[i].c_str());
154 }
155
156 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
157}
158
Steven Moreland5d62e442018-09-13 15:01:02 -0700159status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
160 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700161 if (isUserCommand(code)) {
162 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700163 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700164 }
165
166 const AParcel in = AParcel::readOnly(this, &data);
167 AParcel out = AParcel(this, reply, false /*owns*/);
168
Steven Moreland5d62e442018-09-13 15:01:02 -0700169 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
170 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000171 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800172 int in = data.readFileDescriptor();
173 int out = data.readFileDescriptor();
174 int err = data.readFileDescriptor();
175
176 int argc = data.readInt32();
177 std::vector<String8> utf8Args; // owns memory of utf8s
178 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
179 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
180 utf8Args.push_back(String8(data.readString16()));
181 utf8Pointers.push_back(utf8Args[i].c_str());
182 }
183
184 data.readStrongBinder(); // skip over the IShellCallback
185 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
186
187 // Shell commands should only be callable by ADB.
188 uid_t uid = AIBinder_getCallingUid();
189 if (uid != AID_ROOT && uid != AID_SHELL) {
190 if (resultReceiver != nullptr) {
191 resultReceiver->send(-1);
192 }
193 return STATUS_PERMISSION_DENIED;
194 }
195
196 // Check that the file descriptors are valid.
197 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
198 if (resultReceiver != nullptr) {
199 resultReceiver->send(-1);
200 }
201 return STATUS_BAD_VALUE;
202 }
203
204 binder_status_t status = getClass()->handleShellCommand(
205 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
206 if (resultReceiver != nullptr) {
207 resultReceiver->send(status);
208 }
209 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700210 } else {
211 return BBinder::onTransact(code, data, reply, flags);
212 }
213}
214
Steven Moreland71cddc32018-08-30 23:39:22 -0700215ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700216 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700217 CHECK(binder != nullptr);
218}
219ABpBinder::~ABpBinder() {}
220
Steven Moreland94968952018-09-05 14:42:59 -0700221void ABpBinder::onLastStrongRef(const void* id) {
222 {
223 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
224 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
225 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
226 // longer be able to exist at the time of this method call, there is no longer a need to
227 // recover it.
228
229 ABpBinderTag::Value* value =
230 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
231 if (value != nullptr) {
232 value->binder = nullptr;
233 }
234 }
235
236 BpRefBase::onLastStrongRef(id);
237}
238
239sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700240 if (binder == nullptr) {
241 return nullptr;
242 }
243 if (ABBinderTag::has(binder)) {
244 return static_cast<ABBinder*>(binder.get());
245 }
Steven Moreland94968952018-09-05 14:42:59 -0700246
247 // The following code ensures that for a given binder object (remote or local), if it is not an
248 // ABBinder then at most one ABpBinder object exists in a given process representing it.
249 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
250
251 ABpBinderTag::Value* value =
252 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
253 if (value == nullptr) {
254 value = new ABpBinderTag::Value;
255 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
256 ABpBinderTag::clean);
257 }
258
259 sp<ABpBinder> ret = value->binder.promote();
260 if (ret == nullptr) {
261 ret = new ABpBinder(binder);
262 value->binder = ret;
263 }
264
265 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700266}
267
Steven Moreland2e87adc2018-08-20 19:47:00 -0700268struct AIBinder_Weak {
269 wp<AIBinder> binder;
270};
271AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700272 if (binder == nullptr) {
273 return nullptr;
274 }
275
Steven Moreland2e87adc2018-08-20 19:47:00 -0700276 return new AIBinder_Weak{wp<AIBinder>(binder)};
277}
Steven Moreland9b80e282018-09-19 13:57:23 -0700278void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
279 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700280}
281AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700282 if (weakBinder == nullptr) {
283 return nullptr;
284 }
285
Steven Moreland2e87adc2018-08-20 19:47:00 -0700286 sp<AIBinder> binder = weakBinder->binder.promote();
287 AIBinder_incStrong(binder.get());
288 return binder.get();
289}
290
291AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
292 AIBinder_Class_onDestroy onDestroy,
293 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700294 : onCreate(onCreate),
295 onDestroy(onDestroy),
296 onTransact(onTransact),
297 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700298
299AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
300 AIBinder_Class_onCreate onCreate,
301 AIBinder_Class_onDestroy onDestroy,
302 AIBinder_Class_onTransact onTransact) {
303 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
304 onTransact == nullptr) {
305 return nullptr;
306 }
307
308 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
309}
310
Steven Morelanda194c452019-03-04 16:47:07 -0800311void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
312 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
313
314 // this is required to be called before instances are instantiated
315 clazz->onDump = onDump;
316}
317
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800318void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
319 AIBinder_handleShellCommand handleShellCommand) {
320 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
321
322 clazz->handleShellCommand = handleShellCommand;
323}
324
Steven Moreland901c7452018-09-04 16:17:28 -0700325void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
326 CHECK(who == mWho);
327
328 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700329
330 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
331 sp<IBinder> strongWho = who.promote();
332
333 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
334 if (recipient != nullptr && strongWho != nullptr) {
335 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
336 if (result != ::android::DEAD_OBJECT) {
337 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
338 }
339 }
340
Steven Moreland901c7452018-09-04 16:17:28 -0700341 mWho = nullptr;
342}
343
344AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700345 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700346 CHECK(onDied != nullptr);
347}
348
Steven Moreland64127ca2019-03-13 09:25:44 -0700349void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
350 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
351 [](const sp<TransferDeathRecipient>& tdr) {
352 return tdr->getWho() == nullptr;
353 }),
354 mDeathRecipients.end());
355}
356
357binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700358 CHECK(binder != nullptr);
359
360 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
361
362 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700363 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700364
Steven Moreland64127ca2019-03-13 09:25:44 -0700365 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700366 if (status != STATUS_OK) {
367 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700368 }
369
370 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700371
372 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700373 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700374}
375
Steven Moreland64127ca2019-03-13 09:25:44 -0700376binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700377 CHECK(binder != nullptr);
378
379 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
380
381 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
382 sp<TransferDeathRecipient> recipient = *it;
383
Steven Moreland64127ca2019-03-13 09:25:44 -0700384 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700385 mDeathRecipients.erase(it.base() - 1);
386
Steven Moreland64127ca2019-03-13 09:25:44 -0700387 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700388 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700389 LOG(ERROR) << __func__
390 << ": removed reference to death recipient but unlink failed.";
391 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700392 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700393 }
394 }
395
Steven Moreland5d62e442018-09-13 15:01:02 -0700396 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700397}
398
399// start of C-API methods
400
Steven Moreland2e87adc2018-08-20 19:47:00 -0700401AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
402 if (clazz == nullptr) {
403 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
404 return nullptr;
405 }
406
407 void* userData = clazz->onCreate(args);
408
Steven Moreland71cddc32018-08-30 23:39:22 -0700409 sp<AIBinder> ret = new ABBinder(clazz, userData);
410 ABBinderTag::attach(ret->getBinder());
411
412 AIBinder_incStrong(ret.get());
413 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700414}
415
Steven Moreland5ea54da2018-09-04 13:29:55 -0700416bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700417 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800418 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700419 }
420
421 return binder->isRemote();
422}
423
Steven Moreland65867d72018-09-04 14:22:26 -0700424bool AIBinder_isAlive(const AIBinder* binder) {
425 if (binder == nullptr) {
426 return false;
427 }
428
429 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
430}
431
432binder_status_t AIBinder_ping(AIBinder* binder) {
433 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700434 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700435 }
436
Steven Moreland5d62e442018-09-13 15:01:02 -0700437 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700438}
439
Steven Morelanda194c452019-03-04 16:47:07 -0800440binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
441 if (binder == nullptr) {
442 return STATUS_UNEXPECTED_NULL;
443 }
444
445 ABBinder* bBinder = binder->asABBinder();
446 if (bBinder != nullptr) {
447 AIBinder_onDump onDump = binder->getClass()->onDump;
448 if (onDump == nullptr) {
449 return STATUS_OK;
450 }
451 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
452 }
453
454 ::android::Vector<String16> utf16Args;
455 utf16Args.setCapacity(numArgs);
456 for (uint32_t i = 0; i < numArgs; i++) {
457 utf16Args.push(String16(String8(args[i])));
458 }
459
460 status_t status = binder->getBinder()->dump(fd, utf16Args);
461 return PruneStatusT(status);
462}
463
Steven Moreland901c7452018-09-04 16:17:28 -0700464binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
465 void* cookie) {
466 if (binder == nullptr || recipient == nullptr) {
467 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700468 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700469 }
470
Steven Moreland5d62e442018-09-13 15:01:02 -0700471 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700472 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700473}
474
475binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
476 void* cookie) {
477 if (binder == nullptr || recipient == nullptr) {
478 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700479 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700480 }
481
Steven Moreland5d62e442018-09-13 15:01:02 -0700482 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700483 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700484}
485
Steven Morelandf3034b02018-11-12 17:37:46 -0800486uid_t AIBinder_getCallingUid() {
487 return ::android::IPCThreadState::self()->getCallingUid();
488}
489
490pid_t AIBinder_getCallingPid() {
491 return ::android::IPCThreadState::self()->getCallingPid();
492}
493
Steven Moreland2e87adc2018-08-20 19:47:00 -0700494void AIBinder_incStrong(AIBinder* binder) {
495 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700496 return;
497 }
498
499 binder->incStrong(nullptr);
500}
501void AIBinder_decStrong(AIBinder* binder) {
502 if (binder == nullptr) {
503 LOG(ERROR) << __func__ << ": on null binder";
504 return;
505 }
506
507 binder->decStrong(nullptr);
508}
509int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
510 if (binder == nullptr) {
511 LOG(ERROR) << __func__ << ": on null binder";
512 return -1;
513 }
514
515 return binder->getStrongCount();
516}
517
Steven Moreland71cddc32018-08-30 23:39:22 -0700518bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
519 if (binder == nullptr) {
520 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700521 }
522
Steven Moreland71cddc32018-08-30 23:39:22 -0700523 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700524}
525
526const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
527 if (binder == nullptr) {
528 return nullptr;
529 }
530
531 return binder->getClass();
532}
533
534void* AIBinder_getUserData(AIBinder* binder) {
535 if (binder == nullptr) {
536 return nullptr;
537 }
538
539 ABBinder* bBinder = binder->asABBinder();
540 if (bBinder == nullptr) {
541 return nullptr;
542 }
543
544 return bBinder->getUserData();
545}
546
547binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
548 if (binder == nullptr || in == nullptr) {
549 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700550 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700551 }
552 const AIBinder_Class* clazz = binder->getClass();
553 if (clazz == nullptr) {
554 LOG(ERROR) << __func__
555 << ": Class must be defined for a remote binder transaction. See "
556 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700557 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700558 }
559
Steven Moreland3527c2e2018-09-05 17:07:14 -0700560 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700561 LOG(WARNING) << "A binder object at " << binder
562 << " is being transacted on, however, this object is in the same process as "
563 "its proxy. Transacting with this binder is expensive compared to just "
564 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700565 }
566
Steven Moreland2e87adc2018-08-20 19:47:00 -0700567 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700568 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700569 binder_status_t ret = PruneStatusT(status);
570
571 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700572 delete *in;
573 *in = nullptr;
574 }
575
Steven Moreland5d62e442018-09-13 15:01:02 -0700576 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700577}
578
Steven Moreland9b80e282018-09-19 13:57:23 -0700579static void DestroyParcel(AParcel** parcel) {
580 delete *parcel;
581 *parcel = nullptr;
582}
583
Steven Moreland2e87adc2018-08-20 19:47:00 -0700584binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
585 AParcel** out, binder_flags_t flags) {
586 if (in == nullptr) {
587 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700588 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700589 }
590
Steven Morelandcaa776c2018-09-04 13:48:11 -0700591 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700592 // This object is the input to the transaction. This function takes ownership of it and deletes
593 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700594 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700595
596 if (!isUserCommand(code)) {
597 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700598 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700599 }
600
Steven Moreland46b5fea2019-10-15 11:22:18 -0700601 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
602 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700603 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700604 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700605 }
606
607 if (binder == nullptr || *in == nullptr || out == nullptr) {
608 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700609 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700610 }
611
612 if ((*in)->getBinder() != binder) {
613 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
614 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700615 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700616 }
617
618 *out = new AParcel(binder);
619
Steven Morelandf18615b2018-09-14 11:43:06 -0700620 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700621 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700622
Steven Moreland5d62e442018-09-13 15:01:02 -0700623 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700624 delete *out;
625 *out = nullptr;
626 }
627
Steven Moreland5d62e442018-09-13 15:01:02 -0700628 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700629}
Steven Moreland901c7452018-09-04 16:17:28 -0700630
631AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
632 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
633 if (onBinderDied == nullptr) {
634 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
635 return nullptr;
636 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700637 auto ret = new AIBinder_DeathRecipient(onBinderDied);
638 ret->incStrong(nullptr);
639 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700640}
641
Steven Moreland9b80e282018-09-19 13:57:23 -0700642void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700643 if (recipient == nullptr) {
644 return;
645 }
646
647 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700648}
Steven Morelandea14ef22019-08-20 10:50:08 -0700649
650binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
651 if (binder == nullptr || outExt == nullptr) {
652 if (outExt != nullptr) {
653 *outExt = nullptr;
654 }
655 return STATUS_UNEXPECTED_NULL;
656 }
657
658 sp<IBinder> ext;
659 status_t res = binder->getBinder()->getExtension(&ext);
660
661 if (res != android::OK) {
662 *outExt = nullptr;
663 return PruneStatusT(res);
664 }
665
666 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
667 if (ret != nullptr) ret->incStrong(binder);
668
669 *outExt = ret.get();
670 return STATUS_OK;
671}
672
673binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
674 if (binder == nullptr || ext == nullptr) {
675 return STATUS_UNEXPECTED_NULL;
676 }
677
678 ABBinder* rawBinder = binder->asABBinder();
679 if (rawBinder == nullptr) {
680 return STATUS_INVALID_OPERATION;
681 }
682
683 rawBinder->setExtension(ext->getBinder());
684 return STATUS_OK;
685}
Steven Moreland2f405f52020-07-08 22:24:29 +0000686
687// platform methods follow
688
689void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
690 ABBinder* localBinder = binder->asABBinder();
691 if (localBinder == nullptr) {
692 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
693 }
694
695 localBinder->setRequestingSid(requestingSid);
696}
697
698const char* AIBinder_getCallingSid() {
699 return ::android::IPCThreadState::self()->getCallingSid();
700}
Steven Morelandb2de9532020-05-29 21:44:41 +0000701
702android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
703 if (binder == nullptr) return nullptr;
704 return binder->getBinder();
705}
706
707AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
708 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
709 AIBinder_incStrong(ndkBinder.get());
710 return ndkBinder.get();
711}