blob: ceabce08c8ae664fb64dc3d701e611c2644e7124 [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) {
103 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
104 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -0700105 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700106 }
107
Steven Moreland71cddc32018-08-30 23:39:22 -0700108 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700109 mClazz = clazz;
110
Steven Moreland71cddc32018-08-30 23:39:22 -0700111 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700112}
113
114ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700115 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700116 CHECK(clazz != nullptr);
117}
118ABBinder::~ABBinder() {
119 getClass()->onDestroy(mUserData);
120}
121
122const String16& ABBinder::getInterfaceDescriptor() const {
123 return getClass()->getInterfaceDescriptor();
124}
125
Steven Morelanda194c452019-03-04 16:47:07 -0800126status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
127 AIBinder_onDump onDump = getClass()->onDump;
128
129 if (onDump == nullptr) {
130 return STATUS_OK;
131 }
132
133 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
134 // null in Java
135 if (args.size() > INT32_MAX) {
136 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
137 return STATUS_BAD_VALUE;
138 }
139
140 std::vector<String8> utf8Args; // owns memory of utf8s
141 utf8Args.reserve(args.size());
142 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
143 utf8Pointers.reserve(args.size());
144
145 for (size_t i = 0; i < args.size(); i++) {
146 utf8Args.push_back(String8(args[i]));
147 utf8Pointers.push_back(utf8Args[i].c_str());
148 }
149
150 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
151}
152
Steven Moreland5d62e442018-09-13 15:01:02 -0700153status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
154 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700155 if (isUserCommand(code)) {
156 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700157 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700158 }
159
160 const AParcel in = AParcel::readOnly(this, &data);
161 AParcel out = AParcel(this, reply, false /*owns*/);
162
Steven Moreland5d62e442018-09-13 15:01:02 -0700163 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
164 return PruneStatusT(status);
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800165 } else if (code == SHELL_COMMAND_TRANSACTION) {
166 int in = data.readFileDescriptor();
167 int out = data.readFileDescriptor();
168 int err = data.readFileDescriptor();
169
170 int argc = data.readInt32();
171 std::vector<String8> utf8Args; // owns memory of utf8s
172 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
173 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
174 utf8Args.push_back(String8(data.readString16()));
175 utf8Pointers.push_back(utf8Args[i].c_str());
176 }
177
178 data.readStrongBinder(); // skip over the IShellCallback
179 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
180
181 // Shell commands should only be callable by ADB.
182 uid_t uid = AIBinder_getCallingUid();
183 if (uid != AID_ROOT && uid != AID_SHELL) {
184 if (resultReceiver != nullptr) {
185 resultReceiver->send(-1);
186 }
187 return STATUS_PERMISSION_DENIED;
188 }
189
190 // Check that the file descriptors are valid.
191 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
192 if (resultReceiver != nullptr) {
193 resultReceiver->send(-1);
194 }
195 return STATUS_BAD_VALUE;
196 }
197
198 binder_status_t status = getClass()->handleShellCommand(
199 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
200 if (resultReceiver != nullptr) {
201 resultReceiver->send(status);
202 }
203 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700204 } else {
205 return BBinder::onTransact(code, data, reply, flags);
206 }
207}
208
Steven Moreland71cddc32018-08-30 23:39:22 -0700209ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700210 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700211 CHECK(binder != nullptr);
212}
213ABpBinder::~ABpBinder() {}
214
Steven Moreland94968952018-09-05 14:42:59 -0700215void ABpBinder::onLastStrongRef(const void* id) {
216 {
217 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
218 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
219 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
220 // longer be able to exist at the time of this method call, there is no longer a need to
221 // recover it.
222
223 ABpBinderTag::Value* value =
224 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
225 if (value != nullptr) {
226 value->binder = nullptr;
227 }
228 }
229
230 BpRefBase::onLastStrongRef(id);
231}
232
233sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700234 if (binder == nullptr) {
235 return nullptr;
236 }
237 if (ABBinderTag::has(binder)) {
238 return static_cast<ABBinder*>(binder.get());
239 }
Steven Moreland94968952018-09-05 14:42:59 -0700240
241 // The following code ensures that for a given binder object (remote or local), if it is not an
242 // ABBinder then at most one ABpBinder object exists in a given process representing it.
243 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
244
245 ABpBinderTag::Value* value =
246 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
247 if (value == nullptr) {
248 value = new ABpBinderTag::Value;
249 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
250 ABpBinderTag::clean);
251 }
252
253 sp<ABpBinder> ret = value->binder.promote();
254 if (ret == nullptr) {
255 ret = new ABpBinder(binder);
256 value->binder = ret;
257 }
258
259 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700260}
261
Steven Moreland2e87adc2018-08-20 19:47:00 -0700262struct AIBinder_Weak {
263 wp<AIBinder> binder;
264};
265AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700266 if (binder == nullptr) {
267 return nullptr;
268 }
269
Steven Moreland2e87adc2018-08-20 19:47:00 -0700270 return new AIBinder_Weak{wp<AIBinder>(binder)};
271}
Steven Moreland9b80e282018-09-19 13:57:23 -0700272void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
273 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700274}
275AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700276 if (weakBinder == nullptr) {
277 return nullptr;
278 }
279
Steven Moreland2e87adc2018-08-20 19:47:00 -0700280 sp<AIBinder> binder = weakBinder->binder.promote();
281 AIBinder_incStrong(binder.get());
282 return binder.get();
283}
284
285AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
286 AIBinder_Class_onDestroy onDestroy,
287 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700288 : onCreate(onCreate),
289 onDestroy(onDestroy),
290 onTransact(onTransact),
291 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700292
293AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
294 AIBinder_Class_onCreate onCreate,
295 AIBinder_Class_onDestroy onDestroy,
296 AIBinder_Class_onTransact onTransact) {
297 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
298 onTransact == nullptr) {
299 return nullptr;
300 }
301
302 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
303}
304
Steven Morelanda194c452019-03-04 16:47:07 -0800305void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
306 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
307
308 // this is required to be called before instances are instantiated
309 clazz->onDump = onDump;
310}
311
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800312void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
313 AIBinder_handleShellCommand handleShellCommand) {
314 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
315
316 clazz->handleShellCommand = handleShellCommand;
317}
318
Steven Moreland901c7452018-09-04 16:17:28 -0700319void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
320 CHECK(who == mWho);
321
322 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700323
324 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
325 sp<IBinder> strongWho = who.promote();
326
327 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
328 if (recipient != nullptr && strongWho != nullptr) {
329 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
330 if (result != ::android::DEAD_OBJECT) {
331 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
332 }
333 }
334
Steven Moreland901c7452018-09-04 16:17:28 -0700335 mWho = nullptr;
336}
337
338AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700339 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700340 CHECK(onDied != nullptr);
341}
342
Steven Moreland64127ca2019-03-13 09:25:44 -0700343void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
344 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
345 [](const sp<TransferDeathRecipient>& tdr) {
346 return tdr->getWho() == nullptr;
347 }),
348 mDeathRecipients.end());
349}
350
351binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700352 CHECK(binder != nullptr);
353
354 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
355
356 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700357 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700358
Steven Moreland64127ca2019-03-13 09:25:44 -0700359 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700360 if (status != STATUS_OK) {
361 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700362 }
363
364 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700365
366 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700367 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700368}
369
Steven Moreland64127ca2019-03-13 09:25:44 -0700370binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700371 CHECK(binder != nullptr);
372
373 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
374
375 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
376 sp<TransferDeathRecipient> recipient = *it;
377
Steven Moreland64127ca2019-03-13 09:25:44 -0700378 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700379 mDeathRecipients.erase(it.base() - 1);
380
Steven Moreland64127ca2019-03-13 09:25:44 -0700381 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700382 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700383 LOG(ERROR) << __func__
384 << ": removed reference to death recipient but unlink failed.";
385 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700386 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700387 }
388 }
389
Steven Moreland5d62e442018-09-13 15:01:02 -0700390 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700391}
392
393// start of C-API methods
394
Steven Moreland2e87adc2018-08-20 19:47:00 -0700395AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
396 if (clazz == nullptr) {
397 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
398 return nullptr;
399 }
400
401 void* userData = clazz->onCreate(args);
402
Steven Moreland71cddc32018-08-30 23:39:22 -0700403 sp<AIBinder> ret = new ABBinder(clazz, userData);
404 ABBinderTag::attach(ret->getBinder());
405
406 AIBinder_incStrong(ret.get());
407 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700408}
409
Steven Moreland5ea54da2018-09-04 13:29:55 -0700410bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700411 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800412 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700413 }
414
415 return binder->isRemote();
416}
417
Steven Moreland65867d72018-09-04 14:22:26 -0700418bool AIBinder_isAlive(const AIBinder* binder) {
419 if (binder == nullptr) {
420 return false;
421 }
422
423 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
424}
425
426binder_status_t AIBinder_ping(AIBinder* binder) {
427 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700428 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700429 }
430
Steven Moreland5d62e442018-09-13 15:01:02 -0700431 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700432}
433
Steven Morelanda194c452019-03-04 16:47:07 -0800434binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
435 if (binder == nullptr) {
436 return STATUS_UNEXPECTED_NULL;
437 }
438
439 ABBinder* bBinder = binder->asABBinder();
440 if (bBinder != nullptr) {
441 AIBinder_onDump onDump = binder->getClass()->onDump;
442 if (onDump == nullptr) {
443 return STATUS_OK;
444 }
445 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
446 }
447
448 ::android::Vector<String16> utf16Args;
449 utf16Args.setCapacity(numArgs);
450 for (uint32_t i = 0; i < numArgs; i++) {
451 utf16Args.push(String16(String8(args[i])));
452 }
453
454 status_t status = binder->getBinder()->dump(fd, utf16Args);
455 return PruneStatusT(status);
456}
457
Steven Moreland901c7452018-09-04 16:17:28 -0700458binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
459 void* cookie) {
460 if (binder == nullptr || recipient == nullptr) {
461 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700462 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700463 }
464
Steven Moreland5d62e442018-09-13 15:01:02 -0700465 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700466 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700467}
468
469binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
470 void* cookie) {
471 if (binder == nullptr || recipient == nullptr) {
472 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700473 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700474 }
475
Steven Moreland5d62e442018-09-13 15:01:02 -0700476 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700477 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700478}
479
Steven Morelandf3034b02018-11-12 17:37:46 -0800480uid_t AIBinder_getCallingUid() {
481 return ::android::IPCThreadState::self()->getCallingUid();
482}
483
484pid_t AIBinder_getCallingPid() {
485 return ::android::IPCThreadState::self()->getCallingPid();
486}
487
Steven Moreland2e87adc2018-08-20 19:47:00 -0700488void AIBinder_incStrong(AIBinder* binder) {
489 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700490 return;
491 }
492
493 binder->incStrong(nullptr);
494}
495void AIBinder_decStrong(AIBinder* binder) {
496 if (binder == nullptr) {
497 LOG(ERROR) << __func__ << ": on null binder";
498 return;
499 }
500
501 binder->decStrong(nullptr);
502}
503int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
504 if (binder == nullptr) {
505 LOG(ERROR) << __func__ << ": on null binder";
506 return -1;
507 }
508
509 return binder->getStrongCount();
510}
511
Steven Moreland71cddc32018-08-30 23:39:22 -0700512bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
513 if (binder == nullptr) {
514 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700515 }
516
Steven Moreland71cddc32018-08-30 23:39:22 -0700517 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700518}
519
520const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
521 if (binder == nullptr) {
522 return nullptr;
523 }
524
525 return binder->getClass();
526}
527
528void* AIBinder_getUserData(AIBinder* binder) {
529 if (binder == nullptr) {
530 return nullptr;
531 }
532
533 ABBinder* bBinder = binder->asABBinder();
534 if (bBinder == nullptr) {
535 return nullptr;
536 }
537
538 return bBinder->getUserData();
539}
540
541binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
542 if (binder == nullptr || in == nullptr) {
543 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700544 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700545 }
546 const AIBinder_Class* clazz = binder->getClass();
547 if (clazz == nullptr) {
548 LOG(ERROR) << __func__
549 << ": Class must be defined for a remote binder transaction. See "
550 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700551 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700552 }
553
Steven Moreland3527c2e2018-09-05 17:07:14 -0700554 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700555 LOG(WARNING) << "A binder object at " << binder
556 << " is being transacted on, however, this object is in the same process as "
557 "its proxy. Transacting with this binder is expensive compared to just "
558 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700559 }
560
Steven Moreland2e87adc2018-08-20 19:47:00 -0700561 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700562 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700563 binder_status_t ret = PruneStatusT(status);
564
565 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700566 delete *in;
567 *in = nullptr;
568 }
569
Steven Moreland5d62e442018-09-13 15:01:02 -0700570 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700571}
572
Steven Moreland9b80e282018-09-19 13:57:23 -0700573static void DestroyParcel(AParcel** parcel) {
574 delete *parcel;
575 *parcel = nullptr;
576}
577
Steven Moreland2e87adc2018-08-20 19:47:00 -0700578binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
579 AParcel** out, binder_flags_t flags) {
580 if (in == nullptr) {
581 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700582 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700583 }
584
Steven Morelandcaa776c2018-09-04 13:48:11 -0700585 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700586 // This object is the input to the transaction. This function takes ownership of it and deletes
587 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700588 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700589
590 if (!isUserCommand(code)) {
591 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700592 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700593 }
594
Steven Moreland46b5fea2019-10-15 11:22:18 -0700595 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
596 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700597 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700598 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700599 }
600
601 if (binder == nullptr || *in == nullptr || out == nullptr) {
602 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700603 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700604 }
605
606 if ((*in)->getBinder() != binder) {
607 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
608 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700609 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700610 }
611
612 *out = new AParcel(binder);
613
Steven Morelandf18615b2018-09-14 11:43:06 -0700614 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700615 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700616
Steven Moreland5d62e442018-09-13 15:01:02 -0700617 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700618 delete *out;
619 *out = nullptr;
620 }
621
Steven Moreland5d62e442018-09-13 15:01:02 -0700622 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700623}
Steven Moreland901c7452018-09-04 16:17:28 -0700624
625AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
626 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
627 if (onBinderDied == nullptr) {
628 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
629 return nullptr;
630 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700631 auto ret = new AIBinder_DeathRecipient(onBinderDied);
632 ret->incStrong(nullptr);
633 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700634}
635
Steven Moreland9b80e282018-09-19 13:57:23 -0700636void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700637 if (recipient == nullptr) {
638 return;
639 }
640
641 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700642}
Steven Morelandea14ef22019-08-20 10:50:08 -0700643
644binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
645 if (binder == nullptr || outExt == nullptr) {
646 if (outExt != nullptr) {
647 *outExt = nullptr;
648 }
649 return STATUS_UNEXPECTED_NULL;
650 }
651
652 sp<IBinder> ext;
653 status_t res = binder->getBinder()->getExtension(&ext);
654
655 if (res != android::OK) {
656 *outExt = nullptr;
657 return PruneStatusT(res);
658 }
659
660 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
661 if (ret != nullptr) ret->incStrong(binder);
662
663 *outExt = ret.get();
664 return STATUS_OK;
665}
666
667binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
668 if (binder == nullptr || ext == nullptr) {
669 return STATUS_UNEXPECTED_NULL;
670 }
671
672 ABBinder* rawBinder = binder->asABBinder();
673 if (rawBinder == nullptr) {
674 return STATUS_INVALID_OPERATION;
675 }
676
677 rawBinder->setExtension(ext->getBinder());
678 return STATUS_OK;
679}
Steven Moreland2f405f52020-07-08 22:24:29 +0000680
681// platform methods follow
682
683void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
684 ABBinder* localBinder = binder->asABBinder();
685 if (localBinder == nullptr) {
686 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
687 }
688
689 localBinder->setRequestingSid(requestingSid);
690}
691
692const char* AIBinder_getCallingSid() {
693 return ::android::IPCThreadState::self()->getCallingSid();
694}