blob: 65214bdd9658e9b582e5fe401d182057ec032598 [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 Moreland8afad292020-11-06 22:26:51 +000019#include <android/binder_libbinder.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
Steven Moreland94968952018-09-05 14:42:59 -070060static 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
Andrei Homescu9556bb12020-09-21 16:59:45 -070075std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz,
Steven Moreland63872b52020-10-02 19:28:23 +000076 const String16& newDescriptor, bool set) {
Andrei Homescu9556bb12020-09-21 16:59:45 -070077 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -070078 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070079
Steven Moreland2e87adc2018-08-20 19:47:00 -070080 if (mClazz != nullptr) {
Steven Moreland63872b52020-10-02 19:28:23 +000081 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -070082 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 '"
Steven Moreland63872b52020-10-02 19:28:23 +000090 << newDescriptor << "' but already set to '" << currentDescriptor << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070091 }
92
Steven Moreland71cddc32018-08-30 23:39:22 -070093 // always a failure because we know mClazz != clazz
94 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070095 }
96
Andrei Homescu9556bb12020-09-21 16:59:45 -070097 if (set) {
98 // if this is a local object, it's not one known to libbinder_ndk
99 mClazz = clazz;
Steven Moreland9facfce2020-10-02 17:26:56 +0000100 return true;
Andrei Homescu9556bb12020-09-21 16:59:45 -0700101 }
102
103 return {};
104}
105
106bool AIBinder::associateClass(const AIBinder_Class* clazz) {
107 if (clazz == nullptr) return false;
108
Steven Moreland63872b52020-10-02 19:28:23 +0000109 const String16& newDescriptor = clazz->getInterfaceDescriptor();
Andrei Homescu9556bb12020-09-21 16:59:45 -0700110
111 auto result = associateClassInternal(clazz, newDescriptor, false);
112 if (result.has_value()) return *result;
113
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700114 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700115
Steven Moreland63872b52020-10-02 19:28:23 +0000116 const String16& descriptor = getBinder()->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700117 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000118 if (getBinder()->isBinderAlive()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000119 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
120 << "' but descriptor is actually '" << descriptor << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000121 } else {
122 // b/155793159
Steven Moreland63872b52020-10-02 19:28:23 +0000123 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
Steven Moreland640c4092020-07-08 00:19:58 +0000124 << "' to dead binder.";
125 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700126 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700127 }
128
Steven Moreland9facfce2020-10-02 17:26:56 +0000129 return associateClassInternal(clazz, newDescriptor, true).value();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700130}
131
132ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700133 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700134 CHECK(clazz != nullptr);
135}
136ABBinder::~ABBinder() {
137 getClass()->onDestroy(mUserData);
138}
139
140const String16& ABBinder::getInterfaceDescriptor() const {
141 return getClass()->getInterfaceDescriptor();
142}
143
Steven Morelanda194c452019-03-04 16:47:07 -0800144status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
145 AIBinder_onDump onDump = getClass()->onDump;
146
147 if (onDump == nullptr) {
148 return STATUS_OK;
149 }
150
151 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
152 // null in Java
153 if (args.size() > INT32_MAX) {
154 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
155 return STATUS_BAD_VALUE;
156 }
157
158 std::vector<String8> utf8Args; // owns memory of utf8s
159 utf8Args.reserve(args.size());
160 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
161 utf8Pointers.reserve(args.size());
162
163 for (size_t i = 0; i < args.size(); i++) {
164 utf8Args.push_back(String8(args[i]));
165 utf8Pointers.push_back(utf8Args[i].c_str());
166 }
167
168 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
169}
170
Steven Moreland5d62e442018-09-13 15:01:02 -0700171status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
172 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700173 if (isUserCommand(code)) {
174 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700175 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700176 }
177
178 const AParcel in = AParcel::readOnly(this, &data);
179 AParcel out = AParcel(this, reply, false /*owns*/);
180
Steven Moreland5d62e442018-09-13 15:01:02 -0700181 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
182 return PruneStatusT(status);
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800183 } else if (code == SHELL_COMMAND_TRANSACTION) {
184 int in = data.readFileDescriptor();
185 int out = data.readFileDescriptor();
186 int err = data.readFileDescriptor();
187
188 int argc = data.readInt32();
189 std::vector<String8> utf8Args; // owns memory of utf8s
190 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
191 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
192 utf8Args.push_back(String8(data.readString16()));
193 utf8Pointers.push_back(utf8Args[i].c_str());
194 }
195
196 data.readStrongBinder(); // skip over the IShellCallback
197 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
198
199 // Shell commands should only be callable by ADB.
200 uid_t uid = AIBinder_getCallingUid();
201 if (uid != AID_ROOT && uid != AID_SHELL) {
202 if (resultReceiver != nullptr) {
203 resultReceiver->send(-1);
204 }
205 return STATUS_PERMISSION_DENIED;
206 }
207
208 // Check that the file descriptors are valid.
209 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
210 if (resultReceiver != nullptr) {
211 resultReceiver->send(-1);
212 }
213 return STATUS_BAD_VALUE;
214 }
215
216 binder_status_t status = getClass()->handleShellCommand(
217 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
218 if (resultReceiver != nullptr) {
219 resultReceiver->send(status);
220 }
221 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700222 } else {
223 return BBinder::onTransact(code, data, reply, flags);
224 }
225}
226
Steven Moreland71cddc32018-08-30 23:39:22 -0700227ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700228 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700229 CHECK(binder != nullptr);
230}
231ABpBinder::~ABpBinder() {}
232
Steven Moreland94968952018-09-05 14:42:59 -0700233void ABpBinder::onLastStrongRef(const void* id) {
Steven Moreland2505ec42021-06-25 02:08:49 +0000234 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
Steven Moreland91a96b62021-06-30 23:07:13 +0000235 // the ABpBinder to be deleted. Even though we have no more references on the ABpBinder
236 // (BpRefBase), the remote object may still exist (for instance, if we
237 // receive it from another process, before the ABpBinder is attached).
Steven Moreland94968952018-09-05 14:42:59 -0700238
Steven Moreland2505ec42021-06-25 02:08:49 +0000239 ABpBinderTag::Value* value =
Steven Moreland91a96b62021-06-30 23:07:13 +0000240 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
241 CHECK_NE(nullptr, value) << "ABpBinder must always be attached";
242
243 remote()->withLock([&]() { value->binder = nullptr; });
Steven Moreland94968952018-09-05 14:42:59 -0700244
245 BpRefBase::onLastStrongRef(id);
246}
247
248sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700249 if (binder == nullptr) {
250 return nullptr;
251 }
252 if (ABBinderTag::has(binder)) {
253 return static_cast<ABBinder*>(binder.get());
254 }
Steven Moreland94968952018-09-05 14:42:59 -0700255
Steven Moreland91a96b62021-06-30 23:07:13 +0000256 // The following code ensures that for a given binder object (remote or local), if it is not an
257 // ABBinder then at most one ABpBinder object exists in a given process representing it.
258
Steven Moreland1f1bed62021-06-25 21:50:50 +0000259 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700260 if (value == nullptr) {
261 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000262 auto oldValue = static_cast<ABpBinderTag::Value*>(
263 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
264 nullptr /*cookie*/, ABpBinderTag::clean));
265
266 // allocated by another thread
267 if (oldValue) {
268 delete value;
269 value = oldValue;
270 }
Steven Moreland94968952018-09-05 14:42:59 -0700271 }
272
Steven Moreland1f1bed62021-06-25 21:50:50 +0000273 sp<ABpBinder> ret;
274 binder->withLock([&]() {
275 ret = value->binder.promote();
276 if (ret == nullptr) {
277 ret = sp<ABpBinder>::make(binder);
278 value->binder = ret;
279 }
280 });
Steven Moreland94968952018-09-05 14:42:59 -0700281
282 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700283}
284
Steven Moreland2e87adc2018-08-20 19:47:00 -0700285struct AIBinder_Weak {
286 wp<AIBinder> binder;
287};
288AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700289 if (binder == nullptr) {
290 return nullptr;
291 }
292
Steven Moreland2e87adc2018-08-20 19:47:00 -0700293 return new AIBinder_Weak{wp<AIBinder>(binder)};
294}
Steven Moreland9b80e282018-09-19 13:57:23 -0700295void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
296 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700297}
298AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700299 if (weakBinder == nullptr) {
300 return nullptr;
301 }
302
Steven Moreland2e87adc2018-08-20 19:47:00 -0700303 sp<AIBinder> binder = weakBinder->binder.promote();
304 AIBinder_incStrong(binder.get());
305 return binder.get();
306}
307
Steven Morelandc70e0122021-01-08 02:44:53 +0000308AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
309 if (weak == nullptr) {
310 return nullptr;
311 }
312
313 return new AIBinder_Weak{weak->binder};
314}
315
316bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
317 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
318
319 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
320}
321
322bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
323 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
324
325 return lhs->binder < rhs->binder;
326}
327
Steven Moreland2e87adc2018-08-20 19:47:00 -0700328AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
329 AIBinder_Class_onDestroy onDestroy,
330 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700331 : onCreate(onCreate),
332 onDestroy(onDestroy),
333 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800334 mInterfaceDescriptor(interfaceDescriptor),
335 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700336
337AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
338 AIBinder_Class_onCreate onCreate,
339 AIBinder_Class_onDestroy onDestroy,
340 AIBinder_Class_onTransact onTransact) {
341 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
342 onTransact == nullptr) {
343 return nullptr;
344 }
345
346 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
347}
348
Steven Morelanda194c452019-03-04 16:47:07 -0800349void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
350 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
351
352 // this is required to be called before instances are instantiated
353 clazz->onDump = onDump;
354}
355
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800356void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
357 AIBinder_handleShellCommand handleShellCommand) {
358 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
359
360 clazz->handleShellCommand = handleShellCommand;
361}
362
Stephen Crane8fde87f2020-11-17 15:06:11 -0800363const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
364 CHECK(clazz != nullptr) << "getDescriptor requires non-null clazz";
365
366 return clazz->getInterfaceDescriptorUtf8();
367}
368
Steven Moreland901c7452018-09-04 16:17:28 -0700369void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Steven Moreland8bcb5032021-04-07 23:06:43 +0000370 CHECK(who == mWho) << who.unsafe_get() << "(" << who.get_refs() << ") vs " << mWho.unsafe_get()
371 << " (" << mWho.get_refs() << ")";
Steven Moreland901c7452018-09-04 16:17:28 -0700372
373 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700374
375 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
376 sp<IBinder> strongWho = who.promote();
377
378 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
379 if (recipient != nullptr && strongWho != nullptr) {
380 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
381 if (result != ::android::DEAD_OBJECT) {
382 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
383 }
384 }
385
Steven Moreland901c7452018-09-04 16:17:28 -0700386 mWho = nullptr;
387}
388
389AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700390 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700391 CHECK(onDied != nullptr);
392}
393
Steven Moreland64127ca2019-03-13 09:25:44 -0700394void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
395 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
396 [](const sp<TransferDeathRecipient>& tdr) {
397 return tdr->getWho() == nullptr;
398 }),
399 mDeathRecipients.end());
400}
401
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900402binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700403 CHECK(binder != nullptr);
404
405 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
406
407 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700408 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700409
Steven Moreland64127ca2019-03-13 09:25:44 -0700410 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700411 if (status != STATUS_OK) {
412 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700413 }
414
415 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700416
417 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700418 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700419}
420
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900421binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700422 CHECK(binder != nullptr);
423
424 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
425
426 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
427 sp<TransferDeathRecipient> recipient = *it;
428
Steven Moreland64127ca2019-03-13 09:25:44 -0700429 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700430 mDeathRecipients.erase(it.base() - 1);
431
Steven Moreland64127ca2019-03-13 09:25:44 -0700432 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700433 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700434 LOG(ERROR) << __func__
435 << ": removed reference to death recipient but unlink failed.";
436 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700437 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700438 }
439 }
440
Steven Moreland5d62e442018-09-13 15:01:02 -0700441 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700442}
443
444// start of C-API methods
445
Steven Moreland2e87adc2018-08-20 19:47:00 -0700446AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
447 if (clazz == nullptr) {
448 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
449 return nullptr;
450 }
451
452 void* userData = clazz->onCreate(args);
453
Steven Moreland71cddc32018-08-30 23:39:22 -0700454 sp<AIBinder> ret = new ABBinder(clazz, userData);
455 ABBinderTag::attach(ret->getBinder());
456
457 AIBinder_incStrong(ret.get());
458 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700459}
460
Steven Moreland5ea54da2018-09-04 13:29:55 -0700461bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700462 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800463 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700464 }
465
466 return binder->isRemote();
467}
468
Steven Moreland65867d72018-09-04 14:22:26 -0700469bool AIBinder_isAlive(const AIBinder* binder) {
470 if (binder == nullptr) {
471 return false;
472 }
473
474 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
475}
476
477binder_status_t AIBinder_ping(AIBinder* binder) {
478 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700479 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700480 }
481
Steven Moreland5d62e442018-09-13 15:01:02 -0700482 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700483}
484
Steven Morelanda194c452019-03-04 16:47:07 -0800485binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
486 if (binder == nullptr) {
487 return STATUS_UNEXPECTED_NULL;
488 }
489
490 ABBinder* bBinder = binder->asABBinder();
491 if (bBinder != nullptr) {
492 AIBinder_onDump onDump = binder->getClass()->onDump;
493 if (onDump == nullptr) {
494 return STATUS_OK;
495 }
496 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
497 }
498
499 ::android::Vector<String16> utf16Args;
500 utf16Args.setCapacity(numArgs);
501 for (uint32_t i = 0; i < numArgs; i++) {
502 utf16Args.push(String16(String8(args[i])));
503 }
504
505 status_t status = binder->getBinder()->dump(fd, utf16Args);
506 return PruneStatusT(status);
507}
508
Steven Moreland901c7452018-09-04 16:17:28 -0700509binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
510 void* cookie) {
511 if (binder == nullptr || recipient == nullptr) {
512 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700513 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700514 }
515
Steven Moreland5d62e442018-09-13 15:01:02 -0700516 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700517 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700518}
519
520binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
521 void* cookie) {
522 if (binder == nullptr || recipient == nullptr) {
523 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700524 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700525 }
526
Steven Moreland5d62e442018-09-13 15:01:02 -0700527 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700528 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700529}
530
Steven Morelandf3034b02018-11-12 17:37:46 -0800531uid_t AIBinder_getCallingUid() {
532 return ::android::IPCThreadState::self()->getCallingUid();
533}
534
535pid_t AIBinder_getCallingPid() {
536 return ::android::IPCThreadState::self()->getCallingPid();
537}
538
Steven Moreland2e87adc2018-08-20 19:47:00 -0700539void AIBinder_incStrong(AIBinder* binder) {
540 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700541 return;
542 }
543
544 binder->incStrong(nullptr);
545}
546void AIBinder_decStrong(AIBinder* binder) {
547 if (binder == nullptr) {
548 LOG(ERROR) << __func__ << ": on null binder";
549 return;
550 }
551
552 binder->decStrong(nullptr);
553}
554int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
555 if (binder == nullptr) {
556 LOG(ERROR) << __func__ << ": on null binder";
557 return -1;
558 }
559
560 return binder->getStrongCount();
561}
562
Steven Moreland71cddc32018-08-30 23:39:22 -0700563bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
564 if (binder == nullptr) {
565 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700566 }
567
Steven Moreland71cddc32018-08-30 23:39:22 -0700568 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700569}
570
571const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
572 if (binder == nullptr) {
573 return nullptr;
574 }
575
576 return binder->getClass();
577}
578
579void* AIBinder_getUserData(AIBinder* binder) {
580 if (binder == nullptr) {
581 return nullptr;
582 }
583
584 ABBinder* bBinder = binder->asABBinder();
585 if (bBinder == nullptr) {
586 return nullptr;
587 }
588
589 return bBinder->getUserData();
590}
591
592binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
593 if (binder == nullptr || in == nullptr) {
594 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700595 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700596 }
597 const AIBinder_Class* clazz = binder->getClass();
598 if (clazz == nullptr) {
599 LOG(ERROR) << __func__
600 << ": Class must be defined for a remote binder transaction. See "
601 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700602 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700603 }
604
605 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000606 (*in)->get()->markForBinder(binder->getBinder());
607
Steven Morelandf18615b2018-09-14 11:43:06 -0700608 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700609 binder_status_t ret = PruneStatusT(status);
610
611 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700612 delete *in;
613 *in = nullptr;
614 }
615
Steven Moreland5d62e442018-09-13 15:01:02 -0700616 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700617}
618
Steven Moreland9b80e282018-09-19 13:57:23 -0700619static void DestroyParcel(AParcel** parcel) {
620 delete *parcel;
621 *parcel = nullptr;
622}
623
Steven Moreland2e87adc2018-08-20 19:47:00 -0700624binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
625 AParcel** out, binder_flags_t flags) {
626 if (in == nullptr) {
627 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700628 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700629 }
630
Steven Morelandcaa776c2018-09-04 13:48:11 -0700631 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700632 // This object is the input to the transaction. This function takes ownership of it and deletes
633 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700634 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700635
636 if (!isUserCommand(code)) {
637 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700638 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700639 }
640
Steven Morelandf183fdd2020-10-27 00:12:12 +0000641 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700642 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700643 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700644 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700645 }
646
647 if (binder == nullptr || *in == nullptr || out == nullptr) {
648 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700649 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700650 }
651
652 if ((*in)->getBinder() != binder) {
653 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
654 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700655 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700656 }
657
658 *out = new AParcel(binder);
659
Steven Morelandf18615b2018-09-14 11:43:06 -0700660 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700661 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700662
Steven Moreland5d62e442018-09-13 15:01:02 -0700663 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700664 delete *out;
665 *out = nullptr;
666 }
667
Steven Moreland5d62e442018-09-13 15:01:02 -0700668 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700669}
Steven Moreland901c7452018-09-04 16:17:28 -0700670
671AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
672 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
673 if (onBinderDied == nullptr) {
674 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
675 return nullptr;
676 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700677 auto ret = new AIBinder_DeathRecipient(onBinderDied);
678 ret->incStrong(nullptr);
679 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700680}
681
Steven Moreland9b80e282018-09-19 13:57:23 -0700682void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700683 if (recipient == nullptr) {
684 return;
685 }
686
687 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700688}
Steven Morelandea14ef22019-08-20 10:50:08 -0700689
690binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
691 if (binder == nullptr || outExt == nullptr) {
692 if (outExt != nullptr) {
693 *outExt = nullptr;
694 }
695 return STATUS_UNEXPECTED_NULL;
696 }
697
698 sp<IBinder> ext;
699 status_t res = binder->getBinder()->getExtension(&ext);
700
701 if (res != android::OK) {
702 *outExt = nullptr;
703 return PruneStatusT(res);
704 }
705
706 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
707 if (ret != nullptr) ret->incStrong(binder);
708
709 *outExt = ret.get();
710 return STATUS_OK;
711}
712
713binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
714 if (binder == nullptr || ext == nullptr) {
715 return STATUS_UNEXPECTED_NULL;
716 }
717
718 ABBinder* rawBinder = binder->asABBinder();
719 if (rawBinder == nullptr) {
720 return STATUS_INVALID_OPERATION;
721 }
722
723 rawBinder->setExtension(ext->getBinder());
724 return STATUS_OK;
725}
Steven Moreland2f405f52020-07-08 22:24:29 +0000726
727// platform methods follow
728
729void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
730 ABBinder* localBinder = binder->asABBinder();
731 if (localBinder == nullptr) {
732 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
733 }
734
735 localBinder->setRequestingSid(requestingSid);
736}
737
738const char* AIBinder_getCallingSid() {
739 return ::android::IPCThreadState::self()->getCallingSid();
740}
Steven Morelandb2de9532020-05-29 21:44:41 +0000741
742android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
743 if (binder == nullptr) return nullptr;
744 return binder->getBinder();
745}
746
747AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
748 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
749 AIBinder_incStrong(ndkBinder.get());
750 return ndkBinder.get();
751}