blob: 577f0a0f2bab9d85c006fbc30e141c853b57a794 [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
Janis Danisevskis798a09a2020-08-18 08:35:38 -070017#include <android/binder_context.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070018#include <android/binder_ibinder.h>
Steven Moreland2f405f52020-07-08 22:24:29 +000019#include <android/binder_ibinder_platform.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070020#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070021
Steven Moreland46b5fea2019-10-15 11:22:18 -070022#include <android/binder_stability.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070023#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070024#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070025#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070026
27#include <android-base/logging.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080028#include <binder/IPCThreadState.h>
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080029#include <binder/IResultReceiver.h>
30#include <private/android_filesystem_config.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070031
Steven Moreland901c7452018-09-04 16:17:28 -070032using DeathRecipient = ::android::IBinder::DeathRecipient;
33
Steven Moreland2e87adc2018-08-20 19:47:00 -070034using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080035using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070036using ::android::Parcel;
37using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070038using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070039using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080040using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070041using ::android::wp;
42
Steven Moreland71cddc32018-08-30 23:39:22 -070043namespace ABBinderTag {
44
45static const void* kId = "ABBinder";
46static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070047void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070048
49static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070050 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070051}
52static bool has(const sp<IBinder>& binder) {
53 return binder != nullptr && binder->findObject(kId) == kValue;
54}
55
Steven Moreland6cf66ac2018-11-02 18:14:54 -070056} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070057
Steven Moreland94968952018-09-05 14:42:59 -070058namespace ABpBinderTag {
59
60static std::mutex gLock;
61static const void* kId = "ABpBinder";
62struct Value {
63 wp<ABpBinder> binder;
64};
65void clean(const void* id, void* obj, void* cookie) {
66 CHECK(id == kId) << id << " " << obj << " " << cookie;
67
68 delete static_cast<Value*>(obj);
69};
70
Steven Moreland6cf66ac2018-11-02 18:14:54 -070071} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070072
Steven Moreland2e87adc2018-08-20 19:47:00 -070073AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
74AIBinder::~AIBinder() {}
75
Andrei Homescu9556bb12020-09-21 16:59:45 -070076std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz,
77 const String8& newDescriptor, bool set) {
78 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -070079 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070080
Steven Moreland2e87adc2018-08-20 19:47:00 -070081 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
Andrei Homescu9556bb12020-09-21 16:59:45 -070099 if (set) {
100 // if this is a local object, it's not one known to libbinder_ndk
101 mClazz = clazz;
Steven Moreland9facfce2020-10-02 17:26:56 +0000102 return true;
Andrei Homescu9556bb12020-09-21 16:59:45 -0700103 }
104
105 return {};
106}
107
108bool AIBinder::associateClass(const AIBinder_Class* clazz) {
109 if (clazz == nullptr) return false;
110
111 String8 newDescriptor(clazz->getInterfaceDescriptor());
112
113 auto result = associateClassInternal(clazz, newDescriptor, false);
114 if (result.has_value()) return *result;
115
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700116 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700117
Steven Moreland2e87adc2018-08-20 19:47:00 -0700118 String8 descriptor(getBinder()->getInterfaceDescriptor());
119 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000120 if (getBinder()->isBinderAlive()) {
121 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
122 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
123 } else {
124 // b/155793159
125 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor.c_str()
126 << "' to dead binder.";
127 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700128 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700129 }
130
Steven Moreland9facfce2020-10-02 17:26:56 +0000131 return associateClassInternal(clazz, newDescriptor, true).value();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700132}
133
134ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700135 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700136 CHECK(clazz != nullptr);
137}
138ABBinder::~ABBinder() {
139 getClass()->onDestroy(mUserData);
140}
141
142const String16& ABBinder::getInterfaceDescriptor() const {
143 return getClass()->getInterfaceDescriptor();
144}
145
Steven Morelanda194c452019-03-04 16:47:07 -0800146status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
147 AIBinder_onDump onDump = getClass()->onDump;
148
149 if (onDump == nullptr) {
150 return STATUS_OK;
151 }
152
153 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
154 // null in Java
155 if (args.size() > INT32_MAX) {
156 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
157 return STATUS_BAD_VALUE;
158 }
159
160 std::vector<String8> utf8Args; // owns memory of utf8s
161 utf8Args.reserve(args.size());
162 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
163 utf8Pointers.reserve(args.size());
164
165 for (size_t i = 0; i < args.size(); i++) {
166 utf8Args.push_back(String8(args[i]));
167 utf8Pointers.push_back(utf8Args[i].c_str());
168 }
169
170 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
171}
172
Steven Moreland5d62e442018-09-13 15:01:02 -0700173status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
174 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700175 if (isUserCommand(code)) {
176 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700177 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700178 }
179
180 const AParcel in = AParcel::readOnly(this, &data);
181 AParcel out = AParcel(this, reply, false /*owns*/);
182
Steven Moreland5d62e442018-09-13 15:01:02 -0700183 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
184 return PruneStatusT(status);
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800185 } else if (code == SHELL_COMMAND_TRANSACTION) {
186 int in = data.readFileDescriptor();
187 int out = data.readFileDescriptor();
188 int err = data.readFileDescriptor();
189
190 int argc = data.readInt32();
191 std::vector<String8> utf8Args; // owns memory of utf8s
192 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
193 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
194 utf8Args.push_back(String8(data.readString16()));
195 utf8Pointers.push_back(utf8Args[i].c_str());
196 }
197
198 data.readStrongBinder(); // skip over the IShellCallback
199 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
200
201 // Shell commands should only be callable by ADB.
202 uid_t uid = AIBinder_getCallingUid();
203 if (uid != AID_ROOT && uid != AID_SHELL) {
204 if (resultReceiver != nullptr) {
205 resultReceiver->send(-1);
206 }
207 return STATUS_PERMISSION_DENIED;
208 }
209
210 // Check that the file descriptors are valid.
211 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
212 if (resultReceiver != nullptr) {
213 resultReceiver->send(-1);
214 }
215 return STATUS_BAD_VALUE;
216 }
217
218 binder_status_t status = getClass()->handleShellCommand(
219 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
220 if (resultReceiver != nullptr) {
221 resultReceiver->send(status);
222 }
223 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700224 } else {
225 return BBinder::onTransact(code, data, reply, flags);
226 }
227}
228
Steven Moreland71cddc32018-08-30 23:39:22 -0700229ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700230 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700231 CHECK(binder != nullptr);
232}
233ABpBinder::~ABpBinder() {}
234
Steven Moreland94968952018-09-05 14:42:59 -0700235void ABpBinder::onLastStrongRef(const void* id) {
236 {
237 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
238 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
239 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
240 // longer be able to exist at the time of this method call, there is no longer a need to
241 // recover it.
242
243 ABpBinderTag::Value* value =
244 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
245 if (value != nullptr) {
246 value->binder = nullptr;
247 }
248 }
249
250 BpRefBase::onLastStrongRef(id);
251}
252
253sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700254 if (binder == nullptr) {
255 return nullptr;
256 }
257 if (ABBinderTag::has(binder)) {
258 return static_cast<ABBinder*>(binder.get());
259 }
Steven Moreland94968952018-09-05 14:42:59 -0700260
261 // The following code ensures that for a given binder object (remote or local), if it is not an
262 // ABBinder then at most one ABpBinder object exists in a given process representing it.
263 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
264
265 ABpBinderTag::Value* value =
266 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
267 if (value == nullptr) {
268 value = new ABpBinderTag::Value;
269 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
270 ABpBinderTag::clean);
271 }
272
273 sp<ABpBinder> ret = value->binder.promote();
274 if (ret == nullptr) {
275 ret = new ABpBinder(binder);
276 value->binder = ret;
277 }
278
279 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700280}
281
Steven Moreland2e87adc2018-08-20 19:47:00 -0700282struct AIBinder_Weak {
283 wp<AIBinder> binder;
284};
285AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700286 if (binder == nullptr) {
287 return nullptr;
288 }
289
Steven Moreland2e87adc2018-08-20 19:47:00 -0700290 return new AIBinder_Weak{wp<AIBinder>(binder)};
291}
Steven Moreland9b80e282018-09-19 13:57:23 -0700292void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
293 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700294}
295AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700296 if (weakBinder == nullptr) {
297 return nullptr;
298 }
299
Steven Moreland2e87adc2018-08-20 19:47:00 -0700300 sp<AIBinder> binder = weakBinder->binder.promote();
301 AIBinder_incStrong(binder.get());
302 return binder.get();
303}
304
305AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
306 AIBinder_Class_onDestroy onDestroy,
307 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700308 : onCreate(onCreate),
309 onDestroy(onDestroy),
310 onTransact(onTransact),
311 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700312
313AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
314 AIBinder_Class_onCreate onCreate,
315 AIBinder_Class_onDestroy onDestroy,
316 AIBinder_Class_onTransact onTransact) {
317 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
318 onTransact == nullptr) {
319 return nullptr;
320 }
321
322 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
323}
324
Steven Morelanda194c452019-03-04 16:47:07 -0800325void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
326 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
327
328 // this is required to be called before instances are instantiated
329 clazz->onDump = onDump;
330}
331
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800332void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
333 AIBinder_handleShellCommand handleShellCommand) {
334 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
335
336 clazz->handleShellCommand = handleShellCommand;
337}
338
Steven Moreland901c7452018-09-04 16:17:28 -0700339void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
340 CHECK(who == mWho);
341
342 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700343
344 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
345 sp<IBinder> strongWho = who.promote();
346
347 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
348 if (recipient != nullptr && strongWho != nullptr) {
349 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
350 if (result != ::android::DEAD_OBJECT) {
351 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
352 }
353 }
354
Steven Moreland901c7452018-09-04 16:17:28 -0700355 mWho = nullptr;
356}
357
358AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700359 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700360 CHECK(onDied != nullptr);
361}
362
Steven Moreland64127ca2019-03-13 09:25:44 -0700363void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
364 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
365 [](const sp<TransferDeathRecipient>& tdr) {
366 return tdr->getWho() == nullptr;
367 }),
368 mDeathRecipients.end());
369}
370
371binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700372 CHECK(binder != nullptr);
373
374 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
375
376 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700377 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700378
Steven Moreland64127ca2019-03-13 09:25:44 -0700379 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700380 if (status != STATUS_OK) {
381 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700382 }
383
384 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700385
386 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700387 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700388}
389
Steven Moreland64127ca2019-03-13 09:25:44 -0700390binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700391 CHECK(binder != nullptr);
392
393 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
394
395 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
396 sp<TransferDeathRecipient> recipient = *it;
397
Steven Moreland64127ca2019-03-13 09:25:44 -0700398 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700399 mDeathRecipients.erase(it.base() - 1);
400
Steven Moreland64127ca2019-03-13 09:25:44 -0700401 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700402 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700403 LOG(ERROR) << __func__
404 << ": removed reference to death recipient but unlink failed.";
405 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700406 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700407 }
408 }
409
Steven Moreland5d62e442018-09-13 15:01:02 -0700410 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700411}
412
413// start of C-API methods
414
Steven Moreland2e87adc2018-08-20 19:47:00 -0700415AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
416 if (clazz == nullptr) {
417 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
418 return nullptr;
419 }
420
421 void* userData = clazz->onCreate(args);
422
Steven Moreland71cddc32018-08-30 23:39:22 -0700423 sp<AIBinder> ret = new ABBinder(clazz, userData);
424 ABBinderTag::attach(ret->getBinder());
425
426 AIBinder_incStrong(ret.get());
427 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700428}
429
Steven Moreland5ea54da2018-09-04 13:29:55 -0700430bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700431 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800432 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700433 }
434
435 return binder->isRemote();
436}
437
Steven Moreland65867d72018-09-04 14:22:26 -0700438bool AIBinder_isAlive(const AIBinder* binder) {
439 if (binder == nullptr) {
440 return false;
441 }
442
443 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
444}
445
446binder_status_t AIBinder_ping(AIBinder* binder) {
447 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700448 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700449 }
450
Steven Moreland5d62e442018-09-13 15:01:02 -0700451 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700452}
453
Steven Morelanda194c452019-03-04 16:47:07 -0800454binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
455 if (binder == nullptr) {
456 return STATUS_UNEXPECTED_NULL;
457 }
458
459 ABBinder* bBinder = binder->asABBinder();
460 if (bBinder != nullptr) {
461 AIBinder_onDump onDump = binder->getClass()->onDump;
462 if (onDump == nullptr) {
463 return STATUS_OK;
464 }
465 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
466 }
467
468 ::android::Vector<String16> utf16Args;
469 utf16Args.setCapacity(numArgs);
470 for (uint32_t i = 0; i < numArgs; i++) {
471 utf16Args.push(String16(String8(args[i])));
472 }
473
474 status_t status = binder->getBinder()->dump(fd, utf16Args);
475 return PruneStatusT(status);
476}
477
Steven Moreland901c7452018-09-04 16:17:28 -0700478binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
479 void* cookie) {
480 if (binder == nullptr || recipient == nullptr) {
481 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700482 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700483 }
484
Steven Moreland5d62e442018-09-13 15:01:02 -0700485 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700486 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700487}
488
489binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
490 void* cookie) {
491 if (binder == nullptr || recipient == nullptr) {
492 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700493 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700494 }
495
Steven Moreland5d62e442018-09-13 15:01:02 -0700496 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700497 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700498}
499
Steven Morelandf3034b02018-11-12 17:37:46 -0800500uid_t AIBinder_getCallingUid() {
501 return ::android::IPCThreadState::self()->getCallingUid();
502}
503
504pid_t AIBinder_getCallingPid() {
505 return ::android::IPCThreadState::self()->getCallingPid();
506}
507
Steven Moreland2e87adc2018-08-20 19:47:00 -0700508void AIBinder_incStrong(AIBinder* binder) {
509 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700510 return;
511 }
512
513 binder->incStrong(nullptr);
514}
515void AIBinder_decStrong(AIBinder* binder) {
516 if (binder == nullptr) {
517 LOG(ERROR) << __func__ << ": on null binder";
518 return;
519 }
520
521 binder->decStrong(nullptr);
522}
523int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
524 if (binder == nullptr) {
525 LOG(ERROR) << __func__ << ": on null binder";
526 return -1;
527 }
528
529 return binder->getStrongCount();
530}
531
Steven Moreland71cddc32018-08-30 23:39:22 -0700532bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
533 if (binder == nullptr) {
534 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700535 }
536
Steven Moreland71cddc32018-08-30 23:39:22 -0700537 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700538}
539
540const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
541 if (binder == nullptr) {
542 return nullptr;
543 }
544
545 return binder->getClass();
546}
547
548void* AIBinder_getUserData(AIBinder* binder) {
549 if (binder == nullptr) {
550 return nullptr;
551 }
552
553 ABBinder* bBinder = binder->asABBinder();
554 if (bBinder == nullptr) {
555 return nullptr;
556 }
557
558 return bBinder->getUserData();
559}
560
561binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
562 if (binder == nullptr || in == nullptr) {
563 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700564 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700565 }
566 const AIBinder_Class* clazz = binder->getClass();
567 if (clazz == nullptr) {
568 LOG(ERROR) << __func__
569 << ": Class must be defined for a remote binder transaction. See "
570 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700571 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700572 }
573
Steven Moreland3527c2e2018-09-05 17:07:14 -0700574 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700575 LOG(WARNING) << "A binder object at " << binder
576 << " is being transacted on, however, this object is in the same process as "
577 "its proxy. Transacting with this binder is expensive compared to just "
578 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700579 }
580
Steven Moreland2e87adc2018-08-20 19:47:00 -0700581 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700582 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700583 binder_status_t ret = PruneStatusT(status);
584
585 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700586 delete *in;
587 *in = nullptr;
588 }
589
Steven Moreland5d62e442018-09-13 15:01:02 -0700590 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700591}
592
Steven Moreland9b80e282018-09-19 13:57:23 -0700593static void DestroyParcel(AParcel** parcel) {
594 delete *parcel;
595 *parcel = nullptr;
596}
597
Steven Moreland2e87adc2018-08-20 19:47:00 -0700598binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
599 AParcel** out, binder_flags_t flags) {
600 if (in == nullptr) {
601 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700602 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700603 }
604
Steven Morelandcaa776c2018-09-04 13:48:11 -0700605 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700606 // This object is the input to the transaction. This function takes ownership of it and deletes
607 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700608 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700609
610 if (!isUserCommand(code)) {
611 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700612 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700613 }
614
Steven Moreland46b5fea2019-10-15 11:22:18 -0700615 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
616 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700617 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700618 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700619 }
620
621 if (binder == nullptr || *in == nullptr || out == nullptr) {
622 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700623 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700624 }
625
626 if ((*in)->getBinder() != binder) {
627 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
628 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700629 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700630 }
631
632 *out = new AParcel(binder);
633
Steven Morelandf18615b2018-09-14 11:43:06 -0700634 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700635 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700636
Steven Moreland5d62e442018-09-13 15:01:02 -0700637 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700638 delete *out;
639 *out = nullptr;
640 }
641
Steven Moreland5d62e442018-09-13 15:01:02 -0700642 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700643}
Steven Moreland901c7452018-09-04 16:17:28 -0700644
645AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
646 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
647 if (onBinderDied == nullptr) {
648 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
649 return nullptr;
650 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700651 auto ret = new AIBinder_DeathRecipient(onBinderDied);
652 ret->incStrong(nullptr);
653 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700654}
655
Steven Moreland9b80e282018-09-19 13:57:23 -0700656void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700657 if (recipient == nullptr) {
658 return;
659 }
660
661 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700662}
Steven Morelandea14ef22019-08-20 10:50:08 -0700663
664binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
665 if (binder == nullptr || outExt == nullptr) {
666 if (outExt != nullptr) {
667 *outExt = nullptr;
668 }
669 return STATUS_UNEXPECTED_NULL;
670 }
671
672 sp<IBinder> ext;
673 status_t res = binder->getBinder()->getExtension(&ext);
674
675 if (res != android::OK) {
676 *outExt = nullptr;
677 return PruneStatusT(res);
678 }
679
680 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
681 if (ret != nullptr) ret->incStrong(binder);
682
683 *outExt = ret.get();
684 return STATUS_OK;
685}
686
687binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
688 if (binder == nullptr || ext == nullptr) {
689 return STATUS_UNEXPECTED_NULL;
690 }
691
692 ABBinder* rawBinder = binder->asABBinder();
693 if (rawBinder == nullptr) {
694 return STATUS_INVALID_OPERATION;
695 }
696
697 rawBinder->setExtension(ext->getBinder());
698 return STATUS_OK;
699}
Steven Moreland2f405f52020-07-08 22:24:29 +0000700
701// platform methods follow
702
703void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
704 ABBinder* localBinder = binder->asABBinder();
705 if (localBinder == nullptr) {
706 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
707 }
708
709 localBinder->setRequestingSid(requestingSid);
710}
711
712const char* AIBinder_getCallingSid() {
713 return ::android::IPCThreadState::self()->getCallingSid();
714}
Steven Morelandb2de9532020-05-29 21:44:41 +0000715
716android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
717 if (binder == nullptr) return nullptr;
718 return binder->getBinder();
719}
720
721AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
722 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
723 AIBinder_incStrong(ndkBinder.get());
724 return ndkBinder.get();
725}