blob: 28e3ff43ff87ab659812e98bfe53cdb15a95acf8 [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 Moreland9234c432021-06-29 23:01:10 +000050 // can only attach once
51 CHECK_EQ(nullptr, binder->attachObject(kId, kValue, nullptr /*cookie*/, clean));
Steven Moreland71cddc32018-08-30 23:39:22 -070052}
53static bool has(const sp<IBinder>& binder) {
54 return binder != nullptr && binder->findObject(kId) == kValue;
55}
56
Steven Moreland6cf66ac2018-11-02 18:14:54 -070057} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070058
Steven Moreland94968952018-09-05 14:42:59 -070059namespace ABpBinderTag {
60
Steven Moreland94968952018-09-05 14:42:59 -070061static 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,
Steven Moreland63872b52020-10-02 19:28:23 +000077 const String16& newDescriptor, bool set) {
Andrei Homescu9556bb12020-09-21 16:59:45 -070078 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) {
Steven Moreland63872b52020-10-02 19:28:23 +000082 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -070083 if (newDescriptor == currentDescriptor) {
84 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
Steven Moreland4d196972021-08-12 15:49:44 -070085 << "' match during associateClass, but they are different class objects ("
86 << clazz << " vs " << mClazz << "). 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 '"
Steven Moreland63872b52020-10-02 19:28:23 +000091 << newDescriptor << "' but already set to '" << currentDescriptor << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 }
93
Steven Moreland71cddc32018-08-30 23:39:22 -070094 // always a failure because we know mClazz != clazz
95 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 }
97
Andrei Homescu9556bb12020-09-21 16:59:45 -070098 if (set) {
99 // if this is a local object, it's not one known to libbinder_ndk
100 mClazz = clazz;
Steven Moreland9facfce2020-10-02 17:26:56 +0000101 return true;
Andrei Homescu9556bb12020-09-21 16:59:45 -0700102 }
103
104 return {};
105}
106
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900107// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
108static std::string SanitizeString(const String16& str) {
109 std::string sanitized{String8(str)};
110 for (auto& c : sanitized) {
111 if (!isprint(c)) {
112 c = '?';
113 }
114 }
115 return sanitized;
116}
117
Andrei Homescu9556bb12020-09-21 16:59:45 -0700118bool AIBinder::associateClass(const AIBinder_Class* clazz) {
119 if (clazz == nullptr) return false;
120
Steven Moreland63872b52020-10-02 19:28:23 +0000121 const String16& newDescriptor = clazz->getInterfaceDescriptor();
Andrei Homescu9556bb12020-09-21 16:59:45 -0700122
123 auto result = associateClassInternal(clazz, newDescriptor, false);
124 if (result.has_value()) return *result;
125
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700126 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700127
Steven Moreland63872b52020-10-02 19:28:23 +0000128 const String16& descriptor = getBinder()->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700129 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000130 if (getBinder()->isBinderAlive()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000131 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
Jooyung Han1f9dcd32021-11-03 13:34:29 +0900132 << "' but descriptor is actually '" << SanitizeString(descriptor) << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000133 } else {
134 // b/155793159
Steven Moreland63872b52020-10-02 19:28:23 +0000135 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
Steven Moreland640c4092020-07-08 00:19:58 +0000136 << "' to dead binder.";
137 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700138 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700139 }
140
Steven Moreland9facfce2020-10-02 17:26:56 +0000141 return associateClassInternal(clazz, newDescriptor, true).value();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700142}
143
144ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700145 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700146 CHECK(clazz != nullptr);
147}
148ABBinder::~ABBinder() {
149 getClass()->onDestroy(mUserData);
150}
151
152const String16& ABBinder::getInterfaceDescriptor() const {
153 return getClass()->getInterfaceDescriptor();
154}
155
Steven Morelanda194c452019-03-04 16:47:07 -0800156status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
157 AIBinder_onDump onDump = getClass()->onDump;
158
159 if (onDump == nullptr) {
160 return STATUS_OK;
161 }
162
163 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
164 // null in Java
165 if (args.size() > INT32_MAX) {
166 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
167 return STATUS_BAD_VALUE;
168 }
169
170 std::vector<String8> utf8Args; // owns memory of utf8s
171 utf8Args.reserve(args.size());
172 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
173 utf8Pointers.reserve(args.size());
174
175 for (size_t i = 0; i < args.size(); i++) {
176 utf8Args.push_back(String8(args[i]));
177 utf8Pointers.push_back(utf8Args[i].c_str());
178 }
179
180 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
181}
182
Steven Moreland5d62e442018-09-13 15:01:02 -0700183status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
184 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700185 if (isUserCommand(code)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700186 if (getClass()->writeHeader && !data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700187 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700188 }
189
190 const AParcel in = AParcel::readOnly(this, &data);
191 AParcel out = AParcel(this, reply, false /*owns*/);
192
Steven Moreland5d62e442018-09-13 15:01:02 -0700193 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
194 return PruneStatusT(status);
Steven Moreland88234072020-08-06 19:32:45 +0000195 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800196 int in = data.readFileDescriptor();
197 int out = data.readFileDescriptor();
198 int err = data.readFileDescriptor();
199
200 int argc = data.readInt32();
201 std::vector<String8> utf8Args; // owns memory of utf8s
202 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
203 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
204 utf8Args.push_back(String8(data.readString16()));
205 utf8Pointers.push_back(utf8Args[i].c_str());
206 }
207
208 data.readStrongBinder(); // skip over the IShellCallback
209 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
210
211 // Shell commands should only be callable by ADB.
212 uid_t uid = AIBinder_getCallingUid();
213 if (uid != AID_ROOT && uid != AID_SHELL) {
214 if (resultReceiver != nullptr) {
215 resultReceiver->send(-1);
216 }
217 return STATUS_PERMISSION_DENIED;
218 }
219
220 // Check that the file descriptors are valid.
221 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
222 if (resultReceiver != nullptr) {
223 resultReceiver->send(-1);
224 }
225 return STATUS_BAD_VALUE;
226 }
227
228 binder_status_t status = getClass()->handleShellCommand(
229 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
230 if (resultReceiver != nullptr) {
231 resultReceiver->send(status);
232 }
233 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700234 } else {
235 return BBinder::onTransact(code, data, reply, flags);
236 }
237}
238
Steven Moreland71cddc32018-08-30 23:39:22 -0700239ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700240 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700241 CHECK(binder != nullptr);
242}
243ABpBinder::~ABpBinder() {}
244
Steven Moreland94968952018-09-05 14:42:59 -0700245void ABpBinder::onLastStrongRef(const void* id) {
Steven Moreland2505ec42021-06-25 02:08:49 +0000246 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
Steven Moreland91a96b62021-06-30 23:07:13 +0000247 // the ABpBinder to be deleted. Even though we have no more references on the ABpBinder
248 // (BpRefBase), the remote object may still exist (for instance, if we
249 // receive it from another process, before the ABpBinder is attached).
Steven Moreland94968952018-09-05 14:42:59 -0700250
Steven Moreland2505ec42021-06-25 02:08:49 +0000251 ABpBinderTag::Value* value =
Steven Moreland91a96b62021-06-30 23:07:13 +0000252 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
253 CHECK_NE(nullptr, value) << "ABpBinder must always be attached";
254
255 remote()->withLock([&]() { value->binder = nullptr; });
Steven Moreland94968952018-09-05 14:42:59 -0700256
257 BpRefBase::onLastStrongRef(id);
258}
259
260sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700261 if (binder == nullptr) {
262 return nullptr;
263 }
264 if (ABBinderTag::has(binder)) {
265 return static_cast<ABBinder*>(binder.get());
266 }
Steven Moreland94968952018-09-05 14:42:59 -0700267
Steven Moreland91a96b62021-06-30 23:07:13 +0000268 // The following code ensures that for a given binder object (remote or local), if it is not an
269 // ABBinder then at most one ABpBinder object exists in a given process representing it.
270
Steven Moreland1f1bed62021-06-25 21:50:50 +0000271 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700272 if (value == nullptr) {
273 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000274 auto oldValue = static_cast<ABpBinderTag::Value*>(
275 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
276 nullptr /*cookie*/, ABpBinderTag::clean));
277
278 // allocated by another thread
279 if (oldValue) {
280 delete value;
281 value = oldValue;
282 }
Steven Moreland94968952018-09-05 14:42:59 -0700283 }
284
Steven Moreland1f1bed62021-06-25 21:50:50 +0000285 sp<ABpBinder> ret;
286 binder->withLock([&]() {
287 ret = value->binder.promote();
288 if (ret == nullptr) {
289 ret = sp<ABpBinder>::make(binder);
290 value->binder = ret;
291 }
292 });
Steven Moreland94968952018-09-05 14:42:59 -0700293
294 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700295}
296
Steven Moreland2e87adc2018-08-20 19:47:00 -0700297struct AIBinder_Weak {
298 wp<AIBinder> binder;
299};
300AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700301 if (binder == nullptr) {
302 return nullptr;
303 }
304
Steven Moreland2e87adc2018-08-20 19:47:00 -0700305 return new AIBinder_Weak{wp<AIBinder>(binder)};
306}
Steven Moreland9b80e282018-09-19 13:57:23 -0700307void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
308 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700309}
310AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700311 if (weakBinder == nullptr) {
312 return nullptr;
313 }
314
Steven Moreland2e87adc2018-08-20 19:47:00 -0700315 sp<AIBinder> binder = weakBinder->binder.promote();
316 AIBinder_incStrong(binder.get());
317 return binder.get();
318}
319
Steven Morelandc70e0122021-01-08 02:44:53 +0000320AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
321 if (weak == nullptr) {
322 return nullptr;
323 }
324
325 return new AIBinder_Weak{weak->binder};
326}
327
328bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
329 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
330
331 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
332}
333
334bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
335 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
336
337 return lhs->binder < rhs->binder;
338}
339
Steven Moreland2e87adc2018-08-20 19:47:00 -0700340AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
341 AIBinder_Class_onDestroy onDestroy,
342 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700343 : onCreate(onCreate),
344 onDestroy(onDestroy),
345 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800346 mInterfaceDescriptor(interfaceDescriptor),
347 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700348
349AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
350 AIBinder_Class_onCreate onCreate,
351 AIBinder_Class_onDestroy onDestroy,
352 AIBinder_Class_onTransact onTransact) {
353 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
354 onTransact == nullptr) {
355 return nullptr;
356 }
357
358 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
359}
360
Steven Morelanda194c452019-03-04 16:47:07 -0800361void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
362 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
363
364 // this is required to be called before instances are instantiated
365 clazz->onDump = onDump;
366}
367
Steven Morelandf3ed8062021-08-27 14:31:14 -0700368void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
369 CHECK(clazz != nullptr) << "disableInterfaceTokenHeader requires non-null clazz";
370
371 clazz->writeHeader = false;
372}
373
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800374void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
375 AIBinder_handleShellCommand handleShellCommand) {
376 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
377
378 clazz->handleShellCommand = handleShellCommand;
379}
380
Stephen Crane8fde87f2020-11-17 15:06:11 -0800381const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
382 CHECK(clazz != nullptr) << "getDescriptor requires non-null clazz";
383
384 return clazz->getInterfaceDescriptorUtf8();
385}
386
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000387AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() {
388 if (mOnUnlinked != nullptr) {
389 mOnUnlinked(mCookie);
390 }
391}
392
Steven Moreland901c7452018-09-04 16:17:28 -0700393void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Steven Moreland8bcb5032021-04-07 23:06:43 +0000394 CHECK(who == mWho) << who.unsafe_get() << "(" << who.get_refs() << ") vs " << mWho.unsafe_get()
395 << " (" << mWho.get_refs() << ")";
Steven Moreland901c7452018-09-04 16:17:28 -0700396
397 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700398
399 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
400 sp<IBinder> strongWho = who.promote();
401
402 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
403 if (recipient != nullptr && strongWho != nullptr) {
404 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
405 if (result != ::android::DEAD_OBJECT) {
406 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
407 }
408 }
409
Steven Moreland901c7452018-09-04 16:17:28 -0700410 mWho = nullptr;
411}
412
413AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000414 : mOnDied(onDied), mOnUnlinked(nullptr) {
Steven Moreland901c7452018-09-04 16:17:28 -0700415 CHECK(onDied != nullptr);
416}
417
Steven Moreland64127ca2019-03-13 09:25:44 -0700418void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
419 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
420 [](const sp<TransferDeathRecipient>& tdr) {
421 return tdr->getWho() == nullptr;
422 }),
423 mDeathRecipients.end());
424}
425
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900426binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700427 CHECK(binder != nullptr);
428
429 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
430
431 sp<TransferDeathRecipient> recipient =
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000432 new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked);
Steven Moreland901c7452018-09-04 16:17:28 -0700433
Steven Moreland64127ca2019-03-13 09:25:44 -0700434 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700435 if (status != STATUS_OK) {
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000436 // When we failed to link, the destructor of TransferDeathRecipient runs here, which
437 // ensures that mOnUnlinked is called before we return with an error from this method.
Steven Moreland5d62e442018-09-13 15:01:02 -0700438 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700439 }
440
441 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700442
443 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700444 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700445}
446
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900447binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700448 CHECK(binder != nullptr);
449
450 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
451
452 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
453 sp<TransferDeathRecipient> recipient = *it;
454
Steven Moreland64127ca2019-03-13 09:25:44 -0700455 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700456 mDeathRecipients.erase(it.base() - 1);
457
Steven Moreland64127ca2019-03-13 09:25:44 -0700458 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700459 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700460 LOG(ERROR) << __func__
461 << ": removed reference to death recipient but unlink failed.";
462 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700463 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700464 }
465 }
466
Steven Moreland5d62e442018-09-13 15:01:02 -0700467 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700468}
469
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000470void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
471 mOnUnlinked = onUnlinked;
472}
473
Steven Moreland901c7452018-09-04 16:17:28 -0700474// start of C-API methods
475
Steven Moreland2e87adc2018-08-20 19:47:00 -0700476AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
477 if (clazz == nullptr) {
478 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
479 return nullptr;
480 }
481
482 void* userData = clazz->onCreate(args);
483
Steven Moreland71cddc32018-08-30 23:39:22 -0700484 sp<AIBinder> ret = new ABBinder(clazz, userData);
485 ABBinderTag::attach(ret->getBinder());
486
487 AIBinder_incStrong(ret.get());
488 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700489}
490
Steven Moreland5ea54da2018-09-04 13:29:55 -0700491bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700492 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800493 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700494 }
495
496 return binder->isRemote();
497}
498
Steven Moreland65867d72018-09-04 14:22:26 -0700499bool AIBinder_isAlive(const AIBinder* binder) {
500 if (binder == nullptr) {
501 return false;
502 }
503
504 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
505}
506
507binder_status_t AIBinder_ping(AIBinder* binder) {
508 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700509 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700510 }
511
Steven Moreland5d62e442018-09-13 15:01:02 -0700512 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700513}
514
Steven Morelanda194c452019-03-04 16:47:07 -0800515binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
516 if (binder == nullptr) {
517 return STATUS_UNEXPECTED_NULL;
518 }
519
520 ABBinder* bBinder = binder->asABBinder();
521 if (bBinder != nullptr) {
522 AIBinder_onDump onDump = binder->getClass()->onDump;
523 if (onDump == nullptr) {
524 return STATUS_OK;
525 }
526 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
527 }
528
529 ::android::Vector<String16> utf16Args;
530 utf16Args.setCapacity(numArgs);
531 for (uint32_t i = 0; i < numArgs; i++) {
532 utf16Args.push(String16(String8(args[i])));
533 }
534
535 status_t status = binder->getBinder()->dump(fd, utf16Args);
536 return PruneStatusT(status);
537}
538
Steven Moreland901c7452018-09-04 16:17:28 -0700539binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
540 void* cookie) {
541 if (binder == nullptr || recipient == nullptr) {
542 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700543 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700544 }
545
Steven Moreland5d62e442018-09-13 15:01:02 -0700546 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700547 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700548}
549
550binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
551 void* cookie) {
552 if (binder == nullptr || recipient == nullptr) {
553 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700554 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700555 }
556
Steven Moreland5d62e442018-09-13 15:01:02 -0700557 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700558 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700559}
560
Steven Morelandf3034b02018-11-12 17:37:46 -0800561uid_t AIBinder_getCallingUid() {
562 return ::android::IPCThreadState::self()->getCallingUid();
563}
564
565pid_t AIBinder_getCallingPid() {
566 return ::android::IPCThreadState::self()->getCallingPid();
567}
568
Alice Ryhl59aeae82021-08-25 10:21:26 +0000569bool AIBinder_isHandlingTransaction() {
570 return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr;
571}
572
Steven Moreland2e87adc2018-08-20 19:47:00 -0700573void AIBinder_incStrong(AIBinder* binder) {
574 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700575 return;
576 }
577
578 binder->incStrong(nullptr);
579}
580void AIBinder_decStrong(AIBinder* binder) {
581 if (binder == nullptr) {
582 LOG(ERROR) << __func__ << ": on null binder";
583 return;
584 }
585
586 binder->decStrong(nullptr);
587}
588int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
589 if (binder == nullptr) {
590 LOG(ERROR) << __func__ << ": on null binder";
591 return -1;
592 }
593
594 return binder->getStrongCount();
595}
596
Steven Moreland71cddc32018-08-30 23:39:22 -0700597bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
598 if (binder == nullptr) {
599 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700600 }
601
Steven Moreland71cddc32018-08-30 23:39:22 -0700602 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700603}
604
605const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
606 if (binder == nullptr) {
607 return nullptr;
608 }
609
610 return binder->getClass();
611}
612
613void* AIBinder_getUserData(AIBinder* binder) {
614 if (binder == nullptr) {
615 return nullptr;
616 }
617
618 ABBinder* bBinder = binder->asABBinder();
619 if (bBinder == nullptr) {
620 return nullptr;
621 }
622
623 return bBinder->getUserData();
624}
625
626binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
627 if (binder == nullptr || in == nullptr) {
628 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700629 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700630 }
631 const AIBinder_Class* clazz = binder->getClass();
632 if (clazz == nullptr) {
633 LOG(ERROR) << __func__
634 << ": Class must be defined for a remote binder transaction. See "
635 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700636 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700637 }
638
639 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000640 (*in)->get()->markForBinder(binder->getBinder());
641
Steven Morelandf3ed8062021-08-27 14:31:14 -0700642 status_t status = android::OK;
643 if (clazz->writeHeader) {
644 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
645 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700646 binder_status_t ret = PruneStatusT(status);
647
648 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700649 delete *in;
650 *in = nullptr;
651 }
652
Steven Moreland5d62e442018-09-13 15:01:02 -0700653 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700654}
655
Steven Moreland9b80e282018-09-19 13:57:23 -0700656static void DestroyParcel(AParcel** parcel) {
657 delete *parcel;
658 *parcel = nullptr;
659}
660
Steven Moreland2e87adc2018-08-20 19:47:00 -0700661binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
662 AParcel** out, binder_flags_t flags) {
663 if (in == nullptr) {
664 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700665 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700666 }
667
Steven Morelandcaa776c2018-09-04 13:48:11 -0700668 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700669 // This object is the input to the transaction. This function takes ownership of it and deletes
670 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700671 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700672
673 if (!isUserCommand(code)) {
674 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700675 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700676 }
677
Steven Morelandf183fdd2020-10-27 00:12:12 +0000678 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700679 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700680 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700681 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700682 }
683
684 if (binder == nullptr || *in == nullptr || out == nullptr) {
685 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700686 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700687 }
688
689 if ((*in)->getBinder() != binder) {
690 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
691 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700692 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700693 }
694
695 *out = new AParcel(binder);
696
Steven Morelandf18615b2018-09-14 11:43:06 -0700697 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700698 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700699
Steven Moreland5d62e442018-09-13 15:01:02 -0700700 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700701 delete *out;
702 *out = nullptr;
703 }
704
Steven Moreland5d62e442018-09-13 15:01:02 -0700705 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700706}
Steven Moreland901c7452018-09-04 16:17:28 -0700707
708AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
709 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
710 if (onBinderDied == nullptr) {
711 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
712 return nullptr;
713 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700714 auto ret = new AIBinder_DeathRecipient(onBinderDied);
715 ret->incStrong(nullptr);
716 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700717}
718
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000719void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient,
720 AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) {
721 if (recipient == nullptr) {
722 return;
723 }
724
725 recipient->setOnUnlinked(onUnlinked);
726}
727
Steven Moreland9b80e282018-09-19 13:57:23 -0700728void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700729 if (recipient == nullptr) {
730 return;
731 }
732
733 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700734}
Steven Morelandea14ef22019-08-20 10:50:08 -0700735
736binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
737 if (binder == nullptr || outExt == nullptr) {
738 if (outExt != nullptr) {
739 *outExt = nullptr;
740 }
741 return STATUS_UNEXPECTED_NULL;
742 }
743
744 sp<IBinder> ext;
745 status_t res = binder->getBinder()->getExtension(&ext);
746
747 if (res != android::OK) {
748 *outExt = nullptr;
749 return PruneStatusT(res);
750 }
751
752 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
753 if (ret != nullptr) ret->incStrong(binder);
754
755 *outExt = ret.get();
756 return STATUS_OK;
757}
758
759binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
760 if (binder == nullptr || ext == nullptr) {
761 return STATUS_UNEXPECTED_NULL;
762 }
763
764 ABBinder* rawBinder = binder->asABBinder();
765 if (rawBinder == nullptr) {
766 return STATUS_INVALID_OPERATION;
767 }
768
769 rawBinder->setExtension(ext->getBinder());
770 return STATUS_OK;
771}
Steven Moreland2f405f52020-07-08 22:24:29 +0000772
773// platform methods follow
774
775void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
776 ABBinder* localBinder = binder->asABBinder();
777 if (localBinder == nullptr) {
778 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
779 }
780
781 localBinder->setRequestingSid(requestingSid);
782}
783
784const char* AIBinder_getCallingSid() {
785 return ::android::IPCThreadState::self()->getCallingSid();
786}
Steven Morelandb2de9532020-05-29 21:44:41 +0000787
788android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
789 if (binder == nullptr) return nullptr;
790 return binder->getBinder();
791}
792
793AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
794 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
795 AIBinder_incStrong(ndkBinder.get());
796 return ndkBinder.get();
797}
Ady Abraham96174ac2021-10-15 11:02:03 -0700798
799void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) {
800 binder->asABBinder()->setMinSchedulerPolicy(policy, priority);
801}
Steven Moreland454a26a2021-12-14 01:26:21 +0000802
803void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) {
804 ABBinder* localBinder = binder->asABBinder();
805 if (localBinder == nullptr) {
806 LOG(FATAL) << "AIBinder_setInheritRt must be called on a local binder";
807 }
808
809 localBinder->setInheritRt(inheritRt);
810}